KVM/arm64 fixes for 6.5, part #2

- Fixes for the configuration of SVE/SME traps when hVHE mode is in use
 
  - Allow use of pKVM on systems with FF-A implementations that are v1.0
    compatible
 
  - Request/release percpu IRQs (arch timer, vGIC maintenance) correctly
    when pKVM is in use
 
  - Fix function prototype after __kvm_host_psci_cpu_entry() rename
 
  - Skip to the next instruction when emulating writes to TCR_EL1 on
    AmpereOne systems
 -----BEGIN PGP SIGNATURE-----
 
 iHUEABYIAB0WIQSNXHjWXuzMZutrKNKivnWIJHzdFgUCZMi85QAKCRCivnWIJHzd
 FvmQAP9Mk2hAW/42Z6oZw70xnJMzaLh+h2bx0t91iTvSXBap0gD/dMUAz+BpaGvq
 JppNoBtceA2eJJaDDiOpBHGpybwxtgI=
 =1fwI
 -----END PGP SIGNATURE-----

Merge tag 'kvmarm-fixes-6.5-2' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into HEAD

KVM/arm64 fixes for 6.5, part #2

 - Fixes for the configuration of SVE/SME traps when hVHE mode is in use

 - Allow use of pKVM on systems with FF-A implementations that are v1.0
   compatible

 - Request/release percpu IRQs (arch timer, vGIC maintenance) correctly
   when pKVM is in use

 - Fix function prototype after __kvm_host_psci_cpu_entry() rename

 - Skip to the next instruction when emulating writes to TCR_EL1 on
   AmpereOne systems
This commit is contained in:
Paolo Bonzini 2023-08-04 13:39:07 -04:00
commit 251199f4b3
7 changed files with 90 additions and 56 deletions

View file

