Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp556037img; Thu, 28 Feb 2019 04:14:08 -0800 (PST) X-Google-Smtp-Source: AHgI3IYTazIWxfKyuTGb3Zz4Apr7XCxf5kONQfBOuxNdJsYXfYhF5250MSxotPlr1AMfoTxOATjh X-Received: by 2002:a17:902:9a04:: with SMTP id v4mr7777539plp.34.1551356048283; Thu, 28 Feb 2019 04:14:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551356048; cv=none; d=google.com; s=arc-20160816; b=0pD0O81p5aMhUKhZLfIe6GfnGV1dGlKd9Tvh4qluBzoiEW7Gv5yt49TofGjog/orHN kk+3rDAcpDTZ6AfMC2D4xXxsLKYofUd2pd78ONSmDIXovv6UnZh72NHxLoGE1MofoEIf ih8NYVjndTD6l6Q0koSSaRsLBvYsB9Wb/EtdMaP/ImCb9uSToWMHqVKHQaMA3CO/Cr5F 0n66fCty/XRtgPpBXNejSsgDr9SP2vBRqymgnSP+JYFdqdWacgFtCzMmDwMTDZGU3MmU 0bsZXTlhsScdHUXElnHpQBWbWNF/eQrA4bADqdzvbi2Gun5YMZakLhMxy0yK2Yr36XDm kb/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=CIsGQwN8bm7X1FvClkcT7E084pz908/8WmgE+BQfgZM=; b=CYjXPS8mR4kfoBBU5AOYCK80/kFFoPvJ3MaD8VbE//yRGkaCXoq4uLeBHpO2KHZUYR M0xtZjx1MC22ljgB0tjZc6yNpufQeY0Y9wTYH3iruzm2nSp5hfM1jxSuV+m9JfhV69H3 q0rciUHi4uIUII/FHXEez+GTI8NvgQuB1piZ2QO23FDkiVJys1hb/TPbo0ZuAHaa4+NP gRGFNzEUl6uNooUjCG3xps4E99/99y+2gmcKgvnD/8IeTA8bgnO9Rc6oR/7/G32Ku1P3 QFdHBMUBWPJmvlD3xxC9fi1JvcdwAfGDHqqVZpssQMw3iuV4Z9BD6ThE2BJfYPl9VSUk /K6A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d27si19493289plj.278.2019.02.28.04.13.53; Thu, 28 Feb 2019 04:14:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732346AbfB1K5v (ORCPT + 99 others); Thu, 28 Feb 2019 05:57:51 -0500 Received: from 59-120-53-16.HINET-IP.hinet.net ([59.120.53.16]:56692 "EHLO ATCSQR.andestech.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725934AbfB1K5v (ORCPT ); Thu, 28 Feb 2019 05:57:51 -0500 Received: from ATCSQR.andestech.com (localhost [127.0.0.2] (may be forged)) by ATCSQR.andestech.com with ESMTP id x1SAVeLV086903 for ; Thu, 28 Feb 2019 18:31:40 +0800 (GMT-8) (envelope-from vincentc@andestech.com) Received: from mail.andestech.com (atcpcs16.andestech.com [10.0.1.222]) by ATCSQR.andestech.com with ESMTP id x1SAVDs7086841; Thu, 28 Feb 2019 18:31:13 +0800 (GMT-8) (envelope-from vincentc@andestech.com) Received: from atcsqa06.andestech.com (10.0.15.65) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.123.3; Thu, 28 Feb 2019 18:31:47 +0800 From: Vincent Chen To: , , , , , , CC: , Subject: [PATCH 3/3] riscv: Make WARN() related functions able to trigger a trap exception Date: Thu, 28 Feb 2019 18:31:31 +0800 Message-ID: <1551349891-22683-4-git-send-email-vincentc@andestech.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1551349891-22683-1-git-send-email-vincentc@andestech.com> References: <1551349891-22683-1-git-send-email-vincentc@andestech.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.0.15.65] X-DNSRBL: X-MAIL: ATCSQR.andestech.com x1SAVDs7086841 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This can help developers to analyze the cause of WARN() because the control will be transferred to debugging environment if the debugger is connected. Signed-off-by: Vincent Chen --- arch/riscv/include/asm/bug.h | 27 ++++++++++++++++++--------- arch/riscv/kernel/traps.c | 19 ++++++++++++++++--- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/arch/riscv/include/asm/bug.h b/arch/riscv/include/asm/bug.h index 1cab7f4..b2aa88f 100644 --- a/arch/riscv/include/asm/bug.h +++ b/arch/riscv/include/asm/bug.h @@ -43,38 +43,47 @@ #define __BUG_ENTRY \ __BUG_ENTRY_ADDR "\n\t" \ __BUG_ENTRY_FILE "\n\t" \ - RISCV_SHORT " %1" + RISCV_SHORT " %1 \n\t" \ + RISCV_SHORT " %2" #else #define __BUG_ENTRY \ - __BUG_ENTRY_ADDR + __BUG_ENTRY_ADDR "\n\t" \ + RISCV_SHORT " %2" #endif -#define BUG() \ +#define __BUG_FLAGS(flags) \ do { \ __asm__ __volatile__ ( \ "1:\n\t" \ "ebreak\n" \ - ".pushsection __bug_table,\"a\"\n\t" \ + ".pushsection __bug_table,\"aw\"\n\t" \ "2:\n\t" \ __BUG_ENTRY "\n\t" \ - ".org 2b + %2\n\t" \ + ".org 2b + %3\n\t" \ ".popsection" \ : \ : "i" (__FILE__), "i" (__LINE__), \ - "i" (sizeof(struct bug_entry))); \ - unreachable(); \ + "i" (flags), \ + "i" (sizeof(struct bug_entry))); \ } while (0) + #endif /* !__ASSEMBLY__ */ #else /* CONFIG_GENERIC_BUG */ #ifndef __ASSEMBLY__ -#define BUG() \ +#define __BUG_FLAGS(flags) \ do { \ __asm__ __volatile__ ("ebreak\n"); \ - unreachable(); \ } while (0) #endif /* !__ASSEMBLY__ */ #endif /* CONFIG_GENERIC_BUG */ +#define BUG() do { \ + __BUG_FLAGS(0); \ + unreachable(); \ +} while (0) + +#define __WARN_FLAGS(flags) __BUG_FLAGS(BUGFLAG_WARNING|(flags)) + #define HAVE_ARCH_BUG #include diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index dee0e5e..023208b 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -118,6 +118,17 @@ asmlinkage void name(struct pt_regs *regs) \ DO_ERROR_INFO(do_trap_ecall_m, SIGILL, ILL_ILLTRP, "environment call from M-mode"); +#ifdef CONFIG_GENERIC_BUG +static inline unsigned long get_break_insn_length(unsigned long pc) +{ + bug_insn_t insn; + + if (probe_kernel_address((bug_insn_t *)pc, insn)) + return 0; + return ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) ? 4UL : 2UL; +} +#endif /* CONFIG_GENERIC_BUG */ + asmlinkage void do_trap_break(struct pt_regs *regs) { #ifdef CONFIG_GENERIC_BUG @@ -129,7 +140,8 @@ asmlinkage void do_trap_break(struct pt_regs *regs) case BUG_TRAP_TYPE_NONE: break; case BUG_TRAP_TYPE_WARN: - die(regs, "Kernel BUG. Kernel got an unexpected WARN trapped by ebreak"); + regs->sepc += get_break_insn_length(regs->sepc); + break; case BUG_TRAP_TYPE_BUG: die(regs, "Kernel BUG"); } @@ -149,12 +161,13 @@ int is_valid_bugaddr(unsigned long pc) if (probe_kernel_address((bug_insn_t *)pc, insn)) return 0; if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) - return insn == __BUG_INSN_32; + return (insn == __BUG_INSN_32); else - return (insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16; + return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16); } #endif /* CONFIG_GENERIC_BUG */ + void __init trap_init(void) { /* -- 1.7.1