Skip to content

Commit

Permalink
KVM: Reenter guest after emulation failure if due to access to non-mm…
Browse files Browse the repository at this point in the history
…io address

When shadow pages are in use sometimes KVM try to emulate an instruction
when it accesses a shadowed page. If emulation fails KVM un-shadows the
page and reenter guest to allow vcpu to execute the instruction. If page
is not in shadow page hash KVM assumes that this was attempt to do MMIO
and reports emulation failure to userspace since there is no way to fix
the situation. This logic has a race though. If two vcpus tries to write
to the same shadowed page simultaneously both will enter emulator, but
only one of them will find the page in shadow page hash since the one who
founds it also removes it from there, so another cpu will report failure
to userspace and will abort the guest.

Fix this by checking (in addition to checking shadowed page hash) that
page that caused the emulation belongs to valid memory slot. If it is
then reenter the guest to allow vcpu to reexecute the instruction.

Signed-off-by: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
  • Loading branch information
Gleb Natapov authored and Avi Kivity committed Aug 2, 2010
1 parent edba23e commit a6f177e
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions arch/x86/kvm/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -3930,6 +3930,29 @@ static int handle_emulation_failure(struct kvm_vcpu *vcpu)
return EMULATE_FAIL;
}

static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva)
{
gpa_t gpa;

/*
* if emulation was due to access to shadowed page table
* and it failed try to unshadow page and re-entetr the
* guest to let CPU execute the instruction.
*/
if (kvm_mmu_unprotect_page_virt(vcpu, gva))
return true;

gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL);

if (gpa == UNMAPPED_GVA)
return true; /* let cpu generate fault */

if (!kvm_is_error_hva(gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT)))
return true;

return false;
}

int emulate_instruction(struct kvm_vcpu *vcpu,
unsigned long cr2,
u16 error_code,
Expand Down Expand Up @@ -3998,7 +4021,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,

++vcpu->stat.insn_emulation;
if (r) {
if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
if (reexecute_instruction(vcpu, cr2))
return EMULATE_DONE;
if (emulation_type & EMULTYPE_SKIP)
return EMULATE_FAIL;
Expand All @@ -4019,12 +4042,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);

if (r) { /* emulation failed */
/*
* if emulation was due to access to shadowed page table
* and it failed try to unshadow page and re-entetr the
* guest to let CPU execute the instruction.
*/
if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
if (reexecute_instruction(vcpu, cr2))
return EMULATE_DONE;

return handle_emulation_failure(vcpu);
Expand Down

0 comments on commit a6f177e

Please sign in to comment.