blob: 07d67309451a62cc4bc4ac89c9ec520e30f45639 [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>
15#include <linux/smp_lock.h>
16#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>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ralf Baechlee50c0a82005-05-31 11:49:19 +000025#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/asm.h>
27#include <linux/bitops.h>
28#include <asm/cacheflush.h>
29#include <asm/fpu.h>
30#include <asm/sim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/ucontext.h>
32#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000033#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "signal-common.h"
36
Ralf Baechle66680582007-02-13 01:31:48 +000037/*
38 * Horribly complicated - with the bloody RM9000 workarounds enabled
39 * the signal trampolines is moving to the end of the structure so we can
40 * increase the alignment without breaking software compatibility.
41 */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010042#if ICACHE_REFILLS_WORKAROUND_WAR == 0
43
Ralf Baechle66680582007-02-13 01:31:48 +000044struct sigframe {
45 u32 sf_ass[4]; /* argument save space for o32 */
46 u32 sf_code[2]; /* signal trampoline */
47 struct sigcontext sf_sc;
48 sigset_t sf_mask;
49};
50
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010051struct rt_sigframe {
52 u32 rs_ass[4]; /* argument save space for o32 */
53 u32 rs_code[2]; /* signal trampoline */
54 struct siginfo rs_info;
55 struct ucontext rs_uc;
56};
57
58#else
59
Ralf Baechle66680582007-02-13 01:31:48 +000060struct sigframe {
61 u32 sf_ass[4]; /* argument save space for o32 */
62 u32 sf_pad[2];
63 struct sigcontext sf_sc; /* hw context */
64 sigset_t sf_mask;
65 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
66};
67
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010068struct rt_sigframe {
69 u32 rs_ass[4]; /* argument save space for o32 */
70 u32 rs_pad[2];
71 struct siginfo rs_info;
72 struct ucontext rs_uc;
73 u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */
74};
75
76#endif
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +010079 * Helper routines
80 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +090081static int protected_save_fp_context(struct sigcontext __user *sc)
82{
83 int err;
84 while (1) {
85 lock_fpu_owner();
86 own_fpu_inatomic(1);
87 err = save_fp_context(sc); /* this might fail */
88 unlock_fpu_owner();
89 if (likely(!err))
90 break;
91 /* touch the sigcontext and try again */
92 err = __put_user(0, &sc->sc_fpregs[0]) |
93 __put_user(0, &sc->sc_fpregs[31]) |
94 __put_user(0, &sc->sc_fpc_csr);
95 if (err)
96 break; /* really bad sigcontext */
97 }
98 return err;
99}
100
101static int protected_restore_fp_context(struct sigcontext __user *sc)
102{
103 int err, tmp;
104 while (1) {
105 lock_fpu_owner();
106 own_fpu_inatomic(0);
107 err = restore_fp_context(sc); /* this might fail */
108 unlock_fpu_owner();
109 if (likely(!err))
110 break;
111 /* touch the sigcontext and try again */
112 err = __get_user(tmp, &sc->sc_fpregs[0]) |
113 __get_user(tmp, &sc->sc_fpregs[31]) |
114 __get_user(tmp, &sc->sc_fpc_csr);
115 if (err)
116 break; /* really bad sigcontext */
117 }
118 return err;
119}
120
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100121int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
122{
123 int err = 0;
124 int i;
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900125 unsigned int used_math;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100126
127 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
128
129 err |= __put_user(0, &sc->sc_regs[0]);
130 for (i = 1; i < 32; i++)
131 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
132
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100133#ifdef CONFIG_CPU_HAS_SMARTMIPS
134 err |= __put_user(regs->acx, &sc->sc_acx);
135#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100136 err |= __put_user(regs->hi, &sc->sc_mdhi);
137 err |= __put_user(regs->lo, &sc->sc_mdlo);
138 if (cpu_has_dsp) {
139 err |= __put_user(mfhi1(), &sc->sc_hi1);
140 err |= __put_user(mflo1(), &sc->sc_lo1);
141 err |= __put_user(mfhi2(), &sc->sc_hi2);
142 err |= __put_user(mflo2(), &sc->sc_lo2);
143 err |= __put_user(mfhi3(), &sc->sc_hi3);
144 err |= __put_user(mflo3(), &sc->sc_lo3);
145 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
146 }
147
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900148 used_math = !!used_math();
149 err |= __put_user(used_math, &sc->sc_used_math);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100150
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900151 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100152 /*
153 * Save FPU state to signal context. Signal handler
154 * will "inherit" current FPU state.
155 */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900156 err |= protected_save_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100157 }
158 return err;
159}
160
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900161int fpcsr_pending(unsigned int __user *fpcsr)
162{
163 int err, sig = 0;
164 unsigned int csr, enabled;
165
166 err = __get_user(csr, fpcsr);
167 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
168 /*
169 * If the signal handler set some FPU exceptions, clear it and
170 * send SIGFPE.
171 */
172 if (csr & enabled) {
173 csr &= ~enabled;
174 err |= __put_user(csr, fpcsr);
175 sig = SIGFPE;
176 }
177 return err ?: sig;
178}
179
180static int
181check_and_restore_fp_context(struct sigcontext __user *sc)
182{
183 int err, sig;
184
185 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
186 if (err > 0)
187 err = 0;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900188 err |= protected_restore_fp_context(sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900189 return err ?: sig;
190}
191
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100192int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
193{
194 unsigned int used_math;
195 unsigned long treg;
196 int err = 0;
197 int i;
198
199 /* Always make any pending restarted system calls return -EINTR */
200 current_thread_info()->restart_block.fn = do_no_restart_syscall;
201
202 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100203
204#ifdef CONFIG_CPU_HAS_SMARTMIPS
205 err |= __get_user(regs->acx, &sc->sc_acx);
206#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100207 err |= __get_user(regs->hi, &sc->sc_mdhi);
208 err |= __get_user(regs->lo, &sc->sc_mdlo);
209 if (cpu_has_dsp) {
210 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
211 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
212 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
213 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
214 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
215 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
216 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
217 }
218
219 for (i = 1; i < 32; i++)
220 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
221
222 err |= __get_user(used_math, &sc->sc_used_math);
223 conditional_used_math(used_math);
224
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900225 if (used_math) {
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100226 /* restore fpu context if we have used it before */
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900227 if (!err)
228 err = check_and_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100229 } else {
230 /* signal handler may have used FPU. Give it up. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900231 lose_fpu(0);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100232 }
233
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100234 return err;
235}
236
237void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
238 size_t frame_size)
239{
240 unsigned long sp;
241
242 /* Default to using normal stack */
243 sp = regs->regs[29];
244
245 /*
246 * FPU emulator may have it's own trampoline active just
247 * above the user stack, 16-bytes before the next lowest
248 * 16 byte boundary. Try to avoid trashing it.
249 */
250 sp -= 32;
251
252 /* This is the X/Open sanctioned signal stack switching. */
253 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
254 sp = current->sas_ss_sp + current->sas_ss_size;
255
256 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
257}
258
259int install_sigtramp(unsigned int __user *tramp, unsigned int syscall)
260{
261 int err;
262
263 /*
264 * Set up the return code ...
265 *
266 * li v0, __NR__foo_sigreturn
267 * syscall
268 */
269
270 err = __put_user(0x24020000 + syscall, tramp + 0);
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100271 err |= __put_user(0x0000000c , tramp + 1);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100272 if (ICACHE_REFILLS_WORKAROUND_WAR) {
273 err |= __put_user(0, tramp + 2);
274 err |= __put_user(0, tramp + 3);
275 err |= __put_user(0, tramp + 4);
276 err |= __put_user(0, tramp + 5);
277 err |= __put_user(0, tramp + 6);
278 err |= __put_user(0, tramp + 7);
279 }
280 flush_cache_sigtramp((unsigned long) tramp);
281
282 return err;
283}
284
285/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * Atomically swap in the new signal mask, and wait for a signal.
287 */
288
289#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100290asmlinkage int sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000292 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000293 sigset_t __user *uset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Ralf Baechlefe00f942005-03-01 19:22:29 +0000295 uset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (copy_from_user(&newset, uset, sizeof(sigset_t)))
297 return -EFAULT;
298 sigdelsetmask(&newset, ~_BLOCKABLE);
299
300 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000301 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 current->blocked = newset;
303 recalc_sigpending();
304 spin_unlock_irq(&current->sighand->siglock);
305
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000306 current->state = TASK_INTERRUPTIBLE;
307 schedule();
308 set_thread_flag(TIF_RESTORE_SIGMASK);
309 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311#endif
312
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100313asmlinkage int sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000315 sigset_t newset;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000316 sigset_t __user *unewset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 size_t sigsetsize;
318
319 /* XXX Don't preclude handling different sized sigset_t's. */
320 sigsetsize = regs.regs[5];
321 if (sigsetsize != sizeof(sigset_t))
322 return -EINVAL;
323
Ralf Baechlefe00f942005-03-01 19:22:29 +0000324 unewset = (sigset_t __user *) regs.regs[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (copy_from_user(&newset, unewset, sizeof(newset)))
326 return -EFAULT;
327 sigdelsetmask(&newset, ~_BLOCKABLE);
328
329 spin_lock_irq(&current->sighand->siglock);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000330 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 current->blocked = newset;
Ralf Baechlee0daad42007-02-05 00:10:11 +0000332 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 spin_unlock_irq(&current->sighand->siglock);
334
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000335 current->state = TASK_INTERRUPTIBLE;
336 schedule();
337 set_thread_flag(TIF_RESTORE_SIGMASK);
338 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341#ifdef CONFIG_TRAD_SIGNALS
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900342asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act,
343 struct sigaction __user *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct k_sigaction new_ka, old_ka;
346 int ret;
347 int err = 0;
348
349 if (act) {
350 old_sigset_t mask;
351
352 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
353 return -EFAULT;
354 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
355 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
356 err |= __get_user(mask, &act->sa_mask.sig[0]);
357 if (err)
358 return -EFAULT;
359
360 siginitset(&new_ka.sa.sa_mask, mask);
361 }
362
363 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
364
365 if (!ret && oact) {
366 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000367 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
369 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
370 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
371 err |= __put_user(0, &oact->sa_mask.sig[1]);
372 err |= __put_user(0, &oact->sa_mask.sig[2]);
373 err |= __put_user(0, &oact->sa_mask.sig[3]);
374 if (err)
375 return -EFAULT;
376 }
377
378 return ret;
379}
380#endif
381
382asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
383{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000384 const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
385 stack_t __user *uoss = (stack_t __user *) regs.regs[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long usp = regs.regs[29];
387
388 return do_sigaltstack(uss, uoss, usp);
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#ifdef CONFIG_TRAD_SIGNALS
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100392asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900394 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900396 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900398 frame = (struct sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
400 goto badframe;
401 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
402 goto badframe;
403
404 sigdelsetmask(&blocked, ~_BLOCKABLE);
405 spin_lock_irq(&current->sighand->siglock);
406 current->blocked = blocked;
407 recalc_sigpending();
408 spin_unlock_irq(&current->sighand->siglock);
409
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900410 sig = restore_sigcontext(&regs, &frame->sf_sc);
411 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900413 else if (sig)
414 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /*
417 * Don't let your children do this ...
418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 __asm__ __volatile__(
420 "move\t$29, %0\n\t"
421 "j\tsyscall_exit"
422 :/* no outputs */
423 :"r" (&regs));
424 /* Unreached */
425
426badframe:
427 force_sig(SIGSEGV, current);
428}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000429#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Franck Bui-Huuf90080a2007-02-05 15:24:27 +0100431asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900433 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 sigset_t set;
435 stack_t st;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900436 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900438 frame = (struct rt_sigframe __user *) regs.regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
440 goto badframe;
441 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
442 goto badframe;
443
444 sigdelsetmask(&set, ~_BLOCKABLE);
445 spin_lock_irq(&current->sighand->siglock);
446 current->blocked = set;
447 recalc_sigpending();
448 spin_unlock_irq(&current->sighand->siglock);
449
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900450 sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
451 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900453 else if (sig)
454 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
457 goto badframe;
458 /* It is more difficult to avoid calling this function than to
459 call it and ignore errors. */
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900460 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
463 * Don't let your children do this ...
464 */
465 __asm__ __volatile__(
466 "move\t$29, %0\n\t"
467 "j\tsyscall_exit"
468 :/* no outputs */
469 :"r" (&regs));
470 /* Unreached */
471
472badframe:
473 force_sig(SIGSEGV, current);
474}
475
476#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000477static int setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int signr, sigset_t *set)
479{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900480 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int err = 0;
482
483 frame = get_sigframe(ka, regs, sizeof(*frame));
484 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
485 goto give_sigsegv;
486
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100487 err |= install_sigtramp(frame->sf_code, __NR_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 err |= setup_sigcontext(regs, &frame->sf_sc);
490 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
491 if (err)
492 goto give_sigsegv;
493
494 /*
495 * Arguments to signal handler:
496 *
497 * a0 = signal number
498 * a1 = 0 (should be cause)
499 * a2 = pointer to struct sigcontext
500 *
501 * $25 and c0_epc point to the signal handler, $29 points to the
502 * struct sigframe.
503 */
504 regs->regs[ 4] = signr;
505 regs->regs[ 5] = 0;
506 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
507 regs->regs[29] = (unsigned long) frame;
508 regs->regs[31] = (unsigned long) frame->sf_code;
509 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
510
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100511 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100513 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516give_sigsegv:
517 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000518 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520#endif
521
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000522static int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 int signr, sigset_t *set, siginfo_t *info)
524{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900525 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int err = 0;
527
528 frame = get_sigframe(ka, regs, sizeof(*frame));
529 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
530 goto give_sigsegv;
531
Franck Bui-Huu601dde42007-02-05 15:24:23 +0100532 err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Create siginfo. */
535 err |= copy_siginfo_to_user(&frame->rs_info, info);
536
537 /* Create the ucontext. */
538 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900539 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Atsushi Nemoto9c6031c2006-02-19 23:46:44 +0900540 err |= __put_user((void __user *)current->sas_ss_sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 &frame->rs_uc.uc_stack.ss_sp);
542 err |= __put_user(sas_ss_flags(regs->regs[29]),
543 &frame->rs_uc.uc_stack.ss_flags);
544 err |= __put_user(current->sas_ss_size,
545 &frame->rs_uc.uc_stack.ss_size);
546 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
547 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
548
549 if (err)
550 goto give_sigsegv;
551
552 /*
553 * Arguments to signal handler:
554 *
555 * a0 = signal number
556 * a1 = 0 (should be cause)
557 * a2 = pointer to ucontext
558 *
559 * $25 and c0_epc point to the signal handler, $29 points to
560 * the struct rt_sigframe.
561 */
562 regs->regs[ 4] = signr;
563 regs->regs[ 5] = (unsigned long) &frame->rs_info;
564 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
565 regs->regs[29] = (unsigned long) frame;
566 regs->regs[31] = (unsigned long) frame->rs_code;
567 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
568
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100569 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 current->comm, current->pid,
571 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100572
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575give_sigsegv:
576 force_sigsegv(signr, current);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000577 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000580struct mips_abi mips_abi = {
581#ifdef CONFIG_TRAD_SIGNALS
582 .setup_frame = setup_frame,
583#endif
584 .setup_rt_frame = setup_rt_frame,
585 .restart = __NR_restart_syscall
586};
587
Franck Bui-Huue692eb32007-02-05 15:24:28 +0100588static int handle_signal(unsigned long sig, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
590{
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000591 int ret;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 switch(regs->regs[0]) {
594 case ERESTART_RESTARTBLOCK:
595 case ERESTARTNOHAND:
596 regs->regs[2] = EINTR;
597 break;
598 case ERESTARTSYS:
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000599 if (!(ka->sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 regs->regs[2] = EINTR;
601 break;
602 }
603 /* fallthrough */
604 case ERESTARTNOINTR: /* Userland will reload $v0. */
605 regs->regs[7] = regs->regs[26];
606 regs->cp0_epc -= 8;
607 }
608
609 regs->regs[0] = 0; /* Don't deal with this again. */
610
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000611 if (sig_uses_siginfo(ka))
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000612 ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000614 ret = current->thread.abi->setup_frame(ka, regs, sig, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Steven Rostedt69be8f12005-08-29 11:44:09 -0400616 spin_lock_irq(&current->sighand->siglock);
617 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
618 if (!(ka->sa.sa_flags & SA_NODEFER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 sigaddset(&current->blocked,sig);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400620 recalc_sigpending();
621 spin_unlock_irq(&current->sighand->siglock);
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000622
623 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000626static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct k_sigaction ka;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000629 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 siginfo_t info;
631 int signr;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * We want the common case to go fast, which is why we may in certain
635 * cases get here from kernel mode. Just return without doing anything
636 * if so.
637 */
638 if (!user_mode(regs))
Ralf Baechle40ac5d42006-02-08 13:38:18 +0000639 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000641 if (test_thread_flag(TIF_RESTORE_SIGMASK))
642 oldset = &current->saved_sigmask;
643 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 oldset = &current->blocked;
645
646 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000647 if (signr > 0) {
648 /* Whee! Actually deliver the signal. */
649 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
650 /*
651 * A signal was successfully delivered; the saved
652 * sigmask will have been stored in the signal frame,
653 * and will be restored by sigreturn, so we can simply
654 * clear the TIF_RESTORE_SIGMASK flag.
655 */
656 if (test_thread_flag(TIF_RESTORE_SIGMASK))
657 clear_thread_flag(TIF_RESTORE_SIGMASK);
658 }
Ralf Baechle45887e12006-08-03 21:54:13 +0100659
660 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * Who's code doesn't conform to the restartable syscall convention
665 * dies here!!! The li instruction, a single machine instruction,
666 * must directly be followed by the syscall instruction.
667 */
668 if (regs->regs[0]) {
669 if (regs->regs[2] == ERESTARTNOHAND ||
670 regs->regs[2] == ERESTARTSYS ||
671 regs->regs[2] == ERESTARTNOINTR) {
672 regs->regs[7] = regs->regs[26];
673 regs->cp0_epc -= 8;
674 }
675 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000676 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 regs->regs[7] = regs->regs[26];
678 regs->cp0_epc -= 4;
679 }
Ralf Baechle13fdd312006-08-08 03:47:01 +0100680 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000682
683 /*
684 * If there's no signal to deliver, we just put the saved sigmask
685 * back
686 */
687 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
688 clear_thread_flag(TIF_RESTORE_SIGMASK);
689 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693/*
694 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000695 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000697asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 __u32 thread_info_flags)
699{
700 /* deal with pending signal delivery */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000701 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000702 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}