Skip to contentSkip to conversation

improve speed of "rep ins" emulation

PATCH Superseded Gleb Natapov 2 participants 16 messages Jul 19, 2012

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover improve speed of "rep ins" emulation

With this patches loading 100M initrd takes ~10s instead of ~40s without.

Changelog:
v3->v4:
- check rcx for zero after applying address mask
- check for page boundary after linearizing address

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):
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                 |  309 ++++++++++++++++++++++++++++--------
 6 files changed, 399 insertions(+), 158 deletions(-)
Signature · 1 line
1.7.10
#1 lore raw

PATCH v4 1/5 Provide userspace IO exit completion callback.

Current 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
index 24b7647..de2aff8 100644
@@ -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
#2 lore raw

PATCH v4 2/5 KVM: emulator: make x86 emulation modes enum instead of defines

In reply to Gleb Natapov
---
 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
index 1ac46c22..7c276ca 100644
@@ -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
index f95d242..79899df 100644
@@ -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
#3 lore raw

PATCH v4 3/5 KVM: emulator: move some address manipulation function out of emulator code.

In reply to Gleb Natapov

The 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
index de2aff8..6212575 100644
@@ -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
index 79899df..e317588 100644
@@ -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
#8 lore raw

PATCH v4 4/5 KVM: emulator: make linearize() callable from outside of emulator

In reply to Gleb Natapov

The 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
index 7c276ca..9ce651b 100644
@@ -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
#13 lore raw

PATCH v4 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              |  217 +++++++++++++++++++++++++++++++++------
 4 files changed, 224 insertions(+), 44 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -0
index 6212575..ecf8430 100644
@@ -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
index 7a41878..f3e7bb3 100644
@@ -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
index e10ec0e..bae2c11 100644
@@ -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 +185 -32

254 lines not on this page · raw message

Signature · 1 line
1.7.10
#9 lore raw

Re: [PATCHv4 4/5] KVM: emulator: make linearize() callable from outside of emulator

In reply to Gleb Natapov
Quoted 46 lines · Gleb Natapov

The 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 -8
index 7c276ca..9ce651b 100644
@@ -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;
+};
+
@@ -470,14 +470,6 @@ static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
 	ctxt->seg_override = seg;
 }
 
-static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
-{
-	if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
-		return 0;
-
-	return ctxt->ops->get_cached_segment_base(ctxt, seg);
-}
-
 
+int x86_linearize(struct x86_linearize_params *p, ulong *linear)
 {

arch/x86/include/asm/kvm_emulate.h static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)

Quoted 10 lines
arch/x86/include/asm/kvm_emulate.h +1 -5
-	struct desc_struct desc;
-	bool usable;
 	ulong la;
 	u32 lim;
-	u16 sel;
 	unsigned cpl, rpl;
 
-	la = seg_base(ctxt, addr.seg) + addr.ea;
-	switch (ctxt->mode) {
+	la = get_desc_base(&p->desc) + p->ea;

This makes 64-bit mode slower, since before the patch it avoided reading the segment base for non-fs/gs segments, and only read the segment base for fs/gs. After the patch we always execute 4 VMREADs (and decode the results).

Signature · 1 line
error compiling committee.c: too many arguments to function
#14 lore raw

Re: [PATCHv4 5/5] KVM: Provide fast path for "rep ins" emulation if possible.

In reply to Gleb Natapov
Quoted 12 lines · 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/kvm/x86.c +5 -0
+
+int kvm_fast_string_pio_in(struct kvm_vcpu *vcpu, int size,
+		unsigned short port, u8 ad_bytes_idx)
+{
+	unsigned long rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);

arch/x86/kvm/x86.c int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port) · line 4782

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+	unsigned long linear_addr;
+	unsigned long rcx = kvm_register_read(vcpu, VCPU_REGS_RCX), count;
+	u8 ad_bytes;
+	int r;
+
+	if (ad_bytes_idx > 2)
+		return EMULATE_FAIL;
+
+	ad_bytes = (u8[]){2, 4, 8}[ad_bytes_idx];
+

2 << logs2_ad_bytes instead of this nice functional programming style.

