Merge git://git.infradead.org/~dwmw2/cafe-2.6
[linux-2.6] / arch / mips / kernel / irixsig.c
1 /*
2  * irixsig.c: WHEEE, IRIX signals!  YOW, am I compatible or what?!?!
3  *
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.
7  */
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/mm.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>
17
18 #include <asm/ptrace.h>
19 #include <asm/uaccess.h>
20 #include <asm/unistd.h>
21
22 #undef DEBUG_SIG
23
24 #define _S(nr) (1<<((nr)-1))
25
26 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
27
28 typedef struct {
29         unsigned long sig[4];
30 } irix_sigset_t;
31
32 struct sigctx_irix5 {
33         u32 rmask, cp0_status;
34         u64 pc;
35         u64 regs[32];
36         u64 fpregs[32];
37         u32 usedfp, fpcsr, fpeir, sstk_flags;
38         u64 hi, lo;
39         u64 cp0_cause, cp0_badvaddr, _unused0;
40         irix_sigset_t sigset;
41         u64 weird_fpu_thing;
42         u64 _unused1[31];
43 };
44
45 #ifdef DEBUG_SIG
46 /* Debugging */
47 static inline void dump_irix5_sigctx(struct sigctx_irix5 *c)
48 {
49         int i;
50
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);
55         printk("regs: ");
56         for(i = 0; i < 16; i++)
57                 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
58         printk("\nregs: ");
59         for(i = 16; i < 32; i++)
60                 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
61         printk("\nfpregs: ");
62         for(i = 0; i < 16; i++)
63                 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
64         printk("\nfpregs: ");
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]);
78 }
79 #endif
80
81 static int setup_irix_frame(struct k_sigaction *ka, struct pt_regs *regs,
82                             int signr, sigset_t *oldmask)
83 {
84         struct sigctx_irix5 __user *ctx;
85         unsigned long sp;
86         int error, i;
87
88         sp = regs->regs[29];
89         sp -= sizeof(struct sigctx_irix5);
90         sp &= ~(0xf);
91         ctx = (struct sigctx_irix5 __user *) sp;
92         if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
93                 goto segv_and_exit;
94
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]);
100
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);
107
108         error |= __put_user(0, &ctx->sstk_flags); /* XXX sigstack unimp... todo... */
109
110         error |= __copy_to_user(&ctx->sigset, oldmask, sizeof(irix_sigset_t)) ? -EFAULT : 0;
111
112         if (error)
113                 goto segv_and_exit;
114
115 #ifdef DEBUG_SIG
116         dump_irix5_sigctx(ctx);
117 #endif
118
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;
124
125         return 1;
126
127 segv_and_exit:
128         force_sigsegv(signr, current);
129         return 0;
130 }
131
132 static int inline
133 setup_irix_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
134                int signr, sigset_t *oldmask, siginfo_t *info)
135 {
136         printk("Aiee: setup_tr_frame wants to be written");
137         do_exit(SIGSEGV);
138 }
139
140 static inline int handle_signal(unsigned long sig, siginfo_t *info,
141         struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs)
142 {
143         int ret;
144
145         switch(regs->regs[0]) {
146         case ERESTARTNOHAND:
147                 regs->regs[2] = EINTR;
148                 break;
149         case ERESTARTSYS:
150                 if(!(ka->sa.sa_flags & SA_RESTART)) {
151                         regs->regs[2] = EINTR;
152                         break;
153                 }
154         /* fallthrough */
155         case ERESTARTNOINTR:            /* Userland will reload $v0.  */
156                 regs->cp0_epc -= 8;
157         }
158
159         regs->regs[0] = 0;              /* Don't deal with this again.  */
160
161         if (ka->sa.sa_flags & SA_SIGINFO)
162                 ret = setup_irix_rt_frame(ka, regs, sig, oldset, info);
163         else
164                 ret = setup_irix_frame(ka, regs, sig, oldset);
165
166         spin_lock_irq(&current->sighand->siglock);
167         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
168         if (!(ka->sa.sa_flags & SA_NODEFER))
169                 sigaddset(&current->blocked,sig);
170         recalc_sigpending();
171         spin_unlock_irq(&current->sighand->siglock);
172
173         return ret;
174 }
175
176 void do_irix_signal(struct pt_regs *regs)
177 {
178         struct k_sigaction ka;
179         siginfo_t info;
180         int signr;
181         sigset_t *oldset;
182
183         /*
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
186          * if so.
187          */
188         if (!user_mode(regs))
189                 return;
190
191         if (test_thread_flag(TIF_RESTORE_SIGMASK))
192                 oldset = &current->saved_sigmask;
193         else
194                 oldset = &current->blocked;
195
196         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
197         if (signr > 0) {
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);
206                 }
207
208                 return;
209         }
210
211         /*
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.
215          */
216         if (regs->regs[0]) {
217                 if (regs->regs[2] == ERESTARTNOHAND ||
218                     regs->regs[2] == ERESTARTSYS ||
219                     regs->regs[2] == ERESTARTNOINTR) {
220                         regs->cp0_epc -= 8;
221                 }
222                 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
223                         regs->regs[2] = __NR_restart_syscall;
224                         regs->regs[7] = regs->regs[26];
225                         regs->cp0_epc -= 4;
226                 }
227                 regs->regs[0] = 0;      /* Don't deal with this again.  */
228         }
229
230         /*
231         * If there's no signal to deliver, we just put the saved sigmask
232         * back
233         */
234         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
235                 clear_thread_flag(TIF_RESTORE_SIGMASK);
236                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
237         }
238 }
239
240 asmlinkage void
241 irix_sigreturn(struct pt_regs *regs)
242 {
243         struct sigctx_irix5 __user *context, *magic;
244         unsigned long umask, mask;
245         u64 *fregs;
246         u32 usedfp;
247         int error, sig, i, base = 0;
248         sigset_t blocked;
249
250         /* Always make any pending restarted system calls return -EINTR */
251         current_thread_info()->restart_block.fn = do_no_restart_syscall;
252
253         if (regs->regs[2] == 1000)
254                 base = 1;
255
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];
259 #ifdef DEBUG_SIG
260         printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
261                current->comm, current->pid, context, magic, sig);
262 #endif
263         if (!context)
264                 context = magic;
265         if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
266                 goto badframe;
267
268 #ifdef DEBUG_SIG
269         dump_irix5_sigctx(context);
270 #endif
271
272         error = __get_user(regs->cp0_epc, &context->pc);
273         error |= __get_user(umask, &context->rmask);
274
275         mask = 2;
276         for (i = 1; i < 32; i++, mask <<= 1) {
277                 if (umask & mask)
278                         error |= __get_user(regs->regs[i], &context->regs[i]);
279         }
280         error |= __get_user(regs->hi, &context->hi);
281         error |= __get_user(regs->lo, &context->lo);
282
283         error |= __get_user(usedfp, &context->usedfp);
284         if ((umask & 1) && usedfp) {
285                 fregs = (u64 *) &current->thread.fpu;
286
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);
290         }
291
292         /* XXX do sigstack crapola here... XXX */
293
294         error |= __copy_from_user(&blocked, &context->sigset, sizeof(blocked)) ? -EFAULT : 0;
295
296         if (error)
297                 goto badframe;
298
299         sigdelsetmask(&blocked, ~_BLOCKABLE);
300         spin_lock_irq(&current->sighand->siglock);
301         current->blocked = blocked;
302         recalc_sigpending();
303         spin_unlock_irq(&current->sighand->siglock);
304
305         /*
306          * Don't let your children do this ...
307          */
308         __asm__ __volatile__(
309                 "move\t$29,%0\n\t"
310                 "j\tsyscall_exit"
311                 :/* no outputs */
312                 :"r" (&regs));
313                 /* Unreached */
314
315 badframe:
316         force_sig(SIGSEGV, current);
317 }
318
319 struct sigact_irix5 {
320         int flags;
321         void (*handler)(int);
322         u32 sigset[4];
323         int _unused0[2];
324 };
325
326 #define SIG_SETMASK32   256     /* Goodie from SGI for BSD compatibility:
327                                    set only the low 32 bit of the sigset.  */
328
329 #ifdef DEBUG_SIG
330 static inline void dump_sigact_irix5(struct sigact_irix5 *p)
331 {
332         printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
333                (unsigned long) p->handler,
334                (unsigned long) p->sigset[0]);
335 }
336 #endif
337
338 asmlinkage int
339 irix_sigaction(int sig, const struct sigaction __user *act,
340               struct sigaction __user *oact, void __user *trampoline)
341 {
342         struct k_sigaction new_ka, old_ka;
343         int ret;
344
345 #ifdef DEBUG_SIG
346         printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
347                (!old ? "0" : "OLD"), trampoline);
348         if(new) {
349                 dump_sigact_irix5(new); printk(" ");
350         }
351 #endif
352         if (act) {
353                 sigset_t mask;
354                 int err;
355
356                 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
357                         return -EFAULT;
358                 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
359                 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
360
361                 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
362                 if (err)
363                         return err;
364
365                 /*
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...
369                  */
370                 new_ka.sa_restorer = trampoline;
371         }
372
373 /* XXX Implement SIG_SETMASK32 for IRIX compatibility */
374         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
375
376         if (!ret && oact) {
377                 int err;
378
379                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
380                         return -EFAULT;
381
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;
386                 if (err)
387                         return -EFAULT;
388         }
389
390         return ret;
391 }
392
393 asmlinkage int irix_sigpending(irix_sigset_t __user *set)
394 {
395         return do_sigpending(set, sizeof(*set));
396 }
397
398 asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
399         irix_sigset_t __user *old)
400 {
401         sigset_t oldbits, newbits;
402
403         if (new) {
404                 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
405                         return -EFAULT;
406                 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
407                         return -EFAULT;
408                 sigdelsetmask(&newbits, ~_BLOCKABLE);
409
410                 spin_lock_irq(&current->sighand->siglock);
411                 oldbits = current->blocked;
412
413                 switch(how) {
414                 case 1:
415                         sigorsets(&newbits, &oldbits, &newbits);
416                         break;
417
418                 case 2:
419                         sigandsets(&newbits, &oldbits, &newbits);
420                         break;
421
422                 case 3:
423                         break;
424
425                 case 256:
426                         siginitset(&newbits, newbits.sig[0]);
427                         break;
428
429                 default:
430                         return -EINVAL;
431                 }
432                 recalc_sigpending();
433                 spin_unlock_irq(&current->sighand->siglock);
434         }
435         if (old)
436                 return copy_to_user(old, &current->blocked,
437                                   sizeof(unsigned long)*4) ? -EFAULT : 0;
438
439         return 0;
440 }
441
442 asmlinkage int irix_sigsuspend(struct pt_regs *regs)
443 {
444         sigset_t newset;
445         sigset_t __user *uset;
446
447         uset = (sigset_t __user *) regs->regs[4];
448         if (copy_from_user(&newset, uset, sizeof(sigset_t)))
449                 return -EFAULT;
450         sigdelsetmask(&newset, ~_BLOCKABLE);
451
452         spin_lock_irq(&current->sighand->siglock);
453         current->saved_sigmask = current->blocked;
454         current->blocked = newset;
455         recalc_sigpending();
456         spin_unlock_irq(&current->sighand->siglock);
457
458         current->state = TASK_INTERRUPTIBLE;
459         schedule();
460         set_thread_flag(TIF_RESTORE_SIGMASK);
461         return -ERESTARTNOHAND;
462 }
463
464 /* hate hate hate... */
465 struct irix5_siginfo {
466         int sig, code, error;
467         union {
468                 char unused[128 - (3 * 4)]; /* Safety net. */
469                 struct {
470                         int pid;
471                         union {
472                                 int uid;
473                                 struct {
474                                         int utime, status, stime;
475                                 } child;
476                         } procdata;
477                 } procinfo;
478
479                 unsigned long fault_addr;
480
481                 struct {
482                         int fd;
483                         long band;
484                 } fileinfo;
485
486                 unsigned long sigval;
487         } stuff;
488 };
489
490 asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
491         struct irix5_siginfo __user *info, struct timespec __user *tp)
492 {
493         long expire = MAX_SCHEDULE_TIMEOUT;
494         sigset_t kset;
495         int i, sig, error, timeo = 0;
496         struct timespec ktp;
497
498 #ifdef DEBUG_SIG
499         printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
500                current->comm, current->pid, set, info, tp);
501 #endif
502
503         /* Must always specify the signal set. */
504         if (!set)
505                 return -EINVAL;
506
507         if (copy_from_user(&kset, set, sizeof(set)))
508                 return -EFAULT;
509
510         if (info && clear_user(info, sizeof(*info))) {
511                 error = -EFAULT;
512                 goto out;
513         }
514
515         if (tp) {
516                 if (copy_from_user(&ktp, tp, sizeof(*tp)))
517                         return -EFAULT;
518
519                 if (!ktp.tv_sec && !ktp.tv_nsec)
520                         return -EINVAL;
521
522                 expire = timespec_to_jiffies(&ktp) +
523                          (ktp.tv_sec || ktp.tv_nsec);
524         }
525
526         while(1) {
527                 long tmp = 0;
528
529                 expire = schedule_timeout_interruptible(expire);
530
531                 for (i=0; i<=4; i++)
532                         tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
533
534                 if (tmp)
535                         break;
536                 if (!expire) {
537                         timeo = 1;
538                         break;
539                 }
540                 if (signal_pending(current))
541                         return -EINTR;
542         }
543         if (timeo)
544                 return -EAGAIN;
545
546         for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
547                 if (sigismember (&kset, sig))
548                         continue;
549                 if (sigismember (&current->pending.signal, sig)) {
550                         /* XXX need more than this... */
551                         if (info)
552                                 return copy_to_user(&info->sig, &sig, sizeof(sig));
553                         return 0;
554                 }
555         }
556
557         /* Should not get here, but do something sane if we do. */
558         error = -EINTR;
559
560 out:
561         return error;
562 }
563
564 /* This is here because of irix5_siginfo definition. */
565 #define IRIX_P_PID    0
566 #define IRIX_P_PGID   2
567 #define IRIX_P_ALL    7
568
569 #define W_EXITED     1
570 #define W_TRAPPED    2
571 #define W_STOPPED    4
572 #define W_CONT       8
573 #define W_NOHANG    64
574
575 #define W_MASK      (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
576
577 asmlinkage int irix_waitsys(int type, int pid,
578         struct irix5_siginfo __user *info, int options,
579         struct rusage __user *ru)
580 {
581         int flag, retval;
582         DECLARE_WAITQUEUE(wait, current);
583         struct task_struct *tsk;
584         struct task_struct *p;
585         struct list_head *_p;
586
587         if (!info)
588                 return -EINVAL;
589
590         if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
591                 return -EFAULT;
592
593         if (ru)
594                 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
595                         return -EFAULT;
596
597         if (options & ~W_MASK)
598                 return -EINVAL;
599
600         if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
601                 return -EINVAL;
602
603         add_wait_queue(&current->signal->wait_chldexit, &wait);
604 repeat:
605         flag = 0;
606         current->state = TASK_INTERRUPTIBLE;
607         read_lock(&tasklist_lock);
608         tsk = current;
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)
612                         continue;
613                 if ((type == IRIX_P_PGID) && process_group(p) != pid)
614                         continue;
615                 if ((p->exit_signal != SIGCHLD))
616                         continue;
617                 flag = 1;
618                 switch (p->state) {
619                 case TASK_STOPPED:
620                         if (!p->exit_code)
621                                 continue;
622                         if (!(options & (W_TRAPPED|W_STOPPED)) &&
623                             !(p->ptrace & PT_PTRACED))
624                                 continue;
625                         read_unlock(&tasklist_lock);
626
627                         /* move to end of parent's list to avoid starvation */
628                         write_lock_irq(&tasklist_lock);
629                         remove_parent(p);
630                         add_parent(p);
631                         write_unlock_irq(&tasklist_lock);
632                         retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
633                         if (retval)
634                                 goto end_waitsys;
635
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);
643                         if (retval)
644                                 goto end_waitsys;
645
646                         p->exit_code = 0;
647                         goto end_waitsys;
648
649                 case EXIT_ZOMBIE:
650                         current->signal->cutime += p->utime + p->signal->cutime;
651                         current->signal->cstime += p->stime + p->signal->cstime;
652                         if (ru != NULL)
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);
663                         if (retval)
664                                 return retval;
665
666                         if (p->real_parent != p->parent) {
667                                 write_lock_irq(&tasklist_lock);
668                                 remove_parent(p);
669                                 p->parent = p->real_parent;
670                                 add_parent(p);
671                                 do_notify_parent(p, SIGCHLD);
672                                 write_unlock_irq(&tasklist_lock);
673                         } else
674                                 release_task(p);
675                         goto end_waitsys;
676                 default:
677                         continue;
678                 }
679                 tsk = next_thread(tsk);
680         }
681         read_unlock(&tasklist_lock);
682         if (flag) {
683                 retval = 0;
684                 if (options & W_NOHANG)
685                         goto end_waitsys;
686                 retval = -ERESTARTSYS;
687                 if (signal_pending(current))
688                         goto end_waitsys;
689                 current->state = TASK_INTERRUPTIBLE;
690                 schedule();
691                 goto repeat;
692         }
693         retval = -ECHILD;
694 end_waitsys:
695         current->state = TASK_RUNNING;
696         remove_wait_queue(&current->signal->wait_chldexit, &wait);
697
698         return retval;
699 }
700
701 struct irix5_context {
702         u32 flags;
703         u32 link;
704         u32 sigmask[4];
705         struct { u32 sp, size, flags; } stack;
706         int regs[36];
707         u32 fpregs[32];
708         u32 fpcsr;
709         u32 _unused0;
710         u32 _unused1[47];
711         u32 weird_graphics_thing;
712 };
713
714 asmlinkage int irix_getcontext(struct pt_regs *regs)
715 {
716         int error, i, base = 0;
717         struct irix5_context __user *ctx;
718         unsigned long flags;
719
720         if (regs->regs[2] == 1000)
721                 base = 1;
722         ctx = (struct irix5_context __user *) regs->regs[base + 4];
723
724 #ifdef DEBUG_SIG
725         printk("[%s:%d] irix_getcontext(%p)\n",
726                current->comm, current->pid, ctx);
727 #endif
728
729         if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)));
730                 return -EFAULT;
731
732         error = __put_user(current->thread.irix_oldctx, &ctx->link);
733
734         error |= __copy_to_user(&ctx->sigmask, &current->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
735
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);
740
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]);
749
750         flags = 0x0f;
751         if (!used_math()) {
752                 flags &= ~(0x08);
753         } else {
754                 /* XXX wheee... */
755                 printk("Wheee, no code for saving IRIX FPU context yet.\n");
756         }
757         error |= __put_user(flags, &ctx->flags);
758
759         return error;
760 }
761
762 asmlinkage void irix_setcontext(struct pt_regs *regs)
763 {
764         struct irix5_context __user *ctx;
765         int err, base = 0;
766         u32 flags;
767
768         if (regs->regs[2] == 1000)
769                 base = 1;
770         ctx = (struct irix5_context __user *) regs->regs[base + 4];
771
772 #ifdef DEBUG_SIG
773         printk("[%s:%d] irix_setcontext(%p)\n",
774                current->comm, current->pid, ctx);
775 #endif
776
777         if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
778                 goto segv_and_exit;
779
780         err = __get_user(flags, &ctx->flags);
781         if (flags & 0x02) {
782                 /* XXX sigstack garbage, todo... */
783                 printk("Wheee, cannot do sigstack stuff in setcontext\n");
784         }
785
786         if (flags & 0x04) {
787                 int i;
788
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]);
795         }
796
797         if (flags & 0x08)
798                 /* XXX fpu context, blah... */
799                 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
800
801         err |= __get_user(current->thread.irix_oldctx, &ctx->link);
802         if (err)
803                 goto segv_and_exit;
804
805         /*
806          * Don't let your children do this ...
807          */
808         __asm__ __volatile__(
809                 "move\t$29,%0\n\t"
810                 "j\tsyscall_exit"
811                 :/* no outputs */
812                 :"r" (&regs));
813                 /* Unreached */
814
815 segv_and_exit:
816         force_sigsegv(SIGSEGV, current);
817 }
818
819 struct irix_sigstack {
820         unsigned long sp;
821         int status;
822 };
823
824 asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
825         struct irix_sigstack __user *old)
826 {
827 #ifdef DEBUG_SIG
828         printk("[%s:%d] irix_sigstack(%p,%p)\n",
829                current->comm, current->pid, new, old);
830 #endif
831         if (new) {
832                 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
833                         return -EFAULT;
834         }
835
836         if (old) {
837                 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
838                         return -EFAULT;
839         }
840
841         return 0;
842 }
843
844 struct irix_sigaltstack { unsigned long sp; int size; int status; };
845
846 asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
847                                 struct irix_sigaltstack __user *old)
848 {
849 #ifdef DEBUG_SIG
850         printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
851                current->comm, current->pid, new, old);
852 #endif
853         if (new)
854                 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
855                         return -EFAULT;
856
857         if (old) {
858                 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
859                         return -EFAULT;
860         }
861
862         return 0;
863 }
864
865 struct irix_procset {
866         int cmd, ltype, lid, rtype, rid;
867 };
868
869 asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
870 {
871         if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
872                 return -EFAULT;
873 #ifdef DEBUG_SIG
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,
877                sig);
878 #endif
879         return -EINVAL;
880 }