Skip to content

Commit adfe20f

Browse files
Wanpeng Lirkrcmar
authored andcommitted
KVM: async_pf: Force a nested vmexit if the injected #PF is async_pf
Add an nested_apf field to vcpu->arch.exception to identify an async page fault, and constructs the expected vm-exit information fields. Force a nested VM exit from nested_vmx_check_exception() if the injected #PF is async page fault. Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Radim Krčmář <rkrcmar@redhat.com> Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
1 parent 1261bfa commit adfe20f

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

arch/x86/include/asm/kvm_emulate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct x86_exception {
2323
u16 error_code;
2424
bool nested_page_fault;
2525
u64 address; /* cr2 or nested page fault gpa */
26+
u8 async_page_fault;
2627
};
2728

2829
/*

arch/x86/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ struct kvm_vcpu_arch {
550550
bool reinject;
551551
u8 nr;
552552
u32 error_code;
553+
u8 nested_apf;
553554
} exception;
554555

555556
struct kvm_queued_interrupt {
@@ -651,6 +652,7 @@ struct kvm_vcpu_arch {
651652
u32 id;
652653
bool send_user_only;
653654
u32 host_apf_reason;
655+
unsigned long nested_apf_token;
654656
} apf;
655657

656658
/* OSVW MSRs (AMD only) */

arch/x86/kvm/svm.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,15 +2423,19 @@ static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
24232423
if (!is_guest_mode(&svm->vcpu))
24242424
return 0;
24252425

2426+
vmexit = nested_svm_intercept(svm);
2427+
if (vmexit != NESTED_EXIT_DONE)
2428+
return 0;
2429+
24262430
svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
24272431
svm->vmcb->control.exit_code_hi = 0;
24282432
svm->vmcb->control.exit_info_1 = error_code;
2429-
svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
2430-
2431-
vmexit = nested_svm_intercept(svm);
2432-
if (vmexit == NESTED_EXIT_DONE)
2433-
svm->nested.exit_required = true;
2433+
if (svm->vcpu.arch.exception.nested_apf)
2434+
svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
2435+
else
2436+
svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
24342437

2438+
svm->nested.exit_required = true;
24352439
return vmexit;
24362440
}
24372441

@@ -2653,7 +2657,7 @@ static int nested_svm_intercept(struct vcpu_svm *svm)
26532657
}
26542658
/* async page fault always cause vmexit */
26552659
else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
2656-
svm->vcpu.arch.apf.host_apf_reason != 0)
2660+
svm->vcpu.arch.exception.nested_apf != 0)
26572661
vmexit = NESTED_EXIT_DONE;
26582662
break;
26592663
}

arch/x86/kvm/vmx.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,13 +2422,24 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
24222422
* KVM wants to inject page-faults which it got to the guest. This function
24232423
* checks whether in a nested guest, we need to inject them to L1 or L2.
24242424
*/
2425-
static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
2425+
static int nested_vmx_check_exception(struct kvm_vcpu *vcpu)
24262426
{
24272427
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2428+
unsigned int nr = vcpu->arch.exception.nr;
24282429

2429-
if (!(vmcs12->exception_bitmap & (1u << nr)))
2430+
if (!((vmcs12->exception_bitmap & (1u << nr)) ||
2431+
(nr == PF_VECTOR && vcpu->arch.exception.nested_apf)))
24302432
return 0;
24312433

2434+
if (vcpu->arch.exception.nested_apf) {
2435+
vmcs_write32(VM_EXIT_INTR_ERROR_CODE, vcpu->arch.exception.error_code);
2436+
nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
2437+
PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
2438+
INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
2439+
vcpu->arch.apf.nested_apf_token);
2440+
return 1;
2441+
}
2442+
24322443
nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
24332444
vmcs_read32(VM_EXIT_INTR_INFO),
24342445
vmcs_readl(EXIT_QUALIFICATION));
@@ -2445,7 +2456,7 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu)
24452456
u32 intr_info = nr | INTR_INFO_VALID_MASK;
24462457

24472458
if (!reinject && is_guest_mode(vcpu) &&
2448-
nested_vmx_check_exception(vcpu, nr))
2459+
nested_vmx_check_exception(vcpu))
24492460
return;
24502461

24512462
if (has_error_code) {

arch/x86/kvm/x86.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,12 @@ EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
450450
void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
451451
{
452452
++vcpu->stat.pf_guest;
453-
vcpu->arch.cr2 = fault->address;
453+
vcpu->arch.exception.nested_apf =
454+
is_guest_mode(vcpu) && fault->async_page_fault;
455+
if (vcpu->arch.exception.nested_apf)
456+
vcpu->arch.apf.nested_apf_token = fault->address;
457+
else
458+
vcpu->arch.cr2 = fault->address;
454459
kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
455460
}
456461
EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
@@ -8582,6 +8587,7 @@ void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
85828587
fault.error_code = 0;
85838588
fault.nested_page_fault = false;
85848589
fault.address = work->arch.token;
8590+
fault.async_page_fault = true;
85858591
kvm_inject_page_fault(vcpu, &fault);
85868592
}
85878593
}
@@ -8604,6 +8610,7 @@ void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
86048610
fault.error_code = 0;
86058611
fault.nested_page_fault = false;
86068612
fault.address = work->arch.token;
8613+
fault.async_page_fault = true;
86078614
kvm_inject_page_fault(vcpu, &fault);
86088615
}
86098616
vcpu->arch.apf.halted = false;

0 commit comments

Comments
 (0)