2 * irixsig.c: WHEEE, IRIX signals! YOW, am I compatible or what?!?!
4 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
5 * Copyright (C) 1997 - 2000 Ralf Baechle (ralf@gnu.org)
6 * Copyright (C) 2000 Silicon Graphics, Inc.
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
11 #include <linux/errno.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/time.h>
15 #include <linux/ptrace.h>
16 #include <linux/resource.h>
18 #include <asm/ptrace.h>
19 #include <asm/uaccess.h>
20 #include <asm/unistd.h>
24 #define _S(nr) (1<<((nr)-1))
26 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
33 u32 rmask, cp0_status;
37 u32 usedfp, fpcsr, fpeir, sstk_flags;
39 u64 cp0_cause, cp0_badvaddr, _unused0;
47 static inline void dump_irix5_sigctx(struct sigctx_irix5 *c)
51 printk("misc: rmask[%08lx] status[%08lx] pc[%08lx]\n",
52 (unsigned long) c->rmask,
53 (unsigned long) c->cp0_status,
54 (unsigned long) c->pc);
56 for(i = 0; i < 16; i++)
57 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
59 for(i = 16; i < 32; i++)
60 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
62 for(i = 0; i < 16; i++)
63 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
65 for(i = 16; i < 32; i++)
66 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
67 printk("misc: usedfp[%d] fpcsr[%08lx] fpeir[%08lx] stk_flgs[%08lx]\n",
68 (int) c->usedfp, (unsigned long) c->fpcsr,
69 (unsigned long) c->fpeir, (unsigned long) c->sstk_flags);
70 printk("misc: hi[%08lx] lo[%08lx] cause[%08lx] badvaddr[%08lx]\n",
71 (unsigned long) c->hi, (unsigned long) c->lo,
72 (unsigned long) c->cp0_cause, (unsigned long) c->cp0_badvaddr);
73 printk("misc: sigset<0>[%08lx] sigset<1>[%08lx] sigset<2>[%08lx] "
74 "sigset<3>[%08lx]\n", (unsigned long) c->sigset.sig[0],
75 (unsigned long) c->sigset.sig[1],
76 (unsigned long) c->sigset.sig[2],
77 (unsigned long) c->sigset.sig[3]);
81 static int setup_irix_frame(struct k_sigaction *ka, struct pt_regs *regs,
82 int signr, sigset_t *oldmask)
84 struct sigctx_irix5 __user *ctx;
89 sp -= sizeof(struct sigctx_irix5);
91 ctx = (struct sigctx_irix5 __user *) sp;
92 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
95 error = __put_user(0, &ctx->weird_fpu_thing);
96 error |= __put_user(~(0x00000001), &ctx->rmask);
97 error |= __put_user(0, &ctx->regs[0]);
98 for(i = 1; i < 32; i++)
99 error |= __put_user((u64) regs->regs[i], &ctx->regs[i]);
101 error |= __put_user((u64) regs->hi, &ctx->hi);
102 error |= __put_user((u64) regs->lo, &ctx->lo);
103 error |= __put_user((u64) regs->cp0_epc, &ctx->pc);
104 error |= __put_user(!!used_math(), &ctx->usedfp);
105 error |= __put_user((u64) regs->cp0_cause, &ctx->cp0_cause);
106 error |= __put_user((u64) regs->cp0_badvaddr, &ctx->cp0_badvaddr);
108 error |= __put_user(0, &ctx->sstk_flags); /* XXX sigstack unimp... todo... */
110 error |= __copy_to_user(&ctx->sigset, oldmask, sizeof(irix_sigset_t)) ? -EFAULT : 0;
116 dump_irix5_sigctx(ctx);
119 regs->regs[4] = (unsigned long) signr;
120 regs->regs[5] = 0; /* XXX sigcode XXX */
121 regs->regs[6] = regs->regs[29] = sp;
122 regs->regs[7] = (unsigned long) ka->sa.sa_handler;
123 regs->regs[25] = regs->cp0_epc = (unsigned long) ka->sa_restorer;
128 force_sigsegv(signr, current);
133 setup_irix_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
134 int signr, sigset_t *oldmask, siginfo_t *info)
136 printk("Aiee: setup_tr_frame wants to be written");
140 static inline int handle_signal(unsigned long sig, siginfo_t *info,
141 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs)
145 switch(regs->regs[0]) {
147 regs->regs[2] = EINTR;
150 if(!(ka->sa.sa_flags & SA_RESTART)) {
151 regs->regs[2] = EINTR;
155 case ERESTARTNOINTR: /* Userland will reload $v0. */
159 regs->regs[0] = 0; /* Don't deal with this again. */
161 if (ka->sa.sa_flags & SA_SIGINFO)
162 ret = setup_irix_rt_frame(ka, regs, sig, oldset, info);
164 ret = setup_irix_frame(ka, regs, sig, oldset);
166 spin_lock_irq(¤t->sighand->siglock);
167 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
168 if (!(ka->sa.sa_flags & SA_NODEFER))
169 sigaddset(¤t->blocked,sig);
171 spin_unlock_irq(¤t->sighand->siglock);
176 void do_irix_signal(struct pt_regs *regs)
178 struct k_sigaction ka;
184 * We want the common case to go fast, which is why we may in certain
185 * cases get here from kernel mode. Just return without doing anything
188 if (!user_mode(regs))
191 if (test_thread_flag(TIF_RESTORE_SIGMASK))
192 oldset = ¤t->saved_sigmask;
194 oldset = ¤t->blocked;
196 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
198 /* Whee! Actually deliver the signal. */
199 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
200 /* a signal was successfully delivered; the saved
201 * sigmask will have been stored in the signal frame,
202 * and will be restored by sigreturn, so we can simply
203 * clear the TIF_RESTORE_SIGMASK flag */
204 if (test_thread_flag(TIF_RESTORE_SIGMASK))
205 clear_thread_flag(TIF_RESTORE_SIGMASK);
212 * Who's code doesn't conform to the restartable syscall convention
213 * dies here!!! The li instruction, a single machine instruction,
214 * must directly be followed by the syscall instruction.
217 if (regs->regs[2] == ERESTARTNOHAND ||
218 regs->regs[2] == ERESTARTSYS ||
219 regs->regs[2] == ERESTARTNOINTR) {
222 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
223 regs->regs[2] = __NR_restart_syscall;
224 regs->regs[7] = regs->regs[26];
227 regs->regs[0] = 0; /* Don't deal with this again. */
231 * If there's no signal to deliver, we just put the saved sigmask
234 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
235 clear_thread_flag(TIF_RESTORE_SIGMASK);
236 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
241 irix_sigreturn(struct pt_regs *regs)
243 struct sigctx_irix5 __user *context, *magic;
244 unsigned long umask, mask;
247 int error, sig, i, base = 0;
250 /* Always make any pending restarted system calls return -EINTR */
251 current_thread_info()->restart_block.fn = do_no_restart_syscall;
253 if (regs->regs[2] == 1000)
256 context = (struct sigctx_irix5 __user *) regs->regs[base + 4];
257 magic = (struct sigctx_irix5 __user *) regs->regs[base + 5];
258 sig = (int) regs->regs[base + 6];
260 printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
261 current->comm, current->pid, context, magic, sig);
265 if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
269 dump_irix5_sigctx(context);
272 error = __get_user(regs->cp0_epc, &context->pc);
273 error |= __get_user(umask, &context->rmask);
276 for (i = 1; i < 32; i++, mask <<= 1) {
278 error |= __get_user(regs->regs[i], &context->regs[i]);
280 error |= __get_user(regs->hi, &context->hi);
281 error |= __get_user(regs->lo, &context->lo);
283 error |= __get_user(usedfp, &context->usedfp);
284 if ((umask & 1) && usedfp) {
285 fregs = (u64 *) ¤t->thread.fpu;
287 for(i = 0; i < 32; i++)
288 error |= __get_user(fregs[i], &context->fpregs[i]);
289 error |= __get_user(current->thread.fpu.fcr31, &context->fpcsr);
292 /* XXX do sigstack crapola here... XXX */
294 error |= __copy_from_user(&blocked, &context->sigset, sizeof(blocked)) ? -EFAULT : 0;
299 sigdelsetmask(&blocked, ~_BLOCKABLE);
300 spin_lock_irq(¤t->sighand->siglock);
301 current->blocked = blocked;
303 spin_unlock_irq(¤t->sighand->siglock);
306 * Don't let your children do this ...
308 __asm__ __volatile__(
316 force_sig(SIGSEGV, current);
319 struct sigact_irix5 {
321 void (*handler)(int);
327 static inline void dump_sigact_irix5(struct sigact_irix5 *p)
329 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
330 (unsigned long) p->handler,
331 (unsigned long) p->sigset[0]);
336 irix_sigaction(int sig, const struct sigaction __user *act,
337 struct sigaction __user *oact, void __user *trampoline)
339 struct k_sigaction new_ka, old_ka;
343 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
344 (!old ? "0" : "OLD"), trampoline);
346 dump_sigact_irix5(new); printk(" ");
353 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
355 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
356 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
358 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
363 * Hmmm... methinks IRIX libc always passes a valid trampoline
364 * value for all invocations of sigaction. Will have to
365 * investigate. POSIX POSIX, die die die...
367 new_ka.sa_restorer = trampoline;
370 /* XXX Implement SIG_SETMASK32 for IRIX compatibility */
371 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
376 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
379 err = __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
380 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
381 err |= __copy_to_user(&oact->sa_mask, &old_ka.sa.sa_mask,
382 sizeof(sigset_t)) ? -EFAULT : 0;
390 asmlinkage int irix_sigpending(irix_sigset_t __user *set)
392 return do_sigpending(set, sizeof(*set));
395 asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
396 irix_sigset_t __user *old)
398 sigset_t oldbits, newbits;
401 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
403 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
405 sigdelsetmask(&newbits, ~_BLOCKABLE);
407 spin_lock_irq(¤t->sighand->siglock);
408 oldbits = current->blocked;
412 sigorsets(&newbits, &oldbits, &newbits);
416 sigandsets(&newbits, &oldbits, &newbits);
423 siginitset(&newbits, newbits.sig[0]);
430 spin_unlock_irq(¤t->sighand->siglock);
433 return copy_to_user(old, ¤t->blocked,
434 sizeof(unsigned long)*4) ? -EFAULT : 0;
439 asmlinkage int irix_sigsuspend(struct pt_regs *regs)
442 sigset_t __user *uset;
444 uset = (sigset_t __user *) regs->regs[4];
445 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
447 sigdelsetmask(&newset, ~_BLOCKABLE);
449 spin_lock_irq(¤t->sighand->siglock);
450 current->saved_sigmask = current->blocked;
451 current->blocked = newset;
453 spin_unlock_irq(¤t->sighand->siglock);
455 current->state = TASK_INTERRUPTIBLE;
457 set_thread_flag(TIF_RESTORE_SIGMASK);
458 return -ERESTARTNOHAND;
461 /* hate hate hate... */
462 struct irix5_siginfo {
463 int sig, code, error;
465 char unused[128 - (3 * 4)]; /* Safety net. */
471 int utime, status, stime;
476 unsigned long fault_addr;
483 unsigned long sigval;
487 asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
488 struct irix5_siginfo __user *info, struct timespec __user *tp)
490 long expire = MAX_SCHEDULE_TIMEOUT;
492 int i, sig, error, timeo = 0;
496 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
497 current->comm, current->pid, set, info, tp);
500 /* Must always specify the signal set. */
504 if (copy_from_user(&kset, set, sizeof(set)))
507 if (info && clear_user(info, sizeof(*info))) {
513 if (copy_from_user(&ktp, tp, sizeof(*tp)))
516 if (!ktp.tv_sec && !ktp.tv_nsec)
519 expire = timespec_to_jiffies(&ktp) +
520 (ktp.tv_sec || ktp.tv_nsec);
526 expire = schedule_timeout_interruptible(expire);
529 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
537 if (signal_pending(current))
543 for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
544 if (sigismember (&kset, sig))
546 if (sigismember (¤t->pending.signal, sig)) {
547 /* XXX need more than this... */
549 return copy_to_user(&info->sig, &sig, sizeof(sig));
554 /* Should not get here, but do something sane if we do. */
561 /* This is here because of irix5_siginfo definition. */
563 #define IRIX_P_PGID 2
572 #define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
574 asmlinkage int irix_waitsys(int type, int pid,
575 struct irix5_siginfo __user *info, int options,
576 struct rusage __user *ru)
579 DECLARE_WAITQUEUE(wait, current);
580 struct task_struct *tsk;
581 struct task_struct *p;
582 struct list_head *_p;
587 if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
591 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
594 if (options & ~W_MASK)
597 if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
600 add_wait_queue(¤t->signal->wait_chldexit, &wait);
603 current->state = TASK_INTERRUPTIBLE;
604 read_lock(&tasklist_lock);
606 list_for_each(_p,&tsk->children) {
607 p = list_entry(_p,struct task_struct,sibling);
608 if ((type == IRIX_P_PID) && p->pid != pid)
610 if ((type == IRIX_P_PGID) && process_group(p) != pid)
612 if ((p->exit_signal != SIGCHLD))
619 if (!(options & (W_TRAPPED|W_STOPPED)) &&
620 !(p->ptrace & PT_PTRACED))
622 read_unlock(&tasklist_lock);
624 /* move to end of parent's list to avoid starvation */
625 write_lock_irq(&tasklist_lock);
628 write_unlock_irq(&tasklist_lock);
629 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
633 retval = __put_user(SIGCHLD, &info->sig);
634 retval |= __put_user(0, &info->code);
635 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
636 retval |= __put_user((p->exit_code >> 8) & 0xff,
637 &info->stuff.procinfo.procdata.child.status);
638 retval |= __put_user(p->utime, &info->stuff.procinfo.procdata.child.utime);
639 retval |= __put_user(p->stime, &info->stuff.procinfo.procdata.child.stime);
647 current->signal->cutime += p->utime + p->signal->cutime;
648 current->signal->cstime += p->stime + p->signal->cstime;
650 getrusage(p, RUSAGE_BOTH, ru);
651 retval = __put_user(SIGCHLD, &info->sig);
652 retval |= __put_user(1, &info->code); /* CLD_EXITED */
653 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
654 retval |= __put_user((p->exit_code >> 8) & 0xff,
655 &info->stuff.procinfo.procdata.child.status);
656 retval |= __put_user(p->utime,
657 &info->stuff.procinfo.procdata.child.utime);
658 retval |= __put_user(p->stime,
659 &info->stuff.procinfo.procdata.child.stime);
663 if (p->real_parent != p->parent) {
664 write_lock_irq(&tasklist_lock);
666 p->parent = p->real_parent;
668 do_notify_parent(p, SIGCHLD);
669 write_unlock_irq(&tasklist_lock);
676 tsk = next_thread(tsk);
678 read_unlock(&tasklist_lock);
681 if (options & W_NOHANG)
683 retval = -ERESTARTSYS;
684 if (signal_pending(current))
686 current->state = TASK_INTERRUPTIBLE;
692 current->state = TASK_RUNNING;
693 remove_wait_queue(¤t->signal->wait_chldexit, &wait);
698 struct irix5_context {
702 struct { u32 sp, size, flags; } stack;
708 u32 weird_graphics_thing;
711 asmlinkage int irix_getcontext(struct pt_regs *regs)
713 int error, i, base = 0;
714 struct irix5_context __user *ctx;
717 if (regs->regs[2] == 1000)
719 ctx = (struct irix5_context __user *) regs->regs[base + 4];
722 printk("[%s:%d] irix_getcontext(%p)\n",
723 current->comm, current->pid, ctx);
726 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)));
729 error = __put_user(current->thread.irix_oldctx, &ctx->link);
731 error |= __copy_to_user(&ctx->sigmask, ¤t->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
733 /* XXX Do sigstack stuff someday... */
734 error |= __put_user(0, &ctx->stack.sp);
735 error |= __put_user(0, &ctx->stack.size);
736 error |= __put_user(0, &ctx->stack.flags);
738 error |= __put_user(0, &ctx->weird_graphics_thing);
739 error |= __put_user(0, &ctx->regs[0]);
740 for (i = 1; i < 32; i++)
741 error |= __put_user(regs->regs[i], &ctx->regs[i]);
742 error |= __put_user(regs->lo, &ctx->regs[32]);
743 error |= __put_user(regs->hi, &ctx->regs[33]);
744 error |= __put_user(regs->cp0_cause, &ctx->regs[34]);
745 error |= __put_user(regs->cp0_epc, &ctx->regs[35]);
752 printk("Wheee, no code for saving IRIX FPU context yet.\n");
754 error |= __put_user(flags, &ctx->flags);
759 asmlinkage void irix_setcontext(struct pt_regs *regs)
761 struct irix5_context __user *ctx;
765 if (regs->regs[2] == 1000)
767 ctx = (struct irix5_context __user *) regs->regs[base + 4];
770 printk("[%s:%d] irix_setcontext(%p)\n",
771 current->comm, current->pid, ctx);
774 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
777 err = __get_user(flags, &ctx->flags);
779 /* XXX sigstack garbage, todo... */
780 printk("Wheee, cannot do sigstack stuff in setcontext\n");
786 /* XXX extra control block stuff... todo... */
787 for (i = 1; i < 32; i++)
788 err |= __get_user(regs->regs[i], &ctx->regs[i]);
789 err |= __get_user(regs->lo, &ctx->regs[32]);
790 err |= __get_user(regs->hi, &ctx->regs[33]);
791 err |= __get_user(regs->cp0_epc, &ctx->regs[35]);
795 /* XXX fpu context, blah... */
796 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
798 err |= __get_user(current->thread.irix_oldctx, &ctx->link);
803 * Don't let your children do this ...
805 __asm__ __volatile__(
813 force_sigsegv(SIGSEGV, current);
816 struct irix_sigstack {
821 asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
822 struct irix_sigstack __user *old)
825 printk("[%s:%d] irix_sigstack(%p,%p)\n",
826 current->comm, current->pid, new, old);
829 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
834 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
841 struct irix_sigaltstack { unsigned long sp; int size; int status; };
843 asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
844 struct irix_sigaltstack __user *old)
847 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
848 current->comm, current->pid, new, old);
851 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
855 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
862 struct irix_procset {
863 int cmd, ltype, lid, rtype, rid;
866 asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
868 if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
871 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
872 current->comm, current->pid,
873 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,