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 C8C1AC3DA6D for ; Fri, 23 May 2025 05:31:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53C566B0099; Fri, 23 May 2025 01:31:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5149D6B009A; Fri, 23 May 2025 01:31:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4290E6B009B; Fri, 23 May 2025 01:31:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 24C776B0099 for ; Fri, 23 May 2025 01:31:33 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C59CFC16D3 for ; Fri, 23 May 2025 05:31:32 +0000 (UTC) X-FDA: 83473050024.30.F08DC7C Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf03.hostedemail.com (Postfix) with ESMTP id C8AF620005 for ; Fri, 23 May 2025 05:31:30 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kE6drfKh; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747978290; a=rsa-sha256; cv=none; b=EGwb78s5v3NDSuBHE6jLJz44stP+4m5vbzv/17lVXVMXpEg9PYHfxLVFGANToa2jUkcG+Y ocFrIOfEhybk8NuBnikGFbLyS0WMYkT/HqKauP8zZkIQRlhuggScz1052THCMFQJgM8481 bGa8PKoZV8nj5/Tpnm6U6NDTygI+HG4= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kE6drfKh; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.179 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=1747978290; 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=eyCgE0sNHOlyx9zGD2XlqTc9W1j01DyA5vw9LyX8zMs=; b=YCu3MIaLJ1OUYXbWuibjtkSrdv2ujVz2QpWk0aMV7FJ4MaMlAqQ8ybZoceGGrwxqUDvtvb RmdUFuGzYZMRgb/I2siSnC18rq+K9gA9FuMM0iXmZiZLKUDjy4gScH2iRcJw7EY87XoItK lAhMFuwios03mt/FWCaJvMKD/Cjik4k= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7399838db7fso514439b3a.0 for ; Thu, 22 May 2025 22:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747978290; x=1748583090; 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=eyCgE0sNHOlyx9zGD2XlqTc9W1j01DyA5vw9LyX8zMs=; b=kE6drfKhkxXAYwU6YxwDDMr3bKAkfoD0dExYvUhCXxjjvPef6I5VypvZBGTqAst1zM 2QRC2gorVepBBKj1ZIAKhlkE5ULwpp7KjDaIxTtTeetdO3YMCq0mg0oFAZt23lG9GUDb aGb0IjE93uZanPLInqFjczub9CyZNNSjT/A58gKCkJcmteiLULBFlky4C9gXCWICh6Ra Xm7SjJsFbPoHoeais1Dm1A38qE41tIjRz7VPyUIa3L/WyRTM7pd4xc8R6HF77nFG1vlb 1snhxiUVJ3Mb6WcITifugtMo3UCgod2HvgdIjhtmyMkueje9t9IreaD2vpcADEhU3WTN QXXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747978290; x=1748583090; 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=eyCgE0sNHOlyx9zGD2XlqTc9W1j01DyA5vw9LyX8zMs=; b=M49RpC2qwGYQeohAfPgtJTflgyofX0Ai2wyaj+6ldsaUbL73cw8gCIO1w7V7d12tAO LA2WrTs+tAgyxxomXRKQXUQU/lNYCT/7g/vbmf3ATxUraaNrf3rtSFvC+L0nzWfQGNED 15LQ8DshTRKRrBkhaXF2oC6kkF8XZCmNasYFTIv167Z3+L6A04Wp10G+3Bk/WDW/Ul4u BXvvCdIY2tZGhPXSZv/FlQuEZBVEjOlzu/UPh3T2+gm/EwaXR/50gylzhT58aVJ7dHPe 27Yl5eKZw0Iu8fxm6/0Ci9dkColNnPySBG7tAJwTGa1pAHSsdsNaqxaGpQjL26i1S63U v3Vw== X-Forwarded-Encrypted: i=1; AJvYcCVPvDdQ71gq/YAhUZVdZ8tjBBOcQnmzHF7Efp5Yu1iqxwc8FGDMrNQzhNNXBaF9zSXU74EHzMJpcw==@kvack.org X-Gm-Message-State: AOJu0Yz8K2tC8i7PYFzhyrHTIidpjWiN2MbiTwwO7+U7mDB1O5QiLLo+ GvapxyQlC7SdN8kJps6Pn2Jkdq4SVHqySlmtdkA/pD0gaOocCnP9QsU7J5tsQxUqliE= X-Gm-Gg: ASbGncvHX6amctIRCY9K8FOzXtRNrohl+3J17PRp1jJgEcTjOWnNRLKJMeUXvLFl1t7 OF83vf9mavfWR7mLL2CLrMF3VcRBRV+josW5hAwEdn8VsSdkifpL7qBDnyRT8Tlstrus44VEA4k F7s/uXpJjiXm6Xj9isdgGSPW3mP8Mlak0Jw7pdaGJW76UdUzFf6Krh/6wuFUXd2Bf+VpDu5yyLJ 9dGf+vNZmUv2XMM0yQdwHA3vHVuDmd6+lXe5ls9DMOO0Y/6mr4gnZ+njjo1bW9SfQ/zLP8Kl4Sq v7WpegWbuGl/Fz7ybDH9gQqtcdx6q3qXVMlq0Hxx0N2VDBuTwMOn8bXmYcItdsDoas9Q8o5a X-Google-Smtp-Source: AGHT+IEYaq3mTRlL1mUwIQgSF0Ah42lebiPk7V9cXPaQui6lJTbDlPlC2i5N9IBFP+vAkQAkASfIXg== X-Received: by 2002:a05:6a00:6702:b0:742:9bdc:9688 with SMTP id d2e1a72fcca58-745ecded63cmr1970994b3a.6.1747978289564; Thu, 22 May 2025 22:31:29 -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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 22:31:29 -0700 (PDT) From: Deepak Gupta Date: Thu, 22 May 2025 22:31:08 -0700 Subject: [PATCH v16 05/27] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250522-v5_user_cfi_series-v16-5-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: rspam08 X-Rspamd-Queue-Id: C8AF620005 X-Stat-Signature: bbop5g9dukm7mnjk5yo6qzaa6654neep X-Rspam-User: X-HE-Tag: 1747978290-499259 X-HE-Meta: U2FsdGVkX1//YCIwn/wUjvmCrKItNKYYGt8vlc0wFKEutDgMXHK607JindF7MGlwc665LiUlEAse0liMpiehHpUrcxo0Hv6WJXNCqRh11a2tHYXvylQV15JAkMs/w5pXk7aLTh3MtfTyW4XsZyUWEXUb2U93yO/VkuwAG9WnM4pqBVWd9RzCTlGFplUzzCpx3XcW7qkfHLGepL2FtCLFeqobFNhADeEIRzXL8oKvcVk8AxdsSa4KLPX74iqDa3k8faCS91vTe6Uq3VbhrLrH9DTVZJat3qdXOSsl+nhAGDw5iSxT1/7emcZkFknUHztbQPCI+C4FLuH9GOH1j+Q4Wz9K5AgHuJCCHmKHdV7JWNefP5NVj49wnr96xYjWW+IbmhU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U/Nbx+RstmBb5JbwuTjQFXXE4KQcTVL0PnGnxKs4+1Yt1zVyILqC87JQ8TlVyIrLRN7Hdr5dX4/ZvKF+Aj8lxjkr/2VgPMjj4ItdWA== 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: Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Shadow stack save/restore is gated on feature availiblity and implemented using alternative. CSR can be context switched in `switch_to` as well but soon as kernel shadow stack support gets rolled in, shadow stack pointer will need to be switched at trap entry/exit point (much like `sp`). It can be argued that kernel using shadow stack deployment scenario may not be as prevalant as user mode using this feature. But even if there is some minimal deployment of kernel shadow stack, that means that it needs to be supported. And thus save/restore of shadow stack pointer in entry.S instead of in `switch_to.h`. Reviewed-by: Charlie Jenkins Reviewed-by: Zong Li Reviewed-by: Alexandre Ghiti Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 23 +++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/entry.S | 28 ++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index e3aba3336e63..d851bb5c6da0 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index f5916a70879a..e066f41176ca 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -73,6 +73,9 @@ struct thread_info { */ unsigned long a0, a1, a2; #endif +#ifdef CONFIG_RISCV_USER_CFI + struct cfi_state user_cfi_state; +#endif }; #ifdef CONFIG_SHADOW_CALL_STACK diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h new file mode 100644 index 000000000000..94b214c295c0 --- /dev/null +++ b/arch/riscv/include/asm/usercfi.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ +#ifndef _ASM_RISCV_USERCFI_H +#define _ASM_RISCV_USERCFI_H + +#ifndef __ASSEMBLY__ +#include + +#ifdef CONFIG_RISCV_USER_CFI +struct cfi_state { + unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ + unsigned long user_shdw_stk; /* Current user shadow stack pointer */ + unsigned long shdw_stk_base; /* Base address of shadow stack */ + unsigned long shdw_stk_size; /* size of shadow stack */ +}; + +#endif /* CONFIG_RISCV_USER_CFI */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_RISCV_USERCFI_H */ diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index 16490755304e..f33945432f8f 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -49,6 +49,10 @@ void asm_offsets(void) #endif OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu); +#ifdef CONFIG_RISCV_USER_CFI + OFFSET(TASK_TI_CFI_STATE, task_struct, thread_info.user_cfi_state); + OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk); +#endif OFFSET(TASK_THREAD_F0, task_struct, thread.fstate.f[0]); OFFSET(TASK_THREAD_F1, task_struct, thread.fstate.f[1]); OFFSET(TASK_THREAD_F2, task_struct, thread.fstate.f[2]); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 33a5a9f2a0d4..c4bfe2085c41 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -91,6 +91,32 @@ REG_L a0, TASK_TI_A0(tp) .endm +/* + * If previous mode was U, capture shadow stack pointer and save it away + * Zero CSR_SSP at the same time for sanitization. + */ +.macro save_userssp tmp, status + ALTERNATIVE("nops(4)", + __stringify( \ + andi \tmp, \status, SR_SPP; \ + bnez \tmp, skip_ssp_save; \ + csrrw \tmp, CSR_SSP, x0; \ + REG_S \tmp, TASK_TI_USER_SSP(tp); \ + skip_ssp_save:), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) +.endm + +.macro restore_userssp tmp + ALTERNATIVE("nops(2)", + __stringify( \ + REG_L \tmp, TASK_TI_USER_SSP(tp); \ + csrw CSR_SSP, \tmp), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) +.endm SYM_CODE_START(handle_exception) /* @@ -147,6 +173,7 @@ SYM_CODE_START(handle_exception) REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 + save_userssp s2, s1 csrr s2, CSR_EPC csrr s3, CSR_TVAL csrr s4, CSR_CAUSE @@ -236,6 +263,7 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + restore_userssp s3 1: #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE move a0, sp -- 2.43.0