blob: d5a338a1739c9027fcc3db50c30a59f15f6c2e62 [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>
Ralf Baechle1f717922011-07-27 11:44:47 +010011#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/personality.h>
15#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
Ralf Baechledbda6ac2009-02-08 16:00:26 +000023#include <linux/syscalls.h>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090024#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010025#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000027#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/asm.h>
29#include <linux/bitops.h>
30#include <asm/cacheflush.h>
31#include <asm/fpu.h>
32#include <asm/sim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/ucontext.h>
34#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000035#include <asm/war.h>
David Daneyd814c282010-02-18 16:13:05 -080036#include <asm/vdso.h>
David Howellsb81947c2012-03-28 18:30:02 +010037#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include "signal-common.h"
40
Ralf Baechle137f6f32009-11-24 19:35:41 +000041static int (*save_fp_context)(struct sigcontext __user *sc);
42static int (*restore_fp_context)(struct sigcontext __user *sc);
43
44extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
45extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
46
47extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
48extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
49
Ralf Baechle66680582007-02-13 01:31:48 +000050struct sigframe {
51 u32 sf_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080052 u32 sf_pad[2]; /* Was: signal trampoline */
Ralf Baechle66680582007-02-13 01:31:48 +000053 struct sigcontext sf_sc;
54 sigset_t sf_mask;
55};
56
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010057struct rt_sigframe {
58 u32 rs_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080059 u32 rs_pad[2]; /* Was: signal trampoline */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010060 struct siginfo rs_info;
61 struct ucontext rs_uc;
62};
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010065 * Helper routines
66 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +090067static int protected_save_fp_context(struct sigcontext __user *sc)
68{
69 int err;
70 while (1) {
71 lock_fpu_owner();
72 own_fpu_inatomic(1);
73 err = save_fp_context(sc); /* this might fail */
74 unlock_fpu_owner();
75 if (likely(!err))
76 break;
77 /* touch the sigcontext and try again */
78 err = __put_user(0, &sc->sc_fpregs[0]) |
79 __put_user(0, &sc->sc_fpregs[31]) |
80 __put_user(0, &sc->sc_fpc_csr);
81 if (err)
82 break; /* really bad sigcontext */
83 }
84 return err;
85}
86
87static int protected_restore_fp_context(struct sigcontext __user *sc)
88{
David Daneyc726b822011-01-24 14:51:34 -080089 int err, tmp __maybe_unused;
Atsushi Nemotofaea6232007-04-16 23:19:44 +090090 while (1) {
91 lock_fpu_owner();
92 own_fpu_inatomic(0);
93 err = restore_fp_context(sc); /* this might fail */
94 unlock_fpu_owner();
95 if (likely(!err))
96 break;
97 /* touch the sigcontext and try again */
98 err = __get_user(tmp, &sc->sc_fpregs[0]) |
99 __get_user(tmp, &sc->sc_fpregs[31]) |
100 __get_user(tmp, &sc->sc_fpc_csr);
101 if (err)
102 break; /* really bad sigcontext */
103 }
104 return err;
105}
106
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100107int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
108{
109 int err = 0;
110 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900111 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100112
113 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
114
115 err |= __put_user(0, &sc->sc_regs[0]);
116 for (i = 1; i < 32; i++)
117 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
118
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100119#ifdef CONFIG_CPU_HAS_SMARTMIPS
120 err |= __put_user(regs->acx, &sc->sc_acx);
121#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100122 err |= __put_user(regs->hi, &sc->sc_mdhi);
123 err |= __put_user(regs->lo, &sc->sc_mdlo);
124 if (cpu_has_dsp) {
125 err |= __put_user(mfhi1(), &sc->sc_hi1);
126 err |= __put_user(mflo1(), &sc->sc_lo1);
127 err |= __put_user(mfhi2(), &sc->sc_hi2);
128 err |= __put_user(mflo2(), &sc->sc_lo2);
129 err |= __put_user(mfhi3(), &sc->sc_hi3);
130 err |= __put_user(mflo3(), &sc->sc_lo3);
131 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
132 }
133
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900134 used_math = !!used_math();
135 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100136
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900137 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100138 /*
139 * Save FPU state to signal context. Signal handler
140 * will "inherit" current FPU state.
141 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900142 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100143 }
144 return err;
145}
146
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900147int fpcsr_pending(unsigned int __user *fpcsr)
148{
149 int err, sig = 0;
150 unsigned int csr, enabled;
151
152 err = __get_user(csr, fpcsr);
153 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
154 /*
155 * If the signal handler set some FPU exceptions, clear it and
156 * send SIGFPE.
157 */
158 if (csr & enabled) {
159 csr &= ~enabled;
160 err |= __put_user(csr, fpcsr);
161 sig = SIGFPE;
162 }
163 return err ?: sig;
164}
165
166static int
167check_and_restore_fp_context(struct sigcontext __user *sc)
168{
169 int err, sig;
170
171 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
172 if (err > 0)
173 err = 0;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900174 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900175 return err ?: sig;
176}
177
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100178int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
179{
180 unsigned int used_math;
181 unsigned long treg;
182 int err = 0;
183 int i;
184
185 /* Always make any pending restarted system calls return -EINTR */
186 current_thread_info()->restart_block.fn = do_no_restart_syscall;
187
188 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100189
190#ifdef CONFIG_CPU_HAS_SMARTMIPS
191 err |= __get_user(regs->acx, &sc->sc_acx);
192#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100193 err |= __get_user(regs->hi, &sc->sc_mdhi);
194 err |= __get_user(regs->lo, &sc->sc_mdlo);
195 if (cpu_has_dsp) {
196 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
197 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
198 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
199 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
200 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
201 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
202 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
203 }
204
205 for (i = 1; i < 32; i++)
206 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
207
208 err |= __get_user(used_math, &sc->sc_used_math);
209 conditional_used_math(used_math);
210
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900211 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100212 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900213 if (!err)
214 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100215 } else {
216 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900217 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100218 }
219
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100220 return err;
221}
222
223void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
224 size_t frame_size)
225{
226 unsigned long sp;
227
228 /* Default to using normal stack */
229 sp = regs->regs[29];
230
231 /*
232 * FPU emulator may have it's own trampoline active just
233 * above the user stack, 16-bytes before the next lowest
234 * 16 byte boundary. Try to avoid trashing it.
235 */
236 sp -= 32;
237
238 /* This is the X/Open sanctioned signal stack switching. */
239 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
240 sp = current->sas_ss_sp + current->sas_ss_size;
241
242 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
243}
244
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100245/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * Atomically swap in the new signal mask, and wait for a signal.
247 */
248
249#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100250asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000252 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000253 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Ralf Baechlefe00f942005-03-01 19:22:29 +0000255 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
257 return -EFAULT;
258 sigdelsetmask(&newset, ~_BLOCKABLE);
259
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000260 current->saved_sigmask = current->blocked;
Matt Fleming8598f3c2012-02-14 11:40:52 +0000261 set_current_blocked(&newset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000263 current->state = TASK_INTERRUPTIBLE;
264 schedule();
265 set_thread_flag(TIF_RESTORE_SIGMASK);
266 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268#endif
269
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100270asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000272 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000273 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 size_t sigsetsize;
275
276 /* XXX Don't preclude handling different sized sigset_t's. */
277 sigsetsize = regs.regs[5];
278 if (sigsetsize != sizeof(sigset_t))
279 return -EINVAL;
280
Ralf Baechlefe00f942005-03-01 19:22:29 +0000281 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (copy_from_user(&newset, unewset, sizeof(newset)))
283 return -EFAULT;
284 sigdelsetmask(&newset, ~_BLOCKABLE);
285
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000286 current->saved_sigmask = current->blocked;
Matt Fleming8598f3c2012-02-14 11:40:52 +0000287 set_current_blocked(&newset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000289 current->state = TASK_INTERRUPTIBLE;
290 schedule();
291 set_thread_flag(TIF_RESTORE_SIGMASK);
292 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000296SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
297 struct sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct k_sigaction new_ka, old_ka;
300 int ret;
301 int err = 0;
302
303 if (act) {
304 old_sigset_t mask;
305
306 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
307 return -EFAULT;
308 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
309 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
310 err |= __get_user(mask, &act->sa_mask.sig[0]);
311 if (err)
312 return -EFAULT;
313
314 siginitset(&new_ka.sa.sa_mask, mask);
315 }
316
317 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
318
319 if (!ret && oact) {
320 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000321 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
323 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
324 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
325 err |= __put_user(0, &oact->sa_mask.sig[1]);
326 err |= __put_user(0, &oact->sa_mask.sig[2]);
327 err |= __put_user(0, &oact->sa_mask.sig[3]);
328 if (err)
329 return -EFAULT;
330 }
331
332 return ret;
333}
334#endif
335
336asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
337{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000338 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
339 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long usp = regs.regs[29];
341
342 return do_sigaltstack(uss, uoss, usp);
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100346asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900348 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900350 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900352 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
354 goto badframe;
355 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
356 goto badframe;
357
358 sigdelsetmask(&blocked, ~_BLOCKABLE);
Matt Fleming8598f3c2012-02-14 11:40:52 +0000359 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900361 sig = restore_sigcontext(&regs, &frame->sf_sc);
362 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900364 else if (sig)
365 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /*
368 * Don't let your children do this ...
369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 __asm__ __volatile__(
371 "move\t$29, %0\n\t"
372 "j\tsyscall_exit"
373 :/* no outputs */
374 :"r" (&regs));
375 /* Unreached */
376
377badframe:
378 force_sig(SIGSEGV, current);
379}
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000380#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100382asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900384 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sigset_t set;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900386 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900388 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
390 goto badframe;
391 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
392 goto badframe;
393
394 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Fleming8598f3c2012-02-14 11:40:52 +0000395 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900397 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
398 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900400 else if (sig)
401 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* It is more difficult to avoid calling this function than to
404 call it and ignore errors. */
Al Viro4bfb8c52010-09-28 18:50:57 +0100405 do_sigaltstack(&frame->rs_uc.uc_stack, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
408 * Don't let your children do this ...
409 */
410 __asm__ __volatile__(
411 "move\t$29, %0\n\t"
412 "j\tsyscall_exit"
413 :/* no outputs */
414 :"r" (&regs));
415 /* Unreached */
416
417badframe:
418 force_sig(SIGSEGV, current);
419}
420
421#ifdef CONFIG_TRAD_SIGNALS
David Daneyd814c282010-02-18 16:13:05 -0800422static int setup_frame(void *sig_return, struct k_sigaction *ka,
423 struct pt_regs *regs, int signr, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900425 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int err = 0;
427
428 frame = get_sigframe(ka, regs, sizeof(*frame));
429 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
430 goto give_sigsegv;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 err |= setup_sigcontext(regs, &frame->sf_sc);
433 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
434 if (err)
435 goto give_sigsegv;
436
437 /*
438 * Arguments to signal handler:
439 *
440 * a0 = signal number
441 * a1 = 0 (should be cause)
442 * a2 = pointer to struct sigcontext
443 *
444 * $25 and c0_epc point to the signal handler, $29 points to the
445 * struct sigframe.
446 */
447 regs->regs[ 4] = signr;
448 regs->regs[ 5] = 0;
449 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
450 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800451 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
453
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100454 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100456 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459give_sigsegv:
460 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000461 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463#endif
464
David Daneyd814c282010-02-18 16:13:05 -0800465static int setup_rt_frame(void *sig_return, struct k_sigaction *ka,
466 struct pt_regs *regs, int signr, sigset_t *set,
467 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900469 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int err = 0;
471
472 frame = get_sigframe(ka, regs, sizeof(*frame));
473 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
474 goto give_sigsegv;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Create siginfo. */
477 err |= copy_siginfo_to_user(&frame->rs_info, info);
478
479 /* Create the ucontext. */
480 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900481 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900482 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 &frame->rs_uc.uc_stack.ss_sp);
484 err |= __put_user(sas_ss_flags(regs->regs[29]),
485 &frame->rs_uc.uc_stack.ss_flags);
486 err |= __put_user(current->sas_ss_size,
487 &frame->rs_uc.uc_stack.ss_size);
488 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
489 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
490
491 if (err)
492 goto give_sigsegv;
493
494 /*
495 * Arguments to signal handler:
496 *
497 * a0 = signal number
498 * a1 = 0 (should be cause)
499 * a2 = pointer to ucontext
500 *
501 * $25 and c0_epc point to the signal handler, $29 points to
502 * the struct rt_sigframe.
503 */
504 regs->regs[ 4] = signr;
505 regs->regs[ 5] = (unsigned long) &frame->rs_info;
506 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
507 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800508 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
510
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100511 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 current->comm, current->pid,
513 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100514
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517give_sigsegv:
518 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000519 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000522struct mips_abi mips_abi = {
523#ifdef CONFIG_TRAD_SIGNALS
524 .setup_frame = setup_frame,
David Daneyd814c282010-02-18 16:13:05 -0800525 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000526#endif
527 .setup_rt_frame = setup_rt_frame,
David Daneyd814c282010-02-18 16:13:05 -0800528 .rt_signal_return_offset =
529 offsetof(struct mips_vdso, rt_signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000530 .restart = __NR_restart_syscall
531};
532
Franck Bui-Huue692eb32007-02-05 15:24:28 +0100533static int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
535{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000536 int ret;
David Daneyd814c282010-02-18 16:13:05 -0800537 struct mips_abi *abi = current->thread.abi;
538 void *vdso = current->mm->context.vdso;
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000539
Al Viro8f5a00eb2010-09-28 18:50:37 +0100540 if (regs->regs[0]) {
541 switch(regs->regs[2]) {
542 case ERESTART_RESTARTBLOCK:
543 case ERESTARTNOHAND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 regs->regs[2] = EINTR;
545 break;
Al Viro8f5a00eb2010-09-28 18:50:37 +0100546 case ERESTARTSYS:
547 if (!(ka->sa.sa_flags & SA_RESTART)) {
548 regs->regs[2] = EINTR;
549 break;
550 }
551 /* fallthrough */
552 case ERESTARTNOINTR:
553 regs->regs[7] = regs->regs[26];
554 regs->regs[2] = regs->regs[0];
555 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Al Viro8f5a00eb2010-09-28 18:50:37 +0100558 regs->regs[0] = 0; /* Don't deal with this again. */
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000561 if (sig_uses_siginfo(ka))
David Daneyd814c282010-02-18 16:13:05 -0800562 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
563 ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 else
David Daneyd814c282010-02-18 16:13:05 -0800565 ret = abi->setup_frame(vdso + abi->signal_return_offset,
566 ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Al Viro062ab572010-09-28 18:50:17 +0100568 if (ret)
569 return ret;
570
Matt Fleming8598f3c2012-02-14 11:40:52 +0000571 block_sigmask(ka, sig);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000572
573 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000576static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000579 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 siginfo_t info;
581 int signr;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /*
584 * We want the common case to go fast, which is why we may in certain
585 * cases get here from kernel mode. Just return without doing anything
586 * if so.
587 */
588 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000591 if (test_thread_flag(TIF_RESTORE_SIGMASK))
592 oldset = &current->saved_sigmask;
593 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 oldset = &current->blocked;
595
596 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000597 if (signr > 0) {
598 /* Whee! Actually deliver the signal. */
599 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
600 /*
601 * A signal was successfully delivered; the saved
602 * sigmask will have been stored in the signal frame,
603 * and will be restored by sigreturn, so we can simply
604 * clear the TIF_RESTORE_SIGMASK flag.
605 */
606 if (test_thread_flag(TIF_RESTORE_SIGMASK))
607 clear_thread_flag(TIF_RESTORE_SIGMASK);
608 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100609
610 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (regs->regs[0]) {
614 if (regs->regs[2] == ERESTARTNOHAND ||
615 regs->regs[2] == ERESTARTSYS ||
616 regs->regs[2] == ERESTARTNOINTR) {
Al Viro8f5a00eb2010-09-28 18:50:37 +0100617 regs->regs[2] = regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 regs->regs[7] = regs->regs[26];
Al Viro8f5a00eb2010-09-28 18:50:37 +0100619 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000622 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 regs->regs[7] = regs->regs[26];
624 regs->cp0_epc -= 4;
625 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100626 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000628
629 /*
630 * If there's no signal to deliver, we just put the saved sigmask
631 * back
632 */
633 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
634 clear_thread_flag(TIF_RESTORE_SIGMASK);
635 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/*
640 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000641 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000643asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 __u32 thread_info_flags)
645{
Ralf Baechle1f717922011-07-27 11:44:47 +0100646 local_irq_enable();
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000649 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000650 do_signal(regs);
David Howellsd0420c82009-09-02 09:14:16 +0100651
652 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
653 clear_thread_flag(TIF_NOTIFY_RESUME);
654 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100655 if (current->replacement_session_keyring)
656 key_replace_session_keyring();
David Howellsd0420c82009-09-02 09:14:16 +0100657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
Ralf Baechle137f6f32009-11-24 19:35:41 +0000659
660#ifdef CONFIG_SMP
661static int smp_save_fp_context(struct sigcontext __user *sc)
662{
663 return raw_cpu_has_fpu
664 ? _save_fp_context(sc)
665 : fpu_emulator_save_context(sc);
666}
667
668static int smp_restore_fp_context(struct sigcontext __user *sc)
669{
670 return raw_cpu_has_fpu
671 ? _restore_fp_context(sc)
672 : fpu_emulator_restore_context(sc);
673}
674#endif
675
676static int signal_setup(void)
677{
678#ifdef CONFIG_SMP
679 /* For now just do the cpu_has_fpu check when the functions are invoked */
680 save_fp_context = smp_save_fp_context;
681 restore_fp_context = smp_restore_fp_context;
682#else
683 if (cpu_has_fpu) {
684 save_fp_context = _save_fp_context;
685 restore_fp_context = _restore_fp_context;
686 } else {
687 save_fp_context = fpu_emulator_save_context;
688 restore_fp_context = fpu_emulator_restore_context;
689 }
690#endif
691
692 return 0;
693}
694
695arch_initcall(signal_setup);