Skip to contentSkip to conversation

add xsaves/xrstors support

PATCH Superseded Shuai Ruan 4 participants 12 messages Jul 2, 2015 – Jul 13, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Shuai Ruan

author
#0 lore raw

cover add xsaves/xrstors support

This patchset enable xsaves/xrstors feature.
It includes tree parts:
1. add xsaves/xrstors for xen.
2. add xsaves/xrstors for pv guest.
3. add xsaves/xrstors for hvn guest.

Shuai Ruan (6):
x86/xsaves: enable xsaves/xrstors for pv guest x86/xsaves: enable xsaves/xrstors in xen x86/xsaves: enable xsaves/xrstors for hvm guest
libxc: expose xsaves/xgetbv/xsavec to hvm guest x86/xsaves: support compact format for hvm save/restore x86/xsaves: detect xsaves/xgetbv in xen

 tools/libxc/xc_cpuid_x86.c         |   8 +-
 xen/arch/x86/domain.c              |   3 +
 xen/arch/x86/hvm/hvm.c             |  56 +++++++-
 xen/arch/x86/hvm/vmx/vmcs.c        |   7 +-
 xen/arch/x86/hvm/vmx/vmx.c         |  18 +++
 xen/arch/x86/traps.c               |  85 ++++++++++++
 xen/arch/x86/x86_64/mm.c           |  52 +++++++
 xen/arch/x86/xstate.c              | 271 ++++++++++++++++++++++++++++++++-----
 xen/include/asm-x86/domain.h       |   1 +
 xen/include/asm-x86/hvm/vmx/vmcs.h |   5 +
 xen/include/asm-x86/hvm/vmx/vmx.h  |   2 +
 xen/include/asm-x86/mm.h           |   1 +
 xen/include/asm-x86/msr-index.h    |   2 +
 xen/include/asm-x86/xstate.h       |  13 +-
 14 files changed, 477 insertions(+), 47 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/6 x86/xsaves: enable xsaves/xrstors for pv guest

This patch emualtes xsaves/xrstors instruction and XSS msr access.

As xsaves/xrstors instructions and XSS msr access required be executed only in ring0. So emulation is needed when pv guest use these instructions.

---
 xen/arch/x86/domain.c           |  3 ++
 xen/arch/x86/traps.c            | 85 +++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/mm.c        | 52 +++++++++++++++++++++++++
 xen/arch/x86/xstate.c           | 39 +++++++++++++++++++
 xen/include/asm-x86/domain.h    |  1 +
 xen/include/asm-x86/mm.h        |  1 +
 xen/include/asm-x86/msr-index.h |  2 +
 xen/include/asm-x86/xstate.h    |  3 ++
 8 files changed, 186 insertions(+)
xen/arch/x86/domain.c +3 -0
index a8fe046..66f8231 100644
@@ -426,6 +426,7 @@ int vcpu_initialise(struct vcpu *v)
 
     /* By default, do not emulate */
     v->arch.vm_event.emulate_flags = 0;
+    v->arch.msr_ia32_xss = 0;
 
     rc = mapcache_vcpu_init(v);
     if ( rc )
@@ -1494,6 +1495,8 @@ static void __context_switch(void)
             if ( xcr0 != get_xcr0() && !set_xcr0(xcr0) )
                 BUG();
         }
+        if ( cpu_has_xsaves )
+            wrmsr_safe(MSR_IA32_XSS, n->arch.msr_ia32_xss);
         vcpu_restore_fpu_eager(n);
         n->arch.ctxt_switch_to(n);
     }
xen/arch/x86/traps.c +85 -0

103 lines not on this page · raw message

xen/arch/x86/x86_64/mm.c +52 -0

58 lines not on this page · raw message

xen/arch/x86/xstate.c +39 -0
index d5f5e3b..e34eda3 100644
@@ -65,6 +65,31 @@ uint64_t get_xcr0(void)
     return this_cpu(xcr0);
 }
 
