]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/kernel/fpu/xstate.c
x86/fpu: Rename all the fpregs, xregs, fxregs and fregs handling functions
[mirror_ubuntu-artful-kernel.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 #include <asm/sigframe.h>
14 #include <asm/tlbflush.h>
15
16 static const char *xfeature_names[] =
17 {
18 "x87 floating point registers" ,
19 "SSE registers" ,
20 "AVX registers" ,
21 "MPX bounds registers" ,
22 "MPX CSR" ,
23 "AVX-512 opmask" ,
24 "AVX-512 Hi256" ,
25 "AVX-512 ZMM_Hi256" ,
26 "unknown xstate feature" ,
27 };
28
29 /*
30 * Mask of xstate features supported by the CPU and the kernel:
31 */
32 u64 xfeatures_mask __read_mostly;
33
34 static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
35 static unsigned int xstate_offsets[XFEATURES_NR_MAX], xstate_sizes[XFEATURES_NR_MAX];
36 static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
37
38 /* The number of supported xfeatures in xfeatures_mask: */
39 static unsigned int xfeatures_nr;
40
41 /*
42 * Return whether the system supports a given xfeature.
43 *
44 * Also return the name of the (most advanced) feature that the caller requested:
45 */
46 int cpu_has_xfeatures(u64 xfeatures_needed, const char **feature_name)
47 {
48 u64 xfeatures_missing = xfeatures_needed & ~xfeatures_mask;
49
50 if (unlikely(feature_name)) {
51 long xfeature_idx, max_idx;
52 u64 xfeatures_print;
53 /*
54 * So we use FLS here to be able to print the most advanced
55 * feature that was requested but is missing. So if a driver
56 * asks about "XSTATE_SSE | XSTATE_YMM" we'll print the
57 * missing AVX feature - this is the most informative message
58 * to users:
59 */
60 if (xfeatures_missing)
61 xfeatures_print = xfeatures_missing;
62 else
63 xfeatures_print = xfeatures_needed;
64
65 xfeature_idx = fls64(xfeatures_print)-1;
66 max_idx = ARRAY_SIZE(xfeature_names)-1;
67 xfeature_idx = min(xfeature_idx, max_idx);
68
69 *feature_name = xfeature_names[xfeature_idx];
70 }
71
72 if (xfeatures_missing)
73 return 0;
74
75 return 1;
76 }
77 EXPORT_SYMBOL_GPL(cpu_has_xfeatures);
78
79 /*
80 * When executing XSAVEOPT (optimized XSAVE), if a processor implementation
81 * detects that an FPU state component is still (or is again) in its
82 * initialized state, it may clear the corresponding bit in the header.xfeatures
83 * field, and can skip the writeout of registers to the corresponding memory layout.
84 *
85 * This means that when the bit is zero, the state component might still contain
86 * some previous - non-initialized register state.
87 *
88 * Before writing xstate information to user-space we sanitize those components,
89 * to always ensure that the memory layout of a feature will be in the init state
90 * if the corresponding header bit is zero. This is to ensure that user-space doesn't
91 * see some stale state in the memory layout during signal handling, debugging etc.
92 */
93 void fpstate_sanitize_xstate(struct fpu *fpu)
94 {
95 struct i387_fxsave_struct *fx = &fpu->state.fxsave;
96 int feature_bit;
97 u64 xfeatures;
98
99 if (!use_xsaveopt())
100 return;
101
102 xfeatures = fpu->state.xsave.header.xfeatures;
103
104 /*
105 * None of the feature bits are in init state. So nothing else
106 * to do for us, as the memory layout is up to date.
107 */
108 if ((xfeatures & xfeatures_mask) == xfeatures_mask)
109 return;
110
111 /*
112 * FP is in init state
113 */
114 if (!(xfeatures & XSTATE_FP)) {
115 fx->cwd = 0x37f;
116 fx->swd = 0;
117 fx->twd = 0;
118 fx->fop = 0;
119 fx->rip = 0;
120 fx->rdp = 0;
121 memset(&fx->st_space[0], 0, 128);
122 }
123
124 /*
125 * SSE is in init state
126 */
127 if (!(xfeatures & XSTATE_SSE))
128 memset(&fx->xmm_space[0], 0, 256);
129
130 /*
131 * First two features are FPU and SSE, which above we handled
132 * in a special way already:
133 */
134 feature_bit = 0x2;
135 xfeatures = (xfeatures_mask & ~xfeatures) >> 2;
136
137 /*
138 * Update all the remaining memory layouts according to their
139 * standard xstate layout, if their header bit is in the init
140 * state:
141 */
142 while (xfeatures) {
143 if (xfeatures & 0x1) {
144 int offset = xstate_offsets[feature_bit];
145 int size = xstate_sizes[feature_bit];
146
147 memcpy((void *)fx + offset,
148 (void *)&init_fpstate.xsave + offset,
149 size);
150 }
151
152 xfeatures >>= 1;
153 feature_bit++;
154 }
155 }
156
157 /*
158 * Check for the presence of extended state information in the
159 * user fpstate pointer in the sigcontext.
160 */
161 static inline int check_for_xstate(struct i387_fxsave_struct __user *buf,
162 void __user *fpstate,
163 struct _fpx_sw_bytes *fx_sw)
164 {
165 int min_xstate_size = sizeof(struct i387_fxsave_struct) +
166 sizeof(struct xstate_header);
167 unsigned int magic2;
168
169 if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
170 return -1;
171
172 /* Check for the first magic field and other error scenarios. */
173 if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
174 fx_sw->xstate_size < min_xstate_size ||
175 fx_sw->xstate_size > xstate_size ||
176 fx_sw->xstate_size > fx_sw->extended_size)
177 return -1;
178
179 /*
180 * Check for the presence of second magic word at the end of memory
181 * layout. This detects the case where the user just copied the legacy
182 * fpstate layout with out copying the extended state information
183 * in the memory layout.
184 */
185 if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
186 || magic2 != FP_XSTATE_MAGIC2)
187 return -1;
188
189 return 0;
190 }
191
192 /*
193 * Signal frame handlers.
194 */
195 static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
196 {
197 if (use_fxsr()) {
198 struct xsave_struct *xsave = &tsk->thread.fpu.state.xsave;
199 struct user_i387_ia32_struct env;
200 struct _fpstate_ia32 __user *fp = buf;
201
202 convert_from_fxsr(&env, tsk);
203
204 if (__copy_to_user(buf, &env, sizeof(env)) ||
205 __put_user(xsave->i387.swd, &fp->status) ||
206 __put_user(X86_FXSR_MAGIC, &fp->magic))
207 return -1;
208 } else {
209 struct i387_fsave_struct __user *fp = buf;
210 u32 swd;
211 if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
212 return -1;
213 }
214
215 return 0;
216 }
217
218 static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
219 {
220 struct xsave_struct __user *x = buf;
221 struct _fpx_sw_bytes *sw_bytes;
222 u32 xfeatures;
223 int err;
224
225 /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
226 sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved;
227 err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes));
228
229 if (!use_xsave())
230 return err;
231
232 err |= __put_user(FP_XSTATE_MAGIC2, (__u32 *)(buf + xstate_size));
233
234 /*
235 * Read the xfeatures which we copied (directly from the cpu or
236 * from the state in task struct) to the user buffers.
237 */
238 err |= __get_user(xfeatures, (__u32 *)&x->header.xfeatures);
239
240 /*
241 * For legacy compatible, we always set FP/SSE bits in the bit
242 * vector while saving the state to the user context. This will
243 * enable us capturing any changes(during sigreturn) to
244 * the FP/SSE bits by the legacy applications which don't touch
245 * xfeatures in the xsave header.
246 *
247 * xsave aware apps can change the xfeatures in the xsave
248 * header as well as change any contents in the memory layout.
249 * xrestore as part of sigreturn will capture all the changes.
250 */
251 xfeatures |= XSTATE_FPSSE;
252
253 err |= __put_user(xfeatures, (__u32 *)&x->header.xfeatures);
254
255 return err;
256 }
257
258 static inline int copy_fpregs_to_sigframe(struct xsave_struct __user *buf)
259 {
260 int err;
261
262 if (use_xsave())
263 err = copy_xregs_to_user(buf);
264 else if (use_fxsr())
265 err = copy_fxregs_to_user((struct i387_fxsave_struct __user *) buf);
266 else
267 err = copy_fregs_to_user((struct i387_fsave_struct __user *) buf);
268
269 if (unlikely(err) && __clear_user(buf, xstate_size))
270 err = -EFAULT;
271 return err;
272 }
273
274 /*
275 * Save the fpu, extended register state to the user signal frame.
276 *
277 * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
278 * state is copied.
279 * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
280 *
281 * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
282 * buf != buf_fx for 32-bit frames with fxstate.
283 *
284 * If the fpu, extended register state is live, save the state directly
285 * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise,
286 * copy the thread's fpu state to the user frame starting at 'buf_fx'.
287 *
288 * If this is a 32-bit frame with fxstate, put a fsave header before
289 * the aligned state at 'buf_fx'.
290 *
291 * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
292 * indicating the absence/presence of the extended state to the user.
293 */
294 int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
295 {
296 struct xsave_struct *xsave = &current->thread.fpu.state.xsave;
297 struct task_struct *tsk = current;
298 int ia32_fxstate = (buf != buf_fx);
299
300 ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
301 config_enabled(CONFIG_IA32_EMULATION));
302
303 if (!access_ok(VERIFY_WRITE, buf, size))
304 return -EACCES;
305
306 if (!static_cpu_has(X86_FEATURE_FPU))
307 return fpregs_soft_get(current, NULL, 0,
308 sizeof(struct user_i387_ia32_struct), NULL,
309 (struct _fpstate_ia32 __user *) buf) ? -1 : 1;
310
311 if (fpregs_active()) {
312 /* Save the live register state to the user directly. */
313 if (copy_fpregs_to_sigframe(buf_fx))
314 return -1;
315 /* Update the thread's fxstate to save the fsave header. */
316 if (ia32_fxstate)
317 copy_fxregs_to_kernel(&tsk->thread.fpu);
318 } else {
319 fpstate_sanitize_xstate(&tsk->thread.fpu);
320 if (__copy_to_user(buf_fx, xsave, xstate_size))
321 return -1;
322 }
323
324 /* Save the fsave header for the 32-bit frames. */
325 if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
326 return -1;
327
328 if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
329 return -1;
330
331 return 0;
332 }
333
334 static inline void
335 sanitize_restored_xstate(struct task_struct *tsk,
336 struct user_i387_ia32_struct *ia32_env,
337 u64 xfeatures, int fx_only)
338 {
339 struct xsave_struct *xsave = &tsk->thread.fpu.state.xsave;
340 struct xstate_header *header = &xsave->header;
341
342 if (use_xsave()) {
343 /* These bits must be zero. */
344 memset(header->reserved, 0, 48);
345
346 /*
347 * Init the state that is not present in the memory
348 * layout and not enabled by the OS.
349 */
350 if (fx_only)
351 header->xfeatures = XSTATE_FPSSE;
352 else
353 header->xfeatures &= (xfeatures_mask & xfeatures);
354 }
355
356 if (use_fxsr()) {
357 /*
358 * mscsr reserved bits must be masked to zero for security
359 * reasons.
360 */
361 xsave->i387.mxcsr &= mxcsr_feature_mask;
362
363 convert_to_fxsr(tsk, ia32_env);
364 }
365 }
366
367 /*
368 * Restore the extended state if present. Otherwise, restore the FP/SSE state.
369 */
370 static inline int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
371 {
372 if (use_xsave()) {
373 if ((unsigned long)buf % 64 || fx_only) {
374 u64 init_bv = xfeatures_mask & ~XSTATE_FPSSE;
375 copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
376 return copy_user_to_fxregs(buf);
377 } else {
378 u64 init_bv = xfeatures_mask & ~xbv;
379 if (unlikely(init_bv))
380 copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
381 return copy_user_to_xregs(buf, xbv);
382 }
383 } else if (use_fxsr()) {
384 return copy_user_to_fxregs(buf);
385 } else
386 return copy_user_to_fregs(buf);
387 }
388
389 static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
390 {
391 int ia32_fxstate = (buf != buf_fx);
392 struct task_struct *tsk = current;
393 struct fpu *fpu = &tsk->thread.fpu;
394 int state_size = xstate_size;
395 u64 xfeatures = 0;
396 int fx_only = 0;
397
398 ia32_fxstate &= (config_enabled(CONFIG_X86_32) ||
399 config_enabled(CONFIG_IA32_EMULATION));
400
401 if (!buf) {
402 fpu__clear(fpu);
403 return 0;
404 }
405
406 if (!access_ok(VERIFY_READ, buf, size))
407 return -EACCES;
408
409 fpu__activate_curr(fpu);
410
411 if (!static_cpu_has(X86_FEATURE_FPU))
412 return fpregs_soft_set(current, NULL,
413 0, sizeof(struct user_i387_ia32_struct),
414 NULL, buf) != 0;
415
416 if (use_xsave()) {
417 struct _fpx_sw_bytes fx_sw_user;
418 if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
419 /*
420 * Couldn't find the extended state information in the
421 * memory layout. Restore just the FP/SSE and init all
422 * the other extended state.
423 */
424 state_size = sizeof(struct i387_fxsave_struct);
425 fx_only = 1;
426 } else {
427 state_size = fx_sw_user.xstate_size;
428 xfeatures = fx_sw_user.xfeatures;
429 }
430 }
431
432 if (ia32_fxstate) {
433 /*
434 * For 32-bit frames with fxstate, copy the user state to the
435 * thread's fpu state, reconstruct fxstate from the fsave
436 * header. Sanitize the copied state etc.
437 */
438 struct fpu *fpu = &tsk->thread.fpu;
439 struct user_i387_ia32_struct env;
440 int err = 0;
441
442 /*
443 * Drop the current fpu which clears fpu->fpstate_active. This ensures
444 * that any context-switch during the copy of the new state,
445 * avoids the intermediate state from getting restored/saved.
446 * Thus avoiding the new restored state from getting corrupted.
447 * We will be ready to restore/save the state only after
448 * fpu->fpstate_active is again set.
449 */
450 fpu__drop(fpu);
451
452 if (__copy_from_user(&fpu->state.xsave, buf_fx, state_size) ||
453 __copy_from_user(&env, buf, sizeof(env))) {
454 fpstate_init(&fpu->state);
455 err = -1;
456 } else {
457 sanitize_restored_xstate(tsk, &env, xfeatures, fx_only);
458 }
459
460 fpu->fpstate_active = 1;
461 if (use_eager_fpu()) {
462 preempt_disable();
463 fpu__restore();
464 preempt_enable();
465 }
466
467 return err;
468 } else {
469 /*
470 * For 64-bit frames and 32-bit fsave frames, restore the user
471 * state to the registers directly (with exceptions handled).
472 */
473 user_fpu_begin();
474 if (copy_user_to_fpregs_zeroing(buf_fx, xfeatures, fx_only)) {
475 fpu__clear(fpu);
476 return -1;
477 }
478 }
479
480 return 0;
481 }
482
483 static inline int xstate_sigframe_size(void)
484 {
485 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
486 }
487
488 /*
489 * Restore FPU state from a sigframe:
490 */
491 int fpu__restore_sig(void __user *buf, int ia32_frame)
492 {
493 void __user *buf_fx = buf;
494 int size = xstate_sigframe_size();
495
496 if (ia32_frame && use_fxsr()) {
497 buf_fx = buf + sizeof(struct i387_fsave_struct);
498 size += sizeof(struct i387_fsave_struct);
499 }
500
501 return __fpu__restore_sig(buf, buf_fx, size);
502 }
503
504 unsigned long
505 fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
506 unsigned long *buf_fx, unsigned long *size)
507 {
508 unsigned long frame_size = xstate_sigframe_size();
509
510 *buf_fx = sp = round_down(sp - frame_size, 64);
511 if (ia32_frame && use_fxsr()) {
512 frame_size += sizeof(struct i387_fsave_struct);
513 sp -= sizeof(struct i387_fsave_struct);
514 }
515
516 *size = frame_size;
517
518 return sp;
519 }
520 /*
521 * Prepare the SW reserved portion of the fxsave memory layout, indicating
522 * the presence of the extended state information in the memory layout
523 * pointed by the fpstate pointer in the sigcontext.
524 * This will be saved when ever the FP and extended state context is
525 * saved on the user stack during the signal handler delivery to the user.
526 */
527 static void prepare_fx_sw_frame(void)
528 {
529 int fsave_header_size = sizeof(struct i387_fsave_struct);
530 int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
531
532 if (config_enabled(CONFIG_X86_32))
533 size += fsave_header_size;
534
535 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
536 fx_sw_reserved.extended_size = size;
537 fx_sw_reserved.xfeatures = xfeatures_mask;
538 fx_sw_reserved.xstate_size = xstate_size;
539
540 if (config_enabled(CONFIG_IA32_EMULATION)) {
541 fx_sw_reserved_ia32 = fx_sw_reserved;
542 fx_sw_reserved_ia32.extended_size += fsave_header_size;
543 }
544 }
545
546 /*
547 * Enable the extended processor state save/restore feature.
548 * Called once per CPU onlining.
549 */
550 void fpu__init_cpu_xstate(void)
551 {
552 if (!cpu_has_xsave || !xfeatures_mask)
553 return;
554
555 cr4_set_bits(X86_CR4_OSXSAVE);
556 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
557 }
558
559 /*
560 * Record the offsets and sizes of different state managed by the xsave
561 * memory layout.
562 */
563 static void __init setup_xstate_features(void)
564 {
565 int eax, ebx, ecx, edx, leaf = 0x2;
566
567 xfeatures_nr = fls64(xfeatures_mask);
568
569 do {
570 cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
571
572 if (eax == 0)
573 break;
574
575 xstate_offsets[leaf] = ebx;
576 xstate_sizes[leaf] = eax;
577
578 leaf++;
579 } while (1);
580 }
581
582 static void print_xstate_feature(u64 xstate_mask)
583 {
584 const char *feature_name;
585
586 if (cpu_has_xfeatures(xstate_mask, &feature_name))
587 pr_info("x86/fpu: Supporting XSAVE feature 0x%02Lx: '%s'\n", xstate_mask, feature_name);
588 }
589
590 /*
591 * Print out all the supported xstate features:
592 */
593 static void print_xstate_features(void)
594 {
595 print_xstate_feature(XSTATE_FP);
596 print_xstate_feature(XSTATE_SSE);
597 print_xstate_feature(XSTATE_YMM);
598 print_xstate_feature(XSTATE_BNDREGS);
599 print_xstate_feature(XSTATE_BNDCSR);
600 print_xstate_feature(XSTATE_OPMASK);
601 print_xstate_feature(XSTATE_ZMM_Hi256);
602 print_xstate_feature(XSTATE_Hi16_ZMM);
603 }
604
605 /*
606 * This function sets up offsets and sizes of all extended states in
607 * xsave area. This supports both standard format and compacted format
608 * of the xsave aread.
609 *
610 * Input: void
611 * Output: void
612 */
613 void setup_xstate_comp(void)
614 {
615 unsigned int xstate_comp_sizes[sizeof(xfeatures_mask)*8];
616 int i;
617
618 /*
619 * The FP xstates and SSE xstates are legacy states. They are always
620 * in the fixed offsets in the xsave area in either compacted form
621 * or standard form.
622 */
623 xstate_comp_offsets[0] = 0;
624 xstate_comp_offsets[1] = offsetof(struct i387_fxsave_struct, xmm_space);
625
626 if (!cpu_has_xsaves) {
627 for (i = 2; i < xfeatures_nr; i++) {
628 if (test_bit(i, (unsigned long *)&xfeatures_mask)) {
629 xstate_comp_offsets[i] = xstate_offsets[i];
630 xstate_comp_sizes[i] = xstate_sizes[i];
631 }
632 }
633 return;
634 }
635
636 xstate_comp_offsets[2] = FXSAVE_SIZE + XSAVE_HDR_SIZE;
637
638 for (i = 2; i < xfeatures_nr; i++) {
639 if (test_bit(i, (unsigned long *)&xfeatures_mask))
640 xstate_comp_sizes[i] = xstate_sizes[i];
641 else
642 xstate_comp_sizes[i] = 0;
643
644 if (i > 2)
645 xstate_comp_offsets[i] = xstate_comp_offsets[i-1]
646 + xstate_comp_sizes[i-1];
647
648 }
649 }
650
651 /*
652 * setup the xstate image representing the init state
653 */
654 static void setup_init_fpu_buf(void)
655 {
656 if (!cpu_has_xsave)
657 return;
658
659 setup_xstate_features();
660 print_xstate_features();
661
662 if (cpu_has_xsaves) {
663 init_fpstate.xsave.header.xcomp_bv = (u64)1 << 63 | xfeatures_mask;
664 init_fpstate.xsave.header.xfeatures = xfeatures_mask;
665 }
666
667 /*
668 * Init all the features state with header_bv being 0x0
669 */
670 copy_kernel_to_xregs_booting(&init_fpstate.xsave, -1);
671
672 /*
673 * Dump the init state again. This is to identify the init state
674 * of any feature which is not represented by all zero's.
675 */
676 copy_xregs_to_kernel_booting(&init_fpstate.xsave);
677 }
678
679 /*
680 * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
681 */
682 static void __init init_xstate_size(void)
683 {
684 unsigned int eax, ebx, ecx, edx;
685 int i;
686
687 if (!cpu_has_xsaves) {
688 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
689 xstate_size = ebx;
690 return;
691 }
692
693 xstate_size = FXSAVE_SIZE + XSAVE_HDR_SIZE;
694 for (i = 2; i < 64; i++) {
695 if (test_bit(i, (unsigned long *)&xfeatures_mask)) {
696 cpuid_count(XSTATE_CPUID, i, &eax, &ebx, &ecx, &edx);
697 xstate_size += eax;
698 }
699 }
700 }
701
702 /*
703 * Enable and initialize the xsave feature.
704 * Called once per system bootup.
705 *
706 * ( Not marked __init because of false positive section warnings. )
707 */
708 void fpu__init_system_xstate(void)
709 {
710 unsigned int eax, ebx, ecx, edx;
711
712 if (!cpu_has_xsave) {
713 pr_info("x86/fpu: Legacy x87 FPU detected.\n");
714 return;
715 }
716
717 if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
718 WARN(1, "x86/fpu: XSTATE_CPUID missing!\n");
719 return;
720 }
721
722 cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
723 xfeatures_mask = eax + ((u64)edx << 32);
724
725 if ((xfeatures_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
726 pr_err("x86/fpu: FP/SSE not present amongst the CPU's xstate features: 0x%llx.\n", xfeatures_mask);
727 BUG();
728 }
729
730 /*
731 * Support only the state known to OS.
732 */
733 xfeatures_mask = xfeatures_mask & XCNTXT_MASK;
734
735 /* Enable xstate instructions to be able to continue with initialization: */
736 fpu__init_cpu_xstate();
737
738 /*
739 * Recompute the context size for enabled features
740 */
741 init_xstate_size();
742
743 update_regset_xstate_info(xstate_size, xfeatures_mask);
744 prepare_fx_sw_frame();
745 setup_init_fpu_buf();
746
747 pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is 0x%x bytes, using '%s' format.\n",
748 xfeatures_mask,
749 xstate_size,
750 cpu_has_xsaves ? "compacted" : "standard");
751 }
752
753 /*
754 * Restore minimal FPU state after suspend:
755 */
756 void fpu__resume_cpu(void)
757 {
758 /*
759 * Restore XCR0 on xsave capable CPUs:
760 */
761 if (cpu_has_xsave)
762 xsetbv(XCR_XFEATURE_ENABLED_MASK, xfeatures_mask);
763 }
764
765 /*
766 * Given the xsave area and a state inside, this function returns the
767 * address of the state.
768 *
769 * This is the API that is called to get xstate address in either
770 * standard format or compacted format of xsave area.
771 *
772 * Inputs:
773 * xsave: base address of the xsave area;
774 * xstate: state which is defined in xsave.h (e.g. XSTATE_FP, XSTATE_SSE,
775 * etc.)
776 * Output:
777 * address of the state in the xsave area.
778 */
779 void *get_xsave_addr(struct xsave_struct *xsave, int xstate)
780 {
781 int feature = fls64(xstate) - 1;
782 if (!test_bit(feature, (unsigned long *)&xfeatures_mask))
783 return NULL;
784
785 return (void *)xsave + xstate_comp_offsets[feature];
786 }
787 EXPORT_SYMBOL_GPL(get_xsave_addr);