Skip to contentSkip to conversation

KVM: VMX: Enable XSAVE/XRSTORE for guest

PATCH Superseded Sheng Yang 2 participants 6 messages May 24, 2010 – May 25, 2010

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Sheng Yang

author
#0 lore raw

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

From: Dexuan Cui <[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V3:
1. Enforced the assumption that host OS would use all available xstate bits.
2. Various fixes, addressed Avi's comments.

Change from V2:
Various fixes, addressed Avi's comments.

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.

---

I am still not clear about why we need to reload guest xcr0 when cr4.osxsave set...

 arch/x86/include/asm/kvm_host.h |    1 +
 arch/x86/include/asm/vmx.h      |    1 +
 arch/x86/kvm/kvm_cache_regs.h   |    6 +++
 arch/x86/kvm/vmx.c              |   26 +++++++++++
 arch/x86/kvm/x86.c              |   88 +++++++++++++++++++++++++++++++++++---
 5 files changed, 115 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/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 +26 -0
index 99ae513..a4881ff 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,29 @@ 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);
+
+	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;
+	if (new_bv & ~XCNTXT_MASK)
+		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 +3657,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

184 lines not on this page · raw message

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

From: Dexuan Cui<[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V3:
1. Enforced the assumption that host OS would use all available xstate bits.
2. Various fixes, addressed Avi's comments.

I am still not clear about why we need to reload guest xcr0 when cr4.osxsave set...

When cr4.osxsave=0, then the guest executes with the host xcr0 (since xgetbv will trap; this is similar to the guest running with the host fpu if cr0.ts=0). So if cr4.osxsave transtions, we need to transition xcr0 as well.

Quoted 9 lines
arch/x86/kvm/vmx.c +5 -0
@@ -3354,6 +3356,29 @@ 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);
+
+	if (kvm_register_read(vcpu, VCPU_REGS_RCX) != 0)

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

Quoted 10 lines
arch/x86/kvm/vmx.c +9 -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;
+	if (new_bv&  ~XCNTXT_MASK)
+		goto err;
 

Ok. This means we must update kvm immediately when XCNTXT_MASK changes.

(Otherwise we would use KVM_XCNTXT_MASK which is always smaller than than XCNTXT_MASK).

Quoted 12 lines
arch/x86/kvm/vmx.c +9 -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;
+}
+
 
 

arch/x86/kvm/vmx.c int kvm_arch_init(void *opaque) · line 4182

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
@@ -4124,6 +4176,8 @@ int kvm_arch_init(void *opaque)
 
  	perf_register_guest_info_callbacks(&kvm_guest_cbs);
 
+	host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
+
  	return 0;
 

Will fault on old cpu.

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

Quoted 13 lines
arch/x86/kvm/x86.c +6 -0
  EXPORT_SYMBOL_GPL(fx_init);
@@ -5134,6 +5195,12 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
  	vcpu->guest_fpu_loaded = 1;
  	unlazy_fpu(current);
+	/*
+	 * Restore all possible states in the guest,
+	 * and assume host would use all available bits.
+	 */
+	if (cpu_has_xsave&&  vcpu->arch.xcr0)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
  	fpu_restore_checking(&vcpu->arch.guest_fpu);
 

I think we need to reload xcr0 now to the guest's value.

Quoted 7 lines
arch/x86/kvm/x86.c +0 -0
  	trace_kvm_fpu(1);
  }
arch/x86/kvm/x86.c +0 -0
@@ -5144,6 +5211,13 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  		return;
 
  	vcpu->guest_fpu_loaded = 0;

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

Quoted 10 lines
arch/x86/kvm/x86.c +7 -0
+	/*
+	 * Save all possible states in the guest,
+	 * and assume host would use all available bits.
+	 * Also load host_xcr0 for host usage.
+	 */
+	if (cpu_has_xsave&&  vcpu->arch.xcr0)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
  	fpu_save_init(&vcpu->arch.guest_fpu);
  	++vcpu->stat.fpu_reload;
  	set_bit(KVM_REQ_DEACTIVATE_FPU,&vcpu->requests);

This might be unnecessary.

So far xcr0 life cycle is almost that of save_host_state()/load_host_state(), but not exactly. When loading the guest fpu we switch temporarily to host xcr0, then we have to switch back, but only if gcr4.osxsave. When saving the guest fpu, we're already using the host xcr0:

Quoted 5 lines

void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)

{
    kvm_x86_ops->vcpu_put(vcpu);
    kvm_put_guest_fpu(vcpu);
}