+void xsaves(uint32_t lmask, uint32_t hmask, struct xsave_struct *ptr)
+{
+    asm volatile ( ".byte 0x48,0x0f,0xc7,0x2f"
+                    : "=m" (*ptr)
+                    : "a" (lmask), "d" (hmask), "D" (ptr) );
+}
+
+void xrstors(uint32_t lmask, uint32_t hmask, struct xsave_struct *ptr)
+{
+    asm volatile ( "1: .byte 0x48,0x0f,0xc7,0x1f\n"
+                   ".section .fixup,\"ax\"      \n"
+                   "2: mov %5,%%ecx             \n"
+                   "   xor %1,%1                \n"
+                   "   rep stosb                \n"
+                   "   lea %2,%0                \n"
+                   "   mov %3,%1                \n"
+                   "   jmp 1b                   \n"
+                   ".previous                   \n"
+                   _ASM_EXTABLE(1b, 2b)
+                   : "+&D" (ptr), "+&a" (lmask)
+                   : "m" (*ptr), "g" (lmask), "d" (hmask),
+                     "m" (xsave_cntxt_size)
+                   : "ecx" );
+}
+
 void xsave(struct vcpu *v, uint64_t mask)
 {
     struct xsave_struct *ptr = v->arch.xsave_area;
@@ -268,6 +293,20 @@ static unsigned int _xstate_ctxt_size(u64 xcr0)
     return ebx;
 }
 
