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 A2098C3ABAC for ; Fri, 2 May 2025 23:31:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3E3E6B00B8; Fri, 2 May 2025 19:31:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E9ED16B00B9; Fri, 2 May 2025 19:31:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2C206B00BA; Fri, 2 May 2025 19:31:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9A5586B00B8 for ; Fri, 2 May 2025 19:31:28 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 445A3140980 for ; Fri, 2 May 2025 23:31:29 +0000 (UTC) X-FDA: 83399566698.19.79E1E77 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf21.hostedemail.com (Postfix) with ESMTP id 56E3F1C0007 for ; Fri, 2 May 2025 23:31:27 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZvMbn1S7; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746228687; a=rsa-sha256; cv=none; b=qYGc4tooXvNx0naFpk0H+7JZmdRsQc/xvplRbIE/EEJH19ocPWE7rK+j0H6xqD2fardEuZ v0E41imyQgs7xKJ5V2GoouLUsEM7z7GG1C50rcgcxEYbRBX6uhkoNWWU6eYc+i1fE269Zd MTdU/iCJ4zSwjYUIohAyrFxfRl424aI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZvMbn1S7; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746228687; 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=zzG5aBj4uT4Cxy0Ye5VbMroQI6g4J7udAW2U71MrLVg=; b=MmjirBfpfUVqUP9bpGBMfhX2oC5sH+n6bx5WGUPHRWwseBvJ5BR+SMn+OesKOXoR/ftVTJ 81MTlby3pvZh1azKIyxri1ibF7AEaCSxnEHmj8hwJFuwdhm5QKh8GY5rreQ9hGoxY8WqJO pGaqKCIWkHVrrNdQi4d3oGgH9mIr8V4= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-22d95f0dda4so39572525ad.2 for ; Fri, 02 May 2025 16:31:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746228686; x=1746833486; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=zzG5aBj4uT4Cxy0Ye5VbMroQI6g4J7udAW2U71MrLVg=; b=ZvMbn1S7Hnr2AMwhT0UmS+M9kGUzXAtNAo5GuakJqS/+Y2pI/gr+S/7MQg6mZbXlK/ /nmHDc7El/tH29hY5Zm/8BNH4797Jdty3NUX8sK6H16vzLDsDlhfxV3Q2YVojw/Qu+yT cjVF4ySVCPGcViKjFroCS/7O4V4gjvVLA6ZGPLK6PWZlvBYIGfOr2qqn7Uy//FGpnaTD YToKE7Kszd90pBHtMinsT88m0IsnlOvlQK8lvzyySHD9BYGv9Ozjpuda3d1O3DM57xJ2 9o9hYWveaw4Dp39PhSIGk7HQdtsuCVvd+CjGi7ZmjW64FmtYjQhRaNkFpfh0IK5Pt6xx ST1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746228686; x=1746833486; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zzG5aBj4uT4Cxy0Ye5VbMroQI6g4J7udAW2U71MrLVg=; b=JZVf3lGqdwH4wsqDixmpQTLgaBfeTmeCVX9YLpGnZlMEM3sdWxibJoMozefEMBkGEa NpM3wB3P1y/m/aH9OEctQHW90HLbP+yqBawAv8wnkXg75z39I0uPFuHAzkM8JlwoKOKx EZEgsDdlAUFPDtYjw/ANHKvqEt4twn8z5oLRwy0GpZSHuAJ9lr+bUbdkWwsvORJ21N7U 7cuD88K5HsyDISrSseLoN+YtbHuptGiaM7+pq4UnJlWaYttAGrizUOsvPBPV77i1XOSR nBP2pVIFbTR1uAMI7dwpQ6pMJXNbdXAT2xGF5lhk7u+wmPLb6FPrBDnoIVQUSKLxlcdc ksSg== X-Forwarded-Encrypted: i=1; AJvYcCVUqw+CVPxSaYD0wJ+rSPM56hl2uLJnplC7k7S1dZ49BGN4jHJacm3/vIvy/+V5LDfihJFFDQo8kQ==@kvack.org X-Gm-Message-State: AOJu0YxEoH2fx60PiovZX7B0Twp0HIihm8VweZ1uTDQNLHGAGk8tTntx DAaGXuAkWKDPqqVLntnG83A6JP7rsbncQZD+PXHz46QGmxsLAaAKNODyzAtY6r8= X-Gm-Gg: ASbGncs9Mx84PbjnYoAN8s9zTEhdAKCfwxMIGPGEKzFJ10HL2uH08hJwxsMldGSJZ7D V41YGL7/ghp4v7HaAa5wa721rouIF5V0kWssZBG7GGoL/iYFuCXwHETdt7LY8cgoXwoiwxQjrXq CCmWfrJbSWQy9igyyyr6TH6MibxajmbxL05UPuF1VyeONLDxLco0m88HxpQsLYjZp2iNQvYlK6V MzbtnGYm68PcHs3U6jPEmG1wrq4z8Zm1wzv7+fHU2tF0pOjTqfhRG1WQn6RZHYgcjIDQCFfgQqR 8auC80WXNFo0Jcq7yWGm/s2X+7lEXPnLgdws0XMb2lPYluZN4J/5+Lr/ObK5RQ== X-Google-Smtp-Source: AGHT+IF32JcuU/UOpoww4jQ7Vp+ZM8BNKMHRFkoqxGXs1BOMEDJfAXLOyJDQfQMNdHJqaOQDtkiE4Q== X-Received: by 2002:a17:902:ea10:b0:215:9642:4d7a with SMTP id d9443c01a7336-22e18ac8366mr15406295ad.0.1746228686087; Fri, 02 May 2025 16:31:26 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22e15228ff2sm13367055ad.180.2025.05.02.16.31.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 16:31:25 -0700 (PDT) From: Deepak Gupta Date: Fri, 02 May 2025 16:30:46 -0700 Subject: [PATCH v15 15/27] riscv/traps: Introduce software check exception MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250502-v5_user_cfi_series-v15-15-914966471885@rivosinc.com> References: <20250502-v5_user_cfi_series-v15-0-914966471885@rivosinc.com> In-Reply-To: <20250502-v5_user_cfi_series-v15-0-914966471885@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, rust-for-linux@vger.kernel.org, Zong Li , Deepak Gupta X-Mailer: b4 0.13.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 56E3F1C0007 X-Stat-Signature: cbrxi3goxmjz1monnpghmzj47y8qm5hz X-Rspam-User: X-HE-Tag: 1746228687-272315 X-HE-Meta: U2FsdGVkX19IthAebm3x50SHiXr0SrGvKYz0QFVINPst73Om7QvqZgi2C9J1uGSxNF6lQiOUIapE5vYpPbYgszNmq6xbFygWdrsNDoU3PUjFzNX+Q7QGrpygTQdOAWKPOHCTgQpXmG1paLvmbT3zUqx+bCKeThALoWFgQsJXWJD/6FwPI4/fv+OlgCg4qSwu/74XFF2xOr08Vz+Zxc2B6i5tFN81IqFkkZFZ1ePsrOnUzAchi3uEGat3xUwrufRu292WfhFmE+SXuONaqZaFI/XTYFN4Nfa1T+5gA2eySb6ZOTrYrkWHdlUfFJhM/hafydSwibISRzQZ8YcCbWbQR/qW++kMQ0ExkZUbwC0qvT/k+m2rzHRf8OTSwEZmJkE1xft0SrA6BImZz5BMO5yfNLbDCv9Abu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c5XzAxI/aeHhhhBOy9wnrZZn4ncUk= 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: 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. Reviewed-by: Zong Li 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 | 43 +++++++++++++++++++++++++++++++++ 4 files changed, 49 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 b28ccc6cdeea..34ed149af5d1 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -40,4 +40,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 978115567bca..8d25837a9384 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -474,6 +474,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 8ff8e8b36524..3f7709f4595a 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -354,6 +354,49 @@ 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. + */ +asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *regs) +{ + if (user_mode(regs)) { + irqentry_enter_from_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); + + irqentry_exit_to_user_mode(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.43.0