blob: 5922342bca3991d4b7ab4a9d6f8483fb3e416779 [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>
Ralf Baechledbda6ac2009-02-08 16:00:26 +000022#include <linux/syscalls.h>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090023#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010024#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ralf Baechlee50c0a82005-05-31 11:49:19 +000026#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/asm.h>
28#include <linux/bitops.h>
29#include <asm/cacheflush.h>
30#include <asm/fpu.h>
31#include <asm/sim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/ucontext.h>
33#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000034#include <asm/war.h>
David Daneyd814c282010-02-18 16:13:05 -080035#include <asm/vdso.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "signal-common.h"
38
Ralf Baechle137f6f32009-11-24 19:35:41 +000039static int (*save_fp_context)(struct sigcontext __user *sc);
40static int (*restore_fp_context)(struct sigcontext __user *sc);
41
42extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
43extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
44
45extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
46extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
47
Ralf Baechle66680582007-02-13 01:31:48 +000048struct sigframe {
49 u32 sf_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080050 u32 sf_pad[2]; /* Was: signal trampoline */
Ralf Baechle66680582007-02-13 01:31:48 +000051 struct sigcontext sf_sc;
52 sigset_t sf_mask;
53};
54
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010055struct rt_sigframe {
56 u32 rs_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080057 u32 rs_pad[2]; /* Was: signal trampoline */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010058 struct siginfo rs_info;
59 struct ucontext rs_uc;
60};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010063 * Helper routines
64 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +090065static int protected_save_fp_context(struct sigcontext __user *sc)
66{
67 int err;
68 while (1) {
69 lock_fpu_owner();
70 own_fpu_inatomic(1);
71 err = save_fp_context(sc); /* this might fail */
72 unlock_fpu_owner();
73 if (likely(!err))
74 break;
75 /* touch the sigcontext and try again */
76 err = __put_user(0, &sc->sc_fpregs[0]) |
77 __put_user(0, &sc->sc_fpregs[31]) |
78 __put_user(0, &sc->sc_fpc_csr);
79 if (err)
80 break; /* really bad sigcontext */
81 }
82 return err;
83}
84
85static int protected_restore_fp_context(struct sigcontext __user *sc)
86{
87 int err, tmp;
88 while (1) {
89 lock_fpu_owner();
90 own_fpu_inatomic(0);
91 err = restore_fp_context(sc); /* this might fail */
92 unlock_fpu_owner();
93 if (likely(!err))
94 break;
95 /* touch the sigcontext and try again */
96 err = __get_user(tmp, &sc->sc_fpregs[0]) |
97 __get_user(tmp, &sc->sc_fpregs[31]) |
98 __get_user(tmp, &sc->sc_fpc_csr);
99 if (err)
100 break; /* really bad sigcontext */
101 }
102 return err;
103}
104
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100105int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
106{
107 int err = 0;
108 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900109 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100110
111 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
112
113 err |= __put_user(0, &sc->sc_regs[0]);
114 for (i = 1; i < 32; i++)
115 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
116
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100117#ifdef CONFIG_CPU_HAS_SMARTMIPS
118 err |= __put_user(regs->acx, &sc->sc_acx);
119#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100120 err |= __put_user(regs->hi, &sc->sc_mdhi);
121 err |= __put_user(regs->lo, &sc->sc_mdlo);
122 if (cpu_has_dsp) {
123 err |= __put_user(mfhi1(), &sc->sc_hi1);
124 err |= __put_user(mflo1(), &sc->sc_lo1);
125 err |= __put_user(mfhi2(), &sc->sc_hi2);
126 err |= __put_user(mflo2(), &sc->sc_lo2);
127 err |= __put_user(mfhi3(), &sc->sc_hi3);
128 err |= __put_user(mflo3(), &sc->sc_lo3);
129 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
130 }
131
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900132 used_math = !!used_math();
133 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100134
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900135 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100136 /*
137 * Save FPU state to signal context. Signal handler
138 * will "inherit" current FPU state.
139 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900140 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100141 }
142 return err;
143}
144
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900145int fpcsr_pending(unsigned int __user *fpcsr)
146{
147 int err, sig = 0;
148 unsigned int csr, enabled;
149
150 err = __get_user(csr, fpcsr);
151 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
152 /*
153 * If the signal handler set some FPU exceptions, clear it and
154 * send SIGFPE.
155 */
156 if (csr & enabled) {
157 csr &= ~enabled;
158 err |= __put_user(csr, fpcsr);
159 sig = SIGFPE;
160 }
161 return err ?: sig;
162}
163
164static int
165check_and_restore_fp_context(struct sigcontext __user *sc)
166{
167 int err, sig;
168
169 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
170 if (err > 0)
171 err = 0;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900172 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900173 return err ?: sig;
174}
175
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100176int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
177{
178 unsigned int used_math;
179 unsigned long treg;
180 int err = 0;
181 int i;
182
183 /* Always make any pending restarted system calls return -EINTR */
184 current_thread_info()->restart_block.fn = do_no_restart_syscall;
185
186 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100187
188#ifdef CONFIG_CPU_HAS_SMARTMIPS
189 err |= __get_user(regs->acx, &sc->sc_acx);
190#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100191 err |= __get_user(regs->hi, &sc->sc_mdhi);
192 err |= __get_user(regs->lo, &sc->sc_mdlo);
193 if (cpu_has_dsp) {
194 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
195 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
196 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
197 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
198 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
199 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
200 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
201 }
202
203 for (i = 1; i < 32; i++)
204 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
205
206 err |= __get_user(used_math, &sc->sc_used_math);
207 conditional_used_math(used_math);
208
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900209 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100210 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900211 if (!err)
212 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100213 } else {
214 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900215 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100216 }
217
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100218 return err;
219}
220
221void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
222 size_t frame_size)
223{
224 unsigned long sp;
225
226 /* Default to using normal stack */
227 sp = regs->regs[29];
228
229 /*
230 * FPU emulator may have it's own trampoline active just
231 * above the user stack, 16-bytes before the next lowest
232 * 16 byte boundary. Try to avoid trashing it.
233 */
234 sp -= 32;
235
236 /* This is the X/Open sanctioned signal stack switching. */
237 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
238 sp = current->sas_ss_sp + current->sas_ss_size;
239
240 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
241}
242
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * Atomically swap in the new signal mask, and wait for a signal.
245 */
246
247#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100248asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000250 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000251 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Ralf Baechlefe00f942005-03-01 19:22:29 +0000253 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
255 return -EFAULT;
256 sigdelsetmask(&newset, ~_BLOCKABLE);
257
258 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000259 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 current->blocked = newset;
261 recalc_sigpending();
262 spin_unlock_irq(&current->sighand->siglock);
263
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000264 current->state = TASK_INTERRUPTIBLE;
265 schedule();
266 set_thread_flag(TIF_RESTORE_SIGMASK);
267 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269#endif
270
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100271asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000273 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000274 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 size_t sigsetsize;
276
277 /* XXX Don't preclude handling different sized sigset_t's. */
278 sigsetsize = regs.regs[5];
279 if (sigsetsize != sizeof(sigset_t))
280 return -EINVAL;
281
Ralf Baechlefe00f942005-03-01 19:22:29 +0000282 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (copy_from_user(&newset, unewset, sizeof(newset)))
284 return -EFAULT;
285 sigdelsetmask(&newset, ~_BLOCKABLE);
286
287 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000288 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 current->blocked = newset;
Ralf Baechlee0daad42007-02-05 00:10:11 +0000290 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 spin_unlock_irq(&current->sighand->siglock);
292
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000293 current->state = TASK_INTERRUPTIBLE;
294 schedule();
295 set_thread_flag(TIF_RESTORE_SIGMASK);
296 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000300SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
301 struct sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct k_sigaction new_ka, old_ka;
304 int ret;
305 int err = 0;
306
307 if (act) {
308 old_sigset_t mask;
309
310 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
311 return -EFAULT;
312 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
313 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
314 err |= __get_user(mask, &act->sa_mask.sig[0]);
315 if (err)
316 return -EFAULT;
317
318 siginitset(&new_ka.sa.sa_mask, mask);
319 }
320
321 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
322
323 if (!ret && oact) {
324 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000325 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
327 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
328 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
329 err |= __put_user(0, &oact->sa_mask.sig[1]);
330 err |= __put_user(0, &oact->sa_mask.sig[2]);
331 err |= __put_user(0, &oact->sa_mask.sig[3]);
332 if (err)
333 return -EFAULT;
334 }
335
336 return ret;
337}
338#endif
339
340asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
341{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000342 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
343 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned long usp = regs.regs[29];
345
346 return do_sigaltstack(uss, uoss, usp);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100350asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900352 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900354 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900356 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
358 goto badframe;
359 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
360 goto badframe;
361
362 sigdelsetmask(&blocked, ~_BLOCKABLE);
363 spin_lock_irq(&current->sighand->siglock);
364 current->blocked = blocked;
365 recalc_sigpending();
366 spin_unlock_irq(&current->sighand->siglock);
367
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900368 sig = restore_sigcontext(&regs, &frame->sf_sc);
369 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900371 else if (sig)
372 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /*
375 * Don't let your children do this ...
376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 __asm__ __volatile__(
378 "move\t$29, %0\n\t"
379 "j\tsyscall_exit"
380 :/* no outputs */
381 :"r" (&regs));
382 /* Unreached */
383
384badframe:
385 force_sig(SIGSEGV, current);
386}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000387#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100389asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900391 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sigset_t set;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900393 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900395 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
397 goto badframe;
398 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
399 goto badframe;
400
401 sigdelsetmask(&set, ~_BLOCKABLE);
402 spin_lock_irq(&current->sighand->siglock);
403 current->blocked = set;
404 recalc_sigpending();
405 spin_unlock_irq(&current->sighand->siglock);
406
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900407 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
408 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900410 else if (sig)
411 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* It is more difficult to avoid calling this function than to
414 call it and ignore errors. */
Al Viro4bfb8c52010-09-28 18:50:57 +0100415 do_sigaltstack(&frame->rs_uc.uc_stack, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /*
418 * Don't let your children do this ...
419 */
420 __asm__ __volatile__(
421 "move\t$29, %0\n\t"
422 "j\tsyscall_exit"
423 :/* no outputs */
424 :"r" (&regs));
425 /* Unreached */
426
427badframe:
428 force_sig(SIGSEGV, current);
429}
430
431#ifdef CONFIG_TRAD_SIGNALS
David Daneyd814c282010-02-18 16:13:05 -0800432static int setup_frame(void *sig_return, struct k_sigaction *ka,
433 struct pt_regs *regs, int signr, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900435 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int err = 0;
437
438 frame = get_sigframe(ka, regs, sizeof(*frame));
439 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
440 goto give_sigsegv;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 err |= setup_sigcontext(regs, &frame->sf_sc);
443 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
444 if (err)
445 goto give_sigsegv;
446
447 /*
448 * Arguments to signal handler:
449 *
450 * a0 = signal number
451 * a1 = 0 (should be cause)
452 * a2 = pointer to struct sigcontext
453 *
454 * $25 and c0_epc point to the signal handler, $29 points to the
455 * struct sigframe.
456 */
457 regs->regs[ 4] = signr;
458 regs->regs[ 5] = 0;
459 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
460 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800461 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
463
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100464 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100466 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469give_sigsegv:
470 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000471 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473#endif
474
David Daneyd814c282010-02-18 16:13:05 -0800475static int setup_rt_frame(void *sig_return, struct k_sigaction *ka,
476 struct pt_regs *regs, int signr, sigset_t *set,
477 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900479 struct rt_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* Create siginfo. */
487 err |= copy_siginfo_to_user(&frame->rs_info, info);
488
489 /* Create the ucontext. */
490 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900491 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900492 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 &frame->rs_uc.uc_stack.ss_sp);
494 err |= __put_user(sas_ss_flags(regs->regs[29]),
495 &frame->rs_uc.uc_stack.ss_flags);
496 err |= __put_user(current->sas_ss_size,
497 &frame->rs_uc.uc_stack.ss_size);
498 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
499 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
500
501 if (err)
502 goto give_sigsegv;
503
504 /*
505 * Arguments to signal handler:
506 *
507 * a0 = signal number
508 * a1 = 0 (should be cause)
509 * a2 = pointer to ucontext
510 *
511 * $25 and c0_epc point to the signal handler, $29 points to
512 * the struct rt_sigframe.
513 */
514 regs->regs[ 4] = signr;
515 regs->regs[ 5] = (unsigned long) &frame->rs_info;
516 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
517 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800518 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
520
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100521 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 current->comm, current->pid,
523 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100524
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527give_sigsegv:
528 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000529 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000532struct mips_abi mips_abi = {
533#ifdef CONFIG_TRAD_SIGNALS
534 .setup_frame = setup_frame,
David Daneyd814c282010-02-18 16:13:05 -0800535 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000536#endif
537 .setup_rt_frame = setup_rt_frame,
David Daneyd814c282010-02-18 16:13:05 -0800538 .rt_signal_return_offset =
539 offsetof(struct mips_vdso, rt_signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000540 .restart = __NR_restart_syscall
541};
542
Franck Bui-Huue692eb32007-02-05 15:24:28 +0100543static int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
545{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000546 int ret;
David Daneyd814c282010-02-18 16:13:05 -0800547 struct mips_abi *abi = current->thread.abi;
548 void *vdso = current->mm->context.vdso;
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000549
Al Viro8f5a00eb2010-09-28 18:50:37 +0100550 if (regs->regs[0]) {
551 switch(regs->regs[2]) {
552 case ERESTART_RESTARTBLOCK:
553 case ERESTARTNOHAND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 regs->regs[2] = EINTR;
555 break;
Al Viro8f5a00eb2010-09-28 18:50:37 +0100556 case ERESTARTSYS:
557 if (!(ka->sa.sa_flags & SA_RESTART)) {
558 regs->regs[2] = EINTR;
559 break;
560 }
561 /* fallthrough */
562 case ERESTARTNOINTR:
563 regs->regs[7] = regs->regs[26];
564 regs->regs[2] = regs->regs[0];
565 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Al Viro8f5a00eb2010-09-28 18:50:37 +0100568 regs->regs[0] = 0; /* Don't deal with this again. */
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000571 if (sig_uses_siginfo(ka))
David Daneyd814c282010-02-18 16:13:05 -0800572 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
573 ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 else
David Daneyd814c282010-02-18 16:13:05 -0800575 ret = abi->setup_frame(vdso + abi->signal_return_offset,
576 ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Al Viro062ab572010-09-28 18:50:17 +0100578 if (ret)
579 return ret;
580
Steven Rostedt69be8f12005-08-29 11:44:09 -0400581 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle21a151d2007-10-11 23:46:15 +0100582 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400583 if (!(ka->sa.sa_flags & SA_NODEFER))
Ralf Baechle21a151d2007-10-11 23:46:15 +0100584 sigaddset(&current->blocked, sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400585 recalc_sigpending();
586 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000587
588 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000591static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000594 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 siginfo_t info;
596 int signr;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * We want the common case to go fast, which is why we may in certain
600 * cases get here from kernel mode. Just return without doing anything
601 * if so.
602 */
603 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000606 if (test_thread_flag(TIF_RESTORE_SIGMASK))
607 oldset = &current->saved_sigmask;
608 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 oldset = &current->blocked;
610
611 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000612 if (signr > 0) {
613 /* Whee! Actually deliver the signal. */
614 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
615 /*
616 * A signal was successfully delivered; the saved
617 * sigmask will have been stored in the signal frame,
618 * and will be restored by sigreturn, so we can simply
619 * clear the TIF_RESTORE_SIGMASK flag.
620 */
621 if (test_thread_flag(TIF_RESTORE_SIGMASK))
622 clear_thread_flag(TIF_RESTORE_SIGMASK);
623 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100624
625 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (regs->regs[0]) {
629 if (regs->regs[2] == ERESTARTNOHAND ||
630 regs->regs[2] == ERESTARTSYS ||
631 regs->regs[2] == ERESTARTNOINTR) {
Al Viro8f5a00eb2010-09-28 18:50:37 +0100632 regs->regs[2] = regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 regs->regs[7] = regs->regs[26];
Al Viro8f5a00eb2010-09-28 18:50:37 +0100634 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000637 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 regs->regs[7] = regs->regs[26];
639 regs->cp0_epc -= 4;
640 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100641 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000643
644 /*
645 * If there's no signal to deliver, we just put the saved sigmask
646 * back
647 */
648 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
649 clear_thread_flag(TIF_RESTORE_SIGMASK);
650 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/*
655 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000656 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000658asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 __u32 thread_info_flags)
660{
661 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000662 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000663 do_signal(regs);
David Howellsd0420c82009-09-02 09:14:16 +0100664
665 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
666 clear_thread_flag(TIF_NOTIFY_RESUME);
667 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100668 if (current->replacement_session_keyring)
669 key_replace_session_keyring();
David Howellsd0420c82009-09-02 09:14:16 +0100670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
Ralf Baechle137f6f32009-11-24 19:35:41 +0000672
673#ifdef CONFIG_SMP
674static int smp_save_fp_context(struct sigcontext __user *sc)
675{
676 return raw_cpu_has_fpu
677 ? _save_fp_context(sc)
678 : fpu_emulator_save_context(sc);
679}
680
681static int smp_restore_fp_context(struct sigcontext __user *sc)
682{
683 return raw_cpu_has_fpu
684 ? _restore_fp_context(sc)
685 : fpu_emulator_restore_context(sc);
686}
687#endif
688
689static int signal_setup(void)
690{
691#ifdef CONFIG_SMP
692 /* For now just do the cpu_has_fpu check when the functions are invoked */
693 save_fp_context = smp_save_fp_context;
694 restore_fp_context = smp_restore_fp_context;
695#else
696 if (cpu_has_fpu) {
697 save_fp_context = _save_fp_context;
698 restore_fp_context = _restore_fp_context;
699 } else {
700 save_fp_context = fpu_emulator_save_context;
701 restore_fp_context = fpu_emulator_restore_context;
702 }
703#endif
704
705 return 0;
706}
707
708arch_initcall(signal_setup);