signal: Remove kernel interal si_code magic
[linux-2.6-microblaze.git] / arch / s390 / kernel / compat_signal.c
1 /*
2  *    Copyright IBM Corp. 2000, 2006
3  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
4  *               Gerhard Tonn (ton@de.ibm.com)                  
5  *
6  *  Copyright (C) 1991, 1992  Linus Torvalds
7  *
8  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
9  */
10
11 #include <linux/compat.h>
12 #include <linux/sched.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/tty.h>
24 #include <linux/personality.h>
25 #include <linux/binfmts.h>
26 #include <asm/ucontext.h>
27 #include <linux/uaccess.h>
28 #include <asm/lowcore.h>
29 #include <asm/switch_to.h>
30 #include "compat_linux.h"
31 #include "compat_ptrace.h"
32 #include "entry.h"
33
34 typedef struct 
35 {
36         __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
37         struct sigcontext32 sc;
38         _sigregs32 sregs;
39         int signo;
40         _sigregs_ext32 sregs_ext;
41         __u16 svc_insn;         /* Offset of svc_insn is NOT fixed! */
42 } sigframe32;
43
44 typedef struct 
45 {
46         __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
47         __u16 svc_insn;
48         compat_siginfo_t info;
49         struct ucontext32 uc;
50 } rt_sigframe32;
51
52 static inline void sigset_to_sigset32(unsigned long *set64,
53                                       compat_sigset_word *set32)
54 {
55         set32[0] = (compat_sigset_word) set64[0];
56         set32[1] = (compat_sigset_word)(set64[0] >> 32);
57 }
58
59 static inline void sigset32_to_sigset(compat_sigset_word *set32,
60                                       unsigned long *set64)
61 {
62         set64[0] = (unsigned long) set32[0] | ((unsigned long) set32[1] << 32);
63 }
64
65 int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
66 {
67         int err;
68
69         /* If you change siginfo_t structure, please be sure
70            this code is fixed accordingly.
71            It should never copy any pad contained in the structure
72            to avoid security leaks, but must copy the generic
73            3 ints plus the relevant union member.  
74            This routine must convert siginfo from 64bit to 32bit as well
75            at the same time.  */
76         err = __put_user(from->si_signo, &to->si_signo);
77         err |= __put_user(from->si_errno, &to->si_errno);
78         err |= __put_user(from->si_code, &to->si_code);
79         if (from->si_code < 0)
80                 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
81         else {
82                 switch (siginfo_layout(from->si_signo, from->si_code)) {
83                 case SIL_RT:
84                         err |= __put_user(from->si_int, &to->si_int);
85                         /* fallthrough */
86                 case SIL_KILL:
87                         err |= __put_user(from->si_pid, &to->si_pid);
88                         err |= __put_user(from->si_uid, &to->si_uid);
89                         break;
90                 case SIL_CHLD:
91                         err |= __put_user(from->si_pid, &to->si_pid);
92                         err |= __put_user(from->si_uid, &to->si_uid);
93                         err |= __put_user(from->si_utime, &to->si_utime);
94                         err |= __put_user(from->si_stime, &to->si_stime);
95                         err |= __put_user(from->si_status, &to->si_status);
96                         break;
97                 case SIL_FAULT:
98                         err |= __put_user((unsigned long) from->si_addr,
99                                           &to->si_addr);
100                         break;
101                 case SIL_POLL:
102                         err |= __put_user(from->si_band, &to->si_band);
103                         err |= __put_user(from->si_fd, &to->si_fd);
104                         break;
105                 case SIL_TIMER:
106                         err |= __put_user(from->si_tid, &to->si_tid);
107                         err |= __put_user(from->si_overrun, &to->si_overrun);
108                         err |= __put_user(from->si_int, &to->si_int);
109                         break;
110                 default:
111                         break;
112                 }
113         }
114         return err ? -EFAULT : 0;
115 }
116
117 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
118 {
119         int err;
120         u32 tmp;
121
122         err = __get_user(to->si_signo, &from->si_signo);
123         err |= __get_user(to->si_errno, &from->si_errno);
124         err |= __get_user(to->si_code, &from->si_code);
125
126         if (to->si_code < 0)
127                 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
128         else {
129                 switch (siginfo_layout(to->si_signo, to->si_code)) {
130                 case SIL_RT:
131                         err |= __get_user(to->si_int, &from->si_int);
132                         /* fallthrough */
133                 case SIL_KILL:
134                         err |= __get_user(to->si_pid, &from->si_pid);
135                         err |= __get_user(to->si_uid, &from->si_uid);
136                         break;
137                 case SIL_CHLD:
138                         err |= __get_user(to->si_pid, &from->si_pid);
139                         err |= __get_user(to->si_uid, &from->si_uid);
140                         err |= __get_user(to->si_utime, &from->si_utime);
141                         err |= __get_user(to->si_stime, &from->si_stime);
142                         err |= __get_user(to->si_status, &from->si_status);
143                         break;
144                 case SIL_FAULT:
145                         err |= __get_user(tmp, &from->si_addr);
146                         to->si_addr = (void __force __user *)
147                                 (u64) (tmp & PSW32_ADDR_INSN);
148                         break;
149                 case SIL_POLL:
150                         err |= __get_user(to->si_band, &from->si_band);
151                         err |= __get_user(to->si_fd, &from->si_fd);
152                         break;
153                 case SIL_TIMER:
154                         err |= __get_user(to->si_tid, &from->si_tid);
155                         err |= __get_user(to->si_overrun, &from->si_overrun);
156                         err |= __get_user(to->si_int, &from->si_int);
157                         break;
158                 default:
159                         break;
160                 }
161         }
162         return err ? -EFAULT : 0;
163 }
164
165 /* Store registers needed to create the signal frame */
166 static void store_sigregs(void)
167 {
168         save_access_regs(current->thread.acrs);
169         save_fpu_regs();
170 }
171
172 /* Load registers after signal return */
173 static void load_sigregs(void)
174 {
175         restore_access_regs(current->thread.acrs);
176 }
177
178 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
179 {
180         _sigregs32 user_sregs;
181         int i;
182
183         user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32);
184         user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI;
185         user_sregs.regs.psw.mask |= PSW32_USER_BITS;
186         user_sregs.regs.psw.addr = (__u32) regs->psw.addr |
187                 (__u32)(regs->psw.mask & PSW_MASK_BA);
188         for (i = 0; i < NUM_GPRS; i++)
189                 user_sregs.regs.gprs[i] = (__u32) regs->gprs[i];
190         memcpy(&user_sregs.regs.acrs, current->thread.acrs,
191                sizeof(user_sregs.regs.acrs));
192         fpregs_store((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
193         if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32)))
194                 return -EFAULT;
195         return 0;
196 }
197
198 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
199 {
200         _sigregs32 user_sregs;
201         int i;
202
203         /* Alwys make any pending restarted system call return -EINTR */
204         current->restart_block.fn = do_no_restart_syscall;
205
206         if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs)))
207                 return -EFAULT;
208
209         if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI))
210                 return -EINVAL;
211
212         /* Test the floating-point-control word. */
213         if (test_fp_ctl(user_sregs.fpregs.fpc))
214                 return -EINVAL;
215
216         /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
217         regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
218                 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 |
219                 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 |
220                 (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE);
221         /* Check for invalid user address space control. */
222         if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
223                 regs->psw.mask = PSW_ASC_PRIMARY |
224                         (regs->psw.mask & ~PSW_MASK_ASC);
225         regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN);
226         for (i = 0; i < NUM_GPRS; i++)
227                 regs->gprs[i] = (__u64) user_sregs.regs.gprs[i];
228         memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
229                sizeof(current->thread.acrs));
230         fpregs_load((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
231
232         clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
233         return 0;
234 }
235
236 static int save_sigregs_ext32(struct pt_regs *regs,
237                               _sigregs_ext32 __user *sregs_ext)
238 {
239         __u32 gprs_high[NUM_GPRS];
240         __u64 vxrs[__NUM_VXRS_LOW];
241         int i;
242
243         /* Save high gprs to signal stack */
244         for (i = 0; i < NUM_GPRS; i++)
245                 gprs_high[i] = regs->gprs[i] >> 32;
246         if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high,
247                            sizeof(sregs_ext->gprs_high)))
248                 return -EFAULT;
249
250         /* Save vector registers to signal stack */
251         if (MACHINE_HAS_VX) {
252                 for (i = 0; i < __NUM_VXRS_LOW; i++)
253                         vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
254                 if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
255                                    sizeof(sregs_ext->vxrs_low)) ||
256                     __copy_to_user(&sregs_ext->vxrs_high,
257                                    current->thread.fpu.vxrs + __NUM_VXRS_LOW,
258                                    sizeof(sregs_ext->vxrs_high)))
259                         return -EFAULT;
260         }
261         return 0;
262 }
263
264 static int restore_sigregs_ext32(struct pt_regs *regs,
265                                  _sigregs_ext32 __user *sregs_ext)
266 {
267         __u32 gprs_high[NUM_GPRS];
268         __u64 vxrs[__NUM_VXRS_LOW];
269         int i;
270
271         /* Restore high gprs from signal stack */
272         if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high,
273                              sizeof(sregs_ext->gprs_high)))
274                 return -EFAULT;
275         for (i = 0; i < NUM_GPRS; i++)
276                 *(__u32 *)&regs->gprs[i] = gprs_high[i];
277
278         /* Restore vector registers from signal stack */
279         if (MACHINE_HAS_VX) {
280                 if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
281                                      sizeof(sregs_ext->vxrs_low)) ||
282                     __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
283                                      &sregs_ext->vxrs_high,
284                                      sizeof(sregs_ext->vxrs_high)))
285                         return -EFAULT;
286                 for (i = 0; i < __NUM_VXRS_LOW; i++)
287                         *((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
288         }
289         return 0;
290 }
291
292 COMPAT_SYSCALL_DEFINE0(sigreturn)
293 {
294         struct pt_regs *regs = task_pt_regs(current);
295         sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
296         compat_sigset_t cset;
297         sigset_t set;
298
299         if (__copy_from_user(&cset.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32))
300                 goto badframe;
301         sigset32_to_sigset(cset.sig, set.sig);
302         set_current_blocked(&set);
303         save_fpu_regs();
304         if (restore_sigregs32(regs, &frame->sregs))
305                 goto badframe;
306         if (restore_sigregs_ext32(regs, &frame->sregs_ext))
307                 goto badframe;
308         load_sigregs();
309         return regs->gprs[2];
310 badframe:
311         force_sig(SIGSEGV, current);
312         return 0;
313 }
314
315 COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
316 {
317         struct pt_regs *regs = task_pt_regs(current);
318         rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
319         compat_sigset_t cset;
320         sigset_t set;
321
322         if (__copy_from_user(&cset, &frame->uc.uc_sigmask, sizeof(cset)))
323                 goto badframe;
324         sigset32_to_sigset(cset.sig, set.sig);
325         set_current_blocked(&set);
326         if (compat_restore_altstack(&frame->uc.uc_stack))
327                 goto badframe;
328         save_fpu_regs();
329         if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
330                 goto badframe;
331         if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
332                 goto badframe;
333         load_sigregs();
334         return regs->gprs[2];
335 badframe:
336         force_sig(SIGSEGV, current);
337         return 0;
338 }       
339
340 /*
341  * Set up a signal frame.
342  */
343
344
345 /*
346  * Determine which stack to use..
347  */
348 static inline void __user *
349 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
350 {
351         unsigned long sp;
352
353         /* Default to using normal stack */
354         sp = (unsigned long) A(regs->gprs[15]);
355
356         /* Overflow on alternate signal stack gives SIGSEGV. */
357         if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
358                 return (void __user *) -1UL;
359
360         /* This is the X/Open sanctioned signal stack switching.  */
361         if (ka->sa.sa_flags & SA_ONSTACK) {
362                 if (! sas_ss_flags(sp))
363                         sp = current->sas_ss_sp + current->sas_ss_size;
364         }
365
366         return (void __user *)((sp - frame_size) & -8ul);
367 }
368
369 static int setup_frame32(struct ksignal *ksig, sigset_t *set,
370                          struct pt_regs *regs)
371 {
372         int sig = ksig->sig;
373         sigframe32 __user *frame;
374         struct sigcontext32 sc;
375         unsigned long restorer;
376         size_t frame_size;
377
378         /*
379          * gprs_high are always present for 31-bit compat tasks.
380          * The space for vector registers is only allocated if
381          * the machine supports it
382          */
383         frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved);
384         if (!MACHINE_HAS_VX)
385                 frame_size -= sizeof(frame->sregs_ext.vxrs_low) +
386                               sizeof(frame->sregs_ext.vxrs_high);
387         frame = get_sigframe(&ksig->ka, regs, frame_size);
388         if (frame == (void __user *) -1UL)
389                 return -EFAULT;
390
391         /* Set up backchain. */
392         if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
393                 return -EFAULT;
394
395         /* Create struct sigcontext32 on the signal stack */
396         sigset_to_sigset32(set->sig, sc.oldmask);
397         sc.sregs = (__u32)(unsigned long __force) &frame->sregs;
398         if (__copy_to_user(&frame->sc, &sc, sizeof(frame->sc)))
399                 return -EFAULT;
400
401         /* Store registers needed to create the signal frame */
402         store_sigregs();
403
404         /* Create _sigregs32 on the signal stack */
405         if (save_sigregs32(regs, &frame->sregs))
406                 return -EFAULT;
407
408         /* Place signal number on stack to allow backtrace from handler.  */
409         if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo))
410                 return -EFAULT;
411
412         /* Create _sigregs_ext32 on the signal stack */
413         if (save_sigregs_ext32(regs, &frame->sregs_ext))
414                 return -EFAULT;
415
416         /* Set up to return from userspace.  If provided, use a stub
417            already in userspace.  */
418         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
419                 restorer = (unsigned long __force)
420                         ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
421         } else {
422                 /* Signal frames without vectors registers are short ! */
423                 __u16 __user *svc = (void __user *) frame + frame_size - 2;
424                 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc))
425                         return -EFAULT;
426                 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
427         }
428
429         /* Set up registers for signal handler */
430         regs->gprs[14] = restorer;
431         regs->gprs[15] = (__force __u64) frame;
432         /* Force 31 bit amode and default user address space control. */
433         regs->psw.mask = PSW_MASK_BA |
434                 (PSW_USER_BITS & PSW_MASK_ASC) |
435                 (regs->psw.mask & ~PSW_MASK_ASC);
436         regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler;
437
438         regs->gprs[2] = sig;
439         regs->gprs[3] = (__force __u64) &frame->sc;
440
441         /* We forgot to include these in the sigcontext.
442            To avoid breaking binary compatibility, they are passed as args. */
443         if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
444             sig == SIGTRAP || sig == SIGFPE) {
445                 /* set extra registers only for synchronous signals */
446                 regs->gprs[4] = regs->int_code & 127;
447                 regs->gprs[5] = regs->int_parm_long;
448                 regs->gprs[6] = current->thread.last_break;
449         }
450
451         return 0;
452 }
453
454 static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set,
455                             struct pt_regs *regs)
456 {
457         compat_sigset_t cset;
458         rt_sigframe32 __user *frame;
459         unsigned long restorer;
460         size_t frame_size;
461         u32 uc_flags;
462
463         frame_size = sizeof(*frame) -
464                      sizeof(frame->uc.uc_mcontext_ext.__reserved);
465         /*
466          * gprs_high are always present for 31-bit compat tasks.
467          * The space for vector registers is only allocated if
468          * the machine supports it
469          */
470         uc_flags = UC_GPRS_HIGH;
471         if (MACHINE_HAS_VX) {
472                 uc_flags |= UC_VXRS;
473         } else
474                 frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) +
475                               sizeof(frame->uc.uc_mcontext_ext.vxrs_high);
476         frame = get_sigframe(&ksig->ka, regs, frame_size);
477         if (frame == (void __user *) -1UL)
478                 return -EFAULT;
479
480         /* Set up backchain. */
481         if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame))
482                 return -EFAULT;
483
484         /* Set up to return from userspace.  If provided, use a stub
485            already in userspace.  */
486         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
487                 restorer = (unsigned long __force)
488                         ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
489         } else {
490                 __u16 __user *svc = &frame->svc_insn;
491                 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc))
492                         return -EFAULT;
493                 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
494         }
495
496         /* Create siginfo on the signal stack */
497         if (copy_siginfo_to_user32(&frame->info, &ksig->info))
498                 return -EFAULT;
499
500         /* Store registers needed to create the signal frame */
501         store_sigregs();
502
503         /* Create ucontext on the signal stack. */
504         sigset_to_sigset32(set->sig, cset.sig);
505         if (__put_user(uc_flags, &frame->uc.uc_flags) ||
506             __put_user(0, &frame->uc.uc_link) ||
507             __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
508             save_sigregs32(regs, &frame->uc.uc_mcontext) ||
509             __copy_to_user(&frame->uc.uc_sigmask, &cset, sizeof(cset)) ||
510             save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
511                 return -EFAULT;
512
513         /* Set up registers for signal handler */
514         regs->gprs[14] = restorer;
515         regs->gprs[15] = (__force __u64) frame;
516         /* Force 31 bit amode and default user address space control. */
517         regs->psw.mask = PSW_MASK_BA |
518                 (PSW_USER_BITS & PSW_MASK_ASC) |
519                 (regs->psw.mask & ~PSW_MASK_ASC);
520         regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler;
521
522         regs->gprs[2] = ksig->sig;
523         regs->gprs[3] = (__force __u64) &frame->info;
524         regs->gprs[4] = (__force __u64) &frame->uc;
525         regs->gprs[5] = current->thread.last_break;
526         return 0;
527 }
528
529 /*
530  * OK, we're invoking a handler
531  */     
532
533 void handle_signal32(struct ksignal *ksig, sigset_t *oldset,
534                      struct pt_regs *regs)
535 {
536         int ret;
537
538         /* Set up the stack frame */
539         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
540                 ret = setup_rt_frame32(ksig, oldset, regs);
541         else
542                 ret = setup_frame32(ksig, oldset, regs);
543
544         signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
545 }
546