From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F55D3DE44B for ; Tue, 26 May 2026 11:17:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779794245; cv=none; b=kpfU+wX6gYo2lMZZnOVtOVavl4zxc5fvon6wAy5FQpnwGknpzxYZq7DoDtP7RDc3ck4X96P27IF/yLReWblM20yUKSRsTYVcuykFdVj/Bwdr0J5xl45cbcvOIdWDsCzfFP3LsUgBlKd9XLhDxBr7+EAMnD0jcE4s2sLSKSd5wBs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779794245; c=relaxed/simple; bh=ScOjJcY4lUZi/QiKcDOuYUVGfjH/6STcb98MmPgtlWs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WBNRge4Hm6CmitXsVCFoESVFGSgSHKe+cp/PFyLmlxirSW33kS9uj4rXezLzLBZ8nmaVAYABLSVmblwbyEQwS4YeD0aOHYjRfbsNKJvQKwRdE2ML00/vGINGHU2rNI//UBWAsmSH0468+qPmM4j0UzrpdY0HJ6o5UJwN2HFVu2s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=p57PsWH7; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="p57PsWH7" 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 82706169C; Tue, 26 May 2026 04:17:18 -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 B87673F7D8; Tue, 26 May 2026 04:17:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1779794243; bh=ScOjJcY4lUZi/QiKcDOuYUVGfjH/6STcb98MmPgtlWs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=p57PsWH7Zc/0vwIDEqRb6A8BMGopx8BzxIos9wA6VwJhaJ74/w2g0vmBB0HSm4yEq Z544DK5JqY1WHW0NhjUxiSdJRwI0NSQsMWUca6nP0uuvJLCB8CDP7bOM6XWqXvidh2 nk+vL2A631mKhZf/REEEg2LSG+IZ9Q9hz/WFjSiA= From: Kevin Brodsky Date: Tue, 26 May 2026 12:15:54 +0100 Subject: [PATCH RFC v8 05/24] arm64: Implement asm/kpkeys.h using POE Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260526-kpkeys-v8-5-eaaacdacc67c@arm.com> References: <20260526-kpkeys-v8-0-eaaacdacc67c@arm.com> In-Reply-To: <20260526-kpkeys-v8-0-eaaacdacc67c@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 , Vlastimil Babka , Will Deacon , Yang Shi , Yeoreum Yun , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, x86@kernel.org, Lorenzo Stoakes , Thomas Gleixner X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779794212; l=2665; i=kevin.brodsky@arm.com; s=20260427; h=from:subject:message-id; bh=ScOjJcY4lUZi/QiKcDOuYUVGfjH/6STcb98MmPgtlWs=; b=/HffIylQMDpk1TSgqL1d4Ijti+SGSoyeaOj5/vNUNbYO173PNGvrz3r/7wIUBVck2GnvXCYDK vKmtmOhZJymC2zZtCSQRF67bKDlRhR58WkFHN2xAnO6o1s7iyG2GAdG X-Developer-Key: i=kevin.brodsky@arm.com; a=ed25519; pk=N2QG+eJKrvkNovwhhwJhnJ4+ScVfsGCHldmqLfcMTFs= 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..4dbfeb3dfcfe --- /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 u64 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