blob: 7391fa89651fe92e38777941d701ae69347bb057 [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 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int err;
146
147 synchronize_user_stack();
148 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
149 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
150 (((unsigned long) sf) & 0x03))
151 goto segv;
152
153 err = __get_user(pc, &sf->regs.pc);
154 err |= __get_user(npc, &sf->regs.npc);
155 err |= ((pc | npc) & 0x03);
156
157 err |= __get_user(regs->y, &sf->regs.y);
158 err |= __get_user(psr, &sf->regs.psr);
159
160 err |= __copy_from_user(&regs->u_regs[UREG_G1],
161 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
162
163 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
164
David S. Miller28e61032008-05-11 02:07:19 -0700165 /* Prevent syscall restart. */
166 pt_regs_clear_syscall(regs);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700169 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 err |= restore_fpu_state(regs, fpu_save);
171 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
Al Viro99b06fe2012-12-23 03:41:17 -0500172 err |= restore_altstack(&sf->stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (err)
175 goto segv;
176
177 regs->pc = pc;
178 regs->npc = npc;
179
David S. Miller55984732011-08-20 17:14:54 -0700180 err |= __get_user(rwin_save, &sf->rwin_save);
181 if (!err && rwin_save) {
182 if (restore_rwin_state(rwin_save))
183 goto segv;
184 }
185
Matt Flemingfaddf592011-08-11 14:57:02 +0100186 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return;
188segv:
189 force_sig(SIGSEGV, current);
190}
191
192/* Checks if the fp is valid */
193static inline int invalid_frame_pointer(void __user *fp, int fplen)
194{
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000195 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 1;
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
201static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
202{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700203 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700205 /*
206 * If we are on the alternate signal stack and would overflow it, don't.
207 * Return an always-bogus address instead so we will die with SIGSEGV.
208 */
209 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
210 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* This is the X/Open sanctioned signal stack switching. */
213 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700214 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 sp = current->sas_ss_sp + current->sas_ss_size;
216 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700217
David S. Millerf036d9f2010-02-09 16:18:40 -0800218 sp -= framesize;
219
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700220 /* Always align the stack frame. This handles two cases. First,
221 * sigaltstack need not be mindful of platform specific stack
222 * alignment. Second, if we took this signal because the stack
223 * is not aligned properly, we'd like to take the signal cleanly
224 * and report that.
225 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800226 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700227
David S. Millerf036d9f2010-02-09 16:18:40 -0800228 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
David S. Miller392c2182010-09-21 21:41:12 -0700231static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
232 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David S. Miller5526b7e2008-04-27 02:26:36 -0700234 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700235 int sigframe_size, err, wsaved;
236 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* 1. Make sure everything is clean */
239 synchronize_user_stack();
240
David S. Miller55984732011-08-20 17:14:54 -0700241 wsaved = current_thread_info()->w_saved;
242
243 sigframe_size = sizeof(*sf);
244 if (used_math())
245 sigframe_size += sizeof(__siginfo_fpu_t);
246 if (wsaved)
247 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
David S. Miller5526b7e2008-04-27 02:26:36 -0700249 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 get_sigframe(&ka->sa, regs, sigframe_size);
251
252 if (invalid_frame_pointer(sf, sigframe_size))
253 goto sigill_and_return;
254
David S. Miller55984732011-08-20 17:14:54 -0700255 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* 2. Save the current process state */
258 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
259
260 err |= __put_user(0, &sf->extra_size);
261
262 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700263 __siginfo_fpu_t __user *fp = tail;
264 tail += sizeof(*fp);
265 err |= save_fpu_state(regs, fp);
266 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 } else {
268 err |= __put_user(0, &sf->fpu_save);
269 }
David S. Miller55984732011-08-20 17:14:54 -0700270 if (wsaved) {
271 __siginfo_rwin_t __user *rwp = tail;
272 tail += sizeof(*rwp);
273 err |= save_rwin_state(wsaved, rwp);
274 err |= __put_user(rwp, &sf->rwin_save);
275 } else {
276 err |= __put_user(0, &sf->rwin_save);
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
280 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
281 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700282 if (!wsaved) {
283 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
284 sizeof(struct reg_window32));
285 } else {
286 struct reg_window32 *rp;
287
288 rp = &current_thread_info()->reg_window[wsaved - 1];
289 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (err)
292 goto sigsegv;
293
294 /* 3. signal handler back-trampoline and parameters */
295 regs->u_regs[UREG_FP] = (unsigned long) sf;
296 regs->u_regs[UREG_I0] = signo;
297 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
298 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
299
300 /* 4. signal handler */
301 regs->pc = (unsigned long) ka->sa.sa_handler;
302 regs->npc = (regs->pc + 4);
303
304 /* 5. return to kernel instructions */
305 if (ka->ka_restorer)
306 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
307 else {
308 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
309
310 /* mov __NR_sigreturn, %g1 */
311 err |= __put_user(0x821020d8, &sf->insns[0]);
312
313 /* t 0x10 */
314 err |= __put_user(0x91d02010, &sf->insns[1]);
315 if (err)
316 goto sigsegv;
317
318 /* Flush instruction space. */
319 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
320 }
David S. Miller392c2182010-09-21 21:41:12 -0700321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323sigill_and_return:
324 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700325 return -EINVAL;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327sigsegv:
328 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700329 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
David S. Miller392c2182010-09-21 21:41:12 -0700332static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
333 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700336 int sigframe_size, wsaved;
337 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned int psr;
339 int err;
340
341 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700342 wsaved = current_thread_info()->w_saved;
343 sigframe_size = sizeof(*sf);
344 if (used_math())
345 sigframe_size += sizeof(__siginfo_fpu_t);
346 if (wsaved)
347 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sf = (struct rt_signal_frame __user *)
349 get_sigframe(&ka->sa, regs, sigframe_size);
350 if (invalid_frame_pointer(sf, sigframe_size))
351 goto sigill;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David S. Miller55984732011-08-20 17:14:54 -0700353 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 err = __put_user(regs->pc, &sf->regs.pc);
355 err |= __put_user(regs->npc, &sf->regs.npc);
356 err |= __put_user(regs->y, &sf->regs.y);
357 psr = regs->psr;
358 if (used_math())
359 psr |= PSR_EF;
360 err |= __put_user(psr, &sf->regs.psr);
361 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
362 err |= __put_user(0, &sf->extra_size);
363
364 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700365 __siginfo_fpu_t *fp = tail;
366 tail += sizeof(*fp);
367 err |= save_fpu_state(regs, fp);
368 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 } else {
370 err |= __put_user(0, &sf->fpu_save);
371 }
David S. Miller55984732011-08-20 17:14:54 -0700372 if (wsaved) {
373 __siginfo_rwin_t *rwp = tail;
374 tail += sizeof(*rwp);
375 err |= save_rwin_state(wsaved, rwp);
376 err |= __put_user(rwp, &sf->rwin_save);
377 } else {
378 err |= __put_user(0, &sf->rwin_save);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
381
382 /* Setup sigaltstack */
Al Viro99b06fe2012-12-23 03:41:17 -0500383 err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David S. Miller55984732011-08-20 17:14:54 -0700385 if (!wsaved) {
386 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
387 sizeof(struct reg_window32));
388 } else {
389 struct reg_window32 *rp;
390
391 rp = &current_thread_info()->reg_window[wsaved - 1];
392 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 err |= copy_siginfo_to_user(&sf->info, info);
396
397 if (err)
398 goto sigsegv;
399
400 regs->u_regs[UREG_FP] = (unsigned long) sf;
401 regs->u_regs[UREG_I0] = signo;
402 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
403 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
404
405 regs->pc = (unsigned long) ka->sa.sa_handler;
406 regs->npc = (regs->pc + 4);
407
408 if (ka->ka_restorer)
409 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
410 else {
411 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
412
413 /* mov __NR_sigreturn, %g1 */
414 err |= __put_user(0x821020d8, &sf->insns[0]);
415
416 /* t 0x10 */
417 err |= __put_user(0x91d02010, &sf->insns[1]);
418 if (err)
419 goto sigsegv;
420
421 /* Flush instruction space. */
422 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
423 }
David S. Miller392c2182010-09-21 21:41:12 -0700424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426sigill:
427 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700428 return -EINVAL;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430sigsegv:
431 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700432 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Al Viroa610d6e2012-05-21 23:42:15 -0400435static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436handle_signal(unsigned long signr, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400437 siginfo_t *info, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Al Virob7f9a112012-05-02 09:59:21 -0400439 sigset_t *oldset = sigmask_to_save();
David S. Miller392c2182010-09-21 21:41:12 -0700440 int err;
441
David S. Millerec98c6b2008-04-20 02:14:23 -0700442 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700443 err = setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700444 else
David S. Miller392c2182010-09-21 21:41:12 -0700445 err = setup_frame(ka, regs, signr, oldset);
446
447 if (err)
Al Viroa610d6e2012-05-21 23:42:15 -0400448 return;
David S. Millerec98c6b2008-04-20 02:14:23 -0700449
Al Viroefee9842012-04-28 02:04:15 -0400450 signal_delivered(signr, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
454 struct sigaction *sa)
455{
456 switch(regs->u_regs[UREG_I0]) {
457 case ERESTART_RESTARTBLOCK:
458 case ERESTARTNOHAND:
459 no_system_call_restart:
460 regs->u_regs[UREG_I0] = EINTR;
461 regs->psr |= PSR_C;
462 break;
463 case ERESTARTSYS:
464 if (!(sa->sa_flags & SA_RESTART))
465 goto no_system_call_restart;
466 /* fallthrough */
467 case ERESTARTNOINTR:
468 regs->u_regs[UREG_I0] = orig_i0;
469 regs->pc -= 4;
470 regs->npc -= 4;
471 }
472}
473
474/* Note that 'init' is a special process: it doesn't get signals it doesn't
475 * want to handle. Thus you cannot kill init even with a SIGKILL even by
476 * mistake.
477 */
David S. Miller5a157d52008-07-27 03:38:53 -0700478static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700481 int restart_syscall;
David S. Miller28e61032008-05-11 02:07:19 -0700482 siginfo_t info;
483 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
David S. Miller1d299bc2011-11-14 20:32:16 -0800485 /* It's a lot of work and synchronization to add a new ptrace
486 * register for GDB to save and restore in order to get
487 * orig_i0 correct for syscall restarts when debugging.
488 *
David S. Millere88d2462011-11-15 12:57:00 -0800489 * Although it should be the case that most of the global
490 * registers are volatile across a system call, glibc already
491 * depends upon that fact that we preserve them. So we can't
492 * just use any global register to save away the orig_i0 value.
493 *
494 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
495 * preserved across a system call trap by various pieces of
496 * code in glibc.
497 *
498 * %g7 is used as the "thread register". %g6 is not used in
499 * any fixed manner. %g6 is used as a scratch register and
500 * a compiler temporary, but it's value is never used across
501 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800502 */
David S. Miller28e61032008-05-11 02:07:19 -0700503 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800504 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David S. Miller28e61032008-05-11 02:07:19 -0700506 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
507
508 /* If the debugger messes with the program counter, it clears
509 * the software "in syscall" bit, directing us to not perform
510 * a syscall restart.
511 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800512 restart_syscall = 0;
513 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
514 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800515 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800516 }
517
David S. Miller28e61032008-05-11 02:07:19 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700520 if (restart_syscall)
521 syscall_restart(orig_i0, regs, &ka.sa);
Al Viroa610d6e2012-05-21 23:42:15 -0400522 handle_signal(signr, &ka, &info, regs);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
David S. Miller28e61032008-05-11 02:07:19 -0700525 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
527 regs->u_regs[UREG_I0] == ERESTARTSYS ||
528 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
529 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700530 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 regs->pc -= 4;
532 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700533 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
David S. Miller28e61032008-05-11 02:07:19 -0700535 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
537 regs->u_regs[UREG_G1] = __NR_restart_syscall;
538 regs->pc -= 4;
539 regs->npc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700540 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800542
543 /* if there's no signal to deliver, we just put the saved sigmask
544 * back
545 */
Al Viro51a7b442012-05-21 23:33:55 -0400546 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
David S. Miller5a157d52008-07-27 03:38:53 -0700549void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
550 unsigned long thread_info_flags)
551{
Al Viro6fd84c02012-05-23 15:28:58 -0400552 if (thread_info_flags & _TIF_SIGPENDING)
David S. Miller5a157d52008-07-27 03:38:53 -0700553 do_signal(regs, orig_i0);
554 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
555 clear_thread_flag(TIF_NOTIFY_RESUME);
556 tracehook_notify_resume(regs);
557 }
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560asmlinkage int
561do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
562 unsigned long sp)
563{
564 int ret = -EFAULT;
565
566 /* First see if old state is wanted. */
567 if (ossptr) {
568 if (put_user(current->sas_ss_sp + current->sas_ss_size,
569 &ossptr->the_stack) ||
570 __put_user(on_sig_stack(sp), &ossptr->cur_status))
571 goto out;
572 }
573
574 /* Now see if we want to update the new state. */
575 if (ssptr) {
576 char *ss_sp;
577
578 if (get_user(ss_sp, &ssptr->the_stack))
579 goto out;
580 /* If the current stack was set with sigaltstack, don't
581 swap stacks while we are on it. */
582 ret = -EPERM;
583 if (current->sas_ss_sp && on_sig_stack(sp))
584 goto out;
585
586 /* Since we don't know the extent of the stack, and we don't
587 track onstack-ness, but rather calculate it, we must
588 presume a size. Ho hum this interface is lossy. */
589 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
590 current->sas_ss_size = SIGSTKSZ;
591 }
592 ret = 0;
593out:
594 return ret;
595}