Sparseify MIPS.
[pandora-kernel.git] / arch / mips / kernel / signal.c
1 /*
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  */
10 #include <linux/config.h>
11 #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>
23
24 #include <asm/asm.h>
25 #include <linux/bitops.h>
26 #include <asm/cacheflush.h>
27 #include <asm/fpu.h>
28 #include <asm/sim.h>
29 #include <asm/uaccess.h>
30 #include <asm/ucontext.h>
31 #include <asm/cpu-features.h>
32
33 #include "signal-common.h"
34
35 #define DEBUG_SIG 0
36
37 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38
39 static int do_signal(sigset_t *oldset, struct pt_regs *regs);
40
41 /*
42  * Atomically swap in the new signal mask, and wait for a signal.
43  */
44
45 #ifdef CONFIG_TRAD_SIGNALS
46 save_static_function(sys_sigsuspend);
47 __attribute_used__ noinline static int
48 _sys_sigsuspend(nabi_no_regargs struct pt_regs regs)
49 {
50         sigset_t saveset, newset;
51         sigset_t __user *uset;
52
53         uset = (sigset_t __user *) regs.regs[4];
54         if (copy_from_user(&newset, uset, sizeof(sigset_t)))
55                 return -EFAULT;
56         sigdelsetmask(&newset, ~_BLOCKABLE);
57
58         spin_lock_irq(&current->sighand->siglock);
59         saveset = current->blocked;
60         current->blocked = newset;
61         recalc_sigpending();
62         spin_unlock_irq(&current->sighand->siglock);
63
64         regs.regs[2] = EINTR;
65         regs.regs[7] = 1;
66         while (1) {
67                 current->state = TASK_INTERRUPTIBLE;
68                 schedule();
69                 if (do_signal(&saveset, &regs))
70                         return -EINTR;
71         }
72 }
73 #endif
74
75 save_static_function(sys_rt_sigsuspend);
76 __attribute_used__ noinline static int
77 _sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
78 {
79         sigset_t saveset, newset;
80         sigset_t __user *unewset;
81         size_t sigsetsize;
82
83         /* XXX Don't preclude handling different sized sigset_t's.  */
84         sigsetsize = regs.regs[5];
85         if (sigsetsize != sizeof(sigset_t))
86                 return -EINVAL;
87
88         unewset = (sigset_t __user *) regs.regs[4];
89         if (copy_from_user(&newset, unewset, sizeof(newset)))
90                 return -EFAULT;
91         sigdelsetmask(&newset, ~_BLOCKABLE);
92
93         spin_lock_irq(&current->sighand->siglock);
94         saveset = current->blocked;
95         current->blocked = newset;
96         recalc_sigpending();
97         spin_unlock_irq(&current->sighand->siglock);
98
99         regs.regs[2] = EINTR;
100         regs.regs[7] = 1;
101         while (1) {
102                 current->state = TASK_INTERRUPTIBLE;
103                 schedule();
104                 if (do_signal(&saveset, &regs))
105                         return -EINTR;
106         }
107 }
108
109 #ifdef CONFIG_TRAD_SIGNALS
110 asmlinkage int sys_sigaction(int sig, const struct sigaction *act,
111         struct sigaction *oact)
112 {
113         struct k_sigaction new_ka, old_ka;
114         int ret;
115         int err = 0;
116
117         if (act) {
118                 old_sigset_t mask;
119
120                 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
121                         return -EFAULT;
122                 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
123                 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
124                 err |= __get_user(mask, &act->sa_mask.sig[0]);
125                 if (err)
126                         return -EFAULT;
127
128                 siginitset(&new_ka.sa.sa_mask, mask);
129         }
130
131         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
132
133         if (!ret && oact) {
134                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
135                         return -EFAULT;
136                 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
137                 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
138                 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
139                 err |= __put_user(0, &oact->sa_mask.sig[1]);
140                 err |= __put_user(0, &oact->sa_mask.sig[2]);
141                 err |= __put_user(0, &oact->sa_mask.sig[3]);
142                 if (err)
143                         return -EFAULT;
144         }
145
146         return ret;
147 }
148 #endif
149
150 asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs)
151 {
152         const stack_t __user *uss = (const stack_t __user *) regs.regs[4];
153         stack_t __user *uoss = (stack_t __user *) regs.regs[5];
154         unsigned long usp = regs.regs[29];
155
156         return do_sigaltstack(uss, uoss, usp);
157 }
158
159 #if PLAT_TRAMPOLINE_STUFF_LINE
160 #define __tramp __attribute__((aligned(PLAT_TRAMPOLINE_STUFF_LINE)))
161 #else
162 #define __tramp
163 #endif
164
165 #ifdef CONFIG_TRAD_SIGNALS
166 struct sigframe {
167         u32 sf_ass[4];                  /* argument save space for o32 */
168         u32 sf_code[2] __tramp;         /* signal trampoline */
169         struct sigcontext sf_sc __tramp;
170         sigset_t sf_mask;
171 };
172 #endif
173
174 struct rt_sigframe {
175         u32 rs_ass[4];                  /* argument save space for o32 */
176         u32 rs_code[2] __tramp;         /* signal trampoline */
177         struct siginfo rs_info __tramp;
178         struct ucontext rs_uc;
179 };
180
181 #ifdef CONFIG_TRAD_SIGNALS
182 save_static_function(sys_sigreturn);
183 __attribute_used__ noinline static void
184 _sys_sigreturn(nabi_no_regargs struct pt_regs regs)
185 {
186         struct sigframe *frame;
187         sigset_t blocked;
188
189         frame = (struct sigframe *) regs.regs[29];
190         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
191                 goto badframe;
192         if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
193                 goto badframe;
194
195         sigdelsetmask(&blocked, ~_BLOCKABLE);
196         spin_lock_irq(&current->sighand->siglock);
197         current->blocked = blocked;
198         recalc_sigpending();
199         spin_unlock_irq(&current->sighand->siglock);
200
201         if (restore_sigcontext(&regs, &frame->sf_sc))
202                 goto badframe;
203
204         /*
205          * Don't let your children do this ...
206          */
207         if (current_thread_info()->flags & TIF_SYSCALL_TRACE)
208                 do_syscall_trace(&regs, 1);
209         __asm__ __volatile__(
210                 "move\t$29, %0\n\t"
211                 "j\tsyscall_exit"
212                 :/* no outputs */
213                 :"r" (&regs));
214         /* Unreached */
215
216 badframe:
217         force_sig(SIGSEGV, current);
218 }
219 #endif
220
221 save_static_function(sys_rt_sigreturn);
222 __attribute_used__ noinline static void
223 _sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
224 {
225         struct rt_sigframe *frame;
226         sigset_t set;
227         stack_t st;
228
229         frame = (struct rt_sigframe *) regs.regs[29];
230         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
231                 goto badframe;
232         if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
233                 goto badframe;
234
235         sigdelsetmask(&set, ~_BLOCKABLE);
236         spin_lock_irq(&current->sighand->siglock);
237         current->blocked = set;
238         recalc_sigpending();
239         spin_unlock_irq(&current->sighand->siglock);
240
241         if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
242                 goto badframe;
243
244         if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
245                 goto badframe;
246         /* It is more difficult to avoid calling this function than to
247            call it and ignore errors.  */
248         do_sigaltstack(&st, NULL, regs.regs[29]);
249
250         /*
251          * Don't let your children do this ...
252          */
253         __asm__ __volatile__(
254                 "move\t$29, %0\n\t"
255                 "j\tsyscall_exit"
256                 :/* no outputs */
257                 :"r" (&regs));
258         /* Unreached */
259
260 badframe:
261         force_sig(SIGSEGV, current);
262 }
263
264 #ifdef CONFIG_TRAD_SIGNALS
265 static void inline setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
266         int signr, sigset_t *set)
267 {
268         struct sigframe *frame;
269         int err = 0;
270
271         frame = get_sigframe(ka, regs, sizeof(*frame));
272         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
273                 goto give_sigsegv;
274
275         /*
276          * Set up the return code ...
277          *
278          *         li      v0, __NR_sigreturn
279          *         syscall
280          */
281         if (PLAT_TRAMPOLINE_STUFF_LINE)
282                 __clear_user(frame->sf_code, PLAT_TRAMPOLINE_STUFF_LINE);
283         err |= __put_user(0x24020000 + __NR_sigreturn, frame->sf_code + 0);
284         err |= __put_user(0x0000000c                 , frame->sf_code + 1);
285         flush_cache_sigtramp((unsigned long) frame->sf_code);
286
287         err |= setup_sigcontext(regs, &frame->sf_sc);
288         err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
289         if (err)
290                 goto give_sigsegv;
291
292         /*
293          * Arguments to signal handler:
294          *
295          *   a0 = signal number
296          *   a1 = 0 (should be cause)
297          *   a2 = pointer to struct sigcontext
298          *
299          * $25 and c0_epc point to the signal handler, $29 points to the
300          * struct sigframe.
301          */
302         regs->regs[ 4] = signr;
303         regs->regs[ 5] = 0;
304         regs->regs[ 6] = (unsigned long) &frame->sf_sc;
305         regs->regs[29] = (unsigned long) frame;
306         regs->regs[31] = (unsigned long) frame->sf_code;
307         regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
308
309 #if DEBUG_SIG
310         printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
311                current->comm, current->pid,
312                frame, regs->cp0_epc, frame->regs[31]);
313 #endif
314         return;
315
316 give_sigsegv:
317         force_sigsegv(signr, current);
318 }
319 #endif
320
321 static void inline setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs,
322         int signr, sigset_t *set, siginfo_t *info)
323 {
324         struct rt_sigframe *frame;
325         int err = 0;
326
327         frame = get_sigframe(ka, regs, sizeof(*frame));
328         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
329                 goto give_sigsegv;
330
331         /*
332          * Set up the return code ...
333          *
334          *         li      v0, __NR_rt_sigreturn
335          *         syscall
336          */
337         if (PLAT_TRAMPOLINE_STUFF_LINE)
338                 __clear_user(frame->rs_code, PLAT_TRAMPOLINE_STUFF_LINE);
339         err |= __put_user(0x24020000 + __NR_rt_sigreturn, frame->rs_code + 0);
340         err |= __put_user(0x0000000c                    , frame->rs_code + 1);
341         flush_cache_sigtramp((unsigned long) frame->rs_code);
342
343         /* Create siginfo.  */
344         err |= copy_siginfo_to_user(&frame->rs_info, info);
345
346         /* Create the ucontext.  */
347         err |= __put_user(0, &frame->rs_uc.uc_flags);
348         err |= __put_user(0, &frame->rs_uc.uc_link);
349         err |= __put_user((void *)current->sas_ss_sp,
350                           &frame->rs_uc.uc_stack.ss_sp);
351         err |= __put_user(sas_ss_flags(regs->regs[29]),
352                           &frame->rs_uc.uc_stack.ss_flags);
353         err |= __put_user(current->sas_ss_size,
354                           &frame->rs_uc.uc_stack.ss_size);
355         err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
356         err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
357
358         if (err)
359                 goto give_sigsegv;
360
361         /*
362          * Arguments to signal handler:
363          *
364          *   a0 = signal number
365          *   a1 = 0 (should be cause)
366          *   a2 = pointer to ucontext
367          *
368          * $25 and c0_epc point to the signal handler, $29 points to
369          * the struct rt_sigframe.
370          */
371         regs->regs[ 4] = signr;
372         regs->regs[ 5] = (unsigned long) &frame->rs_info;
373         regs->regs[ 6] = (unsigned long) &frame->rs_uc;
374         regs->regs[29] = (unsigned long) frame;
375         regs->regs[31] = (unsigned long) frame->rs_code;
376         regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
377
378 #if DEBUG_SIG
379         printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
380                current->comm, current->pid,
381                frame, regs->cp0_epc, regs->regs[31]);
382 #endif
383         return;
384
385 give_sigsegv:
386         force_sigsegv(signr, current);
387 }
388
389 extern void setup_rt_frame_n32(struct k_sigaction * ka,
390         struct pt_regs *regs, int signr, sigset_t *set, siginfo_t *info);
391
392 static inline void handle_signal(unsigned long sig, siginfo_t *info,
393         struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
394 {
395         switch(regs->regs[0]) {
396         case ERESTART_RESTARTBLOCK:
397         case ERESTARTNOHAND:
398                 regs->regs[2] = EINTR;
399                 break;
400         case ERESTARTSYS:
401                 if(!(ka->sa.sa_flags & SA_RESTART)) {
402                         regs->regs[2] = EINTR;
403                         break;
404                 }
405         /* fallthrough */
406         case ERESTARTNOINTR:            /* Userland will reload $v0.  */
407                 regs->regs[7] = regs->regs[26];
408                 regs->cp0_epc -= 8;
409         }
410
411         regs->regs[0] = 0;              /* Don't deal with this again.  */
412
413 #ifdef CONFIG_TRAD_SIGNALS
414         if (ka->sa.sa_flags & SA_SIGINFO) {
415 #else
416         if (1) {
417 #endif
418 #ifdef CONFIG_MIPS32_N32
419                 if ((current->thread.mflags & MF_ABI_MASK) == MF_N32)
420                         setup_rt_frame_n32 (ka, regs, sig, oldset, info);
421                 else
422 #endif
423                         setup_rt_frame(ka, regs, sig, oldset, info);
424         }
425 #ifdef CONFIG_TRAD_SIGNALS
426         else
427                 setup_frame(ka, regs, sig, oldset);
428 #endif
429
430         spin_lock_irq(&current->sighand->siglock);
431         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
432         if (!(ka->sa.sa_flags & SA_NODEFER))
433                 sigaddset(&current->blocked,sig);
434         recalc_sigpending();
435         spin_unlock_irq(&current->sighand->siglock);
436 }
437
438 extern int do_signal32(sigset_t *oldset, struct pt_regs *regs);
439 extern int do_irix_signal(sigset_t *oldset, struct pt_regs *regs);
440
441 static int do_signal(sigset_t *oldset, struct pt_regs *regs)
442 {
443         struct k_sigaction ka;
444         siginfo_t info;
445         int signr;
446
447 #ifdef CONFIG_BINFMT_ELF32
448         if ((current->thread.mflags & MF_ABI_MASK) == MF_O32) {
449                 return do_signal32(oldset, regs);
450         }
451 #endif
452
453         /*
454          * We want the common case to go fast, which is why we may in certain
455          * cases get here from kernel mode. Just return without doing anything
456          * if so.
457          */
458         if (!user_mode(regs))
459                 return 1;
460
461         if (try_to_freeze())
462                 goto no_signal;
463
464         if (!oldset)
465                 oldset = &current->blocked;
466
467         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
468         if (signr > 0) {
469                 handle_signal(signr, &info, &ka, oldset, regs);
470                 return 1;
471         }
472
473 no_signal:
474         /*
475          * Who's code doesn't conform to the restartable syscall convention
476          * dies here!!!  The li instruction, a single machine instruction,
477          * must directly be followed by the syscall instruction.
478          */
479         if (regs->regs[0]) {
480                 if (regs->regs[2] == ERESTARTNOHAND ||
481                     regs->regs[2] == ERESTARTSYS ||
482                     regs->regs[2] == ERESTARTNOINTR) {
483                         regs->regs[7] = regs->regs[26];
484                         regs->cp0_epc -= 8;
485                 }
486                 if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
487                         regs->regs[2] = __NR_restart_syscall;
488                         regs->regs[7] = regs->regs[26];
489                         regs->cp0_epc -= 4;
490                 }
491         }
492         return 0;
493 }
494
495 /*
496  * notification of userspace execution resumption
497  * - triggered by current->work.notify_resume
498  */
499 asmlinkage void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
500         __u32 thread_info_flags)
501 {
502         /* deal with pending signal delivery */
503         if (thread_info_flags & _TIF_SIGPENDING) {
504 #ifdef CONFIG_BINFMT_ELF32
505                 if (likely((current->thread.mflags & MF_ABI_MASK) == MF_O32)) {
506                         do_signal32(oldset, regs);
507                         return;
508                 }
509 #endif
510 #ifdef CONFIG_BINFMT_IRIX
511                 if (unlikely(current->personality != PER_LINUX)) {
512                         do_irix_signal(oldset, regs);
513                         return;
514                 }
515 #endif
516                 do_signal(oldset, regs);
517         }
518 }