blob: a4e106c56ab5d1d6d28d8e823b66ea657f6a8aaa [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/signal.h>
17#include <linux/errno.h>
18#include <linux/wait.h>
19#include <linux/ptrace.h>
20#include <linux/unistd.h>
21#include <linux/compiler.h>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/ucontext.h>
31#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000032#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include "signal-common.h"
35
Ralf Baechle66680582007-02-13 01:31:48 +000036/*
37 * Horribly complicated - with the bloody RM9000 workarounds enabled
38 * the signal trampolines is moving to the end of the structure so we can
39 * increase the alignment without breaking software compatibility.
40 */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010041#if ICACHE_REFILLS_WORKAROUND_WAR == 0
42
Ralf Baechle66680582007-02-13 01:31:48 +000043struct sigframe {
44 u32 sf_ass[4]; /* argument save space for o32 */
45 u32 sf_code[2]; /* signal trampoline */
46 struct sigcontext sf_sc;
47 sigset_t sf_mask;
48};
49
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010050struct rt_sigframe {
51 u32 rs_ass[4]; /* argument save space for o32 */
52 u32 rs_code[2]; /* signal trampoline */
53 struct siginfo rs_info;
54 struct ucontext rs_uc;
55};
56
57#else
58
Ralf Baechle66680582007-02-13 01:31:48 +000059struct sigframe {
60 u32 sf_ass[4]; /* argument save space for o32 */
61 u32 sf_pad[2];
62 struct sigcontext sf_sc; /* hw context */
63 sigset_t sf_mask;
64 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
65};
66
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010067struct rt_sigframe {
68 u32 rs_ass[4]; /* argument save space for o32 */
69 u32 rs_pad[2];
70 struct siginfo rs_info;
71 struct ucontext rs_uc;
72 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
73};
74
75#endif
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010078 * Helper routines
79 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +090080static int protected_save_fp_context(struct sigcontext __user *sc)
81{
82 int err;
83 while (1) {
84 lock_fpu_owner();
85 own_fpu_inatomic(1);
86 err = save_fp_context(sc); /* this might fail */
87 unlock_fpu_owner();
88 if (likely(!err))
89 break;
90 /* touch the sigcontext and try again */
91 err = __put_user(0, &sc->sc_fpregs[0]) |
92 __put_user(0, &sc->sc_fpregs[31]) |
93 __put_user(0, &sc->sc_fpc_csr);
94 if (err)
95 break; /* really bad sigcontext */
96 }
97 return err;
98}
99
100static int protected_restore_fp_context(struct sigcontext __user *sc)
101{
102 int err, tmp;
103 while (1) {
104 lock_fpu_owner();
105 own_fpu_inatomic(0);
106 err = restore_fp_context(sc); /* this might fail */
107 unlock_fpu_owner();
108 if (likely(!err))
109 break;
110 /* touch the sigcontext and try again */
111 err = __get_user(tmp, &sc->sc_fpregs[0]) |
112 __get_user(tmp, &sc->sc_fpregs[31]) |
113 __get_user(tmp, &sc->sc_fpc_csr);
114 if (err)
115 break; /* really bad sigcontext */
116 }
117 return err;
118}
119
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100120int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
121{
122 int err = 0;
123 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900124 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100125
126 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
127
128 err |= __put_user(0, &sc->sc_regs[0]);
129 for (i = 1; i < 32; i++)
130 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
131
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100132#ifdef CONFIG_CPU_HAS_SMARTMIPS
133 err |= __put_user(regs->acx, &sc->sc_acx);
134#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100135 err |= __put_user(regs->hi, &sc->sc_mdhi);
136 err |= __put_user(regs->lo, &sc->sc_mdlo);
137 if (cpu_has_dsp) {
138 err |= __put_user(mfhi1(), &sc->sc_hi1);
139 err |= __put_user(mflo1(), &sc->sc_lo1);
140 err |= __put_user(mfhi2(), &sc->sc_hi2);
141 err |= __put_user(mflo2(), &sc->sc_lo2);
142 err |= __put_user(mfhi3(), &sc->sc_hi3);
143 err |= __put_user(mflo3(), &sc->sc_lo3);
144 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
145 }
146
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900147 used_math = !!used_math();
148 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100149
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900150 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100151 /*
152 * Save FPU state to signal context. Signal handler
153 * will "inherit" current FPU state.
154 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900155 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100156 }
157 return err;
158}
159
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900160int fpcsr_pending(unsigned int __user *fpcsr)
161{
162 int err, sig = 0;
163 unsigned int csr, enabled;
164
165 err = __get_user(csr, fpcsr);
166 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
167 /*
168 * If the signal handler set some FPU exceptions, clear it and
169 * send SIGFPE.
170 */
171 if (csr & enabled) {
172 csr &= ~enabled;
173 err |= __put_user(csr, fpcsr);
174 sig = SIGFPE;
175 }
176 return err ?: sig;
177}
178
179static int
180check_and_restore_fp_context(struct sigcontext __user *sc)
181{
182 int err, sig;
183
184 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
185 if (err > 0)
186 err = 0;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900187 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900188 return err ?: sig;
189}
190
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100191int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
192{
193 unsigned int used_math;
194 unsigned long treg;
195 int err = 0;
196 int i;
197
198 /* Always make any pending restarted system calls return -EINTR */
199 current_thread_info()->restart_block.fn = do_no_restart_syscall;
200
201 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100202
203#ifdef CONFIG_CPU_HAS_SMARTMIPS
204 err |= __get_user(regs->acx, &sc->sc_acx);
205#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100206 err |= __get_user(regs->hi, &sc->sc_mdhi);
207 err |= __get_user(regs->lo, &sc->sc_mdlo);
208 if (cpu_has_dsp) {
209 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
210 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
211 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
212 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
213 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
214 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
215 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
216 }
217
218 for (i = 1; i < 32; i++)
219 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
220
221 err |= __get_user(used_math, &sc->sc_used_math);
222 conditional_used_math(used_math);
223
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900224 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100225 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900226 if (!err)
227 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100228 } else {
229 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900230 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100231 }
232
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100233 return err;
234}
235
236void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
237 size_t frame_size)
238{
239 unsigned long sp;
240
241 /* Default to using normal stack */
242 sp = regs->regs[29];
243
244 /*
245 * FPU emulator may have it's own trampoline active just
246 * above the user stack, 16-bytes before the next lowest
247 * 16 byte boundary. Try to avoid trashing it.
248 */
249 sp -= 32;
250
251 /* This is the X/Open sanctioned signal stack switching. */
252 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
253 sp = current->sas_ss_sp + current->sas_ss_size;
254
255 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
256}
257
258int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
259{
260 int err;
261
262 /*
263 * Set up the return code ...
264 *
265 * li v0, __NR__foo_sigreturn
266 * syscall
267 */
268
269 err = __put_user(0x24020000 + syscall, tramp + 0);
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100270 err |= __put_user(0x0000000c , tramp + 1);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100271 if (ICACHE_REFILLS_WORKAROUND_WAR) {
272 err |= __put_user(0, tramp + 2);
273 err |= __put_user(0, tramp + 3);
274 err |= __put_user(0, tramp + 4);
275 err |= __put_user(0, tramp + 5);
276 err |= __put_user(0, tramp + 6);
277 err |= __put_user(0, tramp + 7);
278 }
279 flush_cache_sigtramp((unsigned long) tramp);
280
281 return err;
282}
283
284/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * Atomically swap in the new signal mask, and wait for a signal.
286 */
287
288#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100289asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000291 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000292 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Ralf Baechlefe00f942005-03-01 19:22:29 +0000294 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
296 return -EFAULT;
297 sigdelsetmask(&newset, ~_BLOCKABLE);
298
299 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000300 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 current->blocked = newset;
302 recalc_sigpending();
303 spin_unlock_irq(&current->sighand->siglock);
304
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000305 current->state = TASK_INTERRUPTIBLE;
306 schedule();
307 set_thread_flag(TIF_RESTORE_SIGMASK);
308 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310#endif
311
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100312asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000314 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000315 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 size_t sigsetsize;
317
318 /* XXX Don't preclude handling different sized sigset_t's. */
319 sigsetsize = regs.regs[5];
320 if (sigsetsize != sizeof(sigset_t))
321 return -EINVAL;
322
Ralf Baechlefe00f942005-03-01 19:22:29 +0000323 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (copy_from_user(&newset, unewset, sizeof(newset)))
325 return -EFAULT;
326 sigdelsetmask(&newset, ~_BLOCKABLE);
327
328 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000329 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 current->blocked = newset;
Ralf Baechlee0daad42007-02-05 00:10:11 +0000331 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 spin_unlock_irq(&current->sighand->siglock);
333
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000334 current->state = TASK_INTERRUPTIBLE;
335 schedule();
336 set_thread_flag(TIF_RESTORE_SIGMASK);
337 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340#ifdef CONFIG_TRAD_SIGNALS
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900341asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
342 struct sigaction __user *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct k_sigaction new_ka, old_ka;
345 int ret;
346 int err = 0;
347
348 if (act) {
349 old_sigset_t mask;
350
351 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
352 return -EFAULT;
353 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
354 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
355 err |= __get_user(mask, &act->sa_mask.sig[0]);
356 if (err)
357 return -EFAULT;
358
359 siginitset(&new_ka.sa.sa_mask, mask);
360 }
361
362 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
363
364 if (!ret && oact) {
365 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000366 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
368 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
369 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
370 err |= __put_user(0, &oact->sa_mask.sig[1]);
371 err |= __put_user(0, &oact->sa_mask.sig[2]);
372 err |= __put_user(0, &oact->sa_mask.sig[3]);
373 if (err)
374 return -EFAULT;
375 }
376
377 return ret;
378}
379#endif
380
381asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
382{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000383 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
384 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned long usp = regs.regs[29];
386
387 return do_sigaltstack(uss, uoss, usp);
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100391asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900393 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900395 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900397 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
399 goto badframe;
400 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
401 goto badframe;
402
403 sigdelsetmask(&blocked, ~_BLOCKABLE);
404 spin_lock_irq(&current->sighand->siglock);
405 current->blocked = blocked;
406 recalc_sigpending();
407 spin_unlock_irq(&current->sighand->siglock);
408
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900409 sig = restore_sigcontext(&regs, &frame->sf_sc);
410 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900412 else if (sig)
413 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /*
416 * Don't let your children do this ...
417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 __asm__ __volatile__(
419 "move\t$29, %0\n\t"
420 "j\tsyscall_exit"
421 :/* no outputs */
422 :"r" (&regs));
423 /* Unreached */
424
425badframe:
426 force_sig(SIGSEGV, current);
427}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000428#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100430asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900432 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sigset_t set;
434 stack_t st;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900435 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900437 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
439 goto badframe;
440 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
441 goto badframe;
442
443 sigdelsetmask(&set, ~_BLOCKABLE);
444 spin_lock_irq(&current->sighand->siglock);
445 current->blocked = set;
446 recalc_sigpending();
447 spin_unlock_irq(&current->sighand->siglock);
448
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900449 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
450 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900452 else if (sig)
453 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
456 goto badframe;
457 /* It is more difficult to avoid calling this function than to
458 call it and ignore errors. */
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900459 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /*
462 * Don't let your children do this ...
463 */
464 __asm__ __volatile__(
465 "move\t$29, %0\n\t"
466 "j\tsyscall_exit"
467 :/* no outputs */
468 :"r" (&regs));
469 /* Unreached */
470
471badframe:
472 force_sig(SIGSEGV, current);
473}
474
475#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000476static int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int signr, sigset_t *set)
478{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900479 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int err = 0;
481
482 frame = get_sigframe(ka, regs, sizeof(*frame));
483 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
484 goto give_sigsegv;
485
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100486 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 err |= setup_sigcontext(regs, &frame->sf_sc);
489 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
490 if (err)
491 goto give_sigsegv;
492
493 /*
494 * Arguments to signal handler:
495 *
496 * a0 = signal number
497 * a1 = 0 (should be cause)
498 * a2 = pointer to struct sigcontext
499 *
500 * $25 and c0_epc point to the signal handler, $29 points to the
501 * struct sigframe.
502 */
503 regs->regs[ 4] = signr;
504 regs->regs[ 5] = 0;
505 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
506 regs->regs[29] = (unsigned long) frame;
507 regs->regs[31] = (unsigned long) frame->sf_code;
508 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
509
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100510 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100512 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515give_sigsegv:
516 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000517 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519#endif
520
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000521static int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int signr, sigset_t *set, siginfo_t *info)
523{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900524 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int err = 0;
526
527 frame = get_sigframe(ka, regs, sizeof(*frame));
528 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
529 goto give_sigsegv;
530
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100531 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Create siginfo. */
534 err |= copy_siginfo_to_user(&frame->rs_info, info);
535
536 /* Create the ucontext. */
537 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900538 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900539 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 &frame->rs_uc.uc_stack.ss_sp);
541 err |= __put_user(sas_ss_flags(regs->regs[29]),
542 &frame->rs_uc.uc_stack.ss_flags);
543 err |= __put_user(current->sas_ss_size,
544 &frame->rs_uc.uc_stack.ss_size);
545 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
546 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
547
548 if (err)
549 goto give_sigsegv;
550
551 /*
552 * Arguments to signal handler:
553 *
554 * a0 = signal number
555 * a1 = 0 (should be cause)
556 * a2 = pointer to ucontext
557 *
558 * $25 and c0_epc point to the signal handler, $29 points to
559 * the struct rt_sigframe.
560 */
561 regs->regs[ 4] = signr;
562 regs->regs[ 5] = (unsigned long) &frame->rs_info;
563 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
564 regs->regs[29] = (unsigned long) frame;
565 regs->regs[31] = (unsigned long) frame->rs_code;
566 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
567
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100568 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 current->comm, current->pid,
570 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100571
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574give_sigsegv:
575 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000576 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000579struct mips_abi mips_abi = {
580#ifdef CONFIG_TRAD_SIGNALS
581 .setup_frame = setup_frame,
582#endif
583 .setup_rt_frame = setup_rt_frame,
584 .restart = __NR_restart_syscall
585};
586
Franck Bui-Huue692eb32007-02-05 15:24:28 +0100587static int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
589{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000590 int ret;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 switch(regs->regs[0]) {
593 case ERESTART_RESTARTBLOCK:
594 case ERESTARTNOHAND:
595 regs->regs[2] = EINTR;
596 break;
597 case ERESTARTSYS:
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000598 if (!(ka->sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 regs->regs[2] = EINTR;
600 break;
601 }
602 /* fallthrough */
603 case ERESTARTNOINTR: /* Userland will reload $v0. */
604 regs->regs[7] = regs->regs[26];
605 regs->cp0_epc -= 8;
606 }
607
608 regs->regs[0] = 0; /* Don't deal with this again. */
609
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000610 if (sig_uses_siginfo(ka))
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000611 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 else
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000613 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Steven Rostedt69be8f12005-08-29 11:44:09 -0400615 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle21a151d2007-10-11 23:46:15 +0100616 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400617 if (!(ka->sa.sa_flags & SA_NODEFER))
Ralf Baechle21a151d2007-10-11 23:46:15 +0100618 sigaddset(&current->blocked, sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400619 recalc_sigpending();
620 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000621
622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000625static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000628 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 siginfo_t info;
630 int signr;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * We want the common case to go fast, which is why we may in certain
634 * cases get here from kernel mode. Just return without doing anything
635 * if so.
636 */
637 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000638 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000640 if (test_thread_flag(TIF_RESTORE_SIGMASK))
641 oldset = &current->saved_sigmask;
642 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 oldset = &current->blocked;
644
645 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000646 if (signr > 0) {
647 /* Whee! Actually deliver the signal. */
648 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
649 /*
650 * A signal was successfully delivered; the saved
651 * sigmask will have been stored in the signal frame,
652 * and will be restored by sigreturn, so we can simply
653 * clear the TIF_RESTORE_SIGMASK flag.
654 */
655 if (test_thread_flag(TIF_RESTORE_SIGMASK))
656 clear_thread_flag(TIF_RESTORE_SIGMASK);
657 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100658
659 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Who's code doesn't conform to the restartable syscall convention
664 * dies here!!! The li instruction, a single machine instruction,
665 * must directly be followed by the syscall instruction.
666 */
667 if (regs->regs[0]) {
668 if (regs->regs[2] == ERESTARTNOHAND ||
669 regs->regs[2] == ERESTARTSYS ||
670 regs->regs[2] == ERESTARTNOINTR) {
671 regs->regs[7] = regs->regs[26];
672 regs->cp0_epc -= 8;
673 }
674 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000675 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 regs->regs[7] = regs->regs[26];
677 regs->cp0_epc -= 4;
678 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100679 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000681
682 /*
683 * If there's no signal to deliver, we just put the saved sigmask
684 * back
685 */
686 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
687 clear_thread_flag(TIF_RESTORE_SIGMASK);
688 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692/*
693 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000694 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000696asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 __u32 thread_info_flags)
698{
699 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000700 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000701 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}