vsprintf.c 74.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
/*
 *  linux/lib/vsprintf.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/*
 * Wirzenius wrote this portably, Torvalds fucked it up :-)
 */

12
/*
Linus Torvalds's avatar
Linus Torvalds committed
13
14
15
16
17
18
19
 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
 * - changed to provide snprintf and vsnprintf functions
 * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
 * - scnprintf and vscnprintf
 */

#include <stdarg.h>
Stephen Boyd's avatar
Stephen Boyd committed
20
#include <linux/clk.h>
21
#include <linux/clk-provider.h>
22
#include <linux/module.h>	/* for KSYM_SYMBOL_LEN */
Linus Torvalds's avatar
Linus Torvalds committed
23
24
25
26
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/kernel.h>
27
#include <linux/kallsyms.h>
28
#include <linux/math64.h>
29
#include <linux/uaccess.h>
30
#include <linux/ioport.h>
31
#include <linux/dcache.h>
32
#include <linux/cred.h>
33
#include <linux/uuid.h>
34
#include <linux/of.h>
35
#include <net/addrconf.h>
36
37
#include <linux/siphash.h>
#include <linux/compiler.h>
38
39
40
#ifdef CONFIG_BLOCK
#include <linux/blkdev.h>
#endif
Linus Torvalds's avatar
Linus Torvalds committed
41

42
43
#include "../mm/internal.h"	/* For the trace_print_flags arrays */

Tim Schmielau's avatar
Tim Schmielau committed
44
#include <asm/page.h>		/* for PAGE_SIZE */
45
#include <asm/byteorder.h>	/* cpu_to_le16 */
Linus Torvalds's avatar
Linus Torvalds committed
46

47
#include <linux/string_helpers.h>
48
#include "kstrtox.h"
49

Linus Torvalds's avatar
Linus Torvalds committed
50
/**
51
 * simple_strtoull - convert a string to an unsigned long long
Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
55
56
 *
 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds's avatar
Linus Torvalds committed
57
 */
58
unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
59
{
60
61
	unsigned long long result;
	unsigned int rv;
62

63
64
65
66
	cp = _parse_integer_fixup_radix(cp, &base);
	rv = _parse_integer(cp, base, &result);
	/* FIXME */
	cp += (rv & ~KSTRTOX_OVERFLOW);
67

Linus Torvalds's avatar
Linus Torvalds committed
68
69
	if (endp)
		*endp = (char *)cp;
70

Linus Torvalds's avatar
Linus Torvalds committed
71
72
	return result;
}
73
EXPORT_SYMBOL(simple_strtoull);
Linus Torvalds's avatar
Linus Torvalds committed
74
75

/**
76
 * simple_strtoul - convert a string to an unsigned long
Linus Torvalds's avatar
Linus Torvalds committed
77
78
79
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
80
81
 *
 * This function is obsolete. Please use kstrtoul instead.
Linus Torvalds's avatar
Linus Torvalds committed
82
 */
83
unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
84
{
85
	return simple_strtoull(cp, endp, base);
Linus Torvalds's avatar
Linus Torvalds committed
86
}
87
EXPORT_SYMBOL(simple_strtoul);
Linus Torvalds's avatar
Linus Torvalds committed
88
89

/**
90
 * simple_strtol - convert a string to a signed long
Linus Torvalds's avatar
Linus Torvalds committed
91
92
93
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
94
95
 *
 * This function is obsolete. Please use kstrtol instead.
Linus Torvalds's avatar
Linus Torvalds committed
96
 */