One way to simplify this is to have a vcpu->guest_xcr0_loaded flag and check it when needed. So the transition matrix is:

save_host_state: if gcr4.osxsave, set guest_xcr0_loaded, load it set gcr4.osxsave: ditto
clear gcr4.osxsave: do nothing
load_host_state: if guest_xcr0_loaded, clear it, reload host xcr0 fpu switching: if (switched) switch; reload fpu; if (switched) switch

may be simplified if we move xcr0 reload back to guest entry (... :) but make it lazy:

save_host_state: nothing
set cr4.osxsave: nothing
clear cr4.osxsave: nothing
guest entry: if (gcr4.osxsave && !guest_xcr0_loaded) {
guest_xcr0_loaded = true, load gxcr0 }
load_host_state: if (guest_xcr0_loaded) { guest_xcr0_loaded = false;
load host xcr0 }
fpu switching: if (guest_xcr0_loaded) { guest_xcr0_loaded = false;
load host xcr0 }, do fpu stuff

So we delay xcr0 reload as late as possible for both entry and exit.

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

may be simplified if we move xcr0 reload back to guest entry (... :) but make it lazy:

save_host_state: nothing
set cr4.osxsave: nothing
clear cr4.osxsave: nothing
guest entry: if (gcr4.osxsave && !guest_xcr0_loaded) {
guest_xcr0_loaded = true, load gxcr0 }
load_host_state: if (guest_xcr0_loaded) { guest_xcr0_loaded = false;
load host xcr0 }
fpu switching: if (guest_xcr0_loaded) { guest_xcr0_loaded = false;
load host xcr0 }, do fpu stuff

Quoted 2 lines

So we delay xcr0 reload as late as possible for both entry and exit.

btw, this is similar to how we switch the fpu itself: if-needed-and-not-already-loaded on guest entry, if-loaded on vcpu_put.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
author
#3 lore raw
In reply to Avi Kivity
Quoted 9 lines · On Monday 24 May 2010 21:36:12 Avi Kivity wrote:
Quoted 8 lines · Sheng Yang

From: Dexuan Cui<[email protected]>

Enable XSAVE/XRSTORE for guest.

Change from V3:
1. Enforced the assumption that host OS would use all available xstate bits. 2. Various fixes, addressed Avi's comments.

Quoted 7 lines
Quoted 2 lines

I am still not clear about why we need to reload guest xcr0 when cr4.osxsave set...

When cr4.osxsave=0, then the guest executes with the host xcr0 (since xgetbv will trap; this is similar to the guest running with the host fpu if cr0.ts=0). So if cr4.osxsave transtions, we need to transition xcr0 as well.

Yes...

Quoted 24 lines
Quoted 11 lines
arch/x86/kvm/vmx.c +4 -0
@@ -3354,6 +3356,29 @@ 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);
+

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
+	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;
+	if (new_bv&  ~XCNTXT_MASK)
+		goto err;

Ok. This means we must update kvm immediately when XCNTXT_MASK changes.

Quoted 2 lines

(Otherwise we would use KVM_XCNTXT_MASK which is always smaller than than XCNTXT_MASK).

I guess use host_xcr0 here is better?

Quoted 22 lines
Quoted 13 lines
arch/x86/kvm/vmx.c +9 -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;
+}
+
 

arch/x86/kvm/vmx.c int kvm_arch_init(void *opaque) · line 4182

Quoted 7 lines
arch/x86/kvm/vmx.c +2 -0
@@ -4124,6 +4176,8 @@ int kvm_arch_init(void *opaque)
 
  	perf_register_guest_info_callbacks(&kvm_guest_cbs);
 
+	host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
+
 
  	return 0;

Will fault on old cpu.

...

Quoted 79 lines
Quoted 6 lines
  EXPORT_SYMBOL_GPL(fx_init);
arch/x86/kvm/x86.c +0 -0
@@ -5134,6 +5195,12 @@ void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
  	vcpu->guest_fpu_loaded = 1;

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

Quoted 10 lines
arch/x86/kvm/x86.c +6 -0
  	unlazy_fpu(current);
 
+	/*
+	 * Restore all possible states in the guest,
+	 * and assume host would use all available bits.
+	 */
+	if (cpu_has_xsave&&  vcpu->arch.xcr0)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
 
  	fpu_restore_checking(&vcpu->arch.guest_fpu);

I think we need to reload xcr0 now to the guest's value.

Quoted 18 lines
  	trace_kvm_fpu(1);
  
  }
