x86/xsave: add fastpath for common xstate_ctxt_size() requests
authorAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 5 Jun 2014 15:52:11 +0000 (17:52 +0200)
committerJan Beulich <jbeulich@suse.com>
Thu, 5 Jun 2014 15:52:11 +0000 (17:52 +0200)
xstate_ctxt_size(xfeature_mask) is runtime constant after boot, and for bounds
checking when handling xsave state.  Avoid reloading xcr0 twice to obtain a
number which has already been calculated.

Also annotate xfeature_mask as __read_mostly as it is only ever written once.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/xstate.c

index e202344613a961a86af178ea76924eb98a3534e1..5f3b1612d2fcb48480d73bddf6d91a94de93f7b3 100644 (file)
@@ -24,7 +24,7 @@ bool_t __read_mostly cpu_has_xsaveopt;
 static u32 __read_mostly xsave_cntxt_size;
 
 /* A 64-bit bitmask of the XSAVE/XRSTOR features supported by processor. */
-u64 xfeature_mask;
+u64 __read_mostly xfeature_mask;
 
 /* Cached xcr0 for fast read */
 static DEFINE_PER_CPU(uint64_t, xcr0);
@@ -250,6 +250,33 @@ void xstate_free_save_area(struct vcpu *v)
     v->arch.xsave_area = NULL;
 }
 
+static unsigned int _xstate_ctxt_size(u64 xcr0)
+{
+    u64 act_xcr0 = get_xcr0();
+    u32 eax, ebx = 0, ecx, edx;
+    bool_t ok = set_xcr0(xcr0);
+
+    ASSERT(ok);
+    cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
+    ASSERT(ebx <= ecx);
+    ok = set_xcr0(act_xcr0);
+    ASSERT(ok);
+
+    return ebx;
+}
+
+/* Fastpath for common xstate size requests, avoiding reloads of xcr0. */
+unsigned int xstate_ctxt_size(u64 xcr0)
+{
+    if ( xcr0 == xfeature_mask )
+        return xsave_cntxt_size;
+
+    if ( xcr0 == 0 )
+        return 0;
+
+    return _xstate_ctxt_size(xcr0);
+}
+
 /* Collect the information of processor's extended state */
 void xstate_init(bool_t bsp)
 {
@@ -283,14 +310,14 @@ void xstate_init(bool_t bsp)
          * xsave_cntxt_size is the max size required by enabled features.
          * We know FP/SSE and YMM about eax, and nothing about edx at present.
          */
-        xsave_cntxt_size = xstate_ctxt_size(feature_mask);
+        xsave_cntxt_size = _xstate_ctxt_size(feature_mask);
         printk("%s: using cntxt_size: %#x and states: %#"PRIx64"\n",
             __func__, xsave_cntxt_size, xfeature_mask);
     }
     else
     {
         BUG_ON(xfeature_mask != feature_mask);
-        BUG_ON(xsave_cntxt_size != xstate_ctxt_size(feature_mask));
+        BUG_ON(xsave_cntxt_size != _xstate_ctxt_size(feature_mask));
     }
 
     /* Check XSAVEOPT feature. */
@@ -301,26 +328,6 @@ void xstate_init(bool_t bsp)
         BUG_ON(!cpu_has_xsaveopt != !(eax & XSTATE_FEATURE_XSAVEOPT));
 }
 
-unsigned int xstate_ctxt_size(u64 xcr0)
-{
-    u32 ebx = 0;
-
-    if ( xcr0 )
-    {
-        u64 act_xcr0 = get_xcr0();
-        u32 eax, ecx, edx;
-        bool_t ok = set_xcr0(xcr0);
-
-        ASSERT(ok);
-        cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
-        ASSERT(ebx <= ecx);
-        ok = set_xcr0(act_xcr0);
-        ASSERT(ok);
-    }
-
-    return ebx;
-}
-
 static bool_t valid_xcr0(u64 xcr0)
 {
     /* FP must be unconditionally set. */