Skip to content

Commit 0b6ac34

Browse files
Nadav Har'Elavikivity
authored andcommitted
KVM: nVMX: Correct handling of exception injection
Similar to the previous patch, but concerning injection of exceptions rather than external interrupts. Signed-off-by: Nadav Har'El <[email protected]> Signed-off-by: Marcelo Tosatti <[email protected]>
1 parent b6f1250 commit 0b6ac34

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

arch/x86/kvm/vmx.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,13 +1585,36 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
15851585
vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
15861586
}
15871587

1588+
/*
1589+
* KVM wants to inject page-faults which it got to the guest. This function
1590+
* checks whether in a nested guest, we need to inject them to L1 or L2.
1591+
* This function assumes it is called with the exit reason in vmcs02 being
1592+
* a #PF exception (this is the only case in which KVM injects a #PF when L2
1593+
* is running).
1594+
*/
1595+
static int nested_pf_handled(struct kvm_vcpu *vcpu)
1596+
{
1597+
struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1598+
1599+
/* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
1600+
if (!(vmcs12->exception_bitmap & PF_VECTOR))
1601+
return 0;
1602+
1603+
nested_vmx_vmexit(vcpu);
1604+
return 1;
1605+
}
1606+
15881607
static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
15891608
bool has_error_code, u32 error_code,
15901609
bool reinject)
15911610
{
15921611
struct vcpu_vmx *vmx = to_vmx(vcpu);
15931612
u32 intr_info = nr | INTR_INFO_VALID_MASK;
15941613

1614+
if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1615+
nested_pf_handled(vcpu))
1616+
return;
1617+
15951618
if (has_error_code) {
15961619
vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
15971620
intr_info |= INTR_INFO_DELIVER_CODE_MASK;
@@ -3820,6 +3843,9 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
38203843
{
38213844
struct vcpu_vmx *vmx = to_vmx(vcpu);
38223845

3846+
if (is_guest_mode(vcpu))
3847+
return;
3848+
38233849
if (!cpu_has_virtual_nmis()) {
38243850
/*
38253851
* Tracking the NMI-blocked state in software is built upon

0 commit comments

Comments
 (0)