xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Shuai Ruan <shuai.ruan@linux.intel.com>
To: xen-devel@lists.xen.org
Cc: wei.liu2@citrix.com, Ian.Campbell@citrix.com,
	stefano.stabellini@eu.citrix.com, andrew.cooper3@citrix.com,
	ian.jackson@eu.citrix.com, jbeulich@suse.com, keir@xen.org
Subject: [PATCH 1/3] x86/xsaves: caculate the xstate_comp_offsets base on xcomp_bv
Date: Mon, 22 Feb 2016 13:35:19 +0800	[thread overview]
Message-ID: <1456119321-10384-2-git-send-email-shuai.ruan@linux.intel.com> (raw)
In-Reply-To: <1456119321-10384-1-git-send-email-shuai.ruan@linux.intel.com>

Previous patch using all available features caculate xstate_comp_offsets.
This is wrong.This patch fix this bug by caculating the xstate_comp_offset
based on xcomp_bv of current guest.
Also, the xstate_comp_offset should take alignment into consideration.

Signed-off-by: Shuai Ruan <shuai.ruan@linux.intel.com>
---
 xen/arch/x86/xstate.c        | 29 +++++++++++++++++++++--------
 xen/include/asm-x86/xstate.h |  1 +
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/xen/arch/x86/xstate.c b/xen/arch/x86/xstate.c
index 4f2fb8e..0e7643b 100644
--- a/xen/arch/x86/xstate.c
+++ b/xen/arch/x86/xstate.c
@@ -26,6 +26,7 @@ u64 __read_mostly xfeature_mask;
 
 static unsigned int *__read_mostly xstate_offsets;
 unsigned int *__read_mostly xstate_sizes;
+static unsigned int *__read_mostly xstate_align;
 static unsigned int __read_mostly xstate_features;
 static unsigned int __read_mostly xstate_comp_offsets[sizeof(xfeature_mask)*8];
 
@@ -94,7 +95,7 @@ static bool_t xsave_area_compressed(const struct xsave_struct *xsave_area)
 
 static int setup_xstate_features(bool_t bsp)
 {
-    unsigned int leaf, tmp, eax, ebx;
+    unsigned int leaf, eax, ebx, ecx, edx;
 
     if ( bsp )
     {
@@ -106,34 +107,44 @@ static int setup_xstate_features(bool_t bsp)
         xstate_sizes = xzalloc_array(unsigned int, xstate_features);
         if ( !xstate_sizes )
             return -ENOMEM;
+
+        xstate_align = xzalloc_array(unsigned int, xstate_features);
+        if ( !xstate_align )
+            return -ENOMEM;
     }
 
     for ( leaf = 2; leaf < xstate_features; leaf++ )
     {
         if ( bsp )
+	{
             cpuid_count(XSTATE_CPUID, leaf, &xstate_sizes[leaf],
-                        &xstate_offsets[leaf], &tmp, &tmp);
+                        &xstate_offsets[leaf], &ecx, &edx);
+            xstate_align[leaf] = ecx & XSTATE_ALIGN64;
+	}
         else
         {
             cpuid_count(XSTATE_CPUID, leaf, &eax,
-                        &ebx, &tmp, &tmp);
+                        &ebx, &ecx, &edx);
             BUG_ON(eax != xstate_sizes[leaf]);
             BUG_ON(ebx != xstate_offsets[leaf]);
+            BUG_ON((ecx & XSTATE_ALIGN64) != xstate_align[leaf]);
         }
     }
 
     return 0;
 }
 
-static void __init setup_xstate_comp(void)
+static void setup_xstate_comp(const struct xsave_struct *xsave)
 {
     unsigned int i;
+    u64 xcomp_bv =  xsave->xsave_hdr.xcomp_bv;
 
     /*
      * The FP xstates and SSE xstates are legacy states. They are always
      * in the fixed offsets in the xsave area in either compacted form
      * or standard form.
      */
+    memset(xstate_comp_offsets, 0, sizeof(xstate_comp_offsets));
     xstate_comp_offsets[0] = 0;
     xstate_comp_offsets[1] = XSAVE_SSE_OFFSET;
 
@@ -141,8 +152,10 @@ static void __init setup_xstate_comp(void)
 
     for ( i = 3; i < xstate_features; i++ )
     {
-        xstate_comp_offsets[i] = xstate_comp_offsets[i - 1] +
-                                 (((1ul << i) & xfeature_mask)
+        xstate_comp_offsets[i] = (xstate_align[i] ?
+                                  ROUNDUP(xstate_comp_offsets[i-1], 64) :
+                                  xstate_comp_offsets[i - 1]) +
+                                 (((1ul << i) & xcomp_bv)
                                   ? xstate_sizes[i - 1] : 0);
         ASSERT(xstate_comp_offsets[i] + xstate_sizes[i] <= xsave_cntxt_size);
     }
@@ -172,6 +185,7 @@ void expand_xsave_states(struct vcpu *v, void *dest, unsigned int size)
     }
 
     ASSERT(xsave_area_compressed(xsave));
+    setup_xstate_comp(xsave);
     /*
      * Copy legacy XSAVE area and XSAVE hdr area.
      */
@@ -223,6 +237,7 @@ void compress_xsave_states(struct vcpu *v, const void *src, unsigned int size)
     xsave->xsave_hdr.xstate_bv = xstate_bv;
     xsave->xsave_hdr.xcomp_bv = v->arch.xcr0_accum | XSTATE_COMPACTION_ENABLED;
 
+    setup_xstate_comp(xsave);
     /*
      * Copy each region from the non-compacted offset to the
      * possibly compacted offset.
@@ -568,8 +583,6 @@ void xstate_init(struct cpuinfo_x86 *c)
 
     if ( setup_xstate_features(bsp) && bsp )
         BUG();
-    if ( bsp && (cpu_has_xsaves || cpu_has_xsavec) )
-        setup_xstate_comp();
 }
 
 static bool_t valid_xcr0(u64 xcr0)
diff --git a/xen/include/asm-x86/xstate.h b/xen/include/asm-x86/xstate.h
index 84f0af9..0215070 100644
--- a/xen/include/asm-x86/xstate.h
+++ b/xen/include/asm-x86/xstate.h
@@ -44,6 +44,7 @@
 #define XSTATE_LAZY    (XSTATE_ALL & ~XSTATE_NONLAZY)
 #define XSTATE_COMPACTION_ENABLED  (1ULL << 63)
 
+#define XSTATE_ALIGN64 (1ULL << 1)
 extern u64 xfeature_mask;
 extern unsigned int *xstate_sizes;
 
-- 
1.9.1

  reply	other threads:[~2016-02-22  5:35 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-22  5:35 [PATCH 0/3] x86/xsaves: bugs fix Shuai Ruan
2016-02-22  5:35 ` Shuai Ruan [this message]
2016-02-22 14:48   ` [PATCH 1/3] x86/xsaves: caculate the xstate_comp_offsets base on xcomp_bv Jan Beulich
2016-02-22  5:35 ` [PATCH 2/3] x86/xsaves: fix overwriting between non-lazy/lazy xsave[sc] Shuai Ruan
2016-02-22 17:08   ` Jan Beulich
2016-02-24  7:05     ` Shuai Ruan
     [not found]     ` <20160224070521.GB19785@shuai.ruan@linux.intel.com>
2016-02-24  9:16       ` Jan Beulich
2016-02-26  7:41         ` Shuai Ruan
     [not found]         ` <20160226074133.GA3825@shuai.ruan@linux.intel.com>
2016-02-26  8:42           ` Jan Beulich
2016-02-29  9:06             ` Shuai Ruan
     [not found]             ` <20160229090637.GB3825@shuai.ruan@linux.intel.com>
2016-02-29  9:33               ` Jan Beulich
2016-03-02 10:31                 ` Shuai Ruan
2016-02-22  5:35 ` [PATCH 3/3] x86/xsaves: ebx may return wrong value using CPUID eax=0xdh, ecx =1 Shuai Ruan
2016-02-22 17:18   ` Jan Beulich
2016-02-24  5:42     ` Shuai Ruan
2016-02-24  6:51     ` Shuai Ruan
     [not found]     ` <20160224054209.GA24976@shuai.ruan@linux.intel.com>
2016-02-24  9:18       ` 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=1456119321-10384-2-git-send-email-shuai.ruan@linux.intel.com \
    --to=shuai.ruan@linux.intel.com \
    --cc=Ian.Campbell@citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=keir@xen.org \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=wei.liu2@citrix.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).