blob: 052ea15f1e800ac8627d6aa4f68ca965ed8232fb [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>
13#include <linux/smp_lock.h>
14#include <linux/time.h>
15#include <linux/ptrace.h>
Adrian Bunk83cc5ed2006-06-25 05:47:41 -070016#include <linux/resource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/ptrace.h>
19#include <asm/uaccess.h>
Ralf Baechle1b223c82006-08-03 21:53:10 +010020#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#undef DEBUG_SIG
23
24#define _S(nr) (1<<((nr)-1))
25
26#define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
27
28typedef struct {
29 unsigned long sig[4];
30} irix_sigset_t;
31
32struct 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 */
47static 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
Ralf Baechlefe00f942005-03-01 19:22:29 +000081static int setup_irix_frame(struct k_sigaction *ka, struct pt_regs *regs,
82 int signr, sigset_t *oldmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Ralf Baechlefe00f942005-03-01 19:22:29 +000084 struct sigctx_irix5 __user *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 unsigned long sp;
Ralf Baechlefe00f942005-03-01 19:22:29 +000086 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 sp = regs->regs[29];
89 sp -= sizeof(struct sigctx_irix5);
90 sp &= ~(0xf);
Ralf Baechlefe00f942005-03-01 19:22:29 +000091 ctx = (struct sigctx_irix5 __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)))
93 goto segv_and_exit;
94
Ralf Baechlefe00f942005-03-01 19:22:29 +000095 error = __put_user(0, &ctx->weird_fpu_thing);
96 error |= __put_user(~(0x00000001), &ctx->rmask);
97 error |= __put_user(0, &ctx->regs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 for(i = 1; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +000099 error |= __put_user((u64) regs->regs[i], &ctx->regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Ralf Baechlefe00f942005-03-01 19:22:29 +0000101 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Ralf Baechlefe00f942005-03-01 19:22:29 +0000108 error |= __put_user(0, &ctx->sstk_flags); /* XXX sigstack unimp... todo... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ralf Baechlefe00f942005-03-01 19:22:29 +0000110 error |= __copy_to_user(&ctx->sigset, oldmask, sizeof(irix_sigset_t)) ? -EFAULT : 0;
111
112 if (error)
113 goto segv_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
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
Ralf Baechlefe00f942005-03-01 19:22:29 +0000125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127segv_and_exit:
128 force_sigsegv(signr, current);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Ralf Baechlefe00f942005-03-01 19:22:29 +0000132static int inline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133setup_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
Ralf Baechlefe00f942005-03-01 19:22:29 +0000140static inline int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs)
142{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000143 int ret;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 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)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000162 ret = setup_irix_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 else
Ralf Baechlefe00f942005-03-01 19:22:29 +0000164 ret = setup_irix_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Steven Rostedt69be8f12005-08-29 11:44:09 -0400166 spin_lock_irq(&current->sighand->siglock);
167 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
168 if (!(ka->sa.sa_flags & SA_NODEFER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 sigaddset(&current->blocked,sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400170 recalc_sigpending();
171 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000172
173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Ralf Baechle1b223c82006-08-03 21:53:10 +0100176void do_irix_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct k_sigaction ka;
179 siginfo_t info;
180 int signr;
Ralf Baechle1b223c82006-08-03 21:53:10 +0100181 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
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))
Ralf Baechle1b223c82006-08-03 21:53:10 +0100189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Ralf Baechle1b223c82006-08-03 21:53:10 +0100191 if (test_thread_flag(TIF_RESTORE_SIGMASK))
192 oldset = &current->saved_sigmask;
193 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 oldset = &current->blocked;
195
196 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle1b223c82006-08-03 21:53:10 +0100197 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
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 }
Ralf Baechle1b223c82006-08-03 21:53:10 +0100222 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Ralf Baechle1b223c82006-08-03 21:53:10 +0100228
229 /*
230 * If there's no signal to deliver, we just put the saved sigmask
231 * back
232 */
233 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
234 clear_thread_flag(TIF_RESTORE_SIGMASK);
235 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239asmlinkage void
240irix_sigreturn(struct pt_regs *regs)
241{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000242 struct sigctx_irix5 __user *context, *magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long umask, mask;
244 u64 *fregs;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000245 u32 usedfp;
246 int error, sig, i, base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 sigset_t blocked;
248
249 /* Always make any pending restarted system calls return -EINTR */
250 current_thread_info()->restart_block.fn = do_no_restart_syscall;
251
252 if (regs->regs[2] == 1000)
253 base = 1;
254
Ralf Baechlefe00f942005-03-01 19:22:29 +0000255 context = (struct sigctx_irix5 __user *) regs->regs[base + 4];
256 magic = (struct sigctx_irix5 __user *) regs->regs[base + 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 sig = (int) regs->regs[base + 6];
258#ifdef DEBUG_SIG
259 printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
260 current->comm, current->pid, context, magic, sig);
261#endif
262 if (!context)
263 context = magic;
264 if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
265 goto badframe;
266
267#ifdef DEBUG_SIG
268 dump_irix5_sigctx(context);
269#endif
270
Ralf Baechlefe00f942005-03-01 19:22:29 +0000271 error = __get_user(regs->cp0_epc, &context->pc);
272 error |= __get_user(umask, &context->rmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Ralf Baechlefe00f942005-03-01 19:22:29 +0000274 mask = 2;
275 for (i = 1; i < 32; i++, mask <<= 1) {
276 if (umask & mask)
277 error |= __get_user(regs->regs[i], &context->regs[i]);
278 }
279 error |= __get_user(regs->hi, &context->hi);
280 error |= __get_user(regs->lo, &context->lo);
281
282 error |= __get_user(usedfp, &context->usedfp);
283 if ((umask & 1) && usedfp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 fregs = (u64 *) &current->thread.fpu;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 for(i = 0; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000287 error |= __get_user(fregs[i], &context->fpregs[i]);
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900288 error |= __get_user(current->thread.fpu.fcr31, &context->fpcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 /* XXX do sigstack crapola here... XXX */
292
Ralf Baechlefe00f942005-03-01 19:22:29 +0000293 error |= __copy_from_user(&blocked, &context->sigset, sizeof(blocked)) ? -EFAULT : 0;
294
295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto badframe;
297
298 sigdelsetmask(&blocked, ~_BLOCKABLE);
299 spin_lock_irq(&current->sighand->siglock);
300 current->blocked = blocked;
301 recalc_sigpending();
302 spin_unlock_irq(&current->sighand->siglock);
303
304 /*
305 * Don't let your children do this ...
306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 __asm__ __volatile__(
308 "move\t$29,%0\n\t"
309 "j\tsyscall_exit"
310 :/* no outputs */
311 :"r" (&regs));
312 /* Unreached */
313
314badframe:
315 force_sig(SIGSEGV, current);
316}
317
318struct sigact_irix5 {
319 int flags;
320 void (*handler)(int);
321 u32 sigset[4];
322 int _unused0[2];
323};
324
325#ifdef DEBUG_SIG
326static inline void dump_sigact_irix5(struct sigact_irix5 *p)
327{
328 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
329 (unsigned long) p->handler,
330 (unsigned long) p->sigset[0]);
331}
332#endif
333
334asmlinkage int
Ralf Baechlefe00f942005-03-01 19:22:29 +0000335irix_sigaction(int sig, const struct sigaction __user *act,
336 struct sigaction __user *oact, void __user *trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct k_sigaction new_ka, old_ka;
339 int ret;
340
341#ifdef DEBUG_SIG
342 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
343 (!old ? "0" : "OLD"), trampoline);
344 if(new) {
345 dump_sigact_irix5(new); printk(" ");
346 }
347#endif
348 if (act) {
349 sigset_t mask;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000350 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Ralf Baechlefe00f942005-03-01 19:22:29 +0000352 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
353 return -EFAULT;
354 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
355 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
356
357 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
358 if (err)
359 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /*
362 * Hmmm... methinks IRIX libc always passes a valid trampoline
363 * value for all invocations of sigaction. Will have to
364 * investigate. POSIX POSIX, die die die...
365 */
366 new_ka.sa_restorer = trampoline;
367 }
368
369/* XXX Implement SIG_SETMASK32 for IRIX compatibility */
370 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
371
372 if (!ret && oact) {
Ralf Baechlefe00f942005-03-01 19:22:29 +0000373 int err;
374
375 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000377
378 err = __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
379 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
380 err |= __copy_to_user(&oact->sa_mask, &old_ka.sa.sa_mask,
381 sizeof(sigset_t)) ? -EFAULT : 0;
382 if (err)
383 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
386 return ret;
387}
388
Ralf Baechlefe00f942005-03-01 19:22:29 +0000389asmlinkage int irix_sigpending(irix_sigset_t __user *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 return do_sigpending(set, sizeof(*set));
392}
393
Ralf Baechlefe00f942005-03-01 19:22:29 +0000394asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
395 irix_sigset_t __user *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 sigset_t oldbits, newbits;
398
399 if (new) {
400 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
401 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000402 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
403 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 sigdelsetmask(&newbits, ~_BLOCKABLE);
405
406 spin_lock_irq(&current->sighand->siglock);
407 oldbits = current->blocked;
408
409 switch(how) {
410 case 1:
411 sigorsets(&newbits, &oldbits, &newbits);
412 break;
413
414 case 2:
415 sigandsets(&newbits, &oldbits, &newbits);
416 break;
417
418 case 3:
419 break;
420
421 case 256:
422 siginitset(&newbits, newbits.sig[0]);
423 break;
424
425 default:
426 return -EINVAL;
427 }
428 recalc_sigpending();
429 spin_unlock_irq(&current->sighand->siglock);
430 }
Ralf Baechlefe00f942005-03-01 19:22:29 +0000431 if (old)
432 return copy_to_user(old, &current->blocked,
433 sizeof(unsigned long)*4) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 return 0;
436}
437
438asmlinkage int irix_sigsuspend(struct pt_regs *regs)
439{
Ralf Baechle1b223c82006-08-03 21:53:10 +0100440 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000441 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Ralf Baechlefe00f942005-03-01 19:22:29 +0000443 uset = (sigset_t __user *) regs->regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
445 return -EFAULT;
446 sigdelsetmask(&newset, ~_BLOCKABLE);
447
448 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle1b223c82006-08-03 21:53:10 +0100449 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 current->blocked = newset;
451 recalc_sigpending();
452 spin_unlock_irq(&current->sighand->siglock);
453
Ralf Baechle1b223c82006-08-03 21:53:10 +0100454 current->state = TASK_INTERRUPTIBLE;
455 schedule();
456 set_thread_flag(TIF_RESTORE_SIGMASK);
457 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/* hate hate hate... */
461struct irix5_siginfo {
462 int sig, code, error;
463 union {
464 char unused[128 - (3 * 4)]; /* Safety net. */
465 struct {
466 int pid;
467 union {
468 int uid;
469 struct {
470 int utime, status, stime;
471 } child;
472 } procdata;
473 } procinfo;
474
475 unsigned long fault_addr;
476
477 struct {
478 int fd;
479 long band;
480 } fileinfo;
481
482 unsigned long sigval;
483 } stuff;
484};
485
Ralf Baechlefe00f942005-03-01 19:22:29 +0000486asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
487 struct irix5_siginfo __user *info, struct timespec __user *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 long expire = MAX_SCHEDULE_TIMEOUT;
490 sigset_t kset;
491 int i, sig, error, timeo = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000492 struct timespec ktp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494#ifdef DEBUG_SIG
495 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
496 current->comm, current->pid, set, info, tp);
497#endif
498
499 /* Must always specify the signal set. */
500 if (!set)
501 return -EINVAL;
502
Ralf Baechlefe00f942005-03-01 19:22:29 +0000503 if (copy_from_user(&kset, set, sizeof(set)))
504 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (info && clear_user(info, sizeof(*info))) {
507 error = -EFAULT;
508 goto out;
509 }
510
511 if (tp) {
Ralf Baechlefe00f942005-03-01 19:22:29 +0000512 if (copy_from_user(&ktp, tp, sizeof(*tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000514
515 if (!ktp.tv_sec && !ktp.tv_nsec)
516 return -EINVAL;
517
518 expire = timespec_to_jiffies(&ktp) +
519 (ktp.tv_sec || ktp.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 while(1) {
523 long tmp = 0;
524
Ralf Baechle0d959c22005-11-05 11:26:43 +0000525 expire = schedule_timeout_interruptible(expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 for (i=0; i<=4; i++)
528 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
529
530 if (tmp)
531 break;
532 if (!expire) {
533 timeo = 1;
534 break;
535 }
536 if (signal_pending(current))
537 return -EINTR;
538 }
539 if (timeo)
540 return -EAGAIN;
541
Ralf Baechlefe00f942005-03-01 19:22:29 +0000542 for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (sigismember (&kset, sig))
544 continue;
545 if (sigismember (&current->pending.signal, sig)) {
546 /* XXX need more than this... */
547 if (info)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000548 return copy_to_user(&info->sig, &sig, sizeof(sig));
549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 }
552
553 /* Should not get here, but do something sane if we do. */
554 error = -EINTR;
555
556out:
557 return error;
558}
559
560/* This is here because of irix5_siginfo definition. */
561#define IRIX_P_PID 0
562#define IRIX_P_PGID 2
563#define IRIX_P_ALL 7
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#define W_EXITED 1
566#define W_TRAPPED 2
567#define W_STOPPED 4
568#define W_CONT 8
569#define W_NOHANG 64
570
571#define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
572
Ralf Baechlefe00f942005-03-01 19:22:29 +0000573asmlinkage int irix_waitsys(int type, int pid,
574 struct irix5_siginfo __user *info, int options,
575 struct rusage __user *ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 int flag, retval;
578 DECLARE_WAITQUEUE(wait, current);
579 struct task_struct *tsk;
580 struct task_struct *p;
581 struct list_head *_p;
582
Ralf Baechlefe00f942005-03-01 19:22:29 +0000583 if (!info)
584 return -EINVAL;
585
586 if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
587 return -EFAULT;
588
589 if (ru)
590 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
591 return -EFAULT;
592
593 if (options & ~W_MASK)
594 return -EINVAL;
595
596 if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
597 return -EINVAL;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 add_wait_queue(&current->signal->wait_chldexit, &wait);
600repeat:
601 flag = 0;
602 current->state = TASK_INTERRUPTIBLE;
603 read_lock(&tasklist_lock);
604 tsk = current;
605 list_for_each(_p,&tsk->children) {
606 p = list_entry(_p,struct task_struct,sibling);
607 if ((type == IRIX_P_PID) && p->pid != pid)
608 continue;
609 if ((type == IRIX_P_PGID) && process_group(p) != pid)
610 continue;
611 if ((p->exit_signal != SIGCHLD))
612 continue;
613 flag = 1;
614 switch (p->state) {
615 case TASK_STOPPED:
616 if (!p->exit_code)
617 continue;
618 if (!(options & (W_TRAPPED|W_STOPPED)) &&
619 !(p->ptrace & PT_PTRACED))
620 continue;
621 read_unlock(&tasklist_lock);
622
623 /* move to end of parent's list to avoid starvation */
624 write_lock_irq(&tasklist_lock);
625 remove_parent(p);
Oleg Nesterov8fafabd2006-03-28 16:11:05 -0800626 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 write_unlock_irq(&tasklist_lock);
628 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000629 if (retval)
630 goto end_waitsys;
631
632 retval = __put_user(SIGCHLD, &info->sig);
633 retval |= __put_user(0, &info->code);
634 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
635 retval |= __put_user((p->exit_code >> 8) & 0xff,
636 &info->stuff.procinfo.procdata.child.status);
637 retval |= __put_user(p->utime, &info->stuff.procinfo.procdata.child.utime);
638 retval |= __put_user(p->stime, &info->stuff.procinfo.procdata.child.stime);
639 if (retval)
640 goto end_waitsys;
641
642 p->exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto end_waitsys;
644
645 case EXIT_ZOMBIE:
646 current->signal->cutime += p->utime + p->signal->cutime;
647 current->signal->cstime += p->stime + p->signal->cstime;
648 if (ru != NULL)
649 getrusage(p, RUSAGE_BOTH, ru);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000650 retval = __put_user(SIGCHLD, &info->sig);
651 retval |= __put_user(1, &info->code); /* CLD_EXITED */
652 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
653 retval |= __put_user((p->exit_code >> 8) & 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 &info->stuff.procinfo.procdata.child.status);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000655 retval |= __put_user(p->utime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 &info->stuff.procinfo.procdata.child.utime);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000657 retval |= __put_user(p->stime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 &info->stuff.procinfo.procdata.child.stime);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000659 if (retval)
660 return retval;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (p->real_parent != p->parent) {
663 write_lock_irq(&tasklist_lock);
664 remove_parent(p);
665 p->parent = p->real_parent;
Oleg Nesterov8fafabd2006-03-28 16:11:05 -0800666 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 do_notify_parent(p, SIGCHLD);
668 write_unlock_irq(&tasklist_lock);
669 } else
670 release_task(p);
671 goto end_waitsys;
672 default:
673 continue;
674 }
675 tsk = next_thread(tsk);
676 }
677 read_unlock(&tasklist_lock);
678 if (flag) {
679 retval = 0;
680 if (options & W_NOHANG)
681 goto end_waitsys;
682 retval = -ERESTARTSYS;
683 if (signal_pending(current))
684 goto end_waitsys;
685 current->state = TASK_INTERRUPTIBLE;
686 schedule();
687 goto repeat;
688 }
689 retval = -ECHILD;
690end_waitsys:
691 current->state = TASK_RUNNING;
692 remove_wait_queue(&current->signal->wait_chldexit, &wait);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return retval;
695}
696
697struct irix5_context {
698 u32 flags;
699 u32 link;
700 u32 sigmask[4];
701 struct { u32 sp, size, flags; } stack;
702 int regs[36];
703 u32 fpregs[32];
704 u32 fpcsr;
705 u32 _unused0;
706 u32 _unused1[47];
707 u32 weird_graphics_thing;
708};
709
710asmlinkage int irix_getcontext(struct pt_regs *regs)
711{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000712 int error, i, base = 0;
713 struct irix5_context __user *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 unsigned long flags;
715
716 if (regs->regs[2] == 1000)
717 base = 1;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000718 ctx = (struct irix5_context __user *) regs->regs[base + 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720#ifdef DEBUG_SIG
721 printk("[%s:%d] irix_getcontext(%p)\n",
722 current->comm, current->pid, ctx);
723#endif
724
Ralf Baechlefe00f942005-03-01 19:22:29 +0000725 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EFAULT;
727
Ralf Baechlefe00f942005-03-01 19:22:29 +0000728 error = __put_user(current->thread.irix_oldctx, &ctx->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Ralf Baechlefe00f942005-03-01 19:22:29 +0000730 error |= __copy_to_user(&ctx->sigmask, &current->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* XXX Do sigstack stuff someday... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000733 error |= __put_user(0, &ctx->stack.sp);
734 error |= __put_user(0, &ctx->stack.size);
735 error |= __put_user(0, &ctx->stack.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Ralf Baechlefe00f942005-03-01 19:22:29 +0000737 error |= __put_user(0, &ctx->weird_graphics_thing);
738 error |= __put_user(0, &ctx->regs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 for (i = 1; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000740 error |= __put_user(regs->regs[i], &ctx->regs[i]);
741 error |= __put_user(regs->lo, &ctx->regs[32]);
742 error |= __put_user(regs->hi, &ctx->regs[33]);
743 error |= __put_user(regs->cp0_cause, &ctx->regs[34]);
744 error |= __put_user(regs->cp0_epc, &ctx->regs[35]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 flags = 0x0f;
747 if (!used_math()) {
748 flags &= ~(0x08);
749 } else {
750 /* XXX wheee... */
751 printk("Wheee, no code for saving IRIX FPU context yet.\n");
752 }
Ralf Baechlefe00f942005-03-01 19:22:29 +0000753 error |= __put_user(flags, &ctx->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Ralf Baechlefe00f942005-03-01 19:22:29 +0000755 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Ralf Baechlefe00f942005-03-01 19:22:29 +0000758asmlinkage void irix_setcontext(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000760 struct irix5_context __user *ctx;
761 int err, base = 0;
762 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Ralf Baechlefe00f942005-03-01 19:22:29 +0000764 if (regs->regs[2] == 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 base = 1;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000766 ctx = (struct irix5_context __user *) regs->regs[base + 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768#ifdef DEBUG_SIG
769 printk("[%s:%d] irix_setcontext(%p)\n",
770 current->comm, current->pid, ctx);
771#endif
772
Ralf Baechlefe00f942005-03-01 19:22:29 +0000773 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
774 goto segv_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Ralf Baechlefe00f942005-03-01 19:22:29 +0000776 err = __get_user(flags, &ctx->flags);
777 if (flags & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* XXX sigstack garbage, todo... */
779 printk("Wheee, cannot do sigstack stuff in setcontext\n");
780 }
781
Ralf Baechlefe00f942005-03-01 19:22:29 +0000782 if (flags & 0x04) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int i;
784
785 /* XXX extra control block stuff... todo... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000786 for (i = 1; i < 32; i++)
787 err |= __get_user(regs->regs[i], &ctx->regs[i]);
788 err |= __get_user(regs->lo, &ctx->regs[32]);
789 err |= __get_user(regs->hi, &ctx->regs[33]);
790 err |= __get_user(regs->cp0_epc, &ctx->regs[35]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Ralf Baechlefe00f942005-03-01 19:22:29 +0000793 if (flags & 0x08)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* XXX fpu context, blah... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000795 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Ralf Baechlefe00f942005-03-01 19:22:29 +0000797 err |= __get_user(current->thread.irix_oldctx, &ctx->link);
798 if (err)
799 goto segv_and_exit;
800
801 /*
802 * Don't let your children do this ...
803 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000804 __asm__ __volatile__(
805 "move\t$29,%0\n\t"
806 "j\tsyscall_exit"
807 :/* no outputs */
808 :"r" (&regs));
809 /* Unreached */
810
811segv_and_exit:
812 force_sigsegv(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Ralf Baechlefe00f942005-03-01 19:22:29 +0000815struct irix_sigstack {
816 unsigned long sp;
817 int status;
818};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Ralf Baechlefe00f942005-03-01 19:22:29 +0000820asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
821 struct irix_sigstack __user *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#ifdef DEBUG_SIG
824 printk("[%s:%d] irix_sigstack(%p,%p)\n",
825 current->comm, current->pid, new, old);
826#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (new) {
828 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
Ralf Baechlefe00f942005-03-01 19:22:29 +0000829 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 if (old) {
833 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
Ralf Baechlefe00f942005-03-01 19:22:29 +0000834 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Ralf Baechlefe00f942005-03-01 19:22:29 +0000837 return 0;
838}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Ralf Baechlefe00f942005-03-01 19:22:29 +0000840struct irix_sigaltstack { unsigned long sp; int size; int status; };
841
842asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
843 struct irix_sigaltstack __user *old)
844{
845#ifdef DEBUG_SIG
846 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
847 current->comm, current->pid, new, old);
848#endif
849 if (new)
850 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
851 return -EFAULT;
852
853 if (old) {
854 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
855 return -EFAULT;
856 }
857
858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861struct irix_procset {
862 int cmd, ltype, lid, rtype, rid;
863};
864
Ralf Baechlefe00f942005-03-01 19:22:29 +0000865asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
868 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#ifdef DEBUG_SIG
870 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
871 current->comm, current->pid,
872 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,
873 sig);
874#endif
875 return -EINVAL;
876}