patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / arm / kernel / signal.c
1 /*
2  *  linux/arch/arm/kernel/signal.c
3  *
4  *  Copyright (C) 1995-2002 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/config.h>
11 #include <linux/errno.h>
12 #include <linux/signal.h>
13 #include <linux/ptrace.h>
14 #include <linux/personality.h>
15 #include <linux/suspend.h>
16
17 #include <asm/cacheflush.h>
18 #include <asm/ucontext.h>
19 #include <asm/uaccess.h>
20 #include <asm/unistd.h>
21
22 #include "ptrace.h"
23
24 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
25
26 /*
27  * For ARM syscalls, we encode the syscall number into the instruction.
28  */
29 #define SWI_SYS_SIGRETURN       (0xef000000|(__NR_sigreturn))
30 #define SWI_SYS_RT_SIGRETURN    (0xef000000|(__NR_rt_sigreturn))
31
32 /*
33  * For Thumb syscalls, we pass the syscall number via r7.  We therefore
34  * need two 16-bit instructions.
35  */
36 #define SWI_THUMB_SIGRETURN     (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
37 #define SWI_THUMB_RT_SIGRETURN  (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
38
39 static const unsigned long retcodes[4] = {
40         SWI_SYS_SIGRETURN,      SWI_THUMB_SIGRETURN,
41         SWI_SYS_RT_SIGRETURN,   SWI_THUMB_RT_SIGRETURN
42 };
43
44 static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
45
46 /*
47  * atomically swap in the new signal mask, and wait for a signal.
48  */
49 asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
50 {
51         sigset_t saveset;
52
53         mask &= _BLOCKABLE;
54         spin_lock_irq(&current->sighand->siglock);
55         saveset = current->blocked;
56         siginitset(&current->blocked, mask);
57         recalc_sigpending();
58         spin_unlock_irq(&current->sighand->siglock);
59         regs->ARM_r0 = -EINTR;
60
61         while (1) {
62                 current->state = TASK_INTERRUPTIBLE;
63                 schedule();
64                 if (do_signal(&saveset, regs, 0))
65                         return regs->ARM_r0;
66         }
67 }
68
69 asmlinkage int
70 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
71 {
72         sigset_t saveset, newset;
73
74         /* XXX: Don't preclude handling different sized sigset_t's. */
75         if (sigsetsize != sizeof(sigset_t))
76                 return -EINVAL;
77
78         if (copy_from_user(&newset, unewset, sizeof(newset)))
79                 return -EFAULT;
80         sigdelsetmask(&newset, ~_BLOCKABLE);
81
82         spin_lock_irq(&current->sighand->siglock);
83         saveset = current->blocked;
84         current->blocked = newset;
85         recalc_sigpending();
86         spin_unlock_irq(&current->sighand->siglock);
87         regs->ARM_r0 = -EINTR;
88
89         while (1) {
90                 current->state = TASK_INTERRUPTIBLE;
91                 schedule();
92                 if (do_signal(&saveset, regs, 0))
93                         return regs->ARM_r0;
94         }
95 }
96
97 asmlinkage int 
98 sys_sigaction(int sig, const struct old_sigaction __user *act,
99               struct old_sigaction __user *oact)
100 {
101         struct k_sigaction new_ka, old_ka;
102         int ret;
103
104         if (act) {
105                 old_sigset_t mask;
106                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
107                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
108                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
109                         return -EFAULT;
110                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
111                 __get_user(mask, &act->sa_mask);
112                 siginitset(&new_ka.sa.sa_mask, mask);
113         }
114
115         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
116
117         if (!ret && oact) {
118                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
119                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
120                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
121                         return -EFAULT;
122                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
123                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
124         }
125
126         return ret;
127 }
128
129 /*
130  * Do a signal return; undo the signal stack.
131  */
132 struct sigframe
133 {
134         struct sigcontext sc;
135         unsigned long extramask[_NSIG_WORDS-1];
136         unsigned long retcode;
137 };
138
139 struct rt_sigframe
140 {
141         struct siginfo __user *pinfo;
142         void __user *puc;
143         struct siginfo info;
144         struct ucontext uc;
145         unsigned long retcode;
146 };
147
148 static int
149 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
150 {
151         int err = 0;
152
153         __get_user_error(regs->ARM_r0, &sc->arm_r0, err);
154         __get_user_error(regs->ARM_r1, &sc->arm_r1, err);
155         __get_user_error(regs->ARM_r2, &sc->arm_r2, err);
156         __get_user_error(regs->ARM_r3, &sc->arm_r3, err);
157         __get_user_error(regs->ARM_r4, &sc->arm_r4, err);
158         __get_user_error(regs->ARM_r5, &sc->arm_r5, err);
159         __get_user_error(regs->ARM_r6, &sc->arm_r6, err);
160         __get_user_error(regs->ARM_r7, &sc->arm_r7, err);
161         __get_user_error(regs->ARM_r8, &sc->arm_r8, err);
162         __get_user_error(regs->ARM_r9, &sc->arm_r9, err);
163         __get_user_error(regs->ARM_r10, &sc->arm_r10, err);
164         __get_user_error(regs->ARM_fp, &sc->arm_fp, err);
165         __get_user_error(regs->ARM_ip, &sc->arm_ip, err);
166         __get_user_error(regs->ARM_sp, &sc->arm_sp, err);
167         __get_user_error(regs->ARM_lr, &sc->arm_lr, err);
168         __get_user_error(regs->ARM_pc, &sc->arm_pc, err);
169         __get_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
170
171         err |= !valid_user_regs(regs);
172
173         return err;
174 }
175
176 asmlinkage int sys_sigreturn(struct pt_regs *regs)
177 {
178         struct sigframe __user *frame;
179         sigset_t set;
180
181         /* Always make any pending restarted system calls return -EINTR */
182         current_thread_info()->restart_block.fn = do_no_restart_syscall;
183
184         /*
185          * Since we stacked the signal on a 64-bit boundary,
186          * then 'sp' should be word aligned here.  If it's
187          * not, then the user is trying to mess with us.
188          */
189         if (regs->ARM_sp & 7)
190                 goto badframe;
191
192         frame = (struct sigframe __user *)regs->ARM_sp;
193
194         if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
195                 goto badframe;
196         if (__get_user(set.sig[0], &frame->sc.oldmask)
197             || (_NSIG_WORDS > 1
198                 && __copy_from_user(&set.sig[1], &frame->extramask,
199                                     sizeof(frame->extramask))))
200                 goto badframe;
201
202         sigdelsetmask(&set, ~_BLOCKABLE);
203         spin_lock_irq(&current->sighand->siglock);
204         current->blocked = set;
205         recalc_sigpending();
206         spin_unlock_irq(&current->sighand->siglock);
207
208         if (restore_sigcontext(regs, &frame->sc))
209                 goto badframe;
210
211         /* Send SIGTRAP if we're single-stepping */
212         if (current->ptrace & PT_SINGLESTEP) {
213                 ptrace_cancel_bpt(current);
214                 send_sig(SIGTRAP, current, 1);
215         }
216
217         return regs->ARM_r0;
218
219 badframe:
220         force_sig(SIGSEGV, current);
221         return 0;
222 }
223
224 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
225 {
226         struct rt_sigframe __user *frame;
227         sigset_t set;
228
229         /* Always make any pending restarted system calls return -EINTR */
230         current_thread_info()->restart_block.fn = do_no_restart_syscall;
231
232         /*
233          * Since we stacked the signal on a 64-bit boundary,
234          * then 'sp' should be word aligned here.  If it's
235          * not, then the user is trying to mess with us.
236          */
237         if (regs->ARM_sp & 7)
238                 goto badframe;
239
240         frame = (struct rt_sigframe __user *)regs->ARM_sp;
241
242         if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
243                 goto badframe;
244         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
245                 goto badframe;
246
247         sigdelsetmask(&set, ~_BLOCKABLE);
248         spin_lock_irq(&current->sighand->siglock);
249         current->blocked = set;
250         recalc_sigpending();
251         spin_unlock_irq(&current->sighand->siglock);
252
253         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
254                 goto badframe;
255
256         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
257                 goto badframe;
258
259         /* Send SIGTRAP if we're single-stepping */
260         if (current->ptrace & PT_SINGLESTEP) {
261                 ptrace_cancel_bpt(current);
262                 send_sig(SIGTRAP, current, 1);
263         }
264
265         return regs->ARM_r0;
266
267 badframe:
268         force_sig(SIGSEGV, current);
269         return 0;
270 }
271
272 static int
273 setup_sigcontext(struct sigcontext __user *sc, /*struct _fpstate *fpstate,*/
274                  struct pt_regs *regs, unsigned long mask)
275 {
276         int err = 0;
277
278         __put_user_error(regs->ARM_r0, &sc->arm_r0, err);
279         __put_user_error(regs->ARM_r1, &sc->arm_r1, err);
280         __put_user_error(regs->ARM_r2, &sc->arm_r2, err);
281         __put_user_error(regs->ARM_r3, &sc->arm_r3, err);
282         __put_user_error(regs->ARM_r4, &sc->arm_r4, err);
283         __put_user_error(regs->ARM_r5, &sc->arm_r5, err);
284         __put_user_error(regs->ARM_r6, &sc->arm_r6, err);
285         __put_user_error(regs->ARM_r7, &sc->arm_r7, err);
286         __put_user_error(regs->ARM_r8, &sc->arm_r8, err);
287         __put_user_error(regs->ARM_r9, &sc->arm_r9, err);
288         __put_user_error(regs->ARM_r10, &sc->arm_r10, err);
289         __put_user_error(regs->ARM_fp, &sc->arm_fp, err);
290         __put_user_error(regs->ARM_ip, &sc->arm_ip, err);
291         __put_user_error(regs->ARM_sp, &sc->arm_sp, err);
292         __put_user_error(regs->ARM_lr, &sc->arm_lr, err);
293         __put_user_error(regs->ARM_pc, &sc->arm_pc, err);
294         __put_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
295
296         __put_user_error(current->thread.trap_no, &sc->trap_no, err);
297         __put_user_error(current->thread.error_code, &sc->error_code, err);
298         __put_user_error(current->thread.address, &sc->fault_address, err);
299         __put_user_error(mask, &sc->oldmask, err);
300
301         return err;
302 }
303
304 static inline void __user *
305 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
306 {
307         unsigned long sp = regs->ARM_sp;
308
309         /*
310          * This is the X/Open sanctioned signal stack switching.
311          */
312         if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
313                 sp = current->sas_ss_sp + current->sas_ss_size;
314
315         /*
316          * ATPCS B01 mandates 8-byte alignment
317          */
318         return (void __user *)((sp - framesize) & ~7);
319 }
320
321 static int
322 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
323              unsigned long __user *rc, void __user *frame, int usig)
324 {
325         unsigned long handler = (unsigned long)ka->sa.sa_handler;
326         unsigned long retcode;
327         int thumb = 0;
328         unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
329
330         /*
331          * Maybe we need to deliver a 32-bit signal to a 26-bit task.
332          */
333         if (ka->sa.sa_flags & SA_THIRTYTWO)
334                 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
335
336 #ifdef CONFIG_ARM_THUMB
337         if (elf_hwcap & HWCAP_THUMB) {
338                 /*
339                  * The LSB of the handler determines if we're going to
340                  * be using THUMB or ARM mode for this signal handler.
341                  */
342                 thumb = handler & 1;
343
344                 if (thumb)
345                         cpsr |= PSR_T_BIT;
346                 else
347                         cpsr &= ~PSR_T_BIT;
348         }
349 #endif
350
351         if (ka->sa.sa_flags & SA_RESTORER) {
352                 retcode = (unsigned long)ka->sa.sa_restorer;
353         } else {
354                 unsigned int idx = thumb;
355
356                 if (ka->sa.sa_flags & SA_SIGINFO)
357                         idx += 2;
358
359                 if (__put_user(retcodes[idx], rc))
360                         return 1;
361
362                 /*
363                  * Ensure that the instruction cache sees
364                  * the return code written onto the stack.
365                  */
366                 flush_icache_range((unsigned long)rc,
367                                    (unsigned long)(rc + 1));
368
369                 retcode = ((unsigned long)rc) + thumb;
370         }
371
372         regs->ARM_r0 = usig;
373         regs->ARM_sp = (unsigned long)frame;
374         regs->ARM_lr = retcode;
375         regs->ARM_pc = handler;
376         regs->ARM_cpsr = cpsr;
377
378         return 0;
379 }
380
381 static int
382 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
383 {
384         struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
385         int err = 0;
386
387         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
388                 return 1;
389
390         err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
391
392         if (_NSIG_WORDS > 1) {
393                 err |= __copy_to_user(frame->extramask, &set->sig[1],
394                                       sizeof(frame->extramask));
395         }
396
397         if (err == 0)
398                 err = setup_return(regs, ka, &frame->retcode, frame, usig);
399
400         return err;
401 }
402
403 static int
404 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
405                sigset_t *set, struct pt_regs *regs)
406 {
407         struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
408         stack_t stack;
409         int err = 0;
410
411         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
412                 return 1;
413
414         __put_user_error(&frame->info, &frame->pinfo, err);
415         __put_user_error(&frame->uc, &frame->puc, err);
416         err |= copy_siginfo_to_user(&frame->info, info);
417
418         __put_user_error(0, &frame->uc.uc_flags, err);
419         __put_user_error(NULL, &frame->uc.uc_link, err);
420
421         memset(&stack, 0, sizeof(stack));
422         stack.ss_sp = (void *)current->sas_ss_sp;
423         stack.ss_flags = sas_ss_flags(regs->ARM_sp);
424         stack.ss_size = current->sas_ss_size;
425         err |= __copy_to_user(&frame->uc.uc_stack, &stack, sizeof(stack));
426
427         err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
428                                 regs, set->sig[0]);
429         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
430
431         if (err == 0)
432                 err = setup_return(regs, ka, &frame->retcode, frame, usig);
433
434         if (err == 0) {
435                 /*
436                  * For realtime signals we must also set the second and third
437                  * arguments for the signal handler.
438                  *   -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
439                  */
440                 regs->ARM_r1 = (unsigned long)&frame->info;
441                 regs->ARM_r2 = (unsigned long)&frame->uc;
442         }
443
444         return err;
445 }
446
447 static inline void restart_syscall(struct pt_regs *regs)
448 {
449         regs->ARM_r0 = regs->ARM_ORIG_r0;
450         regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
451 }
452
453 /*
454  * OK, we're invoking a handler
455  */     
456 static void
457 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
458               struct pt_regs * regs, int syscall)
459 {
460         struct thread_info *thread = current_thread_info();
461         struct task_struct *tsk = current;
462         struct k_sigaction *ka = &tsk->sighand->action[sig-1];
463         int usig = sig;
464         int ret;
465
466         /*
467          * If we were from a system call, check for system call restarting...
468          */
469         if (syscall) {
470                 switch (regs->ARM_r0) {
471                 case -ERESTART_RESTARTBLOCK:
472                 case -ERESTARTNOHAND:
473                         regs->ARM_r0 = -EINTR;
474                         break;
475                 case -ERESTARTSYS:
476                         if (!(ka->sa.sa_flags & SA_RESTART)) {
477                                 regs->ARM_r0 = -EINTR;
478                                 break;
479                         }
480                         /* fallthrough */
481                 case -ERESTARTNOINTR:
482                         restart_syscall(regs);
483                 }
484         }
485
486         /*
487          * translate the signal
488          */
489         if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
490                 usig = thread->exec_domain->signal_invmap[usig];
491
492         /*
493          * Set up the stack frame
494          */
495         if (ka->sa.sa_flags & SA_SIGINFO)
496                 ret = setup_rt_frame(usig, ka, info, oldset, regs);
497         else
498                 ret = setup_frame(usig, ka, oldset, regs);
499
500         /*
501          * Check that the resulting registers are actually sane.
502          */
503         ret |= !valid_user_regs(regs);
504
505         /*
506          * Block the signal if we were unsuccessful.
507          */
508         if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
509                 spin_lock_irq(&tsk->sighand->siglock);
510                 sigorsets(&tsk->blocked, &tsk->blocked,
511                           &ka->sa.sa_mask);
512                 sigaddset(&tsk->blocked, sig);
513                 recalc_sigpending();
514                 spin_unlock_irq(&tsk->sighand->siglock);
515         }
516
517         if (ret == 0) {
518                 if (ka->sa.sa_flags & SA_ONESHOT)
519                         ka->sa.sa_handler = SIG_DFL;
520                 return;
521         }
522
523         if (sig == SIGSEGV)
524                 ka->sa.sa_handler = SIG_DFL;
525         force_sig(SIGSEGV, tsk);
526 }
527
528 /*
529  * Note that 'init' is a special process: it doesn't get signals it doesn't
530  * want to handle. Thus you cannot kill init even with a SIGKILL even by
531  * mistake.
532  *
533  * Note that we go through the signals twice: once to check the signals that
534  * the kernel can handle, and then we build all the user-level signal handling
535  * stack-frames in one go after that.
536  */
537 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
538 {
539         siginfo_t info;
540         int signr;
541
542         /*
543          * We want the common case to go fast, which
544          * is why we may in certain cases get here from
545          * kernel mode. Just return without doing anything
546          * if so.
547          */
548         if (!user_mode(regs))
549                 return 0;
550
551         if (current->flags & PF_FREEZE) {
552                 refrigerator(0);
553                 goto no_signal;
554         }
555
556         if (current->ptrace & PT_SINGLESTEP)
557                 ptrace_cancel_bpt(current);
558
559         signr = get_signal_to_deliver(&info, regs, NULL);
560         if (signr > 0) {
561                 handle_signal(signr, &info, oldset, regs, syscall);
562                 if (current->ptrace & PT_SINGLESTEP)
563                         ptrace_set_bpt(current);
564                 return 1;
565         }
566
567  no_signal:
568         /*
569          * No signal to deliver to the process - restart the syscall.
570          */
571         if (syscall) {
572                 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
573                         if (thumb_mode(regs)) {
574                                 regs->ARM_r7 = __NR_restart_syscall;
575                                 regs->ARM_pc -= 2;
576                         } else {
577                                 u32 __user *usp;
578
579                                 regs->ARM_sp -= 12;
580                                 usp = (u32 __user *)regs->ARM_sp;
581
582                                 put_user(regs->ARM_pc, &usp[0]);
583                                 /* swi __NR_restart_syscall */
584                                 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
585                                 /* ldr  pc, [sp], #12 */
586                                 put_user(0xe49df00c, &usp[2]);
587
588                                 flush_icache_range((unsigned long)usp,
589                                                    (unsigned long)(usp + 3));
590
591                                 regs->ARM_pc = regs->ARM_sp + 4;
592                         }
593                 }
594                 if (regs->ARM_r0 == -ERESTARTNOHAND ||
595                     regs->ARM_r0 == -ERESTARTSYS ||
596                     regs->ARM_r0 == -ERESTARTNOINTR) {
597                         restart_syscall(regs);
598                 }
599         }
600         if (current->ptrace & PT_SINGLESTEP)
601                 ptrace_set_bpt(current);
602         return 0;
603 }
604
605 asmlinkage void
606 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
607 {
608         if (thread_flags & _TIF_SIGPENDING)
609                 do_signal(&current->blocked, regs, syscall);
610 }