mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Avi Kivity <avi@qumranet.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 48/50] KVM: x86 emulator: lazily evaluate segment registers
Date: Thu, 26 Jun 2008 15:28:30 +0300	[thread overview]
Message-ID: <1214483312-9265-49-git-send-email-avi@qumranet.com> (raw)
In-Reply-To: <1214483312-9265-1-git-send-email-avi@qumranet.com>

Instead of prefetching all segment bases before emulation, read them at the
last moment.  Since most of them are unneeded, we save some cycles on
Intel machines where this is a bit expensive.

Signed-off-by: Avi Kivity <avi@qumranet.com>
---
 arch/x86/kvm/x86.c                |   21 --------
 arch/x86/kvm/x86_emulate.c        |   96 ++++++++++++++++++++++---------------
 include/asm-x86/kvm_x86_emulate.h |   10 +---
 3 files changed, 60 insertions(+), 67 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ccb4e1e..0fbc032 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2126,27 +2126,6 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 			? X86EMUL_MODE_PROT64 :	cs_db
 			? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
 
-		if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
-			vcpu->arch.emulate_ctxt.cs_base = 0;
-			vcpu->arch.emulate_ctxt.ds_base = 0;
-			vcpu->arch.emulate_ctxt.es_base = 0;
-			vcpu->arch.emulate_ctxt.ss_base = 0;
-		} else {
-			vcpu->arch.emulate_ctxt.cs_base =
-					get_segment_base(vcpu, VCPU_SREG_CS);
-			vcpu->arch.emulate_ctxt.ds_base =
-					get_segment_base(vcpu, VCPU_SREG_DS);
-			vcpu->arch.emulate_ctxt.es_base =
-					get_segment_base(vcpu, VCPU_SREG_ES);
-			vcpu->arch.emulate_ctxt.ss_base =
-					get_segment_base(vcpu, VCPU_SREG_SS);
-		}
-
-		vcpu->arch.emulate_ctxt.gs_base =
-					get_segment_base(vcpu, VCPU_SREG_GS);
-		vcpu->arch.emulate_ctxt.fs_base =
-					get_segment_base(vcpu, VCPU_SREG_FS);
-
 		r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
 
 		/* Reject the instructions other than VMCALL/VMMCALL when
diff --git a/arch/x86/kvm/x86_emulate.c b/arch/x86/kvm/x86_emulate.c
index 38926b7..18ca25c 100644
--- a/arch/x86/kvm/x86_emulate.c
+++ b/arch/x86/kvm/x86_emulate.c
@@ -522,6 +522,39 @@ static inline void jmp_rel(struct decode_cache *c, int rel)
 	register_address_increment(c, &c->eip, rel);
 }
 
+static void set_seg_override(struct decode_cache *c, int seg)
+{
+	c->has_seg_override = true;
+	c->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 kvm_x86_ops->get_segment_base(ctxt->vcpu, seg);
+}
+
+static unsigned long seg_override_base(struct x86_emulate_ctxt *ctxt,
+				       struct decode_cache *c)
+{
+	if (!c->has_seg_override)
+		return 0;
+
+	return seg_base(ctxt, c->seg_override);
+}
+
+static unsigned long es_base(struct x86_emulate_ctxt *ctxt)
+{
+	return seg_base(ctxt, VCPU_SREG_ES);
+}
+
+static unsigned long ss_base(struct x86_emulate_ctxt *ctxt)
+{
+	return seg_base(ctxt, VCPU_SREG_SS);
+}
+
 static int do_fetch_insn_byte(struct x86_emulate_ctxt *ctxt,
 			      struct x86_emulate_ops *ops,
 			      unsigned long linear, u8 *dest)
@@ -735,8 +768,8 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt,
 		}
 		if (c->modrm_rm == 2 || c->modrm_rm == 3 ||
 		    (c->modrm_rm == 6 && c->modrm_mod != 0))
-			if (!c->override_base)
-				c->override_base = &ctxt->ss_base;
+			if (!c->has_seg_override)
+				set_seg_override(c, VCPU_SREG_SS);
 		c->modrm_ea = (u16)c->modrm_ea;
 	} else {
 		/* 32/64-bit ModR/M decode. */
