xref: /openbmc/linux/arch/x86/kernel/fpu/signal.c (revision 2874c5fd)
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  * Try to save it directly to the user frame with disabled page fault handler.
148  * If this fails then do the slow path where the FPU state is first saved to
149  * task's fpu->state and then copy it to the user frame pointed to by the
150  * aligned pointer 'buf_fx'.
151  *
152  * If this is a 32-bit frame with fxstate, put a fsave header before
153  * the aligned state at 'buf_fx'.
154  *
155  * For [f]xsave state, update the SW reserved fields in the [f]xsave frame
156  * indicating the absence/presence of the extended state to the user.
157  */
158 int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
159 {
160 	struct task_struct *tsk = current;
161 	int ia32_fxstate = (buf != buf_fx);
162 	int ret;
163 
164 	ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
165 			 IS_ENABLED(CONFIG_IA32_EMULATION));
166 
167 	if (!access_ok(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 retry:
176 	/*
177 	 * Load the FPU registers if they are not valid for the current task.
178 	 * With a valid FPU state we can attempt to save the state directly to
179 	 * userland's stack frame which will likely succeed. If it does not,
180 	 * resolve the fault in the user memory and try again.
181 	 */
182 	fpregs_lock();
183 	if (test_thread_flag(TIF_NEED_FPU_LOAD))
184 		__fpregs_load_activate();
185 
186 	pagefault_disable();
187 	ret = copy_fpregs_to_sigframe(buf_fx);
188 	pagefault_enable();
189 	fpregs_unlock();
190 
191 	if (ret) {
192 		int aligned_size;
193 		int nr_pages;
194 
195 		aligned_size = offset_in_page(buf_fx) + fpu_user_xstate_size;
196 		nr_pages = DIV_ROUND_UP(aligned_size, PAGE_SIZE);
197 
198 		ret = get_user_pages_unlocked((unsigned long)buf_fx, nr_pages,
199 					      NULL, FOLL_WRITE);
200 		if (ret == nr_pages)
201 			goto retry;
202 		return -EFAULT;
203 	}
204 
205 	/* Save the fsave header for the 32-bit frames. */
206 	if ((ia32_fxstate || !use_fxsr()) && save_fsave_header(tsk, buf))
207 		return -1;
208 
209 	if (use_fxsr() && save_xstate_epilog(buf_fx, ia32_fxstate))
210 		return -1;
211 
212 	return 0;
213 }
214 
215 static inline void
216 sanitize_restored_xstate(union fpregs_state *state,
217 			 struct user_i387_ia32_struct *ia32_env,
218 			 u64 xfeatures, int fx_only)
219 {
220 	struct xregs_state *xsave = &state->xsave;
221 	struct xstate_header *header = &xsave->header;
222 
223 	if (use_xsave()) {
224 		/*
225 		 * Note: we don't need to zero the reserved bits in the
226 		 * xstate_header here because we either didn't copy them at all,
227 		 * or we checked earlier that they aren't set.
228 		 */
229 
230 		/*
231 		 * Init the state that is not present in the memory
232 		 * layout and not enabled by the OS.
233 		 */
234 		if (fx_only)
235 			header->xfeatures = XFEATURE_MASK_FPSSE;
236 		else
237 			header->xfeatures &= xfeatures;
238 	}
239 
240 	if (use_fxsr()) {
241 		/*
242 		 * mscsr reserved bits must be masked to zero for security
243 		 * reasons.
244 		 */
245 		xsave->i387.mxcsr &= mxcsr_feature_mask;
246 
247 		if (ia32_env)
248 			convert_to_fxsr(&state->fxsave, ia32_env);
249 	}
250 }
251 
252 /*
253  * Restore the extended state if present. Otherwise, restore the FP/SSE state.
254  */
255 static int copy_user_to_fpregs_zeroing(void __user *buf, u64 xbv, int fx_only)
256 {
257 	if (use_xsave()) {
258 		if (fx_only) {
259 			u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
260 			copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
261 			return copy_user_to_fxregs(buf);
262 		} else {
263 			u64 init_bv = xfeatures_mask & ~xbv;
264 			if (unlikely(init_bv))
265 				copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
266 			return copy_user_to_xregs(buf, xbv);
267 		}
268 	} else if (use_fxsr()) {
269 		return copy_user_to_fxregs(buf);
270 	} else
271 		return copy_user_to_fregs(buf);
272 }
273 
274 static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
275 {
276 	struct user_i387_ia32_struct *envp = NULL;
277 	int state_size = fpu_kernel_xstate_size;
278 	int ia32_fxstate = (buf != buf_fx);
279 	struct task_struct *tsk = current;
280 	struct fpu *fpu = &tsk->thread.fpu;
281 	struct user_i387_ia32_struct env;
282 	u64 xfeatures = 0;
283 	int fx_only = 0;
284 	int ret = 0;
285 
286 	ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
287 			 IS_ENABLED(CONFIG_IA32_EMULATION));
288 
289 	if (!buf) {
290 		fpu__clear(fpu);
291 		return 0;
292 	}
293 
294 	if (!access_ok(buf, size))
295 		return -EACCES;
296 
297 	if (!static_cpu_has(X86_FEATURE_FPU))
298 		return fpregs_soft_set(current, NULL,
299 				       0, sizeof(struct user_i387_ia32_struct),
300 				       NULL, buf) != 0;
301 
302 	if (use_xsave()) {
303 		struct _fpx_sw_bytes fx_sw_user;
304 		if (unlikely(check_for_xstate(buf_fx, buf_fx, &fx_sw_user))) {
305 			/*
306 			 * Couldn't find the extended state information in the
307 			 * memory layout. Restore just the FP/SSE and init all
308 			 * the other extended state.
309 			 */
310 			state_size = sizeof(struct fxregs_state);
311 			fx_only = 1;
312 			trace_x86_fpu_xstate_check_failed(fpu);
313 		} else {
314 			state_size = fx_sw_user.xstate_size;
315 			xfeatures = fx_sw_user.xfeatures;
316 		}
317 	}
318 
319 	/*
320 	 * The current state of the FPU registers does not matter. By setting
321 	 * TIF_NEED_FPU_LOAD unconditionally it is ensured that the our xstate
322 	 * is not modified on context switch and that the xstate is considered
323 	 * to be loaded again on return to userland (overriding last_cpu avoids
324 	 * the optimisation).
325 	 */
326 	set_thread_flag(TIF_NEED_FPU_LOAD);
327 	__fpu_invalidate_fpregs_state(fpu);
328 
329 	if ((unsigned long)buf_fx % 64)
330 		fx_only = 1;
331 	/*
332 	 * For 32-bit frames with fxstate, copy the fxstate so it can be
333 	 * reconstructed later.
334 	 */
335 	if (ia32_fxstate) {
336 		ret = __copy_from_user(&env, buf, sizeof(env));
337 		if (ret)
338 			goto err_out;
339 		envp = &env;
340 	} else {
341 		/*
342 		 * Attempt to restore the FPU registers directly from user
343 		 * memory. For that to succeed, the user access cannot cause
344 		 * page faults. If it does, fall back to the slow path below,
345 		 * going through the kernel buffer with the enabled pagefault
346 		 * handler.
347 		 */
348 		fpregs_lock();
349 		pagefault_disable();
350 		ret = copy_user_to_fpregs_zeroing(buf_fx, xfeatures, fx_only);
351 		pagefault_enable();
352 		if (!ret) {
353 			fpregs_mark_activate();
354 			fpregs_unlock();
355 			return 0;
356 		}
357 		fpregs_unlock();
358 	}
359 
360 
361 	if (use_xsave() && !fx_only) {
362 		u64 init_bv = xfeatures_mask & ~xfeatures;
363 
364 		if (using_compacted_format()) {
365 			ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
366 		} else {
367 			ret = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
368 
369 			if (!ret && state_size > offsetof(struct xregs_state, header))
370 				ret = validate_xstate_header(&fpu->state.xsave.header);
371 		}
372 		if (ret)
373 			goto err_out;
374 
375 		sanitize_restored_xstate(&fpu->state, envp, xfeatures, fx_only);
376 
377 		fpregs_lock();
378 		if (unlikely(init_bv))
379 			copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
380 		ret = copy_kernel_to_xregs_err(&fpu->state.xsave, xfeatures);
381 
382 	} else if (use_fxsr()) {
383 		ret = __copy_from_user(&fpu->state.fxsave, buf_fx, state_size);
384 		if (ret) {
385 			ret = -EFAULT;
386 			goto err_out;
387 		}
388 
389 		sanitize_restored_xstate(&fpu->state, envp, xfeatures, fx_only);
390 
391 		fpregs_lock();
392 		if (use_xsave()) {
393 			u64 init_bv = xfeatures_mask & ~XFEATURE_MASK_FPSSE;
394 			copy_kernel_to_xregs(&init_fpstate.xsave, init_bv);
395 		}
396 
397 		ret = copy_kernel_to_fxregs_err(&fpu->state.fxsave);
398 	} else {
399 		ret = __copy_from_user(&fpu->state.fsave, buf_fx, state_size);
400 		if (ret)
401 			goto err_out;
402 
403 		fpregs_lock();
404 		ret = copy_kernel_to_fregs_err(&fpu->state.fsave);
405 	}
406 	if (!ret)
407 		fpregs_mark_activate();
408 	fpregs_unlock();
409 
410 err_out:
411 	if (ret)
412 		fpu__clear(fpu);
413 	return ret;
414 }
415 
416 static inline int xstate_sigframe_size(void)
417 {
418 	return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
419 			fpu_user_xstate_size;
420 }
421 
422 /*
423  * Restore FPU state from a sigframe:
424  */
425 int fpu__restore_sig(void __user *buf, int ia32_frame)
426 {
427 	void __user *buf_fx = buf;
428 	int size = xstate_sigframe_size();
429 
430 	if (ia32_frame && use_fxsr()) {
431 		buf_fx = buf + sizeof(struct fregs_state);
432 		size += sizeof(struct fregs_state);
433 	}
434 
435 	return __fpu__restore_sig(buf, buf_fx, size);
436 }
437 
438 unsigned long
439 fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
440 		     unsigned long *buf_fx, unsigned long *size)
441 {
442 	unsigned long frame_size = xstate_sigframe_size();
443 
444 	*buf_fx = sp = round_down(sp - frame_size, 64);
445 	if (ia32_frame && use_fxsr()) {
446 		frame_size += sizeof(struct fregs_state);
447 		sp -= sizeof(struct fregs_state);
448 	}
449 
450 	*size = frame_size;
451 
452 	return sp;
453 }
454 /*
455  * Prepare the SW reserved portion of the fxsave memory layout, indicating
456  * the presence of the extended state information in the memory layout
457  * pointed by the fpstate pointer in the sigcontext.
458  * This will be saved when ever the FP and extended state context is
459  * saved on the user stack during the signal handler delivery to the user.
460  */
461 void fpu__init_prepare_fx_sw_frame(void)
462 {
463 	int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
464 
465 	fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
466 	fx_sw_reserved.extended_size = size;
467 	fx_sw_reserved.xfeatures = xfeatures_mask;
468 	fx_sw_reserved.xstate_size = fpu_user_xstate_size;
469 
470 	if (IS_ENABLED(CONFIG_IA32_EMULATION) ||
471 	    IS_ENABLED(CONFIG_X86_32)) {
472 		int fsave_header_size = sizeof(struct fregs_state);
473 
474 		fx_sw_reserved_ia32 = fx_sw_reserved;
475 		fx_sw_reserved_ia32.extended_size = size + fsave_header_size;
476 	}
477 }
478 
479