blob: 6254041b942f9a9b56295024a0fd1320fb2da6ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
Ralf Baechle02416dc2005-06-15 13:00:12 +000010#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/personality.h>
14#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/signal.h>
17#include <linux/errno.h>
18#include <linux/wait.h>
19#include <linux/ptrace.h>
20#include <linux/unistd.h>
21#include <linux/compiler.h>
Ralf Baechledbda6ac2009-02-08 16:00:26 +000022#include <linux/syscalls.h>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090023#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010024#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ralf Baechlee50c0a82005-05-31 11:49:19 +000026#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/asm.h>
28#include <linux/bitops.h>
29#include <asm/cacheflush.h>
30#include <asm/fpu.h>
31#include <asm/sim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/ucontext.h>
33#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000034#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include "signal-common.h"
37
Ralf Baechle66680582007-02-13 01:31:48 +000038/*
39 * Horribly complicated - with the bloody RM9000 workarounds enabled
40 * the signal trampolines is moving to the end of the structure so we can
41 * increase the alignment without breaking software compatibility.
42 */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010043#if ICACHE_REFILLS_WORKAROUND_WAR == 0
44
Ralf Baechle66680582007-02-13 01:31:48 +000045struct sigframe {
46 u32 sf_ass[4]; /* argument save space for o32 */
47 u32 sf_code[2]; /* signal trampoline */
48 struct sigcontext sf_sc;
49 sigset_t sf_mask;
50};
51
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010052struct rt_sigframe {
53 u32 rs_ass[4]; /* argument save space for o32 */
54 u32 rs_code[2]; /* signal trampoline */
55 struct siginfo rs_info;
56 struct ucontext rs_uc;
57};
58
59#else
60
Ralf Baechle66680582007-02-13 01:31:48 +000061struct sigframe {
62 u32 sf_ass[4]; /* argument save space for o32 */
63 u32 sf_pad[2];
64 struct sigcontext sf_sc; /* hw context */
65 sigset_t sf_mask;
66 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
67};
68
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010069struct rt_sigframe {
70 u32 rs_ass[4]; /* argument save space for o32 */
71 u32 rs_pad[2];
72 struct siginfo rs_info;
73 struct ucontext rs_uc;
74 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
75};
76
77#endif
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010080 * Helper routines
81 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +090082static int protected_save_fp_context(struct sigcontext __user *sc)
83{
84 int err;
85 while (1) {
86 lock_fpu_owner();
87 own_fpu_inatomic(1);
88 err = save_fp_context(sc); /* this might fail */
89 unlock_fpu_owner();
90 if (likely(!err))
91 break;
92 /* touch the sigcontext and try again */
93 err = __put_user(0, &sc->sc_fpregs[0]) |
94 __put_user(0, &sc->sc_fpregs[31]) |
95 __put_user(0, &sc->sc_fpc_csr);
96 if (err)
97 break; /* really bad sigcontext */
98 }
99 return err;
100}
101
102static int protected_restore_fp_context(struct sigcontext __user *sc)
103{
104 int err, tmp;
105 while (1) {
106 lock_fpu_owner();
107 own_fpu_inatomic(0);
108 err = restore_fp_context(sc); /* this might fail */
109 unlock_fpu_owner();
110 if (likely(!err))
111 break;
112 /* touch the sigcontext and try again */
113 err = __get_user(tmp, &sc->sc_fpregs[0]) |
114 __get_user(tmp, &sc->sc_fpregs[31]) |
115 __get_user(tmp, &sc->sc_fpc_csr);
116 if (err)
117 break; /* really bad sigcontext */
118 }
119 return err;
120}
121
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100122int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
123{
124 int err = 0;
125 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900126 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100127
128 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
129
130 err |= __put_user(0, &sc->sc_regs[0]);
131 for (i = 1; i < 32; i++)
132 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
133
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100134#ifdef CONFIG_CPU_HAS_SMARTMIPS
135 err |= __put_user(regs->acx, &sc->sc_acx);
136#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100137 err |= __put_user(regs->hi, &sc->sc_mdhi);
138 err |= __put_user(regs->lo, &sc->sc_mdlo);
139 if (cpu_has_dsp) {
140 err |= __put_user(mfhi1(), &sc->sc_hi1);
141 err |= __put_user(mflo1(), &sc->sc_lo1);
142 err |= __put_user(mfhi2(), &sc->sc_hi2);
143 err |= __put_user(mflo2(), &sc->sc_lo2);
144 err |= __put_user(mfhi3(), &sc->sc_hi3);
145 err |= __put_user(mflo3(), &sc->sc_lo3);
146 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
147 }
148
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900149 used_math = !!used_math();
150 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100151
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900152 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100153 /*
154 * Save FPU state to signal context. Signal handler
155 * will "inherit" current FPU state.
156 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900157 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100158 }
159 return err;
160}
161
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900162int fpcsr_pending(unsigned int __user *fpcsr)
163{
164 int err, sig = 0;
165 unsigned int csr, enabled;
166
167 err = __get_user(csr, fpcsr);
168 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
169 /*
170 * If the signal handler set some FPU exceptions, clear it and
171 * send SIGFPE.
172 */
173 if (csr & enabled) {
174 csr &= ~enabled;
175 err |= __put_user(csr, fpcsr);
176 sig = SIGFPE;
177 }
178 return err ?: sig;
179}
180
181static int
182check_and_restore_fp_context(struct sigcontext __user *sc)
183{
184 int err, sig;
185
186 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
187 if (err > 0)
188 err = 0;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900189 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900190 return err ?: sig;
191}
192
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100193int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
194{
195 unsigned int used_math;
196 unsigned long treg;
197 int err = 0;
198 int i;
199
200 /* Always make any pending restarted system calls return -EINTR */
201 current_thread_info()->restart_block.fn = do_no_restart_syscall;
202
203 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100204
205#ifdef CONFIG_CPU_HAS_SMARTMIPS
206 err |= __get_user(regs->acx, &sc->sc_acx);
207#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100208 err |= __get_user(regs->hi, &sc->sc_mdhi);
209 err |= __get_user(regs->lo, &sc->sc_mdlo);
210 if (cpu_has_dsp) {
211 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
212 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
213 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
214 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
215 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
216 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
217 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
218 }
219
220 for (i = 1; i < 32; i++)
221 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
222
223 err |= __get_user(used_math, &sc->sc_used_math);
224 conditional_used_math(used_math);
225
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900226 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100227 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900228 if (!err)
229 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100230 } else {
231 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900232 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100233 }
234
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100235 return err;
236}
237
238void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
239 size_t frame_size)
240{
241 unsigned long sp;
242
243 /* Default to using normal stack */
244 sp = regs->regs[29];
245
246 /*
247 * FPU emulator may have it's own trampoline active just
248 * above the user stack, 16-bytes before the next lowest
249 * 16 byte boundary. Try to avoid trashing it.
250 */
251 sp -= 32;
252
253 /* This is the X/Open sanctioned signal stack switching. */
254 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
255 sp = current->sas_ss_sp + current->sas_ss_size;
256
257 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
258}
259
260int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
261{
262 int err;
263
264 /*
265 * Set up the return code ...
266 *
267 * li v0, __NR__foo_sigreturn
268 * syscall
269 */
270
271 err = __put_user(0x24020000 + syscall, tramp + 0);
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100272 err |= __put_user(0x0000000c , tramp + 1);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100273 if (ICACHE_REFILLS_WORKAROUND_WAR) {
274 err |= __put_user(0, tramp + 2);
275 err |= __put_user(0, tramp + 3);
276 err |= __put_user(0, tramp + 4);
277 err |= __put_user(0, tramp + 5);
278 err |= __put_user(0, tramp + 6);
279 err |= __put_user(0, tramp + 7);
280 }
281 flush_cache_sigtramp((unsigned long) tramp);
282
283 return err;
284}
285
286/*
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
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100291asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000293 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000294 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Ralf Baechlefe00f942005-03-01 19:22:29 +0000296 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
298 return -EFAULT;
299 sigdelsetmask(&newset, ~_BLOCKABLE);
300
301 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000302 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 current->blocked = newset;
304 recalc_sigpending();
305 spin_unlock_irq(&current->sighand->siglock);
306
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000307 current->state = TASK_INTERRUPTIBLE;
308 schedule();
309 set_thread_flag(TIF_RESTORE_SIGMASK);
310 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312#endif
313
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100314asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000316 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000317 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 size_t sigsetsize;
319
320 /* XXX Don't preclude handling different sized sigset_t's. */
321 sigsetsize = regs.regs[5];
322 if (sigsetsize != sizeof(sigset_t))
323 return -EINVAL;
324
Ralf Baechlefe00f942005-03-01 19:22:29 +0000325 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (copy_from_user(&newset, unewset, sizeof(newset)))
327 return -EFAULT;
328 sigdelsetmask(&newset, ~_BLOCKABLE);
329
330 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000331 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 current->blocked = newset;
Ralf Baechlee0daad42007-02-05 00:10:11 +0000333 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 spin_unlock_irq(&current->sighand->siglock);
335
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000336 current->state = TASK_INTERRUPTIBLE;
337 schedule();
338 set_thread_flag(TIF_RESTORE_SIGMASK);
339 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000343SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
344 struct sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct k_sigaction new_ka, old_ka;
347 int ret;
348 int err = 0;
349
350 if (act) {
351 old_sigset_t mask;
352
353 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
354 return -EFAULT;
355 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
356 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
357 err |= __get_user(mask, &act->sa_mask.sig[0]);
358 if (err)
359 return -EFAULT;
360
361 siginitset(&new_ka.sa.sa_mask, mask);
362 }
363
364 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
365
366 if (!ret && oact) {
367 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000368 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
370 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
371 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
372 err |= __put_user(0, &oact->sa_mask.sig[1]);
373 err |= __put_user(0, &oact->sa_mask.sig[2]);
374 err |= __put_user(0, &oact->sa_mask.sig[3]);
375 if (err)
376 return -EFAULT;
377 }
378
379 return ret;
380}
381#endif
382
383asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
384{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000385 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
386 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned long usp = regs.regs[29];
388
389 return do_sigaltstack(uss, uoss, usp);
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100393asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900395 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900397 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900399 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
401 goto badframe;
402 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
403 goto badframe;
404
405 sigdelsetmask(&blocked, ~_BLOCKABLE);
406 spin_lock_irq(&current->sighand->siglock);
407 current->blocked = blocked;
408 recalc_sigpending();
409 spin_unlock_irq(&current->sighand->siglock);
410
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900411 sig = restore_sigcontext(&regs, &frame->sf_sc);
412 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900414 else if (sig)
415 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /*
418 * Don't let your children do this ...
419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 __asm__ __volatile__(
421 "move\t$29, %0\n\t"
422 "j\tsyscall_exit"
423 :/* no outputs */
424 :"r" (&regs));
425 /* Unreached */
426
427badframe:
428 force_sig(SIGSEGV, current);
429}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000430#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100432asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900434 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sigset_t set;
436 stack_t st;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900437 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900439 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
441 goto badframe;
442 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
443 goto badframe;
444
445 sigdelsetmask(&set, ~_BLOCKABLE);
446 spin_lock_irq(&current->sighand->siglock);
447 current->blocked = set;
448 recalc_sigpending();
449 spin_unlock_irq(&current->sighand->siglock);
450
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900451 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
452 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900454 else if (sig)
455 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
458 goto badframe;
459 /* It is more difficult to avoid calling this function than to
460 call it and ignore errors. */
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900461 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /*
464 * Don't let your children do this ...
465 */
466 __asm__ __volatile__(
467 "move\t$29, %0\n\t"
468 "j\tsyscall_exit"
469 :/* no outputs */
470 :"r" (&regs));
471 /* Unreached */
472
473badframe:
474 force_sig(SIGSEGV, current);
475}
476
477#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000478static int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int signr, sigset_t *set)
480{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900481 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int err = 0;
483
484 frame = get_sigframe(ka, regs, sizeof(*frame));
485 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
486 goto give_sigsegv;
487
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100488 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 err |= setup_sigcontext(regs, &frame->sf_sc);
491 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
492 if (err)
493 goto give_sigsegv;
494
495 /*
496 * Arguments to signal handler:
497 *
498 * a0 = signal number
499 * a1 = 0 (should be cause)
500 * a2 = pointer to struct sigcontext
501 *
502 * $25 and c0_epc point to the signal handler, $29 points to the
503 * struct sigframe.
504 */
505 regs->regs[ 4] = signr;
506 regs->regs[ 5] = 0;
507 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
508 regs->regs[29] = (unsigned long) frame;
509 regs->regs[31] = (unsigned long) frame->sf_code;
510 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
511
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100512 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100514 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +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#endif
522
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000523static int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int signr, sigset_t *set, siginfo_t *info)
525{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900526 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int err = 0;
528
529 frame = get_sigframe(ka, regs, sizeof(*frame));
530 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
531 goto give_sigsegv;
532
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100533 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* Create siginfo. */
536 err |= copy_siginfo_to_user(&frame->rs_info, info);
537
538 /* Create the ucontext. */
539 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900540 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900541 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 &frame->rs_uc.uc_stack.ss_sp);
543 err |= __put_user(sas_ss_flags(regs->regs[29]),
544 &frame->rs_uc.uc_stack.ss_flags);
545 err |= __put_user(current->sas_ss_size,
546 &frame->rs_uc.uc_stack.ss_size);
547 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
548 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
549
550 if (err)
551 goto give_sigsegv;
552
553 /*
554 * Arguments to signal handler:
555 *
556 * a0 = signal number
557 * a1 = 0 (should be cause)
558 * a2 = pointer to ucontext
559 *
560 * $25 and c0_epc point to the signal handler, $29 points to
561 * the struct rt_sigframe.
562 */
563 regs->regs[ 4] = signr;
564 regs->regs[ 5] = (unsigned long) &frame->rs_info;
565 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
566 regs->regs[29] = (unsigned long) frame;
567 regs->regs[31] = (unsigned long) frame->rs_code;
568 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
569
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100570 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 current->comm, current->pid,
572 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100573
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576give_sigsegv:
577 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000578 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000581struct mips_abi mips_abi = {
582#ifdef CONFIG_TRAD_SIGNALS
583 .setup_frame = setup_frame,
584#endif
585 .setup_rt_frame = setup_rt_frame,
586 .restart = __NR_restart_syscall
587};
588
Franck Bui-Huue692eb32007-02-05 15:24:28 +0100589static int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
591{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000592 int ret;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 switch(regs->regs[0]) {
595 case ERESTART_RESTARTBLOCK:
596 case ERESTARTNOHAND:
597 regs->regs[2] = EINTR;
598 break;
599 case ERESTARTSYS:
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000600 if (!(ka->sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 regs->regs[2] = EINTR;
602 break;
603 }
604 /* fallthrough */
605 case ERESTARTNOINTR: /* Userland will reload $v0. */
606 regs->regs[7] = regs->regs[26];
607 regs->cp0_epc -= 8;
608 }
609
610 regs->regs[0] = 0; /* Don't deal with this again. */
611
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000612 if (sig_uses_siginfo(ka))
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000613 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 else
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000615 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Steven Rostedt69be8f12005-08-29 11:44:09 -0400617 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle21a151d2007-10-11 23:46:15 +0100618 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400619 if (!(ka->sa.sa_flags & SA_NODEFER))
Ralf Baechle21a151d2007-10-11 23:46:15 +0100620 sigaddset(&current->blocked, sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400621 recalc_sigpending();
622 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000623
624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000627static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000630 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 siginfo_t info;
632 int signr;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
635 * We want the common case to go fast, which is why we may in certain
636 * cases get here from kernel mode. Just return without doing anything
637 * if so.
638 */
639 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000642 if (test_thread_flag(TIF_RESTORE_SIGMASK))
643 oldset = &current->saved_sigmask;
644 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 oldset = &current->blocked;
646
647 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000648 if (signr > 0) {
649 /* Whee! Actually deliver the signal. */
650 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
651 /*
652 * A signal was successfully delivered; the saved
653 * sigmask will have been stored in the signal frame,
654 * and will be restored by sigreturn, so we can simply
655 * clear the TIF_RESTORE_SIGMASK flag.
656 */
657 if (test_thread_flag(TIF_RESTORE_SIGMASK))
658 clear_thread_flag(TIF_RESTORE_SIGMASK);
659 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100660
661 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * Who's code doesn't conform to the restartable syscall convention
666 * dies here!!! The li instruction, a single machine instruction,
667 * must directly be followed by the syscall instruction.
668 */
669 if (regs->regs[0]) {
670 if (regs->regs[2] == ERESTARTNOHAND ||
671 regs->regs[2] == ERESTARTSYS ||
672 regs->regs[2] == ERESTARTNOINTR) {
673 regs->regs[7] = regs->regs[26];
674 regs->cp0_epc -= 8;
675 }
676 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000677 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 regs->regs[7] = regs->regs[26];
679 regs->cp0_epc -= 4;
680 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100681 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000683
684 /*
685 * If there's no signal to deliver, we just put the saved sigmask
686 * back
687 */
688 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
689 clear_thread_flag(TIF_RESTORE_SIGMASK);
690 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694/*
695 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000696 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000698asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 __u32 thread_info_flags)
700{
701 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000702 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000703 do_signal(regs);
David Howellsd0420c82009-09-02 09:14:16 +0100704
705 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
706 clear_thread_flag(TIF_NOTIFY_RESUME);
707 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100708 if (current->replacement_session_keyring)
709 key_replace_session_keyring();
David Howellsd0420c82009-09-02 09:14:16 +0100710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}