blob: fa581192de214cc5428d7c274f4a28993bd5567f [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
Ralf Baechle66680582007-02-13 01:31:48 +000037/*
38 * Horribly complicated - with the bloody RM9000 workarounds enabled
39 * the signal trampolines is moving to the end of the structure so we can
40 * increase the alignment without breaking software compatibility.
41 */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010042#if ICACHE_REFILLS_WORKAROUND_WAR == 0
43
Ralf Baechle66680582007-02-13 01:31:48 +000044struct sigframe {
45 u32 sf_ass[4]; /* argument save space for o32 */
46 u32 sf_code[2]; /* signal trampoline */
47 struct sigcontext sf_sc;
48 sigset_t sf_mask;
49};
50
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010051struct rt_sigframe {
52 u32 rs_ass[4]; /* argument save space for o32 */
53 u32 rs_code[2]; /* signal trampoline */
54 struct siginfo rs_info;
55 struct ucontext rs_uc;
56};
57
58#else
59
Ralf Baechle66680582007-02-13 01:31:48 +000060struct sigframe {
61 u32 sf_ass[4]; /* argument save space for o32 */
62 u32 sf_pad[2];
63 struct sigcontext sf_sc; /* hw context */
64 sigset_t sf_mask;
65 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
66};
67
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010068struct rt_sigframe {
69 u32 rs_ass[4]; /* argument save space for o32 */
70 u32 rs_pad[2];
71 struct siginfo rs_info;
72 struct ucontext rs_uc;
73 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
74};
75
76#endif
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010079 * Helper routines
80 */
81int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
82{
83 int err = 0;
84 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +090085 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010086
87 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
88
89 err |= __put_user(0, &sc->sc_regs[0]);
90 for (i = 1; i < 32; i++)
91 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
92
Franck Bui-Huu9693a852007-02-02 17:41:47 +010093#ifdef CONFIG_CPU_HAS_SMARTMIPS
94 err |= __put_user(regs->acx, &sc->sc_acx);
95#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010096 err |= __put_user(regs->hi, &sc->sc_mdhi);
97 err |= __put_user(regs->lo, &sc->sc_mdlo);
98 if (cpu_has_dsp) {
99 err |= __put_user(mfhi1(), &sc->sc_hi1);
100 err |= __put_user(mflo1(), &sc->sc_lo1);
101 err |= __put_user(mfhi2(), &sc->sc_hi2);
102 err |= __put_user(mflo2(), &sc->sc_lo2);
103 err |= __put_user(mfhi3(), &sc->sc_hi3);
104 err |= __put_user(mflo3(), &sc->sc_lo3);
105 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
106 }
107
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900108 used_math = !!used_math();
109 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100110
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900111 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100112 /*
113 * Save FPU state to signal context. Signal handler
114 * will "inherit" current FPU state.
115 */
Atsushi Nemoto53231802007-04-14 02:37:26 +0900116 preempt_disable();
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900117 own_fpu(1);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100118 err |= save_fp_context(sc);
Atsushi Nemoto53231802007-04-14 02:37:26 +0900119 preempt_enable();
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100120 }
121 return err;
122}
123
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900124int fpcsr_pending(unsigned int __user *fpcsr)
125{
126 int err, sig = 0;
127 unsigned int csr, enabled;
128
129 err = __get_user(csr, fpcsr);
130 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
131 /*
132 * If the signal handler set some FPU exceptions, clear it and
133 * send SIGFPE.
134 */
135 if (csr & enabled) {
136 csr &= ~enabled;
137 err |= __put_user(csr, fpcsr);
138 sig = SIGFPE;
139 }
140 return err ?: sig;
141}
142
143static int
144check_and_restore_fp_context(struct sigcontext __user *sc)
145{
146 int err, sig;
147
148 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
149 if (err > 0)
150 err = 0;
Atsushi Nemoto53231802007-04-14 02:37:26 +0900151 preempt_disable();
152 own_fpu(0);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900153 err |= restore_fp_context(sc);
Atsushi Nemoto53231802007-04-14 02:37:26 +0900154 preempt_enable();
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900155 return err ?: sig;
156}
157
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100158int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
159{
160 unsigned int used_math;
161 unsigned long treg;
162 int err = 0;
163 int i;
164
165 /* Always make any pending restarted system calls return -EINTR */
166 current_thread_info()->restart_block.fn = do_no_restart_syscall;
167
168 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100169
170#ifdef CONFIG_CPU_HAS_SMARTMIPS
171 err |= __get_user(regs->acx, &sc->sc_acx);
172#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100173 err |= __get_user(regs->hi, &sc->sc_mdhi);
174 err |= __get_user(regs->lo, &sc->sc_mdlo);
175 if (cpu_has_dsp) {
176 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
177 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
178 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
179 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
180 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
181 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
182 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
183 }
184
185 for (i = 1; i < 32; i++)
186 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
187
188 err |= __get_user(used_math, &sc->sc_used_math);
189 conditional_used_math(used_math);
190
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900191 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100192 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900193 if (!err)
194 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100195 } else {
196 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900197 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100198 }
199
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100200 return err;
201}
202
203void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
204 size_t frame_size)
205{
206 unsigned long sp;
207
208 /* Default to using normal stack */
209 sp = regs->regs[29];
210
211 /*
212 * FPU emulator may have it's own trampoline active just
213 * above the user stack, 16-bytes before the next lowest
214 * 16 byte boundary. Try to avoid trashing it.
215 */
216 sp -= 32;
217
218 /* This is the X/Open sanctioned signal stack switching. */
219 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
220 sp = current->sas_ss_sp + current->sas_ss_size;
221
222 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
223}
224
225int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
226{
227 int err;
228
229 /*
230 * Set up the return code ...
231 *
232 * li v0, __NR__foo_sigreturn
233 * syscall
234 */
235
236 err = __put_user(0x24020000 + syscall, tramp + 0);
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100237 err |= __put_user(0x0000000c , tramp + 1);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100238 if (ICACHE_REFILLS_WORKAROUND_WAR) {
239 err |= __put_user(0, tramp + 2);
240 err |= __put_user(0, tramp + 3);
241 err |= __put_user(0, tramp + 4);
242 err |= __put_user(0, tramp + 5);
243 err |= __put_user(0, tramp + 6);
244 err |= __put_user(0, tramp + 7);
245 }
246 flush_cache_sigtramp((unsigned long) tramp);
247
248 return err;
249}
250
251/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * Atomically swap in the new signal mask, and wait for a signal.
253 */
254
255#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100256asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000258 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000259 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Ralf Baechlefe00f942005-03-01 19:22:29 +0000261 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
263 return -EFAULT;
264 sigdelsetmask(&newset, ~_BLOCKABLE);
265
266 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000267 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 current->blocked = newset;
269 recalc_sigpending();
270 spin_unlock_irq(&current->sighand->siglock);
271
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000272 current->state = TASK_INTERRUPTIBLE;
273 schedule();
274 set_thread_flag(TIF_RESTORE_SIGMASK);
275 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277#endif
278
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100279asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000281 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000282 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 size_t sigsetsize;
284
285 /* XXX Don't preclude handling different sized sigset_t's. */
286 sigsetsize = regs.regs[5];
287 if (sigsetsize != sizeof(sigset_t))
288 return -EINVAL;
289
Ralf Baechlefe00f942005-03-01 19:22:29 +0000290 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (copy_from_user(&newset, unewset, sizeof(newset)))
292 return -EFAULT;
293 sigdelsetmask(&newset, ~_BLOCKABLE);
294
295 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000296 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 current->blocked = newset;
Ralf Baechlee0daad42007-02-05 00:10:11 +0000298 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 spin_unlock_irq(&current->sighand->siglock);
300
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000301 current->state = TASK_INTERRUPTIBLE;
302 schedule();
303 set_thread_flag(TIF_RESTORE_SIGMASK);
304 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307#ifdef CONFIG_TRAD_SIGNALS
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900308asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
309 struct sigaction __user *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct k_sigaction new_ka, old_ka;
312 int ret;
313 int err = 0;
314
315 if (act) {
316 old_sigset_t mask;
317
318 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
319 return -EFAULT;
320 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
321 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
322 err |= __get_user(mask, &act->sa_mask.sig[0]);
323 if (err)
324 return -EFAULT;
325
326 siginitset(&new_ka.sa.sa_mask, mask);
327 }
328
329 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
330
331 if (!ret && oact) {
332 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000333 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
335 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
336 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
337 err |= __put_user(0, &oact->sa_mask.sig[1]);
338 err |= __put_user(0, &oact->sa_mask.sig[2]);
339 err |= __put_user(0, &oact->sa_mask.sig[3]);
340 if (err)
341 return -EFAULT;
342 }
343
344 return ret;
345}
346#endif
347
348asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
349{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000350 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
351 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned long usp = regs.regs[29];
353
354 return do_sigaltstack(uss, uoss, usp);
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100358asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900360 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900362 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900364 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
366 goto badframe;
367 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
368 goto badframe;
369
370 sigdelsetmask(&blocked, ~_BLOCKABLE);
371 spin_lock_irq(&current->sighand->siglock);
372 current->blocked = blocked;
373 recalc_sigpending();
374 spin_unlock_irq(&current->sighand->siglock);
375
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900376 sig = restore_sigcontext(&regs, &frame->sf_sc);
377 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900379 else if (sig)
380 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /*
383 * Don't let your children do this ...
384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 __asm__ __volatile__(
386 "move\t$29, %0\n\t"
387 "j\tsyscall_exit"
388 :/* no outputs */
389 :"r" (&regs));
390 /* Unreached */
391
392badframe:
393 force_sig(SIGSEGV, current);
394}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000395#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100397asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900399 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 sigset_t set;
401 stack_t st;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900402 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900404 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
406 goto badframe;
407 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
408 goto badframe;
409
410 sigdelsetmask(&set, ~_BLOCKABLE);
411 spin_lock_irq(&current->sighand->siglock);
412 current->blocked = set;
413 recalc_sigpending();
414 spin_unlock_irq(&current->sighand->siglock);
415
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900416 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
417 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900419 else if (sig)
420 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
423 goto badframe;
424 /* It is more difficult to avoid calling this function than to
425 call it and ignore errors. */
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900426 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /*
429 * Don't let your children do this ...
430 */
431 __asm__ __volatile__(
432 "move\t$29, %0\n\t"
433 "j\tsyscall_exit"
434 :/* no outputs */
435 :"r" (&regs));
436 /* Unreached */
437
438badframe:
439 force_sig(SIGSEGV, current);
440}
441
442#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000443static int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int signr, sigset_t *set)
445{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900446 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int err = 0;
448
449 frame = get_sigframe(ka, regs, sizeof(*frame));
450 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
451 goto give_sigsegv;
452
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100453 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 err |= setup_sigcontext(regs, &frame->sf_sc);
456 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
457 if (err)
458 goto give_sigsegv;
459
460 /*
461 * Arguments to signal handler:
462 *
463 * a0 = signal number
464 * a1 = 0 (should be cause)
465 * a2 = pointer to struct sigcontext
466 *
467 * $25 and c0_epc point to the signal handler, $29 points to the
468 * struct sigframe.
469 */
470 regs->regs[ 4] = signr;
471 regs->regs[ 5] = 0;
472 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
473 regs->regs[29] = (unsigned long) frame;
474 regs->regs[31] = (unsigned long) frame->sf_code;
475 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
476
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100477 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100479 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482give_sigsegv:
483 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000484 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486#endif
487
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000488static int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int signr, sigset_t *set, siginfo_t *info)
490{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900491 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int err = 0;
493
494 frame = get_sigframe(ka, regs, sizeof(*frame));
495 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
496 goto give_sigsegv;
497
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100498 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* Create siginfo. */
501 err |= copy_siginfo_to_user(&frame->rs_info, info);
502
503 /* Create the ucontext. */
504 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900505 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900506 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 &frame->rs_uc.uc_stack.ss_sp);
508 err |= __put_user(sas_ss_flags(regs->regs[29]),
509 &frame->rs_uc.uc_stack.ss_flags);
510 err |= __put_user(current->sas_ss_size,
511 &frame->rs_uc.uc_stack.ss_size);
512 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
513 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
514
515 if (err)
516 goto give_sigsegv;
517
518 /*
519 * Arguments to signal handler:
520 *
521 * a0 = signal number
522 * a1 = 0 (should be cause)
523 * a2 = pointer to ucontext
524 *
525 * $25 and c0_epc point to the signal handler, $29 points to
526 * the struct rt_sigframe.
527 */
528 regs->regs[ 4] = signr;
529 regs->regs[ 5] = (unsigned long) &frame->rs_info;
530 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
531 regs->regs[29] = (unsigned long) frame;
532 regs->regs[31] = (unsigned long) frame->rs_code;
533 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
534
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100535 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 current->comm, current->pid,
537 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100538
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541give_sigsegv:
542 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000543 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000546struct mips_abi mips_abi = {
547#ifdef CONFIG_TRAD_SIGNALS
548 .setup_frame = setup_frame,
549#endif
550 .setup_rt_frame = setup_rt_frame,
551 .restart = __NR_restart_syscall
552};
553
Franck Bui-Huue692eb32007-02-05 15:24:28 +0100554static int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
556{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000557 int ret;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 switch(regs->regs[0]) {
560 case ERESTART_RESTARTBLOCK:
561 case ERESTARTNOHAND:
562 regs->regs[2] = EINTR;
563 break;
564 case ERESTARTSYS:
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000565 if (!(ka->sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 regs->regs[2] = EINTR;
567 break;
568 }
569 /* fallthrough */
570 case ERESTARTNOINTR: /* Userland will reload $v0. */
571 regs->regs[7] = regs->regs[26];
572 regs->cp0_epc -= 8;
573 }
574
575 regs->regs[0] = 0; /* Don't deal with this again. */
576
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000577 if (sig_uses_siginfo(ka))
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000578 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 else
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000580 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Steven Rostedt69be8f12005-08-29 11:44:09 -0400582 spin_lock_irq(&current->sighand->siglock);
583 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
584 if (!(ka->sa.sa_flags & SA_NODEFER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 sigaddset(&current->blocked,sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400586 recalc_sigpending();
587 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000588
589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000592static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000595 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 siginfo_t info;
597 int signr;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
600 * We want the common case to go fast, which is why we may in certain
601 * cases get here from kernel mode. Just return without doing anything
602 * if so.
603 */
604 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000605 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000607 if (test_thread_flag(TIF_RESTORE_SIGMASK))
608 oldset = &current->saved_sigmask;
609 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 oldset = &current->blocked;
611
612 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000613 if (signr > 0) {
614 /* Whee! Actually deliver the signal. */
615 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
616 /*
617 * A signal was successfully delivered; the saved
618 * sigmask will have been stored in the signal frame,
619 * and will be restored by sigreturn, so we can simply
620 * clear the TIF_RESTORE_SIGMASK flag.
621 */
622 if (test_thread_flag(TIF_RESTORE_SIGMASK))
623 clear_thread_flag(TIF_RESTORE_SIGMASK);
624 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100625
626 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * Who's code doesn't conform to the restartable syscall convention
631 * dies here!!! The li instruction, a single machine instruction,
632 * must directly be followed by the syscall instruction.
633 */
634 if (regs->regs[0]) {
635 if (regs->regs[2] == ERESTARTNOHAND ||
636 regs->regs[2] == ERESTARTSYS ||
637 regs->regs[2] == ERESTARTNOINTR) {
638 regs->regs[7] = regs->regs[26];
639 regs->cp0_epc -= 8;
640 }
641 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000642 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 regs->regs[7] = regs->regs[26];
644 regs->cp0_epc -= 4;
645 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100646 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000648
649 /*
650 * If there's no signal to deliver, we just put the saved sigmask
651 * back
652 */
653 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
654 clear_thread_flag(TIF_RESTORE_SIGMASK);
655 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659/*
660 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000661 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000663asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 __u32 thread_info_flags)
665{
666 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000667 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000668 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}