From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D37DC3ABB2 for ; Tue, 17 Sep 2024 00:00:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BC166B0089; Mon, 16 Sep 2024 20:00:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 66BC56B008A; Mon, 16 Sep 2024 20:00:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 50BFC6B008C; Mon, 16 Sep 2024 20:00:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 327CE6B0089 for ; Mon, 16 Sep 2024 20:00:50 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C38CF801BD for ; Tue, 17 Sep 2024 00:00:49 +0000 (UTC) X-FDA: 82572274218.09.01C2ACD Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf11.hostedemail.com (Postfix) with ESMTP id 7055440015 for ; Tue, 17 Sep 2024 00:00:47 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dS6bpgtr; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726531216; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eup1ahvLxbk3GqS/ZMeUjwBFH9cE8D+Ggr+WJlLQMOk=; b=XR9qBbukhzJU/rbSrkT9RBpYNlq1O+PCYoX8cRCN/6rGMyuJHF1W9lbNY86eXmHKDxkq5b 76aOlat25fMOm4Ci8R7tidvo2I5k+eBDFEULPPB1Vbhzx7V26cEnpLvFCbrGGh7OUIEgSi zmnoy4Te9iFnsUVnGAlP+rFj3TT9ahA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dS6bpgtr; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726531216; a=rsa-sha256; cv=none; b=h8PJUJKLJzwRKIPoBOaD5FJOV6TaD+0rjc2BpFlQ/88w6E4LhmpIz4kBA8WAt6fIY4dNx3 ikhZ/hnlv725QIFTobJTcvicQ0cw5ymamsj5Pxnbs76UMlwVlcFAcZ0gEsCAzVlwGYMHkp /CYrxDwW4XB0yrCS0culsoyuQ30TLlc= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2053a0bd0a6so51896315ad.3 for ; Mon, 16 Sep 2024 17:00:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726531246; x=1727136046; darn=kvack.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=eup1ahvLxbk3GqS/ZMeUjwBFH9cE8D+Ggr+WJlLQMOk=; b=dS6bpgtrm9OQqWFEOKYQ5aNBvf44UPm7sVaOETo+37QDe1Uk6cJrBR72Je0kDlZlBL oxMd65j/0wRP8ndjP21BXIGkVBdq7GOY2lrBO9uCf1lNLaF3571Uv0VHpyJyZzb1SPho a8oZDFxy0Ij85oDTuEkbuhtl7sQ40xI66sZS/1y/2n1rczhCmQsxyh03MPmlm/Q0AojE xMyU6orRexiRAD36FsEsr9o8F/traxoAreTmXyBTxBzH6iJNhskuAgDLOdyJwB2YJ7qB mHyAPnMIAmkEjYy8sErf1z/1yFj/J4klerJ88ct9RDt54d1sXnrHWfPzbPwptlOhU1YH vQgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726531246; x=1727136046; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eup1ahvLxbk3GqS/ZMeUjwBFH9cE8D+Ggr+WJlLQMOk=; b=YVTL/VjyZqTjl9D73fSCK7Sjs66/FM60BGAtYG5KIWxvx3IBU3Vx4+gEBOe+bkQyRv AYRt/LumUCwTUPVTmWPvvcMYb0ds274/blwNKYjTN66aJA7I0Ae0HQWQ6X+Wg6KqNAUd Rw18zcsQq1eARFH5+pDL68V1dXE7D0Cr7KuaArqU9L62NmKuCvjpQQGb49kxid0Z8Xpy w/aoGspou3AMe4qa2YdwElJCp/h/TnoC4Sn6E5MOG9XBK8LtKYj8hIVUiz/dUosmN1lT EffipqFw0KTFW/OIqkulh4BK5DuWHFtWOlIsCKBwBG8fyWNmyu2yewA3fC1f42z5BTbw UjGg== X-Forwarded-Encrypted: i=1; AJvYcCXb0kAgM84ejGWXMofYBGdUItdMJCJQpcMaKYCK27wDQCzV3WaYIAF+yD5t8eXRlIloE3TQEuylgQ==@kvack.org X-Gm-Message-State: AOJu0YyBypDCTSCvARA4FzKLpmBB8Bjsg3IwGMsCTeVwF8wScDAn9Xg+ BM9DyAUxFBAy3b9iHgmo4KDs+PpDxIwJUscJSxYkgSHBcjDxL0y4VA2dt5Q5f2I= X-Google-Smtp-Source: AGHT+IH64YpGkOt6sdEnN70vzwbW5vOexKApQ3tQQA6MC8BwStmIPrrAyTwaRSTJo5R11SP51oTbHQ== X-Received: by 2002:a17:902:ea0d:b0:205:7c76:4b2c with SMTP id d9443c01a7336-2076e412882mr247960785ad.48.1726531245442; Mon, 16 Sep 2024 17:00:45 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20794601384sm41311685ad.94.2024.09.16.17.00.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 17:00:45 -0700 (PDT) Date: Mon, 16 Sep 2024 17:00:40 -0700 From: Deepak Gupta To: Andy Chiu Cc: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, quic_zhonhan@quicinc.com, zong.li@sifive.com, zev@bewilderbeest.net, david@redhat.com, peterz@infradead.org, catalin.marinas@arm.com, broonie@kernel.org, dave.hansen@linux.intel.com, atishp@rivosinc.com, bjorn@rivosinc.com, namcaov@gmail.com, usama.anjum@collabora.com, guoren@kernel.org, alx@kernel.org, jszhang@kernel.org, hpa@zytor.com, puranjay@kernel.org, shuah@kernel.org, sorear@fastmail.com, costa.shul@redhat.com, robh@kernel.org, antonb@tenstorrent.com, quic_bjorande@quicinc.com, lorenzo.stoakes@oracle.com, corbet@lwn.net, dawei.li@shingroup.cn, anup@brainfault.org, deller@gmx.de, x86@kernel.org, andrii@kernel.org, willy@infradead.org, kees@kernel.org, mingo@redhat.com, libang.li@antgroup.com, samitolvanen@google.com, greentime.hu@sifive.com, osalvador@suse.de, ajones@ventanamicro.com, revest@chromium.org, ancientmodern4@gmail.com, aou@eecs.berkeley.edu, jerry.shih@sifive.com, alexghiti@rivosinc.com, arnd@arndb.de, yang.lee@linux.alibaba.com, charlie@rivosinc.com, bgray@linux.ibm.com, Liam.Howlett@oracle.com, leobras@redhat.com, songshuaishuai@tinylab.org, xiao.w.wang@intel.com, bp@alien8.de, cuiyunhui@bytedance.com, mchitale@ventanamicro.com, cleger@rivosinc.com, tglx@linutronix.de, krzk+dt@kernel.org, vbabka@suse.cz, brauner@kernel.org, bhe@redhat.com, ke.zhao@shingroup.cn, oleg@redhat.com, samuel.holland@sifive.com, ben.dooks@codethink.co.uk, evan@rivosinc.com, palmer@dabbelt.com, ebiederm@xmission.com, andy.chiu@sifive.com, schwab@suse.de, akpm@linux-foundation.org, sameo@rivosinc.com, tanzhasanwork@gmail.com, rppt@kernel.org, ryan.roberts@arm.com Subject: Re: [PATCH v4 21/30] riscv/traps: Introduce software check exception Message-ID: References: <20240912231650.3740732-1-debug@rivosinc.com> <20240912231650.3740732-22-debug@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspam-User: X-Stat-Signature: h8r3dbb9jdsdxobt8gkkrd7s3h5k6bxg X-Rspamd-Queue-Id: 7055440015 X-Rspamd-Server: rspam11 X-HE-Tag: 1726531247-651661 X-HE-Meta: U2FsdGVkX182NgWKhd0hif9H+6I5rG5yb7to1Dyt/2G0WGFwn/0Bz1+nrm1PhxrJ+ErzdykBiwOeq4KMjbq3+DssEfI9FrdElAMSRnEkbl5uzNttOI/ilZGstauzfAekzttb2kLD+XVy5t6Njzw9+xwhWe2eZCIlRkF2Swn1aaDZJq6zCiQguxHS/WB3n2I9a5ebq4YgJuLSwXEk/Hyib5rzdsO1oZShO8R+iYfW8Fiy1016tvjnCA9VME9LhqjM/9eKYSx/Gn984bj53tx1QHZLEk8OgZqgrN3UuIJvUVdCUHnsbqwtwOaJN/1JNz9scqZL2h+0USQx7LPM3Ik2TKz025/romA6oxgQO35Gt7dKxHeRLHqTzRCtGc0GfxtsZ22AOizaIhXmWyA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WJ5ZBNnznQjvhfhB05GwHPhjnWc4y7c9KjlSQczYWbL/vT2zjxLW+z/wNeB2b4ZsHzGdoE9D/4H4etHS4kn/8EkUTXf7Se81qNSCioDKqS89uKMQexA0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Fri, Sep 13, 2024 at 09:35:50PM +0200, Andy Chiu wrote: >Hi Deepak > >Deepak Gupta 於 2024年9月13日 週五 上午2:32寫道: >> >> zicfiss / zicfilp introduces a new exception to priv isa `software check >> exception` with cause code = 18. This patch implements software check >> exception. >> >> Additionally it implements a cfi violation handler which checks for code >> in xtval. If xtval=2, it means that sw check exception happened because of >> an indirect branch not landing on 4 byte aligned PC or not landing on >> `lpad` instruction or label value embedded in `lpad` not matching label >> value setup in `x7`. If xtval=3, it means that sw check exception happened >> because of mismatch between link register (x1 or x5) and top of shadow >> stack (on execution of `sspopchk`). >> >> In case of cfi violation, SIGSEGV is raised with code=SEGV_CPERR. >> SEGV_CPERR was introduced by x86 shadow stack patches. >> >> Signed-off-by: Deepak Gupta >> --- >> arch/riscv/include/asm/asm-prototypes.h | 1 + >> arch/riscv/include/asm/entry-common.h | 2 ++ >> arch/riscv/kernel/entry.S | 3 ++ >> arch/riscv/kernel/traps.c | 38 +++++++++++++++++++++++++ >> 4 files changed, 44 insertions(+) >> >> diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/asm/asm-prototypes.h >> index cd627ec289f1..5a27cefd7805 100644 >> --- a/arch/riscv/include/asm/asm-prototypes.h >> +++ b/arch/riscv/include/asm/asm-prototypes.h >> @@ -51,6 +51,7 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_u); >> DECLARE_DO_ERROR_INFO(do_trap_ecall_s); >> DECLARE_DO_ERROR_INFO(do_trap_ecall_m); >> DECLARE_DO_ERROR_INFO(do_trap_break); >> +DECLARE_DO_ERROR_INFO(do_trap_software_check); >> >> asmlinkage void handle_bad_stack(struct pt_regs *regs); >> asmlinkage void do_page_fault(struct pt_regs *regs); >> diff --git a/arch/riscv/include/asm/entry-common.h b/arch/riscv/include/asm/entry-common.h >> index 2293e535f865..4068c7e5452a 100644 >> --- a/arch/riscv/include/asm/entry-common.h >> +++ b/arch/riscv/include/asm/entry-common.h >> @@ -39,4 +39,6 @@ static inline int handle_misaligned_store(struct pt_regs *regs) >> } >> #endif >> >> +bool handle_user_cfi_violation(struct pt_regs *regs); >> + >> #endif /* _ASM_RISCV_ENTRY_COMMON_H */ >> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S >> index ca9203e6d76d..2ec75ba864a8 100644 >> --- a/arch/riscv/kernel/entry.S >> +++ b/arch/riscv/kernel/entry.S >> @@ -384,6 +384,9 @@ SYM_DATA_START_LOCAL(excp_vect_table) >> RISCV_PTR do_page_fault /* load page fault */ >> RISCV_PTR do_trap_unknown >> RISCV_PTR do_page_fault /* store page fault */ >> + RISCV_PTR do_trap_unknown /* cause=16 */ >> + RISCV_PTR do_trap_unknown /* cause=17 */ >> + RISCV_PTR do_trap_software_check /* cause=18 is sw check exception */ >> SYM_DATA_END_LABEL(excp_vect_table, SYM_L_LOCAL, excp_vect_table_end) >> >> #ifndef CONFIG_MMU >> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c >> index 51ebfd23e007..32d1453bed72 100644 >> --- a/arch/riscv/kernel/traps.c >> +++ b/arch/riscv/kernel/traps.c >> @@ -354,6 +354,44 @@ void do_trap_ecall_u(struct pt_regs *regs) >> >> } >> >> +#define CFI_TVAL_FCFI_CODE 2 >> +#define CFI_TVAL_BCFI_CODE 3 >> +/* handle cfi violations */ >> +bool handle_user_cfi_violation(struct pt_regs *regs) >> +{ >> + bool ret = false; >> + unsigned long tval = csr_read(CSR_TVAL); >> + >> + if (((tval == CFI_TVAL_FCFI_CODE) && cpu_supports_indirect_br_lp_instr()) || >> + ((tval == CFI_TVAL_BCFI_CODE) && cpu_supports_shadow_stack())) { >> + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, >> + "Oops - control flow violation"); >> + ret = true; >> + } >> + >> + return ret; >> +} >> +/* >> + * software check exception is defined with risc-v cfi spec. Software check >> + * exception is raised when:- >> + * a) An indirect branch doesn't land on 4 byte aligned PC or `lpad` >> + * instruction or `label` value programmed in `lpad` instr doesn't >> + * match with value setup in `x7`. reported code in `xtval` is 2. >> + * b) `sspopchk` instruction finds a mismatch between top of shadow stack (ssp) >> + * and x1/x5. reported code in `xtval` is 3. >> + */ > >It seems like this trap handler does not follow generic entry. This >can cause problems as signal delivery is done in >irqentry_exit_to_user_mode(). Please reference the commit f0bddf50586d >("riscv: entry: Convert to generic entry") for more information. Ack. will fix it. > >> +asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *regs) >> +{ >> + if (user_mode(regs)) { >> + /* not a cfi violation, then merge into flow of unknown trap handler */ >> + if (!handle_user_cfi_violation(regs)) >> + do_trap_unknown(regs); >> + } else { >> + /* sw check exception coming from kernel is a bug in kernel */ >> + die(regs, "Kernel BUG"); >> + } >> +} >> + >> #ifdef CONFIG_MMU >> asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs) >> { >> -- >> 2.45.0 >> >> >> _______________________________________________ >> linux-riscv mailing list >> linux-riscv@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-riscv > >Cheers, >Andy