From patchwork Fri Jan 13 14:45:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 13100883 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0727EC54EBD for ; Fri, 13 Jan 2023 14:46:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=yLXm2WctcRV8y/+HWcgyaFhf1SaAN8z6gCmf1BhgVNc=; b=tOxnNm4s/mL7h0 tImIRDURAMUmYXN23DcJ+DX0RdTpRI3kMND41fOdzg/f7aNJt9H0MKv7iKEgt6kV11LDJi8d7FdmG BMuH5RUCuWTLrtuDOJBZeWiVsx+dKTwMjAKCUC20U7XUBaCvGzt4Sq/JTIEK7enhG6zgi4NoFVsch mydOsjJ+PDN4TDUSBi16Qx0Ryssa9Ry/KtgrxFB7FNXiNa/TNbfhOQa87tzlUtyhrNrzj7Qy7kND6 dhnP2W7ILf+jn/zItFSvY+0ueF97jEUJAXS6ZexVresyYqTYmpYO0PjraWa/lerSkBn8HFwYizjNu qShrmxxI01L9IknO4vGg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pGLJy-0032GG-OD; Fri, 13 Jan 2023 14:46:18 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pGLJt-0032DT-1P for linux-riscv@lists.infradead.org; Fri, 13 Jan 2023 14:46:15 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CE2A961F9C; Fri, 13 Jan 2023 14:46:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC0C6C433F0; Fri, 13 Jan 2023 14:46:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1673621171; bh=bhJdLBWsY2gx6AsQzO84a64x8hRuQO6VoHRS+yuhugs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s37cdjjiAP/akoDJK2wu0NF7+p/MLPgPilAOkQTl+i/26fR21U3j0u4aKsZL+s1U+ czJZcY5v1zBI/cqW0bFJNLzjJ+nOCmfckeL8rrNQ3S1J5PUqeJyaaNbgbk71Qjrmdw CpNX5CO6T0GYLQ+eYb+QcxTHQ7FAEN90OcMJW53TJu5bLBkZ7Rxw3VU9//BQspNulY ht+TGdlf2bXTK8y/MYUahtdXNXvvC9L2BP+Tywak1rV66cpn07VviJsJDQFsar3f6y 8wOvJSvKodf9J5WfZeBzjbFmjGRnOhjA6OD9582g3fSElauRNthEBQi6IkpzsZSvWG aBZUEvY6qE3aA== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] riscv: Add "Code:" to RISC-V splats Date: Fri, 13 Jan 2023 15:45:51 +0100 Message-Id: <20230113144552.138081-2-bjorn@kernel.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113144552.138081-1-bjorn@kernel.org> References: <20230113144552.138081-1-bjorn@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230113_064613_216509_5F298BC1 X-CRM114-Status: GOOD ( 15.37 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Björn Töpel Add "Code:" output to RISC-V splats. Mimic x86-64's byte-for-byte dumps. An example: Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 Oops [#1] Modules linked in: CPU: 1 PID: 1 Comm: swapper/0 Not tainted 6.2.0-rc3-00063-g0ce8b1377b2b-dirty #3 Hardware name: riscv-virtio,qemu (DT) epc : kernel_init+0xc8/0x10e ra : kernel_init+0x70/0x10e epc : ffffffff80bd9938 ra : ffffffff80bd98e0 sp : ff2000000060bec0 gp : ffffffff81730b28 tp : ff6000007ff00000 t0 : 7974697275636573 t1 : 0000000000000000 t2 : 3030303270393d6e s0 : ff2000000060bee0 s1 : ffffffff81732028 a0 : 0000000000000000 a1 : ff6000008157e600 a2 : 0000000000000002 a3 : ffffffff8176a470 a4 : 0000000000000000 a5 : 000000000000000a a6 : 0000000000000118 a7 : ff6000008157e600 s2 : 0000000000000000 s3 : 0000000000000000 s4 : 0000000000000000 s5 : 0000000000000000 s6 : 0000000000000000 s7 : 0000000000000000 s8 : 0000000000000000 s9 : 0000000000000000 s10: 0000000000000000 s11: 0000000000000000 t3 : ffffffff81185ff0 t4 : 0000000000000022 t5 : 000000000000003d t6 : 0000000000000000 status: 0000000200000120 badaddr: 0000000000000000 cause: 000000000000000f [] ret_from_exception+0x0/0x16 Code: 2a 86 79 d1 8c 60 17 a5 69 00 13 05 65 38 ef d0 2e db a9 47 <1c> c1 17 a5 ---[ end trace 0000000000000000 ]--- Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b SMP: stopping secondary CPUs ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b ]--- Signed-off-by: Björn Töpel --- arch/riscv/kernel/traps.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 549bde5c970a..efadff4190e0 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -29,6 +29,33 @@ int show_unhandled_signals = 1; static DEFINE_SPINLOCK(die_lock); +static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs) +{ +#define PROLOGUE_SIZE 20 +#define EPILOGUE_SIZE 3 +#define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE) + u8 opcodes[OPCODE_BUFSIZE]; + unsigned long prologue = regs->epc - PROLOGUE_SIZE; + + if (user_mode(regs)) + return; + + switch (copy_from_kernel_nofault(opcodes, (u8 *)prologue, sizeof(opcodes))) { + case 0: + printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %" + __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes, + opcodes[PROLOGUE_SIZE], opcodes + PROLOGUE_SIZE + 1); + break; + case -EPERM: + /* No access to the user space stack of other tasks. Ignore. */ + break; + default: + printk("%sCode: Unable to access opcode bytes at 0x%lx.\n", + loglvl, prologue); + break; + } +} + void die(struct pt_regs *regs, const char *str) { static int die_counter; @@ -43,8 +70,10 @@ void die(struct pt_regs *regs, const char *str) pr_emerg("%s [#%d]\n", str, ++die_counter); print_modules(); - if (regs) + if (regs) { show_regs(regs); + dump_kernel_instr(KERN_EMERG, regs); + } cause = regs ? regs->cause : -1; ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV); From patchwork Fri Jan 13 14:45:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 13100882 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C2EE8C54EBE for ; Fri, 13 Jan 2023 14:46:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=BHZb7nv1e8NWmtrEh8Tlk85vPkas8FvuLejpW/LBW9U=; b=v+2cuD87nX3QNt ZHeO9/un6IsuOXyygP2ZdU6RPXT8FuWtaGlo/wCpISP7x6pY420IgMdMZSIafFjqD9TZA1+5+0izI eAzbXFgaGJWvW9mbFWUuDZnbUvPZtFepelWHwBA8yp3MwJdFVcjsMvsKeadYhs8IDSNiFdcnHVoZZ Dh4zv/3JPNH01A3QVRsPdUbX6E4CiF3wxjamunWZ9U2Y7i/NZnAUA//uLae+q57z+FfEkgOgd3llT K/0yY4u6kedn1DmzDlB9ux7H3tzYT2RWfODYRVNNGszZ8xcM5OvjkfFs+vwl8/Bc63KccAdX7Mqei iXKCZDO1hM5JQEbMdW1w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pGLK0-0032Gk-9s; Fri, 13 Jan 2023 14:46:20 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pGLJw-0032ES-72 for linux-riscv@lists.infradead.org; Fri, 13 Jan 2023 14:46:18 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B9716B820C9; Fri, 13 Jan 2023 14:46:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7B87C433D2; Fri, 13 Jan 2023 14:46:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1673621173; bh=uVNiYN015zOKGYAR63hxg0e1aLPV1U+u9Ab5GMB5AGc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vNB4FYpN1R5PNtOd0/xoHymyzhJfoSvNDyhNH6pomXpBHz6C5FA5rYzkMEgM2qolb PP3FcgRXKYfCReSGva3gI983FKqej3Y3C16Lq71ULLpXFZTqds0vPVPwckIp/oGRC/ Tc06UIHS4zVhWoWLu/oZ1ppLnvAISGUdi1rNvJ4aK4XKKAa+r8+SWg9TaQx7ZFg789 QWagfHjInlSw+xruSyI9Fsfdo0uVi/99jSR+yJDgTtEsWeLh4Z67btkwlapRRZlx9X i4WEam9AWc6O79NFg+yi9QpxuCXSt8gefltBDwjxl37y4sLrQiq2yTGL5pjykZRNkZ DspERoJ/E7rqw== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] scripts/decodecode: Add support for RISC-V Date: Fri, 13 Jan 2023 15:45:52 +0100 Message-Id: <20230113144552.138081-3-bjorn@kernel.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230113144552.138081-1-bjorn@kernel.org> References: <20230113144552.138081-1-bjorn@kernel.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230113_064616_609757_49F80D22 X-CRM114-Status: GOOD ( 13.26 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Björn Töpel RISC-V has some GNU disassembly quirks, e.g. it requires '-D' to properly disassemble .byte directives similar to Arm [1]. Further, GNU objdump groups RISC-V instruction by 2 or 4 byte chunks, instead doing byte-for-byte. Add the required switches, and translate from short/word to bytes when ARCH is "riscv". An example how to invoke decodecode for RISC-V: $ echo 'Code: 64 c9 29 a0 17 d4 1f 01 13 04 44 b8 93 92 38 00 16 94 00 60 <02> 94 aa e8' | \ AFLAGS="-march=rv64imac_zicbom_zihintpause" \ ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- ./scripts/decodecode Code: 64 c9 29 a0 17 d4 1f 01 13 04 44 b8 93 92 38 00 16 94 00 60 <02> 94 aa e8 All code ======== 0: c964 c.sw s1,84(a0) 2: a029 c.j c <.text+0xc> 4: 011fd417 auipc s0,0x11fd 8: b8440413 addi s0,s0,-1148 # 11fcb88 <.text+0x11fcb88> c: 00389293 slli t0,a7,0x3 10: 9416 c.add s0,t0 12: 6000 c.ld s0,0(s0) 14:* 9402 c.jalr s0 <-- trapping instruction 16: e8aa c.sdsp a0,80(sp) Code starting with the faulting instruction =========================================== 0: 9402 c.jalr s0 2: e8aa c.sdsp a0,80(sp) [1] https://sourceware.org/bugzilla/show_bug.cgi?id=10263 Signed-off-by: Björn Töpel --- scripts/decodecode | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/decodecode b/scripts/decodecode index b28fd2686561..8fe71c292381 100755 --- a/scripts/decodecode +++ b/scripts/decodecode @@ -93,6 +93,11 @@ disas() { ${CROSS_COMPILE}strip $t.o fi + if [ "$ARCH" = "riscv" ]; then + OBJDUMPFLAGS="-M no-aliases --section=.text -D" + ${CROSS_COMPILE}strip $t.o + fi + if [ $pc_sub -ne 0 ]; then if [ $PC ]; then adj_vma=$(( $PC - $pc_sub )) @@ -126,8 +131,13 @@ get_substr_opcode_bytes_num() do substr+="$opc" + opcode="$substr" + if [ "$ARCH" = "riscv" ]; then + opcode=$(echo $opcode | tr ' ' '\n' | tac | tr -d '\n') + fi + # return if opcode bytes do not match @opline anymore - if ! echo $opline | grep -q "$substr"; + if ! echo $opline | grep -q "$opcode"; then break fi