Skip to contentSkip to conversation

Decouple the x86 emulator from the rest of kvm

PATCH New Avi Kivity 1 participant 17 messages Apr 20, 2011

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Decouple the x86 emulator from the rest of kvm

This (longer than expected) patchset decouples the x86 emulator from the rest of kvm. All communication is not done via x86_emulate_ctxt fields and callbacks; there is no access to ctxt->vcpu (which is eliminated by the last patch).

Avi Kivity (16):
KVM: x86 emulator: drop vcpu argument from memory read/write
callbacks
KVM: x86 emulator: drop vcpu argument from pio callbacks
KVM: x86 emulator: drop vcpu argument from segment/gdt/idt callbacks
KVM: x86 emulator: drop vcpu argument from cr/dr/cpl/msr callbacks
KVM: x86 emulator: drop vcpu argument from intercept callback
KVM: x86 emulator: avoid using ctxt->vcpu in check_perm() callbacks
KVM: x86 emulator: add and use new callbacks set_idt(), set_gdt()
KVM: x86 emulator: drop use of is_long_mode()
KVM: x86 emulator: Replace calls to is_pae() and is_paging with

    ->get_cr()

KVM: x86 emulator: emulate CLTS internally
KVM: x86 emulator: make emulate_invlpg() an emulator callback
KVM: x86 emulator: add new ->halt() callback
KVM: x86 emulator: add ->fix_hypercall() callback
KVM: x86 emulator: add new ->wbinvd() callback
KVM: Avoid using x86_emulate_ctxt.vcpu
KVM: x86 emulator: drop x86_emulate_ctxt::vcpu

 arch/x86/include/asm/kvm_emulate.h |   90 ++++++-----
 arch/x86/include/asm/kvm_host.h    |    7 -
 arch/x86/kvm/emulate.c             |  321 +++++++++++++++++++-----------------
 arch/x86/kvm/x86.c                 |  203 ++++++++++++++---------
 4 files changed, 338 insertions(+), 283 deletions(-)
author
#1 lore raw

PATCH 1/16 KVM: x86 emulator: drop vcpu argument from memory read/write callbacks

Making the emulator caller agnostic.

---
 arch/x86/include/asm/kvm_emulate.h |   34 ++++++++++------------
 arch/x86/kvm/emulate.c             |   54 ++++++++++++++++-------------------
 arch/x86/kvm/x86.c                 |   54 ++++++++++++++++++++++-------------
 3 files changed, 75 insertions(+), 67 deletions(-)
