From patchwork Thu Aug 22 15:10:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Gouly X-Patchwork-Id: 13773676 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BA41C52D7C for ; Thu, 22 Aug 2024 15:11:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 269F080034; Thu, 22 Aug 2024 11:11:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 21CB48001E; Thu, 22 Aug 2024 11:11:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 095D580034; Thu, 22 Aug 2024 11:11:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D6D4C8001E for ; Thu, 22 Aug 2024 11:11:52 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 88DDC141597 for ; Thu, 22 Aug 2024 15:11:52 +0000 (UTC) X-FDA: 82480221264.26.21C6889 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf16.hostedemail.com (Postfix) with ESMTP id DE361180006 for ; Thu, 22 Aug 2024 15:11:50 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf16.hostedemail.com: domain of joey.gouly@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=joey.gouly@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724339420; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OpQ2k58t6nEQbtkgVhwz3rLdStOXjxbUhiBjdUnicRA=; b=0YAlJGFdgjEUG1/xGFDVfKFNCSELfCo6HnnMB6wgYQp+N1awzVWI3TzldkTFCwTrmisjDJ 2bJ3r9RenaOck4Y867keGZN3dh7EJTssHKvsulOUfRDK8EZpB6ekaZVTOZnJlY8TsnIkWx Fm6ia4Wy+D8KhhXmLZjufOoXkhfrStE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724339420; a=rsa-sha256; cv=none; b=PLgnpIJFE0WcofprQPuwIRPET1RPOwgnUVO1ijzt8r8lQPq3KtAD1z7tyqlenRm6VIK5Px VmKFt1kS7MCWhY7xYRwK5ti7LxTNeeP3IRv2K9CK0rbNTuWg/WHvCpupAzshNFuXkDDhvO xe64qysfhPE9nohxEZSH2pTZo8tNjE0= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf16.hostedemail.com: domain of joey.gouly@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=joey.gouly@arm.com 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 4605B15BF; Thu, 22 Aug 2024 08:12:16 -0700 (PDT) Received: from e124191.cambridge.arm.com (e124191.cambridge.arm.com [10.1.197.45]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 275E73F58B; Thu, 22 Aug 2024 08:11:46 -0700 (PDT) From: Joey Gouly To: linux-arm-kernel@lists.infradead.org Cc: nd@arm.com, akpm@linux-foundation.org, aneesh.kumar@kernel.org, aneesh.kumar@linux.ibm.com, anshuman.khandual@arm.com, bp@alien8.de, broonie@kernel.org, catalin.marinas@arm.com, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, hpa@zytor.com, joey.gouly@arm.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, maz@kernel.org, mingo@redhat.com, mpe@ellerman.id.au, naveen.n.rao@linux.ibm.com, npiggin@gmail.com, oliver.upton@linux.dev, shuah@kernel.org, skhan@linuxfoundation.org, szabolcs.nagy@arm.com, tglx@linutronix.de, will@kernel.org, x86@kernel.org, kvmarm@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: [PATCH v5 06/30] arm64: context switch POR_EL0 register Date: Thu, 22 Aug 2024 16:10:49 +0100 Message-Id: <20240822151113.1479789-7-joey.gouly@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240822151113.1479789-1-joey.gouly@arm.com> References: <20240822151113.1479789-1-joey.gouly@arm.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DE361180006 X-Stat-Signature: f8ujd1e6dxs79y9qgi4a1uekbzje64yr X-Rspam-User: X-HE-Tag: 1724339510-528824 X-HE-Meta: U2FsdGVkX1+3lUHLPhZSPjrqZ3fPOCb8xXg6T89+tFGbJ6LokyMuOQLsWy/QvVPp8QtwNMDOaGPQpBs0lv8nSi2aT9+HQB6wFvDdXa2BxQRYY3VvavzozFOpYZyW3gqUVY3zYgLnvxs9ZSig8WdOkKcHKZXKxSIOHkKkois+rFj4GI4gs9l3IsNm1TNdAZ+jppKR7PoYh8+b4fnZ6Nj3kRnyRxaDYVixBU5Y7bWMBk8xEHnUfmVzv1Eg/02gxIBCwocgbXUSZ9gfa0T+DSM5ko56XJ3Nx0aLY0tZDvzo4cJf8VVyGdscFAxk1EyUOY6J7GW2jGeKNk+nfcZ1+UzL4ijOEvOdLa7pdnu9aakwTLxBaKK6z2521Wk05ayRZ5P624Tn+cd4jf8TOVZjZVI3mspSyg3JtLyG/61LO3axIUtHZDWgOQ6J8WJLwYuAp4xBxFg6PkNjxsRks4pWdw2WEbj6q6TZbF4tfgScGoFT1dz1Mem9nNWaB0xcxlt6c1DAF5MT4qMY6Kur8yRkhxayRwd8jC+VCtlmnGAUEG03ztVDA4fSt3ASNR8FPo/dInY5oqa4JJ4UdkBPvzuujdX8i/IPmSPw7tNFQ0oL3YOchf7g+WjEK4ys3JZo1630i2lSTpjkFlYqlXo3EE7dwzZVLtU58nn2Z2Yl4GBk1yF7qHYEcjMUKz4ERpTBtLQBiHidFCTeZ4gtc7Caz82uepuICk12AQMYUU+aytr3WwP0sUB7fnxjBxBayKdVyh9ifw+66DFep048yRXkhhNvw8+9kknVBqxVQFzquWxZMptinVUMLSoAYNNVNdeVqHbjbHwgZ0D8Dw7LCP1inEAlDv2T+c3b/7HQkGdrPo8n+dlqN+2z+aZ5GvudDF/CSrnu7sT7i2n/IvI/b0ZPG36n5Ra9zggG+Uedx28njUla9Jx5tiSs3ANsksd+4X4VMCK5z79s/iJyWWXGle2b2mQ2ztj yVRsxw/D qTUZ8OsOZAwKYy6yZ3uOHSDbOTaFOQ86tZDl674X86/GIEeF9xk02jLt8zAV5Lk9coEeMzJjafj2yZIJW3Gp5ifdoD/Trcs+r+bVIu/sfmvblEx8obDQtnZErPfKNjUxB0raCOKJwde8/KD61RJ9DQgBh290yffRvC22EmSooVE6/J+VQR7MH4Vo045MHIkpggYIHNVqCXwDC5neKoJ7bxiFLQ8Ydg1dWV+66tDk6/CqvPeFJixt+Iyye0fY0njk8yZNs38BX8JjHwMM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: POR_EL0 is a register that can be modified by userspace directly, so it must be context switched. Signed-off-by: Joey Gouly Cc: Catalin Marinas Cc: Will Deacon Reviewed-by: Catalin Marinas --- arch/arm64/include/asm/cpufeature.h | 6 ++++++ arch/arm64/include/asm/processor.h | 1 + arch/arm64/include/asm/sysreg.h | 3 +++ arch/arm64/kernel/process.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git arch/arm64/include/asm/cpufeature.h arch/arm64/include/asm/cpufeature.h index 558434267271..3d261cc123c1 100644 --- arch/arm64/include/asm/cpufeature.h +++ arch/arm64/include/asm/cpufeature.h @@ -832,6 +832,12 @@ static inline bool system_supports_lpa2(void) return cpus_have_final_cap(ARM64_HAS_LPA2); } +static inline bool system_supports_poe(void) +{ + return IS_ENABLED(CONFIG_ARM64_POE) && + alternative_has_cap_unlikely(ARM64_HAS_S1POE); +} + int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt); bool try_emulate_mrs(struct pt_regs *regs, u32 isn); diff --git arch/arm64/include/asm/processor.h arch/arm64/include/asm/processor.h index f77371232d8c..e6376f979273 100644 --- arch/arm64/include/asm/processor.h +++ arch/arm64/include/asm/processor.h @@ -184,6 +184,7 @@ struct thread_struct { u64 sctlr_user; u64 svcr; u64 tpidr2_el0; + u64 por_el0; }; static inline unsigned int thread_get_vl(struct thread_struct *thread, diff --git arch/arm64/include/asm/sysreg.h arch/arm64/include/asm/sysreg.h index 4a9ea103817e..494e9efd856f 100644 --- arch/arm64/include/asm/sysreg.h +++ arch/arm64/include/asm/sysreg.h @@ -1077,6 +1077,9 @@ #define POE_RXW UL(0x7) #define POE_MASK UL(0xf) +/* Initial value for Permission Overlay Extension for EL0 */ +#define POR_EL0_INIT POE_RXW + #define ARM64_FEATURE_FIELD_BITS 4 /* Defined for compatibility only, do not add new users. */ diff --git arch/arm64/kernel/process.c arch/arm64/kernel/process.c index 4ae31b7af6c3..a3a61ecdb165 100644 --- arch/arm64/kernel/process.c +++ arch/arm64/kernel/process.c @@ -271,12 +271,23 @@ static void flush_tagged_addr_state(void) clear_thread_flag(TIF_TAGGED_ADDR); } +static void flush_poe(void) +{ + if (!system_supports_poe()) + return; + + write_sysreg_s(POR_EL0_INIT, SYS_POR_EL0); + /* ISB required for kernel uaccess routines when changing POR_EL0 */ + isb(); +} + void flush_thread(void) { fpsimd_flush_thread(); tls_thread_flush(); flush_ptrace_hw_breakpoint(current); flush_tagged_addr_state(); + flush_poe(); } void arch_release_task_struct(struct task_struct *tsk) @@ -371,6 +382,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) if (system_supports_tpidr2()) p->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0); + if (system_supports_poe()) + p->thread.por_el0 = read_sysreg_s(SYS_POR_EL0); + if (stack_start) { if (is_compat_thread(task_thread_info(p))) childregs->compat_sp = stack_start; @@ -495,6 +509,19 @@ static void erratum_1418040_new_exec(void) preempt_enable(); } +static void permission_overlay_switch(struct task_struct *next) +{ + if (!system_supports_poe()) + return; + + current->thread.por_el0 = read_sysreg_s(SYS_POR_EL0); + if (current->thread.por_el0 != next->thread.por_el0) { + write_sysreg_s(next->thread.por_el0, SYS_POR_EL0); + /* ISB required for kernel uaccess routines when chaning POR_EL0 */ + isb(); + } +} + /* * __switch_to() checks current->thread.sctlr_user as an optimisation. Therefore * this function must be called with preemption disabled and the update to @@ -530,6 +557,7 @@ struct task_struct *__switch_to(struct task_struct *prev, ssbs_thread_switch(next); erratum_1418040_thread_switch(next); ptrauth_thread_switch_user(next); + permission_overlay_switch(next); /* * Complete any pending TLB or cache maintenance on this CPU in case