97
long simple_strtol(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
98
{
99
100
	if (*cp == '-')
		return -simple_strtoul(cp + 1, endp, base);
101

102
	return simple_strtoul(cp, endp, base);
Linus Torvalds's avatar
Linus Torvalds committed
103
}
104
EXPORT_SYMBOL(simple_strtol);
Linus Torvalds's avatar
Linus Torvalds committed
105
106
107
108
109
110

/**
 * simple_strtoll - convert a string to a signed long long
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
111
112
 *
 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds's avatar
Linus Torvalds committed
113
 */
Harvey Harrison's avatar
Harvey Harrison committed
114
long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds's avatar
Linus Torvalds committed
115
{
116
	if (*cp == '-')
Harvey Harrison's avatar
Harvey Harrison committed
117
		return -simple_strtoull(cp + 1, endp, base);
118

Harvey Harrison's avatar
Harvey Harrison committed
119
	return simple_strtoull(cp, endp, base);
Linus Torvalds's avatar
Linus Torvalds committed
120
}
121
EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds's avatar
Linus Torvalds committed
122

123
124
static noinline_for_stack
int skip_atoi(const char **s)
Linus Torvalds's avatar
Linus Torvalds committed
125
{
126
	int i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
127

128
	do {
Linus Torvalds's avatar
Linus Torvalds committed
129
		i = i*10 + *((*s)++) - '0';
130
	} while (isdigit(**s));
131

Linus Torvalds's avatar
Linus Torvalds committed
132
133
134
	return i;
}

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Decimal conversion is by far the most typical, and is used for
 * /proc and /sys data. This directly impacts e.g. top performance
 * with many processes running. We optimize it for speed by emitting
 * two characters at a time, using a 200 byte lookup table. This
 * roughly halves the number of multiplications compared to computing
 * the digits one at a time. Implementation strongly inspired by the
 * previous version, which in turn used ideas described at
 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
 * from the author, Douglas W. Jones).
 *
 * It turns out there is precisely one 26 bit fixed-point
 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
 * range happens to be somewhat larger (x <= 1073741898), but that's
 * irrelevant for our purpose.
 *
 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
 * need a 32x32->64 bit multiply, so we simply use the same constant.
 *
 * For dividing a number in the range [100, 10^4-1] by 100, there are
 * several options. The simplest is (x * 0x147b) >> 19, which is valid
 * for all x <= 43698.
158
 */
159

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
static const u16 decpair[100] = {
#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
#undef _
};

/*
 * This will print a single '0' even if r == 0, since we would
177
178
179
 * immediately jump to out_r where two 0s would be written but only
 * one of them accounted for in buf. This is needed by ip4_string
 * below. All other callers pass a non-zero value of r.
180
*/
181
static noinline_for_stack
182
char *put_dec_trunc8(char *buf, unsigned r)
183
{
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	unsigned q;

	/* 1 <= r < 10^8 */
	if (r < 100)
		goto out_r;

	/* 100 <= r < 10^8 */
	q = (r * (u64)0x28f5c29) >> 32;
	*((u16 *)buf) = decpair[r - 100*q];
	buf += 2;

	/* 1 <= q < 10^6 */
	if (q < 100)
		goto out_q;

	/*  100 <= q < 10^6 */
	r = (q * (u64)0x28f5c29) >> 32;
	*((u16 *)buf) = decpair[q - 100*r];
	buf += 2;

	/* 1 <= r < 10^4 */
	if (r < 100)
		goto out_r;

	/* 100 <= r < 10^4 */
	q = (r * 0x147b) >> 19;
	*((u16 *)buf) = decpair[r - 100*q];
	buf += 2;
out_q:
	/* 1 <= q < 100 */
	r = q;
out_r:
	/* 1 <= r < 100 */
	*((u16 *)buf) = decpair[r];
218
	buf += r < 10 ? 1 : 2;
219
220
	return buf;
}
221

222
#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
223
static noinline_for_stack
224
char *put_dec_full8(char *buf, unsigned r)
225
{
226
227
	unsigned q;

228
229
230
231
	/* 0 <= r < 10^8 */
	q = (r * (u64)0x28f5c29) >> 32;
	*((u16 *)buf) = decpair[r - 100*q];
	buf += 2;
232

233
234
235
236
	/* 0 <= q < 10^6 */
	r = (q * (u64)0x28f5c29) >> 32;
	*((u16 *)buf) = decpair[q - 100*r];
	buf += 2;
237

238
239
240
241
	/* 0 <= r < 10^4 */
	q = (r * 0x147b) >> 19;
	*((u16 *)buf) = decpair[r - 100*q];
	buf += 2;
242

243
244
245
246
247
	/* 0 <= q < 100 */
	*((u16 *)buf) = decpair[q];
	buf += 2;
	return buf;
}
248

