From patchwork Tue Oct 29 23:44:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855743 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 E0235D7494F for ; Tue, 29 Oct 2024 23:45:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DB7DC6B00C8; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D144B6B00C7; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6CCB6B00C8; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 987D36B00C5 for ; Tue, 29 Oct 2024 19:45:30 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5A8F4A0166 for ; Tue, 29 Oct 2024 23:45:30 +0000 (UTC) X-FDA: 82728272298.17.0E75CF7 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf12.hostedemail.com (Postfix) with ESMTP id E99884000D for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rVBMCE0U; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1730245448; 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=4xlMf/FGu+cOf/Hv7qUX6Wk1q9xFnQqCr1v5Bv8qZmQ=; b=awMAi6IYJJEXH5KfXei4mPne3pJBlWTen5DH+yPbg5KqFvWWH6B6dwBMIGintzOLLTHZfB bC/T0UP//z559qT4jyAdQuiyB9U19d4LTdksEUMbkybyUVEzDwTAzGw8EehpiFSfNqEpsq NVFvwVqlM9zuaF2zRwMgKJuCqSwux/Q= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rVBMCE0U; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245448; a=rsa-sha256; cv=none; b=Rkmgk7UhLYeFerBTfOTSlL5xIRCN+mUexAe60uIRFUAG6jYpP93MmF9rohDi39bqGUtNLj 6dDJFohxUOwPRqSJRT5CAP3bN1CVA5pM7z0AeYp4wO45TngCxd/fqO1BdmYaqgbkl368aI 3xR/AnoeQVr/Nghexo3QPr/1DxecP7o= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-72070d341e5so2665779b3a.1 for ; Tue, 29 Oct 2024 16:45:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245527; x=1730850327; 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=4xlMf/FGu+cOf/Hv7qUX6Wk1q9xFnQqCr1v5Bv8qZmQ=; b=rVBMCE0U/ueE/wLfwgcQgo1933yNMVMOXeF1bZINHkyiqUcHOVQ3sis2PlqwwA0kYf MldxSPHABLOkJsCQPn9z5kmGii8h7i66dDZ2T0QCFTMnJi2xtPiWaTtiaD7jKl/IxYyd byJSmgOwKNzg6juzIhysFYB3fXW5hoz15pvPXqYa22XYeXBsJYw29XC0rrtz/BmVSpP+ AQpXtSrkCQfONTiU9LNAQhUUpZfWDPa2z22/K/PQ/VzTvgjttl9WSlBNlKb5bQJR5HhG 3a1TbLWp/syyFgZIohF1gbkdl5U1NE/4NyfxcpHZ4LH7Gu5gxsjyXtSO9RStN3/XRv6I XXjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245527; x=1730850327; 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=4xlMf/FGu+cOf/Hv7qUX6Wk1q9xFnQqCr1v5Bv8qZmQ=; b=PW3FG8wYiP3eHoZ2TvjuxHYIqLZ/owz5RIJMYQVotcb7qvKckQFGfHFSNDDNUG0nb2 kfjLJcRj1HfdOzQy+WXJPEpv+nDx+CVAzLNrN0lny6/H87RgJ2fCuf35HTiOMnhheFVG 5c9X1tggzLBM3aUBoYBRennRYvki28LTDQyrhwfRnKKd/dc1E6FLaXzHzSkSCVVm3bTO 94UNObunA46UlUZwahtL2ruq2cIbSESv4dQmvbKNcHf/wagydFz69fAIS4AN7+ap0sH+ QxyHw48TpTumgX191eUJu7CECsN19YKKmBu+51K4u9ocwao4fAyqR1Oeqi4ZB/vAU2ho oqxw== X-Forwarded-Encrypted: i=1; AJvYcCX97Fr/tW5bCT9jPB4PTcZhZowkZVxz2Edf+RFDmdufoGCMf4Be1ADcSoxGAtp4KxFECgRIKPgaZA==@kvack.org X-Gm-Message-State: AOJu0Yy7SbmZkH+jlDC90Qql7t4D3k6gTXPFx5zl/j/S8FU4q5j3AtEU R8pZ5at1mlOPly5MZc9r1t+jUOPxQVYWSYjVOK8v5WRCAPEg2Rt/HspNucIXe2Y= X-Google-Smtp-Source: AGHT+IELr6IagcEBTdUxKgRdLblmitKpEm/GHFYUpbKbGEH4mdOch+QEksQsRpJQnLoaEfYjLi/gYQ== X-Received: by 2002:a05:6a00:2d17:b0:71e:5fa1:d3e4 with SMTP id d2e1a72fcca58-72062f86747mr18633884b3a.2.1730245527282; Tue, 29 Oct 2024 16:45:27 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:26 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:27 -0700 Subject: [PATCH v7 27/32] riscv: enable kernel access to shadow stack memory via FWFT sbi call MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-27-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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 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, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: E99884000D X-Stat-Signature: a68srtw753fjpo6tb6gt3cmmh414pju4 X-HE-Tag: 1730245516-750216 X-HE-Meta: U2FsdGVkX1+9b/TdUEKMBW/kvpRn6OMRprve5gw3c6ihBLy45hTuOrcHaDROcT6pcRDvWCs0exjzJquvHZ455b5jlmywbWtRVOjVCu0XlnA/JvcDu6QgxBTWnDd4lN3S71rnE8XHPq5vqyZ6oG18UpeRXZwkvYv0sZ0d8M0UjjgMEgalBj3k1+1hY3qenczlxVsI2MQF6oZv+r7h80IKpkc4qrwRsoI2Jc15FnRwYRclDCyy3HBekWA+BKDHQb7OFgDOLz+2jhGxnR3gdFMOMzBvhfAyZIaq6ei8A13hun8r8gv4bNpt2TE1a7sNPPC36RNXjd5D29PxUWLVh91h9RvQjbfbGJ9MS01C638iDURq0UI/0VRm7JsABpbERs7Rv5EBCBtDLJsAD8iz8nu2LhmF2T5neIVwpqdw/zEzDbS9a93pap+k16PUss71tRi165oUF7zC3Y5VnpnugiIw2CZpRx/yn3t2Su9UumjnixfFbfGiZmOakZxEA5l4UPnvi4gIPjmrObQ8P/oxSFAxtbjIkIyrAukhofVxv/mMf8o/jZ3ed899bvQk2gDcg6XPsOTlPZE7lj2CjcXGn/wI6IMAtqtPw5+0AAdAX1ZHCvoS4d2NTRnLqPyTpXrKzj1Mpr0KYdl7Nk/FWxHaigyI6C37HKvsXGlZLYut8Mu+zl1JmIuAFiRminBRoEo5h9p8KlBStOB0wOQ1NYvYKRqFBmcaGjoLxbh8fn8QTc6jniidPvabdSGMDmb5yqEzSZ0lfaDZnWhpcYX5uVakNJHSRrdpEDh09mSn2qXaZJpMtqN3IRAvpif8bt3Pl0fpfQH36l/8oLbX68h0tQViUcEO2+SE0Lr2pln0gNFQ9zgwtOmn0hfYHyPJyYxRJYCI8LLz7q7yT4sJoxOSm0Gf6UM06OElxqGW9dVC3jdtR9rAUqef23yeISmfi77dV3NLXgQwKkEuKobNeW5T1VWr9Ll gNjTZPwm EfrOnDyFtJ6yfzUpaDtES4J9nRemRMtftig7q0W14PizoBuTGKG8k5zh4f+zUzChtpB1FfdaJkhC+ne/dLO5og/NWpTfv5AftO6c4j/zdeKsWJ0w9zivK7tUe3624elXDG3h9alT2vXfMkBlG+TxqqJFIOyWHVXYh0M/Tmb3FWAvrXPiIw3fdYZ0O8sruVdtnvT8NGxQAuRdSKd9p793AL3A7yqys5beugq6gu/pT7dW50Gd0Rtpuf/1KUR57CQD6H03LMmFgF4tr4/EjzK3DxCqhTSPZxS8Rst49648yy30DzEXkNbGBFiVAn/m8bST61JELVkWHI8J+f1Hpndldy2GvZeJDnpKLq/VlecA4y271353eVWAaYbrBWFpiuOCpcb5wA/vmgFVV7zamd0i5H4dk8YIfn114N8uVROebjNjr2jYFBggfYThxXnvQFtGy4W6GnVIBuCCeSi0= 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: Kernel will have to perform shadow stack operations on user shadow stack. Like during signal delivery and sigreturn, shadow stack token must be created and validated respectively. Thus shadow stack access for kernel must be enabled. In future when kernel shadow stacks are enabled for linux kernel, it must be enabled as early as possible for better coverage and prevent imbalance between regular stack and shadow stack. After `relocate_enable_mmu` has been done, this is as early as possible it can enabled. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/head.S | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index 766bd33f10cb..a22ab8a41672 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -517,4 +517,8 @@ void asm_offsets(void) DEFINE(FREGS_A6, offsetof(struct ftrace_regs, a6)); DEFINE(FREGS_A7, offsetof(struct ftrace_regs, a7)); #endif + DEFINE(SBI_EXT_FWFT, SBI_EXT_FWFT); + DEFINE(SBI_EXT_FWFT_SET, SBI_EXT_FWFT_SET); + DEFINE(SBI_FWFT_SHADOW_STACK, SBI_FWFT_SHADOW_STACK); + DEFINE(SBI_FWFT_SET_FLAG_LOCK, SBI_FWFT_SET_FLAG_LOCK); } diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 356d5397b2a2..6244408ca917 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -164,6 +164,12 @@ secondary_start_sbi: call relocate_enable_mmu #endif call .Lsetup_trap_vector + li a7, SBI_EXT_FWFT + li a6, SBI_EXT_FWFT_SET + li a0, SBI_FWFT_SHADOW_STACK + li a1, 1 /* enable supervisor to access shadow stack access */ + li a2, SBI_FWFT_SET_FLAG_LOCK + ecall scs_load_current call smp_callin #endif /* CONFIG_SMP */ @@ -320,6 +326,12 @@ SYM_CODE_START(_start_kernel) la tp, init_task la sp, init_thread_union + THREAD_SIZE addi sp, sp, -PT_SIZE_ON_STACK + li a7, SBI_EXT_FWFT + li a6, SBI_EXT_FWFT_SET + li a0, SBI_FWFT_SHADOW_STACK + li a1, 1 /* enable supervisor to access shadow stack access */ + li a2, SBI_FWFT_SET_FLAG_LOCK + ecall scs_load_current #ifdef CONFIG_KASAN