From patchwork Tue Oct 29 23:44:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855801 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BEADCD74953 for ; Wed, 30 Oct 2024 00:45:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=WDn4VlYZq3ca69wSaohW0FOcxt97tLajEm1JpMrdLVQ=; b=FHYyFV8lpNcvms ljo3QSCPOQqj2tcqoP3ap24X74pyUlMcN7KS0Ghg6Of74JUZ22wYIXC/74K8P9SpNqQPk5X+E/6BF M0zlbu+Bei0r7B+p/ILuKZFQ+8rvFZ0JyTEwGAgDcZUrWB+2Jvnb31Wh67Zb1wnFAqalF53105/hn QtU84rdCA+m2/pMEG4P9koMzeUovTbU1Q+yHaUVApgsjSrijgR9s5zKS4/dCZBDTtGJhWds3kXExj +izww20/OvvCiCDkOUTYVV+eeVcXuXManjSmPNFAiA725vGyaIplSo+0SLGyS+jQySI961r9V11oh NPVKmYUTTNukCJzDOiGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t5wqH-0000000GMPw-0FPe; Wed, 30 Oct 2024 00:45:45 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t5vtI-0000000GDaX-0EDd for linux-riscv@lists.infradead.org; Tue, 29 Oct 2024 23:44:57 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-71ec12160f6so4359746b3a.3 for ; Tue, 29 Oct 2024 16:44:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245487; x=1730850287; darn=lists.infradead.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=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=Npml2XygJIOALHRtv3JwYbKdU6nnAujRfWcnJ3B4Wftz4BZo2PI2IYzZBFHw/o8v8G bqFBeSEex6dqy2698tcWDIsgRhTcECmTqaS4YQGkZRL7uR4GQt9kJkFQBEoTBo5yL18h Fw5JOMWnQa2j04VrhS6A0F9ORB3zevA9Os1Hj33M1WAsUZbBgK21oY032C1rybIrOwId q3XJEGkRmtrS+Wo9/OC/aCDy0RtY0n2BEW2dS8N0wURlYXPj7HOKg80PLC0kq8lu+UtE xpgI+mOy6gGo2ADtPzVwnFgL+AHRK+P0gIM1Xv1ZHpvGEUa41wG/ba9+GV+EFpL7ZymN 2Dlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245487; x=1730850287; 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=vcvA1auSaL3VFQzS81Rs8lzkbF+oHOz6Si8rlZEwmTE=; b=JfMR8yc7RoVHKWzaxygz2Z6Z3mxtvZjd3baPe/mJVa5RGdhQxH4K8I/6CwYSoFYe8+ xyI+1Jdsx7P/VAsUJCb12mU8VMrfnTOGupLEY9GhM5rbHFEOzgc1VVwDC7jqoX+dGpr1 +5obO5W3nWvxE/TrhoE1cWpbV6AytCgerLht8d0IkCxoY7+zs+51/euXGRYUDuYdxZDK 7A18TGPGpXJWwE8N9LskQ0z+NCdxEcHaFSIS3MW+iLCYhyc0cIljrJOpZfRR71K6pJgx WwxWnvoPhk6tL4r1tIzssu9ULqU2Lte7SSYxt02gyewdPZw8uPHiyaTukADoRt6uUN1d yN0Q== X-Forwarded-Encrypted: i=1; AJvYcCVq1pvAVljvWqhY4JJH2X+sECnzNpAM837Bm2fzNcVrtSSbXOaGO159PXbuzdPTu7/fh96SclG0PnQB5w==@lists.infradead.org X-Gm-Message-State: AOJu0YwQFRq+76vcbBGjCwmfzYDP+P7zlQii2Hsvn9wMY2oXxpj+0jez QWjSJshRO9r15jJB4BPfVQfTA8IS6OTQZmOrSI9luMyKkYFyomB2yMp89lBduP4= X-Google-Smtp-Source: AGHT+IGGMP4Pw+lp+LPCrok5EqwNoJFdizqNNUNEZiV3YZc6kA7wl06mzdMMPrxb/Znt26d5Smw70g== X-Received: by 2002:a05:6a20:c797:b0:1d4:fcd0:5bea with SMTP id adf61e73a8af0-1d9a83ab7f2mr18393767637.6.1730245486894; Tue, 29 Oct 2024 16:44:46 -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.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:46 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:12 -0700 Subject: [PATCH v7 12/32] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-12-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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241029_164448_373488_205A9369 X-CRM114-Status: GOOD ( 11.43 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range (i.e. VM_SHADOW_STACK) Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 2c6edc8d04a3..7963ab11d924 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -416,6 +416,10 @@ static inline pte_t pte_wrprotect(pte_t pte) /* static inline pte_t pte_mkread(pte_t pte) */ +struct vm_area_struct; +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); +#define pte_mkwrite pte_mkwrite + static inline pte_t pte_mkwrite_novma(pte_t pte) { return __pte(pte_val(pte) | _PAGE_WRITE); @@ -738,6 +742,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pte_pmd(pte_mkyoung(pmd_pte(pmd))); } +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); +#define pmd_mkwrite pmd_mkwrite + static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) { return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index 4ae67324f992..be5d38546bb3 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -155,3 +155,20 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + return pte_mkwrite_novma(pte); +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + return pmd_mkwrite_novma(pmd); +} +