Skip to contentSkip to conversation

add xsaves/xrstors support

PATCH Superseded Shuai Ruan 2 participants 12 messages Nov 3, 2015 – Nov 6, 2015 2 messages not in the archive

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Shuai Ruan

author
#0 lore raw

v9 cover add xsaves/xrstors support

From: Shuai Ruan <[email protected]>

Changes in v9:
* Address comments form Jan:
* Add msr_ia32_xss save/restor support in patch2.
* Change xrstor to alternative asm in patch1.
* Use memcpy() copy the save header to avoid one ugly cast in patch1.
* Fix coding stype errors.

Changes in v8:
* Address comments form Jan/Andrew:
* Add optimisation in set_msr_xss in patch1.
* Change from black listing feature to white listing in pv_cpuid in patch2.
* Add MSR_IA32_XSS save/restore support in patch2.
* Fix some code errors and coding style errors.

Changes in v7:
* Address comments form Jan/Andrew:
* Move macro XSTATE_FIXUP into patch3.
* Change lazy write to xss_msr in patch1.
* Drop inner set of brackets and stray cast in patch2.
* Move the condition and memcpy() wouldn't into the new called functions in patch2.
* Rebase patch4 base on Andrew's"tools/libxc: Improve efficiency of xc_cpuid_apply_policy()".
* For no XSS features are currently supported.For leaf 2...63,ecx remain zero at the moment in patch4.

Changes in v6:
* Address comments from Jan.
* Rebase the code based on Andrew'patch "xen/x86: Record xsave features in c->x86_capabilities".
* Re-split the patch to avoid building errors. Move some func definitions from patch1 to patch2.
* Change func name load/save_xsave_states to compress/expand_xsave_states in patch2.
* Add macro to handle redundancy in xrstor.
* Fix some code errors and coding style errors.
* Add some descriptions in commit message.

Changes in v5:
* Address comments from Andrew/Jan,mainly:
* Add lazy writes of the xss_msr.
* Add xsave_area check when save/restore guest.
* Add xsavec support.
* Use word 2 in xen/include/asm-x86/cpufeature.h.
* Fix some code errors.

Changes in v4:
* Address comments from Andrew, mainly:
* No xsaves suporting for pv guest.
* Using xstate_sizes instead of domain_cpuid in hvm_cupid in patch 3.
* Add support for format translation when perform pv guest migration in patch 2.
* Fix some code errors.

Changes in v3:
* Address comments from Jan/Konrad
* Change the bits of eax/ebx/ecx/edx passed to guest in patch 4.
* Add code to verify whether host supports xsaves or not in patch 1.

Changes in v2:
* Address comments from Andrew/chao/Jan, mainly:
* Add details information of xsaves/xrstors feature.
* Test migration between xsaves-support machine and none-xsaves-support machine
* Remove XGETBV1/XSAVEC/XSAVEOPT out of 'else' in patch 3.
* Change macro name XGETBV to XGETBV1 in patch 4.

This patchset enable xsaves/xrstors feature which will be available on Intel Skylake and later platform. Like xsave/xrstor, xsaves/xrstors feature will save and load processor state from a region of memory called XSAVE area. While unlike xsave/xrstor, xsaves/xrstors:

a) use the compacted format only for the extended region of the XSAVE area which saves memory for you;
b) can operate on supervisor state components so the feature is prerequisite to support new supervisor state components;
c) execute only if CPL=0.

Detail hardware spec can be found in chapter 13 (section 13.11 13.12) of the Intel SDM [1].

[1] Intel SDM (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf)

Shuai Ruan (3):
x86/xsaves: enable xsaves/xrstors/xsavec in xen x86/xsaves: enable xsaves/xrstors for hvm guest
libxc: expose xsaves/xgetbv1/xsavec to hvm guest

 tools/libxc/xc_cpuid_x86.c         |  10 +-
 xen/arch/x86/domain.c              |   7 ++
 xen/arch/x86/domctl.c              |  30 ++++-
 xen/arch/x86/hvm/hvm.c             |  45 ++++++-
 xen/arch/x86/hvm/vmx/vmcs.c        |   5 +-
 xen/arch/x86/hvm/vmx/vmx.c         |  36 +++++-
 xen/arch/x86/i387.c                |   4 +
 xen/arch/x86/traps.c               |   6 +-
 xen/arch/x86/xstate.c              | 242 ++++++++++++++++++++++++++++++++-----
 xen/include/asm-x86/hvm/vmx/vmcs.h |   4 +
 xen/include/asm-x86/hvm/vmx/vmx.h  |   2 +
 xen/include/asm-x86/xstate.h       |   3 +
 12 files changed, 346 insertions(+), 48 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v9 1/3 x86/xsaves: enable xsaves/xrstors/xsavec in xen

This patch uses xsaves/xrstors/xsavec instead of xsaveopt/xrstor to perform the xsave_area switching so that xen itself can benefit from them when available.

For xsaves/xrstors/xsavec only use compact format. Add format conversion support when perform guest os migration.

Also, pv guest will not support xsaves/xrstors.