+unsigned int xstate_ctxt_size_compact(u64 xcr0)
+{
+    u64 act_xcr0 = get_xcr0();
+    u32 eax, ebx = 0, ecx, edx;
+    bool_t ok = set_xcr0(xcr0);
+
+    ASSERT(ok);
+    cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
+    ok = set_xcr0(act_xcr0);
+    ASSERT(ok);
+
+    return ebx;
+}
+
 /* Fastpath for common xstate size requests, avoiding reloads of xcr0. */
 unsigned int xstate_ctxt_size(u64 xcr0)
 {
xen/include/asm-x86/domain.h +1 -0
index 96bde65..bcea9d4 100644
@@ -473,6 +473,7 @@ struct arch_vcpu
      */
     struct xsave_struct *xsave_area;
     uint64_t xcr0;
+    u64 msr_ia32_xss;
     /* Accumulated eXtended features mask for using XSAVE/XRESTORE by Xen
      * itself, as we can never know whether guest OS depends on content
      * preservation whenever guest OS clears one feature flag (for example,
xen/include/asm-x86/mm.h +1 -0
index 8595c38..94a590e 100644
@@ -524,6 +524,7 @@ void make_cr3(struct vcpu *v, unsigned long mfn);
 void update_cr3(struct vcpu *v);
 int vcpu_destroy_pagetables(struct vcpu *);
 struct trap_bounce *propagate_page_fault(unsigned long addr, u16 error_code);
+unsigned long do_page_walk_mfn(struct vcpu *v, unsigned long addr);
 void *do_page_walk(struct vcpu *v, unsigned long addr);
 
 int __sync_local_execstate(void);
xen/include/asm-x86/msr-index.h +2 -0
index 83f2f70..9564113 100644
@@ -58,6 +58,8 @@
 
 #define MSR_IA32_BNDCFGS		0x00000D90
 
+#define MSR_IA32_XSS			0x00000da0
+
 #define MSR_MTRRfix64K_00000		0x00000250
 #define MSR_MTRRfix16K_80000		0x00000258
 #define MSR_MTRRfix16K_A0000		0x00000259
xen/include/asm-x86/xstate.h +3 -0
index 4c690db..59c7156 100644
@@ -82,6 +82,8 @@ struct __packed __attribute__((aligned (64))) xsave_struct
 /* extended state operations */
 bool_t __must_check set_xcr0(u64 xfeatures);
 uint64_t get_xcr0(void);
+void xsaves(uint32_t lmask, uint32_t hmask, struct xsave_struct *ptr);
+void xrstors(uint32_t lmask, uint32_t hmask, struct xsave_struct *ptr);
 void xsave(struct vcpu *v, uint64_t mask);
 void xrstor(struct vcpu *v, uint64_t mask);
 bool_t xsave_enabled(const struct vcpu *v);
@@ -92,6 +94,7 @@ int __must_check handle_xsetbv(u32 index, u64 new_bv);
 void xstate_free_save_area(struct vcpu *v);
 int xstate_alloc_save_area(struct vcpu *v);
 void xstate_init(bool_t bsp);
+unsigned int xstate_ctxt_size_compact(u64 xcr0);
 unsigned int xstate_ctxt_size(u64 xcr0);
 
 #endif /* __ASM_XSTATE_H */
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH 2/6 x86/xsaves: enable xsaves/xrstors in xen

In reply to Shuai Ruan

This patch uses xsaves/xrstors instead of xsaveopt/xrstor when perform task switch in xen if the feature is supported in hardware.

Please note that xsaves/xrstors only use compact format.

---
 xen/arch/x86/xstate.c        | 83 ++++++++++++++++++++++++++++----------------
 xen/include/asm-x86/xstate.h |  3 +-
 2 files changed, 55 insertions(+), 31 deletions(-)
xen/arch/x86/xstate.c +53 -30
index e34eda3..ff67986 100644
@@ -14,6 +14,7 @@
 #include <asm/xstate.h>
 #include <asm/asm_defns.h>
 
+#define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
 static bool_t __read_mostly cpu_has_xsaveopt;
 static bool_t __read_mostly cpu_has_xsavec;
 bool_t __read_mostly cpu_has_xgetbv1;
@@ -102,7 +103,9 @@ void xsave(struct vcpu *v, uint64_t mask)
         typeof(ptr->fpu_sse.fip.sel) fcs = ptr->fpu_sse.fip.sel;
         typeof(ptr->fpu_sse.fdp.sel) fds = ptr->fpu_sse.fdp.sel;
 
-        if ( cpu_has_xsaveopt )
+        if ( cpu_has_xsaves )
+            xsaves(lmask, hmask, ptr);
+        else if ( cpu_has_xsaveopt )
         {
             /*
              * xsaveopt may not write the FPU portion even when the respective
@@ -155,7 +158,9 @@ void xsave(struct vcpu *v, uint64_t mask)
     }
     else
     {
-        if ( cpu_has_xsaveopt )
+        if ( cpu_has_xsaves )
+            xsaves(lmask, hmask, ptr);
+        else if ( cpu_has_xsaveopt )
             asm volatile ( ".byte 0x0f,0xae,0x37"
                            : "=m" (*ptr)
                            : "a" (lmask), "d" (hmask), "D" (ptr) );
@@ -198,36 +203,54 @@ void xrstor(struct vcpu *v, uint64_t mask)
     switch ( __builtin_expect(ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET], 8) )
     {
     default:
-        asm volatile ( "1: .byte 0x48,0x0f,0xae,0x2f\n"
-                       ".section .fixup,\"ax\"      \n"
-                       "2: mov %5,%%ecx             \n"
-                       "   xor %1,%1                \n"
-                       "   rep stosb                \n"
-                       "   lea %2,%0                \n"
-                       "   mov %3,%1                \n"
-                       "   jmp 1b                   \n"
-                       ".previous                   \n"
-                       _ASM_EXTABLE(1b, 2b)
-                       : "+&D" (ptr), "+&a" (lmask)
-                       : "m" (*ptr), "g" (lmask), "d" (hmask),
-                         "m" (xsave_cntxt_size)
-                       : "ecx" );
+        if ( cpu_has_xsaves )
+        {
+            if ( !(v->arch.xsave_area->xsave_hdr.xcomp_bv &
+                 XSTATE_COMPACTION_ENABLED) )
+                v->arch.xsave_area->xsave_hdr.xcomp_bv = get_xcr0() |
+                                    XSTATE_COMPACTION_ENABLED;
+            xrstors(lmask, hmask, ptr);
+        }
+        else
+            asm volatile ( "1: .byte 0x48,0x0f,0xae,0x2f\n"
+                           ".section .fixup,\"ax\"      \n"
+                           "2: mov %5,%%ecx             \n"
+                           "   xor %1,%1                \n"
+                           "   rep stosb                \n"
+                           "   lea %2,%0                \n"
+                           "   mov %3,%1                \n"
+                           "   jmp 1b                   \n"
+                           ".previous                   \n"
+                           _ASM_EXTABLE(1b, 2b)
+                           : "+&D" (ptr), "+&a" (lmask)
+                           : "m" (*ptr), "g" (lmask), "d" (hmask),
+                             "m" (xsave_cntxt_size)
+                           : "ecx" );
         break;
     case 4: case 2:
-        asm volatile ( "1: .byte 0x0f,0xae,0x2f\n"
-                       ".section .fixup,\"ax\" \n"
-                       "2: mov %5,%%ecx        \n"
-                       "   xor %1,%1           \n"
-                       "   rep stosb           \n"
-                       "   lea %2,%0           \n"
-                       "   mov %3,%1           \n"
-                       "   jmp 1b              \n"
-                       ".previous              \n"
-                       _ASM_EXTABLE(1b, 2b)
-                       : "+&D" (ptr), "+&a" (lmask)
-                       : "m" (*ptr), "g" (lmask), "d" (hmask),
-                         "m" (xsave_cntxt_size)
-                       : "ecx" );
+        if ( cpu_has_xsaves )
+        {
+            if ( !(v->arch.xsave_area->xsave_hdr.xcomp_bv &
+                 XSTATE_COMPACTION_ENABLED) )
+                v->arch.xsave_area->xsave_hdr.xcomp_bv = get_xcr0() |
+                                    XSTATE_COMPACTION_ENABLED;
+            xrstors(lmask, hmask, ptr);
+        }
+        else
+            asm volatile ( "1: .byte 0x48,0x0f,0xae,0x2f\n"
+                           ".section .fixup,\"ax\"      \n"
+                           "2: mov %5,%%ecx             \n"
+                           "   xor %1,%1                \n"
+                           "   rep stosb                \n"
+                           "   lea %2,%0                \n"
+                           "   mov %3,%1                \n"
+                           "   jmp 1b                   \n"
+                           ".previous                   \n"
+                           _ASM_EXTABLE(1b, 2b)
+                           : "+&D" (ptr), "+&a" (lmask)
+                           : "m" (*ptr), "g" (lmask), "d" (hmask),
+                             "m" (xsave_cntxt_size)
+                           : "ecx" );
         break;
     }
 }
xen/include/asm-x86/xstate.h +2 -1
index 59c7156..d03d824 100644
@@ -72,7 +72,8 @@ struct __packed __attribute__((aligned (64))) xsave_struct
 
     struct {
         u64 xstate_bv;
-        u64 reserved[7];
+        u64 xcomp_bv;
+        u64 reserved[6];
     } xsave_hdr;                             /* The 64-byte header */
 
     struct { char x[XSTATE_YMM_SIZE]; } ymm; /* YMM */
Signature · 1 line
1.9.1
author
#3 lore raw

PATCH 3/6 x86/xsaves: enable xsaves/xrstors for hvm guest

In reply to Shuai Ruan

This patch enables xsaves for hvm guest, includes:
1.handle xsaves vmcs init and vmexit. 2.add logic to write/read the XSS msr.

---
 xen/arch/x86/hvm/hvm.c             | 40 ++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmcs.c        |  7 ++++++-
 xen/arch/x86/hvm/vmx/vmx.c         | 18 +++++++++++++++++
 xen/arch/x86/xstate.c              |  4 ++--
 xen/include/asm-x86/hvm/vmx/vmcs.h |  5 +++++
 xen/include/asm-x86/hvm/vmx/vmx.h  |  2 ++
 xen/include/asm-x86/xstate.h       |  1 +
 7 files changed, 74 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hvm.c +40 -0
index 535d622..2958e0d 100644
@@ -4269,6 +4269,10 @@ void hvm_hypervisor_cpuid_leaf(uint32_t sub_idx,
     }
 }
 
+#define XSAVEOPT	(1 << 0)
+#define XSAVEC		(1 << 1)
+#define XGETBV		(1 << 2)
+#define XSAVES		(1 << 3)
 void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                                    unsigned int *ecx, unsigned int *edx)
 {
@@ -4355,6 +4359,34 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                     *ebx = _eax + _ebx;
             }
         }
