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)
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>
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);
35 struct sparc_stackf ss;
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;
49 __siginfo_fpu_t __user *fpu_save;
50 unsigned int insns[2];
52 unsigned int extra_size; /* Should be 0 */
53 __siginfo_fpu_t fpu_state;
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)
63 spin_lock_irq(¤t->sighand->siglock);
64 current->saved_sigmask = current->blocked;
65 siginitset(¤t->blocked, set);
67 spin_unlock_irq(¤t->sighand->siglock);
69 current->state = TASK_INTERRUPTIBLE;
71 set_thread_flag(TIF_RESTORE_SIGMASK);
73 return -ERESTARTNOHAND;
76 asmlinkage int sys_sigsuspend(old_sigset_t set)
78 return _sigpause_common(set);
82 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
86 if (test_tsk_thread_flag(current, TIF_USEDFPU))
89 if (current == last_task_used_math) {
90 last_task_used_math = NULL;
95 clear_tsk_thread_flag(current, TIF_USEDFPU);
97 if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
100 err = __copy_from_user(¤t->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(¤t->thread.fpqueue[0],
107 ((sizeof(unsigned long) +
108 (sizeof(unsigned long *)))*16));
112 asmlinkage void do_sigreturn(struct pt_regs *regs)
114 struct signal_frame __user *sf;
115 unsigned long up_psr, pc, npc;
117 __siginfo_fpu_t __user *fpu_save;
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)))
131 if (((unsigned long) sf) & 3)
134 err = __get_user(pc, &sf->info.si_regs.pc);
135 err |= __get_user(npc, &sf->info.si_regs.npc);
140 /* 2. Restore the state */
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 err |= __get_user(fpu_save, &sf->fpu_save);
151 err |= restore_fpu_state(regs, fpu_save);
153 /* This is pretty much atomic, no amount locking would prevent
154 * the races which exist anyways.
156 err |= __get_user(set.sig[0], &sf->info.si_mask);
157 err |= __copy_from_user(&set.sig[1], &sf->extramask,
158 (_NSIG_WORDS-1) * sizeof(unsigned int));
163 sigdelsetmask(&set, ~_BLOCKABLE);
164 spin_lock_irq(¤t->sighand->siglock);
165 current->blocked = set;
167 spin_unlock_irq(¤t->sighand->siglock);
171 force_sig(SIGSEGV, current);
174 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
176 struct rt_signal_frame __user *sf;
177 unsigned int psr, pc, npc;
178 __siginfo_fpu_t __user *fpu_save;
184 synchronize_user_stack();
185 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
186 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
187 (((unsigned long) sf) & 0x03))
190 err = __get_user(pc, &sf->regs.pc);
191 err |= __get_user(npc, &sf->regs.npc);
192 err |= ((pc | npc) & 0x03);
194 err |= __get_user(regs->y, &sf->regs.y);
195 err |= __get_user(psr, &sf->regs.psr);
197 err |= __copy_from_user(®s->u_regs[UREG_G1],
198 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
200 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
202 err |= __get_user(fpu_save, &sf->fpu_save);
205 err |= restore_fpu_state(regs, fpu_save);
206 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
208 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
216 /* It is more difficult to avoid calling this function than to
217 * call it and ignore errors.
221 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
224 sigdelsetmask(&set, ~_BLOCKABLE);
225 spin_lock_irq(¤t->sighand->siglock);
226 current->blocked = set;
228 spin_unlock_irq(¤t->sighand->siglock);
231 force_sig(SIGSEGV, current);
234 /* Checks if the fp is valid */
235 static inline int invalid_frame_pointer(void __user *fp, int fplen)
237 if ((((unsigned long) fp) & 7) ||
238 !__access_ok((unsigned long)fp, fplen) ||
239 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
240 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
246 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
248 unsigned long sp = regs->u_regs[UREG_FP];
251 * If we are on the alternate signal stack and would overflow it, don't.
252 * Return an always-bogus address instead so we will die with SIGSEGV.
254 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
255 return (void __user *) -1L;
257 /* This is the X/Open sanctioned signal stack switching. */
258 if (sa->sa_flags & SA_ONSTACK) {
259 if (sas_ss_flags(sp) == 0)
260 sp = current->sas_ss_sp + current->sas_ss_size;
263 /* Always align the stack frame. This handles two cases. First,
264 * sigaltstack need not be mindful of platform specific stack
265 * alignment. Second, if we took this signal because the stack
266 * is not aligned properly, we'd like to take the signal cleanly
271 return (void __user *)(sp - framesize);
275 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
279 if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
280 put_psr(get_psr() | PSR_EF);
281 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
282 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
283 regs->psr &= ~(PSR_EF);
284 clear_tsk_thread_flag(current, TIF_USEDFPU);
287 if (current == last_task_used_math) {
288 put_psr(get_psr() | PSR_EF);
289 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
290 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
291 last_task_used_math = NULL;
292 regs->psr &= ~(PSR_EF);
295 err |= __copy_to_user(&fpu->si_float_regs[0],
296 ¤t->thread.float_regs[0],
297 (sizeof(unsigned long) * 32));
298 err |= __put_user(current->thread.fsr, &fpu->si_fsr);
299 err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
300 if (current->thread.fpqdepth != 0)
301 err |= __copy_to_user(&fpu->si_fpqueue[0],
302 ¤t->thread.fpqueue[0],
303 ((sizeof(unsigned long) +
304 (sizeof(unsigned long *)))*16));
309 static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
310 int signo, sigset_t *oldset)
312 struct signal_frame __user *sf;
313 int sigframe_size, err;
315 /* 1. Make sure everything is clean */
316 synchronize_user_stack();
318 sigframe_size = SF_ALIGNEDSZ;
320 sigframe_size -= sizeof(__siginfo_fpu_t);
322 sf = (struct signal_frame __user *)
323 get_sigframe(&ka->sa, regs, sigframe_size);
325 if (invalid_frame_pointer(sf, sigframe_size))
326 goto sigill_and_return;
328 if (current_thread_info()->w_saved != 0)
329 goto sigill_and_return;
331 /* 2. Save the current process state */
332 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
334 err |= __put_user(0, &sf->extra_size);
337 err |= save_fpu_state(regs, &sf->fpu_state);
338 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
340 err |= __put_user(0, &sf->fpu_save);
343 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
344 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
345 (_NSIG_WORDS - 1) * sizeof(unsigned int));
346 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
347 sizeof(struct reg_window));
351 /* 3. signal handler back-trampoline and parameters */
352 regs->u_regs[UREG_FP] = (unsigned long) sf;
353 regs->u_regs[UREG_I0] = signo;
354 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
355 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
357 /* 4. signal handler */
358 regs->pc = (unsigned long) ka->sa.sa_handler;
359 regs->npc = (regs->pc + 4);
361 /* 5. return to kernel instructions */
363 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
365 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
367 /* mov __NR_sigreturn, %g1 */
368 err |= __put_user(0x821020d8, &sf->insns[0]);
371 err |= __put_user(0x91d02010, &sf->insns[1]);
375 /* Flush instruction space. */
376 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
383 force_sigsegv(signo, current);
386 static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
387 int signo, sigset_t *oldset, siginfo_t *info)
389 struct rt_signal_frame __user *sf;
394 synchronize_user_stack();
395 sigframe_size = RT_ALIGNEDSZ;
397 sigframe_size -= sizeof(__siginfo_fpu_t);
398 sf = (struct rt_signal_frame __user *)
399 get_sigframe(&ka->sa, regs, sigframe_size);
400 if (invalid_frame_pointer(sf, sigframe_size))
402 if (current_thread_info()->w_saved != 0)
405 err = __put_user(regs->pc, &sf->regs.pc);
406 err |= __put_user(regs->npc, &sf->regs.npc);
407 err |= __put_user(regs->y, &sf->regs.y);
411 err |= __put_user(psr, &sf->regs.psr);
412 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
413 err |= __put_user(0, &sf->extra_size);
416 err |= save_fpu_state(regs, &sf->fpu_state);
417 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
419 err |= __put_user(0, &sf->fpu_save);
421 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
423 /* Setup sigaltstack */
424 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
425 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
426 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
428 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
429 sizeof(struct reg_window));
431 err |= copy_siginfo_to_user(&sf->info, info);
436 regs->u_regs[UREG_FP] = (unsigned long) sf;
437 regs->u_regs[UREG_I0] = signo;
438 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
439 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
441 regs->pc = (unsigned long) ka->sa.sa_handler;
442 regs->npc = (regs->pc + 4);
445 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
447 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
449 /* mov __NR_sigreturn, %g1 */
450 err |= __put_user(0x821020d8, &sf->insns[0]);
453 err |= __put_user(0x91d02010, &sf->insns[1]);
457 /* Flush instruction space. */
458 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
465 force_sigsegv(signo, current);
469 handle_signal(unsigned long signr, struct k_sigaction *ka,
470 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
472 if (ka->sa.sa_flags & SA_SIGINFO)
473 setup_rt_frame(ka, regs, signr, oldset, info);
475 setup_frame(ka, regs, signr, oldset);
477 spin_lock_irq(¤t->sighand->siglock);
478 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
479 if (!(ka->sa.sa_flags & SA_NOMASK))
480 sigaddset(¤t->blocked, signr);
482 spin_unlock_irq(¤t->sighand->siglock);
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:
491 no_system_call_restart:
492 regs->u_regs[UREG_I0] = EINTR;
496 if (!(sa->sa_flags & SA_RESTART))
497 goto no_system_call_restart;
500 regs->u_regs[UREG_I0] = orig_i0;
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
510 asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0, int restart_syscall)
513 struct sparc_deliver_cookie cookie;
514 struct k_sigaction ka;
518 cookie.restart_syscall = restart_syscall;
519 cookie.orig_i0 = orig_i0;
521 if (test_thread_flag(TIF_RESTORE_SIGMASK))
522 oldset = ¤t->saved_sigmask;
524 oldset = ¤t->blocked;
526 signr = get_signal_to_deliver(&info, &ka, regs, &cookie);
528 if (cookie.restart_syscall)
529 syscall_restart(cookie.orig_i0, regs, &ka.sa);
530 handle_signal(signr, &ka, &info, oldset, regs);
532 /* a signal was successfully delivered; the saved
533 * sigmask will have been stored in the signal frame,
534 * and will be restored by sigreturn, so we can simply
535 * clear the TIF_RESTORE_SIGMASK flag.
537 if (test_thread_flag(TIF_RESTORE_SIGMASK))
538 clear_thread_flag(TIF_RESTORE_SIGMASK);
541 if (cookie.restart_syscall &&
542 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
543 regs->u_regs[UREG_I0] == ERESTARTSYS ||
544 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
545 /* replay the system call when we are done */
546 regs->u_regs[UREG_I0] = cookie.orig_i0;
550 if (cookie.restart_syscall &&
551 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
552 regs->u_regs[UREG_G1] = __NR_restart_syscall;
557 /* if there's no signal to deliver, we just put the saved sigmask
560 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
561 clear_thread_flag(TIF_RESTORE_SIGMASK);
562 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
567 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
572 /* First see if old state is wanted. */
574 if (put_user(current->sas_ss_sp + current->sas_ss_size,
575 &ossptr->the_stack) ||
576 __put_user(on_sig_stack(sp), &ossptr->cur_status))
580 /* Now see if we want to update the new state. */
584 if (get_user(ss_sp, &ssptr->the_stack))
586 /* If the current stack was set with sigaltstack, don't
587 swap stacks while we are on it. */
589 if (current->sas_ss_sp && on_sig_stack(sp))
592 /* Since we don't know the extent of the stack, and we don't
593 track onstack-ness, but rather calculate it, we must
594 presume a size. Ho hum this interface is lossy. */
595 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
596 current->sas_ss_size = SIGSTKSZ;
603 void ptrace_signal_deliver(struct pt_regs *regs, void *cookie)
605 struct sparc_deliver_cookie *cp = cookie;
607 if (cp->restart_syscall &&
608 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
609 regs->u_regs[UREG_I0] == ERESTARTSYS ||
610 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
611 /* replay the system call when we are done */
612 regs->u_regs[UREG_I0] = cp->orig_i0;
615 cp->restart_syscall = 0;
618 if (cp->restart_syscall &&
619 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
620 regs->u_regs[UREG_G1] = __NR_restart_syscall;
623 cp->restart_syscall = 0;