arch/x86/kvm/x86.c +7 -0
@@ -5144,6 +5211,13 @@ void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
 
  		return;
 
  	vcpu->guest_fpu_loaded = 0;
 
+	/*
+	 * Save all possible states in the guest,
+	 * and assume host would use all available bits.
+	 * Also load host_xcr0 for host usage.
+	 */
+	if (cpu_has_xsave&&  vcpu->arch.xcr0)
+		xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
 
Quoted 3 lines
  	fpu_save_init(&vcpu->arch.guest_fpu);
  	++vcpu->stat.fpu_reload;
  	set_bit(KVM_REQ_DEACTIVATE_FPU,&vcpu->requests);

This might be unnecessary.

So far xcr0 life cycle is almost that of save_host_state()/load_host_state(), but not exactly. When loading the guest fpu we switch temporarily to host xcr0, then we have to switch back, but only if gcr4.osxsave. When saving the guest fpu, we're

already using the host xcr0:

Quoted 7 lines

void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
{

    kvm_x86_ops->vcpu_put(vcpu);
    kvm_put_guest_fpu(vcpu);

}

One way to simplify this is to have a vcpu->guest_xcr0_loaded flag and check it when needed. So the transition matrix is:

save_host_state: if gcr4.osxsave, set guest_xcr0_loaded, load it set gcr4.osxsave: ditto
clear gcr4.osxsave: do nothing
load_host_state: if guest_xcr0_loaded, clear it, reload host xcr0 fpu switching: if (switched) switch; reload fpu; if (switched) switch

may be simplified if we move xcr0 reload back to guest entry (... :) but make it lazy:

save_host_state: nothing
set cr4.osxsave: nothing
clear cr4.osxsave: nothing
guest entry: if (gcr4.osxsave && !guest_xcr0_loaded) {
guest_xcr0_loaded = true, load gxcr0 }
load_host_state: if (guest_xcr0_loaded) { guest_xcr0_loaded = false;
load host xcr0 }
fpu switching: if (guest_xcr0_loaded) { guest_xcr0_loaded = false;
load host xcr0 }, do fpu stuff

Quoted 1 line

So we delay xcr0 reload as late as possible for both entry and exit.

I think I got it. But why we need do it at "load_host_state()"? I guess just put code before fpu testing in kvm_put_guest_fpu() is fine?

--
regards
Yang, Sheng

#4 lore raw
Quoted 24 lines · Sheng Yang
Quoted 24 lines
Quoted 12 lines
arch/x86/kvm/vmx.c +5 -0
@@ -3354,6 +3356,29 @@ 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);
+
+	if (kvm_register_read(vcpu, VCPU_REGS_RCX) != 0)

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

Quoted 10 lines
arch/x86/kvm/vmx.c +9 -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;
+	if (new_bv&   ~XCNTXT_MASK)
+		goto err;
 

Ok. This means we must update kvm immediately when XCNTXT_MASK changes.

Quoted 5 lines
Quoted 3 lines

(Otherwise we would use KVM_XCNTXT_MASK which is always smaller than than XCNTXT_MASK).

I guess use host_xcr0 here is better?

Yes - it might be smaller than XCNTXT_MASK

Quoted 13 lines
Quoted 13 lines

may be simplified if we move xcr0 reload back to guest entry (... :) but make it lazy:

save_host_state: nothing
set cr4.osxsave: nothing
clear cr4.osxsave: nothing
guest entry: if (gcr4.osxsave&& !guest_xcr0_loaded) {
guest_xcr0_loaded = true, load gxcr0 }

    load_host_state: if (guest_xcr0_loaded) { guest_xcr0_loaded = false;
load host xcr0 }
    fpu switching:  if (guest_xcr0_loaded) { guest_xcr0_loaded = false;

load host xcr0 }, do fpu stuff

Quoted 5 lines
Quoted 2 lines

So we delay xcr0 reload as late as possible for both entry and exit.

I think I got it. But why we need do it at "load_host_state()"? I guess just put code before fpu testing in kvm_put_guest_fpu() is fine?

Right, load_host_state() is bad because it is vmx specific. kvm_put_guest_fpu() (or perhaps kvm_arch_vcpu_put()) is better.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
In reply to Sheng Yang
Quoted 5 lines · Sheng Yang

From: Dexuan Cui<[email protected]>

Enable XSAVE/XRSTORE for guest.

btw2, still missing save/restore support for xcr and xsave-capable fpu. Please make that a separate patch, this one is complicated enough already.

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