Skip to contentSkip to conversation

add xsaves/xrstors support

PATCH Superseded Shuai Ruan 3 participants 13 messages Oct 12, 2015 – Oct 15, 2015 1 message not in the archive

v6 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Shuai Ruan

author
#0 lore raw

v6 cover add xsaves/xrstors support

From: Shuai Ruan <[email protected]>

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].

patch1: add basic definition/function to support xsaves
patch2: add xsaves/xrstors support for xen
patch3-4: add xsaves/xrstors support for hvm guest

[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 (4):
x86/xsaves: add basic definitions/helpers to support xsaves 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         |  16 ++-
 xen/arch/x86/domain.c              |   2 +
 xen/arch/x86/domctl.c              |  35 +++++-
 xen/arch/x86/hvm/hvm.c             |  46 ++++++-
 xen/arch/x86/hvm/vmx/vmcs.c        |   5 +-
 xen/arch/x86/hvm/vmx/vmx.c         |  20 ++++
 xen/arch/x86/i387.c                |   4 +
 xen/arch/x86/traps.c               |   6 +-
 xen/arch/x86/xstate.c              | 237 ++++++++++++++++++++++++++++++++-----
 xen/include/asm-x86/hvm/vcpu.h     |   1 +
 xen/include/asm-x86/hvm/vmx/vmcs.h |   4 +
 xen/include/asm-x86/hvm/vmx/vmx.h  |   2 +
 xen/include/asm-x86/msr-index.h    |   2 +
 xen/include/asm-x86/xstate.h       |  27 ++++-
 14 files changed, 356 insertions(+), 51 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v6 1/4 x86/xsaves: add basic definitions/helpers to support xsaves

This patch add basic definitions/helpers which will be used in later patches.

---
 xen/arch/x86/xstate.c           | 16 ++++++++++++++++
 xen/include/asm-x86/hvm/vcpu.h  |  1 +
 xen/include/asm-x86/msr-index.h |  2 ++
 xen/include/asm-x86/xstate.h    | 25 ++++++++++++++++++++++++-
 4 files changed, 43 insertions(+), 1 deletion(-)
xen/arch/x86/xstate.c +16 -0
index 9ddff90..730368a 100644
@@ -23,6 +23,11 @@ static u32 __read_mostly xsave_cntxt_size;
 
 /* A 64-bit bitmask of the XSAVE/XRSTOR features supported by processor. */
 u64 __read_mostly xfeature_mask;
+unsigned int * __read_mostly xstate_offsets;
+unsigned int * __read_mostly xstate_sizes;
+
+/* Cached xss for fast read */
+static DEFINE_PER_CPU(uint64_t, xss);
 
 /* Cached xcr0 for fast read */
 static DEFINE_PER_CPU(uint64_t, xcr0);
@@ -60,6 +65,17 @@ uint64_t get_xcr0(void)
     return this_cpu(xcr0);
 }
 