249
static noinline_for_stack
250
251
char *put_dec(char *buf, unsigned long long n)
{
252
253
254
255
256
257
	if (n >= 100*1000*1000)
		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
	/* 1 <= n <= 1.6e11 */
	if (n >= 100*1000*1000)
		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
	/* 1 <= n < 1e8 */
258
	return put_dec_trunc8(buf, n);
259
}
260

261
#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
262

263
264
static void
put_dec_full4(char *buf, unsigned r)
265
{
266
267
268
269
270
271
272
273
	unsigned q;

	/* 0 <= r < 10^4 */
	q = (r * 0x147b) >> 19;
	*((u16 *)buf) = decpair[r - 100*q];
	buf += 2;
	/* 0 <= q < 100 */
	*((u16 *)buf) = decpair[q];
274
275
276
277
278
279
280
}

/*
 * Call put_dec_full4 on x % 10000, return x / 10000.
 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
 * holds for all x < 1,128,869,999.  The largest value this
 * helper will ever be asked to convert is 1,125,520,955.
281
 * (second call in the put_dec code, assuming n is all-ones).
282
 */
283
static noinline_for_stack
284
285
286
287
288
289
unsigned put_dec_helper4(char *buf, unsigned x)
{
        uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;

        put_dec_full4(buf, x - q * 10000);
        return q;
290
291
}

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Based on code by Douglas W. Jones found at
 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
 * (with permission from the author).
 * Performs no 64-bit division and hence should be fast on 32-bit machines.
 */
static
char *put_dec(char *buf, unsigned long long n)
{
	uint32_t d3, d2, d1, q, h;

	if (n < 100*1000*1000)
		return put_dec_trunc8(buf, n);

	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
	h   = (n >> 32);
	d2  = (h      ) & 0xffff;
	d3  = (h >> 16); /* implicit "& 0xffff" */

310
311
	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
312
	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
313
314
315
316
317
318
319
	q = put_dec_helper4(buf, q);

	q += 7671 * d3 + 9496 * d2 + 6 * d1;
	q = put_dec_helper4(buf+4, q);

	q += 4749 * d3 + 42 * d2;
	q = put_dec_helper4(buf+8, q);
320

321
322
323
324
325
	q += 281 * d3;
	buf += 12;
	if (q)
		buf = put_dec_trunc8(buf, q);
	else while (buf[-1] == '0')
326
327
328
329
330
331
332
		--buf;

	return buf;
}

#endif

333
334
335
336
337
338
/*
 * Convert passed number to decimal string.
 * Returns the length of string.  On buffer overflow, returns 0.
 *
 * If speed is not important, use snprintf(). It's easy to read the code.
 */
339
int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
340
{
341
342
	/* put_dec requires 2-byte alignment of the buffer. */
	char tmp[sizeof(num) * 3] __aligned(2);
343
344
	int idx, len;

345
346
347
348
349
350
351
	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
	if (num <= 9) {
		tmp[0] = '0' + num;
		len = 1;
	} else {
		len = put_dec(tmp, num) - tmp;
	}
352

353
	if (len > size || width > size)
354
		return 0;
355
356
357
358
359
360
361
362
363

	if (width > len) {
		width = width - len;
		for (idx = 0; idx < width; idx++)
			buf[idx] = ' ';
	} else {
		width = 0;
	}

364
	for (idx = 0; idx < len; ++idx)
365
366
367
		buf[idx + width] = tmp[len - idx - 1];

	return len + width;
368
369
}

370
#define SIGN	1		/* unsigned/signed, must be 1 */
371
#define LEFT	2		/* left justified */
Linus Torvalds's avatar
Linus Torvalds committed
372
373
#define PLUS	4		/* show plus */
#define SPACE	8		/* space if plus */
374
#define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
375
376
#define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
#define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
Linus Torvalds's avatar
Linus Torvalds committed
377

