Skip to content

Commit 51729fb

Browse files
brooniectmarinas
authored andcommitted
KVM: arm64: Trap SME usage in guest
SME defines two new traps which need to be enabled for guests to ensure that they can't use SME, one for the main SME operations which mirrors the traps for SVE and another for access to TPIDR2 in SCTLR_EL2. For VHE manage SMEN along with ZEN in activate_traps() and the FP state management callbacks, along with SCTLR_EL2.EnTPIDR2. There is no existing dynamic management of SCTLR_EL2. For nVHE manage TSM in activate_traps() along with the fine grained traps for TPIDR2 and SMPRI. There is no existing dynamic management of fine grained traps. Signed-off-by: Mark Brown <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 9080774 commit 51729fb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

arch/arm64/kvm/hyp/nvhe/switch.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,24 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
4747
val |= CPTR_EL2_TFP | CPTR_EL2_TZ;
4848
__activate_traps_fpsimd32(vcpu);
4949
}
50+
if (cpus_have_final_cap(ARM64_SME))
51+
val |= CPTR_EL2_TSM;
5052

5153
write_sysreg(val, cptr_el2);
5254
write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
5355

56+
if (cpus_have_final_cap(ARM64_SME)) {
57+
val = read_sysreg_s(SYS_HFGRTR_EL2);
58+
val &= ~(HFGxTR_EL2_nTPIDR2_EL0_MASK |
59+
HFGxTR_EL2_nSMPRI_EL1_MASK);
60+
write_sysreg_s(val, SYS_HFGRTR_EL2);
61+
62+
val = read_sysreg_s(SYS_HFGWTR_EL2);
63+
val &= ~(HFGxTR_EL2_nTPIDR2_EL0_MASK |
64+
HFGxTR_EL2_nSMPRI_EL1_MASK);
65+
write_sysreg_s(val, SYS_HFGWTR_EL2);
66+
}
67+
5468
if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
5569
struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt;
5670

@@ -94,9 +108,25 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
94108

95109
write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2);
96110

111+
if (cpus_have_final_cap(ARM64_SME)) {
112+
u64 val;
113+
114+
val = read_sysreg_s(SYS_HFGRTR_EL2);
115+
val |= HFGxTR_EL2_nTPIDR2_EL0_MASK |
116+
HFGxTR_EL2_nSMPRI_EL1_MASK;
117+
write_sysreg_s(val, SYS_HFGRTR_EL2);
118+
119+
val = read_sysreg_s(SYS_HFGWTR_EL2);
120+
val |= HFGxTR_EL2_nTPIDR2_EL0_MASK |
121+
HFGxTR_EL2_nSMPRI_EL1_MASK;
122+
write_sysreg_s(val, SYS_HFGWTR_EL2);
123+
}
124+
97125
cptr = CPTR_EL2_DEFAULT;
98126
if (vcpu_has_sve(vcpu) && (vcpu->arch.flags & KVM_ARM64_FP_ENABLED))
99127
cptr |= CPTR_EL2_TZ;
128+
if (cpus_have_final_cap(ARM64_SME))
129+
cptr &= ~CPTR_EL2_TSM;
100130

101131
write_sysreg(cptr, cptr_el2);
102132
write_sysreg(__kvm_hyp_host_vector, vbar_el2);

arch/arm64/kvm/hyp/vhe/switch.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
4141

4242
val = read_sysreg(cpacr_el1);
4343
val |= CPACR_EL1_TTA;
44-
val &= ~(CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN);
44+
val &= ~(CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN |
45+
CPACR_EL1_SMEN_EL0EN | CPACR_EL1_SMEN_EL1EN);
4546

4647
/*
4748
* With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to
@@ -62,6 +63,10 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
6263
__activate_traps_fpsimd32(vcpu);
6364
}
6465

66+
if (cpus_have_final_cap(ARM64_SME))
67+
write_sysreg(read_sysreg(sctlr_el2) & ~SCTLR_ELx_ENTP2,
68+
sctlr_el2);
69+
6570
write_sysreg(val, cpacr_el1);
6671

6772
write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1);
@@ -83,6 +88,10 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
8388
*/
8489
asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT));
8590

91+
if (cpus_have_final_cap(ARM64_SME))
92+
write_sysreg(read_sysreg(sctlr_el2) | SCTLR_ELx_ENTP2,
93+
sctlr_el2);
94+
8695
write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
8796

8897
if (!arm64_kernel_unmapped_at_el0())

0 commit comments

Comments
 (0)