blob: 3fd1df9f9ba7e849609f5018d443378a301fb2e4 [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>
21
22#include <asm/uaccess.h>
23#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/pgalloc.h>
25#include <asm/pgtable.h>
26#include <asm/cacheflush.h> /* flush_sig_insns */
27
28#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
29
30extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
31 void *fpqueue, unsigned long *fpqdepth);
32extern void fpload(unsigned long *fpregs, unsigned long *fsr);
33
David S. Miller5526b7e2008-04-27 02:26:36 -070034struct signal_frame {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct sparc_stackf ss;
36 __siginfo_t info;
37 __siginfo_fpu_t __user *fpu_save;
38 unsigned long insns[2] __attribute__ ((aligned (8)));
39 unsigned int extramask[_NSIG_WORDS - 1];
40 unsigned int extra_size; /* Should be 0 */
41 __siginfo_fpu_t fpu_state;
42};
43
44struct rt_signal_frame {
45 struct sparc_stackf ss;
46 siginfo_t info;
47 struct pt_regs regs;
48 sigset_t mask;
49 __siginfo_fpu_t __user *fpu_save;
50 unsigned int insns[2];
51 stack_t stack;
52 unsigned int extra_size; /* Should be 0 */
53 __siginfo_fpu_t fpu_state;
54};
55
56/* Align macros */
David S. Miller5526b7e2008-04-27 02:26:36 -070057#define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
59
David S. Miller2d7d5f02006-01-19 02:42:49 -080060static int _sigpause_common(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 set &= _BLOCKABLE;
63 spin_lock_irq(&current->sighand->siglock);
David S. Miller2d7d5f02006-01-19 02:42:49 -080064 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 siginitset(&current->blocked, set);
66 recalc_sigpending();
67 spin_unlock_irq(&current->sighand->siglock);
68
David S. Miller2d7d5f02006-01-19 02:42:49 -080069 current->state = TASK_INTERRUPTIBLE;
70 schedule();
71 set_thread_flag(TIF_RESTORE_SIGMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David S. Miller2d7d5f02006-01-19 02:42:49 -080073 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
David S. Miller2d7d5f02006-01-19 02:42:49 -080076asmlinkage int sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
David S. Miller2d7d5f02006-01-19 02:42:49 -080078 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static inline int
82restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
83{
84 int err;
85#ifdef CONFIG_SMP
86 if (test_tsk_thread_flag(current, TIF_USEDFPU))
87 regs->psr &= ~PSR_EF;
88#else
89 if (current == last_task_used_math) {
90 last_task_used_math = NULL;
91 regs->psr &= ~PSR_EF;
92 }
93#endif
94 set_used_math();
95 clear_tsk_thread_flag(current, TIF_USEDFPU);
96
97 if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
98 return -EFAULT;
99
100 err = __copy_from_user(&current->thread.float_regs[0], &fpu->si_float_regs[0],
101 (sizeof(unsigned long) * 32));
102 err |= __get_user(current->thread.fsr, &fpu->si_fsr);
103 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
104 if (current->thread.fpqdepth != 0)
105 err |= __copy_from_user(&current->thread.fpqueue[0],
106 &fpu->si_fpqueue[0],
107 ((sizeof(unsigned long) +
108 (sizeof(unsigned long *)))*16));
109 return err;
110}
111
David S. Miller5526b7e2008-04-27 02:26:36 -0700112asmlinkage void do_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
David S. Miller5526b7e2008-04-27 02:26:36 -0700114 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned long up_psr, pc, npc;
116 sigset_t set;
117 __siginfo_fpu_t __user *fpu_save;
118 int err;
119
David S. Miller5526b7e2008-04-27 02:26:36 -0700120 /* Always make any pending restarted system calls return -EINTR */
121 current_thread_info()->restart_block.fn = do_no_restart_syscall;
122
123 synchronize_user_stack();
124
125 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 /* 1. Make sure we are not getting garbage from the user */
128 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
129 goto segv_and_exit;
130
131 if (((unsigned long) sf) & 3)
132 goto segv_and_exit;
133
134 err = __get_user(pc, &sf->info.si_regs.pc);
135 err |= __get_user(npc, &sf->info.si_regs.npc);
136
137 if ((pc | npc) & 3)
138 goto segv_and_exit;
139
140 /* 2. Restore the state */
141 up_psr = regs->psr;
142 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
143
144 /* User can only change condition codes and FPU enabling in %psr. */
145 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
146 | (regs->psr & (PSR_ICC | PSR_EF));
147
David S. Miller28e61032008-05-11 02:07:19 -0700148 /* Prevent syscall restart. */
149 pt_regs_clear_syscall(regs);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 err |= __get_user(fpu_save, &sf->fpu_save);
152
153 if (fpu_save)
154 err |= restore_fpu_state(regs, fpu_save);
155
156 /* This is pretty much atomic, no amount locking would prevent
157 * the races which exist anyways.
158 */
159 err |= __get_user(set.sig[0], &sf->info.si_mask);
160 err |= __copy_from_user(&set.sig[1], &sf->extramask,
161 (_NSIG_WORDS-1) * sizeof(unsigned int));
162
163 if (err)
164 goto segv_and_exit;
165
166 sigdelsetmask(&set, ~_BLOCKABLE);
167 spin_lock_irq(&current->sighand->siglock);
168 current->blocked = set;
169 recalc_sigpending();
170 spin_unlock_irq(&current->sighand->siglock);
171 return;
172
173segv_and_exit:
174 force_sig(SIGSEGV, current);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
178{
179 struct rt_signal_frame __user *sf;
180 unsigned int psr, pc, npc;
181 __siginfo_fpu_t __user *fpu_save;
182 mm_segment_t old_fs;
183 sigset_t set;
184 stack_t st;
185 int err;
186
187 synchronize_user_stack();
188 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
189 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
190 (((unsigned long) sf) & 0x03))
191 goto segv;
192
193 err = __get_user(pc, &sf->regs.pc);
194 err |= __get_user(npc, &sf->regs.npc);
195 err |= ((pc | npc) & 0x03);
196
197 err |= __get_user(regs->y, &sf->regs.y);
198 err |= __get_user(psr, &sf->regs.psr);
199
200 err |= __copy_from_user(&regs->u_regs[UREG_G1],
201 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
202
203 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
204
David S. Miller28e61032008-05-11 02:07:19 -0700205 /* Prevent syscall restart. */
206 pt_regs_clear_syscall(regs);
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 err |= __get_user(fpu_save, &sf->fpu_save);
209
210 if (fpu_save)
211 err |= restore_fpu_state(regs, fpu_save);
212 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
213
214 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
215
216 if (err)
217 goto segv;
218
219 regs->pc = pc;
220 regs->npc = npc;
221
222 /* It is more difficult to avoid calling this function than to
223 * call it and ignore errors.
224 */
225 old_fs = get_fs();
226 set_fs(KERNEL_DS);
227 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
228 set_fs(old_fs);
229
230 sigdelsetmask(&set, ~_BLOCKABLE);
231 spin_lock_irq(&current->sighand->siglock);
232 current->blocked = set;
233 recalc_sigpending();
234 spin_unlock_irq(&current->sighand->siglock);
235 return;
236segv:
237 force_sig(SIGSEGV, current);
238}
239
240/* Checks if the fp is valid */
241static inline int invalid_frame_pointer(void __user *fp, int fplen)
242{
243 if ((((unsigned long) fp) & 7) ||
244 !__access_ok((unsigned long)fp, fplen) ||
245 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
246 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
247 return 1;
248
249 return 0;
250}
251
252static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
253{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700254 unsigned long sp = regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700256 /*
257 * If we are on the alternate signal stack and would overflow it, don't.
258 * Return an always-bogus address instead so we will die with SIGSEGV.
259 */
260 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
261 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* This is the X/Open sanctioned signal stack switching. */
264 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700265 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 sp = current->sas_ss_sp + current->sas_ss_size;
267 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700268
269 /* Always align the stack frame. This handles two cases. First,
270 * sigaltstack need not be mindful of platform specific stack
271 * alignment. Second, if we took this signal because the stack
272 * is not aligned properly, we'd like to take the signal cleanly
273 * and report that.
274 */
275 sp &= ~7UL;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return (void __user *)(sp - framesize);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static inline int
281save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
282{
283 int err = 0;
284#ifdef CONFIG_SMP
285 if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
286 put_psr(get_psr() | PSR_EF);
287 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
288 &current->thread.fpqueue[0], &current->thread.fpqdepth);
289 regs->psr &= ~(PSR_EF);
290 clear_tsk_thread_flag(current, TIF_USEDFPU);
291 }
292#else
293 if (current == last_task_used_math) {
294 put_psr(get_psr() | PSR_EF);
295 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
296 &current->thread.fpqueue[0], &current->thread.fpqdepth);
297 last_task_used_math = NULL;
298 regs->psr &= ~(PSR_EF);
299 }
300#endif
301 err |= __copy_to_user(&fpu->si_float_regs[0],
302 &current->thread.float_regs[0],
303 (sizeof(unsigned long) * 32));
304 err |= __put_user(current->thread.fsr, &fpu->si_fsr);
305 err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
306 if (current->thread.fpqdepth != 0)
307 err |= __copy_to_user(&fpu->si_fpqueue[0],
308 &current->thread.fpqueue[0],
309 ((sizeof(unsigned long) +
310 (sizeof(unsigned long *)))*16));
311 clear_used_math();
312 return err;
313}
314
David S. Miller5526b7e2008-04-27 02:26:36 -0700315static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
316 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
David S. Miller5526b7e2008-04-27 02:26:36 -0700318 struct signal_frame __user *sf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int sigframe_size, err;
320
321 /* 1. Make sure everything is clean */
322 synchronize_user_stack();
323
David S. Miller5526b7e2008-04-27 02:26:36 -0700324 sigframe_size = SF_ALIGNEDSZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!used_math())
326 sigframe_size -= sizeof(__siginfo_fpu_t);
327
David S. Miller5526b7e2008-04-27 02:26:36 -0700328 sf = (struct signal_frame __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 get_sigframe(&ka->sa, regs, sigframe_size);
330
331 if (invalid_frame_pointer(sf, sigframe_size))
332 goto sigill_and_return;
333
334 if (current_thread_info()->w_saved != 0)
335 goto sigill_and_return;
336
337 /* 2. Save the current process state */
338 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
339
340 err |= __put_user(0, &sf->extra_size);
341
342 if (used_math()) {
343 err |= save_fpu_state(regs, &sf->fpu_state);
344 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
345 } else {
346 err |= __put_user(0, &sf->fpu_save);
347 }
348
349 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
350 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
351 (_NSIG_WORDS - 1) * sizeof(unsigned int));
352 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
353 sizeof(struct reg_window));
354 if (err)
355 goto sigsegv;
356
357 /* 3. signal handler back-trampoline and parameters */
358 regs->u_regs[UREG_FP] = (unsigned long) sf;
359 regs->u_regs[UREG_I0] = signo;
360 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
361 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
362
363 /* 4. signal handler */
364 regs->pc = (unsigned long) ka->sa.sa_handler;
365 regs->npc = (regs->pc + 4);
366
367 /* 5. return to kernel instructions */
368 if (ka->ka_restorer)
369 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
370 else {
371 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
372
373 /* mov __NR_sigreturn, %g1 */
374 err |= __put_user(0x821020d8, &sf->insns[0]);
375
376 /* t 0x10 */
377 err |= __put_user(0x91d02010, &sf->insns[1]);
378 if (err)
379 goto sigsegv;
380
381 /* Flush instruction space. */
382 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
383 }
384 return;
385
386sigill_and_return:
387 do_exit(SIGILL);
388sigsegv:
389 force_sigsegv(signo, current);
390}
391
David S. Miller5526b7e2008-04-27 02:26:36 -0700392static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
393 int signo, sigset_t *oldset, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct rt_signal_frame __user *sf;
396 int sigframe_size;
397 unsigned int psr;
398 int err;
399
400 synchronize_user_stack();
401 sigframe_size = RT_ALIGNEDSZ;
402 if (!used_math())
403 sigframe_size -= sizeof(__siginfo_fpu_t);
404 sf = (struct rt_signal_frame __user *)
405 get_sigframe(&ka->sa, regs, sigframe_size);
406 if (invalid_frame_pointer(sf, sigframe_size))
407 goto sigill;
408 if (current_thread_info()->w_saved != 0)
409 goto sigill;
410
411 err = __put_user(regs->pc, &sf->regs.pc);
412 err |= __put_user(regs->npc, &sf->regs.npc);
413 err |= __put_user(regs->y, &sf->regs.y);
414 psr = regs->psr;
415 if (used_math())
416 psr |= PSR_EF;
417 err |= __put_user(psr, &sf->regs.psr);
418 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
419 err |= __put_user(0, &sf->extra_size);
420
421 if (psr & PSR_EF) {
422 err |= save_fpu_state(regs, &sf->fpu_state);
423 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
424 } else {
425 err |= __put_user(0, &sf->fpu_save);
426 }
427 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
428
429 /* Setup sigaltstack */
430 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
431 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
432 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
433
434 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
435 sizeof(struct reg_window));
436
437 err |= copy_siginfo_to_user(&sf->info, info);
438
439 if (err)
440 goto sigsegv;
441
442 regs->u_regs[UREG_FP] = (unsigned long) sf;
443 regs->u_regs[UREG_I0] = signo;
444 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
445 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
446
447 regs->pc = (unsigned long) ka->sa.sa_handler;
448 regs->npc = (regs->pc + 4);
449
450 if (ka->ka_restorer)
451 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
452 else {
453 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
454
455 /* mov __NR_sigreturn, %g1 */
456 err |= __put_user(0x821020d8, &sf->insns[0]);
457
458 /* t 0x10 */
459 err |= __put_user(0x91d02010, &sf->insns[1]);
460 if (err)
461 goto sigsegv;
462
463 /* Flush instruction space. */
464 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
465 }
466 return;
467
468sigill:
469 do_exit(SIGILL);
470sigsegv:
471 force_sigsegv(signo, current);
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static inline void
475handle_signal(unsigned long signr, struct k_sigaction *ka,
David S. Millerec98c6b2008-04-20 02:14:23 -0700476 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
David S. Millerec98c6b2008-04-20 02:14:23 -0700478 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller5526b7e2008-04-27 02:26:36 -0700479 setup_rt_frame(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700480 else
David S. Miller5526b7e2008-04-27 02:26:36 -0700481 setup_frame(ka, regs, signr, oldset);
David S. Millerec98c6b2008-04-20 02:14:23 -0700482
Steven Rostedt69be8f12005-08-29 11:44:09 -0400483 spin_lock_irq(&current->sighand->siglock);
484 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
485 if (!(ka->sa.sa_flags & SA_NOMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 sigaddset(&current->blocked, signr);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400487 recalc_sigpending();
488 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
492 struct sigaction *sa)
493{
494 switch(regs->u_regs[UREG_I0]) {
495 case ERESTART_RESTARTBLOCK:
496 case ERESTARTNOHAND:
497 no_system_call_restart:
498 regs->u_regs[UREG_I0] = EINTR;
499 regs->psr |= PSR_C;
500 break;
501 case ERESTARTSYS:
502 if (!(sa->sa_flags & SA_RESTART))
503 goto no_system_call_restart;
504 /* fallthrough */
505 case ERESTARTNOINTR:
506 regs->u_regs[UREG_I0] = orig_i0;
507 regs->pc -= 4;
508 regs->npc -= 4;
509 }
510}
511
512/* Note that 'init' is a special process: it doesn't get signals it doesn't
513 * want to handle. Thus you cannot kill init even with a SIGKILL even by
514 * mistake.
515 */
David S. Miller28e61032008-05-11 02:07:19 -0700516asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700519 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800520 sigset_t *oldset;
David S. Miller28e61032008-05-11 02:07:19 -0700521 siginfo_t info;
522 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Miller28e61032008-05-11 02:07:19 -0700524 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
525 restart_syscall = 1;
526 else
527 restart_syscall = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
David S. Miller2d7d5f02006-01-19 02:42:49 -0800529 if (test_thread_flag(TIF_RESTORE_SIGMASK))
530 oldset = &current->saved_sigmask;
531 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 oldset = &current->blocked;
533
David S. Miller28e61032008-05-11 02:07:19 -0700534 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
535
536 /* If the debugger messes with the program counter, it clears
537 * the software "in syscall" bit, directing us to not perform
538 * a syscall restart.
539 */
540 if (restart_syscall && !pt_regs_is_syscall(regs))
541 restart_syscall = 0;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700544 if (restart_syscall)
545 syscall_restart(orig_i0, regs, &ka.sa);
David S. Millerec98c6b2008-04-20 02:14:23 -0700546 handle_signal(signr, &ka, &info, oldset, regs);
547
David S. Miller2d7d5f02006-01-19 02:42:49 -0800548 /* a signal was successfully delivered; the saved
549 * sigmask will have been stored in the signal frame,
550 * and will be restored by sigreturn, so we can simply
551 * clear the TIF_RESTORE_SIGMASK flag.
552 */
553 if (test_thread_flag(TIF_RESTORE_SIGMASK))
554 clear_thread_flag(TIF_RESTORE_SIGMASK);
555 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
David S. Miller28e61032008-05-11 02:07:19 -0700557 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
559 regs->u_regs[UREG_I0] == ERESTARTSYS ||
560 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
561 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700562 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 regs->pc -= 4;
564 regs->npc -= 4;
565 }
David S. Miller28e61032008-05-11 02:07:19 -0700566 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
568 regs->u_regs[UREG_G1] = __NR_restart_syscall;
569 regs->pc -= 4;
570 regs->npc -= 4;
571 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800572
573 /* if there's no signal to deliver, we just put the saved sigmask
574 * back
575 */
576 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
577 clear_thread_flag(TIF_RESTORE_SIGMASK);
578 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582asmlinkage int
583do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
584 unsigned long sp)
585{
586 int ret = -EFAULT;
587
588 /* First see if old state is wanted. */
589 if (ossptr) {
590 if (put_user(current->sas_ss_sp + current->sas_ss_size,
591 &ossptr->the_stack) ||
592 __put_user(on_sig_stack(sp), &ossptr->cur_status))
593 goto out;
594 }
595
596 /* Now see if we want to update the new state. */
597 if (ssptr) {
598 char *ss_sp;
599
600 if (get_user(ss_sp, &ssptr->the_stack))
601 goto out;
602 /* If the current stack was set with sigaltstack, don't
603 swap stacks while we are on it. */
604 ret = -EPERM;
605 if (current->sas_ss_sp && on_sig_stack(sp))
606 goto out;
607
608 /* Since we don't know the extent of the stack, and we don't
609 track onstack-ness, but rather calculate it, we must
610 presume a size. Ho hum this interface is lossy. */
611 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
612 current->sas_ss_size = SIGSTKSZ;
613 }
614 ret = 0;
615out:
616 return ret;
617}