378
379
enum format_type {
	FORMAT_TYPE_NONE, /* Just a string part */
380
	FORMAT_TYPE_WIDTH,
381
382
383
384
385
386
387
388
389
	FORMAT_TYPE_PRECISION,
	FORMAT_TYPE_CHAR,
	FORMAT_TYPE_STR,
	FORMAT_TYPE_PTR,
	FORMAT_TYPE_PERCENT_CHAR,
	FORMAT_TYPE_INVALID,
	FORMAT_TYPE_LONG_LONG,
	FORMAT_TYPE_ULONG,
	FORMAT_TYPE_LONG,
390
391
	FORMAT_TYPE_UBYTE,
	FORMAT_TYPE_BYTE,
392
393
394
395
396
397
398
399
400
	FORMAT_TYPE_USHORT,
	FORMAT_TYPE_SHORT,
	FORMAT_TYPE_UINT,
	FORMAT_TYPE_INT,
	FORMAT_TYPE_SIZE_T,
	FORMAT_TYPE_PTRDIFF
};

struct printf_spec {
401
402
403
404
405
406
	unsigned int	type:8;		/* format_type enum */
	signed int	field_width:24;	/* width of output field */
	unsigned int	flags:8;	/* flags to number() */
	unsigned int	base:8;		/* number base, 8, 10 or 16 only */
	signed int	precision:16;	/* # of digits/chars */
} __packed;
407
408
#define FIELD_WIDTH_MAX ((1 << 23) - 1)
#define PRECISION_MAX ((1 << 15) - 1)
409

410
411
412
static noinline_for_stack
char *number(char *buf, char *end, unsigned long long num,
	     struct printf_spec spec)
Linus Torvalds's avatar
Linus Torvalds committed
413
{
414
415
	/* put_dec requires 2-byte alignment of the buffer. */
	char tmp[3 * sizeof(num)] __aligned(2);
416
417
	char sign;
	char locase;
418
	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds's avatar
Linus Torvalds committed
419
	int i;
420
	bool is_zero = num == 0LL;
421
422
	int field_width = spec.field_width;
	int precision = spec.precision;
Linus Torvalds's avatar
Linus Torvalds committed
423

424
425
	BUILD_BUG_ON(sizeof(struct printf_spec) != 8);

426
427
	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
	 * produces same digits or (maybe lowercased) letters */
428
429
430
	locase = (spec.flags & SMALL);
	if (spec.flags & LEFT)
		spec.flags &= ~ZEROPAD;
Linus Torvalds's avatar
Linus Torvalds committed
431
	sign = 0;
432
	if (spec.flags & SIGN) {
433
		if ((signed long long)num < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
434
			sign = '-';
435
			num = -(signed long long)num;
436
			field_width--;
437
		} else if (spec.flags & PLUS) {
Linus Torvalds's avatar
Linus Torvalds committed
438
			sign = '+';
439
			field_width--;
440
		} else if (spec.flags & SPACE) {
Linus Torvalds's avatar
Linus Torvalds committed
441
			sign = ' ';
442
			field_width--;
Linus Torvalds's avatar
Linus Torvalds committed
443
444
		}
	}
445
	if (need_pfx) {
446
		if (spec.base == 16)
447
			field_width -= 2;
448
		else if (!is_zero)
449
			field_width--;
Linus Torvalds's avatar
Linus Torvalds committed
450
	}
451
452

	/* generate full string in tmp[], in reverse order */
Linus Torvalds's avatar
Linus Torvalds committed
453
	i = 0;
454
	if (num < spec.base)
455
		tmp[i++] = hex_asc_upper[num] | locase;
456
457
	else if (spec.base != 10) { /* 8 or 16 */
		int mask = spec.base - 1;
458
		int shift = 3;
459
460
461

		if (spec.base == 16)
			shift = 4;
462
		do {
463
			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
464
465
			num >>= shift;
		} while (num);
466
467
468
	} else { /* base 10 */
		i = put_dec(tmp, num) - tmp;
	}
469
470

	/* printing 100 using %2d gives "100", not "00" */
471
472
	if (i > precision)
		precision = i;
473
	/* leading space padding */
474
	field_width -= precision;
475
	if (!(spec.flags & (ZEROPAD | LEFT))) {
476
		while (--field_width >= 0) {
477
			if (buf < end)
Linus Torvalds's avatar
Linus Torvalds committed
478
479
480
481
				*buf = ' ';
			++buf;
		}
	}
482
	/* sign */
Linus Torvalds's avatar
Linus Torvalds committed
483
	if (sign) {
484
		if (buf < end)
Linus Torvalds's avatar
Linus Torvalds committed
485
486
487
			*buf = sign;
		++buf;
	}
488
489
	/* "0x" / "0" prefix */
	if (need_pfx) {
490
491
492
493
494
		if (spec.base == 16 || !is_zero) {
			if (buf < end)
				*buf = '0';
			++buf;
		}
495
		if (spec.base == 16) {
496
			if (buf < end)
497
				*buf = ('X' | locase);
Linus Torvalds's avatar
Linus Torvalds committed
498
499
500
			++buf;
		}
	}
501
	/* zero or space padding */
502
	if (!(spec.flags & LEFT)) {
503
504
		char c = ' ' + (spec.flags & ZEROPAD);
		BUILD_BUG_ON(' ' + ZEROPAD != '0');
505
		while (--field_width >= 0) {
506
			if (buf < end)
Linus Torvalds's avatar
Linus Torvalds committed
507
508
509
510
				*buf = c;
			++buf;
		}
	}
511
	/* hmm even more zero padding? */
512
	while (i <= --precision) {
513
		if (buf < end)
Linus Torvalds's avatar
Linus Torvalds committed
514
515
516
			*buf = '0';
		++buf;
	}
517
518
	/* actual digits of result */
	while (--i >= 0) {
519
		if (buf < end)
Linus Torvalds's avatar
Linus Torvalds committed
520
521
522
			*buf = tmp[i];
		++buf;
	}
523
	/* trailing space padding */
524
	while (--field_width >= 0) {
525
		if (buf < end)
Linus Torvalds's avatar
Linus Torvalds committed
526
527
528
			*buf = ' ';
		++buf;
	}
529

Linus Torvalds's avatar
Linus Torvalds committed
530
531
532
	return buf;
}

