Subversion Repositories Kolibri OS

Rev

Rev 5270 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5270 Rev 6082
Line 21... Line 21...
21
#include 
21
#include 
22
#include 
22
#include 
23
#include 
23
#include 
24
#include 
24
#include 
Line -... Line 25...
-
 
25
 
25
 
26
#include 
26
#include 
27
#include 
Line 27... Line 28...
27
#include 
28
#include 
28
 
29
 
Line 29... Line -...
29
#include 
-
 
30
#include 		/* for PAGE_SIZE */
-
 
31
 
-
 
32
 
-
 
33
static inline u64 div_u64(u64 dividend, u32 divisor)
-
 
34
{
-
 
35
        u32 remainder;
-
 
36
        return div_u64_rem(dividend, divisor, &remainder);
-
 
37
}
-
 
38
 
-
 
39
static inline s64 div_s64(s64 dividend, s32 divisor)
-
 
40
{
-
 
41
        s32 remainder;
-
 
42
        return div_s64_rem(dividend, divisor, &remainder);
-
 
43
}
30
#include 
Line 44... Line 31...
44
 
31
#include 		/* for PAGE_SIZE */
45
 
32
 
Line 53... Line 40...
53
#endif
40
#endif
Line 54... Line 41...
54
 
41
 
Line 55... Line 42...
55
#define KSTRTOX_OVERFLOW        (1U << 31)
42
#define KSTRTOX_OVERFLOW        (1U << 31)
-
 
43
 
Line 56... Line 44...
56
 
44
const char hex_asc[] = "0123456789abcdef";
57
const char hex_asc[] = "0123456789abcdef";
45
const char hex_asc_upper[] = "0123456789ABCDEF";
Line 208... Line 196...
208
static noinline_for_stack
196
static noinline_for_stack
209
int skip_atoi(const char **s)
197
int skip_atoi(const char **s)
210
{
198
{
211
	int i = 0;
199
	int i = 0;
Line 212... Line 200...
212
 
200
 
213
	while (isdigit(**s))
201
	do {
-
 
202
		i = i*10 + *((*s)++) - '0';
Line 214... Line 203...
214
		i = i*10 + *((*s)++) - '0';
203
	} while (isdigit(**s));
215
 
204
 
Line -... Line 205...
-
 
205
	return i;
216
	return i;
206
}
217
}
207
 
218
 
208
/*
-
 
209
 * Decimal conversion is by far the most typical, and is used for
-
 
210
 * /proc and /sys data. This directly impacts e.g. top performance
-
 
211
 * with many processes running. We optimize it for speed by emitting
-
 
212
 * two characters at a time, using a 200 byte lookup table. This
219
/* Decimal conversion is by far the most typical, and is used
213
 * roughly halves the number of multiplications compared to computing
220
 * for /proc and /sys data. This directly impacts e.g. top performance
214
 * the digits one at a time. Implementation strongly inspired by the
-
 
215
 * previous version, which in turn used ideas described at
-
 
216
 *  (with permission
-
 
217
 * from the author, Douglas W. Jones).
-
 
218
 *
-
 
219
 * It turns out there is precisely one 26 bit fixed-point
-
 
220
 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
-
 
221
 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
-
 
222
 * range happens to be somewhat larger (x <= 1073741898), but that's
-
 
223
 * irrelevant for our purpose.
-
 
224
 *
-
 
225
 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
-
 
226
 * need a 32x32->64 bit multiply, so we simply use the same constant.
-
 
227
 *
221
 * with many processes running. We optimize it for speed
228
 * For dividing a number in the range [100, 10^4-1] by 100, there are
-
 
229
 * several options. The simplest is (x * 0x147b) >> 19, which is valid
-
 
230
 * for all x <= 43698.
-
 
231
 */
-
 
232
 
-
 
233
static const u16 decpair[100] = {
-
 
234
#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
-
 
235
	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
-
 
236
	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
-
 
237
	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
-
 
238
	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
-
 
239
	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
-
 
240
	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
-
 
241
	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
-
 
242
	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
-
 
243
	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
Line -... Line 244...
-
 
244
	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
222
 * using ideas described at 
245
#undef _
-
 
246
};
-
 
247
 
223
 * (with permission from the author, Douglas W. Jones).
248
/*
-
 
249
 * This will print a single '0' even if r == 0, since we would
224
 */
250
 * immediately jump to out_r where two 0s would be written but only
225
 
251
 * one of them accounted for in buf. This is needed by ip4_string
226
#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
252
 * below. All other callers pass a non-zero value of r.
227
/* Formats correctly any integer in [0, 999999999] */
253
*/
Line 228... Line -...
228
static noinline_for_stack
-
 
