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>
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);
23 static void boot_rb_add(const char *str, size_t len)
25 /* leave double '\0' in the end */
26 size_t avail = sizeof(boot_rb) - boot_rb_off - 1;
28 /* store strings separated by '\0' */
31 strcpy(boot_rb + boot_rb_off, str);
32 boot_rb_off += len + 1;
35 static void print_rb_entry(const char *str)
37 sclp_early_printk(printk_skip_level(str));
40 static bool debug_messages_printed(void)
42 return boot_earlyprintk && (boot_ignore_loglevel || boot_console_loglevel > LOGLEVEL_DEBUG);
45 void boot_rb_dump(void)
47 if (debug_messages_printed())
49 sclp_early_printk("Boot messages ring buffer:\n");
50 boot_rb_foreach(print_rb_entry);
53 const char hex_asc[] = "0123456789abcdef";
55 static char *as_hex(char *dst, unsigned long val, int pad)
57 char *p = dst + max(pad, (int)__fls(val | 1) / 4 + 1);
59 for (*p-- = '\0'; p >= dst; val >>= 4)
60 *p-- = hex_asc[val & 0x0f];
65 static char *as_dec(char *buf, unsigned long val, bool is_signed)
67 bool negative = false;
68 char *p = buf + MAX_NUMLEN;
70 if (is_signed && (long)val < 0) {
71 val = (val == LONG_MIN ? LONG_MIN : -(long)val);
77 *--p = '0' + (val % 10);
86 static ssize_t strpad(char *dst, size_t dst_size, const char *src,
87 int _pad, bool zero_pad, bool decimal)
89 ssize_t len = strlen(src), pad = _pad;
92 if (max(len, abs(pad)) >= dst_size)
96 if (decimal && zero_pad && *src == '-') {
102 memset(p, zero_pad ? '0' : ' ', pad - len);
107 if (pad < 0 && -pad > len) {
108 memset(p, ' ', -pad - len);
115 static char *symstart(char *p)
122 static noinline char *findsym(unsigned long ip, unsigned short *off, unsigned short *len)
124 /* symbol entries are in a form "10000 c4 startup\0" */
125 char *a = _decompressor_syms_start;
126 char *b = _decompressor_syms_end;
133 pivot = symstart(a + (b - a) / 2);
134 start = simple_strtoull(pivot, &endp, 16);
135 size = simple_strtoull(endp + 1, &endp, 16);
140 if (ip > start + size) {
141 a = pivot + strlen(pivot) + 1;
151 #define MAX_SYMLEN 64
152 static noinline char *strsym(char *buf, void *ip)
158 p = findsym((unsigned long)ip, &off, &len);
160 strncpy(buf, p, MAX_SYMLEN);
161 /* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */
162 p = buf + strnlen(buf, MAX_SYMLEN - 15);
164 as_hex(p + 3, off, 0);
166 as_hex(p + strlen(p), len, 0);
168 as_hex(buf, (unsigned long)ip, 16);
173 static inline int printk_loglevel(const char *buf)
175 if (buf[0] == KERN_SOH_ASCII && buf[1]) {
181 return MESSAGE_LOGLEVEL_DEFAULT;
184 static void boot_console_earlyprintk(const char *buf)
186 int level = printk_loglevel(buf);
188 /* always print emergency messages */
189 if (level > LOGLEVEL_EMERG && !boot_earlyprintk)
191 buf = printk_skip_level(buf);
192 /* print debug messages only when bootdebug is enabled */
193 if (level == LOGLEVEL_DEBUG && (!bootdebug || !bootdebug_filter_match(skip_timestamp(buf))))
195 if (boot_ignore_loglevel || level < boot_console_loglevel)
196 sclp_early_printk(buf);
199 static char *add_timestamp(char *buf)
201 #ifdef CONFIG_PRINTK_TIME
202 union tod_clock *boot_clock = (union tod_clock *)&get_lowcore()->boot_clock;
203 unsigned long ns = tod_to_ns(get_tod_clock() - boot_clock->tod);
207 buf += strpad(buf, MAX_NUMLEN, as_dec(ts, ns / NSEC_PER_SEC, 0), 5, 0, 0);
209 buf += strpad(buf, MAX_NUMLEN, as_dec(ts, (ns % NSEC_PER_SEC) / NSEC_PER_USEC, 0), 6, 1, 0);
216 #define va_arg_len_type(args, lenmod, typemod) \
217 ((lenmod == 'l') ? va_arg(args, typemod long) : \
218 (lenmod == 'h') ? (typemod short)va_arg(args, typemod int) : \
219 (lenmod == 'H') ? (typemod char)va_arg(args, typemod int) : \
220 (lenmod == 'z') ? va_arg(args, typemod long) : \
221 va_arg(args, typemod int))
223 int boot_printk(const char *fmt, ...)
225 char buf[1024] = { 0 };
226 char *end = buf + sizeof(buf) - 1; /* make sure buf is 0 terminated */
227 bool zero_pad, decimal;
228 char *strval, *p = buf;
229 char valbuf[MAX(MAX_SYMLEN, MAX_NUMLEN)];
235 *p++ = KERN_SOH_ASCII;
236 *p++ = printk_get_level(fmt) ?: '0' + MESSAGE_LOGLEVEL_DEFAULT;
237 p = add_timestamp(p);
238 fmt = printk_skip_level(fmt);
241 for (; p < end && *fmt; fmt++) {
250 zero_pad = (*fmt == '0');
251 pad = simple_strtol(fmt, (char **)&fmt, 10);
252 lenmod = (*fmt == 'h' || *fmt == 'l' || *fmt == 'z') ? *fmt++ : 0;
253 if (lenmod == 'h' && *fmt == 'h') {
262 strval = va_arg(args, char *);
266 if (*++fmt != 'S' || lenmod)
268 strval = strsym(valbuf, va_arg(args, void *));
273 strval = as_dec(valbuf, va_arg_len_type(args, lenmod, signed), 1);
277 strval = as_dec(valbuf, va_arg_len_type(args, lenmod, unsigned), 0);
280 strval = as_hex(valbuf, va_arg_len_type(args, lenmod, unsigned), 0);
285 len = strpad(p, end - p, strval, pad, zero_pad, decimal);
294 boot_rb_add(buf, len);
295 boot_console_earlyprintk(buf);