+void set_msr_xss(u64 xss)
+{
+    wrmsrl(MSR_IA32_XSS, xss);
+    this_cpu(xss) = xss;
+}
+
+uint64_t get_msr_xss(void)
+{
+    return this_cpu(xss);
+}
+
 void xsave(struct vcpu *v, uint64_t mask)
 {
     struct xsave_struct *ptr = v->arch.xsave_area;
xen/include/asm-x86/hvm/vcpu.h +1 -0
index f553814..de81f8a 100644
@@ -173,6 +173,7 @@ struct hvm_vcpu {
 
     u32                 msr_tsc_aux;
     u64                 msr_tsc_adjust;
+    u64                 msr_xss;
 
     union {
         struct arch_vmx_struct vmx;
xen/include/asm-x86/msr-index.h +2 -0
index e9c4723..4e5b31f 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 +24 -1
index f0d8f0b..f23435a 100644
@@ -19,7 +19,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)
@@ -38,8 +42,24 @@
 #define XSTATE_ALL     (~(1ULL << 63))
 #define XSTATE_NONLAZY (XSTATE_LWP | XSTATE_BNDREGS | XSTATE_BNDCSR)
 #define XSTATE_LAZY    (XSTATE_ALL & ~XSTATE_NONLAZY)
+#define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
+
+#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"
 
 extern u64 xfeature_mask;
+extern unsigned int *xstate_offsets, *xstate_sizes;
 
 /* extended state save area */
 struct __packed __attribute__((aligned (64))) xsave_struct
@@ -68,7 +88,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 */
@@ -78,6 +99,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 set_msr_xss(u64 xss);
+uint64_t get_msr_xss(void);
 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
#2 lore raw

Re: [V6 1/4] x86/xsaves: add basic definitions/helpers to support xsaves

Quoted 28 lines · Shuai Ruan

This patch add basic definitions/helpers which will be used in later patches.

---
 xen/arch/x86/xstate.c           | 16 ++++++++++++++++
 xen/include/asm-x86/hvm/vcpu.h  |  1 +
 xen/include/asm-x86/msr-index.h |  2 ++
 xen/include/asm-x86/xstate.h    | 25 ++++++++++++++++++++++++-
 4 files changed, 43 insertions(+), 1 deletion(-)
xen/arch/x86/xstate.c +5 -0
index 9ddff90..730368a 100644
@@ -23,6 +23,11 @@ static u32 __read_mostly xsave_cntxt_size;
 
 /* A 64-bit bitmask of the XSAVE/XRSTOR features supported by processor. */
 u64 __read_mostly xfeature_mask;
+unsigned int * __read_mostly xstate_offsets;
+unsigned int * __read_mostly xstate_sizes;
+
+/* Cached xss for fast read */
+static DEFINE_PER_CPU(uint64_t, xss);
 
 /* Cached xcr0 for fast read */
 static DEFINE_PER_CPU(uint64_t, xcr0);

xen/arch/x86/xstate.c uint64_t get_xcr0(void) · line 71

Quoted 7 lines
xen/arch/x86/xstate.c +4 -0
@@ -60,6 +65,17 @@ uint64_t get_xcr0(void)
     return this_cpu(xcr0);
 }
 
+void set_msr_xss(u64 xss)
+{
+    wrmsrl(MSR_IA32_XSS, xss);
+    this_cpu(xss) = xss;

This can become faster by doing a lazy write.

u64 * this_xss = &this_cpu(xss);

if ( *this_xss != xss )

{
    wrmsrl(MSR_IA32_XSS, xss);
    *this_xss = xss;
}

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#3 lore raw

Re: [V6 1/4] x86/xsaves: add basic definitions/helpers to support xsaves

In reply to Shuai Ruan

xen/arch/x86/xstate.c static u32 __read_mostly xsave_cntxt_size; · line 27

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 12.10.15 at 08:07, <[email protected]> wrote:
xen/arch/x86/xstate.c +2 -0
@@ -23,6 +23,11 @@ static u32 __read_mostly xsave_cntxt_size;
 
 /* A 64-bit bitmask of the XSAVE/XRSTOR features supported by processor. */
 u64 __read_mostly xfeature_mask;
+unsigned int * __read_mostly xstate_offsets;
+unsigned int * __read_mostly xstate_sizes;

You appear to need the latter outside of this file in patch 3, but the former could (and hence should) be static afaict.

xen/include/asm-x86/xstate.h line 27

Quoted 12 lines
xen/include/asm-x86/xstate.h +4 -0
@@ -19,7 +19,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 */

This 512 should now get replaced too.

Quoted 9 lines
xen/include/asm-x86/xstate.h +5 -0
@@ -38,8 +42,24 @@
 #define XSTATE_ALL     (~(1ULL << 63))
 #define XSTATE_NONLAZY (XSTATE_LWP | XSTATE_BNDREGS | XSTATE_BNDCSR)
 #define XSTATE_LAZY    (XSTATE_ALL & ~XSTATE_NONLAZY)
+#define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
+
+#define XSTATE_FIXUP ".section .fixup,\"ax\"      \n"        \
+                     "2: mov %5,%%ecx             \n"        \
+                     "   xor %1,%1                \n"        \

xen/include/asm-x86/xstate.h line 59

Quoted 10 lines
xen/include/asm-x86/xstate.h +10 -0
+                     "   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"

First of all I don't think this belongs here - neither in this patch, nor in this header. It's not being used by this patch, and the connection between variable names used here and in patch 2 is completely lost. If you put this into xrstor() as a local #define, then this may be okay. In any other case the user of the macro has to have a way to control the variable names used.

Jan

author
#4 lore raw

PATCH v6 2/4 x86/xsaves: enable xsaves/xrstors/xsavec in xen

In reply to Shuai Ruan

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        |   2 +
 xen/arch/x86/domctl.c        |  35 ++++++-
 xen/arch/x86/hvm/hvm.c       |  19 +++-
 xen/arch/x86/i387.c          |   4 +
 xen/arch/x86/traps.c         |   6 +-
 xen/arch/x86/xstate.c        | 221 ++++++++++++++++++++++++++++++++++++-------
 xen/include/asm-x86/xstate.h |   2 +
 7 files changed, 244 insertions(+), 45 deletions(-)
xen/arch/x86/domain.c +2 -0
index 7ca9b93..7b3d9f4 100644
@@ -1569,6 +1569,8 @@ static void __context_switch(void)
             if ( xcr0 != get_xcr0() && !set_xcr0(xcr0) )
                 BUG();
         }
+        if ( cpu_has_xsaves && is_hvm_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 +30 -5
index f8a559c..9517741 100644
@@ -897,9 +897,30 @@ 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,
+                                    evc->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 +976,12 @@ 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));
+                if ( (cpu_has_xsaves || cpu_has_xsavec) )
+                    compress_xsave_states(v, _xsave_area,
+                                          evc->size - 2 * sizeof(uint64_t));
+                else
+                    memcpy(v->arch.xsave_area, (void *)_xsave_area,
+                           evc->size - 2 * sizeof(uint64_t));
                 vcpu_unpause(v);
             }
             else