arch/x86/include/asm/kvm_emulate.h +16 -18
index 9b760c8..b4d8467 100644
@@ -92,8 +92,9 @@ struct x86_emulate_ops {
 	 *  @val:   [OUT] Value read from memory, zero-extended to 'u_long'.
 	 *  @bytes: [IN ] Number of bytes to read from memory.
 	 */
-	int (*read_std)(unsigned long addr, void *val,
-			unsigned int bytes, struct kvm_vcpu *vcpu,
+	int (*read_std)(struct x86_emulate_ctxt *ctxt,
+			unsigned long addr, void *val,
+			unsigned int bytes,
 			struct x86_exception *fault);
 
 	/*
@@ -103,8 +104,8 @@ struct x86_emulate_ops {
 	 *  @val:   [OUT] Value write to memory, zero-extended to 'u_long'.
 	 *  @bytes: [IN ] Number of bytes to write to memory.
 	 */
-	int (*write_std)(unsigned long addr, void *val,
-			 unsigned int bytes, struct kvm_vcpu *vcpu,
+	int (*write_std)(struct x86_emulate_ctxt *ctxt,
+			 unsigned long addr, void *val, unsigned int bytes,
 			 struct x86_exception *fault);
 	/*
 	 * fetch: Read bytes of standard (non-emulated/special) memory.
@@ -113,8 +114,8 @@ struct x86_emulate_ops {
 	 *  @val:   [OUT] Value read from memory, zero-extended to 'u_long'.
 	 *  @bytes: [IN ] Number of bytes to read from memory.
 	 */
-	int (*fetch)(unsigned long addr, void *val,
-		     unsigned int bytes, struct kvm_vcpu *vcpu,
+	int (*fetch)(struct x86_emulate_ctxt *ctxt,
+		     unsigned long addr, void *val, unsigned int bytes,
 		     struct x86_exception *fault);
 
 	/*
@@ -123,11 +124,9 @@ struct x86_emulate_ops {
 	 *  @val:   [OUT] Value read from memory, zero-extended to 'u_long'.
 	 *  @bytes: [IN ] Number of bytes to read from memory.
 	 */
-	int (*read_emulated)(unsigned long addr,
-			     void *val,
-			     unsigned int bytes,
-			     struct x86_exception *fault,
-			     struct kvm_vcpu *vcpu);
+	int (*read_emulated)(struct x86_emulate_ctxt *ctxt,
+			     unsigned long addr, void *val, unsigned int bytes,
+			     struct x86_exception *fault);
 
 	/*
 	 * write_emulated: Write bytes to emulated/special memory area.
@@ -136,11 +135,10 @@ struct x86_emulate_ops {
 	 *                required).
 	 *  @bytes: [IN ] Number of bytes to write to memory.
 	 */
-	int (*write_emulated)(unsigned long addr,
-			      const void *val,
+	int (*write_emulated)(struct x86_emulate_ctxt *ctxt,
+			      unsigned long addr, const void *val,
 			      unsigned int bytes,
-			      struct x86_exception *fault,
-			      struct kvm_vcpu *vcpu);
+			      struct x86_exception *fault);
 
 	/*
 	 * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an
@@ -150,12 +148,12 @@ struct x86_emulate_ops {
 	 *  @new:   [IN ] Value to write to @addr.
 	 *  @bytes: [IN ] Number of bytes to access using CMPXCHG.
 	 */
-	int (*cmpxchg_emulated)(unsigned long addr,
+	int (*cmpxchg_emulated)(struct x86_emulate_ctxt *ctxt,
+				unsigned long addr,
 				const void *old,
 				const void *new,
 				unsigned int bytes,
-				struct x86_exception *fault,
-				struct kvm_vcpu *vcpu);
+				struct x86_exception *fault);
 
 	int (*pio_in_emulated)(int size, unsigned short port, void *val,
 			       unsigned int count, struct kvm_vcpu *vcpu);
arch/x86/kvm/emulate.c +25 -29

165 lines not on this page · raw message

arch/x86/kvm/x86.c +34 -20

135 lines not on this page · raw message

Signature · 1 line
1.7.1
author
#2 lore raw

PATCH 2/16 KVM: x86 emulator: drop vcpu argument from pio callbacks

In reply to Avi Kivity

Making the emulator caller agnostic.

---
 arch/x86/include/asm/kvm_emulate.h |   10 ++++++----
 arch/x86/kvm/emulate.c             |    6 +++---
 arch/x86/kvm/x86.c                 |   18 ++++++++++++------
 3 files changed, 21 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_emulate.h +6 -4
index b4d8467..1348bdf 100644
@@ -155,11 +155,13 @@ struct x86_emulate_ops {
 				unsigned int bytes,
 				struct x86_exception *fault);
 
-	int (*pio_in_emulated)(int size, unsigned short port, void *val,
-			       unsigned int count, struct kvm_vcpu *vcpu);
+	int (*pio_in_emulated)(struct x86_emulate_ctxt *ctxt,
+			       int size, unsigned short port, void *val,
+			       unsigned int count);
 
-	int (*pio_out_emulated)(int size, unsigned short port, const void *val,
-				unsigned int count, struct kvm_vcpu *vcpu);
+	int (*pio_out_emulated)(struct x86_emulate_ctxt *ctxt,
+				int size, unsigned short port, const void *val,
+				unsigned int count);
 
 	bool (*get_cached_descriptor)(struct desc_struct *desc, u32 *base3,
 				      int seg, struct kvm_vcpu *vcpu);
arch/x86/kvm/emulate.c +3 -3
index ff64b17..8af08a1 100644
@@ -1125,7 +1125,7 @@ static int pio_in_emulated(struct x86_emulate_ctxt *ctxt,
 		if (n == 0)
 			n = 1;
 		rc->pos = rc->end = 0;
-		if (!ops->pio_in_emulated(size, port, rc->data, n, ctxt->vcpu))
+		if (!ops->pio_in_emulated(ctxt, size, port, rc->data, n))
 			return 0;
 		rc->end = n * size;
 	}
@@ -3892,8 +3892,8 @@ special_insn:
 	case 0xef: /* out dx,(e/r)ax */
 		c->dst.val = c->regs[VCPU_REGS_RDX];
 	do_io_out:
-		ops->pio_out_emulated(c->src.bytes, c->dst.val,
-				      &c->src.val, 1, ctxt->vcpu);
+		ops->pio_out_emulated(ctxt, c->src.bytes, c->dst.val,
+				      &c->src.val, 1);
 		c->dst.type = OP_NONE;	/* Disable writeback. */
 		break;
 	case 0xf4:              /* hlt */
arch/x86/kvm/x86.c +12 -6
index 274652a..e9040a9 100644
@@ -4060,9 +4060,12 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
 }
 
 
-static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
-			     unsigned int count, struct kvm_vcpu *vcpu)
+static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
+				    int size, unsigned short port, void *val,
+				    unsigned int count)
 {
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
+
 	if (vcpu->arch.pio.count)
 		goto data_avail;
 
@@ -4090,10 +4093,12 @@ static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
 	return 0;
 }
 
-static int emulator_pio_out_emulated(int size, unsigned short port,
-			      const void *val, unsigned int count,
-			      struct kvm_vcpu *vcpu)
+static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
+				     int size, unsigned short port,
+				     const void *val, unsigned int count)
 {
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
+
 	trace_kvm_pio(1, port, size, count);
 
 	vcpu->arch.pio.port = port;
@@ -4614,7 +4619,8 @@ EXPORT_SYMBOL_GPL(x86_emulate_instruction);
 int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
 {
 	unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
-	int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu);
+	int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
+					    size, port, &val, 1);
 	/* do not return to emulator after return from userspace */
 	vcpu->arch.pio.count = 0;
 	return ret;
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH 3/16 KVM: x86 emulator: drop vcpu argument from segment/gdt/idt callbacks

In reply to Avi Kivity

Making the emulator caller agnostic.

---
 arch/x86/include/asm/kvm_emulate.h |   22 ++++---
 arch/x86/kvm/emulate.c             |  112 ++++++++++++++++++------------------
 arch/x86/kvm/x86.c                 |   39 +++++++------
 3 files changed, 90 insertions(+), 83 deletions(-)
arch/x86/include/asm/kvm_emulate.h +13 -9
index 1348bdf..656046a 100644
@@ -163,15 +163,19 @@ struct x86_emulate_ops {
 				int size, unsigned short port, const void *val,
 				unsigned int count);
 