533
534
535
536
537
538
539
540
541
542
543
544
545
546
static noinline_for_stack
char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
{
	struct printf_spec spec;

	spec.type = FORMAT_TYPE_PTR;
	spec.field_width = 2 + 2 * size;	/* 0x + hex */
	spec.flags = SPECIAL | SMALL | ZEROPAD;
	spec.base = 16;
	spec.precision = -1;

	return number(buf, end, num, spec);
}

547
static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
{
	size_t size;
	if (buf >= end)	/* nowhere to put anything */
		return;
	size = end - buf;
	if (size <= spaces) {
		memset(buf, ' ', size);
		return;
	}
	if (len) {
		if (len > size - spaces)
			len = size - spaces;
		memmove(buf + spaces, buf, len);
	}
	memset(buf, ' ', spaces);
}

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * Handle field width padding for a string.
 * @buf: current buffer position
 * @n: length of string
 * @end: end of output buffer
 * @spec: for field width and flags
 * Returns: new buffer position after padding.
 */
static noinline_for_stack
char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
{
	unsigned spaces;

	if (likely(n >= spec.field_width))
		return buf;
	/* we want to pad the sucker */
	spaces = spec.field_width - n;
	if (!(spec.flags & LEFT)) {
		move_right(buf - n, end, n, spaces);
		return buf + spaces;
	}
	while (spaces--) {
		if (buf < end)
			*buf = ' ';
		++buf;
	}
	return buf;
}

594
595
596
static noinline_for_stack
char *string(char *buf, char *end, const char *s, struct printf_spec spec)
{
597
598
	int len = 0;
	size_t lim = spec.precision;
599
600
601
602

	if ((unsigned long)s < PAGE_SIZE)
		s = "(null)";

603
604
605
606
	while (lim--) {
		char c = *s++;
		if (!c)
			break;
607
		if (buf < end)
608
			*buf = c;
609
		++buf;
610
		++len;
611
	}
612
	return widen_string(buf, len, end, spec);
613
614
}

