cleanup
[linux-2.4.21-pre4.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 <asm/ptrace.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/fs.h> 
21 #include <linux/mm.h> 
22 #include <linux/file.h> 
23 #include <linux/signal.h>
24 #include <linux/utime.h>
25 #include <linux/resource.h>
26 #include <linux/times.h>
27 #include <linux/utsname.h>
28 #include <linux/timex.h>
29 #include <linux/smp.h>
30 #include <linux/smp_lock.h>
31 #include <linux/sem.h>
32 #include <linux/msg.h>
33 #include <linux/shm.h>
34 #include <linux/slab.h>
35 #include <linux/uio.h>
36 #include <linux/nfs_fs.h>
37 #include <linux/smb_fs.h>
38 #include <linux/smb_mount.h>
39 #include <linux/ncp_fs.h>
40 #include <linux/quota.h>
41 #include <linux/module.h>
42 #include <linux/poll.h>
43 #include <linux/personality.h>
44 #include <linux/stat.h>
45 #include <linux/filter.h>
46 #include <asm/types.h>
47 #include <asm/ipc.h>
48 #include <asm/uaccess.h>
49 #include <linux/elf.h>
50 #include <asm/ppc32.h>
51 #include <asm/ppcdebug.h>
52 #include <asm/unistd.h>
53 #include <asm/ucontext.h>
54
55 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
56 /* 
57  * These are the flags in the MSR that the user is allowed to change
58  * by modifying the saved value of the MSR on the stack.  SE and BE
59  * should not be in this list since gdb may want to change these.  I.e,
60  * you should be able to step out of a signal handler to see what
61  * instruction executes next after the signal handler completes.
62  * Alternately, if you stepped into a signal handler, you should be
63  * able to continue 'til the next breakpoint from within the signal
64  * handler, even if the handler returns.
65  */
66 #define MSR_USERCHANGE  (MSR_FE0 | MSR_FE1)
67
68 struct timespec32 {
69         s32    tv_sec;
70         s32    tv_nsec;
71 };
72
73 struct sigregs32 {
74         /*
75          * the gp_regs array is 32 bit representation of the pt_regs
76          * structure that was stored on the kernle stack during the
77          * system call that was interrupted for the signal.
78          *
79          * Note that the entire pt_regs regs structure will fit in
80          * the gp_regs structure because the ELF_NREG value is 48 for
81          * PPC and the pt_regs structure contains 44 registers
82          */
83         elf_gregset_t32 gp_regs;
84         double          fp_regs[ELF_NFPREG];
85         unsigned int    tramp[2];
86         /*
87          * Programs using the rs6000/xcoff abi can save up to 19 gp
88          * regs and 18 fp regs below sp before decrementing it.
89          */
90         int             abigap[56];
91 };
92
93
94 struct rt_sigframe_32 {
95         /*
96          * Unused space at start of frame to allow for storing of
97          * stack pointers
98          */
99         unsigned long _unused;
100         /*
101          * This is a 32 bit pointer in user address space 
102          *     it is a pointer to the siginfo stucture in the rt stack frame 
103          */
104         u32 pinfo;
105         /*
106          * This is a 32 bit pointer in user address space
107          * it is a pointer to the user context in the rt stack frame
108          */
109         u32 puc;
110         struct siginfo32  info;
111         struct ucontext32 uc;
112 };
113
114
115
116
117
118 extern asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru);
119
120
121 /****************************************************************************/
122 /*  Start of nonRT signal support                                           */
123 /*                                                                          */
124 /*     sigset_t is 32 bits for non-rt signals                               */
125 /*                                                                          */
126 /*  System Calls                                                            */
127 /*       sigaction                sys32_sigaction                           */
128 /*       sigpending               sys32_sigpending                          */
129 /*       sigprocmask              sys32_sigprocmask                         */
130 /*       sigreturn                sys32_sigreturn                           */
131 /*                                                                          */
132 /*  Note sigsuspend has no special 32 bit routine - uses the 64 bit routine */ 
133 /*                                                                          */
134 /*  Other routines                                                          */
135 /*        setup_frame32                                                     */
136 /*                                                                          */
137 /****************************************************************************/
138
139
140 asmlinkage long sys32_sigaction(int sig, struct old_sigaction32 *act, struct old_sigaction32 *oact)
141 {
142         struct k_sigaction new_ka, old_ka;
143         int ret;
144         
145         PPCDBG(PPCDBG_SYS32, "sys32_sigaction - entered - pid=%ld current=%lx comm=%s\n", current->pid, current, current->comm);
146
147         if (sig < 0)
148                 sig = -sig;
149
150         if (act) {
151                 old_sigset_t32 mask;
152
153                 ret = get_user((long)new_ka.sa.sa_handler, &act->sa_handler);
154                 ret |= __get_user((long)new_ka.sa.sa_restorer, &act->sa_restorer);
155                 ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
156                 ret |= __get_user(mask, &act->sa_mask);
157                 if (ret)
158                         return ret;
159                 PPCDBG(PPCDBG_SIGNAL, "sys32_sigaction flags =%lx  \n", new_ka.sa.sa_flags);
160
161                 siginitset(&new_ka.sa.sa_mask, mask);
162         }
163
164         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
165
166         if (!ret && oact)
167         {
168                 ret = put_user((long)old_ka.sa.sa_handler, &oact->sa_handler);
169                 ret |= __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer);
170                 ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
171                 ret |= __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
172         }
173
174         
175         PPCDBG(PPCDBG_SYS32, "sys32_sigaction - exited - pid=%ld current=%lx comm=%s\n", current->pid, current, current->comm);
176
177         return ret;
178 }
179
180
181
182
183 extern asmlinkage long sys_sigpending(old_sigset_t *set);
184
185 asmlinkage long sys32_sigpending(old_sigset_t32 *set)
186 {
187         old_sigset_t s;
188         int ret;
189         mm_segment_t old_fs = get_fs();
190         
191         PPCDBG(PPCDBG_SYS32, "sys32_sigpending - entered - pid=%ld current=%lx comm=%s\n", current->pid, current, current->comm);
192                 
193         set_fs (KERNEL_DS);
194         ret = sys_sigpending(&s);
195         set_fs (old_fs);
196         if (put_user (s, set)) return -EFAULT;
197         
198         PPCDBG(PPCDBG_SYS32, "sys32_sigpending - exited - pid=%ld current=%lx comm=%s\n", current->pid, current, current->comm);
199
200         return ret;
201 }
202
203
204
205
206 extern asmlinkage long sys_sigprocmask(int how, old_sigset_t *set, old_sigset_t *oset);
207
208 /* Note: it is necessary to treat how as an unsigned int, 
209  * with the corresponding cast to a signed int to insure that the 
210  * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
211  * and the register representation of a signed int (msr in 64-bit mode) is performed.
212  */
213 asmlinkage long sys32_sigprocmask(u32 how, old_sigset_t32 *set, old_sigset_t32 *oset)
214 {
215         old_sigset_t s;
216         int ret;
217         mm_segment_t old_fs = get_fs();
218         
219         PPCDBG(PPCDBG_SYS32, "sys32_sigprocmask - entered - pid=%ld current=%lx comm=%s\n", current->pid, current, current->comm);
220         
221         if (set && get_user(s, set))
222                 return -EFAULT;
223         set_fs (KERNEL_DS);
224         ret = sys_sigprocmask((int)how, set ? &s : NULL, oset ? &s : NULL);
225         set_fs (old_fs);
226         
227         PPCDBG(PPCDBG_SYS32, "sys32_sigprocmask - exited - pid=%ld current=%lx comm=%s\n", current->pid, current, current->comm);
228
229         if (ret)
230                 return ret;
231         if (oset && put_user (s, oset))
232                 return -EFAULT;
233         return 0;
234 }
235
236
237
238 /*
239  * When we have signals to deliver, we set up on the
240  * user stack, going down from the original stack pointer:
241  *      a sigregs struct
242  *      one or more sigcontext structs
243  *      a gap of __SIGNAL_FRAMESIZE32 bytes
244  *
245  * Each of these things must be a multiple of 16 bytes in size.
246  *
247 */
248
249
250 /*
251  * Do a signal return; undo the signal stack.
252  */
253 long sys32_sigreturn(unsigned long r3, unsigned long r4, unsigned long r5,
254                      unsigned long r6, unsigned long r7, unsigned long r8,
255                      struct pt_regs *regs)
256 {
257         struct sigcontext32 *sc, sigctx;
258         struct sigregs32 *sr;
259         int ret;
260         elf_gregset_t32 saved_regs;  /* an array of ELF_NGREG unsigned ints (32 bits) */
261         sigset_t set;
262         unsigned int prevsp;
263         int i;
264
265         PPCDBG(PPCDBG_SIGNAL, "sys32_sigreturn - entered - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
266
267         sc = (struct sigcontext32 *)(regs->gpr[1] + __SIGNAL_FRAMESIZE32);
268         if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
269                 goto badframe;
270
271         /*
272          * Note that PPC32 puts the upper 32 bits of the sigmask in the
273          * unused part of the signal stackframe
274          */
275         set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3])<< 32);
276         sigdelsetmask(&set, ~_BLOCKABLE);
277         spin_lock_irq(&current->sigmask_lock);
278         current->blocked = set;
279         recalc_sigpending(current);
280         spin_unlock_irq(&current->sigmask_lock);
281
282         sc++;                   /* Look at next sigcontext */
283         /* If the next sigcontext is actually the sigregs (frame)  */
284         /*   - then no more sigcontexts on the user stack          */  
285         if (sc == (struct sigcontext32*)(u64)sigctx.regs)
286         {
287                 /* Last stacked signal - restore registers */
288                 sr = (struct sigregs32*)(u64)sigctx.regs;
289                 if (regs->msr & MSR_FP )
290                         giveup_fpu(current);
291                 /* 
292                  * Copy the 32 bit register values off the user stack
293                  * into the 32 bit register area
294                  */
295                 if (copy_from_user(saved_regs, &sr->gp_regs,sizeof(sr->gp_regs)))
296                         goto badframe;
297                 /*
298                  * The saved reg structure in the frame is an elf_grepset_t32,
299                  * it is a 32 bit register save of the registers in the
300                  * pt_regs structure that was stored on the kernel stack
301                  * during the system call when the system call was interrupted
302                  * for the signal. Only 32 bits are saved because the
303                  * sigcontext contains a pointer to the regs and the sig
304                  * context address is passed as a pointer to the signal
305                  * handler.  
306                  *
307                  * The entries in the elf_grepset have the same index as the
308                  * elements in the pt_regs structure.
309                  */
310                 saved_regs[PT_MSR] = (regs->msr & ~MSR_USERCHANGE)
311                         | (saved_regs[PT_MSR] & MSR_USERCHANGE);
312                 /*
313                  * Register 2 is the kernel toc - should be reset on
314                  * any calls into the kernel 
315                  */
316                 for (i = 0; i < 32; i++)
317                         regs->gpr[i] = (u64)(saved_regs[i]) & 0xFFFFFFFF;
318
319                 /*
320                  *  restore the non gpr registers 
321                  */
322                 regs->msr = (u64)(saved_regs[PT_MSR]) & 0xFFFFFFFF;
323                 /*
324                  * Insure that the interrupt mode is 64 bit, during 32 bit
325                  * execution. (This is necessary because we only saved
326                  * lower 32 bits of msr.)
327                  */
328                 regs->msr = regs->msr | MSR_ISF;
329
330                 regs->nip = (u64)(saved_regs[PT_NIP]) & 0xFFFFFFFF;
331                 regs->orig_gpr3 = (u64)(saved_regs[PT_ORIG_R3]) & 0xFFFFFFFF; 
332                 regs->ctr = (u64)(saved_regs[PT_CTR]) & 0xFFFFFFFF; 
333                 regs->link = (u64)(saved_regs[PT_LNK]) & 0xFFFFFFFF; 
334                 regs->xer = (u64)(saved_regs[PT_XER]) & 0xFFFFFFFF; 
335                 regs->ccr = (u64)(saved_regs[PT_CCR]) & 0xFFFFFFFF;
336                 /* regs->softe is left unchanged (like the MSR.EE bit) */
337                 /******************************************************/
338                 /* the DAR and the DSISR are only relevant during a   */
339                 /*   data or instruction storage interrupt. The value */
340                 /*   will be set to zero.                             */
341                 /******************************************************/
342                 regs->dar = 0; 
343                 regs->dsisr = 0;
344                 regs->result = (u64)(saved_regs[PT_RESULT]) & 0xFFFFFFFF;
345
346                 if (copy_from_user(current->thread.fpr, &sr->fp_regs, sizeof(sr->fp_regs)))
347                         goto badframe;
348
349                 ret = regs->result;
350         } else {
351                 /* More signals to go */
352                 regs->gpr[1] = (unsigned long)sc - __SIGNAL_FRAMESIZE32;
353                 if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
354                         goto badframe;
355                 sr = (struct sigregs32*)(u64)sigctx.regs;
356                 regs->gpr[3] = ret = sigctx.signal;
357                 regs->gpr[4] = (unsigned long) sc;
358                 regs->link = (unsigned long) &sr->tramp;
359                 regs->nip = sigctx.handler;
360
361                 if (get_user(prevsp, &sr->gp_regs[PT_R1])
362                     || put_user(prevsp, (unsigned int*) regs->gpr[1]))
363                         goto badframe;
364                 current->thread.fpscr = 0;
365         }
366   
367         PPCDBG(PPCDBG_SIGNAL, "sys32_sigreturn - normal exit returning %ld - pid=%ld current=%lx comm=%s \n", ret, current->pid, current, current->comm);
368         return ret;
369
370 badframe:
371         PPCDBG(PPCDBG_SYS32NI, "sys32_sigreturn - badframe - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm);
372         do_exit(SIGSEGV);
373 }       
374
375 /*
376  * Set up a signal frame.
377  */
378 static void
379 setup_frame32(struct pt_regs *regs, struct sigregs32 *frame,
380             unsigned int newsp)
381 {
382         struct sigcontext32 *sc = (struct sigcontext32 *)(u64)newsp;
383         int i;
384
385         if (verify_area(VERIFY_WRITE, frame, sizeof(*frame)))
386                 goto badframe;
387         if (regs->msr & MSR_FP)
388                 giveup_fpu(current);
389
390         /*
391          * Copy the register contents for the pt_regs structure on the
392          *   kernel stack to the elf_gregset_t32 structure on the user
393          *   stack. This is a copy of 64 bit register values to 32 bit
394          *   register values. The high order 32 bits of the 64 bit
395          *   registers are not needed since a 32 bit application is
396          *   running and the saved registers are the contents of the
397          *   user registers at the time of a system call.
398          * 
399          * The values saved on the user stack will be restored into
400          *  the registers during the signal return processing
401          *
402          * Note the +1 is needed in order to get the lower 32 bits
403          * of 64 bit register
404          */
405         for (i = 0; i < 32; i++) {
406                 if (__copy_to_user(&frame->gp_regs[i], (u32*)(&regs->gpr[i])+1, sizeof(u32)))
407                         goto badframe;
408         }
409
410         /*
411          * Copy the non gpr registers to the user stack
412          */
413         if (__copy_to_user(&frame->gp_regs[PT_NIP], (u32*)(&regs->gpr[PT_NIP])+1, sizeof(u32))
414             || __copy_to_user(&frame->gp_regs[PT_MSR], (u32*)(&regs->gpr[PT_MSR])+1, sizeof(u32))
415             || __copy_to_user(&frame->gp_regs[PT_ORIG_R3], (u32*)(&regs->gpr[PT_ORIG_R3])+1,
416                               sizeof(u32))
417             || __copy_to_user(&frame->gp_regs[PT_CTR], (u32*)(&regs->gpr[PT_CTR])+1, sizeof(u32))
418             || __copy_to_user(&frame->gp_regs[PT_LNK], (u32*)(&regs->gpr[PT_LNK])+1, sizeof(u32))
419             || __copy_to_user(&frame->gp_regs[PT_XER], (u32*)(&regs->gpr[PT_XER])+1, sizeof(u32))
420             || __copy_to_user(&frame->gp_regs[PT_CCR], (u32*)(&regs->gpr[PT_CCR])+1, sizeof(u32))
421 # if 0
422             || __copy_to_user(&frame->gp_regs[PT_MQ], (u32*)(&regs->gpr[PT_MQ])+1, sizeof(u32))
423 #endif
424             || __copy_to_user(&frame->gp_regs[PT_RESULT], (u32*)(&regs->gpr[PT_RESULT])+1,
425                               sizeof(u32)))
426                 goto badframe;
427
428         /*
429          * Now copy the floating point registers onto the user stack 
430          *
431          * Also set up so on the completion of the signal handler, the
432          * sys_sigreturn will get control to reset the stack
433          */
434         if (__copy_to_user(&frame->fp_regs, current->thread.fpr,
435                            ELF_NFPREG * sizeof(double))
436             /* li r0, __NR_sigreturn */
437             || __put_user(0x38000000U + __NR_sigreturn, &frame->tramp[0])
438             /* sc */
439             || __put_user(0x44000002U, &frame->tramp[1]))
440                 goto badframe;
441
442         flush_icache_range((unsigned long) &frame->tramp[0],
443                            (unsigned long) &frame->tramp[2]);
444         current->thread.fpscr = 0;      /* turn off all fp exceptions */
445
446         newsp -= __SIGNAL_FRAMESIZE32;
447         if (put_user(regs->gpr[1], (u32*)(u64)newsp)
448             || get_user(regs->nip, &sc->handler)
449             || get_user(regs->gpr[3], &sc->signal))
450                 goto badframe;
451
452         regs->gpr[1] = newsp & 0xFFFFFFFF;
453         /*
454          * first parameter to the signal handler is the signal number
455          *  - the value is in gpr3
456          * second parameter to the signal handler is the sigcontext
457          *   - set the value into gpr4
458          */
459         regs->gpr[4] = (unsigned long) sc;
460         regs->link = (unsigned long) frame->tramp;
461         return;
462
463  badframe:
464         udbg_printf("setup_frame32 - badframe in setup_frame, regs=%p frame=%p newsp=%lx\n", regs, frame, newsp);  PPCDBG_ENTER_DEBUGGER();
465 #if DEBUG_SIG
466         printk("badframe in setup_frame32, regs=%p frame=%p newsp=%lx\n",
467                regs, frame, newsp);
468 #endif
469         do_exit(SIGSEGV);
470 }
471
472
473 /*
474  *  Start of RT signal support
475  *
476  *     sigset_t is 64 bits for rt signals
477  *
478  *  System Calls
479  *       sigaction                sys32_rt_sigaction
480  *       sigpending               sys32_rt_sigpending
481  *       sigprocmask              sys32_rt_sigprocmask
482  *       sigreturn                sys32_rt_sigreturn
483  *       sigtimedwait             sys32_rt_sigtimedwait
484  *       sigqueueinfo             sys32_rt_sigqueueinfo
485  *       sigsuspend               sys32_rt_sigsuspend
486  *
487  *  Other routines
488  *        setup_rt_frame32
489  *        copy_siginfo_to_user32
490  *        siginfo32to64
491  */
492
493
494 /*
495  * This code executes after the rt signal handler in 32 bit mode has
496  * completed and returned  
497  */
498 long sys32_rt_sigreturn(unsigned long r3, unsigned long r4, unsigned long r5,
499                         unsigned long r6, unsigned long r7, unsigned long r8,
500                         struct pt_regs * regs)
501 {
502         struct rt_sigframe_32 *rt_stack_frame;
503         struct sigcontext32 sigctx;
504         struct sigregs32 *signalregs;
505  
506         int i, ret;
507         elf_gregset_t32 saved_regs;   /* an array of 32 bit register values */
508         sigset_t signal_set; 
509         stack_t stack;
510         unsigned int previous_stack;
511
512         ret = 0;
513         /* Adjust the inputted reg1 to point to the first rt signal frame */
514         rt_stack_frame = (struct rt_sigframe_32 *)(regs->gpr[1] + __SIGNAL_FRAMESIZE32);
515         /* Copy the information from the user stack  */
516         if (copy_from_user(&sigctx, &rt_stack_frame->uc.uc_mcontext,sizeof(sigctx))
517             || copy_from_user(&signal_set, &rt_stack_frame->uc.uc_sigmask,sizeof(signal_set))
518             || copy_from_user(&stack,&rt_stack_frame->uc.uc_stack,sizeof(stack)))
519                 /* unable to copy from user storage */
520                 goto badframe;
521
522         /*
523          * Unblock the signal that was processed 
524          *   After a signal handler runs - 
525          *     if the signal is blockable - the signal will be unblocked  
526          *       ( sigkill and sigstop are not blockable)
527          */
528         sigdelsetmask(&signal_set, ~_BLOCKABLE); 
529         /* update the current based on the sigmask found in the rt_stackframe */
530         spin_lock_irq(&current->sigmask_lock);
531         current->blocked = signal_set;
532         recalc_sigpending(current);
533         spin_unlock_irq(&current->sigmask_lock);
534
535         /* Set to point to the next rt_sigframe - this is used to determine whether this 
536          *   is the last signal to process
537          */
538         rt_stack_frame ++;
539
540         if (rt_stack_frame == (struct rt_sigframe_32 *)(u64)(sigctx.regs)) {
541                 signalregs = (struct sigregs32 *) (u64)sigctx.regs;
542                 /* If currently owning the floating point - give them up */
543                 if (regs->msr & MSR_FP)
544                         giveup_fpu(current);
545
546                 if (copy_from_user(saved_regs,&signalregs->gp_regs,sizeof(signalregs->gp_regs))) 
547                         goto badframe;
548
549                 /*
550                  * The saved reg structure in the frame is an elf_grepset_t32,
551                  * it is a 32 bit register save of the registers in the
552                  * pt_regs structure that was stored on the kernel stack
553                  * during the system call when the system call was interrupted
554                  * for the signal. Only 32 bits are saved because the
555                  * sigcontext contains a pointer to the regs and the sig
556                  * context address is passed as a pointer to the signal handler
557                  *
558                  * The entries in the elf_grepset have the same index as
559                  * the elements in the pt_regs structure.
560                  */
561                 saved_regs[PT_MSR] = (regs->msr & ~MSR_USERCHANGE)
562                         | (saved_regs[PT_MSR] & MSR_USERCHANGE);
563                 /*
564                  * Register 2 is the kernel toc - should be reset on any
565                  * calls into the kernel
566                  */
567                 for (i = 0; i < 32; i++)
568                         regs->gpr[i] = (u64)(saved_regs[i]) & 0xFFFFFFFF;
569                 /*
570                  * restore the non gpr registers
571                  */
572                 regs->msr = (u64)(saved_regs[PT_MSR]) & 0xFFFFFFFF;
573                 regs->nip = (u64)(saved_regs[PT_NIP]) & 0xFFFFFFFF;
574                 regs->orig_gpr3 = (u64)(saved_regs[PT_ORIG_R3]) & 0xFFFFFFFF; 
575                 regs->ctr = (u64)(saved_regs[PT_CTR]) & 0xFFFFFFFF; 
576                 regs->link = (u64)(saved_regs[PT_LNK]) & 0xFFFFFFFF; 
577                 regs->xer = (u64)(saved_regs[PT_XER]) & 0xFFFFFFFF; 
578                 regs->ccr = (u64)(saved_regs[PT_CCR]) & 0xFFFFFFFF;
579                 /* regs->softe is left unchanged (like MSR.EE) */
580                 /*
581                  * the DAR and the DSISR are only relevant during a
582                  *   data or instruction storage interrupt. The value
583                  *   will be set to zero.
584                  */
585                 regs->dar = 0; 
586                 regs->dsisr = 0;
587                 regs->result = (u64)(saved_regs[PT_RESULT]) & 0xFFFFFFFF;
588
589                 if (copy_from_user(current->thread.fpr, &signalregs->fp_regs, sizeof(signalregs->fp_regs))) 
590                         goto badframe;
591
592                 ret = regs->result;
593         }
594         else  /* more signals to go  */
595         {
596                 regs->gpr[1] = (u64)rt_stack_frame - __SIGNAL_FRAMESIZE32;
597                 if (copy_from_user(&sigctx, &rt_stack_frame->uc.uc_mcontext,sizeof(sigctx)))
598                 {
599                         goto badframe;
600                 }
601                 signalregs = (struct sigregs32 *) (u64)sigctx.regs;
602                 /* first parm to signal handler is the signal number */
603                 regs->gpr[3] = ret = sigctx.signal;
604                 /* second parm is a pointer to sig info */
605                 get_user(regs->gpr[4], &rt_stack_frame->pinfo);
606                 /* third parm is a pointer to the ucontext */
607                 get_user(regs->gpr[5], &rt_stack_frame->puc);
608                 /* fourth parm is the stack frame */
609                 regs->gpr[6] = (u64)rt_stack_frame;
610                 /* Set up link register to return to sigreturn when the */
611                 /*  signal handler completes */
612                 regs->link = (u64)&signalregs->tramp;
613                 /* Set next instruction to the start fo the signal handler */
614                 regs->nip = sigctx.handler;
615                 /* Set the reg1 to look like a call to the signal handler */
616                 if (get_user(previous_stack,&signalregs->gp_regs[PT_R1])
617                     || put_user(previous_stack, (unsigned long *)regs->gpr[1]))
618                 {
619                         goto badframe;
620                 }
621                 current->thread.fpscr = 0;
622
623         }
624
625         return ret;
626
627  badframe:
628         do_exit(SIGSEGV);     
629 }
630
631
632
633 asmlinkage long sys32_rt_sigaction(int sig, const struct sigaction32 *act, struct sigaction32 *oact, size_t sigsetsize)
634 {
635         struct k_sigaction new_ka, old_ka;
636         int ret;
637         sigset32_t set32;
638
639         PPCDBG(PPCDBG_SIGNAL, "sys32_rt_sigaction - entered - sig=%x \n", sig);
640
641         /* XXX: Don't preclude handling different sized sigset_t's.  */
642         if (sigsetsize != sizeof(sigset32_t))
643                 return -EINVAL;
644
645         if (act) {
646                 ret = get_user((long)new_ka.sa.sa_handler, &act->sa_handler);
647                 ret |= __copy_from_user(&set32, &act->sa_mask,
648                                         sizeof(sigset32_t));
649                 switch (_NSIG_WORDS) {
650                 case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
651                                 | (((long)set32.sig[7]) << 32);
652                 case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
653                                 | (((long)set32.sig[5]) << 32);
654                 case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
655                                 | (((long)set32.sig[3]) << 32);
656                 case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
657                                 | (((long)set32.sig[1]) << 32);
658                 }
659
660                 ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
661                 
662                 if (ret)
663                         return -EFAULT;
664         }
665
666         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
667
668         if (!ret && oact) {
669                 switch (_NSIG_WORDS) {
670                 case 4:
671                         set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
672                         set32.sig[6] = old_ka.sa.sa_mask.sig[3];
673                 case 3:
674                         set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
675                         set32.sig[4] = old_ka.sa.sa_mask.sig[2];
676                 case 2:
677                         set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
678                         set32.sig[2] = old_ka.sa.sa_mask.sig[1];
679                 case 1:
680                         set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
681                         set32.sig[0] = old_ka.sa.sa_mask.sig[0];
682                 }
683                 ret = put_user((long)old_ka.sa.sa_handler, &oact->sa_handler);
684                 ret |= __copy_to_user(&oact->sa_mask, &set32,
685                                       sizeof(sigset32_t));
686                 ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
687         }
688
689   
690         PPCDBG(PPCDBG_SIGNAL, "sys32_rt_sigaction - exiting - sig=%x \n", sig);
691         return ret;
692 }
693
694
695 extern asmlinkage long sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset,
696                                           size_t sigsetsize);
697
698 /*
699  * Note: it is necessary to treat how as an unsigned int, with the
700  * corresponding cast to a signed int to insure that the proper
701  * conversion (sign extension) between the register representation
702  * of a signed int (msr in 32-bit mode) and the register representation
703  * of a signed int (msr in 64-bit mode) is performed.
704  */
705 asmlinkage long sys32_rt_sigprocmask(u32 how, sigset32_t *set, sigset32_t *oset, size_t sigsetsize)
706 {
707         sigset_t s;
708         sigset32_t s32;
709         int ret;
710         mm_segment_t old_fs = get_fs();
711
712         PPCDBG(PPCDBG_SIGNAL, "sys32_rt_sigprocmask - entered how=%x \n", (int)how);
713         
714         if (set) {
715                 if (copy_from_user (&s32, set, sizeof(sigset32_t)))
716                         return -EFAULT;
717     
718                 switch (_NSIG_WORDS) {
719                 case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
720                 case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
721                 case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
722                 case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
723                 }
724         }
725         
726         set_fs (KERNEL_DS);
727         ret = sys_rt_sigprocmask((int)how, set ? &s : NULL, oset ? &s : NULL,
728                                  sigsetsize); 
729         set_fs (old_fs);
730         if (ret)
731                 return ret;
732         if (oset) {
733                 switch (_NSIG_WORDS) {
734                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
735                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
736                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
737                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
738                 }
739                 if (copy_to_user (oset, &s32, sizeof(sigset32_t)))
740                         return -EFAULT;
741         }
742         return 0;
743 }
744
745
746 extern asmlinkage long sys_rt_sigpending(sigset_t *set, size_t sigsetsize);
747
748
749
750 asmlinkage long sys32_rt_sigpending(sigset32_t *set,   __kernel_size_t32 sigsetsize)
751 {
752
753         sigset_t s;
754         sigset32_t s32;
755         int ret;
756         mm_segment_t old_fs = get_fs();
757                 
758         set_fs (KERNEL_DS);
759         ret = sys_rt_sigpending(&s, sigsetsize);
760         set_fs (old_fs);
761         if (!ret) {
762                 switch (_NSIG_WORDS) {
763                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
764                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
765                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
766                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
767                 }
768                 if (copy_to_user (set, &s32, sizeof(sigset32_t)))
769                         return -EFAULT;
770         }
771         return ret;
772 }
773
774
775
776 siginfo_t32 *
777 siginfo64to32(siginfo_t32 *d, siginfo_t *s)
778 {
779         memset (d, 0, sizeof(siginfo_t32));
780         d->si_signo = s->si_signo;
781         d->si_errno = s->si_errno;
782         d->si_code = s->si_code & 0xffff;
783         if (s->si_signo >= SIGRTMIN) {
784                 d->si_pid = s->si_pid;
785                 d->si_uid = s->si_uid;
786                 
787                 d->si_int = s->si_int;
788         } else switch (s->si_signo) {
789         /* XXX: What about POSIX1.b timers */
790         case SIGCHLD:
791                 d->si_pid = s->si_pid;
792                 d->si_status = s->si_status;
793                 d->si_utime = s->si_utime;
794                 d->si_stime = s->si_stime;
795                 break;
796         case SIGSEGV:
797         case SIGBUS:
798         case SIGFPE:
799         case SIGILL:
800                 d->si_addr = (long)(s->si_addr);
801         break;
802         case SIGPOLL:
803                 d->si_band = s->si_band;
804                 d->si_fd = s->si_fd;
805                 break;
806         default:
807                 d->si_pid = s->si_pid;
808                 d->si_uid = s->si_uid;
809                 break;
810         }
811         return d;
812 }
813
814 extern asmlinkage long
815 sys_rt_sigtimedwait(const sigset_t *uthese, siginfo_t *uinfo,
816                     const struct timespec *uts, size_t sigsetsize);
817
818 asmlinkage long
819 sys32_rt_sigtimedwait(sigset32_t *uthese, siginfo_t32 *uinfo,
820                       struct timespec32 *uts, __kernel_size_t32 sigsetsize)
821 {
822         sigset_t s;
823         sigset32_t s32;
824         struct timespec t;
825         int ret;
826         mm_segment_t old_fs = get_fs();
827         siginfo_t info;
828         siginfo_t32 info32;
829                 
830         if (copy_from_user (&s32, uthese, sizeof(sigset32_t)))
831                 return -EFAULT;
832         switch (_NSIG_WORDS) {
833         case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
834         case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
835         case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
836         case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
837         }
838         if (uts) {
839                 ret = get_user (t.tv_sec, &uts->tv_sec);
840                 ret |= __get_user (t.tv_nsec, &uts->tv_nsec);
841                 if (ret)
842                         return -EFAULT;
843         }
844         set_fs (KERNEL_DS);
845         if (uts) 
846                 ret = sys_rt_sigtimedwait(&s, &info, &t, sigsetsize);
847         else
848                 ret = sys_rt_sigtimedwait(&s, &info, (struct timespec *)uts,
849                                 sigsetsize);
850         set_fs (old_fs);
851         if (ret >= 0 && uinfo) {
852                 if (copy_to_user (uinfo, siginfo64to32(&info32, &info),
853                                   sizeof(siginfo_t32)))
854                         return -EFAULT;
855         }
856         return ret;
857 }
858
859
860
861 siginfo_t *
862 siginfo32to64(siginfo_t *d, siginfo_t32 *s)
863 {
864         d->si_signo = s->si_signo;
865         d->si_errno = s->si_errno;
866         d->si_code = s->si_code;
867         if (s->si_signo >= SIGRTMIN) {
868                 d->si_pid = s->si_pid;
869                 d->si_uid = s->si_uid;
870                 d->si_int = s->si_int;
871         
872         } else switch (s->si_signo) {
873         /* XXX: What about POSIX1.b timers */
874         case SIGCHLD:
875                 d->si_pid = s->si_pid;
876                 d->si_status = s->si_status;
877                 d->si_utime = s->si_utime;
878                 d->si_stime = s->si_stime;
879                 break;
880         case SIGSEGV:
881         case SIGBUS:
882         case SIGFPE:
883         case SIGILL:
884                 d->si_addr = (void *)A(s->si_addr);
885                 break;
886         case SIGPOLL:
887                 d->si_band = s->si_band;
888                 d->si_fd = s->si_fd;
889                 break;
890         default:
891                 d->si_pid = s->si_pid;
892                 d->si_uid = s->si_uid;
893                 break;
894         }
895         return d;
896 }
897
898
899 extern asmlinkage long sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo);
900
901 /*
902  * Note: it is necessary to treat pid and sig as unsigned ints, with the
903  * corresponding cast to a signed int to insure that the proper conversion
904  * (sign extension) between the register representation of a signed int
905  * (msr in 32-bit mode) and the register representation of a signed int
906  * (msr in 64-bit mode) is performed.
907  */
908 asmlinkage long sys32_rt_sigqueueinfo(u32 pid, u32 sig, siginfo_t32 *uinfo)
909 {
910         siginfo_t info;
911         siginfo_t32 info32;
912         int ret;
913         mm_segment_t old_fs = get_fs();
914         
915         if (copy_from_user (&info32, uinfo, sizeof(siginfo_t32)))
916                 return -EFAULT;
917         /* XXX: Is this correct? */
918         siginfo32to64(&info, &info32);
919
920         set_fs (KERNEL_DS);
921         ret = sys_rt_sigqueueinfo((int)pid, (int)sig, &info);
922         set_fs (old_fs);
923         return ret;
924 }
925
926
927 int do_signal(sigset_t *oldset, struct pt_regs *regs);
928 int sys32_rt_sigsuspend(sigset32_t* unewset, size_t sigsetsize, int p3, int p4, int p6, int p7, struct pt_regs *regs)
929 {
930         sigset_t saveset, newset;
931         
932         sigset32_t s32;
933
934         /* XXX: Don't preclude handling different sized sigset_t's.  */
935         if (sigsetsize != sizeof(sigset_t))
936                 return -EINVAL;
937
938         if (copy_from_user(&s32, unewset, sizeof(s32)))
939                 return -EFAULT;
940
941         /*
942          * Swap the 2 words of the 64-bit sigset_t (they are stored
943          * in the "wrong" endian in 32-bit user storage).
944          */
945         switch (_NSIG_WORDS) {
946                 case 4: newset.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
947                 case 3: newset.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
948                 case 2: newset.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
949                 case 1: newset.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
950         }
951
952         sigdelsetmask(&newset, ~_BLOCKABLE);
953
954         spin_lock_irq(&current->sigmask_lock);
955         saveset = current->blocked;
956         current->blocked = newset;
957         recalc_sigpending(current);
958         spin_unlock_irq(&current->sigmask_lock);
959
960         regs->result = -EINTR;
961         regs->gpr[3] = EINTR;
962         regs->ccr |= 0x10000000;
963         while (1) {
964                 current->state = TASK_INTERRUPTIBLE;
965                 schedule();
966                 if (do_signal(&saveset, regs))
967                         /*
968                          * If a signal handler needs to be called,
969                          * do_signal() has set R3 to the signal number (the
970                          * first argument of the signal handler), so don't
971                          * overwrite that with EINTR !
972                          * In the other cases, do_signal() doesn't touch 
973                          * R3, so it's still set to -EINTR (see above).
974                          */
975                         return regs->gpr[3];
976         }
977 }
978
979
980 /*
981  * Set up a rt signal frame.
982  */
983 static void
984 setup_rt_frame32(struct pt_regs *regs, struct sigregs32 *frame,
985             unsigned int newsp)
986 {
987         unsigned int copyreg4,copyreg5;
988         struct rt_sigframe_32 * rt_sf = (struct rt_sigframe_32 *) (u64)newsp;
989         int i;
990   
991         if (verify_area(VERIFY_WRITE, frame, sizeof(*frame)))
992                 goto badframe;
993         if (regs->msr & MSR_FP)
994                 giveup_fpu(current);
995
996         /*
997          * Copy the register contents for the pt_regs structure on the
998          *   kernel stack to the elf_gregset_t32 structure on the user
999          *   stack. This is a copy of 64 bit register values to 32 bit
1000          *   register values. The high order 32 bits of the 64 bit
1001          *   registers are not needed since a 32 bit application is
1002          *   running and the saved registers are the contents of the
1003          *   user registers at the time of a system call.
1004          *
1005          * The values saved on the user stack will be restored into
1006          *  the registers during the signal return processing.
1007          *
1008          * Note the +1 is needed in order to get the lower 32 bits
1009          * of 64 bit register
1010          */
1011         for (i = 0; i < 32; i++) {
1012                 if (__copy_to_user(&frame->gp_regs[i], (u32*)(&regs->gpr[i])+1, sizeof(u32)))
1013                         goto badframe;
1014         }
1015
1016         /*
1017          * Copy the non gpr registers to the user stack
1018          */
1019         if (__copy_to_user(&frame->gp_regs[PT_NIP], (u32*)(&regs->gpr[PT_NIP])+1, sizeof(u32))
1020             || __copy_to_user(&frame->gp_regs[PT_MSR], (u32*)(&regs->gpr[PT_MSR])+1, sizeof(u32))
1021             || __copy_to_user(&frame->gp_regs[PT_ORIG_R3], (u32*)(&regs->gpr[PT_ORIG_R3])+1,
1022                               sizeof(u32))
1023             || __copy_to_user(&frame->gp_regs[PT_CTR], (u32*)(&regs->gpr[PT_CTR])+1, sizeof(u32))
1024             || __copy_to_user(&frame->gp_regs[PT_LNK], (u32*)(&regs->gpr[PT_LNK])+1, sizeof(u32))
1025             || __copy_to_user(&frame->gp_regs[PT_XER], (u32*)(&regs->gpr[PT_XER])+1, sizeof(u32))
1026             || __copy_to_user(&frame->gp_regs[PT_CCR], (u32*)(&regs->gpr[PT_CCR])+1, sizeof(u32))
1027             || __copy_to_user(&frame->gp_regs[PT_RESULT], (u32*)(&regs->gpr[PT_RESULT])+1,
1028                               sizeof(u32)))
1029                 goto badframe;
1030
1031
1032         /*
1033          * Now copy the floating point registers onto the user stack
1034          *
1035          * Also set up so on the completion of the signal handler, the
1036          * sys_sigreturn will get control to reset the stack
1037          */
1038         if (__copy_to_user(&frame->fp_regs, current->thread.fpr,
1039                            ELF_NFPREG * sizeof(double))
1040             || __put_user(0x38000000U + __NR_rt_sigreturn, &frame->tramp[0])    /* li r0, __NR_rt_sigreturn */
1041             || __put_user(0x44000002U, &frame->tramp[1]))   /* sc */
1042                 goto badframe;
1043
1044         flush_icache_range((unsigned long) &frame->tramp[0],
1045                            (unsigned long) &frame->tramp[2]);
1046         current->thread.fpscr = 0;      /* turn off all fp exceptions */
1047   
1048         /*
1049          * Retrieve rt_sigframe from stack and
1050          * set up registers for signal handler
1051         */
1052         newsp -= __SIGNAL_FRAMESIZE32;
1053       
1054
1055         if (put_user((u32)(regs->gpr[1]), (unsigned int *)(u64)newsp)
1056             || get_user(regs->nip, &rt_sf->uc.uc_mcontext.handler)
1057             || get_user(regs->gpr[3], &rt_sf->uc.uc_mcontext.signal)
1058             || get_user(copyreg4, &rt_sf->pinfo)
1059             || get_user(copyreg5, &rt_sf->puc))
1060                 goto badframe;
1061
1062         regs->gpr[4] = copyreg4;
1063         regs->gpr[5] = copyreg5;
1064         regs->gpr[1] = newsp;
1065         regs->gpr[6] = (unsigned long) rt_sf;
1066         regs->link = (unsigned long) frame->tramp;
1067
1068         return;
1069
1070  badframe:
1071         udbg_printf("setup_frame32 - badframe in setup_frame, regs=%p frame=%p newsp=%lx\n", regs, frame, newsp);  PPCDBG_ENTER_DEBUGGER();
1072 #if DEBUG_SIG
1073         printk("badframe in setup_frame32, regs=%p frame=%p newsp=%lx\n",
1074                regs, frame, newsp);
1075 #endif
1076         do_exit(SIGSEGV);
1077 }
1078
1079
1080 /*
1081  * OK, we're invoking a handler
1082  */
1083 static void
1084 handle_signal32(unsigned long sig, struct k_sigaction *ka,
1085               siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
1086               unsigned int *newspp, unsigned int frame)
1087 {
1088         struct sigcontext32 *sc;
1089         struct rt_sigframe_32 *rt_stack_frame;
1090         siginfo_t32 siginfo32bit;
1091
1092         if (regs->trap == 0x0C00 /* System Call! */
1093             && ((int)regs->result == -ERESTARTNOHAND ||
1094                 ((int)regs->result == -ERESTARTSYS &&
1095                  !(ka->sa.sa_flags & SA_RESTART))))
1096                 regs->result = -EINTR;
1097
1098         /* Set up the signal frame             */
1099         /*   Determine if an real time frame - siginfo required   */
1100         if (ka->sa.sa_flags & SA_SIGINFO)
1101         {
1102                 siginfo64to32(&siginfo32bit,info);
1103                 *newspp -= sizeof(*rt_stack_frame);
1104                 rt_stack_frame = (struct rt_sigframe_32 *) (u64)(*newspp) ;
1105     
1106                 if (verify_area(VERIFY_WRITE, rt_stack_frame, sizeof(*rt_stack_frame)))
1107                 {
1108                         goto badframe;
1109                 }
1110                 if (__put_user((u32)(u64)ka->sa.sa_handler, &rt_stack_frame->uc.uc_mcontext.handler)
1111                     || __put_user((u32)(u64)&rt_stack_frame->info, &rt_stack_frame->pinfo)
1112                     || __put_user((u32)(u64)&rt_stack_frame->uc, &rt_stack_frame->puc)
1113                     /*  put the siginfo on the user stack                    */
1114                     || __copy_to_user(&rt_stack_frame->info,&siginfo32bit,sizeof(siginfo32bit))
1115                     /*  set the ucontext on the user stack                   */ 
1116                     || __put_user(0,&rt_stack_frame->uc.uc_flags)
1117                     || __put_user(0,&rt_stack_frame->uc.uc_link)
1118                     || __put_user(current->sas_ss_sp, &rt_stack_frame->uc.uc_stack.ss_sp)
1119                     || __put_user(sas_ss_flags(regs->gpr[1]),
1120                                   &rt_stack_frame->uc.uc_stack.ss_flags)
1121                     || __put_user(current->sas_ss_size, &rt_stack_frame->uc.uc_stack.ss_size)
1122                     || __copy_to_user(&rt_stack_frame->uc.uc_sigmask, oldset,sizeof(*oldset))
1123                     /* point the mcontext.regs to the pramble register frame  */
1124                     || __put_user(frame, &rt_stack_frame->uc.uc_mcontext.regs)
1125                     || __put_user(sig,&rt_stack_frame->uc.uc_mcontext.signal))
1126                 {
1127                         goto badframe; 
1128                 }
1129         } else {
1130                 /* Put a sigcontext on the stack */
1131                 *newspp -= sizeof(*sc);
1132                 sc = (struct sigcontext32 *)(u64)*newspp;
1133                 if (verify_area(VERIFY_WRITE, sc, sizeof(*sc)))
1134                         goto badframe;
1135                 /*
1136                  * Note the upper 32 bits of the signal mask are stored
1137                  * in the unused part of the signal stack frame
1138                  */
1139                 if (__put_user((u32)(u64)ka->sa.sa_handler, &sc->handler)
1140                     || __put_user(oldset->sig[0], &sc->oldmask)
1141                     || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
1142                     || __put_user((unsigned int)frame, &sc->regs)
1143                     || __put_user(sig, &sc->signal))
1144                         goto badframe;
1145         }
1146
1147         if (ka->sa.sa_flags & SA_ONESHOT)
1148                 ka->sa.sa_handler = SIG_DFL;
1149
1150         if (!(ka->sa.sa_flags & SA_NODEFER)) {
1151                 spin_lock_irq(&current->sigmask_lock);
1152                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
1153                 sigaddset(&current->blocked,sig);
1154                 recalc_sigpending(current);
1155                 spin_unlock_irq(&current->sigmask_lock);
1156         }
1157         
1158         return;
1159
1160 badframe:
1161 #if DEBUG_SIG
1162         printk("badframe in handle_signal32, regs=%p frame=%lx newsp=%lx\n",
1163                regs, frame, *newspp);
1164         printk("sc=%p sig=%d ka=%p info=%p oldset=%p\n", sc, sig, ka, info, oldset);
1165 #endif
1166         do_exit(SIGSEGV);
1167 }
1168
1169
1170 /*
1171  *  Start Alternate signal stack support
1172  *
1173  *  System Calls
1174  *       sigaltatck               sys32_sigaltstack
1175  */
1176
1177
1178 asmlinkage int sys32_sigaltstack(u32 newstack, u32 oldstack, int p3, int p4, int p6,
1179                                  int p7, struct pt_regs *regs)
1180 {
1181         stack_t uss, uoss;
1182         int ret;
1183         mm_segment_t old_fs;
1184         unsigned long sp;
1185
1186         /*
1187          * set sp to the user stack on entry to the system call
1188          * the system call router sets R9 to the saved registers
1189          */
1190         sp = regs->gpr[1];
1191
1192         /*  Put new stack info in local 64 bit stack struct                      */ 
1193         if (newstack && (get_user((long)uss.ss_sp, &((stack_32_t *)(long)newstack)->ss_sp) ||
1194                          __get_user(uss.ss_flags, &((stack_32_t *)(long)newstack)->ss_flags) ||
1195                          __get_user(uss.ss_size, &((stack_32_t *)(long)newstack)->ss_size)))
1196                 return -EFAULT; 
1197
1198    
1199         old_fs = get_fs();
1200         set_fs(KERNEL_DS);
1201         ret = do_sigaltstack(newstack ? &uss : NULL, oldstack ? &uoss : NULL, sp);
1202         set_fs(old_fs);
1203         /* Copy the stack information to the user output buffer                  */
1204         if (!ret && oldstack  && (put_user((long)uoss.ss_sp, &((stack_32_t *)(long)oldstack)->ss_sp) ||
1205                                   __put_user(uoss.ss_flags, &((stack_32_t *)(long)oldstack)->ss_flags) ||
1206                                   __put_user(uoss.ss_size, &((stack_32_t *)(long)oldstack)->ss_size)))
1207                 return -EFAULT;
1208         return ret;
1209 }
1210
1211
1212
1213 /*
1214  *  Start of do_signal32 routine
1215  *
1216  *   This routine gets control when a pending signal needs to be processed
1217  *     in the 32 bit target thread -
1218  *
1219  *   It handles both rt and non-rt signals
1220  */
1221
1222 /*
1223  * Note that 'init' is a special process: it doesn't get signals it doesn't
1224  * want to handle. Thus you cannot kill init even with a SIGKILL even by
1225  * mistake.
1226  */
1227
1228 int do_signal32(sigset_t *oldset, struct pt_regs *regs)
1229 {
1230         siginfo_t info;
1231         struct k_sigaction *ka;
1232         unsigned int frame, newsp;
1233
1234         if (!oldset)
1235                 oldset = &current->blocked;
1236
1237         newsp = frame = 0;
1238
1239         for (;;) {
1240                 unsigned long signr;
1241                 
1242                 spin_lock_irq(&current->sigmask_lock);
1243                 signr = dequeue_signal(&current->blocked, &info);
1244                 spin_unlock_irq(&current->sigmask_lock);
1245                 ifppcdebug(PPCDBG_SYS32) {
1246                         if (signr)
1247                                 udbg_printf("do_signal32 - processing signal=%2lx - pid=%ld, comm=%s \n", signr, current->pid, current->comm);
1248                 }
1249
1250                 if (!signr)
1251                         break;
1252
1253                 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1254                         /* Let the debugger run.  */
1255                         current->exit_code = signr;
1256                         current->state = TASK_STOPPED;
1257                         notify_parent(current, SIGCHLD);
1258                         schedule();
1259
1260                         /* We're back.  Did the debugger cancel the sig?  */
1261                         if (!(signr = current->exit_code))
1262                                 continue;
1263                         current->exit_code = 0;
1264
1265                         /* The debugger continued.  Ignore SIGSTOP.  */
1266                         if (signr == SIGSTOP)
1267                                 continue;
1268
1269                         /* Update the siginfo structure.  Is this good?  */
1270                         if (signr != info.si_signo) {
1271                                 info.si_signo = signr;
1272                                 info.si_errno = 0;
1273                                 info.si_code = SI_USER;
1274                                 info.si_pid = current->p_pptr->pid;
1275                                 info.si_uid = current->p_pptr->uid;
1276                         }
1277
1278                         /* If the (new) signal is now blocked, requeue it.  */
1279                         if (sigismember(&current->blocked, signr)) {
1280                                 send_sig_info(signr, &info, current);
1281                                 continue;
1282                         }
1283                 }
1284
1285                 ka = &current->sig->action[signr-1];
1286
1287                 if (ka->sa.sa_handler == SIG_IGN) {
1288                         if (signr != SIGCHLD)
1289                                 continue;
1290                         /* Check for SIGCHLD: it's special.  */
1291                         while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
1292                                 /* nothing */;
1293                         continue;
1294                 }
1295
1296                 if (ka->sa.sa_handler == SIG_DFL) {
1297                         int exit_code = signr;
1298
1299                         /* Init gets no signals it doesn't want.  */
1300                         if (current->pid == 1)
1301                                 continue;
1302
1303                         switch (signr) {
1304                         case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
1305                                 continue;
1306
1307                         case SIGTSTP: case SIGTTIN: case SIGTTOU:
1308                                 if (is_orphaned_pgrp(current->pgrp))
1309                                         continue;
1310                                 /* FALLTHRU */
1311
1312                         case SIGSTOP:
1313                                 current->state = TASK_STOPPED;
1314                                 current->exit_code = signr;
1315                                 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1316                                         notify_parent(current, SIGCHLD);
1317                                 schedule();
1318                                 continue;
1319
1320                         case SIGQUIT: case SIGILL: case SIGTRAP:
1321                         case SIGABRT: case SIGFPE: case SIGSEGV:
1322                         case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
1323                                 if (do_coredump(signr, regs))
1324                                         exit_code |= 0x80;
1325                                 /* FALLTHRU */
1326
1327                         default:
1328                                 sig_exit(signr, exit_code, &info);
1329                                 /* NOTREACHED */
1330                         }
1331                 }
1332
1333                 PPCDBG(PPCDBG_SIGNAL, " do signal :sigaction flags = %lx \n" ,ka->sa.sa_flags);
1334                 PPCDBG(PPCDBG_SIGNAL, " do signal :on sig stack  = %lx \n" ,on_sig_stack(regs->gpr[1]));
1335                 PPCDBG(PPCDBG_SIGNAL, " do signal :reg1  = %lx \n" ,regs->gpr[1]);
1336                 PPCDBG(PPCDBG_SIGNAL, " do signal :alt stack  = %lx \n" ,current->sas_ss_sp);
1337                 PPCDBG(PPCDBG_SIGNAL, " do signal :alt stack size  = %lx \n" ,current->sas_ss_size);
1338
1339
1340
1341                 if ( (ka->sa.sa_flags & SA_ONSTACK)
1342                      && (! on_sig_stack(regs->gpr[1])))
1343                         newsp = (current->sas_ss_sp + current->sas_ss_size);
1344                 else
1345                         newsp = regs->gpr[1];
1346                 newsp = frame = newsp - sizeof(struct sigregs32);
1347
1348                 /* Whee!  Actually deliver the signal.  */
1349                 handle_signal32(signr, ka, &info, oldset, regs, &newsp, frame);
1350                 break;
1351         }
1352
1353         if (regs->trap == 0x0C00 /* System Call! */ &&
1354             ((int)regs->result == -ERESTARTNOHAND ||
1355              (int)regs->result == -ERESTARTSYS ||
1356              (int)regs->result == -ERESTARTNOINTR)) {
1357                 regs->gpr[3] = regs->orig_gpr3;
1358                 regs->nip -= 4;         /* Back up & retry system call */
1359                 regs->result = 0;
1360         }
1361
1362         if (newsp == frame)
1363                 return 0;               /* no signals delivered */
1364
1365         /* Invoke correct stack setup routine */
1366         if (ka->sa.sa_flags & SA_SIGINFO) 
1367                 setup_rt_frame32(regs, (struct sigregs32*)(u64)frame, newsp);
1368         else
1369                 setup_frame32(regs, (struct sigregs32*)(u64)frame, newsp);
1370         return 1;
1371 }