Skip to contentSkip to conversation

KVM: VMX: Enable XSAVE/XRSTOR for guest

PATCH Superseded Sheng Yang 3 participants 4 messages May 31, 2010 – Jun 1, 2010

Subsystems (3)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Sheng Yang

author
#0 lore raw

PATCH v7 1/1 KVM: VMX: Enable XSAVE/XRSTOR for guest

From: Dexuan Cui <[email protected]>

This patch enable guest to use XSAVE/XRSTOR instructions.

We assume that host_xcr0 would use all possible bits that OS supported.

And we loaded xcr0 in the same way we handled fpu - do it as late as we can.

---

Change from v6:

Make kvm_set_xcr() generically.

 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/include/asm/vmx.h      |    1 +
 arch/x86/kvm/kvm_cache_regs.h   |    6 ++
 arch/x86/kvm/vmx.c              |   13 ++++
 arch/x86/kvm/x86.c              |  126 ++++++++++++++++++++++++++++++++++++--
 include/linux/kvm_host.h        |    2 +-
 6 files changed, 142 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index d08bb4a..271487b 100644
@@ -302,6 +302,7 @@ struct kvm_vcpu_arch {
 	} update_pte;
 
 	struct fpu guest_fpu;
+	u64 xcr0;
 
 	gva_t mmio_fault_cr2;
 	struct kvm_pio_request pio;
@@ -605,6 +606,7 @@ int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
+void kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr);
 
 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
arch/x86/include/asm/vmx.h +1 -0
index 9e6779f..346ea66 100644
@@ -266,6 +266,7 @@ enum vmcs_field {
 #define EXIT_REASON_EPT_VIOLATION       48
 #define EXIT_REASON_EPT_MISCONFIG       49
 #define EXIT_REASON_WBINVD		54
+#define EXIT_REASON_XSETBV		55
 
 /*
  * Interruption-information format
arch/x86/kvm/kvm_cache_regs.h +6 -0
index d2a98f8..6491ac8 100644
@@ -71,4 +71,10 @@ static inline ulong kvm_read_cr4(struct kvm_vcpu *vcpu)
 	return kvm_read_cr4_bits(vcpu, ~0UL);
 }
 
+static inline u64 kvm_read_edx_eax(struct kvm_vcpu *vcpu)
+{
+	return (kvm_register_read(vcpu, VCPU_REGS_RAX) & -1u)
+		| ((u64)(kvm_register_read(vcpu, VCPU_REGS_RDX) & -1u) << 32);
+}
+
 #endif
arch/x86/kvm/vmx.c +13 -0
index 99ae513..8649627 100644
@@ -36,6 +36,8 @@
 #include <asm/vmx.h>
 #include <asm/virtext.h>
 #include <asm/mce.h>
+#include <asm/i387.h>
+#include <asm/xcr.h>
 
 #include "trace.h"
 
@@ -3354,6 +3356,16 @@ static int handle_wbinvd(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = kvm_read_edx_eax(vcpu);
+	u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
+
+	kvm_set_xcr(vcpu, index, new_bv);
+	skip_emulated_instruction(vcpu);
+	return 1;
+}
+
 static int handle_apic_access(struct kvm_vcpu *vcpu)
 {
 	return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
@@ -3632,6 +3644,7 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[EXIT_REASON_TPR_BELOW_THRESHOLD]     = handle_tpr_below_threshold,
 	[EXIT_REASON_APIC_ACCESS]             = handle_apic_access,
 	[EXIT_REASON_WBINVD]                  = handle_wbinvd,
+	[EXIT_REASON_XSETBV]                  = handle_xsetbv,
 	[EXIT_REASON_TASK_SWITCH]             = handle_task_switch,
 	[EXIT_REASON_MCE_DURING_VMENTRY]      = handle_machine_check,
 	[EXIT_REASON_EPT_VIOLATION]	      = handle_ept_violation,
arch/x86/kvm/x86.c +119 -7

228 lines not on this page · raw message

include/linux/kvm_host.h +1 -1
index 4e8fdbf..3784d58 100644
@@ -88,7 +88,7 @@ struct kvm_vcpu {
 	int srcu_idx;
 
 	int fpu_active;
-	int guest_fpu_loaded;
+	int guest_fpu_loaded, guest_xcr0_loaded;
 	wait_queue_head_t wq;
 	int sigset_active;
 	sigset_t sigset;
Signature · 1 line
1.7.0.1
Reviewed-by on v7 1/1
#1 lore raw
Quoted 9 lines · Sheng Yang

From: Dexuan Cui<[email protected]>

This patch enable guest to use XSAVE/XRSTOR instructions.

We assume that host_xcr0 would use all possible bits that OS supported.

And we loaded xcr0 in the same way we handled fpu - do it as late as we can.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
In reply to Sheng Yang
Quoted 14 lines · Sheng Yang
arch/x86/kvm/vmx.c +2 -0
index 99ae513..8649627 100644
@@ -36,6 +36,8 @@
 #include <asm/vmx.h>
 #include <asm/virtext.h>
 #include <asm/mce.h>
+#include <asm/i387.h>
+#include <asm/xcr.h>
 
 #include "trace.h"
 

arch/x86/kvm/vmx.c static int handle_wbinvd(struct kvm_vcpu *vcpu) · line 3365

Quoted 10 lines
arch/x86/kvm/vmx.c +7 -0
@@ -3354,6 +3356,16 @@ static int handle_wbinvd(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = kvm_read_edx_eax(vcpu);
+	u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
+
+	kvm_set_xcr(vcpu, index, new_bv);
+	skip_emulated_instruction(vcpu);

Should only skip_emulated_instruction if no exception was injected.

Quoted 13 lines
arch/x86/kvm/x86.c +8 -0
index 7be1d36..0fcd8de 100644
 
+int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
+{
+	u64 xcr0;
+
+	/* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now  */
+	if (index != XCR_XFEATURE_ENABLED_MASK)
+		return 1;
+	xcr0 = xcr;

arch/x86/kvm/x86.c void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) · line 501

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+	if (kvm_x86_ops->get_cpl(vcpu) != 0)
+		return 1;
+	if (!(xcr0 & XSTATE_FP))
+		return 1;
+	if ((xcr0 & XSTATE_YMM) && !(xcr0 & XSTATE_SSE))
+		return 1;
+	if (xcr0 & ~host_xcr0)
+		return 1;
+	vcpu->arch.xcr0 = xcr0;
+	xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);

