ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / kernel / signal.c
1 /*
2  *  arch/ppc/kernel/signal.c
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Derived from "arch/i386/kernel/signal.c"
8  *    Copyright (C) 1991, 1992 Linus Torvalds
9  *    1997-11-28  Modified for POSIX.1b signals by Richard Henderson
10  *
11  *  This program is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU General Public License
13  *  as published by the Free Software Foundation; either version
14  *  2 of the License, or (at your option) any later version.
15  */
16
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/kernel.h>
22 #include <linux/signal.h>
23 #include <linux/errno.h>
24 #include <linux/wait.h>
25 #include <linux/ptrace.h>
26 #include <linux/unistd.h>
27 #include <linux/stddef.h>
28 #include <linux/elf.h>
29 #include <linux/tty.h>
30 #include <linux/binfmts.h>
31 #include <asm/ucontext.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
34 #include <asm/cacheflush.h>
35
36 #undef DEBUG_SIG
37
38 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
39
40 extern void sigreturn_exit(struct pt_regs *);
41
42 #define GP_REGS_SIZE    min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
43
44 int do_signal(sigset_t *oldset, struct pt_regs *regs);
45
46 /*
47  * Atomically swap in the new signal mask, and wait for a signal.
48  */
49 int
50 sys_sigsuspend(old_sigset_t mask, int p2, int p3, int p4, int p6, int p7,
51                struct pt_regs *regs)
52 {
53         sigset_t saveset;
54
55         mask &= _BLOCKABLE;
56         spin_lock_irq(&current->sighand->siglock);
57         saveset = current->blocked;
58         siginitset(&current->blocked, mask);
59         recalc_sigpending();
60         spin_unlock_irq(&current->sighand->siglock);
61
62         regs->result = -EINTR;
63         regs->gpr[3] = EINTR;
64         regs->ccr |= 0x10000000;
65         while (1) {
66                 current->state = TASK_INTERRUPTIBLE;
67                 schedule();
68                 if (do_signal(&saveset, regs))
69                         sigreturn_exit(regs);
70         }
71 }
72
73 int
74 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, int p3, int p4,
75                   int p6, int p7, struct pt_regs *regs)
76 {
77         sigset_t saveset, newset;
78
79         /* XXX: Don't preclude handling different sized sigset_t's.  */
80         if (sigsetsize != sizeof(sigset_t))
81                 return -EINVAL;
82
83         if (copy_from_user(&newset, unewset, sizeof(newset)))
84                 return -EFAULT;
85         sigdelsetmask(&newset, ~_BLOCKABLE);
86
87         spin_lock_irq(&current->sighand->siglock);
88         saveset = current->blocked;
89         current->blocked = newset;
90         recalc_sigpending();
91         spin_unlock_irq(&current->sighand->siglock);
92
93         regs->result = -EINTR;
94         regs->gpr[3] = EINTR;
95         regs->ccr |= 0x10000000;
96         while (1) {
97                 current->state = TASK_INTERRUPTIBLE;
98                 schedule();
99                 if (do_signal(&saveset, regs))
100                         sigreturn_exit(regs);
101         }
102 }
103
104
105 int
106 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, int r5,
107                 int r6, int r7, int r8, struct pt_regs *regs)
108 {
109         return do_sigaltstack(uss, uoss, regs->gpr[1]);
110 }
111
112 int
113 sys_sigaction(int sig, const struct old_sigaction __user *act,
114               struct old_sigaction __user *oact)
115 {
116         struct k_sigaction new_ka, old_ka;
117         int ret;
118
119         if (act) {
120                 old_sigset_t mask;
121                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
122                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
123                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
124                         return -EFAULT;
125                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
126                 __get_user(mask, &act->sa_mask);
127                 siginitset(&new_ka.sa.sa_mask, mask);
128         }
129
130         ret = do_sigaction(sig, (act? &new_ka: NULL), (oact? &old_ka: NULL));
131
132         if (!ret && oact) {
133                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
134                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
135                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
136                         return -EFAULT;
137                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
138                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
139         }
140
141         return ret;
142 }
143
144 /*
145  * When we have signals to deliver, we set up on the
146  * user stack, going down from the original stack pointer:
147  *      a sigregs struct
148  *      a sigcontext struct
149  *      a gap of __SIGNAL_FRAMESIZE bytes
150  *
151  * Each of these things must be a multiple of 16 bytes in size.
152  *
153  */
154 struct sigregs {
155         struct mcontext mctx;           /* all the register values */
156         /* Programs using the rs6000/xcoff abi can save up to 19 gp regs
157            and 18 fp regs below sp before decrementing it. */
158         int             abigap[56];
159 };
160
161 /* We use the mc_pad field for the signal return trampoline. */
162 #define tramp   mc_pad
163
164 /*
165  *  When we have rt signals to deliver, we set up on the
166  *  user stack, going down from the original stack pointer:
167  *      one rt_sigframe struct (siginfo + ucontext + ABI gap)
168  *      a gap of __SIGNAL_FRAMESIZE+16 bytes
169  *  (the +16 is to get the siginfo and ucontext in the same
170  *  positions as in older kernels).
171  *
172  *  Each of these things must be a multiple of 16 bytes in size.
173  *
174  */
175 struct rt_sigframe
176 {
177         struct siginfo info;
178         struct ucontext uc;
179         /* Programs using the rs6000/xcoff abi can save up to 19 gp regs
180            and 18 fp regs below sp before decrementing it. */
181         int             abigap[56];
182 };
183
184 /*
185  * Save the current user registers on the user stack.
186  * We only save the altivec registers if the process has used
187  * altivec instructions at some point.
188  */
189 static int
190 save_user_regs(struct pt_regs *regs, struct mcontext *frame, int sigret)
191 {
192         /* save general and floating-point registers */
193         CHECK_FULL_REGS(regs);
194         preempt_disable();
195         if (regs->msr & MSR_FP)
196                 giveup_fpu(current);
197 #ifdef CONFIG_ALTIVEC
198         if (current->thread.used_vr && (regs->msr & MSR_VEC))
199                 giveup_altivec(current);
200 #endif /* CONFIG_ALTIVEC */
201         preempt_enable();
202
203         if (__copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE)
204             || __copy_to_user(&frame->mc_fregs, current->thread.fpr,
205                               ELF_NFPREG * sizeof(double)))
206                 return 1;
207
208         current->thread.fpscr = 0;      /* turn off all fp exceptions */
209
210 #ifdef CONFIG_ALTIVEC
211         /* save altivec registers */
212         if (current->thread.used_vr) {
213                 if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
214                                    ELF_NVRREG * sizeof(vector128)))
215                         return 1;
216                 /* set MSR_VEC in the saved MSR value to indicate that
217                    frame->mc_vregs contains valid data */
218                 if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
219                         return 1;
220         }
221         /* else assert((regs->msr & MSR_VEC) == 0) */
222
223         /* We always copy to/from vrsave, it's 0 if we don't have or don't
224          * use altivec. Since VSCR only contains 32 bits saved in the least
225          * significant bits of a vector, we "cheat" and stuff VRSAVE in the
226          * most significant bits of that same vector. --BenH
227          */
228         if (__put_user(current->thread.vrsave, (u32 *)&frame->mc_vregs[32]))
229                 return 1;
230 #endif /* CONFIG_ALTIVEC */
231
232         if (sigret) {
233                 /* Set up the sigreturn trampoline: li r0,sigret; sc */
234                 if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
235                     || __put_user(0x44000002UL, &frame->tramp[1]))
236                         return 1;
237                 flush_icache_range((unsigned long) &frame->tramp[0],
238                                    (unsigned long) &frame->tramp[2]);
239         }
240
241         return 0;
242 }
243
244 /*
245  * Restore the current user register values from the user stack,
246  * (except for MSR).
247  */
248 static int
249 restore_user_regs(struct pt_regs *regs, struct mcontext __user *sr, int sig)
250 {
251         unsigned long save_r2;
252 #ifdef CONFIG_ALTIVEC
253         unsigned long msr;
254 #endif
255
256         /* backup/restore the TLS as we don't want it to be modified */
257         if (!sig)
258                 save_r2 = regs->gpr[2];
259         /* copy up to but not including MSR */
260         if (__copy_from_user(regs, &sr->mc_gregs, PT_MSR * sizeof(elf_greg_t)))
261                 return 1;
262         /* copy from orig_r3 (the word after the MSR) up to the end */
263         if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
264                              GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
265                 return 1;
266         if (!sig)
267                 regs->gpr[2] = save_r2;
268
269         /* force the process to reload the FP registers from
270            current->thread when it next does FP instructions */
271         regs->msr &= ~MSR_FP;
272         if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
273                              sizeof(sr->mc_fregs)))
274                 return 1;
275
276 #ifdef CONFIG_ALTIVEC
277         /* force the process to reload the altivec registers from
278            current->thread when it next does altivec instructions */
279         regs->msr &= ~MSR_VEC;
280         if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_VEC) != 0) {
281                 /* restore altivec registers from the stack */
282                 if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
283                                      sizeof(sr->mc_vregs)))
284                         return 1;
285         } else if (current->thread.used_vr)
286                 memset(&current->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
287
288         /* Always get VRSAVE back */
289         if (__get_user(current->thread.vrsave, (u32 *)&sr->mc_vregs[32]))
290                 return 1;
291 #endif /* CONFIG_ALTIVEC */
292
293         return 0;
294 }
295
296 /*
297  * Restore the user process's signal mask
298  */
299 static void
300 restore_sigmask(sigset_t *set)
301 {
302         sigdelsetmask(set, ~_BLOCKABLE);
303         spin_lock_irq(&current->sighand->siglock);
304         current->blocked = *set;
305         recalc_sigpending();
306         spin_unlock_irq(&current->sighand->siglock);
307 }
308
309 /*
310  * Set up a signal frame for a "real-time" signal handler
311  * (one which gets siginfo).
312  */
313 static void
314 handle_rt_signal(unsigned long sig, struct k_sigaction *ka,
315                  siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
316                  unsigned long newsp)
317 {
318         struct rt_sigframe __user *rt_sf;
319         struct mcontext __user *frame;
320         unsigned long origsp = newsp;
321
322         /* Set up Signal Frame */
323         /* Put a Real Time Context onto stack */
324         newsp -= sizeof(*rt_sf);
325         rt_sf = (struct rt_sigframe __user *) newsp;
326
327         /* create a stack frame for the caller of the handler */
328         newsp -= __SIGNAL_FRAMESIZE + 16;
329
330         if (verify_area(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
331                 goto badframe;
332
333         /* Put the siginfo & fill in most of the ucontext */
334         if (copy_siginfo_to_user(&rt_sf->info, info)
335             || __put_user(0, &rt_sf->uc.uc_flags)
336             || __put_user(0, &rt_sf->uc.uc_link)
337             || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
338             || __put_user(sas_ss_flags(regs->gpr[1]),
339                           &rt_sf->uc.uc_stack.ss_flags)
340             || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
341             || __put_user(&rt_sf->uc.uc_mcontext, &rt_sf->uc.uc_regs)
342             || __copy_to_user(&rt_sf->uc.uc_sigmask, oldset, sizeof(*oldset)))
343                 goto badframe;
344
345         /* Save user registers on the stack */
346         frame = &rt_sf->uc.uc_mcontext;
347         if (save_user_regs(regs, frame, __NR_rt_sigreturn))
348                 goto badframe;
349
350         if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
351                 goto badframe;
352         regs->gpr[1] = newsp;
353         regs->gpr[3] = sig;
354         regs->gpr[4] = (unsigned long) &rt_sf->info;
355         regs->gpr[5] = (unsigned long) &rt_sf->uc;
356         regs->gpr[6] = (unsigned long) rt_sf;
357         regs->nip = (unsigned long) ka->sa.sa_handler;
358         regs->link = (unsigned long) frame->tramp;
359         regs->trap = 0;
360
361         return;
362
363 badframe:
364 #ifdef DEBUG_SIG
365         printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
366                regs, frame, newsp);
367 #endif
368         if (sig == SIGSEGV)
369                 ka->sa.sa_handler = SIG_DFL;
370         force_sig(SIGSEGV, current);
371 }
372
373 static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
374 {
375         sigset_t set;
376         struct mcontext *mcp;
377
378         if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(set))
379             || __get_user(mcp, &ucp->uc_regs))
380                 return -EFAULT;
381         restore_sigmask(&set);
382         if (restore_user_regs(regs, mcp, sig))
383                 return -EFAULT;
384
385         return 0;
386 }
387
388 int sys_swapcontext(struct ucontext __user *old_ctx,
389                     struct ucontext __user *new_ctx,
390                     int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
391 {
392         unsigned char tmp;
393
394         /* Context size is for future use. Right now, we only make sure
395          * we are passed something we understand
396          */
397         if (ctx_size < sizeof(struct ucontext))
398                 return -EINVAL;
399
400         if (old_ctx != NULL) {
401                 if (verify_area(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
402                     || save_user_regs(regs, &old_ctx->uc_mcontext, 0)
403                     || __copy_to_user(&old_ctx->uc_sigmask,
404                                       &current->blocked, sizeof(sigset_t))
405                     || __put_user(&old_ctx->uc_mcontext, &old_ctx->uc_regs))
406                         return -EFAULT;
407         }
408         if (new_ctx == NULL)
409                 return 0;
410         if (verify_area(VERIFY_READ, new_ctx, sizeof(*new_ctx))
411             || __get_user(tmp, (u8 *) new_ctx)
412             || __get_user(tmp, (u8 *) (new_ctx + 1) - 1))
413                 return -EFAULT;
414
415         /*
416          * If we get a fault copying the context into the kernel's
417          * image of the user's registers, we can't just return -EFAULT
418          * because the user's registers will be corrupted.  For instance
419          * the NIP value may have been updated but not some of the
420          * other registers.  Given that we have done the verify_area
421          * and successfully read the first and last bytes of the region
422          * above, this should only happen in an out-of-memory situation
423          * or if another thread unmaps the region containing the context.
424          * We kill the task with a SIGSEGV in this situation.
425          */
426         if (do_setcontext(new_ctx, regs, 0))
427                 do_exit(SIGSEGV);
428         sigreturn_exit(regs);
429         /* doesn't actually return back to here */
430         return 0;
431 }
432
433 int sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
434                      struct pt_regs *regs)
435 {
436         struct rt_sigframe __user *rt_sf;
437
438         /* Always make any pending restarted system calls return -EINTR */
439         current_thread_info()->restart_block.fn = do_no_restart_syscall;
440
441         rt_sf = (struct rt_sigframe __user *)
442                 (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
443         if (verify_area(VERIFY_READ, rt_sf, sizeof(struct rt_sigframe)))
444                 goto bad;
445         if (do_setcontext(&rt_sf->uc, regs, 1))
446                 goto bad;
447
448         /*
449          * It's not clear whether or why it is desirable to save the
450          * sigaltstack setting on signal delivery and restore it on
451          * signal return.  But other architectures do this and we have
452          * always done it up until now so it is probably better not to
453          * change it.  -- paulus
454          */
455         do_sigaltstack(&rt_sf->uc.uc_stack, NULL, regs->gpr[1]);
456
457         sigreturn_exit(regs);           /* doesn't return here */
458         return 0;
459
460  bad:
461         force_sig(SIGSEGV, current);
462         return 0;
463 }
464
465 /*
466  * OK, we're invoking a handler
467  */
468 static void
469 handle_signal(unsigned long sig, struct k_sigaction *ka,
470               siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
471               unsigned long newsp)
472 {
473         struct sigcontext __user *sc;
474         struct sigregs __user *frame;
475         unsigned long origsp = newsp;
476
477         /* Set up Signal Frame */
478         newsp -= sizeof(struct sigregs);
479         frame = (struct sigregs __user *) newsp;
480
481         /* Put a sigcontext on the stack */
482         newsp -= sizeof(*sc);
483         sc = (struct sigcontext __user *) newsp;
484
485         /* create a stack frame for the caller of the handler */
486         newsp -= __SIGNAL_FRAMESIZE;
487
488         if (verify_area(VERIFY_WRITE, (void *) newsp, origsp - newsp))
489                 goto badframe;
490
491 #if _NSIG != 64
492 #error "Please adjust handle_signal()"
493 #endif
494         if (__put_user((unsigned long) ka->sa.sa_handler, &sc->handler)
495             || __put_user(oldset->sig[0], &sc->oldmask)
496             || __put_user(oldset->sig[1], &sc->_unused[3])
497             || __put_user((struct pt_regs *)frame, &sc->regs)
498             || __put_user(sig, &sc->signal))
499                 goto badframe;
500
501         if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
502                 goto badframe;
503
504         if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
505                 goto badframe;
506         regs->gpr[1] = newsp;
507         regs->gpr[3] = sig;
508         regs->gpr[4] = (unsigned long) sc;
509         regs->nip = (unsigned long) ka->sa.sa_handler;
510         regs->link = (unsigned long) frame->mctx.tramp;
511         regs->trap = 0;
512
513         return;
514
515 badframe:
516 #ifdef DEBUG_SIG
517         printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
518                regs, frame, newsp);
519 #endif
520         if (sig == SIGSEGV)
521                 ka->sa.sa_handler = SIG_DFL;
522         force_sig(SIGSEGV, current);
523 }
524
525 /*
526  * Do a signal return; undo the signal stack.
527  */
528 int sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
529                   struct pt_regs *regs)
530 {
531         struct sigcontext __user *sc;
532         struct sigcontext sigctx;
533         struct mcontext __user *sr;
534         sigset_t set;
535
536         /* Always make any pending restarted system calls return -EINTR */
537         current_thread_info()->restart_block.fn = do_no_restart_syscall;
538
539         sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
540         if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
541                 goto badframe;
542
543         set.sig[0] = sigctx.oldmask;
544         set.sig[1] = sigctx._unused[3];
545         restore_sigmask(&set);
546
547         sr = (struct mcontext *) sigctx.regs;
548         if (verify_area(VERIFY_READ, sr, sizeof(*sr))
549             || restore_user_regs(regs, sr, 1))
550                 goto badframe;
551
552         sigreturn_exit(regs);           /* doesn't return */
553         return 0;
554
555 badframe:
556         force_sig(SIGSEGV, current);
557         return 0;
558 }
559
560 /*
561  * Note that 'init' is a special process: it doesn't get signals it doesn't
562  * want to handle. Thus you cannot kill init even with a SIGKILL even by
563  * mistake.
564  */
565 int do_signal(sigset_t *oldset, struct pt_regs *regs)
566 {
567         siginfo_t info;
568         struct k_sigaction *ka;
569         unsigned long frame, newsp;
570         int signr, ret;
571
572         if (!oldset)
573                 oldset = &current->blocked;
574
575         newsp = frame = 0;
576
577         signr = get_signal_to_deliver(&info, regs, NULL);
578
579         ka = (signr == 0)? NULL: &current->sighand->action[signr-1];
580
581         if (TRAP(regs) == 0x0C00                /* System Call! */
582             && regs->ccr & 0x10000000           /* error signalled */
583             && ((ret = regs->gpr[3]) == ERESTARTSYS
584                 || ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
585                 || ret == ERESTART_RESTARTBLOCK)) {
586
587                 if (signr > 0
588                     && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
589                         || (ret == ERESTARTSYS
590                             && !(ka->sa.sa_flags & SA_RESTART)))) {
591                         /* make the system call return an EINTR error */
592                         regs->result = -EINTR;
593                         regs->gpr[3] = EINTR;
594                         /* note that the cr0.SO bit is already set */
595                 } else {
596                         regs->nip -= 4; /* Back up & retry system call */
597                         regs->result = 0;
598                         regs->trap = 0;
599                         if (ret == ERESTART_RESTARTBLOCK)
600                                 regs->gpr[0] = __NR_restart_syscall;
601                         else
602                                 regs->gpr[3] = regs->orig_gpr3;
603                 }
604         }
605
606         if (signr == 0)
607                 return 0;               /* no signals delivered */
608
609         if ((ka->sa.sa_flags & SA_ONSTACK) && current->sas_ss_size
610             && !on_sig_stack(regs->gpr[1]))
611                 newsp = current->sas_ss_sp + current->sas_ss_size;
612         else
613                 newsp = regs->gpr[1];
614         newsp &= ~0xfUL;
615
616         /* Whee!  Actually deliver the signal.  */
617         if (ka->sa.sa_flags & SA_SIGINFO)
618                 handle_rt_signal(signr, ka, &info, oldset, regs, newsp);
619         else
620                 handle_signal(signr, ka, &info, oldset, regs, newsp);
621
622         if (ka->sa.sa_flags & SA_ONESHOT)
623                 ka->sa.sa_handler = SIG_DFL;
624
625         if (!(ka->sa.sa_flags & SA_NODEFER)) {
626                 spin_lock_irq(&current->sighand->siglock);
627                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
628                 sigaddset(&current->blocked, signr);
629                 recalc_sigpending();
630                 spin_unlock_irq(&current->sighand->siglock);
631         }
632
633         return 1;
634 }
635