blob: 2b7e849f7c6528b1da91146f3f515ccbf21370c3 [file] [log] [blame]
David S. Miller5526b7e2008-04-27 02:26:36 -07001/* linux/arch/sparc/kernel/signal.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/sched.h>
10#include <linux/kernel.h>
11#include <linux/signal.h>
12#include <linux/errno.h>
13#include <linux/wait.h>
14#include <linux/ptrace.h>
15#include <linux/unistd.h>
16#include <linux/mm.h>
17#include <linux/tty.h>
18#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/binfmts.h> /* do_coredum */
20#include <linux/bitops.h>
David S. Miller5a157d52008-07-27 03:38:53 -070021#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/uaccess.h>
24#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/pgalloc.h>
26#include <asm/pgtable.h>
27#include <asm/cacheflush.h> /* flush_sig_insns */
David Howellsd550bbd2012-03-28 18:30:03 +010028#include <asm/switch_to.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Miller55984732011-08-20 17:14:54 -070030#include "sigutil.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
34extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
35 void *fpqueue, unsigned long *fpqdepth);
36extern void fpload(unsigned long *fpregs, unsigned long *fsr);
37
David S. Miller5526b7e2008-04-27 02:26:36 -070038struct signal_frame {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct sparc_stackf ss;
Sam Ravnborg4d7b92a2009-01-02 19:32:59 -080040 __siginfo32_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 __siginfo_fpu_t __user *fpu_save;
42 unsigned long insns[2] __attribute__ ((aligned (8)));
43 unsigned int extramask[_NSIG_WORDS - 1];
44 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070045 __siginfo_rwin_t __user *rwin_save;
46} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48struct rt_signal_frame {
49 struct sparc_stackf ss;
50 siginfo_t info;
51 struct pt_regs regs;
52 sigset_t mask;
53 __siginfo_fpu_t __user *fpu_save;
54 unsigned int insns[2];
55 stack_t stack;
56 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070057 __siginfo_rwin_t __user *rwin_save;
58} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* Align macros */
David S. Miller5526b7e2008-04-27 02:26:36 -070061#define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
63
David S. Miller2d7d5f02006-01-19 02:42:49 -080064static int _sigpause_common(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Matt Flemingfaddf592011-08-11 14:57:02 +010066 sigset_t blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +010067 siginitset(&blocked, set);
Al Viro68f3f162012-05-21 21:42:32 -040068 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
David S. Miller2d7d5f02006-01-19 02:42:49 -080071asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
David S. Miller2d7d5f02006-01-19 02:42:49 -080073 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
David S. Miller5526b7e2008-04-27 02:26:36 -070076asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
David S. Miller5526b7e2008-04-27 02:26:36 -070078 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 unsigned long up_psr, pc, npc;
80 sigset_t set;
81 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070082 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 int err;
84
David S. Miller5526b7e2008-04-27 02:26:36 -070085 /* Always make any pending restarted system calls return -EINTR */
86 current_thread_info()->restart_block.fn = do_no_restart_syscall;
87
88 synchronize_user_stack();
89
90 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* 1. Make sure we are not getting garbage from the user */
93 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
94 goto segv_and_exit;
95
96 if (((unsigned long) sf) & 3)
97 goto segv_and_exit;
98
99 err = __get_user(pc, &sf->info.si_regs.pc);
100 err |= __get_user(npc, &sf->info.si_regs.npc);
101
102 if ((pc | npc) & 3)
103 goto segv_and_exit;
104
105 /* 2. Restore the state */
106 up_psr = regs->psr;
107 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
108
109 /* User can only change condition codes and FPU enabling in %psr. */
110 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
111 | (regs->psr & (PSR_ICC | PSR_EF));
112
David S. Miller28e61032008-05-11 02:07:19 -0700113 /* Prevent syscall restart. */
114 pt_regs_clear_syscall(regs);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (fpu_save)
118 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700119 err |= __get_user(rwin_save, &sf->rwin_save);
120 if (rwin_save)
121 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* This is pretty much atomic, no amount locking would prevent
124 * the races which exist anyways.
125 */
126 err |= __get_user(set.sig[0], &sf->info.si_mask);
127 err |= __copy_from_user(&set.sig[1], &sf->extramask,
128 (_NSIG_WORDS-1) * sizeof(unsigned int));
129
130 if (err)
131 goto segv_and_exit;
132
133 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100134 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
136
137segv_and_exit:
138 force_sig(SIGSEGV, current);
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
142{
143 struct rt_signal_frame __user *sf;
144 unsigned int psr, pc, npc;
145 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700146 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 mm_segment_t old_fs;
148 sigset_t set;
149 stack_t st;
150 int err;
151
152 synchronize_user_stack();
153 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
154 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
155 (((unsigned long) sf) & 0x03))
156 goto segv;
157
158 err = __get_user(pc, &sf->regs.pc);
159 err |= __get_user(npc, &sf->regs.npc);
160 err |= ((pc | npc) & 0x03);
161
162 err |= __get_user(regs->y, &sf->regs.y);
163 err |= __get_user(psr, &sf->regs.psr);
164
165 err |= __copy_from_user(&regs->u_regs[UREG_G1],
166 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
167
168 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
169
David S. Miller28e61032008-05-11 02:07:19 -0700170 /* Prevent syscall restart. */
171 pt_regs_clear_syscall(regs);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700174 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 err |= restore_fpu_state(regs, fpu_save);
176 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
177
178 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
179
180 if (err)
181 goto segv;
182
183 regs->pc = pc;
184 regs->npc = npc;
185
186 /* It is more difficult to avoid calling this function than to
187 * call it and ignore errors.
188 */
189 old_fs = get_fs();
190 set_fs(KERNEL_DS);
191 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
192 set_fs(old_fs);
193
David S. Miller55984732011-08-20 17:14:54 -0700194 err |= __get_user(rwin_save, &sf->rwin_save);
195 if (!err && rwin_save) {
196 if (restore_rwin_state(rwin_save))
197 goto segv;
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100201 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return;
203segv:
204 force_sig(SIGSEGV, current);
205}
206
207/* Checks if the fp is valid */
208static inline int invalid_frame_pointer(void __user *fp, int fplen)
209{
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000210 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 1;
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214}
215
216static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
217{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700218 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700220 /*
221 * If we are on the alternate signal stack and would overflow it, don't.
222 * Return an always-bogus address instead so we will die with SIGSEGV.
223 */
224 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
225 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* This is the X/Open sanctioned signal stack switching. */
228 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700229 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 sp = current->sas_ss_sp + current->sas_ss_size;
231 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700232
David S. Millerf036d9f2010-02-09 16:18:40 -0800233 sp -= framesize;
234
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700235 /* Always align the stack frame. This handles two cases. First,
236 * sigaltstack need not be mindful of platform specific stack
237 * alignment. Second, if we took this signal because the stack
238 * is not aligned properly, we'd like to take the signal cleanly
239 * and report that.
240 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800241 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700242
David S. Millerf036d9f2010-02-09 16:18:40 -0800243 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
David S. Miller392c2182010-09-21 21:41:12 -0700246static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
247 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
David S. Miller5526b7e2008-04-27 02:26:36 -0700249 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700250 int sigframe_size, err, wsaved;
251 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* 1. Make sure everything is clean */
254 synchronize_user_stack();
255
David S. Miller55984732011-08-20 17:14:54 -0700256 wsaved = current_thread_info()->w_saved;
257
258 sigframe_size = sizeof(*sf);
259 if (used_math())
260 sigframe_size += sizeof(__siginfo_fpu_t);
261 if (wsaved)
262 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David S. Miller5526b7e2008-04-27 02:26:36 -0700264 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 get_sigframe(&ka->sa, regs, sigframe_size);
266
267 if (invalid_frame_pointer(sf, sigframe_size))
268 goto sigill_and_return;
269
David S. Miller55984732011-08-20 17:14:54 -0700270 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* 2. Save the current process state */
273 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
274
275 err |= __put_user(0, &sf->extra_size);
276
277 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700278 __siginfo_fpu_t __user *fp = tail;
279 tail += sizeof(*fp);
280 err |= save_fpu_state(regs, fp);
281 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 } else {
283 err |= __put_user(0, &sf->fpu_save);
284 }
David S. Miller55984732011-08-20 17:14:54 -0700285 if (wsaved) {
286 __siginfo_rwin_t __user *rwp = tail;
287 tail += sizeof(*rwp);
288 err |= save_rwin_state(wsaved, rwp);
289 err |= __put_user(rwp, &sf->rwin_save);
290 } else {
291 err |= __put_user(0, &sf->rwin_save);
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
295 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
296 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700297 if (!wsaved) {
298 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
299 sizeof(struct reg_window32));
300 } else {
301 struct reg_window32 *rp;
302
303 rp = &current_thread_info()->reg_window[wsaved - 1];
304 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (err)
307 goto sigsegv;
308
309 /* 3. signal handler back-trampoline and parameters */
310 regs->u_regs[UREG_FP] = (unsigned long) sf;
311 regs->u_regs[UREG_I0] = signo;
312 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
313 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
314
315 /* 4. signal handler */
316 regs->pc = (unsigned long) ka->sa.sa_handler;
317 regs->npc = (regs->pc + 4);
318
319 /* 5. return to kernel instructions */
320 if (ka->ka_restorer)
321 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
322 else {
323 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
324
325 /* mov __NR_sigreturn, %g1 */
326 err |= __put_user(0x821020d8, &sf->insns[0]);
327
328 /* t 0x10 */
329 err |= __put_user(0x91d02010, &sf->insns[1]);
330 if (err)
331 goto sigsegv;
332
333 /* Flush instruction space. */
334 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
335 }
David S. Miller392c2182010-09-21 21:41:12 -0700336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338sigill_and_return:
339 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700340 return -EINVAL;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342sigsegv:
343 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700344 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
David S. Miller392c2182010-09-21 21:41:12 -0700347static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
348 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700351 int sigframe_size, wsaved;
352 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned int psr;
354 int err;
355
356 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700357 wsaved = current_thread_info()->w_saved;
358 sigframe_size = sizeof(*sf);
359 if (used_math())
360 sigframe_size += sizeof(__siginfo_fpu_t);
361 if (wsaved)
362 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sf = (struct rt_signal_frame __user *)
364 get_sigframe(&ka->sa, regs, sigframe_size);
365 if (invalid_frame_pointer(sf, sigframe_size))
366 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
David S. Miller55984732011-08-20 17:14:54 -0700368 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 err = __put_user(regs->pc, &sf->regs.pc);
370 err |= __put_user(regs->npc, &sf->regs.npc);
371 err |= __put_user(regs->y, &sf->regs.y);
372 psr = regs->psr;
373 if (used_math())
374 psr |= PSR_EF;
375 err |= __put_user(psr, &sf->regs.psr);
376 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
377 err |= __put_user(0, &sf->extra_size);
378
379 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700380 __siginfo_fpu_t *fp = tail;
381 tail += sizeof(*fp);
382 err |= save_fpu_state(regs, fp);
383 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else {
385 err |= __put_user(0, &sf->fpu_save);
386 }
David S. Miller55984732011-08-20 17:14:54 -0700387 if (wsaved) {
388 __siginfo_rwin_t *rwp = tail;
389 tail += sizeof(*rwp);
390 err |= save_rwin_state(wsaved, rwp);
391 err |= __put_user(rwp, &sf->rwin_save);
392 } else {
393 err |= __put_user(0, &sf->rwin_save);
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
396
397 /* Setup sigaltstack */
398 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
399 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
400 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
401
David S. Miller55984732011-08-20 17:14:54 -0700402 if (!wsaved) {
403 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
404 sizeof(struct reg_window32));
405 } else {
406 struct reg_window32 *rp;
407
408 rp = &current_thread_info()->reg_window[wsaved - 1];
409 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 err |= copy_siginfo_to_user(&sf->info, info);
413
414 if (err)
415 goto sigsegv;
416
417 regs->u_regs[UREG_FP] = (unsigned long) sf;
418 regs->u_regs[UREG_I0] = signo;
419 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
420 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
421
422 regs->pc = (unsigned long) ka->sa.sa_handler;
423 regs->npc = (regs->pc + 4);
424
425 if (ka->ka_restorer)
426 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
427 else {
428 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
429
430 /* mov __NR_sigreturn, %g1 */
431 err |= __put_user(0x821020d8, &sf->insns[0]);
432
433 /* t 0x10 */
434 err |= __put_user(0x91d02010, &sf->insns[1]);
435 if (err)
436 goto sigsegv;
437
438 /* Flush instruction space. */
439 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
440 }
David S. Miller392c2182010-09-21 21:41:12 -0700441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443sigill:
444 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700445 return -EINVAL;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447sigsegv:
448 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700449 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
David S. Miller392c2182010-09-21 21:41:12 -0700452static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453handle_signal(unsigned long signr, struct k_sigaction *ka,
David S. Millerec98c6b2008-04-20 02:14:23 -0700454 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
David S. Miller392c2182010-09-21 21:41:12 -0700456 int err;
457
David S. Millerec98c6b2008-04-20 02:14:23 -0700458 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700459 err = setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700460 else
David S. Miller392c2182010-09-21 21:41:12 -0700461 err = setup_frame(ka, regs, signr, oldset);
462
463 if (err)
464 return err;
David S. Millerec98c6b2008-04-20 02:14:23 -0700465
Matt Flemingce24d8a2012-03-21 16:33:46 -0700466 block_sigmask(ka, signr);
David S. Miller392c2182010-09-21 21:41:12 -0700467 tracehook_signal_handler(signr, info, ka, regs, 0);
468
469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
473 struct sigaction *sa)
474{
475 switch(regs->u_regs[UREG_I0]) {
476 case ERESTART_RESTARTBLOCK:
477 case ERESTARTNOHAND:
478 no_system_call_restart:
479 regs->u_regs[UREG_I0] = EINTR;
480 regs->psr |= PSR_C;
481 break;
482 case ERESTARTSYS:
483 if (!(sa->sa_flags & SA_RESTART))
484 goto no_system_call_restart;
485 /* fallthrough */
486 case ERESTARTNOINTR:
487 regs->u_regs[UREG_I0] = orig_i0;
488 regs->pc -= 4;
489 regs->npc -= 4;
490 }
491}
492
493/* Note that 'init' is a special process: it doesn't get signals it doesn't
494 * want to handle. Thus you cannot kill init even with a SIGKILL even by
495 * mistake.
496 */
David S. Miller5a157d52008-07-27 03:38:53 -0700497static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700500 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800501 sigset_t *oldset;
David S. Miller28e61032008-05-11 02:07:19 -0700502 siginfo_t info;
503 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
David S. Miller1d299bc2011-11-14 20:32:16 -0800505 /* It's a lot of work and synchronization to add a new ptrace
506 * register for GDB to save and restore in order to get
507 * orig_i0 correct for syscall restarts when debugging.
508 *
David S. Millere88d2462011-11-15 12:57:00 -0800509 * Although it should be the case that most of the global
510 * registers are volatile across a system call, glibc already
511 * depends upon that fact that we preserve them. So we can't
512 * just use any global register to save away the orig_i0 value.
513 *
514 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
515 * preserved across a system call trap by various pieces of
516 * code in glibc.
517 *
518 * %g7 is used as the "thread register". %g6 is not used in
519 * any fixed manner. %g6 is used as a scratch register and
520 * a compiler temporary, but it's value is never used across
521 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800522 */
David S. Miller28e61032008-05-11 02:07:19 -0700523 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800524 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
David S. Miller2d7d5f02006-01-19 02:42:49 -0800526 if (test_thread_flag(TIF_RESTORE_SIGMASK))
527 oldset = &current->saved_sigmask;
528 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 oldset = &current->blocked;
530
David S. Miller28e61032008-05-11 02:07:19 -0700531 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
532
533 /* If the debugger messes with the program counter, it clears
534 * the software "in syscall" bit, directing us to not perform
535 * a syscall restart.
536 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800537 restart_syscall = 0;
538 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
539 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800540 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800541 }
542
David S. Miller28e61032008-05-11 02:07:19 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700545 if (restart_syscall)
546 syscall_restart(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700547 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
548 /* a signal was successfully delivered; the saved
549 * sigmask will have been stored in the signal frame,
550 * and will be restored by sigreturn, so we can simply
551 * clear the TIF_RESTORE_SIGMASK flag.
552 */
553 if (test_thread_flag(TIF_RESTORE_SIGMASK))
554 clear_thread_flag(TIF_RESTORE_SIGMASK);
555 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800556 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
David S. Miller28e61032008-05-11 02:07:19 -0700558 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
560 regs->u_regs[UREG_I0] == ERESTARTSYS ||
561 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
562 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700563 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 regs->pc -= 4;
565 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700566 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
David S. Miller28e61032008-05-11 02:07:19 -0700568 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
570 regs->u_regs[UREG_G1] = __NR_restart_syscall;
571 regs->pc -= 4;
572 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700573 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800575
576 /* if there's no signal to deliver, we just put the saved sigmask
577 * back
578 */
579 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
580 clear_thread_flag(TIF_RESTORE_SIGMASK);
David S. Miller27f20dc2011-10-12 12:27:35 -0700581 set_current_blocked(&current->saved_sigmask);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
David S. Miller5a157d52008-07-27 03:38:53 -0700585void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
586 unsigned long thread_info_flags)
587{
588 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
589 do_signal(regs, orig_i0);
590 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
591 clear_thread_flag(TIF_NOTIFY_RESUME);
592 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100593 if (current->replacement_session_keyring)
594 key_replace_session_keyring();
David S. Miller5a157d52008-07-27 03:38:53 -0700595 }
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598asmlinkage int
599do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
600 unsigned long sp)
601{
602 int ret = -EFAULT;
603
604 /* First see if old state is wanted. */
605 if (ossptr) {
606 if (put_user(current->sas_ss_sp + current->sas_ss_size,
607 &ossptr->the_stack) ||
608 __put_user(on_sig_stack(sp), &ossptr->cur_status))
609 goto out;
610 }
611
612 /* Now see if we want to update the new state. */
613 if (ssptr) {
614 char *ss_sp;
615
616 if (get_user(ss_sp, &ssptr->the_stack))
617 goto out;
618 /* If the current stack was set with sigaltstack, don't
619 swap stacks while we are on it. */
620 ret = -EPERM;
621 if (current->sas_ss_sp && on_sig_stack(sp))
622 goto out;
623
624 /* Since we don't know the extent of the stack, and we don't
625 track onstack-ness, but rather calculate it, we must
626 presume a size. Ho hum this interface is lossy. */
627 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
628 current->sas_ss_size = SIGSTKSZ;
629 }
630 ret = 0;
631out:
632 return ret;
633}