@ -31,6 +31,13 @@
.Lskip_hcrx_\@: .Lskip_hcrx_\@:
.endm .endm
/* Check if running in host at EL2 mode, i.e., (h)VHE. Jump to fail if not. */
.macro __check_hvhe fail, tmp
mrs \tmp, hcr_el2
and \tmp, \tmp, #HCR_E2H
cbz \tmp, \fail
.endm
/* /*
* Allow Non-secure EL1 and EL0 to access physical timer and counter. * Allow Non-secure EL1 and EL0 to access physical timer and counter.
* This is not necessary for VHE, since the host kernel runs in EL2, * This is not necessary for VHE, since the host kernel runs in EL2,
@ -43,9 +50,7 @@
*/ */
.macro __init_el2_timers .macro __init_el2_timers
mov x0, #3 // Enable EL1 physical timers mov x0, #3 // Enable EL1 physical timers
mrs x1, hcr_el2 __check_hvhe .LnVHE_\@, x1
and x1, x1, #HCR_E2H
cbz x1, .LnVHE_\@
lsl x0, x0, #10 lsl x0, x0, #10
.LnVHE_\@: .LnVHE_\@:
msr cnthctl_el2, x0 msr cnthctl_el2, x0
@ -139,15 +144,14 @@
/* Coprocessor traps */ /* Coprocessor traps */
.macro __init_el2_cptr .macro __init_el2_cptr
mrs x1, hcr_el2 __check_hvhe .LnVHE_\@, x1
and x1, x1, #HCR_E2H
cbz x1, .LnVHE_\@
mov x0, #(CPACR_EL1_FPEN_EL1EN | CPACR_EL1_FPEN_EL0EN) mov x0, #(CPACR_EL1_FPEN_EL1EN | CPACR_EL1_FPEN_EL0EN)
b .Lset_cptr_\@ msr cpacr_el1, x0
b .Lskip_set_cptr_\@
.LnVHE_\@: .LnVHE_\@:
mov x0, #0x33ff mov x0, #0x33ff
.Lset_cptr_\@:
msr cptr_el2, x0 // Disable copro. traps to EL2 msr cptr_el2, x0 // Disable copro. traps to EL2
.Lskip_set_cptr_\@:
.endm .endm
/* Disable any fine grained traps */ /* Disable any fine grained traps */
@ -268,19 +272,19 @@
check_override id_aa64pfr0, ID_AA64PFR0_EL1_SVE_SHIFT, .Linit_sve_\@, .Lskip_sve_\@, x1, x2 check_override id_aa64pfr0, ID_AA64PFR0_EL1_SVE_SHIFT, .Linit_sve_\@, .Lskip_sve_\@, x1, x2
.Linit_sve_\@: /* SVE register access */ .Linit_sve_\@: /* SVE register access */
mrs x0, cptr_el2 // Disable SVE traps __check_hvhe .Lcptr_nvhe_\@, x1
mrs x1, hcr_el2
and x1, x1, #HCR_E2H
cbz x1, .Lcptr_nvhe_\@
// VHE case // (h)VHE case
mrs x0, cpacr_el1 // Disable SVE traps
orr x0, x0, #(CPACR_EL1_ZEN_EL1EN | CPACR_EL1_ZEN_EL0EN) orr x0, x0, #(CPACR_EL1_ZEN_EL1EN | CPACR_EL1_ZEN_EL0EN)
b .Lset_cptr_\@ msr cpacr_el1, x0
b .Lskip_set_cptr_\@
.Lcptr_nvhe_\@: // nVHE case .Lcptr_nvhe_\@: // nVHE case
mrs x0, cptr_el2 // Disable SVE traps
bic x0, x0, #CPTR_EL2_TZ bic x0, x0, #CPTR_EL2_TZ
.Lset_cptr_\@:
msr cptr_el2, x0 msr cptr_el2, x0
.Lskip_set_cptr_\@:
isb isb
mov x1, #ZCR_ELx_LEN_MASK // SVE: Enable full vector mov x1, #ZCR_ELx_LEN_MASK // SVE: Enable full vector
msr_s SYS_ZCR_EL2, x1 // length for EL1. msr_s SYS_ZCR_EL2, x1 // length for EL1.
@ -289,9 +293,19 @@
check_override id_aa64pfr1, ID_AA64PFR1_EL1_SME_SHIFT, .Linit_sme_\@, .Lskip_sme_\@, x1, x2 check_override id_aa64pfr1, ID_AA64PFR1_EL1_SME_SHIFT, .Linit_sme_\@, .Lskip_sme_\@, x1, x2
.Linit_sme_\@: /* SME register access and priority mapping */ .Linit_sme_\@: /* SME register access and priority mapping */
__check_hvhe .Lcptr_nvhe_sme_\@, x1
// (h)VHE case
mrs x0, cpacr_el1 // Disable SME traps
orr x0, x0, #(CPACR_EL1_SMEN_EL0EN | CPACR_EL1_SMEN_EL1EN)
msr cpacr_el1, x0
b .Lskip_set_cptr_sme_\@
.Lcptr_nvhe_sme_\@: // nVHE case
mrs x0, cptr_el2 // Disable SME traps mrs x0, cptr_el2 // Disable SME traps
bic x0, x0, #CPTR_EL2_TSM bic x0, x0, #CPTR_EL2_TSM
msr cptr_el2, x0 msr cptr_el2, x0
.Lskip_set_cptr_sme_\@:
isb isb
mrs x1, sctlr_el2 mrs x1, sctlr_el2

View file

