Skip to content

Commit 60fc3d0

Browse files
Sean Christophersonbonzini
authored andcommitted
KVM: x86: Remove emulation_result enums, EMULATE_{DONE,FAIL,USER_EXIT}
Deferring emulation failure handling (in some cases) to the caller of x86_emulate_instruction() has proven fragile, e.g. multiple instances of KVM not setting run->exit_reason on EMULATE_FAIL, largely due to it being difficult to discern what emulation types can return what result, and which combination of types and results are handled where. Now that x86_emulate_instruction() always handles emulation failure, i.e. EMULATION_FAIL is only referenced in callers, remove the emulation_result enums entirely. Per KVM's existing exit handling conventions, return '0' and '1' for "exit to userspace" and "resume guest" respectively. Doing so cleans up many callers, e.g. they can return kvm_emulate_instruction() directly instead of having to interpret its result. Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 8fff271 commit 60fc3d0

File tree

5 files changed

+49
-78
lines changed

5 files changed

+49
-78
lines changed

arch/x86/include/asm/kvm_host.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,12 +1315,6 @@ extern u64 kvm_default_tsc_scaling_ratio;
13151315

13161316
extern u64 kvm_mce_cap_supported;
13171317

1318-
enum emulation_result {
1319-
EMULATE_DONE, /* no further processing */
1320-
EMULATE_USER_EXIT, /* kvm_run ready for userspace exit */
1321-
EMULATE_FAIL, /* can't emulate this instruction */
1322-
};
1323-
13241318
#define EMULTYPE_NO_DECODE (1 << 0)
13251319
#define EMULTYPE_TRAP_UD (1 << 1)
13261320
#define EMULTYPE_SKIP (1 << 2)

arch/x86/kvm/mmu.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5383,7 +5383,6 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,
53835383
void *insn, int insn_len)
53845384
{
53855385
int r, emulation_type = 0;
5386-
enum emulation_result er;
53875386
bool direct = vcpu->arch.mmu->direct_map;
53885387

53895388
/* With shadow page tables, fault_address contains a GVA or nGPA. */
@@ -5450,17 +5449,8 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,
54505449
return 1;
54515450
}
54525451

5453-
er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
5454-
5455-
switch (er) {
5456-
case EMULATE_DONE:
5457-
return 1;
5458-
case EMULATE_USER_EXIT:
5459-
case EMULATE_FAIL:
5460-
return 0;
5461-
default:
5462-
BUG();
5463-
}
5452+
return x86_emulate_instruction(vcpu, cr2, emulation_type, insn,
5453+
insn_len);
54645454
}
54655455
EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
54665456

arch/x86/kvm/svm.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
787787
kvm_rip_write(vcpu, svm->next_rip);
788788
svm_set_interrupt_shadow(vcpu, 0);
789789

790-
return EMULATE_DONE;
790+
return 1;
791791
}
792792

793793
static void svm_queue_exception(struct kvm_vcpu *vcpu)
@@ -2779,8 +2779,7 @@ static int gp_interception(struct vcpu_svm *svm)
27792779
kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
27802780
return 1;
27812781
}
2782-
return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP) !=
2783-
EMULATE_USER_EXIT;
2782+
return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
27842783
}
27852784

27862785
static bool is_erratum_383(void)
@@ -2878,7 +2877,7 @@ static int io_interception(struct vcpu_svm *svm)
28782877
string = (io_info & SVM_IOIO_STR_MASK) != 0;
28792878
in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
28802879
if (string)
2881-
return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
2880+
return kvm_emulate_instruction(vcpu, 0);
28822881

28832882
port = io_info >> 16;
28842883
size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
@@ -3885,17 +3884,15 @@ static int task_switch_interception(struct vcpu_svm *svm)
38853884
int_type == SVM_EXITINTINFO_TYPE_SOFT ||
38863885
(int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
38873886
(int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
3888-
if (skip_emulated_instruction(&svm->vcpu) == EMULATE_USER_EXIT)
3887+
if (!skip_emulated_instruction(&svm->vcpu))
38893888
return 0;
38903889
}
38913890

38923891
if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
38933892
int_vec = -1;
38943893

3895-
3896-
38973894
return kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
3898-
has_error_code, error_code) != EMULATE_USER_EXIT;
3895+
has_error_code, error_code);
38993896
}
39003897

39013898
static int cpuid_interception(struct vcpu_svm *svm)
@@ -3916,21 +3913,20 @@ static int iret_interception(struct vcpu_svm *svm)
39163913
static int invlpg_interception(struct vcpu_svm *svm)
39173914
{
39183915
if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
3919-
return kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3916+
return kvm_emulate_instruction(&svm->vcpu, 0);
39203917

39213918
kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
39223919
return kvm_skip_emulated_instruction(&svm->vcpu);
39233920
}
39243921

39253922
static int emulate_on_interception(struct vcpu_svm *svm)
39263923
{
3927-
return kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
3924+
return kvm_emulate_instruction(&svm->vcpu, 0);
39283925
}
39293926

