Skip to content

Commit 404d5d7

Browse files
Wanpeng Libonzini
Wanpeng Li
authored andcommitted
KVM: X86: Introduce more exit_fastpath_completion enum values
Adds a fastpath_t typedef since enum lines are a bit long, and replace EXIT_FASTPATH_SKIP_EMUL_INS with two new exit_fastpath_completion enum values. - EXIT_FASTPATH_EXIT_HANDLED kvm will still go through it's full run loop, but it would skip invoking the exit handler. - EXIT_FASTPATH_REENTER_GUEST complete fastpath, guest can be re-entered without invoking the exit handler or going back to vcpu_run Tested-by: Haiwei Li <lihaiwei@tencent.com> Cc: Haiwei Li <lihaiwei@tencent.com> Signed-off-by: Wanpeng Li <wanpengli@tencent.com> Message-Id: <1588055009-12677-4-git-send-email-wanpengli@tencent.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 5a9f544 commit 404d5d7

File tree

5 files changed

+39
-27
lines changed

5 files changed

+39
-27
lines changed

arch/x86/include/asm/kvm_host.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ enum {
182182

183183
enum exit_fastpath_completion {
184184
EXIT_FASTPATH_NONE,
185-
EXIT_FASTPATH_SKIP_EMUL_INS,
185+
EXIT_FASTPATH_REENTER_GUEST,
186+
EXIT_FASTPATH_EXIT_HANDLED,
186187
};
188+
typedef enum exit_fastpath_completion fastpath_t;
187189

188190
struct x86_emulate_ctxt;
189191
struct x86_exception;

arch/x86/kvm/svm/svm.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -2893,8 +2893,7 @@ static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
28932893
*info2 = control->exit_info_2;
28942894
}
28952895

2896-
static int handle_exit(struct kvm_vcpu *vcpu,
2897-
enum exit_fastpath_completion exit_fastpath)
2896+
static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
28982897
{
28992898
struct vcpu_svm *svm = to_svm(vcpu);
29002899
struct kvm_run *kvm_run = vcpu->run;
@@ -2952,10 +2951,10 @@ static int handle_exit(struct kvm_vcpu *vcpu,
29522951
__func__, svm->vmcb->control.exit_int_info,
29532952
exit_code);
29542953

2955-
if (exit_fastpath == EXIT_FASTPATH_SKIP_EMUL_INS) {
2956-
kvm_skip_emulated_instruction(vcpu);
2954+
if (exit_fastpath != EXIT_FASTPATH_NONE)
29572955
return 1;
2958-
} else if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
2956+
2957+
if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
29592958
|| !svm_exit_handlers[exit_code]) {
29602959
vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code);
29612960
dump_vmcb(vcpu);
@@ -3324,7 +3323,7 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu)
33243323
svm_complete_interrupts(svm);
33253324
}
33263325

3327-
static enum exit_fastpath_completion svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
3326+
static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
33283327
{
33293328
if (!is_guest_mode(vcpu) &&
33303329
to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
@@ -3336,9 +3335,9 @@ static enum exit_fastpath_completion svm_exit_handlers_fastpath(struct kvm_vcpu
33363335

33373336
void __svm_vcpu_run(unsigned long vmcb_pa, unsigned long *regs);
33383337

3339-
static enum exit_fastpath_completion svm_vcpu_run(struct kvm_vcpu *vcpu)
3338+
static fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
33403339
{
3341-
enum exit_fastpath_completion exit_fastpath;
3340+
fastpath_t exit_fastpath;
33423341
struct vcpu_svm *svm = to_svm(vcpu);
33433342

33443343
svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];

arch/x86/kvm/vmx/vmx.c

+18-8
Original file line numberDiff line numberDiff line change
@@ -5926,8 +5926,7 @@ void dump_vmcs(void)
59265926
* The guest has exited. See if we can fix it or if we need userspace
59275927
* assistance.
59285928
*/
5929-
static int vmx_handle_exit(struct kvm_vcpu *vcpu,
5930-
enum exit_fastpath_completion exit_fastpath)
5929+
static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
59315930
{
59325931
struct vcpu_vmx *vmx = to_vmx(vcpu);
59335932
u32 exit_reason = vmx->exit_reason;
@@ -6034,10 +6033,8 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu,
60346033
}
60356034
}
60366035

6037-
if (exit_fastpath == EXIT_FASTPATH_SKIP_EMUL_INS) {
6038-
kvm_skip_emulated_instruction(vcpu);
6036+
if (exit_fastpath != EXIT_FASTPATH_NONE)
60396037
return 1;
6040-
}
60416038

60426039
if (exit_reason >= kvm_vmx_max_exit_handlers)
60436040
goto unexpected_vmexit;
@@ -6628,7 +6625,7 @@ void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
66286625
}
66296626
}
66306627

