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