Won't this happen on guest entry, since vcpu->guest_xcr0_loaded == 0?

arch/x86/kvm/x86.c static void inject_pending_event(struct kvm_vcpu *vcpu) · line 4609

Quoted 9 lines
arch/x86/kvm/x86.c +5 -0
@@ -4522,6 +4602,24 @@ static void inject_pending_event(struct kvm_vcpu *vcpu)
 	}
 }
 
+static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu)
+{
+	if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
+			!vcpu->guest_xcr0_loaded) {
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);

Only necessary if guest and hosts XCR0 differ?

arch/x86/kvm/x86.c static void inject_pending_event(struct kvm_vcpu *vcpu) · line 4621

Quoted 12 lines
arch/x86/kvm/x86.c +12 -0
+		vcpu->guest_xcr0_loaded = 1;
+	}
+}
+
+static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
+{
+	if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) &&
+			vcpu->guest_xcr0_loaded) {
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
+		vcpu->guest_xcr0_loaded = 0;
+	}
+}

What if you load guest's XCR0, then guest clears CR4.OSXSAVE? (restore of host_xcr0 should be conditional on guest_xcr0_loaded only).

Quoted 38 lines
arch/x86/kvm/x86.c +13 -0
+
 static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 {
 	int r;
@@ -4567,6 +4665,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	kvm_x86_ops->prepare_guest_switch(vcpu);
 	if (vcpu->fpu_active)
 		kvm_load_guest_fpu(vcpu);
+	kvm_load_guest_xcr0(vcpu);
 
 	atomic_set(&vcpu->guest_mode, 1);
 	smp_wmb();
@@ -5118,6 +5217,11 @@ void fx_init(struct kvm_vcpu *vcpu)
 	fpu_alloc(&vcpu->arch.guest_fpu);
 	fpu_finit(&vcpu->arch.guest_fpu);
 
+	/*
+	 * Ensure guest xcr0 is valid for loading
+	 */
+	vcpu->arch.xcr0 = XSTATE_FP;
+
 	vcpu->arch.cr0 |= X86_CR0_ET;
 }
 EXPORT_SYMBOL_GPL(fx_init);