615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
static noinline_for_stack
char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
		  const char *fmt)
{
	const char *array[4], *s;
	const struct dentry *p;
	int depth;
	int i, n;

	switch (fmt[1]) {
		case '2': case '3': case '4':
			depth = fmt[1] - '0';
			break;
		default:
			depth = 1;
	}

	rcu_read_lock();
	for (i = 0; i < depth; i++, d = p) {
634
635
		p = READ_ONCE(d->d_parent);
		array[i] = READ_ONCE(d->d_name.name);
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
		if (p == d) {
			if (i)
				array[i] = "";
			i++;
			break;
		}
	}
	s = array[--i];
	for (n = 0; n != spec.precision; n++, buf++) {
		char c = *s++;
		if (!c) {
			if (!i)
				break;
			c = '/';
			s = array[--i];
		}
		if (buf < end)
			*buf = c;
	}
	rcu_read_unlock();
656
	return widen_string(buf, n, end, spec);
657
658
}

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#ifdef CONFIG_BLOCK
static noinline_for_stack
char *bdev_name(char *buf, char *end, struct block_device *bdev,
		struct printf_spec spec, const char *fmt)
{
	struct gendisk *hd = bdev->bd_disk;
	
	buf = string(buf, end, hd->disk_name, spec);
	if (bdev->bd_part->partno) {
		if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
			if (buf < end)
				*buf = 'p';
			buf++;
		}
		buf = number(buf, end, bdev->bd_part->partno, spec);
	}
	return buf;
}
#endif

679
680
static noinline_for_stack
char *symbol_string(char *buf, char *end, void *ptr,
681
		    struct printf_spec spec, const char *fmt)
682
{
683
	unsigned long value;
684
685
#ifdef CONFIG_KALLSYMS
	char sym[KSYM_SYMBOL_LEN];
686
687
688
689
690
691
692
693
#endif

	if (fmt[1] == 'R')
		ptr = __builtin_extract_return_addr(ptr);
	value = (unsigned long)ptr;

#ifdef CONFIG_KALLSYMS
	if (*fmt == 'B')
694
		sprint_backtrace(sym, value);
695
	else if (*fmt != 'f' && *fmt != 's')
696
697
		sprint_symbol(sym, value);
	else
698
		sprint_symbol_no_offset(sym, value);
699

700
	return string(buf, end, sym, spec);
701
#else
702
	return special_hex_number(buf, end, value, sizeof(void *));
703
704
705
#endif
}

706
707
708
709
710
static const struct printf_spec default_str_spec = {
	.field_width = -1,
	.precision = -1,
};

711
712
713
714
715
716
static const struct printf_spec default_flag_spec = {
	.base = 16,
	.precision = -1,
	.flags = SPECIAL | SMALL,
};

717
718
719
720
721
static const struct printf_spec default_dec_spec = {
	.base = 10,
	.precision = -1,
};

722
723
724
static noinline_for_stack
char *resource_string(char *buf, char *end, struct resource *res,
		      struct printf_spec spec, const char *fmt)
