Skip to content

Commit 861262a

Browse files
brooniectmarinas
authored andcommitted
KVM: arm64: Handle SME host state when running guests
While we don't currently support SME in guests we do currently support it for the host system so we need to take care of SME's impact, including the floating point register state, when running guests. Simiarly to SVE we need to manage the traps in CPACR_RL1, what is new is the handling of streaming mode and ZA. Normally we defer any handling of the floating point register state until the guest first uses it however if the system is in streaming mode FPSIMD and SVE operations may generate SME traps which we would need to distinguish from actual attempts by the guest to use SME. Rather than do this for the time being if we are in streaming mode when entering the guest we force the floating point state to be saved immediately and exit streaming mode, meaning that the guest won't generate SME traps for supported operations. We could handle ZA in the access trap similarly to the FPSIMD/SVE state without the disruption caused by streaming mode but for simplicity handle it the same way as streaming mode for now. This will be revisited when we support SME for guests (hopefully before SME hardware becomes available), for now it will only incur additional cost on systems with SME and even there only if streaming mode or ZA are enabled. 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 51729fb commit 861262a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ struct kvm_vcpu_arch {
454454
#define KVM_ARM64_DEBUG_STATE_SAVE_TRBE (1 << 13) /* Save TRBE context if active */
455455
#define KVM_ARM64_FP_FOREIGN_FPSTATE (1 << 14)
456456
#define KVM_ARM64_ON_UNSUPPORTED_CPU (1 << 15) /* Physical CPU not in supported_cpus */
457+
#define KVM_ARM64_HOST_SME_ENABLED (1 << 16) /* SME enabled for EL0 */
457458

458459
#define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
459460
KVM_GUESTDBG_USE_SW_BP | \

arch/arm64/kvm/fpsimd.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
8282

8383
if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
8484
vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
85+
86+
/*
87+
* We don't currently support SME guests but if we leave
88+
* things in streaming mode then when the guest starts running
89+
* FPSIMD or SVE code it may generate SME traps so as a
90+
* special case if we are in streaming mode we force the host
91+
* state to be saved now and exit streaming mode so that we
92+
* don't have to handle any SME traps for valid guest
93+
* operations. Do this for ZA as well for now for simplicity.
94+
*/
95+
if (system_supports_sme()) {
96+
if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
97+
vcpu->arch.flags |= KVM_ARM64_HOST_SME_ENABLED;
98+
99+
if (read_sysreg_s(SYS_SVCR_EL0) &
100+
(SYS_SVCR_EL0_SM_MASK | SYS_SVCR_EL0_ZA_MASK)) {
101+
vcpu->arch.flags &= ~KVM_ARM64_FP_HOST;
102+
fpsimd_save_and_flush_cpu_state();
103+
}
104+
}
85105
}
86106

87107
/*
@@ -135,6 +155,22 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
135155

136156
local_irq_save(flags);
137157

158+
/*
159+
* If we have VHE then the Hyp code will reset CPACR_EL1 to
160+
* CPACR_EL1_DEFAULT and we need to reenable SME.
161+
*/
162+
if (has_vhe() && system_supports_sme()) {
163+
/* Also restore EL0 state seen on entry */
164+
if (vcpu->arch.flags & KVM_ARM64_HOST_SME_ENABLED)
165+
sysreg_clear_set(CPACR_EL1, 0,
166+
CPACR_EL1_SMEN_EL0EN |
167+
CPACR_EL1_SMEN_EL1EN);
168+
else
169+
sysreg_clear_set(CPACR_EL1,
170+
CPACR_EL1_SMEN_EL0EN,
171+
CPACR_EL1_SMEN_EL1EN);
172+
}
173+
138174
if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
139175
if (vcpu_has_sve(vcpu)) {
140176
__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);

0 commit comments

Comments
 (0)