From patchwork Wed Mar 29 08:29:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yunhui cui X-Patchwork-Id: 76451 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp258980vqo; Wed, 29 Mar 2023 01:41:19 -0700 (PDT) X-Google-Smtp-Source: AKy350biBuK5xHGumFS1RtZALeqA2x1b8aFo61oK8PNdG5PTH6Iyfmr9xDqMqDIr2lG00RqQIcHd X-Received: by 2002:a17:907:d483:b0:932:ac6c:7ef9 with SMTP id vj3-20020a170907d48300b00932ac6c7ef9mr20402736ejc.22.1680079279145; Wed, 29 Mar 2023 01:41:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680079279; cv=none; d=google.com; s=arc-20160816; b=ChWsaHLz2n1WCgwpfRSIZXiRWKesF5dcjmy74hl2ULWchJ/t9KlwouLmmE1b7AZ4R+ jYD8v6R1LtHGxrS/ez0Ygk94VV4WgVg86tWhE7066qvFpAKI+WnXPiEW3Or/o7GeGMsj BQZwpl3gUOaHQq8mYi2/3MXb3tOiDwag1wSXn3/bRfJ42x4TesQWWjVmXZUMM8v+XikL PcHwJpGJvcOS5UvEZIjC6TBSE1IYa/E9nGhz1R3TXY2Y9sCID62XwSpSKfoaXWr6jwxR bVx9GCghTUbBU5PYRMqUWAmo3hxmNhHCwQ0+JabfWIZNfkit0WuIZRLLZnmNOXiQjiz/ Wfsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature; bh=C1Or5bDbG7pY6oTZ/LXhOi2Wc2GzPwk9E7MW4Vddhkk=; b=PTkZ8PAOQPVS6pfEmNhoOwDT4aQnc2HwF07H9brwKNqT4MFiIcOwvj865nPLsbRm5J nmCPobrYReVwpko/HLgfYNYgKl7td+BSoh4baGY9ylOmZ0JXIOn98YKbftQ8IR/ukArI jHf2VIJcHpx0nDI81ZmCFl9YelJLcAfVpsBsjbVUz3X5njAQdUjTz7rDPvfZugj8t1xB 3D9PjT40Cs9rGHnWxJXvQocVSH6QKnr5OClYaOZvq7pLNyL2RhcY8y9liHMfpUbmJkR5 ea8m2s6CM8c9AcivFOiECLWG9BiMOA+lVG0vTD2649FaqFl1BajgWxBXo0hMEOYUwUr2 ormA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=SMOY6yF4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k23-20020a17090627d700b0093b4716633csi18867771ejc.139.2023.03.29.01.40.55; Wed, 29 Mar 2023 01:41:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=SMOY6yF4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230440AbjC2Iah (ORCPT + 99 others); Wed, 29 Mar 2023 04:30:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230043AbjC2Iag (ORCPT ); Wed, 29 Mar 2023 04:30:36 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29BB9268B for ; Wed, 29 Mar 2023 01:30:35 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id fd25so9761332pfb.1 for ; Wed, 29 Mar 2023 01:30:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1680078634; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=C1Or5bDbG7pY6oTZ/LXhOi2Wc2GzPwk9E7MW4Vddhkk=; b=SMOY6yF468+WcCeHsXCJ8Fzv6o84CIyGjKt/mmVuvvhHoiXzjAhLUoCc7MScSHQ4NS HcerTmICPy7F4FO9iZP+qItxyFMVyUTZGvTFkJ6/gpLuW5NTaKIoI/NxA7jKCDXVq2R/ tvP40V5+Zmp3CLAeCjvXam/Bs+GOpGSwxFvyHAKWecLY/1br165z3TVhZ2YLAztyTX6I meV8hu/SI7c9TK9MDfnKEnekGew324t92Dem0pYNqC2dw57a9PT9LE765sKY34mmPQaz PpOF24nILD9MCWaUK2FhyqZokMtSsM2YT2UEAczTrVA92gopTzhGqE7Jk+/VEveo9PDD puaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680078634; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=C1Or5bDbG7pY6oTZ/LXhOi2Wc2GzPwk9E7MW4Vddhkk=; b=jAWkbsSVvmyQVuG2uAU2rsKewyTrk+FormAfJW7rnJSXyPMdRwGKcQo/XcjDFoeW7J C04g+F/t8OOtcblm6QS5Ye3v6nlWezC8GR5wrefINU2Vi8tcuhJPGMQtgRfCHi+0ZbnT KEft1UMsA0vViNW6iQEZwWeO1d0eq359M/dKSzdULG6guf6faXKdu8PCVUC64nQkTIs9 zSAzlcY0vQZ/hrSpzt6Ddv8TDJJZmttEs1v6ncjnZvbI2a96j2gS9X6rr7c70P7GhiYX YzbiQ9WFkt/xgzlz5MIDKgL/O4717DIr+mgeF1gkdgK2O2cPMwI6v7Xe0TfstnLAyhs4 fFtQ== X-Gm-Message-State: AAQBX9dLP5hqFXOYd1TcBc8WBS2262B/Zb0gZdtIlMBuO1kSVtjM7ykB ZAEid39lu3NgSH14BULGIRxpvQ== X-Received: by 2002:aa7:95a4:0:b0:628:cff:1440 with SMTP id a4-20020aa795a4000000b006280cff1440mr18379267pfk.28.1680078634640; Wed, 29 Mar 2023 01:30:34 -0700 (PDT) Received: from PF2E59YH-BKX.inc.bytedance.com ([2408:8606:1800:501::1:3]) by smtp.gmail.com with ESMTPSA id g11-20020a62e30b000000b006281bc04392sm845676pfh.191.2023.03.29.01.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 01:30:34 -0700 (PDT) From: Yunhui Cui To: conor.dooley@microchip.com, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, peterz@infradead.org, mpe@ellerman.id.au, jpoimboe@kernel.org, mark.rutland@arm.com, svens@linux.ibm.com, cuiyunhui@bytedance.com, guoren@kernel.org, jszhang@kernel.org, ebiederm@xmission.com, bjorn@rivosinc.com, heiko@sntech.de, xianting.tian@linux.alibaba.com, mnissler@rivosinc.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] riscv: Dump user opcode bytes on fatal faults Date: Wed, 29 Mar 2023 16:29:50 +0800 Message-Id: <20230329082950.726-1-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.37.3.windows.1 MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761690810351997330?= X-GMAIL-MSGID: =?utf-8?q?1761690810351997330?= We encountered such a problem that when the system starts to execute init, init exits unexpectedly with error message: "unhandled signal 4 code 0x1 ...". We are more curious about which instruction execution caused the exception. After dumping it through show_opcodes(), we found that it was caused by a floating-point instruction. In this way, we found the problem: in the system bringup , it is precisely that we have not enabled the floating point function(CONFIG_FPU is set, but not enalbe COMPAT_HWCAP_ISA_F/D in the dts or acpi). Like commit ba54d856a9d8 ("x86/fault: Dump user opcode bytes on fatal faults"), when an exception occurs, it is necessary to dump the instruction that caused the exception. Signed-off-by: Yunhui Cui --- arch/riscv/include/asm/bug.h | 1 + arch/riscv/kernel/process.c | 30 ++++++++++++++++++++++++++++++ arch/riscv/kernel/traps.c | 1 + 3 files changed, 32 insertions(+) diff --git a/arch/riscv/include/asm/bug.h b/arch/riscv/include/asm/bug.h index 1aaea81fb141..56dab998d05d 100644 --- a/arch/riscv/include/asm/bug.h +++ b/arch/riscv/include/asm/bug.h @@ -86,6 +86,7 @@ struct pt_regs; struct task_struct; void __show_regs(struct pt_regs *regs); +void show_opcodes(struct pt_regs *regs); void die(struct pt_regs *regs, const char *str); void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr); diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 774ffde386ab..9ba9f8719605 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -83,6 +83,36 @@ void show_regs(struct pt_regs *regs) dump_backtrace(regs, NULL, KERN_DEFAULT); } +static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src, + unsigned int nbytes) +{ + if (!user_mode(regs)) + return copy_from_kernel_nofault(buf, (u8 *)src, nbytes); + + /* The user space code from other tasks cannot be accessed. */ + if (regs != task_pt_regs(current)) + return -EPERM; + + return copy_from_user_nofault(buf, (void __user *)src, nbytes); +} + +void show_opcodes(struct pt_regs *regs) +{ + u8 opcodes[4]; + + switch (copy_code(regs, opcodes, regs->epc, sizeof(opcodes))) { + case 0: + pr_info("Opcode: %4ph", opcodes); + break; + case -EPERM: + pr_err("Unable to access userspace of other tasks"); + break; + default: + pr_err("Failed to access opcode"); + break; + } +} + #ifdef CONFIG_COMPAT static bool compat_mode_supported __read_mostly; diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index f6fda94e8e59..892826234ee9 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -100,6 +100,7 @@ void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr) print_vma_addr(KERN_CONT " in ", instruction_pointer(regs)); pr_cont("\n"); __show_regs(regs); + show_opcodes(regs); } force_sig_fault(signo, code, (void __user *)addr);