File Index
Files and directories
Sign in
FILE

printk.c

File preview with syntax highlighting.

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