blob: 52aa5e4ce5e77c0716e083dd41372c16e5ac8967 [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"
Sam Ravnborg529b17a2014-04-21 21:39:26 +020031#include "kernel.h"
David S. Miller55984732011-08-20 17:14:54 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
34 void *fpqueue, unsigned long *fpqdepth);
35extern void fpload(unsigned long *fpregs, unsigned long *fsr);
36
David S. Miller5526b7e2008-04-27 02:26:36 -070037struct signal_frame {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct sparc_stackf ss;
Sam Ravnborg4d7b92a2009-01-02 19:32:59 -080039 __siginfo32_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 __siginfo_fpu_t __user *fpu_save;
41 unsigned long insns[2] __attribute__ ((aligned (8)));
42 unsigned int extramask[_NSIG_WORDS - 1];
43 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070044 __siginfo_rwin_t __user *rwin_save;
45} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47struct rt_signal_frame {
48 struct sparc_stackf ss;
49 siginfo_t info;
50 struct pt_regs regs;
51 sigset_t mask;
52 __siginfo_fpu_t __user *fpu_save;
53 unsigned int insns[2];
54 stack_t stack;
55 unsigned int extra_size; /* Should be 0 */
David S. Miller55984732011-08-20 17:14:54 -070056 __siginfo_rwin_t __user *rwin_save;
57} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* Align macros */
David S. Miller5526b7e2008-04-27 02:26:36 -070060#define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
62
David S. Miller5526b7e2008-04-27 02:26:36 -070063asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
David S. Miller5526b7e2008-04-27 02:26:36 -070065 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 unsigned long up_psr, pc, npc;
67 sigset_t set;
68 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070069 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 int err;
71
David S. Miller5526b7e2008-04-27 02:26:36 -070072 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080073 current->restart_block.fn = do_no_restart_syscall;
David S. Miller5526b7e2008-04-27 02:26:36 -070074
75 synchronize_user_stack();
76
77 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /* 1. Make sure we are not getting garbage from the user */
80 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
81 goto segv_and_exit;
82
83 if (((unsigned long) sf) & 3)
84 goto segv_and_exit;
85
86 err = __get_user(pc, &sf->info.si_regs.pc);
87 err |= __get_user(npc, &sf->info.si_regs.npc);
88
89 if ((pc | npc) & 3)
90 goto segv_and_exit;
91
92 /* 2. Restore the state */
93 up_psr = regs->psr;
94 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
95
96 /* User can only change condition codes and FPU enabling in %psr. */
97 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
98 | (regs->psr & (PSR_ICC | PSR_EF));
99
David S. Miller28e61032008-05-11 02:07:19 -0700100 /* Prevent syscall restart. */
101 pt_regs_clear_syscall(regs);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (fpu_save)
105 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700106 err |= __get_user(rwin_save, &sf->rwin_save);
107 if (rwin_save)
108 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /* This is pretty much atomic, no amount locking would prevent
111 * the races which exist anyways.
112 */
113 err |= __get_user(set.sig[0], &sf->info.si_mask);
114 err |= __copy_from_user(&set.sig[1], &sf->extramask,
115 (_NSIG_WORDS-1) * sizeof(unsigned int));
116
117 if (err)
118 goto segv_and_exit;
119
Matt Flemingfaddf592011-08-11 14:57:02 +0100120 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return;
122
123segv_and_exit:
124 force_sig(SIGSEGV, current);
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
128{
129 struct rt_signal_frame __user *sf;
130 unsigned int psr, pc, npc;
131 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700132 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int err;
135
136 synchronize_user_stack();
137 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
138 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
139 (((unsigned long) sf) & 0x03))
140 goto segv;
141
142 err = __get_user(pc, &sf->regs.pc);
143 err |= __get_user(npc, &sf->regs.npc);
144 err |= ((pc | npc) & 0x03);
145
146 err |= __get_user(regs->y, &sf->regs.y);
147 err |= __get_user(psr, &sf->regs.psr);
148
149 err |= __copy_from_user(&regs->u_regs[UREG_G1],
150 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
151
152 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
153
David S. Miller28e61032008-05-11 02:07:19 -0700154 /* Prevent syscall restart. */
155 pt_regs_clear_syscall(regs);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700158 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 err |= restore_fpu_state(regs, fpu_save);
160 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
Al Viro99b06fe2012-12-23 03:41:17 -0500161 err |= restore_altstack(&sf->stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (err)
164 goto segv;
165
166 regs->pc = pc;
167 regs->npc = npc;
168
David S. Miller55984732011-08-20 17:14:54 -0700169 err |= __get_user(rwin_save, &sf->rwin_save);
170 if (!err && rwin_save) {
171 if (restore_rwin_state(rwin_save))
172 goto segv;
173 }
174
Matt Flemingfaddf592011-08-11 14:57:02 +0100175 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return;
177segv:
178 force_sig(SIGSEGV, current);
179}
180
181/* Checks if the fp is valid */
182static inline int invalid_frame_pointer(void __user *fp, int fplen)
183{
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000184 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 1;
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return 0;
188}
189
Al Viro08f739572012-11-07 23:48:13 -0500190static inline void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700192 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700194 /*
195 * If we are on the alternate signal stack and would overflow it, don't.
196 * Return an always-bogus address instead so we will die with SIGSEGV.
197 */
198 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
199 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* This is the X/Open sanctioned signal stack switching. */
Al Viro08f739572012-11-07 23:48:13 -0500202 sp = sigsp(sp, ksig) - framesize;
David S. Millerf036d9f2010-02-09 16:18:40 -0800203
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700204 /* Always align the stack frame. This handles two cases. First,
205 * sigaltstack need not be mindful of platform specific stack
206 * alignment. Second, if we took this signal because the stack
207 * is not aligned properly, we'd like to take the signal cleanly
208 * and report that.
209 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800210 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700211
David S. Millerf036d9f2010-02-09 16:18:40 -0800212 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Al Viro08f739572012-11-07 23:48:13 -0500215static int setup_frame(struct ksignal *ksig, struct pt_regs *regs,
216 sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
David S. Miller5526b7e2008-04-27 02:26:36 -0700218 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700219 int sigframe_size, err, wsaved;
220 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* 1. Make sure everything is clean */
223 synchronize_user_stack();
224
David S. Miller55984732011-08-20 17:14:54 -0700225 wsaved = current_thread_info()->w_saved;
226
227 sigframe_size = sizeof(*sf);
228 if (used_math())
229 sigframe_size += sizeof(__siginfo_fpu_t);
230 if (wsaved)
231 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
David S. Miller5526b7e2008-04-27 02:26:36 -0700233 sf = (struct signal_frame __user *)
Al Viro08f739572012-11-07 23:48:13 -0500234 get_sigframe(ksig, regs, sigframe_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Al Viro08f739572012-11-07 23:48:13 -0500236 if (invalid_frame_pointer(sf, sigframe_size)) {
237 do_exit(SIGILL);
238 return -EINVAL;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
David S. Miller55984732011-08-20 17:14:54 -0700241 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* 2. Save the current process state */
244 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
245
246 err |= __put_user(0, &sf->extra_size);
247
248 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700249 __siginfo_fpu_t __user *fp = tail;
250 tail += sizeof(*fp);
251 err |= save_fpu_state(regs, fp);
252 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } else {
254 err |= __put_user(0, &sf->fpu_save);
255 }
David S. Miller55984732011-08-20 17:14:54 -0700256 if (wsaved) {
257 __siginfo_rwin_t __user *rwp = tail;
258 tail += sizeof(*rwp);
259 err |= save_rwin_state(wsaved, rwp);
260 err |= __put_user(rwp, &sf->rwin_save);
261 } else {
262 err |= __put_user(0, &sf->rwin_save);
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
266 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
267 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700268 if (!wsaved) {
269 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
270 sizeof(struct reg_window32));
271 } else {
272 struct reg_window32 *rp;
273
274 rp = &current_thread_info()->reg_window[wsaved - 1];
275 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* 3. signal handler back-trampoline and parameters */
281 regs->u_regs[UREG_FP] = (unsigned long) sf;
Al Viro08f739572012-11-07 23:48:13 -0500282 regs->u_regs[UREG_I0] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
284 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
285
286 /* 4. signal handler */
Al Viro08f739572012-11-07 23:48:13 -0500287 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 regs->npc = (regs->pc + 4);
289
290 /* 5. return to kernel instructions */
Al Viro08f739572012-11-07 23:48:13 -0500291 if (ksig->ka.ka_restorer)
292 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 else {
294 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
295
296 /* mov __NR_sigreturn, %g1 */
297 err |= __put_user(0x821020d8, &sf->insns[0]);
298
299 /* t 0x10 */
300 err |= __put_user(0x91d02010, &sf->insns[1]);
301 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Flush instruction space. */
305 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
306 }
David S. Miller392c2182010-09-21 21:41:12 -0700307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Al Viro08f739572012-11-07 23:48:13 -0500310static int setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs,
311 sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700314 int sigframe_size, wsaved;
315 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned int psr;
317 int err;
318
319 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700320 wsaved = current_thread_info()->w_saved;
321 sigframe_size = sizeof(*sf);
322 if (used_math())
323 sigframe_size += sizeof(__siginfo_fpu_t);
324 if (wsaved)
325 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sf = (struct rt_signal_frame __user *)
Al Viro08f739572012-11-07 23:48:13 -0500327 get_sigframe(ksig, regs, sigframe_size);
328 if (invalid_frame_pointer(sf, sigframe_size)) {
329 do_exit(SIGILL);
330 return -EINVAL;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
David S. Miller55984732011-08-20 17:14:54 -0700333 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 err = __put_user(regs->pc, &sf->regs.pc);
335 err |= __put_user(regs->npc, &sf->regs.npc);
336 err |= __put_user(regs->y, &sf->regs.y);
337 psr = regs->psr;
338 if (used_math())
339 psr |= PSR_EF;
340 err |= __put_user(psr, &sf->regs.psr);
341 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
342 err |= __put_user(0, &sf->extra_size);
343
344 if (psr & PSR_EF) {
Sam Ravnborg529b17a2014-04-21 21:39:26 +0200345 __siginfo_fpu_t __user *fp = tail;
David S. Miller55984732011-08-20 17:14:54 -0700346 tail += sizeof(*fp);
347 err |= save_fpu_state(regs, fp);
348 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
350 err |= __put_user(0, &sf->fpu_save);
351 }
David S. Miller55984732011-08-20 17:14:54 -0700352 if (wsaved) {
Sam Ravnborg529b17a2014-04-21 21:39:26 +0200353 __siginfo_rwin_t __user *rwp = tail;
David S. Miller55984732011-08-20 17:14:54 -0700354 tail += sizeof(*rwp);
355 err |= save_rwin_state(wsaved, rwp);
356 err |= __put_user(rwp, &sf->rwin_save);
357 } else {
358 err |= __put_user(0, &sf->rwin_save);
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
361
362 /* Setup sigaltstack */
Al Viro99b06fe2012-12-23 03:41:17 -0500363 err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
David S. Miller55984732011-08-20 17:14:54 -0700365 if (!wsaved) {
366 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
367 sizeof(struct reg_window32));
368 } else {
369 struct reg_window32 *rp;
370
371 rp = &current_thread_info()->reg_window[wsaved - 1];
372 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Al Viro08f739572012-11-07 23:48:13 -0500375 err |= copy_siginfo_to_user(&sf->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500378 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 regs->u_regs[UREG_FP] = (unsigned long) sf;
Al Viro08f739572012-11-07 23:48:13 -0500381 regs->u_regs[UREG_I0] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
383 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
384
Al Viro08f739572012-11-07 23:48:13 -0500385 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 regs->npc = (regs->pc + 4);
387
Al Viro08f739572012-11-07 23:48:13 -0500388 if (ksig->ka.ka_restorer)
389 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 else {
391 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
392
393 /* mov __NR_sigreturn, %g1 */
394 err |= __put_user(0x821020d8, &sf->insns[0]);
395
396 /* t 0x10 */
397 err |= __put_user(0x91d02010, &sf->insns[1]);
398 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* Flush instruction space. */
402 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
403 }
David S. Miller392c2182010-09-21 21:41:12 -0700404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Al Viroa610d6e2012-05-21 23:42:15 -0400407static inline void
Al Viro08f739572012-11-07 23:48:13 -0500408handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Al Virob7f9a112012-05-02 09:59:21 -0400410 sigset_t *oldset = sigmask_to_save();
David S. Miller392c2182010-09-21 21:41:12 -0700411 int err;
412
Al Viro08f739572012-11-07 23:48:13 -0500413 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
414 err = setup_rt_frame(ksig, regs, oldset);
David S. Millerec98c6b2008-04-20 02:14:23 -0700415 else
Al Viro08f739572012-11-07 23:48:13 -0500416 err = setup_frame(ksig, regs, oldset);
417 signal_setup_done(err, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
421 struct sigaction *sa)
422{
423 switch(regs->u_regs[UREG_I0]) {
424 case ERESTART_RESTARTBLOCK:
425 case ERESTARTNOHAND:
426 no_system_call_restart:
427 regs->u_regs[UREG_I0] = EINTR;
428 regs->psr |= PSR_C;
429 break;
430 case ERESTARTSYS:
431 if (!(sa->sa_flags & SA_RESTART))
432 goto no_system_call_restart;
433 /* fallthrough */
434 case ERESTARTNOINTR:
435 regs->u_regs[UREG_I0] = orig_i0;
436 regs->pc -= 4;
437 regs->npc -= 4;
438 }
439}
440
441/* Note that 'init' is a special process: it doesn't get signals it doesn't
442 * want to handle. Thus you cannot kill init even with a SIGKILL even by
443 * mistake.
444 */
David S. Miller5a157d52008-07-27 03:38:53 -0700445static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Al Viro08f739572012-11-07 23:48:13 -0500447 struct ksignal ksig;
David S. Miller28e61032008-05-11 02:07:19 -0700448 int restart_syscall;
Al Viro08f739572012-11-07 23:48:13 -0500449 bool has_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
David S. Miller1d299bc2011-11-14 20:32:16 -0800451 /* It's a lot of work and synchronization to add a new ptrace
452 * register for GDB to save and restore in order to get
453 * orig_i0 correct for syscall restarts when debugging.
454 *
David S. Millere88d2462011-11-15 12:57:00 -0800455 * Although it should be the case that most of the global
456 * registers are volatile across a system call, glibc already
457 * depends upon that fact that we preserve them. So we can't
458 * just use any global register to save away the orig_i0 value.
459 *
460 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
461 * preserved across a system call trap by various pieces of
462 * code in glibc.
463 *
464 * %g7 is used as the "thread register". %g6 is not used in
465 * any fixed manner. %g6 is used as a scratch register and
466 * a compiler temporary, but it's value is never used across
467 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800468 */
David S. Miller28e61032008-05-11 02:07:19 -0700469 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800470 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Al Viro08f739572012-11-07 23:48:13 -0500472 has_handler = get_signal(&ksig);
David S. Miller28e61032008-05-11 02:07:19 -0700473
474 /* If the debugger messes with the program counter, it clears
475 * the software "in syscall" bit, directing us to not perform
476 * a syscall restart.
477 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800478 restart_syscall = 0;
479 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
480 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800481 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800482 }
483
Al Viro08f739572012-11-07 23:48:13 -0500484 if (has_handler) {
David S. Miller28e61032008-05-11 02:07:19 -0700485 if (restart_syscall)
Al Viro08f739572012-11-07 23:48:13 -0500486 syscall_restart(orig_i0, regs, &ksig.ka.sa);
487 handle_signal(&ksig, regs);
488 } else {
489 if (restart_syscall) {
490 switch (regs->u_regs[UREG_I0]) {
491 case ERESTARTNOHAND:
492 case ERESTARTSYS:
493 case ERESTARTNOINTR:
494 /* replay the system call when we are done */
495 regs->u_regs[UREG_I0] = orig_i0;
496 regs->pc -= 4;
497 regs->npc -= 4;
498 pt_regs_clear_syscall(regs);
499 case ERESTART_RESTARTBLOCK:
500 regs->u_regs[UREG_G1] = __NR_restart_syscall;
501 regs->pc -= 4;
502 regs->npc -= 4;
503 pt_regs_clear_syscall(regs);
504 }
505 }
506 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
David S. Miller5a157d52008-07-27 03:38:53 -0700510void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
511 unsigned long thread_info_flags)
512{
Al Viro6fd84c02012-05-23 15:28:58 -0400513 if (thread_info_flags & _TIF_SIGPENDING)
David S. Miller5a157d52008-07-27 03:38:53 -0700514 do_signal(regs, orig_i0);
515 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
516 clear_thread_flag(TIF_NOTIFY_RESUME);
517 tracehook_notify_resume(regs);
518 }
519}
520
Sam Ravnborg529b17a2014-04-21 21:39:26 +0200521asmlinkage int do_sys_sigstack(struct sigstack __user *ssptr,
522 struct sigstack __user *ossptr,
523 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 int ret = -EFAULT;
526
527 /* First see if old state is wanted. */
528 if (ossptr) {
529 if (put_user(current->sas_ss_sp + current->sas_ss_size,
530 &ossptr->the_stack) ||
531 __put_user(on_sig_stack(sp), &ossptr->cur_status))
532 goto out;
533 }
534
535 /* Now see if we want to update the new state. */
536 if (ssptr) {
537 char *ss_sp;
538
539 if (get_user(ss_sp, &ssptr->the_stack))
540 goto out;
541 /* If the current stack was set with sigaltstack, don't
542 swap stacks while we are on it. */
543 ret = -EPERM;
544 if (current->sas_ss_sp && on_sig_stack(sp))
545 goto out;
546
547 /* Since we don't know the extent of the stack, and we don't
548 track onstack-ness, but rather calculate it, we must
549 presume a size. Ho hum this interface is lossy. */
550 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
551 current->sas_ss_size = SIGSTKSZ;
552 }
553 ret = 0;
554out:
555 return ret;
556}