+        if ( count == 1 )
+        {
+            if ( cpu_has_xsaves )
+            {
+                *ebx = XSTATE_AREA_MIN_SIZE;
+                if ( v->arch.xcr0 | v->arch.msr_ia32_xss )
+                    for ( sub_leaf = 2; sub_leaf < 63; sub_leaf++ )
+                    {
+                        if ( !((v->arch.xcr0 | v->arch.msr_ia32_xss)
+			   & (1ULL << sub_leaf)) )
+                            continue;
+                        domain_cpuid(d, input, sub_leaf, &_eax, &_ebx, &_ecx,
+                                     &_edx);
+                        *ebx =  *ebx + _eax;
+                    }
+            }
+            else
+            {
+                *eax &= ~XSAVES;
+                if ( !cpu_has_xgetbv1 )
+                    *eax &= ~XGETBV;
+                if ( !cpu_has_xsavec )
+                    *eax &= ~XSAVEC;
+                if ( !cpu_has_xsaveopt )
+                    *eax &= ~XSAVEOPT;
+                *ebx = *ecx = *edx = 0;
+            }
+        }
         break;
 
     case 0x80000001:
@@ -4454,6 +4486,10 @@ int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         *msr_content = v->arch.hvm_vcpu.guest_efer;
         break;
 
+    case MSR_IA32_XSS:
+        *msr_content = v->arch.msr_ia32_xss;
+        break;
+
     case MSR_IA32_TSC:
         *msr_content = _hvm_rdtsc_intercept();
         break;
@@ -4573,6 +4609,10 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
            return X86EMUL_EXCEPTION;
         break;
 
+    case MSR_IA32_XSS:
+        v->arch.msr_ia32_xss = msr_content;
+        break;
+
     case MSR_IA32_TSC:
         hvm_set_guest_tsc(v, msr_content);
         break;