---
 xen/arch/x86/domain.c        |   7 ++
 xen/arch/x86/domctl.c        |  30 +++++-
 xen/arch/x86/hvm/hvm.c       |  18 +++-
 xen/arch/x86/i387.c          |   4 +
 xen/arch/x86/traps.c         |   6 +-
 xen/arch/x86/xstate.c        | 242 +++++++++++++++++++++++++++++++++++++------
 xen/include/asm-x86/xstate.h |   2 +
 7 files changed, 265 insertions(+), 44 deletions(-)
xen/arch/x86/domain.c +7 -0
index fe3be30..108d4f8 100644
@@ -883,7 +883,12 @@ int arch_set_info_guest(
     {
         memcpy(v->arch.fpu_ctxt, &c.nat->fpu_ctxt, sizeof(c.nat->fpu_ctxt));
         if ( v->arch.xsave_area )
+        {
              v->arch.xsave_area->xsave_hdr.xstate_bv = XSTATE_FP_SSE;
+             if ( cpu_has_xsaves || cpu_has_xsavec )
+                  v->arch.xsave_area->xsave_hdr.xcomp_bv = XSTATE_FP_SSE |
+                                                           XSTATE_COMPACTION_ENABLED;
+        }
     }
 
     if ( !compat )
@@ -1568,6 +1573,8 @@ static void __context_switch(void)
             if ( xcr0 != get_xcr0() && !set_xcr0(xcr0) )
                 BUG();
         }
+        if ( cpu_has_xsaves && has_hvm_container_vcpu(n) )
+            set_msr_xss(n->arch.hvm_vcpu.msr_xss);
         vcpu_restore_fpu_eager(n);
         n->arch.ctxt_switch_to(n);
     }
xen/arch/x86/domctl.c +25 -5
index 0f6fdb9..95b0747 100644
@@ -897,9 +897,29 @@ long arch_do_domctl(
                 ret = -EFAULT;
 
             offset += sizeof(v->arch.xcr0_accum);
-            if ( !ret && copy_to_guest_offset(evc->buffer, offset,
-                                              (void *)v->arch.xsave_area,
-                                              size - 2 * sizeof(uint64_t)) )
+            if ( !ret && (cpu_has_xsaves || cpu_has_xsavec) )
+            {
+                void *xsave_area;
+
+                xsave_area = xmalloc_bytes(size);
+                if ( !xsave_area )
+                {
+                    ret = -ENOMEM;
+                    vcpu_unpause(v);
+                    goto vcpuextstate_out;
+                }
+
+                expand_xsave_states(v, xsave_area,
+                                    size - 2 * sizeof(uint64_t));
+
+                if ( copy_to_guest_offset(evc->buffer, offset, xsave_area,
+                                          size - 2 * sizeof(uint64_t)) )
+                     ret = -EFAULT;
+                xfree(xsave_area);
+           }
+           else if ( !ret && copy_to_guest_offset(evc->buffer, offset,
+                                                  (void *)v->arch.xsave_area,
+                                                  size - 2 * sizeof(uint64_t)) )
                 ret = -EFAULT;
 
             vcpu_unpause(v);
@@ -955,8 +975,8 @@ long arch_do_domctl(
                 v->arch.xcr0_accum = _xcr0_accum;
                 if ( _xcr0_accum & XSTATE_NONLAZY )
                     v->arch.nonlazy_xstate_used = 1;
-                memcpy(v->arch.xsave_area, _xsave_area,
-                       evc->size - 2 * sizeof(uint64_t));
+                compress_xsave_states(v, _xsave_area,
+                                      evc->size - 2 * sizeof(uint64_t));
                 vcpu_unpause(v);
             }
             else
xen/arch/x86/hvm/hvm.c +13 -5
index 21f42a7..dd86730 100644
@@ -2090,6 +2090,9 @@ static int hvm_load_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
 
         memcpy(v->arch.xsave_area, ctxt.fpu_regs, sizeof(ctxt.fpu_regs));
         xsave_area->xsave_hdr.xstate_bv = XSTATE_FP_SSE;
+        if ( cpu_has_xsaves || cpu_has_xsavec )
+            xsave_area->xsave_hdr.xcomp_bv = XSTATE_FP_SSE |
+                                             XSTATE_COMPACTION_ENABLED;
     }
     else
         memcpy(v->arch.fpu_ctxt, ctxt.fpu_regs, sizeof(ctxt.fpu_regs));
@@ -2159,8 +2162,8 @@ 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));
+        expand_xsave_states(v, &ctxt->save_area,
+                            size - offsetof(typeof(*ctxt), save_area));
     }
 
     return 0;
@@ -2259,9 +2262,9 @@ 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));
+    compress_xsave_states(v, &ctxt->save_area,
+                          min(desc->length, size) -
+                          offsetof(struct hvm_hw_cpu_xsave,save_area));
 
     return 0;
 }
@@ -5458,7 +5461,12 @@ void hvm_vcpu_reset_state(struct vcpu *v, uint16_t cs, uint16_t ip)
     fpu_ctxt->fcw = FCW_RESET;
     fpu_ctxt->mxcsr = MXCSR_DEFAULT;
     if ( v->arch.xsave_area )
+    {
         v->arch.xsave_area->xsave_hdr.xstate_bv = XSTATE_FP;
+        if ( cpu_has_xsaves || cpu_has_xsavec )
+            v->arch.xsave_area->xsave_hdr.xcomp_bv = XSTATE_FP |
+                                                     XSTATE_COMPACTION_ENABLED;
+    }
 
     v->arch.vgc_flags = VGCF_online;
     memset(&v->arch.user_regs, 0, sizeof(v->arch.user_regs));
xen/arch/x86/i387.c +4 -0
index 66b51cb..b661d39 100644
@@ -282,7 +282,11 @@ int vcpu_init_fpu(struct vcpu *v)
         return rc;
 
     if ( v->arch.xsave_area )
+    {
         v->arch.fpu_ctxt = &v->arch.xsave_area->fpu_sse;
+        if ( cpu_has_xsaves || cpu_has_xsavec )
+            v->arch.xsave_area->xsave_hdr.xcomp_bv = XSTATE_COMPACTION_ENABLED;
+    }
     else
     {
         v->arch.fpu_ctxt = _xzalloc(sizeof(v->arch.xsave_area->fpu_sse), 16);
xen/arch/x86/traps.c +3 -3
index b32f696..9063314 100644
@@ -935,9 +935,9 @@ void pv_cpuid(struct cpu_user_regs *regs)
             goto unsupported;
         if ( regs->_ecx == 1 )
         {
-            a &= boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)];
-            if ( !cpu_has_xsaves )
-                b = c = d = 0;
+            a &= (boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)] &
+                  ~cpufeat_mask(X86_FEATURE_XSAVES));
+            b = c = d = 0;
         }
         break;
 
xen/arch/x86/xstate.c +211 -31

293 lines not on this page · raw message

xen/include/asm-x86/xstate.h +2 -0
index b95a5b5..414cc99 100644
@@ -91,6 +91,8 @@ void xrstor(struct vcpu *v, uint64_t mask);
 bool_t xsave_enabled(const struct vcpu *v);
 int __must_check validate_xstate(u64 xcr0, u64 xcr0_accum, u64 xstate_bv);
 int __must_check handle_xsetbv(u32 index, u64 new_bv);
+void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size);
+void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size);
 
 /* extended state init and cleanup functions */
 void xstate_free_save_area(struct vcpu *v);
Signature · 1 line
1.9.1
#2 lore raw

Re: [V9 1/3] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 03.11.15 at 07:27, <[email protected]> wrote:

This patch uses xsaves/xrstors/xsavec instead of xsaveopt/xrstor to perform the xsave_area switching so that xen itself can benefit from them when available.

For xsaves/xrstors/xsavec only use compact format. Add format conversion support when perform guest os migration.

Also, pv guest will not support xsaves/xrstors.

---
 xen/arch/x86/domain.c        |   7 ++
 xen/arch/x86/domctl.c        |  30 +++++-
 xen/arch/x86/hvm/hvm.c       |  18 +++-
 xen/arch/x86/i387.c          |   4 +
 xen/arch/x86/traps.c         |   6 +-
 xen/arch/x86/xstate.c        | 242 +++++++++++++++++++++++++++++++++++++------
 xen/include/asm-x86/xstate.h |   2 +
 7 files changed, 265 insertions(+), 44 deletions(-)
xen/arch/x86/domain.c +0 -0
index fe3be30..108d4f8 100644

xen/arch/x86/domain.c int arch_set_info_guest( · line 891

Quoted 9 lines
xen/arch/x86/domain.c +5 -0
@@ -883,7 +883,12 @@ int arch_set_info_guest(
     {
         memcpy(v->arch.fpu_ctxt, &c.nat->fpu_ctxt, sizeof(c.nat->fpu_ctxt));
         if ( v->arch.xsave_area )
+        {
              v->arch.xsave_area->xsave_hdr.xstate_bv = XSTATE_FP_SSE;
+             if ( cpu_has_xsaves || cpu_has_xsavec )
+                  v->arch.xsave_area->xsave_hdr.xcomp_bv = XSTATE_FP_SSE |
+                                                           XSTATE_COMPACTION_ENABLED;
+        }

So here you nicely extend the existing conditional.

xen/arch/x86/domain.c static void __context_switch(void) · line 1577

Quoted 6 lines
xen/arch/x86/domain.c +2 -0
@@ -1568,6 +1573,8 @@ static void __context_switch(void)
             if ( xcr0 != get_xcr0() && !set_xcr0(xcr0) )
                 BUG();
         }
+        if ( cpu_has_xsaves && has_hvm_container_vcpu(n) )
+            set_msr_xss(n->arch.hvm_vcpu.msr_xss);

Why not also here (the previous if() uses cpu_has_xsave, which you surely depend on)? Agreed the difference is minor for modern CPUs, but I wanted to ask anyway. I.e. an explanation will do, no need to re-submit just because of this.

Quoted 59 lines
xen/arch/x86/domain.c +19 -27
@@ -158,6 +334,20 @@ void xsave(struct vcpu *v, uint64_t mask)
         ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET] = word_size;
 }
 
