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);
326 #define SIG_SETMASK32 256 /* Goodie from SGI for BSD compatibility:
327 set only the low 32 bit of the sigset. */
330 static inline void dump_sigact_irix5(struct sigact_irix5 *p)
332 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
333 (unsigned long) p->handler,
334 (unsigned long) p->sigset[0]);
339 irix_sigaction(int sig, const struct sigaction __user *act,
340 struct sigaction __user *oact, void __user *trampoline)
342 struct k_sigaction new_ka, old_ka;
346 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
347 (!old ? "0" : "OLD"), trampoline);
349 dump_sigact_irix5(new); printk(" ");
356 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
358 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
359 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
361 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
366 * Hmmm... methinks IRIX libc always passes a valid trampoline
367 * value for all invocations of sigaction. Will have to
368 * investigate. POSIX POSIX, die die die...
370 new_ka.sa_restorer = trampoline;
373 /* XXX Implement SIG_SETMASK32 for IRIX compatibility */
374 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
379 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
382 err = __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
383 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
384 err |= __copy_to_user(&oact->sa_mask, &old_ka.sa.sa_mask,
385 sizeof(sigset_t)) ? -EFAULT : 0;
393 asmlinkage int irix_sigpending(irix_sigset_t __user *set)
395 return do_sigpending(set, sizeof(*set));
398 asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
399 irix_sigset_t __user *old)
401 sigset_t oldbits, newbits;
404 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
406 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
408 sigdelsetmask(&newbits, ~_BLOCKABLE);
410 spin_lock_irq(¤t->sighand->siglock);
411 oldbits = current->blocked;
415 sigorsets(&newbits, &oldbits, &newbits);
419 sigandsets(&newbits, &oldbits, &newbits);
426 siginitset(&newbits, newbits.sig[0]);
433 spin_unlock_irq(¤t->sighand->siglock);
436 return copy_to_user(old, ¤t->blocked,
437 sizeof(unsigned long)*4) ? -EFAULT : 0;
442 asmlinkage int irix_sigsuspend(struct pt_regs *regs)
445 sigset_t __user *uset;
447 uset = (sigset_t __user *) regs->regs[4];
448 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
450 sigdelsetmask(&newset, ~_BLOCKABLE);
452 spin_lock_irq(¤t->sighand->siglock);
453 current->saved_sigmask = current->blocked;
454 current->blocked = newset;
456 spin_unlock_irq(¤t->sighand->siglock);
458 current->state = TASK_INTERRUPTIBLE;
460 set_thread_flag(TIF_RESTORE_SIGMASK);
461 return -ERESTARTNOHAND;
464 /* hate hate hate... */
465 struct irix5_siginfo {
466 int sig, code, error;
468 char unused[128 - (3 * 4)]; /* Safety net. */
474 int utime, status, stime;
479 unsigned long fault_addr;
486 unsigned long sigval;
490 asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
491 struct irix5_siginfo __user *info, struct timespec __user *tp)
493 long expire = MAX_SCHEDULE_TIMEOUT;
495 int i, sig, error, timeo = 0;
499 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
500 current->comm, current->pid, set, info, tp);
503 /* Must always specify the signal set. */
507 if (copy_from_user(&kset, set, sizeof(set)))
510 if (info && clear_user(info, sizeof(*info))) {
516 if (copy_from_user(&ktp, tp, sizeof(*tp)))
519 if (!ktp.tv_sec && !ktp.tv_nsec)
522 expire = timespec_to_jiffies(&ktp) +
523 (ktp.tv_sec || ktp.tv_nsec);
529 expire = schedule_timeout_interruptible(expire);
532 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
540 if (signal_pending(current))
546 for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
547 if (sigismember (&kset, sig))
549 if (sigismember (¤t->pending.signal, sig)) {
550 /* XXX need more than this... */
552 return copy_to_user(&info->sig, &sig, sizeof(sig));
557 /* Should not get here, but do something sane if we do. */
564 /* This is here because of irix5_siginfo definition. */
566 #define IRIX_P_PGID 2
575 #define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
577 asmlinkage int irix_waitsys(int type, int pid,
578 struct irix5_siginfo __user *info, int options,
579 struct rusage __user *ru)
582 DECLARE_WAITQUEUE(wait, current);
583 struct task_struct *tsk;
584 struct task_struct *p;
585 struct list_head *_p;
590 if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
594 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
597 if (options & ~W_MASK)
600 if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
603 add_wait_queue(¤t->signal->wait_chldexit, &wait);
606 current->state = TASK_INTERRUPTIBLE;
607 read_lock(&tasklist_lock);
609 list_for_each(_p,&tsk->children) {
610 p = list_entry(_p,struct task_struct,sibling);
611 if ((type == IRIX_P_PID) && p->pid != pid)
613 if ((type == IRIX_P_PGID) && process_group(p) != pid)
615 if ((p->exit_signal != SIGCHLD))
622 if (!(options & (W_TRAPPED|W_STOPPED)) &&
623 !(p->ptrace & PT_PTRACED))
625 read_unlock(&tasklist_lock);
627 /* move to end of parent's list to avoid starvation */
628 write_lock_irq(&tasklist_lock);
631 write_unlock_irq(&tasklist_lock);
632 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
636 retval = __put_user(SIGCHLD, &info->sig);
637 retval |= __put_user(0, &info->code);
638 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
639 retval |= __put_user((p->exit_code >> 8) & 0xff,
640 &info->stuff.procinfo.procdata.child.status);
641 retval |= __put_user(p->utime, &info->stuff.procinfo.procdata.child.utime);
642 retval |= __put_user(p->stime, &info->stuff.procinfo.procdata.child.stime);
650 current->signal->cutime += p->utime + p->signal->cutime;
651 current->signal->cstime += p->stime + p->signal->cstime;
653 getrusage(p, RUSAGE_BOTH, ru);
654 retval = __put_user(SIGCHLD, &info->sig);
655 retval |= __put_user(1, &info->code); /* CLD_EXITED */
656 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
657 retval |= __put_user((p->exit_code >> 8) & 0xff,
658 &info->stuff.procinfo.procdata.child.status);
659 retval |= __put_user(p->utime,
660 &info->stuff.procinfo.procdata.child.utime);
661 retval |= __put_user(p->stime,
662 &info->stuff.procinfo.procdata.child.stime);
666 if (p->real_parent != p->parent) {
667 write_lock_irq(&tasklist_lock);
669 p->parent = p->real_parent;
671 do_notify_parent(p, SIGCHLD);
672 write_unlock_irq(&tasklist_lock);
679 tsk = next_thread(tsk);
681 read_unlock(&tasklist_lock);
684 if (options & W_NOHANG)
686 retval = -ERESTARTSYS;
687 if (signal_pending(current))
689 current->state = TASK_INTERRUPTIBLE;
695 current->state = TASK_RUNNING;
696 remove_wait_queue(¤t->signal->wait_chldexit, &wait);
701 struct irix5_context {
705 struct { u32 sp, size, flags; } stack;
711 u32 weird_graphics_thing;
714 asmlinkage int irix_getcontext(struct pt_regs *regs)
716 int error, i, base = 0;
717 struct irix5_context __user *ctx;
720 if (regs->regs[2] == 1000)
722 ctx = (struct irix5_context __user *) regs->regs[base + 4];
725 printk("[%s:%d] irix_getcontext(%p)\n",
726 current->comm, current->pid, ctx);
729 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)));
732 error = __put_user(current->thread.irix_oldctx, &ctx->link);
734 error |= __copy_to_user(&ctx->sigmask, ¤t->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
736 /* XXX Do sigstack stuff someday... */
737 error |= __put_user(0, &ctx->stack.sp);
738 error |= __put_user(0, &ctx->stack.size);
739 error |= __put_user(0, &ctx->stack.flags);
741 error |= __put_user(0, &ctx->weird_graphics_thing);
742 error |= __put_user(0, &ctx->regs[0]);
743 for (i = 1; i < 32; i++)
744 error |= __put_user(regs->regs[i], &ctx->regs[i]);
745 error |= __put_user(regs->lo, &ctx->regs[32]);
746 error |= __put_user(regs->hi, &ctx->regs[33]);
747 error |= __put_user(regs->cp0_cause, &ctx->regs[34]);
748 error |= __put_user(regs->cp0_epc, &ctx->regs[35]);
755 printk("Wheee, no code for saving IRIX FPU context yet.\n");
757 error |= __put_user(flags, &ctx->flags);
762 asmlinkage void irix_setcontext(struct pt_regs *regs)
764 struct irix5_context __user *ctx;
768 if (regs->regs[2] == 1000)
770 ctx = (struct irix5_context __user *) regs->regs[base + 4];
773 printk("[%s:%d] irix_setcontext(%p)\n",
774 current->comm, current->pid, ctx);
777 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
780 err = __get_user(flags, &ctx->flags);
782 /* XXX sigstack garbage, todo... */
783 printk("Wheee, cannot do sigstack stuff in setcontext\n");
789 /* XXX extra control block stuff... todo... */
790 for (i = 1; i < 32; i++)
791 err |= __get_user(regs->regs[i], &ctx->regs[i]);
792 err |= __get_user(regs->lo, &ctx->regs[32]);
793 err |= __get_user(regs->hi, &ctx->regs[33]);
794 err |= __get_user(regs->cp0_epc, &ctx->regs[35]);
798 /* XXX fpu context, blah... */
799 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
801 err |= __get_user(current->thread.irix_oldctx, &ctx->link);
806 * Don't let your children do this ...
808 __asm__ __volatile__(
816 force_sigsegv(SIGSEGV, current);
819 struct irix_sigstack {
824 asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
825 struct irix_sigstack __user *old)
828 printk("[%s:%d] irix_sigstack(%p,%p)\n",
829 current->comm, current->pid, new, old);
832 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
837 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
844 struct irix_sigaltstack { unsigned long sp; int size; int status; };
846 asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
847 struct irix_sigaltstack __user *old)
850 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
851 current->comm, current->pid, new, old);
854 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
858 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
865 struct irix_procset {
866 int cmd, ltype, lid, rtype, rid;
869 asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
871 if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
874 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
875 current->comm, current->pid,
876 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,