improve speed of "rep ins" emulation
v3 cover improve speed of "rep ins" emulation
With this patches loading 100M initrd takes ~10s instead of ~40s without.
Changelog:
v2->v3
- Fix incorrect size parameter for linearize() notices by Marcelo.
- Get rid of linearize() callback in emulation ops.
v1->v2
- add segment check and mask rcx/rdi correctly drying increment.
Gleb Natapov (5):
KVM: Provide userspace IO exit completion callback.
KVM: emulator: make x86 emulation modes enum instead of defines
KVM: emulator: move some address manipulation function out of
emulator code.
KVM: emulator: make linearize() callable from outside of emulator
KVM: Provide fast path for "rep ins" emulation if possible.
arch/x86/include/asm/kvm_emulate.h | 38 +++-- arch/x86/include/asm/kvm_host.h | 32 ++++ arch/x86/kvm/emulate.c | 133 ++++++++-------- arch/x86/kvm/svm.c | 20 ++- arch/x86/kvm/vmx.c | 25 ++- arch/x86/kvm/x86.c | 305 ++++++++++++++++++++++++++++-------- 6 files changed, 395 insertions(+), 158 deletions(-)
Signature · 1 line
1.7.10
PATCH v3 2/5 KVM: emulator: make x86 emulation modes enum instead of defines
--- arch/x86/include/asm/kvm_emulate.h | 22 ++++++++++------------ arch/x86/kvm/emulate.c | 4 +++- 2 files changed, 13 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_emulate.h +10 -12
@@ -236,6 +236,15 @@ struct read_cache {unsigned long end; }; +/* Execution mode, passed to the emulator. */ +enum x86emul_mode { +X86EMUL_MODE_REAL ,/* Real mode. */ +X86EMUL_MODE_VM86 ,/* Virtual 8086 mode. */ +X86EMUL_MODE_PROT16 ,/* 16-bit protected mode. */ +X86EMUL_MODE_PROT32 ,/* 32-bit protected mode. */ +X86EMUL_MODE_PROT64 ,/* 64-bit (long) mode. */ +}; +struct x86_emulate_ctxt {struct x86_emulate_ops *ops;
@@ -243,7 +252,7 @@ struct x86_emulate_ctxt {unsigned long eflags;unsigned long eip;/* eip before instruction emulation */ /* Emulated execution mode, represented by an X86EMUL_MODE value. */ -int mode; +enum x86emul_mode mode;/* interruptibility state, as a result of execution of STI or MOV SS */ int interruptibility;
@@ -293,17 +302,6 @@ struct x86_emulate_ctxt {#define REPE_PREFIX 0xf3#define REPNE_PREFIX 0xf2 -/* Execution mode, passed to the emulator. */ -#define X86EMUL_MODE_REAL 0/* Real mode. */ -#define X86EMUL_MODE_VM86 1/* Virtual 8086 mode. */ -#define X86EMUL_MODE_PROT16 2/* 16-bit protected mode. */ -#define X86EMUL_MODE_PROT32 4/* 32-bit protected mode. */ -#define X86EMUL_MODE_PROT64 8/* 64-bit (long) mode. */ - -/* any protected mode */ -#define X86EMUL_MODE_PROT (X86EMUL_MODE_PROT16|X86EMUL_MODE_PROT32| \ - X86EMUL_MODE_PROT64) -/* CPUID vendors */ #define X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx 0x68747541#define X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx 0x444d4163
arch/x86/kvm/emulate.c +3 -1
@@ -2141,6 +2141,8 @@ static int em_sysenter(struct x86_emulate_ctxt *ctxt)if (msr_data ==0x0 )return emulate_gp (ctxt,0 );break ; +default : +break ; } ctxt->eflags &= ~(EFLG_VM |EFLG_IF |EFLG_RF );
@@ -4179,7 +4181,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) }/* Instruction can only be executed in protected mode */ -if ((ctxt->d & Prot) && !(ctxt->mode &X86EMUL_MODE_PROT )) { +if ((ctxt->d & Prot) && ctxt->mode <X86EMUL_MODE_PROT16 ) { rc =emulate_ud (ctxt); goto done; }
Signature · 1 line
1.7.10
PATCH v3 1/5 KVM: Provide userspace IO exit completion callback.
In reply to Gleb NatapovCurrent code assumes that IO exit was due to instruction emulation and handles execution back to emulator directly. This patch adds new userspace IO exit completion callback that can be set by any other code that caused IO exit to userspace.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 92 +++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 37 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -413,6 +413,7 @@ struct kvm_vcpu_arch {struct x86_emulate_ctxt emulate_ctxt;bool emulate_regs_need_sync_to_vcpu;bool emulate_regs_need_sync_from_vcpu; +int (*complete_userspace_io)(struct kvm_vcpu *vcpu);gpa_t time;struct pvclock_vcpu_time_info hv_clock;
arch/x86/kvm/x86.c +55 -37
136 lines not on this page · raw message
Signature · 1 line
1.7.10
PATCH v3 3/5 KVM: emulator: move some address manipulation function out of emulator code.
In reply to Gleb NatapovThe functions will be used outside of the emulator.
--- arch/x86/include/asm/kvm_host.h | 25 +++++++++++++++++++++++++ arch/x86/kvm/emulate.c | 15 ++------------- 2 files changed, 27 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +25 -0
@@ -980,4 +980,29 @@ int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);void kvm_handle_pmu_event (struct kvm_vcpu *vcpu);void kvm_deliver_pmi (struct kvm_vcpu *vcpu); +static inline unsigned long kvm_ad_mask (u8 ad_bytes) +{ +return (1UL << (ad_bytes <<3 )) -1 ; +} + +/* Access/update address held in a register, based on addressing mode. */ +static inline unsigned long +kvm_address_mask (u8 ad_bytes,unsigned long reg) +{ +if (ad_bytes ==sizeof (unsigned long )) +return reg; +else +return reg &kvm_ad_mask (ad_bytes); +} + +static inline void +kvm_register_address_increment (u8 ad_bytes,unsigned long *reg,int inc) +{ +if (ad_bytes ==sizeof (unsigned long )) + *reg += inc; +else + *reg = (*reg & ~kvm_ad_mask (ad_bytes)) | + ((*reg + inc) &kvm_ad_mask (ad_bytes)); +} +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/emulate.c +2 -13
@@ -433,19 +433,11 @@ static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,return ctxt->ops->intercept (ctxt, &info, stage); } -static inline unsigned long ad_mask (struct x86_emulate_ctxt *ctxt) -{ -return (1UL << (ctxt->ad_bytes <<3 )) -1 ; -} -/* Access/update address held in a register, based on addressing mode. */ static inline unsigned long address_mask (struct x86_emulate_ctxt *ctxt,unsigned long reg) { -if (ctxt->ad_bytes ==sizeof (unsigned long )) -return reg; -else -return reg &ad_mask (ctxt); +return kvm_address_mask (ctxt->ad_bytes, reg); } static inline unsigned long
@@ -457,10 +449,7 @@ register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg)static inline void register_address_increment (struct x86_emulate_ctxt *ctxt,unsigned long *reg,int inc) { -if (ctxt->ad_bytes ==sizeof (unsigned long )) - *reg += inc; -else - *reg = (*reg & ~ad_mask (ctxt)) | ((*reg + inc) &ad_mask (ctxt)); +return kvm_register_address_increment (ctxt->ad_bytes, reg, inc); }static inline void jmp_rel (struct x86_emulate_ctxt *ctxt,int rel)
Signature · 1 line
1.7.10
PATCH v3 4/5 KVM: emulator: make linearize() callable from outside of emulator
In reply to Gleb NatapovThe function will be used outside of the emulator.
--- arch/x86/include/asm/kvm_emulate.h | 16 +++++ arch/x86/kvm/emulate.c | 114 ++++++++++++++++++++---------------- 2 files changed, 79 insertions(+), 51 deletions(-)
arch/x86/include/asm/kvm_emulate.h +16 -0
@@ -374,6 +374,21 @@ enum x86_intercept { nr_x86_intercepts }; +struct x86_linearize_params +{ +enum x86emul_mode mode; +ulong ea; +unsigned size; +unsigned seg; +struct desc_struct desc; +u16 sel; +bool usable; +bool write; +bool fetch; +u8 ad_bytes; +unsigned cpl; +}; +/* Host execution mode. */ #if defined(CONFIG_X86_32 )#define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
@@ -392,4 +407,5 @@ int emulator_task_switch(struct x86_emulate_ctxt *ctxt, u16 tss_selector, int idt_index, int reason, bool has_error_code, u32 error_code);int emulate_int_real (struct x86_emulate_ctxt *ctxt,int irq); +int x86_linearize (struct x86_linearize_params *p,ulong *linear);#endif /* _ASM_X86_KVM_X86_EMULATE_H */
arch/x86/kvm/emulate.c +63 -51
167 lines not on this page · raw message
Signature · 1 line
1.7.10
PATCH v3 5/5 KVM: Provide fast path for "rep ins" emulation if possible.
In reply to Gleb Natapov"rep ins" emulation is going through emulator now. This is slow because emulator knows how to write back only one datum at a time. This patch provides fast path for the instruction in certain conditions. The conditions are: DF flag is not set, destination memory is RAM and single datum does not cross page boundary. If fast path code fails it falls back to emulation.
--- arch/x86/include/asm/kvm_host.h | 6 ++ arch/x86/kvm/svm.c | 20 +++- arch/x86/kvm/vmx.c | 25 +++-- arch/x86/kvm/x86.c | 213 +++++++++++++++++++++++++++++++++------ 4 files changed, 220 insertions(+), 44 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -411,6 +411,10 @@ struct kvm_vcpu_arch {/* emulate context */ struct x86_emulate_ctxt emulate_ctxt; +struct x86_fast_string_pio_ctxt { +unsigned long linear_addr; +u8 ad_bytes; + } fast_string_pio_ctxt;bool emulate_regs_need_sync_to_vcpu;bool emulate_regs_need_sync_from_vcpu;int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
@@ -776,6 +780,8 @@ int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);struct x86_emulate_ctxt ;int kvm_fast_pio_out (struct kvm_vcpu *vcpu,int size,unsigned short port); +int kvm_fast_string_pio_in (struct kvm_vcpu *vcpu,int size,unsigned short port, +u8 ad_bytes_idx);void kvm_emulate_cpuid (struct kvm_vcpu *vcpu);int kvm_emulate_halt (struct kvm_vcpu *vcpu);int kvm_emulate_wbinvd (struct kvm_vcpu *vcpu);
arch/x86/kvm/svm.c +15 -5
@@ -1887,21 +1887,31 @@ static int io_interception(struct vcpu_svm *svm) {struct kvm_vcpu *vcpu = &svm->vcpu;u32 io_info = svm->vmcb->control.exit_info_1;/* address size bug? */ -int size, in, string; +int size, in, string, rep;unsigned port; ++svm->vcpu.stat.io_exits; string = (io_info &SVM_IOIO_STR_MASK ) !=0 ; + rep = (io_info &SVM_IOIO_REP_MASK ) !=0 ; in = (io_info &SVM_IOIO_TYPE_MASK ) !=0 ; -if (string || in) -return emulate_instruction (vcpu,0 ) ==EMULATE_DONE ; port = io_info >>16 ; size = (io_info &SVM_IOIO_SIZE_MASK ) >>SVM_IOIO_SIZE_SHIFT ; svm->next_rip = svm->vmcb->control.exit_info_2; -skip_emulated_instruction (&svm->vcpu); -return kvm_fast_pio_out (vcpu, size, port); +if (!string && !in) { +skip_emulated_instruction (&svm->vcpu); +return kvm_fast_pio_out (vcpu, size, port); + }else if (string && in && rep) { +int addr_size = (io_info &SVM_IOIO_ASIZE_MASK ) >> +SVM_IOIO_ASIZE_SHIFT ; +int r =kvm_fast_string_pio_in (vcpu, size, port, +ffs (addr_size) -1 ); +if (r !=EMULATE_FAIL ) +return r ==EMULATE_DONE ; + } + +return emulate_instruction (vcpu,0 ) ==EMULATE_DONE ; }static int nmi_interception (struct vcpu_svm *svm)
arch/x86/kvm/vmx.c +18 -7
@@ -639,6 +639,7 @@ static unsigned long *vmx_msr_bitmap_longmode;static bool cpu_has_load_ia32_efer;static bool cpu_has_load_perf_global_ctrl; +static bool cpu_has_ins_outs_inst_info; static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);static DEFINE_SPINLOCK (vmx_vpid_lock);
@@ -2522,6 +2523,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)if (((vmx_msr_high >>18 ) &15 ) !=6 )return -EIO ; + cpu_has_ins_outs_inst_info = vmx_msr_high & (1u <<22 ); + vmcs_conf->size = vmx_msr_high &0x1fff ; vmcs_conf->order =get_order (vmcs_config.size); vmcs_conf->revision_id = vmx_msr_low;
@@ -4393,23 +4396,31 @@ static int handle_triple_fault(struct kvm_vcpu *vcpu)static int handle_io (struct kvm_vcpu *vcpu) {unsigned long exit_qualification; -int size, in, string; +int size, in, string, rep;unsigned port; exit_qualification =vmcs_readl (EXIT_QUALIFICATION ); - string = (exit_qualification &16 ) !=0 ; in = (exit_qualification &8 ) !=0 ; + string = (exit_qualification &16 ) !=0 ; + rep = (exit_qualification &32 ) !=0 ; ++vcpu->stat.io_exits; -if (string || in) -return emulate_instruction (vcpu,0 ) ==EMULATE_DONE ; - port = exit_qualification >>16 ; size = (exit_qualification &7 ) +1 ; -skip_emulated_instruction (vcpu); -return kvm_fast_pio_out (vcpu, size, port); +if (!string && !in) { +skip_emulated_instruction (vcpu); +return kvm_fast_pio_out (vcpu, size, port); + }else if (string && in && rep && cpu_has_ins_outs_inst_info) { +u32 inst_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); +int r =kvm_fast_string_pio_in (vcpu, size, port, + (inst_info >>7 ) &7 ); +if (r !=EMULATE_FAIL ) +return r ==EMULATE_DONE ; + } + +return emulate_instruction (vcpu,0 ) ==EMULATE_DONE ; } static void
arch/x86/kvm/x86.c +181 -32
250 lines not on this page · raw message
Signature · 1 line
1.7.10
NACK. Found bugs.
Quoted 18 lines · Gleb Natapov
With this patches loading 100M initrd takes ~10s instead of ~40s without.
Changelog:
v2->v3
- Fix incorrect size parameter forlinearize()notices by Marcelo.
- Get rid oflinearize()callback in emulation ops.
v1->v2
- add segment check and mask rcx/rdi correctly drying increment.Gleb Natapov (5):
KVM: Provide userspace IO exit completion callback.
KVM: emulator: make x86 emulation modes enum instead of defines
KVM: emulator: move some address manipulation function out of
emulator code.
KVM: emulator: makelinearize()callable from outside of emulator
KVM: Provide fast path for "rep ins" emulation if possible.
Quoted 15 lines
arch/x86/include/asm/kvm_emulate.h | 38 +++-- arch/x86/include/asm/kvm_host.h | 32 ++++ arch/x86/kvm/emulate.c | 133 ++++++++-------- arch/x86/kvm/svm.c | 20 ++- arch/x86/kvm/vmx.c | 25 ++- arch/x86/kvm/x86.c | 305 ++++++++++++++++++++++++++++-------- 6 files changed, 395 insertions(+), 158 deletions(-)Signature · 6 lines
1.7.10 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Gleb.