diff options
author | Gleb Natapov <gleb@redhat.com> | 2010-05-10 04:16:56 -0400 |
---|---|---|
committer | Avi Kivity <avi@redhat.com> | 2010-08-01 03:35:40 -0400 |
commit | 6d77dbfc88e37c9efd5c5dd18445cfe819ae17ea (patch) | |
tree | 96a3afb750d254a99dd43fa4730fd6ff187eb2a0 /arch/x86/kvm/x86.c | |
parent | 57bc24cfd655c912498983130326b312e0404db1 (diff) |
KVM: inject #UD if instruction emulation fails and exit to userspace
Do not kill VM when instruction emulation fails. Inject #UD and report
failure to userspace instead. Userspace may choose to reenter guest if
vcpu is in userspace (cpl == 3) in which case guest OS will kill
offending process and continue running.
Signed-off-by: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Diffstat (limited to 'arch/x86/kvm/x86.c')
-rw-r--r-- | arch/x86/kvm/x86.c | 43 |
1 files changed, 18 insertions, 25 deletions
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index fc5611b4007f..ae9d6f3e5d0d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c | |||
@@ -3639,24 +3639,6 @@ int emulator_set_dr(int dr, unsigned long value, struct kvm_vcpu *vcpu) | |||
3639 | return __kvm_set_dr(vcpu, dr, value); | 3639 | return __kvm_set_dr(vcpu, dr, value); |
3640 | } | 3640 | } |
3641 | 3641 | ||
3642 | void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context) | ||
3643 | { | ||
3644 | u8 opcodes[4]; | ||
3645 | unsigned long rip = kvm_rip_read(vcpu); | ||
3646 | unsigned long rip_linear; | ||
3647 | |||
3648 | if (!printk_ratelimit()) | ||
3649 | return; | ||
3650 | |||
3651 | rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS); | ||
3652 | |||
3653 | kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL); | ||
3654 | |||
3655 | printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n", | ||
3656 | context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]); | ||
3657 | } | ||
3658 | EXPORT_SYMBOL_GPL(kvm_report_emulation_failure); | ||
3659 | |||
3660 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) | 3642 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
3661 | { | 3643 | { |
3662 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; | 3644 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
@@ -3863,6 +3845,19 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu) | |||
3863 | kvm_queue_exception(vcpu, ctxt->exception); | 3845 | kvm_queue_exception(vcpu, ctxt->exception); |
3864 | } | 3846 | } |
3865 | 3847 | ||
3848 | static int handle_emulation_failure(struct kvm_vcpu *vcpu) | ||
3849 | { | ||
3850 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; | ||
3851 | |||
3852 | ++vcpu->stat.insn_emulation_fail; | ||
3853 | trace_kvm_emulate_insn_failed(vcpu); | ||
3854 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | ||
3855 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | ||
3856 | vcpu->run->internal.ndata = 0; | ||
3857 | kvm_queue_exception(vcpu, UD_VECTOR); | ||
3858 | return EMULATE_FAIL; | ||
3859 | } | ||
3860 | |||
3866 | int emulate_instruction(struct kvm_vcpu *vcpu, | 3861 | int emulate_instruction(struct kvm_vcpu *vcpu, |
3867 | unsigned long cr2, | 3862 | unsigned long cr2, |
3868 | u16 error_code, | 3863 | u16 error_code, |
@@ -3931,11 +3926,11 @@ int emulate_instruction(struct kvm_vcpu *vcpu, | |||
3931 | 3926 | ||
3932 | ++vcpu->stat.insn_emulation; | 3927 | ++vcpu->stat.insn_emulation; |
3933 | if (r) { | 3928 | if (r) { |
3934 | ++vcpu->stat.insn_emulation_fail; | ||
3935 | trace_kvm_emulate_insn_failed(vcpu); | ||
3936 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) | 3929 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
3937 | return EMULATE_DONE; | 3930 | return EMULATE_DONE; |
3938 | return EMULATE_FAIL; | 3931 | if (emulation_type & EMULTYPE_SKIP) |
3932 | return EMULATE_FAIL; | ||
3933 | return handle_emulation_failure(vcpu); | ||
3939 | } | 3934 | } |
3940 | } | 3935 | } |
3941 | 3936 | ||
@@ -3960,9 +3955,7 @@ restart: | |||
3960 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) | 3955 | if (kvm_mmu_unprotect_page_virt(vcpu, cr2)) |
3961 | return EMULATE_DONE; | 3956 | return EMULATE_DONE; |
3962 | 3957 | ||
3963 | trace_kvm_emulate_insn_failed(vcpu); | 3958 | return handle_emulation_failure(vcpu); |
3964 | kvm_report_emulation_failure(vcpu, "mmio"); | ||
3965 | return EMULATE_FAIL; | ||
3966 | } | 3959 | } |
3967 | 3960 | ||
3968 | toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility); | 3961 | toggle_interruptibility(vcpu, vcpu->arch.emulate_ctxt.interruptibility); |
@@ -4798,7 +4791,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |||
4798 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); | 4791 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
4799 | r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE); | 4792 | r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE); |
4800 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); | 4793 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
4801 | if (r == EMULATE_DO_MMIO) { | 4794 | if (r != EMULATE_DONE) { |
4802 | r = 0; | 4795 | r = 0; |
4803 | goto out; | 4796 | goto out; |
4804 | } | 4797 | } |