diff options
Diffstat (limited to 'lib/vsprintf.c')
| -rw-r--r-- | lib/vsprintf.c | 352 |
1 files changed, 197 insertions, 155 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c index b235c96167d3..da39c608a28c 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c | |||
| @@ -17,6 +17,7 @@ | |||
| 17 | */ | 17 | */ |
| 18 | 18 | ||
| 19 | #include <stdarg.h> | 19 | #include <stdarg.h> |
| 20 | #include <linux/clk-provider.h> | ||
| 20 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ | 21 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ |
| 21 | #include <linux/types.h> | 22 | #include <linux/types.h> |
| 22 | #include <linux/string.h> | 23 | #include <linux/string.h> |
| @@ -32,6 +33,7 @@ | |||
| 32 | 33 | ||
| 33 | #include <asm/page.h> /* for PAGE_SIZE */ | 34 | #include <asm/page.h> /* for PAGE_SIZE */ |
| 34 | #include <asm/sections.h> /* for dereference_function_descriptor() */ | 35 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
| 36 | #include <asm/byteorder.h> /* cpu_to_le16 */ | ||
| 35 | 37 | ||
| 36 | #include <linux/string_helpers.h> | 38 | #include <linux/string_helpers.h> |
| 37 | #include "kstrtox.h" | 39 | #include "kstrtox.h" |
| @@ -121,142 +123,145 @@ int skip_atoi(const char **s) | |||
| 121 | return i; | 123 | return i; |
| 122 | } | 124 | } |
| 123 | 125 | ||
| 124 | /* Decimal conversion is by far the most typical, and is used | 126 | /* |
| 125 | * for /proc and /sys data. This directly impacts e.g. top performance | 127 | * Decimal conversion is by far the most typical, and is used for |
| 126 | * with many processes running. We optimize it for speed | 128 | * /proc and /sys data. This directly impacts e.g. top performance |
| 127 | * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html> | 129 | * with many processes running. We optimize it for speed by emitting |
| 128 | * (with permission from the author, Douglas W. Jones). | 130 | * two characters at a time, using a 200 byte lookup table. This |
| 131 | * roughly halves the number of multiplications compared to computing | ||
| 132 | * the digits one at a time. Implementation strongly inspired by the | ||
| 133 | * previous version, which in turn used ideas described at | ||
| 134 | * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission | ||
| 135 | * from the author, Douglas W. Jones). | ||
| 136 | * | ||
| 137 | * It turns out there is precisely one 26 bit fixed-point | ||
| 138 | * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 | ||
| 139 | * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual | ||
| 140 | * range happens to be somewhat larger (x <= 1073741898), but that's | ||
| 141 | * irrelevant for our purpose. | ||
| 142 | * | ||
| 143 | * For dividing a number in the range [10^4, 10^6-1] by 100, we still | ||
| 144 | * need a 32x32->64 bit multiply, so we simply use the same constant. | ||
| 145 | * | ||
| 146 | * For dividing a number in the range [100, 10^4-1] by 100, there are | ||
| 147 | * several options. The simplest is (x * 0x147b) >> 19, which is valid | ||
| 148 | * for all x <= 43698. | ||
| 129 | */ | 149 | */ |
| 130 | 150 | ||
| 131 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 | 151 | static const u16 decpair[100] = { |
| 132 | /* Formats correctly any integer in [0, 999999999] */ | 152 | #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) |
| 153 | _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), | ||
| 154 | _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), | ||
| 155 | _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), | ||
| 156 | _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), | ||
| 157 | _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), | ||
| 158 | _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), | ||
| 159 | _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), | ||
| 160 | _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), | ||
| 161 | _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), | ||
| 162 | _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), | ||
| 163 | #undef _ | ||
| 164 | }; | ||
| 165 | |||
| 166 | /* | ||
| 167 | * This will print a single '0' even if r == 0, since we would | ||
| 168 | * immediately jump to out_r where two 0s would be written but only | ||
| 169 | * one of them accounted for in buf. This is needed by ip4_string | ||
| 170 | * below. All other callers pass a non-zero value of r. | ||
| 171 | */ | ||
| 133 | static noinline_for_stack | 172 | static noinline_for_stack |
| 134 | char *put_dec_full9(char *buf, unsigned q) | 173 | char *put_dec_trunc8(char *buf, unsigned r) |
| 135 | { | 174 | { |
| 136 | unsigned r; | 175 | unsigned q; |
| 137 | 176 | ||
| 138 | /* | 177 | /* 1 <= r < 10^8 */ |
| 139 | * Possible ways to approx. divide by 10 | 178 | if (r < 100) |
| 140 | * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) | 179 | goto out_r; |
| 141 | * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) | 180 | |
| 142 | * (x * 0x6667) >> 18 x < 43699 | 181 | /* 100 <= r < 10^8 */ |
| 143 | * (x * 0x3334) >> 17 x < 16389 | 182 | q = (r * (u64)0x28f5c29) >> 32; |
| 144 | * (x * 0x199a) >> 16 x < 16389 | 183 | *((u16 *)buf) = decpair[r - 100*q]; |
| 145 | * (x * 0x0ccd) >> 15 x < 16389 | 184 | buf += 2; |
| 146 | * (x * 0x0667) >> 14 x < 2739 | 185 | |
| 147 | * (x * 0x0334) >> 13 x < 1029 | 186 | /* 1 <= q < 10^6 */ |
| 148 | * (x * 0x019a) >> 12 x < 1029 | 187 | if (q < 100) |
| 149 | * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) | 188 | goto out_q; |
| 150 | * (x * 0x0067) >> 10 x < 179 | 189 | |
| 151 | * (x * 0x0034) >> 9 x < 69 same | 190 | /* 100 <= q < 10^6 */ |
| 152 | * (x * 0x001a) >> 8 x < 69 same | 191 | r = (q * (u64)0x28f5c29) >> 32; |
| 153 | * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) | 192 | *((u16 *)buf) = decpair[q - 100*r]; |
| 154 | * (x * 0x0007) >> 6 x < 19 | 193 | buf += 2; |
| 155 | * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html> | 194 | |
| 156 | */ | 195 | /* 1 <= r < 10^4 */ |
| 157 | r = (q * (uint64_t)0x1999999a) >> 32; | 196 | if (r < 100) |
| 158 | *buf++ = (q - 10 * r) + '0'; /* 1 */ | 197 | goto out_r; |
| 159 | q = (r * (uint64_t)0x1999999a) >> 32; | 198 | |
| 160 | *buf++ = (r - 10 * q) + '0'; /* 2 */ | 199 | /* 100 <= r < 10^4 */ |
| 161 | r = (q * (uint64_t)0x1999999a) >> 32; | 200 | q = (r * 0x147b) >> 19; |
| 162 | *buf++ = (q - 10 * r) + '0'; /* 3 */ | 201 | *((u16 *)buf) = decpair[r - 100*q]; |
| 163 | q = (r * (uint64_t)0x1999999a) >> 32; | 202 | buf += 2; |
| 164 | *buf++ = (r - 10 * q) + '0'; /* 4 */ | 203 | out_q: |
| 165 | r = (q * (uint64_t)0x1999999a) >> 32; | 204 | /* 1 <= q < 100 */ |
| 166 | *buf++ = (q - 10 * r) + '0'; /* 5 */ | 205 | r = q; |
| 167 | /* Now value is under 10000, can avoid 64-bit multiply */ | 206 | out_r: |
| 168 | q = (r * 0x199a) >> 16; | 207 | /* 1 <= r < 100 */ |
| 169 | *buf++ = (r - 10 * q) + '0'; /* 6 */ | 208 | *((u16 *)buf) = decpair[r]; |
| 170 | r = (q * 0xcd) >> 11; | 209 | buf += r < 10 ? 1 : 2; |
| 171 | *buf++ = (q - 10 * r) + '0'; /* 7 */ | ||
| 172 | q = (r * 0xcd) >> 11; | ||
| 173 | *buf++ = (r - 10 * q) + '0'; /* 8 */ | ||
| 174 | *buf++ = q + '0'; /* 9 */ | ||
| 175 | return buf; | 210 | return buf; |
| 176 | } | 211 | } |
| 177 | #endif | ||
| 178 | 212 | ||
| 179 | /* Similar to above but do not pad with zeros. | 213 | #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 |
| 180 | * Code can be easily arranged to print 9 digits too, but our callers | ||
| 181 | * always call put_dec_full9() instead when the number has 9 decimal digits. | ||
| 182 | */ | ||
| 183 | static noinline_for_stack | 214 | static noinline_for_stack |
| 184 | char *put_dec_trunc8(char *buf, unsigned r) | 215 | char *put_dec_full8(char *buf, unsigned r) |
| 185 | { | 216 | { |
| 186 | unsigned q; | 217 | unsigned q; |
| 187 | 218 | ||
| 188 | /* Copy of previous function's body with added early returns */ | 219 | /* 0 <= r < 10^8 */ |
| 189 | while (r >= 10000) { | 220 | q = (r * (u64)0x28f5c29) >> 32; |
| 190 | q = r + '0'; | 221 | *((u16 *)buf) = decpair[r - 100*q]; |
| 191 | r = (r * (uint64_t)0x1999999a) >> 32; | 222 | buf += 2; |
| 192 | *buf++ = q - 10*r; | ||
| 193 | } | ||
| 194 | |||
| 195 | q = (r * 0x199a) >> 16; /* r <= 9999 */ | ||
| 196 | *buf++ = (r - 10 * q) + '0'; | ||
| 197 | if (q == 0) | ||
| 198 | return buf; | ||
| 199 | r = (q * 0xcd) >> 11; /* q <= 999 */ | ||
| 200 | *buf++ = (q - 10 * r) + '0'; | ||
| 201 | if (r == 0) | ||
| 202 | return buf; | ||
| 203 | q = (r * 0xcd) >> 11; /* r <= 99 */ | ||
| 204 | *buf++ = (r - 10 * q) + '0'; | ||
| 205 | if (q == 0) | ||
| 206 | return buf; | ||
| 207 | *buf++ = q + '0'; /* q <= 9 */ | ||
| 208 | return buf; | ||
| 209 | } | ||
| 210 | 223 | ||
| 211 | /* There are two algorithms to print larger numbers. | 224 | /* 0 <= q < 10^6 */ |
| 212 | * One is generic: divide by 1000000000 and repeatedly print | 225 | r = (q * (u64)0x28f5c29) >> 32; |
| 213 | * groups of (up to) 9 digits. It's conceptually simple, | 226 | *((u16 *)buf) = decpair[q - 100*r]; |
| 214 | * but requires a (unsigned long long) / 1000000000 division. | 227 | buf += 2; |
| 215 | * | ||
| 216 | * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, | ||
| 217 | * manipulates them cleverly and generates groups of 4 decimal digits. | ||
| 218 | * It so happens that it does NOT require long long division. | ||
| 219 | * | ||
| 220 | * If long is > 32 bits, division of 64-bit values is relatively easy, | ||
| 221 | * and we will use the first algorithm. | ||
| 222 | * If long long is > 64 bits (strange architecture with VERY large long long), | ||
| 223 | * second algorithm can't be used, and we again use the first one. | ||
| 224 | * | ||
| 225 | * Else (if long is 32 bits and long long is 64 bits) we use second one. | ||
| 226 | */ | ||
| 227 | 228 | ||
| 228 | #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 | 229 | /* 0 <= r < 10^4 */ |
| 230 | q = (r * 0x147b) >> 19; | ||
| 231 | *((u16 *)buf) = decpair[r - 100*q]; | ||
| 232 | buf += 2; | ||
| 229 | 233 | ||
| 230 | /* First algorithm: generic */ | 234 | /* 0 <= q < 100 */ |
| 235 | *((u16 *)buf) = decpair[q]; | ||
| 236 | buf += 2; | ||
| 237 | return buf; | ||
| 238 | } | ||
| 231 | 239 | ||
| 232 | static | 240 | static noinline_for_stack |
| 233 | char *put_dec(char *buf, unsigned long long n) | 241 | char *put_dec(char *buf, unsigned long long n) |
| 234 | { | 242 | { |
| 235 | if (n >= 100*1000*1000) { | 243 | if (n >= 100*1000*1000) |
| 236 | while (n >= 1000*1000*1000) | 244 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); |
| 237 | buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); | 245 | /* 1 <= n <= 1.6e11 */ |
| 238 | if (n >= 100*1000*1000) | 246 | if (n >= 100*1000*1000) |
| 239 | return put_dec_full9(buf, n); | 247 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); |
| 240 | } | 248 | /* 1 <= n < 1e8 */ |
| 241 | return put_dec_trunc8(buf, n); | 249 | return put_dec_trunc8(buf, n); |
| 242 | } | 250 | } |
| 243 | 251 | ||
| 244 | #else | 252 | #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 |
| 245 | |||
| 246 | /* Second algorithm: valid only for 64-bit long longs */ | ||
| 247 | 253 | ||
| 248 | /* See comment in put_dec_full9 for choice of constants */ | 254 | static void |
| 249 | static noinline_for_stack | 255 | put_dec_full4(char *buf, unsigned r) |
| 250 | void put_dec_full4(char *buf, unsigned q) | ||
| 251 | { | 256 | { |
| 252 | unsigned r; | 257 | unsigned q; |
| 253 | r = (q * 0xccd) >> 15; | 258 | |
| 254 | buf[0] = (q - 10 * r) + '0'; | 259 | /* 0 <= r < 10^4 */ |
| 255 | q = (r * 0xcd) >> 11; | 260 | q = (r * 0x147b) >> 19; |
| 256 | buf[1] = (r - 10 * q) + '0'; | 261 | *((u16 *)buf) = decpair[r - 100*q]; |
| 257 | r = (q * 0xcd) >> 11; | 262 | buf += 2; |
| 258 | buf[2] = (q - 10 * r) + '0'; | 263 | /* 0 <= q < 100 */ |
| 259 | buf[3] = r + '0'; | 264 | *((u16 *)buf) = decpair[q]; |
| 260 | } | 265 | } |
| 261 | 266 | ||
| 262 | /* | 267 | /* |
| @@ -264,9 +269,9 @@ void put_dec_full4(char *buf, unsigned q) | |||
| 264 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 | 269 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 |
| 265 | * holds for all x < 1,128,869,999. The largest value this | 270 | * holds for all x < 1,128,869,999. The largest value this |
| 266 | * helper will ever be asked to convert is 1,125,520,955. | 271 | * helper will ever be asked to convert is 1,125,520,955. |
| 267 | * (d1 in the put_dec code, assuming n is all-ones). | 272 | * (second call in the put_dec code, assuming n is all-ones). |
| 268 | */ | 273 | */ |
| 269 | static | 274 | static noinline_for_stack |
| 270 | unsigned put_dec_helper4(char *buf, unsigned x) | 275 | unsigned put_dec_helper4(char *buf, unsigned x) |
| 271 | { | 276 | { |
| 272 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; | 277 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; |
| @@ -293,6 +298,8 @@ char *put_dec(char *buf, unsigned long long n) | |||
| 293 | d2 = (h ) & 0xffff; | 298 | d2 = (h ) & 0xffff; |
| 294 | d3 = (h >> 16); /* implicit "& 0xffff" */ | 299 | d3 = (h >> 16); /* implicit "& 0xffff" */ |
| 295 | 300 | ||
| 301 | /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 | ||
| 302 | = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ | ||
| 296 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); | 303 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); |
| 297 | q = put_dec_helper4(buf, q); | 304 | q = put_dec_helper4(buf, q); |
| 298 | 305 | ||
| @@ -322,7 +329,8 @@ char *put_dec(char *buf, unsigned long long n) | |||
| 322 | */ | 329 | */ |
| 323 | int num_to_str(char *buf, int size, unsigned long long num) | 330 | int num_to_str(char *buf, int size, unsigned long long num) |
| 324 | { | 331 | { |
| 325 | char tmp[sizeof(num) * 3]; | 332 | /* put_dec requires 2-byte alignment of the buffer. */ |
| 333 | char tmp[sizeof(num) * 3] __aligned(2); | ||
| 326 | int idx, len; | 334 | int idx, len; |
| 327 | 335 | ||
| 328 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ | 336 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
| @@ -340,11 +348,11 @@ int num_to_str(char *buf, int size, unsigned long long num) | |||
| 340 | return len; | 348 | return len; |
| 341 | } | 349 | } |
| 342 | 350 | ||
| 343 | #define ZEROPAD 1 /* pad with zero */ | 351 | #define SIGN 1 /* unsigned/signed, must be 1 */ |
| 344 | #define SIGN 2 /* unsigned/signed long */ | 352 | #define LEFT 2 /* left justified */ |
| 345 | #define PLUS 4 /* show plus */ | 353 | #define PLUS 4 /* show plus */ |
| 346 | #define SPACE 8 /* space if plus */ | 354 | #define SPACE 8 /* space if plus */ |
| 347 | #define LEFT 16 /* left justified */ | 355 | #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ |
| 348 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ | 356 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
| 349 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ | 357 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
| 350 | 358 | ||
| @@ -383,10 +391,8 @@ static noinline_for_stack | |||
| 383 | char *number(char *buf, char *end, unsigned long long num, | 391 | char *number(char *buf, char *end, unsigned long long num, |
| 384 | struct printf_spec spec) | 392 | struct printf_spec spec) |
| 385 | { | 393 | { |
| 386 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ | 394 | /* put_dec requires 2-byte alignment of the buffer. */ |
| 387 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ | 395 | char tmp[3 * sizeof(num)] __aligned(2); |
| 388 | |||
| 389 | char tmp[66]; | ||
| 390 | char sign; | 396 | char sign; |
| 391 | char locase; | 397 | char locase; |
| 392 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); | 398 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
| @@ -422,12 +428,7 @@ char *number(char *buf, char *end, unsigned long long num, | |||
| 422 | /* generate full string in tmp[], in reverse order */ | 428 | /* generate full string in tmp[], in reverse order */ |
| 423 | i = 0; | 429 | i = 0; |
| 424 | if (num < spec.base) | 430 | if (num < spec.base) |
| 425 | tmp[i++] = digits[num] | locase; | 431 | tmp[i++] = hex_asc_upper[num] | locase; |
| 426 | /* Generic code, for any base: | ||
| 427 | else do { | ||
| 428 | tmp[i++] = (digits[do_div(num,base)] | locase); | ||
| 429 | } while (num != 0); | ||
| 430 | */ | ||
| 431 | else if (spec.base != 10) { /* 8 or 16 */ | 432 | else if (spec.base != 10) { /* 8 or 16 */ |
| 432 | int mask = spec.base - 1; | 433 | int mask = spec.base - 1; |
| 433 | int shift = 3; | 434 | int shift = 3; |
| @@ -435,7 +436,7 @@ char *number(char *buf, char *end, unsigned long long num, | |||
| 435 | if (spec.base == 16) | 436 | if (spec.base == 16) |
| 436 | shift = 4; | 437 | shift = 4; |
| 437 | do { | 438 | do { |
| 438 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); | 439 | tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); |
| 439 | num >>= shift; | 440 | num >>= shift; |
| 440 | } while (num); | 441 | } while (num); |
| 441 | } else { /* base 10 */ | 442 | } else { /* base 10 */ |
| @@ -447,7 +448,7 @@ char *number(char *buf, char *end, unsigned long long num, | |||
| 447 | spec.precision = i; | 448 | spec.precision = i; |
| 448 | /* leading space padding */ | 449 | /* leading space padding */ |
| 449 | spec.field_width -= spec.precision; | 450 | spec.field_width -= spec.precision; |
| 450 | if (!(spec.flags & (ZEROPAD+LEFT))) { | 451 | if (!(spec.flags & (ZEROPAD | LEFT))) { |
| 451 | while (--spec.field_width >= 0) { | 452 | while (--spec.field_width >= 0) { |
| 452 | if (buf < end) | 453 | if (buf < end) |
| 453 | *buf = ' '; | 454 | *buf = ' '; |
| @@ -475,7 +476,8 @@ char *number(char *buf, char *end, unsigned long long num, | |||
| 475 | } | 476 | } |
| 476 | /* zero or space padding */ | 477 | /* zero or space padding */ |
| 477 | if (!(spec.flags & LEFT)) { | 478 | if (!(spec.flags & LEFT)) { |
| 478 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; | 479 | char c = ' ' + (spec.flags & ZEROPAD); |
| 480 | BUILD_BUG_ON(' ' + ZEROPAD != '0'); | ||
| 479 | while (--spec.field_width >= 0) { | 481 | while (--spec.field_width >= 0) { |
| 480 | if (buf < end) | 482 | if (buf < end) |
| 481 | *buf = c; | 483 | *buf = c; |
| @@ -783,11 +785,19 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, | |||
| 783 | if (spec.field_width > 0) | 785 | if (spec.field_width > 0) |
| 784 | len = min_t(int, spec.field_width, 64); | 786 | len = min_t(int, spec.field_width, 64); |
| 785 | 787 | ||
| 786 | for (i = 0; i < len && buf < end - 1; i++) { | 788 | for (i = 0; i < len; ++i) { |
| 787 | buf = hex_byte_pack(buf, addr[i]); | 789 | if (buf < end) |
| 790 | *buf = hex_asc_hi(addr[i]); | ||
| 791 | ++buf; | ||
| 792 | if (buf < end) | ||
| 793 | *buf = hex_asc_lo(addr[i]); | ||
| 794 | ++buf; | ||
| 788 | 795 | ||
| 789 | if (buf < end && separator && i != len - 1) | 796 | if (separator && i != len - 1) { |
| 790 | *buf++ = separator; | 797 | if (buf < end) |
| 798 | *buf = separator; | ||
| 799 | ++buf; | ||
| 800 | } | ||
| 791 | } | 801 | } |
| 792 | 802 | ||
| 793 | return buf; | 803 | return buf; |
| @@ -942,7 +952,7 @@ char *ip4_string(char *p, const u8 *addr, const char *fmt) | |||
| 942 | break; | 952 | break; |
| 943 | } | 953 | } |
| 944 | for (i = 0; i < 4; i++) { | 954 | for (i = 0; i < 4; i++) { |
| 945 | char temp[3]; /* hold each IP quad in reverse order */ | 955 | char temp[4] __aligned(2); /* hold each IP quad in reverse order */ |
| 946 | int digits = put_dec_trunc8(temp, addr[index]) - temp; | 956 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
| 947 | if (leading_zeros) { | 957 | if (leading_zeros) { |
| 948 | if (digits < 3) | 958 | if (digits < 3) |
| @@ -1233,8 +1243,12 @@ char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, | |||
| 1233 | 1243 | ||
| 1234 | len = spec.field_width < 0 ? 1 : spec.field_width; | 1244 | len = spec.field_width < 0 ? 1 : spec.field_width; |
| 1235 | 1245 | ||
| 1236 | /* Ignore the error. We print as many characters as we can */ | 1246 | /* |
| 1237 | string_escape_mem(addr, len, &buf, end - buf, flags, NULL); | 1247 | * string_escape_mem() writes as many characters as it can to |
| 1248 | * the given buffer, and returns the total size of the output | ||
| 1249 | * had the buffer been big enough. | ||
| 1250 | */ | ||
| 1251 | buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); | ||
| 1238 | 1252 | ||
| 1239 | return buf; | 1253 | return buf; |
| 1240 | } | 1254 | } |
| @@ -1322,6 +1336,30 @@ char *address_val(char *buf, char *end, const void *addr, | |||
| 1322 | return number(buf, end, num, spec); | 1336 | return number(buf, end, num, spec); |
| 1323 | } | 1337 | } |
| 1324 | 1338 | ||
| 1339 | static noinline_for_stack | ||
| 1340 | char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, | ||
| 1341 | const char *fmt) | ||
| 1342 | { | ||
| 1343 | if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) | ||
| 1344 | return string(buf, end, NULL, spec); | ||
| 1345 | |||
| 1346 | switch (fmt[1]) { | ||
| 1347 | case 'r': | ||
| 1348 | return number(buf, end, clk_get_rate(clk), spec); | ||
| 1349 | |||
| 1350 | case 'n': | ||
| 1351 | default: | ||
| 1352 | #ifdef CONFIG_COMMON_CLK | ||
| 1353 | return string(buf, end, __clk_get_name(clk), spec); | ||
| 1354 | #else | ||
| 1355 | spec.base = 16; | ||
| 1356 | spec.field_width = sizeof(unsigned long) * 2 + 2; | ||
| 1357 | spec.flags |= SPECIAL | SMALL | ZEROPAD; | ||
| 1358 | return number(buf, end, (unsigned long)clk, spec); | ||
| 1359 | #endif | ||
| 1360 | } | ||
| 1361 | } | ||
| 1362 | |||
| 1325 | int kptr_restrict __read_mostly; | 1363 | int kptr_restrict __read_mostly; |
| 1326 | 1364 | ||
| 1327 | /* | 1365 | /* |
| @@ -1404,6 +1442,11 @@ int kptr_restrict __read_mostly; | |||
| 1404 | * (default assumed to be phys_addr_t, passed by reference) | 1442 | * (default assumed to be phys_addr_t, passed by reference) |
| 1405 | * - 'd[234]' For a dentry name (optionally 2-4 last components) | 1443 | * - 'd[234]' For a dentry name (optionally 2-4 last components) |
| 1406 | * - 'D[234]' Same as 'd' but for a struct file | 1444 | * - 'D[234]' Same as 'd' but for a struct file |
| 1445 | * - 'C' For a clock, it prints the name (Common Clock Framework) or address | ||
| 1446 | * (legacy clock framework) of the clock | ||
| 1447 | * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address | ||
| 1448 | * (legacy clock framework) of the clock | ||
| 1449 | * - 'Cr' For a clock, it prints the current rate of the clock | ||
| 1407 | * | 1450 | * |
| 1408 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 | 1451 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 1409 | * function pointers are really function descriptors, which contain a | 1452 | * function pointers are really function descriptors, which contain a |
| @@ -1548,6 +1591,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |||
| 1548 | return address_val(buf, end, ptr, spec, fmt); | 1591 | return address_val(buf, end, ptr, spec, fmt); |
| 1549 | case 'd': | 1592 | case 'd': |
| 1550 | return dentry_name(buf, end, ptr, spec, fmt); | 1593 | return dentry_name(buf, end, ptr, spec, fmt); |
| 1594 | case 'C': | ||
| 1595 | return clock(buf, end, ptr, spec, fmt); | ||
| 1551 | case 'D': | 1596 | case 'D': |
| 1552 | return dentry_name(buf, end, | 1597 | return dentry_name(buf, end, |
| 1553 | ((const struct file *)ptr)->f_path.dentry, | 1598 | ((const struct file *)ptr)->f_path.dentry, |
| @@ -1738,29 +1783,21 @@ qualifier: | |||
| 1738 | if (spec->qualifier == 'L') | 1783 | if (spec->qualifier == 'L') |
| 1739 | spec->type = FORMAT_TYPE_LONG_LONG; | 1784 | spec->type = FORMAT_TYPE_LONG_LONG; |
| 1740 | else if (spec->qualifier == 'l') { | 1785 | else if (spec->qualifier == 'l') { |
| 1741 | if (spec->flags & SIGN) | 1786 | BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); |
| 1742 | spec->type = FORMAT_TYPE_LONG; | 1787 | spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); |
| 1743 | else | ||
| 1744 | spec->type = FORMAT_TYPE_ULONG; | ||
| 1745 | } else if (_tolower(spec->qualifier) == 'z') { | 1788 | } else if (_tolower(spec->qualifier) == 'z') { |
| 1746 | spec->type = FORMAT_TYPE_SIZE_T; | 1789 | spec->type = FORMAT_TYPE_SIZE_T; |
| 1747 | } else if (spec->qualifier == 't') { | 1790 | } else if (spec->qualifier == 't') { |
| 1748 | spec->type = FORMAT_TYPE_PTRDIFF; | 1791 | spec->type = FORMAT_TYPE_PTRDIFF; |
| 1749 | } else if (spec->qualifier == 'H') { | 1792 | } else if (spec->qualifier == 'H') { |
| 1750 | if (spec->flags & SIGN) | 1793 | BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); |
| 1751 | spec->type = FORMAT_TYPE_BYTE; | 1794 | spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); |
| 1752 | else | ||
| 1753 | spec->type = FORMAT_TYPE_UBYTE; | ||
| 1754 | } else if (spec->qualifier == 'h') { | 1795 | } else if (spec->qualifier == 'h') { |
| 1755 | if (spec->flags & SIGN) | 1796 | BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); |
| 1756 | spec->type = FORMAT_TYPE_SHORT; | 1797 | spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); |
| 1757 | else | ||
| 1758 | spec->type = FORMAT_TYPE_USHORT; | ||
| 1759 | } else { | 1798 | } else { |
| 1760 | if (spec->flags & SIGN) | 1799 | BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); |
| 1761 | spec->type = FORMAT_TYPE_INT; | 1800 | spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); |
| 1762 | else | ||
| 1763 | spec->type = FORMAT_TYPE_UINT; | ||
| 1764 | } | 1801 | } |
| 1765 | 1802 | ||
| 1766 | return ++fmt - start; | 1803 | return ++fmt - start; |
| @@ -1800,6 +1837,11 @@ qualifier: | |||
| 1800 | * %*pE[achnops] print an escaped buffer | 1837 | * %*pE[achnops] print an escaped buffer |
| 1801 | * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 | 1838 | * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 |
| 1802 | * bytes of the input) | 1839 | * bytes of the input) |
| 1840 | * %pC output the name (Common Clock Framework) or address (legacy clock | ||
| 1841 | * framework) of a clock | ||
| 1842 | * %pCn output the name (Common Clock Framework) or address (legacy clock | ||
| 1843 | * framework) of a clock | ||
| 1844 | * %pCr output the current rate of a clock | ||
| 1803 | * %n is ignored | 1845 | * %n is ignored |
| 1804 | * | 1846 | * |
| 1805 | * ** Please update Documentation/printk-formats.txt when making changes ** | 1847 | * ** Please update Documentation/printk-formats.txt when making changes ** |
