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> 5*8977ab65SVasily Gorbik #include <asm/stacktrace.h> 6724dc336SVasily Gorbik #include <asm/lowcore.h> 73ca8b855SVasily Gorbik #include <asm/setup.h> 8724dc336SVasily Gorbik #include <asm/sclp.h> 99a78c70aSVasily Gorbik #include <stdarg.h> 10724dc336SVasily Gorbik #include "boot.h" 11724dc336SVasily Gorbik 12724dc336SVasily Gorbik const char hex_asc[] = "0123456789abcdef"; 13724dc336SVasily Gorbik 149a78c70aSVasily Gorbik static char *as_hex(char *dst, unsigned long val, int pad) 15724dc336SVasily Gorbik { 169a78c70aSVasily Gorbik char *p, *end = p = dst + max(pad, (int)__fls(val | 1) / 4 + 1); 179a78c70aSVasily Gorbik 189a78c70aSVasily Gorbik for (*p-- = 0; p >= dst; val >>= 4) 199a78c70aSVasily Gorbik *p-- = hex_asc[val & 0x0f]; 209a78c70aSVasily Gorbik return end; 21724dc336SVasily Gorbik } 22724dc336SVasily Gorbik 2324621896SVasily Gorbik static char *symstart(char *p) 2424621896SVasily Gorbik { 2524621896SVasily Gorbik while (*p) 2624621896SVasily Gorbik p--; 2724621896SVasily Gorbik return p + 1; 2824621896SVasily Gorbik } 2924621896SVasily Gorbik 3024621896SVasily Gorbik extern char _decompressor_syms_start[], _decompressor_syms_end[]; 3124621896SVasily Gorbik static noinline char *findsym(unsigned long ip, unsigned short *off, unsigned short *len) 3224621896SVasily Gorbik { 3324621896SVasily Gorbik /* symbol entries are in a form "10000 c4 startup\0" */ 3424621896SVasily Gorbik char *a = _decompressor_syms_start; 3524621896SVasily Gorbik char *b = _decompressor_syms_end; 3624621896SVasily Gorbik unsigned long start; 3724621896SVasily Gorbik unsigned long size; 3824621896SVasily Gorbik char *pivot; 3924621896SVasily Gorbik char *endp; 4024621896SVasily Gorbik 4124621896SVasily Gorbik while (a < b) { 4224621896SVasily Gorbik pivot = symstart(a + (b - a) / 2); 4324621896SVasily Gorbik start = simple_strtoull(pivot, &endp, 16); 4424621896SVasily Gorbik size = simple_strtoull(endp + 1, &endp, 16); 4524621896SVasily Gorbik if (ip < start) { 4624621896SVasily Gorbik b = pivot; 4724621896SVasily Gorbik continue; 4824621896SVasily Gorbik } 4924621896SVasily Gorbik if (ip > start + size) { 5024621896SVasily Gorbik a = pivot + strlen(pivot) + 1; 5124621896SVasily Gorbik continue; 5224621896SVasily Gorbik } 5324621896SVasily Gorbik *off = ip - start; 5424621896SVasily Gorbik *len = size; 5524621896SVasily Gorbik return endp + 1; 5624621896SVasily Gorbik } 5724621896SVasily Gorbik return NULL; 5824621896SVasily Gorbik } 5924621896SVasily Gorbik 6024621896SVasily Gorbik static noinline char *strsym(void *ip) 6124621896SVasily Gorbik { 6224621896SVasily Gorbik static char buf[64]; 6324621896SVasily Gorbik unsigned short off; 6424621896SVasily Gorbik unsigned short len; 6524621896SVasily Gorbik char *p; 6624621896SVasily Gorbik 6724621896SVasily Gorbik p = findsym((unsigned long)ip, &off, &len); 6824621896SVasily Gorbik if (p) { 6924621896SVasily Gorbik strncpy(buf, p, sizeof(buf)); 7024621896SVasily Gorbik /* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */ 7124621896SVasily Gorbik p = buf + strnlen(buf, sizeof(buf) - 15); 7224621896SVasily Gorbik strcpy(p, "+0x"); 7324621896SVasily Gorbik p = as_hex(p + 3, off, 0); 7424621896SVasily Gorbik strcpy(p, "/0x"); 7524621896SVasily Gorbik as_hex(p + 3, len, 0); 7624621896SVasily Gorbik } else { 7724621896SVasily Gorbik as_hex(buf, (unsigned long)ip, 16); 7824621896SVasily Gorbik } 7924621896SVasily Gorbik return buf; 8024621896SVasily Gorbik } 8124621896SVasily Gorbik 829a78c70aSVasily Gorbik void decompressor_printk(const char *fmt, ...) 83724dc336SVasily Gorbik { 849a78c70aSVasily Gorbik char buf[1024] = { 0 }; 859a78c70aSVasily Gorbik char *end = buf + sizeof(buf) - 1; /* make sure buf is 0 terminated */ 869a78c70aSVasily Gorbik unsigned long pad; 879a78c70aSVasily Gorbik char *p = buf; 889a78c70aSVasily Gorbik va_list args; 899a78c70aSVasily Gorbik 909a78c70aSVasily Gorbik va_start(args, fmt); 919a78c70aSVasily Gorbik for (; p < end && *fmt; fmt++) { 929a78c70aSVasily Gorbik if (*fmt != '%') { 939a78c70aSVasily Gorbik *p++ = *fmt; 949a78c70aSVasily Gorbik continue; 959a78c70aSVasily Gorbik } 969a78c70aSVasily Gorbik pad = isdigit(*++fmt) ? simple_strtol(fmt, (char **)&fmt, 10) : 0; 979a78c70aSVasily Gorbik switch (*fmt) { 989a78c70aSVasily Gorbik case 's': 999a78c70aSVasily Gorbik p = buf + strlcat(buf, va_arg(args, char *), sizeof(buf)); 1009a78c70aSVasily Gorbik break; 10124621896SVasily Gorbik case 'p': 10224621896SVasily Gorbik if (*++fmt != 'S') 10324621896SVasily Gorbik goto out; 10424621896SVasily Gorbik p = buf + strlcat(buf, strsym(va_arg(args, void *)), sizeof(buf)); 10524621896SVasily Gorbik break; 1069a78c70aSVasily Gorbik case 'l': 1079a78c70aSVasily Gorbik if (*++fmt != 'x' || end - p <= max(sizeof(long) * 2, pad)) 1089a78c70aSVasily Gorbik goto out; 1099a78c70aSVasily Gorbik p = as_hex(p, va_arg(args, unsigned long), pad); 1109a78c70aSVasily Gorbik break; 1119a78c70aSVasily Gorbik case 'x': 1129a78c70aSVasily Gorbik if (end - p <= max(sizeof(int) * 2, pad)) 1139a78c70aSVasily Gorbik goto out; 1149a78c70aSVasily Gorbik p = as_hex(p, va_arg(args, unsigned int), pad); 1159a78c70aSVasily Gorbik break; 1169a78c70aSVasily Gorbik default: 1179a78c70aSVasily Gorbik goto out; 1189a78c70aSVasily Gorbik } 1199a78c70aSVasily Gorbik } 1209a78c70aSVasily Gorbik out: 1219a78c70aSVasily Gorbik va_end(args); 1229a78c70aSVasily Gorbik sclp_early_printk(buf); 123724dc336SVasily Gorbik } 124724dc336SVasily Gorbik 125*8977ab65SVasily Gorbik static noinline void print_stacktrace(void) 126*8977ab65SVasily Gorbik { 127*8977ab65SVasily Gorbik struct stack_info boot_stack = { STACK_TYPE_TASK, BOOT_STACK_OFFSET, 128*8977ab65SVasily Gorbik BOOT_STACK_OFFSET + BOOT_STACK_SIZE }; 129*8977ab65SVasily Gorbik unsigned long sp = S390_lowcore.gpregs_save_area[15]; 130*8977ab65SVasily Gorbik bool first = true; 131*8977ab65SVasily Gorbik 132*8977ab65SVasily Gorbik decompressor_printk("Call Trace:\n"); 133*8977ab65SVasily Gorbik while (!(sp & 0x7) && on_stack(&boot_stack, sp, sizeof(struct stack_frame))) { 134*8977ab65SVasily Gorbik struct stack_frame *sf = (struct stack_frame *)sp; 135*8977ab65SVasily Gorbik 136*8977ab65SVasily Gorbik decompressor_printk(first ? "(sp:%016lx [<%016lx>] %pS)\n" : 137*8977ab65SVasily Gorbik " sp:%016lx [<%016lx>] %pS\n", 138*8977ab65SVasily Gorbik sp, sf->gprs[8], (void *)sf->gprs[8]); 139*8977ab65SVasily Gorbik if (sf->back_chain <= sp) 140*8977ab65SVasily Gorbik break; 141*8977ab65SVasily Gorbik sp = sf->back_chain; 142*8977ab65SVasily Gorbik first = false; 143*8977ab65SVasily Gorbik } 144*8977ab65SVasily Gorbik } 145*8977ab65SVasily Gorbik 146724dc336SVasily Gorbik void print_pgm_check_info(void) 147724dc336SVasily Gorbik { 1489a78c70aSVasily Gorbik unsigned long *gpregs = (unsigned long *)S390_lowcore.gpregs_save_area; 149724dc336SVasily Gorbik struct psw_bits *psw = &psw_bits(S390_lowcore.psw_save_area); 150724dc336SVasily Gorbik 1519a78c70aSVasily Gorbik decompressor_printk("Linux version %s\n", kernel_version); 1529a78c70aSVasily Gorbik decompressor_printk("Kernel fault: interruption code %04x ilc:%x\n", 1539a78c70aSVasily Gorbik S390_lowcore.pgm_code, S390_lowcore.pgm_ilc >> 1); 1549a78c70aSVasily Gorbik if (kaslr_enabled) 1559a78c70aSVasily Gorbik decompressor_printk("Kernel random base: %lx\n", __kaslr_offset); 15624621896SVasily Gorbik decompressor_printk("PSW : %016lx %016lx (%pS)\n", 1579a78c70aSVasily Gorbik S390_lowcore.psw_save_area.mask, 15824621896SVasily Gorbik S390_lowcore.psw_save_area.addr, 15924621896SVasily Gorbik (void *)S390_lowcore.psw_save_area.addr); 1609a78c70aSVasily Gorbik decompressor_printk( 1619a78c70aSVasily 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", 1629a78c70aSVasily Gorbik psw->per, psw->dat, psw->io, psw->ext, psw->key, psw->mcheck, 1639a78c70aSVasily Gorbik psw->wait, psw->pstate, psw->as, psw->cc, psw->pm, psw->ri, 1649a78c70aSVasily Gorbik psw->eaba); 1659a78c70aSVasily Gorbik decompressor_printk("GPRS: %016lx %016lx %016lx %016lx\n", 1669a78c70aSVasily Gorbik gpregs[0], gpregs[1], gpregs[2], gpregs[3]); 1679a78c70aSVasily Gorbik decompressor_printk(" %016lx %016lx %016lx %016lx\n", 1689a78c70aSVasily Gorbik gpregs[4], gpregs[5], gpregs[6], gpregs[7]); 1699a78c70aSVasily Gorbik decompressor_printk(" %016lx %016lx %016lx %016lx\n", 1709a78c70aSVasily Gorbik gpregs[8], gpregs[9], gpregs[10], gpregs[11]); 1719a78c70aSVasily Gorbik decompressor_printk(" %016lx %016lx %016lx %016lx\n", 1729a78c70aSVasily Gorbik gpregs[12], gpregs[13], gpregs[14], gpregs[15]); 173*8977ab65SVasily Gorbik print_stacktrace(); 174724dc336SVasily Gorbik } 175