Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / sparc / kernel / signal_32.c
blobf1f8a03664bc1ff8661498c3bf08d4b11b4b7bd5
1 /* linux/arch/sparc/kernel/signal.c
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 */
9 #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>
19 #include <linux/binfmts.h> /* do_coredum */
20 #include <linux/bitops.h>
21 #include <linux/tracehook.h>
23 #include <asm/uaccess.h>
24 #include <asm/ptrace.h>
25 #include <asm/pgalloc.h>
26 #include <asm/pgtable.h>
27 #include <asm/cacheflush.h> /* flush_sig_insns */
29 #include "sigutil.h"
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
34 void *fpqueue, unsigned long *fpqdepth);
35 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
37 struct signal_frame {
38 struct sparc_stackf ss;
39 __siginfo32_t info;
40 __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 */
44 __siginfo_rwin_t __user *rwin_save;
45 } __attribute__((aligned(8)));
47 struct 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 */
56 __siginfo_rwin_t __user *rwin_save;
57 } __attribute__((aligned(8)));
59 /* Align macros */
60 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
61 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
63 static int _sigpause_common(old_sigset_t set)
65 sigset_t blocked;
67 current->saved_sigmask = current->blocked;
69 set &= _BLOCKABLE;
70 siginitset(&blocked, set);
71 set_current_blocked(&blocked);
73 current->state = TASK_INTERRUPTIBLE;
74 schedule();
75 set_thread_flag(TIF_RESTORE_SIGMASK);
77 return -ERESTARTNOHAND;
80 asmlinkage int sys_sigsuspend(old_sigset_t set)
82 return _sigpause_common(set);
85 asmlinkage void do_sigreturn(struct pt_regs *regs)
87 struct signal_frame __user *sf;
88 unsigned long up_psr, pc, npc;
89 sigset_t set;
90 __siginfo_fpu_t __user *fpu_save;
91 __siginfo_rwin_t __user *rwin_save;
92 int err;
94 /* Always make any pending restarted system calls return -EINTR */
95 current_thread_info()->restart_block.fn = do_no_restart_syscall;
97 synchronize_user_stack();
99 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
101 /* 1. Make sure we are not getting garbage from the user */
102 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
103 goto segv_and_exit;
105 if (((unsigned long) sf) & 3)
106 goto segv_and_exit;
108 err = __get_user(pc, &sf->info.si_regs.pc);
109 err |= __get_user(npc, &sf->info.si_regs.npc);
111 if ((pc | npc) & 3)
112 goto segv_and_exit;
114 /* 2. Restore the state */
115 up_psr = regs->psr;
116 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
118 /* User can only change condition codes and FPU enabling in %psr. */
119 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
120 | (regs->psr & (PSR_ICC | PSR_EF));
122 /* Prevent syscall restart. */
123 pt_regs_clear_syscall(regs);
125 err |= __get_user(fpu_save, &sf->fpu_save);
126 if (fpu_save)
127 err |= restore_fpu_state(regs, fpu_save);
128 err |= __get_user(rwin_save, &sf->rwin_save);
129 if (rwin_save)
130 err |= restore_rwin_state(rwin_save);
132 /* This is pretty much atomic, no amount locking would prevent
133 * the races which exist anyways.
135 err |= __get_user(set.sig[0], &sf->info.si_mask);
136 err |= __copy_from_user(&set.sig[1], &sf->extramask,
137 (_NSIG_WORDS-1) * sizeof(unsigned int));
139 if (err)
140 goto segv_and_exit;
142 sigdelsetmask(&set, ~_BLOCKABLE);
143 set_current_blocked(&set);
144 return;
146 segv_and_exit:
147 force_sig(SIGSEGV, current);
150 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
152 struct rt_signal_frame __user *sf;
153 unsigned int psr, pc, npc;
154 __siginfo_fpu_t __user *fpu_save;
155 __siginfo_rwin_t __user *rwin_save;
156 mm_segment_t old_fs;
157 sigset_t set;
158 stack_t st;
159 int err;
161 synchronize_user_stack();
162 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
163 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
164 (((unsigned long) sf) & 0x03))
165 goto segv;
167 err = __get_user(pc, &sf->regs.pc);
168 err |= __get_user(npc, &sf->regs.npc);
169 err |= ((pc | npc) & 0x03);
171 err |= __get_user(regs->y, &sf->regs.y);
172 err |= __get_user(psr, &sf->regs.psr);
174 err |= __copy_from_user(&regs->u_regs[UREG_G1],
175 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
177 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
179 /* Prevent syscall restart. */
180 pt_regs_clear_syscall(regs);
182 err |= __get_user(fpu_save, &sf->fpu_save);
183 if (!err && fpu_save)
184 err |= restore_fpu_state(regs, fpu_save);
185 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
187 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
189 if (err)
190 goto segv;
192 regs->pc = pc;
193 regs->npc = npc;
195 /* It is more difficult to avoid calling this function than to
196 * call it and ignore errors.
198 old_fs = get_fs();
199 set_fs(KERNEL_DS);
200 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
201 set_fs(old_fs);
203 err |= __get_user(rwin_save, &sf->rwin_save);
204 if (!err && rwin_save) {
205 if (restore_rwin_state(rwin_save))
206 goto segv;
209 sigdelsetmask(&set, ~_BLOCKABLE);
210 set_current_blocked(&set);
211 return;
212 segv:
213 force_sig(SIGSEGV, current);
216 /* Checks if the fp is valid */
217 static inline int invalid_frame_pointer(void __user *fp, int fplen)
219 if ((((unsigned long) fp) & 7) ||
220 !__access_ok((unsigned long)fp, fplen) ||
221 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
222 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
223 return 1;
225 return 0;
228 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
230 unsigned long sp = regs->u_regs[UREG_FP];
233 * If we are on the alternate signal stack and would overflow it, don't.
234 * Return an always-bogus address instead so we will die with SIGSEGV.
236 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
237 return (void __user *) -1L;
239 /* This is the X/Open sanctioned signal stack switching. */
240 if (sa->sa_flags & SA_ONSTACK) {
241 if (sas_ss_flags(sp) == 0)
242 sp = current->sas_ss_sp + current->sas_ss_size;
245 sp -= framesize;
247 /* Always align the stack frame. This handles two cases. First,
248 * sigaltstack need not be mindful of platform specific stack
249 * alignment. Second, if we took this signal because the stack
250 * is not aligned properly, we'd like to take the signal cleanly
251 * and report that.
253 sp &= ~15UL;
255 return (void __user *) sp;
258 static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
259 int signo, sigset_t *oldset)
261 struct signal_frame __user *sf;
262 int sigframe_size, err, wsaved;
263 void __user *tail;
265 /* 1. Make sure everything is clean */
266 synchronize_user_stack();
268 wsaved = current_thread_info()->w_saved;
270 sigframe_size = sizeof(*sf);
271 if (used_math())
272 sigframe_size += sizeof(__siginfo_fpu_t);
273 if (wsaved)
274 sigframe_size += sizeof(__siginfo_rwin_t);
276 sf = (struct signal_frame __user *)
277 get_sigframe(&ka->sa, regs, sigframe_size);
279 if (invalid_frame_pointer(sf, sigframe_size))
280 goto sigill_and_return;
282 tail = sf + 1;
284 /* 2. Save the current process state */
285 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
287 err |= __put_user(0, &sf->extra_size);
289 if (used_math()) {
290 __siginfo_fpu_t __user *fp = tail;
291 tail += sizeof(*fp);
292 err |= save_fpu_state(regs, fp);
293 err |= __put_user(fp, &sf->fpu_save);
294 } else {
295 err |= __put_user(0, &sf->fpu_save);
297 if (wsaved) {
298 __siginfo_rwin_t __user *rwp = tail;
299 tail += sizeof(*rwp);
300 err |= save_rwin_state(wsaved, rwp);
301 err |= __put_user(rwp, &sf->rwin_save);
302 } else {
303 err |= __put_user(0, &sf->rwin_save);
306 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
307 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
308 (_NSIG_WORDS - 1) * sizeof(unsigned int));
309 if (!wsaved) {
310 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
311 sizeof(struct reg_window32));
312 } else {
313 struct reg_window32 *rp;
315 rp = &current_thread_info()->reg_window[wsaved - 1];
316 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
318 if (err)
319 goto sigsegv;
321 /* 3. signal handler back-trampoline and parameters */
322 regs->u_regs[UREG_FP] = (unsigned long) sf;
323 regs->u_regs[UREG_I0] = signo;
324 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
325 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
327 /* 4. signal handler */
328 regs->pc = (unsigned long) ka->sa.sa_handler;
329 regs->npc = (regs->pc + 4);
331 /* 5. return to kernel instructions */
332 if (ka->ka_restorer)
333 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
334 else {
335 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
337 /* mov __NR_sigreturn, %g1 */
338 err |= __put_user(0x821020d8, &sf->insns[0]);
340 /* t 0x10 */
341 err |= __put_user(0x91d02010, &sf->insns[1]);
342 if (err)
343 goto sigsegv;
345 /* Flush instruction space. */
346 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
348 return 0;
350 sigill_and_return:
351 do_exit(SIGILL);
352 return -EINVAL;
354 sigsegv:
355 force_sigsegv(signo, current);
356 return -EFAULT;
359 static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
360 int signo, sigset_t *oldset, siginfo_t *info)
362 struct rt_signal_frame __user *sf;
363 int sigframe_size, wsaved;
364 void __user *tail;
365 unsigned int psr;
366 int err;
368 synchronize_user_stack();
369 wsaved = current_thread_info()->w_saved;
370 sigframe_size = sizeof(*sf);
371 if (used_math())
372 sigframe_size += sizeof(__siginfo_fpu_t);
373 if (wsaved)
374 sigframe_size += sizeof(__siginfo_rwin_t);
375 sf = (struct rt_signal_frame __user *)
376 get_sigframe(&ka->sa, regs, sigframe_size);
377 if (invalid_frame_pointer(sf, sigframe_size))
378 goto sigill;
380 tail = sf + 1;
381 err = __put_user(regs->pc, &sf->regs.pc);
382 err |= __put_user(regs->npc, &sf->regs.npc);
383 err |= __put_user(regs->y, &sf->regs.y);
384 psr = regs->psr;
385 if (used_math())
386 psr |= PSR_EF;
387 err |= __put_user(psr, &sf->regs.psr);
388 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
389 err |= __put_user(0, &sf->extra_size);
391 if (psr & PSR_EF) {
392 __siginfo_fpu_t *fp = tail;
393 tail += sizeof(*fp);
394 err |= save_fpu_state(regs, fp);
395 err |= __put_user(fp, &sf->fpu_save);
396 } else {
397 err |= __put_user(0, &sf->fpu_save);
399 if (wsaved) {
400 __siginfo_rwin_t *rwp = tail;
401 tail += sizeof(*rwp);
402 err |= save_rwin_state(wsaved, rwp);
403 err |= __put_user(rwp, &sf->rwin_save);
404 } else {
405 err |= __put_user(0, &sf->rwin_save);
407 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
409 /* Setup sigaltstack */
410 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
411 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
412 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
414 if (!wsaved) {
415 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
416 sizeof(struct reg_window32));
417 } else {
418 struct reg_window32 *rp;
420 rp = &current_thread_info()->reg_window[wsaved - 1];
421 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
424 err |= copy_siginfo_to_user(&sf->info, info);
426 if (err)
427 goto sigsegv;
429 regs->u_regs[UREG_FP] = (unsigned long) sf;
430 regs->u_regs[UREG_I0] = signo;
431 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
432 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
434 regs->pc = (unsigned long) ka->sa.sa_handler;
435 regs->npc = (regs->pc + 4);
437 if (ka->ka_restorer)
438 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
439 else {
440 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
442 /* mov __NR_sigreturn, %g1 */
443 err |= __put_user(0x821020d8, &sf->insns[0]);
445 /* t 0x10 */
446 err |= __put_user(0x91d02010, &sf->insns[1]);
447 if (err)
448 goto sigsegv;
450 /* Flush instruction space. */
451 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
453 return 0;
455 sigill:
456 do_exit(SIGILL);
457 return -EINVAL;
459 sigsegv:
460 force_sigsegv(signo, current);
461 return -EFAULT;
464 static inline int
465 handle_signal(unsigned long signr, struct k_sigaction *ka,
466 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
468 int err;
470 if (ka->sa.sa_flags & SA_SIGINFO)
471 err = setup_rt_frame(ka, regs, signr, oldset, info);
472 else
473 err = setup_frame(ka, regs, signr, oldset);
475 if (err)
476 return err;
478 block_sigmask(ka, signr);
480 tracehook_signal_handler(signr, info, ka, regs, 0);
482 return 0;
485 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
486 struct sigaction *sa)
488 switch(regs->u_regs[UREG_I0]) {
489 case ERESTART_RESTARTBLOCK:
490 case ERESTARTNOHAND:
491 no_system_call_restart:
492 regs->u_regs[UREG_I0] = EINTR;
493 regs->psr |= PSR_C;
494 break;
495 case ERESTARTSYS:
496 if (!(sa->sa_flags & SA_RESTART))
497 goto no_system_call_restart;
498 /* fallthrough */
499 case ERESTARTNOINTR:
500 regs->u_regs[UREG_I0] = orig_i0;
501 regs->pc -= 4;
502 regs->npc -= 4;
506 /* Note that 'init' is a special process: it doesn't get signals it doesn't
507 * want to handle. Thus you cannot kill init even with a SIGKILL even by
508 * mistake.
510 static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
512 struct k_sigaction ka;
513 int restart_syscall;
514 sigset_t *oldset;
515 siginfo_t info;
516 int signr;
518 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
519 restart_syscall = 1;
520 else
521 restart_syscall = 0;
523 if (test_thread_flag(TIF_RESTORE_SIGMASK))
524 oldset = &current->saved_sigmask;
525 else
526 oldset = &current->blocked;
528 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
530 /* If the debugger messes with the program counter, it clears
531 * the software "in syscall" bit, directing us to not perform
532 * a syscall restart.
534 if (restart_syscall && !pt_regs_is_syscall(regs))
535 restart_syscall = 0;
537 if (signr > 0) {
538 if (restart_syscall)
539 syscall_restart(orig_i0, regs, &ka.sa);
540 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
541 /* a signal was successfully delivered; the saved
542 * sigmask will have been stored in the signal frame,
543 * and will be restored by sigreturn, so we can simply
544 * clear the TIF_RESTORE_SIGMASK flag.
546 if (test_thread_flag(TIF_RESTORE_SIGMASK))
547 clear_thread_flag(TIF_RESTORE_SIGMASK);
549 return;
551 if (restart_syscall &&
552 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
553 regs->u_regs[UREG_I0] == ERESTARTSYS ||
554 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
555 /* replay the system call when we are done */
556 regs->u_regs[UREG_I0] = orig_i0;
557 regs->pc -= 4;
558 regs->npc -= 4;
559 pt_regs_clear_syscall(regs);
561 if (restart_syscall &&
562 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
563 regs->u_regs[UREG_G1] = __NR_restart_syscall;
564 regs->pc -= 4;
565 regs->npc -= 4;
566 pt_regs_clear_syscall(regs);
569 /* if there's no signal to deliver, we just put the saved sigmask
570 * back
572 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
573 clear_thread_flag(TIF_RESTORE_SIGMASK);
574 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
578 void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
579 unsigned long thread_info_flags)
581 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
582 do_signal(regs, orig_i0);
583 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
584 clear_thread_flag(TIF_NOTIFY_RESUME);
585 tracehook_notify_resume(regs);
586 if (current->replacement_session_keyring)
587 key_replace_session_keyring();
591 asmlinkage int
592 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
593 unsigned long sp)
595 int ret = -EFAULT;
597 /* First see if old state is wanted. */
598 if (ossptr) {
599 if (put_user(current->sas_ss_sp + current->sas_ss_size,
600 &ossptr->the_stack) ||
601 __put_user(on_sig_stack(sp), &ossptr->cur_status))
602 goto out;
605 /* Now see if we want to update the new state. */
606 if (ssptr) {
607 char *ss_sp;
609 if (get_user(ss_sp, &ssptr->the_stack))
610 goto out;
611 /* If the current stack was set with sigaltstack, don't
612 swap stacks while we are on it. */
613 ret = -EPERM;
614 if (current->sas_ss_sp && on_sig_stack(sp))
615 goto out;
617 /* Since we don't know the extent of the stack, and we don't
618 track onstack-ness, but rather calculate it, we must
619 presume a size. Ho hum this interface is lossy. */
620 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
621 current->sas_ss_size = SIGSTKSZ;
623 ret = 0;
624 out:
625 return ret;