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