xen/arch/x86/hvm/hvm.c +14 -5
index 3fa2280..177cf5f 100644
@@ -2157,8 +2157,12 @@ 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 || cpu_has_xsavec) )
+            expand_xsave_states(v, &ctxt->save_area,
+                                size - offsetof(typeof(*ctxt), save_area));
+        else
+            memcpy(&ctxt->save_area, v->arch.xsave_area,
+                   size - offsetof(struct hvm_hw_cpu_xsave, save_area));
     }
 
     return 0;
@@ -2257,9 +2261,14 @@ 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 || cpu_has_xsavec) )
+        compress_xsave_states(v, &ctxt->save_area,
+                              min(desc->length, size) -
+                              offsetof(struct hvm_hw_cpu_xsave,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/i387.c +4 -0
index 14f2a79..736197f 100644
@@ -309,7 +309,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 07feb6d..316fc7f 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[X86_FEATURE_XSAVEOPT / 32];
-            if ( !cpu_has_xsaves )
-                b = c = d = 0;
+            a &= (boot_cpu_data.x86_capability[X86_FEATURE_XSAVEOPT / 32] &
+                 ~cpufeat_mask(X86_FEATURE_XSAVES));
+            b = c = d = 0;
         }
         break;
 
xen/arch/x86/xstate.c +189 -32

262 lines not on this page · raw message

xen/include/asm-x86/xstate.h +2 -0
index f23435a..1c4c5fd 100644
@@ -106,6 +106,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
#5 lore raw

Re: [V6 2/4] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 25 lines · Shuai Ruan

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        |   2 +
 xen/arch/x86/domctl.c        |  35 ++++++-
 xen/arch/x86/hvm/hvm.c       |  19 +++-
 xen/arch/x86/i387.c          |   4 +
 xen/arch/x86/traps.c         |   6 +-
 xen/arch/x86/xstate.c        | 221 ++++++++++++++++++++++++++++++++++++-------
 xen/include/asm-x86/xstate.h |   2 +
 7 files changed, 244 insertions(+), 45 deletions(-)
xen/arch/x86/domain.c +0 -0
index 7ca9b93..7b3d9f4 100644

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

Quoted 4 lines
xen/arch/x86/domain.c +1 -0
@@ -1569,6 +1569,8 @@ static void __context_switch(void)
             if ( xcr0 != get_xcr0() && !set_xcr0(xcr0) )
                 BUG();
         }
+        if ( cpu_has_xsaves && is_hvm_vcpu(n) )

Because of the current implementation of PVH mode, the correct predicate here is has_hvm_container(n)

Quoted 43 lines
xen/arch/x86/domain.c +1 -0
+            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 +24 -3
index f8a559c..9517741 100644
@@ -897,9 +897,30 @@ 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,
+                                    evc->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);