-	bool (*get_cached_descriptor)(struct desc_struct *desc, u32 *base3,
-				      int seg, struct kvm_vcpu *vcpu);
-	void (*set_cached_descriptor)(struct desc_struct *desc, u32 base3,
-				      int seg, struct kvm_vcpu *vcpu);
-	u16 (*get_segment_selector)(int seg, struct kvm_vcpu *vcpu);
-	void (*set_segment_selector)(u16 sel, int seg, struct kvm_vcpu *vcpu);
-	unsigned long (*get_cached_segment_base)(int seg, struct kvm_vcpu *vcpu);
-	void (*get_gdt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu);
-	void (*get_idt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu);
+	bool (*get_cached_descriptor)(struct x86_emulate_ctxt *ctxt,
+				      struct desc_struct *desc, u32 *base3,
+				      int seg);
+	void (*set_cached_descriptor)(struct x86_emulate_ctxt *ctxt,
+				      struct desc_struct *desc, u32 base3,
+				      int seg);
+	u16 (*get_segment_selector)(struct x86_emulate_ctxt *ctxt, int seg);
+	void (*set_segment_selector)(struct x86_emulate_ctxt *ctxt,
+				     u16 sel, int seg);
+	unsigned long (*get_cached_segment_base)(struct x86_emulate_ctxt *ctxt,
+						 int seg);
+	void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
+	void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
 	ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu);
 	int (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu);
 	int (*cpl)(struct kvm_vcpu *vcpu);
arch/x86/kvm/emulate.c +56 -56

244 lines not on this page · raw message

arch/x86/kvm/x86.c +21 -18
index e9040a9..6a7fbf6 100644
@@ -4237,28 +4237,29 @@ static int emulator_get_cpl(struct kvm_vcpu *vcpu)
 	return kvm_x86_ops->get_cpl(vcpu);
 }
 
-static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
+static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
 {
-	kvm_x86_ops->get_gdt(vcpu, dt);
+	kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
 }
 
-static void emulator_get_idt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
+static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
 {
-	kvm_x86_ops->get_idt(vcpu, dt);
+	kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
 }
 
-static unsigned long emulator_get_cached_segment_base(int seg,
-						      struct kvm_vcpu *vcpu)
+static unsigned long emulator_get_cached_segment_base(
+	struct x86_emulate_ctxt *ctxt, int seg)
 {
-	return get_segment_base(vcpu, seg);
+	return get_segment_base(emul_to_vcpu(ctxt), seg);
 }
 
-static bool emulator_get_cached_descriptor(struct desc_struct *desc, u32 *base3,
-					   int seg, struct kvm_vcpu *vcpu)
+static bool emulator_get_cached_descriptor(struct x86_emulate_ctxt *ctxt,
+					   struct desc_struct *desc, u32 *base3,
+					   int seg)
 {
 	struct kvm_segment var;
 
-	kvm_get_segment(vcpu, &var, seg);
+	kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
 
 	if (var.unusable)
 		return false;
@@ -4283,9 +4284,11 @@ static bool emulator_get_cached_descriptor(struct desc_struct *desc, u32 *base3,
 	return true;
 }
 
-static void emulator_set_cached_descriptor(struct desc_struct *desc, u32 base3,
-					   int seg, struct kvm_vcpu *vcpu)
+static void emulator_set_cached_descriptor(struct x86_emulate_ctxt *ctxt,
+					   struct desc_struct *desc, u32 base3,
+					   int seg)
 {
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	struct kvm_segment var;
 
 	/* needed to preserve selector */
@@ -4314,22 +4317,22 @@ static void emulator_set_cached_descriptor(struct desc_struct *desc, u32 base3,
 	return;
 }
 
-static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu)
+static u16 emulator_get_segment_selector(struct x86_emulate_ctxt *ctxt, int seg)
 {
 	struct kvm_segment kvm_seg;
 
-	kvm_get_segment(vcpu, &kvm_seg, seg);
+	kvm_get_segment(emul_to_vcpu(ctxt), &kvm_seg, seg);
 	return kvm_seg.selector;
 }
 
-static void emulator_set_segment_selector(u16 sel, int seg,
-					  struct kvm_vcpu *vcpu)
+static void emulator_set_segment_selector(struct x86_emulate_ctxt *ctxt,
+					  u16 sel, int seg)
 {
 	struct kvm_segment kvm_seg;
 
-	kvm_get_segment(vcpu, &kvm_seg, seg);
+	kvm_get_segment(emul_to_vcpu(ctxt), &kvm_seg, seg);
 	kvm_seg.selector = sel;
-	kvm_set_segment(vcpu, &kvm_seg, seg);
+	kvm_set_segment(emul_to_vcpu(ctxt), &kvm_seg, seg);
 }
 
 static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
Signature · 1 line
1.7.1
author
#4 lore raw

PATCH 4/16 KVM: x86 emulator: drop vcpu argument from cr/dr/cpl/msr callbacks

In reply to Avi Kivity

Making the emulator caller agnostic.

---
 arch/x86/include/asm/kvm_emulate.h |   14 +++---
 arch/x86/kvm/emulate.c             |   84 ++++++++++++++++++------------------
 arch/x86/kvm/x86.c                 |   34 ++++++++++----
 3 files changed, 73 insertions(+), 59 deletions(-)
arch/x86/include/asm/kvm_emulate.h +7 -7
index 656046a..2c02e75 100644
@@ -176,13 +176,13 @@ struct x86_emulate_ops {
 						 int seg);
 	void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
 	void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
-	ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu);
-	int (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu);
-	int (*cpl)(struct kvm_vcpu *vcpu);
-	int (*get_dr)(int dr, unsigned long *dest, struct kvm_vcpu *vcpu);
-	int (*set_dr)(int dr, unsigned long value, struct kvm_vcpu *vcpu);
-	int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
-	int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
+	ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr);
+	int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val);
+	int (*cpl)(struct x86_emulate_ctxt *ctxt);
+	int (*get_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong *dest);
+	int (*set_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong value);
+	int (*set_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
+	int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
 	void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */
 	void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */
 	int (*intercept)(struct kvm_vcpu *vcpu,
arch/x86/kvm/emulate.c +42 -42

276 lines not on this page · raw message

arch/x86/kvm/x86.c +24 -10
index 6a7fbf6..16373a5 100644
@@ -4160,15 +4160,15 @@ int emulate_clts(struct kvm_vcpu *vcpu)
 	return X86EMUL_CONTINUE;
 }
 
-int emulator_get_dr(int dr, unsigned long *dest, struct kvm_vcpu *vcpu)
+int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
 {
-	return _kvm_get_dr(vcpu, dr, dest);
+	return _kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
 }
 
-int emulator_set_dr(int dr, unsigned long value, struct kvm_vcpu *vcpu)
+int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
 {
 
-	return __kvm_set_dr(vcpu, dr, value);
+	return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
 }
 
 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
@@ -4176,8 +4176,9 @@ static u64 mk_cr_64(u64 curr_cr, u32 new_val)
 	return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
 }
 
