From b5904199644e204b3ec61db1c0a2ed61d843657b Mon Sep 17 00:00:00 2001 From: Fuad Tabba Date: Thu, 6 Feb 2025 14:04:04 +0000 Subject: [PATCH] ANDROID: KVM: arm64: Calculate traps for all vcpus at hyp vm creation In non-protected mode, vcpu traps are calculated for each vcpu the first time it's run. Protected mode creates the hyp view of the vm and all its vcpus when the first vcpu is run. At that point in time, the remaining vcpus might not have calculated their trap values. To fix this problem, calculate vcpu traps for _all_ vcpus before creating and initializing the hyp view. Bug: 394097954 Bug: 357781595 Signed-off-by: Fuad Tabba Change-Id: I9fdccd035aa2d95775704b8e3dcbd6f549ae3c4c --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 3 ++- arch/arm64/kvm/pkvm.c | 1 + arch/arm64/kvm/sys_regs.c | 14 ++++++++++---- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index aaaf01fcb284..e674da0aaa0e 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1347,6 +1347,7 @@ int __init populate_nv_trap_config(void); bool lock_all_vcpus(struct kvm *kvm); void unlock_all_vcpus(struct kvm *kvm); +void __kvm_calculate_traps(struct kvm_vcpu *vcpu); void kvm_calculate_traps(struct kvm_vcpu *vcpu); /* MMIO helpers */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 4e29800c6b33..df325174bee7 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -857,7 +857,8 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) * This needs to happen after any restriction has been applied * to the feature set. */ - kvm_calculate_traps(vcpu); + if (!is_protected_kvm_enabled()) + kvm_calculate_traps(vcpu); ret = kvm_timer_enable(vcpu); if (ret) diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c index a90482c4cb85..5cddb85fe0a8 100644 --- a/arch/arm64/kvm/pkvm.c +++ b/arch/arm64/kvm/pkvm.c @@ -449,6 +449,7 @@ static int __pkvm_create_hyp_vm(struct kvm *host_kvm) /* Donate memory for the vcpus at hyp and initialize it. */ kvm_for_each_vcpu(idx, host_vcpu, host_kvm) { + __kvm_calculate_traps(host_vcpu); ret = __pkvm_create_hyp_vcpu(host_kvm, host_vcpu, idx); if (ret) goto destroy_vm; diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 786c564a0c33..1009cbebb356 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -4615,11 +4615,10 @@ static void vcpu_set_hcr(struct kvm_vcpu *vcpu) vcpu->arch.hcr_el2 |= HCR_TTLBOS; } -void kvm_calculate_traps(struct kvm_vcpu *vcpu) +void __kvm_calculate_traps(struct kvm_vcpu *vcpu) { struct kvm *kvm = vcpu->kvm; - mutex_lock(&kvm->arch.config_lock); vcpu_set_hcr(vcpu); vcpu_set_ich_hcr(vcpu); @@ -4643,7 +4642,7 @@ void kvm_calculate_traps(struct kvm_vcpu *vcpu) } if (test_bit(KVM_ARCH_FLAG_FGU_INITIALIZED, &kvm->arch.flags)) - goto out; + return; kvm->arch.fgu[HFGxTR_GROUP] = (HFGxTR_EL2_nAMAIR2_EL1 | HFGxTR_EL2_nMAIR2_EL1 | @@ -4698,7 +4697,14 @@ void kvm_calculate_traps(struct kvm_vcpu *vcpu) HAFGRTR_EL2_RES1); set_bit(KVM_ARCH_FLAG_FGU_INITIALIZED, &kvm->arch.flags); -out: +} + +void kvm_calculate_traps(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + + mutex_lock(&kvm->arch.config_lock); + __kvm_calculate_traps(vcpu); mutex_unlock(&kvm->arch.config_lock); }