Skip to contentSkip to conversation

x86/xsaves: ebx may return wrong value using CPUID eax=0xdh, ecx =1

PATCH In review Shuai Ruan 2 participants 2 messages Dec 21, 2015

lore
Share & tools
lore

Shuai Ruan

author
#0 lore raw

PATCH 1/1 x86/xsaves: ebx may return wrong value using CPUID eax=0xdh, ecx =1

Refer to SDM 13.4.3. The value return by ecx[1] with cpuid function 0xdh and sub-fucntion i (i>1) indicates the alignment of the state component i when the compacted format of the extended region of an xsave area is used.

So when hvm guest using CPUID eax=0xdh,ecx=1 to get the size of area used for compacted format, we need to take alignment into consideration.

---
 tools/libxc/xc_cpuid_x86.c | 10 ++++++++--
 xen/arch/x86/hvm/hvm.c     | 12 ++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
tools/libxc/xc_cpuid_x86.c +8 -2
index 8882c01..480bd83 100644
@@ -285,6 +285,7 @@ static void intel_xc_cpuid_policy(xc_interface *xch,
 #define XSAVEC          (1 << 1)
 #define XGETBV1         (1 << 2)
 #define XSAVES          (1 << 3)
+#define XSTATE_ALIGN    (1 << 1)
 /* Configure extended state enumeration leaves (0x0000000D for xsave) */
 static void xc_cpuid_config_xsave(xc_interface *xch,
                                   const struct cpuid_domain_info *info,
@@ -333,8 +334,13 @@ static void xc_cpuid_config_xsave(xc_interface *xch,
             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.
+         * ECX[1] indicates whether the state component
+         * needs alignment when compacted format is used.
+         */
+        regs[2] &= XSTATE_ALIGN;
+        /* Clean up EDX */
+        regs[3] = 0;
         break;
     }
 }
xen/arch/x86/hvm/hvm.c +12 -0
index 08cef1f..c4ac677 100644
@@ -4619,7 +4619,19 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                     for ( sub_leaf = 2; sub_leaf < 63; sub_leaf++ )
                         if ( (v->arch.xcr0 | v->arch.hvm_vcpu.msr_xss) &
                              (1ULL << sub_leaf) )
+                        {
+                            domain_cpuid(d, input, sub_leaf, &_eax, &_ebx,
+                                         &_ecx, &_edx);
+                            /*
+                             * The value return by _ecx[1] indicates the
+                             * alignment of the state component i when the
+                             * compacted format of the extended region of
+                             *  an xsave area is used.
+                             */
+                            if (_ecx & 0x2)
+                                *ebx = ROUNDUP(*ebx, 64);
                             *ebx += xstate_sizes[sub_leaf];
+			}
             }
             else
                 *ebx = *ecx = *edx = 0;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 21.12.15 at 02:50, <[email protected]> wrote:

Refer to SDM 13.4.3. The value return by ecx[1] with cpuid

I think we've clarified before that SDM references should be by section title, not section number.

tools/libxc/xc_cpuid_x86.c static void intel_xc_cpuid_policy(xc_interface *xch, · line 288

Quoted 7 lines
tools/libxc/xc_cpuid_x86.c +1 -0
@@ -285,6 +285,7 @@ static void intel_xc_cpuid_policy(xc_interface *xch,
 #define XSAVEC          (1 << 1)
 #define XGETBV1         (1 << 2)
 #define XSAVES          (1 << 3)
+#define XSTATE_ALIGN    (1 << 1)

XSTATE_ALIGN64? (There might well be another, higher aligment flag in the future.)

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -4619,7 +4619,19 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
                     for ( sub_leaf = 2; sub_leaf < 63; sub_leaf++ )
                         if ( (v->arch.xcr0 | v->arch.hvm_vcpu.msr_xss) &
                              (1ULL << sub_leaf) )

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

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+                        {
+                            domain_cpuid(d, input, sub_leaf, &_eax, &_ebx,
+                                         &_ecx, &_edx);
+                            /*
+                             * The value return by _ecx[1] indicates the
+                             * alignment of the state component i when the
+                             * compacted format of the extended region of
+                             *  an xsave area is used.
+                             */
+                            if (_ecx & 0x2)

Needs a #define just like in libxc.

Jan