Linux Audio

Check our new training course

Loading...
v4.17
  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
 17static 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 */
 23static 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 */
 57static 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
 80static 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 *)(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 *)&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 *)&x->header.xfeatures);
117
118	return err;
119}
120
121static 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 * 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'.
 
150 *
151 * If this is a 32-bit frame with fxstate, put a fsave header before
152 * the aligned state at 'buf_fx'.
153 *
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.
156 */
157int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
158{
159	struct fpu *fpu = &current->thread.fpu;
160	struct xregs_state *xsave = &fpu->state.xsave;
161	struct task_struct *tsk = current;
162	int ia32_fxstate = (buf != buf_fx);
 
163
164	ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
165			 IS_ENABLED(CONFIG_IA32_EMULATION));
166
167	if (!access_ok(VERIFY_WRITE, buf, size))
168		return -EACCES;
169
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;
174
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))
178			return -1;
179		/* Update the thread's fxstate to save the fsave header. */
180		if (ia32_fxstate)
181			copy_fxregs_to_kernel(fpu);
182	} else {
183		/*
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
187		 * directly.
188		 */
189		if (boot_cpu_has(X86_FEATURE_XSAVES)) {
190			WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
191			return -1;
192		}
193
194		fpstate_sanitize_xstate(fpu);
195		if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
196			return -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197	}
198
199	/* Save the fsave header for the 32-bit frames. */
200	if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
201		return -1;
202
203	if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
204		return -1;
205
206	return 0;
207}
208
209static inline void
210sanitize_restored_xstate(struct task_struct *tsk,
211			 struct user_i387_ia32_struct *ia32_env,
212			 u64 xfeatures, int fx_only)
213{
214	struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
215	struct xstate_header *header = &xsave->header;
216
217	if (use_xsave()) {
218		/*
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.
222		 */
223
224		/*
225		 * Init the state that is not present in the memory
226		 * layout and not enabled by the OS.
 
 
 
 
 
 
 
 
227		 */
228		if (fx_only)
229			header->xfeatures = XFEATURE_MASK_FPSSE;
230		else
231			header->xfeatures &= xfeatures;
 
232	}
233
234	if (use_fxsr()) {
235		/*
236		 * mscsr reserved bits must be masked to zero for security
237		 * reasons.
238		 */
239		xsave->i387.mxcsr &= mxcsr_feature_mask;
240
241		convert_to_fxsr(tsk, ia32_env);
 
242	}
243}
244
245/*
246 * Restore the extended state if present. Otherwise, restore the FP/SSE state.
247 */
248static inline int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
249{
 
 
 
250	if (use_xsave()) {
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);
 
 
 
255		} else {
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);
260		}
261	} else if (use_fxsr()) {
262		return copy_user_to_fxregs(buf);
263	} else
264		return copy_user_to_fregs(buf);
265}
266
267static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
268{
 
 
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;
273	u64 xfeatures = 0;
274	int fx_only = 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(VERIFY_READ, buf, size))
285		return -EACCES;
286
287	fpu__initialize(fpu);
288
289	if (!static_cpu_has(X86_FEATURE_FPU))
290		return fpregs_soft_set(current, NULL,
291				       0, sizeof(struct user_i387_ia32_struct),
292				       NULL, buf) != 0;
293
294	if (use_xsave()) {
295		struct _fpx_sw_bytes fx_sw_user;
296		if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
297			/*
298			 * Couldn't find the extended state information in the
299			 * memory layout. Restore just the FP/SSE and init all
300			 * the other extended state.
301			 */
302			state_size = sizeof(struct fxregs_state);
303			fx_only = 1;
304			trace_x86_fpu_xstate_check_failed(fpu);
305		} else {
306			state_size = fx_sw_user.xstate_size;
307			xfeatures = fx_sw_user.xfeatures;
308		}
309	}
310
311	if (ia32_fxstate) {
 
 
 
312		/*
313		 * For 32-bit frames with fxstate, copy the user state to the
314		 * thread's fpu state, reconstruct fxstate from the fsave
315		 * header. Validate and sanitize the copied state.
 
 
316		 */
317		struct fpu *fpu = &tsk->thread.fpu;
318		struct user_i387_ia32_struct env;
319		int err = 0;
 
 
320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321		/*
322		 * Drop the current fpu which clears fpu->initialized. This ensures
323		 * that any context-switch during the copy of the new state,
324		 * avoids the intermediate state from getting restored/saved.
325		 * Thus avoiding the new restored state from getting corrupted.
326		 * We will be ready to restore/save the state only after
327		 * fpu->initialized is again set.
328		 */
329		fpu__drop(fpu);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
331		if (using_compacted_format()) {
332			err = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
333		} else {
334			err = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
335
336			if (!err && state_size > offsetof(struct xregs_state, header))
337				err = validate_xstate_header(&fpu->state.xsave.header);
338		}
 
 
339
340		if (err || __copy_from_user(&env, buf, sizeof(env))) {
341			fpstate_init(&fpu->state);
342			trace_x86_fpu_init_state(fpu);
343			err = -1;
344		} else {
345			sanitize_restored_xstate(tsk, &env, xfeatures, fx_only);
346		}
347
348		fpu->initialized = 1;
349		preempt_disable();
350		fpu__restore(fpu);
351		preempt_enable();
352
353		return err;
354	} else {
355		/*
356		 * For 64-bit frames and 32-bit fsave frames, restore the user
357		 * state to the registers directly (with exceptions handled).
358		 */
359		user_fpu_begin();
360		if (copy_user_to_fpregs_zeroing(buf_fx, xfeatures, fx_only)) {
361			fpu__clear(fpu);
362			return -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363		}
 
 
 
 
 
 
 
 
 
364	}
 
 
 
 
 
