Subversion Repositories Kolibri OS

Rev

Rev 6104 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5078 serge 1
#include 
2
#include 
3
#include 
4
#include 
5
#include 
6
 
7
int x86_clflush_size;
8
unsigned int tsc_khz;
9
 
10
struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
11
{
12
    struct file *filep;
13
    int count;
14
 
15
    filep = malloc(sizeof(*filep));
16
 
17
    if(unlikely(filep == NULL))
18
        return ERR_PTR(-ENOMEM);
19
 
20
    count = size / PAGE_SIZE;
21
 
22
    filep->pages = kzalloc(sizeof(struct page *) * count, 0);
23
    if(unlikely(filep->pages == NULL))
24
    {
25
        kfree(filep);
26
        return ERR_PTR(-ENOMEM);
27
    };
28
 
29
    filep->count     = count;
30
    filep->allocated = 0;
31
    filep->vma       = NULL;
32
 
33
//    printf("%s file %p pages %p count %d\n",
34
//              __FUNCTION__,filep, filep->pages, count);
35
 
36
    return filep;
37
}
38
 
39
static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
40
{
41
        while (bytes) {
42
                if (*start != value)
43
                        return (void *)start;
44
                start++;
45
                bytes--;
46
        }
47
        return NULL;
48
}
49
 
50
/**
51
 * memchr_inv - Find an unmatching character in an area of memory.
52
 * @start: The memory area
53
 * @c: Find a character other than c
54
 * @bytes: The size of the area.
55
 *
56
 * returns the address of the first character other than @c, or %NULL
57
 * if the whole buffer contains just @c.
58
 */
59
void *memchr_inv(const void *start, int c, size_t bytes)
60
{
61
        u8 value = c;
62
        u64 value64;
63
        unsigned int words, prefix;
64
 
65
        if (bytes <= 16)
66
                return check_bytes8(start, value, bytes);
67
 
68
        value64 = value;
69
#if defined(ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
70
        value64 *= 0x0101010101010101;
71
#elif defined(ARCH_HAS_FAST_MULTIPLIER)
72
        value64 *= 0x01010101;
73
        value64 |= value64 << 32;
74
#else
75
        value64 |= value64 << 8;
76
        value64 |= value64 << 16;
77
        value64 |= value64 << 32;
78
#endif
79
 
80
        prefix = (unsigned long)start % 8;
81
        if (prefix) {
82
                u8 *r;
83
 
84
                prefix = 8 - prefix;
85
                r = check_bytes8(start, value, prefix);
86
                if (r)
87
                        return r;
88
                start += prefix;
89
                bytes -= prefix;
90
        }
91
 
92
        words = bytes / 8;
93
 
94
        while (words) {
95
                if (*(u64 *)start != value64)
96
                        return check_bytes8(start, value, 8);
97
                start += 8;
98
                words--;
99
        }
100
 
101
        return check_bytes8(start, value, bytes % 8);
102
}
103
 
104
 
105
 
106
#define _U  0x01    /* upper */
107
#define _L  0x02    /* lower */
108
#define _D  0x04    /* digit */
109
#define _C  0x08    /* cntrl */
110
#define _P  0x10    /* punct */
111
#define _S  0x20    /* white space (space/lf/tab) */
112
#define _X  0x40    /* hex digit */
113
#define _SP 0x80    /* hard space (0x20) */
114
 
115
extern const unsigned char _ctype[];
116
 
117
#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
118
 
119
#define isalnum(c)  ((__ismask(c)&(_U|_L|_D)) != 0)
120
#define isalpha(c)  ((__ismask(c)&(_U|_L)) != 0)
121
#define iscntrl(c)  ((__ismask(c)&(_C)) != 0)
122
#define isdigit(c)  ((__ismask(c)&(_D)) != 0)
123
#define isgraph(c)  ((__ismask(c)&(_P|_U|_L|_D)) != 0)
124
#define islower(c)  ((__ismask(c)&(_L)) != 0)
125
#define isprint(c)  ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
126
#define ispunct(c)  ((__ismask(c)&(_P)) != 0)
127
/* Note: isspace() must return false for %NUL-terminator */
128
#define isspace(c)  ((__ismask(c)&(_S)) != 0)
129
#define isupper(c)  ((__ismask(c)&(_U)) != 0)
130
#define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
131
 
132
#define isascii(c) (((unsigned char)(c))<=0x7f)
133
#define toascii(c) (((unsigned char)(c))&0x7f)
134
 
135
static inline unsigned char __tolower(unsigned char c)
136
{
137
    if (isupper(c))
138
        c -= 'A'-'a';
139
    return c;
140
}
141
 
142
static inline unsigned char __toupper(unsigned char c)
143
{
144
    if (islower(c))
145
        c -= 'a'-'A';
146
    return c;
147
}
148
 
