Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp3035800lqz; Wed, 3 Apr 2024 16:45:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUduSjfaHicfhpc7nN3Y8JFCBxrkfktpFqnRjBUGIddxiN85UaYAd/kyT/gav3xX1+lsmHRE6zUMd+Nqbx49G9A4B4NrQ3q0cV4qspAaA== X-Google-Smtp-Source: AGHT+IE+Iz0x6zROS1YF43VyWZAXVOiwYxrtlTwXSbLBvh9dYiYxspw4tdK1Aib8Laza363MGOFI X-Received: by 2002:a05:622a:8ce:b0:434:5040:51e2 with SMTP id i14-20020a05622a08ce00b00434504051e2mr175061qte.2.1712187904392; Wed, 03 Apr 2024 16:45:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712187904; cv=pass; d=google.com; s=arc-20160816; b=RRX9KiIT67dMLFtL6wQw79y2s8NonMRHAPCByBB0q2Q+nQVuCOXGm3/346PdY971Xc IcD2t3xewPLYmHFbvVhJFJlZx3rCk9Cmu7ggAhyBgh4rzOcx7ShJWofd++liER7XwF7B WteRBtKepNA0Pq1OVPve8dOVNUDcNojCc/J1BnQW0L/otonne2cZ4qePt+GxxQlnd4hc Cay8WxwIp0IWdQWMJwE6V34A4uBR2YzITtu/8jbraNkGCvehka754bs/yUP/piPELPNV 2AFVcKLY9kSBgjNtyxxVYVadT/qGbUMjwQTx3c8llaxDrIk7tNFK91lKguLQUotbZhAY GHEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=w1ajd+NFF25bzcoUpj6TOVq3gYUXEAPUF2iej3ctG8E=; fh=++IUcoA2VqNd4xZX8wC9MLjxr1Pg83BIblidDZso3LQ=; b=IYhCdMYSvQ3ENN4xUpHx3KAJTUEW4VjFLriZkFoPtKpSxRl9vikRrdJmLB8a4KJsnf XIBOW+Cf+c/JgSJsTKhZMUDtFKXZDbhODo8sg/febHlcaDdZWI30XJ0OzmjdEkDbGNct RkC9VV/3ij6frBDVPJY/ASIhxo1ZvDt2UQy3dPG9SLaY9Q9fMqjgUXlaMCe/LSoORsWW hvW+uYuh0FeCAv3QvGjgkypnq/U+7S1U/guijaOc/X3RCs8gPzhugygYQu08nsduWd06 4ujr2FYyHkTWVVSsR3ndbAPP5I9MmrC1P8WbuHGDgUjJXvzbievlh0I4Kgo6hQZTmXMe POrg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Qin+KnVl; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-130730-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130730-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id b13-20020ac87fcd000000b004312328dd19si15728984qtk.385.2024.04.03.16.45.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:45:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-130730-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Qin+KnVl; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-130730-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130730-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 13F291C20F0A for ; Wed, 3 Apr 2024 23:45:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 25E75158A0B; Wed, 3 Apr 2024 23:42:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="Qin+KnVl" Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFA37158865 for ; Wed, 3 Apr 2024 23:41:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712187722; cv=none; b=nq+eOT+g/0Ixm3LFUIgRIvDNE6ZY+IwzbDvjhc+27BnHvBcOhkrWWhWTJ+aaqgE0prRv5fnDPEkpS4h4vZogzvr1MEUBHV2ayGV/AtHZxqev84hMAx5Vb6YiWiiAhIeyQLc2PDlkqlTLNDp7duuFXRQm+XeStN6eUSGju/zVbpQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712187722; c=relaxed/simple; bh=CNl3hprkQqVLkBFECp9sssqM0vv7IwZGZ7tM7RPw44k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uaiBIK5gkt/Ut6TGW+IBhtCJ8UHyDSf7DYpFdxvPUpzABeMERlBOica1k2Eco+MG4WedHn/384PXaxkDXHRXMIxBsWUsFy6E1pcBbO4DLvwL+0L5iqDykiledCN2deBFy53xckAuy3fwb8cuDpEKAiOJUJ7YEUGrbiZXjhrHhmY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=Qin+KnVl; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1e28856ed7aso2974535ad.0 for ; Wed, 03 Apr 2024 16:41:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187719; x=1712792519; darn=vger.kernel.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=Qin+KnVlIIcZhNoYZvq20/1nCED1I2HIiBhT8a85Fn7F9l+JuoHRoqpHIU0PL3sBbl JV1++fC+Z6wgfSgUDq7gdo7WsTv0s5BMDvz+SWDpikGQIAhrgLdtWzOwBS2ddgoNnwtm mSpP6jBdKPE5nBJA1e9ZxIOnFJhVPY2AlCrqx/QRlC+sP3grwmeZWXdEL9pqq00bbH4R IpceTNxix6ng7ED1u1RDxHGuGAZcSHUwvpF93aOPNnlEBh5m0Kopv+HfQNpZ7OVwbBqu GLaNQ54ZwBKAHqkm/CBcYAWsvC+F6e4ltb8sFoTWAi+JY0KTz24QUN8ZF4LZXoloZ9Uh LBaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187719; x=1712792519; 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=KKXt4gxO+4gsbKWu8MJQG4qBianZjlIhimMWNBdKrqMH+p29SvoIs3a9O5tcbyNOZA edmS1dMdxNHtyrEsHVjQOVvZBq+kYcx+yWgCEpANTJVcb+XfQhwEhfk++I9nqeFawmPg POZnZrbv+Ct/Abp1KtF0uqzzR17TGO6M/MjGKUJsJUsf1kn/XGmnpEgJwdXYNJWhnxZO N02E519aKhONFyjpbZyyyI8Rtul6fgW/lv73blqPchFcbjLKifEufFxx3YBK1bHOEVTS HT30of1YmyPPXQrS9OScr46Tej91GIr+uNGp4SSYnD0aXagit37glsmsdYx1rmKAXvTK tIgg== X-Forwarded-Encrypted: i=1; AJvYcCV0hqOycAQ3C96kVtnYcyUxCzEuWQJHVWoPvwtpk+ciQUNs4piHv+NIyRGin7QAZPNwg08yDVS2TZSV85mXJP38gPFmmUPfICBp7oPC X-Gm-Message-State: AOJu0YwVVXXv9QRmwUmQCLYQABF3hWQLJHkgH/wb4a80FgYCHOIXQSBz kjkD2g9Ebp+J7Veq6SRm2erk+GQxqw1o/Qv12h6Oh4zcHZe2DG0Fn+NnvzxtRXA= X-Received: by 2002:a17:902:f105:b0:1e2:718c:61e with SMTP id e5-20020a170902f10500b001e2718c061emr712135plb.27.1712187719240; Wed, 03 Apr 2024 16:41:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.41.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:41:58 -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, conor@kernel.org Cc: 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, corbet@lwn.net, 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 v3 11/29] riscv mm: manufacture shadow stack pte Date: Wed, 3 Apr 2024 16:34:59 -0700 Message-ID: <20240403234054.2020347-12-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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))); -- 2.43.2