Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 80ced186 authored by Mohammed Gamal's avatar Mohammed Gamal Committed by Avi Kivity
Browse files

KVM: VMX: Enhance invalid guest state emulation



- Change returned handle_invalid_guest_state() to return relevant exit codes
- Move triggering the emulation from vmx_vcpu_run() to vmx_handle_exit()
- Return to userspace instead of repeatedly trying to emulate instructions that have already failed

Signed-off-by: default avatarMohammed Gamal <m.gamal005@gmail.com>
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
parent abcf14b5
Loading
Loading
Loading
Loading
+20 −24
Original line number Original line Diff line number Diff line
@@ -107,7 +107,6 @@ struct vcpu_vmx {
	} rmode;
	} rmode;
	int vpid;
	int vpid;
	bool emulation_required;
	bool emulation_required;
	enum emulation_result invalid_state_emulation_result;


	/* Support for vnmi-less CPUs */
	/* Support for vnmi-less CPUs */
	int soft_vnmi_blocked;
	int soft_vnmi_blocked;
@@ -3322,35 +3321,37 @@ static int handle_nmi_window(struct kvm_vcpu *vcpu)
	return 1;
	return 1;
}
}


static void handle_invalid_guest_state(struct kvm_vcpu *vcpu)
static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
{
{
	struct vcpu_vmx *vmx = to_vmx(vcpu);
	struct vcpu_vmx *vmx = to_vmx(vcpu);
	enum emulation_result err = EMULATE_DONE;
	enum emulation_result err = EMULATE_DONE;

	int ret = 1;
	local_irq_enable();
	preempt_enable();


	while (!guest_state_valid(vcpu)) {
	while (!guest_state_valid(vcpu)) {
		err = emulate_instruction(vcpu, 0, 0, 0);
		err = emulate_instruction(vcpu, 0, 0, 0);


		if (err == EMULATE_DO_MMIO)
		if (err == EMULATE_DO_MMIO) {
			break;
			ret = 0;
			goto out;
		}


		if (err != EMULATE_DONE) {
		if (err != EMULATE_DONE) {
			kvm_report_emulation_failure(vcpu, "emulation failure");
			kvm_report_emulation_failure(vcpu, "emulation failure");
			break;
			vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
			vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
			ret = 0;
			goto out;
		}
		}


		if (signal_pending(current))
		if (signal_pending(current))
			break;
			goto out;
		if (need_resched())
		if (need_resched())
			schedule();
			schedule();
	}
	}


	preempt_disable();
	vmx->emulation_required = 0;
	local_irq_disable();
out:

	return ret;
	vmx->invalid_state_emulation_result = err;
}
}


/*
/*
@@ -3406,13 +3407,9 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu)


	trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
	trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));


	/* If we need to emulate an MMIO from handle_invalid_guest_state
	/* If guest state is invalid, start emulating */
	 * we just return 0 */
	if (vmx->emulation_required && emulate_invalid_guest_state)
	if (vmx->emulation_required && emulate_invalid_guest_state) {
		return handle_invalid_guest_state(vcpu);
		if (guest_state_valid(vcpu))
			vmx->emulation_required = 0;
		return vmx->invalid_state_emulation_result != EMULATE_DO_MMIO;
	}


	/* Access CR3 don't cause VMExit in paging mode, so we need
	/* Access CR3 don't cause VMExit in paging mode, so we need
	 * to sync with guest real CR3. */
	 * to sync with guest real CR3. */
@@ -3607,11 +3604,10 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
	if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
	if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
		vmx->entry_time = ktime_get();
		vmx->entry_time = ktime_get();


	/* Handle invalid guest state instead of entering VMX */
	/* Don't enter VMX if guest state is invalid, let the exit handler
	if (vmx->emulation_required && emulate_invalid_guest_state) {
	   start emulation until we arrive back to a valid state */
		handle_invalid_guest_state(vcpu);
	if (vmx->emulation_required && emulate_invalid_guest_state)
		return;
		return;
	}


	if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
	if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
		vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
		vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);