39303927
static int rsm_interception(struct vcpu_svm *svm)
39313928
{
3932-
return kvm_emulate_instruction_from_buffer(&svm->vcpu,
3933-
rsm_ins_bytes, 2) == EMULATE_DONE;
3929+
return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2);
39343930
}
39353931

39363932
static int rdpmc_interception(struct vcpu_svm *svm)
@@ -4719,7 +4715,7 @@ static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
47194715
ret = avic_unaccel_trap_write(svm);
47204716
} else {
47214717
/* Handling Fault */
4722-
ret = (kvm_emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
4718+
ret = kvm_emulate_instruction(&svm->vcpu, 0);
47234719
}
47244720

47254721
return ret;

arch/x86/kvm/vmx/vmx.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ static int __skip_emulated_instruction(struct kvm_vcpu *vcpu)
15161516
/* skipping an emulated instruction also counts */
15171517
vmx_set_interrupt_shadow(vcpu, 0);
15181518

1519-
return EMULATE_DONE;
1519+
return 1;
15201520
}
15211521

15221522
static inline void skip_emulated_instruction(struct kvm_vcpu *vcpu)
@@ -4468,7 +4468,7 @@ static int handle_rmode_exception(struct kvm_vcpu *vcpu,
44684468
* Cause the #SS fault with 0 error code in VM86 mode.
44694469
*/
44704470
if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4471-
if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4471+
if (kvm_emulate_instruction(vcpu, 0)) {
44724472
if (vcpu->arch.halt_request) {
44734473
vcpu->arch.halt_request = 0;
44744474
return kvm_vcpu_halt(vcpu);
@@ -4545,8 +4545,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
45454545
kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
45464546
return 1;
45474547
}
4548-
return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP) !=
4549-
EMULATE_USER_EXIT;
4548+
return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
45504549
}
45514550

45524551
/*
@@ -4643,7 +4642,7 @@ static int handle_io(struct kvm_vcpu *vcpu)
46434642
++vcpu->stat.io_exits;
46444643

46454644
if (string)
4646-
return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
4645+
return kvm_emulate_instruction(vcpu, 0);
46474646

46484647
port = exit_qualification >> 16;
46494648
size = (exit_qualification & 7) + 1;
@@ -4717,7 +4716,7 @@ static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
47174716
static int handle_desc(struct kvm_vcpu *vcpu)
47184717
{
47194718
WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
4720-
return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
4719+
return kvm_emulate_instruction(vcpu, 0);
47214720
}
47224721

47234722
static int handle_cr(struct kvm_vcpu *vcpu)
@@ -4933,7 +4932,7 @@ static int handle_vmcall(struct kvm_vcpu *vcpu)
49334932

49344933
static int handle_invd(struct kvm_vcpu *vcpu)
49354934
{
4936-
return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
4935+
return kvm_emulate_instruction(vcpu, 0);
49374936
}
49384937

49394938
static int handle_invlpg(struct kvm_vcpu *vcpu)
@@ -5000,7 +4999,7 @@ static int handle_apic_access(struct kvm_vcpu *vcpu)
50004999
return kvm_skip_emulated_instruction(vcpu);
50015000
}
50025001
}
5003-
return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
5002+
return kvm_emulate_instruction(vcpu, 0);
50045003
}
50055004

50065005
static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
@@ -5077,7 +5076,7 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
50775076
*/
50785077
return kvm_task_switch(vcpu, tss_selector,
50795078
type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
5080-
reason, has_error_code, error_code) != EMULATE_USER_EXIT;
5079+
reason, has_error_code, error_code);
50815080
}
50825081

50835082
static int handle_ept_violation(struct kvm_vcpu *vcpu)
@@ -5149,8 +5148,7 @@ static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
51495148
if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
51505149
return kvm_skip_emulated_instruction(vcpu);
51515150
else
5152-
return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
5153-
EMULATE_DONE;
5151+
return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP);
51545152
}
51555153

51565154
return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
@@ -5169,7 +5167,6 @@ static int handle_nmi_window(struct kvm_vcpu *vcpu)
51695167
static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
51705168
{
51715169
struct vcpu_vmx *vmx = to_vmx(vcpu);
5172-
enum emulation_result err;
51735170
bool intr_window_requested;
51745171
unsigned count = 130;
51755172

@@ -5190,14 +5187,9 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
51905187
if (kvm_test_request(KVM_REQ_EVENT, vcpu))
51915188
return 1;
51925189

5193-
err = kvm_emulate_instruction(vcpu, 0);
5194-
5195-
if (err == EMULATE_USER_EXIT)
5190+
if (!kvm_emulate_instruction(vcpu, 0))
51965191
return 0;
51975192

5198-
if (WARN_ON_ONCE(err == EMULATE_FAIL))
5199-
return 1;
5200-
52015193
if (vmx->emulation_required && !vmx->rmode.vm86_active &&
52025194
vcpu->arch.exception.pending) {
52035195
vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;

0 commit comments

Comments
 (0)