blob: 33506ff25910f4d8f8974c7a0d9b2098649e8681 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/time.h>
14#include <linux/ptrace.h>
Adrian Bunk83cc5ed2006-06-25 05:47:41 -070015#include <linux/resource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <asm/ptrace.h>
18#include <asm/uaccess.h>
Ralf Baechle1b223c82006-08-03 21:53:10 +010019#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#undef DEBUG_SIG
22
23#define _S(nr) (1<<((nr)-1))
24
25#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
26
Ralf Baechlec4e83082007-10-29 15:00:37 +000027#define _IRIX_NSIG 128
28#define _IRIX_NSIG_BPW BITS_PER_LONG
29#define _IRIX_NSIG_WORDS (_IRIX_NSIG / _IRIX_NSIG_BPW)
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031typedef struct {
Ralf Baechlec4e83082007-10-29 15:00:37 +000032 unsigned long sig[_IRIX_NSIG_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070033} irix_sigset_t;
34
35struct sigctx_irix5 {
36 u32 rmask, cp0_status;
37 u64 pc;
38 u64 regs[32];
39 u64 fpregs[32];
40 u32 usedfp, fpcsr, fpeir, sstk_flags;
41 u64 hi, lo;
42 u64 cp0_cause, cp0_badvaddr, _unused0;
43 irix_sigset_t sigset;
44 u64 weird_fpu_thing;
45 u64 _unused1[31];
46};
47
48#ifdef DEBUG_SIG
49/* Debugging */
50static inline void dump_irix5_sigctx(struct sigctx_irix5 *c)
51{
52 int i;
53
54 printk("misc: rmask[%08lx] status[%08lx] pc[%08lx]\n",
55 (unsigned long) c->rmask,
56 (unsigned long) c->cp0_status,
57 (unsigned long) c->pc);
58 printk("regs: ");
59 for(i = 0; i < 16; i++)
60 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
61 printk("\nregs: ");
62 for(i = 16; i < 32; i++)
63 printk("[%d]<%08lx> ", i, (unsigned long) c->regs[i]);
64 printk("\nfpregs: ");
65 for(i = 0; i < 16; i++)
66 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
67 printk("\nfpregs: ");
68 for(i = 16; i < 32; i++)
69 printk("[%d]<%08lx> ", i, (unsigned long) c->fpregs[i]);
70 printk("misc: usedfp[%d] fpcsr[%08lx] fpeir[%08lx] stk_flgs[%08lx]\n",
71 (int) c->usedfp, (unsigned long) c->fpcsr,
72 (unsigned long) c->fpeir, (unsigned long) c->sstk_flags);
73 printk("misc: hi[%08lx] lo[%08lx] cause[%08lx] badvaddr[%08lx]\n",
74 (unsigned long) c->hi, (unsigned long) c->lo,
75 (unsigned long) c->cp0_cause, (unsigned long) c->cp0_badvaddr);
76 printk("misc: sigset<0>[%08lx] sigset<1>[%08lx] sigset<2>[%08lx] "
77 "sigset<3>[%08lx]\n", (unsigned long) c->sigset.sig[0],
78 (unsigned long) c->sigset.sig[1],
79 (unsigned long) c->sigset.sig[2],
80 (unsigned long) c->sigset.sig[3]);
81}
82#endif
83
Ralf Baechlefe00f942005-03-01 19:22:29 +000084static int setup_irix_frame(struct k_sigaction *ka, struct pt_regs *regs,
85 int signr, sigset_t *oldmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Ralf Baechlefe00f942005-03-01 19:22:29 +000087 struct sigctx_irix5 __user *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned long sp;
Ralf Baechlefe00f942005-03-01 19:22:29 +000089 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 sp = regs->regs[29];
92 sp -= sizeof(struct sigctx_irix5);
93 sp &= ~(0xf);
Ralf Baechlefe00f942005-03-01 19:22:29 +000094 ctx = (struct sigctx_irix5 __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
96 goto segv_and_exit;
97
Ralf Baechlefe00f942005-03-01 19:22:29 +000098 error = __put_user(0, &ctx->weird_fpu_thing);
99 error |= __put_user(~(0x00000001), &ctx->rmask);
100 error |= __put_user(0, &ctx->regs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 for(i = 1; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000102 error |= __put_user((u64) regs->regs[i], &ctx->regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Ralf Baechlefe00f942005-03-01 19:22:29 +0000104 error |= __put_user((u64) regs->hi, &ctx->hi);
105 error |= __put_user((u64) regs->lo, &ctx->lo);
106 error |= __put_user((u64) regs->cp0_epc, &ctx->pc);
107 error |= __put_user(!!used_math(), &ctx->usedfp);
108 error |= __put_user((u64) regs->cp0_cause, &ctx->cp0_cause);
109 error |= __put_user((u64) regs->cp0_badvaddr, &ctx->cp0_badvaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ralf Baechlefe00f942005-03-01 19:22:29 +0000111 error |= __put_user(0, &ctx->sstk_flags); /* XXX sigstack unimp... todo... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Ralf Baechlefe00f942005-03-01 19:22:29 +0000113 error |= __copy_to_user(&ctx->sigset, oldmask, sizeof(irix_sigset_t)) ? -EFAULT : 0;
114
115 if (error)
116 goto segv_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118#ifdef DEBUG_SIG
119 dump_irix5_sigctx(ctx);
120#endif
121
122 regs->regs[4] = (unsigned long) signr;
123 regs->regs[5] = 0; /* XXX sigcode XXX */
124 regs->regs[6] = regs->regs[29] = sp;
125 regs->regs[7] = (unsigned long) ka->sa.sa_handler;
126 regs->regs[25] = regs->cp0_epc = (unsigned long) ka->sa_restorer;
127
Ralf Baechlefe00f942005-03-01 19:22:29 +0000128 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130segv_and_exit:
131 force_sigsegv(signr, current);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Ralf Baechlefe00f942005-03-01 19:22:29 +0000135static int inline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136setup_irix_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
137 int signr, sigset_t *oldmask, siginfo_t *info)
138{
139 printk("Aiee: setup_tr_frame wants to be written");
140 do_exit(SIGSEGV);
141}
142
Ralf Baechlefe00f942005-03-01 19:22:29 +0000143static inline int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs)
145{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000146 int ret;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 switch(regs->regs[0]) {
149 case ERESTARTNOHAND:
150 regs->regs[2] = EINTR;
151 break;
152 case ERESTARTSYS:
153 if(!(ka->sa.sa_flags & SA_RESTART)) {
154 regs->regs[2] = EINTR;
155 break;
156 }
157 /* fallthrough */
158 case ERESTARTNOINTR: /* Userland will reload $v0. */
159 regs->cp0_epc -= 8;
160 }
161
162 regs->regs[0] = 0; /* Don't deal with this again. */
163
164 if (ka->sa.sa_flags & SA_SIGINFO)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000165 ret = setup_irix_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 else
Ralf Baechlefe00f942005-03-01 19:22:29 +0000167 ret = setup_irix_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Steven Rostedt69be8f12005-08-29 11:44:09 -0400169 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle21a151d2007-10-11 23:46:15 +0100170 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400171 if (!(ka->sa.sa_flags & SA_NODEFER))
Ralf Baechle21a151d2007-10-11 23:46:15 +0100172 sigaddset(&current->blocked, sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400173 recalc_sigpending();
174 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000175
176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Ralf Baechle1b223c82006-08-03 21:53:10 +0100179void do_irix_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct k_sigaction ka;
182 siginfo_t info;
183 int signr;
Ralf Baechle1b223c82006-08-03 21:53:10 +0100184 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /*
187 * We want the common case to go fast, which is why we may in certain
188 * cases get here from kernel mode. Just return without doing anything
189 * if so.
190 */
191 if (!user_mode(regs))
Ralf Baechle1b223c82006-08-03 21:53:10 +0100192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Ralf Baechle1b223c82006-08-03 21:53:10 +0100194 if (test_thread_flag(TIF_RESTORE_SIGMASK))
195 oldset = &current->saved_sigmask;
196 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 oldset = &current->blocked;
198
199 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle1b223c82006-08-03 21:53:10 +0100200 if (signr > 0) {
201 /* Whee! Actually deliver the signal. */
202 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
203 /* a signal was successfully delivered; the saved
204 * sigmask will have been stored in the signal frame,
205 * and will be restored by sigreturn, so we can simply
206 * clear the TIF_RESTORE_SIGMASK flag */
207 if (test_thread_flag(TIF_RESTORE_SIGMASK))
208 clear_thread_flag(TIF_RESTORE_SIGMASK);
209 }
210
211 return;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /*
215 * Who's code doesn't conform to the restartable syscall convention
216 * dies here!!! The li instruction, a single machine instruction,
217 * must directly be followed by the syscall instruction.
218 */
219 if (regs->regs[0]) {
220 if (regs->regs[2] == ERESTARTNOHAND ||
221 regs->regs[2] == ERESTARTSYS ||
222 regs->regs[2] == ERESTARTNOINTR) {
223 regs->cp0_epc -= 8;
224 }
Ralf Baechle1b223c82006-08-03 21:53:10 +0100225 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
226 regs->regs[2] = __NR_restart_syscall;
227 regs->regs[7] = regs->regs[26];
228 regs->cp0_epc -= 4;
229 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100230 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Ralf Baechle1b223c82006-08-03 21:53:10 +0100232
233 /*
234 * If there's no signal to deliver, we just put the saved sigmask
235 * back
236 */
237 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
238 clear_thread_flag(TIF_RESTORE_SIGMASK);
239 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243asmlinkage void
244irix_sigreturn(struct pt_regs *regs)
245{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000246 struct sigctx_irix5 __user *context, *magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 unsigned long umask, mask;
248 u64 *fregs;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000249 u32 usedfp;
250 int error, sig, i, base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 sigset_t blocked;
252
253 /* Always make any pending restarted system calls return -EINTR */
254 current_thread_info()->restart_block.fn = do_no_restart_syscall;
255
256 if (regs->regs[2] == 1000)
257 base = 1;
258
Ralf Baechlefe00f942005-03-01 19:22:29 +0000259 context = (struct sigctx_irix5 __user *) regs->regs[base + 4];
260 magic = (struct sigctx_irix5 __user *) regs->regs[base + 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 sig = (int) regs->regs[base + 6];
262#ifdef DEBUG_SIG
263 printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
264 current->comm, current->pid, context, magic, sig);
265#endif
266 if (!context)
267 context = magic;
268 if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
269 goto badframe;
270
271#ifdef DEBUG_SIG
272 dump_irix5_sigctx(context);
273#endif
274
Ralf Baechlefe00f942005-03-01 19:22:29 +0000275 error = __get_user(regs->cp0_epc, &context->pc);
276 error |= __get_user(umask, &context->rmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Ralf Baechlefe00f942005-03-01 19:22:29 +0000278 mask = 2;
279 for (i = 1; i < 32; i++, mask <<= 1) {
280 if (umask & mask)
281 error |= __get_user(regs->regs[i], &context->regs[i]);
282 }
283 error |= __get_user(regs->hi, &context->hi);
284 error |= __get_user(regs->lo, &context->lo);
285
286 error |= __get_user(usedfp, &context->usedfp);
287 if ((umask & 1) && usedfp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 fregs = (u64 *) &current->thread.fpu;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 for(i = 0; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000291 error |= __get_user(fregs[i], &context->fpregs[i]);
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900292 error |= __get_user(current->thread.fpu.fcr31, &context->fpcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 /* XXX do sigstack crapola here... XXX */
296
Ralf Baechlefe00f942005-03-01 19:22:29 +0000297 error |= __copy_from_user(&blocked, &context->sigset, sizeof(blocked)) ? -EFAULT : 0;
298
299 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto badframe;
301
302 sigdelsetmask(&blocked, ~_BLOCKABLE);
303 spin_lock_irq(&current->sighand->siglock);
304 current->blocked = blocked;
305 recalc_sigpending();
306 spin_unlock_irq(&current->sighand->siglock);
307
308 /*
309 * Don't let your children do this ...
310 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 __asm__ __volatile__(
312 "move\t$29,%0\n\t"
313 "j\tsyscall_exit"
314 :/* no outputs */
315 :"r" (&regs));
316 /* Unreached */
317
318badframe:
319 force_sig(SIGSEGV, current);
320}
321
322struct sigact_irix5 {
323 int flags;
324 void (*handler)(int);
325 u32 sigset[4];
326 int _unused0[2];
327};
328
Ralf Baechle633fd562006-08-04 01:49:31 +0100329#define SIG_SETMASK32 256 /* Goodie from SGI for BSD compatibility:
330 set only the low 32 bit of the sigset. */
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#ifdef DEBUG_SIG
333static inline void dump_sigact_irix5(struct sigact_irix5 *p)
334{
335 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
336 (unsigned long) p->handler,
337 (unsigned long) p->sigset[0]);
338}
339#endif
340
341asmlinkage int
Ralf Baechlefe00f942005-03-01 19:22:29 +0000342irix_sigaction(int sig, const struct sigaction __user *act,
343 struct sigaction __user *oact, void __user *trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct k_sigaction new_ka, old_ka;
346 int ret;
347
348#ifdef DEBUG_SIG
349 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
350 (!old ? "0" : "OLD"), trampoline);
351 if(new) {
352 dump_sigact_irix5(new); printk(" ");
353 }
354#endif
355 if (act) {
356 sigset_t mask;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000357 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Ralf Baechlefe00f942005-03-01 19:22:29 +0000359 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
360 return -EFAULT;
361 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
362 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
363
364 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
365 if (err)
366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /*
369 * Hmmm... methinks IRIX libc always passes a valid trampoline
370 * value for all invocations of sigaction. Will have to
371 * investigate. POSIX POSIX, die die die...
372 */
373 new_ka.sa_restorer = trampoline;
374 }
375
376/* XXX Implement SIG_SETMASK32 for IRIX compatibility */
377 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
378
379 if (!ret && oact) {
Ralf Baechlefe00f942005-03-01 19:22:29 +0000380 int err;
381
382 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000384
385 err = __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
386 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
387 err |= __copy_to_user(&oact->sa_mask, &old_ka.sa.sa_mask,
388 sizeof(sigset_t)) ? -EFAULT : 0;
389 if (err)
390 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 return ret;
394}
395
Ralf Baechlefe00f942005-03-01 19:22:29 +0000396asmlinkage int irix_sigpending(irix_sigset_t __user *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 return do_sigpending(set, sizeof(*set));
399}
400
Ralf Baechlefe00f942005-03-01 19:22:29 +0000401asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
402 irix_sigset_t __user *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 sigset_t oldbits, newbits;
405
406 if (new) {
407 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
408 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000409 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
410 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sigdelsetmask(&newbits, ~_BLOCKABLE);
412
413 spin_lock_irq(&current->sighand->siglock);
414 oldbits = current->blocked;
415
416 switch(how) {
417 case 1:
418 sigorsets(&newbits, &oldbits, &newbits);
419 break;
420
421 case 2:
422 sigandsets(&newbits, &oldbits, &newbits);
423 break;
424
425 case 3:
426 break;
427
428 case 256:
429 siginitset(&newbits, newbits.sig[0]);
430 break;
431
432 default:
433 return -EINVAL;
434 }
435 recalc_sigpending();
436 spin_unlock_irq(&current->sighand->siglock);
437 }
Ralf Baechlefe00f942005-03-01 19:22:29 +0000438 if (old)
439 return copy_to_user(old, &current->blocked,
440 sizeof(unsigned long)*4) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return 0;
443}
444
445asmlinkage int irix_sigsuspend(struct pt_regs *regs)
446{
Ralf Baechle1b223c82006-08-03 21:53:10 +0100447 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000448 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Ralf Baechlefe00f942005-03-01 19:22:29 +0000450 uset = (sigset_t __user *) regs->regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
452 return -EFAULT;
453 sigdelsetmask(&newset, ~_BLOCKABLE);
454
455 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle1b223c82006-08-03 21:53:10 +0100456 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 current->blocked = newset;
458 recalc_sigpending();
459 spin_unlock_irq(&current->sighand->siglock);
460
Ralf Baechle1b223c82006-08-03 21:53:10 +0100461 current->state = TASK_INTERRUPTIBLE;
462 schedule();
463 set_thread_flag(TIF_RESTORE_SIGMASK);
464 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/* hate hate hate... */
468struct irix5_siginfo {
469 int sig, code, error;
470 union {
471 char unused[128 - (3 * 4)]; /* Safety net. */
472 struct {
473 int pid;
474 union {
475 int uid;
476 struct {
477 int utime, status, stime;
478 } child;
479 } procdata;
480 } procinfo;
481
482 unsigned long fault_addr;
483
484 struct {
485 int fd;
486 long band;
487 } fileinfo;
488
489 unsigned long sigval;
490 } stuff;
491};
492
Ralf Baechlefe00f942005-03-01 19:22:29 +0000493asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
494 struct irix5_siginfo __user *info, struct timespec __user *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 long expire = MAX_SCHEDULE_TIMEOUT;
497 sigset_t kset;
498 int i, sig, error, timeo = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000499 struct timespec ktp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501#ifdef DEBUG_SIG
502 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
503 current->comm, current->pid, set, info, tp);
504#endif
505
506 /* Must always specify the signal set. */
507 if (!set)
508 return -EINVAL;
509
Ralf Baechlefe00f942005-03-01 19:22:29 +0000510 if (copy_from_user(&kset, set, sizeof(set)))
511 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (info && clear_user(info, sizeof(*info))) {
514 error = -EFAULT;
515 goto out;
516 }
517
518 if (tp) {
Ralf Baechlefe00f942005-03-01 19:22:29 +0000519 if (copy_from_user(&ktp, tp, sizeof(*tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000521
522 if (!ktp.tv_sec && !ktp.tv_nsec)
523 return -EINVAL;
524
525 expire = timespec_to_jiffies(&ktp) +
526 (ktp.tv_sec || ktp.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
529 while(1) {
530 long tmp = 0;
531
Ralf Baechle0d959c22005-11-05 11:26:43 +0000532 expire = schedule_timeout_interruptible(expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ralf Baechlec4e83082007-10-29 15:00:37 +0000534 for (i=0; i < _IRIX_NSIG_WORDS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
536
537 if (tmp)
538 break;
539 if (!expire) {
540 timeo = 1;
541 break;
542 }
543 if (signal_pending(current))
544 return -EINTR;
545 }
546 if (timeo)
547 return -EAGAIN;
548
Ralf Baechlefe00f942005-03-01 19:22:29 +0000549 for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (sigismember (&kset, sig))
551 continue;
552 if (sigismember (&current->pending.signal, sig)) {
553 /* XXX need more than this... */
554 if (info)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000555 return copy_to_user(&info->sig, &sig, sizeof(sig));
556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 }
559
560 /* Should not get here, but do something sane if we do. */
561 error = -EINTR;
562
563out:
564 return error;
565}
566
567/* This is here because of irix5_siginfo definition. */
568#define IRIX_P_PID 0
569#define IRIX_P_PGID 2
570#define IRIX_P_ALL 7
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#define W_EXITED 1
573#define W_TRAPPED 2
574#define W_STOPPED 4
575#define W_CONT 8
576#define W_NOHANG 64
577
578#define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
579
Ralf Baechlefe00f942005-03-01 19:22:29 +0000580asmlinkage int irix_waitsys(int type, int pid,
581 struct irix5_siginfo __user *info, int options,
582 struct rusage __user *ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 int flag, retval;
585 DECLARE_WAITQUEUE(wait, current);
586 struct task_struct *tsk;
587 struct task_struct *p;
588 struct list_head *_p;
589
Ralf Baechlefe00f942005-03-01 19:22:29 +0000590 if (!info)
591 return -EINVAL;
592
593 if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
594 return -EFAULT;
595
596 if (ru)
597 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
598 return -EFAULT;
599
600 if (options & ~W_MASK)
601 return -EINVAL;
602
603 if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
604 return -EINVAL;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 add_wait_queue(&current->signal->wait_chldexit, &wait);
607repeat:
608 flag = 0;
609 current->state = TASK_INTERRUPTIBLE;
610 read_lock(&tasklist_lock);
611 tsk = current;
Ralf Baechle21a151d2007-10-11 23:46:15 +0100612 list_for_each(_p, &tsk->children) {
613 p = list_entry(_p, struct task_struct, sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if ((type == IRIX_P_PID) && p->pid != pid)
615 continue;
Pavel Emelianova47afb02007-10-18 23:39:46 -0700616 if ((type == IRIX_P_PGID) && task_pgrp_nr(p) != pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 continue;
618 if ((p->exit_signal != SIGCHLD))
619 continue;
620 flag = 1;
621 switch (p->state) {
622 case TASK_STOPPED:
623 if (!p->exit_code)
624 continue;
625 if (!(options & (W_TRAPPED|W_STOPPED)) &&
626 !(p->ptrace & PT_PTRACED))
627 continue;
628 read_unlock(&tasklist_lock);
629
630 /* move to end of parent's list to avoid starvation */
631 write_lock_irq(&tasklist_lock);
632 remove_parent(p);
Oleg Nesterov8fafabd2006-03-28 16:11:05 -0800633 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 write_unlock_irq(&tasklist_lock);
635 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000636 if (retval)
637 goto end_waitsys;
638
639 retval = __put_user(SIGCHLD, &info->sig);
640 retval |= __put_user(0, &info->code);
641 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
642 retval |= __put_user((p->exit_code >> 8) & 0xff,
643 &info->stuff.procinfo.procdata.child.status);
644 retval |= __put_user(p->utime, &info->stuff.procinfo.procdata.child.utime);
645 retval |= __put_user(p->stime, &info->stuff.procinfo.procdata.child.stime);
646 if (retval)
647 goto end_waitsys;
648
649 p->exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto end_waitsys;
651
652 case EXIT_ZOMBIE:
653 current->signal->cutime += p->utime + p->signal->cutime;
654 current->signal->cstime += p->stime + p->signal->cstime;
655 if (ru != NULL)
656 getrusage(p, RUSAGE_BOTH, ru);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000657 retval = __put_user(SIGCHLD, &info->sig);
658 retval |= __put_user(1, &info->code); /* CLD_EXITED */
659 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
660 retval |= __put_user((p->exit_code >> 8) & 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 &info->stuff.procinfo.procdata.child.status);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000662 retval |= __put_user(p->utime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 &info->stuff.procinfo.procdata.child.utime);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000664 retval |= __put_user(p->stime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 &info->stuff.procinfo.procdata.child.stime);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000666 if (retval)
667 return retval;
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (p->real_parent != p->parent) {
670 write_lock_irq(&tasklist_lock);
671 remove_parent(p);
672 p->parent = p->real_parent;
Oleg Nesterov8fafabd2006-03-28 16:11:05 -0800673 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 do_notify_parent(p, SIGCHLD);
675 write_unlock_irq(&tasklist_lock);
676 } else
677 release_task(p);
678 goto end_waitsys;
679 default:
680 continue;
681 }
682 tsk = next_thread(tsk);
683 }
684 read_unlock(&tasklist_lock);
685 if (flag) {
686 retval = 0;
687 if (options & W_NOHANG)
688 goto end_waitsys;
689 retval = -ERESTARTSYS;
690 if (signal_pending(current))
691 goto end_waitsys;
692 current->state = TASK_INTERRUPTIBLE;
693 schedule();
694 goto repeat;
695 }
696 retval = -ECHILD;
697end_waitsys:
698 current->state = TASK_RUNNING;
699 remove_wait_queue(&current->signal->wait_chldexit, &wait);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return retval;
702}
703
704struct irix5_context {
705 u32 flags;
706 u32 link;
707 u32 sigmask[4];
708 struct { u32 sp, size, flags; } stack;
709 int regs[36];
710 u32 fpregs[32];
711 u32 fpcsr;
712 u32 _unused0;
713 u32 _unused1[47];
714 u32 weird_graphics_thing;
715};
716
717asmlinkage int irix_getcontext(struct pt_regs *regs)
718{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000719 int error, i, base = 0;
720 struct irix5_context __user *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned long flags;
722
723 if (regs->regs[2] == 1000)
724 base = 1;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000725 ctx = (struct irix5_context __user *) regs->regs[base + 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727#ifdef DEBUG_SIG
728 printk("[%s:%d] irix_getcontext(%p)\n",
729 current->comm, current->pid, ctx);
730#endif
731
Ilpo Järvinen08061332007-08-16 01:03:01 +0300732 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -EFAULT;
734
Ralf Baechlefe00f942005-03-01 19:22:29 +0000735 error = __put_user(current->thread.irix_oldctx, &ctx->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Ralf Baechlefe00f942005-03-01 19:22:29 +0000737 error |= __copy_to_user(&ctx->sigmask, &current->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* XXX Do sigstack stuff someday... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000740 error |= __put_user(0, &ctx->stack.sp);
741 error |= __put_user(0, &ctx->stack.size);
742 error |= __put_user(0, &ctx->stack.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ralf Baechlefe00f942005-03-01 19:22:29 +0000744 error |= __put_user(0, &ctx->weird_graphics_thing);
745 error |= __put_user(0, &ctx->regs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 for (i = 1; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000747 error |= __put_user(regs->regs[i], &ctx->regs[i]);
748 error |= __put_user(regs->lo, &ctx->regs[32]);
749 error |= __put_user(regs->hi, &ctx->regs[33]);
750 error |= __put_user(regs->cp0_cause, &ctx->regs[34]);
751 error |= __put_user(regs->cp0_epc, &ctx->regs[35]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 flags = 0x0f;
754 if (!used_math()) {
755 flags &= ~(0x08);
756 } else {
757 /* XXX wheee... */
758 printk("Wheee, no code for saving IRIX FPU context yet.\n");
759 }
Ralf Baechlefe00f942005-03-01 19:22:29 +0000760 error |= __put_user(flags, &ctx->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Ralf Baechlefe00f942005-03-01 19:22:29 +0000762 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Ralf Baechlefe00f942005-03-01 19:22:29 +0000765asmlinkage void irix_setcontext(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000767 struct irix5_context __user *ctx;
768 int err, base = 0;
769 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Ralf Baechlefe00f942005-03-01 19:22:29 +0000771 if (regs->regs[2] == 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 base = 1;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000773 ctx = (struct irix5_context __user *) regs->regs[base + 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775#ifdef DEBUG_SIG
776 printk("[%s:%d] irix_setcontext(%p)\n",
777 current->comm, current->pid, ctx);
778#endif
779
Ralf Baechlefe00f942005-03-01 19:22:29 +0000780 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
781 goto segv_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Ralf Baechlefe00f942005-03-01 19:22:29 +0000783 err = __get_user(flags, &ctx->flags);
784 if (flags & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* XXX sigstack garbage, todo... */
786 printk("Wheee, cannot do sigstack stuff in setcontext\n");
787 }
788
Ralf Baechlefe00f942005-03-01 19:22:29 +0000789 if (flags & 0x04) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int i;
791
792 /* XXX extra control block stuff... todo... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000793 for (i = 1; i < 32; i++)
794 err |= __get_user(regs->regs[i], &ctx->regs[i]);
795 err |= __get_user(regs->lo, &ctx->regs[32]);
796 err |= __get_user(regs->hi, &ctx->regs[33]);
797 err |= __get_user(regs->cp0_epc, &ctx->regs[35]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
Ralf Baechlefe00f942005-03-01 19:22:29 +0000800 if (flags & 0x08)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* XXX fpu context, blah... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000802 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Ralf Baechlefe00f942005-03-01 19:22:29 +0000804 err |= __get_user(current->thread.irix_oldctx, &ctx->link);
805 if (err)
806 goto segv_and_exit;
807
808 /*
809 * Don't let your children do this ...
810 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000811 __asm__ __volatile__(
812 "move\t$29,%0\n\t"
813 "j\tsyscall_exit"
814 :/* no outputs */
815 :"r" (&regs));
816 /* Unreached */
817
818segv_and_exit:
819 force_sigsegv(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Ralf Baechlefe00f942005-03-01 19:22:29 +0000822struct irix_sigstack {
823 unsigned long sp;
824 int status;
825};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Ralf Baechlefe00f942005-03-01 19:22:29 +0000827asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
828 struct irix_sigstack __user *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#ifdef DEBUG_SIG
831 printk("[%s:%d] irix_sigstack(%p,%p)\n",
832 current->comm, current->pid, new, old);
833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (new) {
835 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
Ralf Baechlefe00f942005-03-01 19:22:29 +0000836 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 if (old) {
840 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
Ralf Baechlefe00f942005-03-01 19:22:29 +0000841 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Ralf Baechlefe00f942005-03-01 19:22:29 +0000844 return 0;
845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Ralf Baechlefe00f942005-03-01 19:22:29 +0000847struct irix_sigaltstack { unsigned long sp; int size; int status; };
848
849asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
850 struct irix_sigaltstack __user *old)
851{
852#ifdef DEBUG_SIG
853 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
854 current->comm, current->pid, new, old);
855#endif
856 if (new)
857 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
858 return -EFAULT;
859
860 if (old) {
861 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
862 return -EFAULT;
863 }
864
865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868struct irix_procset {
869 int cmd, ltype, lid, rtype, rid;
870};
871
Ralf Baechlefe00f942005-03-01 19:22:29 +0000872asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
875 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#ifdef DEBUG_SIG
877 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
878 current->comm, current->pid,
879 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,
880 sig);
881#endif
882 return -EINVAL;
883}