blob: 68f9c8650af47b2731c622cc8ada8768b63d90cb [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 -070032extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
33 void *fpqueue, unsigned long *fpqdepth);
34extern void fpload(unsigned long *fpregs, unsigned long *fsr);
35
David S. Miller5526b7e2008-04-27 02:26:36 -070036struct signal_frame {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct sparc_stackf ss;
Sam Ravnborg4d7b92a2009-01-02 19:32:59 -080038 __siginfo32_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 __siginfo_fpu_t __user *fpu_save;
40 unsigned long insns[2] __attribute__ ((aligned (8)));
41 unsigned int extramask[_NSIG_WORDS - 1];
42 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070043 __siginfo_rwin_t __user *rwin_save;
44} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46struct rt_signal_frame {
47 struct sparc_stackf ss;
48 siginfo_t info;
49 struct pt_regs regs;
50 sigset_t mask;
51 __siginfo_fpu_t __user *fpu_save;
52 unsigned int insns[2];
53 stack_t stack;
54 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070055 __siginfo_rwin_t __user *rwin_save;
56} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/* Align macros */
David S. Miller5526b7e2008-04-27 02:26:36 -070059#define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
61
David S. Miller2d7d5f02006-01-19 02:42:49 -080062static int _sigpause_common(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Matt Flemingfaddf592011-08-11 14:57:02 +010064 sigset_t blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +010065 siginitset(&blocked, set);
Al Viro68f3f162012-05-21 21:42:32 -040066 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
David S. Miller2d7d5f02006-01-19 02:42:49 -080069asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
David S. Miller2d7d5f02006-01-19 02:42:49 -080071 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
David S. Miller5526b7e2008-04-27 02:26:36 -070074asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
David S. Miller5526b7e2008-04-27 02:26:36 -070076 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned long up_psr, pc, npc;
78 sigset_t set;
79 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070080 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int err;
82
David S. Miller5526b7e2008-04-27 02:26:36 -070083 /* Always make any pending restarted system calls return -EINTR */
84 current_thread_info()->restart_block.fn = do_no_restart_syscall;
85
86 synchronize_user_stack();
87
88 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /* 1. Make sure we are not getting garbage from the user */
91 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
92 goto segv_and_exit;
93
94 if (((unsigned long) sf) & 3)
95 goto segv_and_exit;
96
97 err = __get_user(pc, &sf->info.si_regs.pc);
98 err |= __get_user(npc, &sf->info.si_regs.npc);
99
100 if ((pc | npc) & 3)
101 goto segv_and_exit;
102
103 /* 2. Restore the state */
104 up_psr = regs->psr;
105 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
106
107 /* User can only change condition codes and FPU enabling in %psr. */
108 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
109 | (regs->psr & (PSR_ICC | PSR_EF));
110
David S. Miller28e61032008-05-11 02:07:19 -0700111 /* Prevent syscall restart. */
112 pt_regs_clear_syscall(regs);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (fpu_save)
116 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700117 err |= __get_user(rwin_save, &sf->rwin_save);
118 if (rwin_save)
119 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* This is pretty much atomic, no amount locking would prevent
122 * the races which exist anyways.
123 */
124 err |= __get_user(set.sig[0], &sf->info.si_mask);
125 err |= __copy_from_user(&set.sig[1], &sf->extramask,
126 (_NSIG_WORDS-1) * sizeof(unsigned int));
127
128 if (err)
129 goto segv_and_exit;
130
Matt Flemingfaddf592011-08-11 14:57:02 +0100131 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return;
133
134segv_and_exit:
135 force_sig(SIGSEGV, current);
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
139{
140 struct rt_signal_frame __user *sf;
141 unsigned int psr, pc, npc;
142 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700143 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 mm_segment_t old_fs;
145 sigset_t set;
146 stack_t st;
147 int err;
148
149 synchronize_user_stack();
150 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
151 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
152 (((unsigned long) sf) & 0x03))
153 goto segv;
154
155 err = __get_user(pc, &sf->regs.pc);
156 err |= __get_user(npc, &sf->regs.npc);
157 err |= ((pc | npc) & 0x03);
158
159 err |= __get_user(regs->y, &sf->regs.y);
160 err |= __get_user(psr, &sf->regs.psr);
161
162 err |= __copy_from_user(&regs->u_regs[UREG_G1],
163 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
164
165 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
166
David S. Miller28e61032008-05-11 02:07:19 -0700167 /* Prevent syscall restart. */
168 pt_regs_clear_syscall(regs);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700171 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 err |= restore_fpu_state(regs, fpu_save);
173 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
174
175 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
176
177 if (err)
178 goto segv;
179
180 regs->pc = pc;
181 regs->npc = npc;
182
183 /* It is more difficult to avoid calling this function than to
184 * call it and ignore errors.
185 */
186 old_fs = get_fs();
187 set_fs(KERNEL_DS);
188 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
189 set_fs(old_fs);
190
David S. Miller55984732011-08-20 17:14:54 -0700191 err |= __get_user(rwin_save, &sf->rwin_save);
192 if (!err && rwin_save) {
193 if (restore_rwin_state(rwin_save))
194 goto segv;
195 }
196
Matt Flemingfaddf592011-08-11 14:57:02 +0100197 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return;
199segv:
200 force_sig(SIGSEGV, current);
201}
202
203/* Checks if the fp is valid */
204static inline int invalid_frame_pointer(void __user *fp, int fplen)
205{
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000206 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 1;
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210}
211
212static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
213{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700214 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700216 /*
217 * If we are on the alternate signal stack and would overflow it, don't.
218 * Return an always-bogus address instead so we will die with SIGSEGV.
219 */
220 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
221 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* This is the X/Open sanctioned signal stack switching. */
224 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700225 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 sp = current->sas_ss_sp + current->sas_ss_size;
227 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700228
David S. Millerf036d9f2010-02-09 16:18:40 -0800229 sp -= framesize;
230
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700231 /* Always align the stack frame. This handles two cases. First,
232 * sigaltstack need not be mindful of platform specific stack
233 * alignment. Second, if we took this signal because the stack
234 * is not aligned properly, we'd like to take the signal cleanly
235 * and report that.
236 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800237 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700238
David S. Millerf036d9f2010-02-09 16:18:40 -0800239 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
David S. Miller392c2182010-09-21 21:41:12 -0700242static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
243 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
David S. Miller5526b7e2008-04-27 02:26:36 -0700245 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700246 int sigframe_size, err, wsaved;
247 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* 1. Make sure everything is clean */
250 synchronize_user_stack();
251
David S. Miller55984732011-08-20 17:14:54 -0700252 wsaved = current_thread_info()->w_saved;
253
254 sigframe_size = sizeof(*sf);
255 if (used_math())
256 sigframe_size += sizeof(__siginfo_fpu_t);
257 if (wsaved)
258 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
David S. Miller5526b7e2008-04-27 02:26:36 -0700260 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 get_sigframe(&ka->sa, regs, sigframe_size);
262
263 if (invalid_frame_pointer(sf, sigframe_size))
264 goto sigill_and_return;
265
David S. Miller55984732011-08-20 17:14:54 -0700266 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* 2. Save the current process state */
269 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
270
271 err |= __put_user(0, &sf->extra_size);
272
273 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700274 __siginfo_fpu_t __user *fp = tail;
275 tail += sizeof(*fp);
276 err |= save_fpu_state(regs, fp);
277 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 } else {
279 err |= __put_user(0, &sf->fpu_save);
280 }
David S. Miller55984732011-08-20 17:14:54 -0700281 if (wsaved) {
282 __siginfo_rwin_t __user *rwp = tail;
283 tail += sizeof(*rwp);
284 err |= save_rwin_state(wsaved, rwp);
285 err |= __put_user(rwp, &sf->rwin_save);
286 } else {
287 err |= __put_user(0, &sf->rwin_save);
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
291 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
292 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700293 if (!wsaved) {
294 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
295 sizeof(struct reg_window32));
296 } else {
297 struct reg_window32 *rp;
298
299 rp = &current_thread_info()->reg_window[wsaved - 1];
300 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (err)
303 goto sigsegv;
304
305 /* 3. signal handler back-trampoline and parameters */
306 regs->u_regs[UREG_FP] = (unsigned long) sf;
307 regs->u_regs[UREG_I0] = signo;
308 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
309 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
310
311 /* 4. signal handler */
312 regs->pc = (unsigned long) ka->sa.sa_handler;
313 regs->npc = (regs->pc + 4);
314
315 /* 5. return to kernel instructions */
316 if (ka->ka_restorer)
317 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
318 else {
319 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
320
321 /* mov __NR_sigreturn, %g1 */
322 err |= __put_user(0x821020d8, &sf->insns[0]);
323
324 /* t 0x10 */
325 err |= __put_user(0x91d02010, &sf->insns[1]);
326 if (err)
327 goto sigsegv;
328
329 /* Flush instruction space. */
330 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
331 }
David S. Miller392c2182010-09-21 21:41:12 -0700332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334sigill_and_return:
335 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700336 return -EINVAL;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338sigsegv:
339 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700340 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
David S. Miller392c2182010-09-21 21:41:12 -0700343static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
344 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700347 int sigframe_size, wsaved;
348 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned int psr;
350 int err;
351
352 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700353 wsaved = current_thread_info()->w_saved;
354 sigframe_size = sizeof(*sf);
355 if (used_math())
356 sigframe_size += sizeof(__siginfo_fpu_t);
357 if (wsaved)
358 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 sf = (struct rt_signal_frame __user *)
360 get_sigframe(&ka->sa, regs, sigframe_size);
361 if (invalid_frame_pointer(sf, sigframe_size))
362 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David S. Miller55984732011-08-20 17:14:54 -0700364 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 err = __put_user(regs->pc, &sf->regs.pc);
366 err |= __put_user(regs->npc, &sf->regs.npc);
367 err |= __put_user(regs->y, &sf->regs.y);
368 psr = regs->psr;
369 if (used_math())
370 psr |= PSR_EF;
371 err |= __put_user(psr, &sf->regs.psr);
372 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
373 err |= __put_user(0, &sf->extra_size);
374
375 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700376 __siginfo_fpu_t *fp = tail;
377 tail += sizeof(*fp);
378 err |= save_fpu_state(regs, fp);
379 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 } else {
381 err |= __put_user(0, &sf->fpu_save);
382 }
David S. Miller55984732011-08-20 17:14:54 -0700383 if (wsaved) {
384 __siginfo_rwin_t *rwp = tail;
385 tail += sizeof(*rwp);
386 err |= save_rwin_state(wsaved, rwp);
387 err |= __put_user(rwp, &sf->rwin_save);
388 } else {
389 err |= __put_user(0, &sf->rwin_save);
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
392
393 /* Setup sigaltstack */
394 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
395 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
396 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
397
David S. Miller55984732011-08-20 17:14:54 -0700398 if (!wsaved) {
399 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
400 sizeof(struct reg_window32));
401 } else {
402 struct reg_window32 *rp;
403
404 rp = &current_thread_info()->reg_window[wsaved - 1];
405 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 err |= copy_siginfo_to_user(&sf->info, info);
409
410 if (err)
411 goto sigsegv;
412
413 regs->u_regs[UREG_FP] = (unsigned long) sf;
414 regs->u_regs[UREG_I0] = signo;
415 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
416 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
417
418 regs->pc = (unsigned long) ka->sa.sa_handler;
419 regs->npc = (regs->pc + 4);
420
421 if (ka->ka_restorer)
422 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
423 else {
424 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
425
426 /* mov __NR_sigreturn, %g1 */
427 err |= __put_user(0x821020d8, &sf->insns[0]);
428
429 /* t 0x10 */
430 err |= __put_user(0x91d02010, &sf->insns[1]);
431 if (err)
432 goto sigsegv;
433
434 /* Flush instruction space. */
435 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
436 }
David S. Miller392c2182010-09-21 21:41:12 -0700437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439sigill:
440 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700441 return -EINVAL;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443sigsegv:
444 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700445 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Al Viroa610d6e2012-05-21 23:42:15 -0400448static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449handle_signal(unsigned long signr, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400450 siginfo_t *info, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Al Virob7f9a112012-05-02 09:59:21 -0400452 sigset_t *oldset = sigmask_to_save();
David S. Miller392c2182010-09-21 21:41:12 -0700453 int err;
454
David S. Millerec98c6b2008-04-20 02:14:23 -0700455 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700456 err = setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700457 else
David S. Miller392c2182010-09-21 21:41:12 -0700458 err = setup_frame(ka, regs, signr, oldset);
459
460 if (err)
Al Viroa610d6e2012-05-21 23:42:15 -0400461 return;
David S. Millerec98c6b2008-04-20 02:14:23 -0700462
Al Viroefee9842012-04-28 02:04:15 -0400463 signal_delivered(signr, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
467 struct sigaction *sa)
468{
469 switch(regs->u_regs[UREG_I0]) {
470 case ERESTART_RESTARTBLOCK:
471 case ERESTARTNOHAND:
472 no_system_call_restart:
473 regs->u_regs[UREG_I0] = EINTR;
474 regs->psr |= PSR_C;
475 break;
476 case ERESTARTSYS:
477 if (!(sa->sa_flags & SA_RESTART))
478 goto no_system_call_restart;
479 /* fallthrough */
480 case ERESTARTNOINTR:
481 regs->u_regs[UREG_I0] = orig_i0;
482 regs->pc -= 4;
483 regs->npc -= 4;
484 }
485}
486
487/* Note that 'init' is a special process: it doesn't get signals it doesn't
488 * want to handle. Thus you cannot kill init even with a SIGKILL even by
489 * mistake.
490 */
David S. Miller5a157d52008-07-27 03:38:53 -0700491static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700494 int restart_syscall;
David S. Miller28e61032008-05-11 02:07:19 -0700495 siginfo_t info;
496 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
David S. Miller1d299bc2011-11-14 20:32:16 -0800498 /* It's a lot of work and synchronization to add a new ptrace
499 * register for GDB to save and restore in order to get
500 * orig_i0 correct for syscall restarts when debugging.
501 *
David S. Millere88d2462011-11-15 12:57:00 -0800502 * Although it should be the case that most of the global
503 * registers are volatile across a system call, glibc already
504 * depends upon that fact that we preserve them. So we can't
505 * just use any global register to save away the orig_i0 value.
506 *
507 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
508 * preserved across a system call trap by various pieces of
509 * code in glibc.
510 *
511 * %g7 is used as the "thread register". %g6 is not used in
512 * any fixed manner. %g6 is used as a scratch register and
513 * a compiler temporary, but it's value is never used across
514 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800515 */
David S. Miller28e61032008-05-11 02:07:19 -0700516 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800517 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
David S. Miller28e61032008-05-11 02:07:19 -0700519 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
520
521 /* If the debugger messes with the program counter, it clears
522 * the software "in syscall" bit, directing us to not perform
523 * a syscall restart.
524 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800525 restart_syscall = 0;
526 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
527 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800528 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800529 }
530
David S. Miller28e61032008-05-11 02:07:19 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700533 if (restart_syscall)
534 syscall_restart(orig_i0, regs, &ka.sa);
Al Viroa610d6e2012-05-21 23:42:15 -0400535 handle_signal(signr, &ka, &info, regs);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800536 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
David S. Miller28e61032008-05-11 02:07:19 -0700538 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
540 regs->u_regs[UREG_I0] == ERESTARTSYS ||
541 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
542 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700543 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 regs->pc -= 4;
545 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700546 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
David S. Miller28e61032008-05-11 02:07:19 -0700548 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
550 regs->u_regs[UREG_G1] = __NR_restart_syscall;
551 regs->pc -= 4;
552 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700553 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800555
556 /* if there's no signal to deliver, we just put the saved sigmask
557 * back
558 */
Al Viro51a7b442012-05-21 23:33:55 -0400559 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
David S. Miller5a157d52008-07-27 03:38:53 -0700562void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
563 unsigned long thread_info_flags)
564{
Al Viro6fd84c02012-05-23 15:28:58 -0400565 if (thread_info_flags & _TIF_SIGPENDING)
David S. Miller5a157d52008-07-27 03:38:53 -0700566 do_signal(regs, orig_i0);
567 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
568 clear_thread_flag(TIF_NOTIFY_RESUME);
569 tracehook_notify_resume(regs);
570 }
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573asmlinkage int
574do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
575 unsigned long sp)
576{
577 int ret = -EFAULT;
578
579 /* First see if old state is wanted. */
580 if (ossptr) {
581 if (put_user(current->sas_ss_sp + current->sas_ss_size,
582 &ossptr->the_stack) ||
583 __put_user(on_sig_stack(sp), &ossptr->cur_status))
584 goto out;
585 }
586
587 /* Now see if we want to update the new state. */
588 if (ssptr) {
589 char *ss_sp;
590
591 if (get_user(ss_sp, &ssptr->the_stack))
592 goto out;
593 /* If the current stack was set with sigaltstack, don't
594 swap stacks while we are on it. */
595 ret = -EPERM;
596 if (current->sas_ss_sp && on_sig_stack(sp))
597 goto out;
598
599 /* Since we don't know the extent of the stack, and we don't
600 track onstack-ness, but rather calculate it, we must
601 presume a size. Ho hum this interface is lossy. */
602 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
603 current->sas_ss_size = SIGSTKSZ;
604 }
605 ret = 0;
606out:
607 return ret;
608}