1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20
21 #include <asm/asm.h>
22 #include <asm/bitops.h>
23 #include <asm/pgalloc.h>
24 #include <asm/stackframe.h>
25 #include <asm/uaccess.h>
26 #include <asm/ucontext.h>
27 #include <asm/system.h>
28 #include <asm/fpu.h>
29
30 /*
31 * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
32 */
33 #define __NR_O32_sigreturn 4119
34 #define __NR_O32_rt_sigreturn 4193
35 #define __NR_O32_restart_syscall 4253
36
37 #define DEBUG_SIG 0
38
39 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40
41 extern asmlinkage int do_signal32(sigset_t *oldset, struct pt_regs *regs);
42
43 extern asmlinkage void syscall_trace(void);
44
45 /* 32-bit compatibility types */
46
47 #define _NSIG32_BPW 32
48 #define _NSIG32_WORDS (_NSIG / _NSIG32_BPW)
49
50 typedef struct {
51 unsigned int sig[_NSIG32_WORDS];
52 } sigset32_t;
53
54 typedef unsigned int __sighandler32_t;
55 typedef void (*vfptr_t)(void);
56
57 struct sigaction32 {
58 unsigned int sa_flags;
59 __sighandler32_t sa_handler;
60 sigset32_t sa_mask;
61 };
62
63 /* IRIX compatible stack_t */
64 typedef struct sigaltstack32 {
65 s32 ss_sp;
66 __kernel_size_t32 ss_size;
67 int ss_flags;
68 } stack32_t;
69
70 typedef union sigval32 {
71 int sival_int;
72 s32 sival_ptr;
73 } sigval_t32;
74
75 #define SI_PAD_SIZE32 ((SI_MAX_SIZE/sizeof(int)) - 3)
76
77 typedef struct siginfo32 {
78 int si_signo;
79 int si_code;
80 int si_errno;
81
82 union {
83 int _pad[SI_PAD_SIZE32];
84
85 /* kill() */
86 struct {
87 __kernel_pid_t32 _pid; /* sender's pid */
88 __kernel_uid_t32 _uid; /* sender's uid */
89 } _kill;
90
91 /* SIGCHLD */
92 struct {
93 __kernel_pid_t32 _pid; /* which child */
94 __kernel_uid_t32 _uid; /* sender's uid */
95 int _status; /* exit code */
96 __kernel_clock_t32 _utime;
97 __kernel_clock_t32 _stime;
98 } _sigchld;
99
100 /* IRIX SIGCHLD */
101 struct {
102 __kernel_pid_t32 _pid; /* which child */
103 __kernel_clock_t32 _utime;
104 int _status; /* exit code */
105 __kernel_clock_t32 _stime;
106 } _irix_sigchld;
107
108 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
109 struct {
110 s32 _addr; /* faulting insn/memory ref. */
111 } _sigfault;
112
113 /* SIGPOLL, SIGXFSZ (To do ...) */
114 struct {
115 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
116 int _fd;
117 } _sigpoll;
118
119 /* POSIX.1b timers */
120 struct {
121 unsigned int _timer1;
122 unsigned int _timer2;
123 } _timer;
124
125 /* POSIX.1b signals */
126 struct {
127 __kernel_pid_t32 _pid; /* sender's pid */
128 __kernel_uid_t32 _uid; /* sender's uid */
129 sigval_t32 _sigval;
130 } _rt;
131
132 } _sifields;
133 } siginfo_t32;
134
135 struct ucontext32 {
136 u32 uc_flags;
137 s32 uc_link;
138 stack32_t uc_stack;
139 struct sigcontext32 uc_mcontext;
140 sigset_t32 uc_sigmask; /* mask last for extensibility */
141 };
142
143 extern void __put_sigset_unknown_nsig(void);
144 extern void __get_sigset_unknown_nsig(void);
145
put_sigset(const sigset_t * kbuf,sigset32_t * ubuf)146 static inline int put_sigset(const sigset_t *kbuf, sigset32_t *ubuf)
147 {
148 int err = 0;
149
150 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
151 return -EFAULT;
152
153 switch (_NSIG_WORDS) {
154 default:
155 __put_sigset_unknown_nsig();
156 case 2:
157 err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]);
158 err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
159 case 1:
160 err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]);
161 err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
162 }
163
164 return err;
165 }
166
get_sigset(sigset_t * kbuf,const sigset32_t * ubuf)167 static inline int get_sigset(sigset_t *kbuf, const sigset32_t *ubuf)
168 {
169 int err = 0;
170 unsigned long sig[4];
171
172 if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
173 return -EFAULT;
174
175 switch (_NSIG_WORDS) {
176 default:
177 __get_sigset_unknown_nsig();
178 case 2:
179 err |= __get_user (sig[3], &ubuf->sig[3]);
180 err |= __get_user (sig[2], &ubuf->sig[2]);
181 kbuf->sig[1] = sig[2] | (sig[3] << 32);
182 case 1:
183 err |= __get_user (sig[1], &ubuf->sig[1]);
184 err |= __get_user (sig[0], &ubuf->sig[0]);
185 kbuf->sig[0] = sig[0] | (sig[1] << 32);
186 }
187
188 return err;
189 }
190
191 /*
192 * Atomically swap in the new signal mask, and wait for a signal.
193 */
194 save_static_function(sys32_sigsuspend);
_sys32_sigsuspend(abi64_no_regargs,struct pt_regs regs)195 static_unused int _sys32_sigsuspend(abi64_no_regargs, struct pt_regs regs)
196 {
197 sigset32_t *uset;
198 sigset_t newset, saveset;
199
200 uset = (sigset32_t *) regs.regs[4];
201 if (get_sigset(&newset, uset))
202 return -EFAULT;
203 sigdelsetmask(&newset, ~_BLOCKABLE);
204
205 spin_lock_irq(¤t->sigmask_lock);
206 saveset = current->blocked;
207 current->blocked = newset;
208 recalc_sigpending(current);
209 spin_unlock_irq(¤t->sigmask_lock);
210
211 regs.regs[2] = EINTR;
212 regs.regs[7] = 1;
213 while (1) {
214 current->state = TASK_INTERRUPTIBLE;
215 schedule();
216 if (do_signal32(&saveset, ®s))
217 return -EINTR;
218 }
219 }
220
221 save_static_function(sys32_rt_sigsuspend);
_sys32_rt_sigsuspend(abi64_no_regargs,struct pt_regs regs)222 static_unused int _sys32_rt_sigsuspend(abi64_no_regargs, struct pt_regs regs)
223 {
224 sigset32_t *uset;
225 sigset_t newset, saveset;
226 size_t sigsetsize;
227
228 /* XXX Don't preclude handling different sized sigset_t's. */
229 sigsetsize = regs.regs[5];
230 if (sigsetsize != sizeof(sigset32_t))
231 return -EINVAL;
232
233 uset = (sigset32_t *) regs.regs[4];
234 if (get_sigset(&newset, uset))
235 return -EFAULT;
236 sigdelsetmask(&newset, ~_BLOCKABLE);
237
238 spin_lock_irq(¤t->sigmask_lock);
239 saveset = current->blocked;
240 current->blocked = newset;
241 recalc_sigpending(current);
242 spin_unlock_irq(¤t->sigmask_lock);
243
244 regs.regs[2] = EINTR;
245 regs.regs[7] = 1;
246 while (1) {
247 current->state = TASK_INTERRUPTIBLE;
248 schedule();
249 if (do_signal32(&saveset, ®s))
250 return -EINTR;
251 }
252 }
253
sys32_sigaction(int sig,const struct sigaction32 * act,struct sigaction32 * oact)254 asmlinkage int sys32_sigaction(int sig, const struct sigaction32 *act,
255 struct sigaction32 *oact)
256 {
257 struct k_sigaction new_ka, old_ka;
258 int ret;
259 int err = 0;
260
261 if (act) {
262 old_sigset_t mask;
263
264 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
265 return -EFAULT;
266 err |= __get_user((u32)(u64)new_ka.sa.sa_handler,
267 &act->sa_handler);
268 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
269 err |= __get_user(mask, &act->sa_mask.sig[0]);
270 if (err)
271 return -EFAULT;
272
273 siginitset(&new_ka.sa.sa_mask, mask);
274 }
275
276 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
277
278 if (!ret && oact) {
279 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
280 return -EFAULT;
281 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
282 err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
283 &oact->sa_handler);
284 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
285 err |= __put_user(0, &oact->sa_mask.sig[1]);
286 err |= __put_user(0, &oact->sa_mask.sig[2]);
287 err |= __put_user(0, &oact->sa_mask.sig[3]);
288 if (err)
289 return -EFAULT;
290 }
291
292 return ret;
293 }
294
sys32_sigaltstack(abi64_no_regargs,struct pt_regs regs)295 asmlinkage int sys32_sigaltstack(abi64_no_regargs, struct pt_regs regs)
296 {
297 const stack32_t *uss = (const stack32_t *) regs.regs[4];
298 stack32_t *uoss = (stack32_t *) regs.regs[5];
299 unsigned long usp = regs.regs[29];
300 stack_t kss, koss;
301 int ret, err = 0;
302 mm_segment_t old_fs = get_fs();
303 s32 sp;
304
305 if (uss) {
306 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
307 return -EFAULT;
308 err |= __get_user(sp, &uss->ss_sp);
309 kss.ss_sp = (void *) (long) sp;
310 err |= __get_user(kss.ss_size, &uss->ss_size);
311 err |= __get_user(kss.ss_flags, &uss->ss_flags);
312 if (err)
313 return -EFAULT;
314 }
315
316 set_fs (KERNEL_DS);
317 ret = do_sigaltstack(uss ? &kss : NULL , uoss ? &koss : NULL, usp);
318 set_fs (old_fs);
319
320 if (!ret && uoss) {
321 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
322 return -EFAULT;
323 sp = (int) (long) koss.ss_sp;
324 err |= __put_user(sp, &uoss->ss_sp);
325 err |= __put_user(koss.ss_size, &uoss->ss_size);
326 err |= __put_user(koss.ss_flags, &uoss->ss_flags);
327 if (err)
328 return -EFAULT;
329 }
330 return ret;
331 }
332
restore_sigcontext32(struct pt_regs * regs,struct sigcontext32 * sc)333 static asmlinkage int restore_sigcontext32(struct pt_regs *regs,
334 struct sigcontext32 *sc)
335 {
336 int err = 0;
337
338 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
339 err |= __get_user(regs->hi, &sc->sc_mdhi);
340 err |= __get_user(regs->lo, &sc->sc_mdlo);
341
342 #define restore_gp_reg(i) do { \
343 err |= __get_user(regs->regs[i], &sc->sc_regs[i]); \
344 } while(0)
345 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
346 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
347 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
348 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
349 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
350 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
351 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
352 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
353 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
354 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
355 restore_gp_reg(31);
356 #undef restore_gp_reg
357
358 err |= __get_user(current->used_math, &sc->sc_used_math);
359
360 if (current->used_math) {
361 /* restore fpu context if we have used it before */
362 own_fpu();
363 err |= restore_fp_context32(sc);
364 } else {
365 /* signal handler may have used FPU. Give it up. */
366 lose_fpu();
367 }
368
369 return err;
370 }
371
372 struct sigframe {
373 u32 sf_ass[4]; /* argument save space for o32 */
374 u32 sf_code[2]; /* signal trampoline */
375 struct sigcontext32 sf_sc;
376 sigset_t sf_mask;
377 };
378
379 struct rt_sigframe32 {
380 u32 rs_ass[4]; /* argument save space for o32 */
381 u32 rs_code[2]; /* signal trampoline */
382 struct siginfo32 rs_info;
383 struct ucontext32 rs_uc;
384 };
385
copy_siginfo_to_user32(siginfo_t32 * to,siginfo_t * from)386 static int copy_siginfo_to_user32(siginfo_t32 *to, siginfo_t *from)
387 {
388 int err;
389
390 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t32)))
391 return -EFAULT;
392
393 /* If you change siginfo_t structure, please be sure
394 this code is fixed accordingly.
395 It should never copy any pad contained in the structure
396 to avoid security leaks, but must copy the generic
397 3 ints plus the relevant union member.
398 This routine must convert siginfo from 64bit to 32bit as well
399 at the same time. */
400 err = __put_user(from->si_signo, &to->si_signo);
401 err |= __put_user(from->si_errno, &to->si_errno);
402 err |= __put_user((short)from->si_code, &to->si_code);
403 if (from->si_code < 0)
404 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
405 else {
406 switch (from->si_code >> 16) {
407 case __SI_CHLD >> 16:
408 err |= __put_user(from->si_utime, &to->si_utime);
409 err |= __put_user(from->si_stime, &to->si_stime);
410 err |= __put_user(from->si_status, &to->si_status);
411 default:
412 err |= __put_user(from->si_pid, &to->si_pid);
413 err |= __put_user(from->si_uid, &to->si_uid);
414 break;
415 case __SI_FAULT >> 16:
416 err |= __put_user((long)from->si_addr, &to->si_addr);
417 break;
418 case __SI_POLL >> 16:
419 err |= __put_user(from->si_band, &to->si_band);
420 err |= __put_user(from->si_fd, &to->si_fd);
421 break;
422 /* case __SI_RT: This is not generated by the kernel as of now. */
423 }
424 }
425 return err;
426 }
427
sys32_sigreturn(abi64_no_regargs,struct pt_regs regs)428 asmlinkage void sys32_sigreturn(abi64_no_regargs, struct pt_regs regs)
429 {
430 struct sigframe *frame;
431 sigset_t blocked;
432
433 frame = (struct sigframe *) regs.regs[29];
434 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
435 goto badframe;
436 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
437 goto badframe;
438
439 sigdelsetmask(&blocked, ~_BLOCKABLE);
440 spin_lock_irq(¤t->sigmask_lock);
441 current->blocked = blocked;
442 recalc_sigpending(current);
443 spin_unlock_irq(¤t->sigmask_lock);
444
445 if (restore_sigcontext32(®s, &frame->sf_sc))
446 goto badframe;
447
448 /*
449 * Don't let your children do this ...
450 */
451 if (current->ptrace & PT_TRACESYS)
452 syscall_trace();
453 __asm__ __volatile__(
454 "move\t$29, %0\n\t"
455 "j\tret_from_sys_call"
456 :/* no outputs */
457 :"r" (®s));
458 /* Unreached */
459
460 badframe:
461 force_sig(SIGSEGV, current);
462 }
463
sys32_rt_sigreturn(abi64_no_regargs,struct pt_regs regs)464 asmlinkage void sys32_rt_sigreturn(abi64_no_regargs, struct pt_regs regs)
465 {
466 struct rt_sigframe32 *frame;
467 sigset_t set;
468 stack_t st;
469 s32 sp;
470
471 frame = (struct rt_sigframe32 *) regs.regs[29];
472 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
473 goto badframe;
474 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
475 goto badframe;
476
477 sigdelsetmask(&set, ~_BLOCKABLE);
478 spin_lock_irq(¤t->sigmask_lock);
479 current->blocked = set;
480 recalc_sigpending(current);
481 spin_unlock_irq(¤t->sigmask_lock);
482
483 if (restore_sigcontext32(®s, &frame->rs_uc.uc_mcontext))
484 goto badframe;
485
486 /* The ucontext contains a stack32_t, so we must convert! */
487 if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
488 goto badframe;
489 st.ss_size = (long) sp;
490 if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
491 goto badframe;
492 if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
493 goto badframe;
494
495 /* It is more difficult to avoid calling this function than to
496 call it and ignore errors. */
497 do_sigaltstack(&st, NULL, regs.regs[29]);
498
499 /*
500 * Don't let your children do this ...
501 */
502 __asm__ __volatile__(
503 "move\t$29, %0\n\t"
504 "j\tret_from_sys_call"
505 :/* no outputs */
506 :"r" (®s));
507 /* Unreached */
508
509 badframe:
510 force_sig(SIGSEGV, current);
511 }
512
setup_sigcontext32(struct pt_regs * regs,struct sigcontext32 * sc)513 static int inline setup_sigcontext32(struct pt_regs *regs,
514 struct sigcontext32 *sc)
515 {
516 int err = 0;
517
518 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
519 err |= __put_user(regs->cp0_status, &sc->sc_status);
520
521 #define save_gp_reg(i) { \
522 err |= __put_user(regs->regs[i], &sc->sc_regs[i]); \
523 } while(0)
524 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
525 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
526 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
527 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
528 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
529 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
530 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
531 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
532 save_gp_reg(31);
533 #undef save_gp_reg
534
535 err |= __put_user(regs->hi, &sc->sc_mdhi);
536 err |= __put_user(regs->lo, &sc->sc_mdlo);
537 err |= __put_user(regs->cp0_cause, &sc->sc_cause);
538 err |= __put_user(regs->cp0_badvaddr, &sc->sc_badvaddr);
539
540 err |= __put_user(current->used_math, &sc->sc_used_math);
541
542 if (!current->used_math)
543 goto out;
544
545 /*
546 * Save FPU state to signal context. Signal handler will "inherit"
547 * current FPU state.
548 */
549 if (!is_fpu_owner()) {
550 own_fpu();
551 restore_fp(current);
552 }
553 err |= save_fp_context32(sc);
554
555 out:
556 return err;
557 }
558
559 /*
560 * Determine which stack to use..
561 */
get_sigframe(struct k_sigaction * ka,struct pt_regs * regs,size_t frame_size)562 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
563 size_t frame_size)
564 {
565 unsigned long sp;
566
567 /* Default to using normal stack */
568 sp = regs->regs[29];
569
570 /*
571 * FPU emulator may have it's own trampoline active just
572 * above the user stack, 16-bytes before the next lowest
573 * 16 byte boundary. Try to avoid trashing it.
574 */
575 sp -= 32;
576
577 /* This is the X/Open sanctioned signal stack switching. */
578 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
579 sp = current->sas_ss_sp + current->sas_ss_size;
580
581 return (void *)((sp - frame_size) & ALMASK);
582 }
583
setup_frame(struct k_sigaction * ka,struct pt_regs * regs,int signr,sigset_t * set)584 static void inline setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
585 int signr, sigset_t *set)
586 {
587 struct sigframe *frame;
588 int err = 0;
589
590 frame = get_sigframe(ka, regs, sizeof(*frame));
591 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
592 goto give_sigsegv;
593
594 /*
595 * Set up the return code ...
596 *
597 * li v0, __NR_O32_sigreturn
598 * syscall
599 */
600 err |= __put_user(0x24020000 + __NR_O32_sigreturn, frame->sf_code + 0);
601 err |= __put_user(0x0000000c , frame->sf_code + 1);
602 flush_cache_sigtramp((unsigned long) frame->sf_code);
603
604 err |= setup_sigcontext32(regs, &frame->sf_sc);
605 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
606 if (err)
607 goto give_sigsegv;
608
609 /*
610 * Arguments to signal handler:
611 *
612 * a0 = signal number
613 * a1 = 0 (should be cause)
614 * a2 = pointer to struct sigcontext
615 *
616 * $25 and c0_epc point to the signal handler, $29 points to the
617 * struct sigframe.
618 */
619 regs->regs[ 4] = signr;
620 regs->regs[ 5] = 0;
621 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
622 regs->regs[29] = (unsigned long) frame;
623 regs->regs[31] = (unsigned long) frame->sf_code;
624 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
625
626 #if DEBUG_SIG
627 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
628 current->comm, current->pid,
629 frame, regs->cp0_epc, frame->sf_code);
630 #endif
631 return;
632
633 give_sigsegv:
634 if (signr == SIGSEGV)
635 ka->sa.sa_handler = SIG_DFL;
636 force_sig(SIGSEGV, current);
637 }
638
setup_rt_frame(struct k_sigaction * ka,struct pt_regs * regs,int signr,sigset_t * set,siginfo_t * info)639 static void inline setup_rt_frame(struct k_sigaction * ka,
640 struct pt_regs *regs, int signr,
641 sigset_t *set, siginfo_t *info)
642 {
643 struct rt_sigframe32 *frame;
644 int err = 0;
645 s32 sp;
646
647 frame = get_sigframe(ka, regs, sizeof(*frame));
648 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
649 goto give_sigsegv;
650
651 /* Set up to return from userspace. If provided, use a stub already
652 in userspace. */
653 /*
654 * Set up the return code ...
655 *
656 * li v0, __NR_O32_rt_sigreturn
657 * syscall
658 */
659 err |= __put_user(0x24020000 + __NR_O32_rt_sigreturn, frame->rs_code + 0);
660 err |= __put_user(0x0000000c , frame->rs_code + 1);
661 flush_cache_sigtramp((unsigned long) frame->rs_code);
662
663 /* Convert (siginfo_t -> siginfo_t32) and copy to user. */
664 err |= copy_siginfo_to_user32(&frame->rs_info, info);
665
666 /* Create the ucontext. */
667 err |= __put_user(0, &frame->rs_uc.uc_flags);
668 err |= __put_user(0, &frame->rs_uc.uc_link);
669 sp = (int) (long) current->sas_ss_sp;
670 err |= __put_user(sp,
671 &frame->rs_uc.uc_stack.ss_sp);
672 err |= __put_user(sas_ss_flags(regs->regs[29]),
673 &frame->rs_uc.uc_stack.ss_flags);
674 err |= __put_user(current->sas_ss_size,
675 &frame->rs_uc.uc_stack.ss_size);
676 err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
677 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
678
679 if (err)
680 goto give_sigsegv;
681
682 /*
683 * Arguments to signal handler:
684 *
685 * a0 = signal number
686 * a1 = 0 (should be cause)
687 * a2 = pointer to ucontext
688 *
689 * $25 and c0_epc point to the signal handler, $29 points to
690 * the struct rt_sigframe32.
691 */
692 regs->regs[ 4] = signr;
693 regs->regs[ 5] = (unsigned long) &frame->rs_info;
694 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
695 regs->regs[29] = (unsigned long) frame;
696 regs->regs[31] = (unsigned long) frame->rs_code;
697 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
698
699 #if DEBUG_SIG
700 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
701 current->comm, current->pid,
702 frame, regs->cp0_epc, frame->rs_code);
703 #endif
704 return;
705
706 give_sigsegv:
707 if (signr == SIGSEGV)
708 ka->sa.sa_handler = SIG_DFL;
709 force_sig(SIGSEGV, current);
710 }
711
handle_signal(unsigned long sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * oldset,struct pt_regs * regs)712 static inline void handle_signal(unsigned long sig, struct k_sigaction *ka,
713 siginfo_t *info, sigset_t *oldset,
714 struct pt_regs * regs)
715 {
716 if (ka->sa.sa_flags & SA_SIGINFO)
717 setup_rt_frame(ka, regs, sig, oldset, info);
718 else
719 setup_frame(ka, regs, sig, oldset);
720
721 if (ka->sa.sa_flags & SA_ONESHOT)
722 ka->sa.sa_handler = SIG_DFL;
723 if (!(ka->sa.sa_flags & SA_NODEFER)) {
724 spin_lock_irq(¤t->sigmask_lock);
725 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
726 sigaddset(¤t->blocked,sig);
727 recalc_sigpending(current);
728 spin_unlock_irq(¤t->sigmask_lock);
729 }
730 }
731
syscall_restart(struct pt_regs * regs,struct k_sigaction * ka)732 static inline void syscall_restart(struct pt_regs *regs,
733 struct k_sigaction *ka)
734 {
735 switch(regs->regs[0]) {
736 case ERESTARTNOHAND:
737 regs->regs[2] = EINTR;
738 break;
739 case ERESTARTSYS:
740 if(!(ka->sa.sa_flags & SA_RESTART)) {
741 regs->regs[2] = EINTR;
742 break;
743 }
744 /* fallthrough */
745 case ERESTARTNOINTR: /* Userland will reload $v0. */
746 regs->regs[7] = regs->regs[26];
747 regs->cp0_epc -= 8;
748 }
749
750 regs->regs[0] = 0; /* Don't deal with this again. */
751 }
752
do_signal32(sigset_t * oldset,struct pt_regs * regs)753 asmlinkage int do_signal32(sigset_t *oldset, struct pt_regs *regs)
754 {
755 struct k_sigaction *ka;
756 siginfo_t info;
757
758 if (!oldset)
759 oldset = ¤t->blocked;
760
761 for (;;) {
762 unsigned long signr;
763
764 spin_lock_irq(¤t->sigmask_lock);
765 signr = dequeue_signal(¤t->blocked, &info);
766 spin_unlock_irq(¤t->sigmask_lock);
767
768 if (!signr)
769 break;
770
771 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
772 /* Let the debugger run. */
773 current->exit_code = signr;
774 current->state = TASK_STOPPED;
775 notify_parent(current, SIGCHLD);
776 schedule();
777
778 /* We're back. Did the debugger cancel the sig? */
779 if (!(signr = current->exit_code))
780 continue;
781 current->exit_code = 0;
782
783 /* The debugger continued. Ignore SIGSTOP. */
784 if (signr == SIGSTOP)
785 continue;
786
787 /* Update the siginfo structure. Is this good? */
788 if (signr != info.si_signo) {
789 info.si_signo = signr;
790 info.si_errno = 0;
791 info.si_code = SI_USER;
792 info.si_pid = current->p_pptr->pid;
793 info.si_uid = current->p_pptr->uid;
794 }
795
796 /* If the (new) signal is now blocked, requeue it. */
797 if (sigismember(¤t->blocked, signr)) {
798 send_sig_info(signr, &info, current);
799 continue;
800 }
801 }
802
803 ka = ¤t->sig->action[signr-1];
804 if (ka->sa.sa_handler == SIG_IGN) {
805 if (signr != SIGCHLD)
806 continue;
807 /* Check for SIGCHLD: it's special. */
808 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
809 /* nothing */;
810 continue;
811 }
812
813 if (ka->sa.sa_handler == SIG_DFL) {
814 int exit_code = signr;
815
816 /* Init gets no signals it doesn't want. */
817 if (current->pid == 1)
818 continue;
819
820 switch (signr) {
821 case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
822 continue;
823
824 case SIGTSTP: case SIGTTIN: case SIGTTOU:
825 if (is_orphaned_pgrp(current->pgrp))
826 continue;
827 /* FALLTHRU */
828
829 case SIGSTOP:
830 current->state = TASK_STOPPED;
831 current->exit_code = signr;
832 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
833 notify_parent(current, SIGCHLD);
834 schedule();
835 continue;
836
837 case SIGQUIT: case SIGILL: case SIGTRAP:
838 case SIGABRT: case SIGFPE: case SIGSEGV:
839 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
840 if (do_coredump(signr, regs))
841 exit_code |= 0x80;
842 /* FALLTHRU */
843
844 default:
845 sig_exit(signr, exit_code, &info);
846 /* NOTREACHED */
847 }
848 }
849
850 if (regs->regs[0])
851 syscall_restart(regs, ka);
852 /* Whee! Actually deliver the signal. */
853 handle_signal(signr, ka, &info, oldset, regs);
854 return 1;
855 }
856
857 /*
858 * Who's code doesn't conform to the restartable syscall convention
859 * dies here!!! The li instruction, a single machine instruction,
860 * must directly be followed by the syscall instruction.
861 */
862 if (regs->regs[0]) {
863 if (regs->regs[2] == ERESTARTNOHAND ||
864 regs->regs[2] == ERESTARTSYS ||
865 regs->regs[2] == ERESTARTNOINTR) {
866 regs->regs[7] = regs->regs[26];
867 regs->cp0_epc -= 8;
868 }
869 }
870 return 0;
871 }
872
873 extern asmlinkage int sys_sigprocmask(int how, old_sigset_t *set,
874 old_sigset_t *oset);
875
sys32_sigprocmask(int how,old_sigset_t32 * set,old_sigset_t32 * oset)876 asmlinkage int sys32_sigprocmask(int how, old_sigset_t32 *set,
877 old_sigset_t32 *oset)
878 {
879 old_sigset_t s;
880 int ret;
881 mm_segment_t old_fs = get_fs();
882
883 if (set && get_user (s, set))
884 return -EFAULT;
885
886 set_fs (KERNEL_DS);
887 ret = sys_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL);
888 set_fs (old_fs);
889
890 if (!ret && oset && put_user (s, oset))
891 return -EFAULT;
892
893 return ret;
894 }
895
896 asmlinkage long sys_sigpending(old_sigset_t *set);
897
sys32_sigpending(old_sigset_t32 * set)898 asmlinkage int sys32_sigpending(old_sigset_t32 *set)
899 {
900 old_sigset_t pending;
901 int ret;
902 mm_segment_t old_fs = get_fs();
903
904 set_fs (KERNEL_DS);
905 ret = sys_sigpending(&pending);
906 set_fs (old_fs);
907
908 if (put_user(pending, set))
909 return -EFAULT;
910
911 return ret;
912 }
913
sys32_rt_sigaction(int sig,const struct sigaction32 * act,struct sigaction32 * oact,unsigned int sigsetsize)914 asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 *act,
915 struct sigaction32 *oact,
916 unsigned int sigsetsize)
917 {
918 struct k_sigaction new_sa, old_sa;
919 int ret = -EINVAL;
920
921 /* XXX: Don't preclude handling different sized sigset_t's. */
922 if (sigsetsize != sizeof(sigset_t))
923 goto out;
924
925 if (act) {
926 int err = 0;
927
928 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
929 return -EFAULT;
930 err |= __get_user((u32)(u64)new_sa.sa.sa_handler,
931 &act->sa_handler);
932 err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
933 err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
934 if (err)
935 return -EFAULT;
936 }
937
938 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
939
940 if (!ret && oact) {
941 int err = 0;
942
943 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
944 return -EFAULT;
945
946 err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
947 &oact->sa_handler);
948 err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
949 err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
950 if (err)
951 return -EFAULT;
952 }
953 out:
954 return ret;
955 }
956
957 asmlinkage long sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset,
958 size_t sigsetsize);
959
sys32_rt_sigprocmask(int how,sigset32_t * set,sigset32_t * oset,unsigned int sigsetsize)960 asmlinkage int sys32_rt_sigprocmask(int how, sigset32_t *set, sigset32_t *oset,
961 unsigned int sigsetsize)
962 {
963 sigset_t old_set, new_set;
964 int ret;
965 mm_segment_t old_fs = get_fs();
966
967 if (set && get_sigset(&new_set, set))
968 return -EFAULT;
969
970 set_fs (KERNEL_DS);
971 ret = sys_rt_sigprocmask(how, set ? &new_set : NULL,
972 oset ? &old_set : NULL, sigsetsize);
973 set_fs (old_fs);
974
975 if (!ret && oset && put_sigset(&old_set, oset))
976 return -EFAULT;
977
978 return ret;
979 }
980
981 asmlinkage long sys_rt_sigpending(sigset_t *set, size_t sigsetsize);
982
sys32_rt_sigpending(sigset32_t * uset,unsigned int sigsetsize)983 asmlinkage int sys32_rt_sigpending(sigset32_t *uset, unsigned int sigsetsize)
984 {
985 int ret;
986 sigset_t set;
987 mm_segment_t old_fs = get_fs();
988
989 set_fs (KERNEL_DS);
990 ret = sys_rt_sigpending(&set, sigsetsize);
991 set_fs (old_fs);
992
993 if (!ret && put_sigset(&set, uset))
994 return -EFAULT;
995
996 return ret;
997 }
998
999 struct timespec32 {
1000 int tv_sec;
1001 int tv_nsec;
1002 };
1003
sys32_rt_sigtimedwait(sigset_t32 * uthese,siginfo_t32 * uinfo,struct timespec32 * uts,__kernel_size_t32 sigsetsize)1004 asmlinkage int sys32_rt_sigtimedwait(sigset_t32 *uthese, siginfo_t32 *uinfo,
1005 struct timespec32 *uts, __kernel_size_t32 sigsetsize)
1006 {
1007 int ret, sig;
1008 sigset_t these;
1009 sigset_t32 these32;
1010 struct timespec ts;
1011 siginfo_t info;
1012 long timeout = 0;
1013
1014 /*
1015 * As the result of a brainfarting competition a few years ago the
1016 * size of sigset_t for the 32-bit kernel was choosen to be 128 bits
1017 * but nothing so far is actually using that many, 64 are enough. So
1018 * for now we just drop the high bits.
1019 */
1020 if (copy_from_user (&these32, uthese, sizeof(old_sigset_t32)))
1021 return -EFAULT;
1022
1023 switch (_NSIG_WORDS) {
1024 #ifdef __MIPSEB__
1025 case 4: these.sig[3] = these32.sig[6] | (((long)these32.sig[7]) << 32);
1026 case 3: these.sig[2] = these32.sig[4] | (((long)these32.sig[5]) << 32);
1027 case 2: these.sig[1] = these32.sig[2] | (((long)these32.sig[3]) << 32);
1028 case 1: these.sig[0] = these32.sig[0] | (((long)these32.sig[1]) << 32);
1029 #endif
1030 #ifdef __MIPSEL__
1031 case 4: these.sig[3] = these32.sig[7] | (((long)these32.sig[6]) << 32);
1032 case 3: these.sig[2] = these32.sig[5] | (((long)these32.sig[4]) << 32);
1033 case 2: these.sig[1] = these32.sig[3] | (((long)these32.sig[2]) << 32);
1034 case 1: these.sig[0] = these32.sig[1] | (((long)these32.sig[0]) << 32);
1035 #endif
1036 }
1037
1038 /*
1039 * Invert the set of allowed signals to get those we
1040 * want to block.
1041 */
1042 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
1043 signotset(&these);
1044
1045 if (uts) {
1046 if (get_user (ts.tv_sec, &uts->tv_sec) ||
1047 get_user (ts.tv_nsec, &uts->tv_nsec))
1048 return -EINVAL;
1049 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
1050 || ts.tv_sec < 0)
1051 return -EINVAL;
1052 }
1053
1054 spin_lock_irq(¤t->sigmask_lock);
1055 sig = dequeue_signal(&these, &info);
1056 if (!sig) {
1057 /* None ready -- temporarily unblock those we're interested
1058 in so that we'll be awakened when they arrive. */
1059 sigset_t oldblocked = current->blocked;
1060 sigandsets(¤t->blocked, ¤t->blocked, &these);
1061 recalc_sigpending(current);
1062 spin_unlock_irq(¤t->sigmask_lock);
1063
1064 timeout = MAX_SCHEDULE_TIMEOUT;
1065 if (uts)
1066 timeout = (timespec_to_jiffies(&ts)
1067 + (ts.tv_sec || ts.tv_nsec));
1068
1069 current->state = TASK_INTERRUPTIBLE;
1070 timeout = schedule_timeout(timeout);
1071
1072 spin_lock_irq(¤t->sigmask_lock);
1073 sig = dequeue_signal(&these, &info);
1074 current->blocked = oldblocked;
1075 recalc_sigpending(current);
1076 }
1077 spin_unlock_irq(¤t->sigmask_lock);
1078
1079 if (sig) {
1080 ret = sig;
1081 if (uinfo) {
1082 if (copy_siginfo_to_user32(uinfo, &info))
1083 ret = -EFAULT;
1084 }
1085 } else {
1086 ret = -EAGAIN;
1087 if (timeout)
1088 ret = -EINTR;
1089 }
1090
1091 return ret;
1092 }
1093
1094 extern asmlinkage int sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo);
1095
sys32_rt_sigqueueinfo(int pid,int sig,siginfo_t32 * uinfo)1096 asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, siginfo_t32 *uinfo)
1097 {
1098 siginfo_t info;
1099 int ret;
1100 mm_segment_t old_fs = get_fs();
1101
1102 if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
1103 copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
1104 return -EFAULT;
1105 set_fs (KERNEL_DS);
1106 ret = sys_rt_sigqueueinfo(pid, sig, &info);
1107 set_fs (old_fs);
1108 return ret;
1109 }
1110