Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * FPU signal frame handling routines.
4 */
5
6#include <linux/compat.h>
7#include <linux/cpu.h>
8#include <linux/pagemap.h>
9
10#include <asm/fpu/signal.h>
11#include <asm/fpu/regset.h>
12#include <asm/fpu/xstate.h>
13
14#include <asm/sigframe.h>
15#include <asm/trapnr.h>
16#include <asm/trace/fpu.h>
17
18#include "context.h"
19#include "internal.h"
20#include "legacy.h"
21#include "xstate.h"
22
23/*
24 * Check for the presence of extended state information in the
25 * user fpstate pointer in the sigcontext.
26 */
27static inline bool check_xstate_in_sigframe(struct fxregs_state __user *fxbuf,
28 struct _fpx_sw_bytes *fx_sw)
29{
30 int min_xstate_size = sizeof(struct fxregs_state) +
31 sizeof(struct xstate_header);
32 void __user *fpstate = fxbuf;
33 unsigned int magic2;
34
35 if (__copy_from_user(fx_sw, &fxbuf->sw_reserved[0], sizeof(*fx_sw)))
36 return false;
37
38 /* Check for the first magic field and other error scenarios. */
39 if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
40 fx_sw->xstate_size < min_xstate_size ||
41 fx_sw->xstate_size > current->thread.fpu.fpstate->user_size ||
42 fx_sw->xstate_size > fx_sw->extended_size)
43 goto setfx;
44
45 /*
46 * Check for the presence of second magic word at the end of memory
47 * layout. This detects the case where the user just copied the legacy
48 * fpstate layout with out copying the extended state information
49 * in the memory layout.
50 */
51 if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size)))
52 return false;
53
54 if (likely(magic2 == FP_XSTATE_MAGIC2))
55 return true;
56setfx:
57 trace_x86_fpu_xstate_check_failed(¤t->thread.fpu);
58
59 /* Set the parameters for fx only state */
60 fx_sw->magic1 = 0;
61 fx_sw->xstate_size = sizeof(struct fxregs_state);
62 fx_sw->xfeatures = XFEATURE_MASK_FPSSE;
63 return true;
64}
65
66/*
67 * Signal frame handlers.
68 */
69static inline bool save_fsave_header(struct task_struct *tsk, void __user *buf)
70{
71 if (use_fxsr()) {
72 struct xregs_state *xsave = &tsk->thread.fpu.fpstate->regs.xsave;
73 struct user_i387_ia32_struct env;
74 struct _fpstate_32 __user *fp = buf;
75
76 fpregs_lock();
77 if (!test_thread_flag(TIF_NEED_FPU_LOAD))
78 fxsave(&tsk->thread.fpu.fpstate->regs.fxsave);
79 fpregs_unlock();
80
81 convert_from_fxsr(&env, tsk);
82
83 if (__copy_to_user(buf, &env, sizeof(env)) ||
84 __put_user(xsave->i387.swd, &fp->status) ||
85 __put_user(X86_FXSR_MAGIC, &fp->magic))
86 return false;
87 } else {
88 struct fregs_state __user *fp = buf;
89 u32 swd;
90
91 if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
92 return false;
93 }
94
95 return true;
96}
97
98/*
99 * Prepare the SW reserved portion of the fxsave memory layout, indicating
100 * the presence of the extended state information in the memory layout
101 * pointed to by the fpstate pointer in the sigcontext.
102 * This is saved when ever the FP and extended state context is
103 * saved on the user stack during the signal handler delivery to the user.
104 */
105static inline void save_sw_bytes(struct _fpx_sw_bytes *sw_bytes, bool ia32_frame,
106 struct fpstate *fpstate)
107{
108 sw_bytes->magic1 = FP_XSTATE_MAGIC1;
109 sw_bytes->extended_size = fpstate->user_size + FP_XSTATE_MAGIC2_SIZE;
110 sw_bytes->xfeatures = fpstate->user_xfeatures;
111 sw_bytes->xstate_size = fpstate->user_size;
112
113 if (ia32_frame)
114 sw_bytes->extended_size += sizeof(struct fregs_state);
115}
116
117static inline bool save_xstate_epilog(void __user *buf, int ia32_frame,
118 struct fpstate *fpstate)
119{
120 struct xregs_state __user *x = buf;
121 struct _fpx_sw_bytes sw_bytes = {};
122 u32 xfeatures;
123 int err;
124
125 /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
126 save_sw_bytes(&sw_bytes, ia32_frame, fpstate);
127 err = __copy_to_user(&x->i387.sw_reserved, &sw_bytes, sizeof(sw_bytes));
128
129 if (!use_xsave())
130 return !err;
131
132 err |= __put_user(FP_XSTATE_MAGIC2,
133 (__u32 __user *)(buf + fpstate->user_size));
134
135 /*
136 * Read the xfeatures which we copied (directly from the cpu or
137 * from the state in task struct) to the user buffers.
138 */
139 err |= __get_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
140
141 /*
142 * For legacy compatible, we always set FP/SSE bits in the bit
143 * vector while saving the state to the user context. This will
144 * enable us capturing any changes(during sigreturn) to
145 * the FP/SSE bits by the legacy applications which don't touch
146 * xfeatures in the xsave header.
147 *
148 * xsave aware apps can change the xfeatures in the xsave
149 * header as well as change any contents in the memory layout.
150 * xrestore as part of sigreturn will capture all the changes.
151 */
152 xfeatures |= XFEATURE_MASK_FPSSE;
153
154 err |= __put_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
155
156 return !err;
157}
158
159static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf, u32 pkru)
160{
161 if (use_xsave())
162 return xsave_to_user_sigframe(buf, pkru);
163
164 if (use_fxsr())
165 return fxsave_to_user_sigframe((struct fxregs_state __user *) buf);
166 else
167 return fnsave_to_user_sigframe((struct fregs_state __user *) buf);
168}
169
170/*
171 * Save the fpu, extended register state to the user signal frame.
172 *
173 * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
174 * state is copied.
175 * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
176 *
177 * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
178 * buf != buf_fx for 32-bit frames with fxstate.
179 *
180 * Save it directly to the user frame with disabled page fault handler. If
181 * that faults, try to clear the frame which handles the page fault.
182 *
183 * If this is a 32-bit frame with fxstate, put a fsave header before
184 * the aligned state at 'buf_fx'.
185 *
186 * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
187 * indicating the absence/presence of the extended state to the user.
188 */
189bool copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size, u32 pkru)
190{
191 struct task_struct *tsk = current;
192 struct fpstate *fpstate = tsk->thread.fpu.fpstate;
193 bool ia32_fxstate = (buf != buf_fx);
194 int ret;
195
196 ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
197 IS_ENABLED(CONFIG_IA32_EMULATION));
198
199 if (!static_cpu_has(X86_FEATURE_FPU)) {
200 struct user_i387_ia32_struct fp;
201
202 fpregs_soft_get(current, NULL, (struct membuf){.p = &fp,
203 .left = sizeof(fp)});
204 return !copy_to_user(buf, &fp, sizeof(fp));
205 }
206
207 if (!access_ok(buf, size))
208 return false;
209
210 if (use_xsave()) {
211 struct xregs_state __user *xbuf = buf_fx;
212
213 /*
214 * Clear the xsave header first, so that reserved fields are
215 * initialized to zero.
216 */
217 if (__clear_user(&xbuf->header, sizeof(xbuf->header)))
218 return false;
219 }
220retry:
221 /*
222 * Load the FPU registers if they are not valid for the current task.
223 * With a valid FPU state we can attempt to save the state directly to
224 * userland's stack frame which will likely succeed. If it does not,
225 * resolve the fault in the user memory and try again.
226 */
227 fpregs_lock();
228 if (test_thread_flag(TIF_NEED_FPU_LOAD))
229 fpregs_restore_userregs();
230
231 pagefault_disable();
232 ret = copy_fpregs_to_sigframe(buf_fx, pkru);
233 pagefault_enable();
234 fpregs_unlock();
235
236 if (ret) {
237 if (!__clear_user(buf_fx, fpstate->user_size))
238 goto retry;
239 return false;
240 }
241
242 /* Save the fsave header for the 32-bit frames. */
243 if ((ia32_fxstate || !use_fxsr()) && !save_fsave_header(tsk, buf))
244 return false;
245
246 if (use_fxsr() && !save_xstate_epilog(buf_fx, ia32_fxstate, fpstate))
247 return false;
248
249 return true;
250}
251
252static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures,
253 u64 xrestore, bool fx_only)
254{
255 if (use_xsave()) {
256 u64 init_bv = ufeatures & ~xrestore;
257 int ret;
258
259 if (likely(!fx_only))
260 ret = xrstor_from_user_sigframe(buf, xrestore);
261 else
262 ret = fxrstor_from_user_sigframe(buf);
263
264 if (!ret && unlikely(init_bv))
265 os_xrstor(&init_fpstate, init_bv);
266 return ret;
267 } else if (use_fxsr()) {
268 return fxrstor_from_user_sigframe(buf);
269 } else {
270 return frstor_from_user_sigframe(buf);
271 }
272}
273
274/*
275 * Attempt to restore the FPU registers directly from user memory.
276 * Pagefaults are handled and any errors returned are fatal.
277 */
278static bool restore_fpregs_from_user(void __user *buf, u64 xrestore, bool fx_only)
279{
280 struct fpu *fpu = ¤t->thread.fpu;
281 int ret;
282
283 /* Restore enabled features only. */
284 xrestore &= fpu->fpstate->user_xfeatures;
285retry:
286 fpregs_lock();
287 /* Ensure that XFD is up to date */
288 xfd_update_state(fpu->fpstate);
289 pagefault_disable();
290 ret = __restore_fpregs_from_user(buf, fpu->fpstate->user_xfeatures,
291 xrestore, fx_only);
292 pagefault_enable();
293
294 if (unlikely(ret)) {
295 /*
296 * The above did an FPU restore operation, restricted to
297 * the user portion of the registers, and failed, but the
298 * microcode might have modified the FPU registers
299 * nevertheless.
300 *
301 * If the FPU registers do not belong to current, then
302 * invalidate the FPU register state otherwise the task
303 * might preempt current and return to user space with
304 * corrupted FPU registers.
305 */
306 if (test_thread_flag(TIF_NEED_FPU_LOAD))
307 __cpu_invalidate_fpregs_state();
308 fpregs_unlock();
309
310 /* Try to handle #PF, but anything else is fatal. */
311 if (ret != X86_TRAP_PF)
312 return false;
313
314 if (!fault_in_readable(buf, fpu->fpstate->user_size))
315 goto retry;
316 return false;
317 }
318
319 /*
320 * Restore supervisor states: previous context switch etc has done
321 * XSAVES and saved the supervisor states in the kernel buffer from
322 * which they can be restored now.
323 *
324 * It would be optimal to handle this with a single XRSTORS, but
325 * this does not work because the rest of the FPU registers have
326 * been restored from a user buffer directly.
327 */
328 if (test_thread_flag(TIF_NEED_FPU_LOAD) && xfeatures_mask_supervisor())
329 os_xrstor_supervisor(fpu->fpstate);
330
331 fpregs_mark_activate();
332 fpregs_unlock();
333 return true;
334}
335
336static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,
337 bool ia32_fxstate)
338{
339 struct task_struct *tsk = current;
340 struct fpu *fpu = &tsk->thread.fpu;
341 struct user_i387_ia32_struct env;
342 bool success, fx_only = false;
343 union fpregs_state *fpregs;
344 u64 user_xfeatures = 0;
345
346 if (use_xsave()) {
347 struct _fpx_sw_bytes fx_sw_user;
348
349 if (!check_xstate_in_sigframe(buf_fx, &fx_sw_user))
350 return false;
351
352 fx_only = !fx_sw_user.magic1;
353 user_xfeatures = fx_sw_user.xfeatures;
354 } else {
355 user_xfeatures = XFEATURE_MASK_FPSSE;
356 }
357
358 if (likely(!ia32_fxstate)) {
359 /* Restore the FPU registers directly from user memory. */
360 return restore_fpregs_from_user(buf_fx, user_xfeatures, fx_only);
361 }
362
363 /*
364 * Copy the legacy state because the FP portion of the FX frame has
365 * to be ignored for histerical raisins. The legacy state is folded
366 * in once the larger state has been copied.
367 */
368 if (__copy_from_user(&env, buf, sizeof(env)))
369 return false;
370
371 /*
372 * By setting TIF_NEED_FPU_LOAD it is ensured that our xstate is
373 * not modified on context switch and that the xstate is considered
374 * to be loaded again on return to userland (overriding last_cpu avoids
375 * the optimisation).
376 */
377 fpregs_lock();
378 if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
379 /*
380 * If supervisor states are available then save the
381 * hardware state in current's fpstate so that the
382 * supervisor state is preserved. Save the full state for
383 * simplicity. There is no point in optimizing this by only
384 * saving the supervisor states and then shuffle them to
385 * the right place in memory. It's ia32 mode. Shrug.
386 */
387 if (xfeatures_mask_supervisor())
388 os_xsave(fpu->fpstate);
389 set_thread_flag(TIF_NEED_FPU_LOAD);
390 }
391 __fpu_invalidate_fpregs_state(fpu);
392 __cpu_invalidate_fpregs_state();
393 fpregs_unlock();
394
395 fpregs = &fpu->fpstate->regs;
396 if (use_xsave() && !fx_only) {
397 if (copy_sigframe_from_user_to_xstate(tsk, buf_fx))
398 return false;
399 } else {
400 if (__copy_from_user(&fpregs->fxsave, buf_fx,
401 sizeof(fpregs->fxsave)))
402 return false;
403
404 if (IS_ENABLED(CONFIG_X86_64)) {
405 /* Reject invalid MXCSR values. */
406 if (fpregs->fxsave.mxcsr & ~mxcsr_feature_mask)
407 return false;
408 } else {
409 /* Mask invalid bits out for historical reasons (broken hardware). */
410 fpregs->fxsave.mxcsr &= mxcsr_feature_mask;
411 }
412
413 /* Enforce XFEATURE_MASK_FPSSE when XSAVE is enabled */
414 if (use_xsave())
415 fpregs->xsave.header.xfeatures |= XFEATURE_MASK_FPSSE;
416 }
417
418 /* Fold the legacy FP storage */
419 convert_to_fxsr(&fpregs->fxsave, &env);
420
421 fpregs_lock();
422 if (use_xsave()) {
423 /*
424 * Remove all UABI feature bits not set in user_xfeatures
425 * from the memory xstate header which makes the full
426 * restore below bring them into init state. This works for
427 * fx_only mode as well because that has only FP and SSE
428 * set in user_xfeatures.
429 *
430 * Preserve supervisor states!
431 */
432 u64 mask = user_xfeatures | xfeatures_mask_supervisor();
433
434 fpregs->xsave.header.xfeatures &= mask;
435 success = !os_xrstor_safe(fpu->fpstate,
436 fpu_kernel_cfg.max_features);
437 } else {
438 success = !fxrstor_safe(&fpregs->fxsave);
439 }
440
441 if (likely(success))
442 fpregs_mark_activate();
443
444 fpregs_unlock();
445 return success;
446}
447
448static inline unsigned int xstate_sigframe_size(struct fpstate *fpstate)
449{
450 unsigned int size = fpstate->user_size;
451
452 return use_xsave() ? size + FP_XSTATE_MAGIC2_SIZE : size;
453}
454
455/*
456 * Restore FPU state from a sigframe:
457 */
458bool fpu__restore_sig(void __user *buf, int ia32_frame)
459{
460 struct fpu *fpu = ¤t->thread.fpu;
461 void __user *buf_fx = buf;
462 bool ia32_fxstate = false;
463 bool success = false;
464 unsigned int size;
465
466 if (unlikely(!buf)) {
467 fpu__clear_user_states(fpu);
468 return true;
469 }
470
471 size = xstate_sigframe_size(fpu->fpstate);
472
473 ia32_frame &= (IS_ENABLED(CONFIG_X86_32) ||
474 IS_ENABLED(CONFIG_IA32_EMULATION));
475
476 /*
477 * Only FXSR enabled systems need the FX state quirk.
478 * FRSTOR does not need it and can use the fast path.
479 */
480 if (ia32_frame && use_fxsr()) {
481 buf_fx = buf + sizeof(struct fregs_state);
482 size += sizeof(struct fregs_state);
483 ia32_fxstate = true;
484 }
485
486 if (!access_ok(buf, size))
487 goto out;
488
489 if (!IS_ENABLED(CONFIG_X86_64) && !cpu_feature_enabled(X86_FEATURE_FPU)) {
490 success = !fpregs_soft_set(current, NULL, 0,
491 sizeof(struct user_i387_ia32_struct),
492 NULL, buf);
493 } else {
494 success = __fpu_restore_sig(buf, buf_fx, ia32_fxstate);
495 }
496
497out:
498 if (unlikely(!success))
499 fpu__clear_user_states(fpu);
500 return success;
501}
502
503unsigned long
504fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
505 unsigned long *buf_fx, unsigned long *size)
506{
507 unsigned long frame_size = xstate_sigframe_size(current->thread.fpu.fpstate);
508
509 *buf_fx = sp = round_down(sp - frame_size, 64);
510 if (ia32_frame && use_fxsr()) {
511 frame_size += sizeof(struct fregs_state);
512 sp -= sizeof(struct fregs_state);
513 }
514
515 *size = frame_size;
516
517 return sp;
518}
519
520unsigned long __init fpu__get_fpstate_size(void)
521{
522 unsigned long ret = fpu_user_cfg.max_size;
523
524 if (use_xsave())
525 ret += FP_XSTATE_MAGIC2_SIZE;
526
527 /*
528 * This space is needed on (most) 32-bit kernels, or when a 32-bit
529 * app is running on a 64-bit kernel. To keep things simple, just
530 * assume the worst case and always include space for 'freg_state',
531 * even for 64-bit apps on 64-bit kernels. This wastes a bit of
532 * space, but keeps the code simple.
533 */
534 if ((IS_ENABLED(CONFIG_IA32_EMULATION) ||
535 IS_ENABLED(CONFIG_X86_32)) && use_fxsr())
536 ret += sizeof(struct fregs_state);
537
538 return ret;
539}
540
1/*
2 * FPU signal frame handling routines.
3 */
4
5#include <linux/compat.h>
6#include <linux/cpu.h>
7
8#include <asm/fpu/internal.h>
9#include <asm/fpu/signal.h>
10#include <asm/fpu/regset.h>
11#include <asm/fpu/xstate.h>
12
13#include <asm/sigframe.h>
14#include <asm/trace/fpu.h>
15
16static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
17
18/*
19 * Check for the presence of extended state information in the
20 * user fpstate pointer in the sigcontext.
21 */
22static inline int check_for_xstate(struct fxregs_state __user *buf,
23 void __user *fpstate,
24 struct _fpx_sw_bytes *fx_sw)
25{
26 int min_xstate_size = sizeof(struct fxregs_state) +
27 sizeof(struct xstate_header);
28 unsigned int magic2;
29
30 if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
31 return -1;
32
33 /* Check for the first magic field and other error scenarios. */
34 if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
35 fx_sw->xstate_size < min_xstate_size ||
36 fx_sw->xstate_size > fpu_user_xstate_size ||
37 fx_sw->xstate_size > fx_sw->extended_size)
38 return -1;
39
40 /*
41 * Check for the presence of second magic word at the end of memory
42 * layout. This detects the case where the user just copied the legacy
43 * fpstate layout with out copying the extended state information
44 * in the memory layout.
45 */
46 if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
47 || magic2 != FP_XSTATE_MAGIC2)
48 return -1;
49
50 return 0;
51}
52
53/*
54 * Signal frame handlers.
55 */
56static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
57{
58 if (use_fxsr()) {
59 struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
60 struct user_i387_ia32_struct env;
61 struct _fpstate_32 __user *fp = buf;
62
63 convert_from_fxsr(&env, tsk);
64
65 if (__copy_to_user(buf, &env, sizeof(env)) ||
66 __put_user(xsave->i387.swd, &fp->status) ||
67 __put_user(X86_FXSR_MAGIC, &fp->magic))
68 return -1;
69 } else {
70 struct fregs_state __user *fp = buf;
71 u32 swd;
72 if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
73 return -1;
74 }
75
76 return 0;
77}
78
79static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
80{
81 struct xregs_state __user *x = buf;
82 struct _fpx_sw_bytes *sw_bytes;
83 u32 xfeatures;
84 int err;
85
86 /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
87 sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved;
88 err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes));
89
90 if (!use_xsave())
91 return err;
92
93 err |= __put_user(FP_XSTATE_MAGIC2,
94 (__u32 *)(buf + fpu_user_xstate_size));
95
96 /*
97 * Read the xfeatures which we copied (directly from the cpu or
98 * from the state in task struct) to the user buffers.
99 */
100 err |= __get_user(xfeatures, (__u32 *)&x->header.xfeatures);
101
102 /*
103 * For legacy compatible, we always set FP/SSE bits in the bit
104 * vector while saving the state to the user context. This will
105 * enable us capturing any changes(during sigreturn) to
106 * the FP/SSE bits by the legacy applications which don't touch
107 * xfeatures in the xsave header.
108 *
109 * xsave aware apps can change the xfeatures in the xsave
110 * header as well as change any contents in the memory layout.
111 * xrestore as part of sigreturn will capture all the changes.
112 */
113 xfeatures |= XFEATURE_MASK_FPSSE;
114
115 err |= __put_user(xfeatures, (__u32 *)&x->header.xfeatures);
116
117 return err;
118}
119
120static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
121{
122 int err;
123
124 if (use_xsave())
125 err = copy_xregs_to_user(buf);
126 else if (use_fxsr())
127 err = copy_fxregs_to_user((struct fxregs_state __user *) buf);
128 else
129 err = copy_fregs_to_user((struct fregs_state __user *) buf);
130
131 if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
132 err = -EFAULT;
133 return err;
134}
135
136/*
137 * Save the fpu, extended register state to the user signal frame.
138 *
139 * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
140 * state is copied.
141 * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
142 *
143 * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
144 * buf != buf_fx for 32-bit frames with fxstate.
145 *
146 * If the fpu, extended register state is live, save the state directly
147 * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise,
148 * copy the thread's fpu state to the user frame starting at 'buf_fx'.
149 *
150 * If this is a 32-bit frame with fxstate, put a fsave header before
151 * the aligned state at 'buf_fx'.
152 *
153 * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
154 * indicating the absence/presence of the extended state to the user.
155 */
156int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
157{
158 struct xregs_state *xsave = ¤t->thread.fpu.state.xsave;
159 struct task_struct *tsk = current;
160 int ia32_fxstate = (buf != buf_fx);
161
162 ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
163 IS_ENABLED(CONFIG_IA32_EMULATION));
164
165 if (!access_ok(VERIFY_WRITE, buf, size))
166 return -EACCES;
167
168 if (!static_cpu_has(X86_FEATURE_FPU))
169 return fpregs_soft_get(current, NULL, 0,
170 sizeof(struct user_i387_ia32_struct), NULL,
171 (struct _fpstate_32 __user *) buf) ? -1 : 1;
172
173 if (fpregs_active() || using_compacted_format()) {
174 /* Save the live register state to the user directly. */
175 if (copy_fpregs_to_sigframe(buf_fx))
176 return -1;
177 /* Update the thread's fxstate to save the fsave header. */
178 if (ia32_fxstate)
179 copy_fxregs_to_kernel(&tsk->thread.fpu);
180 } else {
181 /*
182 * It is a *bug* if kernel uses compacted-format for xsave
183 * area and we copy it out directly to a signal frame. It
184 * should have been handled above by saving the registers
185 * directly.
186 */
187 if (boot_cpu_has(X86_FEATURE_XSAVES)) {
188 WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
189 return -1;
190 }
191
192 fpstate_sanitize_xstate(&tsk->thread.fpu);
193 if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
194 return -1;
195 }
196
197 /* Save the fsave header for the 32-bit frames. */
198 if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
199 return -1;
200
201 if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
202 return -1;
203
204 return 0;
205}
206
207static inline void
208sanitize_restored_xstate(struct task_struct *tsk,
209 struct user_i387_ia32_struct *ia32_env,
210 u64 xfeatures, int fx_only)
211{
212 struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
213 struct xstate_header *header = &xsave->header;
214
215 if (use_xsave()) {
216 /* These bits must be zero. */
217 memset(header->reserved, 0, 48);
218
219 /*
220 * Init the state that is not present in the memory
221 * layout and not enabled by the OS.
222 */
223 if (fx_only)
224 header->xfeatures = XFEATURE_MASK_FPSSE;
225 else
226 header->xfeatures &= (xfeatures_mask & xfeatures);
227 }
228
229 if (use_fxsr()) {
230 /*
231 * mscsr reserved bits must be masked to zero for security
232 * reasons.
233 */
234 xsave->i387.mxcsr &= mxcsr_feature_mask;
235
236 convert_to_fxsr(tsk, ia32_env);
237 }
238}
239
240/*
241 * Restore the extended state if present. Otherwise, restore the FP/SSE state.
242 */
243static inline int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
244{
245 if (use_xsave()) {
246 if ((unsigned long)buf % 64 || fx_only) {
247 u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
248 copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
249 return copy_user_to_fxregs(buf);
250 } else {
251 u64 init_bv = xfeatures_mask & ~xbv;
252 if (unlikely(init_bv))
253 copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
254 return copy_user_to_xregs(buf, xbv);
255 }
256 } else if (use_fxsr()) {
257 return copy_user_to_fxregs(buf);
258 } else
259 return copy_user_to_fregs(buf);
260}
261
262static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
263{
264 int ia32_fxstate = (buf != buf_fx);
265 struct task_struct *tsk = current;
266 struct fpu *fpu = &tsk->thread.fpu;
267 int state_size = fpu_kernel_xstate_size;
268 u64 xfeatures = 0;
269 int fx_only = 0;
270
271 ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
272 IS_ENABLED(CONFIG_IA32_EMULATION));
273
274 if (!buf) {
275 fpu__clear(fpu);
276 return 0;
277 }
278
279 if (!access_ok(VERIFY_READ, buf, size))
280 return -EACCES;
281
282 fpu__activate_curr(fpu);
283
284 if (!static_cpu_has(X86_FEATURE_FPU))
285 return fpregs_soft_set(current, NULL,
286 0, sizeof(struct user_i387_ia32_struct),
287 NULL, buf) != 0;
288
289 if (use_xsave()) {
290 struct _fpx_sw_bytes fx_sw_user;
291 if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
292 /*
293 * Couldn't find the extended state information in the
294 * memory layout. Restore just the FP/SSE and init all
295 * the other extended state.
296 */
297 state_size = sizeof(struct fxregs_state);
298 fx_only = 1;
299 trace_x86_fpu_xstate_check_failed(fpu);
300 } else {
301 state_size = fx_sw_user.xstate_size;
302 xfeatures = fx_sw_user.xfeatures;
303 }
304 }
305
306 if (ia32_fxstate) {
307 /*
308 * For 32-bit frames with fxstate, copy the user state to the
309 * thread's fpu state, reconstruct fxstate from the fsave
310 * header. Sanitize the copied state etc.
311 */
312 struct fpu *fpu = &tsk->thread.fpu;
313 struct user_i387_ia32_struct env;
314 int err = 0;
315
316 /*
317 * Drop the current fpu which clears fpu->fpstate_active. This ensures
318 * that any context-switch during the copy of the new state,
319 * avoids the intermediate state from getting restored/saved.
320 * Thus avoiding the new restored state from getting corrupted.
321 * We will be ready to restore/save the state only after
322 * fpu->fpstate_active is again set.
323 */
324 fpu__drop(fpu);
325
326 if (using_compacted_format()) {
327 err = copyin_to_xsaves(NULL, buf_fx,
328 &fpu->state.xsave);
329 } else {
330 err = __copy_from_user(&fpu->state.xsave,
331 buf_fx, state_size);
332 }
333
334 if (err || __copy_from_user(&env, buf, sizeof(env))) {
335 fpstate_init(&fpu->state);
336 trace_x86_fpu_init_state(fpu);
337 err = -1;
338 } else {
339 sanitize_restored_xstate(tsk, &env, xfeatures, fx_only);
340 }
341
342 fpu->fpstate_active = 1;
343 preempt_disable();
344 fpu__restore(fpu);
345 preempt_enable();
346
347 return err;
348 } else {
349 /*
350 * For 64-bit frames and 32-bit fsave frames, restore the user
351 * state to the registers directly (with exceptions handled).
352 */
353 user_fpu_begin();
354 if (copy_user_to_fpregs_zeroing(buf_fx, xfeatures, fx_only)) {
355 fpu__clear(fpu);
356 return -1;
357 }
358 }
359
360 return 0;
361}
362
363static inline int xstate_sigframe_size(void)
364{
365 return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
366 fpu_user_xstate_size;
367}
368
369/*
370 * Restore FPU state from a sigframe:
371 */
372int fpu__restore_sig(void __user *buf, int ia32_frame)
373{
374 void __user *buf_fx = buf;
375 int size = xstate_sigframe_size();
376
377 if (ia32_frame && use_fxsr()) {
378 buf_fx = buf + sizeof(struct fregs_state);
379 size += sizeof(struct fregs_state);
380 }
381
382 return __fpu__restore_sig(buf, buf_fx, size);
383}
384
385unsigned long
386fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
387 unsigned long *buf_fx, unsigned long *size)
388{
389 unsigned long frame_size = xstate_sigframe_size();
390
391 *buf_fx = sp = round_down(sp - frame_size, 64);
392 if (ia32_frame && use_fxsr()) {
393 frame_size += sizeof(struct fregs_state);
394 sp -= sizeof(struct fregs_state);
395 }
396
397 *size = frame_size;
398
399 return sp;
400}
401/*
402 * Prepare the SW reserved portion of the fxsave memory layout, indicating
403 * the presence of the extended state information in the memory layout
404 * pointed by the fpstate pointer in the sigcontext.
405 * This will be saved when ever the FP and extended state context is
406 * saved on the user stack during the signal handler delivery to the user.
407 */
408void fpu__init_prepare_fx_sw_frame(void)
409{
410 int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
411
412 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
413 fx_sw_reserved.extended_size = size;
414 fx_sw_reserved.xfeatures = xfeatures_mask;
415 fx_sw_reserved.xstate_size = fpu_user_xstate_size;
416
417 if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
418 IS_ENABLED(CONFIG_X86_32)) {
419 int fsave_header_size = sizeof(struct fregs_state);
420
421 fx_sw_reserved_ia32 = fx_sw_reserved;
422 fx_sw_reserved_ia32.extended_size = size + fsave_header_size;
423 }
424}
425