xref: /openbmc/linux/arch/powerpc/kernel/signal_64.c (revision 236d93c4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  PowerPC version
4  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5  *
6  *  Derived from "arch/i386/kernel/signal.c"
7  *    Copyright (C) 1991, 1992 Linus Torvalds
8  *    1997-11-28  Modified for POSIX.1b signals by Richard Henderson
9  */
10 
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/unistd.h>
19 #include <linux/stddef.h>
20 #include <linux/elf.h>
21 #include <linux/ptrace.h>
22 #include <linux/ratelimit.h>
23 #include <linux/syscalls.h>
24 #include <linux/pagemap.h>
25 
26 #include <asm/sigcontext.h>
27 #include <asm/ucontext.h>
28 #include <linux/uaccess.h>
29 #include <asm/unistd.h>
30 #include <asm/cacheflush.h>
31 #include <asm/syscalls.h>
32 #include <asm/vdso.h>
33 #include <asm/switch_to.h>
34 #include <asm/tm.h>
35 #include <asm/asm-prototypes.h>
36 
37 #include "signal.h"
38 
39 
40 #define GP_REGS_SIZE	min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
41 #define FP_REGS_SIZE	sizeof(elf_fpregset_t)
42 
43 #define TRAMP_TRACEBACK	4
44 #define TRAMP_SIZE	7
45 
46 /*
47  * When we have signals to deliver, we set up on the user stack,
48  * going down from the original stack pointer:
49  *	1) a rt_sigframe struct which contains the ucontext
50  *	2) a gap of __SIGNAL_FRAMESIZE bytes which acts as a dummy caller
51  *	   frame for the signal handler.
52  */
53 
54 struct rt_sigframe {
55 	/* sys_rt_sigreturn requires the ucontext be the first field */
56 	struct ucontext uc;
57 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
58 	struct ucontext uc_transact;
59 #endif
60 	unsigned long _unused[2];
61 	unsigned int tramp[TRAMP_SIZE];
62 	struct siginfo __user *pinfo;
63 	void __user *puc;
64 	struct siginfo info;
65 	/* New 64 bit little-endian ABI allows redzone of 512 bytes below sp */
66 	char abigap[USER_REDZONE_SIZE];
67 } __attribute__ ((aligned (16)));
68 
69 /*
70  * This computes a quad word aligned pointer inside the vmx_reserve array
71  * element. For historical reasons sigcontext might not be quad word aligned,
72  * but the location we write the VMX regs to must be. See the comment in
73  * sigcontext for more detail.
74  */
75 #ifdef CONFIG_ALTIVEC
76 static elf_vrreg_t __user *sigcontext_vmx_regs(struct sigcontext __user *sc)
77 {
78 	return (elf_vrreg_t __user *) (((unsigned long)sc->vmx_reserve + 15) & ~0xful);
79 }
80 #endif
81 
82 static void prepare_setup_sigcontext(struct task_struct *tsk)
83 {
84 #ifdef CONFIG_ALTIVEC
85 	/* save altivec registers */
86 	if (tsk->thread.used_vr)
87 		flush_altivec_to_thread(tsk);
88 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
89 		tsk->thread.vrsave = mfspr(SPRN_VRSAVE);
90 #endif /* CONFIG_ALTIVEC */
91 
92 	flush_fp_to_thread(tsk);
93 
94 #ifdef CONFIG_VSX
95 	if (tsk->thread.used_vsr)
96 		flush_vsx_to_thread(tsk);
97 #endif /* CONFIG_VSX */
98 }
99 
100 /*
101  * Set up the sigcontext for the signal frame.
102  */
103 
104 #define unsafe_setup_sigcontext(sc, tsk, signr, set, handler, ctx_has_vsx_region, label)\
105 do {											\
106 	if (__unsafe_setup_sigcontext(sc, tsk, signr, set, handler, ctx_has_vsx_region))\
107 		goto label;								\
108 } while (0)
109 static long notrace __unsafe_setup_sigcontext(struct sigcontext __user *sc,
110 					struct task_struct *tsk, int signr, sigset_t *set,
111 					unsigned long handler, int ctx_has_vsx_region)
112 {
113 	/* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
114 	 * process never used altivec yet (MSR_VEC is zero in pt_regs of
115 	 * the context). This is very important because we must ensure we
116 	 * don't lose the VRSAVE content that may have been set prior to
117 	 * the process doing its first vector operation
118 	 * Userland shall check AT_HWCAP to know whether it can rely on the
119 	 * v_regs pointer or not
120 	 */
121 #ifdef CONFIG_ALTIVEC
122 	elf_vrreg_t __user *v_regs = sigcontext_vmx_regs(sc);
123 #endif
124 	struct pt_regs *regs = tsk->thread.regs;
125 	unsigned long msr = regs->msr;
126 	/* Force usr to alway see softe as 1 (interrupts enabled) */
127 	unsigned long softe = 0x1;
128 
129 	BUG_ON(tsk != current);
130 
131 #ifdef CONFIG_ALTIVEC
132 	unsafe_put_user(v_regs, &sc->v_regs, efault_out);
133 
134 	/* save altivec registers */
135 	if (tsk->thread.used_vr) {
136 		/* Copy 33 vec registers (vr0..31 and vscr) to the stack */
137 		unsafe_copy_to_user(v_regs, &tsk->thread.vr_state,
138 				    33 * sizeof(vector128), efault_out);
139 		/* set MSR_VEC in the MSR value in the frame to indicate that sc->v_reg)
140 		 * contains valid data.
141 		 */
142 		msr |= MSR_VEC;
143 	}
144 	/* We always copy to/from vrsave, it's 0 if we don't have or don't
145 	 * use altivec.
146 	 */
147 	unsafe_put_user(tsk->thread.vrsave, (u32 __user *)&v_regs[33], efault_out);
148 #else /* CONFIG_ALTIVEC */
149 	unsafe_put_user(0, &sc->v_regs, efault_out);
150 #endif /* CONFIG_ALTIVEC */
151 	/* copy fpr regs and fpscr */
152 	unsafe_copy_fpr_to_user(&sc->fp_regs, tsk, efault_out);
153 
154 	/*
155 	 * Clear the MSR VSX bit to indicate there is no valid state attached
156 	 * to this context, except in the specific case below where we set it.
157 	 */
158 	msr &= ~MSR_VSX;
159 #ifdef CONFIG_VSX
160 	/*
161 	 * Copy VSX low doubleword to local buffer for formatting,
162 	 * then out to userspace.  Update v_regs to point after the
163 	 * VMX data.
164 	 */
165 	if (tsk->thread.used_vsr && ctx_has_vsx_region) {
166 		v_regs += ELF_NVRREG;
167 		unsafe_copy_vsx_to_user(v_regs, tsk, efault_out);
168 		/* set MSR_VSX in the MSR value in the frame to
169 		 * indicate that sc->vs_reg) contains valid data.
170 		 */
171 		msr |= MSR_VSX;
172 	}
173 #endif /* CONFIG_VSX */
174 	unsafe_put_user(&sc->gp_regs, &sc->regs, efault_out);
175 	unsafe_copy_to_user(&sc->gp_regs, regs, GP_REGS_SIZE, efault_out);
176 	unsafe_put_user(msr, &sc->gp_regs[PT_MSR], efault_out);
177 	unsafe_put_user(softe, &sc->gp_regs[PT_SOFTE], efault_out);
178 	unsafe_put_user(signr, &sc->signal, efault_out);
179 	unsafe_put_user(handler, &sc->handler, efault_out);
180 	if (set != NULL)
181 		unsafe_put_user(set->sig[0], &sc->oldmask, efault_out);
182 
183 	return 0;
184 
185 efault_out:
186 	return -EFAULT;
187 }
188 
189 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
190 /*
191  * As above, but Transactional Memory is in use, so deliver sigcontexts
192  * containing checkpointed and transactional register states.
193  *
194  * To do this, we treclaim (done before entering here) to gather both sets of
195  * registers and set up the 'normal' sigcontext registers with rolled-back
196  * register values such that a simple signal handler sees a correct
197  * checkpointed register state.  If interested, a TM-aware sighandler can
198  * examine the transactional registers in the 2nd sigcontext to determine the
199  * real origin of the signal.
200  */
201 static long setup_tm_sigcontexts(struct sigcontext __user *sc,
202 				 struct sigcontext __user *tm_sc,
203 				 struct task_struct *tsk,
204 				 int signr, sigset_t *set, unsigned long handler,
205 				 unsigned long msr)
206 {
207 	/* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
208 	 * process never used altivec yet (MSR_VEC is zero in pt_regs of
209 	 * the context). This is very important because we must ensure we
210 	 * don't lose the VRSAVE content that may have been set prior to
211 	 * the process doing its first vector operation
212 	 * Userland shall check AT_HWCAP to know wether it can rely on the
213 	 * v_regs pointer or not.
214 	 */
215 #ifdef CONFIG_ALTIVEC
216 	elf_vrreg_t __user *v_regs = sigcontext_vmx_regs(sc);
217 	elf_vrreg_t __user *tm_v_regs = sigcontext_vmx_regs(tm_sc);
218 #endif
219 	struct pt_regs *regs = tsk->thread.regs;
220 	long err = 0;
221 
222 	BUG_ON(tsk != current);
223 
224 	BUG_ON(!MSR_TM_ACTIVE(msr));
225 
226 	WARN_ON(tm_suspend_disabled);
227 
228 	/* Restore checkpointed FP, VEC, and VSX bits from ckpt_regs as
229 	 * it contains the correct FP, VEC, VSX state after we treclaimed
230 	 * the transaction and giveup_all() was called on reclaiming.
231 	 */
232 	msr |= tsk->thread.ckpt_regs.msr & (MSR_FP | MSR_VEC | MSR_VSX);
233 
234 #ifdef CONFIG_ALTIVEC
235 	err |= __put_user(v_regs, &sc->v_regs);
236 	err |= __put_user(tm_v_regs, &tm_sc->v_regs);
237 
238 	/* save altivec registers */
239 	if (tsk->thread.used_vr) {
240 		/* Copy 33 vec registers (vr0..31 and vscr) to the stack */
241 		err |= __copy_to_user(v_regs, &tsk->thread.ckvr_state,
242 				      33 * sizeof(vector128));
243 		/* If VEC was enabled there are transactional VRs valid too,
244 		 * else they're a copy of the checkpointed VRs.
245 		 */
246 		if (msr & MSR_VEC)
247 			err |= __copy_to_user(tm_v_regs,
248 					      &tsk->thread.vr_state,
249 					      33 * sizeof(vector128));
250 		else
251 			err |= __copy_to_user(tm_v_regs,
252 					      &tsk->thread.ckvr_state,
253 					      33 * sizeof(vector128));
254 
255 		/* set MSR_VEC in the MSR value in the frame to indicate
256 		 * that sc->v_reg contains valid data.
257 		 */
258 		msr |= MSR_VEC;
259 	}
260 	/* We always copy to/from vrsave, it's 0 if we don't have or don't
261 	 * use altivec.
262 	 */
263 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
264 		tsk->thread.ckvrsave = mfspr(SPRN_VRSAVE);
265 	err |= __put_user(tsk->thread.ckvrsave, (u32 __user *)&v_regs[33]);
266 	if (msr & MSR_VEC)
267 		err |= __put_user(tsk->thread.vrsave,
268 				  (u32 __user *)&tm_v_regs[33]);
269 	else
270 		err |= __put_user(tsk->thread.ckvrsave,
271 				  (u32 __user *)&tm_v_regs[33]);
272 
273 #else /* CONFIG_ALTIVEC */
274 	err |= __put_user(0, &sc->v_regs);
275 	err |= __put_user(0, &tm_sc->v_regs);
276 #endif /* CONFIG_ALTIVEC */
277 
278 	/* copy fpr regs and fpscr */
279 	err |= copy_ckfpr_to_user(&sc->fp_regs, tsk);
280 	if (msr & MSR_FP)
281 		err |= copy_fpr_to_user(&tm_sc->fp_regs, tsk);
282 	else
283 		err |= copy_ckfpr_to_user(&tm_sc->fp_regs, tsk);
284 
285 #ifdef CONFIG_VSX
286 	/*
287 	 * Copy VSX low doubleword to local buffer for formatting,
288 	 * then out to userspace.  Update v_regs to point after the
289 	 * VMX data.
290 	 */
291 	if (tsk->thread.used_vsr) {
292 		v_regs += ELF_NVRREG;
293 		tm_v_regs += ELF_NVRREG;
294 
295 		err |= copy_ckvsx_to_user(v_regs, tsk);
296 
297 		if (msr & MSR_VSX)
298 			err |= copy_vsx_to_user(tm_v_regs, tsk);
299 		else
300 			err |= copy_ckvsx_to_user(tm_v_regs, tsk);
301 
302 		/* set MSR_VSX in the MSR value in the frame to
303 		 * indicate that sc->vs_reg) contains valid data.
304 		 */
305 		msr |= MSR_VSX;
306 	}
307 #endif /* CONFIG_VSX */
308 
309 	err |= __put_user(&sc->gp_regs, &sc->regs);
310 	err |= __put_user(&tm_sc->gp_regs, &tm_sc->regs);
311 	err |= __copy_to_user(&tm_sc->gp_regs, regs, GP_REGS_SIZE);
312 	err |= __copy_to_user(&sc->gp_regs,
313 			      &tsk->thread.ckpt_regs, GP_REGS_SIZE);
314 	err |= __put_user(msr, &tm_sc->gp_regs[PT_MSR]);
315 	err |= __put_user(msr, &sc->gp_regs[PT_MSR]);
316 	err |= __put_user(signr, &sc->signal);
317 	err |= __put_user(handler, &sc->handler);
318 	if (set != NULL)
319 		err |=  __put_user(set->sig[0], &sc->oldmask);
320 
321 	return err;
322 }
323 #endif
324 
325 /*
326  * Restore the sigcontext from the signal frame.
327  */
328 #define unsafe_restore_sigcontext(tsk, set, sig, sc, label) do {	\
329 	if (__unsafe_restore_sigcontext(tsk, set, sig, sc))		\
330 		goto label;						\
331 } while (0)
332 static long notrace __unsafe_restore_sigcontext(struct task_struct *tsk, sigset_t *set,
333 						int sig, struct sigcontext __user *sc)
334 {
335 #ifdef CONFIG_ALTIVEC
336 	elf_vrreg_t __user *v_regs;
337 #endif
338 	unsigned long save_r13 = 0;
339 	unsigned long msr;
340 	struct pt_regs *regs = tsk->thread.regs;
341 #ifdef CONFIG_VSX
342 	int i;
343 #endif
344 
345 	BUG_ON(tsk != current);
346 
347 	/* If this is not a signal return, we preserve the TLS in r13 */
348 	if (!sig)
349 		save_r13 = regs->gpr[13];
350 
351 	/* copy the GPRs */
352 	unsafe_copy_from_user(regs->gpr, sc->gp_regs, sizeof(regs->gpr), efault_out);
353 	unsafe_get_user(regs->nip, &sc->gp_regs[PT_NIP], efault_out);
354 	/* get MSR separately, transfer the LE bit if doing signal return */
355 	unsafe_get_user(msr, &sc->gp_regs[PT_MSR], efault_out);
356 	if (sig)
357 		regs_set_return_msr(regs, (regs->msr & ~MSR_LE) | (msr & MSR_LE));
358 	unsafe_get_user(regs->orig_gpr3, &sc->gp_regs[PT_ORIG_R3], efault_out);
359 	unsafe_get_user(regs->ctr, &sc->gp_regs[PT_CTR], efault_out);
360 	unsafe_get_user(regs->link, &sc->gp_regs[PT_LNK], efault_out);
361 	unsafe_get_user(regs->xer, &sc->gp_regs[PT_XER], efault_out);
362 	unsafe_get_user(regs->ccr, &sc->gp_regs[PT_CCR], efault_out);
363 	/* Don't allow userspace to set SOFTE */
364 	set_trap_norestart(regs);
365 	unsafe_get_user(regs->dar, &sc->gp_regs[PT_DAR], efault_out);
366 	unsafe_get_user(regs->dsisr, &sc->gp_regs[PT_DSISR], efault_out);
367 	unsafe_get_user(regs->result, &sc->gp_regs[PT_RESULT], efault_out);
368 
369 	if (!sig)
370 		regs->gpr[13] = save_r13;
371 	if (set != NULL)
372 		unsafe_get_user(set->sig[0], &sc->oldmask, efault_out);
373 
374 	/*
375 	 * Force reload of FP/VEC.
376 	 * This has to be done before copying stuff into tsk->thread.fpr/vr
377 	 * for the reasons explained in the previous comment.
378 	 */
379 	regs_set_return_msr(regs, regs->msr & ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX));
380 
381 #ifdef CONFIG_ALTIVEC
382 	unsafe_get_user(v_regs, &sc->v_regs, efault_out);
383 	if (v_regs && !access_ok(v_regs, 34 * sizeof(vector128)))
384 		return -EFAULT;
385 	/* Copy 33 vec registers (vr0..31 and vscr) from the stack */
386 	if (v_regs != NULL && (msr & MSR_VEC) != 0) {
387 		unsafe_copy_from_user(&tsk->thread.vr_state, v_regs,
388 				      33 * sizeof(vector128), efault_out);
389 		tsk->thread.used_vr = true;
390 	} else if (tsk->thread.used_vr) {
391 		memset(&tsk->thread.vr_state, 0, 33 * sizeof(vector128));
392 	}
393 	/* Always get VRSAVE back */
394 	if (v_regs != NULL)
395 		unsafe_get_user(tsk->thread.vrsave, (u32 __user *)&v_regs[33], efault_out);
396 	else
397 		tsk->thread.vrsave = 0;
398 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
399 		mtspr(SPRN_VRSAVE, tsk->thread.vrsave);
400 #endif /* CONFIG_ALTIVEC */
401 	/* restore floating point */
402 	unsafe_copy_fpr_from_user(tsk, &sc->fp_regs, efault_out);
403 #ifdef CONFIG_VSX
404 	/*
405 	 * Get additional VSX data. Update v_regs to point after the
406 	 * VMX data.  Copy VSX low doubleword from userspace to local
407 	 * buffer for formatting, then into the taskstruct.
408 	 */
409 	v_regs += ELF_NVRREG;
410 	if ((msr & MSR_VSX) != 0) {
411 		unsafe_copy_vsx_from_user(tsk, v_regs, efault_out);
412 		tsk->thread.used_vsr = true;
413 	} else {
414 		for (i = 0; i < 32 ; i++)
415 			tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
416 	}
417 #endif
418 	return 0;
419 
420 efault_out:
421 	return -EFAULT;
422 }
423 
424 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
425 /*
426  * Restore the two sigcontexts from the frame of a transactional processes.
427  */
428 
429 static long restore_tm_sigcontexts(struct task_struct *tsk,
430 				   struct sigcontext __user *sc,
431 				   struct sigcontext __user *tm_sc)
432 {
433 #ifdef CONFIG_ALTIVEC
434 	elf_vrreg_t __user *v_regs, *tm_v_regs;
435 #endif
436 	unsigned long err = 0;
437 	unsigned long msr;
438 	struct pt_regs *regs = tsk->thread.regs;
439 #ifdef CONFIG_VSX
440 	int i;
441 #endif
442 
443 	BUG_ON(tsk != current);
444 
445 	if (tm_suspend_disabled)
446 		return -EINVAL;
447 
448 	/* copy the GPRs */
449 	err |= __copy_from_user(regs->gpr, tm_sc->gp_regs, sizeof(regs->gpr));
450 	err |= __copy_from_user(&tsk->thread.ckpt_regs, sc->gp_regs,
451 				sizeof(regs->gpr));
452 
453 	/*
454 	 * TFHAR is restored from the checkpointed 'wound-back' ucontext's NIP.
455 	 * TEXASR was set by the signal delivery reclaim, as was TFIAR.
456 	 * Users doing anything abhorrent like thread-switching w/ signals for
457 	 * TM-Suspended code will have to back TEXASR/TFIAR up themselves.
458 	 * For the case of getting a signal and simply returning from it,
459 	 * we don't need to re-copy them here.
460 	 */
461 	err |= __get_user(regs->nip, &tm_sc->gp_regs[PT_NIP]);
462 	err |= __get_user(tsk->thread.tm_tfhar, &sc->gp_regs[PT_NIP]);
463 
464 	/* get MSR separately, transfer the LE bit if doing signal return */
465 	err |= __get_user(msr, &sc->gp_regs[PT_MSR]);
466 	/* Don't allow reserved mode. */
467 	if (MSR_TM_RESV(msr))
468 		return -EINVAL;
469 
470 	/* pull in MSR LE from user context */
471 	regs_set_return_msr(regs, (regs->msr & ~MSR_LE) | (msr & MSR_LE));
472 
473 	/* The following non-GPR non-FPR non-VR state is also checkpointed: */
474 	err |= __get_user(regs->ctr, &tm_sc->gp_regs[PT_CTR]);
475 	err |= __get_user(regs->link, &tm_sc->gp_regs[PT_LNK]);
476 	err |= __get_user(regs->xer, &tm_sc->gp_regs[PT_XER]);
477 	err |= __get_user(regs->ccr, &tm_sc->gp_regs[PT_CCR]);
478 	err |= __get_user(tsk->thread.ckpt_regs.ctr,
479 			  &sc->gp_regs[PT_CTR]);
480 	err |= __get_user(tsk->thread.ckpt_regs.link,
481 			  &sc->gp_regs[PT_LNK]);
482 	err |= __get_user(tsk->thread.ckpt_regs.xer,
483 			  &sc->gp_regs[PT_XER]);
484 	err |= __get_user(tsk->thread.ckpt_regs.ccr,
485 			  &sc->gp_regs[PT_CCR]);
486 	/* Don't allow userspace to set SOFTE */
487 	set_trap_norestart(regs);
488 	/* These regs are not checkpointed; they can go in 'regs'. */
489 	err |= __get_user(regs->dar, &sc->gp_regs[PT_DAR]);
490 	err |= __get_user(regs->dsisr, &sc->gp_regs[PT_DSISR]);
491 	err |= __get_user(regs->result, &sc->gp_regs[PT_RESULT]);
492 
493 	/*
494 	 * Force reload of FP/VEC.
495 	 * This has to be done before copying stuff into tsk->thread.fpr/vr
496 	 * for the reasons explained in the previous comment.
497 	 */
498 	regs_set_return_msr(regs, regs->msr & ~(MSR_FP | MSR_FE0 | MSR_FE1 | MSR_VEC | MSR_VSX));
499 
500 #ifdef CONFIG_ALTIVEC
501 	err |= __get_user(v_regs, &sc->v_regs);
502 	err |= __get_user(tm_v_regs, &tm_sc->v_regs);
503 	if (err)
504 		return err;
505 	if (v_regs && !access_ok(v_regs, 34 * sizeof(vector128)))
506 		return -EFAULT;
507 	if (tm_v_regs && !access_ok(tm_v_regs, 34 * sizeof(vector128)))
508 		return -EFAULT;
509 	/* Copy 33 vec registers (vr0..31 and vscr) from the stack */
510 	if (v_regs != NULL && tm_v_regs != NULL && (msr & MSR_VEC) != 0) {
511 		err |= __copy_from_user(&tsk->thread.ckvr_state, v_regs,
512 					33 * sizeof(vector128));
513 		err |= __copy_from_user(&tsk->thread.vr_state, tm_v_regs,
514 					33 * sizeof(vector128));
515 		current->thread.used_vr = true;
516 	}
517 	else if (tsk->thread.used_vr) {
518 		memset(&tsk->thread.vr_state, 0, 33 * sizeof(vector128));
519 		memset(&tsk->thread.ckvr_state, 0, 33 * sizeof(vector128));
520 	}
521 	/* Always get VRSAVE back */
522 	if (v_regs != NULL && tm_v_regs != NULL) {
523 		err |= __get_user(tsk->thread.ckvrsave,
524 				  (u32 __user *)&v_regs[33]);
525 		err |= __get_user(tsk->thread.vrsave,
526 				  (u32 __user *)&tm_v_regs[33]);
527 	}
528 	else {
529 		tsk->thread.vrsave = 0;
530 		tsk->thread.ckvrsave = 0;
531 	}
532 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
533 		mtspr(SPRN_VRSAVE, tsk->thread.vrsave);
534 #endif /* CONFIG_ALTIVEC */
535 	/* restore floating point */
536 	err |= copy_fpr_from_user(tsk, &tm_sc->fp_regs);
537 	err |= copy_ckfpr_from_user(tsk, &sc->fp_regs);
538 #ifdef CONFIG_VSX
539 	/*
540 	 * Get additional VSX data. Update v_regs to point after the
541 	 * VMX data.  Copy VSX low doubleword from userspace to local
542 	 * buffer for formatting, then into the taskstruct.
543 	 */
544 	if (v_regs && ((msr & MSR_VSX) != 0)) {
545 		v_regs += ELF_NVRREG;
546 		tm_v_regs += ELF_NVRREG;
547 		err |= copy_vsx_from_user(tsk, tm_v_regs);
548 		err |= copy_ckvsx_from_user(tsk, v_regs);
549 		tsk->thread.used_vsr = true;
550 	} else {
551 		for (i = 0; i < 32 ; i++) {
552 			tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
553 			tsk->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
554 		}
555 	}
556 #endif
557 	tm_enable();
558 	/* Make sure the transaction is marked as failed */
559 	tsk->thread.tm_texasr |= TEXASR_FS;
560 
561 	/*
562 	 * Disabling preemption, since it is unsafe to be preempted
563 	 * with MSR[TS] set without recheckpointing.
564 	 */
565 	preempt_disable();
566 
567 	/* pull in MSR TS bits from user context */
568 	regs_set_return_msr(regs, regs->msr | (msr & MSR_TS_MASK));
569 
570 	/*
571 	 * Ensure that TM is enabled in regs->msr before we leave the signal
572 	 * handler. It could be the case that (a) user disabled the TM bit
573 	 * through the manipulation of the MSR bits in uc_mcontext or (b) the
574 	 * TM bit was disabled because a sufficient number of context switches
575 	 * happened whilst in the signal handler and load_tm overflowed,
576 	 * disabling the TM bit. In either case we can end up with an illegal
577 	 * TM state leading to a TM Bad Thing when we return to userspace.
578 	 *
579 	 * CAUTION:
580 	 * After regs->MSR[TS] being updated, make sure that get_user(),
581 	 * put_user() or similar functions are *not* called. These
582 	 * functions can generate page faults which will cause the process
583 	 * to be de-scheduled with MSR[TS] set but without calling
584 	 * tm_recheckpoint(). This can cause a bug.
585 	 */
586 	regs_set_return_msr(regs, regs->msr | MSR_TM);
587 
588 	/* This loads the checkpointed FP/VEC state, if used */
589 	tm_recheckpoint(&tsk->thread);
590 
591 	msr_check_and_set(msr & (MSR_FP | MSR_VEC));
592 	if (msr & MSR_FP) {
593 		load_fp_state(&tsk->thread.fp_state);
594 		regs_set_return_msr(regs, regs->msr | (MSR_FP | tsk->thread.fpexc_mode));
595 	}
596 	if (msr & MSR_VEC) {
597 		load_vr_state(&tsk->thread.vr_state);
598 		regs_set_return_msr(regs, regs->msr | MSR_VEC);
599 	}
600 
601 	preempt_enable();
602 
603 	return err;
604 }
605 #else /* !CONFIG_PPC_TRANSACTIONAL_MEM */
606 static long restore_tm_sigcontexts(struct task_struct *tsk, struct sigcontext __user *sc,
607 				   struct sigcontext __user *tm_sc)
608 {
609 	return -EINVAL;
610 }
611 #endif
612 
613 /*
614  * Setup the trampoline code on the stack
615  */
616 static long setup_trampoline(unsigned int syscall, unsigned int __user *tramp)
617 {
618 	int i;
619 	long err = 0;
620 
621 	/* Call the handler and pop the dummy stackframe*/
622 	err |= __put_user(PPC_RAW_BCTRL(), &tramp[0]);
623 	err |= __put_user(PPC_RAW_ADDI(_R1, _R1, __SIGNAL_FRAMESIZE), &tramp[1]);
624 
625 	err |= __put_user(PPC_RAW_LI(_R0, syscall), &tramp[2]);
626 	err |= __put_user(PPC_RAW_SC(), &tramp[3]);
627 
628 	/* Minimal traceback info */
629 	for (i=TRAMP_TRACEBACK; i < TRAMP_SIZE ;i++)
630 		err |= __put_user(0, &tramp[i]);
631 
632 	if (!err)
633 		flush_icache_range((unsigned long) &tramp[0],
634 			   (unsigned long) &tramp[TRAMP_SIZE]);
635 
636 	return err;
637 }
638 
639 /*
640  * Userspace code may pass a ucontext which doesn't include VSX added
641  * at the end.  We need to check for this case.
642  */
643 #define UCONTEXTSIZEWITHOUTVSX \
644 		(sizeof(struct ucontext) - 32*sizeof(long))
645 
646 /*
647  * Handle {get,set,swap}_context operations
648  */
649 SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,
650 		struct ucontext __user *, new_ctx, long, ctx_size)
651 {
652 	sigset_t set;
653 	unsigned long new_msr = 0;
654 	int ctx_has_vsx_region = 0;
655 
656 	if (new_ctx &&
657 	    get_user(new_msr, &new_ctx->uc_mcontext.gp_regs[PT_MSR]))
658 		return -EFAULT;
659 	/*
660 	 * Check that the context is not smaller than the original
661 	 * size (with VMX but without VSX)
662 	 */
663 	if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
664 		return -EINVAL;
665 	/*
666 	 * If the new context state sets the MSR VSX bits but
667 	 * it doesn't provide VSX state.
668 	 */
669 	if ((ctx_size < sizeof(struct ucontext)) &&
670 	    (new_msr & MSR_VSX))
671 		return -EINVAL;
672 	/* Does the context have enough room to store VSX data? */
673 	if (ctx_size >= sizeof(struct ucontext))
674 		ctx_has_vsx_region = 1;
675 
676 	if (old_ctx != NULL) {
677 		prepare_setup_sigcontext(current);
678 		if (!user_write_access_begin(old_ctx, ctx_size))
679 			return -EFAULT;
680 
681 		unsafe_setup_sigcontext(&old_ctx->uc_mcontext, current, 0, NULL,
682 					0, ctx_has_vsx_region, efault_out);
683 		unsafe_copy_to_user(&old_ctx->uc_sigmask, &current->blocked,
684 				    sizeof(sigset_t), efault_out);
685 
686 		user_write_access_end();
687 	}
688 	if (new_ctx == NULL)
689 		return 0;
690 	if (!access_ok(new_ctx, ctx_size) ||
691 	    fault_in_readable((char __user *)new_ctx, ctx_size))
692 		return -EFAULT;
693 
694 	/*
695 	 * If we get a fault copying the context into the kernel's
696 	 * image of the user's registers, we can't just return -EFAULT
697 	 * because the user's registers will be corrupted.  For instance
698 	 * the NIP value may have been updated but not some of the
699 	 * other registers.  Given that we have done the access_ok
700 	 * and successfully read the first and last bytes of the region
701 	 * above, this should only happen in an out-of-memory situation
702 	 * or if another thread unmaps the region containing the context.
703 	 * We kill the task with a SIGSEGV in this situation.
704 	 */
705 
706 	if (__get_user_sigset(&set, &new_ctx->uc_sigmask)) {
707 		force_exit_sig(SIGSEGV);
708 		return -EFAULT;
709 	}
710 	set_current_blocked(&set);
711 
712 	if (!user_read_access_begin(new_ctx, ctx_size))
713 		return -EFAULT;
714 	if (__unsafe_restore_sigcontext(current, NULL, 0, &new_ctx->uc_mcontext)) {
715 		user_read_access_end();
716 		force_exit_sig(SIGSEGV);
717 		return -EFAULT;
718 	}
719 	user_read_access_end();
720 
721 	/* This returns like rt_sigreturn */
722 	set_thread_flag(TIF_RESTOREALL);
723 
724 	return 0;
725 
726 efault_out:
727 	user_write_access_end();
728 	return -EFAULT;
729 }
730 
731 
732 /*
733  * Do a signal return; undo the signal stack.
734  */
735 
736 SYSCALL_DEFINE0(rt_sigreturn)
737 {
738 	struct pt_regs *regs = current_pt_regs();
739 	struct ucontext __user *uc = (struct ucontext __user *)regs->gpr[1];
740 	sigset_t set;
741 	unsigned long msr;
742 
743 	/* Always make any pending restarted system calls return -EINTR */
744 	current->restart_block.fn = do_no_restart_syscall;
745 
746 	if (!access_ok(uc, sizeof(*uc)))
747 		goto badframe;
748 
749 	if (__get_user_sigset(&set, &uc->uc_sigmask))
750 		goto badframe;
751 	set_current_blocked(&set);
752 
753 	if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM)) {
754 		/*
755 		 * If there is a transactional state then throw it away.
756 		 * The purpose of a sigreturn is to destroy all traces of the
757 		 * signal frame, this includes any transactional state created
758 		 * within in. We only check for suspended as we can never be
759 		 * active in the kernel, we are active, there is nothing better to
760 		 * do than go ahead and Bad Thing later.
761 		 * The cause is not important as there will never be a
762 		 * recheckpoint so it's not user visible.
763 		 */
764 		if (MSR_TM_SUSPENDED(mfmsr()))
765 			tm_reclaim_current(0);
766 
767 		/*
768 		 * Disable MSR[TS] bit also, so, if there is an exception in the
769 		 * code below (as a page fault in copy_ckvsx_to_user()), it does
770 		 * not recheckpoint this task if there was a context switch inside
771 		 * the exception.
772 		 *
773 		 * A major page fault can indirectly call schedule(). A reschedule
774 		 * process in the middle of an exception can have a side effect
775 		 * (Changing the CPU MSR[TS] state), since schedule() is called
776 		 * with the CPU MSR[TS] disable and returns with MSR[TS]=Suspended
777 		 * (switch_to() calls tm_recheckpoint() for the 'new' process). In
778 		 * this case, the process continues to be the same in the CPU, but
779 		 * the CPU state just changed.
780 		 *
781 		 * This can cause a TM Bad Thing, since the MSR in the stack will
782 		 * have the MSR[TS]=0, and this is what will be used to RFID.
783 		 *
784 		 * Clearing MSR[TS] state here will avoid a recheckpoint if there
785 		 * is any process reschedule in kernel space. The MSR[TS] state
786 		 * does not need to be saved also, since it will be replaced with
787 		 * the MSR[TS] that came from user context later, at
788 		 * restore_tm_sigcontexts.
789 		 */
790 		regs_set_return_msr(regs, regs->msr & ~MSR_TS_MASK);
791 
792 		if (__get_user(msr, &uc->uc_mcontext.gp_regs[PT_MSR]))
793 			goto badframe;
794 	}
795 
796 	if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) && MSR_TM_ACTIVE(msr)) {
797 		/* We recheckpoint on return. */
798 		struct ucontext __user *uc_transact;
799 
800 		/* Trying to start TM on non TM system */
801 		if (!cpu_has_feature(CPU_FTR_TM))
802 			goto badframe;
803 
804 		if (__get_user(uc_transact, &uc->uc_link))
805 			goto badframe;
806 		if (restore_tm_sigcontexts(current, &uc->uc_mcontext,
807 					   &uc_transact->uc_mcontext))
808 			goto badframe;
809 	} else {
810 		/*
811 		 * Fall through, for non-TM restore
812 		 *
813 		 * Unset MSR[TS] on the thread regs since MSR from user
814 		 * context does not have MSR active, and recheckpoint was
815 		 * not called since restore_tm_sigcontexts() was not called
816 		 * also.
817 		 *
818 		 * If not unsetting it, the code can RFID to userspace with
819 		 * MSR[TS] set, but without CPU in the proper state,
820 		 * causing a TM bad thing.
821 		 */
822 		regs_set_return_msr(current->thread.regs,
823 				current->thread.regs->msr & ~MSR_TS_MASK);
824 		if (!user_read_access_begin(&uc->uc_mcontext, sizeof(uc->uc_mcontext)))
825 			goto badframe;
826 
827 		unsafe_restore_sigcontext(current, NULL, 1, &uc->uc_mcontext,
828 					  badframe_block);
829 
830 		user_read_access_end();
831 	}
832 
833 	if (restore_altstack(&uc->uc_stack))
834 		goto badframe;
835 
836 	set_thread_flag(TIF_RESTOREALL);
837 
838 	return 0;
839 
840 badframe_block:
841 	user_read_access_end();
842 badframe:
843 	signal_fault(current, regs, "rt_sigreturn", uc);
844 
845 	force_sig(SIGSEGV);
846 	return 0;
847 }
848 
849 int handle_rt_signal64(struct ksignal *ksig, sigset_t *set,
850 		struct task_struct *tsk)
851 {
852 	struct rt_sigframe __user *frame;
853 	unsigned long newsp = 0;
854 	long err = 0;
855 	struct pt_regs *regs = tsk->thread.regs;
856 	/* Save the thread's msr before get_tm_stackpointer() changes it */
857 	unsigned long msr = regs->msr;
858 
859 	frame = get_sigframe(ksig, tsk, sizeof(*frame), 0);
860 
861 	/*
862 	 * This only applies when calling unsafe_setup_sigcontext() and must be
863 	 * called before opening the uaccess window.
864 	 */
865 	if (!MSR_TM_ACTIVE(msr))
866 		prepare_setup_sigcontext(tsk);
867 
868 	if (!user_write_access_begin(frame, sizeof(*frame)))
869 		goto badframe;
870 
871 	unsafe_put_user(&frame->info, &frame->pinfo, badframe_block);
872 	unsafe_put_user(&frame->uc, &frame->puc, badframe_block);
873 
874 	/* Create the ucontext.  */
875 	unsafe_put_user(0, &frame->uc.uc_flags, badframe_block);
876 	unsafe_save_altstack(&frame->uc.uc_stack, regs->gpr[1], badframe_block);
877 
878 	if (MSR_TM_ACTIVE(msr)) {
879 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
880 		/* The ucontext_t passed to userland points to the second
881 		 * ucontext_t (for transactional state) with its uc_link ptr.
882 		 */
883 		unsafe_put_user(&frame->uc_transact, &frame->uc.uc_link, badframe_block);
884 
885 		user_write_access_end();
886 
887 		err |= setup_tm_sigcontexts(&frame->uc.uc_mcontext,
888 					    &frame->uc_transact.uc_mcontext,
889 					    tsk, ksig->sig, NULL,
890 					    (unsigned long)ksig->ka.sa.sa_handler,
891 					    msr);
892 
893 		if (!user_write_access_begin(&frame->uc.uc_sigmask,
894 					     sizeof(frame->uc.uc_sigmask)))
895 			goto badframe;
896 
897 #endif
898 	} else {
899 		unsafe_put_user(0, &frame->uc.uc_link, badframe_block);
900 		unsafe_setup_sigcontext(&frame->uc.uc_mcontext, tsk, ksig->sig,
901 					NULL, (unsigned long)ksig->ka.sa.sa_handler,
902 					1, badframe_block);
903 	}
904 
905 	unsafe_copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set), badframe_block);
906 	user_write_access_end();
907 
908 	/* Save the siginfo outside of the unsafe block. */
909 	if (copy_siginfo_to_user(&frame->info, &ksig->info))
910 		goto badframe;
911 
912 	/* Make sure signal handler doesn't get spurious FP exceptions */
913 	tsk->thread.fp_state.fpscr = 0;
914 
915 	/* Set up to return from userspace. */
916 	if (tsk->mm->context.vdso) {
917 		regs_set_return_ip(regs, VDSO64_SYMBOL(tsk->mm->context.vdso, sigtramp_rt64));
918 	} else {
919 		err |= setup_trampoline(__NR_rt_sigreturn, &frame->tramp[0]);
920 		if (err)
921 			goto badframe;
922 		regs_set_return_ip(regs, (unsigned long) &frame->tramp[0]);
923 	}
924 
925 	/* Allocate a dummy caller frame for the signal handler. */
926 	newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
927 	err |= put_user(regs->gpr[1], (unsigned long __user *)newsp);
928 
929 	/* Set up "regs" so we "return" to the signal handler. */
930 	if (is_elf2_task()) {
931 		regs->ctr = (unsigned long) ksig->ka.sa.sa_handler;
932 		regs->gpr[12] = regs->ctr;
933 	} else {
934 		/* Handler is *really* a pointer to the function descriptor for
935 		 * the signal routine.  The first entry in the function
936 		 * descriptor is the entry address of signal and the second
937 		 * entry is the TOC value we need to use.
938 		 */
939 		struct func_desc __user *ptr =
940 			(struct func_desc __user *)ksig->ka.sa.sa_handler;
941 
942 		err |= get_user(regs->ctr, &ptr->addr);
943 		err |= get_user(regs->gpr[2], &ptr->toc);
944 	}
945 
946 	/* enter the signal handler in native-endian mode */
947 	regs_set_return_msr(regs, (regs->msr & ~MSR_LE) | (MSR_KERNEL & MSR_LE));
948 	regs->gpr[1] = newsp;
949 	regs->gpr[3] = ksig->sig;
950 	regs->result = 0;
951 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
952 		regs->gpr[4] = (unsigned long)&frame->info;
953 		regs->gpr[5] = (unsigned long)&frame->uc;
954 		regs->gpr[6] = (unsigned long) frame;
955 	} else {
956 		regs->gpr[4] = (unsigned long)&frame->uc.uc_mcontext;
957 	}
958 	if (err)
959 		goto badframe;
960 
961 	return 0;
962 
963 badframe_block:
964 	user_write_access_end();
965 badframe:
966 	signal_fault(current, regs, "handle_rt_signal64", frame);
967 
968 	return 1;
969 }
970