229
char *put_dec_full9(char *buf, unsigned q)
-
 
230
{
-
 
231
	unsigned r;
-
 
232
 
254
static noinline_for_stack
233
	/*
255
char *put_dec_trunc8(char *buf, unsigned r)
234
	 * Possible ways to approx. divide by 10
256
{
-
 
257
	unsigned q;
235
	 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
258
 
236
	 * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
259
	/* 1 <= r < 10^8 */
237
	 * (x * 0x6667) >> 18     x <      43699
260
	if (r < 100)
238
	 * (x * 0x3334) >> 17     x <      16389
261
		goto out_r;
239
	 * (x * 0x199a) >> 16     x <      16389
-
 
-
 
262
 
240
	 * (x * 0x0ccd) >> 15     x <      16389
263
	/* 100 <= r < 10^8 */
241
	 * (x * 0x0667) >> 14     x <       2739
-
 
242
	 * (x * 0x0334) >> 13     x <       1029
-
 
243
	 * (x * 0x019a) >> 12     x <       1029
-
 
244
	 * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
264
	q = (r * (u64)0x28f5c29) >> 32;
245
	 * (x * 0x0067) >> 10     x <        179
265
	*((u16 *)buf) = decpair[r - 100*q];
246
	 * (x * 0x0034) >>  9     x <         69 same
266
	buf += 2;
247
	 * (x * 0x001a) >>  8     x <         69 same
-
 
248
	 * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
267
 
249
	 * (x * 0x0007) >>  6     x <         19
268
	/* 1 <= q < 10^6 */
250
	 * See 
269
	if (q < 100)
251
	 */
-
 
252
	r      = (q * (uint64_t)0x1999999a) >> 32;
270
		goto out_q;
-
 
271
 
253
	*buf++ = (q - 10 * r) + '0'; /* 1 */
272
	/*  100 <= q < 10^6 */
254
	q      = (r * (uint64_t)0x1999999a) >> 32;
273
	r = (q * (u64)0x28f5c29) >> 32;
255
	*buf++ = (r - 10 * q) + '0'; /* 2 */
274
	*((u16 *)buf) = decpair[q - 100*r];
-
 
275
	buf += 2;
256
	r      = (q * (uint64_t)0x1999999a) >> 32;
276
 
257
	*buf++ = (q - 10 * r) + '0'; /* 3 */
-
 
258
	q      = (r * (uint64_t)0x1999999a) >> 32;
277
	/* 1 <= r < 10^4 */
259
	*buf++ = (r - 10 * q) + '0'; /* 4 */
278
	if (r < 100)
260
	r      = (q * (uint64_t)0x1999999a) >> 32;
279
		goto out_r;
-
 
280
 
261
	*buf++ = (q - 10 * r) + '0'; /* 5 */
281
	/* 100 <= r < 10^4 */
-
 
282
	q = (r * 0x147b) >> 19;
-
 
283
	*((u16 *)buf) = decpair[r - 100*q];
262
	/* Now value is under 10000, can avoid 64-bit multiply */
284
	buf += 2;
263
	q      = (r * 0x199a) >> 16;
285
out_q:
264
	*buf++ = (r - 10 * q)  + '0'; /* 6 */
286
	/* 1 <= q < 100 */
265
	r      = (q * 0xcd) >> 11;
287
	r = q;
266
	*buf++ = (q - 10 * r)  + '0'; /* 7 */
288
out_r:
267
	q      = (r * 0xcd) >> 11;
-
 
Line 268... Line 289...
268
	*buf++ = (r - 10 * q) + '0'; /* 8 */
289
	/* 1 <= r < 100 */
269
	*buf++ = q + '0'; /* 9 */
-
 
270
	return buf;
-
 
271
}
-
 
272
#endif
290
	*((u16 *)buf) = decpair[r];
273
 
291
	buf += r < 10 ? 1 : 2;
274
/* Similar to above but do not pad with zeros.
292
	return buf;
275
 * Code can be easily arranged to print 9 digits too, but our callers
293
}
Line 276... Line -...
276
 * always call put_dec_full9() instead when the number has 9 decimal digits.
-
 
277
 */
-
 
278
static noinline_for_stack
294
 
