Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / sparc / kernel / signal.c
blob3fd1df9f9ba7e849609f5018d443378a301fb2e4
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>
22 #include <asm/uaccess.h>
23 #include <asm/ptrace.h>
24 #include <asm/pgalloc.h>
25 #include <asm/pgtable.h>
26 #include <asm/cacheflush.h> /* flush_sig_insns */
28 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
30 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
31 void *fpqueue, unsigned long *fpqdepth);
32 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
34 struct signal_frame {
35 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;
44 struct 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;
56 /* Align macros */
57 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
58 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
60 static int _sigpause_common(old_sigset_t set)
62 set &= _BLOCKABLE;
63 spin_lock_irq(&current->sighand->siglock);
64 current->saved_sigmask = current->blocked;
65 siginitset(&current->blocked, set);
66 recalc_sigpending();
67 spin_unlock_irq(&current->sighand->siglock);
69 current->state = TASK_INTERRUPTIBLE;
70 schedule();
71 set_thread_flag(TIF_RESTORE_SIGMASK);
73 return -ERESTARTNOHAND;
76 asmlinkage int sys_sigsuspend(old_sigset_t set)
78 return _sigpause_common(set);
81 static inline int
82 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
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;
93 #endif
94 set_used_math();
95 clear_tsk_thread_flag(current, TIF_USEDFPU);
97 if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
98 return -EFAULT;
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;
112 asmlinkage void do_sigreturn(struct pt_regs *regs)
114 struct signal_frame __user *sf;
115 unsigned long up_psr, pc, npc;
116 sigset_t set;
117 __siginfo_fpu_t __user *fpu_save;
118 int err;
120 /* Always make any pending restarted system calls return -EINTR */
121 current_thread_info()->restart_block.fn = do_no_restart_syscall;
123 synchronize_user_stack();
125 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
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;
131 if (((unsigned long) sf) & 3)
132 goto segv_and_exit;
134 err = __get_user(pc, &sf->info.si_regs.pc);
135 err |= __get_user(npc, &sf->info.si_regs.npc);
137 if ((pc | npc) & 3)
138 goto segv_and_exit;
140 /* 2. Restore the state */
141 up_psr = regs->psr;
142 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
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));
148 /* Prevent syscall restart. */
149 pt_regs_clear_syscall(regs);
151 err |= __get_user(fpu_save, &sf->fpu_save);
153 if (fpu_save)
154 err |= restore_fpu_state(regs, fpu_save);
156 /* This is pretty much atomic, no amount locking would prevent
157 * the races which exist anyways.
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));
163 if (err)
164 goto segv_and_exit;
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;
173 segv_and_exit:
174 force_sig(SIGSEGV, current);
177 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
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;
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;
193 err = __get_user(pc, &sf->regs.pc);
194 err |= __get_user(npc, &sf->regs.npc);
195 err |= ((pc | npc) & 0x03);
197 err |= __get_user(regs->y, &sf->regs.y);
198 err |= __get_user(psr, &sf->regs.psr);
200 err |= __copy_from_user(&regs->u_regs[UREG_G1],
201 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
203 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
205 /* Prevent syscall restart. */
206 pt_regs_clear_syscall(regs);
208 err |= __get_user(fpu_save, &sf->fpu_save);
210 if (fpu_save)
211 err |= restore_fpu_state(regs, fpu_save);
212 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
214 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
216 if (err)
217 goto segv;
219 regs->pc = pc;
220 regs->npc = npc;
222 /* It is more difficult to avoid calling this function than to
223 * call it and ignore errors.
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);
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;
236 segv:
237 force_sig(SIGSEGV, current);
240 /* Checks if the fp is valid */
241 static inline int invalid_frame_pointer(void __user *fp, int fplen)
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;
249 return 0;
252 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
254 unsigned long sp = regs->u_regs[UREG_FP];
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.
260 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
261 return (void __user *) -1L;
263 /* This is the X/Open sanctioned signal stack switching. */
264 if (sa->sa_flags & SA_ONSTACK) {
265 if (sas_ss_flags(sp) == 0)
266 sp = current->sas_ss_sp + current->sas_ss_size;
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.
275 sp &= ~7UL;
277 return (void __user *)(sp - framesize);
280 static inline int
281 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
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);
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);
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;
315 static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
316 int signo, sigset_t *oldset)
318 struct signal_frame __user *sf;
319 int sigframe_size, err;
321 /* 1. Make sure everything is clean */
322 synchronize_user_stack();
324 sigframe_size = SF_ALIGNEDSZ;
325 if (!used_math())
326 sigframe_size -= sizeof(__siginfo_fpu_t);
328 sf = (struct signal_frame __user *)
329 get_sigframe(&ka->sa, regs, sigframe_size);
331 if (invalid_frame_pointer(sf, sigframe_size))
332 goto sigill_and_return;
334 if (current_thread_info()->w_saved != 0)
335 goto sigill_and_return;
337 /* 2. Save the current process state */
338 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
340 err |= __put_user(0, &sf->extra_size);
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);
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;
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;
363 /* 4. signal handler */
364 regs->pc = (unsigned long) ka->sa.sa_handler;
365 regs->npc = (regs->pc + 4);
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);
373 /* mov __NR_sigreturn, %g1 */
374 err |= __put_user(0x821020d8, &sf->insns[0]);
376 /* t 0x10 */
377 err |= __put_user(0x91d02010, &sf->insns[1]);
378 if (err)
379 goto sigsegv;
381 /* Flush instruction space. */
382 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
384 return;
386 sigill_and_return:
387 do_exit(SIGILL);
388 sigsegv:
389 force_sigsegv(signo, current);
392 static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
393 int signo, sigset_t *oldset, siginfo_t *info)
395 struct rt_signal_frame __user *sf;
396 int sigframe_size;
397 unsigned int psr;
398 int err;
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;
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);
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);
427 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
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);
434 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
435 sizeof(struct reg_window));
437 err |= copy_siginfo_to_user(&sf->info, info);
439 if (err)
440 goto sigsegv;
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;
447 regs->pc = (unsigned long) ka->sa.sa_handler;
448 regs->npc = (regs->pc + 4);
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);
455 /* mov __NR_sigreturn, %g1 */
456 err |= __put_user(0x821020d8, &sf->insns[0]);
458 /* t 0x10 */
459 err |= __put_user(0x91d02010, &sf->insns[1]);
460 if (err)
461 goto sigsegv;
463 /* Flush instruction space. */
464 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
466 return;
468 sigill:
469 do_exit(SIGILL);
470 sigsegv:
471 force_sigsegv(signo, current);
474 static inline void
475 handle_signal(unsigned long signr, struct k_sigaction *ka,
476 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
478 if (ka->sa.sa_flags & SA_SIGINFO)
479 setup_rt_frame(ka, regs, signr, oldset, info);
480 else
481 setup_frame(ka, regs, signr, oldset);
483 spin_lock_irq(&current->sighand->siglock);
484 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
485 if (!(ka->sa.sa_flags & SA_NOMASK))
486 sigaddset(&current->blocked, signr);
487 recalc_sigpending();
488 spin_unlock_irq(&current->sighand->siglock);
491 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
492 struct sigaction *sa)
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;
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.
516 asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0)
518 struct k_sigaction ka;
519 int restart_syscall;
520 sigset_t *oldset;
521 siginfo_t info;
522 int signr;
524 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
525 restart_syscall = 1;
526 else
527 restart_syscall = 0;
529 if (test_thread_flag(TIF_RESTORE_SIGMASK))
530 oldset = &current->saved_sigmask;
531 else
532 oldset = &current->blocked;
534 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
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.
540 if (restart_syscall && !pt_regs_is_syscall(regs))
541 restart_syscall = 0;
543 if (signr > 0) {
544 if (restart_syscall)
545 syscall_restart(orig_i0, regs, &ka.sa);
546 handle_signal(signr, &ka, &info, oldset, regs);
548 /* 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.
553 if (test_thread_flag(TIF_RESTORE_SIGMASK))
554 clear_thread_flag(TIF_RESTORE_SIGMASK);
555 return;
557 if (restart_syscall &&
558 (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 */
562 regs->u_regs[UREG_I0] = orig_i0;
563 regs->pc -= 4;
564 regs->npc -= 4;
566 if (restart_syscall &&
567 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
568 regs->u_regs[UREG_G1] = __NR_restart_syscall;
569 regs->pc -= 4;
570 regs->npc -= 4;
573 /* if there's no signal to deliver, we just put the saved sigmask
574 * back
576 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
577 clear_thread_flag(TIF_RESTORE_SIGMASK);
578 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
582 asmlinkage int
583 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
584 unsigned long sp)
586 int ret = -EFAULT;
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;
596 /* Now see if we want to update the new state. */
597 if (ssptr) {
598 char *ss_sp;
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;
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;
614 ret = 0;
615 out:
616 return ret;