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 A601CECAAA1 for ; Tue, 1 Nov 2022 13:34: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: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=DC/SdQs8oMUgAPeHq/9/2M6yUE+UmY+D71Zc610v6vE=; b=d6x2Jr1bhITOaK C64mhL5zTfo0MWb7NWzo5yZIJElxu0nJPcjzDN2vCJzQ8azE7RynUAq4OLvm4Q0jgA+NIj+t9b5Fr 3DIXP8DQViA0q24XtIqpYUjskSkJdyALkUfml/e94zcqYZluA0tghcAMy3BdQgie8IZXmvhafKVHH SS91rUr+nBSsOX3j6ad7klRhlSeji5ct6GnsLHRREAvPoXq+qWOYzhkj6xojuBRUiLWIazRiQEnKW dMjqBWP/o0ehjxdUP4MABooIA4gR+MLQZQFJ/F9kAo0HsVuvsPRg+0ybmF+xpdtIJwqFHZ9flHNWy MW7orkHfgyuuHOyZLa4w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oprOR-005I62-Ro; Tue, 01 Nov 2022 13:33:27 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oprON-005I5N-NO for linux-arm-kernel@lists.infradead.org; Tue, 01 Nov 2022 13:33:25 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3D69BB81D8E; Tue, 1 Nov 2022 13:33:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF039C433D6; Tue, 1 Nov 2022 13:33:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667309599; bh=5XxivnaAct8X5b7nrwCTNG1mC1yuQ5qBa7xK3g6EmMo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=COwcbDtIF+lWbHFixg1gMjywf1TEzsx5s9v51BvESMJ5UOHlB3qqY7GxVp5XgQ2BK lXToQns5J0TNRDqRmA9bMbsOPQg77lOwtrEVUdM96D8d01jhWYj11j1uRflc3+ACzt 9I/Z08qdB7JaCvXw6IA/8z3gEOlDe4P7nt4fgQ72v+9CnJPZa4KXUO0KdGV/8RfLo0 /hbfdwrp7hAgQj+7j67ulv9l2n6DZrtBRhiV1mGrrdBu6cI20z/ge9XEum2fF3x2Bg eWqGIlAE4Nm78Gqz7tY/+eS02kTZVH0Gb08/azJOtCenxoH2Wxtr+EgG00q+NcvY4N nvvx25qfWTUsw== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1oprOH-002yyQ-BG; Tue, 01 Nov 2022 13:33:17 +0000 Date: Tue, 01 Nov 2022 13:33:16 +0000 Message-ID: <86bkpqer4z.wl-maz@kernel.org> From: Marc Zyngier To: Mark Brown Cc: Catalin Marinas , Will Deacon , Peter Maydell , Richard Henderson , Vincent Donnefort , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, stable@vger.kernel.org Subject: Re: [PATCH v2 2/2] KVM: arm64: Trap access to SMPRI_EL1 and TPIDR2 in VHE mode In-Reply-To: <20221101112716.52035-3-broonie@kernel.org> References: <20221101112716.52035-1-broonie@kernel.org> <20221101112716.52035-3-broonie@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: broonie@kernel.org, catalin.marinas@arm.com, will@kernel.org, peter.maydell@linaro.org, richard.henderson@linaro.org, vdonnefort@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221101_063324_090518_1E287466 X-CRM114-Status: GOOD ( 37.86 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, 01 Nov 2022 11:27:15 +0000, Mark Brown wrote: > > On systems with SME access to the SMPRI_EL1 priority management register is > controlled by the nSMPRI_EL1 fine grained trap and TPIDR2_EL0 is controlled > by nTPIDR2_EL0. We manage these traps in nVHE mode but do not do so when in > VHE mode, add the required management. > > Without this these registers could be used as side channels where implemented. > > Fixes: 861262ab8627 ("KVM: arm64: Handle SME host state when running guests") > Signed-off-by: Mark Brown > Cc: stable@vger.kernel.org > --- > arch/arm64/kvm/hyp/vhe/switch.c | 26 ++++++++++++++++++++++++-- > 1 file changed, 24 insertions(+), 2 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c > index 7acb87eaa092..9dac3a1a85f7 100644 > --- a/arch/arm64/kvm/hyp/vhe/switch.c > +++ b/arch/arm64/kvm/hyp/vhe/switch.c > @@ -63,10 +63,20 @@ static void __activate_traps(struct kvm_vcpu *vcpu) > __activate_traps_fpsimd32(vcpu); > } > > - if (cpus_have_final_cap(ARM64_SME)) > + if (cpus_have_final_cap(ARM64_SME)) { > write_sysreg(read_sysreg(sctlr_el2) & ~SCTLR_ELx_ENTP2, > sctlr_el2); I still question this. As far as I can tell, it only affects the host context (HCR_EL2.{E2H,TGE}={1,1}). This is outlined in the description of the HFGWTR_EL2.nTPIDR2_EL0 bit: If EL2 is implemented and enabled in the current Security state, HCR_EL2.{E2H, TGE} != {1, 1}, and either EL3 is not implemented or SCR_EL3.FGTEn == 1, then MSR writes of TPIDR2_EL0 at EL1 and EL0 using AArch64 are trapped to EL2 and reported with EC syndrome value 0x18, unless the write generates a higher priority exception. Similar language is there for the read counterpart. So I can only conclude that messing with SCTLR_EL2 is superfluous and doesn't affect the execution in a guest context. > > + sysreg_clear_set_s(SYS_HFGRTR_EL2, > + HFGxTR_EL2_nSMPRI_EL1_MASK | > + HFGxTR_EL2_nTPIDR2_EL0_MASK, > + 0); > + sysreg_clear_set_s(SYS_HFGWTR_EL2, > + HFGxTR_EL2_nSMPRI_EL1_MASK | > + HFGxTR_EL2_nTPIDR2_EL0_MASK, > + 0); > + } > + > write_sysreg(val, cpacr_el1); > > write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1); > @@ -88,9 +98,21 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu) > */ > asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); > > - if (cpus_have_final_cap(ARM64_SME)) > + if (cpus_have_final_cap(ARM64_SME)) { > + /* > + * Enable access to SMPRI_EL1 - we don't need to > + * control nTPIDR2_EL0 in VHE mode. > + */ This comment is factually wrong. > + sysreg_clear_set_s(SYS_HFGRTR_EL2, 0, > + HFGxTR_EL2_nSMPRI_EL1_MASK | > + HFGxTR_EL2_nTPIDR2_EL0_MASK); > + sysreg_clear_set_s(SYS_HFGWTR_EL2, 0, > + HFGxTR_EL2_nSMPRI_EL1_MASK | > + HFGxTR_EL2_nTPIDR2_EL0_MASK); > + > write_sysreg(read_sysreg(sctlr_el2) | SCTLR_ELx_ENTP2, > sctlr_el2); Same as above. It serves no purpose AFAICT. > + } > > write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1); > And then you realise that the VHE and nVHE behaviours are strictly identical and that they should simply be merged. Or rather, that we should make use of the existing infrastructure to enable/disable the trapping of the SME ops when it actually matters: at load/put time for VHE, at enter/exit for nVHE. I came up with the following patch, compile-tested only. Thanks, M. >From 5c0913a6de47e32b071ea5534319015e0a61db71 Mon Sep 17 00:00:00 2001 From: Marc Zyngier Date: Tue, 1 Nov 2022 12:19:51 +0000 Subject: [PATCH] KVM: arm64: Fix SMPRI_EL1/TPIDR2_EL0 trapping on VHE The trapping of SMPRI_EL1 and TPIDR2_EL0 currently only really work on nVHE, as only this mode uses the fine-grained trapping that controls these two registers. Move the trapping enable/disable code into __{de,}activate_traps_common(), allowing it to be called when it actually matters on VHE, and remove the flipping of EL2 control for TPIDR2_EL0, which only affects the host access of this register. Fixes: 861262ab8627 ("KVM: arm64: Handle SME host state when running guests") Signed-off-by: Marc Zyngier Cc: stable@vger.kernel.org --- arch/arm64/kvm/hyp/include/hyp/switch.h | 20 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 26 ------------------------- arch/arm64/kvm/hyp/vhe/switch.c | 8 -------- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/include/hyp/switch.h index 6cbbb6c02f66..3330d1b76bdd 100644 --- a/arch/arm64/kvm/hyp/include/hyp/switch.h +++ b/arch/arm64/kvm/hyp/include/hyp/switch.h @@ -87,6 +87,17 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu) vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2); write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2); + + if (cpus_have_final_cap(ARM64_SME)) { + sysreg_clear_set_s(SYS_HFGRTR_EL2, + HFGxTR_EL2_nSMPRI_EL1_MASK | + HFGxTR_EL2_nTPIDR2_EL0_MASK, + 0); + sysreg_clear_set_s(SYS_HFGWTR_EL2, + HFGxTR_EL2_nSMPRI_EL1_MASK | + HFGxTR_EL2_nTPIDR2_EL0_MASK, + 0); + } } static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu) @@ -96,6 +107,15 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu) write_sysreg(0, hstr_el2); if (kvm_arm_support_pmu_v3()) write_sysreg(0, pmuserenr_el0); + + if (cpus_have_final_cap(ARM64_SME)) { + sysreg_clear_set_s(SYS_HFGRTR_EL2, 0, + HFGxTR_EL2_nSMPRI_EL1_MASK | + HFGxTR_EL2_nTPIDR2_EL0_MASK); + sysreg_clear_set_s(SYS_HFGWTR_EL2, 0, + HFGxTR_EL2_nSMPRI_EL1_MASK | + HFGxTR_EL2_nTPIDR2_EL0_MASK); + } } static inline void ___activate_traps(struct kvm_vcpu *vcpu) diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c index 8e9d49a964be..c2cb46ca4fb6 100644 --- a/arch/arm64/kvm/hyp/nvhe/switch.c +++ b/arch/arm64/kvm/hyp/nvhe/switch.c @@ -55,18 +55,6 @@ static void __activate_traps(struct kvm_vcpu *vcpu) write_sysreg(val, cptr_el2); write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2); - if (cpus_have_final_cap(ARM64_SME)) { - val = read_sysreg_s(SYS_HFGRTR_EL2); - val &= ~(HFGxTR_EL2_nTPIDR2_EL0_MASK | - HFGxTR_EL2_nSMPRI_EL1_MASK); - write_sysreg_s(val, SYS_HFGRTR_EL2); - - val = read_sysreg_s(SYS_HFGWTR_EL2); - val &= ~(HFGxTR_EL2_nTPIDR2_EL0_MASK | - HFGxTR_EL2_nSMPRI_EL1_MASK); - write_sysreg_s(val, SYS_HFGWTR_EL2); - } - if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) { struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; @@ -110,20 +98,6 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu) write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2); - if (cpus_have_final_cap(ARM64_SME)) { - u64 val; - - val = read_sysreg_s(SYS_HFGRTR_EL2); - val |= HFGxTR_EL2_nTPIDR2_EL0_MASK | - HFGxTR_EL2_nSMPRI_EL1_MASK; - write_sysreg_s(val, SYS_HFGRTR_EL2); - - val = read_sysreg_s(SYS_HFGWTR_EL2); - val |= HFGxTR_EL2_nTPIDR2_EL0_MASK | - HFGxTR_EL2_nSMPRI_EL1_MASK; - write_sysreg_s(val, SYS_HFGWTR_EL2); - } - cptr = CPTR_EL2_DEFAULT; if (vcpu_has_sve(vcpu) && (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)) cptr |= CPTR_EL2_TZ; diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c index 7acb87eaa092..1a97391fedd2 100644 --- a/arch/arm64/kvm/hyp/vhe/switch.c +++ b/arch/arm64/kvm/hyp/vhe/switch.c @@ -63,10 +63,6 @@ static void __activate_traps(struct kvm_vcpu *vcpu) __activate_traps_fpsimd32(vcpu); } - if (cpus_have_final_cap(ARM64_SME)) - write_sysreg(read_sysreg(sctlr_el2) & ~SCTLR_ELx_ENTP2, - sctlr_el2); - write_sysreg(val, cpacr_el1); write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1); @@ -88,10 +84,6 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu) */ asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); - if (cpus_have_final_cap(ARM64_SME)) - write_sysreg(read_sysreg(sctlr_el2) | SCTLR_ELx_ENTP2, - sctlr_el2); - write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1); if (!arm64_kernel_unmapped_at_el0()) -- 2.34.1 -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel