blob: 488a7c542a3ae7e68ae12599557fb6198eb377ed [file] [log] [blame]
Stephen Rothwell81e70092005-10-18 11:17:58 +10001/*
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
Stephen Rothwell81e70092005-10-18 11:17:58 +100020#include <linux/sched.h>
21#include <linux/mm.h>
22#include <linux/smp.h>
Stephen Rothwell81e70092005-10-18 11:17:58 +100023#include <linux/kernel.h>
24#include <linux/signal.h>
25#include <linux/errno.h>
26#include <linux/elf.h>
Lucas Woods05ead012007-12-13 15:56:06 -080027#include <linux/ptrace.h>
Christian Dietrich76462232011-06-04 05:36:54 +000028#include <linux/ratelimit.h>
Stephen Rothwell81e70092005-10-18 11:17:58 +100029#ifdef CONFIG_PPC64
30#include <linux/syscalls.h>
31#include <linux/compat.h>
Stephen Rothwell81e70092005-10-18 11:17:58 +100032#else
33#include <linux/wait.h>
Stephen Rothwell81e70092005-10-18 11:17:58 +100034#include <linux/unistd.h>
35#include <linux/stddef.h>
36#include <linux/tty.h>
37#include <linux/binfmts.h>
Stephen Rothwell81e70092005-10-18 11:17:58 +100038#endif
39
40#include <asm/uaccess.h>
41#include <asm/cacheflush.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010042#include <asm/syscalls.h>
David Gibsonc5ff7002005-11-09 11:21:07 +110043#include <asm/sigcontext.h>
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +110044#include <asm/vdso.h>
David Howellsae3a1972012-03-28 18:30:02 +010045#include <asm/switch_to.h>
Stephen Rothwell81e70092005-10-18 11:17:58 +100046#ifdef CONFIG_PPC64
Stephen Rothwell879168e2005-11-03 15:32:07 +110047#include "ppc32.h"
Stephen Rothwell81e70092005-10-18 11:17:58 +100048#include <asm/unistd.h>
Stephen Rothwell81e70092005-10-18 11:17:58 +100049#else
50#include <asm/ucontext.h>
51#include <asm/pgtable.h>
52#endif
53
Benjamin Herrenschmidt22e38f22007-06-04 15:15:49 +100054#include "signal.h"
55
Stephen Rothwell81e70092005-10-18 11:17:58 +100056#undef DEBUG_SIG
57
Stephen Rothwell81e70092005-10-18 11:17:58 +100058#ifdef CONFIG_PPC64
Stephen Rothwellb09a4912005-10-18 14:51:57 +100059#define sys_rt_sigreturn compat_sys_rt_sigreturn
60#define sys_sigaction compat_sys_sigaction
61#define sys_swapcontext compat_sys_swapcontext
62#define sys_sigreturn compat_sys_sigreturn
Stephen Rothwell81e70092005-10-18 11:17:58 +100063
64#define old_sigaction old_sigaction32
65#define sigcontext sigcontext32
66#define mcontext mcontext32
67#define ucontext ucontext32
68
Al Viro7cce2462012-12-23 03:26:46 -050069#define __save_altstack __compat_save_altstack
70
Stephen Rothwell81e70092005-10-18 11:17:58 +100071/*
Michael Neulingc1cb2992008-07-08 18:43:41 +100072 * Userspace code may pass a ucontext which doesn't include VSX added
73 * at the end. We need to check for this case.
74 */
75#define UCONTEXTSIZEWITHOUTVSX \
76 (sizeof(struct ucontext) - sizeof(elf_vsrreghalf_t32))
77
78/*
Stephen Rothwell81e70092005-10-18 11:17:58 +100079 * Returning 0 means we return to userspace via
80 * ret_from_except and thus restore all user
81 * registers from *regs. This is what we need
82 * to do when a signal has been delivered.
83 */
Stephen Rothwell81e70092005-10-18 11:17:58 +100084
85#define GP_REGS_SIZE min(sizeof(elf_gregset_t32), sizeof(struct pt_regs32))
86#undef __SIGNAL_FRAMESIZE
87#define __SIGNAL_FRAMESIZE __SIGNAL_FRAMESIZE32
88#undef ELF_NVRREG
89#define ELF_NVRREG ELF_NVRREG32
90
91/*
92 * Functions for flipping sigsets (thanks to brain dead generic
93 * implementation that makes things simple for little endian only)
94 */
95static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
96{
97 compat_sigset_t cset;
98
99 switch (_NSIG_WORDS) {
Will Deacona313f4c2011-11-08 04:51:19 +0000100 case 4: cset.sig[6] = set->sig[3] & 0xffffffffull;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000101 cset.sig[7] = set->sig[3] >> 32;
102 case 3: cset.sig[4] = set->sig[2] & 0xffffffffull;
103 cset.sig[5] = set->sig[2] >> 32;
104 case 2: cset.sig[2] = set->sig[1] & 0xffffffffull;
105 cset.sig[3] = set->sig[1] >> 32;
106 case 1: cset.sig[0] = set->sig[0] & 0xffffffffull;
107 cset.sig[1] = set->sig[0] >> 32;
108 }
109 return copy_to_user(uset, &cset, sizeof(*uset));
110}
111
Paul Mackerras9b7cf8b2005-10-19 23:13:04 +1000112static inline int get_sigset_t(sigset_t *set,
113 const compat_sigset_t __user *uset)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000114{
115 compat_sigset_t s32;
116
117 if (copy_from_user(&s32, uset, sizeof(*uset)))
118 return -EFAULT;
119
120 /*
121 * Swap the 2 words of the 64-bit sigset_t (they are stored
122 * in the "wrong" endian in 32-bit user storage).
123 */
124 switch (_NSIG_WORDS) {
125 case 4: set->sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
126 case 3: set->sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
127 case 2: set->sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
128 case 1: set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
129 }
130 return 0;
131}
132
133static inline int get_old_sigaction(struct k_sigaction *new_ka,
134 struct old_sigaction __user *act)
135{
136 compat_old_sigset_t mask;
137 compat_uptr_t handler, restorer;
138
139 if (get_user(handler, &act->sa_handler) ||
140 __get_user(restorer, &act->sa_restorer) ||
141 __get_user(new_ka->sa.sa_flags, &act->sa_flags) ||
142 __get_user(mask, &act->sa_mask))
143 return -EFAULT;
144 new_ka->sa.sa_handler = compat_ptr(handler);
145 new_ka->sa.sa_restorer = compat_ptr(restorer);
146 siginitset(&new_ka->sa.sa_mask, mask);
147 return 0;
148}
149
Al Viro29e646d2006-02-01 05:28:09 -0500150#define to_user_ptr(p) ptr_to_compat(p)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000151#define from_user_ptr(p) compat_ptr(p)
152
153static inline int save_general_regs(struct pt_regs *regs,
154 struct mcontext __user *frame)
155{
156 elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
157 int i;
158
Paul Mackerras1bd79332006-03-08 13:24:22 +1100159 WARN_ON(!FULL_REGS(regs));
David Woodhouse401d1f02005-11-15 18:52:18 +0000160
161 for (i = 0; i <= PT_RESULT; i ++) {
162 if (i == 14 && !FULL_REGS(regs))
163 i = 32;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000164 if (__put_user((unsigned int)gregs[i], &frame->mc_gregs[i]))
165 return -EFAULT;
David Woodhouse401d1f02005-11-15 18:52:18 +0000166 }
Stephen Rothwell81e70092005-10-18 11:17:58 +1000167 return 0;
168}
169
170static inline int restore_general_regs(struct pt_regs *regs,
171 struct mcontext __user *sr)
172{
173 elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
174 int i;
175
176 for (i = 0; i <= PT_RESULT; i++) {
177 if ((i == PT_MSR) || (i == PT_SOFTE))
178 continue;
179 if (__get_user(gregs[i], &sr->mc_gregs[i]))
180 return -EFAULT;
181 }
182 return 0;
183}
184
185#else /* CONFIG_PPC64 */
186
Stephen Rothwell81e70092005-10-18 11:17:58 +1000187#define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
188
189static inline int put_sigset_t(sigset_t __user *uset, sigset_t *set)
190{
191 return copy_to_user(uset, set, sizeof(*uset));
192}
193
Paul Mackerras9b7cf8b2005-10-19 23:13:04 +1000194static inline int get_sigset_t(sigset_t *set, const sigset_t __user *uset)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000195{
196 return copy_from_user(set, uset, sizeof(*uset));
197}
198
199static inline int get_old_sigaction(struct k_sigaction *new_ka,
200 struct old_sigaction __user *act)
201{
202 old_sigset_t mask;
203
204 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
205 __get_user(new_ka->sa.sa_handler, &act->sa_handler) ||
Al Viro43f16812012-04-22 17:01:49 -0400206 __get_user(new_ka->sa.sa_restorer, &act->sa_restorer) ||
207 __get_user(new_ka->sa.sa_flags, &act->sa_flags) ||
208 __get_user(mask, &act->sa_mask))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000209 return -EFAULT;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000210 siginitset(&new_ka->sa.sa_mask, mask);
211 return 0;
212}
213
Al Viro29e646d2006-02-01 05:28:09 -0500214#define to_user_ptr(p) ((unsigned long)(p))
215#define from_user_ptr(p) ((void __user *)(p))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000216
217static inline int save_general_regs(struct pt_regs *regs,
218 struct mcontext __user *frame)
219{
Paul Mackerras1bd79332006-03-08 13:24:22 +1100220 WARN_ON(!FULL_REGS(regs));
Stephen Rothwell81e70092005-10-18 11:17:58 +1000221 return __copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE);
222}
223
224static inline int restore_general_regs(struct pt_regs *regs,
225 struct mcontext __user *sr)
226{
227 /* copy up to but not including MSR */
228 if (__copy_from_user(regs, &sr->mc_gregs,
229 PT_MSR * sizeof(elf_greg_t)))
230 return -EFAULT;
231 /* copy from orig_r3 (the word after the MSR) up to the end */
232 if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
233 GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
234 return -EFAULT;
235 return 0;
236}
237
238#endif /* CONFIG_PPC64 */
239
Stephen Rothwell81e70092005-10-18 11:17:58 +1000240long sys_sigaction(int sig, struct old_sigaction __user *act,
241 struct old_sigaction __user *oact)
242{
243 struct k_sigaction new_ka, old_ka;
244 int ret;
245
246#ifdef CONFIG_PPC64
247 if (sig < 0)
248 sig = -sig;
249#endif
250
251 if (act) {
252 if (get_old_sigaction(&new_ka, act))
253 return -EFAULT;
254 }
255
256 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
257 if (!ret && oact) {
258 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
259 __put_user(to_user_ptr(old_ka.sa.sa_handler),
260 &oact->sa_handler) ||
261 __put_user(to_user_ptr(old_ka.sa.sa_restorer),
262 &oact->sa_restorer) ||
263 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
264 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
265 return -EFAULT;
266 }
267
268 return ret;
269}
270
271/*
272 * When we have signals to deliver, we set up on the
273 * user stack, going down from the original stack pointer:
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000274 * an ABI gap of 56 words
275 * an mcontext struct
Stephen Rothwell81e70092005-10-18 11:17:58 +1000276 * a sigcontext struct
277 * a gap of __SIGNAL_FRAMESIZE bytes
278 *
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000279 * Each of these things must be a multiple of 16 bytes in size. The following
280 * structure represent all of this except the __SIGNAL_FRAMESIZE gap
Stephen Rothwell81e70092005-10-18 11:17:58 +1000281 *
282 */
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000283struct sigframe {
284 struct sigcontext sctx; /* the sigcontext */
Stephen Rothwell81e70092005-10-18 11:17:58 +1000285 struct mcontext mctx; /* all the register values */
286 /*
287 * Programs using the rs6000/xcoff abi can save up to 19 gp
288 * regs and 18 fp regs below sp before decrementing it.
289 */
290 int abigap[56];
291};
292
293/* We use the mc_pad field for the signal return trampoline. */
294#define tramp mc_pad
295
296/*
297 * When we have rt signals to deliver, we set up on the
298 * user stack, going down from the original stack pointer:
299 * one rt_sigframe struct (siginfo + ucontext + ABI gap)
300 * a gap of __SIGNAL_FRAMESIZE+16 bytes
301 * (the +16 is to get the siginfo and ucontext in the same
302 * positions as in older kernels).
303 *
304 * Each of these things must be a multiple of 16 bytes in size.
305 *
306 */
307struct rt_sigframe {
308#ifdef CONFIG_PPC64
309 compat_siginfo_t info;
310#else
311 struct siginfo info;
312#endif
313 struct ucontext uc;
314 /*
315 * Programs using the rs6000/xcoff abi can save up to 19 gp
316 * regs and 18 fp regs below sp before decrementing it.
317 */
318 int abigap[56];
319};
320
Michael Neuling6a274c02008-07-02 14:06:37 +1000321#ifdef CONFIG_VSX
322unsigned long copy_fpr_to_user(void __user *to,
323 struct task_struct *task)
324{
325 double buf[ELF_NFPREG];
326 int i;
327
328 /* save FPR copy to local buffer then write to the thread_struct */
329 for (i = 0; i < (ELF_NFPREG - 1) ; i++)
330 buf[i] = task->thread.TS_FPR(i);
331 memcpy(&buf[i], &task->thread.fpscr, sizeof(double));
332 return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
333}
334
335unsigned long copy_fpr_from_user(struct task_struct *task,
336 void __user *from)
337{
338 double buf[ELF_NFPREG];
339 int i;
340
341 if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
342 return 1;
343 for (i = 0; i < (ELF_NFPREG - 1) ; i++)
344 task->thread.TS_FPR(i) = buf[i];
345 memcpy(&task->thread.fpscr, &buf[i], sizeof(double));
346
347 return 0;
348}
349
350unsigned long copy_vsx_to_user(void __user *to,
351 struct task_struct *task)
352{
353 double buf[ELF_NVSRHALFREG];
354 int i;
355
356 /* save FPR copy to local buffer then write to the thread_struct */
357 for (i = 0; i < ELF_NVSRHALFREG; i++)
358 buf[i] = task->thread.fpr[i][TS_VSRLOWOFFSET];
359 return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
360}
361
362unsigned long copy_vsx_from_user(struct task_struct *task,
363 void __user *from)
364{
365 double buf[ELF_NVSRHALFREG];
366 int i;
367
368 if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
369 return 1;
370 for (i = 0; i < ELF_NVSRHALFREG ; i++)
371 task->thread.fpr[i][TS_VSRLOWOFFSET] = buf[i];
372 return 0;
373}
374#else
375inline unsigned long copy_fpr_to_user(void __user *to,
376 struct task_struct *task)
377{
378 return __copy_to_user(to, task->thread.fpr,
379 ELF_NFPREG * sizeof(double));
380}
381
382inline unsigned long copy_fpr_from_user(struct task_struct *task,
383 void __user *from)
384{
385 return __copy_from_user(task->thread.fpr, from,
386 ELF_NFPREG * sizeof(double));
387}
388#endif
389
Stephen Rothwell81e70092005-10-18 11:17:58 +1000390/*
391 * Save the current user registers on the user stack.
392 * We only save the altivec/spe registers if the process has used
393 * altivec/spe instructions at some point.
394 */
395static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
Michael Neuling16c29d12008-10-23 00:42:36 +0000396 int sigret, int ctx_has_vsx_region)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000397{
Michael Neuling9e751182008-06-25 14:07:17 +1000398 unsigned long msr = regs->msr;
399
Stephen Rothwell81e70092005-10-18 11:17:58 +1000400 /* Make sure floating point registers are stored in regs */
401 flush_fp_to_thread(current);
402
Michael Neulingc6e67712008-06-25 14:07:18 +1000403 /* save general registers */
404 if (save_general_regs(regs, frame))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000405 return 1;
406
Stephen Rothwell81e70092005-10-18 11:17:58 +1000407#ifdef CONFIG_ALTIVEC
408 /* save altivec registers */
409 if (current->thread.used_vr) {
410 flush_altivec_to_thread(current);
411 if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
412 ELF_NVRREG * sizeof(vector128)))
413 return 1;
414 /* set MSR_VEC in the saved MSR value to indicate that
415 frame->mc_vregs contains valid data */
Michael Neuling9e751182008-06-25 14:07:17 +1000416 msr |= MSR_VEC;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000417 }
418 /* else assert((regs->msr & MSR_VEC) == 0) */
419
420 /* We always copy to/from vrsave, it's 0 if we don't have or don't
421 * use altivec. Since VSCR only contains 32 bits saved in the least
422 * significant bits of a vector, we "cheat" and stuff VRSAVE in the
423 * most significant bits of that same vector. --BenH
424 */
425 if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
426 return 1;
427#endif /* CONFIG_ALTIVEC */
Michael Neuling6a274c02008-07-02 14:06:37 +1000428 if (copy_fpr_to_user(&frame->mc_fregs, current))
Michael Neulingc6e67712008-06-25 14:07:18 +1000429 return 1;
Michael Neuling6a274c02008-07-02 14:06:37 +1000430#ifdef CONFIG_VSX
Michael Neulingce48b212008-06-25 14:07:18 +1000431 /*
432 * Copy VSR 0-31 upper half from thread_struct to local
433 * buffer, then write that to userspace. Also set MSR_VSX in
434 * the saved MSR value to indicate that frame->mc_vregs
435 * contains valid data
436 */
Michael Neuling16c29d12008-10-23 00:42:36 +0000437 if (current->thread.used_vsr && ctx_has_vsx_region) {
Michael Neuling7c292172008-07-11 16:29:12 +1000438 __giveup_vsx(current);
Michael Neuling6a274c02008-07-02 14:06:37 +1000439 if (copy_vsx_to_user(&frame->mc_vsregs, current))
Michael Neulingce48b212008-06-25 14:07:18 +1000440 return 1;
441 msr |= MSR_VSX;
442 }
Michael Neulingc6e67712008-06-25 14:07:18 +1000443#endif /* CONFIG_VSX */
Stephen Rothwell81e70092005-10-18 11:17:58 +1000444#ifdef CONFIG_SPE
445 /* save spe registers */
446 if (current->thread.used_spe) {
447 flush_spe_to_thread(current);
448 if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
449 ELF_NEVRREG * sizeof(u32)))
450 return 1;
451 /* set MSR_SPE in the saved MSR value to indicate that
452 frame->mc_vregs contains valid data */
Michael Neuling9e751182008-06-25 14:07:17 +1000453 msr |= MSR_SPE;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000454 }
455 /* else assert((regs->msr & MSR_SPE) == 0) */
456
457 /* We always copy to/from spefscr */
458 if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
459 return 1;
460#endif /* CONFIG_SPE */
461
Michael Neuling9e751182008-06-25 14:07:17 +1000462 if (__put_user(msr, &frame->mc_gregs[PT_MSR]))
463 return 1;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000464 if (sigret) {
465 /* Set up the sigreturn trampoline: li r0,sigret; sc */
466 if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
467 || __put_user(0x44000002UL, &frame->tramp[1]))
468 return 1;
469 flush_icache_range((unsigned long) &frame->tramp[0],
470 (unsigned long) &frame->tramp[2]);
471 }
472
473 return 0;
474}
475
476/*
477 * Restore the current user register values from the user stack,
478 * (except for MSR).
479 */
480static long restore_user_regs(struct pt_regs *regs,
481 struct mcontext __user *sr, int sig)
482{
483 long err;
484 unsigned int save_r2 = 0;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000485 unsigned long msr;
Michael Neulingc6e67712008-06-25 14:07:18 +1000486#ifdef CONFIG_VSX
Michael Neulingc6e67712008-06-25 14:07:18 +1000487 int i;
488#endif
Stephen Rothwell81e70092005-10-18 11:17:58 +1000489
490 /*
491 * restore general registers but not including MSR or SOFTE. Also
492 * take care of keeping r2 (TLS) intact if not a signal
493 */
494 if (!sig)
495 save_r2 = (unsigned int)regs->gpr[2];
496 err = restore_general_regs(regs, sr);
Al Viro9a81c162010-09-20 21:48:57 +0100497 regs->trap = 0;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000498 err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
Stephen Rothwell81e70092005-10-18 11:17:58 +1000499 if (!sig)
500 regs->gpr[2] = (unsigned long) save_r2;
501 if (err)
502 return 1;
503
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000504 /* if doing signal return, restore the previous little-endian mode */
505 if (sig)
506 regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
507
Paul Mackerras5388fb12006-01-11 22:11:39 +1100508 /*
509 * Do this before updating the thread state in
510 * current->thread.fpr/vr/evr. That way, if we get preempted
511 * and another task grabs the FPU/Altivec/SPE, it won't be
512 * tempted to save the current CPU state into the thread_struct
513 * and corrupt what we are writing there.
514 */
515 discard_lazy_cpu_state();
516
Stephen Rothwell81e70092005-10-18 11:17:58 +1000517#ifdef CONFIG_ALTIVEC
Michael Neulingc6e67712008-06-25 14:07:18 +1000518 /*
519 * Force the process to reload the altivec registers from
520 * current->thread when it next does altivec instructions
521 */
Stephen Rothwell81e70092005-10-18 11:17:58 +1000522 regs->msr &= ~MSR_VEC;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000523 if (msr & MSR_VEC) {
Stephen Rothwell81e70092005-10-18 11:17:58 +1000524 /* restore altivec registers from the stack */
525 if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
526 sizeof(sr->mc_vregs)))
527 return 1;
528 } else if (current->thread.used_vr)
529 memset(current->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
530
531 /* Always get VRSAVE back */
532 if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
533 return 1;
534#endif /* CONFIG_ALTIVEC */
Michael Neuling6a274c02008-07-02 14:06:37 +1000535 if (copy_fpr_from_user(current, &sr->mc_fregs))
536 return 1;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000537
Michael Neulingc6e67712008-06-25 14:07:18 +1000538#ifdef CONFIG_VSX
Michael Neulingce48b212008-06-25 14:07:18 +1000539 /*
540 * Force the process to reload the VSX registers from
541 * current->thread when it next does VSX instruction.
542 */
543 regs->msr &= ~MSR_VSX;
544 if (msr & MSR_VSX) {
545 /*
546 * Restore altivec registers from the stack to a local
547 * buffer, then write this out to the thread_struct
548 */
Michael Neuling6a274c02008-07-02 14:06:37 +1000549 if (copy_vsx_from_user(current, &sr->mc_vsregs))
Michael Neulingce48b212008-06-25 14:07:18 +1000550 return 1;
Michael Neulingce48b212008-06-25 14:07:18 +1000551 } else if (current->thread.used_vsr)
552 for (i = 0; i < 32 ; i++)
553 current->thread.fpr[i][TS_VSRLOWOFFSET] = 0;
Michael Neulingc6e67712008-06-25 14:07:18 +1000554#endif /* CONFIG_VSX */
555 /*
556 * force the process to reload the FP registers from
557 * current->thread when it next does FP instructions
558 */
559 regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
560
Stephen Rothwell81e70092005-10-18 11:17:58 +1000561#ifdef CONFIG_SPE
562 /* force the process to reload the spe registers from
563 current->thread when it next does spe instructions */
564 regs->msr &= ~MSR_SPE;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000565 if (msr & MSR_SPE) {
Stephen Rothwell81e70092005-10-18 11:17:58 +1000566 /* restore spe registers from the stack */
567 if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
568 ELF_NEVRREG * sizeof(u32)))
569 return 1;
570 } else if (current->thread.used_spe)
571 memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
572
573 /* Always get SPEFSCR back */
574 if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs + ELF_NEVRREG))
575 return 1;
576#endif /* CONFIG_SPE */
577
Stephen Rothwell81e70092005-10-18 11:17:58 +1000578 return 0;
579}
580
581#ifdef CONFIG_PPC64
Stephen Rothwellb09a4912005-10-18 14:51:57 +1000582long compat_sys_rt_sigaction(int sig, const struct sigaction32 __user *act,
Stephen Rothwell81e70092005-10-18 11:17:58 +1000583 struct sigaction32 __user *oact, size_t sigsetsize)
584{
585 struct k_sigaction new_ka, old_ka;
586 int ret;
587
588 /* XXX: Don't preclude handling different sized sigset_t's. */
589 if (sigsetsize != sizeof(compat_sigset_t))
590 return -EINVAL;
591
592 if (act) {
593 compat_uptr_t handler;
594
595 ret = get_user(handler, &act->sa_handler);
596 new_ka.sa.sa_handler = compat_ptr(handler);
597 ret |= get_sigset_t(&new_ka.sa.sa_mask, &act->sa_mask);
598 ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
599 if (ret)
600 return -EFAULT;
601 }
602
603 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
604 if (!ret && oact) {
Al Viro29e646d2006-02-01 05:28:09 -0500605 ret = put_user(to_user_ptr(old_ka.sa.sa_handler), &oact->sa_handler);
Stephen Rothwell81e70092005-10-18 11:17:58 +1000606 ret |= put_sigset_t(&oact->sa_mask, &old_ka.sa.sa_mask);
607 ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
608 }
609 return ret;
610}
611
Stephen Rothwell81e70092005-10-18 11:17:58 +1000612int copy_siginfo_to_user32(struct compat_siginfo __user *d, siginfo_t *s)
613{
614 int err;
615
616 if (!access_ok (VERIFY_WRITE, d, sizeof(*d)))
617 return -EFAULT;
618
619 /* If you change siginfo_t structure, please be sure
620 * this code is fixed accordingly.
621 * It should never copy any pad contained in the structure
622 * to avoid security leaks, but must copy the generic
623 * 3 ints plus the relevant union member.
624 * This routine must convert siginfo from 64bit to 32bit as well
625 * at the same time.
626 */
627 err = __put_user(s->si_signo, &d->si_signo);
628 err |= __put_user(s->si_errno, &d->si_errno);
629 err |= __put_user((short)s->si_code, &d->si_code);
630 if (s->si_code < 0)
631 err |= __copy_to_user(&d->_sifields._pad, &s->_sifields._pad,
632 SI_PAD_SIZE32);
633 else switch(s->si_code >> 16) {
634 case __SI_CHLD >> 16:
635 err |= __put_user(s->si_pid, &d->si_pid);
636 err |= __put_user(s->si_uid, &d->si_uid);
637 err |= __put_user(s->si_utime, &d->si_utime);
638 err |= __put_user(s->si_stime, &d->si_stime);
639 err |= __put_user(s->si_status, &d->si_status);
640 break;
641 case __SI_FAULT >> 16:
642 err |= __put_user((unsigned int)(unsigned long)s->si_addr,
643 &d->si_addr);
644 break;
645 case __SI_POLL >> 16:
646 err |= __put_user(s->si_band, &d->si_band);
647 err |= __put_user(s->si_fd, &d->si_fd);
648 break;
649 case __SI_TIMER >> 16:
650 err |= __put_user(s->si_tid, &d->si_tid);
651 err |= __put_user(s->si_overrun, &d->si_overrun);
652 err |= __put_user(s->si_int, &d->si_int);
653 break;
654 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
655 case __SI_MESGQ >> 16:
656 err |= __put_user(s->si_int, &d->si_int);
657 /* fallthrough */
658 case __SI_KILL >> 16:
659 default:
660 err |= __put_user(s->si_pid, &d->si_pid);
661 err |= __put_user(s->si_uid, &d->si_uid);
662 break;
663 }
664 return err;
665}
666
667#define copy_siginfo_to_user copy_siginfo_to_user32
668
Roland McGrath9c0c44d2008-04-20 08:19:24 +1000669int copy_siginfo_from_user32(siginfo_t *to, struct compat_siginfo __user *from)
670{
671 memset(to, 0, sizeof *to);
672
673 if (copy_from_user(to, from, 3*sizeof(int)) ||
674 copy_from_user(to->_sifields._pad,
675 from->_sifields._pad, SI_PAD_SIZE32))
676 return -EFAULT;
677
678 return 0;
679}
Stephen Rothwell81e70092005-10-18 11:17:58 +1000680#endif /* CONFIG_PPC64 */
681
Stephen Rothwell81e70092005-10-18 11:17:58 +1000682/*
683 * Set up a signal frame for a "real-time" signal handler
684 * (one which gets siginfo).
685 */
Christoph Hellwigf478f542007-06-04 15:15:52 +1000686int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,
Stephen Rothwell81e70092005-10-18 11:17:58 +1000687 siginfo_t *info, sigset_t *oldset,
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000688 struct pt_regs *regs)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000689{
690 struct rt_sigframe __user *rt_sf;
691 struct mcontext __user *frame;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000692 void __user *addr;
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000693 unsigned long newsp = 0;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000694
695 /* Set up Signal Frame */
696 /* Put a Real Time Context onto stack */
Josh Boyerefbda862009-03-25 06:23:59 +0000697 rt_sf = get_sigframe(ka, regs, sizeof(*rt_sf), 1);
Olof Johanssond0c3d532007-10-12 10:20:07 +1000698 addr = rt_sf;
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000699 if (unlikely(rt_sf == NULL))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000700 goto badframe;
701
702 /* Put the siginfo & fill in most of the ucontext */
703 if (copy_siginfo_to_user(&rt_sf->info, info)
704 || __put_user(0, &rt_sf->uc.uc_flags)
705 || __put_user(0, &rt_sf->uc.uc_link)
Al Viro7cce2462012-12-23 03:26:46 -0500706 || __save_altstack(&rt_sf->uc.uc_stack, regs->gpr[1])
Stephen Rothwell81e70092005-10-18 11:17:58 +1000707 || __put_user(to_user_ptr(&rt_sf->uc.uc_mcontext),
708 &rt_sf->uc.uc_regs)
709 || put_sigset_t(&rt_sf->uc.uc_sigmask, oldset))
710 goto badframe;
711
712 /* Save user registers on the stack */
713 frame = &rt_sf->uc.uc_mcontext;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000714 addr = frame;
Benjamin Herrenschmidta5bba932006-05-30 13:51:37 +1000715 if (vdso32_rt_sigtramp && current->mm->context.vdso_base) {
Michael Neuling16c29d12008-10-23 00:42:36 +0000716 if (save_user_regs(regs, frame, 0, 1))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000717 goto badframe;
Benjamin Herrenschmidta5bba932006-05-30 13:51:37 +1000718 regs->link = current->mm->context.vdso_base + vdso32_rt_sigtramp;
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +1100719 } else {
Michael Neuling16c29d12008-10-23 00:42:36 +0000720 if (save_user_regs(regs, frame, __NR_rt_sigreturn, 1))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000721 goto badframe;
722 regs->link = (unsigned long) frame->tramp;
723 }
Paul Mackerrascc657f52005-11-14 21:55:15 +1100724
725 current->thread.fpscr.val = 0; /* turn off all fp exceptions */
726
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000727 /* create a stack frame for the caller of the handler */
728 newsp = ((unsigned long)rt_sf) - (__SIGNAL_FRAMESIZE + 16);
Olof Johanssond0c3d532007-10-12 10:20:07 +1000729 addr = (void __user *)regs->gpr[1];
Paul Mackerrase2b55302005-10-22 14:46:33 +1000730 if (put_user(regs->gpr[1], (u32 __user *)newsp))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000731 goto badframe;
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000732
733 /* Fill registers for signal handler */
Stephen Rothwell81e70092005-10-18 11:17:58 +1000734 regs->gpr[1] = newsp;
735 regs->gpr[3] = sig;
736 regs->gpr[4] = (unsigned long) &rt_sf->info;
737 regs->gpr[5] = (unsigned long) &rt_sf->uc;
738 regs->gpr[6] = (unsigned long) rt_sf;
739 regs->nip = (unsigned long) ka->sa.sa_handler;
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000740 /* enter the signal handler in big-endian mode */
741 regs->msr &= ~MSR_LE;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000742 return 1;
743
744badframe:
745#ifdef DEBUG_SIG
746 printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
747 regs, frame, newsp);
748#endif
Christian Dietrich76462232011-06-04 05:36:54 +0000749 if (show_unhandled_signals)
750 printk_ratelimited(KERN_INFO
751 "%s[%d]: bad frame in handle_rt_signal32: "
752 "%p nip %08lx lr %08lx\n",
753 current->comm, current->pid,
754 addr, regs->nip, regs->link);
Olof Johanssond0c3d532007-10-12 10:20:07 +1000755
Stephen Rothwell81e70092005-10-18 11:17:58 +1000756 force_sigsegv(sig, current);
757 return 0;
758}
759
760static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
761{
762 sigset_t set;
763 struct mcontext __user *mcp;
764
765 if (get_sigset_t(&set, &ucp->uc_sigmask))
766 return -EFAULT;
767#ifdef CONFIG_PPC64
768 {
769 u32 cmcp;
770
771 if (__get_user(cmcp, &ucp->uc_regs))
772 return -EFAULT;
773 mcp = (struct mcontext __user *)(u64)cmcp;
Paul Mackerras7c85d1f2006-06-09 13:02:59 +1000774 /* no need to check access_ok(mcp), since mcp < 4GB */
Stephen Rothwell81e70092005-10-18 11:17:58 +1000775 }
776#else
777 if (__get_user(mcp, &ucp->uc_regs))
778 return -EFAULT;
Paul Mackerras7c85d1f2006-06-09 13:02:59 +1000779 if (!access_ok(VERIFY_READ, mcp, sizeof(*mcp)))
780 return -EFAULT;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000781#endif
Al Viro17440f12012-04-27 14:09:19 -0400782 set_current_blocked(&set);
Stephen Rothwell81e70092005-10-18 11:17:58 +1000783 if (restore_user_regs(regs, mcp, sig))
784 return -EFAULT;
785
786 return 0;
787}
788
789long sys_swapcontext(struct ucontext __user *old_ctx,
Paul Mackerras1bd79332006-03-08 13:24:22 +1100790 struct ucontext __user *new_ctx,
791 int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000792{
793 unsigned char tmp;
Michael Neuling16c29d12008-10-23 00:42:36 +0000794 int ctx_has_vsx_region = 0;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000795
Michael Neulingc1cb2992008-07-08 18:43:41 +1000796#ifdef CONFIG_PPC64
797 unsigned long new_msr = 0;
798
Andreas Schwab77eb50a2008-11-06 00:49:00 +0000799 if (new_ctx) {
800 struct mcontext __user *mcp;
801 u32 cmcp;
802
803 /*
804 * Get pointer to the real mcontext. No need for
805 * access_ok since we are dealing with compat
806 * pointers.
807 */
808 if (__get_user(cmcp, &new_ctx->uc_regs))
809 return -EFAULT;
810 mcp = (struct mcontext __user *)(u64)cmcp;
811 if (__get_user(new_msr, &mcp->mc_gregs[PT_MSR]))
812 return -EFAULT;
813 }
Michael Neulingc1cb2992008-07-08 18:43:41 +1000814 /*
815 * Check that the context is not smaller than the original
816 * size (with VMX but without VSX)
817 */
818 if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
819 return -EINVAL;
820 /*
821 * If the new context state sets the MSR VSX bits but
822 * it doesn't provide VSX state.
823 */
824 if ((ctx_size < sizeof(struct ucontext)) &&
825 (new_msr & MSR_VSX))
826 return -EINVAL;
Michael Neuling16c29d12008-10-23 00:42:36 +0000827 /* Does the context have enough room to store VSX data? */
828 if (ctx_size >= sizeof(struct ucontext))
829 ctx_has_vsx_region = 1;
Michael Neulingc1cb2992008-07-08 18:43:41 +1000830#else
Stephen Rothwell81e70092005-10-18 11:17:58 +1000831 /* Context size is for future use. Right now, we only make sure
832 * we are passed something we understand
833 */
834 if (ctx_size < sizeof(struct ucontext))
835 return -EINVAL;
Michael Neulingc1cb2992008-07-08 18:43:41 +1000836#endif
Stephen Rothwell81e70092005-10-18 11:17:58 +1000837 if (old_ctx != NULL) {
Paul Mackerras1c9bb1a2006-12-20 13:57:06 +1100838 struct mcontext __user *mctx;
839
840 /*
841 * old_ctx might not be 16-byte aligned, in which
842 * case old_ctx->uc_mcontext won't be either.
843 * Because we have the old_ctx->uc_pad2 field
844 * before old_ctx->uc_mcontext, we need to round down
845 * from &old_ctx->uc_mcontext to a 16-byte boundary.
846 */
847 mctx = (struct mcontext __user *)
848 ((unsigned long) &old_ctx->uc_mcontext & ~0xfUL);
Michael Neuling16c29d12008-10-23 00:42:36 +0000849 if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
850 || save_user_regs(regs, mctx, 0, ctx_has_vsx_region)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000851 || put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
Paul Mackerras1c9bb1a2006-12-20 13:57:06 +1100852 || __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000853 return -EFAULT;
854 }
855 if (new_ctx == NULL)
856 return 0;
Michael Neuling16c29d12008-10-23 00:42:36 +0000857 if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
Stephen Rothwell81e70092005-10-18 11:17:58 +1000858 || __get_user(tmp, (u8 __user *) new_ctx)
Michael Neuling16c29d12008-10-23 00:42:36 +0000859 || __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000860 return -EFAULT;
861
862 /*
863 * If we get a fault copying the context into the kernel's
864 * image of the user's registers, we can't just return -EFAULT
865 * because the user's registers will be corrupted. For instance
866 * the NIP value may have been updated but not some of the
867 * other registers. Given that we have done the access_ok
868 * and successfully read the first and last bytes of the region
869 * above, this should only happen in an out-of-memory situation
870 * or if another thread unmaps the region containing the context.
871 * We kill the task with a SIGSEGV in this situation.
872 */
873 if (do_setcontext(new_ctx, regs, 0))
874 do_exit(SIGSEGV);
David Woodhouse401d1f02005-11-15 18:52:18 +0000875
876 set_thread_flag(TIF_RESTOREALL);
Stephen Rothwell81e70092005-10-18 11:17:58 +1000877 return 0;
878}
879
880long sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
881 struct pt_regs *regs)
882{
883 struct rt_sigframe __user *rt_sf;
884
885 /* Always make any pending restarted system calls return -EINTR */
886 current_thread_info()->restart_block.fn = do_no_restart_syscall;
887
888 rt_sf = (struct rt_sigframe __user *)
889 (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
890 if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
891 goto bad;
892 if (do_setcontext(&rt_sf->uc, regs, 1))
893 goto bad;
894
895 /*
896 * It's not clear whether or why it is desirable to save the
897 * sigaltstack setting on signal delivery and restore it on
898 * signal return. But other architectures do this and we have
899 * always done it up until now so it is probably better not to
900 * change it. -- paulus
901 */
902#ifdef CONFIG_PPC64
Al Viro7cce2462012-12-23 03:26:46 -0500903 if (compat_restore_altstack(&rt_sf->uc.uc_stack))
904 goto bad;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000905#else
Al Viro7cce2462012-12-23 03:26:46 -0500906 if (restore_altstack(&rt_sf->uc.uc_stack))
907 goto bad;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000908#endif
David Woodhouse401d1f02005-11-15 18:52:18 +0000909 set_thread_flag(TIF_RESTOREALL);
910 return 0;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000911
912 bad:
Christian Dietrich76462232011-06-04 05:36:54 +0000913 if (show_unhandled_signals)
914 printk_ratelimited(KERN_INFO
915 "%s[%d]: bad frame in sys_rt_sigreturn: "
916 "%p nip %08lx lr %08lx\n",
917 current->comm, current->pid,
918 rt_sf, regs->nip, regs->link);
Olof Johanssond0c3d532007-10-12 10:20:07 +1000919
Stephen Rothwell81e70092005-10-18 11:17:58 +1000920 force_sig(SIGSEGV, current);
921 return 0;
922}
923
924#ifdef CONFIG_PPC32
925int sys_debug_setcontext(struct ucontext __user *ctx,
926 int ndbg, struct sig_dbg_op __user *dbg,
927 int r6, int r7, int r8,
928 struct pt_regs *regs)
929{
930 struct sig_dbg_op op;
931 int i;
Paul Mackerras7c85d1f2006-06-09 13:02:59 +1000932 unsigned char tmp;
Stephen Rothwell81e70092005-10-18 11:17:58 +1000933 unsigned long new_msr = regs->msr;
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000934#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Stephen Rothwell81e70092005-10-18 11:17:58 +1000935 unsigned long new_dbcr0 = current->thread.dbcr0;
936#endif
937
938 for (i=0; i<ndbg; i++) {
Paul Mackerras7c85d1f2006-06-09 13:02:59 +1000939 if (copy_from_user(&op, dbg + i, sizeof(op)))
Stephen Rothwell81e70092005-10-18 11:17:58 +1000940 return -EFAULT;
941 switch (op.dbg_type) {
942 case SIG_DBG_SINGLE_STEPPING:
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000943#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Stephen Rothwell81e70092005-10-18 11:17:58 +1000944 if (op.dbg_value) {
945 new_msr |= MSR_DE;
946 new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
947 } else {
Dave Kleikamp3bffb652010-02-08 11:51:18 +0000948 new_dbcr0 &= ~DBCR0_IC;
949 if (!DBCR_ACTIVE_EVENTS(new_dbcr0,
950 current->thread.dbcr1)) {
951 new_msr &= ~MSR_DE;
952 new_dbcr0 &= ~DBCR0_IDM;
953 }
Stephen Rothwell81e70092005-10-18 11:17:58 +1000954 }
955#else
956 if (op.dbg_value)
957 new_msr |= MSR_SE;
958 else
959 new_msr &= ~MSR_SE;
960#endif
961 break;
962 case SIG_DBG_BRANCH_TRACING:
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000963#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Stephen Rothwell81e70092005-10-18 11:17:58 +1000964 return -EINVAL;
965#else
966 if (op.dbg_value)
967 new_msr |= MSR_BE;
968 else
969 new_msr &= ~MSR_BE;
970#endif
971 break;
972
973 default:
974 return -EINVAL;
975 }
976 }
977
978 /* We wait until here to actually install the values in the
979 registers so if we fail in the above loop, it will not
980 affect the contents of these registers. After this point,
981 failure is a problem, anyway, and it's very unlikely unless
982 the user is really doing something wrong. */
983 regs->msr = new_msr;
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000984#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Stephen Rothwell81e70092005-10-18 11:17:58 +1000985 current->thread.dbcr0 = new_dbcr0;
986#endif
987
Paul Mackerras7c85d1f2006-06-09 13:02:59 +1000988 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx))
989 || __get_user(tmp, (u8 __user *) ctx)
990 || __get_user(tmp, (u8 __user *) (ctx + 1) - 1))
991 return -EFAULT;
992
Stephen Rothwell81e70092005-10-18 11:17:58 +1000993 /*
994 * If we get a fault copying the context into the kernel's
995 * image of the user's registers, we can't just return -EFAULT
996 * because the user's registers will be corrupted. For instance
997 * the NIP value may have been updated but not some of the
998 * other registers. Given that we have done the access_ok
999 * and successfully read the first and last bytes of the region
1000 * above, this should only happen in an out-of-memory situation
1001 * or if another thread unmaps the region containing the context.
1002 * We kill the task with a SIGSEGV in this situation.
1003 */
1004 if (do_setcontext(ctx, regs, 1)) {
Christian Dietrich76462232011-06-04 05:36:54 +00001005 if (show_unhandled_signals)
1006 printk_ratelimited(KERN_INFO "%s[%d]: bad frame in "
1007 "sys_debug_setcontext: %p nip %08lx "
1008 "lr %08lx\n",
1009 current->comm, current->pid,
1010 ctx, regs->nip, regs->link);
Olof Johanssond0c3d532007-10-12 10:20:07 +10001011
Stephen Rothwell81e70092005-10-18 11:17:58 +10001012 force_sig(SIGSEGV, current);
1013 goto out;
1014 }
1015
1016 /*
1017 * It's not clear whether or why it is desirable to save the
1018 * sigaltstack setting on signal delivery and restore it on
1019 * signal return. But other architectures do this and we have
1020 * always done it up until now so it is probably better not to
1021 * change it. -- paulus
1022 */
Al Viro7cce2462012-12-23 03:26:46 -05001023 restore_altstack(&ctx->uc_stack);
Stephen Rothwell81e70092005-10-18 11:17:58 +10001024
David Woodhouse401d1f02005-11-15 18:52:18 +00001025 set_thread_flag(TIF_RESTOREALL);
Stephen Rothwell81e70092005-10-18 11:17:58 +10001026 out:
1027 return 0;
1028}
1029#endif
1030
1031/*
1032 * OK, we're invoking a handler
1033 */
Christoph Hellwigf478f542007-06-04 15:15:52 +10001034int handle_signal32(unsigned long sig, struct k_sigaction *ka,
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +10001035 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
Stephen Rothwell81e70092005-10-18 11:17:58 +10001036{
1037 struct sigcontext __user *sc;
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +10001038 struct sigframe __user *frame;
1039 unsigned long newsp = 0;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001040
1041 /* Set up Signal Frame */
Josh Boyerefbda862009-03-25 06:23:59 +00001042 frame = get_sigframe(ka, regs, sizeof(*frame), 1);
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +10001043 if (unlikely(frame == NULL))
Stephen Rothwell81e70092005-10-18 11:17:58 +10001044 goto badframe;
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +10001045 sc = (struct sigcontext __user *) &frame->sctx;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001046
1047#if _NSIG != 64
1048#error "Please adjust handle_signal()"
1049#endif
1050 if (__put_user(to_user_ptr(ka->sa.sa_handler), &sc->handler)
1051 || __put_user(oldset->sig[0], &sc->oldmask)
1052#ifdef CONFIG_PPC64
1053 || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
1054#else
1055 || __put_user(oldset->sig[1], &sc->_unused[3])
1056#endif
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +10001057 || __put_user(to_user_ptr(&frame->mctx), &sc->regs)
Stephen Rothwell81e70092005-10-18 11:17:58 +10001058 || __put_user(sig, &sc->signal))
1059 goto badframe;
1060
Benjamin Herrenschmidta5bba932006-05-30 13:51:37 +10001061 if (vdso32_sigtramp && current->mm->context.vdso_base) {
Michael Neuling16c29d12008-10-23 00:42:36 +00001062 if (save_user_regs(regs, &frame->mctx, 0, 1))
Stephen Rothwell81e70092005-10-18 11:17:58 +10001063 goto badframe;
Benjamin Herrenschmidta5bba932006-05-30 13:51:37 +10001064 regs->link = current->mm->context.vdso_base + vdso32_sigtramp;
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +11001065 } else {
Michael Neuling16c29d12008-10-23 00:42:36 +00001066 if (save_user_regs(regs, &frame->mctx, __NR_sigreturn, 1))
Stephen Rothwell81e70092005-10-18 11:17:58 +10001067 goto badframe;
1068 regs->link = (unsigned long) frame->mctx.tramp;
1069 }
1070
Paul Mackerrascc657f52005-11-14 21:55:15 +11001071 current->thread.fpscr.val = 0; /* turn off all fp exceptions */
1072
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +10001073 /* create a stack frame for the caller of the handler */
1074 newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001075 if (put_user(regs->gpr[1], (u32 __user *)newsp))
1076 goto badframe;
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +10001077
Stephen Rothwell81e70092005-10-18 11:17:58 +10001078 regs->gpr[1] = newsp;
1079 regs->gpr[3] = sig;
1080 regs->gpr[4] = (unsigned long) sc;
1081 regs->nip = (unsigned long) ka->sa.sa_handler;
Paul Mackerrasfab5db92006-06-07 16:14:40 +10001082 /* enter the signal handler in big-endian mode */
1083 regs->msr &= ~MSR_LE;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001084
1085 return 1;
1086
1087badframe:
1088#ifdef DEBUG_SIG
1089 printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
1090 regs, frame, newsp);
1091#endif
Christian Dietrich76462232011-06-04 05:36:54 +00001092 if (show_unhandled_signals)
1093 printk_ratelimited(KERN_INFO
1094 "%s[%d]: bad frame in handle_signal32: "
1095 "%p nip %08lx lr %08lx\n",
1096 current->comm, current->pid,
1097 frame, regs->nip, regs->link);
Olof Johanssond0c3d532007-10-12 10:20:07 +10001098
Stephen Rothwell81e70092005-10-18 11:17:58 +10001099 force_sigsegv(sig, current);
1100 return 0;
1101}
1102
1103/*
1104 * Do a signal return; undo the signal stack.
1105 */
1106long sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
1107 struct pt_regs *regs)
1108{
1109 struct sigcontext __user *sc;
1110 struct sigcontext sigctx;
1111 struct mcontext __user *sr;
Olof Johanssond0c3d532007-10-12 10:20:07 +10001112 void __user *addr;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001113 sigset_t set;
1114
1115 /* Always make any pending restarted system calls return -EINTR */
1116 current_thread_info()->restart_block.fn = do_no_restart_syscall;
1117
1118 sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
Olof Johanssond0c3d532007-10-12 10:20:07 +10001119 addr = sc;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001120 if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
1121 goto badframe;
1122
1123#ifdef CONFIG_PPC64
1124 /*
1125 * Note that PPC32 puts the upper 32 bits of the sigmask in the
1126 * unused part of the signal stackframe
1127 */
1128 set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
1129#else
1130 set.sig[0] = sigctx.oldmask;
1131 set.sig[1] = sigctx._unused[3];
1132#endif
Al Viro17440f12012-04-27 14:09:19 -04001133 set_current_blocked(&set);
Stephen Rothwell81e70092005-10-18 11:17:58 +10001134
1135 sr = (struct mcontext __user *)from_user_ptr(sigctx.regs);
Olof Johanssond0c3d532007-10-12 10:20:07 +10001136 addr = sr;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001137 if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
1138 || restore_user_regs(regs, sr, 1))
1139 goto badframe;
1140
David Woodhouse401d1f02005-11-15 18:52:18 +00001141 set_thread_flag(TIF_RESTOREALL);
Stephen Rothwell81e70092005-10-18 11:17:58 +10001142 return 0;
Stephen Rothwell81e70092005-10-18 11:17:58 +10001143
1144badframe:
Christian Dietrich76462232011-06-04 05:36:54 +00001145 if (show_unhandled_signals)
1146 printk_ratelimited(KERN_INFO
1147 "%s[%d]: bad frame in sys_sigreturn: "
1148 "%p nip %08lx lr %08lx\n",
1149 current->comm, current->pid,
1150 addr, regs->nip, regs->link);
Olof Johanssond0c3d532007-10-12 10:20:07 +10001151
Stephen Rothwell81e70092005-10-18 11:17:58 +10001152 force_sig(SIGSEGV, current);
1153 return 0;
1154}