1 // SPDX-License-Identifier: GPL-2.0
3 * FPU signal frame handling routines.
6 #include <linux/compat.h>
9 #include <asm/fpu/internal.h>
10 #include <asm/fpu/signal.h>
11 #include <asm/fpu/regset.h>
12 #include <asm/fpu/xstate.h>
14 #include <asm/sigframe.h>
15 #include <asm/trace/fpu.h>
17 static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
20 * Check for the presence of extended state information in the
21 * user fpstate pointer in the sigcontext.
23 static inline int check_for_xstate(struct fxregs_state __user *buf,
25 struct _fpx_sw_bytes *fx_sw)
27 int min_xstate_size = sizeof(struct fxregs_state) +
28 sizeof(struct xstate_header);
31 if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
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)
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.
47 if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
48 || magic2 != FP_XSTATE_MAGIC2)
55 * Signal frame handlers.
57 static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
60 struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
61 struct user_i387_ia32_struct env;
62 struct _fpstate_32 __user *fp = buf;
64 convert_from_fxsr(&env, tsk);
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))
71 struct fregs_state __user *fp = buf;
73 if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
80 static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
82 struct xregs_state __user *x = buf;
83 struct _fpx_sw_bytes *sw_bytes;
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));
94 err |= __put_user(FP_XSTATE_MAGIC2,
95 (__u32 *)(buf + fpu_user_xstate_size));
98 * Read the xfeatures which we copied (directly from the cpu or
99 * from the state in task struct) to the user buffers.
101 err |= __get_user(xfeatures, (__u32 *)&x->header.xfeatures);
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.
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.
114 xfeatures |= XFEATURE_MASK_FPSSE;
116 err |= __put_user(xfeatures, (__u32 *)&x->header.xfeatures);
121 static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
126 err = copy_xregs_to_user(buf);
128 err = copy_fxregs_to_user((struct fxregs_state __user *) buf);
130 err = copy_fregs_to_user((struct fregs_state __user *) buf);
132 if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
138 * Save the fpu, extended register state to the user signal frame.
140 * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
142 * 'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
144 * buf == buf_fx for 64-bit frames and 32-bit fsave frame.
145 * buf != buf_fx for 32-bit frames with fxstate.
147 * If the fpu, extended register state is live, save the state directly
148 * to the user frame pointed by the aligned pointer 'buf_fx'. Otherwise,
149 * copy the thread's fpu state to the user frame starting at 'buf_fx'.
151 * If this is a 32-bit frame with fxstate, put a fsave header before
152 * the aligned state at 'buf_fx'.
154 * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
155 * indicating the absence/presence of the extended state to the user.
157 int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
159 struct fpu *fpu = ¤t->thread.fpu;
160 struct xregs_state *xsave = &fpu->state.xsave;
161 struct task_struct *tsk = current;
162 int ia32_fxstate = (buf != buf_fx);
164 ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
165 IS_ENABLED(CONFIG_IA32_EMULATION));
167 if (!access_ok(VERIFY_WRITE, buf, size))
170 if (!static_cpu_has(X86_FEATURE_FPU))
171 return fpregs_soft_get(current, NULL, 0,
172 sizeof(struct user_i387_ia32_struct), NULL,
173 (struct _fpstate_32 __user *) buf) ? -1 : 1;
175 if (fpu->initialized || using_compacted_format()) {
176 /* Save the live register state to the user directly. */
177 if (copy_fpregs_to_sigframe(buf_fx))
179 /* Update the thread's fxstate to save the fsave header. */
181 copy_fxregs_to_kernel(fpu);
184 * It is a *bug* if kernel uses compacted-format for xsave
185 * area and we copy it out directly to a signal frame. It
186 * should have been handled above by saving the registers
189 if (boot_cpu_has(X86_FEATURE_XSAVES)) {
190 WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
194 fpstate_sanitize_xstate(fpu);
195 if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
199 /* Save the fsave header for the 32-bit frames. */
200 if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
203 if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
210 sanitize_restored_xstate(struct task_struct *tsk,
211 struct user_i387_ia32_struct *ia32_env,
212 u64 xfeatures, int fx_only)
214 struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
215 struct xstate_header *header = &xsave->header;
219 * Note: we don't need to zero the reserved bits in the
220 * xstate_header here because we either didn't copy them at all,
221 * or we checked earlier that they aren't set.
225 * Init the state that is not present in the memory
226 * layout and not enabled by the OS.
229 header->xfeatures = XFEATURE_MASK_FPSSE;
231 header->xfeatures &= xfeatures;
236 * mscsr reserved bits must be masked to zero for security
239 xsave->i387.mxcsr &= mxcsr_feature_mask;
241 convert_to_fxsr(tsk, ia32_env);
246 * Restore the extended state if present. Otherwise, restore the FP/SSE state.
248 static inline int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
251 if ((unsigned long)buf % 64 || fx_only) {
252 u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
253 copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
254 return copy_user_to_fxregs(buf);
256 u64 init_bv = xfeatures_mask & ~xbv;
257 if (unlikely(init_bv))
258 copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
259 return copy_user_to_xregs(buf, xbv);
261 } else if (use_fxsr()) {
262 return copy_user_to_fxregs(buf);
264 return copy_user_to_fregs(buf);
267 static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
269 int ia32_fxstate = (buf != buf_fx);
270 struct task_struct *tsk = current;
271 struct fpu *fpu = &tsk->thread.fpu;
272 int state_size = fpu_kernel_xstate_size;
277 ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
278 IS_ENABLED(CONFIG_IA32_EMULATION));
285 if (!access_ok(VERIFY_READ, buf, size)) {
290 fpu__initialize(fpu);
292 if (!static_cpu_has(X86_FEATURE_FPU)) {
293 ret = fpregs_soft_set(current, NULL,
294 0, sizeof(struct user_i387_ia32_struct),
302 struct _fpx_sw_bytes fx_sw_user;
303 if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
305 * Couldn't find the extended state information in the
306 * memory layout. Restore just the FP/SSE and init all
307 * the other extended state.
309 state_size = sizeof(struct fxregs_state);
311 trace_x86_fpu_xstate_check_failed(fpu);
313 state_size = fx_sw_user.xstate_size;
314 xfeatures = fx_sw_user.xfeatures;
320 * For 32-bit frames with fxstate, copy the user state to the
321 * thread's fpu state, reconstruct fxstate from the fsave
322 * header. Validate and sanitize the copied state.
324 struct user_i387_ia32_struct env;
328 * Drop the current fpu which clears fpu->initialized. This ensures
329 * that any context-switch during the copy of the new state,
330 * avoids the intermediate state from getting restored/saved.
331 * Thus avoiding the new restored state from getting corrupted.
332 * We will be ready to restore/save the state only after
333 * fpu->initialized is again set.
337 if (using_compacted_format()) {
338 err = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
340 err = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
342 if (!err && state_size > offsetof(struct xregs_state, header))
343 err = validate_xstate_header(&fpu->state.xsave.header);
346 if (err || __copy_from_user(&env, buf, sizeof(env))) {
347 fpstate_init(&fpu->state);
348 trace_x86_fpu_init_state(fpu);
351 sanitize_restored_xstate(tsk, &env, xfeatures, fx_only);
355 fpu->initialized = 1;
359 /* Failure is already handled */
363 * For 64-bit frames and 32-bit fsave frames, restore the user
364 * state to the registers directly (with exceptions handled).
367 if (!copy_user_to_fpregs_zeroing(buf_fx, xfeatures, fx_only))
377 static inline int xstate_sigframe_size(void)
379 return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
380 fpu_user_xstate_size;
384 * Restore FPU state from a sigframe:
386 int fpu__restore_sig(void __user *buf, int ia32_frame)
388 void __user *buf_fx = buf;
389 int size = xstate_sigframe_size();
391 if (ia32_frame && use_fxsr()) {
392 buf_fx = buf + sizeof(struct fregs_state);
393 size += sizeof(struct fregs_state);
396 return __fpu__restore_sig(buf, buf_fx, size);
400 fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
401 unsigned long *buf_fx, unsigned long *size)
403 unsigned long frame_size = xstate_sigframe_size();
405 *buf_fx = sp = round_down(sp - frame_size, 64);
406 if (ia32_frame && use_fxsr()) {
407 frame_size += sizeof(struct fregs_state);
408 sp -= sizeof(struct fregs_state);
416 * Prepare the SW reserved portion of the fxsave memory layout, indicating
417 * the presence of the extended state information in the memory layout
418 * pointed by the fpstate pointer in the sigcontext.
419 * This will be saved when ever the FP and extended state context is
420 * saved on the user stack during the signal handler delivery to the user.
422 void fpu__init_prepare_fx_sw_frame(void)
424 int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
426 fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
427 fx_sw_reserved.extended_size = size;
428 fx_sw_reserved.xfeatures = xfeatures_mask;
429 fx_sw_reserved.xstate_size = fpu_user_xstate_size;
431 if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
432 IS_ENABLED(CONFIG_X86_32)) {
433 int fsave_header_size = sizeof(struct fregs_state);
435 fx_sw_reserved_ia32 = fx_sw_reserved;
436 fx_sw_reserved_ia32.extended_size = size + fsave_header_size;