]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - arch/ppc64/kernel/signal32.c
3c2fa5c284c04ef223c468c2df53f6c7676dc20a
[linux-2.6.git] / arch / ppc64 / kernel / signal32.c
1 /*
2  * signal32.c: Support 32bit signal syscalls.
3  *
4  * Copyright (C) 2001 IBM
5  * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
7  *
8  * These routines maintain argument size conversion between 32bit and 64bit
9  * environment.
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/config.h>
18 #include <linux/sched.h>
19 #include <linux/mm.h> 
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/kernel.h>
23 #include <linux/signal.h>
24 #include <linux/syscalls.h>
25 #include <linux/errno.h>
26 #include <linux/elf.h>
27 #include <linux/compat.h>
28 #include <linux/ptrace.h>
29 #include <asm/ppc32.h>
30 #include <asm/uaccess.h>
31 #include <asm/ppcdebug.h>
32 #include <asm/unistd.h>
33 #include <asm/cacheflush.h>
34 #include <asm/vdso.h>
35
36 #define DEBUG_SIG 0
37
38 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
39
40 #define GP_REGS_SIZE32  min(sizeof(elf_gregset_t32), sizeof(struct pt_regs32))
41
42 /*
43  * When we have signals to deliver, we set up on the
44  * user stack, going down from the original stack pointer:
45  *      a sigregs32 struct
46  *      a sigcontext32 struct
47  *      a gap of __SIGNAL_FRAMESIZE32 bytes
48  *
49  * Each of these things must be a multiple of 16 bytes in size.
50  *
51  */
52 struct sigregs32 {
53         struct mcontext32       mctx;           /* all the register values */
54         /*
55          * Programs using the rs6000/xcoff abi can save up to 19 gp
56          * regs and 18 fp regs below sp before decrementing it.
57          */
58         int                     abigap[56];
59 };
60
61 /* We use the mc_pad field for the signal return trampoline. */
62 #define tramp   mc_pad
63
64 /*
65  *  When we have rt signals to deliver, we set up on the
66  *  user stack, going down from the original stack pointer:
67  *      one rt_sigframe32 struct (siginfo + ucontext + ABI gap)
68  *      a gap of __SIGNAL_FRAMESIZE32+16 bytes
69  *  (the +16 is to get the siginfo and ucontext32 in the same
70  *  positions as in older kernels).
71  *
72  *  Each of these things must be a multiple of 16 bytes in size.
73  *
74  */
75 struct rt_sigframe32 {
76         compat_siginfo_t        info;
77         struct ucontext32       uc;
78         /*
79          * Programs using the rs6000/xcoff abi can save up to 19 gp
80          * regs and 18 fp regs below sp before decrementing it.
81          */
82         int                     abigap[56];
83 };
84
85
86 /*
87  * Common utility functions used by signal and context support
88  *
89  */
90
91 /*
92  * Restore the user process's signal mask
93  * (implemented in signal.c)
94  */
95 extern void restore_sigmask(sigset_t *set);
96
97 /*
98  * Functions for flipping sigsets (thanks to brain dead generic
99  * implementation that makes things simple for little endian only
100  */
101 static inline void compat_from_sigset(compat_sigset_t *compat, sigset_t *set)
102 {
103         switch (_NSIG_WORDS) {
104         case 4: compat->sig[5] = set->sig[3] & 0xffffffffull ;
105                 compat->sig[7] = set->sig[3] >> 32; 
106         case 3: compat->sig[4] = set->sig[2] & 0xffffffffull ;
107                 compat->sig[5] = set->sig[2] >> 32; 
108         case 2: compat->sig[2] = set->sig[1] & 0xffffffffull ;
109                 compat->sig[3] = set->sig[1] >> 32; 
110         case 1: compat->sig[0] = set->sig[0] & 0xffffffffull ;
111                 compat->sig[1] = set->sig[0] >> 32; 
112         }
113 }
114
115 static inline void sigset_from_compat(sigset_t *set, compat_sigset_t *compat)
116 {
117         switch (_NSIG_WORDS) {
118         case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32);
119         case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32);
120         case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32);
121         case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32);
122         }
123 }
124
125
126 /*
127  * Save the current user registers on the user stack.
128  * We only save the altivec registers if the process has used
129  * altivec instructions at some point.
130  */
131 static int save_user_regs(struct pt_regs *regs, struct mcontext32 __user *frame, int sigret)
132 {
133         elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
134         int i, err = 0;
135
136         /* Make sure floating point registers are stored in regs */
137         flush_fp_to_thread(current);
138
139         /* save general and floating-point registers */
140         for (i = 0; i <= PT_RESULT; i ++)
141                 err |= __put_user((unsigned int)gregs[i], &frame->mc_gregs[i]);
142         err |= __copy_to_user(&frame->mc_fregs, current->thread.fpr,
143                               ELF_NFPREG * sizeof(double));
144         if (err)
145                 return 1;
146
147         current->thread.fpscr = 0;      /* turn off all fp exceptions */
148
149 #ifdef CONFIG_ALTIVEC
150         /* save altivec registers */
151         if (current->thread.used_vr) {
152                 flush_altivec_to_thread(current);
153                 if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
154                                    ELF_NVRREG32 * sizeof(vector128)))
155                         return 1;
156                 /* set MSR_VEC in the saved MSR value to indicate that
157                    frame->mc_vregs contains valid data */
158                 if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
159                         return 1;
160         }
161         /* else assert((regs->msr & MSR_VEC) == 0) */
162
163         /* We always copy to/from vrsave, it's 0 if we don't have or don't
164          * use altivec. Since VSCR only contains 32 bits saved in the least
165          * significant bits of a vector, we "cheat" and stuff VRSAVE in the
166          * most significant bits of that same vector. --BenH
167          */
168         if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
169                 return 1;
170 #endif /* CONFIG_ALTIVEC */
171
172         if (sigret) {
173                 /* Set up the sigreturn trampoline: li r0,sigret; sc */
174                 if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
175                     || __put_user(0x44000002UL, &frame->tramp[1]))
176                         return 1;
177                 flush_icache_range((unsigned long) &frame->tramp[0],
178                                    (unsigned long) &frame->tramp[2]);
179         }
180
181         return 0;
182 }
183
184 /*
185  * Restore the current user register values from the user stack,
186  * (except for MSR).
187  */
188 static long restore_user_regs(struct pt_regs *regs,
189                               struct mcontext32 __user *sr, int sig)
190 {
191         elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
192         int i;
193         long err = 0;
194         unsigned int save_r2 = 0;
195 #ifdef CONFIG_ALTIVEC
196         unsigned long msr;
197 #endif
198
199         /*
200          * restore general registers but not including MSR or SOFTE. Also
201          * take care of keeping r2 (TLS) intact if not a signal
202          */
203         if (!sig)
204                 save_r2 = (unsigned int)regs->gpr[2];
205         for (i = 0; i <= PT_RESULT; i++) {
206                 if ((i == PT_MSR) || (i == PT_SOFTE))
207                         continue;
208                 err |= __get_user(gregs[i], &sr->mc_gregs[i]);
209         }
210         if (!sig)
211                 regs->gpr[2] = (unsigned long) save_r2;
212         if (err)
213                 return 1;
214
215         /* force the process to reload the FP registers from
216            current->thread when it next does FP instructions */
217         regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
218         if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
219                              sizeof(sr->mc_fregs)))
220                 return 1;
221
222 #ifdef CONFIG_ALTIVEC
223         /* force the process to reload the altivec registers from
224            current->thread when it next does altivec instructions */
225         regs->msr &= ~MSR_VEC;
226         if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_VEC) != 0) {
227                 /* restore altivec registers from the stack */
228                 if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
229                                      sizeof(sr->mc_vregs)))
230                         return 1;
231         } else if (current->thread.used_vr)
232                 memset(current->thread.vr, 0, ELF_NVRREG32 * sizeof(vector128));
233
234         /* Always get VRSAVE back */
235         if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
236                 return 1;
237 #endif /* CONFIG_ALTIVEC */
238
239 #ifndef CONFIG_SMP
240         preempt_disable();
241         if (last_task_used_math == current)
242                 last_task_used_math = NULL;
243         if (last_task_used_altivec == current)
244                 last_task_used_altivec = NULL;
245         preempt_enable();
246 #endif
247         return 0;
248 }
249
250
251 /*
252  *  Start of nonRT signal support
253  *
254  *     sigset_t is 32 bits for non-rt signals
255  *
256  *  System Calls
257  *       sigaction                sys32_sigaction
258  *       sigreturn                sys32_sigreturn
259  *
260  *  Note sigsuspend has no special 32 bit routine - uses the 64 bit routine
261  *
262  *  Other routines
263  *        setup_frame32
264  */
265
266 /*
267  * Atomically swap in the new signal mask, and wait for a signal.
268  */
269 long sys32_sigsuspend(old_sigset_t mask, int p2, int p3, int p4, int p6, int p7,
270                struct pt_regs *regs)
271 {
272         sigset_t saveset;
273
274         mask &= _BLOCKABLE;
275         spin_lock_irq(&current->sighand->siglock);
276         saveset = current->blocked;
277         siginitset(&current->blocked, mask);
278         recalc_sigpending();
279         spin_unlock_irq(&current->sighand->siglock);
280
281         regs->result = -EINTR;
282         regs->gpr[3] = EINTR;
283         regs->ccr |= 0x10000000;
284         while (1) {
285                 current->state = TASK_INTERRUPTIBLE;
286                 schedule();
287                 if (do_signal32(&saveset, regs))
288                         /*
289                          * Returning 0 means we return to userspace via
290                          * ret_from_except and thus restore all user
291                          * registers from *regs.  This is what we need
292                          * to do when a signal has been delivered.
293                          */
294                         return 0;
295         }
296 }
297
298 long sys32_sigaction(int sig, struct old_sigaction32 __user *act,
299                 struct old_sigaction32 __user *oact)
300 {
301         struct k_sigaction new_ka, old_ka;
302         int ret;
303         
304         if (sig < 0)
305                 sig = -sig;
306
307         if (act) {
308                 compat_old_sigset_t mask;
309                 compat_uptr_t handler, restorer;
310
311                 if (get_user(handler, &act->sa_handler) ||
312                     __get_user(restorer, &act->sa_restorer) ||
313                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
314                     __get_user(mask, &act->sa_mask))
315                         return -EFAULT;
316                 new_ka.sa.sa_handler = compat_ptr(handler);
317                 new_ka.sa.sa_restorer = compat_ptr(restorer);
318                 siginitset(&new_ka.sa.sa_mask, mask);
319         }
320
321         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
322         if (!ret && oact) {
323                 if (put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
324                     __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
325                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
326                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
327                         return -EFAULT;
328         }
329
330         return ret;
331 }
332
333
334
335 /*
336  *  Start of RT signal support
337  *
338  *     sigset_t is 64 bits for rt signals
339  *
340  *  System Calls
341  *       sigaction                sys32_rt_sigaction
342  *       sigpending               sys32_rt_sigpending
343  *       sigprocmask              sys32_rt_sigprocmask
344  *       sigreturn                sys32_rt_sigreturn
345  *       sigqueueinfo             sys32_rt_sigqueueinfo
346  *       sigsuspend               sys32_rt_sigsuspend
347  *
348  *  Other routines
349  *        setup_rt_frame32
350  *        copy_siginfo_to_user32
351  *        siginfo32to64
352  */
353
354
355 long sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
356                 struct sigaction32 __user *oact, size_t sigsetsize)
357 {
358         struct k_sigaction new_ka, old_ka;
359         int ret;
360         compat_sigset_t set32;
361
362         /* XXX: Don't preclude handling different sized sigset_t's.  */
363         if (sigsetsize != sizeof(compat_sigset_t))
364                 return -EINVAL;
365
366         if (act) {
367                 compat_uptr_t handler;
368
369                 ret = get_user(handler, &act->sa_handler);
370                 new_ka.sa.sa_handler = compat_ptr(handler);
371                 ret |= __copy_from_user(&set32, &act->sa_mask,
372                                         sizeof(compat_sigset_t));
373                 sigset_from_compat(&new_ka.sa.sa_mask, &set32);
374                 ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
375                 if (ret)
376                         return -EFAULT;
377         }
378
379         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
380         if (!ret && oact) {
381                 compat_from_sigset(&set32, &old_ka.sa.sa_mask);
382                 ret = put_user((long)old_ka.sa.sa_handler, &oact->sa_handler);
383                 ret |= __copy_to_user(&oact->sa_mask, &set32,
384                                       sizeof(compat_sigset_t));
385                 ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
386         }
387         return ret;
388 }
389
390 /*
391  * Note: it is necessary to treat how as an unsigned int, with the
392  * corresponding cast to a signed int to insure that the proper
393  * conversion (sign extension) between the register representation
394  * of a signed int (msr in 32-bit mode) and the register representation
395  * of a signed int (msr in 64-bit mode) is performed.
396  */
397 long sys32_rt_sigprocmask(u32 how, compat_sigset_t __user *set,
398                 compat_sigset_t __user *oset, size_t sigsetsize)
399 {
400         sigset_t s;
401         sigset_t __user *up;
402         compat_sigset_t s32;
403         int ret;
404         mm_segment_t old_fs = get_fs();
405
406         if (set) {
407                 if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
408                         return -EFAULT;    
409                 sigset_from_compat(&s, &s32);
410         }
411         
412         set_fs(KERNEL_DS);
413         /* This is valid because of the set_fs() */
414         up = (sigset_t __user *) &s;
415         ret = sys_rt_sigprocmask((int)how, set ? up : NULL, oset ? up : NULL,
416                                  sigsetsize); 
417         set_fs(old_fs);
418         if (ret)
419                 return ret;
420         if (oset) {
421                 compat_from_sigset(&s32, &s);
422                 if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
423                         return -EFAULT;
424         }
425         return 0;
426 }
427
428 long sys32_rt_sigpending(compat_sigset_t __user *set, compat_size_t sigsetsize)
429 {
430         sigset_t s;
431         compat_sigset_t s32;
432         int ret;
433         mm_segment_t old_fs = get_fs();
434
435         set_fs(KERNEL_DS);
436         /* The __user pointer cast is valid because of the set_fs() */
437         ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
438         set_fs(old_fs);
439         if (!ret) {
440                 compat_from_sigset(&s32, &s);
441                 if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
442                         return -EFAULT;
443         }
444         return ret;
445 }
446
447
448 int copy_siginfo_to_user32(struct compat_siginfo __user *d, siginfo_t *s)
449 {
450         int err;
451
452         if (!access_ok (VERIFY_WRITE, d, sizeof(*d)))
453                 return -EFAULT;
454
455         /* If you change siginfo_t structure, please be sure
456          * this code is fixed accordingly.
457          * It should never copy any pad contained in the structure
458          * to avoid security leaks, but must copy the generic
459          * 3 ints plus the relevant union member.
460          * This routine must convert siginfo from 64bit to 32bit as well
461          * at the same time.
462          */
463         err = __put_user(s->si_signo, &d->si_signo);
464         err |= __put_user(s->si_errno, &d->si_errno);
465         err |= __put_user((short)s->si_code, &d->si_code);
466         if (s->si_code < 0)
467                 err |= __copy_to_user(&d->_sifields._pad, &s->_sifields._pad,
468                                       SI_PAD_SIZE32);
469         else switch(s->si_code >> 16) {
470         case __SI_CHLD >> 16:
471                 err |= __put_user(s->si_pid, &d->si_pid);
472                 err |= __put_user(s->si_uid, &d->si_uid);
473                 err |= __put_user(s->si_utime, &d->si_utime);
474                 err |= __put_user(s->si_stime, &d->si_stime);
475                 err |= __put_user(s->si_status, &d->si_status);
476                 break;
477         case __SI_FAULT >> 16:
478                 err |= __put_user((unsigned int)(unsigned long)s->si_addr,
479                                   &d->si_addr);
480                 break;
481         case __SI_POLL >> 16:
482                 err |= __put_user(s->si_band, &d->si_band);
483                 err |= __put_user(s->si_fd, &d->si_fd);
484                 break;
485         case __SI_TIMER >> 16:
486                 err |= __put_user(s->si_tid, &d->si_tid);
487                 err |= __put_user(s->si_overrun, &d->si_overrun);
488                 err |= __put_user(s->si_int, &d->si_int);
489                 break;
490         case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
491         case __SI_MESGQ >> 16:
492                 err |= __put_user(s->si_int, &d->si_int);
493                 /* fallthrough */
494         case __SI_KILL >> 16:
495         default:
496                 err |= __put_user(s->si_pid, &d->si_pid);
497                 err |= __put_user(s->si_uid, &d->si_uid);
498                 break;
499         }
500         return err;
501 }
502
503 /*
504  * Note: it is necessary to treat pid and sig as unsigned ints, with the
505  * corresponding cast to a signed int to insure that the proper conversion
506  * (sign extension) between the register representation of a signed int
507  * (msr in 32-bit mode) and the register representation of a signed int
508  * (msr in 64-bit mode) is performed.
509  */
510 long sys32_rt_sigqueueinfo(u32 pid, u32 sig, compat_siginfo_t __user *uinfo)
511 {
512         siginfo_t info;
513         int ret;
514         mm_segment_t old_fs = get_fs();
515         
516         if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
517             copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE32))
518                 return -EFAULT;
519         set_fs (KERNEL_DS);
520         /* The __user pointer cast is valid becasuse of the set_fs() */
521         ret = sys_rt_sigqueueinfo((int)pid, (int)sig, (siginfo_t __user *) &info);
522         set_fs (old_fs);
523         return ret;
524 }
525
526 int sys32_rt_sigsuspend(compat_sigset_t __user * unewset, size_t sigsetsize, int p3,
527                 int p4, int p6, int p7, struct pt_regs *regs)
528 {
529         sigset_t saveset, newset;
530         compat_sigset_t s32;
531
532         /* XXX: Don't preclude handling different sized sigset_t's.  */
533         if (sigsetsize != sizeof(sigset_t))
534                 return -EINVAL;
535
536         if (copy_from_user(&s32, unewset, sizeof(s32)))
537                 return -EFAULT;
538
539         /*
540          * Swap the 2 words of the 64-bit sigset_t (they are stored
541          * in the "wrong" endian in 32-bit user storage).
542          */
543         sigset_from_compat(&newset, &s32);
544
545         sigdelsetmask(&newset, ~_BLOCKABLE);
546         spin_lock_irq(&current->sighand->siglock);
547         saveset = current->blocked;
548         current->blocked = newset;
549         recalc_sigpending();
550         spin_unlock_irq(&current->sighand->siglock);
551
552         regs->result = -EINTR;
553         regs->gpr[3] = EINTR;
554         regs->ccr |= 0x10000000;
555         while (1) {
556                 current->state = TASK_INTERRUPTIBLE;
557                 schedule();
558                 if (do_signal32(&saveset, regs))
559                         /*
560                          * Returning 0 means we return to userspace via
561                          * ret_from_except and thus restore all user
562                          * registers from *regs.  This is what we need
563                          * to do when a signal has been delivered.
564                          */
565                         return 0;
566         }
567 }
568
569 /*
570  *  Start Alternate signal stack support
571  *
572  *  System Calls
573  *       sigaltatck               sys32_sigaltstack
574  */
575
576 int sys32_sigaltstack(u32 __new, u32 __old, int r5,
577                       int r6, int r7, int r8, struct pt_regs *regs)
578 {
579         stack_32_t __user * newstack = (stack_32_t __user *)(long) __new;
580         stack_32_t __user * oldstack = (stack_32_t __user *)(long) __old;
581         stack_t uss, uoss;
582         int ret;
583         mm_segment_t old_fs;
584         unsigned long sp;
585         compat_uptr_t ss_sp;
586
587         /*
588          * set sp to the user stack on entry to the system call
589          * the system call router sets R9 to the saved registers
590          */
591         sp = regs->gpr[1];
592
593         /* Put new stack info in local 64 bit stack struct */
594         if (newstack) {
595                 if (get_user(ss_sp, &newstack->ss_sp) ||
596                     __get_user(uss.ss_flags, &newstack->ss_flags) ||
597                     __get_user(uss.ss_size, &newstack->ss_size))
598                         return -EFAULT;
599                 uss.ss_sp = compat_ptr(ss_sp);
600         }
601
602         old_fs = get_fs();
603         set_fs(KERNEL_DS);
604         /* The __user pointer casts are valid because of the set_fs() */
605         ret = do_sigaltstack(
606                 newstack ? (stack_t __user *) &uss : NULL,
607                 oldstack ? (stack_t __user *) &uoss : NULL,
608                 sp);
609         set_fs(old_fs);
610         /* Copy the stack information to the user output buffer */
611         if (!ret && oldstack  &&
612                 (put_user((long)uoss.ss_sp, &oldstack->ss_sp) ||
613                  __put_user(uoss.ss_flags, &oldstack->ss_flags) ||
614                  __put_user(uoss.ss_size, &oldstack->ss_size)))
615                 return -EFAULT;
616         return ret;
617 }
618
619
620 /*
621  * Set up a signal frame for a "real-time" signal handler
622  * (one which gets siginfo).
623  */
624 static int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,
625                               siginfo_t *info, sigset_t *oldset,
626                               struct pt_regs * regs, unsigned long newsp)
627 {
628         struct rt_sigframe32 __user *rt_sf;
629         struct mcontext32 __user *frame;
630         unsigned long origsp = newsp;
631         compat_sigset_t c_oldset;
632
633         /* Set up Signal Frame */
634         /* Put a Real Time Context onto stack */
635         newsp -= sizeof(*rt_sf);
636         rt_sf = (struct rt_sigframe32 __user *)newsp;
637
638         /* create a stack frame for the caller of the handler */
639         newsp -= __SIGNAL_FRAMESIZE32 + 16;
640
641         if (!access_ok(VERIFY_WRITE, (void __user *)newsp, origsp - newsp))
642                 goto badframe;
643
644         compat_from_sigset(&c_oldset, oldset);
645
646         /* Put the siginfo & fill in most of the ucontext */
647         if (copy_siginfo_to_user32(&rt_sf->info, info)
648             || __put_user(0, &rt_sf->uc.uc_flags)
649             || __put_user(0, &rt_sf->uc.uc_link)
650             || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
651             || __put_user(sas_ss_flags(regs->gpr[1]),
652                           &rt_sf->uc.uc_stack.ss_flags)
653             || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
654             || __put_user((u32)(u64)&rt_sf->uc.uc_mcontext, &rt_sf->uc.uc_regs)
655             || __copy_to_user(&rt_sf->uc.uc_sigmask, &c_oldset, sizeof(c_oldset)))
656                 goto badframe;
657
658         /* Save user registers on the stack */
659         frame = &rt_sf->uc.uc_mcontext;
660         if (put_user(regs->gpr[1], (u32 __user *)newsp))
661                 goto badframe;
662
663         if (vdso32_rt_sigtramp && current->thread.vdso_base) {
664                 if (save_user_regs(regs, frame, 0))
665                         goto badframe;
666                 regs->link = current->thread.vdso_base + vdso32_rt_sigtramp;
667         } else {
668                 if (save_user_regs(regs, frame, __NR_rt_sigreturn))
669                         goto badframe;
670                 regs->link = (unsigned long) frame->tramp;
671         }
672         regs->gpr[1] = (unsigned long) newsp;
673         regs->gpr[3] = sig;
674         regs->gpr[4] = (unsigned long) &rt_sf->info;
675         regs->gpr[5] = (unsigned long) &rt_sf->uc;
676         regs->gpr[6] = (unsigned long) rt_sf;
677         regs->nip = (unsigned long) ka->sa.sa_handler;
678         regs->trap = 0;
679         regs->result = 0;
680
681         if (test_thread_flag(TIF_SINGLESTEP))
682                 ptrace_notify(SIGTRAP);
683
684         return 1;
685
686 badframe:
687 #if DEBUG_SIG
688         printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
689                regs, frame, newsp);
690 #endif
691         force_sigsegv(sig, current);
692         return 0;
693 }
694
695 static long do_setcontext32(struct ucontext32 __user *ucp, struct pt_regs *regs, int sig)
696 {
697         compat_sigset_t c_set;
698         sigset_t set;
699         u32 mcp;
700
701         if (__copy_from_user(&c_set, &ucp->uc_sigmask, sizeof(c_set))
702             || __get_user(mcp, &ucp->uc_regs))
703                 return -EFAULT;
704         sigset_from_compat(&set, &c_set);
705         restore_sigmask(&set);
706         if (restore_user_regs(regs, (struct mcontext32 __user *)(u64)mcp, sig))
707                 return -EFAULT;
708
709         return 0;
710 }
711
712 /*
713  * Handle {get,set,swap}_context operations for 32 bits processes
714  */
715
716 long sys32_swapcontext(struct ucontext32 __user *old_ctx,
717                        struct ucontext32 __user *new_ctx,
718                        int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
719 {
720         unsigned char tmp;
721         compat_sigset_t c_set;
722
723         /* Context size is for future use. Right now, we only make sure
724          * we are passed something we understand
725          */
726         if (ctx_size < sizeof(struct ucontext32))
727                 return -EINVAL;
728
729         if (old_ctx != NULL) {
730                 compat_from_sigset(&c_set, &current->blocked);
731                 if (!access_ok(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
732                     || save_user_regs(regs, &old_ctx->uc_mcontext, 0)
733                     || __copy_to_user(&old_ctx->uc_sigmask, &c_set, sizeof(c_set))
734                     || __put_user((u32)(u64)&old_ctx->uc_mcontext, &old_ctx->uc_regs))
735                         return -EFAULT;
736         }
737         if (new_ctx == NULL)
738                 return 0;
739         if (!access_ok(VERIFY_READ, new_ctx, sizeof(*new_ctx))
740             || __get_user(tmp, (u8 __user *) new_ctx)
741             || __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
742                 return -EFAULT;
743
744         /*
745          * If we get a fault copying the context into the kernel's
746          * image of the user's registers, we can't just return -EFAULT
747          * because the user's registers will be corrupted.  For instance
748          * the NIP value may have been updated but not some of the
749          * other registers.  Given that we have done the access_ok
750          * and successfully read the first and last bytes of the region
751          * above, this should only happen in an out-of-memory situation
752          * or if another thread unmaps the region containing the context.
753          * We kill the task with a SIGSEGV in this situation.
754          */
755         if (do_setcontext32(new_ctx, regs, 0))
756                 do_exit(SIGSEGV);
757
758         return 0;
759 }
760
761 long sys32_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
762                      struct pt_regs *regs)
763 {
764         struct rt_sigframe32 __user *rt_sf;
765         int ret;
766
767
768         /* Always make any pending restarted system calls return -EINTR */
769         current_thread_info()->restart_block.fn = do_no_restart_syscall;
770
771         rt_sf = (struct rt_sigframe32 __user *)
772                 (regs->gpr[1] + __SIGNAL_FRAMESIZE32 + 16);
773         if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
774                 goto bad;
775         if (do_setcontext32(&rt_sf->uc, regs, 1))
776                 goto bad;
777
778         /*
779          * It's not clear whether or why it is desirable to save the
780          * sigaltstack setting on signal delivery and restore it on
781          * signal return.  But other architectures do this and we have
782          * always done it up until now so it is probably better not to
783          * change it.  -- paulus
784          * We use the sys32_ version that does the 32/64 bits conversion
785          * and takes userland pointer directly. What about error checking ?
786          * nobody does any...
787          */
788         sys32_sigaltstack((u32)(u64)&rt_sf->uc.uc_stack, 0, 0, 0, 0, 0, regs);
789
790         ret = regs->result;
791
792         return ret;
793
794  bad:
795         force_sig(SIGSEGV, current);
796         return 0;
797 }
798
799
800 /*
801  * OK, we're invoking a handler
802  */
803 static int handle_signal32(unsigned long sig, struct k_sigaction *ka,
804                             siginfo_t *info, sigset_t *oldset,
805                             struct pt_regs * regs, unsigned long newsp)
806 {
807         struct sigcontext32 __user *sc;
808         struct sigregs32 __user *frame;
809         unsigned long origsp = newsp;
810
811         /* Set up Signal Frame */
812         newsp -= sizeof(struct sigregs32);
813         frame = (struct sigregs32 __user *) newsp;
814
815         /* Put a sigcontext on the stack */
816         newsp -= sizeof(*sc);
817         sc = (struct sigcontext32 __user *) newsp;
818
819         /* create a stack frame for the caller of the handler */
820         newsp -= __SIGNAL_FRAMESIZE32;
821
822         if (!access_ok(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
823                 goto badframe;
824
825 #if _NSIG != 64
826 #error "Please adjust handle_signal32()"
827 #endif
828         if (__put_user((u32)(u64)ka->sa.sa_handler, &sc->handler)
829             || __put_user(oldset->sig[0], &sc->oldmask)
830             || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
831             || __put_user((u32)(u64)frame, &sc->regs)
832             || __put_user(sig, &sc->signal))
833                 goto badframe;
834
835         if (vdso32_sigtramp && current->thread.vdso_base) {
836                 if (save_user_regs(regs, &frame->mctx, 0))
837                         goto badframe;
838                 regs->link = current->thread.vdso_base + vdso32_sigtramp;
839         } else {
840                 if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
841                         goto badframe;
842                 regs->link = (unsigned long) frame->mctx.tramp;
843         }
844
845         if (put_user(regs->gpr[1], (u32 __user *)newsp))
846                 goto badframe;
847         regs->gpr[1] = (unsigned long) newsp;
848         regs->gpr[3] = sig;
849         regs->gpr[4] = (unsigned long) sc;
850         regs->nip = (unsigned long) ka->sa.sa_handler;
851         regs->trap = 0;
852         regs->result = 0;
853
854         if (test_thread_flag(TIF_SINGLESTEP))
855                 ptrace_notify(SIGTRAP);
856
857         return 1;
858
859 badframe:
860 #if DEBUG_SIG
861         printk("badframe in handle_signal, regs=%p frame=%x newsp=%x\n",
862                regs, frame, *newspp);
863 #endif
864         force_sigsegv(sig, current);
865         return 0;
866 }
867
868 /*
869  * Do a signal return; undo the signal stack.
870  */
871 long sys32_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
872                        struct pt_regs *regs)
873 {
874         struct sigcontext32 __user *sc;
875         struct sigcontext32 sigctx;
876         struct mcontext32 __user *sr;
877         sigset_t set;
878         int ret;
879
880         /* Always make any pending restarted system calls return -EINTR */
881         current_thread_info()->restart_block.fn = do_no_restart_syscall;
882
883         sc = (struct sigcontext32 __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE32);
884         if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
885                 goto badframe;
886
887         /*
888          * Note that PPC32 puts the upper 32 bits of the sigmask in the
889          * unused part of the signal stackframe
890          */
891         set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
892         restore_sigmask(&set);
893
894         sr = (struct mcontext32 __user *)(u64)sigctx.regs;
895         if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
896             || restore_user_regs(regs, sr, 1))
897                 goto badframe;
898
899         ret = regs->result;
900         return ret;
901
902 badframe:
903         force_sig(SIGSEGV, current);
904         return 0;
905 }
906
907
908
909 /*
910  *  Start of do_signal32 routine
911  *
912  *   This routine gets control when a pending signal needs to be processed
913  *     in the 32 bit target thread -
914  *
915  *   It handles both rt and non-rt signals
916  */
917
918 /*
919  * Note that 'init' is a special process: it doesn't get signals it doesn't
920  * want to handle. Thus you cannot kill init even with a SIGKILL even by
921  * mistake.
922  */
923
924 int do_signal32(sigset_t *oldset, struct pt_regs *regs)
925 {
926         siginfo_t info;
927         unsigned int frame, newsp;
928         int signr, ret;
929         struct k_sigaction ka;
930
931         if (!oldset)
932                 oldset = &current->blocked;
933
934         newsp = frame = 0;
935
936         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
937
938         if (TRAP(regs) == 0x0C00                /* System Call! */
939             && regs->ccr & 0x10000000           /* error signalled */
940             && ((ret = regs->gpr[3]) == ERESTARTSYS
941                 || ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
942                 || ret == ERESTART_RESTARTBLOCK)) {
943
944                 if (signr > 0
945                     && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
946                         || (ret == ERESTARTSYS
947                             && !(ka.sa.sa_flags & SA_RESTART)))) {
948                         /* make the system call return an EINTR error */
949                         regs->result = -EINTR;
950                         regs->gpr[3] = EINTR;
951                         /* note that the cr0.SO bit is already set */
952                 } else {
953                         regs->nip -= 4; /* Back up & retry system call */
954                         regs->result = 0;
955                         regs->trap = 0;
956                         if (ret == ERESTART_RESTARTBLOCK)
957                                 regs->gpr[0] = __NR_restart_syscall;
958                         else
959                                 regs->gpr[3] = regs->orig_gpr3;
960                 }
961         }
962
963         if (signr == 0)
964                 return 0;               /* no signals delivered */
965
966         if ((ka.sa.sa_flags & SA_ONSTACK) && current->sas_ss_size
967             && (!on_sig_stack(regs->gpr[1])))
968                 newsp = (current->sas_ss_sp + current->sas_ss_size);
969         else
970                 newsp = regs->gpr[1];
971         newsp &= ~0xfUL;
972
973         /* Whee!  Actually deliver the signal.  */
974         if (ka.sa.sa_flags & SA_SIGINFO)
975                 ret = handle_rt_signal32(signr, &ka, &info, oldset, regs, newsp);
976         else
977                 ret = handle_signal32(signr, &ka, &info, oldset, regs, newsp);
978
979         if (ret && !(ka.sa.sa_flags & SA_NODEFER)) {
980                 spin_lock_irq(&current->sighand->siglock);
981                 sigorsets(&current->blocked, &current->blocked,
982                           &ka.sa.sa_mask);
983                 sigaddset(&current->blocked, signr);
984                 recalc_sigpending();
985                 spin_unlock_irq(&current->sighand->siglock);
986         }
987
988         return ret;
989 }