279
char *put_dec_trunc8(char *buf, unsigned r)
295
#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
280
{
296
static noinline_for_stack
281
	unsigned q;
297
char *put_dec_full8(char *buf, unsigned r)
282
 
298
{
-
 
299
	unsigned q;
283
	/* Copy of previous function's body with added early returns */
300
 
284
	while (r >= 10000) {
301
	/* 0 <= r < 10^8 */
285
		q = r + '0';
302
	q = (r * (u64)0x28f5c29) >> 32;
-
 
303
	*((u16 *)buf) = decpair[r - 100*q];
286
		r  = (r * (uint64_t)0x1999999a) >> 32;
304
	buf += 2;
287
		*buf++ = q - 10*r;
305
 
288
	}
306
	/* 0 <= q < 10^6 */
289
 
307
	r = (q * (u64)0x28f5c29) >> 32;
290
	q      = (r * 0x199a) >> 16;	/* r <= 9999 */
308
	*((u16 *)buf) = decpair[q - 100*r];
291
	*buf++ = (r - 10 * q)  + '0';
309
	buf += 2;
292
	if (q == 0)
310
 
293
		return buf;
311
	/* 0 <= r < 10^4 */
294
	r      = (q * 0xcd) >> 11;	/* q <= 999 */
-
 
295
	*buf++ = (q - 10 * r)  + '0';
-
 
296
	if (r == 0)
312
	q = (r * 0x147b) >> 19;
297
		return buf;
313
	*((u16 *)buf) = decpair[r - 100*q];
Line 298... Line -...
298
	q      = (r * 0xcd) >> 11;	/* r <= 99 */
-
 
299
	*buf++ = (r - 10 * q) + '0';
-
 
300
	if (q == 0)
-
 
301
		return buf;
-
 
302
	*buf++ = q + '0';		 /* q <= 9 */
-
 
303
	return buf;
-
 
304
}
-
 
305
 
-
 
306
/* There are two algorithms to print larger numbers.
-
 
307
 * One is generic: divide by 1000000000 and repeatedly print
-
 
308
 * groups of (up to) 9 digits. It's conceptually simple,
-
 
309
 * but requires a (unsigned long long) / 1000000000 division.
-
 
310
 *
-
 
311
 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
-
 
312
 * manipulates them cleverly and generates groups of 4 decimal digits.
-
 
313
 * It so happens that it does NOT require long long division.
-
 
314
 *
-
 
315
 * If long is > 32 bits, division of 64-bit values is relatively easy,
-
 
316
 * and we will use the first algorithm.
-
 
317
 * If long long is > 64 bits (strange architecture with VERY large long long),
314
	buf += 2;
318
 * second algorithm can't be used, and we again use the first one.
-
 
319
 *
-
 
320
 * Else (if long is 32 bits and long long is 64 bits) we use second one.
315
 
321
 */
316
	/* 0 <= q < 100 */
322
 
-
 
323
#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
-
 
324
 
-
 
325
/* First algorithm: generic */
317
	*((u16 *)buf) = decpair[q];
-
 
318
	buf += 2;
-
 
319
	return buf;
-
 
320
}
326
 
321
 
327
static
322
static noinline_for_stack
328
char *put_dec(char *buf, unsigned long long n)
323
char *put_dec(char *buf, unsigned long long n)
329
{
324
{
Line 330... Line -...
330
	if (n >= 100*1000*1000) {
-
 
-
 
325
		if (n >= 100*1000*1000)
Line 331... Line -...
331
		while (n >= 1000*1000*1000)
-
 
332
			buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
-
 
333
		if (n >= 100*1000*1000)
-
 
334
			return put_dec_full9(buf, n);
326
		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
335
	}
327
	/* 1 <= n <= 1.6e11 */
336
	return put_dec_trunc8(buf, n);
328
	if (n >= 100*1000*1000)
337
}
329
		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
338
 
-
 
-
 
330
	/* 1 <= n < 1e8 */
339
#else
331
	return put_dec_trunc8(buf, n);
340
 
332
}
341
/* Second algorithm: valid only for 64-bit long longs */
333
 
342
 
334
#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
343
/* See comment in put_dec_full9 for choice of constants */
335
 
344
static noinline_for_stack
336
static void
345
void put_dec_full4(char *buf, unsigned q)
337
put_dec_full4(char *buf, unsigned r)
Line 346... Line 338...
346
{
338
{
347
	unsigned r;
339
	unsigned q;
348
	r      = (q * 0xccd) >> 15;
340
 
349
	buf[0] = (q - 10 * r) + '0';
341
	/* 0 <= r < 10^4 */
350
	q      = (r * 0xcd) >> 11;
342
	q = (r * 0x147b) >> 19;
351
	buf[1] = (r - 10 * q)  + '0';
343
	*((u16 *)buf) = decpair[r - 100*q];
352
	r      = (q * 0xcd) >> 11;
344
	buf += 2;
353
	buf[2] = (q - 10 * r)  + '0';
345
	/* 0 <= q < 100 */
354
	buf[3] = r + '0';
346
	*((u16 *)buf) = decpair[q];
355
}
347
}
356
 
348
 