149
#define tolower(c) __tolower(c)
150
#define toupper(c) __toupper(c)
151
 
152
/*
153
 * Fast implementation of tolower() for internal usage. Do not use in your
154
 * code.
155
 */
156
static inline char _tolower(const char c)
157
{
158
    return c | 0x20;
159
}
160
 
161
 
162
 
163
//const char hex_asc[] = "0123456789abcdef";
164
 
165
/**
166
 * hex_to_bin - convert a hex digit to its real value
167
 * @ch: ascii character represents hex digit
168
 *
169
 * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
170
 * input.
171
 */
172
int hex_to_bin(char ch)
173
{
174
    if ((ch >= '0') && (ch <= '9'))
175
        return ch - '0';
176
    ch = tolower(ch);
177
    if ((ch >= 'a') && (ch <= 'f'))
178
        return ch - 'a' + 10;
179
    return -1;
180
}
181
EXPORT_SYMBOL(hex_to_bin);
182
 
183
/**
184
 * hex2bin - convert an ascii hexadecimal string to its binary representation
185
 * @dst: binary result
186
 * @src: ascii hexadecimal string
187
 * @count: result length
188
 *
189
 * Return 0 on success, -1 in case of bad input.
190
 */
191
int hex2bin(u8 *dst, const char *src, size_t count)
192
{
193
    while (count--) {
194
        int hi = hex_to_bin(*src++);
195
        int lo = hex_to_bin(*src++);
196
 
197
        if ((hi < 0) || (lo < 0))
198
            return -1;
199
 
200
        *dst++ = (hi << 4) | lo;
201
    }
202
    return 0;
203
}
204
EXPORT_SYMBOL(hex2bin);
205
 
206
/**
207
 * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
208
 * @buf: data blob to dump
209
 * @len: number of bytes in the @buf
210
 * @rowsize: number of bytes to print per line; must be 16 or 32
211
 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
212
 * @linebuf: where to put the converted data
213
 * @linebuflen: total size of @linebuf, including space for terminating NUL
214
 * @ascii: include ASCII after the hex output
215
 *
216
 * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
217
 * 16 or 32 bytes of input data converted to hex + ASCII output.
218
 *
219
 * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
220
 * to a hex + ASCII dump at the supplied memory location.
221
 * The converted output is always NUL-terminated.
222
 *
223
 * E.g.:
224
 *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
225
 *          linebuf, sizeof(linebuf), true);
226
 *
227
 * example output buffer:
228
 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
229
 */
230
void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
231
            int groupsize, char *linebuf, size_t linebuflen,
232
            bool ascii)
233
{
234
    const u8 *ptr = buf;
235
    u8 ch;
236
    int j, lx = 0;
237
    int ascii_column;
238
 
239
    if (rowsize != 16 && rowsize != 32)
240
        rowsize = 16;
241
 
242
    if (!len)
243
        goto nil;
244
    if (len > rowsize)      /* limit to one line at a time */
245
        len = rowsize;
246
    if ((len % groupsize) != 0) /* no mixed size output */
247
        groupsize = 1;
248
 
249
    switch (groupsize) {
250
    case 8: {
251
        const u64 *ptr8 = buf;
252
        int ngroups = len / groupsize;
253
 
254
        for (j = 0; j < ngroups; j++)
255
            lx += scnprintf(linebuf + lx, linebuflen - lx,
256
                    "%s%16.16llx", j ? " " : "",
257
                    (unsigned long long)*(ptr8 + j));
258
        ascii_column = 17 * ngroups + 2;
259
        break;
260
    }
261
 
262
    case 4: {
263
        const u32 *ptr4 = buf;
264
        int ngroups = len / groupsize;
265
 
266
        for (j = 0; j < ngroups; j++)
267
            lx += scnprintf(linebuf + lx, linebuflen - lx,
268
                    "%s%8.8x", j ? " " : "", *(ptr4 + j));
269
        ascii_column = 9 * ngroups + 2;
270
        break;
271
    }
272
 
273
    case 2: {
274
        const u16 *ptr2 = buf;
275
        int ngroups = len / groupsize;
276
 
277
        for (j = 0; j < ngroups; j++)
278
            lx += scnprintf(linebuf + lx, linebuflen - lx,
279
                    "%s%4.4x", j ? " " : "", *(ptr2 + j));
280
        ascii_column = 5 * ngroups + 2;
281
        break;
282
    }
283
 
284
    default:
285
        for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
286
            ch = ptr[j];
287
            linebuf[lx++] = hex_asc_hi(ch);
288
            linebuf[lx++] = hex_asc_lo(ch);
289
            linebuf[lx++] = ' ';
290
        }
291
        if (j)
292
            lx--;
293
 
294
        ascii_column = 3 * rowsize + 2;
295
        break;
296
    }
