blob: 7d5d8e1f841569e02b02a8ddf303b47770cb694f [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. Miller5526b7e2008-04-27 02:26:36 -070062asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
David S. Miller5526b7e2008-04-27 02:26:36 -070064 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned long up_psr, pc, npc;
66 sigset_t set;
67 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -070068 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int err;
70
David S. Miller5526b7e2008-04-27 02:26:36 -070071 /* Always make any pending restarted system calls return -EINTR */
72 current_thread_info()->restart_block.fn = do_no_restart_syscall;
73
74 synchronize_user_stack();
75
76 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* 1. Make sure we are not getting garbage from the user */
79 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
80 goto segv_and_exit;
81
82 if (((unsigned long) sf) & 3)
83 goto segv_and_exit;
84
85 err = __get_user(pc, &sf->info.si_regs.pc);
86 err |= __get_user(npc, &sf->info.si_regs.npc);
87
88 if ((pc | npc) & 3)
89 goto segv_and_exit;
90
91 /* 2. Restore the state */
92 up_psr = regs->psr;
93 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
94
95 /* User can only change condition codes and FPU enabling in %psr. */
96 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
97 | (regs->psr & (PSR_ICC | PSR_EF));
98
David S. Miller28e61032008-05-11 02:07:19 -070099 /* Prevent syscall restart. */
100 pt_regs_clear_syscall(regs);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 err |= __get_user(fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (fpu_save)
104 err |= restore_fpu_state(regs, fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700105 err |= __get_user(rwin_save, &sf->rwin_save);
106 if (rwin_save)
107 err |= restore_rwin_state(rwin_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* This is pretty much atomic, no amount locking would prevent
110 * the races which exist anyways.
111 */
112 err |= __get_user(set.sig[0], &sf->info.si_mask);
113 err |= __copy_from_user(&set.sig[1], &sf->extramask,
114 (_NSIG_WORDS-1) * sizeof(unsigned int));
115
116 if (err)
117 goto segv_and_exit;
118
Matt Flemingfaddf592011-08-11 14:57:02 +0100119 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return;
121
122segv_and_exit:
123 force_sig(SIGSEGV, current);
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
127{
128 struct rt_signal_frame __user *sf;
129 unsigned int psr, pc, npc;
130 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700131 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int err;
134
135 synchronize_user_stack();
136 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
137 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
138 (((unsigned long) sf) & 0x03))
139 goto segv;
140
141 err = __get_user(pc, &sf->regs.pc);
142 err |= __get_user(npc, &sf->regs.npc);
143 err |= ((pc | npc) & 0x03);
144
145 err |= __get_user(regs->y, &sf->regs.y);
146 err |= __get_user(psr, &sf->regs.psr);
147
148 err |= __copy_from_user(&regs->u_regs[UREG_G1],
149 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
150
151 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
152
David S. Miller28e61032008-05-11 02:07:19 -0700153 /* Prevent syscall restart. */
154 pt_regs_clear_syscall(regs);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700157 if (!err && fpu_save)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 err |= restore_fpu_state(regs, fpu_save);
159 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
Al Viro99b06fe2012-12-23 03:41:17 -0500160 err |= restore_altstack(&sf->stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (err)
163 goto segv;
164
165 regs->pc = pc;
166 regs->npc = npc;
167
David S. Miller55984732011-08-20 17:14:54 -0700168 err |= __get_user(rwin_save, &sf->rwin_save);
169 if (!err && rwin_save) {
170 if (restore_rwin_state(rwin_save))
171 goto segv;
172 }
173
Matt Flemingfaddf592011-08-11 14:57:02 +0100174 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return;
176segv:
177 force_sig(SIGSEGV, current);
178}
179
180/* Checks if the fp is valid */
181static inline int invalid_frame_pointer(void __user *fp, int fplen)
182{
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000183 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 1;
Sam Ravnborg3c23a762012-05-11 11:35:14 +0000185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return 0;
187}
188
Al Viro08f73952012-11-07 23:48:13 -0500189static inline void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700191 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700193 /*
194 * If we are on the alternate signal stack and would overflow it, don't.
195 * Return an always-bogus address instead so we will die with SIGSEGV.
196 */
197 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
198 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /* This is the X/Open sanctioned signal stack switching. */
Al Viro08f73952012-11-07 23:48:13 -0500201 sp = sigsp(sp, ksig) - framesize;
David S. Millerf036d9f2010-02-09 16:18:40 -0800202
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700203 /* Always align the stack frame. This handles two cases. First,
204 * sigaltstack need not be mindful of platform specific stack
205 * alignment. Second, if we took this signal because the stack
206 * is not aligned properly, we'd like to take the signal cleanly
207 * and report that.
208 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800209 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700210
David S. Millerf036d9f2010-02-09 16:18:40 -0800211 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Al Viro08f73952012-11-07 23:48:13 -0500214static int setup_frame(struct ksignal *ksig, struct pt_regs *regs,
215 sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
David S. Miller5526b7e2008-04-27 02:26:36 -0700217 struct signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700218 int sigframe_size, err, wsaved;
219 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* 1. Make sure everything is clean */
222 synchronize_user_stack();
223
David S. Miller55984732011-08-20 17:14:54 -0700224 wsaved = current_thread_info()->w_saved;
225
226 sigframe_size = sizeof(*sf);
227 if (used_math())
228 sigframe_size += sizeof(__siginfo_fpu_t);
229 if (wsaved)
230 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
David S. Miller5526b7e2008-04-27 02:26:36 -0700232 sf = (struct signal_frame __user *)
Al Viro08f73952012-11-07 23:48:13 -0500233 get_sigframe(ksig, regs, sigframe_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Al Viro08f73952012-11-07 23:48:13 -0500235 if (invalid_frame_pointer(sf, sigframe_size)) {
236 do_exit(SIGILL);
237 return -EINVAL;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
David S. Miller55984732011-08-20 17:14:54 -0700240 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* 2. Save the current process state */
243 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
244
245 err |= __put_user(0, &sf->extra_size);
246
247 if (used_math()) {
David S. Miller55984732011-08-20 17:14:54 -0700248 __siginfo_fpu_t __user *fp = tail;
249 tail += sizeof(*fp);
250 err |= save_fpu_state(regs, fp);
251 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 } else {
253 err |= __put_user(0, &sf->fpu_save);
254 }
David S. Miller55984732011-08-20 17:14:54 -0700255 if (wsaved) {
256 __siginfo_rwin_t __user *rwp = tail;
257 tail += sizeof(*rwp);
258 err |= save_rwin_state(wsaved, rwp);
259 err |= __put_user(rwp, &sf->rwin_save);
260 } else {
261 err |= __put_user(0, &sf->rwin_save);
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
265 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
266 (_NSIG_WORDS - 1) * sizeof(unsigned int));
David S. Miller55984732011-08-20 17:14:54 -0700267 if (!wsaved) {
268 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
269 sizeof(struct reg_window32));
270 } else {
271 struct reg_window32 *rp;
272
273 rp = &current_thread_info()->reg_window[wsaved - 1];
274 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (err)
Al Viro08f73952012-11-07 23:48:13 -0500277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* 3. signal handler back-trampoline and parameters */
280 regs->u_regs[UREG_FP] = (unsigned long) sf;
Al Viro08f73952012-11-07 23:48:13 -0500281 regs->u_regs[UREG_I0] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
283 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
284
285 /* 4. signal handler */
Al Viro08f73952012-11-07 23:48:13 -0500286 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 regs->npc = (regs->pc + 4);
288
289 /* 5. return to kernel instructions */
Al Viro08f73952012-11-07 23:48:13 -0500290 if (ksig->ka.ka_restorer)
291 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 else {
293 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
294
295 /* mov __NR_sigreturn, %g1 */
296 err |= __put_user(0x821020d8, &sf->insns[0]);
297
298 /* t 0x10 */
299 err |= __put_user(0x91d02010, &sf->insns[1]);
300 if (err)
Al Viro08f73952012-11-07 23:48:13 -0500301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Flush instruction space. */
304 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
305 }
David S. Miller392c2182010-09-21 21:41:12 -0700306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Al Viro08f73952012-11-07 23:48:13 -0500309static int setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs,
310 sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700313 int sigframe_size, wsaved;
314 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned int psr;
316 int err;
317
318 synchronize_user_stack();
David S. Miller55984732011-08-20 17:14:54 -0700319 wsaved = current_thread_info()->w_saved;
320 sigframe_size = sizeof(*sf);
321 if (used_math())
322 sigframe_size += sizeof(__siginfo_fpu_t);
323 if (wsaved)
324 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sf = (struct rt_signal_frame __user *)
Al Viro08f73952012-11-07 23:48:13 -0500326 get_sigframe(ksig, regs, sigframe_size);
327 if (invalid_frame_pointer(sf, sigframe_size)) {
328 do_exit(SIGILL);
329 return -EINVAL;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David S. Miller55984732011-08-20 17:14:54 -0700332 tail = sf + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 err = __put_user(regs->pc, &sf->regs.pc);
334 err |= __put_user(regs->npc, &sf->regs.npc);
335 err |= __put_user(regs->y, &sf->regs.y);
336 psr = regs->psr;
337 if (used_math())
338 psr |= PSR_EF;
339 err |= __put_user(psr, &sf->regs.psr);
340 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
341 err |= __put_user(0, &sf->extra_size);
342
343 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700344 __siginfo_fpu_t *fp = tail;
345 tail += sizeof(*fp);
346 err |= save_fpu_state(regs, fp);
347 err |= __put_user(fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 } else {
349 err |= __put_user(0, &sf->fpu_save);
350 }
David S. Miller55984732011-08-20 17:14:54 -0700351 if (wsaved) {
352 __siginfo_rwin_t *rwp = tail;
353 tail += sizeof(*rwp);
354 err |= save_rwin_state(wsaved, rwp);
355 err |= __put_user(rwp, &sf->rwin_save);
356 } else {
357 err |= __put_user(0, &sf->rwin_save);
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
360
361 /* Setup sigaltstack */
Al Viro99b06fe2012-12-23 03:41:17 -0500362 err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David S. Miller55984732011-08-20 17:14:54 -0700364 if (!wsaved) {
365 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
366 sizeof(struct reg_window32));
367 } else {
368 struct reg_window32 *rp;
369
370 rp = &current_thread_info()->reg_window[wsaved - 1];
371 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Al Viro08f73952012-11-07 23:48:13 -0500374 err |= copy_siginfo_to_user(&sf->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (err)
Al Viro08f73952012-11-07 23:48:13 -0500377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 regs->u_regs[UREG_FP] = (unsigned long) sf;
Al Viro08f73952012-11-07 23:48:13 -0500380 regs->u_regs[UREG_I0] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
382 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
383
Al Viro08f73952012-11-07 23:48:13 -0500384 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 regs->npc = (regs->pc + 4);
386
Al Viro08f73952012-11-07 23:48:13 -0500387 if (ksig->ka.ka_restorer)
388 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 else {
390 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
391
392 /* mov __NR_sigreturn, %g1 */
393 err |= __put_user(0x821020d8, &sf->insns[0]);
394
395 /* t 0x10 */
396 err |= __put_user(0x91d02010, &sf->insns[1]);
397 if (err)
Al Viro08f73952012-11-07 23:48:13 -0500398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* Flush instruction space. */
401 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
402 }
David S. Miller392c2182010-09-21 21:41:12 -0700403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Al Viroa610d6e2012-05-21 23:42:15 -0400406static inline void
Al Viro08f73952012-11-07 23:48:13 -0500407handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Al Virob7f9a112012-05-02 09:59:21 -0400409 sigset_t *oldset = sigmask_to_save();
David S. Miller392c2182010-09-21 21:41:12 -0700410 int err;
411
Al Viro08f73952012-11-07 23:48:13 -0500412 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
413 err = setup_rt_frame(ksig, regs, oldset);
David S. Millerec98c6b2008-04-20 02:14:23 -0700414 else
Al Viro08f73952012-11-07 23:48:13 -0500415 err = setup_frame(ksig, regs, oldset);
416 signal_setup_done(err, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
420 struct sigaction *sa)
421{
422 switch(regs->u_regs[UREG_I0]) {
423 case ERESTART_RESTARTBLOCK:
424 case ERESTARTNOHAND:
425 no_system_call_restart:
426 regs->u_regs[UREG_I0] = EINTR;
427 regs->psr |= PSR_C;
428 break;
429 case ERESTARTSYS:
430 if (!(sa->sa_flags & SA_RESTART))
431 goto no_system_call_restart;
432 /* fallthrough */
433 case ERESTARTNOINTR:
434 regs->u_regs[UREG_I0] = orig_i0;
435 regs->pc -= 4;
436 regs->npc -= 4;
437 }
438}
439
440/* Note that 'init' is a special process: it doesn't get signals it doesn't
441 * want to handle. Thus you cannot kill init even with a SIGKILL even by
442 * mistake.
443 */
David S. Miller5a157d52008-07-27 03:38:53 -0700444static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Al Viro08f73952012-11-07 23:48:13 -0500446 struct ksignal ksig;
David S. Miller28e61032008-05-11 02:07:19 -0700447 int restart_syscall;
Al Viro08f73952012-11-07 23:48:13 -0500448 bool has_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
David S. Miller1d299bc2011-11-14 20:32:16 -0800450 /* It's a lot of work and synchronization to add a new ptrace
451 * register for GDB to save and restore in order to get
452 * orig_i0 correct for syscall restarts when debugging.
453 *
David S. Millere88d2462011-11-15 12:57:00 -0800454 * Although it should be the case that most of the global
455 * registers are volatile across a system call, glibc already
456 * depends upon that fact that we preserve them. So we can't
457 * just use any global register to save away the orig_i0 value.
458 *
459 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
460 * preserved across a system call trap by various pieces of
461 * code in glibc.
462 *
463 * %g7 is used as the "thread register". %g6 is not used in
464 * any fixed manner. %g6 is used as a scratch register and
465 * a compiler temporary, but it's value is never used across
466 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800467 */
David S. Miller28e61032008-05-11 02:07:19 -0700468 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
David S. Millere88d2462011-11-15 12:57:00 -0800469 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Al Viro08f73952012-11-07 23:48:13 -0500471 has_handler = get_signal(&ksig);
David S. Miller28e61032008-05-11 02:07:19 -0700472
473 /* If the debugger messes with the program counter, it clears
474 * the software "in syscall" bit, directing us to not perform
475 * a syscall restart.
476 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800477 restart_syscall = 0;
478 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
479 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800480 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800481 }
482
Al Viro08f73952012-11-07 23:48:13 -0500483 if (has_handler) {
David S. Miller28e61032008-05-11 02:07:19 -0700484 if (restart_syscall)
Al Viro08f73952012-11-07 23:48:13 -0500485 syscall_restart(orig_i0, regs, &ksig.ka.sa);
486 handle_signal(&ksig, regs);
487 } else {
488 if (restart_syscall) {
489 switch (regs->u_regs[UREG_I0]) {
490 case ERESTARTNOHAND:
491 case ERESTARTSYS:
492 case ERESTARTNOINTR:
493 /* replay the system call when we are done */
494 regs->u_regs[UREG_I0] = orig_i0;
495 regs->pc -= 4;
496 regs->npc -= 4;
497 pt_regs_clear_syscall(regs);
498 case ERESTART_RESTARTBLOCK:
499 regs->u_regs[UREG_G1] = __NR_restart_syscall;
500 regs->pc -= 4;
501 regs->npc -= 4;
502 pt_regs_clear_syscall(regs);
503 }
504 }
505 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
David S. Miller5a157d52008-07-27 03:38:53 -0700509void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
510 unsigned long thread_info_flags)
511{
Al Viro6fd84c02012-05-23 15:28:58 -0400512 if (thread_info_flags & _TIF_SIGPENDING)
David S. Miller5a157d52008-07-27 03:38:53 -0700513 do_signal(regs, orig_i0);
514 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
515 clear_thread_flag(TIF_NOTIFY_RESUME);
516 tracehook_notify_resume(regs);
517 }
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520asmlinkage int
521do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
522 unsigned long sp)
523{
524 int ret = -EFAULT;
525
526 /* First see if old state is wanted. */
527 if (ossptr) {
528 if (put_user(current->sas_ss_sp + current->sas_ss_size,
529 &ossptr->the_stack) ||
530 __put_user(on_sig_stack(sp), &ossptr->cur_status))
531 goto out;
532 }
533
534 /* Now see if we want to update the new state. */
535 if (ssptr) {
536 char *ss_sp;
537
538 if (get_user(ss_sp, &ssptr->the_stack))
539 goto out;
540 /* If the current stack was set with sigaltstack, don't
541 swap stacks while we are on it. */
542 ret = -EPERM;
543 if (current->sas_ss_sp && on_sig_stack(sp))
544 goto out;
545
546 /* Since we don't know the extent of the stack, and we don't
547 track onstack-ness, but rather calculate it, we must
548 presume a size. Ho hum this interface is lossy. */
549 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
550 current->sas_ss_size = SIGSTKSZ;
551 }
552 ret = 0;
553out:
554 return ret;
555}