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