blob: e0178e117f684917cf5a2b96b1d82ffbff24ee89 [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 Baechlec3fc5cd2013-05-29 01:07:19 +020011#include <linux/context_tracking.h>
Ralf Baechle1f717922011-07-27 11:44:47 +010012#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
14#include <linux/mm.h>
15#include <linux/personality.h>
16#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/signal.h>
19#include <linux/errno.h>
20#include <linux/wait.h>
21#include <linux/ptrace.h>
22#include <linux/unistd.h>
23#include <linux/compiler.h>
Ralf Baechledbda6ac2009-02-08 16:00:26 +000024#include <linux/syscalls.h>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090025#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010026#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Ralf Baechlee50c0a82005-05-31 11:49:19 +000028#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/asm.h>
30#include <linux/bitops.h>
31#include <asm/cacheflush.h>
32#include <asm/fpu.h>
33#include <asm/sim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/ucontext.h>
35#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000036#include <asm/war.h>
David Daneyd814c282010-02-18 16:13:05 -080037#include <asm/vdso.h>
David Howellsb81947c2012-03-28 18:30:02 +010038#include <asm/dsp.h>
Douglas Leung01be0572013-03-25 13:21:11 -050039#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "signal-common.h"
42
Ralf Baechle137f6f32009-11-24 19:35:41 +000043static int (*save_fp_context)(struct sigcontext __user *sc);
44static int (*restore_fp_context)(struct sigcontext __user *sc);
45
46extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
47extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
48
Ralf Baechle66680582007-02-13 01:31:48 +000049struct sigframe {
50 u32 sf_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080051 u32 sf_pad[2]; /* Was: signal trampoline */
Ralf Baechle66680582007-02-13 01:31:48 +000052 struct sigcontext sf_sc;
53 sigset_t sf_mask;
54};
55
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010056struct rt_sigframe {
57 u32 rs_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080058 u32 rs_pad[2]; /* Was: signal trampoline */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010059 struct siginfo rs_info;
60 struct ucontext rs_uc;
61};
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
Paul Burtonb2ead522014-01-27 15:23:02 +000064 * Thread saved context copy to/from a signal context presumed to be on the
65 * user stack, and therefore accessed with appropriate macros from uaccess.h.
66 */
67static int copy_fp_to_sigcontext(struct sigcontext __user *sc)
68{
69 int i;
70 int err = 0;
71
72 for (i = 0; i < 32; i++) {
73 err |=
74 __put_user(get_fpr64(&current->thread.fpu.fpr[i], 0),
75 &sc->sc_fpregs[i]);
76 }
77 err |= __put_user(current->thread.fpu.fcr31, &sc->sc_fpc_csr);
78
79 return err;
80}
81
82static int copy_fp_from_sigcontext(struct sigcontext __user *sc)
83{
84 int i;
85 int err = 0;
86 u64 fpr_val;
87
88 for (i = 0; i < 32; i++) {
89 err |= __get_user(fpr_val, &sc->sc_fpregs[i]);
90 set_fpr64(&current->thread.fpu.fpr[i], 0, fpr_val);
91 }
92 err |= __get_user(current->thread.fpu.fcr31, &sc->sc_fpc_csr);
93
94 return err;
95}
96
97/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010098 * Helper routines
99 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900100static int protected_save_fp_context(struct sigcontext __user *sc)
101{
102 int err;
103 while (1) {
104 lock_fpu_owner();
Paul Burtonff3aa5f2014-01-27 15:23:03 +0000105 if (is_fpu_owner()) {
106 err = save_fp_context(sc);
107 unlock_fpu_owner();
108 } else {
109 unlock_fpu_owner();
110 err = copy_fp_to_sigcontext(sc);
111 }
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900112 if (likely(!err))
113 break;
114 /* touch the sigcontext and try again */
115 err = __put_user(0, &sc->sc_fpregs[0]) |
116 __put_user(0, &sc->sc_fpregs[31]) |
117 __put_user(0, &sc->sc_fpc_csr);
118 if (err)
119 break; /* really bad sigcontext */
120 }
121 return err;
122}
123
124static int protected_restore_fp_context(struct sigcontext __user *sc)
125{
David Daneyc726b822011-01-24 14:51:34 -0800126 int err, tmp __maybe_unused;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900127 while (1) {
128 lock_fpu_owner();
Paul Burtonff3aa5f2014-01-27 15:23:03 +0000129 if (is_fpu_owner()) {
130 err = restore_fp_context(sc);
131 unlock_fpu_owner();
132 } else {
133 unlock_fpu_owner();
134 err = copy_fp_from_sigcontext(sc);
135 }
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900136 if (likely(!err))
137 break;
138 /* touch the sigcontext and try again */
139 err = __get_user(tmp, &sc->sc_fpregs[0]) |
140 __get_user(tmp, &sc->sc_fpregs[31]) |
141 __get_user(tmp, &sc->sc_fpc_csr);
142 if (err)
143 break; /* really bad sigcontext */
144 }
145 return err;
146}
147
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100148int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
149{
150 int err = 0;
151 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900152 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100153
154 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
155
156 err |= __put_user(0, &sc->sc_regs[0]);
157 for (i = 1; i < 32; i++)
158 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
159
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100160#ifdef CONFIG_CPU_HAS_SMARTMIPS
161 err |= __put_user(regs->acx, &sc->sc_acx);
162#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100163 err |= __put_user(regs->hi, &sc->sc_mdhi);
164 err |= __put_user(regs->lo, &sc->sc_mdlo);
165 if (cpu_has_dsp) {
166 err |= __put_user(mfhi1(), &sc->sc_hi1);
167 err |= __put_user(mflo1(), &sc->sc_lo1);
168 err |= __put_user(mfhi2(), &sc->sc_hi2);
169 err |= __put_user(mflo2(), &sc->sc_lo2);
170 err |= __put_user(mfhi3(), &sc->sc_hi3);
171 err |= __put_user(mflo3(), &sc->sc_lo3);
172 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
173 }
174
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900175 used_math = !!used_math();
176 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100177
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900178 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100179 /*
180 * Save FPU state to signal context. Signal handler
181 * will "inherit" current FPU state.
182 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900183 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100184 }
185 return err;
186}
187
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900188int fpcsr_pending(unsigned int __user *fpcsr)
189{
190 int err, sig = 0;
191 unsigned int csr, enabled;
192
193 err = __get_user(csr, fpcsr);
194 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
195 /*
196 * If the signal handler set some FPU exceptions, clear it and
197 * send SIGFPE.
198 */
199 if (csr & enabled) {
200 csr &= ~enabled;
201 err |= __put_user(csr, fpcsr);
202 sig = SIGFPE;
203 }
204 return err ?: sig;
205}
206
207static int
208check_and_restore_fp_context(struct sigcontext __user *sc)
209{
210 int err, sig;
211
212 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
213 if (err > 0)
214 err = 0;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900215 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900216 return err ?: sig;
217}
218
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100219int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
220{
221 unsigned int used_math;
222 unsigned long treg;
223 int err = 0;
224 int i;
225
226 /* Always make any pending restarted system calls return -EINTR */
227 current_thread_info()->restart_block.fn = do_no_restart_syscall;
228
229 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100230
231#ifdef CONFIG_CPU_HAS_SMARTMIPS
232 err |= __get_user(regs->acx, &sc->sc_acx);
233#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100234 err |= __get_user(regs->hi, &sc->sc_mdhi);
235 err |= __get_user(regs->lo, &sc->sc_mdlo);
236 if (cpu_has_dsp) {
237 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
238 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
239 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
240 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
241 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
242 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
243 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
244 }
245
246 for (i = 1; i < 32; i++)
247 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
248
249 err |= __get_user(used_math, &sc->sc_used_math);
250 conditional_used_math(used_math);
251
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900252 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100253 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900254 if (!err)
255 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100256 } else {
257 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900258 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100259 }
260
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100261 return err;
262}
263
264void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
265 size_t frame_size)
266{
267 unsigned long sp;
268
269 /* Default to using normal stack */
270 sp = regs->regs[29];
271
272 /*
273 * FPU emulator may have it's own trampoline active just
274 * above the user stack, 16-bytes before the next lowest
275 * 16 byte boundary. Try to avoid trashing it.
276 */
277 sp -= 32;
278
279 /* This is the X/Open sanctioned signal stack switching. */
280 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
281 sp = current->sas_ss_sp + current->sas_ss_size;
282
283 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
284}
285
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * Atomically swap in the new signal mask, and wait for a signal.
288 */
289
290#ifdef CONFIG_TRAD_SIGNALS
Al Viro1910f4a2012-12-25 16:25:18 -0500291SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Al Viro1910f4a2012-12-25 16:25:18 -0500293 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295#endif
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000298SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
299 struct sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct k_sigaction new_ka, old_ka;
302 int ret;
303 int err = 0;
304
305 if (act) {
306 old_sigset_t mask;
307
308 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
309 return -EFAULT;
310 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
311 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
312 err |= __get_user(mask, &act->sa_mask.sig[0]);
313 if (err)
314 return -EFAULT;
315
316 siginitset(&new_ka.sa.sa_mask, mask);
317 }
318
319 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
320
321 if (!ret && oact) {
322 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000323 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
325 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
326 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
327 err |= __put_user(0, &oact->sa_mask.sig[1]);
328 err |= __put_user(0, &oact->sa_mask.sig[2]);
329 err |= __put_user(0, &oact->sa_mask.sig[3]);
330 if (err)
331 return -EFAULT;
332 }
333
334 return ret;
335}
336#endif
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100339asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900341 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900343 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900345 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
347 goto badframe;
348 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
349 goto badframe;
350
Matt Fleming8598f3c2012-02-14 11:40:52 +0000351 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900353 sig = restore_sigcontext(&regs, &frame->sf_sc);
354 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900356 else if (sig)
357 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /*
360 * Don't let your children do this ...
361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 __asm__ __volatile__(
363 "move\t$29, %0\n\t"
364 "j\tsyscall_exit"
365 :/* no outputs */
366 :"r" (&regs));
367 /* Unreached */
368
369badframe:
370 force_sig(SIGSEGV, current);
371}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000372#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100374asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900376 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 sigset_t set;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900378 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900380 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
382 goto badframe;
383 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
384 goto badframe;
385
Matt Fleming8598f3c2012-02-14 11:40:52 +0000386 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900388 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
389 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900391 else if (sig)
392 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Al Viroea536ad2012-12-23 03:13:40 -0500394 if (restore_altstack(&frame->rs_uc.uc_stack))
395 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * Don't let your children do this ...
399 */
400 __asm__ __volatile__(
401 "move\t$29, %0\n\t"
402 "j\tsyscall_exit"
403 :/* no outputs */
404 :"r" (&regs));
405 /* Unreached */
406
407badframe:
408 force_sig(SIGSEGV, current);
409}
410
411#ifdef CONFIG_TRAD_SIGNALS
David Daneyd814c282010-02-18 16:13:05 -0800412static int setup_frame(void *sig_return, struct k_sigaction *ka,
413 struct pt_regs *regs, int signr, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900415 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int err = 0;
417
418 frame = get_sigframe(ka, regs, sizeof(*frame));
419 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
420 goto give_sigsegv;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 err |= setup_sigcontext(regs, &frame->sf_sc);
423 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
424 if (err)
425 goto give_sigsegv;
426
427 /*
428 * Arguments to signal handler:
429 *
430 * a0 = signal number
431 * a1 = 0 (should be cause)
432 * a2 = pointer to struct sigcontext
433 *
434 * $25 and c0_epc point to the signal handler, $29 points to the
435 * struct sigframe.
436 */
437 regs->regs[ 4] = signr;
438 regs->regs[ 5] = 0;
439 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
440 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800441 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
443
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100444 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100446 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449give_sigsegv:
450 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000451 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453#endif
454
David Daneyd814c282010-02-18 16:13:05 -0800455static int setup_rt_frame(void *sig_return, struct k_sigaction *ka,
Ralf Baechle70342282013-01-22 12:59:30 +0100456 struct pt_regs *regs, int signr, sigset_t *set,
David Daneyd814c282010-02-18 16:13:05 -0800457 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900459 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int err = 0;
461
462 frame = get_sigframe(ka, regs, sizeof(*frame));
463 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
464 goto give_sigsegv;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Create siginfo. */
467 err |= copy_siginfo_to_user(&frame->rs_info, info);
468
Ralf Baechle70342282013-01-22 12:59:30 +0100469 /* Create the ucontext. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900471 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Al Viroea536ad2012-12-23 03:13:40 -0500472 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
474 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
475
476 if (err)
477 goto give_sigsegv;
478
479 /*
480 * Arguments to signal handler:
481 *
482 * a0 = signal number
483 * a1 = 0 (should be cause)
484 * a2 = pointer to ucontext
485 *
486 * $25 and c0_epc point to the signal handler, $29 points to
487 * the struct rt_sigframe.
488 */
489 regs->regs[ 4] = signr;
490 regs->regs[ 5] = (unsigned long) &frame->rs_info;
491 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
492 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800493 regs->regs[31] = (unsigned long) sig_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
495
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100496 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 current->comm, current->pid,
498 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100499
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502give_sigsegv:
503 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000504 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000507struct mips_abi mips_abi = {
508#ifdef CONFIG_TRAD_SIGNALS
509 .setup_frame = setup_frame,
David Daneyd814c282010-02-18 16:13:05 -0800510 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000511#endif
Ralf Baechle70342282013-01-22 12:59:30 +0100512 .setup_rt_frame = setup_rt_frame,
David Daneyd814c282010-02-18 16:13:05 -0800513 .rt_signal_return_offset =
514 offsetof(struct mips_vdso, rt_signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000515 .restart = __NR_restart_syscall
516};
517
Al Viroa610d6e2012-05-21 23:42:15 -0400518static void handle_signal(unsigned long sig, siginfo_t *info,
Al Virob7f9a112012-05-02 09:59:21 -0400519 struct k_sigaction *ka, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Al Virob7f9a112012-05-02 09:59:21 -0400521 sigset_t *oldset = sigmask_to_save();
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000522 int ret;
David Daneyd814c282010-02-18 16:13:05 -0800523 struct mips_abi *abi = current->thread.abi;
Douglas Leung01be0572013-03-25 13:21:11 -0500524#ifdef CONFIG_CPU_MICROMIPS
525 void *vdso;
526 unsigned int tmp = (unsigned int)current->mm->context.vdso;
527
528 set_isa16_mode(tmp);
529 vdso = (void *)tmp;
530#else
David Daneyd814c282010-02-18 16:13:05 -0800531 void *vdso = current->mm->context.vdso;
Douglas Leung01be0572013-03-25 13:21:11 -0500532#endif
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000533
Al Viro8f5a00eb2010-09-28 18:50:37 +0100534 if (regs->regs[0]) {
535 switch(regs->regs[2]) {
536 case ERESTART_RESTARTBLOCK:
537 case ERESTARTNOHAND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 regs->regs[2] = EINTR;
539 break;
Al Viro8f5a00eb2010-09-28 18:50:37 +0100540 case ERESTARTSYS:
541 if (!(ka->sa.sa_flags & SA_RESTART)) {
542 regs->regs[2] = EINTR;
543 break;
544 }
545 /* fallthrough */
546 case ERESTARTNOINTR:
547 regs->regs[7] = regs->regs[26];
548 regs->regs[2] = regs->regs[0];
549 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Ralf Baechle70342282013-01-22 12:59:30 +0100552 regs->regs[0] = 0; /* Don't deal with this again. */
Al Viro8f5a00eb2010-09-28 18:50:37 +0100553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000555 if (sig_uses_siginfo(ka))
David Daneyd814c282010-02-18 16:13:05 -0800556 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
557 ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 else
David Daneyd814c282010-02-18 16:13:05 -0800559 ret = abi->setup_frame(vdso + abi->signal_return_offset,
560 ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Al Viro062ab572010-09-28 18:50:17 +0100562 if (ret)
Al Viroa610d6e2012-05-21 23:42:15 -0400563 return;
Al Viro062ab572010-09-28 18:50:17 +0100564
Al Viroefee9842012-04-28 02:04:15 -0400565 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000568static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct k_sigaction ka;
571 siginfo_t info;
572 int signr;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000575 if (signr > 0) {
Ralf Baechle70342282013-01-22 12:59:30 +0100576 /* Whee! Actually deliver the signal. */
Al Viroa610d6e2012-05-21 23:42:15 -0400577 handle_signal(signr, &info, &ka, regs);
Ralf Baechle45887e12006-08-03 21:54:13 +0100578 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (regs->regs[0]) {
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100582 switch (regs->regs[2]) {
583 case ERESTARTNOHAND:
584 case ERESTARTSYS:
585 case ERESTARTNOINTR:
Al Viro8f5a00eb2010-09-28 18:50:37 +0100586 regs->regs[2] = regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 regs->regs[7] = regs->regs[26];
Al Viro8f5a00eb2010-09-28 18:50:37 +0100588 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100589 break;
590
591 case ERESTART_RESTARTBLOCK:
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000592 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 regs->regs[7] = regs->regs[26];
594 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100595 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Ralf Baechle70342282013-01-22 12:59:30 +0100597 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000599
600 /*
601 * If there's no signal to deliver, we just put the saved sigmask
602 * back
603 */
Al Viro51a7b442012-05-21 23:33:55 -0400604 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/*
608 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000609 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000611asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 __u32 thread_info_flags)
613{
Ralf Baechle1f717922011-07-27 11:44:47 +0100614 local_irq_enable();
615
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200616 user_exit();
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* deal with pending signal delivery */
Al Viro6fd84c02012-05-23 15:28:58 -0400619 if (thread_info_flags & _TIF_SIGPENDING)
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000620 do_signal(regs);
David Howellsd0420c82009-09-02 09:14:16 +0100621
622 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
623 clear_thread_flag(TIF_NOTIFY_RESUME);
624 tracehook_notify_resume(regs);
625 }
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200626
627 user_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
Ralf Baechle137f6f32009-11-24 19:35:41 +0000629
630#ifdef CONFIG_SMP
631static int smp_save_fp_context(struct sigcontext __user *sc)
632{
633 return raw_cpu_has_fpu
634 ? _save_fp_context(sc)
Paul Burtonb2ead522014-01-27 15:23:02 +0000635 : copy_fp_to_sigcontext(sc);
Ralf Baechle137f6f32009-11-24 19:35:41 +0000636}
637
638static int smp_restore_fp_context(struct sigcontext __user *sc)
639{
640 return raw_cpu_has_fpu
641 ? _restore_fp_context(sc)
Paul Burtonb2ead522014-01-27 15:23:02 +0000642 : copy_fp_from_sigcontext(sc);
Ralf Baechle137f6f32009-11-24 19:35:41 +0000643}
644#endif
645
646static int signal_setup(void)
647{
648#ifdef CONFIG_SMP
649 /* For now just do the cpu_has_fpu check when the functions are invoked */
650 save_fp_context = smp_save_fp_context;
651 restore_fp_context = smp_restore_fp_context;
652#else
653 if (cpu_has_fpu) {
654 save_fp_context = _save_fp_context;
655 restore_fp_context = _restore_fp_context;
656 } else {
Paul Burtonb2ead522014-01-27 15:23:02 +0000657 save_fp_context = copy_fp_from_sigcontext;
658 restore_fp_context = copy_fp_to_sigcontext;
Ralf Baechle137f6f32009-11-24 19:35:41 +0000659 }
660#endif
661
662 return 0;
663}
664
665arch_initcall(signal_setup);