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 C83FEC3DA6D for ; Fri, 23 May 2025 05:32:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6279D6B00AC; Fri, 23 May 2025 01:32:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5D7786B00AD; Fri, 23 May 2025 01:32:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 49F906B00AE; Fri, 23 May 2025 01:32:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2703D6B00AC for ; Fri, 23 May 2025 01:32:06 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9EFE95FABC for ; Fri, 23 May 2025 05:32:05 +0000 (UTC) X-FDA: 83473051410.06.7E4B556 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf07.hostedemail.com (Postfix) with ESMTP id CCCB84000C for ; Fri, 23 May 2025 05:32:03 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GQnvi104; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 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=1747978323; 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=qBllhcM+g/f8QCzBHKheZbKKKIppUnksj+280J0SHjZbWl6dr+zAxvR23IhlY6sWPa/3Ij d9OgPq3GDzAl7PulKYgkRDKgtRN2eUnTKfKJhyS/cTm14RJyPEqIabaeH+MaFPJcnbEaDY fYtnebY5yRIG6HPCa1d8O6bSKOOYVGU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GQnvi104; spf=pass (imf07.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747978323; a=rsa-sha256; cv=none; b=zO460HNAtwu69TrGSZ/N9Cu+i2bxPghDaNPj+KqPHVRiXYng5h0OsUnB4VJC2/T9oiv9b/ j7KWppLcesPhMzjjb4QukqWx+rIHtPW5c61xCvNbTregJErebmhkTSOGuRKcGGVMMBFC0/ EZheMBWFM86wPwXwA5c55fWlos5DCLI= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-7399838db7fso514718b3a.0 for ; Thu, 22 May 2025 22:32:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747978323; x=1748583123; 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=GQnvi104EEcIcY91R4TY7g6piYrN/ecIFllccJYQhhZdk7wZ1TW6Y/SbD8D70WwUVj R8dr8+W0xWRXgkRtF8IfA0tOnlR1wJNGAMkbDOx9FF0hWAAlzIGNmJ11R5KcgRqMUl6W h6TA9OsTQWHeG/Oavv96kIsBmgAsEd8V/mcvkF+8iYKTJfwjmUq+KaKgES42bFfDVLDz 1x3pcq8NL+swoMAXwQH008tfFlWpbakpERtbeNYC9hHaXx8z67MBjSFM9p8Ir/XVuJyw N0EKyRffaTJvGGwo+o0n3v/sHmArewM9S9ufbwwlpZgGaTWF8SeXR1ue1XpWkwOuQTXR qjMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747978323; x=1748583123; 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=YzOJabj0ma0eY6EVAfetbv4hWr8S5IQj+A75sopWLk2JXDSfFwbsC1yV9Gx9DKabhu M45DGikHbrZ2WB10Da+C2quEsbKEmbff2GfbiQmhSomMgeGuj0meBqu1Y+VaXpdJW1Z7 HAHAFO1vSDjvnh/W3tAba6MDnRp3WaqLPqNwDJKpkh2CDuvZp3IbGb5GD2C/ofBZWH7r g+Z67O/DRaxmJT9ot/nbT/oiws7ke1z8oq1XkEGifnKSLiaACe4svHL/P+ympDH1T3NC 5POqk49tg07ltwoIuI0EKDx9rCvS6JMOZoiIlNBuivaxDQpkUvh06FspHPZOBO0EWSvB iWpA== X-Forwarded-Encrypted: i=1; AJvYcCUlQyVI0/t2cAelJsIc4L6M9G2eQKQxU2YzHV7BAhBWfwTIMQRPVl7PqLEJR0nNSH86u5NBterL/Q==@kvack.org X-Gm-Message-State: AOJu0YxRhxJisNbc4nFWR4IH8WG7P1uhOb7Mk9/uUzaQZzwkBjGQvfZJ AoOI6jGv0O/pXJdZsH42BK1Ca6Azv1jD6VoezWHcdKPPhgD1L/E2mwOUrWyrFhPcNp4= X-Gm-Gg: ASbGncvVF0+imDWU5i38vCoy5nwLN8rvM7oYFLRRlqF7PwehYCl5hNJi3vCgS7w3gBS tmwWcgjbGIQNtJ/f9rQ5hvj9cIWcQrRQUVQP/1wGtkWLY2O0LbTVRy8v9cDsXRLPNY82S/0Gj5t Wft9cBkB4fbFjjbDOfLTIJ5Cfa5NxMOAiLbC1cLLfWBMn10EQwP+qcSB4XTiUlF+cIgk2ZiKu1h PDK/kLY+74LYtM7uLQFe5OQDqIT68sULmr8NIn8Ao6yxD1R8eQwINlbke77sBCbzh8NCEjOuWEE 2ynto+nJw7lCdOOpE6+UWPmjeGigiKtApDXCSyU+FZeTbBhyO9yGYCLTgb2UiQ== X-Google-Smtp-Source: AGHT+IF1RjEUzefPwiQ1kAsNbNi7ztuTv0w6and+LKuuSIHiE7eCfoV9bmUDF/5BN7/EfBUJa5esFQ== X-Received: by 2002:a05:6a00:3cc1:b0:73d:f9d2:9c64 with SMTP id d2e1a72fcca58-745ece323edmr3025586b3a.10.1747978322627; Thu, 22 May 2025 22:32:02 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-742a982a0a4sm12474336b3a.101.2025.05.22.22.31.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 22:32:02 -0700 (PDT) From: Deepak Gupta Date: Thu, 22 May 2025 22:31:18 -0700 Subject: [PATCH v16 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: <20250522-v5_user_cfi_series-v16-15-64f61a35eee7@rivosinc.com> References: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@rivosinc.com> In-Reply-To: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@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: rspam01 X-Rspamd-Queue-Id: CCCB84000C X-Stat-Signature: us1h98dm8atwbpdwn8ckowmcfc3518ww X-Rspam-User: X-HE-Tag: 1747978323-404574 X-HE-Meta: U2FsdGVkX1+DJFhYSIbScw5sYVjAIwRWBrToP504aY+2rC3DPCxO1A5UsIhmI4fhcEkjiKo8rK9KAZ6qwultkUr8y7bh4SITTRiKbeiaqeGQ/4NpZfGUL2ujzbuU1G+nvOBaRB52e/3XuNeHk+1+SASd2DCdG7pldcbw6b8TRClvBdxOct8lzmHYRiOS5lxld8a3HA3ETBkqkZ2GVK/tRhCKrBCWSzbGYZprW3N0F2uZ0qWgfxSxS9Lj9joQ+fTiVBGmkmHFt/cJtMkYv06wE4sxnbjG88q68aMT2rDvHdQ5hswTWAUH6F+Pu6rv6vfdbulalKUrzjgg8wdNz8pRPAqomHGmlbvocFsO7RrKO6qmqfFSdA/4g/03KaI2Eu7DKKaGJxFenDRnmK7IgcLw0TE+ox37m7SyxSbEnPXT9tHDdXEWw8sxf+J5bPVGu3TVIaJS3oUOcSYS6SleEB00bi+eZul35PEdX2FQFZvxXaOsI8wZVZSuF/76ycolcwpEjJ79UkWU6XOLNYHPi/qAgROtVA7AoYex4C/HUSB7lwgHgYZcRZ8PR1KiU5hFYV07fwHw4VSfmepg65k0yI2/zWxbhWCFkowN9yVJDcq//kilvqp1WghrdfR1Xs/lVLfe1c3Op/9EZA9rS0CzkRqAwDCLmVfDqrMZJJsR04xqEFy5kfAI5DWyINbfcxBaZ4REfAcPpF750uPduVsOaXCkBiw5SiR5iOA/XqY3pD7Q88SjUEQmjH2N8DU/FwWYRu6b2UEB/cN5j2X32y17WlxkiC/aTmJmcC0+bELLQnmJI5L9Gro0yLHRsRnisJiZdRNwLKMy3vx3LteNax5Zu1bpD193zoLOrUZhHQArnclYYSwACjsSY+CM5GcMO8VoG/bpBXEHudyc9GVZT1RY5SqiTR8kIRV0XWzXXfV8EgwOuNJrG68Xqh6qnsJ3xZfkodnRAxjCoz1bHzXaD5LYQ6+ DikdIpW1 qAjbd7IFWVBZpuNj8m7vL3wU0UEHHu4PXpaiW/SR1RQiET35vYmpjY/dvESqpM/elpwZyxVe+l4irDDlDY5p3B3Vam2d2beBIoro4YC3nPvoh0PI= 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