blob: 545bf11bd2edb42fc05b37e2db9bb993be3abc4e [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.
Leonid Yegoshinca750642013-12-12 16:57:19 +00009 * Copyright (C) 2014, Imagination Technologies Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Ralf Baechle02416dc2005-06-15 13:00:12 +000011#include <linux/cache.h>
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +020012#include <linux/context_tracking.h>
Ralf Baechle1f717922011-07-27 11:44:47 +010013#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/personality.h>
17#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/signal.h>
20#include <linux/errno.h>
21#include <linux/wait.h>
22#include <linux/ptrace.h>
23#include <linux/unistd.h>
24#include <linux/compiler.h>
Ralf Baechledbda6ac2009-02-08 16:00:26 +000025#include <linux/syscalls.h>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090026#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010027#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Ralf Baechlee50c0a82005-05-31 11:49:19 +000029#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/asm.h>
31#include <linux/bitops.h>
32#include <asm/cacheflush.h>
33#include <asm/fpu.h>
34#include <asm/sim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/ucontext.h>
36#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000037#include <asm/war.h>
David Daneyd814c282010-02-18 16:13:05 -080038#include <asm/vdso.h>
David Howellsb81947c2012-03-28 18:30:02 +010039#include <asm/dsp.h>
Douglas Leung01be0572013-03-25 13:21:11 -050040#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "signal-common.h"
43
Ralf Baechle137f6f32009-11-24 19:35:41 +000044static int (*save_fp_context)(struct sigcontext __user *sc);
45static int (*restore_fp_context)(struct sigcontext __user *sc);
46
47extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
48extern asmlinkage int _restore_fp_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/*
Paul Burtonb2ead522014-01-27 15:23:02 +000065 * Thread saved context copy to/from a signal context presumed to be on the
66 * user stack, and therefore accessed with appropriate macros from uaccess.h.
67 */
68static int copy_fp_to_sigcontext(struct sigcontext __user *sc)
69{
70 int i;
71 int err = 0;
72
Paul Burton6bbfd652014-01-27 15:23:04 +000073 for (i = 0; i < NUM_FPU_REGS; i++) {
Paul Burtonb2ead522014-01-27 15:23:02 +000074 err |=
75 __put_user(get_fpr64(&current->thread.fpu.fpr[i], 0),
76 &sc->sc_fpregs[i]);
77 }
78 err |= __put_user(current->thread.fpu.fcr31, &sc->sc_fpc_csr);
79
80 return err;
81}
82
83static int copy_fp_from_sigcontext(struct sigcontext __user *sc)
84{
85 int i;
86 int err = 0;
87 u64 fpr_val;
88
Paul Burton6bbfd652014-01-27 15:23:04 +000089 for (i = 0; i < NUM_FPU_REGS; i++) {
Paul Burtonb2ead522014-01-27 15:23:02 +000090 err |= __get_user(fpr_val, &sc->sc_fpregs[i]);
91 set_fpr64(&current->thread.fpu.fpr[i], 0, fpr_val);
92 }
93 err |= __get_user(current->thread.fpu.fcr31, &sc->sc_fpc_csr);
94
95 return err;
96}
97
98/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010099 * Helper routines
100 */
Paul Burton16f77de2014-06-18 15:00:46 +0100101static int protected_save_fp_context(struct sigcontext __user *sc)
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900102{
103 int err;
Leonid Yegoshinca750642013-12-12 16:57:19 +0000104#ifndef CONFIG_EVA
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900105 while (1) {
106 lock_fpu_owner();
Paul Burtonff3aa5f2014-01-27 15:23:03 +0000107 if (is_fpu_owner()) {
108 err = save_fp_context(sc);
109 unlock_fpu_owner();
110 } else {
111 unlock_fpu_owner();
112 err = copy_fp_to_sigcontext(sc);
113 }
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900114 if (likely(!err))
115 break;
116 /* touch the sigcontext and try again */
117 err = __put_user(0, &sc->sc_fpregs[0]) |
118 __put_user(0, &sc->sc_fpregs[31]) |
119 __put_user(0, &sc->sc_fpc_csr);
120 if (err)
121 break; /* really bad sigcontext */
122 }
Leonid Yegoshinca750642013-12-12 16:57:19 +0000123#else
124 /*
125 * EVA does not have FPU EVA instructions so saving fpu context directly
126 * does not work.
127 */
Leonid Yegoshinca750642013-12-12 16:57:19 +0000128 lose_fpu(1);
129 err = save_fp_context(sc); /* this might fail */
Leonid Yegoshinca750642013-12-12 16:57:19 +0000130#endif
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900131 return err;
132}
133
Paul Burton16f77de2014-06-18 15:00:46 +0100134static int protected_restore_fp_context(struct sigcontext __user *sc)
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900135{
David Daneyc726b822011-01-24 14:51:34 -0800136 int err, tmp __maybe_unused;
Leonid Yegoshinca750642013-12-12 16:57:19 +0000137#ifndef CONFIG_EVA
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900138 while (1) {
139 lock_fpu_owner();
Paul Burtonff3aa5f2014-01-27 15:23:03 +0000140 if (is_fpu_owner()) {
141 err = restore_fp_context(sc);
142 unlock_fpu_owner();
143 } else {
144 unlock_fpu_owner();
145 err = copy_fp_from_sigcontext(sc);
146 }
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900147 if (likely(!err))
148 break;
149 /* touch the sigcontext and try again */
150 err = __get_user(tmp, &sc->sc_fpregs[0]) |
151 __get_user(tmp, &sc->sc_fpregs[31]) |
152 __get_user(tmp, &sc->sc_fpc_csr);
153 if (err)
154 break; /* really bad sigcontext */
155 }
Leonid Yegoshinca750642013-12-12 16:57:19 +0000156#else
157 /*
158 * EVA does not have FPU EVA instructions so restoring fpu context
159 * directly does not work.
160 */
Leonid Yegoshinca750642013-12-12 16:57:19 +0000161 lose_fpu(0);
162 err = restore_fp_context(sc); /* this might fail */
Leonid Yegoshinca750642013-12-12 16:57:19 +0000163#endif
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900164 return err;
165}
166
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100167int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
168{
169 int err = 0;
170 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900171 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100172
173 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
174
175 err |= __put_user(0, &sc->sc_regs[0]);
176 for (i = 1; i < 32; i++)
177 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
178
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100179#ifdef CONFIG_CPU_HAS_SMARTMIPS
180 err |= __put_user(regs->acx, &sc->sc_acx);
181#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100182 err |= __put_user(regs->hi, &sc->sc_mdhi);
183 err |= __put_user(regs->lo, &sc->sc_mdlo);
184 if (cpu_has_dsp) {
185 err |= __put_user(mfhi1(), &sc->sc_hi1);
186 err |= __put_user(mflo1(), &sc->sc_lo1);
187 err |= __put_user(mfhi2(), &sc->sc_hi2);
188 err |= __put_user(mflo2(), &sc->sc_lo2);
189 err |= __put_user(mfhi3(), &sc->sc_hi3);
190 err |= __put_user(mflo3(), &sc->sc_lo3);
191 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
192 }
193
Paul Burton16f77de2014-06-18 15:00:46 +0100194 used_math = !!used_math();
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900195 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100196
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900197 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100198 /*
199 * Save FPU state to signal context. Signal handler
200 * will "inherit" current FPU state.
201 */
Paul Burton16f77de2014-06-18 15:00:46 +0100202 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100203 }
204 return err;
205}
206
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900207int fpcsr_pending(unsigned int __user *fpcsr)
208{
209 int err, sig = 0;
210 unsigned int csr, enabled;
211
212 err = __get_user(csr, fpcsr);
213 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
214 /*
215 * If the signal handler set some FPU exceptions, clear it and
216 * send SIGFPE.
217 */
218 if (csr & enabled) {
219 csr &= ~enabled;
220 err |= __put_user(csr, fpcsr);
221 sig = SIGFPE;
222 }
223 return err ?: sig;
224}
225
226static int
Paul Burton16f77de2014-06-18 15:00:46 +0100227check_and_restore_fp_context(struct sigcontext __user *sc)
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900228{
229 int err, sig;
230
231 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
232 if (err > 0)
233 err = 0;
Paul Burton16f77de2014-06-18 15:00:46 +0100234 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900235 return err ?: sig;
236}
237
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100238int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
239{
240 unsigned int used_math;
241 unsigned long treg;
242 int err = 0;
243 int i;
244
245 /* Always make any pending restarted system calls return -EINTR */
246 current_thread_info()->restart_block.fn = do_no_restart_syscall;
247
248 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100249
250#ifdef CONFIG_CPU_HAS_SMARTMIPS
251 err |= __get_user(regs->acx, &sc->sc_acx);
252#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100253 err |= __get_user(regs->hi, &sc->sc_mdhi);
254 err |= __get_user(regs->lo, &sc->sc_mdlo);
255 if (cpu_has_dsp) {
256 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
257 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
258 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
259 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
260 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
261 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
262 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
263 }
264
265 for (i = 1; i < 32; i++)
266 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
267
268 err |= __get_user(used_math, &sc->sc_used_math);
269 conditional_used_math(used_math);
270
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900271 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100272 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900273 if (!err)
Paul Burton16f77de2014-06-18 15:00:46 +0100274 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100275 } else {
Paul Burton16f77de2014-06-18 15:00:46 +0100276 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900277 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100278 }
279
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100280 return err;
281}
282
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100283void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100284 size_t frame_size)
285{
286 unsigned long sp;
287
288 /* Default to using normal stack */
289 sp = regs->regs[29];
290
291 /*
292 * FPU emulator may have it's own trampoline active just
293 * above the user stack, 16-bytes before the next lowest
294 * 16 byte boundary. Try to avoid trashing it.
295 */
296 sp -= 32;
297
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100298 sp = sigsp(sp, ksig);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100299
300 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
301}
302
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100303/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * Atomically swap in the new signal mask, and wait for a signal.
305 */
306
307#ifdef CONFIG_TRAD_SIGNALS
Al Viro1910f4a2012-12-25 16:25:18 -0500308SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Al Viro1910f4a2012-12-25 16:25:18 -0500310 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312#endif
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000315SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
316 struct sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct k_sigaction new_ka, old_ka;
319 int ret;
320 int err = 0;
321
322 if (act) {
323 old_sigset_t mask;
324
325 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
326 return -EFAULT;
327 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
328 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
329 err |= __get_user(mask, &act->sa_mask.sig[0]);
330 if (err)
331 return -EFAULT;
332
333 siginitset(&new_ka.sa.sa_mask, mask);
334 }
335
336 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
337
338 if (!ret && oact) {
339 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000340 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
342 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
343 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
344 err |= __put_user(0, &oact->sa_mask.sig[1]);
345 err |= __put_user(0, &oact->sa_mask.sig[2]);
346 err |= __put_user(0, &oact->sa_mask.sig[3]);
347 if (err)
348 return -EFAULT;
349 }
350
351 return ret;
352}
353#endif
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100356asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900358 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900360 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900362 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
364 goto badframe;
365 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
366 goto badframe;
367
Matt Fleming8598f3c2012-02-14 11:40:52 +0000368 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900370 sig = restore_sigcontext(&regs, &frame->sf_sc);
371 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900373 else if (sig)
374 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /*
377 * Don't let your children do this ...
378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 __asm__ __volatile__(
380 "move\t$29, %0\n\t"
381 "j\tsyscall_exit"
382 :/* no outputs */
383 :"r" (&regs));
384 /* Unreached */
385
386badframe:
387 force_sig(SIGSEGV, current);
388}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000389#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100391asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900393 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sigset_t set;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900395 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900397 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
399 goto badframe;
400 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
401 goto badframe;
402
Matt Fleming8598f3c2012-02-14 11:40:52 +0000403 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900405 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
406 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900408 else if (sig)
409 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Al Viroea536ad2012-12-23 03:13:40 -0500411 if (restore_altstack(&frame->rs_uc.uc_stack))
412 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /*
415 * Don't let your children do this ...
416 */
417 __asm__ __volatile__(
418 "move\t$29, %0\n\t"
419 "j\tsyscall_exit"
420 :/* no outputs */
421 :"r" (&regs));
422 /* Unreached */
423
424badframe:
425 force_sig(SIGSEGV, current);
426}
427
428#ifdef CONFIG_TRAD_SIGNALS
Richard Weinberger81d103b2013-10-06 22:25:42 +0200429static int setup_frame(void *sig_return, struct ksignal *ksig,
430 struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900432 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int err = 0;
434
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100435 frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
Richard Weinberger81d103b2013-10-06 22:25:42 +0200437 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 err |= setup_sigcontext(regs, &frame->sf_sc);
440 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
441 if (err)
Richard Weinberger81d103b2013-10-06 22:25:42 +0200442 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /*
445 * Arguments to signal handler:
446 *
447 * a0 = signal number
448 * a1 = 0 (should be cause)
449 * a2 = pointer to struct sigcontext
450 *
451 * $25 and c0_epc point to the signal handler, $29 points to the
452 * struct sigframe.
453 */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200454 regs->regs[ 4] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 regs->regs[ 5] = 0;
456 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
457 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800458 regs->regs[31] = (unsigned long) sig_return;
Richard Weinberger81d103b2013-10-06 22:25:42 +0200459 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100461 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100463 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466#endif
467
Richard Weinberger81d103b2013-10-06 22:25:42 +0200468static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
469 struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900471 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int err = 0;
473
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100474 frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
Richard Weinberger81d103b2013-10-06 22:25:42 +0200476 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* Create siginfo. */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200479 err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Ralf Baechle70342282013-01-22 12:59:30 +0100481 /* Create the ucontext. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900483 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Al Viroea536ad2012-12-23 03:13:40 -0500484 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
486 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
487
488 if (err)
Richard Weinberger81d103b2013-10-06 22:25:42 +0200489 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /*
492 * Arguments to signal handler:
493 *
494 * a0 = signal number
495 * a1 = 0 (should be cause)
496 * a2 = pointer to ucontext
497 *
498 * $25 and c0_epc point to the signal handler, $29 points to
499 * the struct rt_sigframe.
500 */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200501 regs->regs[ 4] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 regs->regs[ 5] = (unsigned long) &frame->rs_info;
503 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
504 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800505 regs->regs[31] = (unsigned long) sig_return;
Richard Weinberger81d103b2013-10-06 22:25:42 +0200506 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100508 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 current->comm, current->pid,
510 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100511
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000515struct mips_abi mips_abi = {
516#ifdef CONFIG_TRAD_SIGNALS
517 .setup_frame = setup_frame,
David Daneyd814c282010-02-18 16:13:05 -0800518 .signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000519#endif
Ralf Baechle70342282013-01-22 12:59:30 +0100520 .setup_rt_frame = setup_rt_frame,
David Daneyd814c282010-02-18 16:13:05 -0800521 .rt_signal_return_offset =
522 offsetof(struct mips_vdso, rt_signal_trampoline),
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000523 .restart = __NR_restart_syscall
524};
525
Richard Weinberger81d103b2013-10-06 22:25:42 +0200526static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Al Virob7f9a112012-05-02 09:59:21 -0400528 sigset_t *oldset = sigmask_to_save();
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000529 int ret;
David Daneyd814c282010-02-18 16:13:05 -0800530 struct mips_abi *abi = current->thread.abi;
Douglas Leung01be0572013-03-25 13:21:11 -0500531#ifdef CONFIG_CPU_MICROMIPS
532 void *vdso;
Maciej W. Rozycki2fabc7d2014-11-15 22:08:09 +0000533 unsigned long tmp = (unsigned long)current->mm->context.vdso;
Douglas Leung01be0572013-03-25 13:21:11 -0500534
535 set_isa16_mode(tmp);
536 vdso = (void *)tmp;
537#else
David Daneyd814c282010-02-18 16:13:05 -0800538 void *vdso = current->mm->context.vdso;
Douglas Leung01be0572013-03-25 13:21:11 -0500539#endif
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000540
Al Viro8f5a00eb2010-09-28 18:50:37 +0100541 if (regs->regs[0]) {
542 switch(regs->regs[2]) {
543 case ERESTART_RESTARTBLOCK:
544 case ERESTARTNOHAND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 regs->regs[2] = EINTR;
546 break;
Al Viro8f5a00eb2010-09-28 18:50:37 +0100547 case ERESTARTSYS:
Richard Weinberger81d103b2013-10-06 22:25:42 +0200548 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
Al Viro8f5a00eb2010-09-28 18:50:37 +0100549 regs->regs[2] = EINTR;
550 break;
551 }
552 /* fallthrough */
553 case ERESTARTNOINTR:
554 regs->regs[7] = regs->regs[26];
555 regs->regs[2] = regs->regs[0];
556 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Ralf Baechle70342282013-01-22 12:59:30 +0100559 regs->regs[0] = 0; /* Don't deal with this again. */
Al Viro8f5a00eb2010-09-28 18:50:37 +0100560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Richard Weinberger81d103b2013-10-06 22:25:42 +0200562 if (sig_uses_siginfo(&ksig->ka))
David Daneyd814c282010-02-18 16:13:05 -0800563 ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
Richard Weinberger81d103b2013-10-06 22:25:42 +0200564 ksig, regs, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 else
Richard Weinberger81d103b2013-10-06 22:25:42 +0200566 ret = abi->setup_frame(vdso + abi->signal_return_offset, ksig,
567 regs, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Richard Weinberger81d103b2013-10-06 22:25:42 +0200569 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000572static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Richard Weinberger81d103b2013-10-06 22:25:42 +0200574 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Richard Weinberger81d103b2013-10-06 22:25:42 +0200576 if (get_signal(&ksig)) {
Ralf Baechle70342282013-01-22 12:59:30 +0100577 /* Whee! Actually deliver the signal. */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200578 handle_signal(&ksig, regs);
Ralf Baechle45887e12006-08-03 21:54:13 +0100579 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (regs->regs[0]) {
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100583 switch (regs->regs[2]) {
584 case ERESTARTNOHAND:
585 case ERESTARTSYS:
586 case ERESTARTNOINTR:
Al Viro8f5a00eb2010-09-28 18:50:37 +0100587 regs->regs[2] = regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 regs->regs[7] = regs->regs[26];
Al Viro8f5a00eb2010-09-28 18:50:37 +0100589 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100590 break;
591
592 case ERESTART_RESTARTBLOCK:
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000593 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 regs->regs[7] = regs->regs[26];
595 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Ralf Baechle70342282013-01-22 12:59:30 +0100598 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000600
601 /*
602 * If there's no signal to deliver, we just put the saved sigmask
603 * back
604 */
Al Viro51a7b442012-05-21 23:33:55 -0400605 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/*
609 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000610 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000612asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 __u32 thread_info_flags)
614{
Ralf Baechle1f717922011-07-27 11:44:47 +0100615 local_irq_enable();
616
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200617 user_exit();
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* deal with pending signal delivery */
Al Viro6fd84c02012-05-23 15:28:58 -0400620 if (thread_info_flags & _TIF_SIGPENDING)
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000621 do_signal(regs);
David Howellsd0420c82009-09-02 09:14:16 +0100622
623 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
624 clear_thread_flag(TIF_NOTIFY_RESUME);
625 tracehook_notify_resume(regs);
626 }
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200627
628 user_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Ralf Baechle137f6f32009-11-24 19:35:41 +0000630
631#ifdef CONFIG_SMP
Leonid Yegoshinca750642013-12-12 16:57:19 +0000632#ifndef CONFIG_EVA
Ralf Baechle137f6f32009-11-24 19:35:41 +0000633static int smp_save_fp_context(struct sigcontext __user *sc)
634{
635 return raw_cpu_has_fpu
636 ? _save_fp_context(sc)
Paul Burtonb2ead522014-01-27 15:23:02 +0000637 : copy_fp_to_sigcontext(sc);
Ralf Baechle137f6f32009-11-24 19:35:41 +0000638}
639
640static int smp_restore_fp_context(struct sigcontext __user *sc)
641{
642 return raw_cpu_has_fpu
643 ? _restore_fp_context(sc)
Paul Burtonb2ead522014-01-27 15:23:02 +0000644 : copy_fp_from_sigcontext(sc);
Ralf Baechle137f6f32009-11-24 19:35:41 +0000645}
Leonid Yegoshinca750642013-12-12 16:57:19 +0000646#endif /* CONFIG_EVA */
Ralf Baechle137f6f32009-11-24 19:35:41 +0000647#endif
648
649static int signal_setup(void)
650{
Leonid Yegoshinca750642013-12-12 16:57:19 +0000651#ifndef CONFIG_EVA
Ralf Baechle137f6f32009-11-24 19:35:41 +0000652#ifdef CONFIG_SMP
653 /* For now just do the cpu_has_fpu check when the functions are invoked */
654 save_fp_context = smp_save_fp_context;
655 restore_fp_context = smp_restore_fp_context;
656#else
657 if (cpu_has_fpu) {
658 save_fp_context = _save_fp_context;
659 restore_fp_context = _restore_fp_context;
660 } else {
Paul Burton14fa12d2014-10-28 11:25:51 +0000661 save_fp_context = copy_fp_to_sigcontext;
662 restore_fp_context = copy_fp_from_sigcontext;
Ralf Baechle137f6f32009-11-24 19:35:41 +0000663 }
Leonid Yegoshinca750642013-12-12 16:57:19 +0000664#endif /* CONFIG_SMP */
665#else
Paul Burton14fa12d2014-10-28 11:25:51 +0000666 save_fp_context = copy_fp_to_sigcontext;
667 restore_fp_context = copy_fp_from_sigcontext;
Ralf Baechle137f6f32009-11-24 19:35:41 +0000668#endif
669
670 return 0;
671}
672
673arch_initcall(signal_setup);