xen/arch/x86/domctl.c long arch_do_domctl( · line 979

Quoted 6 lines
xen/arch/x86/domctl.c +1 -2
@@ -955,8 +976,12 @@ 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));
+                if ( (cpu_has_xsaves || cpu_has_xsavec) )

You can drop the inner set of brackets.

Quoted 13 lines
xen/arch/x86/domctl.c +5 -0
+                    compress_xsave_states(v, _xsave_area,
+                                          evc->size - 2 * sizeof(uint64_t));
+                else
+                    memcpy(v->arch.xsave_area, (void *)_xsave_area,
+                           evc->size - 2 * sizeof(uint64_t));
                 vcpu_unpause(v);
             }
             else
xen/arch/x86/hvm/hvm.c +0 -0
index 3fa2280..177cf5f 100644

xen/arch/x86/hvm/hvm.c static int hvm_save_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2160

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +1 -2
@@ -2157,8 +2157,12 @@ 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 || cpu_has_xsavec) )

And here

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +5 -0
+            expand_xsave_states(v, &ctxt->save_area,
+                                size - offsetof(typeof(*ctxt), save_area));
+        else
+            memcpy(&ctxt->save_area, v->arch.xsave_area,
+                   size - offsetof(struct hvm_hw_cpu_xsave, save_area));
     }
 
     return 0;

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2264

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -3
@@ -2257,9 +2261,14 @@ 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 || cpu_has_xsavec) )

And here.

With these 4 things fixed, Reviewed-by: Andrew Cooper <[email protected]>

author
#6 lore raw

Re: [V6 2/4] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Shuai Ruan

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.

Quoted 8 lines
Quoted 4 lines

Also, pv guest will not support xsaves/xrstors.

---

With these 4 things fixed, Reviewed-by: Andrew Cooper <[email protected]>

Ok.
Thanks.

Quoted 5 lines

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

#7 lore raw

Re: [V6 2/4] x86/xsaves: enable xsaves/xrstors/xsavec in xen

In reply to Shuai Ruan
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 12.10.15 at 08:07, <[email protected]> wrote:
xen/arch/x86/domctl.c +8 -3
@@ -897,9 +897,30 @@ 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 )
+                {

xen/arch/x86/domctl.c long arch_do_domctl( · line 917

Quoted 10 lines
xen/arch/x86/domctl.c +10 -0
+                    ret = -ENOMEM;
+                    vcpu_unpause(v);
+                    goto vcpuextstate_out;
+                }
+
+                expand_xsave_states(v, xsave_area,
+                                    evc->size - 2 * sizeof(uint64_t));
+
+                if ( copy_to_guest_offset(evc->buffer, offset, xsave_area,
+                                          size - 2 * sizeof(uint64_t)) )

Here you use size, which is fine, but why do you use evc->size three lines up from here?

xen/arch/x86/domctl.c long arch_do_domctl( · line 923

Quoted 6 lines
xen/arch/x86/domctl.c +6 -0
+                     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)) )

I also think code readability would benefit from folding the two copy_to_guest_offset()s, which differ in only the pointer used.

xen/arch/x86/domctl.c long arch_do_domctl( · line 983

Quoted 11 lines
xen/arch/x86/domctl.c +5 -2
@@ -955,8 +976,12 @@ 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));
+                if ( (cpu_has_xsaves || cpu_has_xsavec) )
+                    compress_xsave_states(v, _xsave_area,
+                                          evc->size - 2 * sizeof(uint64_t));
+                else
+                    memcpy(v->arch.xsave_area, (void *)_xsave_area,

Stray cast got added here.

Quoted 5 lines
xen/arch/x86/domctl.c +0 -1
@@ -2257,9 +2261,14 @@ 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,

xen/arch/x86/domctl.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2271

Quoted 10 lines
xen/arch/x86/domctl.c +8 -2
-           min(desc->length, size) - offsetof(struct hvm_hw_cpu_xsave,
-           save_area));
+    if ( (cpu_has_xsaves || cpu_has_xsavec) )
+        compress_xsave_states(v, &ctxt->save_area,
+                              min(desc->length, size) -
+                              offsetof(struct hvm_hw_cpu_xsave,save_area));
+    else
+        memcpy(v->arch.xsave_area, &ctxt->save_area,
+               min(desc->length, size) - offsetof(struct hvm_hw_cpu_xsave,
+               save_area));