xen/arch/x86/hvm/vmx/vmcs.c +6 -1
index 4c5ceb5..8e61e3f 100644
@@ -230,7 +230,8 @@ static int vmx_init_vmcs_config(void)
                SECONDARY_EXEC_ENABLE_EPT |
                SECONDARY_EXEC_ENABLE_RDTSCP |
                SECONDARY_EXEC_PAUSE_LOOP_EXITING |
-               SECONDARY_EXEC_ENABLE_INVPCID);
+               SECONDARY_EXEC_ENABLE_INVPCID |
+               SECONDARY_EXEC_XSAVES);
         rdmsrl(MSR_IA32_VMX_MISC, _vmx_misc_cap);
         if ( _vmx_misc_cap & VMX_MISC_VMWRITE_ALL )
             opt |= SECONDARY_EXEC_ENABLE_VMCS_SHADOWING;
@@ -921,6 +922,7 @@ void virtual_vmcs_vmwrite(void *vvmcs, u32 vmcs_encoding, u64 val)
     virtual_vmcs_exit(vvmcs);
 }
 
+#define VMX_XSS_EXIT_BITMAP 0
 static int construct_vmcs(struct vcpu *v)
 {
     struct domain *d = v->domain;
@@ -1204,6 +1206,9 @@ static int construct_vmcs(struct vcpu *v)
         __vmwrite(GUEST_PAT, guest_pat);
     }
 
+    if ( cpu_has_vmx_xsaves )
+        __vmwrite(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
+
     vmx_vmcs_exit(v);
 
     /* PVH: paging mode is updated by arch_set_info_guest(). */
xen/arch/x86/hvm/vmx/vmx.c +18 -0
index fc29b89..7c950b3 100644
@@ -2683,6 +2683,16 @@ static int vmx_handle_apic_write(void)
     return vlapic_apicv_write(current, exit_qualification & 0xfff);
 }
 
+static void vmx_handle_xsaves(void)
+{
+    WARN();
+}
+
+static void vmx_handle_xrstors(void)
+{
+    WARN();
+}
+
 void vmx_vmexit_handler(struct cpu_user_regs *regs)
 {
     unsigned long exit_qualification, exit_reason, idtv_info, intr_info = 0;
@@ -3201,6 +3211,14 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
         vmx_vcpu_flush_pml_buffer(v);
         break;
 
+    case EXIT_REASON_XSAVES:
+        vmx_handle_xsaves();
+        break;
+
+    case EXIT_REASON_XRSTORS:
+        vmx_handle_xrstors();
+        break;
+
     case EXIT_REASON_ACCESS_GDTR_OR_IDTR:
     case EXIT_REASON_ACCESS_LDTR_OR_TR:
     case EXIT_REASON_VMX_PREEMPTION_TIMER_EXPIRED:
xen/arch/x86/xstate.c +2 -2
index ff67986..73a16b9 100644
@@ -15,8 +15,8 @@
 #include <asm/asm_defns.h>
 
 #define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
-static bool_t __read_mostly cpu_has_xsaveopt;
-static bool_t __read_mostly cpu_has_xsavec;
+bool_t __read_mostly cpu_has_xsaveopt;
+bool_t __read_mostly cpu_has_xsavec;
 bool_t __read_mostly cpu_has_xgetbv1;
 bool_t __read_mostly cpu_has_xsaves;
 
xen/include/asm-x86/hvm/vmx/vmcs.h +5 -0
index 1104bda..2687634 100644
@@ -225,6 +225,7 @@ extern u32 vmx_vmentry_control;
 #define SECONDARY_EXEC_ENABLE_VMFUNC            0x00002000
 #define SECONDARY_EXEC_ENABLE_VMCS_SHADOWING    0x00004000
 #define SECONDARY_EXEC_ENABLE_PML               0x00020000
+#define SECONDARY_EXEC_XSAVES                   0x00100000
 extern u32 vmx_secondary_exec_control;
 
 #define VMX_EPT_EXEC_ONLY_SUPPORTED             0x00000001
@@ -287,6 +288,8 @@ extern u32 vmx_secondary_exec_control;
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VMCS_SHADOWING)
 #define cpu_has_vmx_pml \
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_PML)
+#define cpu_has_vmx_xsaves \
+    (vmx_secondary_exec_control & SECONDARY_EXEC_XSAVES)
 
 #define VMCS_RID_TYPE_MASK              0x80000000
 
