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>
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 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
31 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
32 void *fpqueue, unsigned long *fpqdepth);
33 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
36 struct sparc_stackf ss;
38 __siginfo_fpu_t __user *fpu_save;
39 unsigned long insns[2] __attribute__ ((aligned (8)));
40 unsigned int extramask[_NSIG_WORDS - 1];
41 unsigned int extra_size; /* Should be 0 */
42 __siginfo_fpu_t fpu_state;
45 struct rt_signal_frame {
46 struct sparc_stackf ss;
50 __siginfo_fpu_t __user *fpu_save;
51 unsigned int insns[2];
53 unsigned int extra_size; /* Should be 0 */
54 __siginfo_fpu_t fpu_state;
58 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
59 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
61 static int _sigpause_common(old_sigset_t set)
64 spin_lock_irq(¤t->sighand->siglock);
65 current->saved_sigmask = current->blocked;
66 siginitset(¤t->blocked, set);
68 spin_unlock_irq(¤t->sighand->siglock);
70 current->state = TASK_INTERRUPTIBLE;
72 set_thread_flag(TIF_RESTORE_SIGMASK);
74 return -ERESTARTNOHAND;
77 asmlinkage int sys_sigsuspend(old_sigset_t set)
79 return _sigpause_common(set);
83 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
87 if (test_tsk_thread_flag(current, TIF_USEDFPU))
90 if (current == last_task_used_math) {
91 last_task_used_math = NULL;
96 clear_tsk_thread_flag(current, TIF_USEDFPU);
98 if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
101 err = __copy_from_user(¤t->thread.float_regs[0], &fpu->si_float_regs[0],
102 (sizeof(unsigned long) * 32));
103 err |= __get_user(current->thread.fsr, &fpu->si_fsr);
104 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
105 if (current->thread.fpqdepth != 0)
106 err |= __copy_from_user(¤t->thread.fpqueue[0],
108 ((sizeof(unsigned long) +
109 (sizeof(unsigned long *)))*16));
113 asmlinkage void do_sigreturn(struct pt_regs *regs)
115 struct signal_frame __user *sf;
116 unsigned long up_psr, pc, npc;
118 __siginfo_fpu_t __user *fpu_save;
121 /* Always make any pending restarted system calls return -EINTR */
122 current_thread_info()->restart_block.fn = do_no_restart_syscall;
124 synchronize_user_stack();
126 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
128 /* 1. Make sure we are not getting garbage from the user */
129 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
132 if (((unsigned long) sf) & 3)
135 err = __get_user(pc, &sf->info.si_regs.pc);
136 err |= __get_user(npc, &sf->info.si_regs.npc);
141 /* 2. Restore the state */
143 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
145 /* User can only change condition codes and FPU enabling in %psr. */
146 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
147 | (regs->psr & (PSR_ICC | PSR_EF));
149 /* Prevent syscall restart. */
150 pt_regs_clear_syscall(regs);
152 err |= __get_user(fpu_save, &sf->fpu_save);
155 err |= restore_fpu_state(regs, fpu_save);
157 /* This is pretty much atomic, no amount locking would prevent
158 * the races which exist anyways.
160 err |= __get_user(set.sig[0], &sf->info.si_mask);
161 err |= __copy_from_user(&set.sig[1], &sf->extramask,
162 (_NSIG_WORDS-1) * sizeof(unsigned int));
167 sigdelsetmask(&set, ~_BLOCKABLE);
168 spin_lock_irq(¤t->sighand->siglock);
169 current->blocked = set;
171 spin_unlock_irq(¤t->sighand->siglock);
175 force_sig(SIGSEGV, current);
178 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
180 struct rt_signal_frame __user *sf;
181 unsigned int psr, pc, npc;
182 __siginfo_fpu_t __user *fpu_save;
188 synchronize_user_stack();
189 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
190 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
191 (((unsigned long) sf) & 0x03))
194 err = __get_user(pc, &sf->regs.pc);
195 err |= __get_user(npc, &sf->regs.npc);
196 err |= ((pc | npc) & 0x03);
198 err |= __get_user(regs->y, &sf->regs.y);
199 err |= __get_user(psr, &sf->regs.psr);
201 err |= __copy_from_user(®s->u_regs[UREG_G1],
202 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
204 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
206 /* Prevent syscall restart. */
207 pt_regs_clear_syscall(regs);
209 err |= __get_user(fpu_save, &sf->fpu_save);
212 err |= restore_fpu_state(regs, fpu_save);
213 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
215 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
223 /* It is more difficult to avoid calling this function than to
224 * call it and ignore errors.
228 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
231 sigdelsetmask(&set, ~_BLOCKABLE);
232 spin_lock_irq(¤t->sighand->siglock);
233 current->blocked = set;
235 spin_unlock_irq(¤t->sighand->siglock);
238 force_sig(SIGSEGV, current);
241 /* Checks if the fp is valid */
242 static inline int invalid_frame_pointer(void __user *fp, int fplen)
244 if ((((unsigned long) fp) & 7) ||
245 !__access_ok((unsigned long)fp, fplen) ||
246 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
247 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
253 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
255 unsigned long sp = regs->u_regs[UREG_FP];
258 * If we are on the alternate signal stack and would overflow it, don't.
259 * Return an always-bogus address instead so we will die with SIGSEGV.
261 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
262 return (void __user *) -1L;
264 /* This is the X/Open sanctioned signal stack switching. */
265 if (sa->sa_flags & SA_ONSTACK) {
266 if (sas_ss_flags(sp) == 0)
267 sp = current->sas_ss_sp + current->sas_ss_size;
270 /* Always align the stack frame. This handles two cases. First,
271 * sigaltstack need not be mindful of platform specific stack
272 * alignment. Second, if we took this signal because the stack
273 * is not aligned properly, we'd like to take the signal cleanly
278 return (void __user *)(sp - framesize);
282 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
286 if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
287 put_psr(get_psr() | PSR_EF);
288 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
289 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
290 regs->psr &= ~(PSR_EF);
291 clear_tsk_thread_flag(current, TIF_USEDFPU);
294 if (current == last_task_used_math) {
295 put_psr(get_psr() | PSR_EF);
296 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
297 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
298 last_task_used_math = NULL;
299 regs->psr &= ~(PSR_EF);
302 err |= __copy_to_user(&fpu->si_float_regs[0],
303 ¤t->thread.float_regs[0],
304 (sizeof(unsigned long) * 32));
305 err |= __put_user(current->thread.fsr, &fpu->si_fsr);
306 err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
307 if (current->thread.fpqdepth != 0)
308 err |= __copy_to_user(&fpu->si_fpqueue[0],
309 ¤t->thread.fpqueue[0],
310 ((sizeof(unsigned long) +
311 (sizeof(unsigned long *)))*16));
316 static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
317 int signo, sigset_t *oldset)
319 struct signal_frame __user *sf;
320 int sigframe_size, err;
322 /* 1. Make sure everything is clean */
323 synchronize_user_stack();
325 sigframe_size = SF_ALIGNEDSZ;
327 sigframe_size -= sizeof(__siginfo_fpu_t);
329 sf = (struct signal_frame __user *)
330 get_sigframe(&ka->sa, regs, sigframe_size);
332 if (invalid_frame_pointer(sf, sigframe_size))
333 goto sigill_and_return;
335 if (current_thread_info()->w_saved != 0)
336 goto sigill_and_return;
338 /* 2. Save the current process state */
339 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
341 err |= __put_user(0, &sf->extra_size);
344 err |= save_fpu_state(regs, &sf->fpu_state);
345 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
347 err |= __put_user(0, &sf->fpu_save);
350 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
351 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
352 (_NSIG_WORDS - 1) * sizeof(unsigned int));
353 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
354 sizeof(struct reg_window));
358 /* 3. signal handler back-trampoline and parameters */
359 regs->u_regs[UREG_FP] = (unsigned long) sf;
360 regs->u_regs[UREG_I0] = signo;
361 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
362 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
364 /* 4. signal handler */
365 regs->pc = (unsigned long) ka->sa.sa_handler;
366 regs->npc = (regs->pc + 4);
368 /* 5. return to kernel instructions */
370 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
372 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
374 /* mov __NR_sigreturn, %g1 */
375 err |= __put_user(0x821020d8, &sf->insns[0]);
378 err |= __put_user(0x91d02010, &sf->insns[1]);
382 /* Flush instruction space. */
383 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
390 force_sigsegv(signo, current);
393 static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
394 int signo, sigset_t *oldset, siginfo_t *info)
396 struct rt_signal_frame __user *sf;
401 synchronize_user_stack();
402 sigframe_size = RT_ALIGNEDSZ;
404 sigframe_size -= sizeof(__siginfo_fpu_t);
405 sf = (struct rt_signal_frame __user *)
406 get_sigframe(&ka->sa, regs, sigframe_size);
407 if (invalid_frame_pointer(sf, sigframe_size))
409 if (current_thread_info()->w_saved != 0)
412 err = __put_user(regs->pc, &sf->regs.pc);
413 err |= __put_user(regs->npc, &sf->regs.npc);
414 err |= __put_user(regs->y, &sf->regs.y);
418 err |= __put_user(psr, &sf->regs.psr);
419 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
420 err |= __put_user(0, &sf->extra_size);
423 err |= save_fpu_state(regs, &sf->fpu_state);
424 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
426 err |= __put_user(0, &sf->fpu_save);
428 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
430 /* Setup sigaltstack */
431 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
432 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
433 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
435 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
436 sizeof(struct reg_window));
438 err |= copy_siginfo_to_user(&sf->info, info);
443 regs->u_regs[UREG_FP] = (unsigned long) sf;
444 regs->u_regs[UREG_I0] = signo;
445 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
446 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
448 regs->pc = (unsigned long) ka->sa.sa_handler;
449 regs->npc = (regs->pc + 4);
452 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
454 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
456 /* mov __NR_sigreturn, %g1 */
457 err |= __put_user(0x821020d8, &sf->insns[0]);
460 err |= __put_user(0x91d02010, &sf->insns[1]);
464 /* Flush instruction space. */
465 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
472 force_sigsegv(signo, current);
476 handle_signal(unsigned long signr, struct k_sigaction *ka,
477 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
479 if (ka->sa.sa_flags & SA_SIGINFO)
480 setup_rt_frame(ka, regs, signr, oldset, info);
482 setup_frame(ka, regs, signr, oldset);
484 spin_lock_irq(¤t->sighand->siglock);
485 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
486 if (!(ka->sa.sa_flags & SA_NOMASK))
487 sigaddset(¤t->blocked, signr);
489 spin_unlock_irq(¤t->sighand->siglock);
492 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
493 struct sigaction *sa)
495 switch(regs->u_regs[UREG_I0]) {
496 case ERESTART_RESTARTBLOCK:
498 no_system_call_restart:
499 regs->u_regs[UREG_I0] = EINTR;
503 if (!(sa->sa_flags & SA_RESTART))
504 goto no_system_call_restart;
507 regs->u_regs[UREG_I0] = orig_i0;
513 /* Note that 'init' is a special process: it doesn't get signals it doesn't
514 * want to handle. Thus you cannot kill init even with a SIGKILL even by
517 static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
519 struct k_sigaction ka;
525 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
530 if (test_thread_flag(TIF_RESTORE_SIGMASK))
531 oldset = ¤t->saved_sigmask;
533 oldset = ¤t->blocked;
535 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
537 /* If the debugger messes with the program counter, it clears
538 * the software "in syscall" bit, directing us to not perform
541 if (restart_syscall && !pt_regs_is_syscall(regs))
546 syscall_restart(orig_i0, regs, &ka.sa);
547 handle_signal(signr, &ka, &info, oldset, regs);
549 /* a signal was successfully delivered; the saved
550 * sigmask will have been stored in the signal frame,
551 * and will be restored by sigreturn, so we can simply
552 * clear the TIF_RESTORE_SIGMASK flag.
554 if (test_thread_flag(TIF_RESTORE_SIGMASK))
555 clear_thread_flag(TIF_RESTORE_SIGMASK);
557 tracehook_signal_handler(signr, &info, &ka, regs, 0);
560 if (restart_syscall &&
561 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
562 regs->u_regs[UREG_I0] == ERESTARTSYS ||
563 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
564 /* replay the system call when we are done */
565 regs->u_regs[UREG_I0] = orig_i0;
569 if (restart_syscall &&
570 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
571 regs->u_regs[UREG_G1] = __NR_restart_syscall;
576 /* if there's no signal to deliver, we just put the saved sigmask
579 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
580 clear_thread_flag(TIF_RESTORE_SIGMASK);
581 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
585 void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
586 unsigned long thread_info_flags)
588 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
589 do_signal(regs, orig_i0);
590 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
591 clear_thread_flag(TIF_NOTIFY_RESUME);
592 tracehook_notify_resume(regs);
597 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
602 /* First see if old state is wanted. */
604 if (put_user(current->sas_ss_sp + current->sas_ss_size,
605 &ossptr->the_stack) ||
606 __put_user(on_sig_stack(sp), &ossptr->cur_status))
610 /* Now see if we want to update the new state. */
614 if (get_user(ss_sp, &ssptr->the_stack))
616 /* If the current stack was set with sigaltstack, don't
617 swap stacks while we are on it. */
619 if (current->sas_ss_sp && on_sig_stack(sp))
622 /* Since we don't know the extent of the stack, and we don't
623 track onstack-ness, but rather calculate it, we must
624 presume a size. Ho hum this interface is lossy. */
625 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
626 current->sas_ss_size = SIGSTKSZ;