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