@@ -356,6 +359,8 @@ enum vmcs_field {
 #define EOI_EXIT_BITMAP(n) (EOI_EXIT_BITMAP0 + (n) * 2) /* n = 0...3 */
     VMREAD_BITMAP                   = 0x00002026,
     VMWRITE_BITMAP                  = 0x00002028,
+    XSS_EXIT_BITMAP                 = 0x0000202c,
+    XSS_EXIT_BITMAP_HIGH            = 0x0000202d,
     GUEST_PHYSICAL_ADDRESS          = 0x00002400,
     VMCS_LINK_POINTER               = 0x00002800,
     GUEST_IA32_DEBUGCTL             = 0x00002802,
xen/include/asm-x86/hvm/vmx/vmx.h +2 -0
index 35f804a..b4c5e73 100644
@@ -187,6 +187,8 @@ static inline unsigned long pi_get_pir(struct pi_desc *pi_desc, int group)
 #define EXIT_REASON_APIC_WRITE          56
 #define EXIT_REASON_INVPCID             58
 #define EXIT_REASON_PML_FULL            62
+#define EXIT_REASON_XSAVES              63
+#define EXIT_REASON_XRSTORS             64
 
 /*
  * Interruption-information format
xen/include/asm-x86/xstate.h +1 -0
index d03d824..1357063 100644
@@ -44,6 +44,7 @@
 
 extern u64 xfeature_mask;
 extern bool_t cpu_has_xsaves, cpu_has_xgetbv1;
+extern bool_t cpu_has_xsavec, cpu_has_xsaveopt;
 
 /* extended state save area */
 struct __packed __attribute__((aligned (64))) xsave_struct
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH 3/6] x86/xsaves: enable xsaves/xrstors for hvm guest

Quoted 20 lines · Shuai Ruan

This patch enables xsaves for hvm guest, includes:
1.handle xsaves vmcs init and vmexit. 2.add logic to write/read the XSS msr.

---
 xen/arch/x86/hvm/hvm.c             | 40 ++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmcs.c        |  7 ++++++-
 xen/arch/x86/hvm/vmx/vmx.c         | 18 +++++++++++++++++
 xen/arch/x86/xstate.c              |  4 ++--
 xen/include/asm-x86/hvm/vmx/vmcs.h |  5 +++++
 xen/include/asm-x86/hvm/vmx/vmx.h  |  2 ++
 xen/include/asm-x86/xstate.h       |  1 +
 7 files changed, 74 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -0
index 535d622..2958e0d 100644

xen/arch/x86/hvm/hvm.c void hvm_hypervisor_cpuid_leaf(uint32_t sub_idx, · line 4275

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +4 -0
@@ -4269,6 +4269,10 @@ void hvm_hypervisor_cpuid_leaf(uint32_t sub_idx,
     }
 }
 
+#define XSAVEOPT	(1 << 0)
+#define XSAVEC		(1 << 1)
+#define XGETBV		(1 << 2)
+#define XSAVES		(1 << 3)

Hard tab is used.

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +0 -0
 void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                                    unsigned int *ecx, unsigned int *edx)
 {
@@ -4355,6 +4359,34 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                     *ebx = _eax + _ebx;
             }
         }