297
    if (!ascii)
298
        goto nil;
299
 
300
    while (lx < (linebuflen - 1) && lx < (ascii_column - 1))
301
        linebuf[lx++] = ' ';
302
    for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
303
        ch = ptr[j];
304
        linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
305
    }
306
nil:
307
    linebuf[lx++] = '\0';
308
}
309
 
310
/**
311
 * print_hex_dump - print a text hex dump to syslog for a binary blob of data
312
 * @level: kernel log level (e.g. KERN_DEBUG)
313
 * @prefix_str: string to prefix each line with;
314
 *  caller supplies trailing spaces for alignment if desired
315
 * @prefix_type: controls whether prefix of an offset, address, or none
316
 *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
317
 * @rowsize: number of bytes to print per line; must be 16 or 32
318
 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
319
 * @buf: data blob to dump
320
 * @len: number of bytes in the @buf
321
 * @ascii: include ASCII after the hex output
322
 *
323
 * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
324
 * to the kernel log at the specified kernel log level, with an optional
325
 * leading prefix.
326
 *
327
 * print_hex_dump() works on one "line" of output at a time, i.e.,
328
 * 16 or 32 bytes of input data converted to hex + ASCII output.
329
 * print_hex_dump() iterates over the entire input @buf, breaking it into
330
 * "line size" chunks to format and print.
331
 *
332
 * E.g.:
333
 *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
334
 *          16, 1, frame->data, frame->len, true);
335
 *
336
 * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
337
 * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
338
 * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
339
 * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
340
 */
341
void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
342
            int rowsize, int groupsize,
343
            const void *buf, size_t len, bool ascii)
344
{
345
    const u8 *ptr = buf;
346
    int i, linelen, remaining = len;
347
    unsigned char linebuf[32 * 3 + 2 + 32 + 1];
348
 
349
    if (rowsize != 16 && rowsize != 32)
350
        rowsize = 16;
351
 
352
    for (i = 0; i < len; i += rowsize) {
353
        linelen = min(remaining, rowsize);
354
        remaining -= rowsize;
355
 
356
        hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
357
                   linebuf, sizeof(linebuf), ascii);
358
 
359
        switch (prefix_type) {
360
        case DUMP_PREFIX_ADDRESS:
361
            printk("%s%s%p: %s\n",
362
                   level, prefix_str, ptr + i, linebuf);
363
            break;
364
        case DUMP_PREFIX_OFFSET:
365
            printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
366
            break;
367
        default:
368
            printk("%s%s%s\n", level, prefix_str, linebuf);
369
            break;
370
        }
371
    }
372
}
373
 
374
void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
375
                          const void *buf, size_t len)
376
{
377
    print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
378
                       buf, len, true);
379
}
380
 
381
 
382
static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
383
                unsigned int *ecx, unsigned int *edx)
384
{
385
    /* ecx is often an input as well as an output. */
386
    asm volatile("cpuid"
387
        : "=a" (*eax),
388
          "=b" (*ebx),
389
          "=c" (*ecx),
390
          "=d" (*edx)
391
        : "0" (*eax), "2" (*ecx)
392
        : "memory");
393
}
394
 
395
static inline void cpuid(unsigned int op,
396
                         unsigned int *eax, unsigned int *ebx,
397
                         unsigned int *ecx, unsigned int *edx)
398
{
399
        *eax = op;
400
        *ecx = 0;
401
        __cpuid(eax, ebx, ecx, edx);
402
}
403
 
404
void cpu_detect()
405
{
406
    u32 junk, tfms, cap0, misc;
407
 
408
    cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
409
 
410
    if (cap0 & (1<<19))
411
    {
412
        x86_clflush_size = ((misc >> 8) & 0xff) * 8;
413
    }
414
 
415
    tsc_khz = GetCpuFreq()/1000;
416
}
417
 
418
 
419
 
420
void *kmemdup(const void *src, size_t len, gfp_t gfp)
421
{
422
    void *p;
423
 
424
    p = kmalloc(len, gfp);
425
    if (p)
426
        memcpy(p, src, len);
427
    return p;
428
}
429
 
430
 
431
unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
432
{
433
        const unsigned long *p = addr;
434
        unsigned long result = 0;
435
        unsigned long tmp;
436
 
437
        while (size & ~(BITS_PER_LONG-1)) {
438
                if (~(tmp = *(p++)))
439
                        goto found;
440
                result += BITS_PER_LONG;
441
                size -= BITS_PER_LONG;
442
        }
443
        if (!size)
444
                return result;
445
 
446
        tmp = (*p) | (~0UL << size);
447
        if (tmp == ~0UL)        /* Are any bits zero? */
448
                return result + size;   /* Nope. */
449
found:
450
        return result + ffz(tmp);
451
}
452