6631-
static enum exit_fastpath_completion vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
6628+
static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
66326629
{
66336630
switch (to_vmx(vcpu)->exit_reason) {
66346631
case EXIT_REASON_MSR_WRITE:
@@ -6640,12 +6637,13 @@ static enum exit_fastpath_completion vmx_exit_handlers_fastpath(struct kvm_vcpu
66406637

66416638
bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
66426639

6643-
static enum exit_fastpath_completion vmx_vcpu_run(struct kvm_vcpu *vcpu)
6640+
static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
66446641
{
6645-
enum exit_fastpath_completion exit_fastpath;
6642+
fastpath_t exit_fastpath;
66466643
struct vcpu_vmx *vmx = to_vmx(vcpu);
66476644
unsigned long cr3, cr4;
66486645

6646+
reenter_guest:
66496647
/* Record the guest's net vcpu time for enforced NMI injections. */
66506648
if (unlikely(!enable_vnmi &&
66516649
vmx->loaded_vmcs->soft_vnmi_blocked))
@@ -6807,6 +6805,18 @@ static enum exit_fastpath_completion vmx_vcpu_run(struct kvm_vcpu *vcpu)
68076805
return EXIT_FASTPATH_NONE;
68086806

68096807
exit_fastpath = vmx_exit_handlers_fastpath(vcpu);
6808+
if (exit_fastpath == EXIT_FASTPATH_REENTER_GUEST) {
6809+
if (!kvm_vcpu_exit_request(vcpu)) {
6810+
/*
6811+
* FIXME: this goto should be a loop in vcpu_enter_guest,
6812+
* but it would incur the cost of a retpoline for now.
6813+
* Revisit once static calls are available.
6814+
*/
6815+
goto reenter_guest;
6816+
}
6817+
exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
6818+
}
6819+
68106820
return exit_fastpath;
68116821
}
68126822

arch/x86/kvm/x86.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -1608,27 +1608,28 @@ static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data
16081608
return 1;
16091609
}
16101610

1611-
enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
1611+
fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
16121612
{
16131613
u32 msr = kvm_rcx_read(vcpu);
16141614
u64 data;
1615-
int ret = 0;
1615+
fastpath_t ret = EXIT_FASTPATH_NONE;
16161616

16171617
switch (msr) {
16181618
case APIC_BASE_MSR + (APIC_ICR >> 4):
16191619
data = kvm_read_edx_eax(vcpu);
1620-
ret = handle_fastpath_set_x2apic_icr_irqoff(vcpu, data);
1620+
if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) {
1621+
kvm_skip_emulated_instruction(vcpu);
1622+
ret = EXIT_FASTPATH_EXIT_HANDLED;
1623+
}
16211624
break;
16221625
default:
1623-
return EXIT_FASTPATH_NONE;
1626+
break;
16241627
}
16251628

1626-
if (!ret) {
1629+
if (ret != EXIT_FASTPATH_NONE)
16271630
trace_kvm_msr_write(msr, data);
1628-
return EXIT_FASTPATH_SKIP_EMUL_INS;
1629-
}
16301631

1631-
return EXIT_FASTPATH_NONE;
1632+
return ret;
16321633
}
16331634
EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff);
16341635

@@ -8205,7 +8206,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
82058206
bool req_int_win =
82068207
dm_request_for_irq_injection(vcpu) &&
82078208
kvm_cpu_accept_dm_intr(vcpu);
8208-
enum exit_fastpath_completion exit_fastpath;
8209+
fastpath_t exit_fastpath;
82098210

82108211
bool req_immediate_exit = false;
82118212

arch/x86/kvm/x86.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
274274
bool kvm_vector_hashing_enabled(void);
275275
int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
276276
int emulation_type, void *insn, int insn_len);
277-
enum exit_fastpath_completion handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
277+
fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
278278

279279
extern u64 host_xcr0;
280280
extern u64 supported_xcr0;

0 commit comments

Comments
 (0)