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)
250 sp = regs->u_regs[UREG_FP];
252 /* This is the X/Open sanctioned signal stack switching. */
253 if (sa->sa_flags & SA_ONSTACK) {
254 if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
255 sp = current->sas_ss_sp + current->sas_ss_size;
257 return (void __user *)(sp - framesize);
261 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
265 if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
266 put_psr(get_psr() | PSR_EF);
267 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
268 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
269 regs->psr &= ~(PSR_EF);
270 clear_tsk_thread_flag(current, TIF_USEDFPU);
273 if (current == last_task_used_math) {
274 put_psr(get_psr() | PSR_EF);
275 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
276 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
277 last_task_used_math = NULL;
278 regs->psr &= ~(PSR_EF);
281 err |= __copy_to_user(&fpu->si_float_regs[0],
282 ¤t->thread.float_regs[0],
283 (sizeof(unsigned long) * 32));
284 err |= __put_user(current->thread.fsr, &fpu->si_fsr);
285 err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
286 if (current->thread.fpqdepth != 0)
287 err |= __copy_to_user(&fpu->si_fpqueue[0],
288 ¤t->thread.fpqueue[0],
289 ((sizeof(unsigned long) +
290 (sizeof(unsigned long *)))*16));
295 static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
296 int signo, sigset_t *oldset)
298 struct signal_frame __user *sf;
299 int sigframe_size, err;
301 /* 1. Make sure everything is clean */
302 synchronize_user_stack();
304 sigframe_size = SF_ALIGNEDSZ;
306 sigframe_size -= sizeof(__siginfo_fpu_t);
308 sf = (struct signal_frame __user *)
309 get_sigframe(&ka->sa, regs, sigframe_size);
311 if (invalid_frame_pointer(sf, sigframe_size))
312 goto sigill_and_return;
314 if (current_thread_info()->w_saved != 0)
315 goto sigill_and_return;
317 /* 2. Save the current process state */
318 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
320 err |= __put_user(0, &sf->extra_size);
323 err |= save_fpu_state(regs, &sf->fpu_state);
324 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
326 err |= __put_user(0, &sf->fpu_save);
329 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
330 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
331 (_NSIG_WORDS - 1) * sizeof(unsigned int));
332 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
333 sizeof(struct reg_window));
337 /* 3. signal handler back-trampoline and parameters */
338 regs->u_regs[UREG_FP] = (unsigned long) sf;
339 regs->u_regs[UREG_I0] = signo;
340 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
341 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
343 /* 4. signal handler */
344 regs->pc = (unsigned long) ka->sa.sa_handler;
345 regs->npc = (regs->pc + 4);
347 /* 5. return to kernel instructions */
349 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
351 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
353 /* mov __NR_sigreturn, %g1 */
354 err |= __put_user(0x821020d8, &sf->insns[0]);
357 err |= __put_user(0x91d02010, &sf->insns[1]);
361 /* Flush instruction space. */
362 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
369 force_sigsegv(signo, current);
372 static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
373 int signo, sigset_t *oldset, siginfo_t *info)
375 struct rt_signal_frame __user *sf;
380 synchronize_user_stack();
381 sigframe_size = RT_ALIGNEDSZ;
383 sigframe_size -= sizeof(__siginfo_fpu_t);
384 sf = (struct rt_signal_frame __user *)
385 get_sigframe(&ka->sa, regs, sigframe_size);
386 if (invalid_frame_pointer(sf, sigframe_size))
388 if (current_thread_info()->w_saved != 0)
391 err = __put_user(regs->pc, &sf->regs.pc);
392 err |= __put_user(regs->npc, &sf->regs.npc);
393 err |= __put_user(regs->y, &sf->regs.y);
397 err |= __put_user(psr, &sf->regs.psr);
398 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
399 err |= __put_user(0, &sf->extra_size);
402 err |= save_fpu_state(regs, &sf->fpu_state);
403 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
405 err |= __put_user(0, &sf->fpu_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 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
415 sizeof(struct reg_window));
417 err |= copy_siginfo_to_user(&sf->info, info);
422 regs->u_regs[UREG_FP] = (unsigned long) sf;
423 regs->u_regs[UREG_I0] = signo;
424 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
425 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
427 regs->pc = (unsigned long) ka->sa.sa_handler;
428 regs->npc = (regs->pc + 4);
431 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
433 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
435 /* mov __NR_sigreturn, %g1 */
436 err |= __put_user(0x821020d8, &sf->insns[0]);
439 err |= __put_user(0x91d02010, &sf->insns[1]);
443 /* Flush instruction space. */
444 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
451 force_sigsegv(signo, current);
455 handle_signal(unsigned long signr, struct k_sigaction *ka,
456 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
458 if (ka->sa.sa_flags & SA_SIGINFO)
459 setup_rt_frame(ka, regs, signr, oldset, info);
461 setup_frame(ka, regs, signr, oldset);
463 spin_lock_irq(¤t->sighand->siglock);
464 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
465 if (!(ka->sa.sa_flags & SA_NOMASK))
466 sigaddset(¤t->blocked, signr);
468 spin_unlock_irq(¤t->sighand->siglock);
471 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
472 struct sigaction *sa)
474 switch(regs->u_regs[UREG_I0]) {
475 case ERESTART_RESTARTBLOCK:
477 no_system_call_restart:
478 regs->u_regs[UREG_I0] = EINTR;
482 if (!(sa->sa_flags & SA_RESTART))
483 goto no_system_call_restart;
486 regs->u_regs[UREG_I0] = orig_i0;
492 /* Note that 'init' is a special process: it doesn't get signals it doesn't
493 * want to handle. Thus you cannot kill init even with a SIGKILL even by
496 asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0, int restart_syscall)
499 struct sparc_deliver_cookie cookie;
500 struct k_sigaction ka;
504 cookie.restart_syscall = restart_syscall;
505 cookie.orig_i0 = orig_i0;
507 if (test_thread_flag(TIF_RESTORE_SIGMASK))
508 oldset = ¤t->saved_sigmask;
510 oldset = ¤t->blocked;
512 signr = get_signal_to_deliver(&info, &ka, regs, &cookie);
514 if (cookie.restart_syscall)
515 syscall_restart(cookie.orig_i0, regs, &ka.sa);
516 handle_signal(signr, &ka, &info, oldset, regs);
518 /* a signal was successfully delivered; the saved
519 * sigmask will have been stored in the signal frame,
520 * and will be restored by sigreturn, so we can simply
521 * clear the TIF_RESTORE_SIGMASK flag.
523 if (test_thread_flag(TIF_RESTORE_SIGMASK))
524 clear_thread_flag(TIF_RESTORE_SIGMASK);
527 if (cookie.restart_syscall &&
528 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
529 regs->u_regs[UREG_I0] == ERESTARTSYS ||
530 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
531 /* replay the system call when we are done */
532 regs->u_regs[UREG_I0] = cookie.orig_i0;
536 if (cookie.restart_syscall &&
537 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
538 regs->u_regs[UREG_G1] = __NR_restart_syscall;
543 /* if there's no signal to deliver, we just put the saved sigmask
546 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
547 clear_thread_flag(TIF_RESTORE_SIGMASK);
548 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
553 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
558 /* First see if old state is wanted. */
560 if (put_user(current->sas_ss_sp + current->sas_ss_size,
561 &ossptr->the_stack) ||
562 __put_user(on_sig_stack(sp), &ossptr->cur_status))
566 /* Now see if we want to update the new state. */
570 if (get_user(ss_sp, &ssptr->the_stack))
572 /* If the current stack was set with sigaltstack, don't
573 swap stacks while we are on it. */
575 if (current->sas_ss_sp && on_sig_stack(sp))
578 /* Since we don't know the extent of the stack, and we don't
579 track onstack-ness, but rather calculate it, we must
580 presume a size. Ho hum this interface is lossy. */
581 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
582 current->sas_ss_size = SIGSTKSZ;
589 void ptrace_signal_deliver(struct pt_regs *regs, void *cookie)
591 struct sparc_deliver_cookie *cp = cookie;
593 if (cp->restart_syscall &&
594 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
595 regs->u_regs[UREG_I0] == ERESTARTSYS ||
596 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
597 /* replay the system call when we are done */
598 regs->u_regs[UREG_I0] = cp->orig_i0;
601 cp->restart_syscall = 0;
604 if (cp->restart_syscall &&
605 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
606 regs->u_regs[UREG_G1] = __NR_restart_syscall;
609 cp->restart_syscall = 0;