Skip to content

Commit 58fbbf2

Browse files
ktian1avikivity
authored andcommitted
KVM: APIC: avoid instruction emulation for EOI writes
Instruction emulation for EOI writes can be skipped, since sane guest simply uses MOV instead of string operations. This is a nice improvement when guest doesn't support x2apic or hyper-V EOI support. a single VM bandwidth is observed with ~8% bandwidth improvement (7.4Gbps->8Gbps), by saving ~5% cycles from EOI emulation. Signed-off-by: Kevin Tian <kevin.tian@intel.com> <Based on earlier work from>: Signed-off-by: Eddie Dong <eddie.dong@intel.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 45133ec commit 58fbbf2

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎arch/x86/include/asm/vmx.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ enum vmcs_field {
350350
#define DEBUG_REG_ACCESS_REG(eq) (((eq) >> 8) & 0xf) /* 11:8, general purpose reg. */
351351

352352

353+
/*
354+
* Exit Qualifications for APIC-Access
355+
*/
356+
#define APIC_ACCESS_OFFSET 0xfff /* 11:0, offset within the APIC page */
357+
#define APIC_ACCESS_TYPE 0xf000 /* 15:12, access type */
358+
#define TYPE_LINEAR_APIC_INST_READ (0 << 12)
359+
#define TYPE_LINEAR_APIC_INST_WRITE (1 << 12)
360+
#define TYPE_LINEAR_APIC_INST_FETCH (2 << 12)
361+
#define TYPE_LINEAR_APIC_EVENT (3 << 12)
362+
#define TYPE_PHYSICAL_APIC_EVENT (10 << 12)
363+
#define TYPE_PHYSICAL_APIC_INST (15 << 12)
364+
353365
/* segment AR */
354366
#define SEGMENT_AR_L_MASK (1 << 13)
355367

‎arch/x86/kvm/lapic.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,15 @@ static int apic_mmio_write(struct kvm_io_device *this,
864864
return 0;
865865
}
866866

867+
void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu)
868+
{
869+
struct kvm_lapic *apic = vcpu->arch.apic;
870+
871+
if (apic)
872+
apic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
873+
}
874+
EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi);
875+
867876
void kvm_free_lapic(struct kvm_vcpu *vcpu)
868877
{
869878
if (!vcpu->arch.apic)

‎arch/x86/kvm/lapic.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
2626
void kvm_lapic_reset(struct kvm_vcpu *vcpu);
2727
u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
2828
void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
29+
void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu);
2930
void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value);
3031
u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu);
3132
void kvm_apic_set_version(struct kvm_vcpu *vcpu);

‎arch/x86/kvm/vmx.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ module_param(vmm_exclusive, bool, S_IRUGO);
7171
static int __read_mostly yield_on_hlt = 1;
7272
module_param(yield_on_hlt, bool, S_IRUGO);
7373

74+
static int __read_mostly fasteoi = 1;
75+
module_param(fasteoi, bool, S_IRUGO);
76+
7477
/*
7578
* If nested=1, nested virtualization is supported, i.e., guests may use
7679
* VMX and be a hypervisor for its own guests. If nested=0, guests may not
@@ -4540,6 +4543,24 @@ static int handle_xsetbv(struct kvm_vcpu *vcpu)
45404543

45414544
static int handle_apic_access(struct kvm_vcpu *vcpu)
45424545
{
4546+
if (likely(fasteoi)) {
4547+
unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4548+
int access_type, offset;
4549+
4550+
access_type = exit_qualification & APIC_ACCESS_TYPE;
4551+
offset = exit_qualification & APIC_ACCESS_OFFSET;
4552+
/*
4553+
* Sane guest uses MOV to write EOI, with written value
4554+
* not cared. So make a short-circuit here by avoiding
4555+
* heavy instruction emulation.
4556+
*/
4557+
if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4558+
(offset == APIC_EOI)) {
4559+
kvm_lapic_set_eoi(vcpu);
4560+
skip_emulated_instruction(vcpu);
4561+
return 1;
4562+
}
4563+
}
45434564
return emulate_instruction(vcpu, 0) == EMULATE_DONE;
45444565
}
45454566

0 commit comments

Comments
 (0)