6df1f15e0cd5cac51c9df181082a1bd2d8e6844d
[linux-2.6-block.git] / arch / x86 / kernel / fpu / signal.c
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
9 #include <asm/fpu/internal.h>
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/trace/fpu.h>
16
17 static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
18
19 /*
20  * Check for the presence of extended state information in the
21  * user fpstate pointer in the sigcontext.
22  */
23 static inline int check_for_xstate(struct fxregs_state __user *buf,
24                                    void __user *fpstate,
25                                    struct _fpx_sw_bytes *fx_sw)
26 {
27         int min_xstate_size = sizeof(struct fxregs_state) +
28                               sizeof(struct xstate_header);
29         unsigned int magic2;
30
31         if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
32                 return -1;
33
34         /* Check for the first magic field and other error scenarios. */
35         if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
36             fx_sw->xstate_size < min_xstate_size ||
37             fx_sw->xstate_size > fpu_user_xstate_size ||
38             fx_sw->xstate_size > fx_sw->extended_size)
39                 return -1;
40
41         /*
42          * Check for the presence of second magic word at the end of memory
43          * layout. This detects the case where the user just copied the legacy
44          * fpstate layout with out copying the extended state information
45          * in the memory layout.
46          */
47         if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
48             || magic2 != FP_XSTATE_MAGIC2)
49                 return -1;
50
51         return 0;
52 }
53
54 /*
55  * Signal frame handlers.
56  */
57 static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
58 {
59         if (use_fxsr()) {
60                 struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
61                 struct user_i387_ia32_struct env;
62                 struct _fpstate_32 __user *fp = buf;
63
64                 convert_from_fxsr(&env, tsk);
65
66                 if (__copy_to_user(buf, &env, sizeof(env)) ||
67                     __put_user(xsave->i387.swd, &fp->status) ||
68                     __put_user(X86_FXSR_MAGIC, &fp->magic))
69                         return -1;
70         } else {
71                 struct fregs_state __user *fp = buf;
72                 u32 swd;
73                 if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
74                         return -1;
75         }
76
77         return 0;
78 }
79
80 static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
81 {
82         struct xregs_state __user *x = buf;
83         struct _fpx_sw_bytes *sw_bytes;
84         u32 xfeatures;
85         int err;
86
87         /* Setup the bytes not touched by the [f]xsave and reserved for SW. */
88         sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved;
89         err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes));
90
91         if (!use_xsave())
92                 return err;
93
94         err |= __put_user(FP_XSTATE_MAGIC2,
95                           (__u32 __user *)(buf + fpu_user_xstate_size));
96
97         /*
98          * Read the xfeatures which we copied (directly from the cpu or
99          * from the state in task struct) to the user buffers.
100          */
101         err |= __get_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
102
103         /*
104          * For legacy compatible, we always set FP/SSE bits in the bit
105          * vector while saving the state to the user context. This will
106          * enable us capturing any changes(during sigreturn) to
107          * the FP/SSE bits by the legacy applications which don't touch
108          * xfeatures in the xsave header.
109          *
110          * xsave aware apps can change the xfeatures in the xsave
111          * header as well as change any contents in the memory layout.
112          * xrestore as part of sigreturn will capture all the changes.
113          */
114         xfeatures |= XFEATURE_MASK_FPSSE;
115
116         err |= __put_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
117
118         return err;
119 }
120
121 static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
122 {
123         int err;
124
125         if (use_xsave())
126                 err = copy_xregs_to_user(buf);
127         else if (use_fxsr())
128                 err = copy_fxregs_to_user((struct fxregs_state __user *) buf);
129         else
130                 err = copy_fregs_to_user((struct fregs_state __user *) buf);
131
132         if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
133                 err = -EFAULT;
134         return err;
135 }
136
137 /*
138  * Save the fpu, extended register state to the user signal frame.
139  *
140  * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
141  *  state is copied.
142  *  'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
143  *
144  *      buf == buf_fx for 64-bit frames and 32-bit fsave frame.
145  *      buf != buf_fx for 32-bit frames with fxstate.
146  *
147  * Save the state to task's fpu->state and then copy it to the user frame
148  * pointed to by the aligned pointer '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  */
156 int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
157 {
158         struct fpu *fpu = &current->thread.fpu;
159         struct xregs_state *xsave = &fpu->state.xsave;
160         struct task_struct *tsk = current;
161         int ia32_fxstate = (buf != buf_fx);
162
163         ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
164                          IS_ENABLED(CONFIG_IA32_EMULATION));
165
166         if (!access_ok(buf, size))
167                 return -EACCES;
168
169         if (!static_cpu_has(X86_FEATURE_FPU))
170                 return fpregs_soft_get(current, NULL, 0,
171                         sizeof(struct user_i387_ia32_struct), NULL,
172                         (struct _fpstate_32 __user *) buf) ? -1 : 1;
173
174         /*
175          * If we do not need to load the FPU registers at return to userspace
176          * then the CPU has the current state and we need to save it. Otherwise,
177          * it has already been done and we can skip it.
178          */
179         fpregs_lock();
180         if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
181                 copy_fpregs_to_fpstate(fpu);
182                 set_thread_flag(TIF_NEED_FPU_LOAD);
183         }
184         fpregs_unlock();
185
186         if (using_compacted_format()) {
187                 if (copy_xstate_to_user(buf_fx, xsave, 0, size))
188                         return -1;
189         } else {
190                 fpstate_sanitize_xstate(fpu);
191                 if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
192                         return -1;
193         }
194
195         /* Save the fsave header for the 32-bit frames. */
196         if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
197                 return -1;
198
199         if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
200                 return -1;
201
202         return 0;
203 }
204
205 static inline void
206 sanitize_restored_xstate(union fpregs_state *state,
207                          struct user_i387_ia32_struct *ia32_env,
208                          u64 xfeatures, int fx_only)
209 {
210         struct xregs_state *xsave = &state->xsave;
211         struct xstate_header *header = &xsave->header;
212
213         if (use_xsave()) {
214                 /*
215                  * Note: we don't need to zero the reserved bits in the
216                  * xstate_header here because we either didn't copy them at all,
217                  * or we checked earlier that they aren't set.
218                  */
219
220                 /*
221                  * Init the state that is not present in the memory
222                  * layout and not enabled by the OS.
223                  */
224                 if (fx_only)
225                         header->xfeatures = XFEATURE_MASK_FPSSE;
226                 else
227                         header->xfeatures &= xfeatures;
228         }
229
230         if (use_fxsr()) {
231                 /*
232                  * mscsr reserved bits must be masked to zero for security
233                  * reasons.
234                  */
235                 xsave->i387.mxcsr &= mxcsr_feature_mask;
236
237                 if (ia32_env)
238                         convert_to_fxsr(&state->fxsave, ia32_env);
239         }
240 }
241
242 /*
243  * Restore the extended state if present. Otherwise, restore the FP/SSE state.
244  */
245 static inline int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
246 {
247         if (use_xsave()) {
248                 if ((unsigned long)buf % 64 || fx_only) {
249                         u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
250                         copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
251                         return copy_user_to_fxregs(buf);
252                 } else {
253                         u64 init_bv = xfeatures_mask & ~xbv;
254                         if (unlikely(init_bv))
255                                 copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
256                         return copy_user_to_xregs(buf, xbv);
257                 }
258         } else if (use_fxsr()) {
259                 return copy_user_to_fxregs(buf);
260         } else
261                 return copy_user_to_fregs(buf);
262 }
263
264 static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
265 {
266         struct user_i387_ia32_struct *envp = NULL;
267         int state_size = fpu_kernel_xstate_size;
268         int ia32_fxstate = (buf != buf_fx);
269         struct task_struct *tsk = current;
270         struct fpu *fpu = &tsk->thread.fpu;
271         struct user_i387_ia32_struct env;
272         u64 xfeatures = 0;
273         int fx_only = 0;
274         int ret = 0;
275
276         ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
277                          IS_ENABLED(CONFIG_IA32_EMULATION));
278
279         if (!buf) {
280                 fpu__clear(fpu);
281                 return 0;
282         }
283
284         if (!access_ok(buf, size))
285                 return -EACCES;
286
287         if (!static_cpu_has(X86_FEATURE_FPU))
288                 return fpregs_soft_set(current, NULL,
289                                        0, sizeof(struct user_i387_ia32_struct),
290                                        NULL, buf) != 0;
291
292         if (use_xsave()) {
293                 struct _fpx_sw_bytes fx_sw_user;
294                 if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
295                         /*
296                          * Couldn't find the extended state information in the
297                          * memory layout. Restore just the FP/SSE and init all
298                          * the other extended state.
299                          */
300                         state_size = sizeof(struct fxregs_state);
301                         fx_only = 1;
302                         trace_x86_fpu_xstate_check_failed(fpu);
303                 } else {
304                         state_size = fx_sw_user.xstate_size;
305                         xfeatures = fx_sw_user.xfeatures;
306                 }
307         }
308
309         /*
310          * The current state of the FPU registers does not matter. By setting
311          * TIF_NEED_FPU_LOAD unconditionally it is ensured that the our xstate
312          * is not modified on context switch and that the xstate is considered
313          * to be loaded again on return to userland (overriding last_cpu avoids
314          * the optimisation).
315          */
316         set_thread_flag(TIF_NEED_FPU_LOAD);
317         __fpu_invalidate_fpregs_state(fpu);
318
319         if ((unsigned long)buf_fx % 64)
320                 fx_only = 1;
321         /*
322          * For 32-bit frames with fxstate, copy the fxstate so it can be
323          * reconstructed later.
324          */
325         if (ia32_fxstate) {
326                 ret = __copy_from_user(&env, buf, sizeof(env));
327                 if (ret)
328                         goto err_out;
329                 envp = &env;
330         }
331
332         if (use_xsave() && !fx_only) {
333                 u64 init_bv = xfeatures_mask & ~xfeatures;
334
335                 if (using_compacted_format()) {
336                         ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
337                 } else {
338                         ret = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
339
340                         if (!ret && state_size > offsetof(struct xregs_state, header))
341                                 ret = validate_xstate_header(&fpu->state.xsave.header);
342                 }
343                 if (ret)
344                         goto err_out;
345
346                 sanitize_restored_xstate(&fpu->state, envp, xfeatures, fx_only);
347
348                 fpregs_lock();
349                 if (unlikely(init_bv))
350                         copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
351                 ret = copy_kernel_to_xregs_err(&fpu->state.xsave, xfeatures);
352
353         } else if (use_fxsr()) {
354                 ret = __copy_from_user(&fpu->state.fxsave, buf_fx, state_size);
355                 if (ret) {
356                         ret = -EFAULT;
357                         goto err_out;
358                 }
359
360                 sanitize_restored_xstate(&fpu->state, envp, xfeatures, fx_only);
361
362                 fpregs_lock();
363                 if (use_xsave()) {
364                         u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
365                         copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
366                 }
367
368                 ret = copy_kernel_to_fxregs_err(&fpu->state.fxsave);
369         } else {
370                 ret = __copy_from_user(&fpu->state.fsave, buf_fx, state_size);
371                 if (ret)
372                         goto err_out;
373
374                 fpregs_lock();
375                 ret = copy_kernel_to_fregs_err(&fpu->state.fsave);
376         }
377         if (!ret)
378                 fpregs_mark_activate();
379         fpregs_unlock();
380
381 err_out:
382         if (ret)
383                 fpu__clear(fpu);
384         return ret;
385 }
386
387 static inline int xstate_sigframe_size(void)
388 {
389         return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
390                         fpu_user_xstate_size;
391 }
392
393 /*
394  * Restore FPU state from a sigframe:
395  */
396 int fpu__restore_sig(void __user *buf, int ia32_frame)
397 {
398         void __user *buf_fx = buf;
399         int size = xstate_sigframe_size();
400
401         if (ia32_frame && use_fxsr()) {
402                 buf_fx = buf + sizeof(struct fregs_state);
403                 size += sizeof(struct fregs_state);
404         }
405
406         return __fpu__restore_sig(buf, buf_fx, size);
407 }
408
409 unsigned long
410 fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
411                      unsigned long *buf_fx, unsigned long *size)
412 {
413         unsigned long frame_size = xstate_sigframe_size();
414
415         *buf_fx = sp = round_down(sp - frame_size, 64);
416         if (ia32_frame && use_fxsr()) {
417                 frame_size += sizeof(struct fregs_state);
418                 sp -= sizeof(struct fregs_state);
419         }
420
421         *size = frame_size;
422
423         return sp;
424 }
425 /*
426  * Prepare the SW reserved portion of the fxsave memory layout, indicating
427  * the presence of the extended state information in the memory layout
428  * pointed by the fpstate pointer in the sigcontext.
429  * This will be saved when ever the FP and extended state context is
430  * saved on the user stack during the signal handler delivery to the user.
431  */
432 void fpu__init_prepare_fx_sw_frame(void)
433 {
434         int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
435
436         fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
437         fx_sw_reserved.extended_size = size;
438         fx_sw_reserved.xfeatures = xfeatures_mask;
439         fx_sw_reserved.xstate_size = fpu_user_xstate_size;
440
441         if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
442             IS_ENABLED(CONFIG_X86_32)) {
443                 int fsave_header_size = sizeof(struct fregs_state);
444
445                 fx_sw_reserved_ia32 = fx_sw_reserved;
446                 fx_sw_reserved_ia32.extended_size = size + fsave_header_size;
447         }
448 }
449