Each time I look at these two hunks I wonder whether the condition and memcpy() wouldn't better move into the new called functions.

xen/arch/x86/domctl.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)

Quoted 4 lines
xen/arch/x86/domctl.c +4 -0
+void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)
+{
+    const struct xsave_struct *xsave = v->arch.xsave_area;
+    struct xsave_struct *dest_xsave = (struct xsave_struct *)dest;

Pointless cast.

xen/arch/x86/domctl.c

Quoted 4 lines
xen/arch/x86/domctl.c +4 -0
+void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size)
+{
+    struct xsave_struct *xsave = v->arch.xsave_area;
+    struct xsave_struct *src_xsave = (struct xsave_struct *)src;

Here the cast is even bogus, as it removes const-ness.

xen/arch/x86/domctl.c

Quoted 4 lines
xen/arch/x86/domctl.c +4 -0
+    u64 xstate_bv = src_xsave->xsave_hdr.xstate_bv;
+    u64 valid;
+
+    ASSERT(!xsave_area_compressed((struct xsave_struct *)src));

Pointless cast (to same type).

Jan

author
#8 lore raw

Re: [V6 2/4] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 12.10.15 at 08:07, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +3 -3
@@ -2257,9 +2261,14 @@ 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 || cpu_has_xsavec) )
+        compress_xsave_states(v, &ctxt->save_area,
+                              min(desc->length, size) -
Quoted 9 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h) · line 2271

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5 -0
+                              offsetof(struct hvm_hw_cpu_xsave,save_area));
+    else
+        memcpy(v->arch.xsave_area, &ctxt->save_area,
+               min(desc->length, size) - offsetof(struct hvm_hw_cpu_xsave,
+               save_area));

Each time I look at these two hunks I wonder whether the condition and memcpy() wouldn't better move into the new called functions.

Will func name
void load_xsave_area(struct vcpu *v, struct hvm_hw_cpu_xsave *ctxt) void save_xsave_area(struct vcpu *v, struct hvm_hw_cpu_xsave *ctxt) Ok ?

Quoted 7 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

#10 lore raw

Re: [V6 2/4] x86/xsaves: enable xsaves/xrstors/xsavec in xen

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 15.10.15 at 03:58, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich

Review static int hvm_load_cpu_xsave_states(struct domain

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 12.10.15 at 08:07, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -2257,9 +2261,14 @@ static int hvm_load_cpu_xsave_states(struct domain 

*d, hvm_domain_context_t *h)

Quoted 10 lines

xen/arch/x86/hvm/hvm.c static int hvm_load_cpu_xsave_states(struct domain *d, hvm_domain_context_t *h)

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +4 -3
     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 || cpu_has_xsavec) )
+        compress_xsave_states(v, &ctxt->save_area,
+                              min(desc->length, size) -
+                              offsetof(struct 

hvm_hw_cpu_xsave,save_area));

Quoted 5 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +4 -0
+    else
+        memcpy(v->arch.xsave_area, &ctxt->save_area,
+               min(desc->length, size) - offsetof(struct hvm_hw_cpu_xsave,
+               save_area));
Quoted 7 lines
Quoted 3 lines

Each time I look at these two hunks I wonder whether the condition and memcpy() wouldn't better move into the new called functions.

Will func name
void load_xsave_area(struct vcpu *v, struct hvm_hw_cpu_xsave *ctxt) void save_xsave_area(struct vcpu *v, struct hvm_hw_cpu_xsave *ctxt) Ok ?

Yes, or just keep the current names. (As a side note - one of the two clearly wants its second argument constified.)

Jan

author
#11 lore raw

PATCH v6 3/4 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             | 27 +++++++++++++++++++++++++++
 xen/arch/x86/hvm/vmx/vmcs.c        |  5 ++++-
 xen/arch/x86/hvm/vmx/vmx.c         | 20 ++++++++++++++++++++
 xen/include/asm-x86/hvm/vmx/vmcs.h |  4 ++++
 xen/include/asm-x86/hvm/vmx/vmx.h  |  2 ++
 5 files changed, 57 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/hvm.c +27 -0
