ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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         /* Send SIGTRAP if we're single-stepping */
257         if (current->ptrace & PT_SINGLESTEP) {
258                 ptrace_cancel_bpt(current);
259                 send_sig(SIGTRAP, current, 1);
260         }
261
262         return regs->ARM_r0;
263
264 badframe:
265         force_sig(SIGSEGV, current);
266         return 0;
267 }
268
269 static int
270 setup_sigcontext(struct sigcontext __user *sc, /*struct _fpstate *fpstate,*/
271                  struct pt_regs *regs, unsigned long mask)
272 {
273         int err = 0;
274
275         __put_user_error(regs->ARM_r0, &sc->arm_r0, err);
276         __put_user_error(regs->ARM_r1, &sc->arm_r1, err);
277         __put_user_error(regs->ARM_r2, &sc->arm_r2, err);
278         __put_user_error(regs->ARM_r3, &sc->arm_r3, err);
279         __put_user_error(regs->ARM_r4, &sc->arm_r4, err);
280         __put_user_error(regs->ARM_r5, &sc->arm_r5, err);
281         __put_user_error(regs->ARM_r6, &sc->arm_r6, err);
282         __put_user_error(regs->ARM_r7, &sc->arm_r7, err);
283         __put_user_error(regs->ARM_r8, &sc->arm_r8, err);
284         __put_user_error(regs->ARM_r9, &sc->arm_r9, err);
285         __put_user_error(regs->ARM_r10, &sc->arm_r10, err);
286         __put_user_error(regs->ARM_fp, &sc->arm_fp, err);
287         __put_user_error(regs->ARM_ip, &sc->arm_ip, err);
288         __put_user_error(regs->ARM_sp, &sc->arm_sp, err);
289         __put_user_error(regs->ARM_lr, &sc->arm_lr, err);
290         __put_user_error(regs->ARM_pc, &sc->arm_pc, err);
291         __put_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
292
293         __put_user_error(current->thread.trap_no, &sc->trap_no, err);
294         __put_user_error(current->thread.error_code, &sc->error_code, err);
295         __put_user_error(current->thread.address, &sc->fault_address, err);
296         __put_user_error(mask, &sc->oldmask, err);
297
298         return err;
299 }
300
301 static inline void __user *
302 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
303 {
304         unsigned long sp = regs->ARM_sp;
305
306         /*
307          * This is the X/Open sanctioned signal stack switching.
308          */
309         if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
310                 sp = current->sas_ss_sp + current->sas_ss_size;
311
312         /*
313          * ATPCS B01 mandates 8-byte alignment
314          */
315         return (void __user *)((sp - framesize) & ~7);
316 }
317
318 static int
319 setup_return(struct pt_regs *regs, struct k_sigaction *ka,
320              unsigned long __user *rc, void __user *frame, int usig)
321 {
322         unsigned long handler = (unsigned long)ka->sa.sa_handler;
323         unsigned long retcode;
324         int thumb = 0;
325         unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
326
327         /*
328          * Maybe we need to deliver a 32-bit signal to a 26-bit task.
329          */
330         if (ka->sa.sa_flags & SA_THIRTYTWO)
331                 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
332
333 #ifdef CONFIG_ARM_THUMB
334         if (elf_hwcap & HWCAP_THUMB) {
335                 /*
336                  * The LSB of the handler determines if we're going to
337                  * be using THUMB or ARM mode for this signal handler.
338                  */
339                 thumb = handler & 1;
340
341                 if (thumb)
342                         cpsr |= PSR_T_BIT;
343                 else
344                         cpsr &= ~PSR_T_BIT;
345         }
346 #endif
347
348         if (ka->sa.sa_flags & SA_RESTORER) {
349                 retcode = (unsigned long)ka->sa.sa_restorer;
350         } else {
351                 unsigned int idx = thumb;
352
353                 if (ka->sa.sa_flags & SA_SIGINFO)
354                         idx += 2;
355
356                 if (__put_user(retcodes[idx], rc))
357                         return 1;
358
359                 /*
360                  * Ensure that the instruction cache sees
361                  * the return code written onto the stack.
362                  */
363                 flush_icache_range((unsigned long)rc,
364                                    (unsigned long)(rc + 1));
365
366                 retcode = ((unsigned long)rc) + thumb;
367         }
368
369         regs->ARM_r0 = usig;
370         regs->ARM_sp = (unsigned long)frame;
371         regs->ARM_lr = retcode;
372         regs->ARM_pc = handler;
373         regs->ARM_cpsr = cpsr;
374
375         return 0;
376 }
377
378 static int
379 setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
380 {
381         struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
382         int err = 0;
383
384         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
385                 return 1;
386
387         err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
388
389         if (_NSIG_WORDS > 1) {
390                 err |= __copy_to_user(frame->extramask, &set->sig[1],
391                                       sizeof(frame->extramask));
392         }
393
394         if (err == 0)
395                 err = setup_return(regs, ka, &frame->retcode, frame, usig);
396
397         return err;
398 }
399
400 static int
401 setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
402                sigset_t *set, struct pt_regs *regs)
403 {
404         struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
405         int err = 0;
406
407         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
408                 return 1;
409
410         __put_user_error(&frame->info, &frame->pinfo, err);
411         __put_user_error(&frame->uc, &frame->puc, err);
412         err |= copy_siginfo_to_user(&frame->info, info);
413
414         /* Clear all the bits of the ucontext we don't use.  */
415         err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
416
417         err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
418                                 regs, set->sig[0]);
419         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
420
421         if (err == 0)
422                 err = setup_return(regs, ka, &frame->retcode, frame, usig);
423
424         if (err == 0) {
425                 /*
426                  * For realtime signals we must also set the second and third
427                  * arguments for the signal handler.
428                  *   -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
429                  */
430                 regs->ARM_r1 = (unsigned long)frame->pinfo;
431                 regs->ARM_r2 = (unsigned long)frame->puc;
432         }
433
434         return err;
435 }
436
437 static inline void restart_syscall(struct pt_regs *regs)
438 {
439         regs->ARM_r0 = regs->ARM_ORIG_r0;
440         regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
441 }
442
443 /*
444  * OK, we're invoking a handler
445  */     
446 static void
447 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
448               struct pt_regs * regs, int syscall)
449 {
450         struct thread_info *thread = current_thread_info();
451         struct task_struct *tsk = current;
452         struct k_sigaction *ka = &tsk->sighand->action[sig-1];
453         int usig = sig;
454         int ret;
455
456         /*
457          * If we were from a system call, check for system call restarting...
458          */
459         if (syscall) {
460                 switch (regs->ARM_r0) {
461                 case -ERESTART_RESTARTBLOCK:
462                 case -ERESTARTNOHAND:
463                         regs->ARM_r0 = -EINTR;
464                         break;
465                 case -ERESTARTSYS:
466                         if (!(ka->sa.sa_flags & SA_RESTART)) {
467                                 regs->ARM_r0 = -EINTR;
468                                 break;
469                         }
470                         /* fallthrough */
471                 case -ERESTARTNOINTR:
472                         restart_syscall(regs);
473                 }
474         }
475
476         /*
477          * translate the signal
478          */
479         if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
480                 usig = thread->exec_domain->signal_invmap[usig];
481
482         /*
483          * Set up the stack frame
484          */
485         if (ka->sa.sa_flags & SA_SIGINFO)
486                 ret = setup_rt_frame(usig, ka, info, oldset, regs);
487         else
488                 ret = setup_frame(usig, ka, oldset, regs);
489
490         /*
491          * Check that the resulting registers are actually sane.
492          */
493         ret |= !valid_user_regs(regs);
494
495         /*
496          * Block the signal if we were unsuccessful.
497          */
498         if (ret != 0 || !(ka->sa.sa_flags & SA_NODEFER)) {
499                 spin_lock_irq(&tsk->sighand->siglock);
500                 sigorsets(&tsk->blocked, &tsk->blocked,
501                           &ka->sa.sa_mask);
502                 sigaddset(&tsk->blocked, sig);
503                 recalc_sigpending();
504                 spin_unlock_irq(&tsk->sighand->siglock);
505         }
506
507         if (ret == 0) {
508                 if (ka->sa.sa_flags & SA_ONESHOT)
509                         ka->sa.sa_handler = SIG_DFL;
510                 return;
511         }
512
513         if (sig == SIGSEGV)
514                 ka->sa.sa_handler = SIG_DFL;
515         force_sig(SIGSEGV, tsk);
516 }
517
518 /*
519  * Note that 'init' is a special process: it doesn't get signals it doesn't
520  * want to handle. Thus you cannot kill init even with a SIGKILL even by
521  * mistake.
522  *
523  * Note that we go through the signals twice: once to check the signals that
524  * the kernel can handle, and then we build all the user-level signal handling
525  * stack-frames in one go after that.
526  */
527 static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
528 {
529         siginfo_t info;
530         int signr;
531
532         /*
533          * We want the common case to go fast, which
534          * is why we may in certain cases get here from
535          * kernel mode. Just return without doing anything
536          * if so.
537          */
538         if (!user_mode(regs))
539                 return 0;
540
541         if (current->flags & PF_FREEZE) {
542                 refrigerator(0);
543                 goto no_signal;
544         }
545
546         if (current->ptrace & PT_SINGLESTEP)
547                 ptrace_cancel_bpt(current);
548
549         signr = get_signal_to_deliver(&info, regs, NULL);
550         if (signr > 0) {
551                 handle_signal(signr, &info, oldset, regs, syscall);
552                 if (current->ptrace & PT_SINGLESTEP)
553                         ptrace_set_bpt(current);
554                 return 1;
555         }
556
557  no_signal:
558         /*
559          * No signal to deliver to the process - restart the syscall.
560          */
561         if (syscall) {
562                 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
563                         if (thumb_mode(regs)) {
564                                 regs->ARM_r7 = __NR_restart_syscall;
565                                 regs->ARM_pc -= 2;
566                         } else {
567                                 u32 *usp;
568
569                                 regs->ARM_sp -= 12;
570                                 usp = (u32 *)regs->ARM_sp;
571
572                                 put_user(regs->ARM_pc, &usp[0]);
573                                 /* swi __NR_restart_syscall */
574                                 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
575                                 /* ldr  pc, [sp], #12 */
576                                 put_user(0xe49df00c, &usp[2]);
577
578                                 flush_icache_range((unsigned long)usp,
579                                                    (unsigned long)(usp + 3));
580
581                                 regs->ARM_pc = regs->ARM_sp + 4;
582                         }
583                 }
584                 if (regs->ARM_r0 == -ERESTARTNOHAND ||
585                     regs->ARM_r0 == -ERESTARTSYS ||
586                     regs->ARM_r0 == -ERESTARTNOINTR) {
587                         restart_syscall(regs);
588                 }
589         }
590         if (current->ptrace & PT_SINGLESTEP)
591                 ptrace_set_bpt(current);
592         return 0;
593 }
594
595 asmlinkage void
596 do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
597 {
598         if (thread_flags & _TIF_SIGPENDING)
599                 do_signal(&current->blocked, regs, syscall);
600 }