@@ -807,6 +840,7 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 
 	memset(c, 0, sizeof(struct decode_cache));
 	c->eip = ctxt->vcpu->arch.rip;
+	ctxt->cs_base = seg_base(ctxt, VCPU_SREG_CS);
 	memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
 
 	switch (mode) {
@@ -845,23 +879,15 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 				/* switch between 2/4 bytes */
 				c->ad_bytes = def_ad_bytes ^ 6;
 			break;
+		case 0x26:	/* ES override */
 		case 0x2e:	/* CS override */
-			c->override_base = &ctxt->cs_base;
-			break;
+		case 0x36:	/* SS override */
 		case 0x3e:	/* DS override */
-			c->override_base = &ctxt->ds_base;
-			break;
-		case 0x26:	/* ES override */
-			c->override_base = &ctxt->es_base;
+			set_seg_override(c, (c->b >> 3) & 3);
 			break;
 		case 0x64:	/* FS override */
-			c->override_base = &ctxt->fs_base;
-			break;
 		case 0x65:	/* GS override */
-			c->override_base = &ctxt->gs_base;
-			break;
-		case 0x36:	/* SS override */
-			c->override_base = &ctxt->ss_base;
+			set_seg_override(c, c->b & 7);
 			break;
 		case 0x40 ... 0x4f: /* REX */
 			if (mode != X86EMUL_MODE_PROT64)
@@ -933,15 +959,11 @@ done_prefixes:
 	if (rc)
 		goto done;
 
-	if (!c->override_base)
-		c->override_base = &ctxt->ds_base;
-	if (mode == X86EMUL_MODE_PROT64 &&
-	    c->override_base != &ctxt->fs_base &&
-	    c->override_base != &ctxt->gs_base)
-		c->override_base = NULL;
+	if (!c->has_seg_override)
+		set_seg_override(c, VCPU_SREG_DS);
 
-	if (c->override_base && !(!c->twobyte && c->b == 0x8d))
-		c->modrm_ea += *c->override_base;
+	if (!(!c->twobyte && c->b == 0x8d))
+		c->modrm_ea += seg_override_base(ctxt, c);
 
 	if (c->ad_bytes != 8)
 		c->modrm_ea = (u32)c->modrm_ea;
@@ -1043,7 +1065,7 @@ static inline void emulate_push(struct x86_emulate_ctxt *ctxt)
 	c->dst.bytes = c->op_bytes;
 	c->dst.val = c->src.val;
 	register_address_increment(c, &c->regs[VCPU_REGS_RSP], -c->op_bytes);
-	c->dst.ptr = (void *) register_address(c, ctxt->ss_base,
+	c->dst.ptr = (void *) register_address(c, ss_base(ctxt),
 					       c->regs[VCPU_REGS_RSP]);
 }
 
@@ -1053,7 +1075,7 @@ static inline int emulate_grp1a(struct x86_emulate_ctxt *ctxt,
 	struct decode_cache *c = &ctxt->decode;
 	int rc;
 
-	rc = ops->read_std(register_address(c, ctxt->ss_base,
+	rc = ops->read_std(register_address(c, ss_base(ctxt),
 					    c->regs[VCPU_REGS_RSP]),
 			   &c->dst.val, c->dst.bytes, ctxt->vcpu);
 	if (rc != 0)
@@ -1375,11 +1397,11 @@ special_insn:
 		register_address_increment(c, &c->regs[VCPU_REGS_RSP],
 					   -c->op_bytes);
 		c->dst.ptr = (void *) register_address(
-			c, ctxt->ss_base, c->regs[VCPU_REGS_RSP]);
+			c, ss_base(ctxt), c->regs[VCPU_REGS_RSP]);
 		break;
 	case 0x58 ... 0x5f: /* pop reg */
 	pop_instruction:
-		if ((rc = ops->read_std(register_address(c, ctxt->ss_base,
+		if ((rc = ops->read_std(register_address(c, ss_base(ctxt),
 			c->regs[VCPU_REGS_RSP]), c->dst.ptr,
 			c->op_bytes, ctxt->vcpu)) != 0)
 			goto done;
@@ -1405,7 +1427,7 @@ special_insn:
 				c->rep_prefix ?
 				address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
 				(ctxt->eflags & EFLG_DF),
-				register_address(c, ctxt->es_base,
+				register_address(c, es_base(ctxt),
 						 c->regs[VCPU_REGS_RDI]),
 				c->rep_prefix,
 				c->regs[VCPU_REGS_RDX]) == 0) {
@@ -1421,9 +1443,8 @@ special_insn:
 				c->rep_prefix ?
 				address_mask(c, c->regs[VCPU_REGS_RCX]) : 1,
 				(ctxt->eflags & EFLG_DF),
-				register_address(c, c->override_base ?
-							*c->override_base :
-							ctxt->ds_base,
+					 register_address(c,
+					  seg_override_base(ctxt, c),
 						 c->regs[VCPU_REGS_RSI]),
 				c->rep_prefix,
 				c->regs[VCPU_REGS_RDX]) == 0) {
@@ -1559,11 +1580,10 @@ special_insn:
 		c->dst.type = OP_MEM;
 		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
 		c->dst.ptr = (unsigned long *)register_address(c,
-						   ctxt->es_base,
+						   es_base(ctxt),
 						   c->regs[VCPU_REGS_RDI]);
 		if ((rc = ops->read_emulated(register_address(c,
-		      c->override_base ? *c->override_base :
-					ctxt->ds_base,
+					   seg_override_base(ctxt, c),
 					c->regs[VCPU_REGS_RSI]),
 					&c->dst.val,
 					c->dst.bytes, ctxt->vcpu)) != 0)
@@ -1579,8 +1599,7 @@ special_insn:
 		c->src.type = OP_NONE; /* Disable writeback. */
 		c->src.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
 		c->src.ptr = (unsigned long *)register_address(c,
-				c->override_base ? *c->override_base :
-						   ctxt->ds_base,
+				       seg_override_base(ctxt, c),
 						   c->regs[VCPU_REGS_RSI]);
 		if ((rc = ops->read_emulated((unsigned long)c->src.ptr,
 						&c->src.val,
@@ -1591,7 +1610,7 @@ special_insn:
 		c->dst.type = OP_NONE; /* Disable writeback. */
 		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
 		c->dst.ptr = (unsigned long *)register_address(c,
-						   ctxt->es_base,
+						   es_base(ctxt),
 						   c->regs[VCPU_REGS_RDI]);
 		if ((rc = ops->read_emulated((unsigned long)c->dst.ptr,
 						&c->dst.val,
@@ -1615,7 +1634,7 @@ special_insn:
 		c->dst.type = OP_MEM;
 		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
 		c->dst.ptr = (unsigned long *)register_address(c,
-						   ctxt->es_base,
+						   es_base(ctxt),
 						   c->regs[VCPU_REGS_RDI]);
 		c->dst.val = c->regs[VCPU_REGS_RAX];
 		register_address_increment(c, &c->regs[VCPU_REGS_RDI],
@@ -1627,8 +1646,7 @@ special_insn:
 		c->dst.bytes = (c->d & ByteOp) ? 1 : c->op_bytes;
 		c->dst.ptr = (unsigned long *)&c->regs[VCPU_REGS_RAX];
 		if ((rc = ops->read_emulated(register_address(c,
-				c->override_base ? *c->override_base :
-						   ctxt->ds_base,
+						 seg_override_base(ctxt, c),
 						 c->regs[VCPU_REGS_RSI]),
 						 &c->dst.val,
 						 c->dst.bytes,
diff --git a/include/asm-x86/kvm_x86_emulate.h b/include/asm-x86/kvm_x86_emulate.h
index 9fda4b3..4e8c1e4 100644
--- a/include/asm-x86/kvm_x86_emulate.h
+++ b/include/asm-x86/kvm_x86_emulate.h
@@ -124,7 +124,8 @@ struct decode_cache {
 	u8 rex_prefix;
 	struct operand src;
 	struct operand dst;
-	unsigned long *override_base;
+	bool has_seg_override;
+	u8 seg_override;
 	unsigned int d;
 	unsigned long regs[NR_VCPU_REGS];
 	unsigned long eip;
@@ -151,12 +152,7 @@ struct x86_emulate_ctxt {
 	/* Emulated execution mode, represented by an X86EMUL_MODE value. */
 	int mode;
 
-	unsigned long cs_base;
-	unsigned long ds_base;
-	unsigned long es_base;
-	unsigned long ss_base;
-	unsigned long gs_base;
-	unsigned long fs_base;
+	u32 cs_base;
 
 	/* decode cache */
 
-- 
1.5.6


  parent reply	other threads:[~2008-06-26 12:35 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-26 12:27 [PATCH 00/50] KVM patches review for the 2.6.27 merge window Avi Kivity
2008-06-26 12:27 ` [PATCH 01/50] KVM: remove long -> void *user -> long cast Avi Kivity
2008-06-26 12:27 ` [PATCH 02/50] KVM: add statics were possible, function definition in lapic.h Avi Kivity
2008-06-26 12:27 ` [PATCH 03/50] KVM: VMX: move APIC_ACCESS trace entry to generic code Avi Kivity
2008-06-26 12:27 ` [PATCH 04/50] KVM: SVM: implement dedicated NMI exit handler Avi Kivity
2008-06-26 12:27 ` [PATCH 05/50] KVM: SVM: implement dedicated INTR " Avi Kivity
2008-06-26 12:27 ` [PATCH 06/50] KVM: add missing kvmtrace bits Avi Kivity
2008-06-26 12:27 ` [PATCH 07/50] KVM: SVM: add missing kvmtrace markers Avi Kivity
2008-06-26 12:27 ` [PATCH 08/50] KVM: SVM: add tracing support for TDP page faults Avi Kivity
2008-06-26 12:27 ` [PATCH 09/50] KVM: Handle vma regions with no backing page Avi Kivity
2008-06-26 12:27 ` [PATCH 10/50] KVM: PIT: support mode 3 Avi Kivity
2008-06-26 12:27 ` [PATCH 11/50] KVM: SVM: Fake MSR_K7 performance counters Avi Kivity
2008-06-26 12:27 ` [PATCH 12/50] KVM: VMX: Trivial vmcs_write64() code simplification Avi Kivity
2008-06-26 12:27 ` [PATCH 13/50] KVM: MMU: Fix false flooding when a pte points to page table Avi Kivity
2008-06-26 12:27 ` [PATCH 14/50] KVM: Handle virtualization instruction #UD faults during reboot Avi Kivity
2008-06-26 12:27 ` [PATCH 15/50] KVM: VMX: Add list of potentially locally cached vcpus Avi Kivity
2008-06-26 12:27 ` [PATCH 16/50] KVM: Remove decache_vcpus_on_cpu() and related callbacks Avi Kivity
2008-06-26 12:27 ` [PATCH 17/50] KVM: Remove unnecessary ->decache_regs() call Avi Kivity
2008-06-26 12:28 ` [PATCH 18/50] KVM: IOAPIC/LAPIC: Enable NMI support Avi Kivity
2008-06-26 12:28 ` [PATCH 19/50] KVM: VMX: Enable NMI with in-kernel irqchip Avi Kivity
2008-06-26 12:28 ` [PATCH 20/50] KVM: Order segment register constants in the same way as cpu operand encoding Avi Kivity
2008-06-26 12:28 ` [PATCH 21/50] KVM: MTRR support Avi Kivity
2008-06-26 12:28 ` [PATCH 22/50] KVM: Prefixes segment functions that will be exported with "kvm_" Avi Kivity
2008-06-26 12:28 ` [PATCH 23/50] KVM: x86 emulator: Update c->dst.bytes in decode instruction Avi Kivity
2008-06-26 12:28 ` [PATCH 24/50] KVM: x86 emulator: add support for jmp far 0xea Avi Kivity
2008-06-26 12:28 ` [PATCH 25/50] KVM: x86 emulator: adds support to mov r,imm (opcode 0xb8) instruction Avi Kivity
2008-06-26 12:28 ` [PATCH 26/50] KVM: x86 emulator: Add support for mov seg, r (0x8e) instruction Avi Kivity
2008-06-26 12:28 ` [PATCH 27/50] KVM: x86 emulator: Add support for mov r, sreg (0x8c) instruction Avi Kivity
2008-06-26 12:28 ` [PATCH 28/50] KVM: MMU: Optimize prefetch_page() Avi Kivity
2008-06-26 12:28 ` [PATCH 29/50] KVM: x86 emulator: simplify push imm8 emulation Avi Kivity
2008-06-26 12:28 ` [PATCH 30/50] KVM: x86 emulator: implement 'push imm' (opcode 0x68) Avi Kivity
2008-06-26 12:28 ` [PATCH 31/50] KVM: MMU: Move nonpaging_prefetch_page() Avi Kivity
2008-06-26 12:28 ` [PATCH 32/50] KVM: MMU: Avoid page prefetch on SVM Avi Kivity
2008-06-26 12:28 ` [PATCH 33/50] KVM: kvm_io_device: extend in_range() to manage len and write attribute Avi Kivity
2008-06-26 12:28 ` [PATCH 34/50] KVM: Add coalesced MMIO support (common part) Avi Kivity
2008-06-26 12:28 ` [PATCH 35/50] KVM: Add coalesced MMIO support (x86 part) Avi Kivity
2008-06-26 12:28 ` [PATCH 36/50] KVM: Add coalesced MMIO support (powerpc part) Avi Kivity
2008-06-26 12:28 ` [PATCH 37/50] KVM: Add coalesced MMIO support (ia64 part) Avi Kivity
2008-06-26 12:28 ` [PATCH 38/50] KVM: only abort guest entry if timer count goes from 0->1 Avi Kivity
2008-06-26 12:28 ` [PATCH 39/50] KVM: Do not calculate linear rip in emulation failure report Avi Kivity
2008-06-26 12:28 ` [PATCH 40/50] KVM: Support mixed endian machines Avi Kivity
2008-06-26 12:28 ` [PATCH 41/50] KVM: Use printk_rlimit() instead of reporting emulation failures just once Avi Kivity
2008-06-26 12:28 ` [PATCH 42/50] KVM: x86 emulator: emulate nop and xchg reg, acc (opcodes 0x90 - 0x97) Avi Kivity
2008-06-26 12:28 ` [PATCH 43/50] KVM: x86 emulator: handle undecoded rex.b with r/m = 5 in certain cases Avi Kivity
2008-06-26 12:28 ` [PATCH 44/50] KVM: x86 emulator: simplify sib decoding Avi Kivity
2008-06-26 12:28 ` [PATCH 45/50] KVM: x86 emulator: simplify r/m decoding Avi Kivity
2008-06-26 12:28 ` [PATCH 46/50] KVM: x86 emulator: simplify rip relative decoding Avi Kivity
2008-06-26 12:28 ` [PATCH 47/50] KVM: x86 emulator: avoid segment base adjust for lea Avi Kivity
2008-06-26 12:28 ` Avi Kivity [this message]
2008-06-26 12:28 ` [PATCH 49/50] KVM: MMU: When debug is enabled, make it a run-time parameter Avi Kivity
2008-06-26 12:28 ` [PATCH 50/50] KVM: MMU: Fix printk format Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1214483312-9265-49-git-send-email-avi@qumranet.com \
    --to=avi@qumranet.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®