Skip to content

Commit

Permalink
fixup! KVM: arm64: Expose TSO capability to guests and context switch
Browse files Browse the repository at this point in the history
  • Loading branch information
asahilina committed Sep 21, 2024
1 parent 9cb61a9 commit 4c2c5cc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
6 changes: 4 additions & 2 deletions arch/arm64/include/asm/kvm_emulate.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
{
if (!vcpu_has_run_once(vcpu))
vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
if (IS_ENABLED(CONFIG_ARM64_ACTLR_STATE) &&
alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE))
if (IS_ENABLED(CONFIG_ARM64_ACTLR_STATE) && (
alternative_has_cap_unlikely(ARM64_HAS_ACTLR_VIRT) ||
alternative_has_cap_unlikely(ARM64_HAS_ACTLR_VIRT_APPLE)
))
vcpu->arch.hcr_el2 &= ~HCR_TACR;

/*
Expand Down
26 changes: 26 additions & 0 deletions arch/arm64/kernel/cpufeature_impdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ static bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope)
}
#endif

static bool has_apple_actlr_virt_impdef(const struct arm64_cpu_capabilities *entry, int scope)
{
u64 midr = read_cpuid_id() & MIDR_CPU_MODEL_MASK;

return midr >= MIDR_APPLE_M1_ICESTORM && midr <= MIDR_APPLE_M1_FIRESTORM_MAX;
}

static bool has_apple_actlr_virt(const struct arm64_cpu_capabilities *entry, int scope)
{
u64 midr = read_cpuid_id() & MIDR_CPU_MODEL_MASK;

return midr >= MIDR_APPLE_M2_BLIZZARD && midr <= MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, 0xfff);
}

static const struct arm64_cpu_capabilities arm64_impdef_features[] = {
#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL
{
Expand All @@ -82,6 +96,18 @@ static const struct arm64_cpu_capabilities arm64_impdef_features[] = {
.matches = has_tso_fixed,
},
#endif
{
.desc = "ACTLR virtualization (IMPDEF, Apple)",
.capability = ARM64_HAS_ACTLR_VIRT_APPLE,
.type = SCOPE_LOCAL_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU,
.matches = has_apple_actlr_virt_impdef,
},
{
.desc = "ACTLR virtualization (architectural?)",
.capability = ARM64_HAS_ACTLR_VIRT,
.type = SCOPE_LOCAL_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU,
.matches = has_apple_actlr_virt,
},
{},
};

Expand Down
19 changes: 13 additions & 6 deletions arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <asm/kvm_mmu.h>

#define SYS_IMP_APL_ACTLR_EL12 sys_reg(3, 6, 15, 14, 6)
#define SYS_ACTLR_EL12 sys_reg(3, 5, 1, 0, 1)

static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
{
Expand Down Expand Up @@ -103,9 +104,12 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
ctxt_sys_reg(ctxt, SP_EL1) = read_sysreg(sp_el1);
ctxt_sys_reg(ctxt, ELR_EL1) = read_sysreg_el1(SYS_ELR);
ctxt_sys_reg(ctxt, SPSR_EL1) = read_sysreg_el1(SYS_SPSR);
if (IS_ENABLED(CONFIG_ARM64_ACTLR_STATE) &&
alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE))
ctxt_sys_reg(ctxt, ACTLR_EL1) = read_sysreg_s(SYS_IMP_APL_ACTLR_EL12);
if (IS_ENABLED(CONFIG_ARM64_ACTLR_STATE)) {
if (alternative_has_cap_unlikely(ARM64_HAS_ACTLR_VIRT))
ctxt_sys_reg(ctxt, ACTLR_EL1) = read_sysreg_s(SYS_ACTLR_EL12);
else if (alternative_has_cap_unlikely(ARM64_HAS_ACTLR_VIRT_APPLE))
ctxt_sys_reg(ctxt, ACTLR_EL1) = read_sysreg_s(SYS_IMP_APL_ACTLR_EL12);
}
}

static inline void __sysreg_save_el2_return_state(struct kvm_cpu_context *ctxt)
Expand Down Expand Up @@ -176,9 +180,12 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
write_sysreg(ctxt_sys_reg(ctxt, PAR_EL1), par_el1);
write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL1), tpidr_el1);

if (IS_ENABLED(CONFIG_ARM64_ACTLR_STATE) &&
alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE))
write_sysreg_s(ctxt_sys_reg(ctxt, ACTLR_EL1), SYS_IMP_APL_ACTLR_EL12);
if (IS_ENABLED(CONFIG_ARM64_ACTLR_STATE)) {
if (alternative_has_cap_unlikely(ARM64_HAS_ACTLR_VIRT))
write_sysreg_s(ctxt_sys_reg(ctxt, ACTLR_EL1), SYS_ACTLR_EL12);
else if (alternative_has_cap_unlikely(ARM64_HAS_ACTLR_VIRT_APPLE))
write_sysreg_s(ctxt_sys_reg(ctxt, ACTLR_EL1), SYS_IMP_APL_ACTLR_EL12);
}

if (ctxt_has_mte(ctxt)) {
write_sysreg_el1(ctxt_sys_reg(ctxt, TFSR_EL1), SYS_TFSR);
Expand Down
2 changes: 2 additions & 0 deletions arch/arm64/tools/cpucaps
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ BTI
# Unreliable: use system_supports_32bit_el0() instead.
HAS_32BIT_EL0_DO_NOT_USE
HAS_32BIT_EL1
HAS_ACTLR_VIRT
HAS_ACTLR_VIRT_APPLE
HAS_ADDRESS_AUTH
HAS_ADDRESS_AUTH_ARCH_QARMA3
HAS_ADDRESS_AUTH_ARCH_QARMA5
Expand Down

0 comments on commit 4c2c5cc

Please sign in to comment.