xref: /linux/arch/s390/boot/pgm_check_info.c (revision ba1a6be994e8444baf23c14d7be045811197ccb3)
1724dc336SVasily Gorbik // SPDX-License-Identifier: GPL-2.0
2724dc336SVasily Gorbik #include <linux/kernel.h>
3724dc336SVasily Gorbik #include <linux/string.h>
49a78c70aSVasily Gorbik #include <linux/ctype.h>
58977ab65SVasily Gorbik #include <asm/stacktrace.h>
6*ba1a6be9SVasily Gorbik #include <asm/boot_data.h>
7724dc336SVasily Gorbik #include <asm/lowcore.h>
83ca8b855SVasily Gorbik #include <asm/setup.h>
9724dc336SVasily Gorbik #include <asm/sclp.h>
10*ba1a6be9SVasily Gorbik #include <asm/uv.h>
119a78c70aSVasily Gorbik #include <stdarg.h>
12724dc336SVasily Gorbik #include "boot.h"
13724dc336SVasily Gorbik 
14724dc336SVasily Gorbik const char hex_asc[] = "0123456789abcdef";
15724dc336SVasily Gorbik 
169a78c70aSVasily Gorbik static char *as_hex(char *dst, unsigned long val, int pad)
17724dc336SVasily Gorbik {
189a78c70aSVasily Gorbik 	char *p, *end = p = dst + max(pad, (int)__fls(val | 1) / 4 + 1);
199a78c70aSVasily Gorbik 
209a78c70aSVasily Gorbik 	for (*p-- = 0; p >= dst; val >>= 4)
219a78c70aSVasily Gorbik 		*p-- = hex_asc[val & 0x0f];
229a78c70aSVasily Gorbik 	return end;
23724dc336SVasily Gorbik }
24724dc336SVasily Gorbik 
2524621896SVasily Gorbik static char *symstart(char *p)
2624621896SVasily Gorbik {
2724621896SVasily Gorbik 	while (*p)
2824621896SVasily Gorbik 		p--;
2924621896SVasily Gorbik 	return p + 1;
3024621896SVasily Gorbik }
3124621896SVasily Gorbik 
3224621896SVasily Gorbik extern char _decompressor_syms_start[], _decompressor_syms_end[];
3324621896SVasily Gorbik static noinline char *findsym(unsigned long ip, unsigned short *off, unsigned short *len)
3424621896SVasily Gorbik {
3524621896SVasily Gorbik 	/* symbol entries are in a form "10000 c4 startup\0" */
3624621896SVasily Gorbik 	char *a = _decompressor_syms_start;
3724621896SVasily Gorbik 	char *b = _decompressor_syms_end;
3824621896SVasily Gorbik 	unsigned long start;
3924621896SVasily Gorbik 	unsigned long size;
4024621896SVasily Gorbik 	char *pivot;
4124621896SVasily Gorbik 	char *endp;
4224621896SVasily Gorbik 
4324621896SVasily Gorbik 	while (a < b) {
4424621896SVasily Gorbik 		pivot = symstart(a + (b - a) / 2);
4524621896SVasily Gorbik 		start = simple_strtoull(pivot, &endp, 16);
4624621896SVasily Gorbik 		size = simple_strtoull(endp + 1, &endp, 16);
4724621896SVasily Gorbik 		if (ip < start) {
4824621896SVasily Gorbik 			b = pivot;
4924621896SVasily Gorbik 			continue;
5024621896SVasily Gorbik 		}
5124621896SVasily Gorbik 		if (ip > start + size) {
5224621896SVasily Gorbik 			a = pivot + strlen(pivot) + 1;
5324621896SVasily Gorbik 			continue;
5424621896SVasily Gorbik 		}
5524621896SVasily Gorbik 		*off = ip - start;
5624621896SVasily Gorbik 		*len = size;
5724621896SVasily Gorbik 		return endp + 1;
5824621896SVasily Gorbik 	}
5924621896SVasily Gorbik 	return NULL;
6024621896SVasily Gorbik }
6124621896SVasily Gorbik 
6224621896SVasily Gorbik static noinline char *strsym(void *ip)
6324621896SVasily Gorbik {
6424621896SVasily Gorbik 	static char buf[64];
6524621896SVasily Gorbik 	unsigned short off;
6624621896SVasily Gorbik 	unsigned short len;
6724621896SVasily Gorbik 	char *p;
6824621896SVasily Gorbik 
6924621896SVasily Gorbik 	p = findsym((unsigned long)ip, &off, &len);
7024621896SVasily Gorbik 	if (p) {
7124621896SVasily Gorbik 		strncpy(buf, p, sizeof(buf));
7224621896SVasily Gorbik 		/* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */
7324621896SVasily Gorbik 		p = buf + strnlen(buf, sizeof(buf) - 15);
7424621896SVasily Gorbik 		strcpy(p, "+0x");
7524621896SVasily Gorbik 		p = as_hex(p + 3, off, 0);
7624621896SVasily Gorbik 		strcpy(p, "/0x");
7724621896SVasily Gorbik 		as_hex(p + 3, len, 0);
7824621896SVasily Gorbik 	} else {
7924621896SVasily Gorbik 		as_hex(buf, (unsigned long)ip, 16);
8024621896SVasily Gorbik 	}
8124621896SVasily Gorbik 	return buf;
8224621896SVasily Gorbik }
8324621896SVasily Gorbik 
849a78c70aSVasily Gorbik void decompressor_printk(const char *fmt, ...)
85724dc336SVasily Gorbik {
869a78c70aSVasily Gorbik 	char buf[1024] = { 0 };
879a78c70aSVasily Gorbik 	char *end = buf + sizeof(buf) - 1; /* make sure buf is 0 terminated */
889a78c70aSVasily Gorbik 	unsigned long pad;
899a78c70aSVasily Gorbik 	char *p = buf;
909a78c70aSVasily Gorbik 	va_list args;
919a78c70aSVasily Gorbik 
929a78c70aSVasily Gorbik 	va_start(args, fmt);
939a78c70aSVasily Gorbik 	for (; p < end && *fmt; fmt++) {
949a78c70aSVasily Gorbik 		if (*fmt != '%') {
959a78c70aSVasily Gorbik 			*p++ = *fmt;
969a78c70aSVasily Gorbik 			continue;
979a78c70aSVasily Gorbik 		}
989a78c70aSVasily Gorbik 		pad = isdigit(*++fmt) ? simple_strtol(fmt, (char **)&fmt, 10) : 0;
999a78c70aSVasily Gorbik 		switch (*fmt) {
1009a78c70aSVasily Gorbik 		case 's':
1019a78c70aSVasily Gorbik 			p = buf + strlcat(buf, va_arg(args, char *), sizeof(buf));
1029a78c70aSVasily Gorbik 			break;
10324621896SVasily Gorbik 		case 'p':
10424621896SVasily Gorbik 			if (*++fmt != 'S')
10524621896SVasily Gorbik 				goto out;
10624621896SVasily Gorbik 			p = buf + strlcat(buf, strsym(va_arg(args, void *)), sizeof(buf));
10724621896SVasily Gorbik 			break;
1089a78c70aSVasily Gorbik 		case 'l':
1099a78c70aSVasily Gorbik 			if (*++fmt != 'x' || end - p <= max(sizeof(long) * 2, pad))
1109a78c70aSVasily Gorbik 				goto out;
1119a78c70aSVasily Gorbik 			p = as_hex(p, va_arg(args, unsigned long), pad);
1129a78c70aSVasily Gorbik 			break;
1139a78c70aSVasily Gorbik 		case 'x':
1149a78c70aSVasily Gorbik 			if (end - p <= max(sizeof(int) * 2, pad))
1159a78c70aSVasily Gorbik 				goto out;
1169a78c70aSVasily Gorbik 			p = as_hex(p, va_arg(args, unsigned int), pad);
1179a78c70aSVasily Gorbik 			break;
1189a78c70aSVasily Gorbik 		default:
1199a78c70aSVasily Gorbik 			goto out;
1209a78c70aSVasily Gorbik 		}
1219a78c70aSVasily Gorbik 	}
1229a78c70aSVasily Gorbik out:
1239a78c70aSVasily Gorbik 	va_end(args);
1249a78c70aSVasily Gorbik 	sclp_early_printk(buf);
125724dc336SVasily Gorbik }
126724dc336SVasily Gorbik 
1278977ab65SVasily Gorbik static noinline void print_stacktrace(void)
1288977ab65SVasily Gorbik {
1298977ab65SVasily Gorbik 	struct stack_info boot_stack = { STACK_TYPE_TASK, BOOT_STACK_OFFSET,
1308977ab65SVasily Gorbik 					 BOOT_STACK_OFFSET + BOOT_STACK_SIZE };
1318977ab65SVasily Gorbik 	unsigned long sp = S390_lowcore.gpregs_save_area[15];
1328977ab65SVasily Gorbik 	bool first = true;
1338977ab65SVasily Gorbik 
1348977ab65SVasily Gorbik 	decompressor_printk("Call Trace:\n");
1358977ab65SVasily Gorbik 	while (!(sp & 0x7) && on_stack(&boot_stack, sp, sizeof(struct stack_frame))) {
1368977ab65SVasily Gorbik 		struct stack_frame *sf = (struct stack_frame *)sp;
1378977ab65SVasily Gorbik 
1388977ab65SVasily Gorbik 		decompressor_printk(first ? "(sp:%016lx [<%016lx>] %pS)\n" :
1398977ab65SVasily Gorbik 					    " sp:%016lx [<%016lx>] %pS\n",
1408977ab65SVasily Gorbik 				    sp, sf->gprs[8], (void *)sf->gprs[8]);
1418977ab65SVasily Gorbik 		if (sf->back_chain <= sp)
1428977ab65SVasily Gorbik 			break;
1438977ab65SVasily Gorbik 		sp = sf->back_chain;
1448977ab65SVasily Gorbik 		first = false;
1458977ab65SVasily Gorbik 	}
1468977ab65SVasily Gorbik }
1478977ab65SVasily Gorbik 
148724dc336SVasily Gorbik void print_pgm_check_info(void)
149724dc336SVasily Gorbik {
1509a78c70aSVasily Gorbik 	unsigned long *gpregs = (unsigned long *)S390_lowcore.gpregs_save_area;
151724dc336SVasily Gorbik 	struct psw_bits *psw = &psw_bits(S390_lowcore.psw_save_area);
152724dc336SVasily Gorbik 
1539a78c70aSVasily Gorbik 	decompressor_printk("Linux version %s\n", kernel_version);
154*ba1a6be9SVasily Gorbik 	if (!is_prot_virt_guest() && early_command_line[0])
155*ba1a6be9SVasily Gorbik 		decompressor_printk("Kernel command line: %s\n", early_command_line);
1569a78c70aSVasily Gorbik 	decompressor_printk("Kernel fault: interruption code %04x ilc:%x\n",
1579a78c70aSVasily Gorbik 			    S390_lowcore.pgm_code, S390_lowcore.pgm_ilc >> 1);
1589a78c70aSVasily Gorbik 	if (kaslr_enabled)
1599a78c70aSVasily Gorbik 		decompressor_printk("Kernel random base: %lx\n", __kaslr_offset);
16024621896SVasily Gorbik 	decompressor_printk("PSW : %016lx %016lx (%pS)\n",
1619a78c70aSVasily Gorbik 			    S390_lowcore.psw_save_area.mask,
16224621896SVasily Gorbik 			    S390_lowcore.psw_save_area.addr,
16324621896SVasily Gorbik 			    (void *)S390_lowcore.psw_save_area.addr);
1649a78c70aSVasily Gorbik 	decompressor_printk(
1659a78c70aSVasily Gorbik 		"      R:%x T:%x IO:%x EX:%x Key:%x M:%x W:%x P:%x AS:%x CC:%x PM:%x RI:%x EA:%x\n",
1669a78c70aSVasily Gorbik 		psw->per, psw->dat, psw->io, psw->ext, psw->key, psw->mcheck,
1679a78c70aSVasily Gorbik 		psw->wait, psw->pstate, psw->as, psw->cc, psw->pm, psw->ri,
1689a78c70aSVasily Gorbik 		psw->eaba);
1699a78c70aSVasily Gorbik 	decompressor_printk("GPRS: %016lx %016lx %016lx %016lx\n",
1709a78c70aSVasily Gorbik 			    gpregs[0], gpregs[1], gpregs[2], gpregs[3]);
1719a78c70aSVasily Gorbik 	decompressor_printk("      %016lx %016lx %016lx %016lx\n",
1729a78c70aSVasily Gorbik 			    gpregs[4], gpregs[5], gpregs[6], gpregs[7]);
1739a78c70aSVasily Gorbik 	decompressor_printk("      %016lx %016lx %016lx %016lx\n",
1749a78c70aSVasily Gorbik 			    gpregs[8], gpregs[9], gpregs[10], gpregs[11]);
1759a78c70aSVasily Gorbik 	decompressor_printk("      %016lx %016lx %016lx %016lx\n",
1769a78c70aSVasily Gorbik 			    gpregs[12], gpregs[13], gpregs[14], gpregs[15]);
1778977ab65SVasily Gorbik 	print_stacktrace();
178*ba1a6be9SVasily Gorbik 	decompressor_printk("Last Breaking-Event-Address:\n");
179*ba1a6be9SVasily Gorbik 	decompressor_printk(" [<%016lx>] %pS\n", (unsigned long)S390_lowcore.breaking_event_addr,
180*ba1a6be9SVasily Gorbik 			    (void *)S390_lowcore.breaking_event_addr);
181724dc336SVasily Gorbik }
182