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