+#define XSTATE_FIXUP ".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 xrstor(struct vcpu *v, uint64_t mask)
 {
     uint32_t hmask = mask >> 32;
@@ -187,39 +377,22 @@ 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" );
+        alternative_input("1: "".byte 0x48,0x0f,0xae,0x2f",
+                          ".byte 0x48,0x0f,0xc7,0x1f",
+                          X86_FEATURE_XSAVES,
+                          "D" (ptr), "m" (*ptr), "a" (lmask), "d" (hmask));
+        asm volatile (XSTATE_FIXUP);
         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)

xen/arch/x86/domain.c void xrstor(struct vcpu *v, uint64_t mask) · line 395

Quoted 10 lines
xen/arch/x86/domain.c +6 -1
-                       : "ecx" );
+        alternative_input("1: "".byte 0x0f,0xae,0x2f",
+                          ".byte 0x0f,0xc7,0x1f",
+                          X86_FEATURE_XSAVES,
+                          "D" (ptr), "m" (*ptr), "a" (lmask), "d" (hmask));
+        asm volatile (XSTATE_FIXUP);
         break;
     }
 }
+#undef XSTATE_FIXUP

Repeating my comment on v8: "I wonder whether at least for the restore side alternative asm wouldn't result in better readable code and at the same time in a smaller patch." Did you at least look into that option?

Quoted 5 lines
xen/arch/x86/domain.c +0 -1
@@ -343,11 +516,18 @@ void xstate_init(struct cpuinfo_x86 *c)
 
     /* Mask out features not currently understood by Xen. */
     eax &= (cpufeat_mask(X86_FEATURE_XSAVEOPT) |
-            cpufeat_mask(X86_FEATURE_XSAVEC));

xen/arch/x86/domain.c void xstate_init(struct cpuinfo_x86 *c) · line 528

Quoted 10 lines
xen/arch/x86/domain.c +6 -0
+            cpufeat_mask(X86_FEATURE_XSAVEC) |
+            cpufeat_mask(X86_FEATURE_XGETBV1) |
+            cpufeat_mask(X86_FEATURE_XSAVES));
 
     c->x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)] = eax;
 
     BUG_ON(eax != boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)]);
+
+    if ( setup_xstate_features(bsp) && bsp )
+        BUG();

Well, not what I had hoped for, but okay.

Jan

author
#3 lore raw

Re: [V9 1/3] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 18 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 03.11.15 at 07:27, <[email protected]> wrote:
xen/arch/x86/domain.c +0 -0
index fe3be30..108d4f8 100644

xen/arch/x86/domain.c int arch_set_info_guest( · line 891

Quoted 9 lines
xen/arch/x86/domain.c +5 -0
@@ -883,7 +883,12 @@ int arch_set_info_guest(
     {
         memcpy(v->arch.fpu_ctxt, &c.nat->fpu_ctxt, sizeof(c.nat->fpu_ctxt));
         if ( v->arch.xsave_area )
+        {
              v->arch.xsave_area->xsave_hdr.xstate_bv = XSTATE_FP_SSE;
+             if ( cpu_has_xsaves || cpu_has_xsavec )
+                  v->arch.xsave_area->xsave_hdr.xcomp_bv = XSTATE_FP_SSE |
+                                                           XSTATE_COMPACTION_ENABLED;
+        }

So here you nicely extend the existing conditional.

Quoted 12 lines

xen/arch/x86/domain.c static void __context_switch(void) · line 1577

Quoted 6 lines
xen/arch/x86/domain.c +2 -0
@@ -1568,6 +1573,8 @@ static void __context_switch(void)
             if ( xcr0 != get_xcr0() && !set_xcr0(xcr0) )
                 BUG();
         }
+        if ( cpu_has_xsaves && has_hvm_container_vcpu(n) )
+            set_msr_xss(n->arch.hvm_vcpu.msr_xss);

Why not also here (the previous if() uses cpu_has_xsave, which you surely depend on)? Agreed the difference is minor for modern CPUs, but I wanted to ask anyway. I.e. an explanation will do, no need to re-submit just because of this.

Yes. It is better to put the cpu_has_xsaves into the previous if().

Quoted 65 lines
Quoted 65 lines
xen/arch/x86/domain.c +24 -28
@@ -158,6 +334,20 @@ void xsave(struct vcpu *v, uint64_t mask)
         ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET] = word_size;
 }
