xref: /openbmc/linux/arch/powerpc/kernel/signal_32.c (revision abfbd895)
1 /*
2  * Signal handling for 32bit PPC and 32bit tasks on 64bit PPC
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  * Copyright (C) 2001 IBM
7  * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
9  *
10  *  Derived from "arch/i386/kernel/signal.c"
11  *    Copyright (C) 1991, 1992 Linus Torvalds
12  *    1997-11-28  Modified for POSIX.1b signals by Richard Henderson
13  *
14  *  This program is free software; you can redistribute it and/or
15  *  modify it under the terms of the GNU General Public License
16  *  as published by the Free Software Foundation; either version
17  *  2 of the License, or (at your option) any later version.
18  */
19 
20 #include <linux/sched.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/kernel.h>
24 #include <linux/signal.h>
25 #include <linux/errno.h>
26 #include <linux/elf.h>
27 #include <linux/ptrace.h>
28 #include <linux/ratelimit.h>
29 #ifdef CONFIG_PPC64
30 #include <linux/syscalls.h>
31 #include <linux/compat.h>
32 #else
33 #include <linux/wait.h>
34 #include <linux/unistd.h>
35 #include <linux/stddef.h>
36 #include <linux/tty.h>
37 #include <linux/binfmts.h>
38 #endif
39 
40 #include <asm/uaccess.h>
41 #include <asm/cacheflush.h>
42 #include <asm/syscalls.h>
43 #include <asm/sigcontext.h>
44 #include <asm/vdso.h>
45 #include <asm/switch_to.h>
46 #include <asm/tm.h>
47 #ifdef CONFIG_PPC64
48 #include "ppc32.h"
49 #include <asm/unistd.h>
50 #else
51 #include <asm/ucontext.h>
52 #include <asm/pgtable.h>
53 #endif
54 
55 #include "signal.h"
56 
57 
58 #ifdef CONFIG_PPC64
59 #define sys_rt_sigreturn	compat_sys_rt_sigreturn
60 #define sys_swapcontext	compat_sys_swapcontext
61 #define sys_sigreturn	compat_sys_sigreturn
62 
63 #define old_sigaction	old_sigaction32
64 #define sigcontext	sigcontext32
65 #define mcontext	mcontext32
66 #define ucontext	ucontext32
67 
68 #define __save_altstack __compat_save_altstack
69 
70 /*
71  * Userspace code may pass a ucontext which doesn't include VSX added
72  * at the end.  We need to check for this case.
73  */
74 #define UCONTEXTSIZEWITHOUTVSX \
75 		(sizeof(struct ucontext) - sizeof(elf_vsrreghalf_t32))
76 
77 /*
78  * Returning 0 means we return to userspace via
79  * ret_from_except and thus restore all user
80  * registers from *regs.  This is what we need
81  * to do when a signal has been delivered.
82  */
83 
84 #define GP_REGS_SIZE	min(sizeof(elf_gregset_t32), sizeof(struct pt_regs32))
85 #undef __SIGNAL_FRAMESIZE
86 #define __SIGNAL_FRAMESIZE	__SIGNAL_FRAMESIZE32
87 #undef ELF_NVRREG
88 #define ELF_NVRREG	ELF_NVRREG32
89 
90 /*
91  * Functions for flipping sigsets (thanks to brain dead generic
92  * implementation that makes things simple for little endian only)
93  */
94 static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
95 {
96 	compat_sigset_t	cset;
97 
98 	switch (_NSIG_WORDS) {
99 	case 4: cset.sig[6] = set->sig[3] & 0xffffffffull;
100 		cset.sig[7] = set->sig[3] >> 32;
101 	case 3: cset.sig[4] = set->sig[2] & 0xffffffffull;
102 		cset.sig[5] = set->sig[2] >> 32;
103 	case 2: cset.sig[2] = set->sig[1] & 0xffffffffull;
104 		cset.sig[3] = set->sig[1] >> 32;
105 	case 1: cset.sig[0] = set->sig[0] & 0xffffffffull;
106 		cset.sig[1] = set->sig[0] >> 32;
107 	}
108 	return copy_to_user(uset, &cset, sizeof(*uset));
109 }
110 
111 static inline int get_sigset_t(sigset_t *set,
112 			       const compat_sigset_t __user *uset)
113 {
114 	compat_sigset_t s32;
115 
116 	if (copy_from_user(&s32, uset, sizeof(*uset)))
117 		return -EFAULT;
118 
119 	/*
120 	 * Swap the 2 words of the 64-bit sigset_t (they are stored
121 	 * in the "wrong" endian in 32-bit user storage).
122 	 */
123 	switch (_NSIG_WORDS) {
124 	case 4: set->sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
125 	case 3: set->sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
126 	case 2: set->sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
127 	case 1: set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
128 	}
129 	return 0;
130 }
131 
132 #define to_user_ptr(p)		ptr_to_compat(p)
133 #define from_user_ptr(p)	compat_ptr(p)
134 
135 static inline int save_general_regs(struct pt_regs *regs,
136 		struct mcontext __user *frame)
137 {
138 	elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
139 	int i;
140 
141 	WARN_ON(!FULL_REGS(regs));
142 
143 	for (i = 0; i <= PT_RESULT; i ++) {
144 		if (i == 14 && !FULL_REGS(regs))
145 			i = 32;
146 		if (__put_user((unsigned int)gregs[i], &frame->mc_gregs[i]))
147 			return -EFAULT;
148 	}
149 	return 0;
150 }
151 
152 static inline int restore_general_regs(struct pt_regs *regs,
153 		struct mcontext __user *sr)
154 {
155 	elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
156 	int i;
157 
158 	for (i = 0; i <= PT_RESULT; i++) {
159 		if ((i == PT_MSR) || (i == PT_SOFTE))
160 			continue;
161 		if (__get_user(gregs[i], &sr->mc_gregs[i]))
162 			return -EFAULT;
163 	}
164 	return 0;
165 }
166 
167 #else /* CONFIG_PPC64 */
168 
169 #define GP_REGS_SIZE	min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
170 
171 static inline int put_sigset_t(sigset_t __user *uset, sigset_t *set)
172 {
173 	return copy_to_user(uset, set, sizeof(*uset));
174 }
175 
176 static inline int get_sigset_t(sigset_t *set, const sigset_t __user *uset)
177 {
178 	return copy_from_user(set, uset, sizeof(*uset));
179 }
180 
181 #define to_user_ptr(p)		((unsigned long)(p))
182 #define from_user_ptr(p)	((void __user *)(p))
183 
184 static inline int save_general_regs(struct pt_regs *regs,
185 		struct mcontext __user *frame)
186 {
187 	WARN_ON(!FULL_REGS(regs));
188 	return __copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE);
189 }
190 
191 static inline int restore_general_regs(struct pt_regs *regs,
192 		struct mcontext __user *sr)
193 {
194 	/* copy up to but not including MSR */
195 	if (__copy_from_user(regs, &sr->mc_gregs,
196 				PT_MSR * sizeof(elf_greg_t)))
197 		return -EFAULT;
198 	/* copy from orig_r3 (the word after the MSR) up to the end */
199 	if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
200 				GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
201 		return -EFAULT;
202 	return 0;
203 }
204 #endif
205 
206 /*
207  * When we have signals to deliver, we set up on the
208  * user stack, going down from the original stack pointer:
209  *	an ABI gap of 56 words
210  *	an mcontext struct
211  *	a sigcontext struct
212  *	a gap of __SIGNAL_FRAMESIZE bytes
213  *
214  * Each of these things must be a multiple of 16 bytes in size. The following
215  * structure represent all of this except the __SIGNAL_FRAMESIZE gap
216  *
217  */
218 struct sigframe {
219 	struct sigcontext sctx;		/* the sigcontext */
220 	struct mcontext	mctx;		/* all the register values */
221 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
222 	struct sigcontext sctx_transact;
223 	struct mcontext	mctx_transact;
224 #endif
225 	/*
226 	 * Programs using the rs6000/xcoff abi can save up to 19 gp
227 	 * regs and 18 fp regs below sp before decrementing it.
228 	 */
229 	int			abigap[56];
230 };
231 
232 /* We use the mc_pad field for the signal return trampoline. */
233 #define tramp	mc_pad
234 
235 /*
236  *  When we have rt signals to deliver, we set up on the
237  *  user stack, going down from the original stack pointer:
238  *	one rt_sigframe struct (siginfo + ucontext + ABI gap)
239  *	a gap of __SIGNAL_FRAMESIZE+16 bytes
240  *  (the +16 is to get the siginfo and ucontext in the same
241  *  positions as in older kernels).
242  *
243  *  Each of these things must be a multiple of 16 bytes in size.
244  *
245  */
246 struct rt_sigframe {
247 #ifdef CONFIG_PPC64
248 	compat_siginfo_t info;
249 #else
250 	struct siginfo info;
251 #endif
252 	struct ucontext	uc;
253 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
254 	struct ucontext	uc_transact;
255 #endif
256 	/*
257 	 * Programs using the rs6000/xcoff abi can save up to 19 gp
258 	 * regs and 18 fp regs below sp before decrementing it.
259 	 */
260 	int			abigap[56];
261 };
262 
263 #ifdef CONFIG_VSX
264 unsigned long copy_fpr_to_user(void __user *to,
265 			       struct task_struct *task)
266 {
267 	u64 buf[ELF_NFPREG];
268 	int i;
269 
270 	/* save FPR copy to local buffer then write to the thread_struct */
271 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
272 		buf[i] = task->thread.TS_FPR(i);
273 	buf[i] = task->thread.fp_state.fpscr;
274 	return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
275 }
276 
277 unsigned long copy_fpr_from_user(struct task_struct *task,
278 				 void __user *from)
279 {
280 	u64 buf[ELF_NFPREG];
281 	int i;
282 
283 	if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
284 		return 1;
285 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
286 		task->thread.TS_FPR(i) = buf[i];
287 	task->thread.fp_state.fpscr = buf[i];
288 
289 	return 0;
290 }
291 
292 unsigned long copy_vsx_to_user(void __user *to,
293 			       struct task_struct *task)
294 {
295 	u64 buf[ELF_NVSRHALFREG];
296 	int i;
297 
298 	/* save FPR copy to local buffer then write to the thread_struct */
299 	for (i = 0; i < ELF_NVSRHALFREG; i++)
300 		buf[i] = task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
301 	return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
302 }
303 
304 unsigned long copy_vsx_from_user(struct task_struct *task,
305 				 void __user *from)
306 {
307 	u64 buf[ELF_NVSRHALFREG];
308 	int i;
309 
310 	if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
311 		return 1;
312 	for (i = 0; i < ELF_NVSRHALFREG ; i++)
313 		task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
314 	return 0;
315 }
316 
317 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
318 unsigned long copy_transact_fpr_to_user(void __user *to,
319 				  struct task_struct *task)
320 {
321 	u64 buf[ELF_NFPREG];
322 	int i;
323 
324 	/* save FPR copy to local buffer then write to the thread_struct */
325 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
326 		buf[i] = task->thread.TS_TRANS_FPR(i);
327 	buf[i] = task->thread.transact_fp.fpscr;
328 	return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
329 }
330 
331 unsigned long copy_transact_fpr_from_user(struct task_struct *task,
332 					  void __user *from)
333 {
334 	u64 buf[ELF_NFPREG];
335 	int i;
336 
337 	if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
338 		return 1;
339 	for (i = 0; i < (ELF_NFPREG - 1) ; i++)
340 		task->thread.TS_TRANS_FPR(i) = buf[i];
341 	task->thread.transact_fp.fpscr = buf[i];
342 
343 	return 0;
344 }
345 
346 unsigned long copy_transact_vsx_to_user(void __user *to,
347 				  struct task_struct *task)
348 {
349 	u64 buf[ELF_NVSRHALFREG];
350 	int i;
351 
352 	/* save FPR copy to local buffer then write to the thread_struct */
353 	for (i = 0; i < ELF_NVSRHALFREG; i++)
354 		buf[i] = task->thread.transact_fp.fpr[i][TS_VSRLOWOFFSET];
355 	return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
356 }
357 
358 unsigned long copy_transact_vsx_from_user(struct task_struct *task,
359 					  void __user *from)
360 {
361 	u64 buf[ELF_NVSRHALFREG];
362 	int i;
363 
364 	if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
365 		return 1;
366 	for (i = 0; i < ELF_NVSRHALFREG ; i++)
367 		task->thread.transact_fp.fpr[i][TS_VSRLOWOFFSET] = buf[i];
368 	return 0;
369 }
370 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
371 #else
372 inline unsigned long copy_fpr_to_user(void __user *to,
373 				      struct task_struct *task)
374 {
375 	return __copy_to_user(to, task->thread.fp_state.fpr,
376 			      ELF_NFPREG * sizeof(double));
377 }
378 
379 inline unsigned long copy_fpr_from_user(struct task_struct *task,
380 					void __user *from)
381 {
382 	return __copy_from_user(task->thread.fp_state.fpr, from,
383 			      ELF_NFPREG * sizeof(double));
384 }
385 
386 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
387 inline unsigned long copy_transact_fpr_to_user(void __user *to,
388 					 struct task_struct *task)
389 {
390 	return __copy_to_user(to, task->thread.transact_fp.fpr,
391 			      ELF_NFPREG * sizeof(double));
392 }
393 
394 inline unsigned long copy_transact_fpr_from_user(struct task_struct *task,
395 						 void __user *from)
396 {
397 	return __copy_from_user(task->thread.transact_fp.fpr, from,
398 				ELF_NFPREG * sizeof(double));
399 }
400 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
401 #endif
402 
403 /*
404  * Save the current user registers on the user stack.
405  * We only save the altivec/spe registers if the process has used
406  * altivec/spe instructions at some point.
407  */
408 static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
409 			  struct mcontext __user *tm_frame, int sigret,
410 			  int ctx_has_vsx_region)
411 {
412 	unsigned long msr = regs->msr;
413 
414 	/* Make sure floating point registers are stored in regs */
415 	flush_fp_to_thread(current);
416 
417 	/* save general registers */
418 	if (save_general_regs(regs, frame))
419 		return 1;
420 
421 #ifdef CONFIG_ALTIVEC
422 	/* save altivec registers */
423 	if (current->thread.used_vr) {
424 		flush_altivec_to_thread(current);
425 		if (__copy_to_user(&frame->mc_vregs, &current->thread.vr_state,
426 				   ELF_NVRREG * sizeof(vector128)))
427 			return 1;
428 		/* set MSR_VEC in the saved MSR value to indicate that
429 		   frame->mc_vregs contains valid data */
430 		msr |= MSR_VEC;
431 	}
432 	/* else assert((regs->msr & MSR_VEC) == 0) */
433 
434 	/* We always copy to/from vrsave, it's 0 if we don't have or don't
435 	 * use altivec. Since VSCR only contains 32 bits saved in the least
436 	 * significant bits of a vector, we "cheat" and stuff VRSAVE in the
437 	 * most significant bits of that same vector. --BenH
438 	 * Note that the current VRSAVE value is in the SPR at this point.
439 	 */
440 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
441 		current->thread.vrsave = mfspr(SPRN_VRSAVE);
442 	if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
443 		return 1;
444 #endif /* CONFIG_ALTIVEC */
445 	if (copy_fpr_to_user(&frame->mc_fregs, current))
446 		return 1;
447 
448 	/*
449 	 * Clear the MSR VSX bit to indicate there is no valid state attached
450 	 * to this context, except in the specific case below where we set it.
451 	 */
452 	msr &= ~MSR_VSX;
453 #ifdef CONFIG_VSX
454 	/*
455 	 * Copy VSR 0-31 upper half from thread_struct to local
456 	 * buffer, then write that to userspace.  Also set MSR_VSX in
457 	 * the saved MSR value to indicate that frame->mc_vregs
458 	 * contains valid data
459 	 */
460 	if (current->thread.used_vsr && ctx_has_vsx_region) {
461 		__giveup_vsx(current);
462 		if (copy_vsx_to_user(&frame->mc_vsregs, current))
463 			return 1;
464 		msr |= MSR_VSX;
465 	}
466 #endif /* CONFIG_VSX */
467 #ifdef CONFIG_SPE
468 	/* save spe registers */
469 	if (current->thread.used_spe) {
470 		flush_spe_to_thread(current);
471 		if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
472 				   ELF_NEVRREG * sizeof(u32)))
473 			return 1;
474 		/* set MSR_SPE in the saved MSR value to indicate that
475 		   frame->mc_vregs contains valid data */
476 		msr |= MSR_SPE;
477 	}
478 	/* else assert((regs->msr & MSR_SPE) == 0) */
479 
480 	/* We always copy to/from spefscr */
481 	if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
482 		return 1;
483 #endif /* CONFIG_SPE */
484 
485 	if (__put_user(msr, &frame->mc_gregs[PT_MSR]))
486 		return 1;
487 	/* We need to write 0 the MSR top 32 bits in the tm frame so that we
488 	 * can check it on the restore to see if TM is active
489 	 */
490 	if (tm_frame && __put_user(0, &tm_frame->mc_gregs[PT_MSR]))
491 		return 1;
492 
493 	if (sigret) {
494 		/* Set up the sigreturn trampoline: li r0,sigret; sc */
495 		if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
496 		    || __put_user(0x44000002UL, &frame->tramp[1]))
497 			return 1;
498 		flush_icache_range((unsigned long) &frame->tramp[0],
499 				   (unsigned long) &frame->tramp[2]);
500 	}
501 
502 	return 0;
503 }
504 
505 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
506 /*
507  * Save the current user registers on the user stack.
508  * We only save the altivec/spe registers if the process has used
509  * altivec/spe instructions at some point.
510  * We also save the transactional registers to a second ucontext in the
511  * frame.
512  *
513  * See save_user_regs() and signal_64.c:setup_tm_sigcontexts().
514  */
515 static int save_tm_user_regs(struct pt_regs *regs,
516 			     struct mcontext __user *frame,
517 			     struct mcontext __user *tm_frame, int sigret)
518 {
519 	unsigned long msr = regs->msr;
520 
521 	/* Remove TM bits from thread's MSR.  The MSR in the sigcontext
522 	 * just indicates to userland that we were doing a transaction, but we
523 	 * don't want to return in transactional state.  This also ensures
524 	 * that flush_fp_to_thread won't set TIF_RESTORE_TM again.
525 	 */
526 	regs->msr &= ~MSR_TS_MASK;
527 
528 	/* Make sure floating point registers are stored in regs */
529 	flush_fp_to_thread(current);
530 
531 	/* Save both sets of general registers */
532 	if (save_general_regs(&current->thread.ckpt_regs, frame)
533 	    || save_general_regs(regs, tm_frame))
534 		return 1;
535 
536 	/* Stash the top half of the 64bit MSR into the 32bit MSR word
537 	 * of the transactional mcontext.  This way we have a backward-compatible
538 	 * MSR in the 'normal' (checkpointed) mcontext and additionally one can
539 	 * also look at what type of transaction (T or S) was active at the
540 	 * time of the signal.
541 	 */
542 	if (__put_user((msr >> 32), &tm_frame->mc_gregs[PT_MSR]))
543 		return 1;
544 
545 #ifdef CONFIG_ALTIVEC
546 	/* save altivec registers */
547 	if (current->thread.used_vr) {
548 		flush_altivec_to_thread(current);
549 		if (__copy_to_user(&frame->mc_vregs, &current->thread.vr_state,
550 				   ELF_NVRREG * sizeof(vector128)))
551 			return 1;
552 		if (msr & MSR_VEC) {
553 			if (__copy_to_user(&tm_frame->mc_vregs,
554 					   &current->thread.transact_vr,
555 					   ELF_NVRREG * sizeof(vector128)))
556 				return 1;
557 		} else {
558 			if (__copy_to_user(&tm_frame->mc_vregs,
559 					   &current->thread.vr_state,
560 					   ELF_NVRREG * sizeof(vector128)))
561 				return 1;
562 		}
563 
564 		/* set MSR_VEC in the saved MSR value to indicate that
565 		 * frame->mc_vregs contains valid data
566 		 */
567 		msr |= MSR_VEC;
568 	}
569 
570 	/* We always copy to/from vrsave, it's 0 if we don't have or don't
571 	 * use altivec. Since VSCR only contains 32 bits saved in the least
572 	 * significant bits of a vector, we "cheat" and stuff VRSAVE in the
573 	 * most significant bits of that same vector. --BenH
574 	 */
575 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
576 		current->thread.vrsave = mfspr(SPRN_VRSAVE);
577 	if (__put_user(current->thread.vrsave,
578 		       (u32 __user *)&frame->mc_vregs[32]))
579 		return 1;
580 	if (msr & MSR_VEC) {
581 		if (__put_user(current->thread.transact_vrsave,
582 			       (u32 __user *)&tm_frame->mc_vregs[32]))
583 			return 1;
584 	} else {
585 		if (__put_user(current->thread.vrsave,
586 			       (u32 __user *)&tm_frame->mc_vregs[32]))
587 			return 1;
588 	}
589 #endif /* CONFIG_ALTIVEC */
590 
591 	if (copy_fpr_to_user(&frame->mc_fregs, current))
592 		return 1;
593 	if (msr & MSR_FP) {
594 		if (copy_transact_fpr_to_user(&tm_frame->mc_fregs, current))
595 			return 1;
596 	} else {
597 		if (copy_fpr_to_user(&tm_frame->mc_fregs, current))
598 			return 1;
599 	}
600 
601 #ifdef CONFIG_VSX
602 	/*
603 	 * Copy VSR 0-31 upper half from thread_struct to local
604 	 * buffer, then write that to userspace.  Also set MSR_VSX in
605 	 * the saved MSR value to indicate that frame->mc_vregs
606 	 * contains valid data
607 	 */
608 	if (current->thread.used_vsr) {
609 		__giveup_vsx(current);
610 		if (copy_vsx_to_user(&frame->mc_vsregs, current))
611 			return 1;
612 		if (msr & MSR_VSX) {
613 			if (copy_transact_vsx_to_user(&tm_frame->mc_vsregs,
614 						      current))
615 				return 1;
616 		} else {
617 			if (copy_vsx_to_user(&tm_frame->mc_vsregs, current))
618 				return 1;
619 		}
620 
621 		msr |= MSR_VSX;
622 	}
623 #endif /* CONFIG_VSX */
624 #ifdef CONFIG_SPE
625 	/* SPE regs are not checkpointed with TM, so this section is
626 	 * simply the same as in save_user_regs().
627 	 */
628 	if (current->thread.used_spe) {
629 		flush_spe_to_thread(current);
630 		if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
631 				   ELF_NEVRREG * sizeof(u32)))
632 			return 1;
633 		/* set MSR_SPE in the saved MSR value to indicate that
634 		 * frame->mc_vregs contains valid data */
635 		msr |= MSR_SPE;
636 	}
637 
638 	/* We always copy to/from spefscr */
639 	if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
640 		return 1;
641 #endif /* CONFIG_SPE */
642 
643 	if (__put_user(msr, &frame->mc_gregs[PT_MSR]))
644 		return 1;
645 	if (sigret) {
646 		/* Set up the sigreturn trampoline: li r0,sigret; sc */
647 		if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
648 		    || __put_user(0x44000002UL, &frame->tramp[1]))
649 			return 1;
650 		flush_icache_range((unsigned long) &frame->tramp[0],
651 				   (unsigned long) &frame->tramp[2]);
652 	}
653 
654 	return 0;
655 }
656 #endif
657 
658 /*
659  * Restore the current user register values from the user stack,
660  * (except for MSR).
661  */
662 static long restore_user_regs(struct pt_regs *regs,
663 			      struct mcontext __user *sr, int sig)
664 {
665 	long err;
666 	unsigned int save_r2 = 0;
667 	unsigned long msr;
668 #ifdef CONFIG_VSX
669 	int i;
670 #endif
671 
672 	/*
673 	 * restore general registers but not including MSR or SOFTE. Also
674 	 * take care of keeping r2 (TLS) intact if not a signal
675 	 */
676 	if (!sig)
677 		save_r2 = (unsigned int)regs->gpr[2];
678 	err = restore_general_regs(regs, sr);
679 	regs->trap = 0;
680 	err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
681 	if (!sig)
682 		regs->gpr[2] = (unsigned long) save_r2;
683 	if (err)
684 		return 1;
685 
686 	/* if doing signal return, restore the previous little-endian mode */
687 	if (sig)
688 		regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
689 
690 	/*
691 	 * Do this before updating the thread state in
692 	 * current->thread.fpr/vr/evr.  That way, if we get preempted
693 	 * and another task grabs the FPU/Altivec/SPE, it won't be
694 	 * tempted to save the current CPU state into the thread_struct
695 	 * and corrupt what we are writing there.
696 	 */
697 	discard_lazy_cpu_state();
698 
699 #ifdef CONFIG_ALTIVEC
700 	/*
701 	 * Force the process to reload the altivec registers from
702 	 * current->thread when it next does altivec instructions
703 	 */
704 	regs->msr &= ~MSR_VEC;
705 	if (msr & MSR_VEC) {
706 		/* restore altivec registers from the stack */
707 		if (__copy_from_user(&current->thread.vr_state, &sr->mc_vregs,
708 				     sizeof(sr->mc_vregs)))
709 			return 1;
710 	} else if (current->thread.used_vr)
711 		memset(&current->thread.vr_state, 0,
712 		       ELF_NVRREG * sizeof(vector128));
713 
714 	/* Always get VRSAVE back */
715 	if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
716 		return 1;
717 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
718 		mtspr(SPRN_VRSAVE, current->thread.vrsave);
719 #endif /* CONFIG_ALTIVEC */
720 	if (copy_fpr_from_user(current, &sr->mc_fregs))
721 		return 1;
722 
723 #ifdef CONFIG_VSX
724 	/*
725 	 * Force the process to reload the VSX registers from
726 	 * current->thread when it next does VSX instruction.
727 	 */
728 	regs->msr &= ~MSR_VSX;
729 	if (msr & MSR_VSX) {
730 		/*
731 		 * Restore altivec registers from the stack to a local
732 		 * buffer, then write this out to the thread_struct
733 		 */
734 		if (copy_vsx_from_user(current, &sr->mc_vsregs))
735 			return 1;
736 	} else if (current->thread.used_vsr)
737 		for (i = 0; i < 32 ; i++)
738 			current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
739 #endif /* CONFIG_VSX */
740 	/*
741 	 * force the process to reload the FP registers from
742 	 * current->thread when it next does FP instructions
743 	 */
744 	regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
745 
746 #ifdef CONFIG_SPE
747 	/* force the process to reload the spe registers from
748 	   current->thread when it next does spe instructions */
749 	regs->msr &= ~MSR_SPE;
750 	if (msr & MSR_SPE) {
751 		/* restore spe registers from the stack */
752 		if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
753 				     ELF_NEVRREG * sizeof(u32)))
754 			return 1;
755 	} else if (current->thread.used_spe)
756 		memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
757 
758 	/* Always get SPEFSCR back */
759 	if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs + ELF_NEVRREG))
760 		return 1;
761 #endif /* CONFIG_SPE */
762 
763 	return 0;
764 }
765 
766 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
767 /*
768  * Restore the current user register values from the user stack, except for
769  * MSR, and recheckpoint the original checkpointed register state for processes
770  * in transactions.
771  */
772 static long restore_tm_user_regs(struct pt_regs *regs,
773 				 struct mcontext __user *sr,
774 				 struct mcontext __user *tm_sr)
775 {
776 	long err;
777 	unsigned long msr, msr_hi;
778 #ifdef CONFIG_VSX
779 	int i;
780 #endif
781 
782 	/*
783 	 * restore general registers but not including MSR or SOFTE. Also
784 	 * take care of keeping r2 (TLS) intact if not a signal.
785 	 * See comment in signal_64.c:restore_tm_sigcontexts();
786 	 * TFHAR is restored from the checkpointed NIP; TEXASR and TFIAR
787 	 * were set by the signal delivery.
788 	 */
789 	err = restore_general_regs(regs, tm_sr);
790 	err |= restore_general_regs(&current->thread.ckpt_regs, sr);
791 
792 	err |= __get_user(current->thread.tm_tfhar, &sr->mc_gregs[PT_NIP]);
793 
794 	err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
795 	if (err)
796 		return 1;
797 
798 	/* Restore the previous little-endian mode */
799 	regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
800 
801 	/*
802 	 * Do this before updating the thread state in
803 	 * current->thread.fpr/vr/evr.  That way, if we get preempted
804 	 * and another task grabs the FPU/Altivec/SPE, it won't be
805 	 * tempted to save the current CPU state into the thread_struct
806 	 * and corrupt what we are writing there.
807 	 */
808 	discard_lazy_cpu_state();
809 
810 #ifdef CONFIG_ALTIVEC
811 	regs->msr &= ~MSR_VEC;
812 	if (msr & MSR_VEC) {
813 		/* restore altivec registers from the stack */
814 		if (__copy_from_user(&current->thread.vr_state, &sr->mc_vregs,
815 				     sizeof(sr->mc_vregs)) ||
816 		    __copy_from_user(&current->thread.transact_vr,
817 				     &tm_sr->mc_vregs,
818 				     sizeof(sr->mc_vregs)))
819 			return 1;
820 	} else if (current->thread.used_vr) {
821 		memset(&current->thread.vr_state, 0,
822 		       ELF_NVRREG * sizeof(vector128));
823 		memset(&current->thread.transact_vr, 0,
824 		       ELF_NVRREG * sizeof(vector128));
825 	}
826 
827 	/* Always get VRSAVE back */
828 	if (__get_user(current->thread.vrsave,
829 		       (u32 __user *)&sr->mc_vregs[32]) ||
830 	    __get_user(current->thread.transact_vrsave,
831 		       (u32 __user *)&tm_sr->mc_vregs[32]))
832 		return 1;
833 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
834 		mtspr(SPRN_VRSAVE, current->thread.vrsave);
835 #endif /* CONFIG_ALTIVEC */
836 
837 	regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
838 
839 	if (copy_fpr_from_user(current, &sr->mc_fregs) ||
840 	    copy_transact_fpr_from_user(current, &tm_sr->mc_fregs))
841 		return 1;
842 
843 #ifdef CONFIG_VSX
844 	regs->msr &= ~MSR_VSX;
845 	if (msr & MSR_VSX) {
846 		/*
847 		 * Restore altivec registers from the stack to a local
848 		 * buffer, then write this out to the thread_struct
849 		 */
850 		if (copy_vsx_from_user(current, &sr->mc_vsregs) ||
851 		    copy_transact_vsx_from_user(current, &tm_sr->mc_vsregs))
852 			return 1;
853 	} else if (current->thread.used_vsr)
854 		for (i = 0; i < 32 ; i++) {
855 			current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
856 			current->thread.transact_fp.fpr[i][TS_VSRLOWOFFSET] = 0;
857 		}
858 #endif /* CONFIG_VSX */
859 
860 #ifdef CONFIG_SPE
861 	/* SPE regs are not checkpointed with TM, so this section is
862 	 * simply the same as in restore_user_regs().
863 	 */
864 	regs->msr &= ~MSR_SPE;
865 	if (msr & MSR_SPE) {
866 		if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
867 				     ELF_NEVRREG * sizeof(u32)))
868 			return 1;
869 	} else if (current->thread.used_spe)
870 		memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
871 
872 	/* Always get SPEFSCR back */
873 	if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs
874 		       + ELF_NEVRREG))
875 		return 1;
876 #endif /* CONFIG_SPE */
877 
878 	/* Get the top half of the MSR from the user context */
879 	if (__get_user(msr_hi, &tm_sr->mc_gregs[PT_MSR]))
880 		return 1;
881 	msr_hi <<= 32;
882 	/* If TM bits are set to the reserved value, it's an invalid context */
883 	if (MSR_TM_RESV(msr_hi))
884 		return 1;
885 	/* Pull in the MSR TM bits from the user context */
886 	regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr_hi & MSR_TS_MASK);
887 	/* Now, recheckpoint.  This loads up all of the checkpointed (older)
888 	 * registers, including FP and V[S]Rs.  After recheckpointing, the
889 	 * transactional versions should be loaded.
890 	 */
891 	tm_enable();
892 	/* Make sure the transaction is marked as failed */
893 	current->thread.tm_texasr |= TEXASR_FS;
894 	/* This loads the checkpointed FP/VEC state, if used */
895 	tm_recheckpoint(&current->thread, msr);
896 
897 	/* This loads the speculative FP/VEC state, if used */
898 	if (msr & MSR_FP) {
899 		do_load_up_transact_fpu(&current->thread);
900 		regs->msr |= (MSR_FP | current->thread.fpexc_mode);
901 	}
902 #ifdef CONFIG_ALTIVEC
903 	if (msr & MSR_VEC) {
904 		do_load_up_transact_altivec(&current->thread);
905 		regs->msr |= MSR_VEC;
906 	}
907 #endif
908 
909 	return 0;
910 }
911 #endif
912 
913 #ifdef CONFIG_PPC64
914 int copy_siginfo_to_user32(struct compat_siginfo __user *d, const siginfo_t *s)
915 {
916 	int err;
917 
918 	if (!access_ok (VERIFY_WRITE, d, sizeof(*d)))
919 		return -EFAULT;
920 
921 	/* If you change siginfo_t structure, please be sure
922 	 * this code is fixed accordingly.
923 	 * It should never copy any pad contained in the structure
924 	 * to avoid security leaks, but must copy the generic
925 	 * 3 ints plus the relevant union member.
926 	 * This routine must convert siginfo from 64bit to 32bit as well
927 	 * at the same time.
928 	 */
929 	err = __put_user(s->si_signo, &d->si_signo);
930 	err |= __put_user(s->si_errno, &d->si_errno);
931 	err |= __put_user((short)s->si_code, &d->si_code);
932 	if (s->si_code < 0)
933 		err |= __copy_to_user(&d->_sifields._pad, &s->_sifields._pad,
934 				      SI_PAD_SIZE32);
935 	else switch(s->si_code >> 16) {
936 	case __SI_CHLD >> 16:
937 		err |= __put_user(s->si_pid, &d->si_pid);
938 		err |= __put_user(s->si_uid, &d->si_uid);
939 		err |= __put_user(s->si_utime, &d->si_utime);
940 		err |= __put_user(s->si_stime, &d->si_stime);
941 		err |= __put_user(s->si_status, &d->si_status);
942 		break;
943 	case __SI_FAULT >> 16:
944 		err |= __put_user((unsigned int)(unsigned long)s->si_addr,
945 				  &d->si_addr);
946 		break;
947 	case __SI_POLL >> 16:
948 		err |= __put_user(s->si_band, &d->si_band);
949 		err |= __put_user(s->si_fd, &d->si_fd);
950 		break;
951 	case __SI_TIMER >> 16:
952 		err |= __put_user(s->si_tid, &d->si_tid);
953 		err |= __put_user(s->si_overrun, &d->si_overrun);
954 		err |= __put_user(s->si_int, &d->si_int);
955 		break;
956 	case __SI_SYS >> 16:
957 		err |= __put_user(ptr_to_compat(s->si_call_addr), &d->si_call_addr);
958 		err |= __put_user(s->si_syscall, &d->si_syscall);
959 		err |= __put_user(s->si_arch, &d->si_arch);
960 		break;
961 	case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
962 	case __SI_MESGQ >> 16:
963 		err |= __put_user(s->si_int, &d->si_int);
964 		/* fallthrough */
965 	case __SI_KILL >> 16:
966 	default:
967 		err |= __put_user(s->si_pid, &d->si_pid);
968 		err |= __put_user(s->si_uid, &d->si_uid);
969 		break;
970 	}
971 	return err;
972 }
973 
974 #define copy_siginfo_to_user	copy_siginfo_to_user32
975 
976 int copy_siginfo_from_user32(siginfo_t *to, struct compat_siginfo __user *from)
977 {
978 	if (copy_from_user(to, from, 3*sizeof(int)) ||
979 	    copy_from_user(to->_sifields._pad,
980 			   from->_sifields._pad, SI_PAD_SIZE32))
981 		return -EFAULT;
982 
983 	return 0;
984 }
985 #endif /* CONFIG_PPC64 */
986 
987 /*
988  * Set up a signal frame for a "real-time" signal handler
989  * (one which gets siginfo).
990  */
991 int handle_rt_signal32(struct ksignal *ksig, sigset_t *oldset,
992 		       struct pt_regs *regs)
993 {
994 	struct rt_sigframe __user *rt_sf;
995 	struct mcontext __user *frame;
996 	struct mcontext __user *tm_frame = NULL;
997 	void __user *addr;
998 	unsigned long newsp = 0;
999 	int sigret;
1000 	unsigned long tramp;
1001 
1002 	/* Set up Signal Frame */
1003 	/* Put a Real Time Context onto stack */
1004 	rt_sf = get_sigframe(ksig, get_tm_stackpointer(regs), sizeof(*rt_sf), 1);
1005 	addr = rt_sf;
1006 	if (unlikely(rt_sf == NULL))
1007 		goto badframe;
1008 
1009 	/* Put the siginfo & fill in most of the ucontext */
1010 	if (copy_siginfo_to_user(&rt_sf->info, &ksig->info)
1011 	    || __put_user(0, &rt_sf->uc.uc_flags)
1012 	    || __save_altstack(&rt_sf->uc.uc_stack, regs->gpr[1])
1013 	    || __put_user(to_user_ptr(&rt_sf->uc.uc_mcontext),
1014 		    &rt_sf->uc.uc_regs)
1015 	    || put_sigset_t(&rt_sf->uc.uc_sigmask, oldset))
1016 		goto badframe;
1017 
1018 	/* Save user registers on the stack */
1019 	frame = &rt_sf->uc.uc_mcontext;
1020 	addr = frame;
1021 	if (vdso32_rt_sigtramp && current->mm->context.vdso_base) {
1022 		sigret = 0;
1023 		tramp = current->mm->context.vdso_base + vdso32_rt_sigtramp;
1024 	} else {
1025 		sigret = __NR_rt_sigreturn;
1026 		tramp = (unsigned long) frame->tramp;
1027 	}
1028 
1029 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1030 	tm_frame = &rt_sf->uc_transact.uc_mcontext;
1031 	if (MSR_TM_ACTIVE(regs->msr)) {
1032 		if (__put_user((unsigned long)&rt_sf->uc_transact,
1033 			       &rt_sf->uc.uc_link) ||
1034 		    __put_user((unsigned long)tm_frame,
1035 			       &rt_sf->uc_transact.uc_regs))
1036 			goto badframe;
1037 		if (save_tm_user_regs(regs, frame, tm_frame, sigret))
1038 			goto badframe;
1039 	}
1040 	else
1041 #endif
1042 	{
1043 		if (__put_user(0, &rt_sf->uc.uc_link))
1044 			goto badframe;
1045 		if (save_user_regs(regs, frame, tm_frame, sigret, 1))
1046 			goto badframe;
1047 	}
1048 	regs->link = tramp;
1049 
1050 	current->thread.fp_state.fpscr = 0;	/* turn off all fp exceptions */
1051 
1052 	/* create a stack frame for the caller of the handler */
1053 	newsp = ((unsigned long)rt_sf) - (__SIGNAL_FRAMESIZE + 16);
1054 	addr = (void __user *)regs->gpr[1];
1055 	if (put_user(regs->gpr[1], (u32 __user *)newsp))
1056 		goto badframe;
1057 
1058 	/* Fill registers for signal handler */
1059 	regs->gpr[1] = newsp;
1060 	regs->gpr[3] = ksig->sig;
1061 	regs->gpr[4] = (unsigned long) &rt_sf->info;
1062 	regs->gpr[5] = (unsigned long) &rt_sf->uc;
1063 	regs->gpr[6] = (unsigned long) rt_sf;
1064 	regs->nip = (unsigned long) ksig->ka.sa.sa_handler;
1065 	/* enter the signal handler in native-endian mode */
1066 	regs->msr &= ~MSR_LE;
1067 	regs->msr |= (MSR_KERNEL & MSR_LE);
1068 	return 0;
1069 
1070 badframe:
1071 	if (show_unhandled_signals)
1072 		printk_ratelimited(KERN_INFO
1073 				   "%s[%d]: bad frame in handle_rt_signal32: "
1074 				   "%p nip %08lx lr %08lx\n",
1075 				   current->comm, current->pid,
1076 				   addr, regs->nip, regs->link);
1077 
1078 	return 1;
1079 }
1080 
1081 static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
1082 {
1083 	sigset_t set;
1084 	struct mcontext __user *mcp;
1085 
1086 	if (get_sigset_t(&set, &ucp->uc_sigmask))
1087 		return -EFAULT;
1088 #ifdef CONFIG_PPC64
1089 	{
1090 		u32 cmcp;
1091 
1092 		if (__get_user(cmcp, &ucp->uc_regs))
1093 			return -EFAULT;
1094 		mcp = (struct mcontext __user *)(u64)cmcp;
1095 		/* no need to check access_ok(mcp), since mcp < 4GB */
1096 	}
1097 #else
1098 	if (__get_user(mcp, &ucp->uc_regs))
1099 		return -EFAULT;
1100 	if (!access_ok(VERIFY_READ, mcp, sizeof(*mcp)))
1101 		return -EFAULT;
1102 #endif
1103 	set_current_blocked(&set);
1104 	if (restore_user_regs(regs, mcp, sig))
1105 		return -EFAULT;
1106 
1107 	return 0;
1108 }
1109 
1110 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1111 static int do_setcontext_tm(struct ucontext __user *ucp,
1112 			    struct ucontext __user *tm_ucp,
1113 			    struct pt_regs *regs)
1114 {
1115 	sigset_t set;
1116 	struct mcontext __user *mcp;
1117 	struct mcontext __user *tm_mcp;
1118 	u32 cmcp;
1119 	u32 tm_cmcp;
1120 
1121 	if (get_sigset_t(&set, &ucp->uc_sigmask))
1122 		return -EFAULT;
1123 
1124 	if (__get_user(cmcp, &ucp->uc_regs) ||
1125 	    __get_user(tm_cmcp, &tm_ucp->uc_regs))
1126 		return -EFAULT;
1127 	mcp = (struct mcontext __user *)(u64)cmcp;
1128 	tm_mcp = (struct mcontext __user *)(u64)tm_cmcp;
1129 	/* no need to check access_ok(mcp), since mcp < 4GB */
1130 
1131 	set_current_blocked(&set);
1132 	if (restore_tm_user_regs(regs, mcp, tm_mcp))
1133 		return -EFAULT;
1134 
1135 	return 0;
1136 }
1137 #endif
1138 
1139 long sys_swapcontext(struct ucontext __user *old_ctx,
1140 		     struct ucontext __user *new_ctx,
1141 		     int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
1142 {
1143 	unsigned char tmp;
1144 	int ctx_has_vsx_region = 0;
1145 
1146 #ifdef CONFIG_PPC64
1147 	unsigned long new_msr = 0;
1148 
1149 	if (new_ctx) {
1150 		struct mcontext __user *mcp;
1151 		u32 cmcp;
1152 
1153 		/*
1154 		 * Get pointer to the real mcontext.  No need for
1155 		 * access_ok since we are dealing with compat
1156 		 * pointers.
1157 		 */
1158 		if (__get_user(cmcp, &new_ctx->uc_regs))
1159 			return -EFAULT;
1160 		mcp = (struct mcontext __user *)(u64)cmcp;
1161 		if (__get_user(new_msr, &mcp->mc_gregs[PT_MSR]))
1162 			return -EFAULT;
1163 	}
1164 	/*
1165 	 * Check that the context is not smaller than the original
1166 	 * size (with VMX but without VSX)
1167 	 */
1168 	if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
1169 		return -EINVAL;
1170 	/*
1171 	 * If the new context state sets the MSR VSX bits but
1172 	 * it doesn't provide VSX state.
1173 	 */
1174 	if ((ctx_size < sizeof(struct ucontext)) &&
1175 	    (new_msr & MSR_VSX))
1176 		return -EINVAL;
1177 	/* Does the context have enough room to store VSX data? */
1178 	if (ctx_size >= sizeof(struct ucontext))
1179 		ctx_has_vsx_region = 1;
1180 #else
1181 	/* Context size is for future use. Right now, we only make sure
1182 	 * we are passed something we understand
1183 	 */
1184 	if (ctx_size < sizeof(struct ucontext))
1185 		return -EINVAL;
1186 #endif
1187 	if (old_ctx != NULL) {
1188 		struct mcontext __user *mctx;
1189 
1190 		/*
1191 		 * old_ctx might not be 16-byte aligned, in which
1192 		 * case old_ctx->uc_mcontext won't be either.
1193 		 * Because we have the old_ctx->uc_pad2 field
1194 		 * before old_ctx->uc_mcontext, we need to round down
1195 		 * from &old_ctx->uc_mcontext to a 16-byte boundary.
1196 		 */
1197 		mctx = (struct mcontext __user *)
1198 			((unsigned long) &old_ctx->uc_mcontext & ~0xfUL);
1199 		if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
1200 		    || save_user_regs(regs, mctx, NULL, 0, ctx_has_vsx_region)
1201 		    || put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
1202 		    || __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
1203 			return -EFAULT;
1204 	}
1205 	if (new_ctx == NULL)
1206 		return 0;
1207 	if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
1208 	    || __get_user(tmp, (u8 __user *) new_ctx)
1209 	    || __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
1210 		return -EFAULT;
1211 
1212 	/*
1213 	 * If we get a fault copying the context into the kernel's
1214 	 * image of the user's registers, we can't just return -EFAULT
1215 	 * because the user's registers will be corrupted.  For instance
1216 	 * the NIP value may have been updated but not some of the
1217 	 * other registers.  Given that we have done the access_ok
1218 	 * and successfully read the first and last bytes of the region
1219 	 * above, this should only happen in an out-of-memory situation
1220 	 * or if another thread unmaps the region containing the context.
1221 	 * We kill the task with a SIGSEGV in this situation.
1222 	 */
1223 	if (do_setcontext(new_ctx, regs, 0))
1224 		do_exit(SIGSEGV);
1225 
1226 	set_thread_flag(TIF_RESTOREALL);
1227 	return 0;
1228 }
1229 
1230 long sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
1231 		     struct pt_regs *regs)
1232 {
1233 	struct rt_sigframe __user *rt_sf;
1234 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1235 	struct ucontext __user *uc_transact;
1236 	unsigned long msr_hi;
1237 	unsigned long tmp;
1238 	int tm_restore = 0;
1239 #endif
1240 	/* Always make any pending restarted system calls return -EINTR */
1241 	current->restart_block.fn = do_no_restart_syscall;
1242 
1243 	rt_sf = (struct rt_sigframe __user *)
1244 		(regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
1245 	if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
1246 		goto bad;
1247 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1248 	if (__get_user(tmp, &rt_sf->uc.uc_link))
1249 		goto bad;
1250 	uc_transact = (struct ucontext __user *)(uintptr_t)tmp;
1251 	if (uc_transact) {
1252 		u32 cmcp;
1253 		struct mcontext __user *mcp;
1254 
1255 		if (__get_user(cmcp, &uc_transact->uc_regs))
1256 			return -EFAULT;
1257 		mcp = (struct mcontext __user *)(u64)cmcp;
1258 		/* The top 32 bits of the MSR are stashed in the transactional
1259 		 * ucontext. */
1260 		if (__get_user(msr_hi, &mcp->mc_gregs[PT_MSR]))
1261 			goto bad;
1262 
1263 		if (MSR_TM_ACTIVE(msr_hi<<32)) {
1264 			/* We only recheckpoint on return if we're
1265 			 * transaction.
1266 			 */
1267 			tm_restore = 1;
1268 			if (do_setcontext_tm(&rt_sf->uc, uc_transact, regs))
1269 				goto bad;
1270 		}
1271 	}
1272 	if (!tm_restore)
1273 		/* Fall through, for non-TM restore */
1274 #endif
1275 	if (do_setcontext(&rt_sf->uc, regs, 1))
1276 		goto bad;
1277 
1278 	/*
1279 	 * It's not clear whether or why it is desirable to save the
1280 	 * sigaltstack setting on signal delivery and restore it on
1281 	 * signal return.  But other architectures do this and we have
1282 	 * always done it up until now so it is probably better not to
1283 	 * change it.  -- paulus
1284 	 */
1285 #ifdef CONFIG_PPC64
1286 	if (compat_restore_altstack(&rt_sf->uc.uc_stack))
1287 		goto bad;
1288 #else
1289 	if (restore_altstack(&rt_sf->uc.uc_stack))
1290 		goto bad;
1291 #endif
1292 	set_thread_flag(TIF_RESTOREALL);
1293 	return 0;
1294 
1295  bad:
1296 	if (show_unhandled_signals)
1297 		printk_ratelimited(KERN_INFO
1298 				   "%s[%d]: bad frame in sys_rt_sigreturn: "
1299 				   "%p nip %08lx lr %08lx\n",
1300 				   current->comm, current->pid,
1301 				   rt_sf, regs->nip, regs->link);
1302 
1303 	force_sig(SIGSEGV, current);
1304 	return 0;
1305 }
1306 
1307 #ifdef CONFIG_PPC32
1308 int sys_debug_setcontext(struct ucontext __user *ctx,
1309 			 int ndbg, struct sig_dbg_op __user *dbg,
1310 			 int r6, int r7, int r8,
1311 			 struct pt_regs *regs)
1312 {
1313 	struct sig_dbg_op op;
1314 	int i;
1315 	unsigned char tmp;
1316 	unsigned long new_msr = regs->msr;
1317 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1318 	unsigned long new_dbcr0 = current->thread.debug.dbcr0;
1319 #endif
1320 
1321 	for (i=0; i<ndbg; i++) {
1322 		if (copy_from_user(&op, dbg + i, sizeof(op)))
1323 			return -EFAULT;
1324 		switch (op.dbg_type) {
1325 		case SIG_DBG_SINGLE_STEPPING:
1326 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1327 			if (op.dbg_value) {
1328 				new_msr |= MSR_DE;
1329 				new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
1330 			} else {
1331 				new_dbcr0 &= ~DBCR0_IC;
1332 				if (!DBCR_ACTIVE_EVENTS(new_dbcr0,
1333 						current->thread.debug.dbcr1)) {
1334 					new_msr &= ~MSR_DE;
1335 					new_dbcr0 &= ~DBCR0_IDM;
1336 				}
1337 			}
1338 #else
1339 			if (op.dbg_value)
1340 				new_msr |= MSR_SE;
1341 			else
1342 				new_msr &= ~MSR_SE;
1343 #endif
1344 			break;
1345 		case SIG_DBG_BRANCH_TRACING:
1346 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1347 			return -EINVAL;
1348 #else
1349 			if (op.dbg_value)
1350 				new_msr |= MSR_BE;
1351 			else
1352 				new_msr &= ~MSR_BE;
1353 #endif
1354 			break;
1355 
1356 		default:
1357 			return -EINVAL;
1358 		}
1359 	}
1360 
1361 	/* We wait until here to actually install the values in the
1362 	   registers so if we fail in the above loop, it will not
1363 	   affect the contents of these registers.  After this point,
1364 	   failure is a problem, anyway, and it's very unlikely unless
1365 	   the user is really doing something wrong. */
1366 	regs->msr = new_msr;
1367 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1368 	current->thread.debug.dbcr0 = new_dbcr0;
1369 #endif
1370 
1371 	if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx))
1372 	    || __get_user(tmp, (u8 __user *) ctx)
1373 	    || __get_user(tmp, (u8 __user *) (ctx + 1) - 1))
1374 		return -EFAULT;
1375 
1376 	/*
1377 	 * If we get a fault copying the context into the kernel's
1378 	 * image of the user's registers, we can't just return -EFAULT
1379 	 * because the user's registers will be corrupted.  For instance
1380 	 * the NIP value may have been updated but not some of the
1381 	 * other registers.  Given that we have done the access_ok
1382 	 * and successfully read the first and last bytes of the region
1383 	 * above, this should only happen in an out-of-memory situation
1384 	 * or if another thread unmaps the region containing the context.
1385 	 * We kill the task with a SIGSEGV in this situation.
1386 	 */
1387 	if (do_setcontext(ctx, regs, 1)) {
1388 		if (show_unhandled_signals)
1389 			printk_ratelimited(KERN_INFO "%s[%d]: bad frame in "
1390 					   "sys_debug_setcontext: %p nip %08lx "
1391 					   "lr %08lx\n",
1392 					   current->comm, current->pid,
1393 					   ctx, regs->nip, regs->link);
1394 
1395 		force_sig(SIGSEGV, current);
1396 		goto out;
1397 	}
1398 
1399 	/*
1400 	 * It's not clear whether or why it is desirable to save the
1401 	 * sigaltstack setting on signal delivery and restore it on
1402 	 * signal return.  But other architectures do this and we have
1403 	 * always done it up until now so it is probably better not to
1404 	 * change it.  -- paulus
1405 	 */
1406 	restore_altstack(&ctx->uc_stack);
1407 
1408 	set_thread_flag(TIF_RESTOREALL);
1409  out:
1410 	return 0;
1411 }
1412 #endif
1413 
1414 /*
1415  * OK, we're invoking a handler
1416  */
1417 int handle_signal32(struct ksignal *ksig, sigset_t *oldset, struct pt_regs *regs)
1418 {
1419 	struct sigcontext __user *sc;
1420 	struct sigframe __user *frame;
1421 	struct mcontext __user *tm_mctx = NULL;
1422 	unsigned long newsp = 0;
1423 	int sigret;
1424 	unsigned long tramp;
1425 
1426 	/* Set up Signal Frame */
1427 	frame = get_sigframe(ksig, get_tm_stackpointer(regs), sizeof(*frame), 1);
1428 	if (unlikely(frame == NULL))
1429 		goto badframe;
1430 	sc = (struct sigcontext __user *) &frame->sctx;
1431 
1432 #if _NSIG != 64
1433 #error "Please adjust handle_signal()"
1434 #endif
1435 	if (__put_user(to_user_ptr(ksig->ka.sa.sa_handler), &sc->handler)
1436 	    || __put_user(oldset->sig[0], &sc->oldmask)
1437 #ifdef CONFIG_PPC64
1438 	    || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
1439 #else
1440 	    || __put_user(oldset->sig[1], &sc->_unused[3])
1441 #endif
1442 	    || __put_user(to_user_ptr(&frame->mctx), &sc->regs)
1443 	    || __put_user(ksig->sig, &sc->signal))
1444 		goto badframe;
1445 
1446 	if (vdso32_sigtramp && current->mm->context.vdso_base) {
1447 		sigret = 0;
1448 		tramp = current->mm->context.vdso_base + vdso32_sigtramp;
1449 	} else {
1450 		sigret = __NR_sigreturn;
1451 		tramp = (unsigned long) frame->mctx.tramp;
1452 	}
1453 
1454 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1455 	tm_mctx = &frame->mctx_transact;
1456 	if (MSR_TM_ACTIVE(regs->msr)) {
1457 		if (save_tm_user_regs(regs, &frame->mctx, &frame->mctx_transact,
1458 				      sigret))
1459 			goto badframe;
1460 	}
1461 	else
1462 #endif
1463 	{
1464 		if (save_user_regs(regs, &frame->mctx, tm_mctx, sigret, 1))
1465 			goto badframe;
1466 	}
1467 
1468 	regs->link = tramp;
1469 
1470 	current->thread.fp_state.fpscr = 0;	/* turn off all fp exceptions */
1471 
1472 	/* create a stack frame for the caller of the handler */
1473 	newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
1474 	if (put_user(regs->gpr[1], (u32 __user *)newsp))
1475 		goto badframe;
1476 
1477 	regs->gpr[1] = newsp;
1478 	regs->gpr[3] = ksig->sig;
1479 	regs->gpr[4] = (unsigned long) sc;
1480 	regs->nip = (unsigned long) (unsigned long)ksig->ka.sa.sa_handler;
1481 	/* enter the signal handler in big-endian mode */
1482 	regs->msr &= ~MSR_LE;
1483 	return 0;
1484 
1485 badframe:
1486 	if (show_unhandled_signals)
1487 		printk_ratelimited(KERN_INFO
1488 				   "%s[%d]: bad frame in handle_signal32: "
1489 				   "%p nip %08lx lr %08lx\n",
1490 				   current->comm, current->pid,
1491 				   frame, regs->nip, regs->link);
1492 
1493 	return 1;
1494 }
1495 
1496 /*
1497  * Do a signal return; undo the signal stack.
1498  */
1499 long sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
1500 		       struct pt_regs *regs)
1501 {
1502 	struct sigframe __user *sf;
1503 	struct sigcontext __user *sc;
1504 	struct sigcontext sigctx;
1505 	struct mcontext __user *sr;
1506 	void __user *addr;
1507 	sigset_t set;
1508 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1509 	struct mcontext __user *mcp, *tm_mcp;
1510 	unsigned long msr_hi;
1511 #endif
1512 
1513 	/* Always make any pending restarted system calls return -EINTR */
1514 	current->restart_block.fn = do_no_restart_syscall;
1515 
1516 	sf = (struct sigframe __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
1517 	sc = &sf->sctx;
1518 	addr = sc;
1519 	if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
1520 		goto badframe;
1521 
1522 #ifdef CONFIG_PPC64
1523 	/*
1524 	 * Note that PPC32 puts the upper 32 bits of the sigmask in the
1525 	 * unused part of the signal stackframe
1526 	 */
1527 	set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
1528 #else
1529 	set.sig[0] = sigctx.oldmask;
1530 	set.sig[1] = sigctx._unused[3];
1531 #endif
1532 	set_current_blocked(&set);
1533 
1534 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1535 	mcp = (struct mcontext __user *)&sf->mctx;
1536 	tm_mcp = (struct mcontext __user *)&sf->mctx_transact;
1537 	if (__get_user(msr_hi, &tm_mcp->mc_gregs[PT_MSR]))
1538 		goto badframe;
1539 	if (MSR_TM_ACTIVE(msr_hi<<32)) {
1540 		if (!cpu_has_feature(CPU_FTR_TM))
1541 			goto badframe;
1542 		if (restore_tm_user_regs(regs, mcp, tm_mcp))
1543 			goto badframe;
1544 	} else
1545 #endif
1546 	{
1547 		sr = (struct mcontext __user *)from_user_ptr(sigctx.regs);
1548 		addr = sr;
1549 		if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
1550 		    || restore_user_regs(regs, sr, 1))
1551 			goto badframe;
1552 	}
1553 
1554 	set_thread_flag(TIF_RESTOREALL);
1555 	return 0;
1556 
1557 badframe:
1558 	if (show_unhandled_signals)
1559 		printk_ratelimited(KERN_INFO
1560 				   "%s[%d]: bad frame in sys_sigreturn: "
1561 				   "%p nip %08lx lr %08lx\n",
1562 				   current->comm, current->pid,
1563 				   addr, regs->nip, regs->link);
1564 
1565 	force_sig(SIGSEGV, current);
1566 	return 0;
1567 }
1568