blob: e7b04928caec328ea493237d8fe1bfec835eadd8 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38
Ralf Baechle66680582007-02-13 01:31:48 +000039/*
40 * Horribly complicated - with the bloody RM9000 workarounds enabled
41 * the signal trampolines is moving to the end of the structure so we can
42 * increase the alignment without breaking software compatibility.
43 */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010044#if ICACHE_REFILLS_WORKAROUND_WAR == 0
45
Ralf Baechle66680582007-02-13 01:31:48 +000046struct sigframe {
47 u32 sf_ass[4]; /* argument save space for o32 */
48 u32 sf_code[2]; /* signal trampoline */
49 struct sigcontext sf_sc;
50 sigset_t sf_mask;
51};
52
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010053struct rt_sigframe {
54 u32 rs_ass[4]; /* argument save space for o32 */
55 u32 rs_code[2]; /* signal trampoline */
56 struct siginfo rs_info;
57 struct ucontext rs_uc;
58};
59
60#else
61
Ralf Baechle66680582007-02-13 01:31:48 +000062struct sigframe {
63 u32 sf_ass[4]; /* argument save space for o32 */
64 u32 sf_pad[2];
65 struct sigcontext sf_sc; /* hw context */
66 sigset_t sf_mask;
67 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
68};
69
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010070struct rt_sigframe {
71 u32 rs_ass[4]; /* argument save space for o32 */
72 u32 rs_pad[2];
73 struct siginfo rs_info;
74 struct ucontext rs_uc;
75 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
76};
77
78#endif
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010081 * Helper routines
82 */
83int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
84{
85 int err = 0;
86 int i;
87
88 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
89
90 err |= __put_user(0, &sc->sc_regs[0]);
91 for (i = 1; i < 32; i++)
92 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
93
94 err |= __put_user(regs->hi, &sc->sc_mdhi);
95 err |= __put_user(regs->lo, &sc->sc_mdlo);
96 if (cpu_has_dsp) {
97 err |= __put_user(mfhi1(), &sc->sc_hi1);
98 err |= __put_user(mflo1(), &sc->sc_lo1);
99 err |= __put_user(mfhi2(), &sc->sc_hi2);
100 err |= __put_user(mflo2(), &sc->sc_lo2);
101 err |= __put_user(mfhi3(), &sc->sc_hi3);
102 err |= __put_user(mflo3(), &sc->sc_lo3);
103 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
104 }
105
106 err |= __put_user(!!used_math(), &sc->sc_used_math);
107
108 if (used_math()) {
109 /*
110 * Save FPU state to signal context. Signal handler
111 * will "inherit" current FPU state.
112 */
113 preempt_disable();
114
115 if (!is_fpu_owner()) {
116 own_fpu();
117 restore_fp(current);
118 }
119 err |= save_fp_context(sc);
120
121 preempt_enable();
122 }
123 return err;
124}
125
126int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
127{
128 unsigned int used_math;
129 unsigned long treg;
130 int err = 0;
131 int i;
132
133 /* Always make any pending restarted system calls return -EINTR */
134 current_thread_info()->restart_block.fn = do_no_restart_syscall;
135
136 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
137 err |= __get_user(regs->hi, &sc->sc_mdhi);
138 err |= __get_user(regs->lo, &sc->sc_mdlo);
139 if (cpu_has_dsp) {
140 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
141 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
142 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
143 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
144 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
145 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
146 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
147 }
148
149 for (i = 1; i < 32; i++)
150 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
151
152 err |= __get_user(used_math, &sc->sc_used_math);
153 conditional_used_math(used_math);
154
155 preempt_disable();
156
157 if (used_math()) {
158 /* restore fpu context if we have used it before */
159 own_fpu();
160 err |= restore_fp_context(sc);
161 } else {
162 /* signal handler may have used FPU. Give it up. */
163 lose_fpu();
164 }
165
166 preempt_enable();
167
168 return err;
169}
170
171void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
172 size_t frame_size)
173{
174 unsigned long sp;
175
176 /* Default to using normal stack */
177 sp = regs->regs[29];
178
179 /*
180 * FPU emulator may have it's own trampoline active just
181 * above the user stack, 16-bytes before the next lowest
182 * 16 byte boundary. Try to avoid trashing it.
183 */
184 sp -= 32;
185
186 /* This is the X/Open sanctioned signal stack switching. */
187 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
188 sp = current->sas_ss_sp + current->sas_ss_size;
189
190 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
191}
192
193int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
194{
195 int err;
196
197 /*
198 * Set up the return code ...
199 *
200 * li v0, __NR__foo_sigreturn
201 * syscall
202 */
203
204 err = __put_user(0x24020000 + syscall, tramp + 0);
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100205 err |= __put_user(0x0000000c , tramp + 1);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100206 if (ICACHE_REFILLS_WORKAROUND_WAR) {
207 err |= __put_user(0, tramp + 2);
208 err |= __put_user(0, tramp + 3);
209 err |= __put_user(0, tramp + 4);
210 err |= __put_user(0, tramp + 5);
211 err |= __put_user(0, tramp + 6);
212 err |= __put_user(0, tramp + 7);
213 }
214 flush_cache_sigtramp((unsigned long) tramp);
215
216 return err;
217}
218
219/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * Atomically swap in the new signal mask, and wait for a signal.
221 */
222
223#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100224asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000226 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000227 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Ralf Baechlefe00f942005-03-01 19:22:29 +0000229 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
231 return -EFAULT;
232 sigdelsetmask(&newset, ~_BLOCKABLE);
233
234 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000235 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 current->blocked = newset;
237 recalc_sigpending();
238 spin_unlock_irq(&current->sighand->siglock);
239
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000240 current->state = TASK_INTERRUPTIBLE;
241 schedule();
242 set_thread_flag(TIF_RESTORE_SIGMASK);
243 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245#endif
246
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100247asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000249 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000250 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 size_t sigsetsize;
252
253 /* XXX Don't preclude handling different sized sigset_t's. */
254 sigsetsize = regs.regs[5];
255 if (sigsetsize != sizeof(sigset_t))
256 return -EINVAL;
257
Ralf Baechlefe00f942005-03-01 19:22:29 +0000258 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (copy_from_user(&newset, unewset, sizeof(newset)))
260 return -EFAULT;
261 sigdelsetmask(&newset, ~_BLOCKABLE);
262
263 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000264 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 current->blocked = newset;
Ralf Baechlee0daad42007-02-05 00:10:11 +0000266 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_unlock_irq(&current->sighand->siglock);
268
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000269 current->state = TASK_INTERRUPTIBLE;
270 schedule();
271 set_thread_flag(TIF_RESTORE_SIGMASK);
272 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275#ifdef CONFIG_TRAD_SIGNALS
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900276asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
277 struct sigaction __user *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct k_sigaction new_ka, old_ka;
280 int ret;
281 int err = 0;
282
283 if (act) {
284 old_sigset_t mask;
285
286 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
287 return -EFAULT;
288 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
289 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
290 err |= __get_user(mask, &act->sa_mask.sig[0]);
291 if (err)
292 return -EFAULT;
293
294 siginitset(&new_ka.sa.sa_mask, mask);
295 }
296
297 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
298
299 if (!ret && oact) {
300 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000301 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
303 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
304 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
305 err |= __put_user(0, &oact->sa_mask.sig[1]);
306 err |= __put_user(0, &oact->sa_mask.sig[2]);
307 err |= __put_user(0, &oact->sa_mask.sig[3]);
308 if (err)
309 return -EFAULT;
310 }
311
312 return ret;
313}
314#endif
315
316asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
317{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000318 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
319 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned long usp = regs.regs[29];
321
322 return do_sigaltstack(uss, uoss, usp);
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100326asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900328 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 sigset_t blocked;
330
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900331 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
333 goto badframe;
334 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
335 goto badframe;
336
337 sigdelsetmask(&blocked, ~_BLOCKABLE);
338 spin_lock_irq(&current->sighand->siglock);
339 current->blocked = blocked;
340 recalc_sigpending();
341 spin_unlock_irq(&current->sighand->siglock);
342
343 if (restore_sigcontext(&regs, &frame->sf_sc))
344 goto badframe;
345
346 /*
347 * Don't let your children do this ...
348 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 __asm__ __volatile__(
350 "move\t$29, %0\n\t"
351 "j\tsyscall_exit"
352 :/* no outputs */
353 :"r" (&regs));
354 /* Unreached */
355
356badframe:
357 force_sig(SIGSEGV, current);
358}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000359#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100361asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900363 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sigset_t set;
365 stack_t st;
366
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900367 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
369 goto badframe;
370 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
371 goto badframe;
372
373 sigdelsetmask(&set, ~_BLOCKABLE);
374 spin_lock_irq(&current->sighand->siglock);
375 current->blocked = set;
376 recalc_sigpending();
377 spin_unlock_irq(&current->sighand->siglock);
378
379 if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
380 goto badframe;
381
382 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
383 goto badframe;
384 /* It is more difficult to avoid calling this function than to
385 call it and ignore errors. */
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900386 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /*
389 * Don't let your children do this ...
390 */
391 __asm__ __volatile__(
392 "move\t$29, %0\n\t"
393 "j\tsyscall_exit"
394 :/* no outputs */
395 :"r" (&regs));
396 /* Unreached */
397
398badframe:
399 force_sig(SIGSEGV, current);
400}
401
402#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000403int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int signr, sigset_t *set)
405{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900406 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int err = 0;
408
409 frame = get_sigframe(ka, regs, sizeof(*frame));
410 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
411 goto give_sigsegv;
412
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100413 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 err |= setup_sigcontext(regs, &frame->sf_sc);
416 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
417 if (err)
418 goto give_sigsegv;
419
420 /*
421 * Arguments to signal handler:
422 *
423 * a0 = signal number
424 * a1 = 0 (should be cause)
425 * a2 = pointer to struct sigcontext
426 *
427 * $25 and c0_epc point to the signal handler, $29 points to the
428 * struct sigframe.
429 */
430 regs->regs[ 4] = signr;
431 regs->regs[ 5] = 0;
432 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
433 regs->regs[29] = (unsigned long) frame;
434 regs->regs[31] = (unsigned long) frame->sf_code;
435 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
436
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100437 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100439 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442give_sigsegv:
443 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000444 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446#endif
447
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000448int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int signr, sigset_t *set, siginfo_t *info)
450{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900451 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int err = 0;
453
454 frame = get_sigframe(ka, regs, sizeof(*frame));
455 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
456 goto give_sigsegv;
457
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100458 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* Create siginfo. */
461 err |= copy_siginfo_to_user(&frame->rs_info, info);
462
463 /* Create the ucontext. */
464 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900465 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900466 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 &frame->rs_uc.uc_stack.ss_sp);
468 err |= __put_user(sas_ss_flags(regs->regs[29]),
469 &frame->rs_uc.uc_stack.ss_flags);
470 err |= __put_user(current->sas_ss_size,
471 &frame->rs_uc.uc_stack.ss_size);
472 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
473 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
474
475 if (err)
476 goto give_sigsegv;
477
478 /*
479 * Arguments to signal handler:
480 *
481 * a0 = signal number
482 * a1 = 0 (should be cause)
483 * a2 = pointer to ucontext
484 *
485 * $25 and c0_epc point to the signal handler, $29 points to
486 * the struct rt_sigframe.
487 */
488 regs->regs[ 4] = signr;
489 regs->regs[ 5] = (unsigned long) &frame->rs_info;
490 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
491 regs->regs[29] = (unsigned long) frame;
492 regs->regs[31] = (unsigned long) frame->rs_code;
493 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
494
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100495 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 current->comm, current->pid,
497 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100498
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501give_sigsegv:
502 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000503 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Franck Bui-Huue692eb32007-02-05 15:24:28 +0100506static int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
508{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000509 int ret;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 switch(regs->regs[0]) {
512 case ERESTART_RESTARTBLOCK:
513 case ERESTARTNOHAND:
514 regs->regs[2] = EINTR;
515 break;
516 case ERESTARTSYS:
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000517 if (!(ka->sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 regs->regs[2] = EINTR;
519 break;
520 }
521 /* fallthrough */
522 case ERESTARTNOINTR: /* Userland will reload $v0. */
523 regs->regs[7] = regs->regs[26];
524 regs->cp0_epc -= 8;
525 }
526
527 regs->regs[0] = 0; /* Don't deal with this again. */
528
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000529 if (sig_uses_siginfo(ka))
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000530 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 else
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000532 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Steven Rostedt69be8f12005-08-29 11:44:09 -0400534 spin_lock_irq(&current->sighand->siglock);
535 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
536 if (!(ka->sa.sa_flags & SA_NODEFER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sigaddset(&current->blocked,sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400538 recalc_sigpending();
539 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000540
541 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000544void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000547 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 siginfo_t info;
549 int signr;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * We want the common case to go fast, which is why we may in certain
553 * cases get here from kernel mode. Just return without doing anything
554 * if so.
555 */
556 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000559 if (test_thread_flag(TIF_RESTORE_SIGMASK))
560 oldset = &current->saved_sigmask;
561 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 oldset = &current->blocked;
563
564 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000565 if (signr > 0) {
566 /* Whee! Actually deliver the signal. */
567 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
568 /*
569 * A signal was successfully delivered; the saved
570 * sigmask will have been stored in the signal frame,
571 * and will be restored by sigreturn, so we can simply
572 * clear the TIF_RESTORE_SIGMASK flag.
573 */
574 if (test_thread_flag(TIF_RESTORE_SIGMASK))
575 clear_thread_flag(TIF_RESTORE_SIGMASK);
576 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100577
578 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * Who's code doesn't conform to the restartable syscall convention
583 * dies here!!! The li instruction, a single machine instruction,
584 * must directly be followed by the syscall instruction.
585 */
586 if (regs->regs[0]) {
587 if (regs->regs[2] == ERESTARTNOHAND ||
588 regs->regs[2] == ERESTARTSYS ||
589 regs->regs[2] == ERESTARTNOINTR) {
590 regs->regs[7] = regs->regs[26];
591 regs->cp0_epc -= 8;
592 }
593 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
594 regs->regs[2] = __NR_restart_syscall;
595 regs->regs[7] = regs->regs[26];
596 regs->cp0_epc -= 4;
597 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100598 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000600
601 /*
602 * If there's no signal to deliver, we just put the saved sigmask
603 * back
604 */
605 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
606 clear_thread_flag(TIF_RESTORE_SIGMASK);
607 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
612 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000613 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000615asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 __u32 thread_info_flags)
617{
618 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000619 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
620 current->thread.abi->do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}