+#define XSTATE_FIXUP ".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 xrstor(struct vcpu *v, uint64_t mask)
 {
     uint32_t hmask = mask >> 32;
@@ -187,39 +377,22 @@ 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" );
+        alternative_input("1: "".byte 0x48,0x0f,0xae,0x2f",
+                          ".byte 0x48,0x0f,0xc7,0x1f",
+                          X86_FEATURE_XSAVES,
+                          "D" (ptr), "m" (*ptr), "a" (lmask), "d" (hmask));
+        asm volatile (XSTATE_FIXUP);
         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" );
+        alternative_input("1: "".byte 0x0f,0xae,0x2f",
+                          ".byte 0x0f,0xc7,0x1f",
+                          X86_FEATURE_XSAVES,
+                          "D" (ptr), "m" (*ptr), "a" (lmask), "d" (hmask));
+        asm volatile (XSTATE_FIXUP);
         break;
Quoted 9 lines

xen/arch/x86/domain.c void xrstor(struct vcpu *v, uint64_t mask) · line 395

Quoted 3 lines
xen/arch/x86/domain.c +1 -0
     }
 }
+#undef XSTATE_FIXUP

Repeating my comment on v8: "I wonder whether at least for the restore side alternative asm wouldn't result in better readable code and at the same time in a smaller patch." Did you at least look into that option?

I may misunderstand your meaning. I have adressed the comment by changing the restor side using alternative_input. Does "alternative_input" not what you want ? if it is not what you want, please give me some suggestions how to address this ?
Thanks

Quoted 5 lines

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

A message that is not in the archive (sent off-list or not yet received): [email protected]@linux.intel.com

#5 lore raw

Re: [V9 1/3] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 79 lines
Quoted 1 line
Quoted 1 line · On 05.11.15 at 02:34, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 03.11.15 at 07:27, <[email protected]> wrote:
xen/arch/x86/xstate.c +14 -10
@@ -158,6 +334,20 @@ void xsave(struct vcpu *v, uint64_t mask)
         ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET] = word_size;
 }
+#define XSTATE_FIXUP ".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 xrstor(struct vcpu *v, uint64_t mask)
 {
     uint32_t hmask = mask >> 32;
@@ -187,39 +377,22 @@ 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)
Quoted 8 lines

xen/arch/x86/xstate.c void xrstor(struct vcpu *v, uint64_t mask)

Quoted 8 lines
xen/arch/x86/xstate.c +4 -4
-                       : "+&D" (ptr), "+&a" (lmask)
-                       : "m" (*ptr), "g" (lmask), "d" (hmask),
-                         "m" (xsave_cntxt_size)
-                       : "ecx" );
+        alternative_input("1: "".byte 0x48,0x0f,0xae,0x2f",
+                          ".byte 0x48,0x0f,0xc7,0x1f",
+                          X86_FEATURE_XSAVES,
+                          "D" (ptr), "m" (*ptr), "a" (lmask), "d" 

(hmask));

Quoted 13 lines
Quoted 13 lines
xen/arch/x86/xstate.c +1 -10
+        asm volatile (XSTATE_FIXUP);
         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)
Quoted 8 lines

xen/arch/x86/xstate.c void xrstor(struct vcpu *v, uint64_t mask)

Quoted 8 lines
xen/arch/x86/xstate.c +4 -4
-                       : "+&D" (ptr), "+&a" (lmask)
-                       : "m" (*ptr), "g" (lmask), "d" (hmask),
-                         "m" (xsave_cntxt_size)
-                       : "ecx" );
+        alternative_input("1: "".byte 0x0f,0xae,0x2f",
+                          ".byte 0x0f,0xc7,0x1f",
+                          X86_FEATURE_XSAVES,
+                          "D" (ptr), "m" (*ptr), "a" (lmask), "d" 

(hmask));

Quoted 11 lines

xen/arch/x86/xstate.c void xrstor(struct vcpu *v, uint64_t mask) · line 395

Quoted 5 lines
xen/arch/x86/xstate.c +2 -0
+        asm volatile (XSTATE_FIXUP);
         break;
     }
 }
+#undef XSTATE_FIXUP

Repeating my comment on v8: "I wonder whether at least for the restore side alternative asm wouldn't result in better readable code and at the same time in a smaller patch." Did you at least look into that option?

Quoted 5 lines

I may misunderstand your meaning. I have adressed the comment by changing the restor side using alternative_input. Does "alternative_input" not what you want ?
if it is not what you want, please give me some suggestions how to address this ?

Oh, I'm sorry, I should have looked more closely. The fact that XSTATE_FIXUP survived made me draw wrong conclusions without looking more closely. Now the bad news is - you can't split things like this, as the compiler doesn't make any guarantees as to register values between two asm()-s. The whole construct needs to and up as a single asm(), which is why XSTATE_FIXUP and is unlikely to be of much use here (at least in the context of this patch; a separate cleanup patch might eliminate the redundancy).

Jan

author
#6 lore raw

Re: [V9 1/3] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 41 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 05.11.15 at 02:34, <[email protected]> wrote:
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 03.11.15 at 07:27, <[email protected]> wrote:
xen/arch/x86/xstate.c +0 -10
@@ -158,6 +334,20 @@ void xsave(struct vcpu *v, uint64_t mask)
     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)
Quoted 8 lines

xen/arch/x86/xstate.c void xrstor(struct vcpu *v, uint64_t mask)

