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