x86/fpu, x86/mm/pkeys: Add PKRU xsave fields and data structures
[cascardo/linux.git] / arch / x86 / kernel / fpu / xstate.c
1 /*
2  * xsave/xrstor support.
3  *
4  * Author: Suresh Siddha <suresh.b.siddha@intel.com>
5  */
6 #include <linux/compat.h>
7 #include <linux/cpu.h>
8
9 #include <asm/fpu/api.h>
10 #include <asm/fpu/internal.h>
11 #include <asm/fpu/signal.h>
12 #include <asm/fpu/regset.h>
13
14 #include <asm/tlbflush.h>
15
16 /*
17  * Although we spell it out in here, the Processor Trace
18  * xfeature is completely unused.  We use other mechanisms
19  * to save/restore PT state in Linux.
20  */
21 static const char *xfeature_names[] =
22 {
23         "x87 floating point registers"  ,
24         "SSE registers"                 ,
25         "AVX registers"                 ,
26         "MPX bounds registers"          ,
27         "MPX CSR"                       ,
28         "AVX-512 opmask"                ,
29         "AVX-512 Hi256"                 ,
30         "AVX-512 ZMM_Hi256"             ,
31         "Processor Trace (unused)"      ,
32         "Protection Keys User registers",
33         "unknown xstate feature"        ,
34 };
35
36 /*
37  * Mask of xstate features supported by the CPU and the kernel:
38  */
39 u64 xfeatures_mask __read_mostly;
40
41 static unsigned int xstate_offsets[XFEATURE_MAX] = { [ 0 ... XFEATURE_MAX - 1] = -1};
42 static unsigned int xstate_sizes[XFEATURE_MAX]   = { [ 0 ... XFEATURE_MAX - 1] = -1};
43 static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
44
45 /*
46  * Clear all of the X86_FEATURE_* bits that are unavailable
47  * when the CPU has no XSAVE support.
48  */
49 void fpu__xstate_clear_all_cpu_caps(void)
50 {
51         setup_clear_cpu_cap(X86_FEATURE_XSAVE);
52         setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
53         setup_clear_cpu_cap(X86_FEATURE_XSAVEC);
54         setup_clear_cpu_cap(X86_FEATURE_XSAVES);
55         setup_clear_cpu_cap(X86_FEATURE_AVX);
56         setup_clear_cpu_cap(X86_FEATURE_AVX2);
57         setup_clear_cpu_cap(X86_FEATURE_AVX512F);
58         setup_clear_cpu_cap(X86_FEATURE_AVX512PF);
59         setup_clear_cpu_cap(X86_FEATURE_AVX512ER);
60         setup_clear_cpu_cap(X86_FEATURE_AVX512CD);
61         setup_clear_cpu_cap(X86_FEATURE_MPX);
62         setup_clear_cpu_cap(X86_FEATURE_XGETBV1);
63         setup_clear_cpu_cap(X86_FEATURE_PKU);
64 }
65
66 /*
67  * Return whether the system supports a given xfeature.
68  *
69  * Also return the name of the (most advanced) feature that the caller requested:
70  */
71 int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
72 {
73         u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask;
74
75         if (unlikely(feature_name)) {
76                 long xfeature_idx, max_idx;
77                 u64 xfeatures_print;
78                 /*
79                  * So we use FLS here to be able to print the most advanced
80                  * feature that was requested but is missing. So if a driver
81                  * asks about "XFEATURE_MASK_SSE | XFEATURE_MASK_YMM" we'll print the
82                  * missing AVX feature - this is the most informative message
83                  * to users:
84                  */
85                 if (xfeatures_missing)
86                         xfeatures_print = xfeatures_missing;
87                 else
88                         xfeatures_print = xfeatures_needed;
89
90                 xfeature_idx = fls64(xfeatures_print)-1;
91                 max_idx = ARRAY_SIZE(xfeature_names)-1;
92                 xfeature_idx = min(xfeature_idx, max_idx);
93
94                 *feature_name = xfeature_names[xfeature_idx];
95         }
96
97         if (xfeatures_missing)
98                 return 0;
99
100         return 1;
101 }
102 EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
103
104 /*
105  * When executing XSAVEOPT (or other optimized XSAVE instructions), if
106  * a processor implementation detects that an FPU state component is still
107  * (or is again) in its initialized state, it may clear the corresponding
108  * bit in the header.xfeatures field, and can skip the writeout of registers
109  * to the corresponding memory layout.
110  *
111  * This means that when the bit is zero, the state component might still contain
112  * some previous - non-initialized register state.
113  *
114  * Before writing xstate information to user-space we sanitize those components,
115  * to always ensure that the memory layout of a feature will be in the init state
116  * if the corresponding header bit is zero. This is to ensure that user-space doesn't
117  * see some stale state in the memory layout during signal handling, debugging etc.
118  */
119 void fpstate_sanitize_xstate(struct fpu *fpu)
120 {
121         struct fxregs_state *fx = &fpu->state.fxsave;
122         int feature_bit;
123         u64 xfeatures;
124
125         if (!use_xsaveopt())
126                 return;
127
128         xfeatures = fpu->state.xsave.header.xfeatures;
129
130         /*
131          * None of the feature bits are in init state. So nothing else
132          * to do for us, as the memory layout is up to date.
133          */
134         if ((xfeatures & xfeatures_mask) == xfeatures_mask)
135                 return;
136
137         /*
138          * FP is in init state
139          */
140         if (!(xfeatures & XFEATURE_MASK_FP)) {
141                 fx->cwd = 0x37f;
142                 fx->swd = 0;
143                 fx->twd = 0;
144                 fx->fop = 0;
145                 fx->rip = 0;
146                 fx->rdp = 0;
147                 memset(&fx->st_space[0], 0, 128);
148         }
149
150         /*
151          * SSE is in init state
152          */
153         if (!(xfeatures & XFEATURE_MASK_SSE))
154                 memset(&fx->xmm_space[0], 0, 256);
155
156         /*
157          * First two features are FPU and SSE, which above we handled
158          * in a special way already:
159          */
160         feature_bit = 0x2;
161         xfeatures = (xfeatures_mask & ~xfeatures) >> 2;
162
163         /*
164          * Update all the remaining memory layouts according to their
165          * standard xstate layout, if their header bit is in the init
166          * state:
167          */
168         while (xfeatures) {
169                 if (xfeatures & 0x1) {
170                         int offset = xstate_offsets[feature_bit];
171                         int size = xstate_sizes[feature_bit];
172
173                         memcpy((void *)fx + offset,
174                                (void *)&init_fpstate.xsave + offset,
175                                size);
176                 }
177
178                 xfeatures >>= 1;
179                 feature_bit++;
180         }
181 }
182
183 /*
184  * Enable the extended processor state save/restore feature.
185  * Called once per CPU onlining.
186  */
187 void fpu__init_cpu_xstate(void)
188 {
189         if (!cpu_has_xsave || !xfeatures_mask)
190                 return;
191
192         cr4_set_bits(X86_CR4_OSXSAVE);
193         xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
194 }
195
196 /*
197  * Note that in the future we will likely need a pair of
198  * functions here: one for user xstates and the other for
199  * system xstates.  For now, they are the same.
200  */
201 static int xfeature_enabled(enum xfeature xfeature)
202 {
203         return !!(xfeatures_mask & (1UL << xfeature));
204 }
205
206 /*
207  * Record the offsets and sizes of various xstates contained
208  * in the XSAVE state memory layout.
209  */
210 static void __init setup_xstate_features(void)
211 {
212         u32 eax, ebx, ecx, edx, i;
213         /* start at the beginnning of the "extended state" */
214         unsigned int last_good_offset = offsetof(struct xregs_state,
215                                                  extended_state_area);
216
217         for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
218                 if (!xfeature_enabled(i))
219                         continue;
220
221                 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
222                 xstate_offsets[i] = ebx;
223                 xstate_sizes[i] = eax;
224                 /*
225                  * In our xstate size checks, we assume that the
226                  * highest-numbered xstate feature has the
227                  * highest offset in the buffer.  Ensure it does.
228                  */
229                 WARN_ONCE(last_good_offset > xstate_offsets[i],
230                         "x86/fpu: misordered xstate at %d\n", last_good_offset);
231                 last_good_offset = xstate_offsets[i];
232
233                 printk(KERN_INFO "x86/fpu: xstate_offset[%d]: %4d, xstate_sizes[%d]: %4d\n", i, ebx, i, eax);
234         }
235 }
236
237 static void __init print_xstate_feature(u64 xstate_mask)
238 {
239         const char *feature_name;
240
241         if (cpu_has_xfeatures(xstate_mask, &feature_name))
242                 pr_info("x86/fpu: Supporting XSAVE feature 0x%03Lx: '%s'\n", xstate_mask, feature_name);
243 }
244
245 /*
246  * Print out all the supported xstate features:
247  */
248 static void __init print_xstate_features(void)
249 {
250         print_xstate_feature(XFEATURE_MASK_FP);
251         print_xstate_feature(XFEATURE_MASK_SSE);
252         print_xstate_feature(XFEATURE_MASK_YMM);
253         print_xstate_feature(XFEATURE_MASK_BNDREGS);
254         print_xstate_feature(XFEATURE_MASK_BNDCSR);
255         print_xstate_feature(XFEATURE_MASK_OPMASK);
256         print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
257         print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
258         print_xstate_feature(XFEATURE_MASK_PKRU);
259 }
260
261 /*
262  * This function sets up offsets and sizes of all extended states in
263  * xsave area. This supports both standard format and compacted format
264  * of the xsave aread.
265  */
266 static void __init setup_xstate_comp(void)
267 {
268         unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8];
269         int i;
270
271         /*
272          * The FP xstates and SSE xstates are legacy states. They are always
273          * in the fixed offsets in the xsave area in either compacted form
274          * or standard form.
275          */
276         xstate_comp_offsets[0] = 0;
277         xstate_comp_offsets[1] = offsetof(struct fxregs_state, xmm_space);
278
279         if (!cpu_has_xsaves) {
280                 for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
281                         if (xfeature_enabled(i)) {
282                                 xstate_comp_offsets[i] = xstate_offsets[i];
283                                 xstate_comp_sizes[i] = xstate_sizes[i];
284                         }
285                 }
286                 return;
287         }
288
289         xstate_comp_offsets[FIRST_EXTENDED_XFEATURE] =
290                 FXSAVE_SIZE + XSAVE_HDR_SIZE;
291
292         for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
293                 if (xfeature_enabled(i))
294                         xstate_comp_sizes[i] = xstate_sizes[i];
295                 else
296                         xstate_comp_sizes[i] = 0;
297
298                 if (i > FIRST_EXTENDED_XFEATURE)
299                         xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
300                                         + xstate_comp_sizes[i-1];
301
302         }
303 }
304
305 /*
306  * setup the xstate image representing the init state
307  */
308 static void __init setup_init_fpu_buf(void)
309 {
310         static int on_boot_cpu __initdata = 1;
311
312         WARN_ON_FPU(!on_boot_cpu);
313         on_boot_cpu = 0;
314
315         if (!cpu_has_xsave)
316                 return;
317
318         setup_xstate_features();
319         print_xstate_features();
320
321         if (cpu_has_xsaves) {
322                 init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask;
323                 init_fpstate.xsave.header.xfeatures = xfeatures_mask;
324         }
325
326         /*
327          * Init all the features state with header_bv being 0x0
328          */
329         copy_kernel_to_xregs_booting(&init_fpstate.xsave);
330
331         /*
332          * Dump the init state again. This is to identify the init state
333          * of any feature which is not represented by all zero's.
334          */
335         copy_xregs_to_kernel_booting(&init_fpstate.xsave);
336 }
337
338 static int xfeature_is_supervisor(int xfeature_nr)
339 {
340         /*
341          * We currently do not support supervisor states, but if
342          * we did, we could find out like this.
343          *
344          * SDM says: If state component i is a user state component,
345          * ECX[0] return 0; if state component i is a supervisor
346          * state component, ECX[0] returns 1.
347         u32 eax, ebx, ecx, edx;
348         cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx;
349         return !!(ecx & 1);
350         */
351         return 0;
352 }
353 /*
354 static int xfeature_is_user(int xfeature_nr)
355 {
356         return !xfeature_is_supervisor(xfeature_nr);
357 }
358 */
359
360 /*
361  * This check is important because it is easy to get XSTATE_*
362  * confused with XSTATE_BIT_*.
363  */
364 #define CHECK_XFEATURE(nr) do {         \
365         WARN_ON(nr < FIRST_EXTENDED_XFEATURE);  \
366         WARN_ON(nr >= XFEATURE_MAX);    \
367 } while (0)
368
369 /*
370  * We could cache this like xstate_size[], but we only use
371  * it here, so it would be a waste of space.
372  */
373 static int xfeature_is_aligned(int xfeature_nr)
374 {
375         u32 eax, ebx, ecx, edx;
376
377         CHECK_XFEATURE(xfeature_nr);
378         cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
379         /*
380          * The value returned by ECX[1] indicates the alignment
381          * of state component i when the compacted format
382          * of the extended region of an XSAVE area is used
383          */
384         return !!(ecx & 2);
385 }
386
387 static int xfeature_uncompacted_offset(int xfeature_nr)
388 {
389         u32 eax, ebx, ecx, edx;
390
391         CHECK_XFEATURE(xfeature_nr);
392         cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
393         return ebx;
394 }
395
396 static int xfeature_size(int xfeature_nr)
397 {
398         u32 eax, ebx, ecx, edx;
399
400         CHECK_XFEATURE(xfeature_nr);
401         cpuid_count(XSTATE_CPUID, xfeature_nr, &eax, &ebx, &ecx, &edx);
402         return eax;
403 }
404
405 /*
406  * 'XSAVES' implies two different things:
407  * 1. saving of supervisor/system state
408  * 2. using the compacted format
409  *
410  * Use this function when dealing with the compacted format so
411  * that it is obvious which aspect of 'XSAVES' is being handled
412  * by the calling code.
413  */
414 static int using_compacted_format(void)
415 {
416         return cpu_has_xsaves;
417 }
418
419 static void __xstate_dump_leaves(void)
420 {
421         int i;
422         u32 eax, ebx, ecx, edx;
423         static int should_dump = 1;
424
425         if (!should_dump)
426                 return;
427         should_dump = 0;
428         /*
429          * Dump out a few leaves past the ones that we support
430          * just in case there are some goodies up there
431          */
432         for (i = 0; i < XFEATURE_MAX + 10; i++) {
433                 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
434                 pr_warn("CPUID[%02x, %02x]: eax=%08x ebx=%08x ecx=%08x edx=%08x\n",
435                         XSTATE_CPUID, i, eax, ebx, ecx, edx);
436         }
437 }
438
439 #define XSTATE_WARN_ON(x) do {                                                  \
440         if (WARN_ONCE(x, "XSAVE consistency problem, dumping leaves")) {        \
441                 __xstate_dump_leaves();                                         \
442         }                                                                       \
443 } while (0)
444
445 #define XCHECK_SZ(sz, nr, nr_macro, __struct) do {                      \
446         if ((nr == nr_macro) &&                                         \
447             WARN_ONCE(sz != sizeof(__struct),                           \
448                 "%s: struct is %zu bytes, cpu state %d bytes\n",        \
449                 __stringify(nr_macro), sizeof(__struct), sz)) {         \
450                 __xstate_dump_leaves();                                 \
451         }                                                               \
452 } while (0)
453
454 /*
455  * We have a C struct for each 'xstate'.  We need to ensure
456  * that our software representation matches what the CPU
457  * tells us about the state's size.
458  */
459 static void check_xstate_against_struct(int nr)
460 {
461         /*
462          * Ask the CPU for the size of the state.
463          */
464         int sz = xfeature_size(nr);
465         /*
466          * Match each CPU state with the corresponding software
467          * structure.
468          */
469         XCHECK_SZ(sz, nr, XFEATURE_YMM,       struct ymmh_struct);
470         XCHECK_SZ(sz, nr, XFEATURE_BNDREGS,   struct mpx_bndreg_state);
471         XCHECK_SZ(sz, nr, XFEATURE_BNDCSR,    struct mpx_bndcsr_state);
472         XCHECK_SZ(sz, nr, XFEATURE_OPMASK,    struct avx_512_opmask_state);
473         XCHECK_SZ(sz, nr, XFEATURE_ZMM_Hi256, struct avx_512_zmm_uppers_state);
474         XCHECK_SZ(sz, nr, XFEATURE_Hi16_ZMM,  struct avx_512_hi16_state);
475         XCHECK_SZ(sz, nr, XFEATURE_PKRU,      struct pkru_state);
476
477         /*
478          * Make *SURE* to add any feature numbers in below if
479          * there are "holes" in the xsave state component
480          * numbers.
481          */
482         if ((nr < XFEATURE_YMM) ||
483             (nr >= XFEATURE_MAX) ||
484             (nr == XFEATURE_PT_UNIMPLEMENTED_SO_FAR)) {
485                 WARN_ONCE(1, "no structure for xstate: %d\n", nr);
486                 XSTATE_WARN_ON(1);
487         }
488 }
489
490 /*
491  * This essentially double-checks what the cpu told us about
492  * how large the XSAVE buffer needs to be.  We are recalculating
493  * it to be safe.
494  */
495 static void do_extra_xstate_size_checks(void)
496 {
497         int paranoid_xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
498         int i;
499
500         for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) {
501                 if (!xfeature_enabled(i))
502                         continue;
503
504                 check_xstate_against_struct(i);
505                 /*
506                  * Supervisor state components can be managed only by
507                  * XSAVES, which is compacted-format only.
508                  */
509                 if (!using_compacted_format())
510                         XSTATE_WARN_ON(xfeature_is_supervisor(i));
511
512                 /* Align from the end of the previous feature */
513                 if (xfeature_is_aligned(i))
514                         paranoid_xstate_size = ALIGN(paranoid_xstate_size, 64);
515                 /*
516                  * The offset of a given state in the non-compacted
517                  * format is given to us in a CPUID leaf.  We check
518                  * them for being ordered (increasing offsets) in
519                  * setup_xstate_features().
520                  */
521                 if (!using_compacted_format())
522                         paranoid_xstate_size = xfeature_uncompacted_offset(i);
523                 /*
524                  * The compacted-format offset always depends on where
525                  * the previous state ended.
526                  */
527                 paranoid_xstate_size += xfeature_size(i);
528         }
529         XSTATE_WARN_ON(paranoid_xstate_size != xstate_size);
530 }
531
532 /*
533  * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
534  *
535  * Note the SDM's wording here.  "sub-function 0" only enumerates
536  * the size of the *user* states.  If we use it to size a buffer
537  * that we use 'XSAVES' on, we could potentially overflow the
538  * buffer because 'XSAVES' saves system states too.
539  *
540  * Note that we do not currently set any bits on IA32_XSS so
541  * 'XCR0 | IA32_XSS == XCR0' for now.
542  */
543 static unsigned int __init calculate_xstate_size(void)
544 {
545         unsigned int eax, ebx, ecx, edx;
546         unsigned int calculated_xstate_size;
547
548         if (!cpu_has_xsaves) {
549                 /*
550                  * - CPUID function 0DH, sub-function 0:
551                  *    EBX enumerates the size (in bytes) required by
552                  *    the XSAVE instruction for an XSAVE area
553                  *    containing all the *user* state components
554                  *    corresponding to bits currently set in XCR0.
555                  */
556                 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
557                 calculated_xstate_size = ebx;
558         } else {
559                 /*
560                  * - CPUID function 0DH, sub-function 1:
561                  *    EBX enumerates the size (in bytes) required by
562                  *    the XSAVES instruction for an XSAVE area
563                  *    containing all the state components
564                  *    corresponding to bits currently set in
565                  *    XCR0 | IA32_XSS.
566                  */
567                 cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
568                 calculated_xstate_size = ebx;
569         }
570         return calculated_xstate_size;
571 }
572
573 /*
574  * Will the runtime-enumerated 'xstate_size' fit in the init
575  * task's statically-allocated buffer?
576  */
577 static bool is_supported_xstate_size(unsigned int test_xstate_size)
578 {
579         if (test_xstate_size <= sizeof(union fpregs_state))
580                 return true;
581
582         pr_warn("x86/fpu: xstate buffer too small (%zu < %d), disabling xsave\n",
583                         sizeof(union fpregs_state), test_xstate_size);
584         return false;
585 }
586
587 static int init_xstate_size(void)
588 {
589         /* Recompute the context size for enabled features: */
590         unsigned int possible_xstate_size = calculate_xstate_size();
591
592         /* Ensure we have the space to store all enabled: */
593         if (!is_supported_xstate_size(possible_xstate_size))
594                 return -EINVAL;
595
596         /*
597          * The size is OK, we are definitely going to use xsave,
598          * make it known to the world that we need more space.
599          */
600         xstate_size = possible_xstate_size;
601         do_extra_xstate_size_checks();
602         return 0;
603 }
604
605 /*
606  * We enabled the XSAVE hardware, but something went wrong and
607  * we can not use it.  Disable it.
608  */
609 static void fpu__init_disable_system_xstate(void)
610 {
611         xfeatures_mask = 0;
612         cr4_clear_bits(X86_CR4_OSXSAVE);
613         fpu__xstate_clear_all_cpu_caps();
614 }
615
616 /*
617  * Enable and initialize the xsave feature.
618  * Called once per system bootup.
619  */
620 void __init fpu__init_system_xstate(void)
621 {
622         unsigned int eax, ebx, ecx, edx;
623         static int on_boot_cpu __initdata = 1;
624         int err;
625
626         WARN_ON_FPU(!on_boot_cpu);
627         on_boot_cpu = 0;
628
629         if (!cpu_has_xsave) {
630                 pr_info("x86/fpu: Legacy x87 FPU detected.\n");
631                 return;
632         }
633
634         if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
635                 WARN_ON_FPU(1);
636                 return;
637         }
638
639         cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
640         xfeatures_mask = eax + ((u64)edx << 32);
641
642         if ((xfeatures_mask & XFEATURE_MASK_FPSSE) != XFEATURE_MASK_FPSSE) {
643                 pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask);
644                 BUG();
645         }
646
647         xfeatures_mask &= fpu__get_supported_xfeatures_mask();
648
649         /* Enable xstate instructions to be able to continue with initialization: */
650         fpu__init_cpu_xstate();
651         err = init_xstate_size();
652         if (err) {
653                 /* something went wrong, boot without any XSAVE support */
654                 fpu__init_disable_system_xstate();
655                 return;
656         }
657
658         update_regset_xstate_info(xstate_size, xfeatures_mask);
659         fpu__init_prepare_fx_sw_frame();
660         setup_init_fpu_buf();
661         setup_xstate_comp();
662
663         pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n",
664                 xfeatures_mask,
665                 xstate_size,
666                 cpu_has_xsaves ? "compacted" : "standard");
667 }
668
669 /*
670  * Restore minimal FPU state after suspend:
671  */
672 void fpu__resume_cpu(void)
673 {
674         /*
675          * Restore XCR0 on xsave capable CPUs:
676          */
677         if (cpu_has_xsave)
678                 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
679 }
680
681 /*
682  * Given the xsave area and a state inside, this function returns the
683  * address of the state.
684  *
685  * This is the API that is called to get xstate address in either
686  * standard format or compacted format of xsave area.
687  *
688  * Note that if there is no data for the field in the xsave buffer
689  * this will return NULL.
690  *
691  * Inputs:
692  *      xstate: the thread's storage area for all FPU data
693  *      xstate_feature: state which is defined in xsave.h (e.g.
694  *      XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...)
695  * Output:
696  *      address of the state in the xsave area, or NULL if the
697  *      field is not present in the xsave buffer.
698  */
699 void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
700 {
701         int feature_nr = fls64(xstate_feature) - 1;
702         /*
703          * Do we even *have* xsave state?
704          */
705         if (!boot_cpu_has(X86_FEATURE_XSAVE))
706                 return NULL;
707
708         /*
709          * We should not ever be requesting features that we
710          * have not enabled.  Remember that pcntxt_mask is
711          * what we write to the XCR0 register.
712          */
713         WARN_ONCE(!(xfeatures_mask & xstate_feature),
714                   "get of unsupported state");
715         /*
716          * This assumes the last 'xsave*' instruction to
717          * have requested that 'xstate_feature' be saved.
718          * If it did not, we might be seeing and old value
719          * of the field in the buffer.
720          *
721          * This can happen because the last 'xsave' did not
722          * request that this feature be saved (unlikely)
723          * or because the "init optimization" caused it
724          * to not be saved.
725          */
726         if (!(xsave->header.xfeatures & xstate_feature))
727                 return NULL;
728
729         return (void *)xsave + xstate_comp_offsets[feature_nr];
730 }
731 EXPORT_SYMBOL_GPL(get_xsave_addr);
732
733 /*
734  * This wraps up the common operations that need to occur when retrieving
735  * data from xsave state.  It first ensures that the current task was
736  * using the FPU and retrieves the data in to a buffer.  It then calculates
737  * the offset of the requested field in the buffer.
738  *
739  * This function is safe to call whether the FPU is in use or not.
740  *
741  * Note that this only works on the current task.
742  *
743  * Inputs:
744  *      @xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP,
745  *      XFEATURE_MASK_SSE, etc...)
746  * Output:
747  *      address of the state in the xsave area or NULL if the state
748  *      is not present or is in its 'init state'.
749  */
750 const void *get_xsave_field_ptr(int xsave_state)
751 {
752         struct fpu *fpu = &current->thread.fpu;
753
754         if (!fpu->fpstate_active)
755                 return NULL;
756         /*
757          * fpu__save() takes the CPU's xstate registers
758          * and saves them off to the 'fpu memory buffer.
759          */
760         fpu__save(fpu);
761
762         return get_xsave_addr(&fpu->state.xsave, xsave_state);
763 }