Skip to content

Commit a506fdd

Browse files
vittyvkbonzini
authored andcommitted
KVM: nSVM: implement nested_svm_load_cr3() and use it for host->guest switch
Undesired triple fault gets injected to L1 guest on SVM when L2 is launched with certain CR3 values. #TF is raised by mmu_check_root() check in fast_pgd_switch() and the root cause is that when kvm_set_cr3() is called from nested_prepare_vmcb_save() with NPT enabled CR3 points to a nGPA so we can't check it with kvm_is_visible_gfn(). Using generic kvm_set_cr3() when switching to nested guest is not a great idea as we'll have to distinguish between 'real' CR3s and 'nested' CR3s to e.g. not call kvm_mmu_new_pgd() with nGPA. Following nVMX implement nested-specific nested_svm_load_cr3() doing the job. To support the change, nested_svm_load_cr3() needs to be re-ordered with nested_svm_init_mmu_context(). Note: the current implementation is sub-optimal as we always do TLB flush/MMU sync but this is still an improvement as we at least stop doing kvm_mmu_reset_context(). Fixes: 7c390d3 ("kvm: x86: Add fast CR3 switch code path") Signed-off-by: Vitaly Kuznetsov <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent bf7dea4 commit a506fdd

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,6 +4952,8 @@ void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, u32 cr0, u32 cr4, u32 efer,
49524952
union kvm_mmu_role new_role =
49534953
kvm_calc_shadow_mmu_root_page_role(vcpu, false);
49544954

4955+
__kvm_mmu_new_pgd(vcpu, nested_cr3, new_role.base, false, false);
4956+
49554957
if (new_role.as_u64 != context->mmu_role.as_u64)
49564958
shadow_mmu_init_context(vcpu, context, cr0, cr4, efer, new_role);
49574959
}

arch/x86/kvm/svm/nested.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,28 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)
348348
static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
349349
bool nested_npt)
350350
{
351-
return kvm_set_cr3(vcpu, cr3);
351+
if (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))
352+
return -EINVAL;
353+
354+
if (!nested_npt && is_pae_paging(vcpu) &&
355+
(cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
356+
if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
357+
return -EINVAL;
358+
}
359+
360+
/*
361+
* TODO: optimize unconditional TLB flush/MMU sync here and in
362+
* kvm_init_shadow_npt_mmu().
363+
*/
364+
if (!nested_npt)
365+
kvm_mmu_new_pgd(vcpu, cr3, false, false);
366+
367+
vcpu->arch.cr3 = cr3;
368+
kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
369+
370+
kvm_init_mmu(vcpu, false);
371+
372+
return 0;
352373
}
353374

354375
static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *nested_vmcb)
@@ -364,9 +385,6 @@ static void nested_prepare_vmcb_save(struct vcpu_svm *svm, struct vmcb *nested_v
364385
svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
365386
svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
366387
svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
367-
(void)nested_svm_load_cr3(&svm->vcpu, nested_vmcb->save.cr3,
368-
nested_npt_enabled(svm));
369-
370388
svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
371389
kvm_rax_write(&svm->vcpu, nested_vmcb->save.rax);
372390
kvm_rsp_write(&svm->vcpu, nested_vmcb->save.rsp);
@@ -388,11 +406,6 @@ static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
388406
if (nested_npt_enabled(svm))
389407
nested_svm_init_mmu_context(&svm->vcpu);
390408

391-
/* Guest paging mode is active - reset mmu */
392-
kvm_mmu_reset_context(&svm->vcpu);
393-
394-
svm_flush_tlb(&svm->vcpu);
395-
396409
svm->vmcb->control.tsc_offset = svm->vcpu.arch.tsc_offset =
397410
svm->vcpu.arch.l1_tsc_offset + svm->nested.ctl.tsc_offset;
398411

@@ -424,11 +437,18 @@ static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
424437
int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
425438
struct vmcb *nested_vmcb)
426439
{
440+
int ret;
441+
427442
svm->nested.vmcb = vmcb_gpa;
428443
load_nested_vmcb_control(svm, &nested_vmcb->control);
429444
nested_prepare_vmcb_save(svm, nested_vmcb);
430445
nested_prepare_vmcb_control(svm);
431446

447+
ret = nested_svm_load_cr3(&svm->vcpu, nested_vmcb->save.cr3,
448+
nested_npt_enabled(svm));
449+
if (ret)
450+
return ret;
451+
432452
svm_set_gif(svm, true);
433453

434454
return 0;

0 commit comments

Comments
 (0)