From patchwork Mon Nov 11 20:53:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871377 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 21075D3ABFB for ; Mon, 11 Nov 2024 21:06:13 +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=/hCe7OQH7sT0H5/5DChnsUV7btCaYTuAeRj2i7sp75A=; b=cd+F7ypZwhZIni Og7lRLB7rV+ohZToDRMjiRUBnZiI2h6lvT1KBXdTzf2WyFmPvy2Isi8gRJxGPl8JOY++xziVkwmvk gnPRL/XFqq84UFpDM2zvwYaS1juFD9z+6cr8ULmRLbc+ENCvEy0PBlExtvJLSb3H54NuUcFw9CNt1 M9o1xOb8RRdAi9mt36sPouWHa5Qj1p8QDRK9rAyUcU3txEsQt9c7kP3FnJZKRZHHznJlkTIABCttT aRb37GGeK1FKaV4LoLP+hgsiGs6QEE/UnzqnqBRdfF7QPjH9nvxWayyAuLcUcUc7cS+0qPHNEp/Qc Cb4zuJXGebyPTkzbSDIg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tAbbs-00000001Iuj-1V0D; Mon, 11 Nov 2024 21:06:08 +0000 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tAbQP-00000001Fr8-3Pc8 for linux-riscv@lists.infradead.org; Mon, 11 Nov 2024 20:54:22 +0000 Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-7ea9739647bso3401792a12.0 for ; Mon, 11 Nov 2024 12:54:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358456; x=1731963256; 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=HvFaRabdOYgj/FaUxHA5lpjBxuSoaWHPSkkVGjDU8NY=; b=PjyFFQ8jGLBlHtemcEt27Eb62edgzgyzTCzZKbxop8wI2d8UpPDsFk5WXv3sSVIYk3 7nVBFy4JoyEwgOM/Sm3+OrK+RjHstACCSD1Ccef676L81dHiKfpiCwBUlnmXZ4weQ3MI EqkLemvRA62MNA7eVfMm2gVeV91x+JLHTPiCUiJ+Y/BGdJ0i7x9PxR3JepRlLjay5lhQ gyCwaNaiBubTY9PUjmGrSQw2+zy0aiOykYhzkVghW+mvUyRC99vyo/8sGJ9bTbRoEXNw cy8mS9jpgUwmUm4pRurAk43VX8bOfVRq4Zm7yU490o0D73i/6TRr3+ODXU8MNsInfID1 2OVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358456; x=1731963256; 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=HvFaRabdOYgj/FaUxHA5lpjBxuSoaWHPSkkVGjDU8NY=; b=g0nO6TXJDI6pBug5qc/cx7rEgYsVzibSCszjbw6+5wphgAWpIm4q/vHKLvIzwKJAD+ IW9hRLBtYq3qO2XXRfeRUqes4QTy1eOfr1Uxqhts3Se1gv+NP66R7y5Ki6jf/DTCu08P RrYAGQHyLJZKwlwuq3Ej48M3f0m03G1VGXBFqOiiU1F5pLjTzAYc007zuKh+0vSYEZId 6hWTKG6Y65MaZD8CTeBn34fSsMzy9acq/zmpNzyEMqYAAQZL3QxUMP0nht4m65SvXsRP 9IzFSsrr1Xvgspv6f78ZeMs4D78IF1SjbQw5UNHRod92YF4VgBqvTfnGU6elIQznxofF BGHQ== X-Forwarded-Encrypted: i=1; AJvYcCXPc3eq7UNvxzDrJQvd/vCEo7zaGiqf6b3oSNFk13abkHwgT6W2GqFS5B2K2opGHkWZzUM+5H01cKoksA==@lists.infradead.org X-Gm-Message-State: AOJu0YzYzfzDlMGvXv8hJYbyz2CiPj1nB4Hi/fEyZezPt5MDxXAWFyVB mk16FprrWWjkaFkCxHYxdUbs7dYymzIdtLAXuWWCGWs+5RB5EqEhyFW5DMZMFik= X-Google-Smtp-Source: AGHT+IHkeWur3sgROxcRoGTFfP+xBQFc9m8RxiHforXzu5iQMPat3nHq0j8ICMSCMJgsQveJpQW4TA== X-Received: by 2002:a17:90b:4acb:b0:2e2:af53:9326 with SMTP id 98e67ed59e1d1-2e9b177fc76mr19427178a91.30.1731358455997; Mon, 11 Nov 2024 12:54:15 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:15 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:53:53 -0800 Subject: [PATCH v8 08/29] riscv mm: manufacture shadow stack pte MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-8-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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-20241111_125417_974819_F92B6921 X-CRM114-Status: GOOD ( 10.19 ) 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 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 Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 4948a1f18ae8..2c6edc8d04a3 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -421,6 +421,11 @@ 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) +{ + 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) @@ -738,6 +743,11 @@ 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) +{ + 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)));