@@ -5132,6 +5236,12 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 	if (vcpu->guest_fpu_loaded)
 		return;
 
+	/*
+	 * Restore all possible states in the guest,
+	 * and assume host would use all available bits.
+	 * Guest xcr0 would be loaded later.
+	 */
+	kvm_put_guest_xcr0(vcpu);
 	vcpu->guest_fpu_loaded = 1;
 	unlazy_fpu(current);
 	fpu_restore_checking(&vcpu->arch.guest_fpu);

arch/x86/kvm/x86.c void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) · line 5254

Quoted 5 lines
arch/x86/kvm/x86.c +2 -0
@@ -5140,6 +5250,8 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
 {
+	kvm_put_guest_xcr0(vcpu);
+

Should be in kvm_arch_vcpu_put?

Quoted 23 lines
arch/x86/kvm/x86.c +0 -0
 	if (!vcpu->guest_fpu_loaded)
 		return;
 
include/linux/kvm_host.h +1 -1
index 4e8fdbf..3784d58 100644
@@ -88,7 +88,7 @@ struct kvm_vcpu {
 	int srcu_idx;
 
 	int fpu_active;
-	int guest_fpu_loaded;
+	int guest_fpu_loaded, guest_xcr0_loaded;
 	wait_queue_head_t wq;
 	int sigset_active;
 	sigset_t sigset;
Signature · 6 lines
1.7.0.1

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
Quoted 19 lines · Marcelo Tosatti
Quoted 17 lines · Sheng Yang
arch/x86/kvm/vmx.c +2 -0
index 99ae513..8649627 100644
@@ -36,6 +36,8 @@
  #include<asm/vmx.h>
  #include<asm/virtext.h>
  #include<asm/mce.h>
+#include<asm/i387.h>
+#include<asm/xcr.h>
 
  #include "trace.h"
 
@@ -3354,6 +3356,16 @@ static int handle_wbinvd(struct kvm_vcpu *vcpu)
  	return 1;
  }
 
Quoted 10 lines

arch/x86/kvm/vmx.c static int handle_wbinvd(struct kvm_vcpu *vcpu)

Quoted 8 lines
arch/x86/kvm/vmx.c +7 -0
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = kvm_read_edx_eax(vcpu);
+	u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
+
+	kvm_set_xcr(vcpu, index, new_bv);
+	skip_emulated_instruction(vcpu);
 

Should only skip_emulated_instruction if no exception was injected.

Good catch. So, unit testing failure cases _is_ important.

Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/vmx.c +12 -0
+int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
+{
+	u64 xcr0;
+
+	/* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now  */
+	if (index != XCR_XFEATURE_ENABLED_MASK)
+		return 1;
+	xcr0 = xcr;
+	if (kvm_x86_ops->get_cpl(vcpu) != 0)
+		return 1;
+	if (!(xcr0&  XSTATE_FP))
+		return 1;
Quoted 9 lines

arch/x86/kvm/vmx.c

Quoted 7 lines
arch/x86/kvm/vmx.c +6 -0
+	if ((xcr0&  XSTATE_YMM)&&  !(xcr0&  XSTATE_SSE))
+		return 1;
+	if (xcr0&  ~host_xcr0)
+		return 1;
+	vcpu->arch.xcr0 = xcr0;
+	xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);

Won't this happen on guest entry, since vcpu->guest_xcr0_loaded == 0?

In fact we don't know what vcpu->guest_xcr0_loaded is. Best to unload it explicitly (and drop the xsetbv() call).

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +7 -0
+		vcpu->guest_xcr0_loaded = 1;
+	}
+}
+
+static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)
+{
+	if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)&&
Quoted 9 lines

arch/x86/kvm/vmx.c

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+			vcpu->guest_xcr0_loaded) {
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
+		vcpu->guest_xcr0_loaded = 0;
+	}
+}

What if you load guest's XCR0, then guest clears CR4.OSXSAVE? (restore of host_xcr0 should be conditional on guest_xcr0_loaded only).

Good catch again.

Quoted 9 lines

arch/x86/kvm/vmx.c void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) · line 5255

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
@@ -5140,6 +5250,8 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
  void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
 {
+	kvm_put_guest_xcr0(vcpu);
+
 

Should be in kvm_arch_vcpu_put?

That's called unconditionally from kvm_arch_vcpu_put(), so equivalent.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.