blob: 719364752e7758c3b936203951d4dcd8b5e2f149 [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 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100227 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Ralf Baechle1b223c82006-08-03 21:53:10 +0100229
230 /*
231 * If there's no signal to deliver, we just put the saved sigmask
232 * back
233 */
234 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
235 clear_thread_flag(TIF_RESTORE_SIGMASK);
236 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240asmlinkage void
241irix_sigreturn(struct pt_regs *regs)
242{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000243 struct sigctx_irix5 __user *context, *magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned long umask, mask;
245 u64 *fregs;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000246 u32 usedfp;
247 int error, sig, i, base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 sigset_t blocked;
249
250 /* Always make any pending restarted system calls return -EINTR */
251 current_thread_info()->restart_block.fn = do_no_restart_syscall;
252
253 if (regs->regs[2] == 1000)
254 base = 1;
255
Ralf Baechlefe00f942005-03-01 19:22:29 +0000256 context = (struct sigctx_irix5 __user *) regs->regs[base + 4];
257 magic = (struct sigctx_irix5 __user *) regs->regs[base + 5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sig = (int) regs->regs[base + 6];
259#ifdef DEBUG_SIG
260 printk("[%s:%d] IRIX sigreturn(scp[%p],ucp[%p],sig[%d])\n",
261 current->comm, current->pid, context, magic, sig);
262#endif
263 if (!context)
264 context = magic;
265 if (!access_ok(VERIFY_READ, context, sizeof(struct sigctx_irix5)))
266 goto badframe;
267
268#ifdef DEBUG_SIG
269 dump_irix5_sigctx(context);
270#endif
271
Ralf Baechlefe00f942005-03-01 19:22:29 +0000272 error = __get_user(regs->cp0_epc, &context->pc);
273 error |= __get_user(umask, &context->rmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Ralf Baechlefe00f942005-03-01 19:22:29 +0000275 mask = 2;
276 for (i = 1; i < 32; i++, mask <<= 1) {
277 if (umask & mask)
278 error |= __get_user(regs->regs[i], &context->regs[i]);
279 }
280 error |= __get_user(regs->hi, &context->hi);
281 error |= __get_user(regs->lo, &context->lo);
282
283 error |= __get_user(usedfp, &context->usedfp);
284 if ((umask & 1) && usedfp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 fregs = (u64 *) &current->thread.fpu;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 for(i = 0; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000288 error |= __get_user(fregs[i], &context->fpregs[i]);
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900289 error |= __get_user(current->thread.fpu.fcr31, &context->fpcsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
292 /* XXX do sigstack crapola here... XXX */
293
Ralf Baechlefe00f942005-03-01 19:22:29 +0000294 error |= __copy_from_user(&blocked, &context->sigset, sizeof(blocked)) ? -EFAULT : 0;
295
296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto badframe;
298
299 sigdelsetmask(&blocked, ~_BLOCKABLE);
300 spin_lock_irq(&current->sighand->siglock);
301 current->blocked = blocked;
302 recalc_sigpending();
303 spin_unlock_irq(&current->sighand->siglock);
304
305 /*
306 * Don't let your children do this ...
307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 __asm__ __volatile__(
309 "move\t$29,%0\n\t"
310 "j\tsyscall_exit"
311 :/* no outputs */
312 :"r" (&regs));
313 /* Unreached */
314
315badframe:
316 force_sig(SIGSEGV, current);
317}
318
319struct sigact_irix5 {
320 int flags;
321 void (*handler)(int);
322 u32 sigset[4];
323 int _unused0[2];
324};
325
326#ifdef DEBUG_SIG
327static inline void dump_sigact_irix5(struct sigact_irix5 *p)
328{
329 printk("<f[%d] hndlr[%08lx] msk[%08lx]>", p->flags,
330 (unsigned long) p->handler,
331 (unsigned long) p->sigset[0]);
332}
333#endif
334
335asmlinkage int
Ralf Baechlefe00f942005-03-01 19:22:29 +0000336irix_sigaction(int sig, const struct sigaction __user *act,
337 struct sigaction __user *oact, void __user *trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct k_sigaction new_ka, old_ka;
340 int ret;
341
342#ifdef DEBUG_SIG
343 printk(" (%d,%s,%s,%08lx) ", sig, (!new ? "0" : "NEW"),
344 (!old ? "0" : "OLD"), trampoline);
345 if(new) {
346 dump_sigact_irix5(new); printk(" ");
347 }
348#endif
349 if (act) {
350 sigset_t mask;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Ralf Baechlefe00f942005-03-01 19:22:29 +0000353 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
354 return -EFAULT;
355 err = __get_user(new_ka.sa.sa_handler, &act->sa_handler);
356 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
357
358 err |= __copy_from_user(&mask, &act->sa_mask, sizeof(sigset_t)) ? -EFAULT : 0;
359 if (err)
360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /*
363 * Hmmm... methinks IRIX libc always passes a valid trampoline
364 * value for all invocations of sigaction. Will have to
365 * investigate. POSIX POSIX, die die die...
366 */
367 new_ka.sa_restorer = trampoline;
368 }
369
370/* XXX Implement SIG_SETMASK32 for IRIX compatibility */
371 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
372
373 if (!ret && oact) {
Ralf Baechlefe00f942005-03-01 19:22:29 +0000374 int err;
375
376 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000378
379 err = __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
380 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
381 err |= __copy_to_user(&oact->sa_mask, &old_ka.sa.sa_mask,
382 sizeof(sigset_t)) ? -EFAULT : 0;
383 if (err)
384 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
387 return ret;
388}
389
Ralf Baechlefe00f942005-03-01 19:22:29 +0000390asmlinkage int irix_sigpending(irix_sigset_t __user *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 return do_sigpending(set, sizeof(*set));
393}
394
Ralf Baechlefe00f942005-03-01 19:22:29 +0000395asmlinkage int irix_sigprocmask(int how, irix_sigset_t __user *new,
396 irix_sigset_t __user *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 sigset_t oldbits, newbits;
399
400 if (new) {
401 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
402 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000403 if (__copy_from_user(&newbits, new, sizeof(unsigned long)*4))
404 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sigdelsetmask(&newbits, ~_BLOCKABLE);
406
407 spin_lock_irq(&current->sighand->siglock);
408 oldbits = current->blocked;
409
410 switch(how) {
411 case 1:
412 sigorsets(&newbits, &oldbits, &newbits);
413 break;
414
415 case 2:
416 sigandsets(&newbits, &oldbits, &newbits);
417 break;
418
419 case 3:
420 break;
421
422 case 256:
423 siginitset(&newbits, newbits.sig[0]);
424 break;
425
426 default:
427 return -EINVAL;
428 }
429 recalc_sigpending();
430 spin_unlock_irq(&current->sighand->siglock);
431 }
Ralf Baechlefe00f942005-03-01 19:22:29 +0000432 if (old)
433 return copy_to_user(old, &current->blocked,
434 sizeof(unsigned long)*4) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return 0;
437}
438
439asmlinkage int irix_sigsuspend(struct pt_regs *regs)
440{
Ralf Baechle1b223c82006-08-03 21:53:10 +0100441 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000442 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Ralf Baechlefe00f942005-03-01 19:22:29 +0000444 uset = (sigset_t __user *) regs->regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
446 return -EFAULT;
447 sigdelsetmask(&newset, ~_BLOCKABLE);
448
449 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle1b223c82006-08-03 21:53:10 +0100450 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 current->blocked = newset;
452 recalc_sigpending();
453 spin_unlock_irq(&current->sighand->siglock);
454
Ralf Baechle1b223c82006-08-03 21:53:10 +0100455 current->state = TASK_INTERRUPTIBLE;
456 schedule();
457 set_thread_flag(TIF_RESTORE_SIGMASK);
458 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461/* hate hate hate... */
462struct irix5_siginfo {
463 int sig, code, error;
464 union {
465 char unused[128 - (3 * 4)]; /* Safety net. */
466 struct {
467 int pid;
468 union {
469 int uid;
470 struct {
471 int utime, status, stime;
472 } child;
473 } procdata;
474 } procinfo;
475
476 unsigned long fault_addr;
477
478 struct {
479 int fd;
480 long band;
481 } fileinfo;
482
483 unsigned long sigval;
484 } stuff;
485};
486
Ralf Baechlefe00f942005-03-01 19:22:29 +0000487asmlinkage int irix_sigpoll_sys(unsigned long __user *set,
488 struct irix5_siginfo __user *info, struct timespec __user *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 long expire = MAX_SCHEDULE_TIMEOUT;
491 sigset_t kset;
492 int i, sig, error, timeo = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000493 struct timespec ktp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495#ifdef DEBUG_SIG
496 printk("[%s:%d] irix_sigpoll_sys(%p,%p,%p)\n",
497 current->comm, current->pid, set, info, tp);
498#endif
499
500 /* Must always specify the signal set. */
501 if (!set)
502 return -EINVAL;
503
Ralf Baechlefe00f942005-03-01 19:22:29 +0000504 if (copy_from_user(&kset, set, sizeof(set)))
505 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (info && clear_user(info, sizeof(*info))) {
508 error = -EFAULT;
509 goto out;
510 }
511
512 if (tp) {
Ralf Baechlefe00f942005-03-01 19:22:29 +0000513 if (copy_from_user(&ktp, tp, sizeof(*tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -EFAULT;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000515
516 if (!ktp.tv_sec && !ktp.tv_nsec)
517 return -EINVAL;
518
519 expire = timespec_to_jiffies(&ktp) +
520 (ktp.tv_sec || ktp.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
523 while(1) {
524 long tmp = 0;
525
Ralf Baechle0d959c22005-11-05 11:26:43 +0000526 expire = schedule_timeout_interruptible(expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 for (i=0; i<=4; i++)
529 tmp |= (current->pending.signal.sig[i] & kset.sig[i]);
530
531 if (tmp)
532 break;
533 if (!expire) {
534 timeo = 1;
535 break;
536 }
537 if (signal_pending(current))
538 return -EINTR;
539 }
540 if (timeo)
541 return -EAGAIN;
542
Ralf Baechlefe00f942005-03-01 19:22:29 +0000543 for (sig = 1; i <= 65 /* IRIX_NSIG */; sig++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (sigismember (&kset, sig))
545 continue;
546 if (sigismember (&current->pending.signal, sig)) {
547 /* XXX need more than this... */
548 if (info)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000549 return copy_to_user(&info->sig, &sig, sizeof(sig));
550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 }
553
554 /* Should not get here, but do something sane if we do. */
555 error = -EINTR;
556
557out:
558 return error;
559}
560
561/* This is here because of irix5_siginfo definition. */
562#define IRIX_P_PID 0
563#define IRIX_P_PGID 2
564#define IRIX_P_ALL 7
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#define W_EXITED 1
567#define W_TRAPPED 2
568#define W_STOPPED 4
569#define W_CONT 8
570#define W_NOHANG 64
571
572#define W_MASK (W_EXITED | W_TRAPPED | W_STOPPED | W_CONT | W_NOHANG)
573
Ralf Baechlefe00f942005-03-01 19:22:29 +0000574asmlinkage int irix_waitsys(int type, int pid,
575 struct irix5_siginfo __user *info, int options,
576 struct rusage __user *ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 int flag, retval;
579 DECLARE_WAITQUEUE(wait, current);
580 struct task_struct *tsk;
581 struct task_struct *p;
582 struct list_head *_p;
583
Ralf Baechlefe00f942005-03-01 19:22:29 +0000584 if (!info)
585 return -EINVAL;
586
587 if (!access_ok(VERIFY_WRITE, info, sizeof(*info)))
588 return -EFAULT;
589
590 if (ru)
591 if (!access_ok(VERIFY_WRITE, ru, sizeof(*ru)))
592 return -EFAULT;
593
594 if (options & ~W_MASK)
595 return -EINVAL;
596
597 if (type != IRIX_P_PID && type != IRIX_P_PGID && type != IRIX_P_ALL)
598 return -EINVAL;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 add_wait_queue(&current->signal->wait_chldexit, &wait);
601repeat:
602 flag = 0;
603 current->state = TASK_INTERRUPTIBLE;
604 read_lock(&tasklist_lock);
605 tsk = current;
606 list_for_each(_p,&tsk->children) {
607 p = list_entry(_p,struct task_struct,sibling);
608 if ((type == IRIX_P_PID) && p->pid != pid)
609 continue;
610 if ((type == IRIX_P_PGID) && process_group(p) != pid)
611 continue;
612 if ((p->exit_signal != SIGCHLD))
613 continue;
614 flag = 1;
615 switch (p->state) {
616 case TASK_STOPPED:
617 if (!p->exit_code)
618 continue;
619 if (!(options & (W_TRAPPED|W_STOPPED)) &&
620 !(p->ptrace & PT_PTRACED))
621 continue;
622 read_unlock(&tasklist_lock);
623
624 /* move to end of parent's list to avoid starvation */
625 write_lock_irq(&tasklist_lock);
626 remove_parent(p);
Oleg Nesterov8fafabd2006-03-28 16:11:05 -0800627 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 write_unlock_irq(&tasklist_lock);
629 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000630 if (retval)
631 goto end_waitsys;
632
633 retval = __put_user(SIGCHLD, &info->sig);
634 retval |= __put_user(0, &info->code);
635 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
636 retval |= __put_user((p->exit_code >> 8) & 0xff,
637 &info->stuff.procinfo.procdata.child.status);
638 retval |= __put_user(p->utime, &info->stuff.procinfo.procdata.child.utime);
639 retval |= __put_user(p->stime, &info->stuff.procinfo.procdata.child.stime);
640 if (retval)
641 goto end_waitsys;
642
643 p->exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto end_waitsys;
645
646 case EXIT_ZOMBIE:
647 current->signal->cutime += p->utime + p->signal->cutime;
648 current->signal->cstime += p->stime + p->signal->cstime;
649 if (ru != NULL)
650 getrusage(p, RUSAGE_BOTH, ru);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000651 retval = __put_user(SIGCHLD, &info->sig);
652 retval |= __put_user(1, &info->code); /* CLD_EXITED */
653 retval |= __put_user(p->pid, &info->stuff.procinfo.pid);
654 retval |= __put_user((p->exit_code >> 8) & 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 &info->stuff.procinfo.procdata.child.status);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000656 retval |= __put_user(p->utime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 &info->stuff.procinfo.procdata.child.utime);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000658 retval |= __put_user(p->stime,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 &info->stuff.procinfo.procdata.child.stime);
Ralf Baechlefe00f942005-03-01 19:22:29 +0000660 if (retval)
661 return retval;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (p->real_parent != p->parent) {
664 write_lock_irq(&tasklist_lock);
665 remove_parent(p);
666 p->parent = p->real_parent;
Oleg Nesterov8fafabd2006-03-28 16:11:05 -0800667 add_parent(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 do_notify_parent(p, SIGCHLD);
669 write_unlock_irq(&tasklist_lock);
670 } else
671 release_task(p);
672 goto end_waitsys;
673 default:
674 continue;
675 }
676 tsk = next_thread(tsk);
677 }
678 read_unlock(&tasklist_lock);
679 if (flag) {
680 retval = 0;
681 if (options & W_NOHANG)
682 goto end_waitsys;
683 retval = -ERESTARTSYS;
684 if (signal_pending(current))
685 goto end_waitsys;
686 current->state = TASK_INTERRUPTIBLE;
687 schedule();
688 goto repeat;
689 }
690 retval = -ECHILD;
691end_waitsys:
692 current->state = TASK_RUNNING;
693 remove_wait_queue(&current->signal->wait_chldexit, &wait);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return retval;
696}
697
698struct irix5_context {
699 u32 flags;
700 u32 link;
701 u32 sigmask[4];
702 struct { u32 sp, size, flags; } stack;
703 int regs[36];
704 u32 fpregs[32];
705 u32 fpcsr;
706 u32 _unused0;
707 u32 _unused1[47];
708 u32 weird_graphics_thing;
709};
710
711asmlinkage int irix_getcontext(struct pt_regs *regs)
712{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000713 int error, i, base = 0;
714 struct irix5_context __user *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned long flags;
716
717 if (regs->regs[2] == 1000)
718 base = 1;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000719 ctx = (struct irix5_context __user *) regs->regs[base + 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721#ifdef DEBUG_SIG
722 printk("[%s:%d] irix_getcontext(%p)\n",
723 current->comm, current->pid, ctx);
724#endif
725
Ralf Baechlefe00f942005-03-01 19:22:29 +0000726 if (!access_ok(VERIFY_WRITE, ctx, sizeof(*ctx)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EFAULT;
728
Ralf Baechlefe00f942005-03-01 19:22:29 +0000729 error = __put_user(current->thread.irix_oldctx, &ctx->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Ralf Baechlefe00f942005-03-01 19:22:29 +0000731 error |= __copy_to_user(&ctx->sigmask, &current->blocked, sizeof(irix_sigset_t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* XXX Do sigstack stuff someday... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000734 error |= __put_user(0, &ctx->stack.sp);
735 error |= __put_user(0, &ctx->stack.size);
736 error |= __put_user(0, &ctx->stack.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Ralf Baechlefe00f942005-03-01 19:22:29 +0000738 error |= __put_user(0, &ctx->weird_graphics_thing);
739 error |= __put_user(0, &ctx->regs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 for (i = 1; i < 32; i++)
Ralf Baechlefe00f942005-03-01 19:22:29 +0000741 error |= __put_user(regs->regs[i], &ctx->regs[i]);
742 error |= __put_user(regs->lo, &ctx->regs[32]);
743 error |= __put_user(regs->hi, &ctx->regs[33]);
744 error |= __put_user(regs->cp0_cause, &ctx->regs[34]);
745 error |= __put_user(regs->cp0_epc, &ctx->regs[35]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 flags = 0x0f;
748 if (!used_math()) {
749 flags &= ~(0x08);
750 } else {
751 /* XXX wheee... */
752 printk("Wheee, no code for saving IRIX FPU context yet.\n");
753 }
Ralf Baechlefe00f942005-03-01 19:22:29 +0000754 error |= __put_user(flags, &ctx->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Ralf Baechlefe00f942005-03-01 19:22:29 +0000756 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Ralf Baechlefe00f942005-03-01 19:22:29 +0000759asmlinkage void irix_setcontext(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000761 struct irix5_context __user *ctx;
762 int err, base = 0;
763 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Ralf Baechlefe00f942005-03-01 19:22:29 +0000765 if (regs->regs[2] == 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 base = 1;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000767 ctx = (struct irix5_context __user *) regs->regs[base + 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769#ifdef DEBUG_SIG
770 printk("[%s:%d] irix_setcontext(%p)\n",
771 current->comm, current->pid, ctx);
772#endif
773
Ralf Baechlefe00f942005-03-01 19:22:29 +0000774 if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)))
775 goto segv_and_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Ralf Baechlefe00f942005-03-01 19:22:29 +0000777 err = __get_user(flags, &ctx->flags);
778 if (flags & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* XXX sigstack garbage, todo... */
780 printk("Wheee, cannot do sigstack stuff in setcontext\n");
781 }
782
Ralf Baechlefe00f942005-03-01 19:22:29 +0000783 if (flags & 0x04) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int i;
785
786 /* XXX extra control block stuff... todo... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000787 for (i = 1; i < 32; i++)
788 err |= __get_user(regs->regs[i], &ctx->regs[i]);
789 err |= __get_user(regs->lo, &ctx->regs[32]);
790 err |= __get_user(regs->hi, &ctx->regs[33]);
791 err |= __get_user(regs->cp0_epc, &ctx->regs[35]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Ralf Baechlefe00f942005-03-01 19:22:29 +0000794 if (flags & 0x08)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* XXX fpu context, blah... */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000796 printk(KERN_ERR "Wheee, cannot restore FPU context yet...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Ralf Baechlefe00f942005-03-01 19:22:29 +0000798 err |= __get_user(current->thread.irix_oldctx, &ctx->link);
799 if (err)
800 goto segv_and_exit;
801
802 /*
803 * Don't let your children do this ...
804 */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000805 __asm__ __volatile__(
806 "move\t$29,%0\n\t"
807 "j\tsyscall_exit"
808 :/* no outputs */
809 :"r" (&regs));
810 /* Unreached */
811
812segv_and_exit:
813 force_sigsegv(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Ralf Baechlefe00f942005-03-01 19:22:29 +0000816struct irix_sigstack {
817 unsigned long sp;
818 int status;
819};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Ralf Baechlefe00f942005-03-01 19:22:29 +0000821asmlinkage int irix_sigstack(struct irix_sigstack __user *new,
822 struct irix_sigstack __user *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824#ifdef DEBUG_SIG
825 printk("[%s:%d] irix_sigstack(%p,%p)\n",
826 current->comm, current->pid, new, old);
827#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (new) {
829 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
Ralf Baechlefe00f942005-03-01 19:22:29 +0000830 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 if (old) {
834 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
Ralf Baechlefe00f942005-03-01 19:22:29 +0000835 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Ralf Baechlefe00f942005-03-01 19:22:29 +0000838 return 0;
839}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Ralf Baechlefe00f942005-03-01 19:22:29 +0000841struct irix_sigaltstack { unsigned long sp; int size; int status; };
842
843asmlinkage int irix_sigaltstack(struct irix_sigaltstack __user *new,
844 struct irix_sigaltstack __user *old)
845{
846#ifdef DEBUG_SIG
847 printk("[%s:%d] irix_sigaltstack(%p,%p)\n",
848 current->comm, current->pid, new, old);
849#endif
850 if (new)
851 if (!access_ok(VERIFY_READ, new, sizeof(*new)))
852 return -EFAULT;
853
854 if (old) {
855 if (!access_ok(VERIFY_WRITE, old, sizeof(*old)))
856 return -EFAULT;
857 }
858
859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862struct irix_procset {
863 int cmd, ltype, lid, rtype, rid;
864};
865
Ralf Baechlefe00f942005-03-01 19:22:29 +0000866asmlinkage int irix_sigsendset(struct irix_procset __user *pset, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 if (!access_ok(VERIFY_READ, pset, sizeof(*pset)))
869 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#ifdef DEBUG_SIG
871 printk("[%s:%d] irix_sigsendset([%d,%d,%d,%d,%d],%d)\n",
872 current->comm, current->pid,
873 pset->cmd, pset->ltype, pset->lid, pset->rtype, pset->rid,
874 sig);
875#endif
876 return -EINVAL;
877}