Quoted 8 lines
xen/arch/x86/xstate.c +4 -4
-                       : "+&D" (ptr), "+&a" (lmask)
-                       : "m" (*ptr), "g" (lmask), "d" (hmask),
-                         "m" (xsave_cntxt_size)
-                       : "ecx" );
+        alternative_input("1: "".byte 0x0f,0xae,0x2f",
+                          ".byte 0x0f,0xc7,0x1f",
+                          X86_FEATURE_XSAVES,
+                          "D" (ptr), "m" (*ptr), "a" (lmask), "d" 

(hmask));

Quoted 11 lines

xen/arch/x86/xstate.c void xrstor(struct vcpu *v, uint64_t mask) · line 395

Quoted 5 lines
xen/arch/x86/xstate.c +2 -0
+        asm volatile (XSTATE_FIXUP);
         break;
     }
 }
+#undef XSTATE_FIXUP

Repeating my comment on v8: "I wonder whether at least for the restore side alternative asm wouldn't result in better readable code and at the same time in a smaller patch." Did you at least look into that option?

Quoted 5 lines

I may misunderstand your meaning. I have adressed the comment by changing the restor side using alternative_input. Does "alternative_input" not what you want ?
if it is not what you want, please give me some suggestions how to address this ?

Quoted 9 lines

Oh, I'm sorry, I should have looked more closely. The fact that XSTATE_FIXUP survived made me draw wrong conclusions without looking more closely. Now the bad news is - you can't split things like this, as the compiler doesn't make any guarantees as to register values between two asm()-s. The whole construct needs to and up as a single asm(), which is why XSTATE_FIXUP and is unlikely to be of much use here (at least in the context of this patch; a separate cleanup patch might eliminate the redundancy).

Ok. So alternative_input will not used here (means use the way xrstor in Patch 8)? Or put the XSTATE_FIXUP into alternative_input ? Which one is ok to you ?

Thanks

Quoted 6 lines

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

A message that is not in the archive (sent off-list or not yet received): [email protected]@linux.intel.com

#8 lore raw

Re: [V9 1/3] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.11.15 at 10:57, <[email protected]> wrote:

Ok. So alternative_input will not used here (means use the way xrstor in Patch 8)? Or put the XSTATE_FIXUP into alternative_input ? Which one is ok to you ?

The latter, if necessary by extending alternative_input() accordingly (or provide a second, more flexible variant if need be; iirc Linux has gained a couple of variants over the years).

Jan

author
#9 lore raw

Re: [V9 1/3] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 9 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.11.15 at 10:57, <[email protected]> wrote:

Ok. So alternative_input will not used here (means use the way xrstor in Patch 8)? Or put the XSTATE_FIXUP into alternative_input ? Which one is ok to you ?

The latter, if necessary by extending alternative_input() accordingly (or provide a second, more flexible variant if need be; iirc Linux has gained a couple of variants over the years).

Ok,I will introduct 2 new macro to handle this.

1. #define ASM_OUTPUT2(a...) a
The first one(Porting from linux) is used when more than one output parameter is needed.

2. #define alternative_io_fixup(oldinstr, newinstr, feature, fixup, output, input...)		\
	asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) "\n"     \
	fixup   							\
	: output : "i"(0), ## input)

The second is to handle alternaive asm with fixup.

Then xrstor side will be:
alternative_io_fixup("1: "".byte 0x48,0x0f,0xae,0x2f",

                          ".byte 0x48,0x0f,0xc7,0x1f",
                          X86_FEATURE_XSAVES,
                          XSTATE_FIXUP,
                          ASM_OUTPUT2("+&D" (ptr), "+&a" (lmask)),
                          "m" (*ptr), "g" (lmask), "d" (hmask), 
			  "m" (xsave_cntxt_size) : "ecx");

Also, the position of the operand used in XSTATE_FIXUP will changed.
Any suggestions of this ?

Thanks

Quoted 3 lines

[email protected]
http://lists.xen.org/xen-devel

author
#10 lore raw

PATCH v9 2/3 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.

Add IA32_XSS_MSR save/rstore support.

---
 xen/arch/x86/hvm/hvm.c             | 27 +++++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmcs.c        |  5 ++++-
 xen/arch/x86/hvm/vmx/vmx.c         | 36 +++++++++++++++++++++++++++++++++++-
 xen/arch/x86/xstate.c              |  2 +-
 xen/include/asm-x86/hvm/vmx/vmcs.h |  4 ++++
 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 +27 -0
index dd86730..739ed33 100644
@@ -4604,6 +4604,20 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                     *ebx = _eax + _ebx;
             }
         }
+        if ( count == 1 )
+        {
+            if ( cpu_has_xsaves && cpu_has_vmx_xsaves )
+            {
+                *ebx = XSTATE_AREA_MIN_SIZE;
+                if ( v->arch.xcr0 | v->arch.hvm_vcpu.msr_xss )
+                    for ( sub_leaf = 2; sub_leaf < 63; sub_leaf++ )
+                        if ( (v->arch.xcr0 | v->arch.hvm_vcpu.msr_xss) &
+                             (1ULL << sub_leaf) )
+                            *ebx += xstate_sizes[sub_leaf];
+            }
+            else
+                *ebx = *ecx = *edx = 0;
+        }
         break;
 
     case 0x80000001:
