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)))
32 u32 rmask, cp0_status;
36 u32 usedfp, fpcsr, fpeir, sstk_flags;
38 u64 cp0_cause, cp0_badvaddr, _unused0;
46 static inline void dump_irix5_sigctx(struct sigctx_irix5 *c)
50 printk("misc: rmask[%08lx] status[%08lx] pc[%08lx]\n",
51 (unsigned long) c->rmask,
52 (unsigned long) c->cp0_status,
53 (unsigned long) c->pc);
55 for(i = 0; i < 16; i++)
56 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
58 for(i = 16; i < 32; i++)
59 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
61 for(i = 0; i < 16; i++)
62 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
64 for(i = 16; i < 32; i++)
65 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
66 printk("misc: usedfp[%d] fpcsr[%08lx] fpeir[%08lx] stk_flgs[%08lx]\n",
67 (int) c->usedfp, (unsigned long) c->fpcsr,
68 (unsigned long) c->fpeir, (unsigned long) c->sstk_flags);
69 printk("misc: hi[%08lx] lo[%08lx] cause[%08lx] badvaddr[%08lx]\n",
70 (unsigned long) c->hi, (unsigned long) c->lo,
71 (unsigned long) c->cp0_cause, (unsigned long) c->cp0_badvaddr);
72 printk("misc: sigset<0>[%08lx] sigset<1>[%08lx] sigset<2>[%08lx] "
73 "sigset<3>[%08lx]\n", (unsigned long) c->sigset.sig[0],
74 (unsigned long) c->sigset.sig[1],
75 (unsigned long) c->sigset.sig[2],
76 (unsigned long) c->sigset.sig[3]);
80 static int setup_irix_frame(struct k_sigaction *ka, struct pt_regs *regs,
81 int signr, sigset_t *oldmask)
83 struct sigctx_irix5 __user *ctx;
88 sp -= sizeof(struct sigctx_irix5);
90 ctx = (struct sigctx_irix5 __user *) sp;
91 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
94 error = __put_user(0, &ctx->weird_fpu_thing);
95 error |= __put_user(~(0x00000001), &ctx->rmask);
96 error |= __put_user(0, &ctx->regs[0]);
97 for(i = 1; i < 32; i++)
98 error |= __put_user((u64) regs->regs[i], &ctx->regs[i]);
100 error |= __put_user((u64) regs->hi, &ctx->hi);
101 error |= __put_user((u64) regs->lo, &ctx->lo);
102 error |= __put_user((u64) regs->cp0_epc, &ctx->pc);
103 error |= __put_user(!!used_math(), &ctx->usedfp);
104 error |= __put_user((u64) regs->cp0_cause, &ctx->cp0_cause);
105 error |= __put_user((u64) regs->cp0_badvaddr, &ctx->cp0_badvaddr);
107 error |= __put_user(0, &ctx->sstk_flags); /* XXX sigstack unimp... todo... */
109 error |= __copy_to_user(&ctx->sigset, oldmask, sizeof(irix_sigset_t)) ? -EFAULT : 0;
115 dump_irix5_sigctx(ctx);
118 regs->regs[4] = (unsigned long) signr;
119 regs->regs[5] = 0; /* XXX sigcode XXX */
120 regs->regs[6] = regs->regs[29] = sp;
121 regs->regs[7] = (unsigned long) ka->sa.sa_handler;
122 regs->regs[25] = regs->cp0_epc = (unsigned long) ka->sa_restorer;
127 force_sigsegv(signr, current);
132 setup_irix_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
133 int signr, sigset_t *oldmask, siginfo_t *info)
135 printk("Aiee: setup_tr_frame wants to be written");
139 static inline int handle_signal(unsigned long sig, siginfo_t *info,
140 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs)
144 switch(regs->regs[0]) {
146 regs->regs[2] = EINTR;
149 if(!(ka->sa.sa_flags & SA_RESTART)) {
150 regs->regs[2] = EINTR;
154 case ERESTARTNOINTR: /* Userland will reload $v0. */
158 regs->regs[0] = 0; /* Don't deal with this again. */
160 if (ka->sa.sa_flags & SA_SIGINFO)
161 ret = setup_irix_rt_frame(ka, regs, sig, oldset, info);
163 ret = setup_irix_frame(ka, regs, sig, oldset);
165 spin_lock_irq(¤t->sighand->siglock);
166 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
167 if (!(ka->sa.sa_flags & SA_NODEFER))
168 sigaddset(¤t->blocked,sig);
170 spin_unlock_irq(¤t->sighand->siglock);
175 void do_irix_signal(struct pt_regs *regs)
177 struct k_sigaction ka;
183 * We want the common case to go fast, which is why we may in certain
184 * cases get here from kernel mode. Just return without doing anything
187 if (!user_mode(regs))
190 if (test_thread_flag(TIF_RESTORE_SIGMASK))
191 oldset = ¤t->saved_sigmask;
193 oldset = ¤t->blocked;
195 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
197 /* Whee! Actually deliver the signal. */
198 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
199 /* a signal was successfully delivered; the saved
200 * sigmask will have been stored in the signal frame,
201 * and will be restored by sigreturn, so we can simply
202 * clear the TIF_RESTORE_SIGMASK flag */
203 if (test_thread_flag(TIF_RESTORE_SIGMASK))
204 clear_thread_flag(TIF_RESTORE_SIGMASK);
211 * Who's code doesn't conform to the restartable syscall convention
212 * dies here!!! The li instruction, a single machine instruction,
213 * must directly be followed by the syscall instruction.
216 if (regs->regs[2] == ERESTARTNOHAND ||
217 regs->regs[2] == ERESTARTSYS ||
218 regs->regs[2] == ERESTARTNOINTR) {
221 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
222 regs->regs[2] = __NR_restart_syscall;
223 regs->regs[7] = regs->regs[26];
226 regs->regs[0] = 0; /* Don't deal with this again. */
230 * If there's no signal to deliver, we just put the saved sigmask
233 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
234 clear_thread_flag(TIF_RESTORE_SIGMASK);
235 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
240 irix_sigreturn(struct pt_regs *regs)
242 struct sigctx_irix5 __user *context, *magic;
243 unsigned long umask, mask;
246 int error, sig, i, base = 0;
249 /* Always make any pending restarted system calls return -EINTR */
250 current_thread_info()->restart_block.fn = do_no_restart_syscall;
252 if (regs->regs[2] == 1000)
255 context = (struct sigctx_irix5 __user *) regs->regs[base + 4];
256 magic = (struct sigctx_irix5 __user *) regs->regs[base + 5];
257 sig = (int) regs->regs[base + 6];
259 printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
260 current->comm, current->pid, context, magic, sig);
264 if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
268 dump_irix5_sigctx(context);
271 error = __get_user(regs->cp0_epc, &context->pc);
272 error |= __get_user(umask, &context->rmask);
275 for (i = 1; i < 32; i++, mask <<= 1) {
277 error |= __get_user(regs->regs[i], &context->regs[i]);
279 error |= __get_user(regs->hi, &context->hi);
280 error |= __get_user(regs->lo, &context->lo);
282 error |= __get_user(usedfp, &context->usedfp);
283 if ((umask & 1) && usedfp) {
284 fregs = (u64 *) ¤t->thread.fpu;
286 for(i = 0; i < 32; i++)
287 error |= __get_user(fregs[i], &context->fpregs[i]);
288 error |= __get_user(current->thread.fpu.fcr31, &context->fpcsr);
291 /* XXX do sigstack crapola here... XXX */
293 error |= __copy_from_user(&blocked, &context->sigset, sizeof(blocked)) ? -EFAULT : 0;
298 sigdelsetmask(&blocked, ~_BLOCKABLE);
299 spin_lock_irq(¤t->sighand->siglock);
300 current->blocked = blocked;
302 spin_unlock_irq(¤t->sighand->siglock);
305 * Don't let your children do this ...
307 __asm__ __volatile__(
315 force_sig(SIGSEGV, current);
318 struct sigact_irix5 {
320 void (*handler)(int);
325 #define SIG_SETMASK32 256 /* Goodie from SGI for BSD compatibility:
326 set only the low 32 bit of the sigset. */
329 static inline void dump_sigact_irix5(struct sigact_irix5 *p)
331 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
332 (unsigned long) p->handler,
333 (unsigned long) p->sigset[0]);
338 irix_sigaction(int sig, const struct sigaction __user *act,
339 struct sigaction __user *oact, void __user *trampoline)
341 struct k_sigaction new_ka, old_ka;
345 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
346 (!old ? "0" : "OLD"), trampoline);
348 dump_sigact_irix5(new); printk(" ");
355 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
357 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
358 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
360 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
365 * Hmmm... methinks IRIX libc always passes a valid trampoline
366 * value for all invocations of sigaction. Will have to
367 * investigate. POSIX POSIX, die die die...
369 new_ka.sa_restorer = trampoline;
372 /* XXX Implement SIG_SETMASK32 for IRIX compatibility */
373 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
378 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
381 err = __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
382 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
383 err |= __copy_to_user(&oact->sa_mask, &old_ka.sa.sa_mask,
384 sizeof(sigset_t)) ? -EFAULT : 0;
392 asmlinkage int irix_sigpending(irix_sigset_t __user *set)
394 return do_sigpending(set, sizeof(*set));
397 asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
398 irix_sigset_t __user *old)
400 sigset_t oldbits, newbits;
403 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
405 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
407 sigdelsetmask(&newbits, ~_BLOCKABLE);
409 spin_lock_irq(¤t->sighand->siglock);
410 oldbits = current->blocked;
414 sigorsets(&newbits, &oldbits, &newbits);
418 sigandsets(&newbits, &oldbits, &newbits);
425 siginitset(&newbits, newbits.sig[0]);
432 spin_unlock_irq(¤t->sighand->siglock);
435 return copy_to_user(old, ¤t->blocked,
436 sizeof(unsigned long)*4) ? -EFAULT : 0;
441 asmlinkage int irix_sigsuspend(struct pt_regs *regs)
444 sigset_t __user *uset;
446 uset = (sigset_t __user *) regs->regs[4];
447 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
449 sigdelsetmask(&newset, ~_BLOCKABLE);
451 spin_lock_irq(¤t->sighand->siglock);
452 current->saved_sigmask = current->blocked;
453 current->blocked = newset;
455 spin_unlock_irq(¤t->sighand->siglock);
457 current->state = TASK_INTERRUPTIBLE;
459 set_thread_flag(TIF_RESTORE_SIGMASK);
460 return -ERESTARTNOHAND;
463 /* hate hate hate... */
464 struct irix5_siginfo {
465 int sig, code, error;
467 char unused[128 - (3 * 4)]; /* Safety net. */
473 int utime, status, stime;
478 unsigned long fault_addr;
485 unsigned long sigval;
489 asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
490 struct irix5_siginfo __user *info, struct timespec __user *tp)
492 long expire = MAX_SCHEDULE_TIMEOUT;
494 int i, sig, error, timeo = 0;
498 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
499 current->comm, current->pid, set, info, tp);
502 /* Must always specify the signal set. */
506 if (copy_from_user(&kset, set, sizeof(set)))
509 if (info && clear_user(info, sizeof(*info))) {
515 if (copy_from_user(&ktp, tp, sizeof(*tp)))
518 if (!ktp.tv_sec && !ktp.tv_nsec)
521 expire = timespec_to_jiffies(&ktp) +
522 (ktp.tv_sec || ktp.tv_nsec);
528 expire = schedule_timeout_interruptible(expire);
531 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
539 if (signal_pending(current))
545 for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
546 if (sigismember (&kset, sig))
548 if (sigismember (¤t->pending.signal, sig)) {
549 /* XXX need more than this... */
551 return copy_to_user(&info->sig, &sig, sizeof(sig));
556 /* Should not get here, but do something sane if we do. */
563 /* This is here because of irix5_siginfo definition. */
565 #define IRIX_P_PGID 2
574 #define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
576 asmlinkage int irix_waitsys(int type, int pid,
577 struct irix5_siginfo __user *info, int options,
578 struct rusage __user *ru)
581 DECLARE_WAITQUEUE(wait, current);
582 struct task_struct *tsk;
583 struct task_struct *p;
584 struct list_head *_p;
589 if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
593 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
596 if (options & ~W_MASK)
599 if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
602 add_wait_queue(¤t->signal->wait_chldexit, &wait);
605 current->state = TASK_INTERRUPTIBLE;
606 read_lock(&tasklist_lock);
608 list_for_each(_p,&tsk->children) {
609 p = list_entry(_p,struct task_struct,sibling);
610 if ((type == IRIX_P_PID) && p->pid != pid)
612 if ((type == IRIX_P_PGID) && process_group(p) != pid)
614 if ((p->exit_signal != SIGCHLD))
621 if (!(options & (W_TRAPPED|W_STOPPED)) &&
622 !(p->ptrace & PT_PTRACED))
624 read_unlock(&tasklist_lock);
626 /* move to end of parent's list to avoid starvation */
627 write_lock_irq(&tasklist_lock);
630 write_unlock_irq(&tasklist_lock);
631 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
635 retval = __put_user(SIGCHLD, &info->sig);
636 retval |= __put_user(0, &info->code);
637 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
638 retval |= __put_user((p->exit_code >> 8) & 0xff,
639 &info->stuff.procinfo.procdata.child.status);
640 retval |= __put_user(p->utime, &info->stuff.procinfo.procdata.child.utime);
641 retval |= __put_user(p->stime, &info->stuff.procinfo.procdata.child.stime);
649 current->signal->cutime += p->utime + p->signal->cutime;
650 current->signal->cstime += p->stime + p->signal->cstime;
652 getrusage(p, RUSAGE_BOTH, ru);
653 retval = __put_user(SIGCHLD, &info->sig);
654 retval |= __put_user(1, &info->code); /* CLD_EXITED */
655 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
656 retval |= __put_user((p->exit_code >> 8) & 0xff,
657 &info->stuff.procinfo.procdata.child.status);
658 retval |= __put_user(p->utime,
659 &info->stuff.procinfo.procdata.child.utime);
660 retval |= __put_user(p->stime,
661 &info->stuff.procinfo.procdata.child.stime);
665 if (p->real_parent != p->parent) {
666 write_lock_irq(&tasklist_lock);
668 p->parent = p->real_parent;
670 do_notify_parent(p, SIGCHLD);
671 write_unlock_irq(&tasklist_lock);
678 tsk = next_thread(tsk);
680 read_unlock(&tasklist_lock);
683 if (options & W_NOHANG)
685 retval = -ERESTARTSYS;
686 if (signal_pending(current))
688 current->state = TASK_INTERRUPTIBLE;
694 current->state = TASK_RUNNING;
695 remove_wait_queue(¤t->signal->wait_chldexit, &wait);
700 struct irix5_context {
704 struct { u32 sp, size, flags; } stack;
710 u32 weird_graphics_thing;
713 asmlinkage int irix_getcontext(struct pt_regs *regs)
715 int error, i, base = 0;
716 struct irix5_context __user *ctx;
719 if (regs->regs[2] == 1000)
721 ctx = (struct irix5_context __user *) regs->regs[base + 4];
724 printk("[%s:%d] irix_getcontext(%p)\n",
725 current->comm, current->pid, ctx);
728 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)));
731 error = __put_user(current->thread.irix_oldctx, &ctx->link);
733 error |= __copy_to_user(&ctx->sigmask, ¤t->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
735 /* XXX Do sigstack stuff someday... */
736 error |= __put_user(0, &ctx->stack.sp);
737 error |= __put_user(0, &ctx->stack.size);
738 error |= __put_user(0, &ctx->stack.flags);
740 error |= __put_user(0, &ctx->weird_graphics_thing);
741 error |= __put_user(0, &ctx->regs[0]);
742 for (i = 1; i < 32; i++)
743 error |= __put_user(regs->regs[i], &ctx->regs[i]);
744 error |= __put_user(regs->lo, &ctx->regs[32]);
745 error |= __put_user(regs->hi, &ctx->regs[33]);
746 error |= __put_user(regs->cp0_cause, &ctx->regs[34]);
747 error |= __put_user(regs->cp0_epc, &ctx->regs[35]);
754 printk("Wheee, no code for saving IRIX FPU context yet.\n");
756 error |= __put_user(flags, &ctx->flags);
761 asmlinkage void irix_setcontext(struct pt_regs *regs)
763 struct irix5_context __user *ctx;
767 if (regs->regs[2] == 1000)
769 ctx = (struct irix5_context __user *) regs->regs[base + 4];
772 printk("[%s:%d] irix_setcontext(%p)\n",
773 current->comm, current->pid, ctx);
776 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
779 err = __get_user(flags, &ctx->flags);
781 /* XXX sigstack garbage, todo... */
782 printk("Wheee, cannot do sigstack stuff in setcontext\n");
788 /* XXX extra control block stuff... todo... */
789 for (i = 1; i < 32; i++)
790 err |= __get_user(regs->regs[i], &ctx->regs[i]);
791 err |= __get_user(regs->lo, &ctx->regs[32]);
792 err |= __get_user(regs->hi, &ctx->regs[33]);
793 err |= __get_user(regs->cp0_epc, &ctx->regs[35]);
797 /* XXX fpu context, blah... */
798 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
800 err |= __get_user(current->thread.irix_oldctx, &ctx->link);
805 * Don't let your children do this ...
807 __asm__ __volatile__(
815 force_sigsegv(SIGSEGV, current);
818 struct irix_sigstack {
823 asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
824 struct irix_sigstack __user *old)
827 printk("[%s:%d] irix_sigstack(%p,%p)\n",
828 current->comm, current->pid, new, old);
831 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
836 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
843 struct irix_sigaltstack { unsigned long sp; int size; int status; };
845 asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
846 struct irix_sigaltstack __user *old)
849 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
850 current->comm, current->pid, new, old);
853 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
857 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
864 struct irix_procset {
865 int cmd, ltype, lid, rtype, rid;
868 asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
870 if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
873 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
874 current->comm, current->pid,
875 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,