Skip to contentSkip to conversation

KVM: VMX: Enable XSAVE/XRSTORE for guest

PATCH Superseded Sheng Yang 2 participants 8 messages May 19, 2010 – May 21, 2010

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Sheng Yang

author
#0 lore raw

PATCH v2 1/1 KVM: VMX: Enable XSAVE/XRSTORE for guest

From: Dexuan Cui <[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V1:

1. Use FPU API.
2. Fix CPUID issue.
3. Save/restore all possible guest xstate fields when switching. Because we don't know which fields guest has already touched.

---
 arch/x86/include/asm/kvm_host.h |    1 +
 arch/x86/include/asm/vmx.h      |    1 +
 arch/x86/kvm/vmx.c              |   28 +++++++++++++
 arch/x86/kvm/x86.c              |   85 +++++++++++++++++++++++++++++++++++---
 4 files changed, 108 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index d08bb4a..78d7b06 100644
@@ -302,6 +302,7 @@ struct kvm_vcpu_arch {
 	} update_pte;
 
 	struct fpu guest_fpu;
+	uint64_t xcr0, host_xcr0;
 
 	gva_t mmio_fault_cr2;
 	struct kvm_pio_request pio;
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/vmx.c +28 -0
index 99ae513..2ee8ff6 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"
 
@@ -2616,6 +2618,8 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 	vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
 	if (enable_ept)
 		vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
+	if (cpu_has_xsave)
+		vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_OSXSAVE;
 	vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
 
 	tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
@@ -3354,6 +3358,29 @@ static int handle_wbinvd(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = ((u64)(kvm_register_read(vcpu, VCPU_REGS_RDX) << 32)) |
+		kvm_register_read(vcpu, VCPU_REGS_RAX);
+
+	if (kvm_register_read(vcpu, VCPU_REGS_RCX) != 0)
+		goto err;
+	if (vmx_get_cpl(vcpu) != 0)
+		goto err;
+	if (!(new_bv & XSTATE_FP) ||
+	     (new_bv & ~vcpu->arch.host_xcr0))
+		goto err;
+	if ((new_bv & XSTATE_YMM) && !(new_bv & XSTATE_SSE))
+		goto err;
+	vcpu->arch.xcr0 = new_bv;
+	xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
+	skip_emulated_instruction(vcpu);
+	return 1;
+err:
+	kvm_inject_gp(vcpu, 0);
+	return 1;
+}
+
 static int handle_apic_access(struct kvm_vcpu *vcpu)
 {
 	return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
@@ -3632,6 +3659,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 +78 -7

178 lines not on this page · raw message

Signature · 1 line
1.7.0.1
author
#1 lore raw

PATCH 1/1 qemu-kvm: Enable xsave related CPUID

---
 target-i386/cpuid.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
target-i386/cpuid.c +32 -0
index eebf038..21e94f3 100644
@@ -1067,6 +1067,38 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
         *ecx = 0;
         *edx = 0;
         break;
+    case 0xD:
+        /* Processor Extended State */
+        if (!(env->cpuid_ext_features & CPUID_EXT_XSAVE)) {
+            *eax = 0;
+            *ebx = 0;
+            *ecx = 0;
+            *edx = 0;
+            break;
+        }
+        if (count == 0) {
+            *eax = 0x7;
+            *ebx = 0x340;
+            *ecx = 0x340;
+            *edx = 0;
+        } else if (count == 1) {
+            /* eax = 1, so we can continue with others */
+            *eax = 1;
+            *ebx = 0;
+            *ecx = 0;
+            *edx = 0;
+        } else if (count == 2) {
+            *eax = 0x100;
+            *ebx = 0x240;
+            *ecx = 0;
+            *edx = 0;
+        } else {
+            *eax = 0;
+            *ebx = 0;
+            *ecx = 0;
+            *edx = 0;
+        }
+        break;
     case 0x80000000:
         *eax = env->cpuid_xlevel;
         *ebx = env->cpuid_vendor1;
Signature · 1 line
1.7.0.1
#2 lore raw

Re: [PATCH] qemu-kvm: Enable xsave related CPUID

Quoted 4 lines · Sheng Yang
---
  target-i386/cpuid.c |   32 ++++++++++++++++++++++++++++++++

Can send to Anthony directly, while tcg doesn't support xsave/ymm, all the code here is generic.

Quoted 33 lines
  1 files changed, 32 insertions(+), 0 deletions(-)
target-i386/cpuid.c +23 -0
index eebf038..21e94f3 100644
@@ -1067,6 +1067,38 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
          *ecx = 0;
          *edx = 0;
          break;
+    case 0xD:
+        /* Processor Extended State */
+        if (!(env->cpuid_ext_features&  CPUID_EXT_XSAVE)) {
+            *eax = 0;
+            *ebx = 0;
+            *ecx = 0;
+            *edx = 0;
+            break;
+        }
+        if (count == 0) {
+            *eax = 0x7;
+            *ebx = 0x340;
+            *ecx = 0x340;
+            *edx = 0;
+        } else if (count == 1) {
+            /* eax = 1, so we can continue with others */
+            *eax = 1;
+            *ebx = 0;
+            *ecx = 0;
+            *edx = 0;
+        } else if (count == 2) {
+            *eax = 0x100;
+            *ebx = 0x240;

target-i386/cpuid.c void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, · line 1102

Quoted 10 lines
target-i386/cpuid.c +9 -0
+            *ecx = 0;
+            *edx = 0;
+        } else {
+            *eax = 0;
+            *ebx = 0;
+            *ecx = 0;
+            *edx = 0;
+        }
+        break;
 

Lots of magic numbers. Symbolic constants or explanatory comments.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#3 lore raw
In reply to Sheng Yang
Quoted 25 lines · Sheng Yang

From: Dexuan Cui<[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V1:

1. Use FPU API.
2. Fix CPUID issue.
3. Save/restore all possible guest xstate fields when switching. Because we don't know which fields guest has already touched.

---
  arch/x86/include/asm/kvm_host.h |    1 +
  arch/x86/include/asm/vmx.h      |    1 +
  arch/x86/kvm/vmx.c              |   28 +++++++++++++
  arch/x86/kvm/x86.c              |   85 +++++++++++++++++++++++++++++++++++---
  4 files changed, 108 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -0
index d08bb4a..78d7b06 100644

arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 306

Quoted 5 lines
arch/x86/include/asm/kvm_host.h +1 -0
@@ -302,6 +302,7 @@ struct kvm_vcpu_arch {
  	} update_pte;
 
  	struct fpu guest_fpu;
+	uint64_t xcr0, host_xcr0;
 

host_xcr0 can be a global.

Quoted 16 lines
arch/x86/include/asm/kvm_host.h +0 -0
  /*
   * Interruption-information format
arch/x86/kvm/vmx.c +2 -0
index 99ae513..2ee8ff6 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 vmx_vcpu_setup(struct vcpu_vmx *vmx) · line 2623

Quoted 6 lines
arch/x86/kvm/vmx.c +2 -0
@@ -2616,6 +2618,8 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  	vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  	if (enable_ept)
  		vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
+	if (cpu_has_xsave)
+		vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_OSXSAVE;
 

First, we should only allow the guest to play with cr4.osxsave if guest_has_xsave in cpuid; otherwise we need to #GP if the guest sets it. Second, it may be better to trap when the guest sets it (should be rare); this way, we only need to save/restore xcr0 if the guest has enabled cr4.osxsave.

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

Quoted 12 lines
arch/x86/kvm/vmx.c +0 -0
  	vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
 
  	tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
arch/x86/kvm/vmx.c +4 -0
@@ -3354,6 +3358,29 @@ static int handle_wbinvd(struct kvm_vcpu *vcpu)
  	return 1;
  }
 
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = ((u64)(kvm_register_read(vcpu, VCPU_REGS_RDX)<<  32)) |
+		kvm_register_read(vcpu, VCPU_REGS_RAX);
 

I think you need to trim the upper 32 bits of rax.

Please introduce helpers for reading edx:eax into a u64 and vice versa. We can then use the helpers here and in the msr code.

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

Quoted 11 lines
arch/x86/kvm/vmx.c +10 -0
+
+	if (kvm_register_read(vcpu, VCPU_REGS_RCX) != 0)
+		goto err;
+	if (vmx_get_cpl(vcpu) != 0)
+		goto err;
+	if (!(new_bv&  XSTATE_FP) ||
+	     (new_bv&  ~vcpu->arch.host_xcr0))
+		goto err;
+	if ((new_bv&  XSTATE_YMM)&&  !(new_bv&  XSTATE_SSE))
+		goto err;
 

This is a little worrying. What if a new bit is introduced later that depends on other bits? We'll need to add a dependency between ZMM and YMM or whatever, and old versions will be broken.

So I think we need to check xcr0 not against host_xcr0 but instead against a whitelist of xcr0 bits that we know how to handle (currently fpu, see, and ymm).

Quoted 24 lines
arch/x86/kvm/vmx.c +14 -0
+	vcpu->arch.xcr0 = new_bv;
+	xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
+	skip_emulated_instruction(vcpu);
+	return 1;
+err:
+	kvm_inject_gp(vcpu, 0);
+	return 1;
+}
+
 
@@ -149,6 +150,11 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
  	{ NULL }
  };
 
+static inline u32 bit(int bitno)
+{
+	return 1<<  (bitno&  31);
+}
+
  static void kvm_on_user_return(struct user_return_notifier *urn)
  {
  	unsigned slot;

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

Quoted 10 lines
arch/x86/kvm/vmx.c +6 -0
@@ -473,6 +479,17 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  }
  EXPORT_SYMBOL_GPL(kvm_lmsw);
 
+static bool guest_cpuid_has_xsave(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *best;
+
+	best = kvm_find_cpuid_entry(vcpu, 1, 0);
+	if (best->ecx&  bit(X86_FEATURE_XSAVE))
 

Sanity: if (best && ...)

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

Quoted 4 lines
arch/x86/kvm/vmx.c +3 -0
+		return true;
+
+	return false;
 

Can avoid the if (): return best && (best->ecx & ...);

arch/x86/kvm/vmx.c int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) · line 502

Quoted 11 lines
arch/x86/kvm/vmx.c +2 -0
+}
+
  int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  {
  	unsigned long old_cr4 = kvm_read_cr4(vcpu);
arch/x86/kvm/vmx.c +1 -0
@@ -481,6 +498,9 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  	if (cr4&  CR4_RESERVED_BITS)
  		return 1;
 
+	if (!guest_cpuid_has_xsave(vcpu)&&  X86_CR4_OSXSAVE)
 

s/&&.*//

Quoted 17 lines
arch/x86/kvm/vmx.c +2 -0
+		return 1;
+
  	if (is_long_mode(vcpu)) {
  		if (!(cr4&  X86_CR4_PAE))

return 1;

arch/x86/kvm/vmx.c +1 -0
@@ -1887,6 +1902,7 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  	unsigned f_lm = 0;
  #endif
  	unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
+	unsigned f_xsave = cpu_has_xsave ? F(XSAVE) : 0;
 
  	/* cpuid 1.edx */
  	const u32 kvm_supported_word0_x86_features =

arch/x86/kvm/vmx.c static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, · line 1936

Quoted 6 lines
arch/x86/kvm/vmx.c +1 -1
@@ -1916,7 +1932,7 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  		0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
  		0 /* Reserved, DCA */ | F(XMM4_1) |
  		F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
-		0 /* Reserved, XSAVE, OSXSAVE */;
+		0 /* Reserved, AES */ | f_xsave | 0 /* OSXSAVE */;
 

Enough to put F(XSAVE) there, no? The code should mask it out if not present, like XMM4_2.

Quoted 16 lines
arch/x86/kvm/vmx.c +11 -0
+static void kvm_update_cpuid(struct kvm_vcpu *vcpu,
+			     struct kvm_cpuid_entry2 *best)
+{
+	/* Update OSXSAVE bit */
+	if (cpu_has_xsave&&  best->function == 0x1) {
+		best->ecx&= ~(bit(X86_FEATURE_OSXSAVE));
+		if (kvm_read_cr4(vcpu)&  X86_CR4_OSXSAVE)
+			best->ecx |= bit(X86_FEATURE_OSXSAVE);
+	}
+}
+
  void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  {
  	u32 function, index;

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

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -0
@@ -4389,6 +4430,7 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  	kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  	best = kvm_find_cpuid_entry(vcpu, function, index);
  	if (best) {
+		kvm_update_cpuid(vcpu, best);
 

Slightly faster to do it at kvm_set_cr4() time. Not sure it matters.

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

Quoted 11 lines
arch/x86/kvm/vmx.c +0 -0
  		kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  		kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  		kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
arch/x86/kvm/vmx.c +3 -0
@@ -5118,6 +5160,11 @@ void fx_init(struct kvm_vcpu *vcpu)
  	fpu_alloc(&vcpu->arch.guest_fpu);
  	fpu_finit(&vcpu->arch.guest_fpu);
 
+	if (cpu_has_xsave) {
+		vcpu->arch.host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
+		vcpu->arch.xcr0 = vcpu->arch.host_xcr0;
 

Should be initialized to the default value.

arch/x86/kvm/vmx.c void fx_init(struct kvm_vcpu *vcpu)

Quoted 9 lines
arch/x86/kvm/vmx.c +2 -0
  void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  {
  	if (vcpu->guest_fpu_loaded)
  		return;
 
  	vcpu->guest_fpu_loaded = 1;
+	if (cpu_has_xsave)
+		vcpu->arch.host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);

Why read it every time?

arch/x86/kvm/vmx.c

Quoted 8 lines
  	unlazy_fpu(current);
arch/x86/kvm/vmx.c +5 -0
+	/* Restore all possible states in the guest */
+	if (cpu_has_xsave&&  guest_cpuid_has_xsave(vcpu))
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			cpuid_get_possible_xcr0(vcpu));
  	fpu_restore_checking(&vcpu->arch.guest_fpu);
+	if (cpu_has_xsave)

if guest enabled xsave...

arch/x86/kvm/vmx.c

Quoted 2 lines
arch/x86/kvm/vmx.c +1 -0
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);

Need to do it on every entry, not just fpu reload, since xgetbv does not check cr0.ts.

Need to add save/restore support for xcrs.

Need to add save/restore support for xsave state.

Please send a test case for this (see qemu-kvm.git user/test/x86 for examples), to be run twice: once with -cpu host,-xsave and once with -cpu host,+xsave.

Things to check:

- Set cr4.xsave without cpuid.xsave -> #GP
- Set cr4.xsave with cpuid.xsave -> works, sets cr4.xsave, sets cpuid.osxsave
- clearing cr4.xsave
- xsetbv/xgetbv/xsave/xrstor with cr4.xsave enabled/disabled
- interdepdencies between xcr0 bits (fpu, sse, ymm), illegal combinations, illegal bits, illegal xcrs
- anything else you can think of...

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
author
#4 lore raw

PATCH v3 1/1 KVM: VMX: Enable XSAVE/XRSTORE for guest

From: Dexuan Cui <[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V2:
Addressed comments from Avi.

Change from V1:

1. Use FPU API.
2. Fix CPUID issue.
3. Save/restore all possible guest xstate fields when switching. Because we don't know which fields guest has already touched.

---

Avi, could you help to review this kernel patch first? Testcase and LM are in progress now.

 arch/x86/include/asm/kvm_host.h |    1 +
 arch/x86/include/asm/vmx.h      |    1 +
 arch/x86/kvm/vmx.c              |   28 ++++++++++++
 arch/x86/kvm/x86.c              |   88 +++++++++++++++++++++++++++++++++++---
 4 files changed, 111 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index d08bb4a..3938bd1 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;
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/vmx.c +28 -0
index 99ae513..a63f206 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"
 
@@ -247,6 +249,9 @@ static const u32 vmx_msr_index[] = {
 };
 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
 
+#define MERGE_TO_U64(low, high) \
+		(((low) & -1u) | ((u64)((high) & -1u) << 32))
+
 static inline bool is_page_fault(u32 intr_info)
 {
 	return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
@@ -3354,6 +3359,28 @@ static int handle_wbinvd(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = MERGE_TO_U64(kvm_register_read(vcpu, VCPU_REGS_RAX),
+		kvm_register_read(vcpu, VCPU_REGS_RDX));
+
+	if (kvm_register_read(vcpu, VCPU_REGS_RCX) != 0)
+		goto err;
+	if (vmx_get_cpl(vcpu) != 0)
+		goto err;
+	if (!(new_bv & XSTATE_FP))
+		goto err;
+	if ((new_bv & XSTATE_YMM) && !(new_bv & XSTATE_SSE))
+		goto err;
+	vcpu->arch.xcr0 = new_bv;
+	xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
+	skip_emulated_instruction(vcpu);
+	return 1;
+err:
+	kvm_inject_gp(vcpu, 0);
+	return 1;
+}
+
 static int handle_apic_access(struct kvm_vcpu *vcpu)
 {
 	return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
@@ -3632,6 +3659,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 +81 -7

179 lines not on this page · raw message

Signature · 1 line
1.7.0.1
#5 lore raw
Quoted 53 lines · Sheng Yang

From: Dexuan Cui<[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V2:
Addressed comments from Avi.

Change from V1:

1. Use FPU API.
2. Fix CPUID issue.
3. Save/restore all possible guest xstate fields when switching. Because we don't know which fields guest has already touched.

arch/x86/include/asm/kvm_host.h +1 -0
index d08bb4a..3938bd1 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;
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/vmx.c +2 -0
index 99ae513..a63f206 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 const u32 vmx_msr_index[] = { · line 255

Quoted 7 lines
arch/x86/kvm/vmx.c +3 -0
@@ -247,6 +249,9 @@ static const u32 vmx_msr_index[] = {
  };
  #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
 
+#define MERGE_TO_U64(low, high) \
+		(((low)&  -1u) | ((u64)((high)&  -1u)<<  32))
+
 

static inline u64 kvm_read_edx_eax(vcpu) in cache_regs.h

Quoted 5 lines
arch/x86/kvm/vmx.c +3 -0
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = MERGE_TO_U64(kvm_register_read(vcpu, VCPU_REGS_RAX),

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

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+		kvm_register_read(vcpu, VCPU_REGS_RDX));
+
+	if (kvm_register_read(vcpu, VCPU_REGS_RCX) != 0)
+		goto err;
+	if (vmx_get_cpl(vcpu) != 0)
+		goto err;
+	if (!(new_bv&  XSTATE_FP))
+		goto err;
+	if ((new_bv&  XSTATE_YMM)&&  !(new_bv&  XSTATE_SSE))
+		goto err;

What about a check against unknown bits?

Quoted 6 lines
arch/x86/kvm/vmx.c +5 -0
+	vcpu->arch.xcr0 = new_bv;
+	xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
+	skip_emulated_instruction(vcpu);
+	return 1;
+err:

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

Quoted 10 lines
arch/x86/kvm/vmx.c +5 -0
+	kvm_inject_gp(vcpu, 0);
+	return 1;
+}
+
  static int handle_apic_access(struct kvm_vcpu *vcpu)
  {
  	return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
 
 
+static u64 host_xcr0;

__read_mostly.

Quoted 7 lines
arch/x86/kvm/vmx.c +6 -0
+
+static void update_cpuid(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *best;
+
+	best = kvm_find_cpuid_entry(vcpu, 1, 0);

arch/x86/kvm/vmx.c

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	if (!best)
+		return;
+
+	/* Update OSXSAVE bit */
+	if (cpu_has_xsave&&  best->function == 0x1) {
+		best->ecx&= ~(bit(X86_FEATURE_OSXSAVE));
+		if (kvm_read_cr4(vcpu)&  X86_CR4_OSXSAVE)
+			best->ecx |= bit(X86_FEATURE_OSXSAVE);
+	}
+}

Note: need to update after userspace writes cpuid as well.

Quoted 15 lines
arch/x86/kvm/vmx.c +4 -0
+
  int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  {
  	unsigned long old_cr4 = kvm_read_cr4(vcpu);
@@ -481,6 +513,9 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  	if (cr4&  CR4_RESERVED_BITS)
  		return 1;
 
+	if (!guest_cpuid_has_xsave(vcpu)&&  (cr4&  X86_CR4_OSXSAVE))
+		return 1;
+
  	if (is_long_mode(vcpu)) {
  		if (!(cr4&  X86_CR4_PAE))
  			return 1;

arch/x86/kvm/vmx.c int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) · line 538

Quoted 7 lines
arch/x86/kvm/vmx.c +3 -0
@@ -497,6 +532,9 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  	if ((cr4 ^ old_cr4)&  pdptr_bits)
  		kvm_mmu_reset_context(vcpu);
 
+	if ((cr4 ^ old_cr4)&  X86_CR4_OSXSAVE)
+		update_cpuid(vcpu);
+
 

I think we need to reload the guest's xcr0 at this point. Alternatively, call vmx_load_host_state() to ensure the the next entry will reload it.

arch/x86/kvm/vmx.c static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, · line 1968

Quoted 7 lines
arch/x86/kvm/vmx.c +1 -1
@@ -1931,7 +1964,7 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 
  	switch (function) {
  	case 0:
-		entry->eax = min(entry->eax, (u32)0xb);
+		entry->eax = min(entry->eax, (u32)0xd);
 

Do we need any special handling for leaf 0xc?

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

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
@@ -4567,6 +4616,8 @@ 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);
+	if (kvm_read_cr4(vcpu)&  X86_CR4_OSXSAVE)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
 

Better done in vmx_save_host_state(), so we only do it on context switches or entries from userspace.

kvm_read_cr4_bits() is faster - doesn't need a vmcs_readl().

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

Quoted 12 lines
arch/x86/kvm/vmx.c +4 -0
  	atomic_set(&vcpu->guest_mode, 1);
  	smp_wmb();
@@ -5118,6 +5169,10 @@ 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 */
+	if (cpu_has_xsave)
+		vcpu->arch.xcr0 = XSTATE_FP;
+
 

Can do it unconditionally, not that it matters much.

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

Quoted 12 lines
arch/x86/kvm/vmx.c +0 -0
  void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  {

if (vcpu->guest_fpu_loaded)

arch/x86/kvm/vmx.c +4 -0
@@ -5134,6 +5197,10 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
  	vcpu->guest_fpu_loaded = 1;
  	unlazy_fpu(current);
+	/* Restore all possible states in the guest */
+	if (cpu_has_xsave&&  guest_cpuid_has_xsave(vcpu))
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			cpuid_get_possible_xcr0(vcpu));
 

Best to calculate it out of the fast path, when guest cpuid is set. Need to check it at this time as well.

Also can avoid it if guest xcr0 == host xcr0.

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

Quoted 12 lines
arch/x86/kvm/vmx.c +0 -0
  	fpu_restore_checking(&vcpu->arch.guest_fpu);
  	trace_kvm_fpu(1);
  }
arch/x86/kvm/vmx.c +4 -0
@@ -5144,7 +5211,14 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  		return;
 
  	vcpu->guest_fpu_loaded = 0;
+	/* Save all possible states in the guest */
+	if (cpu_has_xsave&&  guest_cpuid_has_xsave(vcpu))
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			cpuid_get_possible_xcr0(vcpu));
 

Ditto.

Quoted 8 lines
arch/x86/kvm/vmx.c +3 -0
  	fpu_save_init(&vcpu->arch.guest_fpu);
+	if (cpu_has_xsave)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			host_xcr0);
  	++vcpu->stat.fpu_reload;
  	set_bit(KVM_REQ_DEACTIVATE_FPU,&vcpu->requests);
  	trace_kvm_fpu(0);
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
author
#6 lore raw
Quoted 118 lines · On Thursday 20 May 2010 17:46:40 Avi Kivity wrote:
Quoted 62 lines · Sheng Yang

From: Dexuan Cui<[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V2:
Addressed comments from Avi.

Change from V1:

1. Use FPU API.
2. Fix CPUID issue.
3. Save/restore all possible guest xstate fields when switching. Because we don't know which fields guest has already touched.

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index d08bb4a..3938bd1 100644

arch/x86/include/asm/kvm_host.h +1 -0
@@ -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;

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/vmx.c +2 -0
index 99ae513..a63f206 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 const u32 vmx_msr_index[] = { · line 255

Quoted 7 lines
arch/x86/kvm/vmx.c +3 -0
@@ -247,6 +249,9 @@ static const u32 vmx_msr_index[] = {
 
  };
  #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
 
+#define MERGE_TO_U64(low, high) \
+		(((low)&  -1u) | ((u64)((high)&  -1u)<<  32))
+

static inline u64 kvm_read_edx_eax(vcpu) in cache_regs.h

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

Quoted 13 lines
arch/x86/kvm/vmx.c +13 -0
+static int handle_xsetbv(struct kvm_vcpu *vcpu)
+{
+	u64 new_bv = MERGE_TO_U64(kvm_register_read(vcpu, VCPU_REGS_RAX),
+		kvm_register_read(vcpu, VCPU_REGS_RDX));
+
+	if (kvm_register_read(vcpu, VCPU_REGS_RCX) != 0)
+		goto err;
+	if (vmx_get_cpl(vcpu) != 0)
+		goto err;
+	if (!(new_bv&  XSTATE_FP))
+		goto err;
+	if ((new_bv&  XSTATE_YMM)&&  !(new_bv&  XSTATE_SSE))
+		goto err;

What about a check against unknown bits?

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+	vcpu->arch.xcr0 = new_bv;
+	xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
+	skip_emulated_instruction(vcpu);
+	return 1;
+err:
+	kvm_inject_gp(vcpu, 0);

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

Quoted 10 lines
arch/x86/kvm/vmx.c +4 -0
+	return 1;
+}
+
 
  static int handle_apic_access(struct kvm_vcpu *vcpu)
  {
 
  	return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
 
+static u64 host_xcr0;

__read_mostly.

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+
+static void update_cpuid(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *best;
+
+	best = kvm_find_cpuid_entry(vcpu, 1, 0);
+	if (!best)
+		return;
+
+	/* Update OSXSAVE bit */
Quoted 8 lines

arch/x86/kvm/vmx.c

Quoted 6 lines
arch/x86/kvm/vmx.c +6 -0
+	if (cpu_has_xsave&&  best->function == 0x1) {
+		best->ecx&= ~(bit(X86_FEATURE_OSXSAVE));
+		if (kvm_read_cr4(vcpu)&  X86_CR4_OSXSAVE)
+			best->ecx |= bit(X86_FEATURE_OSXSAVE);
+	}
+}

Note: need to update after userspace writes cpuid as well.

Not quite understand. Userspace set OSXSAVE should be trimmed IMO...

Quoted 32 lines
Quoted 32 lines
arch/x86/kvm/vmx.c +4 -0
+
 
  int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  {
 
  	unsigned long old_cr4 = kvm_read_cr4(vcpu);
@@ -481,6 +513,9 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned
 long cr4)
 
  	if (cr4&  CR4_RESERVED_BITS)
 
  		return 1;
 
+	if (!guest_cpuid_has_xsave(vcpu)&&  (cr4&  X86_CR4_OSXSAVE))
+		return 1;
+
  	if (is_long_mode(vcpu)) {

if (!(cr4& X86_CR4_PAE))

return 1;

arch/x86/kvm/vmx.c +0 -0
@@ -497,6 +532,9 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned
 long cr4)
 
  	if ((cr4 ^ old_cr4)&  pdptr_bits)
 
  		kvm_mmu_reset_context(vcpu);
 
Quoted 7 lines

arch/x86/kvm/vmx.c int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned · line 540

Quoted 3 lines
arch/x86/kvm/vmx.c +3 -0
+	if ((cr4 ^ old_cr4)&  X86_CR4_OSXSAVE)
+		update_cpuid(vcpu);
+

I think we need to reload the guest's xcr0 at this point. Alternatively, call vmx_load_host_state() to ensure the the next entry will reload it.

Current xcr0 would be loaded when next vmentry.

And if we use prepare_guest_switch(), how about SVM?

Quoted 11 lines

arch/x86/kvm/vmx.c static void do_cpuid_ent(struct kvm_cpuid_entry2 · line 1969

Quoted 9 lines
arch/x86/kvm/vmx.c +1 -1
@@ -1931,7 +1964,7 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2
 *entry, u32 function,
 
  	switch (function) {
 
  	case 0:
-		entry->eax = min(entry->eax, (u32)0xb);
+		entry->eax = min(entry->eax, (u32)0xd);

Do we need any special handling for leaf 0xc?

Don't think so. CPUID would return all 0 for it.

Quoted 41 lines

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

Quoted 10 lines
arch/x86/kvm/vmx.c +2 -0
@@ -4567,6 +4616,8 @@ 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);
 
+	if (kvm_read_cr4(vcpu)&  X86_CR4_OSXSAVE)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);

Better done in vmx_save_host_state(), so we only do it on context switches or entries from userspace.

kvm_read_cr4_bits() is faster - doesn't need a vmcs_readl().

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

Quoted 12 lines
  	atomic_set(&vcpu->guest_mode, 1);
  	smp_wmb();
arch/x86/kvm/vmx.c +4 -0
@@ -5118,6 +5169,10 @@ 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 */
+	if (cpu_has_xsave)
+		vcpu->arch.xcr0 = XSTATE_FP;
+

Can do it unconditionally, not that it matters much.

Quoted 10 lines
arch/x86/kvm/vmx.c +0 -0
  void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  {

if (vcpu->guest_fpu_loaded)

arch/x86/kvm/vmx.c +0 -0
@@ -5134,6 +5197,10 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
  	vcpu->guest_fpu_loaded = 1;
  	unlazy_fpu(current);
 
Quoted 7 lines

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

Quoted 4 lines
arch/x86/kvm/vmx.c +4 -0
+	/* Restore all possible states in the guest */
+	if (cpu_has_xsave&&  guest_cpuid_has_xsave(vcpu))
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			cpuid_get_possible_xcr0(vcpu));

Best to calculate it out of the fast path, when guest cpuid is set. Need to check it at this time as well.

You mean guest_cpuid_has_xsave()? Not quite understand the point here...

Quoted 2 lines

Also can avoid it if guest xcr0 == host xcr0.

I don't know the assumption that "host use all possible xcr0 bits" can apply. If so, only use host_xcr0 should be fine.

Would update other points. Thanks.
--
regards
Yang, Sheng

Quoted 28 lines
Quoted 7 lines
arch/x86/kvm/vmx.c +0 -0
  	fpu_restore_checking(&vcpu->arch.guest_fpu);
  	trace_kvm_fpu(1);
  
  }

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

Quoted 9 lines
arch/x86/kvm/vmx.c +4 -0
@@ -5144,7 +5211,14 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
 
  		return;
 
  	vcpu->guest_fpu_loaded = 0;
 
+	/* Save all possible states in the guest */
+	if (cpu_has_xsave&&  guest_cpuid_has_xsave(vcpu))
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			cpuid_get_possible_xcr0(vcpu));

Ditto.

Quoted 9 lines
arch/x86/kvm/vmx.c +3 -0
  	fpu_save_init(&vcpu->arch.guest_fpu);
 
+	if (cpu_has_xsave)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			host_xcr0);
  	++vcpu->stat.fpu_reload;
  	set_bit(KVM_REQ_DEACTIVATE_FPU,&vcpu->requests);
  	trace_kvm_fpu(0);
#7 lore raw
Quoted 20 lines · Sheng Yang
Quoted 11 lines
Quoted 11 lines
arch/x86/kvm/x86.c +10 -0
+
+static void update_cpuid(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *best;
+
+	best = kvm_find_cpuid_entry(vcpu, 1, 0);
+	if (!best)
+		return;
+
+	/* Update OSXSAVE bit */
Quoted 9 lines

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

Quoted 7 lines
arch/x86/kvm/x86.c +6 -0
+	if (cpu_has_xsave&&   best->function == 0x1) {
+		best->ecx&= ~(bit(X86_FEATURE_OSXSAVE));
+		if (kvm_read_cr4(vcpu)&   X86_CR4_OSXSAVE)
+			best->ecx |= bit(X86_FEATURE_OSXSAVE);
+	}
+}

Note: need to update after userspace writes cpuid as well.

Quoted 2 lines

Not quite understand. Userspace set OSXSAVE should be trimmed IMO...

Two cases: userspace does KVM_SET_CPUID2 with osxsave set but cr4.xsave clear, or the other way round.

So we should set cpuid.osxsave depending to cr4.xsave whenever cr4 OR cpuid is modified, and completely ignore userspace setting for that bit.

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
(quoted diff) +0 -0
@@ -497,6 +532,9 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned
 long cr4)
 
   	if ((cr4 ^ old_cr4)&   pdptr_bits)
 
   		kvm_mmu_reset_context(vcpu);
 
Quoted 8 lines

arch/x86/kvm/x86.c int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) · line 537

Quoted 4 lines
arch/x86/kvm/x86.c +3 -0
+	if ((cr4 ^ old_cr4)&   X86_CR4_OSXSAVE)
+		update_cpuid(vcpu);
+

I think we need to reload the guest's xcr0 at this point. Alternatively, call vmx_load_host_state() to ensure the the next entry will reload it.

Quoted 2 lines

Current xcr0 would be loaded when next vmentry.

True.

Quoted 2 lines

And if we use prepare_guest_switch(), how about SVM?

kvm_arch_vcpu_load() looks like a good place, as long as interrupts don't use the fpu.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/x86.c +0 -0
@@ -5134,6 +5197,10 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
   	vcpu->guest_fpu_loaded = 1;
   	unlazy_fpu(current);
 
Quoted 8 lines

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

Quoted 5 lines
arch/x86/kvm/x86.c +4 -0
+	/* Restore all possible states in the guest */
+	if (cpu_has_xsave&&   guest_cpuid_has_xsave(vcpu))
+		xsetbv(XCR_XFEATURE_ENABLED_MASK,
+			cpuid_get_possible_xcr0(vcpu));
 

Best to calculate it out of the fast path, when guest cpuid is set. Need to check it at this time as well.

Quoted 2 lines

You mean guest_cpuid_has_xsave()? Not quite understand the point here...

Also cpuid_get_possible_cr0(). So we have something like

   if (vcpu->save_xcr0)
       xsetbv(vcpu->save_xcr0);

Those cpuid functions have loops, we don't want them running every context switch.

Quoted 5 lines
Quoted 2 lines

Also can avoid it if guest xcr0 == host xcr0.

I don't know the assumption that "host use all possible xcr0 bits" can apply. If so, only use host_xcr0 should be fine.

I think we can rely on it. Those bits are a service to userspace and the guest is just a different kind of userspace, so it makes sense to expose the same set.

Quoted 2 lines

Would update other points. Thanks.

Thanks.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.