vserver 1.9.5.x5
[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/spe registers if the process has used
187  * altivec/spe instructions at some point.
188  */
189 static int
190 save_user_regs(struct pt_regs *regs, struct mcontext __user *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 #ifdef CONFIG_SPE
202         if (current->thread.used_spe && (regs->msr & MSR_SPE))
203                 giveup_spe(current);
204 #endif /* CONFIG_ALTIVEC */
205         preempt_enable();
206
207         if (__copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE)
208             || __copy_to_user(&frame->mc_fregs, current->thread.fpr,
209                               ELF_NFPREG * sizeof(double)))
210                 return 1;
211
212         current->thread.fpscr = 0;      /* turn off all fp exceptions */
213
214 #ifdef CONFIG_ALTIVEC
215         /* save altivec registers */
216         if (current->thread.used_vr) {
217                 if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
218                                    ELF_NVRREG * sizeof(vector128)))
219                         return 1;
220                 /* set MSR_VEC in the saved MSR value to indicate that
221                    frame->mc_vregs contains valid data */
222                 if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
223                         return 1;
224         }
225         /* else assert((regs->msr & MSR_VEC) == 0) */
226
227         /* We always copy to/from vrsave, it's 0 if we don't have or don't
228          * use altivec. Since VSCR only contains 32 bits saved in the least
229          * significant bits of a vector, we "cheat" and stuff VRSAVE in the
230          * most significant bits of that same vector. --BenH
231          */
232         if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
233                 return 1;
234 #endif /* CONFIG_ALTIVEC */
235
236 #ifdef CONFIG_SPE
237         /* save spe registers */
238         if (current->thread.used_spe) {
239                 if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
240                                    ELF_NEVRREG * sizeof(u32)))
241                         return 1;
242                 /* set MSR_SPE in the saved MSR value to indicate that
243                    frame->mc_vregs contains valid data */
244                 if (__put_user(regs->msr | MSR_SPE, &frame->mc_gregs[PT_MSR]))
245                         return 1;
246         }
247         /* else assert((regs->msr & MSR_SPE) == 0) */
248
249         /* We always copy to/from spefscr */
250         if (__put_user(current->thread.spefscr, (u32 *)&frame->mc_vregs + ELF_NEVRREG))
251                 return 1;
252 #endif /* CONFIG_SPE */
253
254         if (sigret) {
255                 /* Set up the sigreturn trampoline: li r0,sigret; sc */
256                 if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
257                     || __put_user(0x44000002UL, &frame->tramp[1]))
258                         return 1;
259                 flush_icache_range((unsigned long) &frame->tramp[0],
260                                    (unsigned long) &frame->tramp[2]);
261         }
262
263         return 0;
264 }
265
266 /*
267  * Restore the current user register values from the user stack,
268  * (except for MSR).
269  */
270 static int
271 restore_user_regs(struct pt_regs *regs, struct mcontext __user *sr, int sig)
272 {
273         unsigned long save_r2 = 0;
274 #if defined(CONFIG_ALTIVEC) || defined(CONFIG_SPE)
275         unsigned long msr;
276 #endif
277
278         /* backup/restore the TLS as we don't want it to be modified */
279         if (!sig)
280                 save_r2 = regs->gpr[2];
281         /* copy up to but not including MSR */
282         if (__copy_from_user(regs, &sr->mc_gregs, PT_MSR * sizeof(elf_greg_t)))
283                 return 1;
284         /* copy from orig_r3 (the word after the MSR) up to the end */
285         if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
286                              GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
287                 return 1;
288         if (!sig)
289                 regs->gpr[2] = save_r2;
290
291         /* force the process to reload the FP registers from
292            current->thread when it next does FP instructions */
293         regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
294         if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
295                              sizeof(sr->mc_fregs)))
296                 return 1;
297
298 #ifdef CONFIG_ALTIVEC
299         /* force the process to reload the altivec registers from
300            current->thread when it next does altivec instructions */
301         regs->msr &= ~MSR_VEC;
302         if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_VEC) != 0) {
303                 /* restore altivec registers from the stack */
304                 if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
305                                      sizeof(sr->mc_vregs)))
306                         return 1;
307         } else if (current->thread.used_vr)
308                 memset(&current->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
309
310         /* Always get VRSAVE back */
311         if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
312                 return 1;
313 #endif /* CONFIG_ALTIVEC */
314
315 #ifdef CONFIG_SPE
316         /* force the process to reload the spe registers from
317            current->thread when it next does spe instructions */
318         regs->msr &= ~MSR_SPE;
319         if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_SPE) != 0) {
320                 /* restore spe registers from the stack */
321                 if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
322                                      ELF_NEVRREG * sizeof(u32)))
323                         return 1;
324         } else if (current->thread.used_spe)
325                 memset(&current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
326
327         /* Always get SPEFSCR back */
328         if (__get_user(current->thread.spefscr, (u32 *)&sr->mc_vregs + ELF_NEVRREG))
329                 return 1;
330 #endif /* CONFIG_SPE */
331
332 #ifndef CONFIG_SMP
333         preempt_disable();
334         if (last_task_used_math == current)
335                 last_task_used_math = NULL;
336         if (last_task_used_altivec == current)
337                 last_task_used_altivec = NULL;
338         if (last_task_used_spe == current)
339                 last_task_used_spe = NULL;
340         preempt_enable();
341 #endif
342         return 0;
343 }
344
345 /*
346  * Restore the user process's signal mask
347  */
348 static void
349 restore_sigmask(sigset_t *set)
350 {
351         sigdelsetmask(set, ~_BLOCKABLE);
352         spin_lock_irq(&current->sighand->siglock);
353         current->blocked = *set;
354         recalc_sigpending();
355         spin_unlock_irq(&current->sighand->siglock);
356 }
357
358 /*
359  * Set up a signal frame for a "real-time" signal handler
360  * (one which gets siginfo).
361  */
362 static void
363 handle_rt_signal(unsigned long sig, struct k_sigaction *ka,
364                  siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
365                  unsigned long newsp)
366 {
367         struct rt_sigframe __user *rt_sf;
368         struct mcontext __user *frame;
369         unsigned long origsp = newsp;
370
371         /* Set up Signal Frame */
372         /* Put a Real Time Context onto stack */
373         newsp -= sizeof(*rt_sf);
374         rt_sf = (struct rt_sigframe __user *) newsp;
375
376         /* create a stack frame for the caller of the handler */
377         newsp -= __SIGNAL_FRAMESIZE + 16;
378
379         if (verify_area(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
380                 goto badframe;
381
382         /* Put the siginfo & fill in most of the ucontext */
383         if (copy_siginfo_to_user(&rt_sf->info, info)
384             || __put_user(0, &rt_sf->uc.uc_flags)
385             || __put_user(0, &rt_sf->uc.uc_link)
386             || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
387             || __put_user(sas_ss_flags(regs->gpr[1]),
388                           &rt_sf->uc.uc_stack.ss_flags)
389             || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
390             || __put_user(&rt_sf->uc.uc_mcontext, &rt_sf->uc.uc_regs)
391             || __copy_to_user(&rt_sf->uc.uc_sigmask, oldset, sizeof(*oldset)))
392                 goto badframe;
393
394         /* Save user registers on the stack */
395         frame = &rt_sf->uc.uc_mcontext;
396         if (save_user_regs(regs, frame, __NR_rt_sigreturn))
397                 goto badframe;
398
399         if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
400                 goto badframe;
401         regs->gpr[1] = newsp;
402         regs->gpr[3] = sig;
403         regs->gpr[4] = (unsigned long) &rt_sf->info;
404         regs->gpr[5] = (unsigned long) &rt_sf->uc;
405         regs->gpr[6] = (unsigned long) rt_sf;
406         regs->nip = (unsigned long) ka->sa.sa_handler;
407         regs->link = (unsigned long) frame->tramp;
408         regs->trap = 0;
409
410         return;
411
412 badframe:
413 #ifdef DEBUG_SIG
414         printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
415                regs, frame, newsp);
416 #endif
417         force_sigsegv(sig, current);
418 }
419
420 static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
421 {
422         sigset_t set;
423         struct mcontext __user *mcp;
424
425         if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(set))
426             || __get_user(mcp, &ucp->uc_regs))
427                 return -EFAULT;
428         restore_sigmask(&set);
429         if (restore_user_regs(regs, mcp, sig))
430                 return -EFAULT;
431
432         return 0;
433 }
434
435 int sys_swapcontext(struct ucontext __user *old_ctx,
436                     struct ucontext __user *new_ctx,
437                     int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
438 {
439         unsigned char tmp;
440
441         /* Context size is for future use. Right now, we only make sure
442          * we are passed something we understand
443          */
444         if (ctx_size < sizeof(struct ucontext))
445                 return -EINVAL;
446
447         if (old_ctx != NULL) {
448                 if (verify_area(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
449                     || save_user_regs(regs, &old_ctx->uc_mcontext, 0)
450                     || __copy_to_user(&old_ctx->uc_sigmask,
451                                       &current->blocked, sizeof(sigset_t))
452                     || __put_user(&old_ctx->uc_mcontext, &old_ctx->uc_regs))
453                         return -EFAULT;
454         }
455         if (new_ctx == NULL)
456                 return 0;
457         if (verify_area(VERIFY_READ, new_ctx, sizeof(*new_ctx))
458             || __get_user(tmp, (u8 __user *) new_ctx)
459             || __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
460                 return -EFAULT;
461
462         /*
463          * If we get a fault copying the context into the kernel's
464          * image of the user's registers, we can't just return -EFAULT
465          * because the user's registers will be corrupted.  For instance
466          * the NIP value may have been updated but not some of the
467          * other registers.  Given that we have done the verify_area
468          * and successfully read the first and last bytes of the region
469          * above, this should only happen in an out-of-memory situation
470          * or if another thread unmaps the region containing the context.
471          * We kill the task with a SIGSEGV in this situation.
472          */
473         if (do_setcontext(new_ctx, regs, 0))
474                 do_exit(SIGSEGV);
475         sigreturn_exit(regs);
476         /* doesn't actually return back to here */
477         return 0;
478 }
479
480 int sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
481                      struct pt_regs *regs)
482 {
483         struct rt_sigframe __user *rt_sf;
484
485         /* Always make any pending restarted system calls return -EINTR */
486         current_thread_info()->restart_block.fn = do_no_restart_syscall;
487
488         rt_sf = (struct rt_sigframe __user *)
489                 (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
490         if (verify_area(VERIFY_READ, rt_sf, sizeof(struct rt_sigframe)))
491                 goto bad;
492         if (do_setcontext(&rt_sf->uc, regs, 1))
493                 goto bad;
494
495         /*
496          * It's not clear whether or why it is desirable to save the
497          * sigaltstack setting on signal delivery and restore it on
498          * signal return.  But other architectures do this and we have
499          * always done it up until now so it is probably better not to
500          * change it.  -- paulus
501          */
502         do_sigaltstack(&rt_sf->uc.uc_stack, NULL, regs->gpr[1]);
503
504         sigreturn_exit(regs);           /* doesn't return here */
505         return 0;
506
507  bad:
508         force_sig(SIGSEGV, current);
509         return 0;
510 }
511
512 int sys_debug_setcontext(struct ucontext __user *ctx,
513                          int ndbg, struct sig_dbg_op *dbg,
514                          int r6, int r7, int r8,
515                          struct pt_regs *regs)
516 {
517         struct sig_dbg_op op;
518         int i;
519         unsigned long new_msr = regs->msr;
520 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
521         unsigned long new_dbcr0 = current->thread.dbcr0;
522 #endif
523
524         for (i=0; i<ndbg; i++) {
525                 if (__copy_from_user(&op, dbg, sizeof(op)))
526                         return -EFAULT;
527                 switch (op.dbg_type) {
528                 case SIG_DBG_SINGLE_STEPPING:
529 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
530                         if (op.dbg_value) {
531                                 new_msr |= MSR_DE;
532                                 new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
533                         } else {
534                                 new_msr &= ~MSR_DE;
535                                 new_dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);
536                         }
537 #else
538                         if (op.dbg_value)
539                                 new_msr |= MSR_SE;
540                         else
541                                 new_msr &= ~MSR_SE;
542 #endif
543                         break;
544                 case SIG_DBG_BRANCH_TRACING:
545 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
546                         return -EINVAL;
547 #else
548                         if (op.dbg_value)
549                                 new_msr |= MSR_BE;
550                         else
551                                 new_msr &= ~MSR_BE;
552 #endif
553                         break;
554
555                 default:
556                         return -EINVAL;
557                 }
558         }
559
560         /* We wait until here to actually install the values in the
561            registers so if we fail in the above loop, it will not
562            affect the contents of these registers.  After this point,
563            failure is a problem, anyway, and it's very unlikely unless
564            the user is really doing something wrong. */
565         regs->msr = new_msr;
566 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
567         current->thread.dbcr0 = new_dbcr0;
568 #endif
569
570         /*
571          * If we get a fault copying the context into the kernel's
572          * image of the user's registers, we can't just return -EFAULT
573          * because the user's registers will be corrupted.  For instance
574          * the NIP value may have been updated but not some of the
575          * other registers.  Given that we have done the verify_area
576          * and successfully read the first and last bytes of the region
577          * above, this should only happen in an out-of-memory situation
578          * or if another thread unmaps the region containing the context.
579          * We kill the task with a SIGSEGV in this situation.
580          */
581         if (do_setcontext(ctx, regs, 1)) {
582                 force_sig(SIGSEGV, current);
583                 goto out;
584         }
585
586         /*
587          * It's not clear whether or why it is desirable to save the
588          * sigaltstack setting on signal delivery and restore it on
589          * signal return.  But other architectures do this and we have
590          * always done it up until now so it is probably better not to
591          * change it.  -- paulus
592          */
593         do_sigaltstack(&ctx->uc_stack, NULL, regs->gpr[1]);
594
595         sigreturn_exit(regs);
596         /* doesn't actually return back to here */
597
598  out:
599         return 0;
600 }
601
602 /*
603  * OK, we're invoking a handler
604  */
605 static void
606 handle_signal(unsigned long sig, struct k_sigaction *ka,
607               siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
608               unsigned long newsp)
609 {
610         struct sigcontext __user *sc;
611         struct sigregs __user *frame;
612         unsigned long origsp = newsp;
613
614         /* Set up Signal Frame */
615         newsp -= sizeof(struct sigregs);
616         frame = (struct sigregs __user *) newsp;
617
618         /* Put a sigcontext on the stack */
619         newsp -= sizeof(*sc);
620         sc = (struct sigcontext __user *) newsp;
621
622         /* create a stack frame for the caller of the handler */
623         newsp -= __SIGNAL_FRAMESIZE;
624
625         if (verify_area(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
626                 goto badframe;
627
628 #if _NSIG != 64
629 #error "Please adjust handle_signal()"
630 #endif
631         if (__put_user((unsigned long) ka->sa.sa_handler, &sc->handler)
632             || __put_user(oldset->sig[0], &sc->oldmask)
633             || __put_user(oldset->sig[1], &sc->_unused[3])
634             || __put_user((struct pt_regs *)frame, &sc->regs)
635             || __put_user(sig, &sc->signal))
636                 goto badframe;
637
638         if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
639                 goto badframe;
640
641         if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
642                 goto badframe;
643         regs->gpr[1] = newsp;
644         regs->gpr[3] = sig;
645         regs->gpr[4] = (unsigned long) sc;
646         regs->nip = (unsigned long) ka->sa.sa_handler;
647         regs->link = (unsigned long) frame->mctx.tramp;
648         regs->trap = 0;
649
650         return;
651
652 badframe:
653 #ifdef DEBUG_SIG
654         printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
655                regs, frame, newsp);
656 #endif
657         force_sigsegv(sig, current);
658 }
659
660 /*
661  * Do a signal return; undo the signal stack.
662  */
663 int sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
664                   struct pt_regs *regs)
665 {
666         struct sigcontext __user *sc;
667         struct sigcontext sigctx;
668         struct mcontext __user *sr;
669         sigset_t set;
670
671         /* Always make any pending restarted system calls return -EINTR */
672         current_thread_info()->restart_block.fn = do_no_restart_syscall;
673
674         sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
675         if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
676                 goto badframe;
677
678         set.sig[0] = sigctx.oldmask;
679         set.sig[1] = sigctx._unused[3];
680         restore_sigmask(&set);
681
682         sr = (struct mcontext __user *) sigctx.regs;
683         if (verify_area(VERIFY_READ, sr, sizeof(*sr))
684             || restore_user_regs(regs, sr, 1))
685                 goto badframe;
686
687         sigreturn_exit(regs);           /* doesn't return */
688         return 0;
689
690 badframe:
691         force_sig(SIGSEGV, current);
692         return 0;
693 }
694
695 /*
696  * Note that 'init' is a special process: it doesn't get signals it doesn't
697  * want to handle. Thus you cannot kill init even with a SIGKILL even by
698  * mistake.
699  */
700 int do_signal(sigset_t *oldset, struct pt_regs *regs)
701 {
702         siginfo_t info;
703         struct k_sigaction ka;
704         unsigned long frame, newsp;
705         int signr, ret;
706
707         if (!oldset)
708                 oldset = &current->blocked;
709
710         newsp = frame = 0;
711
712         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
713
714         if (TRAP(regs) == 0x0C00                /* System Call! */
715             && regs->ccr & 0x10000000           /* error signalled */
716             && ((ret = regs->gpr[3]) == ERESTARTSYS
717                 || ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
718                 || ret == ERESTART_RESTARTBLOCK)) {
719
720                 if (signr > 0
721                     && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
722                         || (ret == ERESTARTSYS
723                             && !(ka.sa.sa_flags & SA_RESTART)))) {
724                         /* make the system call return an EINTR error */
725                         regs->result = -EINTR;
726                         regs->gpr[3] = EINTR;
727                         /* note that the cr0.SO bit is already set */
728                 } else {
729                         regs->nip -= 4; /* Back up & retry system call */
730                         regs->result = 0;
731                         regs->trap = 0;
732                         if (ret == ERESTART_RESTARTBLOCK)
733                                 regs->gpr[0] = __NR_restart_syscall;
734                         else
735                                 regs->gpr[3] = regs->orig_gpr3;
736                 }
737         }
738
739         if (signr == 0)
740                 return 0;               /* no signals delivered */
741
742         if ((ka.sa.sa_flags & SA_ONSTACK) && current->sas_ss_size
743             && !on_sig_stack(regs->gpr[1]))
744                 newsp = current->sas_ss_sp + current->sas_ss_size;
745         else
746                 newsp = regs->gpr[1];
747         newsp &= ~0xfUL;
748
749         /* Whee!  Actually deliver the signal.  */
750         if (ka.sa.sa_flags & SA_SIGINFO)
751                 handle_rt_signal(signr, &ka, &info, oldset, regs, newsp);
752         else
753                 handle_signal(signr, &ka, &info, oldset, regs, newsp);
754
755         if (!(ka.sa.sa_flags & SA_NODEFER)) {
756                 spin_lock_irq(&current->sighand->siglock);
757                 sigorsets(&current->blocked,&current->blocked,&ka.sa.sa_mask);
758                 sigaddset(&current->blocked, signr);
759                 recalc_sigpending();
760                 spin_unlock_irq(&current->sighand->siglock);
761         }
762
763         return 1;
764 }
765