-static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
+static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
 {
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	unsigned long value;
 
 	switch (cr) {
@@ -4204,8 +4205,9 @@ static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
 	return value;
 }
 
-static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
+static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
 {
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	int res = 0;
 
 	switch (cr) {
@@ -4232,9 +4234,9 @@ static int emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
 	return res;
 }
 
-static int emulator_get_cpl(struct kvm_vcpu *vcpu)
+static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
 {
-	return kvm_x86_ops->get_cpl(vcpu);
+	return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
 }
 
 static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
@@ -4335,6 +4337,18 @@ static void emulator_set_segment_selector(struct x86_emulate_ctxt *ctxt,
 	kvm_set_segment(emul_to_vcpu(ctxt), &kvm_seg, seg);
 }
 
+static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
+			    u32 msr_index, u64 *pdata)
+{
+	return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
+}
+
+static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
+			    u32 msr_index, u64 data)
+{
+	return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
+}
+
 static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
 {
 	preempt_disable();
@@ -4379,8 +4393,8 @@ static struct x86_emulate_ops emulate_ops = {
 	.cpl                 = emulator_get_cpl,
 	.get_dr              = emulator_get_dr,
 	.set_dr              = emulator_set_dr,
-	.set_msr             = kvm_set_msr,
-	.get_msr             = kvm_get_msr,
+	.set_msr             = emulator_set_msr,
+	.get_msr             = emulator_get_msr,
 	.get_fpu             = emulator_get_fpu,
 	.put_fpu             = emulator_put_fpu,
 	.intercept           = emulator_intercept,
Signature · 1 line
1.7.1
author
#5 lore raw

PATCH 5/16 KVM: x86 emulator: drop vcpu argument from intercept callback

In reply to Avi Kivity

Making the emulator caller agnostic.

---
 arch/x86/include/asm/kvm_emulate.h |    2 +-
 arch/x86/kvm/emulate.c             |    2 +-
 arch/x86/kvm/x86.c                 |    4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -1
index 2c02e75..e2b082a 100644
@@ -185,7 +185,7 @@ struct x86_emulate_ops {
 	int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
 	void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */
 	void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */
-	int (*intercept)(struct kvm_vcpu *vcpu,
+	int (*intercept)(struct x86_emulate_ctxt *ctxt,
 			 struct x86_instruction_info *info,
 			 enum x86_intercept_stage stage);
 };
arch/x86/kvm/emulate.c +1 -1
index 57c730b..55ca5a5 100644
@@ -438,7 +438,7 @@ static int emulator_check_intercept(struct x86_emulate_ctxt *ctxt,
 		.next_rip   = ctxt->eip,
 	};
 
-	return ctxt->ops->intercept(ctxt->vcpu, &info, stage);
+	return ctxt->ops->intercept(ctxt, &info, stage);
 }
 
 static inline unsigned long ad_mask(struct decode_cache *c)
arch/x86/kvm/x86.c +2 -2
index 16373a5..4f7248e 100644
@@ -4365,11 +4365,11 @@ static void emulator_put_fpu(struct x86_emulate_ctxt *ctxt)
 	preempt_enable();
 }
 
-static int emulator_intercept(struct kvm_vcpu *vcpu,
+static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
 			      struct x86_instruction_info *info,
 			      enum x86_intercept_stage stage)
 {
-	return kvm_x86_ops->check_intercept(vcpu, info, stage);
+	return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
 }
 
 static struct x86_emulate_ops emulate_ops = {
Signature · 1 line
1.7.1
author
#6 lore raw

PATCH 6/16 KVM: x86 emulator: avoid using ctxt->vcpu in check_perm() callbacks

In reply to Avi Kivity

Unneeded for register access.

---
 arch/x86/kvm/emulate.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
index 55ca5a5..8020f1b 100644
@@ -2720,7 +2720,7 @@ static int check_svme(struct x86_emulate_ctxt *ctxt)
 
 static int check_svme_pa(struct x86_emulate_ctxt *ctxt)
 {
-	u64 rax = kvm_register_read(ctxt->vcpu, VCPU_REGS_RAX);
+	u64 rax = ctxt->decode.regs[VCPU_REGS_RAX];
 
 	/* Valid physical address? */
 	if (rax & 0xffff000000000000)
@@ -2742,7 +2742,7 @@ static int check_rdtsc(struct x86_emulate_ctxt *ctxt)
 static int check_rdpmc(struct x86_emulate_ctxt *ctxt)
 {
 	u64 cr4 = ctxt->ops->get_cr(ctxt, 4);
-	u64 rcx = kvm_register_read(ctxt->vcpu, VCPU_REGS_RCX);
+	u64 rcx = ctxt->decode.regs[VCPU_REGS_RCX];
 
 	if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) ||
 	    (rcx > 3))
Signature · 1 line
1.7.1
author
#7 lore raw

PATCH 7/16 KVM: x86 emulator: add and use new callbacks set_idt(), set_gdt()

In reply to Avi Kivity

Replacing direct calls to realmode_lgdt(), realmode_lidt().

---
 arch/x86/include/asm/kvm_emulate.h |    2 ++
 arch/x86/include/asm/kvm_host.h    |    3 ---
 arch/x86/kvm/emulate.c             |   14 +++++++-------
 arch/x86/kvm/x86.c                 |   26 ++++++++++++--------------
 4 files changed, 21 insertions(+), 24 deletions(-)
arch/x86/include/asm/kvm_emulate.h +2 -0
index e2b082a..4d1546a 100644
@@ -176,6 +176,8 @@ struct x86_emulate_ops {
 						 int seg);
 	void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
 	void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
+	void (*set_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
+	void (*set_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
 	ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr);
 	int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val);
 	int (*cpl)(struct x86_emulate_ctxt *ctxt);
arch/x86/include/asm/kvm_host.h +0 -3
index e50bffc..a8616ca 100644
@@ -681,9 +681,6 @@ static inline int emulate_instruction(struct kvm_vcpu *vcpu,
 	return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
 }
 
-void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
-void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
-
 void kvm_enable_efer_bits(u64);
 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
arch/x86/kvm/emulate.c +7 -7
index 8020f1b..fb431f3 100644
@@ -3494,6 +3494,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
 	int rc = X86EMUL_CONTINUE;
 	int saved_dst_type = c->dst.type;
 	int irq; /* Used for int 3, int, and into */
+	struct desc_ptr desc_ptr;
 
 	ctxt->decode.mem_read.pos = 0;
 
@@ -4005,9 +4006,6 @@ twobyte_insn:
 	switch (c->b) {
 	case 0x01: /* lgdt, lidt, lmsw */
 		switch (c->modrm_reg) {
-			u16 size;
-			unsigned long address;
-
 		case 0: /* vmcall */
 			if (c->modrm_mod != 3 || c->modrm_rm != 1)
 				goto cannot_emulate;
@@ -4023,10 +4021,11 @@ twobyte_insn:
 			break;
 		case 2: /* lgdt */
 			rc = read_descriptor(ctxt, ops, c->src.addr.mem,
-					     &size, &address, c->op_bytes);
+					     &desc_ptr.size, &desc_ptr.address,
+					     c->op_bytes);
 			if (rc != X86EMUL_CONTINUE)
 				goto done;
-			realmode_lgdt(ctxt->vcpu, size, address);
+			ctxt->ops->set_gdt(ctxt, &desc_ptr);
 			/* Disable writeback. */
 			c->dst.type = OP_NONE;
 			break;
@@ -4041,11 +4040,12 @@ twobyte_insn:
 				}
 			} else {
 				rc = read_descriptor(ctxt, ops, c->src.addr.mem,
-						     &size, &address,
+						     &desc_ptr.size,
+						     &desc_ptr.address,
 						     c->op_bytes);
 				if (rc != X86EMUL_CONTINUE)
 					goto done;
-				realmode_lidt(ctxt->vcpu, size, address);
+				ctxt->ops->set_idt(ctxt, &desc_ptr);
 			}
 			/* Disable writeback. */
 			c->dst.type = OP_NONE;
arch/x86/kvm/x86.c +12 -14
index 4f7248e..7cd3a3b 100644
@@ -4249,6 +4249,16 @@ static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
 	kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
 }
 