@ -278,7 +278,7 @@ asmlinkage void __noreturn hyp_panic_bad_stack(void);
asmlinkage void kvm_unexpected_el2_exception(void); asmlinkage void kvm_unexpected_el2_exception(void);
struct kvm_cpu_context; struct kvm_cpu_context;
void handle_trap(struct kvm_cpu_context *host_ctxt); void handle_trap(struct kvm_cpu_context *host_ctxt);
asmlinkage void __noreturn kvm_host_psci_cpu_entry(bool is_cpu_on); asmlinkage void __noreturn __kvm_host_psci_cpu_entry(bool is_cpu_on);
void __noreturn __pkvm_init_finalise(void); void __noreturn __pkvm_init_finalise(void);
void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc); void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc);
void kvm_patch_vector_branch(struct alt_instr *alt, void kvm_patch_vector_branch(struct alt_instr *alt,

View file

@ -571,6 +571,14 @@ static inline bool vcpu_has_feature(struct kvm_vcpu *vcpu, int feature)
return test_bit(feature, vcpu->arch.features); return test_bit(feature, vcpu->arch.features);
} }
static __always_inline void kvm_write_cptr_el2(u64 val)
{
if (has_vhe() || has_hvhe())
write_sysreg(val, cpacr_el1);
else
write_sysreg(val, cptr_el2);
}
static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu) static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
{ {
u64 val; u64 val;
@ -578,8 +586,16 @@ static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
if (has_vhe()) { if (has_vhe()) {
val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN | val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN |
CPACR_EL1_ZEN_EL1EN); CPACR_EL1_ZEN_EL1EN);
if (cpus_have_final_cap(ARM64_SME))
val |= CPACR_EL1_SMEN_EL1EN;
} else if (has_hvhe()) { } else if (has_hvhe()) {
val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN); val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN);
if (!vcpu_has_sve(vcpu) ||
(vcpu->arch.fp_state != FP_STATE_GUEST_OWNED))
val |= CPACR_EL1_ZEN_EL1EN | CPACR_EL1_ZEN_EL0EN;
if (cpus_have_final_cap(ARM64_SME))
val |= CPACR_EL1_SMEN_EL1EN | CPACR_EL1_SMEN_EL0EN;
} else { } else {
val = CPTR_NVHE_EL2_RES1; val = CPTR_NVHE_EL2_RES1;
@ -597,9 +613,6 @@ static __always_inline void kvm_reset_cptr_el2(struct kvm_vcpu *vcpu)
{ {
u64 val = kvm_get_reset_cptr_el2(vcpu); u64 val = kvm_get_reset_cptr_el2(vcpu);
if (has_vhe() || has_hvhe()) kvm_write_cptr_el2(val);
write_sysreg(val, cpacr_el1);
else
write_sysreg(val, cptr_el2);
} }
#endif /* __ARM64_KVM_EMULATE_H__ */ #endif /* __ARM64_KVM_EMULATE_H__ */

View file

@ -55,7 +55,7 @@ DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
static bool vgic_present, kvm_arm_initialised; static bool vgic_present, kvm_arm_initialised;
static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled); static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use); DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
bool is_kvm_arm_initialised(void) bool is_kvm_arm_initialised(void)
@ -1864,18 +1864,24 @@ static void cpu_hyp_reinit(void)
cpu_hyp_init_features(); cpu_hyp_init_features();
} }
static void _kvm_arch_hardware_enable(void *discard) static void cpu_hyp_init(void *discard)
{ {
if (!__this_cpu_read(kvm_arm_hardware_enabled)) { if (!__this_cpu_read(kvm_hyp_initialized)) {
cpu_hyp_reinit(); cpu_hyp_reinit();
__this_cpu_write(kvm_arm_hardware_enabled, 1); __this_cpu_write(kvm_hyp_initialized, 1);
}
}
static void cpu_hyp_uninit(void *discard)
{
if (__this_cpu_read(kvm_hyp_initialized)) {
cpu_hyp_reset();
__this_cpu_write(kvm_hyp_initialized, 0);
} }
} }
int kvm_arch_hardware_enable(void) int kvm_arch_hardware_enable(void)
{ {
int was_enabled;
/* /*
* Most calls to this function are made with migration * Most calls to this function are made with migration
* disabled, but not with preemption disabled. The former is * disabled, but not with preemption disabled. The former is
@ -1884,36 +1890,23 @@ int kvm_arch_hardware_enable(void)
*/ */
preempt_disable(); preempt_disable();
was_enabled = __this_cpu_read(kvm_arm_hardware_enabled); cpu_hyp_init(NULL);
_kvm_arch_hardware_enable(NULL);
if (!was_enabled) { kvm_vgic_cpu_up();
kvm_vgic_cpu_up(); kvm_timer_cpu_up();
kvm_timer_cpu_up();
}
preempt_enable(); preempt_enable();
return 0; return 0;
} }
static void _kvm_arch_hardware_disable(void *discard)
{
if (__this_cpu_read(kvm_arm_hardware_enabled)) {
cpu_hyp_reset();
__this_cpu_write(kvm_arm_hardware_enabled, 0);
}
}
void kvm_arch_hardware_disable(void) void kvm_arch_hardware_disable(void)
{ {
if (__this_cpu_read(kvm_arm_hardware_enabled)) { kvm_timer_cpu_down();
kvm_timer_cpu_down(); kvm_vgic_cpu_down();
kvm_vgic_cpu_down();
}
if (!is_protected_kvm_enabled()) if (!is_protected_kvm_enabled())
_kvm_arch_hardware_disable(NULL); cpu_hyp_uninit(NULL);
} }
#ifdef CONFIG_CPU_PM #ifdef CONFIG_CPU_PM
@ -1922,16 +1915,16 @@ static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
void *v) void *v)
{ {
/* /*
* kvm_arm_hardware_enabled is left with its old value over * kvm_hyp_initialized is left with its old value over
* PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
* re-enable hyp. * re-enable hyp.
*/ */
switch (cmd) { switch (cmd) {
case CPU_PM_ENTER: case CPU_PM_ENTER:
if (__this_cpu_read(kvm_arm_hardware_enabled)) if (__this_cpu_read(kvm_hyp_initialized))
/* /*
* don't update kvm_arm_hardware_enabled here * don't update kvm_hyp_initialized here
* so that the hardware will be re-enabled * so that the hyp will be re-enabled
* when we resume. See below. * when we resume. See below.
*/ */
cpu_hyp_reset(); cpu_hyp_reset();
@ -1939,8 +1932,8 @@ static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
return NOTIFY_OK; return NOTIFY_OK;
case CPU_PM_ENTER_FAILED: case CPU_PM_ENTER_FAILED:
case CPU_PM_EXIT: case CPU_PM_EXIT:
if (__this_cpu_read(kvm_arm_hardware_enabled)) if (__this_cpu_read(kvm_hyp_initialized))
/* The hardware was enabled before suspend. */ /* The hyp was enabled before suspend. */
cpu_hyp_reinit(); cpu_hyp_reinit();
return NOTIFY_OK; return NOTIFY_OK;
@ -2021,7 +2014,7 @@ static int __init init_subsystems(void)
/* /*
* Enable hardware so that subsystem initialisation can access EL2. * Enable hardware so that subsystem initialisation can access EL2.
*/ */
on_each_cpu(_kvm_arch_hardware_enable, NULL, 1); on_each_cpu(cpu_hyp_init, NULL, 1);
/* /*
* Register CPU lower-power notifier * Register CPU lower-power notifier
@ -2059,7 +2052,7 @@ static int __init init_subsystems(void)
hyp_cpu_pm_exit(); hyp_cpu_pm_exit();
if (err || !is_protected_kvm_enabled()) if (err || !is_protected_kvm_enabled())
on_each_cpu(_kvm_arch_hardware_disable, NULL, 1); on_each_cpu(cpu_hyp_uninit, NULL, 1);
return err; return err;
} }
@ -2097,7 +2090,7 @@ static int __init do_pkvm_init(u32 hyp_va_bits)
* The stub hypercalls are now disabled, so set our local flag to * The stub hypercalls are now disabled, so set our local flag to
* prevent a later re-init attempt in kvm_arch_hardware_enable(). * prevent a later re-init attempt in kvm_arch_hardware_enable().
*/ */
__this_cpu_write(kvm_arm_hardware_enabled, 1); __this_cpu_write(kvm_hyp_initialized, 1);
preempt_enable(); preempt_enable();
return ret; return ret;

View file

@ -457,6 +457,7 @@ static bool handle_ampere1_tcr(struct kvm_vcpu *vcpu)
*/ */
val &= ~(TCR_HD | TCR_HA); val &= ~(TCR_HD | TCR_HA);
write_sysreg_el1(val, SYS_TCR); write_sysreg_el1(val, SYS_TCR);
__kvm_skip_instr(vcpu);
return true; return true;
} }

View file

@ -705,7 +705,20 @@ int hyp_ffa_init(void *pages)
if (res.a0 == FFA_RET_NOT_SUPPORTED) if (res.a0 == FFA_RET_NOT_SUPPORTED)
return 0; return 0;
if (res.a0 != FFA_VERSION_1_0) /*
* Firmware returns the maximum supported version of the FF-A
* implementation. Check that the returned version is
* backwards-compatible with the hyp according to the rules in DEN0077A
* v1.1 REL0 13.2.1.
*
* Of course, things are never simple when dealing with firmware. v1.1
* broke ABI with v1.0 on several structures, which is itself
* incompatible with the aforementioned versioning scheme. The
* expectation is that v1.x implementations that do not support the v1.0
* ABI return NOT_SUPPORTED rather than a version number, according to
* DEN0077A v1.1 REL0 18.6.4.
*/
if (FFA_MAJOR_VERSION(res.a0) != 1)
return -EOPNOTSUPP; return -EOPNOTSUPP;
arm_smccc_1_1_smc(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0, &res); arm_smccc_1_1_smc(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0, &res);

View file

@ -63,7 +63,7 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
__activate_traps_fpsimd32(vcpu); __activate_traps_fpsimd32(vcpu);
} }
write_sysreg(val, cptr_el2); kvm_write_cptr_el2(val);
write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2); write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) { if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {