From: Dave Hansen <[email protected]>

Note: our xsaves support is currently broken and disabled.  This
patch does not fix it, but it is an incremental improvement.  It
might be useful to someone backporting the entire set of XSAVES
patches at some point, but it should not be backported alone.

There are currently two xsave buffer formats: standard and
compacted.  The standard format is waht 'XSAVE' and 'XSAVEOPT'
produce while 'XSAVES' and 'XSAVEC' produce a compacted-formet
buffer.  (The kernel never uses XSAVEC)

But, the XSAVES buffer *ALSO* contains "system state components"
which are never saved by a plain XSAVE.  So, XSAVES has two
things that might make its buffer differently-sized from an
XSAVE-produced one.

The current code assumes that an XSAVES buffer's size is simply
the sum of the sizes of the (user) states which are supported.
This seems to work in most cases, but it is not consistent with
what the SDM says, and it breaks if we 'align' a component in the
buffer.  The calculation is also unnecessary work since the CPU
*tells* us the size of the buffer directly.

This patch just reads the size of the buffer right out of the
CPUID leaf instead of trying to derive it.

Signed-off-by: Dave Hansen <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Linux Kernel Mailing List <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Fenghua Yu <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: [email protected]
---

 b/arch/x86/kernel/fpu/xstate.c |   36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff -puN arch/x86/kernel/fpu/xstate.c~fix-xstate_size-calculation 
arch/x86/kernel/fpu/xstate.c
--- a/arch/x86/kernel/fpu/xstate.c~fix-xstate_size-calculation  2015-07-24 
09:50:36.418385438 -0700
+++ b/arch/x86/kernel/fpu/xstate.c      2015-07-27 11:02:13.305376883 -0700
@@ -292,24 +292,40 @@ static void __init setup_init_fpu_buf(vo
 
 /*
  * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
+ *
+ * Note the SDM's wording here.  "sub-function 0" only enumerates
+ * the size of the *user* states.  If we use it to size a buffer
+ * that we use 'XSAVES' on, we could potentially overflow the
+ * buffer because 'XSAVES' saves system states too.
+ *
+ * Note that we do not currently set any bits on IA32_XSS so
+ * 'XCR0 | IA32_XSS == XCR0' for now.
  */
 static void __init init_xstate_size(void)
 {
        unsigned int eax, ebx, ecx, edx;
-       int i;
 
        if (!cpu_has_xsaves) {
+               /*
+                * - CPUID function 0DH, sub-function 0:
+                *    EBX enumerates the size (in bytes) required by
+                *    the XSAVE instruction for an XSAVE area
+                *    containing all the *user* state components
+                *    corresponding to bits currently set in XCR0.
+                */
                cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
                xstate_size = ebx;
-               return;
-       }
-
-       xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
-       for (i = 2; i < 64; i++) {
-               if (test_bit(i, (unsigned long *)&xfeatures_mask)) {
-                       cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
-                       xstate_size += eax;
-               }
+       } else {
+               /*
+                * - CPUID function 0DH, sub-function 1:
+                *    EBX enumerates the size (in bytes) required by
+                *    the XSAVES instruction for an XSAVE area
+                *    containing all the state components
+                *    corresponding to bits currently set in
+                *    XCR0 | IA32_XSS.
+                */
+               cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
+               xstate_size = ebx;
        }
 }
 
_
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to