@@ -4703,6 +4717,12 @@ 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:
+        if ( !cpu_has_xsaves )
+            goto gp_fault;
+        *msr_content = v->arch.hvm_vcpu.msr_xss;
+        break;
+
     case MSR_IA32_TSC:
         *msr_content = _hvm_rdtsc_intercept();
         break;
@@ -4835,6 +4855,13 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,
            return X86EMUL_EXCEPTION;
         break;
 
+    case MSR_IA32_XSS:
+        /* No XSS features currently supported for guests. */
+        if ( !cpu_has_xsaves || msr_content != 0 )
+            goto gp_fault;
+        v->arch.hvm_vcpu.msr_xss = msr_content;
+        break;
+
     case MSR_IA32_TSC:
         hvm_set_guest_tsc(v, msr_content);
         break;
xen/arch/x86/hvm/vmx/vmcs.c +4 -1
index 4ea1ad1..5404101 100644
@@ -240,7 +240,8 @@ static int vmx_init_vmcs_config(void)
                SECONDARY_EXEC_PAUSE_LOOP_EXITING |
                SECONDARY_EXEC_ENABLE_INVPCID |
                SECONDARY_EXEC_ENABLE_VM_FUNCTIONS |
-               SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS);
+               SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS |
+               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;
@@ -1249,6 +1250,8 @@ static int construct_vmcs(struct vcpu *v)
         __vmwrite(HOST_PAT, host_pat);
         __vmwrite(GUEST_PAT, guest_pat);
     }
+    if ( cpu_has_vmx_xsaves )
+        __vmwrite(XSS_EXIT_BITMAP, 0);
 
     vmx_vmcs_exit(v);
 
xen/arch/x86/hvm/vmx/vmx.c +35 -1
index bbec0e8..8cdad0f 100644
@@ -625,7 +625,7 @@ static int vmx_load_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 
 static unsigned int __init vmx_init_msr(void)
 {
-    return !!cpu_has_mpx;
+    return !!cpu_has_mpx + !!cpu_has_xsaves;
 }
 
 static void vmx_save_msr(struct vcpu *v, struct hvm_msr *ctxt)
@@ -640,6 +640,14 @@ static void vmx_save_msr(struct vcpu *v, struct hvm_msr *ctxt)
     }
 
     vmx_vmcs_exit(v);
+
+    if ( cpu_has_xsaves )
+    {
+        ctxt->msr[ctxt->count].val = v->arch.hvm_vcpu.msr_xss;
+        if ( ctxt->msr[ctxt->count].val )
+            ctxt->msr[ctxt->count++].index = MSR_IA32_XSS;
+    }
+
 }
 
 static int vmx_load_msr(struct vcpu *v, struct hvm_msr *ctxt)
@@ -659,6 +667,12 @@ static int vmx_load_msr(struct vcpu *v, struct hvm_msr *ctxt)
             else
                 err = -ENXIO;
             break;
+        case MSR_IA32_XSS:
+            if ( cpu_has_xsaves )
+                v->arch.hvm_vcpu.msr_xss = ctxt->msr[i].val;
+            else
+                err = -ENXIO;
+            break;
         default:
             continue;
         }
@@ -2852,6 +2866,18 @@ static void vmx_idtv_reinject(unsigned long idtv_info)
     }
 }
 
+static void vmx_handle_xsaves(void)
+{
+    gdprintk(XENLOG_ERR, "xsaves should not cause vmexit\n");
+    domain_crash(current->domain);
+}
+
+static void vmx_handle_xrstors(void)
+{
+    gdprintk(XENLOG_ERR, "xrstors should not cause vmexit\n");
+    domain_crash(current->domain);
+}
+
 static int vmx_handle_apic_write(void)
 {
     unsigned long exit_qualification;
@@ -3423,6 +3449,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 +1 -1
index ada40ff..babd87d 100644
@@ -25,7 +25,7 @@ static u32 __read_mostly xsave_cntxt_size;
 u64 __read_mostly xfeature_mask;
 
 static unsigned int * __read_mostly xstate_offsets;
-static unsigned int * __read_mostly xstate_sizes;
+unsigned int * __read_mostly xstate_sizes;
 static unsigned int __read_mostly xstate_features;
 static unsigned int __read_mostly xstate_comp_offsets[sizeof(xfeature_mask)*8];
 
xen/include/asm-x86/hvm/vmx/vmcs.h +4 -0
index 865d9fc..1a5d827 100644
@@ -225,6 +225,7 @@ extern u32 vmx_vmentry_control;
 #define SECONDARY_EXEC_ENABLE_VMCS_SHADOWING    0x00004000
 #define SECONDARY_EXEC_ENABLE_PML               0x00020000
 #define SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS   0x00040000
+#define SECONDARY_EXEC_XSAVES                   0x00100000
 extern u32 vmx_secondary_exec_control;
 
 #define VMX_EPT_EXEC_ONLY_SUPPORTED                         0x00000001
@@ -290,6 +291,8 @@ extern u64 vmx_ept_vpid_cap;
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS)
 #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
 
