Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp868214lqh; Thu, 28 Mar 2024 21:48:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWIoK+UFkkLc0fvoRLPep3kU6QegTauGQWDWpAoMa7aIhnRNVcQqeYsL00m3+tCfR2EUGwfZdyJTKlpVl51vq5oYPP65fVT9VCX1HM10w== X-Google-Smtp-Source: AGHT+IFRhpwGkObCL3bKG6RdZDBwonRSG1De/B8Q/8NnGydz+Re2j8gH/5fmyqYlhDrEf03kwFRP X-Received: by 2002:a05:6808:398c:b0:3c3:d29e:428 with SMTP id gq12-20020a056808398c00b003c3d29e0428mr1544915oib.2.1711687691183; Thu, 28 Mar 2024 21:48:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711687691; cv=pass; d=google.com; s=arc-20160816; b=tuzHS7mJxxpB0a9pb86tknwUGXjQcXIGXTZKLcXZBxW0SBSW+WGeKletEZDRTAn0Mk BDUq3a7WaYcWLsrmubDibwWndiyIIhm7FBjmJwSX8ckGb9VtmzDjSP8VTvjQkIzCD8oG DVl2ZrBbyFJKH6H8L4uEwYazCkYHtVA3/X0CZ4ixKeS/cwNX48JtHQsOHVzvi9L/XTFD o8FjhNnzlIxcnqsh8eTgDwCOqEWwFH8CZlH0BORwllYtxVrZ5YYUlFy7gbqbKLFMXFp0 MzGadttO07a2TGdOpCQILOYDVrEdzWDxEHtb7DMq3ACHio2zbIYRO2FlPM0dNI6+frQo Cx4A== 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=5zqmWAjHe9apVQg4z/OPwk74OrH36F+ugbAmftC8WUs=; b=l0mYW4OQvdaaN87NmOceSEoF07kURPNtt00tukcv2hjiODPLrG9Im14MowJpOA2Ft7 6ZYFOXJSgDacmmtmITOCeVQsKc5UbUXrZUEC0JNDUJhMOV+hZKV04F9q6D7cmBcuub5S KK2YMXqQMkl7cuRAJpbRWk8l6wcZ11PN9YvX57ZlJMQY6v+3JFLWve8EyZuBZktXW3m+ P/WUSayaYY3OOGPvxMXDsy0vaGQQjC3b7vK4N5QS4wBlcLVi8SoNuDQQZJzWnWhdFbjp ah+hXLYPH9R6MnIP85U8dw0yxVgdxwaR9+4a7A9Tzt7LYc40sdpxAr3njqdoLMrWx17+ Y8sg==; 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="o5XW9U/J"; 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-124127-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124127-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y25-20020a056a00181900b006e684c6f942si3170616pfa.122.2024.03.28.21.48.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:48:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124127-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="o5XW9U/J"; 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-124127-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124127-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D2C1C28A842 for ; Fri, 29 Mar 2024 04:48:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AA93A42065; Fri, 29 Mar 2024 04:46:22 +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="o5XW9U/J" Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (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 534F940C0C for ; Fri, 29 Mar 2024 04:46:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687581; cv=none; b=sEUMA28+tzX+8MPHExuPEzsUVPpW70lQ2Rh1bluw7TtUccu+o6uQ76qAMTR2uNeOsSZpwVwrzwTlpws1QQXtNfvLd4w7gzi5wi6LUNJfUwwO9MIu+D/G95KPI66aQQZt0B0GzVMsgxO21GMQ2LRzEZgYPbABoKg3sWaR1xfpFl0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711687581; c=relaxed/simple; bh=CNl3hprkQqVLkBFECp9sssqM0vv7IwZGZ7tM7RPw44k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=t/K20IddAOmmRMlEME8+BsX0qbF01E09iFAxMByXsB3h3Juqx75UlgWWVtZ4FJW4juSYOrn2tEBdiQ19/LMd9ASS+Gi8Noza8sm3pbWgWK92vwj6/Ih7jlBeasWvWGQ4oOYArCuknl4Ppa97IaNcHiAiBu1JJ8B9rqvuMODupmo= 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=o5XW9U/J; arc=none smtp.client-ip=209.85.167.180 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-oi1-f180.google.com with SMTP id 5614622812f47-3c3d7e7402dso1225026b6e.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=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=o5XW9U/JqDcaia3OfwV5wrptK104pX7I+qFj4VPK+EhVFBXgIdDgSq9fTtdZ4x0AbA iPyUegwdYLZocjaR3edLSpRogZgod0VIEQw01f36724oye1+xdw0xCwcuuZHupCc67Fe Lg8Y+JxL9BnQMOhrk2HTUqIywUvkYWZllKwsQ2tgQY1R96v6ael8T/SFRMPqumi7rgoq M0EN4vC0ilSab0D2nHOl/kDSBtUD2fdozwwpqcZHNkX/FmuUaDH/VZpGZqmtlyFl8qVy RuVgp0TPiV9bnZNTb8i7ozkHHkwszz6Qk1hjum/+6CBWuxRAB/OxEkuvlH5nPIMWPigk 9cww== 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=ZffNCtLvEzeml+n4Qd75a169UTt39B1ibCh/OhuP98+qEPIcOeMzBAiAB49oKnkWwK 6TfaYXwSckp5EbhGrowH8dBkRk8I54I+7U3HC1VvGBlZ++1WJwxhIKxYnlbV98gsr0rZ VgGV8F6F3RQeh6eifIfBsJq+hYm3Vjafr2pLwNHVHAfFXbViotW0mmliSmHZG3Htr3z3 UDPUwN+XOBxDzp+S3bMgOzqQFHNvhbRchjdbd2Ak0PVd4kbmrOQ286KfxadsI0IpHfQ+ W8L6fz6PMLrhXhh15ltqXEnVlV6wHZHjmXr3JkohjqHY0dBI57Yd1jykHZdtjsMqsZ2p k2Hg== X-Forwarded-Encrypted: i=1; AJvYcCUTvSmgo5LM/6cjZ4YIAmq73G1A6jl8/PYacI5pS1JqRjo/QEhutfZnESOeXDjiW8pcn1TGppR3HZHM3/Fm/7UorRsiXwtn5jIxFMmS X-Gm-Message-State: AOJu0YwQicVJfCAydiavRyH9/G0X4rrJaa7dbUErmDKhIPdze9TLjHTV LB69sPmBKwUdBlcdQpITnKRyhk21bVVBqLj73KK88e6++bOb/8dqCfaU05PPXrc= 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> 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