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