diff mbox

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

Message ID 1456119321-10384-4-git-send-email-shuai.ruan@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Shuai Ruan Feb. 22, 2016, 5:35 a.m. UTC
Refer to SDM 13.4.3 Extended Region of an XSAVE Area. 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.

Signed-off-by: Shuai Ruan <shuai.ruan@linux.intel.com>
---
 tools/libxc/xc_cpuid_x86.c | 10 ++++++++--
 xen/arch/x86/hvm/hvm.c     | 12 ++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

Comments

Jan Beulich Feb. 22, 2016, 5:18 p.m. UTC | #1
>>> On 22.02.16 at 06:35, <shuai.ruan@linux.intel.com> wrote:
> --- a/xen/arch/x86/hvm/hvm.c
> +++ b/xen/arch/x86/hvm/hvm.c
> @@ -4703,7 +4703,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 & XSTATE_ALIGN64)
> +                                *ebx = ROUNDUP(*ebx, 64);
>                              *ebx += xstate_sizes[sub_leaf];
> +			}
>              }

Besides the various coding style issues I wonder how you get
away without any similar adjustment to pv_cpuid().

Jan
Shuai Ruan Feb. 24, 2016, 5:42 a.m. UTC | #2
On Mon, Feb 22, 2016 at 10:18:42AM -0700, Jan Beulich wrote:
> > @@ -4703,7 +4703,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 & XSTATE_ALIGN64)
> > +                                *ebx = ROUNDUP(*ebx, 64);
> >                              *ebx += xstate_sizes[sub_leaf];
> > +			}
> >              }
> 
> Besides the various coding style issues I wonder how you get
> away without any similar adjustment to pv_cpuid().
Current pv does not support xsaves , and ebx return by CPUID (eax= 0xd,
ecx =1 ) is depend on xcr0 | msr_xss. msr_xss is only support in hvm.

Thanks
> 
> Jan
> 
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> http://lists.xen.org/xen-devel
>
Shuai Ruan Feb. 24, 2016, 6:51 a.m. UTC | #3
On Mon, Feb 22, 2016 at 10:18:42AM -0700, Jan Beulich wrote:
> >                               (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 & XSTATE_ALIGN64)
> > +                                *ebx = ROUNDUP(*ebx, 64);
> >                              *ebx += xstate_sizes[sub_leaf];
> > +			}
> >              }
> 
> Besides the various coding style issues I wonder how you get
> away without any similar adjustment to pv_cpuid().
> 
Please ignore the email reponse to your question earlier.
I will add pv_cpuid() support too.

Thanks 
> Jan
> 
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> http://lists.xen.org/xen-devel
>
Jan Beulich Feb. 24, 2016, 9:18 a.m. UTC | #4
>>> On 24.02.16 at 06:42, <shuai.ruan@linux.intel.com> wrote:
> On Mon, Feb 22, 2016 at 10:18:42AM -0700, Jan Beulich wrote:
>> > @@ -4703,7 +4703,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 & XSTATE_ALIGN64)
>> > +                                *ebx = ROUNDUP(*ebx, 64);
>> >                              *ebx += xstate_sizes[sub_leaf];
>> > +			}
>> >              }
>> 
>> Besides the various coding style issues I wonder how you get
>> away without any similar adjustment to pv_cpuid().
> Current pv does not support xsaves , and ebx return by CPUID (eax= 0xd,
> ecx =1 ) is depend on xcr0 | msr_xss. msr_xss is only support in hvm.

My reading of the manual didn't result in such a connection. Mind
pointing out where this is being stated? (From an abstract
perspective I also can't see why the alignment flag would be tied
to XSAVES.)

Jan
diff mbox

Patch

diff --git a/tools/libxc/xc_cpuid_x86.c b/tools/libxc/xc_cpuid_x86.c
index c142595..d36f20b 100644
--- a/tools/libxc/xc_cpuid_x86.c
+++ b/tools/libxc/xc_cpuid_x86.c
@@ -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_ALIGN64  (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_ALIGN64;
+        /* Clean up EDX */
+        regs[3] = 0;
         break;
     }
 }
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index fe382ce..4ccd392 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -4703,7 +4703,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 & XSTATE_ALIGN64)
+                                *ebx = ROUNDUP(*ebx, 64);
                             *ebx += xstate_sizes[sub_leaf];
+			}
             }
             else
                 *ebx = *ecx = *edx = 0;