Quoted 38 lines
arch/x86/kvm/x86.c +38 -0
+	rcx = kvm_address_mask(ad_bytes, rcx);
+
+	if (rcx == 0) {
+		kvm_x86_ops->skip_emulated_instruction(vcpu);
+		return EMULATE_DONE;
+	}
+
+	if (kvm_get_rflags(vcpu) & X86_EFLAGS_DF)
+		return EMULATE_FAIL;
+
+	rdi = kvm_address_mask(ad_bytes, rdi);
+
+	r = kvm_linearize_address(vcpu, get_emulation_mode(vcpu),
+			rdi, VCPU_SREG_ES, rcx * size, true, false, ad_bytes,
+			&linear_addr);
+
+	if (r >= 0)
+		return EMULATE_FAIL;
+
+	count = (PAGE_SIZE - offset_in_page(linear_addr))/size;
+
+	if (count == 0) /* 'in' crosses page boundry */
+		return EMULATE_FAIL;
+
+	count = min(count, rcx);
+
+	r = __kvm_fast_string_pio_in(vcpu, size, port, linear_addr, count);
+
+	if (r != EMULATE_DO_MMIO)
+		return r;
+
+	vcpu->arch.fast_string_pio_ctxt.linear_addr = linear_addr;
+	vcpu->arch.fast_string_pio_ctxt.ad_bytes = ad_bytes;
+	vcpu->arch.complete_userspace_io = complete_fast_string_pio;
+	return EMULATE_DO_MMIO;
+}
+EXPORT_SYMBOL_GPL(kvm_fast_string_pio_in);
+
Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Re: [PATCHv4 3/5] KVM: emulator: move some address manipulation function out of emulator code.

In reply to Gleb Natapov
Quoted 68 lines · Gleb Natapov

The 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
index de2aff8..6212575 100644
@@ -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 +1 -9
index 79899df..e317588 100644
@@ -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

arch/x86/kvm/emulate.c register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) · line 453

Quoted 9 lines
arch/x86/kvm/emulate.c +1 -4
@@ -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);
 }

All those exports suggest it's better to move the fast path into the emulator. Suppose we change register access to use callbacks instead of bulk load/save, could we reuse the exising code?

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [PATCHv4 3/5] KVM: emulator: move some address manipulation function out of emulator code.

Quoted 67 lines · Avi Kivity
Quoted 66 lines · Gleb Natapov

The 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
index de2aff8..6212575 100644
@@ -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 +1 -9
index 79899df..e317588 100644
@@ -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);
 }
 
Quoted 14 lines

arch/x86/kvm/emulate.c register_address(struct x86_emulate_ctxt *ctxt, unsigned long reg) · line 453

Quoted 11 lines
arch/x86/kvm/emulate.c +1 -4
 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);
 }

All those exports suggest it's better to move the fast path into the emulator.

We've already being through that. Putting the code into emulator gives us nothing unless it also works on emulator context and working on partially initialized emulator context is first dangerous and second slower.

Quoted 3 lines

Suppose we change register access to use callbacks instead of
bulk load/save, could we reuse the exising code?

I do not see that problem. This helper function do basic arithmetics on three values. The value itself will be fetched on demand by the emulator.

--
Gleb.

#10 lore raw

Re: [PATCHv4 4/5] KVM: emulator: make linearize() callable from outside of emulator

In reply to Avi Kivity
Quoted 54 lines · Avi Kivity
Quoted 53 lines · Gleb Natapov

The 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 -11
index 7c276ca..9ce651b 100644
@@ -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;
+};
+
@@ -470,14 +470,6 @@ static void set_seg_override(struct x86_emulate_ctxt *ctxt, int seg)
 	ctxt->seg_override = seg;
 }
 
-static unsigned long seg_base(struct x86_emulate_ctxt *ctxt, int seg)
-{
-	if (ctxt->mode == X86EMUL_MODE_PROT64 && seg < VCPU_SREG_FS)
-		return 0;
-
-	return ctxt->ops->get_cached_segment_base(ctxt, seg);
-}
-
 