365
366	return 0;
 
 
 
367}
368
369static inline int xstate_sigframe_size(void)
370{
371	return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
372			fpu_user_xstate_size;
373}
374
375/*
376 * Restore FPU state from a sigframe:
377 */
378int fpu__restore_sig(void __user *buf, int ia32_frame)
379{
380	void __user *buf_fx = buf;
381	int size = xstate_sigframe_size();
382
383	if (ia32_frame && use_fxsr()) {
384		buf_fx = buf + sizeof(struct fregs_state);
385		size += sizeof(struct fregs_state);
386	}
387
388	return __fpu__restore_sig(buf, buf_fx, size);
389}
390
391unsigned long
392fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
393		     unsigned long *buf_fx, unsigned long *size)
394{
395	unsigned long frame_size = xstate_sigframe_size();
396
397	*buf_fx = sp = round_down(sp - frame_size, 64);
398	if (ia32_frame && use_fxsr()) {
399		frame_size += sizeof(struct fregs_state);
400		sp -= sizeof(struct fregs_state);
401	}
402
403	*size = frame_size;
404
405	return sp;
406}
407/*
408 * Prepare the SW reserved portion of the fxsave memory layout, indicating
409 * the presence of the extended state information in the memory layout
410 * pointed by the fpstate pointer in the sigcontext.
411 * This will be saved when ever the FP and extended state context is
412 * saved on the user stack during the signal handler delivery to the user.
413 */
414void fpu__init_prepare_fx_sw_frame(void)
415{
416	int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
417
418	fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
419	fx_sw_reserved.extended_size = size;
420	fx_sw_reserved.xfeatures = xfeatures_mask;
421	fx_sw_reserved.xstate_size = fpu_user_xstate_size;
422
423	if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
424	    IS_ENABLED(CONFIG_X86_32)) {
425		int fsave_header_size = sizeof(struct fregs_state);
426
427		fx_sw_reserved_ia32 = fx_sw_reserved;
428		fx_sw_reserved_ia32.extended_size = size + fsave_header_size;
429	}
430}
431
v5.9
  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
 18static struct _fpx_sw_bytes fx_sw_reserved, fx_sw_reserved_ia32;
 19
 20/*
 21 * Check for the presence of extended state information in the
 22 * user fpstate pointer in the sigcontext.
 23 */
 24static inline int check_for_xstate(struct fxregs_state __user *buf,
 25				   void __user *fpstate,
 26				   struct _fpx_sw_bytes *fx_sw)
 27{
 28	int min_xstate_size = sizeof(struct fxregs_state) +
 29			      sizeof(struct xstate_header);
 30	unsigned int magic2;
 31
 32	if (__copy_from_user(fx_sw, &buf->sw_reserved[0], sizeof(*fx_sw)))
 33		return -1;
 34
 35	/* Check for the first magic field and other error scenarios. */
 36	if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
 37	    fx_sw->xstate_size < min_xstate_size ||
 38	    fx_sw->xstate_size > fpu_user_xstate_size ||
 39	    fx_sw->xstate_size > fx_sw->extended_size)
 40		return -1;
 41
 42	/*
 43	 * Check for the presence of second magic word at the end of memory
 44	 * layout. This detects the case where the user just copied the legacy
 45	 * fpstate layout with out copying the extended state information
 46	 * in the memory layout.
 47	 */
 48	if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size))
 49	    || magic2 != FP_XSTATE_MAGIC2)
 50		return -1;
 51
 52	return 0;
 53}
 54
 55/*
 56 * Signal frame handlers.
 57 */
 58static inline int save_fsave_header(struct task_struct *tsk, void __user *buf)
 59{
 60	if (use_fxsr()) {
 61		struct xregs_state *xsave = &tsk->thread.fpu.state.xsave;
 62		struct user_i387_ia32_struct env;
 63		struct _fpstate_32 __user *fp = buf;
 64
 65		fpregs_lock();
 66		if (!test_thread_flag(TIF_NEED_FPU_LOAD))
 67			copy_fxregs_to_kernel(&tsk->thread.fpu);
 68		fpregs_unlock();
 69
 70		convert_from_fxsr(&env, tsk);
 71
 72		if (__copy_to_user(buf, &env, sizeof(env)) ||
 73		    __put_user(xsave->i387.swd, &fp->status) ||
 74		    __put_user(X86_FXSR_MAGIC, &fp->magic))
 75			return -1;
 76	} else {
 77		struct fregs_state __user *fp = buf;
 78		u32 swd;
 79		if (__get_user(swd, &fp->swd) || __put_user(swd, &fp->status))
 80			return -1;
 81	}
 82
 83	return 0;
 84}
 85
 86static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
 87{
 88	struct xregs_state __user *x = buf;
 89	struct _fpx_sw_bytes *sw_bytes;
 90	u32 xfeatures;
 91	int err;
 92
 93	/* Setup the bytes not touched by the [f]xsave and reserved for SW. */
 94	sw_bytes = ia32_frame ? &fx_sw_reserved_ia32 : &fx_sw_reserved;
 95	err = __copy_to_user(&x->i387.sw_reserved, sw_bytes, sizeof(*sw_bytes));
 96
 97	if (!use_xsave())
 98		return err;
 99
100	err |= __put_user(FP_XSTATE_MAGIC2,
101			  (__u32 __user *)(buf + fpu_user_xstate_size));
102
103	/*
104	 * Read the xfeatures which we copied (directly from the cpu or
105	 * from the state in task struct) to the user buffers.
106	 */
107	err |= __get_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
108
109	/*
110	 * For legacy compatible, we always set FP/SSE bits in the bit
111	 * vector while saving the state to the user context. This will
112	 * enable us capturing any changes(during sigreturn) to
113	 * the FP/SSE bits by the legacy applications which don't touch
114	 * xfeatures in the xsave header.
115	 *
116	 * xsave aware apps can change the xfeatures in the xsave
117	 * header as well as change any contents in the memory layout.
118	 * xrestore as part of sigreturn will capture all the changes.
119	 */
120	xfeatures |= XFEATURE_MASK_FPSSE;
121
122	err |= __put_user(xfeatures, (__u32 __user *)&x->header.xfeatures);
123
124	return err;
125}
126
127static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
128{
129	int err;
130
131	if (use_xsave())
132		err = copy_xregs_to_user(buf);
133	else if (use_fxsr())
134		err = copy_fxregs_to_user((struct fxregs_state __user *) buf);
135	else
136		err = copy_fregs_to_user((struct fregs_state __user *) buf);
137
138	if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
139		err = -EFAULT;
140	return err;
141}
142
143/*
144 * Save the fpu, extended register state to the user signal frame.
145 *
146 * 'buf_fx' is the 64-byte aligned pointer at which the [f|fx|x]save
147 *  state is copied.
148 *  'buf' points to the 'buf_fx' or to the fsave header followed by 'buf_fx'.
149 *
150 *	buf == buf_fx for 64-bit frames and 32-bit fsave frame.
151 *	buf != buf_fx for 32-bit frames with fxstate.
152 *
153 * Try to save it directly to the user frame with disabled page fault handler.
154 * If this fails then do the slow path where the FPU state is first saved to
155 * task's fpu->state and then copy it to the user frame pointed to by the
156 * aligned pointer 'buf_fx'.
157 *
158 * If this is a 32-bit frame with fxstate, put a fsave header before
159 * the aligned state at 'buf_fx'.
160 *
161 * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
162 * indicating the absence/presence of the extended state to the user.
163 */
164int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
165{
 
 
166	struct task_struct *tsk = current;
167	int ia32_fxstate = (buf != buf_fx);
168	int ret;
169
170	ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
171			 IS_ENABLED(CONFIG_IA32_EMULATION));
172
173	if (!static_cpu_has(X86_FEATURE_FPU)) {
174		struct user_i387_ia32_struct fp;
175		fpregs_soft_get(current, NULL, (struct membuf){.p = &fp,
176						.left = sizeof(fp)});
177		return copy_to_user(buf, &fp, sizeof(fp)) ? -EFAULT : 0;
178	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
180	if (!access_ok(buf, size))
181		return -EACCES;
182retry:
183	/*
184	 * Load the FPU registers if they are not valid for the current task.
185	 * With a valid FPU state we can attempt to save the state directly to
186	 * userland's stack frame which will likely succeed. If it does not,
187	 * resolve the fault in the user memory and try again.
188	 */
189	fpregs_lock();
190	if (test_thread_flag(TIF_NEED_FPU_LOAD))
191		__fpregs_load_activate();
192
193	pagefault_disable();
194	ret = copy_fpregs_to_sigframe(buf_fx);
195	pagefault_enable();
196	fpregs_unlock();
197
198	if (ret) {
199		if (!fault_in_pages_writeable(buf_fx, fpu_user_xstate_size))
200			goto retry;
201		return -EFAULT;
202	}
203
204	/* Save the fsave header for the 32-bit frames. */
205	if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
206		return -1;
207
208	if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
209		return -1;
210
211	return 0;
212}
213
214static inline void
215sanitize_restored_user_xstate(union fpregs_state *state,
216			      struct user_i387_ia32_struct *ia32_env,
217			      u64 user_xfeatures, int fx_only)
218{
219	struct xregs_state *xsave = &state->xsave;
220	struct xstate_header *header = &xsave->header;
221
222	if (use_xsave()) {
223		/*
224		 * Note: we don't need to zero the reserved bits in the
225		 * xstate_header here because we either didn't copy them at all,
226		 * or we checked earlier that they aren't set.
227		 */
228
229		/*
230		 * 'user_xfeatures' might have bits clear which are
231		 * set in header->xfeatures. This represents features that
232		 * were in init state prior to a signal delivery, and need
233		 * to be reset back to the init state.  Clear any user
234		 * feature bits which are set in the kernel buffer to get
235		 * them back to the init state.
236		 *
237		 * Supervisor state is unchanged by input from userspace.
238		 * Ensure supervisor state bits stay set and supervisor
239		 * state is not modified.
240		 */
241		if (fx_only)
242			header->xfeatures = XFEATURE_MASK_FPSSE;
243		else
244			header->xfeatures &= user_xfeatures |
245					     xfeatures_mask_supervisor();
246	}
247
248	if (use_fxsr()) {
249		/*
250		 * mscsr reserved bits must be masked to zero for security
251		 * reasons.
252		 */
253		xsave->i387.mxcsr &= mxcsr_feature_mask;
254
255		if (ia32_env)
256			convert_to_fxsr(&state->fxsave, ia32_env);
257	}
258}
259
260/*
261 * Restore the extended state if present. Otherwise, restore the FP/SSE state.
262 */
263static int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
264{
265	u64 init_bv;
266	int r;
267
268	if (use_xsave()) {
269		if (fx_only) {
270			init_bv = xfeatures_mask_user() & ~XFEATURE_MASK_FPSSE;
271
272			r = copy_user_to_fxregs(buf);
273			if (!r)
274				copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
275			return r;
276		} else {
277			init_bv = xfeatures_mask_user() & ~xbv;
278
279			r = copy_user_to_xregs(buf, xbv);
280			if (!r && unlikely(init_bv))
281				copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
282			return r;
283		}
284	} else if (use_fxsr()) {
285		return copy_user_to_fxregs(buf);
286	} else
287		return copy_user_to_fregs(buf);
288}
289
290static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
291{
292	struct user_i387_ia32_struct *envp = NULL;
293	int state_size = fpu_kernel_xstate_size;
294	int ia32_fxstate = (buf != buf_fx);
295	struct task_struct *tsk = current;
296	struct fpu *fpu = &tsk->thread.fpu;
297	struct user_i387_ia32_struct env;
298	u64 user_xfeatures = 0;
299	int fx_only = 0;
300	int ret = 0;
301
302	ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
303			 IS_ENABLED(CONFIG_IA32_EMULATION));
304
305	if (!buf) {
306		fpu__clear_user_states(fpu);
307		return 0;
308	}
309
310	if (!access_ok(buf, size))
311		return -EACCES;
312
 
 
313	if (!static_cpu_has(X86_FEATURE_FPU))
314		return fpregs_soft_set(current, NULL,
315				       0, sizeof(struct user_i387_ia32_struct),
316				       NULL, buf) != 0;
317
318	if (use_xsave()) {
319		struct _fpx_sw_bytes fx_sw_user;
320		if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
321			/*
322			 * Couldn't find the extended state information in the
323			 * memory layout. Restore just the FP/SSE and init all
324			 * the other extended state.
325			 */
326			state_size = sizeof(struct fxregs_state);
327			fx_only = 1;
328			trace_x86_fpu_xstate_check_failed(fpu);
329		} else {
330			state_size = fx_sw_user.xstate_size;
331			user_xfeatures = fx_sw_user.xfeatures;
332		}
333	}
334
335	if ((unsigned long)buf_fx % 64)
336		fx_only = 1;
337
338	if (!ia32_fxstate) {
339		/*
340		 * Attempt to restore the FPU registers directly from user
341		 * memory. For that to succeed, the user access cannot cause
342		 * page faults. If it does, fall back to the slow path below,
343		 * going through the kernel buffer with the enabled pagefault
344		 * handler.
345		 */
346		fpregs_lock();
347		pagefault_disable();
348		ret = copy_user_to_fpregs_zeroing(buf_fx, user_xfeatures, fx_only);
349		pagefault_enable();
350		if (!ret) {
351
352			/*
353			 * Restore supervisor states: previous context switch
354			 * etc has done XSAVES and saved the supervisor states
355			 * in the kernel buffer from which they can be restored
356			 * now.
357			 *
358			 * We cannot do a single XRSTORS here - which would
359			 * be nice - because the rest of the FPU registers are
360			 * being restored from a user buffer directly. The
361			 * single XRSTORS happens below, when the user buffer
362			 * has been copied to the kernel one.
363			 */
364			if (test_thread_flag(TIF_NEED_FPU_LOAD) &&
365			    xfeatures_mask_supervisor())
366				copy_kernel_to_xregs(&fpu->state.xsave,
367						     xfeatures_mask_supervisor());
368			fpregs_mark_activate();
369			fpregs_unlock();
370			return 0;
371		}
372		fpregs_unlock();
373	} else {
374		/*
375		 * For 32-bit frames with fxstate, copy the fxstate so it can
376		 * be reconstructed later.
 
 
 
 
377		 */
378		ret = __copy_from_user(&env, buf, sizeof(env));
379		if (ret)
380			goto err_out;
381		envp = &env;
382	}
383
384	/*
385	 * By setting TIF_NEED_FPU_LOAD it is ensured that our xstate is
386	 * not modified on context switch and that the xstate is considered
387	 * to be loaded again on return to userland (overriding last_cpu avoids
388	 * the optimisation).
389	 */
390	fpregs_lock();
391
392	if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
393
394		/*
395		 * Supervisor states are not modified by user space input.  Save
396		 * current supervisor states first and invalidate the FPU regs.
397		 */
398		if (xfeatures_mask_supervisor())
399			copy_supervisor_to_kernel(&fpu->state.xsave);
400		set_thread_flag(TIF_NEED_FPU_LOAD);
401	}
402	__fpu_invalidate_fpregs_state(fpu);
403	fpregs_unlock();
404
405	if (use_xsave() && !fx_only) {
406		u64 init_bv = xfeatures_mask_user() & ~user_xfeatures;
407
408		if (using_compacted_format()) {
409			ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
410		} else {
411			ret = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
412
413			if (!ret && state_size > offsetof(struct xregs_state, header))
414				ret = validate_user_xstate_header(&fpu->state.xsave.header);
415		}
416		if (ret)
417			goto err_out;
418
419		sanitize_restored_user_xstate(&fpu->state, envp, user_xfeatures,
420					      fx_only);
 
 
 
 
 
421
422		fpregs_lock();
423		if (unlikely(init_bv))
424			copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
 
425
 
 
426		/*
427		 * Restore previously saved supervisor xstates along with
428		 * copied-in user xstates.
429		 */
430		ret = copy_kernel_to_xregs_err(&fpu->state.xsave,
431					       user_xfeatures | xfeatures_mask_supervisor());
432
433	} else if (use_fxsr()) {
434		ret = __copy_from_user(&fpu->state.fxsave, buf_fx, state_size);
435		if (ret) {
436			ret = -EFAULT;
437			goto err_out;
438		}
439
440		sanitize_restored_user_xstate(&fpu->state, envp, user_xfeatures,
441					      fx_only);
442
443		fpregs_lock();
444		if (use_xsave()) {
445			u64 init_bv;
446
447			init_bv = xfeatures_mask_user() & ~XFEATURE_MASK_FPSSE;
448			copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
449		}
450
451		ret = copy_kernel_to_fxregs_err(&fpu->state.fxsave);
452	} else {
453		ret = __copy_from_user(&fpu->state.fsave, buf_fx, state_size);
454		if (ret)
455			goto err_out;
456
457		fpregs_lock();
458		ret = copy_kernel_to_fregs_err(&fpu->state.fsave);
459	}
460	if (!ret)
461		fpregs_mark_activate();
462	else
463		fpregs_deactivate(fpu);
464	fpregs_unlock();
465
466err_out:
467	if (ret)
468		fpu__clear_user_states(fpu);
469	return ret;
470}
471
472static inline int xstate_sigframe_size(void)
473{
474	return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
475			fpu_user_xstate_size;
476}
477
478/*
479 * Restore FPU state from a sigframe:
480 */
481int fpu__restore_sig(void __user *buf, int ia32_frame)
482{
483	void __user *buf_fx = buf;
484	int size = xstate_sigframe_size();
485
486	if (ia32_frame && use_fxsr()) {
487		buf_fx = buf + sizeof(struct fregs_state);
488		size += sizeof(struct fregs_state);
489	}
490
491	return __fpu__restore_sig(buf, buf_fx, size);
492}
493
494unsigned long
495fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
496		     unsigned long *buf_fx, unsigned long *size)
497{
498	unsigned long frame_size = xstate_sigframe_size();
499
500	*buf_fx = sp = round_down(sp - frame_size, 64);
501	if (ia32_frame && use_fxsr()) {
502		frame_size += sizeof(struct fregs_state);
503		sp -= sizeof(struct fregs_state);
504	}
505
506	*size = frame_size;
507
508	return sp;
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 */
517void 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_user();
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