Skip to content

Commit 8586206

Browse files
Fuad TabbaSasha Levin
authored andcommitted
KVM: arm64: Fix ID register initialization for non-protected pKVM guests
[ Upstream commit 7e7c2cf ] In protected mode, the hypervisor maintains a separate instance of the `kvm` structure for each VM. For non-protected VMs, this structure is initialized from the host's `kvm` state. Currently, `pkvm_init_features_from_host()` copies the `KVM_ARCH_FLAG_ID_REGS_INITIALIZED` flag from the host without the underlying `id_regs` data being initialized. This results in the hypervisor seeing the flag as set while the ID registers remain zeroed. Consequently, `kvm_has_feat()` checks at EL2 fail (return 0) for non-protected VMs. This breaks logic that relies on feature detection, such as `ctxt_has_tcrx()` for TCR2_EL1 support. As a result, certain system registers (e.g., TCR2_EL1, PIR_EL1, POR_EL1) are not saved/restored during the world switch, which could lead to state corruption. Fix this by explicitly copying the ID registers from the host `kvm` to the hypervisor `kvm` for non-protected VMs during initialization, since we trust the host with its non-protected guests' features. Also ensure `KVM_ARCH_FLAG_ID_REGS_INITIALIZED` is cleared initially in `pkvm_init_features_from_host` so that `vm_copy_id_regs` can properly initialize them and set the flag once done. Fixes: 41d6028 ("KVM: arm64: Convert the SVE guest vcpu flag to a vm flag") Signed-off-by: Fuad Tabba <tabba@google.com> Link: https://patch.msgid.link/20260213143815.1732675-4-tabba@google.com Signed-off-by: Marc Zyngier <maz@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 77ede05 commit 8586206

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const struc
345345
/* No restrictions for non-protected VMs. */
346346
if (!kvm_vm_is_protected(kvm)) {
347347
hyp_vm->kvm.arch.flags = host_arch_flags;
348+
hyp_vm->kvm.arch.flags &= ~BIT_ULL(KVM_ARCH_FLAG_ID_REGS_INITIALIZED);
348349

349350
bitmap_copy(kvm->arch.vcpu_features,
350351
host_kvm->arch.vcpu_features,
@@ -471,6 +472,35 @@ static int pkvm_vcpu_init_sve(struct pkvm_hyp_vcpu *hyp_vcpu, struct kvm_vcpu *h
471472
return ret;
472473
}
473474

475+
static int vm_copy_id_regs(struct pkvm_hyp_vcpu *hyp_vcpu)
476+
{
477+
struct pkvm_hyp_vm *hyp_vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu);
478+
const struct kvm *host_kvm = hyp_vm->host_kvm;
479+
struct kvm *kvm = &hyp_vm->kvm;
480+
481+
if (!test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &host_kvm->arch.flags))
482+
return -EINVAL;
483+
484+
if (test_and_set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
485+
return 0;
486+
487+
memcpy(kvm->arch.id_regs, host_kvm->arch.id_regs, sizeof(kvm->arch.id_regs));
488+
489+
return 0;
490+
}
491+
492+
static int pkvm_vcpu_init_sysregs(struct pkvm_hyp_vcpu *hyp_vcpu)
493+
{
494+
int ret = 0;
495+
496+
if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
497+
kvm_init_pvm_id_regs(&hyp_vcpu->vcpu);
498+
else
499+
ret = vm_copy_id_regs(hyp_vcpu);
500+
501+
return ret;
502+
}
503+
474504
static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
475505
struct pkvm_hyp_vm *hyp_vm,
476506
struct kvm_vcpu *host_vcpu)
@@ -490,8 +520,9 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
490520
hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
491521
hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
492522

493-
if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
494-
kvm_init_pvm_id_regs(&hyp_vcpu->vcpu);
523+
ret = pkvm_vcpu_init_sysregs(hyp_vcpu);
524+
if (ret)
525+
goto done;
495526

496527
ret = pkvm_vcpu_init_traps(hyp_vcpu);
497528
if (ret)

0 commit comments

Comments
 (0)