blob: 7d5a631d6cab6f912854d40d83bbdc6f0d158e31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Ralf Baechle02416dc2005-06-15 13:00:12 +000010#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/personality.h>
14#include <linux/smp.h>
15#include <linux/smp_lock.h>
16#include <linux/kernel.h>
17#include <linux/signal.h>
18#include <linux/errno.h>
19#include <linux/wait.h>
20#include <linux/ptrace.h>
21#include <linux/unistd.h>
22#include <linux/compiler.h>
23
Ralf Baechlee50c0a82005-05-31 11:49:19 +000024#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/asm.h>
26#include <linux/bitops.h>
27#include <asm/cacheflush.h>
28#include <asm/fpu.h>
29#include <asm/sim.h>
30#include <asm/uaccess.h>
31#include <asm/ucontext.h>
32#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000033#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "signal-common.h"
36
37#define DEBUG_SIG 0
38
39#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010042 * Helper routines
43 */
44int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
45{
46 int err = 0;
47 int i;
48
49 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
50
51 err |= __put_user(0, &sc->sc_regs[0]);
52 for (i = 1; i < 32; i++)
53 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
54
55 err |= __put_user(regs->hi, &sc->sc_mdhi);
56 err |= __put_user(regs->lo, &sc->sc_mdlo);
57 if (cpu_has_dsp) {
58 err |= __put_user(mfhi1(), &sc->sc_hi1);
59 err |= __put_user(mflo1(), &sc->sc_lo1);
60 err |= __put_user(mfhi2(), &sc->sc_hi2);
61 err |= __put_user(mflo2(), &sc->sc_lo2);
62 err |= __put_user(mfhi3(), &sc->sc_hi3);
63 err |= __put_user(mflo3(), &sc->sc_lo3);
64 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
65 }
66
67 err |= __put_user(!!used_math(), &sc->sc_used_math);
68
69 if (used_math()) {
70 /*
71 * Save FPU state to signal context. Signal handler
72 * will "inherit" current FPU state.
73 */
74 preempt_disable();
75
76 if (!is_fpu_owner()) {
77 own_fpu();
78 restore_fp(current);
79 }
80 err |= save_fp_context(sc);
81
82 preempt_enable();
83 }
84 return err;
85}
86
87int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
88{
89 unsigned int used_math;
90 unsigned long treg;
91 int err = 0;
92 int i;
93
94 /* Always make any pending restarted system calls return -EINTR */
95 current_thread_info()->restart_block.fn = do_no_restart_syscall;
96
97 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
98 err |= __get_user(regs->hi, &sc->sc_mdhi);
99 err |= __get_user(regs->lo, &sc->sc_mdlo);
100 if (cpu_has_dsp) {
101 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
102 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
103 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
104 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
105 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
106 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
107 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
108 }
109
110 for (i = 1; i < 32; i++)
111 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
112
113 err |= __get_user(used_math, &sc->sc_used_math);
114 conditional_used_math(used_math);
115
116 preempt_disable();
117
118 if (used_math()) {
119 /* restore fpu context if we have used it before */
120 own_fpu();
121 err |= restore_fp_context(sc);
122 } else {
123 /* signal handler may have used FPU. Give it up. */
124 lose_fpu();
125 }
126
127 preempt_enable();
128
129 return err;
130}
131
132void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
133 size_t frame_size)
134{
135 unsigned long sp;
136
137 /* Default to using normal stack */
138 sp = regs->regs[29];
139
140 /*
141 * FPU emulator may have it's own trampoline active just
142 * above the user stack, 16-bytes before the next lowest
143 * 16 byte boundary. Try to avoid trashing it.
144 */
145 sp -= 32;
146
147 /* This is the X/Open sanctioned signal stack switching. */
148 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
149 sp = current->sas_ss_sp + current->sas_ss_size;
150
151 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
152}
153
154int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
155{
156 int err;
157
158 /*
159 * Set up the return code ...
160 *
161 * li v0, __NR__foo_sigreturn
162 * syscall
163 */
164
165 err = __put_user(0x24020000 + syscall, tramp + 0);
166 err |= __put_user(0x0000000c , tramp + 1);
167 if (ICACHE_REFILLS_WORKAROUND_WAR) {
168 err |= __put_user(0, tramp + 2);
169 err |= __put_user(0, tramp + 3);
170 err |= __put_user(0, tramp + 4);
171 err |= __put_user(0, tramp + 5);
172 err |= __put_user(0, tramp + 6);
173 err |= __put_user(0, tramp + 7);
174 }
175 flush_cache_sigtramp((unsigned long) tramp);
176
177 return err;
178}
179
180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Atomically swap in the new signal mask, and wait for a signal.
182 */
183
184#ifdef CONFIG_TRAD_SIGNALS
185save_static_function(sys_sigsuspend);
186__attribute_used__ noinline static int
187_sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
188{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000189 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000190 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Ralf Baechlefe00f942005-03-01 19:22:29 +0000192 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
194 return -EFAULT;
195 sigdelsetmask(&newset, ~_BLOCKABLE);
196
197 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000198 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 current->blocked = newset;
200 recalc_sigpending();
201 spin_unlock_irq(&current->sighand->siglock);
202
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000203 current->state = TASK_INTERRUPTIBLE;
204 schedule();
205 set_thread_flag(TIF_RESTORE_SIGMASK);
206 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208#endif
209
210save_static_function(sys_rt_sigsuspend);
211__attribute_used__ noinline static int
212_sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
213{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000214 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000215 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 size_t sigsetsize;
217
218 /* XXX Don't preclude handling different sized sigset_t's. */
219 sigsetsize = regs.regs[5];
220 if (sigsetsize != sizeof(sigset_t))
221 return -EINVAL;
222
Ralf Baechlefe00f942005-03-01 19:22:29 +0000223 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (copy_from_user(&newset, unewset, sizeof(newset)))
225 return -EFAULT;
226 sigdelsetmask(&newset, ~_BLOCKABLE);
227
228 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000229 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 current->blocked = newset;
Ralf Baechlee0daad42007-02-05 00:10:11 +0000231 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_unlock_irq(&current->sighand->siglock);
233
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000234 current->state = TASK_INTERRUPTIBLE;
235 schedule();
236 set_thread_flag(TIF_RESTORE_SIGMASK);
237 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240#ifdef CONFIG_TRAD_SIGNALS
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900241asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
242 struct sigaction __user *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct k_sigaction new_ka, old_ka;
245 int ret;
246 int err = 0;
247
248 if (act) {
249 old_sigset_t mask;
250
251 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
252 return -EFAULT;
253 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
254 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
255 err |= __get_user(mask, &act->sa_mask.sig[0]);
256 if (err)
257 return -EFAULT;
258
259 siginitset(&new_ka.sa.sa_mask, mask);
260 }
261
262 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
263
264 if (!ret && oact) {
265 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000266 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
268 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
269 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
270 err |= __put_user(0, &oact->sa_mask.sig[1]);
271 err |= __put_user(0, &oact->sa_mask.sig[2]);
272 err |= __put_user(0, &oact->sa_mask.sig[3]);
273 if (err)
274 return -EFAULT;
275 }
276
277 return ret;
278}
279#endif
280
281asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
282{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000283 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
284 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned long usp = regs.regs[29];
286
287 return do_sigaltstack(uss, uoss, usp);
288}
289
Ralf Baechle02416dc2005-06-15 13:00:12 +0000290/*
291 * Horribly complicated - with the bloody RM9000 workarounds enabled
292 * the signal trampolines is moving to the end of the structure so we can
293 * increase the alignment without breaking software compatibility.
294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#ifdef CONFIG_TRAD_SIGNALS
296struct sigframe {
297 u32 sf_ass[4]; /* argument save space for o32 */
Ralf Baechle02416dc2005-06-15 13:00:12 +0000298#if ICACHE_REFILLS_WORKAROUND_WAR
299 u32 sf_pad[2];
300#else
301 u32 sf_code[2]; /* signal trampoline */
302#endif
303 struct sigcontext sf_sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 sigset_t sf_mask;
Ralf Baechle02416dc2005-06-15 13:00:12 +0000305#if ICACHE_REFILLS_WORKAROUND_WAR
306 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
309#endif
310
311struct rt_sigframe {
312 u32 rs_ass[4]; /* argument save space for o32 */
Ralf Baechle02416dc2005-06-15 13:00:12 +0000313#if ICACHE_REFILLS_WORKAROUND_WAR
314 u32 rs_pad[2];
315#else
316 u32 rs_code[2]; /* signal trampoline */
317#endif
318 struct siginfo rs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct ucontext rs_uc;
Ralf Baechle02416dc2005-06-15 13:00:12 +0000320#if ICACHE_REFILLS_WORKAROUND_WAR
321 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324
325#ifdef CONFIG_TRAD_SIGNALS
326save_static_function(sys_sigreturn);
327__attribute_used__ noinline static void
328_sys_sigreturn(nabi_no_regargs struct pt_regs regs)
329{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900330 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 sigset_t blocked;
332
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900333 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
335 goto badframe;
336 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
337 goto badframe;
338
339 sigdelsetmask(&blocked, ~_BLOCKABLE);
340 spin_lock_irq(&current->sighand->siglock);
341 current->blocked = blocked;
342 recalc_sigpending();
343 spin_unlock_irq(&current->sighand->siglock);
344
345 if (restore_sigcontext(&regs, &frame->sf_sc))
346 goto badframe;
347
348 /*
349 * Don't let your children do this ...
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 __asm__ __volatile__(
352 "move\t$29, %0\n\t"
353 "j\tsyscall_exit"
354 :/* no outputs */
355 :"r" (&regs));
356 /* Unreached */
357
358badframe:
359 force_sig(SIGSEGV, current);
360}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000361#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363save_static_function(sys_rt_sigreturn);
364__attribute_used__ noinline static void
365_sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
366{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900367 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 sigset_t set;
369 stack_t st;
370
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900371 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
373 goto badframe;
374 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
375 goto badframe;
376
377 sigdelsetmask(&set, ~_BLOCKABLE);
378 spin_lock_irq(&current->sighand->siglock);
379 current->blocked = set;
380 recalc_sigpending();
381 spin_unlock_irq(&current->sighand->siglock);
382
383 if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
384 goto badframe;
385
386 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
387 goto badframe;
388 /* It is more difficult to avoid calling this function than to
389 call it and ignore errors. */
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900390 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /*
393 * Don't let your children do this ...
394 */
395 __asm__ __volatile__(
396 "move\t$29, %0\n\t"
397 "j\tsyscall_exit"
398 :/* no outputs */
399 :"r" (&regs));
400 /* Unreached */
401
402badframe:
403 force_sig(SIGSEGV, current);
404}
405
406#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000407int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int signr, sigset_t *set)
409{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900410 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int err = 0;
412
413 frame = get_sigframe(ka, regs, sizeof(*frame));
414 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
415 goto give_sigsegv;
416
Ralf Baechle02416dc2005-06-15 13:00:12 +0000417 install_sigtramp(frame->sf_code, __NR_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 err |= setup_sigcontext(regs, &frame->sf_sc);
420 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
421 if (err)
422 goto give_sigsegv;
423
424 /*
425 * Arguments to signal handler:
426 *
427 * a0 = signal number
428 * a1 = 0 (should be cause)
429 * a2 = pointer to struct sigcontext
430 *
431 * $25 and c0_epc point to the signal handler, $29 points to the
432 * struct sigframe.
433 */
434 regs->regs[ 4] = signr;
435 regs->regs[ 5] = 0;
436 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
437 regs->regs[29] = (unsigned long) frame;
438 regs->regs[31] = (unsigned long) frame->sf_code;
439 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
440
441#if DEBUG_SIG
442 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
443 current->comm, current->pid,
444 frame, regs->cp0_epc, frame->regs[31]);
445#endif
Ralf Baechlee0daad42007-02-05 00:10:11 +0000446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448give_sigsegv:
449 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000450 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452#endif
453
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000454int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int signr, sigset_t *set, siginfo_t *info)
456{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900457 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int err = 0;
459
460 frame = get_sigframe(ka, regs, sizeof(*frame));
461 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
462 goto give_sigsegv;
463
Ralf Baechle02416dc2005-06-15 13:00:12 +0000464 install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* Create siginfo. */
467 err |= copy_siginfo_to_user(&frame->rs_info, info);
468
469 /* Create the ucontext. */
470 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900471 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900472 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 &frame->rs_uc.uc_stack.ss_sp);
474 err |= __put_user(sas_ss_flags(regs->regs[29]),
475 &frame->rs_uc.uc_stack.ss_flags);
476 err |= __put_user(current->sas_ss_size,
477 &frame->rs_uc.uc_stack.ss_size);
478 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
479 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
480
481 if (err)
482 goto give_sigsegv;
483
484 /*
485 * Arguments to signal handler:
486 *
487 * a0 = signal number
488 * a1 = 0 (should be cause)
489 * a2 = pointer to ucontext
490 *
491 * $25 and c0_epc point to the signal handler, $29 points to
492 * the struct rt_sigframe.
493 */
494 regs->regs[ 4] = signr;
495 regs->regs[ 5] = (unsigned long) &frame->rs_info;
496 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
497 regs->regs[29] = (unsigned long) frame;
498 regs->regs[31] = (unsigned long) frame->rs_code;
499 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
500
501#if DEBUG_SIG
502 printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
503 current->comm, current->pid,
504 frame, regs->cp0_epc, regs->regs[31]);
505#endif
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508give_sigsegv:
509 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000510 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000513static inline int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
515{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000516 int ret;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 switch(regs->regs[0]) {
519 case ERESTART_RESTARTBLOCK:
520 case ERESTARTNOHAND:
521 regs->regs[2] = EINTR;
522 break;
523 case ERESTARTSYS:
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000524 if (!(ka->sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 regs->regs[2] = EINTR;
526 break;
527 }
528 /* fallthrough */
529 case ERESTARTNOINTR: /* Userland will reload $v0. */
530 regs->regs[7] = regs->regs[26];
531 regs->cp0_epc -= 8;
532 }
533
534 regs->regs[0] = 0; /* Don't deal with this again. */
535
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000536 if (sig_uses_siginfo(ka))
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000537 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 else
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000539 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Steven Rostedt69be8f12005-08-29 11:44:09 -0400541 spin_lock_irq(&current->sighand->siglock);
542 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
543 if (!(ka->sa.sa_flags & SA_NODEFER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 sigaddset(&current->blocked,sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400545 recalc_sigpending();
546 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000547
548 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000551void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000554 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 siginfo_t info;
556 int signr;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * We want the common case to go fast, which is why we may in certain
560 * cases get here from kernel mode. Just return without doing anything
561 * if so.
562 */
563 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000564 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000566 if (test_thread_flag(TIF_RESTORE_SIGMASK))
567 oldset = &current->saved_sigmask;
568 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 oldset = &current->blocked;
570
571 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000572 if (signr > 0) {
573 /* Whee! Actually deliver the signal. */
574 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
575 /*
576 * A signal was successfully delivered; the saved
577 * sigmask will have been stored in the signal frame,
578 * and will be restored by sigreturn, so we can simply
579 * clear the TIF_RESTORE_SIGMASK flag.
580 */
581 if (test_thread_flag(TIF_RESTORE_SIGMASK))
582 clear_thread_flag(TIF_RESTORE_SIGMASK);
583 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100584
585 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /*
589 * Who's code doesn't conform to the restartable syscall convention
590 * dies here!!! The li instruction, a single machine instruction,
591 * must directly be followed by the syscall instruction.
592 */
593 if (regs->regs[0]) {
594 if (regs->regs[2] == ERESTARTNOHAND ||
595 regs->regs[2] == ERESTARTSYS ||
596 regs->regs[2] == ERESTARTNOINTR) {
597 regs->regs[7] = regs->regs[26];
598 regs->cp0_epc -= 8;
599 }
600 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
601 regs->regs[2] = __NR_restart_syscall;
602 regs->regs[7] = regs->regs[26];
603 regs->cp0_epc -= 4;
604 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100605 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000607
608 /*
609 * If there's no signal to deliver, we just put the saved sigmask
610 * back
611 */
612 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
613 clear_thread_flag(TIF_RESTORE_SIGMASK);
614 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/*
619 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000620 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000622asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 __u32 thread_info_flags)
624{
625 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000626 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
627 current->thread.abi->do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}