From mboxrd@z Thu Jan 1 00:00:00 1970 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 8433DCD3427 for ; Tue, 5 May 2026 16:07:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type: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=1wXOKxD5sGL6mfcCXWS3lqXU5doM7M6+nrpyXTdgpAo=; b=XoRYH78wiKKlrwrgCNbZoHT+KL a3hJx/CT8puL+4XhXH/qLnN8++nGLcjQgRtlFXQjA0HMGHkrmoFNS0LkuEz/wkiIomcqicz1hj7CJ vpCdHWxCmzCW3XSr4j6oXwITWKHwBS94I8G3xgbQqyp5s6sg2l7/a3pgIrbU9WWvINb4Vcc0EfsAd UsQp/XfIHdLwRzbzsdPKwBPSFmOrsT06THoa4lWjcxkcYDOAvWr99FFaeQzV/W2rsxElRi7kA6KCU ZVRTiPeH8vGKGz5P4d6SmWJBLNSoGg8OkwzeRb99bJiNsOBWisYtYoed5zIt2oI6OfrbE3wrFOL5i faRpiZ0Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wKIJ0-0000000Gmqv-0rqw; Tue, 05 May 2026 16:07:30 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wKIIy-0000000Gmof-1U9S for linux-arm-kernel@lists.infradead.org; Tue, 05 May 2026 16:07:29 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DF37514BF; Tue, 5 May 2026 09:07:21 -0700 (PDT) Received: from localhost.localdomain (e123572-lin.cambridge.arm.com [10.1.194.54]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 21CC83F763; Tue, 5 May 2026 09:07:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1777997247; bh=6h53wIhICiTdohxtZ773SNUpArSJEO1cKJs65JAmPCY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=d2gpOI5gY7qiZ6klE1noMUGXimHfRGXXfFw+/eM4tO7XW+EALiXnHmq1wjynsIv+G KggwbbLDH3ULPGGYBhxAF57HxF7iuKb7O+JN4F44w7LnIGL5e2z5c1jXFkxZrybi0O ZFh8CEGhMPWhgXvF9lkm0kTEcgXMpk4ij4xg1hsI= From: Kevin Brodsky Date: Tue, 05 May 2026 17:05:54 +0100 Subject: [PATCH RFC v7 05/24] arm64: Implement asm/kpkeys.h using POE MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260505-kpkeys-v7-5-20c0bdd97197@arm.com> References: <20260505-kpkeys-v7-0-20c0bdd97197@arm.com> In-Reply-To: <20260505-kpkeys-v7-0-20c0bdd97197@arm.com> To: linux-hardening@vger.kernel.org Cc: Kevin Brodsky , Andrew Morton , Andy Lutomirski , Catalin Marinas , Dave Hansen , "David Hildenbrand (Arm)" , Ira Weiny , Jann Horn , Jeff Xu , Joey Gouly , Kees Cook , Linus Walleij , Marc Zyngier , Mark Brown , Matthew Wilcox , Maxwell Bland , "Mike Rapoport (IBM)" , Peter Zijlstra , Pierre Langlois , Quentin Perret , Rick Edgecombe , Ryan Roberts , Will Deacon , Yang Shi , Yeoreum Yun , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, x86@kernel.org, Lorenzo Stoakes , Thomas Gleixner , Vlastimil Babka X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1777997220; l=2665; i=kevin.brodsky@arm.com; s=20260427; h=from:subject:message-id; bh=6h53wIhICiTdohxtZ773SNUpArSJEO1cKJs65JAmPCY=; b=kMmOZAzlr5JiNYZ8sm1nG7q9J8yJ+5iLF5bQYnzwypt2/PnQ0z9eV0aNwTIPyYfR+nQ0ESKVw Fi7x6wj+1aNCumcVAwAGrapKsxO92BmroPdMDLaQgffctO55Vf5ZLh3 X-Developer-Key: i=kevin.brodsky@arm.com; a=ed25519; pk=N2QG+eJKrvkNovwhhwJhnJ4+ScVfsGCHldmqLfcMTFs= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260505_090728_481008_FAB48613 X-CRM114-Status: GOOD ( 16.44 ) X-BeenThere: linux-arm-kernel@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-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Implement the kpkeys interface if CONFIG_ARM64_POE is enabled. The permissions for KPKEYS_PKEY_DEFAULT (pkey 0) are set to RWX as this pkey is also used for code mappings. To allow to be included from assembly, also add appropriate #ifdef's to . Signed-off-by: Kevin Brodsky --- arch/arm64/include/asm/kpkeys.h | 59 +++++++++++++++++++++++++++++++++++++++++ arch/arm64/include/asm/por.h | 4 +++ 2 files changed, 63 insertions(+) diff --git a/arch/arm64/include/asm/kpkeys.h b/arch/arm64/include/asm/kpkeys.h new file mode 100644 index 000000000000..7dad3532cacf --- /dev/null +++ b/arch/arm64/include/asm/kpkeys.h @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_KPKEYS_H +#define __ASM_KPKEYS_H + +#include +#include +#include + +#include + +/* + * Equivalent to por_set_kpkeys_context(0, KPKEYS_CTX_DEFAULT), but can also be + * used in assembly. + */ +#define POR_EL1_INIT POR_ELx_PERM_PREP(KPKEYS_PKEY_DEFAULT, POE_RWX) + +#ifndef __ASSEMBLY__ + +static inline bool arch_supports_kpkeys(void) +{ + return system_supports_poe(); +} + +#ifdef CONFIG_ARM64_POE + +static inline u64 por_set_kpkeys_context(u64 por, int ctx) +{ + por = por_elx_set_pkey_perms(por, KPKEYS_PKEY_DEFAULT, POE_RWX); + + return por; +} + +static __always_inline void __kpkeys_set_pkey_reg_nosync(u64 pkey_reg) +{ + write_sysreg_s(pkey_reg, SYS_POR_EL1); +} + +static __always_inline int arch_kpkeys_set_context(int ctx) +{ + u64 prev_por = read_sysreg_s(SYS_POR_EL1); + u64 new_por = por_set_kpkeys_context(prev_por, ctx); + + __kpkeys_set_pkey_reg_nosync(new_por); + isb(); + + return prev_por; +} + +static __always_inline void arch_kpkeys_restore_pkey_reg(u64 pkey_reg) +{ + __kpkeys_set_pkey_reg_nosync(pkey_reg); + isb(); +} + +#endif /* CONFIG_ARM64_POE */ + +#endif /* __ASSEMBLY__ */ + +#endif /* __ASM_KPKEYS_H */ diff --git a/arch/arm64/include/asm/por.h b/arch/arm64/include/asm/por.h index bffb4d2b1246..58dce4b8021b 100644 --- a/arch/arm64/include/asm/por.h +++ b/arch/arm64/include/asm/por.h @@ -10,6 +10,8 @@ #define POR_EL0_INIT POR_ELx_PERM_PREP(0, POE_RWX) +#ifndef __ASSEMBLY__ + static inline bool por_elx_allows_read(u64 por, u8 pkey) { u8 perm = POR_ELx_PERM_GET(pkey, por); @@ -38,4 +40,6 @@ static inline u64 por_elx_set_pkey_perms(u64 por, u8 pkey, u64 perms) return (por & ~(POE_MASK << shift)) | (perms << shift); } +#endif /* __ASSEMBLY__ */ + #endif /* _ASM_ARM64_POR_H */ -- 2.51.2