xref: /linux/arch/s390/boot/printk.c (revision b731bc5f49651bb85ef31fa1db6e76a0fe10d572)
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/sclp.h>
12 #include <asm/uv.h>
13 #include "boot.h"
14 
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 
boot_rb_add(const char * str,size_t len)23 static void boot_rb_add(const char *str, size_t len)
24 {
25 	/* leave double '\0' in the end */
26 	size_t avail = sizeof(boot_rb) - boot_rb_off - 1;
27 
28 	/* store strings separated by '\0' */
29 	if (len + 1 > avail)
30 		boot_rb_off = 0;
31 	strcpy(boot_rb + boot_rb_off, str);
32 	boot_rb_off += len + 1;
33 }
34 
print_rb_entry(const char * str)35 static void print_rb_entry(const char *str)
36 {
37 	sclp_early_printk(printk_skip_level(str));
38 }
39 
debug_messages_printed(void)40 static bool debug_messages_printed(void)
41 {
42 	return boot_earlyprintk && (boot_ignore_loglevel || boot_console_loglevel > LOGLEVEL_DEBUG);
43 }
44 
boot_rb_dump(void)45 void boot_rb_dump(void)
46 {
47 	if (debug_messages_printed())
48 		return;
49 	sclp_early_printk("Boot messages ring buffer:\n");
50 	boot_rb_foreach(print_rb_entry);
51 }
52 
53 const char hex_asc[] = "0123456789abcdef";
54 
as_hex(char * dst,unsigned long val,int pad)55 static char *as_hex(char *dst, unsigned long val, int pad)
56 {
57 	char *p = dst + max(pad, (int)__fls(val | 1) / 4 + 1);
58 
59 	for (*p-- = '\0'; p >= dst; val >>= 4)
60 		*p-- = hex_asc[val & 0x0f];
61 	return dst;
62 }
63 
64 #define MAX_NUMLEN 21
as_dec(char * buf,unsigned long val,bool is_signed)65 static char *as_dec(char *buf, unsigned long val, bool is_signed)
66 {
67 	bool negative = false;
68 	char *p = buf + MAX_NUMLEN;
69 
70 	if (is_signed && (long)val < 0) {
71 		val = (val == LONG_MIN ? LONG_MIN : -(long)val);
72 		negative = true;
73 	}
74 
75 	*--p = '\0';
76 	do {
77 		*--p = '0' + (val % 10);
78 		val /= 10;
79 	} while (val);
80 
81 	if (negative)
82 		*--p = '-';
83 	return p;
84 }
85 
strpad(char * dst,size_t dst_size,const char * src,int _pad,bool zero_pad,bool decimal)86 static ssize_t strpad(char *dst, size_t dst_size, const char *src,
87 		      int _pad, bool zero_pad, bool decimal)
88 {
89 	ssize_t len = strlen(src), pad = _pad;
90 	char *p = dst;
91 
92 	if (max(len, abs(pad)) >= dst_size)
93 		return -E2BIG;
94 
95 	if (pad > len) {
96 		if (decimal && zero_pad && *src == '-') {
97 			*p++ = '-';
98 			src++;
99 			len--;
100 			pad--;
101 		}
102 		memset(p, zero_pad ? '0' : ' ', pad - len);
103 		p += pad - len;
104 	}
105 	memcpy(p, src, len);
106 	p += len;
107 	if (pad < 0 && -pad > len) {
108 		memset(p, ' ', -pad - len);
109 		p += -pad - len;
110 	}
111 	*p = '\0';
112 	return p - dst;
113 }
114 
symstart(char * p)115 static char *symstart(char *p)
116 {
117 	while (*p)
118 		p--;
119 	return p + 1;
120 }
121 
findsym(unsigned long ip,unsigned short * off,unsigned short * len)122 static noinline char *findsym(unsigned long ip, unsigned short *off, unsigned short *len)
123 {
124 	/* symbol entries are in a form "10000 c4 startup\0" */
125 	char *a = _decompressor_syms_start;
126 	char *b = _decompressor_syms_end;
127 	unsigned long start;
128 	unsigned long size;
129 	char *pivot;
130 	char *endp;
131 
132 	while (a < b) {
133 		pivot = symstart(a + (b - a) / 2);
134 		start = simple_strtoull(pivot, &endp, 16);
135 		size = simple_strtoull(endp + 1, &endp, 16);
136 		if (ip < start) {
137 			b = pivot;
138 			continue;
139 		}
140 		if (ip > start + size) {
141 			a = pivot + strlen(pivot) + 1;
142 			continue;
143 		}
144 		*off = ip - start;
145 		*len = size;
146 		return endp + 1;
147 	}
148 	return NULL;
149 }
150 
151 #define MAX_SYMLEN 64
strsym(char * buf,void * ip)152 static noinline char *strsym(char *buf, void *ip)
153 {
154 	unsigned short off;
155 	unsigned short len;
156 	char *p;
157 
158 	p = findsym((unsigned long)ip, &off, &len);
159 	if (p) {
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);
163 		strcpy(p, "+0x");
164 		as_hex(p + 3, off, 0);
165 		strcat(p, "/0x");
166 		as_hex(p + strlen(p), len, 0);
167 	} else {
168 		as_hex(buf, (unsigned long)ip, 16);
169 	}
170 	return buf;
171 }
172 
printk_loglevel(const char * buf)173 static inline int printk_loglevel(const char *buf)
174 {
175 	if (buf[0] == KERN_SOH_ASCII && buf[1]) {
176 		switch (buf[1]) {
177 		case '0' ... '7':
178 			return buf[1] - '0';
179 		}
180 	}
181 	return MESSAGE_LOGLEVEL_DEFAULT;
182 }
183 
boot_console_earlyprintk(const char * buf)184 static void boot_console_earlyprintk(const char *buf)
185 {
186 	int level = printk_loglevel(buf);
187 
188 	/* always print emergency messages */
189 	if (level > LOGLEVEL_EMERG && !boot_earlyprintk)
190 		return;
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))))
194 		return;
195 	if (boot_ignore_loglevel || level < boot_console_loglevel)
196 		sclp_early_printk(buf);
197 }
198 
add_timestamp(char * buf)199 static char *add_timestamp(char *buf)
200 {
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);
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 
boot_printk(const char * fmt,...)223 int 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 	}
290 out:
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 }
299