xen/arch/x86/hvm/hvm.c void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, · line 4371

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+        if ( count == 1 )
+        {
+            if ( cpu_has_xsaves )
+            {
+                *ebx = XSTATE_AREA_MIN_SIZE;
+                if ( v->arch.xcr0 | v->arch.msr_ia32_xss )
+                    for ( sub_leaf = 2; sub_leaf < 63; sub_leaf++ )
+                    {
+                        if ( !((v->arch.xcr0 | v->arch.msr_ia32_xss)
+			   & (1ULL << sub_leaf)) )

alignment.

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5 -0
+                            continue;
+                        domain_cpuid(d, input, sub_leaf, &_eax, &_ebx, &_ecx,
+                                     &_edx);
+                        *ebx =  *ebx + _eax;
+                    }

xen/arch/x86/hvm/hvm.c void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, · line 4386

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+            }
+            else
+            {
+                *eax &= ~XSAVES;
+                if ( !cpu_has_xgetbv1 )
+                    *eax &= ~XGETBV;
+                if ( !cpu_has_xsavec )
+                    *eax &= ~XSAVEC;
+                if ( !cpu_has_xsaveopt )
+                    *eax &= ~XSAVEOPT;

I think these features(XGETBV/XSAVEC/XSAVEOPT) should be independent of XSAVES, e.g. here they should not be enclosed with 'else' statement.

Chao

author
#5 lore raw

PATCH 4/6 libxc: expose xsaves/xgetbv/xsavec to hvm guest

In reply to Shuai Ruan

This patch exposes xsaves/xgetbv/xsavec to hvm guest.

---
 tools/libxc/xc_cpuid_x86.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/libxc/xc_cpuid_x86.c +4 -4
index c97f91a..0ed8b68 100644
@@ -211,6 +211,9 @@ static void intel_xc_cpuid_policy(
 }
 
 #define XSAVEOPT        (1 << 0)
+#define XSAVEC          (1 << 1)
+#define XGETBV          (1 << 2)
+#define XSAVES          (1 << 3)
 /* Configure extended state enumeration leaves (0x0000000D for xsave) */
 static void xc_cpuid_config_xsave(
     xc_interface *xch, domid_t domid, uint64_t xfeature_mask,
@@ -247,8 +250,7 @@ static void xc_cpuid_config_xsave(
         regs[1] = 512 + 64; /* FP/SSE + XSAVE.HEADER */
         break;
     case 1: /* leaf 1 */
-        regs[0] &= XSAVEOPT;
-        regs[1] = regs[2] = regs[3] = 0;
+        regs[0] &= (XSAVEOPT | XSAVEC | XGETBV | XSAVES);
         break;
     case 2 ... 63: /* sub-leaves */
         if ( !(xfeature_mask & (1ULL << input[1])) )
@@ -256,8 +258,6 @@ static void xc_cpuid_config_xsave(
             regs[0] = regs[1] = regs[2] = regs[3] = 0;
             break;
         }
-        /* Don't touch EAX, EBX. Also cleanup ECX and EDX */
-        regs[2] = regs[3] = 0;
         break;
     }
 }
Signature · 1 line
1.9.1
#6 lore raw

Re: [PATCH 4/6] libxc: expose xsaves/xgetbv/xsavec to hvm guest

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 02.07.15 at 16:02, <[email protected]> wrote:

This patch exposes xsaves/xgetbv/xsavec to hvm guest.

---
 tools/libxc/xc_cpuid_x86.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/libxc/xc_cpuid_x86.c +0 -0
index c97f91a..0ed8b68 100644

tools/libxc/xc_cpuid_x86.c static void intel_xc_cpuid_policy( · line 215

Quoted 5 lines
tools/libxc/xc_cpuid_x86.c +2 -0
@@ -211,6 +211,9 @@ static void intel_xc_cpuid_policy(
 }
 
 #define XSAVEOPT        (1 << 0)
+#define XSAVEC          (1 << 1)
+#define XGETBV          (1 << 2)

This needs a better macro name, as the bit isn't about XGETBV in general, but the case of it supporting ECX=1 on input.

Jan

author
#7 lore raw

PATCH 5/6 x86/xsaves: support compact format for hvm save/restore

In reply to Shuai Ruan

xsaves/xrstors only use compat format, so format convertion is needed when perform save/restore.

---
 xen/arch/x86/hvm/hvm.c       |  16 +++--
 xen/arch/x86/xstate.c        | 137 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/xstate.h |   6 ++
 3 files changed, 154 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/hvm.c +11 -5
index 2958e0d..86d1579 100644
@@ -2045,8 +2045,11 @@ static int hvm_save_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)
         ctxt->xfeature_mask = xfeature_mask;
         ctxt->xcr0 = v->arch.xcr0;
         ctxt->xcr0_accum = v->arch.xcr0_accum;
-        memcpy(&ctxt->save_area, v->arch.xsave_area,
-               size - offsetof(struct hvm_hw_cpu_xsave, save_area));
+        if ( cpu_has_xsaves )
+            save_xsave_states(v, (u8 *)&ctxt->save_area);
+        else
+            memcpy(&ctxt->save_area, v->arch.xsave_area,
+                   size - offsetof(struct hvm_hw_cpu_xsave, save_area));
     }
 
     return 0;
@@ -2145,9 +2148,12 @@ static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)
     v->arch.xcr0_accum = ctxt->xcr0_accum;
     if ( ctxt->xcr0_accum & XSTATE_NONLAZY )
         v->arch.nonlazy_xstate_used = 1;
-    memcpy(v->arch.xsave_area, &ctxt->save_area,
-           min(desc->length, size) - offsetof(struct hvm_hw_cpu_xsave,
-           save_area));
+    if ( cpu_has_xsaves )
+        load_xsave_states(v, (u8 *)&ctxt->save_area);
+    else
+        memcpy(v->arch.xsave_area, &ctxt->save_area,
+               min(desc->length, size) - offsetof(struct hvm_hw_cpu_xsave,
+               save_area));
 
     return 0;
 }
xen/arch/x86/xstate.c +137 -0

155 lines not on this page · raw message