Line 357... Line 349...
357
/*
349
/*
Line 386... Line 378...
386
	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
378
	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
387
	h   = (n >> 32);
379
	h   = (n >> 32);
388
	d2  = (h      ) & 0xffff;
380
	d2  = (h      ) & 0xffff;
389
	d3  = (h >> 16); /* implicit "& 0xffff" */
381
	d3  = (h >> 16); /* implicit "& 0xffff" */
Line -... Line 382...
-
 
382
 
-
 
383
	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
390
 
384
	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
391
	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
385
	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
Line 392... Line 386...
392
	q = put_dec_helper4(buf, q);
386
	q = put_dec_helper4(buf, q);
393
 
387
 
Line 415... Line 409...
415
 *
409
 *
416
 * If speed is not important, use snprintf(). It's easy to read the code.
410
 * If speed is not important, use snprintf(). It's easy to read the code.
417
 */
411
 */
418
int num_to_str(char *buf, int size, unsigned long long num)
412
int num_to_str(char *buf, int size, unsigned long long num)
419
{
413
{
-
 
414
	/* put_dec requires 2-byte alignment of the buffer. */
420
	char tmp[sizeof(num) * 3];
415
	char tmp[sizeof(num) * 3] __aligned(2);
421
	int idx, len;
416
	int idx, len;
Line 422... Line 417...
422
 
417
 
423
	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
418
	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
424
	if (num <= 9) {
419
	if (num <= 9) {
Line 433... Line 428...
433
	for (idx = 0; idx < len; ++idx)
428
	for (idx = 0; idx < len; ++idx)
434
		buf[idx] = tmp[len - idx - 1];
429
		buf[idx] = tmp[len - idx - 1];
435
	return len;
430
	return len;
436
}
431
}
Line 437... Line 432...
437
 
432
 
438
#define ZEROPAD	1		/* pad with zero */
433
#define SIGN	1		/* unsigned/signed, must be 1 */
439
#define SIGN	2		/* unsigned/signed long */
434
#define LEFT	2		/* left justified */
440
#define PLUS	4		/* show plus */
435
#define PLUS	4		/* show plus */
441
#define SPACE	8		/* space if plus */
436
#define SPACE	8		/* space if plus */
442
#define LEFT	16		/* left justified */
437
#define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
443
#define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
438
#define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
Line 444... Line 439...
444
#define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
439
#define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
445
 
440
 
Line 476... Line 471...
476
 
471
 
477
static noinline_for_stack
472
static noinline_for_stack
478
char *number(char *buf, char *end, unsigned long long num,
473
char *number(char *buf, char *end, unsigned long long num,
479
	     struct printf_spec spec)
474
	     struct printf_spec spec)
480
{
475
{
481
	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
-
 
482
	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
-
 
483
 
476
	/* put_dec requires 2-byte alignment of the buffer. */
484
	char tmp[66];
477
	char tmp[3 * sizeof(num)] __aligned(2);
485
	char sign;
478
	char sign;
486
	char locase;
479
	char locase;
487
	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
480
	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
488
	int i;
481
	int i;
Line 515... Line 508...
515
	}
508
	}
Line 516... Line 509...
516
 
509
 
517
	/* generate full string in tmp[], in reverse order */
510
	/* generate full string in tmp[], in reverse order */
518
	i = 0;
511
	i = 0;
519
	if (num < spec.base)
512
	if (num < spec.base)
520
		tmp[i++] = digits[num] | locase;
-
 
521
	/* Generic code, for any base:
-
 
522
	else do {
-
 
523
		tmp[i++] = (digits[do_div(num,base)] | locase);
-
 
524
	} while (num != 0);
-
 
525
	*/
513
		tmp[i++] = hex_asc_upper[num] | locase;
526
	else if (spec.base != 10) { /* 8 or 16 */
514
	else if (spec.base != 10) { /* 8 or 16 */
527
		int mask = spec.base - 1;
515
		int mask = spec.base - 1;
Line 528... Line 516...
528
		int shift = 3;
516
		int shift = 3;
529
 
517
 
530
		if (spec.base == 16)
518
		if (spec.base == 16)
531
			shift = 4;
519
			shift = 4;
532
		do {
520
		do {
533
			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
521
			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
534
			num >>= shift;
522
			num >>= shift;
535
		} while (num);
523
		} while (num);
536
	} else { /* base 10 */
524
	} else { /* base 10 */
Line 540... Line 528...
540
	/* printing 100 using %2d gives "100", not "00" */
528
	/* printing 100 using %2d gives "100", not "00" */
541
	if (i > spec.precision)
529
	if (i > spec.precision)
542
		spec.precision = i;
530
		spec.precision = i;
543
	/* leading space padding */
531
	/* leading space padding */
544
	spec.field_width -= spec.precision;
532
	spec.field_width -= spec.precision;
545
	if (!(spec.flags & (ZEROPAD+LEFT))) {
533
	if (!(spec.flags & (ZEROPAD | LEFT))) {
546
		while (--spec.field_width >= 0) {
534
		while (--spec.field_width >= 0) {
547
			if (buf < end)
535
			if (buf < end)
548
				*buf = ' ';
536
				*buf = ' ';
549
			++buf;
537
			++buf;
550
		}
538
		}
Line 568... Line 556...
568
			++buf;
556
			++buf;
569
		}
557
		}
570
	}
558
	}
571
	/* zero or space padding */
559
	/* zero or space padding */
572
	if (!(spec.flags & LEFT)) {
560
	if (!(spec.flags & LEFT)) {
573
		char c = (spec.flags & ZEROPAD) ? '0' : ' ';
561
		char c = ' ' + (spec.flags & ZEROPAD);
-
 
562
		BUILD_BUG_ON(' ' + ZEROPAD != '0');
574
		while (--spec.field_width >= 0) {
563
		while (--spec.field_width >= 0) {
575
			if (buf < end)
564
			if (buf < end)
576
				*buf = c;
565
				*buf = c;
577
			++buf;
566
			++buf;
578
		}
567
		}
Line 710... Line 699...
710
	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
699
	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
711
#define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
700
#define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
712
#define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
701
#define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
713
#define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
702
#define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
714
#define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
703
#define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
715
#undef max
-
 
716
#define max(a,b) ((a) > (b) ? (a) : (b))
-
 
717
	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
704
	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
718
		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
705
		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
Line 719... Line 706...
719
 
706
 
720
	char *p = sym, *pend = sym + sizeof(sym);
707
	char *p = sym, *pend = sym + sizeof(sym);
Line 740... Line 727...
740
	} else {
727
	} else {
741
		p = string(p, pend, "??? ", str_spec);
728
		p = string(p, pend, "??? ", str_spec);
742
		specp = &mem_spec;
729
		specp = &mem_spec;
743
		decode = 0;
730
		decode = 0;
744
	}
731
	}
-
 
732
	if (decode && res->flags & IORESOURCE_UNSET) {
-
 
733
		p = string(p, pend, "size ", str_spec);
-
 
734
		p = number(p, pend, resource_size(res), *specp);
-
 
735
	} else {
745
	p = number(p, pend, res->start, *specp);
736
	p = number(p, pend, res->start, *specp);
746
	if (res->start != res->end) {
737
	if (res->start != res->end) {
747
		*p++ = '-';
738
		*p++ = '-';
748
		p = number(p, pend, res->end, *specp);
739
		p = number(p, pend, res->end, *specp);
749
	}
740
		}
-
 
741
	}
750
	if (decode) {
742
	if (decode) {
751
		if (res->flags & IORESOURCE_MEM_64)
743
		if (res->flags & IORESOURCE_MEM_64)
752
			p = string(p, pend, " 64bit", str_spec);
744
			p = string(p, pend, " 64bit", str_spec);
753
		if (res->flags & IORESOURCE_PREFETCH)
745
		if (res->flags & IORESOURCE_PREFETCH)
754
			p = string(p, pend, " pref", str_spec);
746
			p = string(p, pend, " pref", str_spec);
Line 798... Line 790...
798
	}
790
	}
Line 799... Line 791...
799
 
791
 
800
	if (spec.field_width > 0)
792
	if (spec.field_width > 0)
Line 801... Line 793...
801
		len = min_t(int, spec.field_width, 64);
793
		len = min_t(int, spec.field_width, 64);
-
 
794
 
-
 
795
	for (i = 0; i < len; ++i) {
-
 
796
		if (buf < end)
-
 
797
			*buf = hex_asc_hi(addr[i]);
802
 
798
		++buf;
-
 
799
		if (buf < end)
-
 
800
			*buf = hex_asc_lo(addr[i]);
-
 
801
		++buf;
-
 
802
 
-
 
803
		if (separator && i != len - 1) {
-
 
804
			if (buf < end)
-
 
805
				*buf = separator;
-
 
806
			++buf;
-
 
807
		}
-
 
808
	}
-
 
809
 
-
 
810
	return buf;
-
 
811
}
-
 
812
 
-
 
813
static noinline_for_stack
-
 
814
char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
-
 
815
		    struct printf_spec spec, const char *fmt)
