Skip to contentSkip to conversation

RFC: cache_regs in kvm_emulate_pio

DISCUSSION Marcelo Tosatti 2 participants 8 messages Jun 21, 2008 – Jun 27, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw
Quoted 5 lines · Avi Kivity
Quoted 5 lines

/*
* Sync the rsp and rip registers into the vcpu structure. This allows
* registers to be accessed by indexing vcpu->arch.regs.
*/

Quoted 7 lines
Quoted 4 lines

But I think it just refers to the interface in general, so that nobody would try to access RSP or RIP (and RAX in AMD's case) before calling ->cache_regs().

It refers to the fact that sometimes you don't know which registers you refer to, e.g. in the emulator.

How's this?

Test performed with idle UP guest booted with "nohz=off clocksource=acpi_pm", so most of the exits are acpi timer reads. This average is from available entries in the dmesg buffer, about 1500.

"regs_available" and "regs_dirty" could be 8 bits, and placed in a hole instead of in the middle of longs.

avg cycles to exit to qemu:
before: 3376
after: 3181

195 cycles (~= 6.1% improvement)

avg cycles to exit to qemu, handle exit and return to __vcpu_run before irq_disable:

before: 7482 cycles.
after: 7227 cycles.

255 cycles (~= 3.5% improvement)

arch/x86/kvm/lapic.c +2 -1
index 73f43de..ecf26e3 100644
@@ -32,6 +32,7 @@
 #include <asm/current.h>
 #include <asm/apicdef.h>
 #include <asm/atomic.h>
+#include "kvm_cache_regs.h"
 #include "irq.h"
 
 #define PRId64 "d"
@@ -558,7 +559,7 @@ static void __report_tpr_access(struct kvm_lapic *apic, bool write)
 	struct kvm_run *run = vcpu->run;
 
 	set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
-	kvm_x86_ops->cache_regs(vcpu);
+	cache_regs(vcpu, REGS_RIP);
 	run->tpr_access.rip = vcpu->arch.rip;
 	run->tpr_access.is_write = write;
 }
arch/x86/kvm/svm.c +25 -8
index 238e8f3..8554b37 100644
@@ -709,21 +709,38 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)
 	rdtscll(vcpu->arch.host_tsc);
 }
 
-static void svm_cache_regs(struct kvm_vcpu *vcpu)
+static void svm_cache_regs(struct kvm_vcpu *vcpu, enum kvm_reg_set regs)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
-	vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
-	vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
-	vcpu->arch.rip = svm->vmcb->save.rip;
+	switch (regs) {
+	case REGS_GPR:
+		vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
+		break;
+	case REGS_RSP:
+		vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
+		break;
+	case REGS_RIP:
+		vcpu->arch.rip = svm->vmcb->save.rip;
+		break;
+	}
 }
 
-static void svm_decache_regs(struct kvm_vcpu *vcpu)
+static void svm_decache_regs(struct kvm_vcpu *vcpu, enum kvm_reg_set regs)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-	svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
-	svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
-	svm->vmcb->save.rip = vcpu->arch.rip;
+
+	switch (regs) {
+	case REGS_GPR:
+		svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
+		break;
+	case REGS_RSP:
+		svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
+		break;
+	case REGS_RIP:
+		svm->vmcb->save.rip = vcpu->arch.rip;
+		break;
+	}
 }
 
 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx.c +40 -9

83 lines not on this page · raw message

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

186 lines not on this page · raw message

include/asm-x86/kvm_host.h +12 -4
index 851184d..56f01cc 100644
@@ -120,6 +120,12 @@ enum {
 	VCPU_SREG_LDTR,
 };
 
+enum kvm_reg_set {
+	REGS_GPR,
+	REGS_RSP,
+	REGS_RIP,
+};
+
 #include <asm/kvm_x86_emulate.h>
 
 #define KVM_NR_MEM_OBJS 40