index 177cf5f..a7c45be 100644
@@ -4561,6 +4561,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:
@@ -4660,6 +4674,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;
@@ -4792,6 +4812,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 3592a88..7185d55 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 +20 -0
index bbec0e8..5d723e8 100644
@@ -2852,6 +2852,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 +3435,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/include/asm-x86/hvm/vmx/vmcs.h +4 -0
index f1126d4..79c2c58 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
@@ -291,6 +292,8 @@ extern u32 vmx_secondary_exec_control;
     (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
 
@@ -365,6 +368,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 2ed62f9..cb66925 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
Signature · 1 line
1.9.1
author
#12 lore raw

PATCH v6 4/4 libxc: expose xsaves/xgetbv1/xsavec to hvm guest

In reply to Shuai Ruan

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.

---
 tools/libxc/xc_cpuid_x86.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/libxc/xc_cpuid_x86.c +12 -4
index e146a3e..b0d6ecc 100644
@@ -210,6 +210,10 @@ static void intel_xc_cpuid_policy(
 }
 
 #define XSAVEOPT        (1 << 0)
+#define XSAVEC          (1 << 1)
+#define XGETBV1         (1 << 2)
+#define XSAVES          (1 << 3)
+#define XSS_SUPPORT     (1 << 0)
 /* Configure extended state enumeration leaves (0x0000000D for xsave) */
 static void xc_cpuid_config_xsave(
     xc_interface *xch, domid_t domid, uint64_t xfeature_mask,
@@ -246,8 +250,9 @@ 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 | XGETBV1 | XSAVES);
+        regs[2] &= xfeature_mask;
+        regs[3] = 0;
         break;
     case 2 ... 63: /* sub-leaves */
         if ( !(xfeature_mask & (1ULL << input[1])) )
@@ -255,8 +260,11 @@ 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;
+        /* Don't touch EAX, EBX and cleanup EDX.
+         * Bit 0 of ECX represent whether sub-leave is supported in IA32_XSS
+         * msr or supported in XCR0.*/
+        regs[2] &= XSS_SUPPORT;
+        regs[3] = 0;
         break;
     }
 }
Signature · 1 line
1.9.1
#13 lore raw

Re: [V6 4/4] libxc: expose xsaves/xgetbv1/xsavec to hvm guest

Quoted 10 lines · Shuai Ruan

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.

This patch needs rebasing over c/s bf87f3a "tools/libxc: Improve efficiency of xc_cpuid_apply_policy()"

Quoted 20 lines
---
 tools/libxc/xc_cpuid_x86.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/libxc/xc_cpuid_x86.c +4 -0
index e146a3e..b0d6ecc 100644
@@ -210,6 +210,10 @@ static void intel_xc_cpuid_policy(
 }
 
 #define XSAVEOPT        (1 << 0)
+#define XSAVEC          (1 << 1)
+#define XGETBV1         (1 << 2)
+#define XSAVES          (1 << 3)
+#define XSS_SUPPORT     (1 << 0)
 /* Configure extended state enumeration leaves (0x0000000D for xsave) */
 static void xc_cpuid_config_xsave(
     xc_interface *xch, domid_t domid, uint64_t xfeature_mask,

tools/libxc/xc_cpuid_x86.c static void xc_cpuid_config_xsave( · line 253

Quoted 6 lines
tools/libxc/xc_cpuid_x86.c +1 -2
@@ -246,8 +250,9 @@ 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 | XGETBV1 | XSAVES);

The correct mask here depends on !info->hvm, to hide XSAVES from PV guests.

Quoted 6 lines
tools/libxc/xc_cpuid_x86.c +2 -0
+        regs[2] &= xfeature_mask;
+        regs[3] = 0;
         break;
     case 2 ... 63: /* sub-leaves */
         if ( !(xfeature_mask & (1ULL << input[1])) )

tools/libxc/xc_cpuid_x86.c static void xc_cpuid_config_xsave( · line 266

Quoted 9 lines
tools/libxc/xc_cpuid_x86.c +4 -2
@@ -255,8 +260,11 @@ 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;
+        /* Don't touch EAX, EBX and cleanup EDX.
+         * Bit 0 of ECX represent whether sub-leave is supported in IA32_XSS
+         * msr or supported in XCR0.*/
+        regs[2] &= XSS_SUPPORT;

No XSS features are currently supported, even in HVM guests. This should be unilaterally zero at the moment.

~Andrew

Quoted 4 lines
tools/libxc/xc_cpuid_x86.c +1 -0
+        regs[3] = 0;
         break;
     }
 }