725
726
{
#ifndef IO_RSRC_PRINTK_SIZE
727
#define IO_RSRC_PRINTK_SIZE	6
728
729
730
#endif

#ifndef MEM_RSRC_PRINTK_SIZE
731
#define MEM_RSRC_PRINTK_SIZE	10
732
#endif
733
	static const struct printf_spec io_spec = {
734
		.base = 16,
735
		.field_width = IO_RSRC_PRINTK_SIZE,
736
737
738
		.precision = -1,
		.flags = SPECIAL | SMALL | ZEROPAD,
	};
739
740
741
742
743
744
	static const struct printf_spec mem_spec = {
		.base = 16,
		.field_width = MEM_RSRC_PRINTK_SIZE,
		.precision = -1,
		.flags = SPECIAL | SMALL | ZEROPAD,
	};
745
746
747
748
749
750
	static const struct printf_spec bus_spec = {
		.base = 16,
		.field_width = 2,
		.precision = -1,
		.flags = SMALL | ZEROPAD,
	};
751
	static const struct printf_spec str_spec = {
752
753
754
755
		.field_width = -1,
		.precision = 10,
		.flags = LEFT,
	};
756
757
758
759
760

	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
#define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
#define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
Bjorn Helgaas's avatar
Bjorn Helgaas committed
761
#define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
762
763
764
765
#define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];

766
	char *p = sym, *pend = sym + sizeof(sym);
767
	int decode = (fmt[0] == 'R') ? 1 : 0;
768
	const struct printf_spec *specp;
769
770

	*p++ = '[';
771
	if (res->flags & IORESOURCE_IO) {
772
		p = string(p, pend, "io  ", str_spec);
773
774
		specp = &io_spec;
	} else if (res->flags & IORESOURCE_MEM) {
775
		p = string(p, pend, "mem ", str_spec);
776
777
		specp = &mem_spec;
	} else if (res->flags & IORESOURCE_IRQ) {
778
		p = string(p, pend, "irq ", str_spec);
779
		specp = &default_dec_spec;
780
	} else if (res->flags & IORESOURCE_DMA) {
781
		p = string(p, pend, "dma ", str_spec);
782
		specp = &default_dec_spec;
783
784
785
	} else if (res->flags & IORESOURCE_BUS) {
		p = string(p, pend, "bus ", str_spec);
		specp = &bus_spec;
786
	} else {
787
		p = string(p, pend, "??? ", str_spec);
788
		specp = &mem_spec;
789
		decode = 0;
790
	}
791
792
793
794
795
796
797
798
799
	if (decode && res->flags & IORESOURCE_UNSET) {
		p = string(p, pend, "size ", str_spec);
		p = number(p, pend, resource_size(res), *specp);
	} else {
		p = number(p, pend, res->start, *specp);
		if (res->start != res->end) {
			*p++ = '-';
			p = number(p, pend, res->end, *specp);
		}
800
	}
801
	if (decode) {
802
803
804
805
		if (res->flags & IORESOURCE_MEM_64)
			p = string(p, pend, " 64bit", str_spec);
		if (res->flags & IORESOURCE_PREFETCH)
			p = string(p, pend, " pref", str_spec);
Bjorn Helgaas's avatar
Bjorn Helgaas committed
806
807
		if (res->flags & IORESOURCE_WINDOW)
			p = string(p, pend, " window", str_spec);
808
809
		if (res->flags & IORESOURCE_DISABLED)
			p = string(p, pend, " disabled", str_spec);
810
811
	} else {
		p = string(p, pend, " flags ", str_spec);
812
		p = number(p, pend, res->flags, default_flag_spec);
813
	}
814
	*p++ = ']';
815
	*p = '\0';
816

817
	return string(buf, end, sym, spec);
818
819
}

820
821
822
823
static noinline_for_stack
char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
		 const char *fmt)
{
824
	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
				   negative value, fallback to the default */
	char separator;

	if (spec.field_width == 0)
		/* nothing to print */
		return buf;

	if (ZERO_OR_NULL_PTR(addr))
		/* NULL pointer */
		return string(buf, end, NULL, spec);

	switch (fmt[1]) {
	case 'C':
		separator = ':';
		break;
	case 'D':
		separator = '-';
		break;
	case 'N':
		separator = 0;
		break;
	default:
		separator = ' ';
		break;
	}

	if (spec.field_width > 0)
		len = min_t(int, spec.field_width, 64);

854
855
856
857
858
859
860
	for (i = 0; i < len; ++i) {
		if (buf < end)
			*buf = hex_asc_hi(addr[i]);
		++buf;
		if (buf < end)
			*buf = hex_asc_lo(addr[i]);
		++buf;
861

862
863
864
865
866
		if (separator && i != len - 1) {
			if (buf < end)
				*buf = separator;
			++buf;
		}
867
868
869
870
871
	}

	return buf;
}

872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
static noinline_for_stack
char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
		    struct printf_spec spec, const char *fmt)
{
	const int CHUNKSZ = 32;
	int nr_bits = max_t(int, spec.field_width, 0);
	int i, chunksz;
	bool first = true;

	/* reused to print numbers */
	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };

	chunksz = nr_bits & (CHUNKSZ - 1);
	if (chunksz == 0)
		chunksz = CHUNKSZ;

	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
	for (; i >= 0; i -= CHUNKSZ) {
		u32 chunkmask, val;
		int word, bit;

		chunkmask = ((1ULL << chunksz) - 1);
		word = i / BITS_PER_LONG;
		bit = i % BITS_PER_LONG;
		val = (bitmap[word] >> bit) & chunkmask;

		if (!first) {
			if (buf < end)
				*buf = ',';
			buf++;
		}
		first = false;

		spec.field_width = DIV_ROUND_UP(chunksz, 4);
		buf = number(buf, end, val, spec);

		chunksz = CHUNKSZ;
	}
	return buf;
}