-
 
816
{
-
 
817
	const int CHUNKSZ = 32;
-
 
818
	int nr_bits = max_t(int, spec.field_width, 0);
-
 
819
	int i, chunksz;
-
 
820
	bool first = true;
-
 
821
 
-
 
822
	/* reused to print numbers */
-
 
823
	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
-
 
824
 
-
 
825
	chunksz = nr_bits & (CHUNKSZ - 1);
-
 
826
	if (chunksz == 0)
-
 
827
		chunksz = CHUNKSZ;
-
 
828
 
-
 
829
	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
-
 
830
	for (; i >= 0; i -= CHUNKSZ) {
-
 
831
		u32 chunkmask, val;
-
 
832
		int word, bit;
-
 
833
 
-
 
834
		chunkmask = ((1ULL << chunksz) - 1);
-
 
835
		word = i / BITS_PER_LONG;
-
 
836
		bit = i % BITS_PER_LONG;
-
 
837
		val = (bitmap[word] >> bit) & chunkmask;
-
 
838
 
-
 
839
		if (!first) {
-
 
840
			if (buf < end)
-
 
841
				*buf = ',';
-
 
842
			buf++;
-
 
843
		}
-
 
844
		first = false;
-
 
845
 
-
 
846
		spec.field_width = DIV_ROUND_UP(chunksz, 4);
-
 
847
		buf = number(buf, end, val, spec);
-
 
848
 
-
 
849
		chunksz = CHUNKSZ;
-
 
850
	}
-
 
851
	return buf;
-
 
852
}
-
 