+static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
+{
+	kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
+}
+
+static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
+{
+	kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
+}
+
 static unsigned long emulator_get_cached_segment_base(
 	struct x86_emulate_ctxt *ctxt, int seg)
 {
@@ -4388,6 +4398,8 @@ static struct x86_emulate_ops emulate_ops = {
 	.get_cached_segment_base = emulator_get_cached_segment_base,
 	.get_gdt             = emulator_get_gdt,
 	.get_idt	     = emulator_get_idt,
+	.set_gdt             = emulator_set_gdt,
+	.set_idt	     = emulator_set_idt,
 	.get_cr              = emulator_get_cr,
 	.set_cr              = emulator_set_cr,
 	.cpl                 = emulator_get_cpl,
@@ -5049,20 +5061,6 @@ int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
 				       rip, instruction, 3, NULL);
 }
 
-void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
-{
-	struct desc_ptr dt = { limit, base };
-
-	kvm_x86_ops->set_gdt(vcpu, &dt);
-}
-
-void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
-{
-	struct desc_ptr dt = { limit, base };
-
-	kvm_x86_ops->set_idt(vcpu, &dt);
-}
-
 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
 {
 	struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
Signature · 1 line
1.7.1
author
#8 lore raw

PATCH 8/16 KVM: x86 emulator: drop use of is_long_mode()

In reply to Avi Kivity

Requires ctxt->vcpu, which is to be abolished. Replace with open calls to get_msr().

---
 arch/x86/kvm/emulate.c |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
arch/x86/kvm/emulate.c +12 -7
index fb431f3..a4227bf 100644
@@ -1844,12 +1844,14 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	struct desc_struct cs, ss;
 	u64 msr_data;
 	u16 cs_sel, ss_sel;
+	u64 efer = 0;
 
 	/* syscall is not available in real mode */
 	if (ctxt->mode == X86EMUL_MODE_REAL ||
 	    ctxt->mode == X86EMUL_MODE_VM86)
 		return emulate_ud(ctxt);
 
+	ops->get_msr(ctxt, MSR_EFER, &efer);
 	setup_syscalls_segments(ctxt, ops, &cs, &ss);
 
 	ops->get_msr(ctxt, MSR_STAR, &msr_data);
@@ -1857,7 +1859,7 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	cs_sel = (u16)(msr_data & 0xfffc);
 	ss_sel = (u16)(msr_data + 8);
 
-	if (is_long_mode(ctxt->vcpu)) {
+	if (efer & EFER_LMA) {
 		cs.d = 0;
 		cs.l = 1;
 	}
@@ -1867,7 +1869,7 @@ emulate_syscall(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	ops->set_segment_selector(ctxt, ss_sel, VCPU_SREG_SS);
 
 	c->regs[VCPU_REGS_RCX] = c->eip;
-	if (is_long_mode(ctxt->vcpu)) {
+	if (efer & EFER_LMA) {
 #ifdef CONFIG_X86_64
 		c->regs[VCPU_REGS_R11] = ctxt->eflags & ~EFLG_RF;
 
@@ -1897,7 +1899,9 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	struct desc_struct cs, ss;
 	u64 msr_data;
 	u16 cs_sel, ss_sel;
+	u64 efer = 0;
 
+	ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
 	/* inject #GP if in real mode */
 	if (ctxt->mode == X86EMUL_MODE_REAL)
 		return emulate_gp(ctxt, 0);
@@ -1927,8 +1931,7 @@ emulate_sysenter(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	cs_sel &= ~SELECTOR_RPL_MASK;
 	ss_sel = cs_sel + 8;
 	ss_sel &= ~SELECTOR_RPL_MASK;
-	if (ctxt->mode == X86EMUL_MODE_PROT64
-		|| is_long_mode(ctxt->vcpu)) {
+	if (ctxt->mode == X86EMUL_MODE_PROT64 || (efer & EFER_LMA)) {
 		cs.d = 0;
 		cs.l = 1;
 	}
@@ -2603,6 +2606,7 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
 	struct decode_cache *c = &ctxt->decode;
 	u64 new_val = c->src.val64;
 	int cr = c->modrm_reg;
+	u64 efer = 0;
 
 	static u64 cr_reserved_bits[] = {
 		0xffffffff00000000ULL,
@@ -2620,7 +2624,7 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
 
 	switch (cr) {
 	case 0: {
-		u64 cr4, efer;
+		u64 cr4;
 		if (((new_val & X86_CR0_PG) && !(new_val & X86_CR0_PE)) ||
 		    ((new_val & X86_CR0_NW) && !(new_val & X86_CR0_CD)))
 			return emulate_gp(ctxt, 0);
@@ -2637,7 +2641,8 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
 	case 3: {
 		u64 rsvd = 0;
 
-		if (is_long_mode(ctxt->vcpu))
+		ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
+		if (efer & EFER_LMA)
 			rsvd = CR3_L_MODE_RESERVED_BITS;
 		else if (is_pae(ctxt->vcpu))
 			rsvd = CR3_PAE_RESERVED_BITS;
@@ -2650,7 +2655,7 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
 		break;
 		}
 	case 4: {
-		u64 cr4, efer;
+		u64 cr4;
 
 		cr4 = ctxt->ops->get_cr(ctxt, 4);
 		ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
Signature · 1 line
1.7.1
author
#9 lore raw

PATCH 9/16 KVM: x86 emulator: Replace calls to is_pae() and is_paging with ->get_cr()

In reply to Avi Kivity

Avoid use of ctxt->vcpu.

---
 arch/x86/kvm/emulate.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
index a4227bf..dc495a0 100644
@@ -2644,9 +2644,9 @@ static int check_cr_write(struct x86_emulate_ctxt *ctxt)
 		ctxt->ops->get_msr(ctxt, MSR_EFER, &efer);
 		if (efer & EFER_LMA)
 			rsvd = CR3_L_MODE_RESERVED_BITS;
-		else if (is_pae(ctxt->vcpu))
+		else if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_PAE)
 			rsvd = CR3_PAE_RESERVED_BITS;
-		else if (is_paging(ctxt->vcpu))
+		else if (ctxt->ops->get_cr(ctxt, 0) & X86_CR0_PG)
 			rsvd = CR3_NONPAE_RESERVED_BITS;
 
 		if (new_val & rsvd)
Signature · 1 line
1.7.1
author
#10 lore raw

PATCH 10/16 KVM: x86 emulator: emulate CLTS internally

In reply to Avi Kivity

Avoid using ctxt->vcpu; we can do everything with ->get_cr() and ->set_cr().

A side effect is that we no longer activate the fpu on emulated CLTS; but that should be very rare.

---
 arch/x86/include/asm/kvm_host.h |    1 -
 arch/x86/kvm/emulate.c          |   12 +++++++++++-
 arch/x86/kvm/x86.c              |    7 -------
 3 files changed, 11 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
index a8616ca..9c3567e 100644
@@ -691,7 +691,6 @@ int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port);
 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
 int kvm_emulate_halt(struct kvm_vcpu *vcpu);
 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
-int emulate_clts(struct kvm_vcpu *vcpu);
 int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);
 
 void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
arch/x86/kvm/emulate.c +11 -1
index dc495a0..91c4a14 100644
@@ -2579,6 +2579,16 @@ static int em_invlpg(struct x86_emulate_ctxt *ctxt)
 	return X86EMUL_CONTINUE;
 }
 
+static int em_clts(struct x86_emulate_ctxt *ctxt)
+{
+	ulong cr0;
+
+	cr0 = ctxt->ops->get_cr(ctxt, 0);
+	cr0 &= ~X86_CR0_TS;
+	ctxt->ops->set_cr(ctxt, 0, cr0);
+	return X86EMUL_CONTINUE;
+}
+
 static bool valid_cr(int nr)
 {
 	switch (nr) {
@@ -4079,7 +4089,7 @@ twobyte_insn:
 		rc = emulate_syscall(ctxt, ops);
 		break;
 	case 0x06:
-		emulate_clts(ctxt->vcpu);
+		rc = em_clts(ctxt);
 		break;
 	case 0x09:		/* wbinvd */
 		kvm_emulate_wbinvd(ctxt->vcpu);
arch/x86/kvm/x86.c +0 -7
index 7cd3a3b..a9e8386 100644
@@ -4153,13 +4153,6 @@ int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
 
-int emulate_clts(struct kvm_vcpu *vcpu)
-{
-	kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
-	kvm_x86_ops->fpu_activate(vcpu);
-	return X86EMUL_CONTINUE;
-}
-
 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
 {
 	return _kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
Signature · 1 line
1.7.1
author
#11 lore raw

PATCH 11/16 KVM: x86 emulator: make emulate_invlpg() an emulator callback

In reply to Avi Kivity

Removing direct calls to KVM.

---
 arch/x86/include/asm/kvm_emulate.h |    1 +
 arch/x86/include/asm/kvm_host.h    |    1 -
 arch/x86/kvm/emulate.c             |    2 +-
 arch/x86/kvm/x86.c                 |    6 +++---
 4 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
index 4d1546a..f890769 100644
@@ -154,6 +154,7 @@ struct x86_emulate_ops {
 				const void *new,
 				unsigned int bytes,
 				struct x86_exception *fault);
+	void (*invlpg)(struct x86_emulate_ctxt *ctxt, ulong addr);
 
 	int (*pio_in_emulated)(struct x86_emulate_ctxt *ctxt,
 			       int size, unsigned short port, void *val,
arch/x86/include/asm/kvm_host.h +0 -1
index 9c3567e..d957d0d 100644
@@ -690,7 +690,6 @@ struct x86_emulate_ctxt;
 int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port);
 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
 int kvm_emulate_halt(struct kvm_vcpu *vcpu);
-int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
 int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);
 
 void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
arch/x86/kvm/emulate.c +1 -1
index 91c4a14..6fca45f 100644
@@ -2573,7 +2573,7 @@ static int em_invlpg(struct x86_emulate_ctxt *ctxt)
 
 	rc = linearize(ctxt, c->src.addr.mem, 1, false, &linear);
 	if (rc == X86EMUL_CONTINUE)
-		emulate_invlpg(ctxt->vcpu, linear);
+		ctxt->ops->invlpg(ctxt, linear);
 	/* Disable writeback. */
 	c->dst.type = OP_NONE;
 	return X86EMUL_CONTINUE;
arch/x86/kvm/x86.c +3 -3
index a9e8386..8af49b3 100644
@@ -4128,10 +4128,9 @@ static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
 	return kvm_x86_ops->get_segment_base(vcpu, seg);
 }
 
-int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
+static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
 {
-	kvm_mmu_invlpg(vcpu, address);
-	return X86EMUL_CONTINUE;
+	kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
 }
 
 int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
@@ -4382,6 +4381,7 @@ static struct x86_emulate_ops emulate_ops = {
 	.read_emulated       = emulator_read_emulated,
 	.write_emulated      = emulator_write_emulated,
 	.cmpxchg_emulated    = emulator_cmpxchg_emulated,
+	.invlpg              = emulator_invlpg,
 	.pio_in_emulated     = emulator_pio_in_emulated,
 	.pio_out_emulated    = emulator_pio_out_emulated,
 	.get_cached_descriptor = emulator_get_cached_descriptor,
Signature · 1 line
1.7.1
author
#12 lore raw

PATCH 12/16 KVM: x86 emulator: add new ->halt() callback

In reply to Avi Kivity

Instead of reaching into vcpu internals.

---
 arch/x86/include/asm/kvm_emulate.h |    1 +
 arch/x86/kvm/emulate.c             |    2 +-
 arch/x86/kvm/x86.c                 |    6 ++++++
 3 files changed, 8 insertions(+), 1 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
index f890769..d30f1e9 100644
@@ -186,6 +186,7 @@ struct x86_emulate_ops {
 	int (*set_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong value);
 	int (*set_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
 	int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
+	void (*halt)(struct x86_emulate_ctxt *ctxt);
 	void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */
 	void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */
 	int (*intercept)(struct x86_emulate_ctxt *ctxt,
arch/x86/kvm/emulate.c +1 -1
index 6fca45f..a2a5008 100644
@@ -3913,7 +3913,7 @@ special_insn:
 		c->dst.type = OP_NONE;	/* Disable writeback. */
 		break;
 	case 0xf4:              /* hlt */
-		ctxt->vcpu->arch.halt_request = 1;
+		ctxt->ops->halt(ctxt);
 		break;
 	case 0xf5:	/* cmc */
 		/* complement carry flag from eflags reg */
arch/x86/kvm/x86.c +6 -0
index 8af49b3..2246cf1 100644
@@ -4351,6 +4351,11 @@ static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
 	return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
 }
 
+static void emulator_halt(struct x86_emulate_ctxt *ctxt)
+{
+	emul_to_vcpu(ctxt)->arch.halt_request = 1;
+}
+
 static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
 {
 	preempt_disable();
@@ -4400,6 +4405,7 @@ static struct x86_emulate_ops emulate_ops = {
 	.set_dr              = emulator_set_dr,
 	.set_msr             = emulator_set_msr,
 	.get_msr             = emulator_get_msr,
+	.halt                = emulator_halt,
 	.get_fpu             = emulator_get_fpu,
 	.put_fpu             = emulator_put_fpu,
 	.intercept           = emulator_intercept,
Signature · 1 line
1.7.1
author
#13 lore raw

PATCH 13/16 KVM: x86 emulator: add ->fix_hypercall() callback

In reply to Avi Kivity

Artificial, but needed to remove direct calls to KVM.

---
 arch/x86/include/asm/kvm_emulate.h |    1 +
 arch/x86/include/asm/kvm_host.h    |    2 --
 arch/x86/kvm/emulate.c             |    4 ++--
 arch/x86/kvm/x86.c                 |    6 +++++-
 4 files changed, 8 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
index d30f1e9..d30840d 100644
@@ -187,6 +187,7 @@ struct x86_emulate_ops {
 	int (*set_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
 	int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
 	void (*halt)(struct x86_emulate_ctxt *ctxt);
+	int (*fix_hypercall)(struct x86_emulate_ctxt *ctxt);
 	void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */
 	void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */
 	int (*intercept)(struct x86_emulate_ctxt *ctxt,
arch/x86/include/asm/kvm_host.h +0 -2
index d957d0d..6cfc1ab 100644
@@ -752,8 +752,6 @@ gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
 
 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
 
-int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
-
 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code,
 		       void *insn, int insn_len);
 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
arch/x86/kvm/emulate.c +2 -2
index a2a5008..a41f406 100644
@@ -4025,7 +4025,7 @@ twobyte_insn:
 			if (c->modrm_mod != 3 || c->modrm_rm != 1)
 				goto cannot_emulate;
 
-			rc = kvm_fix_hypercall(ctxt->vcpu);
+			rc = ctxt->ops->fix_hypercall(ctxt);
 			if (rc != X86EMUL_CONTINUE)
 				goto done;
 
@@ -4048,7 +4048,7 @@ twobyte_insn:
 			if (c->modrm_mod == 3) {
 				switch (c->modrm_rm) {
 				case 1:
-					rc = kvm_fix_hypercall(ctxt->vcpu);
+					rc = ctxt->ops->fix_hypercall(ctxt);
 					break;
 				default:
 					goto cannot_emulate;
arch/x86/kvm/x86.c +5 -1
index 2246cf1..4a2b40e 100644
@@ -152,6 +152,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 
 u64 __read_mostly host_xcr0;
 
+int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
+
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
@@ -4406,6 +4408,7 @@ static struct x86_emulate_ops emulate_ops = {
 	.set_msr             = emulator_set_msr,
 	.get_msr             = emulator_get_msr,
 	.halt                = emulator_halt,
+	.fix_hypercall       = emulator_fix_hypercall,
 	.get_fpu             = emulator_get_fpu,
 	.put_fpu             = emulator_put_fpu,
 	.intercept           = emulator_intercept,
@@ -5042,8 +5045,9 @@ out:
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
 
-int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
+int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
 {
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	char instruction[3];
 	unsigned long rip = kvm_rip_read(vcpu);
 
Signature · 1 line
1.7.1
author
#14 lore raw

PATCH 14/16 KVM: x86 emulator: add new ->wbinvd() callback

In reply to Avi Kivity

Instead of calling kvm_emulate_wbinvd() directly.

---
 arch/x86/include/asm/kvm_emulate.h |    1 +
 arch/x86/kvm/emulate.c             |    2 +-
 arch/x86/kvm/x86.c                 |    6 ++++++
 3 files changed, 8 insertions(+), 1 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
index d30840d..51341d6 100644
@@ -187,6 +187,7 @@ struct x86_emulate_ops {
 	int (*set_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
 	int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
 	void (*halt)(struct x86_emulate_ctxt *ctxt);
+	void (*wbinvd)(struct x86_emulate_ctxt *ctxt);
 	int (*fix_hypercall)(struct x86_emulate_ctxt *ctxt);
 	void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */
 	void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */
arch/x86/kvm/emulate.c +1 -1
index a41f406..f683ce1 100644
@@ -4092,7 +4092,7 @@ twobyte_insn:
 		rc = em_clts(ctxt);
 		break;
 	case 0x09:		/* wbinvd */
-		kvm_emulate_wbinvd(ctxt->vcpu);
+		ctxt->ops->wbinvd(ctxt);
 		break;
 	case 0x08:		/* invd */
 	case 0x0d:		/* GrpP (prefetch) */
arch/x86/kvm/x86.c +6 -0
index 4a2b40e..5d853d5 100644
@@ -4154,6 +4154,11 @@ int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
 
+static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
+{
+	kvm_emulate_wbinvd(emul_to_vcpu(ctxt));
+}
+
 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
 {
 	return _kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
@@ -4408,6 +4413,7 @@ static struct x86_emulate_ops emulate_ops = {
 	.set_msr             = emulator_set_msr,
 	.get_msr             = emulator_get_msr,
 	.halt                = emulator_halt,
+	.wbinvd              = emulator_wbinvd,
 	.fix_hypercall       = emulator_fix_hypercall,
 	.get_fpu             = emulator_get_fpu,
 	.put_fpu             = emulator_put_fpu,
Signature · 1 line
1.7.1
author
#15 lore raw

PATCH 15/16 KVM: Avoid using x86_emulate_ctxt.vcpu

In reply to Avi Kivity

We can use container_of() instead.

---
 arch/x86/kvm/x86.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +1 -1
index 5d853d5..65a5b0c 100644
@@ -4366,7 +4366,7 @@ static void emulator_halt(struct x86_emulate_ctxt *ctxt)
 static void emulator_get_fpu(struct x86_emulate_ctxt *ctxt)
 {
 	preempt_disable();
-	kvm_load_guest_fpu(ctxt->vcpu);
+	kvm_load_guest_fpu(emul_to_vcpu(ctxt));
 	/*
 	 * CR0.TS may reference the host fpu state, not the guest fpu state,
 	 * so it may be clear at this point.
Signature · 1 line
1.7.1
author
#16 lore raw

PATCH 16/16 KVM: x86 emulator: drop x86_emulate_ctxt::vcpu

In reply to Avi Kivity

No longer used.

---
 arch/x86/include/asm/kvm_emulate.h |    2 --
 arch/x86/kvm/x86.c                 |    1 -
 2 files changed, 0 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_emulate.h +0 -2
index 51341d6..127ea3e 100644
@@ -269,8 +269,6 @@ struct x86_emulate_ctxt {
 	struct x86_emulate_ops *ops;
 
 	/* Register state before/after emulation. */
-	struct kvm_vcpu *vcpu;
-
 	unsigned long eflags;
 	unsigned long eip; /* eip before instruction emulation */
 	/* Emulated execution mode, represented by an X86EMUL_MODE value. */
arch/x86/kvm/x86.c +0 -1
index 65a5b0c..a831d5d 100644
@@ -4463,7 +4463,6 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
 
 	kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
 
-	vcpu->arch.emulate_ctxt.vcpu = vcpu;
 	vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
 	vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
 	vcpu->arch.emulate_ctxt.mode =
Signature · 1 line
1.7.1