1 2 #include <dragonstub/dragonstub.h> 3 #include <dragonstub/linux/stdarg.h> 4 #include <dragonstub/bug.h> 5 #include <dragonstub/minmax.h> 6 #include <dragonstub/limits.h> 7 #include <dragonstub/compiler_types.h> 8 #include <dragonstub/linux/err.h> 9 #include <dragonstub/linux/byteorder.h> 10 #include <dragonstub/linux/div64.h> 11 12 13 struct printf_spec { 14 unsigned int type : 8; /* format_type enum */ 15 signed int field_width : 24; /* width of output field */ 16 unsigned int flags : 8; /* flags to number() */ 17 unsigned int base : 8; /* number base, 8, 10 or 16 only */ 18 signed int precision : 16; /* # of digits/chars */ 19 } __packed; 20 21 #define SIGN 1 /* unsigned/signed, must be 1 */ 22 #define LEFT 2 /* left justified */ 23 #define PLUS 4 /* show plus */ 24 #define SPACE 8 /* space if plus */ 25 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 26 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 27 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 28 29 static_assert(SIGN == 1); 30 static_assert(ZEROPAD == ('0' - ' ')); 31 static_assert(SMALL == ('a' ^ 'A')); 32 33 enum format_type { 34 FORMAT_TYPE_NONE, /* Just a string part */ 35 FORMAT_TYPE_WIDTH, 36 FORMAT_TYPE_PRECISION, 37 FORMAT_TYPE_CHAR, 38 FORMAT_TYPE_STR, 39 FORMAT_TYPE_PTR, 40 FORMAT_TYPE_PERCENT_CHAR, 41 FORMAT_TYPE_INVALID, 42 FORMAT_TYPE_LONG_LONG, 43 FORMAT_TYPE_ULONG, 44 FORMAT_TYPE_LONG, 45 FORMAT_TYPE_UBYTE, 46 FORMAT_TYPE_BYTE, 47 FORMAT_TYPE_USHORT, 48 FORMAT_TYPE_SHORT, 49 FORMAT_TYPE_UINT, 50 FORMAT_TYPE_INT, 51 FORMAT_TYPE_SIZE_T, 52 FORMAT_TYPE_PTRDIFF 53 }; 54 55 static noinline_for_stack int skip_atoi(const char **s) 56 { 57 int i = 0; 58 59 do { 60 i = i * 10 + *((*s)++) - '0'; 61 } while (isdigit(**s)); 62 63 return i; 64 } 65 66 /* 67 * Decimal conversion is by far the most typical, and is used for 68 * /proc and /sys data. This directly impacts e.g. top performance 69 * with many processes running. We optimize it for speed by emitting 70 * two characters at a time, using a 200 byte lookup table. This 71 * roughly halves the number of multiplications compared to computing 72 * the digits one at a time. Implementation strongly inspired by the 73 * previous version, which in turn used ideas described at 74 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 75 * from the author, Douglas W. Jones). 76 * 77 * It turns out there is precisely one 26 bit fixed-point 78 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 79 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 80 * range happens to be somewhat larger (x <= 1073741898), but that's 81 * irrelevant for our purpose. 82 * 83 * For dividing a number in the range [10^4, 10^6-1] by 100, we still 84 * need a 32x32->64 bit multiply, so we simply use the same constant. 85 * 86 * For dividing a number in the range [100, 10^4-1] by 100, there are 87 * several options. The simplest is (x * 0x147b) >> 19, which is valid 88 * for all x <= 43698. 89 */ 90 91 static const u16 decpair[100] = { 92 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 93 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 94 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 95 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 96 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 97 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 98 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 99 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 100 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 101 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 102 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 103 #undef _ 104 }; 105 106 /* 107 * This will print a single '0' even if r == 0, since we would 108 * immediately jump to out_r where two 0s would be written but only 109 * one of them accounted for in buf. This is needed by ip4_string 110 * below. All other callers pass a non-zero value of r. 111 */ 112 static noinline_for_stack char *put_dec_trunc8(char *buf, unsigned r) 113 { 114 unsigned q; 115 116 /* 1 <= r < 10^8 */ 117 if (r < 100) 118 goto out_r; 119 120 /* 100 <= r < 10^8 */ 121 q = (r * (u64)0x28f5c29) >> 32; 122 *((u16 *)buf) = decpair[r - 100 * q]; 123 buf += 2; 124 125 /* 1 <= q < 10^6 */ 126 if (q < 100) 127 goto out_q; 128 129 /* 100 <= q < 10^6 */ 130 r = (q * (u64)0x28f5c29) >> 32; 131 *((u16 *)buf) = decpair[q - 100 * r]; 132 buf += 2; 133 134 /* 1 <= r < 10^4 */ 135 if (r < 100) 136 goto out_r; 137 138 /* 100 <= r < 10^4 */ 139 q = (r * 0x147b) >> 19; 140 *((u16 *)buf) = decpair[r - 100 * q]; 141 buf += 2; 142 out_q: 143 /* 1 <= q < 100 */ 144 r = q; 145 out_r: 146 /* 1 <= r < 100 */ 147 *((u16 *)buf) = decpair[r]; 148 buf += r < 10 ? 1 : 2; 149 return buf; 150 } 151 152 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 153 static noinline_for_stack char *put_dec_full8(char *buf, unsigned r) 154 { 155 unsigned q; 156 157 /* 0 <= r < 10^8 */ 158 q = (r * (u64)0x28f5c29) >> 32; 159 *((u16 *)buf) = decpair[r - 100 * q]; 160 buf += 2; 161 162 /* 0 <= q < 10^6 */ 163 r = (q * (u64)0x28f5c29) >> 32; 164 *((u16 *)buf) = decpair[q - 100 * r]; 165 buf += 2; 166 167 /* 0 <= r < 10^4 */ 168 q = (r * 0x147b) >> 19; 169 *((u16 *)buf) = decpair[r - 100 * q]; 170 buf += 2; 171 172 /* 0 <= q < 100 */ 173 *((u16 *)buf) = decpair[q]; 174 buf += 2; 175 return buf; 176 } 177 178 static noinline_for_stack char *put_dec(char *buf, unsigned long long n) 179 { 180 if (n >= 100 * 1000 * 1000) 181 buf = put_dec_full8(buf, do_div(n, 100 * 1000 * 1000)); 182 /* 1 <= n <= 1.6e11 */ 183 if (n >= 100 * 1000 * 1000) 184 buf = put_dec_full8(buf, do_div(n, 100 * 1000 * 1000)); 185 /* 1 <= n < 1e8 */ 186 return put_dec_trunc8(buf, n); 187 } 188 189 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 190 191 static void 192 put_dec_full4(char *buf, unsigned r) 193 { 194 unsigned q; 195 196 /* 0 <= r < 10^4 */ 197 q = (r * 0x147b) >> 19; 198 *((u16 *)buf) = decpair[r - 100*q]; 199 buf += 2; 200 /* 0 <= q < 100 */ 201 *((u16 *)buf) = decpair[q]; 202 } 203 204 /* 205 * Call put_dec_full4 on x % 10000, return x / 10000. 206 * The approximation x/10000 == (x * 0x346DC5D7) >> 43 207 * holds for all x < 1,128,869,999. The largest value this 208 * helper will ever be asked to convert is 1,125,520,955. 209 * (second call in the put_dec code, assuming n is all-ones). 210 */ 211 static noinline_for_stack 212 unsigned put_dec_helper4(char *buf, unsigned x) 213 { 214 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 215 216 put_dec_full4(buf, x - q * 10000); 217 return q; 218 } 219 220 /* Based on code by Douglas W. Jones found at 221 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 222 * (with permission from the author). 223 * Performs no 64-bit division and hence should be fast on 32-bit machines. 224 */ 225 static 226 char *put_dec(char *buf, unsigned long long n) 227 { 228 uint32_t d3, d2, d1, q, h; 229 230 if (n < 100*1000*1000) 231 return put_dec_trunc8(buf, n); 232 233 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 234 h = (n >> 32); 235 d2 = (h ) & 0xffff; 236 d3 = (h >> 16); /* implicit "& 0xffff" */ 237 238 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 239 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 240 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 241 q = put_dec_helper4(buf, q); 242 243 q += 7671 * d3 + 9496 * d2 + 6 * d1; 244 q = put_dec_helper4(buf+4, q); 245 246 q += 4749 * d3 + 42 * d2; 247 q = put_dec_helper4(buf+8, q); 248 249 q += 281 * d3; 250 buf += 12; 251 if (q) 252 buf = put_dec_trunc8(buf, q); 253 else while (buf[-1] == '0') 254 --buf; 255 256 return buf; 257 } 258 259 #endif 260 261 #define FIELD_WIDTH_MAX ((1 << 23) - 1) 262 #define PRECISION_MAX ((1 << 15) - 1) 263 264 static void set_field_width(struct printf_spec *spec, int width) 265 { 266 spec->field_width = width; 267 if (WARN_ONCE(spec->field_width != width, "field width %d too large", 268 width)) { 269 spec->field_width = 270 clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 271 } 272 } 273 274 static void set_precision(struct printf_spec *spec, int prec) 275 { 276 spec->precision = prec; 277 if (WARN_ONCE(spec->precision != prec, "precision %d too large", 278 prec)) { 279 spec->precision = clamp(prec, 0, PRECISION_MAX); 280 } 281 } 282 283 static noinline_for_stack char * 284 number(char *buf, char *end, unsigned long long num, struct printf_spec spec) 285 { 286 /* put_dec requires 2-byte alignment of the buffer. */ 287 char tmp[3 * sizeof(num)] __aligned(2); 288 char sign; 289 char locase; 290 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 291 int i; 292 bool is_zero = num == 0LL; 293 int field_width = spec.field_width; 294 int precision = spec.precision; 295 296 /* locase = 0 or 0x20. ORing digits or letters with 'locase' 297 * produces same digits or (maybe lowercased) letters */ 298 locase = (spec.flags & SMALL); 299 if (spec.flags & LEFT) 300 spec.flags &= ~ZEROPAD; 301 sign = 0; 302 if (spec.flags & SIGN) { 303 if ((signed long long)num < 0) { 304 sign = '-'; 305 num = -(signed long long)num; 306 field_width--; 307 } else if (spec.flags & PLUS) { 308 sign = '+'; 309 field_width--; 310 } else if (spec.flags & SPACE) { 311 sign = ' '; 312 field_width--; 313 } 314 } 315 if (need_pfx) { 316 if (spec.base == 16) 317 field_width -= 2; 318 else if (!is_zero) 319 field_width--; 320 } 321 322 /* generate full string in tmp[], in reverse order */ 323 i = 0; 324 if (num < spec.base) 325 tmp[i++] = hex_asc_upper[num] | locase; 326 else if (spec.base != 10) { /* 8 or 16 */ 327 int mask = spec.base - 1; 328 int shift = 3; 329 330 if (spec.base == 16) 331 shift = 4; 332 do { 333 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | 334 locase); 335 num >>= shift; 336 } while (num); 337 } else { /* base 10 */ 338 i = put_dec(tmp, num) - tmp; 339 } 340 341 /* printing 100 using %2d gives "100", not "00" */ 342 if (i > precision) 343 precision = i; 344 /* leading space padding */ 345 field_width -= precision; 346 if (!(spec.flags & (ZEROPAD | LEFT))) { 347 while (--field_width >= 0) { 348 if (buf < end) 349 *buf = ' '; 350 ++buf; 351 } 352 } 353 /* sign */ 354 if (sign) { 355 if (buf < end) 356 *buf = sign; 357 ++buf; 358 } 359 /* "0x" / "0" prefix */ 360 if (need_pfx) { 361 if (spec.base == 16 || !is_zero) { 362 if (buf < end) 363 *buf = '0'; 364 ++buf; 365 } 366 if (spec.base == 16) { 367 if (buf < end) 368 *buf = ('X' | locase); 369 ++buf; 370 } 371 } 372 /* zero or space padding */ 373 if (!(spec.flags & LEFT)) { 374 char c = ' ' + (spec.flags & ZEROPAD); 375 376 while (--field_width >= 0) { 377 if (buf < end) 378 *buf = c; 379 ++buf; 380 } 381 } 382 /* hmm even more zero padding? */ 383 while (i <= --precision) { 384 if (buf < end) 385 *buf = '0'; 386 ++buf; 387 } 388 /* actual digits of result */ 389 while (--i >= 0) { 390 if (buf < end) 391 *buf = tmp[i]; 392 ++buf; 393 } 394 /* trailing space padding */ 395 while (--field_width >= 0) { 396 if (buf < end) 397 *buf = ' '; 398 ++buf; 399 } 400 401 return buf; 402 } 403 404 static noinline_for_stack char * 405 special_hex_number(char *buf, char *end, unsigned long long num, int size) 406 { 407 struct printf_spec spec; 408 409 spec.type = FORMAT_TYPE_PTR; 410 spec.field_width = 2 + 2 * size; /* 0x + hex */ 411 spec.flags = SPECIAL | SMALL | ZEROPAD; 412 spec.base = 16; 413 spec.precision = -1; 414 415 return number(buf, end, num, spec); 416 } 417 418 static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 419 { 420 size_t size; 421 if (buf >= end) /* nowhere to put anything */ 422 return; 423 size = end - buf; 424 if (size <= spaces) { 425 memset(buf, ' ', size); 426 return; 427 } 428 if (len) { 429 if (len > size - spaces) 430 len = size - spaces; 431 memmove(buf + spaces, buf, len); 432 } 433 memset(buf, ' ', spaces); 434 } 435 436 /* 437 * Handle field width padding for a string. 438 * @buf: current buffer position 439 * @n: length of string 440 * @end: end of output buffer 441 * @spec: for field width and flags 442 * Returns: new buffer position after padding. 443 */ 444 static noinline_for_stack char *widen_string(char *buf, int n, char *end, 445 struct printf_spec spec) 446 { 447 unsigned spaces; 448 449 if (likely(n >= spec.field_width)) 450 return buf; 451 /* we want to pad the sucker */ 452 spaces = spec.field_width - n; 453 if (!(spec.flags & LEFT)) { 454 move_right(buf - n, end, n, spaces); 455 return buf + spaces; 456 } 457 while (spaces--) { 458 if (buf < end) 459 *buf = ' '; 460 ++buf; 461 } 462 return buf; 463 } 464 465 /* Handle string from a well known address. */ 466 static char *string_nocheck(char *buf, char *end, const char *s, 467 struct printf_spec spec) 468 { 469 int len = 0; 470 int lim = spec.precision; 471 472 while (lim--) { 473 char c = *s++; 474 if (!c) 475 break; 476 if (buf < end) 477 *buf = c; 478 ++buf; 479 ++len; 480 } 481 return widen_string(buf, len, end, spec); 482 } 483 484 /* Be careful: error messages must fit into the given buffer. */ 485 static char *error_string(char *buf, char *end, const char *s, 486 struct printf_spec spec) 487 { 488 /* 489 * Hard limit to avoid a completely insane messages. It actually 490 * works pretty well because most error messages are in 491 * the many pointer format modifiers. 492 */ 493 if (spec.precision == -1) 494 spec.precision = 2 * sizeof(void *); 495 496 return string_nocheck(buf, end, s, spec); 497 } 498 499 /* 500 * Do not call any complex external code here. Nested printk()/vsprintf() 501 * might cause infinite loops. Failures might break printk() and would 502 * be hard to debug. 503 */ 504 static const char *check_pointer_msg(const void *ptr) 505 { 506 if (!ptr) 507 return "(null)"; 508 509 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 510 return "(efault)"; 511 512 return NULL; 513 } 514 515 static int check_pointer(char **buf, char *end, const void *ptr, 516 struct printf_spec spec) 517 { 518 const char *err_msg; 519 520 err_msg = check_pointer_msg(ptr); 521 if (err_msg) { 522 *buf = error_string(*buf, end, err_msg, spec); 523 return -EFAULT; 524 } 525 526 return 0; 527 } 528 529 static noinline_for_stack char *string(char *buf, char *end, const char *s, 530 struct printf_spec spec) 531 { 532 if (check_pointer(&buf, end, s, spec)) 533 return buf; 534 535 return string_nocheck(buf, end, s, spec); 536 } 537 538 static char *pointer_string(char *buf, char *end, const void *ptr, 539 struct printf_spec spec) 540 { 541 spec.base = 16; 542 spec.flags |= SMALL; 543 if (spec.field_width == -1) { 544 spec.field_width = 2 * sizeof(ptr); 545 spec.flags |= ZEROPAD; 546 } 547 548 return number(buf, end, (unsigned long int)ptr, spec); 549 } 550 551 552 static noinline_for_stack 553 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 554 const char *fmt) 555 { 556 int i, len = 1; /* if we pass '%ph[CDN]', field width remains 557 negative value, fallback to the default */ 558 char separator; 559 560 if (spec.field_width == 0) 561 /* nothing to print */ 562 return buf; 563 564 if (check_pointer(&buf, end, addr, spec)) 565 return buf; 566 567 switch (fmt[1]) { 568 case 'C': 569 separator = ':'; 570 break; 571 case 'D': 572 separator = '-'; 573 break; 574 case 'N': 575 separator = 0; 576 break; 577 default: 578 separator = ' '; 579 break; 580 } 581 582 if (spec.field_width > 0) 583 len = min_t(int, spec.field_width, 64); 584 585 for (i = 0; i < len; ++i) { 586 if (buf < end) 587 *buf = hex_asc_hi(addr[i]); 588 ++buf; 589 if (buf < end) 590 *buf = hex_asc_lo(addr[i]); 591 ++buf; 592 593 if (separator && i != len - 1) { 594 if (buf < end) 595 *buf = separator; 596 ++buf; 597 } 598 } 599 600 return buf; 601 } 602 603 static noinline_for_stack 604 char *mac_address_string(char *buf, char *end, u8 *addr, 605 struct printf_spec spec, const char *fmt) 606 { 607 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 608 char *p = mac_addr; 609 int i; 610 char separator; 611 bool reversed = false; 612 613 if (check_pointer(&buf, end, addr, spec)) 614 return buf; 615 616 switch (fmt[1]) { 617 case 'F': 618 separator = '-'; 619 break; 620 621 case 'R': 622 reversed = true; 623 fallthrough; 624 625 default: 626 separator = ':'; 627 break; 628 } 629 630 for (i = 0; i < 6; i++) { 631 if (reversed) 632 p = hex_byte_pack(p, addr[5 - i]); 633 else 634 p = hex_byte_pack(p, addr[i]); 635 636 if (fmt[0] == 'M' && i != 5) 637 *p++ = separator; 638 } 639 *p = '\0'; 640 641 return string_nocheck(buf, end, mac_addr, spec); 642 } 643 644 static char *default_pointer(char *buf, char *end, const void *ptr, 645 struct printf_spec spec) 646 { 647 return pointer_string(buf, end, ptr, spec); 648 } 649 650 651 static char *err_ptr(char *buf, char *end, void *ptr, 652 struct printf_spec spec) 653 { 654 int err = PTR_ERR(ptr); 655 // const char *sym = errname(err); 656 657 // if (sym) 658 // return string_nocheck(buf, end, sym, spec); 659 660 /* 661 * Somebody passed ERR_PTR(-1234) or some other non-existing 662 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to 663 * printing it as its decimal representation. 664 */ 665 spec.flags |= SIGN; 666 spec.base = 10; 667 return number(buf, end, err, spec); 668 } 669 670 /* 671 * Show a '%p' thing. A kernel extension is that the '%p' is followed 672 * by an extra set of alphanumeric characters that are extended format 673 * specifiers. 674 * 675 * Please update scripts/checkpatch.pl when adding/removing conversion 676 * characters. (Search for "check for vsprintf extension"). 677 * 678 * Right now we handle: 679 * 680 * - 'S' For symbolic direct pointers (or function descriptors) with offset 681 * - 's' For symbolic direct pointers (or function descriptors) without offset 682 * - '[Ss]R' as above with __builtin_extract_return_addr() translation 683 * - 'S[R]b' as above with module build ID (for use in backtraces) 684 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 685 * %ps and %pS. Be careful when re-using these specifiers. 686 * - 'B' For backtraced symbolic direct pointers with offset 687 * - 'Bb' as above with module build ID (for use in backtraces) 688 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 689 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 690 * - 'b[l]' For a bitmap, the number of bits is determined by the field 691 * width which must be explicitly specified either as part of the 692 * format string '%32b[l]' or through '%*b[l]', [l] selects 693 * range-list format instead of hex format 694 * - 'M' For a 6-byte MAC address, it prints the address in the 695 * usual colon-separated hex notation 696 * - 'm' For a 6-byte MAC address, it prints the hex address without colons 697 * - 'MF' For a 6-byte MAC FDDI address, it prints the address 698 * with a dash-separated hex notation 699 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 700 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 701 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 702 * IPv6 uses colon separated network-order 16 bit hex with leading 0's 703 * [S][pfs] 704 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 705 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 706 * - 'i' [46] for 'raw' IPv4/IPv6 addresses 707 * IPv6 omits the colons (01020304...0f) 708 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 709 * [S][pfs] 710 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 711 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 712 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 713 * - 'I[6S]c' for IPv6 addresses printed as specified by 714 * https://tools.ietf.org/html/rfc5952 715 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 716 * of the following flags (see string_escape_mem() for the 717 * details): 718 * a - ESCAPE_ANY 719 * c - ESCAPE_SPECIAL 720 * h - ESCAPE_HEX 721 * n - ESCAPE_NULL 722 * o - ESCAPE_OCTAL 723 * p - ESCAPE_NP 724 * s - ESCAPE_SPACE 725 * By default ESCAPE_ANY_NP is used. 726 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 727 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 728 * Options for %pU are: 729 * b big endian lower case hex (default) 730 * B big endian UPPER case hex 731 * l little endian lower case hex 732 * L little endian UPPER case hex 733 * big endian output byte order is: 734 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 735 * little endian output byte order is: 736 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 737 * - 'V' For a struct va_format which contains a format string * and va_list *, 738 * call vsnprintf(->format, *->va_list). 739 * Implements a "recursive vsnprintf". 740 * Do not use this feature without some mechanism to verify the 741 * correctness of the format string and va_list arguments. 742 * - 'K' For a kernel pointer that should be hidden from unprivileged users. 743 * Use only for procfs, sysfs and similar files, not printk(); please 744 * read the documentation (path below) first. 745 * - 'NF' For a netdev_features_t 746 * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value. 747 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 748 * a certain separator (' ' by default): 749 * C colon 750 * D dash 751 * N no separator 752 * The maximum supported length is 64 bytes of the input. Consider 753 * to use print_hex_dump() for the larger input. 754 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 755 * (default assumed to be phys_addr_t, passed by reference) 756 * - 'd[234]' For a dentry name (optionally 2-4 last components) 757 * - 'D[234]' Same as 'd' but for a struct file 758 * - 'g' For block_device name (gendisk + partition number) 759 * - 't[RT][dt][r][s]' For time and date as represented by: 760 * R struct rtc_time 761 * T time64_t 762 * - 'C' For a clock, it prints the name (Common Clock Framework) or address 763 * (legacy clock framework) of the clock 764 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 765 * (legacy clock framework) of the clock 766 * - 'G' For flags to be printed as a collection of symbolic strings that would 767 * construct the specific value. Supported flags given by option: 768 * p page flags (see struct page) given as pointer to unsigned long 769 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 770 * v vma flags (VM_*) given as pointer to unsigned long 771 * - 'OF[fnpPcCF]' For a device tree object 772 * Without any optional arguments prints the full_name 773 * f device node full_name 774 * n device node name 775 * p device node phandle 776 * P device node path spec (name + @unit) 777 * F device node flags 778 * c major compatible string 779 * C full compatible string 780 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 781 * Without an option prints the full name of the node 782 * f full name 783 * P node name, including a possible unit address 784 * - 'x' For printing the address unmodified. Equivalent to "%lx". 785 * Please read the documentation (path below) before using! 786 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 787 * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 788 * or user (u) memory to probe, and: 789 * s a string, equivalent to "%s" on direct vsnprintf() use 790 * 791 * ** When making changes please also update: 792 * Documentation/core-api/printk-formats.rst 793 * 794 * Note: The default behaviour (unadorned %p) is to hash the address, 795 * rendering it useful as a unique identifier. 796 * 797 * There is also a '%pA' format specifier, but it is only intended to be used 798 * from Rust code to format core::fmt::Arguments. Do *not* use it from C. 799 * See rust/kernel/print.rs for details. 800 */ 801 static noinline_for_stack char *pointer(const char *fmt, char *buf, char *end, 802 void *ptr, struct printf_spec spec) 803 { 804 switch (*fmt) { 805 // case 'S': 806 // case 's': 807 // ptr = dereference_symbol_descriptor(ptr); 808 // fallthrough; 809 // case 'B': 810 // return symbol_string(buf, end, ptr, spec, fmt); 811 // case 'R': 812 // case 'r': 813 // return resource_string(buf, end, ptr, spec, fmt); 814 case 'h': 815 return hex_string(buf, end, ptr, spec, fmt); 816 // case 'b': 817 // switch (fmt[1]) { 818 // case 'l': 819 // return bitmap_list_string(buf, end, ptr, spec, fmt); 820 // default: 821 // return bitmap_string(buf, end, ptr, spec, fmt); 822 // } 823 case 'M': /* Colon separated: 00:01:02:03:04:05 */ 824 case 'm': /* Contiguous: 000102030405 */ 825 /* [mM]F (FDDI) */ 826 /* [mM]R (Reverse order; Bluetooth) */ 827 return mac_address_string(buf, end, ptr, spec, fmt); 828 // case 'I': /* Formatted IP supported 829 // * 4: 1.2.3.4 830 // * 6: 0001:0203:...:0708 831 // * 6c: 1::708 or 1::1.2.3.4 832 // */ 833 // case 'i': /* Contiguous: 834 // * 4: 001.002.003.004 835 // * 6: 000102...0f 836 // */ 837 // return ip_addr_string(buf, end, ptr, spec, fmt); 838 // case 'E': 839 // return escaped_string(buf, end, ptr, spec, fmt); 840 // case 'U': 841 // return uuid_string(buf, end, ptr, spec, fmt); 842 // case 'V': 843 // return va_format(buf, end, ptr, spec, fmt); 844 // case 'K': 845 // return restricted_pointer(buf, end, ptr, spec); 846 // case 'N': 847 // return netdev_bits(buf, end, ptr, spec, fmt); 848 // case '4': 849 // return fourcc_string(buf, end, ptr, spec, fmt); 850 // case 'a': 851 // return address_val(buf, end, ptr, spec, fmt); 852 // case 'd': 853 // return dentry_name(buf, end, ptr, spec, fmt); 854 // case 't': 855 // return time_and_date(buf, end, ptr, spec, fmt); 856 // case 'C': 857 // return clock(buf, end, ptr, spec, fmt); 858 // case 'D': 859 // return file_dentry_name(buf, end, ptr, spec, fmt); 860 #ifdef CONFIG_BLOCK 861 case 'g': 862 return bdev_name(buf, end, ptr, spec, fmt); 863 #endif 864 865 // case 'G': 866 // return flags_string(buf, end, ptr, spec, fmt); 867 // case 'O': 868 // return device_node_string(buf, end, ptr, spec, fmt + 1); 869 // case 'f': 870 // return fwnode_string(buf, end, ptr, spec, fmt + 1); 871 // case 'A': 872 // if (!IS_ENABLED(CONFIG_RUST)) { 873 // WARN_ONCE(1, "Please remove %%pA from non-Rust code\n"); 874 // return error_string(buf, end, "(%pA?)", spec); 875 // } 876 // return rust_fmt_argument(buf, end, ptr); 877 case 'x': 878 return pointer_string(buf, end, ptr, spec); 879 case 'e': 880 /* %pe with a non-ERR_PTR gets treated as plain %p */ 881 if (!IS_ERR(ptr)) 882 return default_pointer(buf, end, ptr, spec); 883 return err_ptr(buf, end, ptr, spec); 884 case 'u': 885 case 'k': 886 switch (fmt[1]) { 887 case 's': 888 return string(buf, end, ptr, spec); 889 default: 890 return error_string(buf, end, "(einval)", spec); 891 } 892 default: 893 return default_pointer(buf, end, ptr, spec); 894 } 895 } 896 897 /* 898 * Helper function to decode printf style format. 899 * Each call decode a token from the format and return the 900 * number of characters read (or likely the delta where it wants 901 * to go on the next call). 902 * The decoded token is returned through the parameters 903 * 904 * 'h', 'l', or 'L' for integer fields 905 * 'z' support added 23/7/1999 S.H. 906 * 'z' changed to 'Z' --davidm 1/25/99 907 * 'Z' changed to 'z' --adobriyan 2017-01-25 908 * 't' added for ptrdiff_t 909 * 910 * @fmt: the format string 911 * @type of the token returned 912 * @flags: various flags such as +, -, # tokens.. 913 * @field_width: overwritten width 914 * @base: base of the number (octal, hex, ...) 915 * @precision: precision of a number 916 * @qualifier: qualifier of a number (long, size_t, ...) 917 */ 918 static noinline_for_stack int format_decode(const char *fmt, 919 struct printf_spec *spec) 920 { 921 const char *start = fmt; 922 char qualifier; 923 924 /* we finished early by reading the field width */ 925 if (spec->type == FORMAT_TYPE_WIDTH) { 926 if (spec->field_width < 0) { 927 spec->field_width = -spec->field_width; 928 spec->flags |= LEFT; 929 } 930 spec->type = FORMAT_TYPE_NONE; 931 goto precision; 932 } 933 934 /* we finished early by reading the precision */ 935 if (spec->type == FORMAT_TYPE_PRECISION) { 936 if (spec->precision < 0) 937 spec->precision = 0; 938 939 spec->type = FORMAT_TYPE_NONE; 940 goto qualifier; 941 } 942 943 /* By default */ 944 spec->type = FORMAT_TYPE_NONE; 945 946 for (; *fmt; ++fmt) { 947 if (*fmt == '%') 948 break; 949 } 950 951 /* Return the current non-format string */ 952 if (fmt != start || !*fmt) 953 return fmt - start; 954 955 /* Process flags */ 956 spec->flags = 0; 957 958 while (1) { /* this also skips first '%' */ 959 bool found = true; 960 961 ++fmt; 962 963 switch (*fmt) { 964 case '-': 965 spec->flags |= LEFT; 966 break; 967 case '+': 968 spec->flags |= PLUS; 969 break; 970 case ' ': 971 spec->flags |= SPACE; 972 break; 973 case '#': 974 spec->flags |= SPECIAL; 975 break; 976 case '0': 977 spec->flags |= ZEROPAD; 978 break; 979 default: 980 found = false; 981 } 982 983 if (!found) 984 break; 985 } 986 987 /* get field width */ 988 spec->field_width = -1; 989 990 if (isdigit(*fmt)) 991 spec->field_width = skip_atoi(&fmt); 992 else if (*fmt == '*') { 993 /* it's the next argument */ 994 spec->type = FORMAT_TYPE_WIDTH; 995 return ++fmt - start; 996 } 997 998 precision: 999 /* get the precision */ 1000 spec->precision = -1; 1001 if (*fmt == '.') { 1002 ++fmt; 1003 if (isdigit(*fmt)) { 1004 spec->precision = skip_atoi(&fmt); 1005 if (spec->precision < 0) 1006 spec->precision = 0; 1007 } else if (*fmt == '*') { 1008 /* it's the next argument */ 1009 spec->type = FORMAT_TYPE_PRECISION; 1010 return ++fmt - start; 1011 } 1012 } 1013 1014 qualifier: 1015 /* get the conversion qualifier */ 1016 qualifier = 0; 1017 if (*fmt == 'h' || _tolower(*fmt) == 'l' || *fmt == 'z' || 1018 *fmt == 't') { 1019 qualifier = *fmt++; 1020 if (unlikely(qualifier == *fmt)) { 1021 if (qualifier == 'l') { 1022 qualifier = 'L'; 1023 ++fmt; 1024 } else if (qualifier == 'h') { 1025 qualifier = 'H'; 1026 ++fmt; 1027 } 1028 } 1029 } 1030 1031 /* default base */ 1032 spec->base = 10; 1033 switch (*fmt) { 1034 case 'c': 1035 spec->type = FORMAT_TYPE_CHAR; 1036 return ++fmt - start; 1037 1038 case 's': 1039 spec->type = FORMAT_TYPE_STR; 1040 return ++fmt - start; 1041 1042 case 'p': 1043 spec->type = FORMAT_TYPE_PTR; 1044 return ++fmt - start; 1045 1046 case '%': 1047 spec->type = FORMAT_TYPE_PERCENT_CHAR; 1048 return ++fmt - start; 1049 1050 /* integer number formats - set up the flags and "break" */ 1051 case 'o': 1052 spec->base = 8; 1053 break; 1054 1055 case 'x': 1056 spec->flags |= SMALL; 1057 fallthrough; 1058 1059 case 'X': 1060 spec->base = 16; 1061 break; 1062 1063 case 'd': 1064 case 'i': 1065 spec->flags |= SIGN; 1066 break; 1067 case 'u': 1068 break; 1069 1070 case 'n': 1071 /* 1072 * Since %n poses a greater security risk than 1073 * utility, treat it as any other invalid or 1074 * unsupported format specifier. 1075 */ 1076 fallthrough; 1077 1078 default: 1079 WARN_ONCE(1, 1080 "Please remove unsupported %%%c in format string\n", 1081 *fmt); 1082 spec->type = FORMAT_TYPE_INVALID; 1083 return fmt - start; 1084 } 1085 1086 if (qualifier == 'L') 1087 spec->type = FORMAT_TYPE_LONG_LONG; 1088 else if (qualifier == 'l') { 1089 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 1090 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 1091 } else if (qualifier == 'z') { 1092 spec->type = FORMAT_TYPE_SIZE_T; 1093 } else if (qualifier == 't') { 1094 spec->type = FORMAT_TYPE_PTRDIFF; 1095 } else if (qualifier == 'H') { 1096 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 1097 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 1098 } else if (qualifier == 'h') { 1099 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 1100 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 1101 } else { 1102 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 1103 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 1104 } 1105 1106 return ++fmt - start; 1107 } 1108 1109 /** 1110 * vsnprintf - Format a string and place it in a buffer 1111 * @buf: The buffer to place the result into 1112 * @size: The size of the buffer, including the trailing null space 1113 * @fmt: The format string to use 1114 * @args: Arguments for the format string 1115 * 1116 * This function generally follows C99 vsnprintf, but has some 1117 * extensions and a few limitations: 1118 * 1119 * - ``%n`` is unsupported 1120 * - ``%p*`` is handled by pointer() 1121 * 1122 * See pointer() or Documentation/core-api/printk-formats.rst for more 1123 * extensive description. 1124 * 1125 * **Please update the documentation in both places when making changes** 1126 * 1127 * The return value is the number of characters which would 1128 * be generated for the given input, excluding the trailing 1129 * '\0', as per ISO C99. If you want to have the exact 1130 * number of characters written into @buf as return value 1131 * (not including the trailing '\0'), use vscnprintf(). If the 1132 * return is greater than or equal to @size, the resulting 1133 * string is truncated. 1134 * 1135 * If you're not already dealing with a va_list consider using snprintf(). 1136 */ 1137 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 1138 { 1139 unsigned long long num; 1140 char *str, *end; 1141 struct printf_spec spec = { 0 }; 1142 1143 /* Reject out-of-range values early. Large positive sizes are 1144 used for unknown buffer sizes. */ 1145 if (WARN_ON_ONCE(size > INT_MAX)) 1146 return 0; 1147 1148 str = buf; 1149 end = buf + size; 1150 1151 /* Make sure end is always >= buf */ 1152 if (end < buf) { 1153 end = ((void *)-1); 1154 size = end - buf; 1155 } 1156 1157 while (*fmt) { 1158 const char *old_fmt = fmt; 1159 int read = format_decode(fmt, &spec); 1160 1161 fmt += read; 1162 1163 switch (spec.type) { 1164 case FORMAT_TYPE_NONE: { 1165 int copy = read; 1166 if (str < end) { 1167 if (copy > end - str) 1168 copy = end - str; 1169 memcpy(str, old_fmt, copy); 1170 } 1171 str += read; 1172 break; 1173 } 1174 1175 case FORMAT_TYPE_WIDTH: 1176 set_field_width(&spec, va_arg(args, int)); 1177 break; 1178 1179 case FORMAT_TYPE_PRECISION: 1180 set_precision(&spec, va_arg(args, int)); 1181 break; 1182 1183 case FORMAT_TYPE_CHAR: { 1184 char c; 1185 1186 if (!(spec.flags & LEFT)) { 1187 while (--spec.field_width > 0) { 1188 if (str < end) 1189 *str = ' '; 1190 ++str; 1191 } 1192 } 1193 c = (unsigned char)va_arg(args, int); 1194 if (str < end) 1195 *str = c; 1196 ++str; 1197 while (--spec.field_width > 0) { 1198 if (str < end) 1199 *str = ' '; 1200 ++str; 1201 } 1202 break; 1203 } 1204 1205 case FORMAT_TYPE_STR: 1206 str = string(str, end, va_arg(args, char *), spec); 1207 break; 1208 1209 case FORMAT_TYPE_PTR: 1210 str = pointer(fmt, str, end, va_arg(args, void *), 1211 spec); 1212 while (isalnum(*fmt)) 1213 fmt++; 1214 break; 1215 1216 case FORMAT_TYPE_PERCENT_CHAR: 1217 if (str < end) 1218 *str = '%'; 1219 ++str; 1220 break; 1221 1222 case FORMAT_TYPE_INVALID: 1223 /* 1224 * Presumably the arguments passed gcc's type 1225 * checking, but there is no safe or sane way 1226 * for us to continue parsing the format and 1227 * fetching from the va_list; the remaining 1228 * specifiers and arguments would be out of 1229 * sync. 1230 */ 1231 goto out; 1232 1233 default: 1234 switch (spec.type) { 1235 case FORMAT_TYPE_LONG_LONG: 1236 num = va_arg(args, long long); 1237 break; 1238 case FORMAT_TYPE_ULONG: 1239 num = va_arg(args, unsigned long); 1240 break; 1241 case FORMAT_TYPE_LONG: 1242 num = va_arg(args, long); 1243 break; 1244 case FORMAT_TYPE_SIZE_T: 1245 if (spec.flags & SIGN) 1246 num = va_arg(args, ssize_t); 1247 else 1248 num = va_arg(args, size_t); 1249 break; 1250 case FORMAT_TYPE_PTRDIFF: 1251 num = va_arg(args, ptrdiff_t); 1252 break; 1253 case FORMAT_TYPE_UBYTE: 1254 num = (unsigned char)va_arg(args, int); 1255 break; 1256 case FORMAT_TYPE_BYTE: 1257 num = (signed char)va_arg(args, int); 1258 break; 1259 case FORMAT_TYPE_USHORT: 1260 num = (unsigned short)va_arg(args, int); 1261 break; 1262 case FORMAT_TYPE_SHORT: 1263 num = (short)va_arg(args, int); 1264 break; 1265 case FORMAT_TYPE_INT: 1266 num = (int)va_arg(args, int); 1267 break; 1268 default: 1269 num = va_arg(args, unsigned int); 1270 } 1271 1272 str = number(str, end, num, spec); 1273 } 1274 } 1275 1276 out: 1277 if (size > 0) { 1278 if (str < end) 1279 *str = '\0'; 1280 else 1281 end[-1] = '\0'; 1282 } 1283 1284 /* the trailing null byte doesn't count towards the total */ 1285 return str - buf; 1286 } 1287 1288 /** 1289 * snprintf - Format a string and place it in a buffer 1290 * @buf: The buffer to place the result into 1291 * @size: The size of the buffer, including the trailing null space 1292 * @fmt: The format string to use 1293 * @...: Arguments for the format string 1294 * 1295 * The return value is the number of characters which would be 1296 * generated for the given input, excluding the trailing null, 1297 * as per ISO C99. If the return is greater than or equal to 1298 * @size, the resulting string is truncated. 1299 * 1300 * See the vsnprintf() documentation for format string extensions over C99. 1301 */ 1302 int snprintf(char *buf, size_t size, const char *fmt, ...) 1303 { 1304 va_list args; 1305 int i; 1306 1307 va_start(args, fmt); 1308 i = vsnprintf(buf, size, fmt, args); 1309 va_end(args); 1310 1311 return i; 1312 }