2 * irixsig.c: WHEEE, IRIX signals! YOW, am I compatable 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>
16 #include <asm/ptrace.h>
17 #include <asm/uaccess.h>
19 extern asmlinkage void syscall_trace(void);
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 void setup_irix_frame(struct k_sigaction *ka, struct pt_regs *regs,
81 int signr, sigset_t *oldmask)
84 struct sigctx_irix5 *ctx;
88 sp -= sizeof(struct sigctx_irix5);
90 ctx = (struct sigctx_irix5 *) sp;
91 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
94 __put_user(0, &ctx->weird_fpu_thing);
95 __put_user(~(0x00000001), &ctx->rmask);
96 __put_user(0, &ctx->regs[0]);
97 for(i = 1; i < 32; i++)
98 __put_user((u64) regs->regs[i], &ctx->regs[i]);
100 __put_user((u64) regs->hi, &ctx->hi);
101 __put_user((u64) regs->lo, &ctx->lo);
102 __put_user((u64) regs->cp0_epc, &ctx->pc);
103 __put_user(current->used_math, &ctx->usedfp);
104 __put_user((u64) regs->cp0_cause, &ctx->cp0_cause);
105 __put_user((u64) regs->cp0_badvaddr, &ctx->cp0_badvaddr);
107 __put_user(0, &ctx->sstk_flags); /* XXX sigstack unimp... todo... */
109 __copy_to_user(&ctx->sigset, oldmask, sizeof(irix_sigset_t));
112 dump_irix5_sigctx(ctx);
115 regs->regs[4] = (unsigned long) signr;
116 regs->regs[5] = 0; /* XXX sigcode XXX */
117 regs->regs[6] = regs->regs[29] = sp;
118 regs->regs[7] = (unsigned long) ka->sa.sa_handler;
119 regs->regs[25] = regs->cp0_epc = (unsigned long) ka->sa_restorer;
124 if (signr == SIGSEGV)
125 ka->sa.sa_handler = SIG_DFL;
126 force_sig(SIGSEGV, current);
130 setup_irix_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
131 int signr, sigset_t *oldmask, siginfo_t *info)
133 printk("Aiee: setup_tr_frame wants to be written");
137 static inline void handle_signal(unsigned long sig, struct k_sigaction *ka,
138 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
140 if (ka->sa.sa_flags & SA_SIGINFO)
141 setup_irix_rt_frame(ka, regs, sig, oldset, info);
143 setup_irix_frame(ka, regs, sig, oldset);
145 if (ka->sa.sa_flags & SA_ONESHOT)
146 ka->sa.sa_handler = SIG_DFL;
147 if (!(ka->sa.sa_flags & SA_NODEFER)) {
148 spin_lock_irq(¤t->sigmask_lock);
149 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
150 sigaddset(¤t->blocked,sig);
151 recalc_sigpending(current);
152 spin_unlock_irq(¤t->sigmask_lock);
156 static inline void syscall_restart(struct pt_regs *regs, struct k_sigaction *ka)
158 switch(regs->regs[0]) {
160 regs->regs[2] = EINTR;
163 if(!(ka->sa.sa_flags & SA_RESTART)) {
164 regs->regs[2] = EINTR;
168 case ERESTARTNOINTR: /* Userland will reload $v0. */
172 regs->regs[0] = 0; /* Don't deal with this again. */
175 asmlinkage int do_irix_signal(sigset_t *oldset, struct pt_regs *regs)
177 struct k_sigaction *ka;
181 oldset = ¤t->blocked;
186 spin_lock_irq(¤t->sigmask_lock);
187 signr = dequeue_signal(¤t->blocked, &info);
188 spin_unlock_irq(¤t->sigmask_lock);
193 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
194 /* Let the debugger run. */
195 current->exit_code = signr;
196 current->state = TASK_STOPPED;
197 notify_parent(current, SIGCHLD);
200 /* We're back. Did the debugger cancel the sig? */
201 if (!(signr = current->exit_code))
203 current->exit_code = 0;
205 /* The debugger continued. Ignore SIGSTOP. */
206 if (signr == SIGSTOP)
209 /* Update the siginfo structure. Is this good? */
210 if (signr != info.si_signo) {
211 info.si_signo = signr;
213 info.si_code = SI_USER;
214 info.si_pid = current->p_pptr->pid;
215 info.si_uid = current->p_pptr->uid;
218 /* If the (new) signal is now blocked, requeue it. */
219 if (sigismember(¤t->blocked, signr)) {
220 send_sig_info(signr, &info, current);
225 ka = ¤t->sig->action[signr-1];
226 if (ka->sa.sa_handler == SIG_IGN) {
227 if (signr != SIGCHLD)
229 /* Check for SIGCHLD: it's special. */
230 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
235 if (ka->sa.sa_handler == SIG_DFL) {
236 int exit_code = signr;
238 /* Init gets no signals it doesn't want. */
239 if (current->pid == 1)
243 case SIGCONT: case SIGCHLD: case SIGWINCH:
246 case SIGTSTP: case SIGTTIN: case SIGTTOU:
247 if (is_orphaned_pgrp(current->pgrp))
252 current->state = TASK_STOPPED;
253 current->exit_code = signr;
254 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
255 notify_parent(current, SIGCHLD);
259 case SIGQUIT: case SIGILL: case SIGTRAP:
260 case SIGABRT: case SIGFPE: case SIGSEGV:
261 if (do_coredump(signr, regs))
266 sigaddset(¤t->pending.signal, signr);
267 recalc_sigpending(current);
268 current->flags |= PF_SIGNALED;
275 syscall_restart(regs, ka);
276 /* Whee! Actually deliver the signal. */
277 handle_signal(signr, ka, &info, oldset, regs);
282 * Who's code doesn't conform to the restartable syscall convention
283 * dies here!!! The li instruction, a single machine instruction,
284 * must directly be followed by the syscall instruction.
287 if (regs->regs[2] == ERESTARTNOHAND ||
288 regs->regs[2] == ERESTARTSYS ||
289 regs->regs[2] == ERESTARTNOINTR) {
297 irix_sigreturn(struct pt_regs *regs)
299 struct sigctx_irix5 *context, *magic;
300 unsigned long umask, mask;
302 int sig, i, base = 0;
305 if(regs->regs[2] == 1000)
308 context = (struct sigctx_irix5 *) regs->regs[base + 4];
309 magic = (struct sigctx_irix5 *) regs->regs[base + 5];
310 sig = (int) regs->regs[base + 6];
312 printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
313 current->comm, current->pid, context, magic, sig);
317 if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
321 dump_irix5_sigctx(context);
324 __get_user(regs->cp0_epc, &context->pc);
325 umask = context->rmask; mask = 2;
326 for (i = 1; i < 32; i++, mask <<= 1) {
328 __get_user(regs->regs[i], &context->regs[i]);
330 __get_user(regs->hi, &context->hi);
331 __get_user(regs->lo, &context->lo);
333 if ((umask & 1) && context->usedfp) {
334 fregs = (u64 *) ¤t->thread.fpu;
335 for(i = 0; i < 32; i++)
336 fregs[i] = (u64) context->fpregs[i];
337 __get_user(current->thread.fpu.hard.control, &context->fpcsr);
340 /* XXX do sigstack crapola here... XXX */
342 if (__copy_from_user(&blocked, &context->sigset, sizeof(blocked)))
345 sigdelsetmask(&blocked, ~_BLOCKABLE);
346 spin_lock_irq(¤t->sigmask_lock);
347 current->blocked = blocked;
348 recalc_sigpending(current);
349 spin_unlock_irq(¤t->sigmask_lock);
352 * Don't let your children do this ...
354 if (current->ptrace & PT_TRACESYS)
356 __asm__ __volatile__(
358 "j\tret_from_sys_call"
364 force_sig(SIGSEGV, current);
367 struct sigact_irix5 {
369 void (*handler)(int);
375 static inline void dump_sigact_irix5(struct sigact_irix5 *p)
377 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
378 (unsigned long) p->handler,
379 (unsigned long) p->sigset[0]);
384 irix_sigaction(int sig, const struct sigaction *act,
385 struct sigaction *oact, void *trampoline)
387 struct k_sigaction new_ka, old_ka;
391 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
392 (!old ? "0" : "OLD"), trampoline);
394 dump_sigact_irix5(new); printk(" ");
399 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
400 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
401 __get_user(new_ka.sa.sa_flags, &act->sa_flags))
404 __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t));
407 * Hmmm... methinks IRIX libc always passes a valid trampoline
408 * value for all invocations of sigaction. Will have to
409 * investigate. POSIX POSIX, die die die...
411 new_ka.sa_restorer = trampoline;
414 /* XXX Implement SIG_SETMASK32 for IRIX compatibility */
415 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
418 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
419 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
420 __put_user(old_ka.sa.sa_flags, &oact->sa_flags))
422 __copy_to_user(&old_ka.sa.sa_mask, &oact->sa_mask,
429 asmlinkage int irix_sigpending(irix_sigset_t *set)
431 return do_sigpending(set, sizeof(*set));
434 asmlinkage int irix_sigprocmask(int how, irix_sigset_t *new, irix_sigset_t *old)
436 sigset_t oldbits, newbits;
440 error = verify_area(VERIFY_READ, new, sizeof(*new));
443 __copy_from_user(&newbits, new, sizeof(unsigned long)*4);
444 sigdelsetmask(&newbits, ~_BLOCKABLE);
446 spin_lock_irq(¤t->sigmask_lock);
447 oldbits = current->blocked;
451 sigorsets(&newbits, &oldbits, &newbits);
455 sigandsets(&newbits, &oldbits, &newbits);
462 siginitset(&newbits, newbits.sig[0]);
468 recalc_sigpending(current);
469 spin_unlock_irq(¤t->sigmask_lock);
472 error = verify_area(VERIFY_WRITE, old, sizeof(*old));
475 __copy_to_user(old, ¤t->blocked, sizeof(unsigned long)*4);
481 asmlinkage int irix_sigsuspend(struct pt_regs *regs)
483 sigset_t *uset, saveset, newset;
485 uset = (sigset_t *) regs->regs[4];
486 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
488 sigdelsetmask(&newset, ~_BLOCKABLE);
490 spin_lock_irq(¤t->sigmask_lock);
491 saveset = current->blocked;
492 current->blocked = newset;
493 recalc_sigpending(current);
494 spin_unlock_irq(¤t->sigmask_lock);
496 regs->regs[2] = -EINTR;
498 current->state = TASK_INTERRUPTIBLE;
500 if (do_irix_signal(&saveset, regs))
505 /* hate hate hate... */
506 struct irix5_siginfo {
507 int sig, code, error;
509 char unused[128 - (3 * 4)]; /* Safety net. */
515 int utime, status, stime;
520 unsigned long fault_addr;
527 unsigned long sigval;
531 static inline unsigned long timespectojiffies(struct timespec *value)
533 unsigned long sec = (unsigned) value->tv_sec;
534 long nsec = value->tv_nsec;
536 if (sec > (LONG_MAX / HZ))
538 nsec += 1000000000L / HZ - 1;
539 nsec /= 1000000000L / HZ;
540 return HZ * sec + nsec;
543 asmlinkage int irix_sigpoll_sys(unsigned long *set, struct irix5_siginfo *info,
546 long expire = MAX_SCHEDULE_TIMEOUT;
548 int i, sig, error, timeo = 0;
551 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
552 current->comm, current->pid, set, info, tp);
555 /* Must always specify the signal set. */
559 error = verify_area(VERIFY_READ, set, sizeof(kset));
563 __copy_from_user(&kset, set, sizeof(set));
567 if (info && clear_user(info, sizeof(*info))) {
573 error = verify_area(VERIFY_READ, tp, sizeof(*tp));
576 if(!tp->tv_sec && !tp->tv_nsec) {
580 expire = timespectojiffies(tp)+(tp->tv_sec||tp->tv_nsec);
586 current->state = TASK_INTERRUPTIBLE;
587 expire = schedule_timeout(expire);
590 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
598 if (signal_pending(current))
604 for(sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
605 if (sigismember (&kset, sig))
607 if (sigismember (¤t->pending.signal, sig)) {
608 /* XXX need more than this... */
616 /* Should not get here, but do something sane if we do. */
623 /* This is here because of irix5_siginfo definition. */
628 extern int getrusage(struct task_struct *, int, struct rusage *);
636 #define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
638 asmlinkage int irix_waitsys(int type, int pid, struct irix5_siginfo *info,
639 int options, struct rusage *ru)
642 DECLARE_WAITQUEUE(wait, current);
643 struct task_struct *p;
649 retval = verify_area(VERIFY_WRITE, info, sizeof(*info));
653 retval = verify_area(VERIFY_WRITE, ru, sizeof(*ru));
657 if (options & ~(W_MASK)) {
661 if (type != P_PID && type != P_PGID && type != P_ALL) {
665 add_wait_queue(¤t->wait_chldexit, &wait);
668 current->state = TASK_INTERRUPTIBLE;
669 read_lock(&tasklist_lock);
670 for (p = current->p_cptr; p; p = p->p_osptr) {
671 if ((type == P_PID) && p->pid != pid)
673 if ((type == P_PGID) && p->pgrp != pid)
675 if ((p->exit_signal != SIGCHLD))
682 if (!(options & (W_TRAPPED|W_STOPPED)) &&
683 !(p->ptrace & PT_PTRACED))
685 read_unlock(&tasklist_lock);
687 getrusage(p, RUSAGE_BOTH, ru);
688 __put_user(SIGCHLD, &info->sig);
689 __put_user(0, &info->code);
690 __put_user(p->pid, &info->stuff.procinfo.pid);
691 __put_user((p->exit_code >> 8) & 0xff,
692 &info->stuff.procinfo.procdata.child.status);
693 __put_user(p->times.tms_utime, &info->stuff.procinfo.procdata.child.utime);
694 __put_user(p->times.tms_stime, &info->stuff.procinfo.procdata.child.stime);
699 current->times.tms_cutime += p->times.tms_utime + p->times.tms_cutime;
700 current->times.tms_cstime += p->times.tms_stime + p->times.tms_cstime;
701 read_unlock(&tasklist_lock);
703 getrusage(p, RUSAGE_BOTH, ru);
704 __put_user(SIGCHLD, &info->sig);
705 __put_user(1, &info->code); /* CLD_EXITED */
706 __put_user(p->pid, &info->stuff.procinfo.pid);
707 __put_user((p->exit_code >> 8) & 0xff,
708 &info->stuff.procinfo.procdata.child.status);
709 __put_user(p->times.tms_utime,
710 &info->stuff.procinfo.procdata.child.utime);
711 __put_user(p->times.tms_stime,
712 &info->stuff.procinfo.procdata.child.stime);
714 if (p->p_opptr != p->p_pptr) {
715 write_lock_irq(&tasklist_lock);
717 p->p_pptr = p->p_opptr;
719 notify_parent(p, SIGCHLD);
720 write_unlock_irq(&tasklist_lock);
728 read_unlock(&tasklist_lock);
731 if (options & W_NOHANG)
733 retval = -ERESTARTSYS;
734 if (signal_pending(current))
736 current->state = TASK_INTERRUPTIBLE;
742 current->state = TASK_RUNNING;
743 remove_wait_queue(¤t->wait_chldexit, &wait);
749 struct irix5_context {
753 struct { u32 sp, size, flags; } stack;
759 u32 weird_graphics_thing;
762 asmlinkage int irix_getcontext(struct pt_regs *regs)
764 int error, i, base = 0;
765 struct irix5_context *ctx;
768 if (regs->regs[2] == 1000)
770 ctx = (struct irix5_context *) regs->regs[base + 4];
773 printk("[%s:%d] irix_getcontext(%p)\n",
774 current->comm, current->pid, ctx);
777 error = verify_area(VERIFY_WRITE, ctx, sizeof(*ctx));
780 __put_user(current->thread.irix_oldctx, &ctx->link);
782 __copy_to_user(&ctx->sigmask, ¤t->blocked, sizeof(irix_sigset_t));
784 /* XXX Do sigstack stuff someday... */
785 __put_user(0, &ctx->stack.sp);
786 __put_user(0, &ctx->stack.size);
787 __put_user(0, &ctx->stack.flags);
789 __put_user(0, &ctx->weird_graphics_thing);
790 __put_user(0, &ctx->regs[0]);
791 for (i = 1; i < 32; i++)
792 __put_user(regs->regs[i], &ctx->regs[i]);
793 __put_user(regs->lo, &ctx->regs[32]);
794 __put_user(regs->hi, &ctx->regs[33]);
795 __put_user(regs->cp0_cause, &ctx->regs[34]);
796 __put_user(regs->cp0_epc, &ctx->regs[35]);
799 if(!current->used_math) {
803 printk("Wheee, no code for saving IRIX FPU context yet.\n");
805 __put_user(flags, &ctx->flags);
812 asmlinkage unsigned long irix_setcontext(struct pt_regs *regs)
815 struct irix5_context *ctx;
817 if(regs->regs[2] == 1000)
819 ctx = (struct irix5_context *) regs->regs[base + 4];
822 printk("[%s:%d] irix_setcontext(%p)\n",
823 current->comm, current->pid, ctx);
826 error = verify_area(VERIFY_READ, ctx, sizeof(*ctx));
830 if (ctx->flags & 0x02) {
831 /* XXX sigstack garbage, todo... */
832 printk("Wheee, cannot do sigstack stuff in setcontext\n");
835 if (ctx->flags & 0x04) {
838 /* XXX extra control block stuff... todo... */
839 for(i = 1; i < 32; i++)
840 regs->regs[i] = ctx->regs[i];
841 regs->lo = ctx->regs[32];
842 regs->hi = ctx->regs[33];
843 regs->cp0_epc = ctx->regs[35];
846 if (ctx->flags & 0x08) {
847 /* XXX fpu context, blah... */
848 printk("Wheee, cannot restore FPU context yet...\n");
850 current->thread.irix_oldctx = ctx->link;
851 error = regs->regs[2];
857 struct irix_sigstack { unsigned long sp; int status; };
859 asmlinkage int irix_sigstack(struct irix_sigstack *new, struct irix_sigstack *old)
864 printk("[%s:%d] irix_sigstack(%p,%p)\n",
865 current->comm, current->pid, new, old);
868 error = verify_area(VERIFY_READ, new, sizeof(*new));
874 error = verify_area(VERIFY_WRITE, old, sizeof(*old));
884 struct irix_sigaltstack { unsigned long sp; int size; int status; };
886 asmlinkage int irix_sigaltstack(struct irix_sigaltstack *new,
887 struct irix_sigaltstack *old)
892 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
893 current->comm, current->pid, new, old);
896 error = verify_area(VERIFY_READ, new, sizeof(*new));
902 error = verify_area(VERIFY_WRITE, old, sizeof(*old));
914 struct irix_procset {
915 int cmd, ltype, lid, rtype, rid;
918 asmlinkage int irix_sigsendset(struct irix_procset *pset, int sig)
922 error = verify_area(VERIFY_READ, pset, sizeof(*pset));
926 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
927 current->comm, current->pid,
928 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,