@@ -217,8 +223,10 @@ struct kvm_vcpu_arch {
 	int interrupt_window_open;
 	unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
 	DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
-	unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
-	unsigned long rip;      /* needs vcpu_load_rsp_rip() */
+	unsigned long regs[NR_VCPU_REGS]; /* needs cache_regs() */
+	unsigned long rip;      /* needs cache_regs() */
+	u16 regs_available;
+	u16 regs_dirty;
 
 	unsigned long cr0;
 	unsigned long cr2;
@@ -410,8 +418,8 @@ struct kvm_x86_ops {
 	unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
 	void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
 		       int *exception);
-	void (*cache_regs)(struct kvm_vcpu *vcpu);
-	void (*decache_regs)(struct kvm_vcpu *vcpu);
+	void (*cache_regs)(struct kvm_vcpu *vcpu, enum kvm_reg_set regs);
+	void (*decache_regs)(struct kvm_vcpu *vcpu, enum kvm_reg_set regs);
 	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
 
arch/x86/kvm/kvm_cache_regs.h new file +27 -0
@@ -0,0 +1,27 @@
+#ifndef ASM_KVM_CACHE_REGS_H
+#define ASM_KVM_CACHE_REGS_H
+static inline void cache_regs(struct kvm_vcpu *vcpu, enum kvm_reg_set reg_set)
+{
+	if (!__test_and_set_bit(reg_set, &vcpu->arch.regs_available))
+		kvm_x86_ops->cache_regs(vcpu, reg_set);
+}
+
+static inline void decache_regs(struct kvm_vcpu *vcpu, enum kvm_reg_set reg_set)
+{
+	__set_bit(reg_set, &vcpu->arch.regs_dirty);
+}
+
+static inline void cache_all_regs(struct kvm_vcpu *vcpu)
+{
+	cache_regs(vcpu, REGS_RSP);
+	cache_regs(vcpu, REGS_RIP);
+	cache_regs(vcpu, REGS_GPR);
+}
+
+static inline void decache_all_regs(struct kvm_vcpu *vcpu)
+{
+	decache_regs(vcpu, REGS_RSP);
+	decache_regs(vcpu, REGS_RIP);
+	decache_regs(vcpu, REGS_GPR);
+}
+#endif

----- End forwarded message -----

----- End forwarded message -----

#1 lore raw
Quoted 12 lines · Marcelo Tosatti
Quoted 10 lines · Avi Kivity
Quoted 10 lines

/*
* Sync the rsp and rip registers into the vcpu structure. This allows
* registers to be accessed by indexing vcpu->arch.regs.
*/

But I think it just refers to the interface in general, so that nobody would try to access RSP or RIP (and RAX in AMD's case) before calling ->cache_regs().

Quoted 6 lines
Quoted 2 lines

It refers to the fact that sometimes you don't know which registers you refer to, e.g. in the emulator.

How's this?

Looks good, but we can aim higher. The cache_regs() API was always confusing (I usually swap the two parts). If we replace all ->regs access with accessors, we can make it completely transparent.

It will be tricky in the emulator, but worthwhile, no?

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#2 lore raw
Quoted 5 lines · Avi Kivity

Looks good, but we can aim higher. The cache_regs() API was always confusing (I usually swap the two parts). If we replace all ->regs access with accessors, we can make it completely transparent.

It will be tricky in the emulator, but worthwhile, no?

Yes, agree. Will go for accessors.

Thanks.

#3 lore raw
In reply to Avi Kivity
Quoted 20 lines · Avi Kivity
Quoted 11 lines · Marcelo Tosatti
Quoted 9 lines · Avi Kivity
Quoted 9 lines

/*
* Sync the rsp and rip registers into the vcpu structure. This allows
* registers to be accessed by indexing vcpu->arch.regs.
*/

But I think it just refers to the interface in general, so that nobody would try to access RSP or RIP (and RAX in AMD's case) before calling ->cache_regs().

Quoted 7 lines
Quoted 3 lines

It refers to the fact that sometimes you don't know which registers you refer to, e.g. in the emulator.

How's this?

Quoted 5 lines

Looks good, but we can aim higher. The cache_regs() API was always confusing (I usually swap the two parts). If we replace all ->regs access with accessors, we can make it completely transparent.

It will be tricky in the emulator, but worthwhile, no?

OK, in the emulator an interface on top of guest_register_write() is needed to save registers so that the original contents can be restored on failure. Some brave soul can do it later, so I added a TODO in x86.c.

Smells better now?

arch/x86/kvm/kvm_cache_regs.h renamed from null +21 -0
@@ -0,0 +1,21 @@
+#ifndef ASM_KVM_CACHE_REGS_H
+#define ASM_KVM_CACHE_REGS_H
+
+static inline unsigned long guest_register_read(struct kvm_vcpu *vcpu,
+						enum kvm_reg reg)
+{
+	if (!__test_and_set_bit(reg, &vcpu->arch.regs_available))
+		kvm_x86_ops->cache_regs(vcpu, reg);
+
+	return vcpu->arch.regs[reg];
+}
+
+static inline void guest_register_write(struct kvm_vcpu *vcpu,
+					enum kvm_reg reg,
+					unsigned long val)
+{
+	vcpu->arch.regs[reg] = val;
+	__set_bit(reg, &vcpu->arch.regs_dirty);
+}
+
+#endif
arch/x86/kvm/lapic.c +2 -2
index 73f43de..97919b6 100644
@@ -32,6 +32,7 @@
 #include <asm/current.h>
 #include <asm/apicdef.h>
 #include <asm/atomic.h>
+#include "kvm_cache_regs.h"
 #include "irq.h"
 
 #define PRId64 "d"
@@ -558,8 +559,7 @@ static void __report_tpr_access(struct kvm_lapic *apic, bool write)
 	struct kvm_run *run = vcpu->run;
 
 	set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
-	kvm_x86_ops->cache_regs(vcpu);
-	run->tpr_access.rip = vcpu->arch.rip;
+	run->tpr_access.rip = guest_register_read(vcpu, VCPU_REGS_RIP);
 	run->tpr_access.is_write = write;
 }
 
arch/x86/kvm/svm.c +32 -9

66 lines not on this page · raw message

arch/x86/kvm/vmx.c +28 -30

141 lines not on this page · raw message

arch/x86/kvm/x86.c +159 -124

484 lines not on this page · raw message

arch/x86/kvm/x86_emulate.c +13 -9
index 38926b7..c74b9d9 100644
@@ -26,6 +26,7 @@
 #define DPRINTF(_f, _a ...) printf(_f , ## _a)
 #else
 #include <linux/kvm_host.h>
+#include "kvm_cache_regs.h"
 #define DPRINTF(x...) do {} while (0)
 #endif
 #include <linux/module.h>
@@ -806,7 +807,7 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	/* Shadow copy of register state. Committed on successful emulation. */
 
 	memset(c, 0, sizeof(struct decode_cache));
-	c->eip = ctxt->vcpu->arch.rip;
+	c->eip = guest_register_read(ctxt->vcpu, VCPU_REGS_RIP);
 	memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
 
 	switch (mode) {
@@ -1245,7 +1246,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	if (c->rep_prefix && (c->d & String)) {
 		/* All REP prefixes have the same first termination condition */
 		if (c->regs[VCPU_REGS_RCX] == 0) {
-			ctxt->vcpu->arch.rip = c->eip;
+			guest_register_write(ctxt->vcpu, VCPU_REGS_RIP, c->eip);
 			goto done;
 		}
 		/* The second termination condition only applies for REPE
@@ -1259,17 +1260,20 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 				(c->b == 0xae) || (c->b == 0xaf)) {
 			if ((c->rep_prefix == REPE_PREFIX) &&
 				((ctxt->eflags & EFLG_ZF) == 0)) {
-					ctxt->vcpu->arch.rip = c->eip;
+					guest_register_write(ctxt->vcpu,
+							     VCPU_REGS_RIP,
+							     c->eip);
 					goto done;
 			}
 			if ((c->rep_prefix == REPNE_PREFIX) &&
 				((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
-				ctxt->vcpu->arch.rip = c->eip;
+				guest_register_write(ctxt->vcpu, VCPU_REGS_RIP,
+						     c->eip);
 				goto done;
 			}
 		}
 		c->regs[VCPU_REGS_RCX]--;
-		c->eip = ctxt->vcpu->arch.rip;
+		c->eip = guest_register_read(ctxt->vcpu, VCPU_REGS_RIP);
 	}
 
 	if (c->src.type == OP_MEM) {
@@ -1750,7 +1754,7 @@ writeback:
 
 	/* Commit shadow register state. */
 	memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-	ctxt->vcpu->arch.rip = c->eip;
+	guest_register_write(ctxt->vcpu, VCPU_REGS_RIP, c->eip);
 
 done:
 	if (rc == X86EMUL_UNHANDLEABLE) {
@@ -1775,7 +1779,7 @@ twobyte_insn:
 				goto done;
 
 			/* Let the processor re-execute the fixed hypercall */
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = guest_register_read(ctxt->vcpu, VCPU_REGS_RIP);
 			/* Disable writeback. */
 			c->dst.type = OP_NONE;
 			break;
@@ -1871,7 +1875,7 @@ twobyte_insn:
 		rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
 		if (rc) {
 			kvm_inject_gp(ctxt->vcpu, 0);
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = guest_register_read(ctxt->vcpu, VCPU_REGS_RIP);
 		}
 		rc = X86EMUL_CONTINUE;
 		c->dst.type = OP_NONE;
@@ -1881,7 +1885,7 @@ twobyte_insn:
 		rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
 		if (rc) {
 			kvm_inject_gp(ctxt->vcpu, 0);
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = guest_register_read(ctxt->vcpu, VCPU_REGS_RIP);
 		} else {
 			c->regs[VCPU_REGS_RAX] = (u32)msr_data;
 			c->regs[VCPU_REGS_RDX] = msr_data >> 32;
include/asm-x86/kvm_host.h +18 -5
index 851184d..cc5c94b 100644
@@ -87,7 +87,7 @@ extern struct list_head vm_list;
 struct kvm_vcpu;
 struct kvm;
 
-enum {
+enum kvm_reg {
 	VCPU_REGS_RAX = 0,
 	VCPU_REGS_RCX = 1,
 	VCPU_REGS_RDX = 2,
@@ -106,9 +106,21 @@ enum {
 	VCPU_REGS_R14 = 14,
 	VCPU_REGS_R15 = 15,
 #endif
+	VCPU_REGS_RIP = 16,
 	NR_VCPU_REGS
 };
 
+/*
+ * List of registers already read by kvm_x86_ops->run().
+ */
+#define KVM_CACHED_REGS ((1 << VCPU_REGS_RCX) | (1 << VCPU_REGS_RDX) |	 	\
+			(1 << VCPU_REGS_RBX)  | (1 << VCPU_REGS_RBP) | 		\
+			(1 << VCPU_REGS_RSI)  | (1 << VCPU_REGS_RDI) | 		\
+			(1 << VCPU_REGS_R8)   | (1 << VCPU_REGS_R9)  | 		\
+			(1 << VCPU_REGS_R10)  | (1 << VCPU_REGS_R11) | 		\
+			(1 << VCPU_REGS_R12)  | (1 << VCPU_REGS_R13) | 		\
+			(1 << VCPU_REGS_R14)  | (1 << VCPU_REGS_R15))
+
 enum {
 	VCPU_SREG_ES,
 	VCPU_SREG_CS,
@@ -217,8 +229,9 @@ struct kvm_vcpu_arch {
 	int interrupt_window_open;
 	unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
 	DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
-	unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
-	unsigned long rip;      /* needs vcpu_load_rsp_rip() */
+	unsigned long regs[NR_VCPU_REGS]; /* needs cache_regs() */
+	u32 regs_available;
+	u32 regs_dirty;
 
 	unsigned long cr0;
 	unsigned long cr2;
@@ -410,8 +423,8 @@ struct kvm_x86_ops {
 	unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
 	void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
 		       int *exception);
-	void (*cache_regs)(struct kvm_vcpu *vcpu);
-	void (*decache_regs)(struct kvm_vcpu *vcpu);
+	void (*cache_regs)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
+	void (*decache_regs)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
 	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
 
#4 lore raw
Quoted 29 lines · Marcelo Tosatti
Quoted 6 lines
Quoted 2 lines

Looks good, but we can aim higher. The cache_regs() API was always confusing (I usually swap the two parts). If we replace all ->regs access with accessors, we can make it completely transparent.

It will be tricky in the emulator, but worthwhile, no?

OK, in the emulator an interface on top of guest_register_write() is needed to save registers so that the original contents can be restored on failure. Some brave soul can do it later, so I added a TODO in x86.c.

Smells better now?

arch/x86/kvm/kvm_cache_regs.h renamed from null +11 -0
@@ -0,0 +1,21 @@
+#ifndef ASM_KVM_CACHE_REGS_H
+#define ASM_KVM_CACHE_REGS_H
+
+static inline unsigned long guest_register_read(struct kvm_vcpu *vcpu,
+						enum kvm_reg reg)
+{
+	if (!__test_and_set_bit(reg, &vcpu->arch.regs_available))
+		kvm_x86_ops->cache_regs(vcpu, reg);
+
+	return vcpu->arch.regs[reg];
+}

arch/x86/kvm/kvm_cache_regs.h line 21

Quoted 10 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +10 -0
+
+static inline void guest_register_write(struct kvm_vcpu *vcpu,
+					enum kvm_reg reg,
+					unsigned long val)
+{
+	vcpu->arch.regs[reg] = val;
+	__set_bit(reg, &vcpu->arch.regs_dirty);
+}
+
+#endif

A new header file is excessive. Also, these are global names, so please prefix with kvm_.

arch/x86/kvm/kvm_cache_regs.h static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 248

Quoted 9 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +2 -1
@@ -241,7 +242,8 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
 		       svm->vmcb->save.rip,
 		       svm->next_rip);
 
-	vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
+	svm->vmcb->save.rip = svm->next_rip;
+	guest_register_write(vcpu, VCPU_REGS_RIP, svm->vmcb->save.rip);
 	svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
 

No need to write into save.rip, is there?

Quoted 14 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +1 -1
-static void svm_cache_regs(struct kvm_vcpu *vcpu)
+static void svm_cache_regs(struct kvm_vcpu *vcpu, enum kvm_reg reg)

{

arch/x86/kvm/kvm_cache_regs.h renamed from null +4 -3
 	struct vcpu_svm *svm = to_svm(vcpu);
 
-	vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
-	vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
-	vcpu->arch.rip = svm->vmcb->save.rip;
+	switch (reg) {
+	case VCPU_REGS_RAX:
+		vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
+		break;

arch/x86/kvm/kvm_cache_regs.h

Quoted 10 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +9 -0
+	case VCPU_REGS_RSP:
+		vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
+		break;
+	case VCPU_REGS_RIP:
+		vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
+		break;
+	default:
+		break;
+	}
 }

For svm we ought to unconditionally copy all the registers and mark all registers as available, since it's so cheap. This will avoid some callbacks.

We can even to it on the vmexit path, so there will be no svm_cache_regs() at all.

arch/x86/kvm/kvm_cache_regs.h static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 712

Quoted 7 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +1 -1
@@ -707,9 +708,9 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
 	unsigned long rip;
 	u32 interruptibility;
 
-	rip = vmcs_readl(GUEST_RIP);
+	rip = guest_register_read(vcpu, VCPU_REGS_RIP);
 

Perhaps we ought to have a guest_rip_read() since rip is not truly a GPR.

arch/x86/kvm/kvm_cache_regs.h static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · line 2383

Quoted 9 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +0 -1
 static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
@@ -2370,22 +2379,18 @@ static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 			    (u32)((u64)vcpu->arch.regs[reg] >> 32), handler);
 		switch (cr) {
 		case 0:
-			vcpu_load_rsp_rip(vcpu);
 			kvm_set_cr0(vcpu, vcpu->arch.regs[reg]);
 

What if reg points at rsp? You need to replace arch.regs[*] with the accessor.

arch/x86/kvm/kvm_cache_regs.h static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid, · line 66

Quoted 6 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +1 -0
@@ -61,6 +62,7 @@ static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
 				    struct kvm_cpuid_entry2 __user *entries);
 
 struct kvm_x86_ops *kvm_x86_ops;
+EXPORT_SYMBOL(kvm_x86_ops);
 

_GPL

arch/x86/kvm/kvm_cache_regs.h static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,

Quoted 10 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +9 -0
+static void flush_regs(struct kvm_vcpu *vcpu)
+{
+	if (__test_and_clear_bit(VCPU_REGS_RSP, &vcpu->arch.regs_dirty))
+		kvm_x86_ops->decache_regs(vcpu, VCPU_REGS_RSP);
+	if (__test_and_clear_bit(VCPU_REGS_RIP, &vcpu->arch.regs_dirty))
+		kvm_x86_ops->decache_regs(vcpu, VCPU_REGS_RIP);
+	if (__test_and_clear_bit(VCPU_REGS_RAX, &vcpu->arch.regs_dirty))
+		kvm_x86_ops->decache_regs(vcpu, VCPU_REGS_RAX);
+}

This is better done in $subarch_vcpu_run, as it avoids callbacks and knows exactly which regs to look at. We can do it unconditionally for svm, too.

Quoted 9 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +7 -0
+void cache_all_regs(struct kvm_vcpu *vcpu)
+{
+	kvm_x86_ops->cache_regs(vcpu, VCPU_REGS_RAX);
+	kvm_x86_ops->cache_regs(vcpu, VCPU_REGS_RSP);
+	kvm_x86_ops->cache_regs(vcpu, VCPU_REGS_RIP);
+}
+

arch/x86/kvm/kvm_cache_regs.h

Quoted 10 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +10 -0
+void decache_all_regs(struct kvm_vcpu *vcpu)
+{
+	guest_register_write(vcpu, VCPU_REGS_RAX,
+			     vcpu->arch.regs[VCPU_REGS_RAX]);
+	guest_register_write(vcpu, VCPU_REGS_RSP,
+			     vcpu->arch.regs[VCPU_REGS_RSP]);
+	guest_register_write(vcpu, VCPU_REGS_RIP,
+			     vcpu->arch.regs[VCPU_REGS_RIP]);
+}
+

static

arch/x86/kvm/kvm_cache_regs.h again: · line 2909

Quoted 7 lines
arch/x86/kvm/kvm_cache_regs.h renamed from null +2 -0
@@ -2865,6 +2904,8 @@ again:
 	local_irq_enable();
 
 	++vcpu->stat.exits;
+	vcpu->arch.regs_available = KVM_CACHED_REGS;
+	vcpu->arch.regs_dirty = 0;
 

How can you have a constant for this? Each subarch has different cached regs. This ought to be set in $subarch_vcpu_run.

Quoted 14 lines
include/asm-x86/kvm_host.h +1 -1
index 851184d..cc5c94b 100644
@@ -87,7 +87,7 @@ extern struct list_head vm_list;
 struct kvm_vcpu;
 struct kvm;
 
-enum {
+enum kvm_reg {
 	VCPU_REGS_RAX = 0,
 	VCPU_REGS_RCX = 1,
 	VCPU_REGS_RDX = 2,

include/asm-x86/kvm_host.h enum { · line 112

Quoted 7 lines
include/asm-x86/kvm_host.h +1 -0
@@ -106,9 +106,21 @@ enum {
 	VCPU_REGS_R14 = 14,
 	VCPU_REGS_R15 = 15,
 #endif
+	VCPU_REGS_RIP = 16,
 	NR_VCPU_REGS
 };
 

No, rip is not a GPR.

include/asm-x86/kvm_host.h enum {

Quoted 13 lines
include/asm-x86/kvm_host.h +11 -0
+/*
+ * List of registers already read by kvm_x86_ops->run().
+ */
+#define KVM_CACHED_REGS ((1 << VCPU_REGS_RCX) | (1 << VCPU_REGS_RDX) |	 	\
+			(1 << VCPU_REGS_RBX)  | (1 << VCPU_REGS_RBP) | 		\
+			(1 << VCPU_REGS_RSI)  | (1 << VCPU_REGS_RDI) | 		\
+			(1 << VCPU_REGS_R8)   | (1 << VCPU_REGS_R9)  | 		\
+			(1 << VCPU_REGS_R10)  | (1 << VCPU_REGS_R11) | 		\
+			(1 << VCPU_REGS_R12)  | (1 << VCPU_REGS_R13) | 		\
+			(1 << VCPU_REGS_R14)  | (1 << VCPU_REGS_R15))
+
 

As mentioned earlier, this is subarch specific (and better written as ~(1 << VCPU_REGS_R?X)).

include/asm-x86/kvm_host.h struct kvm_x86_ops { · line 430

Quoted 11 lines
include/asm-x86/kvm_host.h +2 -2
@@ -410,8 +423,8 @@ struct kvm_x86_ops {
 	unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
 	void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
 		       int *exception);
-	void (*cache_regs)(struct kvm_vcpu *vcpu);
-	void (*decache_regs)(struct kvm_vcpu *vcpu);
+	void (*cache_regs)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
+	void (*decache_regs)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
 	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
 

Remove the plural s as the callbacks now affect a single register.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#5 lore raw
Quoted 13 lines · Avi Kivity

arch/x86/kvm/vmx.c static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) · line 2383

Quoted 10 lines

static int set_guest_debug(struct kvm_vcpu *vcpu, struct
kvm_debug_guest *dbg)

arch/x86/kvm/vmx.c +0 -1
@@ -2370,22 +2379,18 @@ static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 			    (u32)((u64)vcpu->arch.regs[reg] >> 32), handler);
 		switch (cr) {
 		case 0:
-			vcpu_load_rsp_rip(vcpu);
 			kvm_set_cr0(vcpu, vcpu->arch.regs[reg]);
 

What if reg points at rsp? You need to replace arch.regs[*] with the accessor.

Catch!

Quoted 9 lines

arch/x86/kvm/x86.c again: · line 2908

Quoted 6 lines
arch/x86/kvm/x86.c +2 -0
@@ -2865,6 +2904,8 @@ again:
 	local_irq_enable();
  	++vcpu->stat.exits;
+	vcpu->arch.regs_available = KVM_CACHED_REGS;
+	vcpu->arch.regs_dirty = 0;
 

How can you have a constant for this? Each subarch has different cached regs. This ought to be set in $subarch_vcpu_run.

This is the intersection of registers cached by both architectures. But I agree that moving it down to subarch code is saner.

Quoted 14 lines
Quoted 14 lines
include/asm-x86/kvm_host.h +1 -0
index 851184d..cc5c94b 100644
@@ -87,7 +87,7 @@ extern struct list_head vm_list;
 struct kvm_vcpu;
 struct kvm;
 -enum {
+enum kvm_reg {
 	VCPU_REGS_RAX = 0,
 	VCPU_REGS_RCX = 1,
 	VCPU_REGS_RDX = 2,
@@ -106,9 +106,21 @@ enum {
 	VCPU_REGS_R14 = 14,
Quoted 8 lines

include/asm-x86/kvm_host.h enum { · line 112

Quoted 6 lines
include/asm-x86/kvm_host.h +1 -0
 	VCPU_REGS_R15 = 15,
 #endif
+	VCPU_REGS_RIP = 16,
 	NR_VCPU_REGS
 };
 

No, rip is not a GPR.

We need the RIP index to be part of the kvm_reg space (to index in the dirty/available bitmaps). Otherwise you have to special case it.

Sure it is not a GPR, but what is the problem storing RIP in regs array instead of a separate variable ?

Quoted 1 line

Perhaps we ought to have a guest_rip_read() since rip is not truly a GPR.

#define kvm_guest_read_rip(vcpu) kvm_guest_register_read(vcpu, VCPU_REGS_RIP)

?

Will fix the remaining comments.

#6 lore raw
In reply to Avi Kivity
Quoted 2 lines · Avi Kivity

A new header file is excessive. Also, these are global names, so please prefix with kvm_.

The reason for a separate header is because these accessors need both kvm_vcpu (linux/kvm_host.h) and kvm_vcpu_arch (asm/kvm_host.h).

I couldnt think of a better location to put them. Ideas?

Quoted 10 lines

Review static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 248

Quoted 8 lines
(quoted diff) +2 -0
@@ -241,7 +242,8 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
 		       svm->vmcb->save.rip,
 		       svm->next_rip);
 -	vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
+	svm->vmcb->save.rip = svm->next_rip;
+	guest_register_write(vcpu, VCPU_REGS_RIP, svm->vmcb->save.rip);
 	svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
 

No need to write into save.rip, is there?

I'm not sure why it was there in the first place. Perhaps there's code which reads svm->vmcb->save.rip directly as the current RIP after this point?

Also "kvm_guest_register_read" is too long, so I dropped "guest".

arch/x86/kvm/kvm_cache_regs.h new file +36 -0
@@ -0,0 +1,36 @@
+#ifndef ASM_KVM_CACHE_REGS_H
+#define ASM_KVM_CACHE_REGS_H
+
+static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu,
+					      enum kvm_reg reg)
+{
+	if (!__test_and_set_bit(reg, &vcpu->arch.regs_available))
+		kvm_x86_ops->cache_reg(vcpu, reg);
+
+	return vcpu->arch.regs[reg];
+}
+
+static inline void kvm_register_write(struct kvm_vcpu *vcpu,
+				      enum kvm_reg reg,
+				      unsigned long val)
+{
+	vcpu->arch.regs[reg] = val;
+	__set_bit(reg, &vcpu->arch.regs_dirty);
+}
+
+static inline unsigned long kvm_rip_read(struct kvm_vcpu *vcpu)
+{
+	if (!__test_and_set_bit(VCPU_REGS_RIP, &vcpu->arch.regs_available))
+		kvm_x86_ops->cache_reg(vcpu, VCPU_REGS_RIP);
+
+	return vcpu->arch.rip;
+}
+
+static inline void kvm_rip_write(struct kvm_vcpu *vcpu,
+			 	 unsigned long val)
+{
+	vcpu->arch.rip = val;
+	__set_bit(VCPU_REGS_RIP, &vcpu->arch.regs_dirty);
+}
+
+#endif
arch/x86/kvm/lapic.c +2 -2
index 73f43de..9fde0ac 100644
@@ -32,6 +32,7 @@
 #include <asm/current.h>
 #include <asm/apicdef.h>
 #include <asm/atomic.h>
+#include "kvm_cache_regs.h"
 #include "irq.h"
 
 #define PRId64 "d"
@@ -558,8 +559,7 @@ static void __report_tpr_access(struct kvm_lapic *apic, bool write)
 	struct kvm_run *run = vcpu->run;
 
 	set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
-	kvm_x86_ops->cache_regs(vcpu);
-	run->tpr_access.rip = vcpu->arch.rip;
+	run->tpr_access.rip = kvm_rip_read(vcpu);
 	run->tpr_access.is_write = write;
 }
 
arch/x86/kvm/svm.c +52 -11

112 lines not on this page · raw message

arch/x86/kvm/vmx.c +58 -46

222 lines not on this page · raw message

arch/x86/kvm/x86.c +143 -124

450 lines not on this page · raw message

arch/x86/kvm/x86_emulate.c +10 -9
index 38926b7..64fe207 100644
@@ -26,6 +26,7 @@
 #define DPRINTF(_f, _a ...) printf(_f , ## _a)
 #else
 #include <linux/kvm_host.h>
+#include "kvm_cache_regs.h"
 #define DPRINTF(x...) do {} while (0)
 #endif
 #include <linux/module.h>
@@ -806,7 +807,7 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	/* Shadow copy of register state. Committed on successful emulation. */
 
 	memset(c, 0, sizeof(struct decode_cache));
-	c->eip = ctxt->vcpu->arch.rip;
+	c->eip = kvm_rip_read(ctxt->vcpu);
 	memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
 
 	switch (mode) {
@@ -1245,7 +1246,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	if (c->rep_prefix && (c->d & String)) {
 		/* All REP prefixes have the same first termination condition */
 		if (c->regs[VCPU_REGS_RCX] == 0) {
-			ctxt->vcpu->arch.rip = c->eip;
+			kvm_rip_write(ctxt->vcpu, c->eip);
 			goto done;
 		}
 		/* The second termination condition only applies for REPE
@@ -1259,17 +1260,17 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 				(c->b == 0xae) || (c->b == 0xaf)) {
 			if ((c->rep_prefix == REPE_PREFIX) &&
 				((ctxt->eflags & EFLG_ZF) == 0)) {
-					ctxt->vcpu->arch.rip = c->eip;
+					kvm_rip_write(ctxt->vcpu, c->eip);
 					goto done;
 			}
 			if ((c->rep_prefix == REPNE_PREFIX) &&
 				((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
-				ctxt->vcpu->arch.rip = c->eip;
+				kvm_rip_write(ctxt->vcpu, c->eip);
 				goto done;
 			}
 		}
 		c->regs[VCPU_REGS_RCX]--;
-		c->eip = ctxt->vcpu->arch.rip;
+		c->eip = kvm_rip_read(ctxt->vcpu);
 	}
 
 	if (c->src.type == OP_MEM) {
@@ -1750,7 +1751,7 @@ writeback:
 
 	/* Commit shadow register state. */
 	memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-	ctxt->vcpu->arch.rip = c->eip;
+	kvm_rip_write(ctxt->vcpu, c->eip);
 
 done:
 	if (rc == X86EMUL_UNHANDLEABLE) {
@@ -1775,7 +1776,7 @@ twobyte_insn:
 				goto done;
 
 			/* Let the processor re-execute the fixed hypercall */
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = kvm_rip_read(ctxt->vcpu);
 			/* Disable writeback. */
 			c->dst.type = OP_NONE;
 			break;
@@ -1871,7 +1872,7 @@ twobyte_insn:
 		rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
 		if (rc) {
 			kvm_inject_gp(ctxt->vcpu, 0);
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = kvm_rip_read(ctxt->vcpu);
 		}
 		rc = X86EMUL_CONTINUE;
 		c->dst.type = OP_NONE;
@@ -1881,7 +1882,7 @@ twobyte_insn:
 		rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
 		if (rc) {
 			kvm_inject_gp(ctxt->vcpu, 0);
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = kvm_rip_read(ctxt->vcpu);
 		} else {
 			c->regs[VCPU_REGS_RAX] = (u32)msr_data;
 			c->regs[VCPU_REGS_RDX] = msr_data >> 32;
include/asm-x86/kvm_host.h +13 -5
index 851184d..515add9 100644
@@ -87,7 +87,7 @@ extern struct list_head vm_list;
 struct kvm_vcpu;
 struct kvm;
 
-enum {
+enum kvm_reg {
 	VCPU_REGS_RAX = 0,
 	VCPU_REGS_RCX = 1,
 	VCPU_REGS_RDX = 2,
@@ -109,6 +109,8 @@ enum {
 	NR_VCPU_REGS
 };
 
+#define VCPU_REGS_RIP NR_VCPU_REGS
+
 enum {
 	VCPU_SREG_ES,
 	VCPU_SREG_CS,
@@ -217,8 +219,14 @@ struct kvm_vcpu_arch {
 	int interrupt_window_open;
 	unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
 	DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
-	unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
-	unsigned long rip;      /* needs vcpu_load_rsp_rip() */
+	/*
+ 	 * rip and regs accesses must go through
+ 	 * kvm_{register,rip}_{read,write} functions.
+ 	 */
+	unsigned long regs[NR_VCPU_REGS];
+	unsigned long rip;
+	u32 regs_available;
+	u32 regs_dirty;
 
 	unsigned long cr0;
 	unsigned long cr2;
@@ -410,8 +418,8 @@ struct kvm_x86_ops {
 	unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
 	void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
 		       int *exception);
-	void (*cache_regs)(struct kvm_vcpu *vcpu);
-	void (*decache_regs)(struct kvm_vcpu *vcpu);
+	void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
+	void (*decache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
 	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
 
#7 lore raw
Quoted 20 lines · Marcelo Tosatti
Quoted 2 lines · Avi Kivity

A new header file is excessive. Also, these are global names, so please prefix with kvm_.

The reason for a separate header is because these accessors need both kvm_vcpu (linux/kvm_host.h) and kvm_vcpu_arch (asm/kvm_host.h).

I couldnt think of a better location to put them. Ideas?

Quoted 10 lines

Review static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 248

Quoted 8 lines
(quoted diff) +2 -0
@@ -241,7 +242,8 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
 		       svm->vmcb->save.rip,
 		       svm->next_rip);
 -	vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
+	svm->vmcb->save.rip = svm->next_rip;
+	guest_register_write(vcpu, VCPU_REGS_RIP, svm->vmcb->save.rip);
 	svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
 

No need to write into save.rip, is there?

Quoted 5 lines

I'm not sure why it was there in the first place. Perhaps there's code which reads svm->vmcb->save.rip directly as the current RIP after this point?

Also "kvm_guest_register_read" is too long, so I dropped "guest".

There was a missing convertion in vmx_vcpu_reset(). And haven't tested AMD.

arch/x86/kvm/kvm_cache_regs.h new file +36 -0
@@ -0,0 +1,36 @@
+#ifndef ASM_KVM_CACHE_REGS_H
+#define ASM_KVM_CACHE_REGS_H
+
+static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu,
+					      enum kvm_reg reg)
+{
+	if (!__test_and_set_bit(reg, &vcpu->arch.regs_available))
+		kvm_x86_ops->cache_reg(vcpu, reg);
+
+	return vcpu->arch.regs[reg];
+}
+
+static inline void kvm_register_write(struct kvm_vcpu *vcpu,
+				      enum kvm_reg reg,
+				      unsigned long val)
+{
+	vcpu->arch.regs[reg] = val;
+	__set_bit(reg, &vcpu->arch.regs_dirty);
+}
+
+static inline unsigned long kvm_rip_read(struct kvm_vcpu *vcpu)
+{
+	if (!__test_and_set_bit(VCPU_REGS_RIP, &vcpu->arch.regs_available))
+		kvm_x86_ops->cache_reg(vcpu, VCPU_REGS_RIP);
+
+	return vcpu->arch.rip;
+}
+
+static inline void kvm_rip_write(struct kvm_vcpu *vcpu,
+			 	 unsigned long val)
+{
+	vcpu->arch.rip = val;
+	__set_bit(VCPU_REGS_RIP, &vcpu->arch.regs_dirty);
+}
+
+#endif
arch/x86/kvm/lapic.c +2 -2
index 73f43de..9fde0ac 100644
@@ -32,6 +32,7 @@
 #include <asm/current.h>
 #include <asm/apicdef.h>
 #include <asm/atomic.h>
+#include "kvm_cache_regs.h"
 #include "irq.h"
 
 #define PRId64 "d"
@@ -558,8 +559,7 @@ static void __report_tpr_access(struct kvm_lapic *apic, bool write)
 	struct kvm_run *run = vcpu->run;
 
 	set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
-	kvm_x86_ops->cache_regs(vcpu);
-	run->tpr_access.rip = vcpu->arch.rip;
+	run->tpr_access.rip = kvm_rip_read(vcpu);
 	run->tpr_access.is_write = write;
 }
 
arch/x86/kvm/svm.c +52 -11

112 lines not on this page · raw message

arch/x86/kvm/vmx.c +61 -49

235 lines not on this page · raw message

arch/x86/kvm/x86.c +143 -124

450 lines not on this page · raw message

arch/x86/kvm/x86_emulate.c +10 -9
index 38926b7..64fe207 100644
@@ -26,6 +26,7 @@
 #define DPRINTF(_f, _a ...) printf(_f , ## _a)
 #else
 #include <linux/kvm_host.h>
+#include "kvm_cache_regs.h"
 #define DPRINTF(x...) do {} while (0)
 #endif
 #include <linux/module.h>
@@ -806,7 +807,7 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	/* Shadow copy of register state. Committed on successful emulation. */
 
 	memset(c, 0, sizeof(struct decode_cache));
-	c->eip = ctxt->vcpu->arch.rip;
+	c->eip = kvm_rip_read(ctxt->vcpu);
 	memcpy(c->regs, ctxt->vcpu->arch.regs, sizeof c->regs);
 
 	switch (mode) {
@@ -1245,7 +1246,7 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 	if (c->rep_prefix && (c->d & String)) {
 		/* All REP prefixes have the same first termination condition */
 		if (c->regs[VCPU_REGS_RCX] == 0) {
-			ctxt->vcpu->arch.rip = c->eip;
+			kvm_rip_write(ctxt->vcpu, c->eip);
 			goto done;
 		}
 		/* The second termination condition only applies for REPE
@@ -1259,17 +1260,17 @@ x86_emulate_insn(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 				(c->b == 0xae) || (c->b == 0xaf)) {
 			if ((c->rep_prefix == REPE_PREFIX) &&
 				((ctxt->eflags & EFLG_ZF) == 0)) {
-					ctxt->vcpu->arch.rip = c->eip;
+					kvm_rip_write(ctxt->vcpu, c->eip);
 					goto done;
 			}
 			if ((c->rep_prefix == REPNE_PREFIX) &&
 				((ctxt->eflags & EFLG_ZF) == EFLG_ZF)) {
-				ctxt->vcpu->arch.rip = c->eip;
+				kvm_rip_write(ctxt->vcpu, c->eip);
 				goto done;
 			}
 		}
 		c->regs[VCPU_REGS_RCX]--;
-		c->eip = ctxt->vcpu->arch.rip;
+		c->eip = kvm_rip_read(ctxt->vcpu);
 	}
 
 	if (c->src.type == OP_MEM) {
@@ -1750,7 +1751,7 @@ writeback:
 
 	/* Commit shadow register state. */
 	memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-	ctxt->vcpu->arch.rip = c->eip;
+	kvm_rip_write(ctxt->vcpu, c->eip);
 
 done:
 	if (rc == X86EMUL_UNHANDLEABLE) {
@@ -1775,7 +1776,7 @@ twobyte_insn:
 				goto done;
 
 			/* Let the processor re-execute the fixed hypercall */
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = kvm_rip_read(ctxt->vcpu);
 			/* Disable writeback. */
 			c->dst.type = OP_NONE;
 			break;
@@ -1871,7 +1872,7 @@ twobyte_insn:
 		rc = kvm_set_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], msr_data);
 		if (rc) {
 			kvm_inject_gp(ctxt->vcpu, 0);
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = kvm_rip_read(ctxt->vcpu);
 		}
 		rc = X86EMUL_CONTINUE;
 		c->dst.type = OP_NONE;
@@ -1881,7 +1882,7 @@ twobyte_insn:
 		rc = kvm_get_msr(ctxt->vcpu, c->regs[VCPU_REGS_RCX], &msr_data);
 		if (rc) {
 			kvm_inject_gp(ctxt->vcpu, 0);
-			c->eip = ctxt->vcpu->arch.rip;
+			c->eip = kvm_rip_read(ctxt->vcpu);
 		} else {
 			c->regs[VCPU_REGS_RAX] = (u32)msr_data;
 			c->regs[VCPU_REGS_RDX] = msr_data >> 32;
include/asm-x86/kvm_host.h +13 -5
index 851184d..515add9 100644
@@ -87,7 +87,7 @@ extern struct list_head vm_list;
 struct kvm_vcpu;
 struct kvm;
 
-enum {
+enum kvm_reg {
 	VCPU_REGS_RAX = 0,
 	VCPU_REGS_RCX = 1,
 	VCPU_REGS_RDX = 2,
@@ -109,6 +109,8 @@ enum {
 	NR_VCPU_REGS
 };
 
+#define VCPU_REGS_RIP NR_VCPU_REGS
+
 enum {
 	VCPU_SREG_ES,
 	VCPU_SREG_CS,
@@ -217,8 +219,14 @@ struct kvm_vcpu_arch {
 	int interrupt_window_open;
 	unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
 	DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
-	unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
-	unsigned long rip;      /* needs vcpu_load_rsp_rip() */
+	/*
+ 	 * rip and regs accesses must go through
+ 	 * kvm_{register,rip}_{read,write} functions.
+ 	 */
+	unsigned long regs[NR_VCPU_REGS];
+	unsigned long rip;
+	u32 regs_available;
+	u32 regs_dirty;
 
 	unsigned long cr0;
 	unsigned long cr2;
@@ -410,8 +418,8 @@ struct kvm_x86_ops {
 	unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
 	void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
 		       int *exception);
-	void (*cache_regs)(struct kvm_vcpu *vcpu);
-	void (*decache_regs)(struct kvm_vcpu *vcpu);
+	void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
+	void (*decache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
 	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);