blob: fd3ef2c2afbc37732d9bedcb9fff59d1dda935ea [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>
Douglas Leung01be0572013-03-25 13:21:11 -050038#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include "signal-common.h"
41
Ralf Baechle137f6f32009-11-24 19:35:41 +000042static int (*save_fp_context)(struct sigcontext __user *sc);
43static int (*restore_fp_context)(struct sigcontext __user *sc);
44
45extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
46extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
47
48extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
49extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
50
Ralf Baechle66680582007-02-13 01:31:48 +000051struct sigframe {
52 u32 sf_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080053 u32 sf_pad[2]; /* Was: signal trampoline */
Ralf Baechle66680582007-02-13 01:31:48 +000054 struct sigcontext sf_sc;
55 sigset_t sf_mask;
56};
57
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010058struct rt_sigframe {
59 u32 rs_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080060 u32 rs_pad[2]; /* Was: signal trampoline */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010061 struct siginfo rs_info;
62 struct ucontext rs_uc;
63};
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010066 * Helper routines
67 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +090068static int protected_save_fp_context(struct sigcontext __user *sc)
69{
70 int err;
71 while (1) {
72 lock_fpu_owner();
73 own_fpu_inatomic(1);
74 err = save_fp_context(sc); /* this might fail */
75 unlock_fpu_owner();
76 if (likely(!err))
77 break;
78 /* touch the sigcontext and try again */
79 err = __put_user(0, &sc->sc_fpregs[0]) |
80 __put_user(0, &sc->sc_fpregs[31]) |
81 __put_user(0, &sc->sc_fpc_csr);
82 if (err)
83 break; /* really bad sigcontext */
84 }
85 return err;
86}
87
88static int protected_restore_fp_context(struct sigcontext __user *sc)
89{
David Daneyc726b822011-01-24 14:51:34 -080090 int err, tmp __maybe_unused;
Atsushi Nemotofaea6232007-04-16 23:19:44 +090091 while (1) {
92 lock_fpu_owner();
93 own_fpu_inatomic(0);
94 err = restore_fp_context(sc); /* this might fail */
95 unlock_fpu_owner();
96 if (likely(!err))
97 break;
98 /* touch the sigcontext and try again */
99 err = __get_user(tmp, &sc->sc_fpregs[0]) |
100 __get_user(tmp, &sc->sc_fpregs[31]) |
101 __get_user(tmp, &sc->sc_fpc_csr);
102 if (err)
103 break; /* really bad sigcontext */
104 }
105 return err;
106}
107
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100108int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
109{
110 int err = 0;
111 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900112 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100113
114 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
115
116 err |= __put_user(0, &sc->sc_regs[0]);
117 for (i = 1; i < 32; i++)
118 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
119
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100120#ifdef CONFIG_CPU_HAS_SMARTMIPS
121 err |= __put_user(regs->acx, &sc->sc_acx);
122#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100123 err |= __put_user(regs->hi, &sc->sc_mdhi);
124 err |= __put_user(regs->lo, &sc->sc_mdlo);
125 if (cpu_has_dsp) {
126 err |= __put_user(mfhi1(), &sc->sc_hi1);
127 err |= __put_user(mflo1(), &sc->sc_lo1);
128 err |= __put_user(mfhi2(), &sc->sc_hi2);
129 err |= __put_user(mflo2(), &sc->sc_lo2);
130 err |= __put_user(mfhi3(), &sc->sc_hi3);
131 err |= __put_user(mflo3(), &sc->sc_lo3);
132 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
133 }
134
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900135 used_math = !!used_math();
136 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100137
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900138 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100139 /*
140 * Save FPU state to signal context. Signal handler
141 * will "inherit" current FPU state.
142 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900143 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100144 }
145 return err;
146}
147
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900148int fpcsr_pending(unsigned int __user *fpcsr)
149{
150 int err, sig = 0;
151 unsigned int csr, enabled;
152
153 err = __get_user(csr, fpcsr);
154 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
155 /*
156 * If the signal handler set some FPU exceptions, clear it and
157 * send SIGFPE.
158 */
159 if (csr & enabled) {
160 csr &= ~enabled;
161 err |= __put_user(csr, fpcsr);
162 sig = SIGFPE;
163 }
164 return err ?: sig;
165}
166
167static int
168check_and_restore_fp_context(struct sigcontext __user *sc)
169{
170 int err, sig;
171
172 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
173 if (err > 0)
174 err = 0;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900175 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900176 return err ?: sig;
177}
178
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100179int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
180{
181 unsigned int used_math;
182 unsigned long treg;
183 int err = 0;
184 int i;
185
186 /* Always make any pending restarted system calls return -EINTR */
187 current_thread_info()->restart_block.fn = do_no_restart_syscall;
188
189 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100190
191#ifdef CONFIG_CPU_HAS_SMARTMIPS
192 err |= __get_user(regs->acx, &sc->sc_acx);
193#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100194 err |= __get_user(regs->hi, &sc->sc_mdhi);
195 err |= __get_user(regs->lo, &sc->sc_mdlo);
196 if (cpu_has_dsp) {
197 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
198 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
199 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
200 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
201 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
202 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
203 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
204 }
205
206 for (i = 1; i < 32; i++)
207 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
208
209 err |= __get_user(used_math, &sc->sc_used_math);
210 conditional_used_math(used_math);
211
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900212 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100213 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900214 if (!err)
215 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100216 } else {
217 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900218 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100219 }
220
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100221 return err;
222}
223
224void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
225 size_t frame_size)
226{
227 unsigned long sp;
228
229 /* Default to using normal stack */
230 sp = regs->regs[29];
231
232 /*
233 * FPU emulator may have it's own trampoline active just
234 * above the user stack, 16-bytes before the next lowest
235 * 16 byte boundary. Try to avoid trashing it.
236 */
237 sp -= 32;
238
239 /* This is the X/Open sanctioned signal stack switching. */
240 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
241 sp = current->sas_ss_sp + current->sas_ss_size;
242
243 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
244}
245
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100246/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * Atomically swap in the new signal mask, and wait for a signal.
248 */
249
250#ifdef CONFIG_TRAD_SIGNALS
Al Viro1910f4a2012-12-25 16:25:18 -0500251SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Al Viro1910f4a2012-12-25 16:25:18 -0500253 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255#endif
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000258SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
259 struct sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct k_sigaction new_ka, old_ka;
262 int ret;
263 int err = 0;
264
265 if (act) {
266 old_sigset_t mask;
267
268 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
269 return -EFAULT;
270 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
271 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
272 err |= __get_user(mask, &act->sa_mask.sig[0]);
273 if (err)
274 return -EFAULT;
275
276 siginitset(&new_ka.sa.sa_mask, mask);
277 }
278
279 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
280
281 if (!ret && oact) {
282 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000283 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
285 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
286 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
287 err |= __put_user(0, &oact->sa_mask.sig[1]);
288 err |= __put_user(0, &oact->sa_mask.sig[2]);
289 err |= __put_user(0, &oact->sa_mask.sig[3]);
290 if (err)
291 return -EFAULT;
292 }
293
294 return ret;
295}
296#endif
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100299asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900301 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900303 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900305 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
307 goto badframe;
308 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
309 goto badframe;
310
Matt Fleming8598f3c2012-02-14 11:40:52 +0000311 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900313 sig = restore_sigcontext(&regs, &frame->sf_sc);
314 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900316 else if (sig)
317 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /*
320 * Don't let your children do this ...
321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 __asm__ __volatile__(
323 "move\t$29, %0\n\t"
324 "j\tsyscall_exit"
325 :/* no outputs */
326 :"r" (&regs));
327 /* Unreached */
328
329badframe:
330 force_sig(SIGSEGV, current);
331}
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000332#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100334asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900336 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 sigset_t set;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900338 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900340 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
342 goto badframe;
343 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
344 goto badframe;
345
Matt Fleming8598f3c2012-02-14 11:40:52 +0000346 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900348 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
349 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900351 else if (sig)
352 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Al Viroea536ad2012-12-23 03:13:40 -0500354 if (restore_altstack(&frame->rs_uc.uc_stack))
355 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /*
358 * Don't let your children do this ...
359 */
360 __asm__ __volatile__(
361 "move\t$29, %0\n\t"
362 "j\tsyscall_exit"
363 :/* no outputs */
364 :"r" (&regs));
365 /* Unreached */
366
367badframe:
368 force_sig(SIGSEGV, current);
369}
370
371#ifdef CONFIG_TRAD_SIGNALS
David Daneyd814c282010-02-18 16:13:05 -0800372static int setup_frame(void *sig_return, struct k_sigaction *ka,
373 struct pt_regs *regs, int signr, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900375 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int err = 0;
377
378 frame = get_sigframe(ka, regs, sizeof(*frame));
379 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
380 goto give_sigsegv;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 err |= setup_sigcontext(regs, &frame->sf_sc);
383 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
384 if (err)
385 goto give_sigsegv;
386
387 /*
388 * Arguments to signal handler:
389 *
390 * a0 = signal number
391 * a1 = 0 (should be cause)
392 * a2 = pointer to struct sigcontext
393 *
394 * $25 and c0_epc point to the signal handler, $29 points to the
395 * struct sigframe.
396 */
397 regs->regs[ 4] = signr;
398 regs->regs[ 5] = 0;
399 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
400 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800401 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
403
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100404 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100406 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409give_sigsegv:
410 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000411 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413#endif
414
David Daneyd814c282010-02-18 16:13:05 -0800415static int setup_rt_frame(void *sig_return, struct k_sigaction *ka,
Ralf Baechle70342282013-01-22 12:59:30 +0100416 struct pt_regs *regs, int signr, sigset_t *set,
David Daneyd814c282010-02-18 16:13:05 -0800417 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900419 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int err = 0;
421
422 frame = get_sigframe(ka, regs, sizeof(*frame));
423 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
424 goto give_sigsegv;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Create siginfo. */
427 err |= copy_siginfo_to_user(&frame->rs_info, info);
428
Ralf Baechle70342282013-01-22 12:59:30 +0100429 /* Create the ucontext. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900431 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Al Viroea536ad2012-12-23 03:13:40 -0500432 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
434 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
435
436 if (err)
437 goto give_sigsegv;
438
439 /*
440 * Arguments to signal handler:
441 *
442 * a0 = signal number
443 * a1 = 0 (should be cause)
444 * a2 = pointer to ucontext
445 *
446 * $25 and c0_epc point to the signal handler, $29 points to
447 * the struct rt_sigframe.
448 */
449 regs->regs[ 4] = signr;
450 regs->regs[ 5] = (unsigned long) &frame->rs_info;
451 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
452 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800453 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
455
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100456 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 current->comm, current->pid,
458 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100459
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462give_sigsegv:
463 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000464 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000467struct mips_abi mips_abi = {
468#ifdef CONFIG_TRAD_SIGNALS
469 .setup_frame = setup_frame,
David Daneyd814c282010-02-18 16:13:05 -0800470 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000471#endif
Ralf Baechle70342282013-01-22 12:59:30 +0100472 .setup_rt_frame = setup_rt_frame,
David Daneyd814c282010-02-18 16:13:05 -0800473 .rt_signal_return_offset =
474 offsetof(struct mips_vdso, rt_signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000475 .restart = __NR_restart_syscall
476};
477
Al Viroa610d6e2012-05-21 23:42:15 -0400478static void handle_signal(unsigned long sig, siginfo_t *info,
Al Virob7f9a112012-05-02 09:59:21 -0400479 struct k_sigaction *ka, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Al Virob7f9a112012-05-02 09:59:21 -0400481 sigset_t *oldset = sigmask_to_save();
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000482 int ret;
David Daneyd814c282010-02-18 16:13:05 -0800483 struct mips_abi *abi = current->thread.abi;
Douglas Leung01be0572013-03-25 13:21:11 -0500484#ifdef CONFIG_CPU_MICROMIPS
485 void *vdso;
486 unsigned int tmp = (unsigned int)current->mm->context.vdso;
487
488 set_isa16_mode(tmp);
489 vdso = (void *)tmp;
490#else
David Daneyd814c282010-02-18 16:13:05 -0800491 void *vdso = current->mm->context.vdso;
Douglas Leung01be0572013-03-25 13:21:11 -0500492#endif
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000493
Al Viro8f5a00eb2010-09-28 18:50:37 +0100494 if (regs->regs[0]) {
495 switch(regs->regs[2]) {
496 case ERESTART_RESTARTBLOCK:
497 case ERESTARTNOHAND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 regs->regs[2] = EINTR;
499 break;
Al Viro8f5a00eb2010-09-28 18:50:37 +0100500 case ERESTARTSYS:
501 if (!(ka->sa.sa_flags & SA_RESTART)) {
502 regs->regs[2] = EINTR;
503 break;
504 }
505 /* fallthrough */
506 case ERESTARTNOINTR:
507 regs->regs[7] = regs->regs[26];
508 regs->regs[2] = regs->regs[0];
509 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Ralf Baechle70342282013-01-22 12:59:30 +0100512 regs->regs[0] = 0; /* Don't deal with this again. */
Al Viro8f5a00eb2010-09-28 18:50:37 +0100513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000515 if (sig_uses_siginfo(ka))
David Daneyd814c282010-02-18 16:13:05 -0800516 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
517 ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 else
David Daneyd814c282010-02-18 16:13:05 -0800519 ret = abi->setup_frame(vdso + abi->signal_return_offset,
520 ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Al Viro062ab572010-09-28 18:50:17 +0100522 if (ret)
Al Viroa610d6e2012-05-21 23:42:15 -0400523 return;
Al Viro062ab572010-09-28 18:50:17 +0100524
Al Viroefee9842012-04-28 02:04:15 -0400525 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000528static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct k_sigaction ka;
531 siginfo_t info;
532 int signr;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000535 if (signr > 0) {
Ralf Baechle70342282013-01-22 12:59:30 +0100536 /* Whee! Actually deliver the signal. */
Al Viroa610d6e2012-05-21 23:42:15 -0400537 handle_signal(signr, &info, &ka, regs);
Ralf Baechle45887e12006-08-03 21:54:13 +0100538 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (regs->regs[0]) {
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100542 switch (regs->regs[2]) {
543 case ERESTARTNOHAND:
544 case ERESTARTSYS:
545 case ERESTARTNOINTR:
Al Viro8f5a00eb2010-09-28 18:50:37 +0100546 regs->regs[2] = regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 regs->regs[7] = regs->regs[26];
Al Viro8f5a00eb2010-09-28 18:50:37 +0100548 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100549 break;
550
551 case ERESTART_RESTARTBLOCK:
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000552 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 regs->regs[7] = regs->regs[26];
554 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Ralf Baechle70342282013-01-22 12:59:30 +0100557 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000559
560 /*
561 * If there's no signal to deliver, we just put the saved sigmask
562 * back
563 */
Al Viro51a7b442012-05-21 23:33:55 -0400564 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567/*
568 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000569 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000571asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 __u32 thread_info_flags)
573{
Ralf Baechle1f717922011-07-27 11:44:47 +0100574 local_irq_enable();
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* deal with pending signal delivery */
Al Viro6fd84c02012-05-23 15:28:58 -0400577 if (thread_info_flags & _TIF_SIGPENDING)
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000578 do_signal(regs);
David Howellsd0420c82009-09-02 09:14:16 +0100579
580 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
581 clear_thread_flag(TIF_NOTIFY_RESUME);
582 tracehook_notify_resume(regs);
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
Ralf Baechle137f6f32009-11-24 19:35:41 +0000585
586#ifdef CONFIG_SMP
587static int smp_save_fp_context(struct sigcontext __user *sc)
588{
589 return raw_cpu_has_fpu
590 ? _save_fp_context(sc)
591 : fpu_emulator_save_context(sc);
592}
593
594static int smp_restore_fp_context(struct sigcontext __user *sc)
595{
596 return raw_cpu_has_fpu
597 ? _restore_fp_context(sc)
598 : fpu_emulator_restore_context(sc);
599}
600#endif
601
602static int signal_setup(void)
603{
604#ifdef CONFIG_SMP
605 /* For now just do the cpu_has_fpu check when the functions are invoked */
606 save_fp_context = smp_save_fp_context;
607 restore_fp_context = smp_restore_fp_context;
608#else
609 if (cpu_has_fpu) {
610 save_fp_context = _save_fp_context;
611 restore_fp_context = _restore_fp_context;
612 } else {
613 save_fp_context = fpu_emulator_save_context;
614 restore_fp_context = fpu_emulator_restore_context;
615 }
616#endif
617
618 return 0;
619}
620
621arch_initcall(signal_setup);