xen/include/asm-x86/xstate.h +6 -0
index 1357063..38129a1 100644
@@ -22,7 +22,11 @@
 
 #define XCR_XFEATURE_ENABLED_MASK 0x00000000  /* index of XCR0 */
 
+#define XSAVE_HDR_SIZE            64
+#define XSAVE_SSE_OFFSET          160
 #define XSTATE_YMM_SIZE           256
+#define FXSAVE_SIZE               512
+#define XSAVE_HDR_OFFSET          FXSAVE_SIZE
 #define XSTATE_AREA_MIN_SIZE      (512 + 64)  /* FP/SSE + XSAVE.HEADER */
 
 #define XSTATE_FP      (1ULL << 0)
@@ -86,6 +90,8 @@ bool_t __must_check set_xcr0(u64 xfeatures);
 uint64_t get_xcr0(void);
 void xsaves(uint32_t lmask, uint32_t hmask, struct xsave_struct *ptr);
 void xrstors(uint32_t lmask, uint32_t hmask, struct xsave_struct *ptr);
+void save_xsave_states(struct vcpu *v, u8 *dest);
+void load_xsave_states(struct vcpu *v, u8 *src);
 void xsave(struct vcpu *v, uint64_t mask);
 void xrstor(struct vcpu *v, uint64_t mask);
 bool_t xsave_enabled(const struct vcpu *v);
Signature · 1 line
1.9.1
author
#8 lore raw

PATCH 6/6 x86/xsaves: detect xsaves/xgetbv in xen

In reply to Shuai Ruan

As xsaves/xgetbv already support, so switch on.

---
 xen/arch/x86/xstate.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/xstate.c +4 -4
index c20f865..ebf9920 100644
@@ -525,15 +525,15 @@ void xstate_init(bool_t bsp)
     {
         cpu_has_xsaveopt = !!(eax & XSTATE_FEATURE_XSAVEOPT);
         cpu_has_xsavec = !!(eax & XSTATE_FEATURE_XSAVEC);
-        /* XXX cpu_has_xgetbv1 = !!(eax & XSTATE_FEATURE_XGETBV1); */
-        /* XXX cpu_has_xsaves = !!(eax & XSTATE_FEATURE_XSAVES); */
+        cpu_has_xgetbv1 = !!(eax & XSTATE_FEATURE_XGETBV1);
+        cpu_has_xsaves = !!(eax & XSTATE_FEATURE_XSAVES);
     }
     else
     {
         BUG_ON(!cpu_has_xsaveopt != !(eax & XSTATE_FEATURE_XSAVEOPT));
         BUG_ON(!cpu_has_xsavec != !(eax & XSTATE_FEATURE_XSAVEC));
-        /* XXX BUG_ON(!cpu_has_xgetbv1 != !(eax & XSTATE_FEATURE_XGETBV1)); */
-        /* XXX BUG_ON(!cpu_has_xsaves != !(eax & XSTATE_FEATURE_XSAVES)); */
+        BUG_ON(!cpu_has_xgetbv1 != !(eax & XSTATE_FEATURE_XGETBV1));
+        BUG_ON(!cpu_has_xsaves != !(eax & XSTATE_FEATURE_XSAVES));
     }
 
     if ( cpu_has_xsaves )
Signature · 1 line
1.9.1
#9 lore raw
In reply to Shuai Ruan
Quoted 5 lines · Shuai Ruan

This patchset enable xsaves/xrstors feature.
It includes tree parts:
1. add xsaves/xrstors for xen.
2. add xsaves/xrstors for pv guest.
3. add xsaves/xrstors for hvn guest.

What is xsaves/xrstors and why might I want Xen to use it? What advantages does it give? When might these instructions be available? Where can I read more details about this?

~Andrew

#10 lore raw

OK,I will add thest.Thanks.

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: Thursday, July 2, 2015 10:09 PM
To: Ruan, Shuai; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Nakajima, Jun; [email protected]; Dong, Eddie; Tian, Kevin
Subject: Re: [PATCH 0/6] add xsaves/xrstors support

Quoted 5 lines · Shuai Ruan

This patchset enable xsaves/xrstors feature.
It includes tree parts:
1. add xsaves/xrstors for xen.
2. add xsaves/xrstors for pv guest.
3. add xsaves/xrstors for hvn guest.

What is xsaves/xrstors and why might I want Xen to use it? What advantages does it give? When might these instructions be available? Where can I read more details about this?

~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#11 lore raw
Quoted 1 line · Shuai

OK,I will add thest.Thanks.

And while you are at it, you need to perform some migration testing to cover compatibility cases. Doing this will show why we currently do not use compressed xsave layouts.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel