From 0c0cd7e2f895f76f0f8f102dd123602f203d98f2 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Wed, 12 Apr 2023 09:58:20 +0000 Subject: [PATCH] crash(8): Add powerpc support. --- sys/arch/powerpc/powerpc/db_disasm.c | 146 ++++++++++++--------------- sys/arch/powerpc/powerpc/db_trace.c | 18 +++- usr.sbin/crash/Makefile | 8 +- usr.sbin/crash/arch/generic.c | 2 + usr.sbin/crash/arch/powerpc.c | 66 ++++++++++++ usr.sbin/crash/arch/powerpc64.c | 1 + usr.sbin/crash/crash.c | 4 +- 7 files changed, 157 insertions(+), 88 deletions(-) create mode 100644 usr.sbin/crash/arch/powerpc.c create mode 100644 usr.sbin/crash/arch/powerpc64.c diff --git a/sys/arch/powerpc/powerpc/db_disasm.c b/sys/arch/powerpc/powerpc/db_disasm.c index 83fd6e2c2d68..077d4e840798 100644 --- a/sys/arch/powerpc/powerpc/db_disasm.c +++ b/sys/arch/powerpc/powerpc/db_disasm.c @@ -9,6 +9,8 @@ __KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.29 2020/07/06 10:31:24 rin Exp $"); #endif #include +#include + #include #include @@ -84,8 +86,7 @@ typedef u_int32_t instr_t; typedef void (op_class_func) (instr_t, vaddr_t); u_int32_t extract_field(u_int32_t value, u_int32_t base, u_int32_t width); -void disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, - char *disasm_str, size_t slen); +void disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc); void dis_ppc(const struct opcode *opcodeset, instr_t instr, vaddr_t loc); op_class_func op_ill, op_base; @@ -412,7 +413,7 @@ const struct opcode opcodes_3f[] = { struct specialreg { - int reg; + unsigned reg; const char *name; }; @@ -619,22 +620,9 @@ extract_field(u_int32_t value, u_int32_t base, u_int32_t width) const struct opcode * search_op(const struct opcode *); void -disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, - char *disasm_str, size_t slen) +disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc) { - char * pstr; enum function_mask func; - int len; - -#define ADD_LEN(s) do { \ - len = (s); \ - slen -= len; \ - pstr += len; \ - } while(0) -#define APP_PSTR(fmt, arg) ADD_LEN(snprintf(pstr, slen, (fmt), (arg))) -#define APP_PSTRS(fmt) ADD_LEN(snprintf(pstr, slen, "%s", (fmt))) - - pstr = disasm_str; func = popcode->func; if (func & Op_BC) { @@ -646,12 +634,13 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, /* standard, no decrement */ if (BO & 16) { if (popcode->code == 0x40000000) { - APP_PSTRS("c"); + db_printf("c"); func |= Op_BO | Op_BI; } } else { - APP_PSTRS(condstr[((BO & 8) >> 1) + (BI & 3)]); + db_printf("%s", + condstr[((BO & 8) >> 1) + (BI & 3)]); if (BI >= 4) func |= Op_crfS; } @@ -659,20 +648,20 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, else { /* decrement and branch */ if (BO & 2) - APP_PSTRS("dz"); + db_printf("dz"); else - APP_PSTRS("dnz"); + db_printf("dnz"); if ((BO & 24) == 0) - APP_PSTRS("f"); + db_printf("f"); else if ((BO & 24) == 8) - APP_PSTRS("t"); + db_printf("t"); else func |= Op_BI; } if (popcode->code == 0x4c000020) - APP_PSTRS("lr"); + db_printf("lr"); else if (popcode->code == 0x4c000420) - APP_PSTRS("ctr"); + db_printf("ctr"); if ((BO & 20) != 20 && (func & Op_BO) == 0) func |= Op_BP; /* branch prediction hint */ } @@ -680,26 +669,26 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, u_int OE; OE = extract_field(instr, 31 - 21, 1); if (OE) { - APP_PSTRS("o"); + db_printf("o"); } func &= ~Op_OE; } switch (func & Op_LKM) { case Op_Rc: if (instr & 0x1) - APP_PSTRS("."); + db_printf("."); break; case Op_AA: if (instr & 0x1) - APP_PSTRS("l"); + db_printf("l"); if (instr & 0x2) { - APP_PSTRS("a"); + db_printf("a"); loc = 0; /* Absolute address */ } break; case Op_LK: if (instr & 0x1) - APP_PSTRS("l"); + db_printf("l"); break; default: func &= ~Op_LKM; @@ -716,91 +705,91 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, if ((vaddr_t)BD < loc) y ^= 1; } - APP_PSTR("%c", y ? '+' : '-'); + db_printf("%c", y ? '+' : '-'); func &= ~Op_BP; } - APP_PSTRS("\t"); + db_printf("\t"); /* XXX: special cases here, out of flags in a 32bit word. */ if (strcmp(popcode->name, "wrteei") == 0) { int E; E = extract_field(instr, 31 - 16, 5); - APP_PSTR("%d", E); + db_printf("%d", E); return; } else if (strcmp(popcode->name, "mtfsfi") == 0) { u_int UI; UI = extract_field(instr, 31 - 8, 3); - APP_PSTR("crf%u, ", UI); + db_printf("crf%u, ", UI); UI = extract_field(instr, 31 - 19, 4); - APP_PSTR("0x%x", UI); + db_printf("0x%x", UI); } /* XXX: end of special cases here. */ if ((func & Op_FM) == Op_FM) { u_int FM; FM = extract_field(instr, 31 - 14, 8); - APP_PSTR("0x%x, ", FM); + db_printf("0x%x, ", FM); func &= ~Op_FM; } if (func & Op_D) { /* Op_ST is the same */ u_int D; D = extract_field(instr, 31 - 10, 5); - APP_PSTR("r%d, ", D); + db_printf("r%d, ", D); func &= ~Op_D; } if (func & Op_crbD) { u_int crbD; crbD = extract_field(instr, 31 - 10, 5); - APP_PSTR("crb%d, ", crbD); + db_printf("crb%d, ", crbD); func &= ~Op_crbD; } if (func & Op_crfD) { u_int crfD; crfD = extract_field(instr, 31 - 8, 3); - APP_PSTR("crf%d, ", crfD); + db_printf("crf%d, ", crfD); func &= ~Op_crfD; } if (func & Op_TO) { u_int TO; TO = extract_field(instr, 31 - 10, 1); - APP_PSTR("%d, ", TO); + db_printf("%d, ", TO); func &= ~Op_TO; } if (func & Op_crfS) { u_int crfS; crfS = extract_field(instr, 31 - 13, 3); - APP_PSTR("crf%d, ", crfS); + db_printf("crf%d, ", crfS); func &= ~Op_crfS; } if (func & Op_CRM) { u_int CRM; CRM = extract_field(instr, 31 - 19, 8); - APP_PSTR("0x%x, ", CRM); + db_printf("0x%x, ", CRM); func &= ~Op_CRM; } if (func & Op_BO) { u_int BO; BO = extract_field(instr, 31 - 10, 5); - APP_PSTR("%d, ", BO); + db_printf("%d, ", BO); func &= ~Op_BO; } if (func & Op_BI) { u_int BI; BI = extract_field(instr, 31 - 15, 5); - APP_PSTR("%d, ", BI); + db_printf("%d, ", BI); func &= ~Op_BI; } if (func & Op_dA) { /* register A indirect with displacement */ u_int A; A = extract_field(instr, 31 - 31, 16); if (A & 0x8000) { - APP_PSTRS("-"); + db_printf("-"); A = 0x10000-A; } - APP_PSTR("0x%x", A); + db_printf("0x%x", A); A = extract_field(instr, 31 - 15, 5); - APP_PSTR("(r%d)", A); + db_printf("(r%d)", A); func &= ~Op_dA; } if (func & Op_spr) { @@ -822,56 +811,56 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, if (spr == regs[i].reg) break; if (regs[i].name == NULL) - APP_PSTR("[unknown special reg (%d)]", spr); + db_printf("[unknown special reg (%d)]", spr); else - APP_PSTR("%s", regs[i].name); + db_printf("%s", regs[i].name); if (popcode->name[1] == 't') /* spr is destination */ - APP_PSTRS(", "); + db_printf(", "); func &= ~Op_spr; } if (func & Op_SR) { u_int SR; SR = extract_field(instr, 31 - 15, 3); - APP_PSTR("sr%d", SR); + db_printf("sr%d", SR); if (popcode->name[1] == 't') /* SR is destination */ - APP_PSTRS(", "); + db_printf(", "); func &= ~Op_SR; } if (func & Op_A) { u_int A; A = extract_field(instr, 31 - 15, 5); - APP_PSTR("r%d, ", A); + db_printf("r%d, ", A); func &= ~Op_A; } if (func & Op_S) { u_int D; D = extract_field(instr, 31 - 10, 5); - APP_PSTR("r%d, ", D); + db_printf("r%d, ", D); func &= ~Op_S; } if (func & Op_C) { u_int C; C = extract_field(instr, 31 - 25, 5); - APP_PSTR("r%d, ", C); + db_printf("r%d, ", C); func &= ~Op_C; } if (func & Op_B) { u_int B; B = extract_field(instr, 31 - 20, 5); - APP_PSTR("r%d", B); + db_printf("r%d", B); func &= ~Op_B; } if (func & Op_crbA) { u_int crbA; crbA = extract_field(instr, 31 - 15, 5); - APP_PSTR("%d, ", crbA); + db_printf("%d, ", crbA); func &= ~Op_crbA; } if (func & Op_crbB) { u_int crbB; crbB = extract_field(instr, 31 - 20, 5); - APP_PSTR("%d, ", crbB); + db_printf("%d, ", crbB); func &= ~Op_crbB; } if (func & Op_LI) { @@ -881,8 +870,7 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, LI = LI << 8; LI = LI >> 6; LI += loc; - db_symstr(pstr, slen, LI, DB_STGY_ANY); - ADD_LEN(strlen(pstr)); + db_printsym(LI, DB_STGY_ANY, db_printf); func &= ~Op_LI; } switch (func & Op_SIMM) { @@ -890,7 +878,7 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, case Op_SIMM: /* same as Op_d */ IMM = extract_field(instr, 31 - 31, 16); if (IMM & 0x8000) { - APP_PSTRS("-"); + db_printf("-"); IMM = 0x10000-IMM; } func &= ~Op_SIMM; @@ -900,7 +888,7 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, func &= ~Op_UIMM; goto common; common: - APP_PSTR("0x%x", IMM); + db_printf("0x%x", IMM); break; default: ; @@ -912,14 +900,13 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, BD = BD << 18; BD = BD >> 16; BD += loc; - db_symstr(pstr, slen, BD, DB_STGY_ANY); - ADD_LEN(strlen(pstr)); + db_printsym(BD, DB_STGY_ANY, db_printf); func &= ~Op_BD; } if (func & Op_ds) { u_int ds; ds = extract_field(instr, 31 - 29, 14) << 2; - APP_PSTR("0x%x", ds); + db_printf("0x%x", ds); func &= ~Op_ds; } if (func & Op_me) { @@ -927,42 +914,42 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, mel = extract_field(instr, 31 - 25, 4); meh = extract_field(instr, 31 - 26, 1); me = meh << 4 | mel; - APP_PSTR(", 0x%x", me); + db_printf(", 0x%x", me); func &= ~Op_me; } if ((func & Op_SH) && (func & Op_sh_mb_sh)) { u_int SH; SH = extract_field(instr, 31 - 20, 5); - APP_PSTR("%d", SH); + db_printf("%d", SH); } if ((func & Op_MB) && (func & Op_sh_mb_sh)) { u_int MB; u_int ME; MB = extract_field(instr, 31 - 25, 5); - APP_PSTR(", %d", MB); + db_printf(", %d", MB); ME = extract_field(instr, 31 - 30, 5); - APP_PSTR(", %d", ME); + db_printf(", %d", ME); } if ((func & Op_sh) && ! (func & Op_sh_mb_sh)) { u_int sh, shl, shh; shl = extract_field(instr, 31 - 19, 4); shh = extract_field(instr, 31 - 20, 1); sh = shh << 4 | shl; - APP_PSTR(", %d", sh); + db_printf(", %d", sh); } if ((func & Op_mb) && ! (func & Op_sh_mb_sh)) { u_int mb, mbl, mbh; mbl = extract_field(instr, 31 - 25, 4); mbh = extract_field(instr, 31 - 26, 1); mb = mbh << 4 | mbl; - APP_PSTR(", %d", mb); + db_printf(", %d", mb); } if ((func & Op_me) && ! (func & Op_sh_mb_sh)) { u_int me, mel, meh; mel = extract_field(instr, 31 - 25, 4); meh = extract_field(instr, 31 - 26, 1); me = meh << 4 | mel; - APP_PSTR(", %d", me); + db_printf(", %d", me); } if (func & Op_tbr) { u_int tbr; @@ -984,9 +971,9 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, reg = 0; } if (reg == 0) - APP_PSTR(", [unknown tbr %d ]", tbr); + db_printf(", [unknown tbr %d ]", tbr); else - APP_PSTR(", %s", reg); + db_printf(", %s", reg); func &= ~Op_tbr; } if (func & Op_NB) { @@ -994,12 +981,9 @@ disasm_fields(const struct opcode *popcode, instr_t instr, vaddr_t loc, NB = extract_field(instr, 31 - 20, 5); if (NB == 0) NB = 32; - APP_PSTR(", %d", NB); + db_printf(", %d", NB); func &= ~Op_SR; } -#undef ADD_LEN -#undef APP_PSTR -#undef APP_PSTRS } void @@ -1056,16 +1040,14 @@ dis_ppc(const struct opcode *opcodeset, instr_t instr, vaddr_t loc) const struct opcode *op; int found = 0; int i; - char disasm_str[80]; for (i = 0, op = &opcodeset[0]; found == 0 && op->mask != 0; i++, op = &opcodeset[i]) { if ((instr & op->mask) == op->code) { found = 1; - disasm_fields(op, instr, loc, disasm_str, - sizeof disasm_str); - db_printf("%s%s\n", op->name, disasm_str); + db_printf("%s", op->name); + disasm_fields(op, instr, loc); return; } } diff --git a/sys/arch/powerpc/powerpc/db_trace.c b/sys/arch/powerpc/powerpc/db_trace.c index bcb7ef481490..c71da4dc01a0 100644 --- a/sys/arch/powerpc/powerpc/db_trace.c +++ b/sys/arch/powerpc/powerpc/db_trace.c @@ -53,11 +53,19 @@ __KERNEL_RCSID(0, "$NetBSD: db_trace.c,v 1.60 2020/07/06 09:34:18 rin Exp $"); #elif defined(PPC_BOOKE) #include #else -#include unknown powerpc variants +#ifdef _KERNEL +#error unknown powerpc variants +#endif +#endif + +#ifndef _KERNEL /* crash(8) */ +#include +#define PAGE_SIZE ((unsigned)sysconf(_SC_PAGESIZE)) #endif #include #include +#include #include #include @@ -125,9 +133,13 @@ db_stack_trace_print(db_expr_t addr, bool have_addr, db_expr_t count, bool kernel_only = true; bool trace_thread = false; bool lwpaddr = false; +#ifdef _KERNEL extern int trapexit[], sctrapexit[]; #ifdef PPC_BOOKE extern int intrcall[]; +#endif +#else + extern void *trapexit, *sctrapexit, *intrcall; #endif bool full = false; bool in_kernel = true; @@ -157,7 +169,7 @@ db_stack_trace_print(db_expr_t addr, bool have_addr, db_expr_t count, (*pr)("trace: pid %d ", p->p_pid); } else { (*pr)("trace: pid %d ", (int)addr); - p = proc_find_raw(addr); + p = db_proc_find((pid_t)addr); if (p == NULL) { (*pr)("not found\n"); return; @@ -196,7 +208,7 @@ db_stack_trace_print(db_expr_t addr, bool have_addr, db_expr_t count, (*pr)("0x%08lx: ", frame); if (lr + 4 == (db_addr_t) trapexit || -#ifdef PPC_BOOKE +#if !defined(_KERNEL) || defined(PPC_BOOKE) lr + 4 == (db_addr_t) intrcall || #endif lr + 4 == (db_addr_t) sctrapexit) { diff --git a/usr.sbin/crash/Makefile b/usr.sbin/crash/Makefile index d9b9f0ce073e..7f28de1c912a 100644 --- a/usr.sbin/crash/Makefile +++ b/usr.sbin/crash/Makefile @@ -21,12 +21,16 @@ DPADD+= ${LIBUTIL} ${LIBKVM} ${LIBEDIT} ${LIBTERMINFO} || ${MACHINE_CPU} == "arm" \ || ${MACHINE_CPU} == "aarch64" \ || ${MACHINE_CPU} == "mips" \ - || ${MACHINE_ARCH} == "m68k" + || ${MACHINE_ARCH} == "m68k" \ + || ${MACHINE_ARCH} == "powerpc" \ + || ${MACHINE_ARCH} == "powerpc64" SRCS+= db_trace.c .if ${MACHINE_CPU} == "mips" SRCS+= db_interface.c mips_stacktrace.c CPPFLAGS+= -DDDB -.elif ${MACHINE_ARCH} != "m68k" +.elif ${MACHINE_ARCH} != "m68k" \ + && ${MACHINE_ARCH} != "powerpc" \ + && ${MACHINE_ARCH} != "powerpc64" SRCS+= db_machdep.c .endif REALCRASH=yes diff --git a/usr.sbin/crash/arch/generic.c b/usr.sbin/crash/arch/generic.c index a53aa935a135..901670fcbea5 100644 --- a/usr.sbin/crash/arch/generic.c +++ b/usr.sbin/crash/arch/generic.c @@ -34,6 +34,8 @@ __RCSID("$NetBSD: generic.c,v 1.1 2009/03/07 22:08:08 ad Exp $"); #endif /* not lint */ +#include + #include #include diff --git a/usr.sbin/crash/arch/powerpc.c b/usr.sbin/crash/arch/powerpc.c new file mode 100644 index 000000000000..f2698596a6ce --- /dev/null +++ b/usr.sbin/crash/arch/powerpc.c @@ -0,0 +1,66 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2023 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__RCSID("$NetBSD$"); + +#include +#include +#include + +#include "extern.h" + +enum { + N_TRAPEXIT, + N_SCTRAPEXIT, + N_INTRCALL, + N +}; + +void *trapexit; +void *sctrapexit; +void *intrcall; + +static struct nlist nl[] = { + [N_TRAPEXIT] = { .n_name = "trapexit" }, + [N_SCTRAPEXIT] = { .n_name = "sctrapexit" }, + [N_INTRCALL] = { .n_name = "intrcall" }, + [N] = { .n_name = NULL }, +}; + +void +db_mach_init(kvm_t *kd) +{ + + if (kvm_nlist(kd, nl) == -1) + errx(EXIT_FAILURE, "kvm_nlist: %s", kvm_geterr(kd)); + + trapexit = (void *)nl[N_TRAPEXIT].n_value; + sctrapexit = (void *)nl[N_SCTRAPEXIT].n_value; + intrcall = (void *)nl[N_INTRCALL].n_value; +} diff --git a/usr.sbin/crash/arch/powerpc64.c b/usr.sbin/crash/arch/powerpc64.c new file mode 100644 index 000000000000..f4681331803b --- /dev/null +++ b/usr.sbin/crash/arch/powerpc64.c @@ -0,0 +1 @@ +#include "powerpc.c" diff --git a/usr.sbin/crash/crash.c b/usr.sbin/crash/crash.c index ea9073f07d71..1f1fcc182ebc 100644 --- a/usr.sbin/crash/crash.c +++ b/usr.sbin/crash/crash.c @@ -34,7 +34,7 @@ __RCSID("$NetBSD: crash.c,v 1.14 2020/08/17 04:15:33 mrg Exp $"); #endif /* not lint */ -#include +#include #include #include @@ -45,6 +45,8 @@ __RCSID("$NetBSD: crash.c,v 1.14 2020/08/17 04:15:33 mrg Exp $"); #include #endif +#include + #include #include #include