+int x86_linearize(struct x86_linearize_params *p, ulong *linear)
 {
-	struct desc_struct desc;
-	bool usable;
 	ulong la;
 	u32 lim;
-	u16 sel;
 	unsigned cpl, rpl;
Quoted 9 lines

arch/x86/include/asm/kvm_emulate.h

Quoted 3 lines
arch/x86/include/asm/kvm_emulate.h +1 -2
-	la = seg_base(ctxt, addr.seg) + addr.ea;
-	switch (ctxt->mode) {
+	la = get_desc_base(&p->desc) + p->ea;

This makes 64-bit mode slower, since before the patch it avoided reading the segment base for non-fs/gs segments, and only read the segment base for fs/gs. After the patch we always execute 4 VMREADs (and decode the results).

That's easy to fix by making caller prepare fake desc if mode is 64-bit and segment is non-fs/gs. The question is if this even measurable?

--
Gleb.

#15 lore raw

Re: [PATCHv4 5/5] KVM: Provide fast path for "rep ins" emulation if possible.

In reply to Avi Kivity
Quoted 17 lines · Avi Kivity
Quoted 16 lines · 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/kvm/x86.c +9 -0
+
+int kvm_fast_string_pio_in(struct kvm_vcpu *vcpu, int size,
+		unsigned short port, u8 ad_bytes_idx)
+{
+	unsigned long rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
+	unsigned long linear_addr;
+	unsigned long rcx = kvm_register_read(vcpu, VCPU_REGS_RCX), count;
+	u8 ad_bytes;
+	int r;
Quoted 9 lines

arch/x86/kvm/x86.c int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port) · line 4782

Quoted 6 lines
arch/x86/kvm/x86.c +6 -0
+
+	if (ad_bytes_idx > 2)
+		return EMULATE_FAIL;
+
+	ad_bytes = (u8[]){2, 4, 8}[ad_bytes_idx];
+

2 << logs2_ad_bytes instead of this nice functional programming style.

Heh, my way looks cooler :)

Quoted 30 lines
Quoted 30 lines
arch/x86/kvm/x86.c +30 -0
+	rcx = kvm_address_mask(ad_bytes, rcx);
+
+	if (rcx == 0) {
+		kvm_x86_ops->skip_emulated_instruction(vcpu);
+		return EMULATE_DONE;
+	}
+
+	if (kvm_get_rflags(vcpu) & X86_EFLAGS_DF)
+		return EMULATE_FAIL;
+
+	rdi = kvm_address_mask(ad_bytes, rdi);
+
+	r = kvm_linearize_address(vcpu, get_emulation_mode(vcpu),
+			rdi, VCPU_SREG_ES, rcx * size, true, false, ad_bytes,
+			&linear_addr);
+
+	if (r >= 0)
+		return EMULATE_FAIL;
+
+	count = (PAGE_SIZE - offset_in_page(linear_addr))/size;
+
+	if (count == 0) /* 'in' crosses page boundry */
+		return EMULATE_FAIL;
+
+	count = min(count, rcx);
+
+	r = __kvm_fast_string_pio_in(vcpu, size, port, linear_addr, count);
+
+	if (r != EMULATE_DO_MMIO)
+		return r;
Quoted 12 lines
Quoted 8 lines
arch/x86/kvm/x86.c +8 -0
+
+	vcpu->arch.fast_string_pio_ctxt.linear_addr = linear_addr;
+	vcpu->arch.fast_string_pio_ctxt.ad_bytes = ad_bytes;
+	vcpu->arch.complete_userspace_io = complete_fast_string_pio;
+	return EMULATE_DO_MMIO;
+}
+EXPORT_SYMBOL_GPL(kvm_fast_string_pio_in);
+
Signature · 1 line
error compiling committee.c: too many arguments to function

--
Gleb.

#11 lore raw

Re: [PATCHv4 4/5] KVM: emulator: make linearize() callable from outside of emulator

In reply to Gleb Natapov
Quoted 18 lines · Gleb Natapov
Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/emulate.c +1 -3
+int x86_linearize(struct x86_linearize_params *p, ulong *linear)
 {
-	struct desc_struct desc;
-	bool usable;
 	ulong la;
 	u32 lim;
-	u16 sel;
 	unsigned cpl, rpl;
Quoted 9 lines

arch/x86/kvm/emulate.c static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) · line 569