static noinline_for_stack
char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
			 struct printf_spec spec, const char *fmt)
{
	int nr_bits = max_t(int, spec.field_width, 0);
	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
	int cur, rbot, rtop;
	bool first = true;

	rbot = cur = find_first_bit(bitmap, nr_bits);
	while (cur < nr_bits) {
		rtop = cur;
		cur = find_next_bit(bitmap, nr_bits, cur + 1);
		if (cur < nr_bits && cur <= rtop + 1)
			continue;

		if (!first) {
			if (buf < end)
				*buf = ',';
			buf++;
		}
		first = false;

936
		buf = number(buf, end, rbot, default_dec_spec);
937
938
939
940
941
		if (rbot < rtop) {
			if (buf < end)
				*buf = '-';
			buf++;

942
			buf = number(buf, end, rtop, default_dec_spec);
943
944
945
946
947
948
949
		}

		rbot = cur;
	}
	return buf;
}

950
951
952
static noinline_for_stack
char *mac_address_string(char *buf, char *end, u8 *addr,
			 struct printf_spec spec, const char *fmt)
953
{
954
	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
955
956
	char *p = mac_addr;
	int i;
957
	char separator;
958
	bool reversed = false;
959

960
961
	switch (fmt[1]) {
	case 'F':
962
		separator = '-';
963
964
965
966
967
968
969
		break;

	case 'R':
		reversed = true;
		/* fall through */

	default:
970
		separator = ':';
971
		break;
972
	}
973
974

	for (i = 0; i < 6; i++) {
975
976
977
978
979
		if (reversed)
			p = hex_byte_pack(p, addr[5 - i]);
		else
			p = hex_byte_pack(p, addr[i]);

980
		if (fmt[0] == 'M' && i != 5)
981
			*p++ = separator;
982
983
984
	}
	*p = '\0';

985
	return string(buf, end, mac_addr, spec);
986
987
}

988
989
static noinline_for_stack
char *ip4_string(char *p, const u8 *addr, const char *fmt)
990
991
{
	int i;
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
	bool leading_zeros = (fmt[0] == 'i');
	int index;
	int step;

	switch (fmt[2]) {
	case 'h':
#ifdef __BIG_ENDIAN
		index = 0;
		step = 1;
#else
		index = 3;
		step = -1;
#endif
		break;
	case 'l':
		index = 3;
		step = -1;
		break;
	case 'n':
	case 'b':
	default:
		index = 0;
		step = 1;
		break;
	}
1017
	for (i = 0; i < 4; i++) {
1018
		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
1019
		int digits = put_dec_trunc8(temp, addr[index]) - temp;
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
		if (leading_zeros) {
			if (digits < 3)
				*p++ = '0';
			if (digits < 2)
				*p++ = '0';
		}
		/* reverse the digits in the quad */
		while (digits--)
			*p++ = temp[digits];
		if (i < 3)
			*p++ = '.';
1031
		index += step;
1032
1033
	}
	*p = '\0';
1034

1035
1036
1037
	return p;
}

1038
1039
static noinline_for_stack
char *ip6_compressed_string(char *p, const char *addr)
1040
{
1041
	int i, j, range;
1042
1043
1044
1045
	unsigned char zerolength[8];
	int longest = 1;
	int colonpos = -1;
	u16 word;
1046
	u8 hi, lo;
1047
	bool needcolon = false;
1048
1049
1050
1051
1052
1053
	bool useIPv4;
	struct in6_addr in6;

	memcpy(&in6, addr, sizeof(struct in6_addr));

	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064

	memset(zerolength, 0, sizeof(zerolength));

	if (useIPv4)
		range = 6;
	else
		range = 8;

	/* find position of longest 0 run */
	for (i = 0; i < range; i++) {
		for (j = i; j < range; j++) {
1065
			if (in6.s6_addr16[j] != 0)
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
				break;
			zerolength[i]++;
		}
	}
	for (i = 0; i < range; i++) {
		if (zerolength[i] > longest) {
			longest = zerolength[i];
			colonpos = i;
		}
	}
1076
1077
	if (longest == 1)		/* don't compress a single 0 */
		colonpos = -1;
1078

1079
1080