All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shuai Ruan <shuai.ruan@linux.intel.com>
To: xen-devel@lists.xen.org
Cc: andrew.cooper3@citrix.com, keir@xen.org, jbeulich@suse.com
Subject: [PATCH V7 3/3] x86/xsaves: ebx may return wrong value using CPUID eax=0xdh, ecx =1
Date: Thu, 31 Mar 2016 16:57:37 +0800	[thread overview]
Message-ID: <1459414657-7558-4-git-send-email-shuai.ruan@linux.intel.com> (raw)
In-Reply-To: <1459414657-7558-1-git-send-email-shuai.ruan@linux.intel.com>

From: Shuai Ruan <shuai.ruan@intel.com>

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/pv 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 side is fixed by
"tools/libxc: Calculate xstate cpuid leaf from guest information"
by Andrew Cooper

Signed-off-by: Shuai Ruan <shuai.ruan@intel.com>
---
v2: Address comments by Jan:
1. take alignment into consideration in pv_cpuid.
2. fix coding style issues

 xen/arch/x86/hvm/hvm.c       |  6 +++++-
 xen/arch/x86/traps.c         | 12 ++++++++++++
 xen/arch/x86/xstate.c        |  2 +-
 xen/include/asm-x86/xstate.h |  1 +
 4 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index 5aef3cb..c6cd4fb 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -4743,14 +4743,18 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
         }
         if ( count == 1 )
         {
-            if ( cpu_has_xsaves && cpu_has_vmx_xsaves )
+            if ( (cpu_has_xsaves && cpu_has_vmx_xsaves) || cpu_has_xsavec )
             {
                 *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) )
+                        {
+                            if ( test_bit(sub_leaf, &xstate_align) )
+                                *ebx = ROUNDUP(*ebx, 64);
                             *ebx += xstate_sizes[sub_leaf];
+                        }
             }
             else
                 *ebx = *ecx = *edx = 0;
diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c
index 6fbb1cf..8694da6 100644
--- a/xen/arch/x86/traps.c
+++ b/xen/arch/x86/traps.c
@@ -1020,6 +1020,18 @@ void pv_cpuid(struct cpu_user_regs *regs)
             a &= (boot_cpu_data.x86_capability[cpufeat_word(X86_FEATURE_XSAVEOPT)] &
                   ~cpufeat_mask(X86_FEATURE_XSAVES));
             b = c = d = 0;
+            if ( cpu_has_xsavec )
+            {
+                b = XSTATE_AREA_MIN_SIZE;
+                if ( curr->arch.xcr0 )
+                    for( subleaf = 2; subleaf < 63; subleaf++ )
+                        if ( (1ULL << subleaf) & curr->arch.xcr0 )
+                        {
+                            if ( test_bit(subleaf, &xstate_align) )
+                                b = ROUNDUP(b, 64);
+                            b += xstate_sizes[subleaf];
+                        }
+            }
             break;
         }
         break;
diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c
index f4ea54d..850b778 100644
--- a/xen/arch/x86/xstate.c
+++ b/xen/arch/x86/xstate.c
@@ -26,7 +26,7 @@ u64 __read_mostly xfeature_mask;
 
 static unsigned int *__read_mostly xstate_offsets;
 unsigned int *__read_mostly xstate_sizes;
-static u64 __read_mostly xstate_align;
+u64 __read_mostly xstate_align;
 static unsigned int __read_mostly xstate_features;
 
 static uint32_t __read_mostly mxcsr_mask = 0x0000ffbf;
diff --git a/xen/include/asm-x86/xstate.h b/xen/include/asm-x86/xstate.h
index 91d1c39..535443a 100644
--- a/xen/include/asm-x86/xstate.h
+++ b/xen/include/asm-x86/xstate.h
@@ -50,6 +50,7 @@
 #define XSTATE_ALIGN64 (1U << 1)
 
 extern u64 xfeature_mask;
+extern u64 xstate_align;
 extern unsigned int *xstate_sizes;
 
 /* extended state save area */
-- 
1.9.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

  parent reply	other threads:[~2016-03-31  8:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-31  8:57 [PATCH V7 0/3] xsaves bug fix Shuai Ruan
2016-03-31  8:57 ` [PATCH V7 1/3] x86/xsaves: fix overwriting between non-lazy/lazy xsaves Shuai Ruan
2016-04-04 15:51   ` Jan Beulich
2016-04-05  5:30     ` Shuai Ruan
     [not found]     ` <20160405053023.GA16876@shuai.ruan@linux.intel.com>
2016-04-05  7:17       ` Jan Beulich
2016-04-05  7:29         ` Shuai Ruan
2016-04-25  6:51   ` Jan Beulich
2016-04-29  1:36     ` Shuai Ruan
     [not found]     ` <20160429013616.GB4359@shuai.ruan@linux.intel.com>
2016-04-29  7:05       ` Jan Beulich
2016-03-31  8:57 ` [PATCH V7 2/3] x86/xsaves: fix two remained issues Shuai Ruan
2016-04-04 16:03   ` Jan Beulich
2016-03-31  8:57 ` Shuai Ruan [this message]
2016-04-05  8:31   ` [PATCH V7 3/3] x86/xsaves: ebx may return wrong value using CPUID eax=0xdh, ecx =1 Jan Beulich
2016-04-06  7:01     ` Shuai Ruan
     [not found]     ` <20160406070034.GA26357@shuai.ruan@linux.intel.com>
2016-04-07  0:29       ` Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1459414657-7558-4-git-send-email-shuai.ruan@linux.intel.com \
    --to=shuai.ruan@linux.intel.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=keir@xen.org \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.