From patchwork Fri Mar 29 04:44:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610054 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 2FA1DCD1283 for ; Fri, 29 Mar 2024 04:46:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BBD06B00A3; Fri, 29 Mar 2024 00:46:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 642D96B00A4; Fri, 29 Mar 2024 00:46:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41F6A6B00A5; Fri, 29 Mar 2024 00:46:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1F7016B00A3 for ; Fri, 29 Mar 2024 00:46:22 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D2EC8A127A for ; Fri, 29 Mar 2024 04:46:21 +0000 (UTC) X-FDA: 81948840162.21.27A6528 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) by imf06.hostedemail.com (Postfix) with ESMTP id 2CF33180009 for ; Fri, 29 Mar 2024 04:46:19 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F7FBNfbj; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.180 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=1711687580; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w1ajd+NFF25bzcoUpj6TOVq3gYUXEAPUF2iej3ctG8E=; b=7DezdBbNg0n1cCx5tBHxyBXVbrCzsnNNZNzb3lZLOlmvYne+5x6CfqVEmBKDVOFTgee/ZY 61Wu6F/zCdcfbY4DCNFd/iMxvjg8Wn4Evx2fab4qBcIulAGkIREy5f0svsr/np1cRexfaO 6KEATqPZKAX+ZOoy8sYahRtam4wOgcs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687580; a=rsa-sha256; cv=none; b=Gb2K6CmUhg4fS5tM2J6nqhT1I+NoRDZXi8iLkplMiU2MsRLseIrWMqTlbrBs7c+YZWflDv dzLmOy09Q7ZthOIfXu58tTp6zLRdbyVn4eumn1hdJvqcw8ko/GdxOclPyQDpP/itRe/uZW wr2FZA6v3jf1PV3NP4b4Be3PdLPFgEA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=F7FBNfbj; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.180 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-3c3d7e7402dso1225031b6e.1 for ; Thu, 28 Mar 2024 21:46:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687579; x=1712292379; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=w1ajd+NFF25bzcoUpj6TOVq3gYUXEAPUF2iej3ctG8E=; b=F7FBNfbjd0qjfPZE5c64GDyQHu4SmORqQYXvKI7jKW4RjX4jCYSwzZg9YXRv2Wge9f vrDx3BPBAGWDK+1db+xLBobfXXAIlLEbQL2066jfqzk33OdSNmAVys5/+N3LpXKz8OVI OMlL3gdiFunElsYjR2Mp6KKU9HUB3rI+LXr9LcjFmuebRltI89Gri3lqvt1rk9f+/zL8 C6e0lHso+cf7okoICFJBJI94UNle5RqgQrLNGMn71EzcmtgmhGO60Fzj7NzyvKxSNeng 3zUSqv6cAFlhQ5hp7uxSU7QL5GjsWkstDkmz/LqD1w02i5PpjzXshivRjyc/bI2fWBSr qtTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687579; x=1712292379; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w1ajd+NFF25bzcoUpj6TOVq3gYUXEAPUF2iej3ctG8E=; b=AePUjvMffVchVlIrWK1NI5pOJPnNWWXxEyeh2Y9cvBcnC+7mnPgwCfw3VbBgmjdzfo UcdTR9XcD0Jkwux7Keuwyztd2w1yiF1q1Q0RAhp/pXeIv7puYH2FXIqMV+xYM9Kt1HYE ILD7lMuSXUZTpT/ktxAzvDFuT+alMyVEKVoPiRFSTEqUWP+4KLuUKfV7tdhs1EMoOr6v P4Fg5rHCZXfOIcHPTAtNFy+1r6YwUKG6BjVEXFAqliCtZ7cyOR0OxSlqLCl6aj++cV35 DFDT4qTXz4zIm5D6KQhLuPW3180DDXbcqBQfcXwfe6oVl9YqmjobpUhLNIKoYZmvo0T2 VTSg== X-Forwarded-Encrypted: i=1; AJvYcCWdLLtO/g31Fb1/rB2J6dbvhjzdPa18v7tFOBkxBsKM16eDCOMLGnr4h2qNwKOQLRKTEAvQkZplFqakIKh/5cuDI/Y= X-Gm-Message-State: AOJu0Yw58BcYg4/yZBFzRFrz94fEFAcTFVHnlbWfr9SW1jD1iLJ297Im D0443QbIrLvAACkKF3NkceoYKRFlpzarxh84QTCpsPWFn6dug+uyxPANdk+J0hI= X-Google-Smtp-Source: AGHT+IH3K7i4PAKAFUMOdgNrbbbhpm+48RguclKkN/rSLzo7SW0Yi7+dWUtv4WvqoPHa2AHkl7gVfA== X-Received: by 2002:a05:6808:2126:b0:3c3:ed9f:7e65 with SMTP id r38-20020a056808212600b003c3ed9f7e65mr1522250oiw.21.1711687579203; Thu, 28 Mar 2024 21:46:19 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:18 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@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-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 10/27] riscv mm: manufacture shadow stack pte Date: Thu, 28 Mar 2024 21:44:42 -0700 Message-Id: <20240329044459.3990638-11-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2CF33180009 X-Rspam-User: X-Stat-Signature: 7fryd8s7hkjp73c3xehredzw7xyogc4y X-Rspamd-Server: rspam03 X-HE-Tag: 1711687579-791036 X-HE-Meta: U2FsdGVkX1+m0nC7ulrLG4YY8FZ6xR85+B8MPwbUFqq9SgnZsdJ17RvlUGO6I3EaKpJ5CS6TRndfHKUPIBdq2pNuIcoK3abr5cNhEnHOhzuMujLWSDB6DeEOkGXD0M77lb6d3n0y/6jVJUkDx+Ha/zOOiTxNyXTwewxwLgetN/DEWQWI3LRIDBb82jE63D2sckp4qYOfNhsqf6nqr/oJ3d7e1/SmkAdfxk5NIjDua72pVbkCkbi8/lAODVQetJ9W9WNB1E/NS2UykO4rSAKvZYPHLnguh9EtZvxAUt+b7iiKbrMvXCzKTCc0MueQOFXFnG7UMDYN2h4ps81e1ZTVEP49bTLUFH2GCfAHcfTEbiCbyVuaXO3dCPZgW2Hwhy+XcWS+3CyeMBMQ0GzKf8nVwPdhlUQH/yFsGeKpIBWQ9MvlfSzbTjyAs0hNHnMs7ZZwpe1vVaZAaVTs2XefrTi3Im5fyGP5U5NPdn5pX+42lawy+/SmkDwaS6hhdX3uOuG37I0Gw5GmNBqYPFguvP7UAYxYYhxgKTvcIKFs3OoBWQvPbR6HCdwaEcE1XuJbtcSU477+7nPH2qT7jATd9E3Qvb+sWMGQKsOpGkwWYPhS3FUEcNFp3CF1Ho9J10hoqpaVnKCb6CqcYD3qdOSLd65ahthl99AShado49z4NM6Im3mTJHEwl9mWEgfX9UQZHY6TO8Jg4K/gbENjXQmLrntDOszudBaY+7ELlg9p5d37yh+wr7+uHwhTDFaDu0vMLt3ecOVHcAyQwpAaPeTUWLJzEnJxEw544YO+/BtfjnUXea93n7Tdw/avQdkZ6lEf+OyOn4xeRyfpGauSJ6X3udW0G42yaZDk3WegY3Ksc4AXUfjjoy6Dho3DdwIkW54gugDWgKE7XCeoloifuxyTto6Shq5PT5A1PkUL+KRMTo44A7QBvRlUwvfxGPMx6C0HkVuD+X5YqFIsEq1mD0LD59J cJJ0HdTL CUCh0BicPTl7VUzKzUbuKvReDoO/6Rt3vwzu2ReMDLtVbQHHSEyuTzri2HTraTa3xIIi7s3lGpWwgTXNLRZjXAVkR7MLCGfVmp+nMLPCkILFeoL+DhwxGc4DoNNdpMpZtIsAXfI8RsRnr8r0e82gZNVa2+XCPbKVKEfJaox7cYCZHXHbDH+IasAo2VDAtoT7x5nj7nxy1JoCowP86eLB3Vr68N9uAf1EHdVu5cfPohtWihCc= 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: This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 4d5983bc6766..6362407f1e83 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -408,6 +408,12 @@ static inline pte_t pte_mkwrite_novma(pte_t pte) return __pte(pte_val(pte) | _PAGE_WRITE); } +static inline pte_t pte_mkwrite_shstk(pte_t pte) +{ + /* shadow stack on risc-v is XWR = 010. Clear everything and only set _PAGE_WRITE */ + return __pte((pte_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + /* static inline pte_t pte_mkexec(pte_t pte) */ static inline pte_t pte_mkdirty(pte_t pte) @@ -693,6 +699,12 @@ static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); } +static inline pmd_t pmd_mkwrite_shstk(pmd_t pte) +{ + /* shadow stack on risc-v is XWR = 010. Clear everything and only set _PAGE_WRITE */ + return __pmd((pmd_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + static inline pmd_t pmd_wrprotect(pmd_t pmd) { return pte_pmd(pte_wrprotect(pmd_pte(pmd)));