blob: ac8e66b50f07d812c52ae0f65a40236be9bfb89d [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;
67
David S. Miller2d7d5f02006-01-19 02:42:49 -080068 current->saved_sigmask = current->blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +010069
70 set &= _BLOCKABLE;
71 siginitset(&blocked, set);
72 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
David S. Miller2d7d5f02006-01-19 02:42:49 -080074 current->state = TASK_INTERRUPTIBLE;
75 schedule();
76 set_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
David S. Miller2d7d5f02006-01-19 02:42:49 -080078 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
David S. Miller2d7d5f02006-01-19 02:42:49 -080081asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
David S. Miller2d7d5f02006-01-19 02:42:49 -080083 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
David S. Miller5526b7e2008-04-27 02:26:36 -070086asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
David S. Miller5526b7e2008-04-27 02:26:36 -070088 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned long up_psr, pc, npc;
90 sigset_t set;
91 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070092 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int err;
94
David S. Miller5526b7e2008-04-27 02:26:36 -070095 /* Always make any pending restarted system calls return -EINTR */
96 current_thread_info()->restart_block.fn = do_no_restart_syscall;
97
98 synchronize_user_stack();
99
100 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* 1. Make sure we are not getting garbage from the user */
103 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
104 goto segv_and_exit;
105
106 if (((unsigned long) sf) & 3)
107 goto segv_and_exit;
108
109 err = __get_user(pc, &sf->info.si_regs.pc);
110 err |= __get_user(npc, &sf->info.si_regs.npc);
111
112 if ((pc | npc) & 3)
113 goto segv_and_exit;
114
115 /* 2. Restore the state */
116 up_psr = regs->psr;
117 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
118
119 /* User can only change condition codes and FPU enabling in %psr. */
120 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
121 | (regs->psr & (PSR_ICC | PSR_EF));
122
David S. Miller28e61032008-05-11 02:07:19 -0700123 /* Prevent syscall restart. */
124 pt_regs_clear_syscall(regs);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (fpu_save)
128 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700129 err |= __get_user(rwin_save, &sf->rwin_save);
130 if (rwin_save)
131 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* This is pretty much atomic, no amount locking would prevent
134 * the races which exist anyways.
135 */
136 err |= __get_user(set.sig[0], &sf->info.si_mask);
137 err |= __copy_from_user(&set.sig[1], &sf->extramask,
138 (_NSIG_WORDS-1) * sizeof(unsigned int));
139
140 if (err)
141 goto segv_and_exit;
142
143 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100144 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return;
146
147segv_and_exit:
148 force_sig(SIGSEGV, current);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
152{
153 struct rt_signal_frame __user *sf;
154 unsigned int psr, pc, npc;
155 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700156 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 mm_segment_t old_fs;
158 sigset_t set;
159 stack_t st;
160 int err;
161
162 synchronize_user_stack();
163 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
164 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
165 (((unsigned long) sf) & 0x03))
166 goto segv;
167
168 err = __get_user(pc, &sf->regs.pc);
169 err |= __get_user(npc, &sf->regs.npc);
170 err |= ((pc | npc) & 0x03);
171
172 err |= __get_user(regs->y, &sf->regs.y);
173 err |= __get_user(psr, &sf->regs.psr);
174
175 err |= __copy_from_user(&regs->u_regs[UREG_G1],
176 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
177
178 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
179
David S. Miller28e61032008-05-11 02:07:19 -0700180 /* Prevent syscall restart. */
181 pt_regs_clear_syscall(regs);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700184 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 err |= restore_fpu_state(regs, fpu_save);
186 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
187
188 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
189
190 if (err)
191 goto segv;
192
193 regs->pc = pc;
194 regs->npc = npc;
195
196 /* It is more difficult to avoid calling this function than to
197 * call it and ignore errors.
198 */
199 old_fs = get_fs();
200 set_fs(KERNEL_DS);
201 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
202 set_fs(old_fs);
203
David S. Miller55984732011-08-20 17:14:54 -0700204 err |= __get_user(rwin_save, &sf->rwin_save);
205 if (!err && rwin_save) {
206 if (restore_rwin_state(rwin_save))
207 goto segv;
208 }
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100211 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return;
213segv:
214 force_sig(SIGSEGV, current);
215}
216
217/* Checks if the fp is valid */
218static inline int invalid_frame_pointer(void __user *fp, int fplen)
219{
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000220 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 1;
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224}
225
226static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
227{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700228 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700230 /*
231 * If we are on the alternate signal stack and would overflow it, don't.
232 * Return an always-bogus address instead so we will die with SIGSEGV.
233 */
234 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
235 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* This is the X/Open sanctioned signal stack switching. */
238 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700239 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 sp = current->sas_ss_sp + current->sas_ss_size;
241 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700242
David S. Millerf036d9f2010-02-09 16:18:40 -0800243 sp -= framesize;
244
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700245 /* Always align the stack frame. This handles two cases. First,
246 * sigaltstack need not be mindful of platform specific stack
247 * alignment. Second, if we took this signal because the stack
248 * is not aligned properly, we'd like to take the signal cleanly
249 * and report that.
250 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800251 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700252
David S. Millerf036d9f2010-02-09 16:18:40 -0800253 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
David S. Miller392c2182010-09-21 21:41:12 -0700256static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
257 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
David S. Miller5526b7e2008-04-27 02:26:36 -0700259 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700260 int sigframe_size, err, wsaved;
261 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* 1. Make sure everything is clean */
264 synchronize_user_stack();
265
David S. Miller55984732011-08-20 17:14:54 -0700266 wsaved = current_thread_info()->w_saved;
267
268 sigframe_size = sizeof(*sf);
269 if (used_math())
270 sigframe_size += sizeof(__siginfo_fpu_t);
271 if (wsaved)
272 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David S. Miller5526b7e2008-04-27 02:26:36 -0700274 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 get_sigframe(&ka->sa, regs, sigframe_size);
276
277 if (invalid_frame_pointer(sf, sigframe_size))
278 goto sigill_and_return;
279
David S. Miller55984732011-08-20 17:14:54 -0700280 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* 2. Save the current process state */
283 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
284
285 err |= __put_user(0, &sf->extra_size);
286
287 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700288 __siginfo_fpu_t __user *fp = tail;
289 tail += sizeof(*fp);
290 err |= save_fpu_state(regs, fp);
291 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 } else {
293 err |= __put_user(0, &sf->fpu_save);
294 }
David S. Miller55984732011-08-20 17:14:54 -0700295 if (wsaved) {
296 __siginfo_rwin_t __user *rwp = tail;
297 tail += sizeof(*rwp);
298 err |= save_rwin_state(wsaved, rwp);
299 err |= __put_user(rwp, &sf->rwin_save);
300 } else {
301 err |= __put_user(0, &sf->rwin_save);
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
305 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
306 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700307 if (!wsaved) {
308 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
309 sizeof(struct reg_window32));
310 } else {
311 struct reg_window32 *rp;
312
313 rp = &current_thread_info()->reg_window[wsaved - 1];
314 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (err)
317 goto sigsegv;
318
319 /* 3. signal handler back-trampoline and parameters */
320 regs->u_regs[UREG_FP] = (unsigned long) sf;
321 regs->u_regs[UREG_I0] = signo;
322 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
323 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
324
325 /* 4. signal handler */
326 regs->pc = (unsigned long) ka->sa.sa_handler;
327 regs->npc = (regs->pc + 4);
328
329 /* 5. return to kernel instructions */
330 if (ka->ka_restorer)
331 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
332 else {
333 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
334
335 /* mov __NR_sigreturn, %g1 */
336 err |= __put_user(0x821020d8, &sf->insns[0]);
337
338 /* t 0x10 */
339 err |= __put_user(0x91d02010, &sf->insns[1]);
340 if (err)
341 goto sigsegv;
342
343 /* Flush instruction space. */
344 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
345 }
David S. Miller392c2182010-09-21 21:41:12 -0700346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348sigill_and_return:
349 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700350 return -EINVAL;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352sigsegv:
353 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700354 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
David S. Miller392c2182010-09-21 21:41:12 -0700357static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
358 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700361 int sigframe_size, wsaved;
362 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned int psr;
364 int err;
365
366 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700367 wsaved = current_thread_info()->w_saved;
368 sigframe_size = sizeof(*sf);
369 if (used_math())
370 sigframe_size += sizeof(__siginfo_fpu_t);
371 if (wsaved)
372 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 sf = (struct rt_signal_frame __user *)
374 get_sigframe(&ka->sa, regs, sigframe_size);
375 if (invalid_frame_pointer(sf, sigframe_size))
376 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David S. Miller55984732011-08-20 17:14:54 -0700378 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 err = __put_user(regs->pc, &sf->regs.pc);
380 err |= __put_user(regs->npc, &sf->regs.npc);
381 err |= __put_user(regs->y, &sf->regs.y);
382 psr = regs->psr;
383 if (used_math())
384 psr |= PSR_EF;
385 err |= __put_user(psr, &sf->regs.psr);
386 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
387 err |= __put_user(0, &sf->extra_size);
388
389 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700390 __siginfo_fpu_t *fp = tail;
391 tail += sizeof(*fp);
392 err |= save_fpu_state(regs, fp);
393 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 } else {
395 err |= __put_user(0, &sf->fpu_save);
396 }
David S. Miller55984732011-08-20 17:14:54 -0700397 if (wsaved) {
398 __siginfo_rwin_t *rwp = tail;
399 tail += sizeof(*rwp);
400 err |= save_rwin_state(wsaved, rwp);
401 err |= __put_user(rwp, &sf->rwin_save);
402 } else {
403 err |= __put_user(0, &sf->rwin_save);
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
406
407 /* Setup sigaltstack */
408 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
409 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
410 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
411
David S. Miller55984732011-08-20 17:14:54 -0700412 if (!wsaved) {
413 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
414 sizeof(struct reg_window32));
415 } else {
416 struct reg_window32 *rp;
417
418 rp = &current_thread_info()->reg_window[wsaved - 1];
419 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 err |= copy_siginfo_to_user(&sf->info, info);
423
424 if (err)
425 goto sigsegv;
426
427 regs->u_regs[UREG_FP] = (unsigned long) sf;
428 regs->u_regs[UREG_I0] = signo;
429 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
430 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
431
432 regs->pc = (unsigned long) ka->sa.sa_handler;
433 regs->npc = (regs->pc + 4);
434
435 if (ka->ka_restorer)
436 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
437 else {
438 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
439
440 /* mov __NR_sigreturn, %g1 */
441 err |= __put_user(0x821020d8, &sf->insns[0]);
442
443 /* t 0x10 */
444 err |= __put_user(0x91d02010, &sf->insns[1]);
445 if (err)
446 goto sigsegv;
447
448 /* Flush instruction space. */
449 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
450 }
David S. Miller392c2182010-09-21 21:41:12 -0700451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453sigill:
454 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700455 return -EINVAL;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457sigsegv:
458 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700459 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
David S. Miller392c2182010-09-21 21:41:12 -0700462static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463handle_signal(unsigned long signr, struct k_sigaction *ka,
David S. Millerec98c6b2008-04-20 02:14:23 -0700464 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
David S. Miller392c2182010-09-21 21:41:12 -0700466 int err;
467
David S. Millerec98c6b2008-04-20 02:14:23 -0700468 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700469 err = setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700470 else
David S. Miller392c2182010-09-21 21:41:12 -0700471 err = setup_frame(ka, regs, signr, oldset);
472
473 if (err)
474 return err;
David S. Millerec98c6b2008-04-20 02:14:23 -0700475
Matt Flemingce24d8a2012-03-21 16:33:46 -0700476 block_sigmask(ka, signr);
David S. Miller392c2182010-09-21 21:41:12 -0700477 tracehook_signal_handler(signr, info, ka, regs, 0);
478
479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
483 struct sigaction *sa)
484{
485 switch(regs->u_regs[UREG_I0]) {
486 case ERESTART_RESTARTBLOCK:
487 case ERESTARTNOHAND:
488 no_system_call_restart:
489 regs->u_regs[UREG_I0] = EINTR;
490 regs->psr |= PSR_C;
491 break;
492 case ERESTARTSYS:
493 if (!(sa->sa_flags & SA_RESTART))
494 goto no_system_call_restart;
495 /* fallthrough */
496 case ERESTARTNOINTR:
497 regs->u_regs[UREG_I0] = orig_i0;
498 regs->pc -= 4;
499 regs->npc -= 4;
500 }
501}
502
503/* Note that 'init' is a special process: it doesn't get signals it doesn't
504 * want to handle. Thus you cannot kill init even with a SIGKILL even by
505 * mistake.
506 */
David S. Miller5a157d52008-07-27 03:38:53 -0700507static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700510 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800511 sigset_t *oldset;
David S. Miller28e61032008-05-11 02:07:19 -0700512 siginfo_t info;
513 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
David S. Miller1d299bc2011-11-14 20:32:16 -0800515 /* It's a lot of work and synchronization to add a new ptrace
516 * register for GDB to save and restore in order to get
517 * orig_i0 correct for syscall restarts when debugging.
518 *
David S. Millere88d2462011-11-15 12:57:00 -0800519 * Although it should be the case that most of the global
520 * registers are volatile across a system call, glibc already
521 * depends upon that fact that we preserve them. So we can't
522 * just use any global register to save away the orig_i0 value.
523 *
524 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
525 * preserved across a system call trap by various pieces of
526 * code in glibc.
527 *
528 * %g7 is used as the "thread register". %g6 is not used in
529 * any fixed manner. %g6 is used as a scratch register and
530 * a compiler temporary, but it's value is never used across
531 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800532 */
David S. Miller28e61032008-05-11 02:07:19 -0700533 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800534 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
David S. Miller2d7d5f02006-01-19 02:42:49 -0800536 if (test_thread_flag(TIF_RESTORE_SIGMASK))
537 oldset = &current->saved_sigmask;
538 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 oldset = &current->blocked;
540
David S. Miller28e61032008-05-11 02:07:19 -0700541 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
542
543 /* If the debugger messes with the program counter, it clears
544 * the software "in syscall" bit, directing us to not perform
545 * a syscall restart.
546 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800547 restart_syscall = 0;
548 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
549 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800550 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800551 }
552
David S. Miller28e61032008-05-11 02:07:19 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700555 if (restart_syscall)
556 syscall_restart(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700557 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
558 /* a signal was successfully delivered; the saved
559 * sigmask will have been stored in the signal frame,
560 * and will be restored by sigreturn, so we can simply
561 * clear the TIF_RESTORE_SIGMASK flag.
562 */
563 if (test_thread_flag(TIF_RESTORE_SIGMASK))
564 clear_thread_flag(TIF_RESTORE_SIGMASK);
565 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800566 return;
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] == ERESTARTNOHAND ||
570 regs->u_regs[UREG_I0] == ERESTARTSYS ||
571 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
572 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700573 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 regs->pc -= 4;
575 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700576 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
David S. Miller28e61032008-05-11 02:07:19 -0700578 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
580 regs->u_regs[UREG_G1] = __NR_restart_syscall;
581 regs->pc -= 4;
582 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700583 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800585
586 /* if there's no signal to deliver, we just put the saved sigmask
587 * back
588 */
589 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
590 clear_thread_flag(TIF_RESTORE_SIGMASK);
David S. Miller27f20dc2011-10-12 12:27:35 -0700591 set_current_blocked(&current->saved_sigmask);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
David S. Miller5a157d52008-07-27 03:38:53 -0700595void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
596 unsigned long thread_info_flags)
597{
598 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
599 do_signal(regs, orig_i0);
600 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
601 clear_thread_flag(TIF_NOTIFY_RESUME);
602 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100603 if (current->replacement_session_keyring)
604 key_replace_session_keyring();
David S. Miller5a157d52008-07-27 03:38:53 -0700605 }
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608asmlinkage int
609do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
610 unsigned long sp)
611{
612 int ret = -EFAULT;
613
614 /* First see if old state is wanted. */
615 if (ossptr) {
616 if (put_user(current->sas_ss_sp + current->sas_ss_size,
617 &ossptr->the_stack) ||
618 __put_user(on_sig_stack(sp), &ossptr->cur_status))
619 goto out;
620 }
621
622 /* Now see if we want to update the new state. */
623 if (ssptr) {
624 char *ss_sp;
625
626 if (get_user(ss_sp, &ssptr->the_stack))
627 goto out;
628 /* If the current stack was set with sigaltstack, don't
629 swap stacks while we are on it. */
630 ret = -EPERM;
631 if (current->sas_ss_sp && on_sig_stack(sp))
632 goto out;
633
634 /* Since we don't know the extent of the stack, and we don't
635 track onstack-ness, but rather calculate it, we must
636 presume a size. Ho hum this interface is lossy. */
637 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
638 current->sas_ss_size = SIGSTKSZ;
639 }
640 ret = 0;
641out:
642 return ret;
643}