@@ -364,6 +367,7 @@ enum vmcs_field {
     VMREAD_BITMAP                   = 0x00002026,
     VMWRITE_BITMAP                  = 0x00002028,
     VIRT_EXCEPTION_INFO             = 0x0000202a,
+    XSS_EXIT_BITMAP                 = 0x0000202c,
     GUEST_PHYSICAL_ADDRESS          = 0x00002400,
     VMCS_LINK_POINTER               = 0x00002800,
     GUEST_IA32_DEBUGCTL             = 0x00002802,
xen/include/asm-x86/hvm/vmx/vmx.h +2 -0
index e750a76..fdaacbb 100644
@@ -188,6 +188,8 @@ static inline unsigned long pi_get_pir(struct pi_desc *pi_desc, int group)
 #define EXIT_REASON_INVPCID             58
 #define EXIT_REASON_VMFUNC              59
 #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 414cc99..12d939b 100644
@@ -45,6 +45,7 @@
 #define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
 
 extern u64 xfeature_mask;
+extern unsigned int *xstate_sizes;
 
 /* extended state save area */
 struct __packed __attribute__((aligned (64))) xsave_struct
Signature · 1 line
1.9.1
Reviewed-by on v9 2/3
#11 lore raw

Re: [V9 2/3] x86/xsaves: enable xsaves/xrstors for hvm guest

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 03.11.15 at 07:27, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -640,6 +640,14 @@ static void vmx_save_msr(struct vcpu *v, struct hvm_msr *ctxt)
     }
 

xen/arch/x86/hvm/vmx/vmx.c static void vmx_save_msr(struct vcpu *v, struct hvm_msr *ctxt) · line 651

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
     vmx_vmcs_exit(v);
+
+    if ( cpu_has_xsaves )
+    {
+        ctxt->msr[ctxt->count].val = v->arch.hvm_vcpu.msr_xss;
+        if ( ctxt->msr[ctxt->count].val )
+            ctxt->msr[ctxt->count++].index = MSR_IA32_XSS;
+    }
+
 }

Stray blank line (not the first time I have to make this comment on this series).

With it removed,

author
#12 lore raw

Re: [V9 2/3] x86/xsaves: enable xsaves/xrstors for hvm guest

Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 03.11.15 at 07:27, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -640,6 +640,14 @@ static void vmx_save_msr(struct vcpu *v, struct hvm_msr *ctxt)
     }
 
Quoted 13 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_save_msr(struct vcpu *v, struct hvm_msr *ctxt) · line 651

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
     vmx_vmcs_exit(v);
+
+    if ( cpu_has_xsaves )
+    {
+        ctxt->msr[ctxt->count].val = v->arch.hvm_vcpu.msr_xss;
+        if ( ctxt->msr[ctxt->count].val )
+            ctxt->msr[ctxt->count++].index = MSR_IA32_XSS;
+    }
+
 }

Stray blank line (not the first time I have to make this comment on this series).

Sorry for that.

Quoted 4 lines

With it removed,

Thanks.

Quoted 6 lines

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

author
#13 lore raw

PATCH v9 3/3 libxc: expose xsaves/xgetbv1/xsavec to hvm guest

In reply to Shuai Ruan

From: Shuai Ruan <[email protected]>

This patch exposes xsaves/xgetbv1/xsavec to hvm guest. The reserved bits of eax/ebx/ecx/edx must be cleaned up when call cpuid(0dh) with leaf 1 or 2..63.

According to the spec the following bits must be reserved:
For leaf 1, bits 03-04/08-31 of ecx is reserved. Edx is reserved. For leaf 2...63, bits 01-31 of ecx is reserved, Edx is reserved.

But as no XSS festures are currently supported, even in HVM guests, for leaf 2...63, ecx should be zero at the moment.

---
 tools/libxc/xc_cpuid_x86.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/libxc/xc_cpuid_x86.c +8 -2
index 031c848..8882c01 100644
@@ -282,6 +282,9 @@ static void intel_xc_cpuid_policy(xc_interface *xch,
 }
 
 #define XSAVEOPT        (1 << 0)
+#define XSAVEC          (1 << 1)
+#define XGETBV1         (1 << 2)
+#define XSAVES          (1 << 3)
 /* Configure extended state enumeration leaves (0x0000000D for xsave) */
 static void xc_cpuid_config_xsave(xc_interface *xch,
                                   const struct cpuid_domain_info *info,
@@ -318,8 +321,11 @@ static void xc_cpuid_config_xsave(xc_interface *xch,
         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 | XGETBV1 | XSAVES);
+        if ( !info->hvm )
+            regs[0] &= ~XSAVES;
+        regs[2] &= info->xfeature_mask;
+        regs[3] = 0;
         break;
     case 2 ... 63: /* sub-leaves */
         if ( !(info->xfeature_mask & (1ULL << input[1])) )
Signature · 1 line
1.9.1