853
 
-
 
854
static noinline_for_stack
-
 
855
char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
-
 
856
			 struct printf_spec spec, const char *fmt)
-
 
857
{
-
 
858
	int nr_bits = max_t(int, spec.field_width, 0);
-
 
859
	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
-
 
860
	int cur, rbot, rtop;
-
 
861
	bool first = true;
-
 
862
 
-
 
863
	/* reused to print numbers */
-
 
864
	spec = (struct printf_spec){ .base = 10 };
-
 
865
 
-
 
866
	rbot = cur = find_first_bit(bitmap, nr_bits);
-
 
867
	while (cur < nr_bits) {
-
 
868
		rtop = cur;
-
 
869
		cur = find_next_bit(bitmap, nr_bits, cur + 1);
Line -... Line 870...
-
 
870
		if (cur < nr_bits && cur <= rtop + 1)
803
	for (i = 0; i < len && buf < end - 1; i++) {
871
			continue;
804
		buf = hex_byte_pack(buf, addr[i]);
872
 
-
 
873
		if (!first) {
805
 
874
			if (buf < end)
-
 
875
				*buf = ',';
Line -... Line 876...
-
 
876
			buf++;
-
 
877
		}
-
 
878
		first = false;
-
 
879
 
-
 
880
		buf = number(buf, end, rbot, spec);
-
 
881
		if (rbot < rtop) {
-
 
882
			if (buf < end)
-
 
883
				*buf = '-';
-
 
884
			buf++;
-
 
885
 
-
 
886
			buf = number(buf, end, rtop, spec);
806
		if (buf < end && separator && i != len - 1)
887
	}
807
			*buf++ = separator;
888
 
Line 808... Line 889...
808
	}
889
		rbot = cur;
809
 
890
	}
Line 876... Line 957...
876
		index = 0;
957
		index = 0;
877
		step = 1;
958
		step = 1;
878
		break;
959
		break;
879
	}
960
	}
880
	for (i = 0; i < 4; i++) {
961
	for (i = 0; i < 4; i++) {
881
		char temp[3];	/* hold each IP quad in reverse order */
962
		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
882
		int digits = put_dec_trunc8(temp, addr[index]) - temp;
963
		int digits = put_dec_trunc8(temp, addr[index]) - temp;
883
		if (leading_zeros) {
964
		if (leading_zeros) {
884
			if (digits < 3)
965
			if (digits < 3)
885
				*p++ = '0';
966
				*p++ = '0';
886
			if (digits < 2)
967
			if (digits < 2)
Line 926... Line 1007...
926
 * - 's' For symbolic direct pointers without offset
1007
 * - 's' For symbolic direct pointers without offset
927
 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1008
 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
928
 * - 'B' For backtraced symbolic direct pointers with offset
1009
 * - 'B' For backtraced symbolic direct pointers with offset
929
 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1010
 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
930
 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1011
 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
-
 
1012
 * - 'b[l]' For a bitmap, the number of bits is determined by the field
-
 
1013
 *       width which must be explicitly specified either as part of the
-
 
1014
 *       format string '%32b[l]' or through '%*b[l]', [l] selects
-
 
1015
 *       range-list format instead of hex format
931
 * - 'M' For a 6-byte MAC address, it prints the address in the
1016
 * - 'M' For a 6-byte MAC address, it prints the address in the
932
 *       usual colon-separated hex notation
1017
 *       usual colon-separated hex notation
933
 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1018
 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
934
 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1019
 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
935
 *       with a dash-separated hex notation
1020
 *       with a dash-separated hex notation
Line 947... Line 1032...
947
 *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1032
 *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
948
 *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1033
 *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
949
 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1034
 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
950
 * - 'I[6S]c' for IPv6 addresses printed as specified by
1035
 * - 'I[6S]c' for IPv6 addresses printed as specified by
951
 *       http://tools.ietf.org/html/rfc5952
1036
 *       http://tools.ietf.org/html/rfc5952
-
 
1037
 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
-
 
1038
 *                of the following flags (see string_escape_mem() for the
-
 
1039
 *                details):
-
 
1040
 *                  a - ESCAPE_ANY
-
 
1041
 *                  c - ESCAPE_SPECIAL
-
 
1042
 *                  h - ESCAPE_HEX
-
 
1043
 *                  n - ESCAPE_NULL
-
 
1044
 *                  o - ESCAPE_OCTAL
-
 
1045
 *                  p - ESCAPE_NP
-
 
1046
 *                  s - ESCAPE_SPACE
-
 
1047
 *                By default ESCAPE_ANY_NP is used.
952
 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1048
 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
953
 *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1049
 *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
954
 *       Options for %pU are:
1050
 *       Options for %pU are:
955
 *         b big endian lower case hex (default)
1051
 *         b big endian lower case hex (default)
956
 *         B big endian UPPER case hex
1052
 *         B big endian UPPER case hex
Line 976... Line 1072...
976
 *            to use print_hex_dump() for the larger input.
1072
 *            to use print_hex_dump() for the larger input.
977
 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1073
 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
978
 *           (default assumed to be phys_addr_t, passed by reference)
1074
 *           (default assumed to be phys_addr_t, passed by reference)
979
 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1075
 * - 'd[234]' For a dentry name (optionally 2-4 last components)
980
 * - 'D[234]' Same as 'd' but for a struct file
1076
 * - 'D[234]' Same as 'd' but for a struct file
-
 
1077
 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
-
 
1078
 *       (legacy clock framework) of the clock
-
 
1079
 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
-
 
1080
 *        (legacy clock framework) of the clock
-
 
1081
 * - 'Cr' For a clock, it prints the current rate of the clock
981
 *
1082
 *
982
 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1083
 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
983
 * function pointers are really function descriptors, which contain a
1084
 * function pointers are really function descriptors, which contain a
984
 * pointer to the real address.
1085
 * pointer to the real address.
985
 */
1086
 */
Line 1186... Line 1287...
1186
		spec->type = FORMAT_TYPE_STR;
1287
		spec->type = FORMAT_TYPE_STR;
1187
		return ++fmt - start;
1288
		return ++fmt - start;
Line 1188... Line 1289...
1188
 
1289
 
1189
	case 'p':
1290
	case 'p':
1190
		spec->type = FORMAT_TYPE_PTR;
1291
		spec->type = FORMAT_TYPE_PTR;
1191
		return fmt - start;
-
 
Line 1192... Line 1292...
1192
		/* skip alnum */
1292
		return ++fmt - start;
1193
 
1293
 
1194
	case '%':
1294
	case '%':
Line 1228... Line 1328...
1228
	}
1328
	}
Line 1229... Line 1329...
1229
 
1329
 
1230
	if (spec->qualifier == 'L')
1330
	if (spec->qualifier == 'L')
1231
		spec->type = FORMAT_TYPE_LONG_LONG;
1331
		spec->type = FORMAT_TYPE_LONG_LONG;
1232
	else if (spec->qualifier == 'l') {
-
 
1233
		if (spec->flags & SIGN)
1332
	else if (spec->qualifier == 'l') {
1234
			spec->type = FORMAT_TYPE_LONG;
-
 
1235
		else
1333
		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1236
			spec->type = FORMAT_TYPE_ULONG;
1334
		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
1237
	} else if (_tolower(spec->qualifier) == 'z') {
1335
	} else if (_tolower(spec->qualifier) == 'z') {
1238
		spec->type = FORMAT_TYPE_SIZE_T;
1336
		spec->type = FORMAT_TYPE_SIZE_T;
1239
	} else if (spec->qualifier == 't') {
1337
	} else if (spec->qualifier == 't') {
1240
		spec->type = FORMAT_TYPE_PTRDIFF;
1338
		spec->type = FORMAT_TYPE_PTRDIFF;
1241
	} else if (spec->qualifier == 'H') {
-
 
1242
		if (spec->flags & SIGN)
1339
	} else if (spec->qualifier == 'H') {
1243
			spec->type = FORMAT_TYPE_BYTE;
-
 
1244
		else
1340
		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1245
			spec->type = FORMAT_TYPE_UBYTE;
1341
		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
1246
	} else if (spec->qualifier == 'h') {
-
 
1247
		if (spec->flags & SIGN)
1342
	} else if (spec->qualifier == 'h') {
1248
			spec->type = FORMAT_TYPE_SHORT;
-
 
1249
		else
1343
		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1250
			spec->type = FORMAT_TYPE_USHORT;
1344
		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
1251
	} else {
-
 
1252
		if (spec->flags & SIGN)
1345
	} else {
1253
			spec->type = FORMAT_TYPE_INT;
-
 
1254
		else
1346
		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1255
			spec->type = FORMAT_TYPE_UINT;
1347
		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Line 1256... Line 1348...
1256
	}
1348
	}
1257
 
1349
 
Line 1271... Line 1363...
1271
 * %pF output the name of a function pointer with its offset
1363
 * %pF output the name of a function pointer with its offset
1272
 * %pf output the name of a function pointer without its offset
1364
 * %pf output the name of a function pointer without its offset
1273
 * %pB output the name of a backtrace symbol with its offset
1365
 * %pB output the name of a backtrace symbol with its offset
1274
 * %pR output the address range in a struct resource with decoded flags
1366
 * %pR output the address range in a struct resource with decoded flags
1275
 * %pr output the address range in a struct resource with raw flags
1367
 * %pr output the address range in a struct resource with raw flags
-
 
1368
 * %pb output the bitmap with field width as the number of bits
-
 
1369
 * %pbl output the bitmap as range list with field width as the number of bits
1276
 * %pM output a 6-byte MAC address with colons
1370
 * %pM output a 6-byte MAC address with colons
1277
 * %pMR output a 6-byte MAC address with colons in reversed order
1371
 * %pMR output a 6-byte MAC address with colons in reversed order
1278
 * %pMF output a 6-byte MAC address with dashes
1372
 * %pMF output a 6-byte MAC address with dashes
1279
 * %pm output a 6-byte MAC address without colons
1373
 * %pm output a 6-byte MAC address without colons
1280
 * %pmR output a 6-byte MAC address without colons in reversed order
1374
 * %pmR output a 6-byte MAC address without colons in reversed order
Line 1288... Line 1382...
1288
 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1382
 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1289
 *   case.
1383
 *   case.
1290
 * %*pE[achnops] print an escaped buffer
1384
 * %*pE[achnops] print an escaped buffer
1291
 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1385
 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1292
 *           bytes of the input)
1386
 *           bytes of the input)
-
 
1387
 * %pC output the name (Common Clock Framework) or address (legacy clock
-
 
1388
 *     framework) of a clock
-
 
1389
 * %pCn output the name (Common Clock Framework) or address (legacy clock
-
 
1390
 *      framework) of a clock
-
 
1391
 * %pCr output the current rate of a clock
1293
 * %n is ignored
1392
 * %n is ignored
1294
 *
1393
 *
1295
 * ** Please update Documentation/printk-formats.txt when making changes **
1394
 * ** Please update Documentation/printk-formats.txt when making changes **
1296
 *
1395
 *
1297
 * The return value is the number of characters which would
1396
 * The return value is the number of characters which would
Line 1310... Line 1409...
1310
	char *str, *end;
1409
	char *str, *end;
1311
	struct printf_spec spec = {0};
1410
	struct printf_spec spec = {0};
Line 1312... Line 1411...
1312
 
1411
 
1313
	/* Reject out-of-range values early.  Large positive sizes are
1412
	/* Reject out-of-range values early.  Large positive sizes are
1314
	   used for unknown buffer sizes. */
1413
	   used for unknown buffer sizes. */
1315
    if ((int) size < 0)
1414
	if (WARN_ON_ONCE(size > INT_MAX))
Line 1316... Line 1415...
1316
		return 0;
1415
		return 0;
1317
 
1416
 
Line 1376... Line 1475...
1376
		case FORMAT_TYPE_STR:
1475
		case FORMAT_TYPE_STR:
1377
			str = string(str, end, va_arg(args, char *), spec);
1476
			str = string(str, end, va_arg(args, char *), spec);
1378
			break;
1477
			break;
Line 1379... Line 1478...
1379
 
1478
 
1380
		case FORMAT_TYPE_PTR:
1479
		case FORMAT_TYPE_PTR:
1381
			str = pointer(fmt+1, str, end, va_arg(args, void *),
1480
			str = pointer(fmt, str, end, va_arg(args, void *),
1382
				      spec);
1481
				      spec);
1383
			while (isalnum(*fmt))
1482
			while (isalnum(*fmt))
1384
				fmt++;
1483
				fmt++;