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/time.h>
14 #include <linux/ptrace.h>
15 #include <linux/resource.h>
17 #include <asm/ptrace.h>
18 #include <asm/uaccess.h>
19 #include <asm/unistd.h>
23 #define _S(nr) (1<<((nr)-1))
25 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
27 #define _IRIX_NSIG 128
28 #define _IRIX_NSIG_BPW BITS_PER_LONG
29 #define _IRIX_NSIG_WORDS (_IRIX_NSIG / _IRIX_NSIG_BPW)
32 unsigned long sig[_IRIX_NSIG_WORDS];
36 u32 rmask, cp0_status;
40 u32 usedfp, fpcsr, fpeir, sstk_flags;
42 u64 cp0_cause, cp0_badvaddr, _unused0;
50 static inline void dump_irix5_sigctx(struct sigctx_irix5 *c)
54 printk("misc: rmask[%08lx] status[%08lx] pc[%08lx]\n",
55 (unsigned long) c->rmask,
56 (unsigned long) c->cp0_status,
57 (unsigned long) c->pc);
59 for(i = 0; i < 16; i++)
60 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
62 for(i = 16; i < 32; i++)
63 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
65 for(i = 0; i < 16; i++)
66 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
68 for(i = 16; i < 32; i++)
69 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
70 printk("misc: usedfp[%d] fpcsr[%08lx] fpeir[%08lx] stk_flgs[%08lx]\n",
71 (int) c->usedfp, (unsigned long) c->fpcsr,
72 (unsigned long) c->fpeir, (unsigned long) c->sstk_flags);
73 printk("misc: hi[%08lx] lo[%08lx] cause[%08lx] badvaddr[%08lx]\n",
74 (unsigned long) c->hi, (unsigned long) c->lo,
75 (unsigned long) c->cp0_cause, (unsigned long) c->cp0_badvaddr);
76 printk("misc: sigset<0>[%08lx] sigset<1>[%08lx] sigset<2>[%08lx] "
77 "sigset<3>[%08lx]\n", (unsigned long) c->sigset.sig[0],
78 (unsigned long) c->sigset.sig[1],
79 (unsigned long) c->sigset.sig[2],
80 (unsigned long) c->sigset.sig[3]);
84 static int setup_irix_frame(struct k_sigaction *ka, struct pt_regs *regs,
85 int signr, sigset_t *oldmask)
87 struct sigctx_irix5 __user *ctx;
92 sp -= sizeof(struct sigctx_irix5);
94 ctx = (struct sigctx_irix5 __user *) sp;
95 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
98 error = __put_user(0, &ctx->weird_fpu_thing);
99 error |= __put_user(~(0x00000001), &ctx->rmask);
100 error |= __put_user(0, &ctx->regs[0]);
101 for(i = 1; i < 32; i++)
102 error |= __put_user((u64) regs->regs[i], &ctx->regs[i]);
104 error |= __put_user((u64) regs->hi, &ctx->hi);
105 error |= __put_user((u64) regs->lo, &ctx->lo);
106 error |= __put_user((u64) regs->cp0_epc, &ctx->pc);
107 error |= __put_user(!!used_math(), &ctx->usedfp);
108 error |= __put_user((u64) regs->cp0_cause, &ctx->cp0_cause);
109 error |= __put_user((u64) regs->cp0_badvaddr, &ctx->cp0_badvaddr);
111 error |= __put_user(0, &ctx->sstk_flags); /* XXX sigstack unimp... todo... */
113 error |= __copy_to_user(&ctx->sigset, oldmask, sizeof(irix_sigset_t)) ? -EFAULT : 0;
119 dump_irix5_sigctx(ctx);
122 regs->regs[4] = (unsigned long) signr;
123 regs->regs[5] = 0; /* XXX sigcode XXX */
124 regs->regs[6] = regs->regs[29] = sp;
125 regs->regs[7] = (unsigned long) ka->sa.sa_handler;
126 regs->regs[25] = regs->cp0_epc = (unsigned long) ka->sa_restorer;
131 force_sigsegv(signr, current);
136 setup_irix_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
137 int signr, sigset_t *oldmask, siginfo_t *info)
139 printk("Aiee: setup_tr_frame wants to be written");
143 static inline int handle_signal(unsigned long sig, siginfo_t *info,
144 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs)
148 switch(regs->regs[0]) {
150 regs->regs[2] = EINTR;
153 if(!(ka->sa.sa_flags & SA_RESTART)) {
154 regs->regs[2] = EINTR;
158 case ERESTARTNOINTR: /* Userland will reload $v0. */
162 regs->regs[0] = 0; /* Don't deal with this again. */
164 if (ka->sa.sa_flags & SA_SIGINFO)
165 ret = setup_irix_rt_frame(ka, regs, sig, oldset, info);
167 ret = setup_irix_frame(ka, regs, sig, oldset);
169 spin_lock_irq(¤t->sighand->siglock);
170 sigorsets(¤t->blocked, ¤t->blocked, &ka->sa.sa_mask);
171 if (!(ka->sa.sa_flags & SA_NODEFER))
172 sigaddset(¤t->blocked, sig);
174 spin_unlock_irq(¤t->sighand->siglock);
179 void do_irix_signal(struct pt_regs *regs)
181 struct k_sigaction ka;
187 * We want the common case to go fast, which is why we may in certain
188 * cases get here from kernel mode. Just return without doing anything
191 if (!user_mode(regs))
194 if (test_thread_flag(TIF_RESTORE_SIGMASK))
195 oldset = ¤t->saved_sigmask;
197 oldset = ¤t->blocked;
199 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
201 /* Whee! Actually deliver the signal. */
202 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
203 /* a signal was successfully delivered; the saved
204 * sigmask will have been stored in the signal frame,
205 * and will be restored by sigreturn, so we can simply
206 * clear the TIF_RESTORE_SIGMASK flag */
207 if (test_thread_flag(TIF_RESTORE_SIGMASK))
208 clear_thread_flag(TIF_RESTORE_SIGMASK);
215 * Who's code doesn't conform to the restartable syscall convention
216 * dies here!!! The li instruction, a single machine instruction,
217 * must directly be followed by the syscall instruction.
220 if (regs->regs[2] == ERESTARTNOHAND ||
221 regs->regs[2] == ERESTARTSYS ||
222 regs->regs[2] == ERESTARTNOINTR) {
225 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
226 regs->regs[2] = __NR_restart_syscall;
227 regs->regs[7] = regs->regs[26];
230 regs->regs[0] = 0; /* Don't deal with this again. */
234 * If there's no signal to deliver, we just put the saved sigmask
237 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
238 clear_thread_flag(TIF_RESTORE_SIGMASK);
239 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
244 irix_sigreturn(struct pt_regs *regs)
246 struct sigctx_irix5 __user *context, *magic;
247 unsigned long umask, mask;
250 int error, sig, i, base = 0;
253 /* Always make any pending restarted system calls return -EINTR */
254 current_thread_info()->restart_block.fn = do_no_restart_syscall;
256 if (regs->regs[2] == 1000)
259 context = (struct sigctx_irix5 __user *) regs->regs[base + 4];
260 magic = (struct sigctx_irix5 __user *) regs->regs[base + 5];
261 sig = (int) regs->regs[base + 6];
263 printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
264 current->comm, current->pid, context, magic, sig);
268 if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
272 dump_irix5_sigctx(context);
275 error = __get_user(regs->cp0_epc, &context->pc);
276 error |= __get_user(umask, &context->rmask);
279 for (i = 1; i < 32; i++, mask <<= 1) {
281 error |= __get_user(regs->regs[i], &context->regs[i]);
283 error |= __get_user(regs->hi, &context->hi);
284 error |= __get_user(regs->lo, &context->lo);
286 error |= __get_user(usedfp, &context->usedfp);
287 if ((umask & 1) && usedfp) {
288 fregs = (u64 *) ¤t->thread.fpu;
290 for(i = 0; i < 32; i++)
291 error |= __get_user(fregs[i], &context->fpregs[i]);
292 error |= __get_user(current->thread.fpu.fcr31, &context->fpcsr);
295 /* XXX do sigstack crapola here... XXX */
297 error |= __copy_from_user(&blocked, &context->sigset, sizeof(blocked)) ? -EFAULT : 0;
302 sigdelsetmask(&blocked, ~_BLOCKABLE);
303 spin_lock_irq(¤t->sighand->siglock);
304 current->blocked = blocked;
306 spin_unlock_irq(¤t->sighand->siglock);
309 * Don't let your children do this ...
311 __asm__ __volatile__(
319 force_sig(SIGSEGV, current);
322 struct sigact_irix5 {
324 void (*handler)(int);
329 #define SIG_SETMASK32 256 /* Goodie from SGI for BSD compatibility:
330 set only the low 32 bit of the sigset. */
333 static inline void dump_sigact_irix5(struct sigact_irix5 *p)
335 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
336 (unsigned long) p->handler,
337 (unsigned long) p->sigset[0]);
342 irix_sigaction(int sig, const struct sigaction __user *act,
343 struct sigaction __user *oact, void __user *trampoline)
345 struct k_sigaction new_ka, old_ka;
349 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
350 (!old ? "0" : "OLD"), trampoline);
352 dump_sigact_irix5(new); printk(" ");
359 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
361 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
362 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
364 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
369 * Hmmm... methinks IRIX libc always passes a valid trampoline
370 * value for all invocations of sigaction. Will have to
371 * investigate. POSIX POSIX, die die die...
373 new_ka.sa_restorer = trampoline;
376 /* XXX Implement SIG_SETMASK32 for IRIX compatibility */
377 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
382 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
385 err = __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
386 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
387 err |= __copy_to_user(&oact->sa_mask, &old_ka.sa.sa_mask,
388 sizeof(sigset_t)) ? -EFAULT : 0;
396 asmlinkage int irix_sigpending(irix_sigset_t __user *set)
398 return do_sigpending(set, sizeof(*set));
401 asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
402 irix_sigset_t __user *old)
404 sigset_t oldbits, newbits;
407 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
409 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
411 sigdelsetmask(&newbits, ~_BLOCKABLE);
413 spin_lock_irq(¤t->sighand->siglock);
414 oldbits = current->blocked;
418 sigorsets(&newbits, &oldbits, &newbits);
422 sigandsets(&newbits, &oldbits, &newbits);
429 siginitset(&newbits, newbits.sig[0]);
433 spin_unlock_irq(¤t->sighand->siglock);
437 spin_unlock_irq(¤t->sighand->siglock);
440 return copy_to_user(old, ¤t->blocked,
441 sizeof(unsigned long)*4) ? -EFAULT : 0;
446 asmlinkage int irix_sigsuspend(struct pt_regs *regs)
449 sigset_t __user *uset;
451 uset = (sigset_t __user *) regs->regs[4];
452 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
454 sigdelsetmask(&newset, ~_BLOCKABLE);
456 spin_lock_irq(¤t->sighand->siglock);
457 current->saved_sigmask = current->blocked;
458 current->blocked = newset;
460 spin_unlock_irq(¤t->sighand->siglock);
462 current->state = TASK_INTERRUPTIBLE;
464 set_thread_flag(TIF_RESTORE_SIGMASK);
465 return -ERESTARTNOHAND;
468 /* hate hate hate... */
469 struct irix5_siginfo {
470 int sig, code, error;
472 char unused[128 - (3 * 4)]; /* Safety net. */
478 int utime, status, stime;
483 unsigned long fault_addr;
490 unsigned long sigval;
494 asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
495 struct irix5_siginfo __user *info, struct timespec __user *tp)
497 long expire = MAX_SCHEDULE_TIMEOUT;
499 int i, sig, error, timeo = 0;
503 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
504 current->comm, current->pid, set, info, tp);
507 /* Must always specify the signal set. */
511 if (copy_from_user(&kset, set, sizeof(set)))
514 if (info && clear_user(info, sizeof(*info))) {
520 if (copy_from_user(&ktp, tp, sizeof(*tp)))
523 if (!ktp.tv_sec && !ktp.tv_nsec)
526 expire = timespec_to_jiffies(&ktp) +
527 (ktp.tv_sec || ktp.tv_nsec);
533 expire = schedule_timeout_interruptible(expire);
535 for (i=0; i < _IRIX_NSIG_WORDS; i++)
536 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
544 if (signal_pending(current))
550 for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
551 if (sigismember (&kset, sig))
553 if (sigismember (¤t->pending.signal, sig)) {
554 /* XXX need more than this... */
556 return copy_to_user(&info->sig, &sig, sizeof(sig));
561 /* Should not get here, but do something sane if we do. */
568 /* This is here because of irix5_siginfo definition. */
570 #define IRIX_P_PGID 2
579 #define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
581 asmlinkage int irix_waitsys(int type, int upid,
582 struct irix5_siginfo __user *info, int options,
583 struct rusage __user *ru)
585 struct pid *pid = NULL;
587 DECLARE_WAITQUEUE(wait, current);
588 struct task_struct *tsk;
589 struct task_struct *p;
590 struct list_head *_p;
595 if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
599 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
602 if (options & ~W_MASK)
605 if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
608 if (type != IRIX_P_ALL)
609 pid = find_get_pid(upid);
610 add_wait_queue(¤t->signal->wait_chldexit, &wait);
613 current->state = TASK_INTERRUPTIBLE;
614 read_lock(&tasklist_lock);
616 list_for_each(_p, &tsk->children) {
617 p = list_entry(_p, struct task_struct, sibling);
618 if ((type == IRIX_P_PID) && task_pid(p) != pid)
620 if ((type == IRIX_P_PGID) && task_pgrp(p) != pid)
622 if ((p->exit_signal != SIGCHLD))
629 if (!(options & (W_TRAPPED|W_STOPPED)) &&
630 !(p->ptrace & PT_PTRACED))
632 read_unlock(&tasklist_lock);
634 /* move to end of parent's list to avoid starvation */
635 write_lock_irq(&tasklist_lock);
638 write_unlock_irq(&tasklist_lock);
639 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
643 retval = __put_user(SIGCHLD, &info->sig);
644 retval |= __put_user(0, &info->code);
645 retval |= __put_user(task_pid_vnr(p), &info->stuff.procinfo.pid);
646 retval |= __put_user((p->exit_code >> 8) & 0xff,
647 &info->stuff.procinfo.procdata.child.status);
648 retval |= __put_user(p->utime, &info->stuff.procinfo.procdata.child.utime);
649 retval |= __put_user(p->stime, &info->stuff.procinfo.procdata.child.stime);
657 current->signal->cutime += p->utime + p->signal->cutime;
658 current->signal->cstime += p->stime + p->signal->cstime;
660 getrusage(p, RUSAGE_BOTH, ru);
661 retval = __put_user(SIGCHLD, &info->sig);
662 retval |= __put_user(1, &info->code); /* CLD_EXITED */
663 retval |= __put_user(task_pid_vnr(p), &info->stuff.procinfo.pid);
664 retval |= __put_user((p->exit_code >> 8) & 0xff,
665 &info->stuff.procinfo.procdata.child.status);
666 retval |= __put_user(p->utime,
667 &info->stuff.procinfo.procdata.child.utime);
668 retval |= __put_user(p->stime,
669 &info->stuff.procinfo.procdata.child.stime);
673 if (p->real_parent != p->parent) {
674 write_lock_irq(&tasklist_lock);
676 p->parent = p->real_parent;
678 do_notify_parent(p, SIGCHLD);
679 write_unlock_irq(&tasklist_lock);
686 tsk = next_thread(tsk);
688 read_unlock(&tasklist_lock);
691 if (options & W_NOHANG)
693 retval = -ERESTARTSYS;
694 if (signal_pending(current))
696 current->state = TASK_INTERRUPTIBLE;
702 current->state = TASK_RUNNING;
703 remove_wait_queue(¤t->signal->wait_chldexit, &wait);
709 struct irix5_context {
713 struct { u32 sp, size, flags; } stack;
719 u32 weird_graphics_thing;
722 asmlinkage int irix_getcontext(struct pt_regs *regs)
724 int error, i, base = 0;
725 struct irix5_context __user *ctx;
728 if (regs->regs[2] == 1000)
730 ctx = (struct irix5_context __user *) regs->regs[base + 4];
733 printk("[%s:%d] irix_getcontext(%p)\n",
734 current->comm, current->pid, ctx);
737 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
740 error = __put_user(current->thread.irix_oldctx, &ctx->link);
742 error |= __copy_to_user(&ctx->sigmask, ¤t->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
744 /* XXX Do sigstack stuff someday... */
745 error |= __put_user(0, &ctx->stack.sp);
746 error |= __put_user(0, &ctx->stack.size);
747 error |= __put_user(0, &ctx->stack.flags);
749 error |= __put_user(0, &ctx->weird_graphics_thing);
750 error |= __put_user(0, &ctx->regs[0]);
751 for (i = 1; i < 32; i++)
752 error |= __put_user(regs->regs[i], &ctx->regs[i]);
753 error |= __put_user(regs->lo, &ctx->regs[32]);
754 error |= __put_user(regs->hi, &ctx->regs[33]);
755 error |= __put_user(regs->cp0_cause, &ctx->regs[34]);
756 error |= __put_user(regs->cp0_epc, &ctx->regs[35]);
763 printk("Wheee, no code for saving IRIX FPU context yet.\n");
765 error |= __put_user(flags, &ctx->flags);
770 asmlinkage void irix_setcontext(struct pt_regs *regs)
772 struct irix5_context __user *ctx;
776 if (regs->regs[2] == 1000)
778 ctx = (struct irix5_context __user *) regs->regs[base + 4];
781 printk("[%s:%d] irix_setcontext(%p)\n",
782 current->comm, current->pid, ctx);
785 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
788 err = __get_user(flags, &ctx->flags);
790 /* XXX sigstack garbage, todo... */
791 printk("Wheee, cannot do sigstack stuff in setcontext\n");
797 /* XXX extra control block stuff... todo... */
798 for (i = 1; i < 32; i++)
799 err |= __get_user(regs->regs[i], &ctx->regs[i]);
800 err |= __get_user(regs->lo, &ctx->regs[32]);
801 err |= __get_user(regs->hi, &ctx->regs[33]);
802 err |= __get_user(regs->cp0_epc, &ctx->regs[35]);
806 /* XXX fpu context, blah... */
807 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
809 err |= __get_user(current->thread.irix_oldctx, &ctx->link);
814 * Don't let your children do this ...
816 __asm__ __volatile__(
824 force_sigsegv(SIGSEGV, current);
827 struct irix_sigstack {
832 asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
833 struct irix_sigstack __user *old)
836 printk("[%s:%d] irix_sigstack(%p,%p)\n",
837 current->comm, current->pid, new, old);
840 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
845 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
852 struct irix_sigaltstack { unsigned long sp; int size; int status; };
854 asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
855 struct irix_sigaltstack __user *old)
858 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
859 current->comm, current->pid, new, old);
862 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
866 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
873 struct irix_procset {
874 int cmd, ltype, lid, rtype, rid;
877 asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
879 if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
882 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
883 current->comm, current->pid,
884 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,