Quoted 3 lines
arch/x86/kvm/emulate.c +1 -2
-	la = seg_base(ctxt, addr.seg) + addr.ea;
-	switch (ctxt->mode) {
+	la = get_desc_base(&p->desc) + p->ea;

This makes 64-bit mode slower, since before the patch it avoided reading the segment base for non-fs/gs segments, and only read the segment base for fs/gs. After the patch we always execute 4 VMREADs (and decode the results).

Quoted 2 lines

That's easy to fix by making caller prepare fake desc if mode is 64-bit and segment is non-fs/gs. The question is if this even measurable?

I'm sure it will be measurable, esp. on older processors. Why not measure it?

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw

Re: [PATCHv4 4/5] KVM: emulator: make linearize() callable from outside of emulator

Quoted 20 lines · Avi Kivity
Quoted 18 lines · Gleb Natapov
Quoted 18 lines

arch/x86/kvm/emulate.c static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size) · line 569

Quoted 12 lines
arch/x86/kvm/emulate.c +2 -5
+int x86_linearize(struct x86_linearize_params *p, ulong *linear)
 {
-	struct desc_struct desc;
-	bool usable;
 	ulong la;
 	u32 lim;
-	u16 sel;
 	unsigned cpl, rpl;
 
-	la = seg_base(ctxt, addr.seg) + addr.ea;
-	switch (ctxt->mode) {
+	la = get_desc_base(&p->desc) + p->ea;

This makes 64-bit mode slower, since before the patch it avoided reading the segment base for non-fs/gs segments, and only read the segment base for fs/gs. After the patch we always execute 4 VMREADs (and decode the results).

Quoted 7 lines
Quoted 2 lines

That's easy to fix by making caller prepare fake desc if mode is 64-bit and segment is non-fs/gs. The question is if this even measurable?

I'm sure it will be measurable, esp. on older processors. Why not measure it?

It is easier to just fix it :) Will do and resend if you are agree with general approach.

--
Gleb.

#6 lore raw

Re: [PATCHv4 3/5] KVM: emulator: move some address manipulation function out of emulator code.

In reply to Gleb Natapov
Quoted 7 lines · Gleb Natapov
Quoted 2 lines

All those exports suggest it's better to move the fast path into the emulator.

We've already being through that. Putting the code into emulator gives us nothing unless it also works on emulator context and working on partially initialized emulator context is first dangerous and second slower.

We can make it work on an x86_pio_ctxt. What it gives us is common code. (x86_emulate_ctxt can include a x86_pio_ctxt when it does pio).

Quoted 6 lines
Quoted 4 lines

Suppose we change register access to use callbacks instead of
bulk load/save, could we reuse the exising code?

I do not see that problem. This helper function do basic arithmetics on three values. The value itself will be fetched on demand by the emulator.

I meant to reduce the emulator initialization cost, so it isn't slow.

btw, I'm guessing that the main speedup comes not from avoiding the decode, but by doing page-at-a-time instead of word-at-a-time.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

Re: [PATCHv4 3/5] KVM: emulator: move some address manipulation function out of emulator code.

Quoted 4 lines · Avi Kivity
Quoted 3 lines · Gleb Natapov
Quoted 2 lines

All those exports suggest it's better to move the fast path into the emulator.

Quoted 8 lines
Quoted 4 lines

We've already being through that. Putting the code into emulator gives us nothing unless it also works on emulator context and working on partially initialized emulator context is first dangerous and second slower.

We can make it work on an x86_pio_ctxt. What it gives us is common code. (x86_emulate_ctxt can include a x86_pio_ctxt when it does pio).

My patches do similar thing, but instead of x86_pio_ctxt they use x86_linearize_ctx. The code is common.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Suppose we change register access to use callbacks instead of
bulk load/save, could we reuse the exising code?

Quoted 8 lines
Quoted 2 lines

I do not see that problem. This helper function do basic arithmetics on three values. The value itself will be fetched on demand by the emulator.

I meant to reduce the emulator initialization cost, so it isn't slow.

btw, I'm guessing that the main speedup comes not from avoiding the decode, but by doing page-at-a-time instead of word-at-a-time.

And hacking emulator doing page at a time is not trivial an complicates it for no good reason. BTW you were the one who suggested implementing fast pio as a separate code path.

--
Gleb.