printk.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/stdarg.h>
  4. #include <linux/string.h>
  5. #include <linux/ctype.h>
  6. #include <asm/stacktrace.h>
  7. #include <asm/boot_data.h>
  8. #include <asm/sections.h>
  9. #include <asm/lowcore.h>
  10. #include <asm/setup.h>
  11. #include <asm/timex.h>
  12. #include <asm/sclp.h>
  13. #include <asm/uv.h>
  14. #include "boot.h"
  15. int boot_console_loglevel = CONFIG_CONSOLE_LOGLEVEL_DEFAULT;
  16. bool boot_ignore_loglevel;
  17. char __bootdata(boot_rb)[PAGE_SIZE * 2];
  18. bool __bootdata(boot_earlyprintk);
  19. size_t __bootdata(boot_rb_off);
  20. char __bootdata(bootdebug_filter)[128];
  21. bool __bootdata(bootdebug);
  22. static void boot_rb_add(const char *str, size_t len)
  23. {
  24. /* leave double '\0' in the end */
  25. size_t avail = sizeof(boot_rb) - boot_rb_off - 1;
  26. /* store strings separated by '\0' */
  27. if (len + 1 > avail)
  28. boot_rb_off = 0;
  29. avail = sizeof(boot_rb) - boot_rb_off - 1;
  30. strscpy(boot_rb + boot_rb_off, str, avail);
  31. boot_rb_off += len + 1;
  32. }
  33. static void print_rb_entry(const char *str)
  34. {
  35. sclp_early_printk(printk_skip_level(str));
  36. }
  37. static bool debug_messages_printed(void)
  38. {
  39. return boot_earlyprintk && (boot_ignore_loglevel || boot_console_loglevel > LOGLEVEL_DEBUG);
  40. }
  41. void boot_rb_dump(void)
  42. {
  43. if (debug_messages_printed())
  44. return;
  45. sclp_early_printk("Boot messages ring buffer:\n");
  46. boot_rb_foreach(print_rb_entry);
  47. }
  48. const char hex_asc[] = "0123456789abcdef";
  49. static char *as_hex(char *dst, unsigned long val, int pad)
  50. {
  51. char *p = dst + max(pad, (int)__fls(val | 1) / 4 + 1);
  52. for (*p-- = '\0'; p >= dst; val >>= 4)
  53. *p-- = hex_asc[val & 0x0f];
  54. return dst;
  55. }
  56. #define MAX_NUMLEN 21
  57. static char *as_dec(char *buf, unsigned long val, bool is_signed)
  58. {
  59. bool negative = false;
  60. char *p = buf + MAX_NUMLEN;
  61. if (is_signed && (long)val < 0) {
  62. val = (val == LONG_MIN ? LONG_MIN : -(long)val);
  63. negative = true;
  64. }
  65. *--p = '\0';
  66. do {
  67. *--p = '0' + (val % 10);
  68. val /= 10;
  69. } while (val);
  70. if (negative)
  71. *--p = '-';
  72. return p;
  73. }
  74. static ssize_t strpad(char *dst, size_t dst_size, const char *src,
  75. int _pad, bool zero_pad, bool decimal)
  76. {
  77. ssize_t len = strlen(src), pad = _pad;
  78. char *p = dst;
  79. if (max(len, abs(pad)) >= dst_size)
  80. return -E2BIG;
  81. if (pad > len) {
  82. if (decimal && zero_pad && *src == '-') {
  83. *p++ = '-';
  84. src++;
  85. len--;
  86. pad--;
  87. }
  88. memset(p, zero_pad ? '0' : ' ', pad - len);
  89. p += pad - len;
  90. }
  91. memcpy(p, src, len);
  92. p += len;
  93. if (pad < 0 && -pad > len) {
  94. memset(p, ' ', -pad - len);
  95. p += -pad - len;
  96. }
  97. *p = '\0';
  98. return p - dst;
  99. }
  100. static char *symstart(char *p)
  101. {
  102. while (*p)
  103. p--;
  104. return p + 1;
  105. }
  106. static noinline char *findsym(unsigned long ip, unsigned short *off, unsigned short *len)
  107. {
  108. /* symbol entries are in a form "10000 c4 startup\0" */
  109. char *a = _decompressor_syms_start;
  110. char *b = _decompressor_syms_end;
  111. unsigned long start;
  112. unsigned long size;
  113. char *pivot;
  114. char *endp;
  115. while (a < b) {
  116. pivot = symstart(a + (b - a) / 2);
  117. start = simple_strtoull(pivot, &endp, 16);
  118. size = simple_strtoull(endp + 1, &endp, 16);
  119. if (ip < start) {
  120. b = pivot;
  121. continue;
  122. }
  123. if (ip > start + size) {
  124. a = pivot + strlen(pivot) + 1;
  125. continue;
  126. }
  127. *off = ip - start;
  128. *len = size;
  129. return endp + 1;
  130. }
  131. return NULL;
  132. }
  133. #define MAX_SYMLEN 64
  134. static noinline char *strsym(char *buf, void *ip)
  135. {
  136. unsigned short off;
  137. unsigned short len;
  138. char *p;
  139. p = findsym((unsigned long)ip, &off, &len);
  140. if (p) {
  141. strscpy(buf, p, MAX_SYMLEN);
  142. /* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */
  143. p = buf + strnlen(buf, MAX_SYMLEN - 15);
  144. strscpy(p, "+0x", MAX_SYMLEN - (p - buf));
  145. as_hex(p + 3, off, 0);
  146. strcat(p, "/0x");
  147. as_hex(p + strlen(p), len, 0);
  148. } else {
  149. as_hex(buf, (unsigned long)ip, 16);
  150. }
  151. return buf;
  152. }
  153. static inline int printk_loglevel(const char *buf)
  154. {
  155. if (buf[0] == KERN_SOH_ASCII && buf[1]) {
  156. switch (buf[1]) {
  157. case '0' ... '7':
  158. return buf[1] - '0';
  159. }
  160. }
  161. return MESSAGE_LOGLEVEL_DEFAULT;
  162. }
  163. static void boot_console_earlyprintk(const char *buf)
  164. {
  165. int level = printk_loglevel(buf);
  166. /* always print emergency messages */
  167. if (level > LOGLEVEL_EMERG && !boot_earlyprintk)
  168. return;
  169. buf = printk_skip_level(buf);
  170. /* print debug messages only when bootdebug is enabled */
  171. if (level == LOGLEVEL_DEBUG && (!bootdebug || !bootdebug_filter_match(skip_timestamp(buf))))
  172. return;
  173. if (boot_ignore_loglevel || level < boot_console_loglevel)
  174. sclp_early_printk(buf);
  175. }
  176. static char *add_timestamp(char *buf)
  177. {
  178. #ifdef CONFIG_PRINTK_TIME
  179. unsigned long ns = tod_to_ns(__get_tod_clock_monotonic());
  180. char ts[MAX_NUMLEN];
  181. *buf++ = '[';
  182. buf += strpad(buf, MAX_NUMLEN, as_dec(ts, ns / NSEC_PER_SEC, 0), 5, 0, 0);
  183. *buf++ = '.';
  184. buf += strpad(buf, MAX_NUMLEN, as_dec(ts, (ns % NSEC_PER_SEC) / NSEC_PER_USEC, 0), 6, 1, 0);
  185. *buf++ = ']';
  186. *buf++ = ' ';
  187. #endif
  188. return buf;
  189. }
  190. #define va_arg_len_type(args, lenmod, typemod) \
  191. ((lenmod == 'l') ? va_arg(args, typemod long) : \
  192. (lenmod == 'h') ? (typemod short)va_arg(args, typemod int) : \
  193. (lenmod == 'H') ? (typemod char)va_arg(args, typemod int) : \
  194. (lenmod == 'z') ? va_arg(args, typemod long) : \
  195. va_arg(args, typemod int))
  196. int boot_printk(const char *fmt, ...)
  197. {
  198. char buf[1024] = { 0 };
  199. char *end = buf + sizeof(buf) - 1; /* make sure buf is 0 terminated */
  200. bool zero_pad, decimal;
  201. char *strval, *p = buf;
  202. char valbuf[MAX(MAX_SYMLEN, MAX_NUMLEN)];
  203. va_list args;
  204. char lenmod;
  205. ssize_t len;
  206. int pad;
  207. *p++ = KERN_SOH_ASCII;
  208. *p++ = printk_get_level(fmt) ?: '0' + MESSAGE_LOGLEVEL_DEFAULT;
  209. p = add_timestamp(p);
  210. fmt = printk_skip_level(fmt);
  211. va_start(args, fmt);
  212. for (; p < end && *fmt; fmt++) {
  213. if (*fmt != '%') {
  214. *p++ = *fmt;
  215. continue;
  216. }
  217. if (*++fmt == '%') {
  218. *p++ = '%';
  219. continue;
  220. }
  221. zero_pad = (*fmt == '0');
  222. pad = simple_strtol(fmt, (char **)&fmt, 10);
  223. lenmod = (*fmt == 'h' || *fmt == 'l' || *fmt == 'z') ? *fmt++ : 0;
  224. if (lenmod == 'h' && *fmt == 'h') {
  225. lenmod = 'H';
  226. fmt++;
  227. }
  228. decimal = false;
  229. switch (*fmt) {
  230. case 's':
  231. if (lenmod)
  232. goto out;
  233. strval = va_arg(args, char *);
  234. zero_pad = false;
  235. break;
  236. case 'p':
  237. if (*++fmt != 'S' || lenmod)
  238. goto out;
  239. strval = strsym(valbuf, va_arg(args, void *));
  240. zero_pad = false;
  241. break;
  242. case 'd':
  243. case 'i':
  244. strval = as_dec(valbuf, va_arg_len_type(args, lenmod, signed), 1);
  245. decimal = true;
  246. break;
  247. case 'u':
  248. strval = as_dec(valbuf, va_arg_len_type(args, lenmod, unsigned), 0);
  249. break;
  250. case 'x':
  251. strval = as_hex(valbuf, va_arg_len_type(args, lenmod, unsigned), 0);
  252. break;
  253. default:
  254. goto out;
  255. }
  256. len = strpad(p, end - p, strval, pad, zero_pad, decimal);
  257. if (len == -E2BIG)
  258. break;
  259. p += len;
  260. }
  261. out:
  262. va_end(args);
  263. len = strlen(buf);
  264. if (len) {
  265. boot_rb_add(buf, len);
  266. boot_console_earlyprintk(buf);
  267. }
  268. return len;
  269. }