Merge branch 'for-jeff' of git://electric-eye.fr.zoreil.com/home/romieu/linux-2.6
[linux-2.6] / arch / s390 / kernel / signal.c
1 /*
2  *  arch/s390/kernel/signal.c
3  *
4  *    Copyright (C) IBM Corp. 1999,2006
5  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6  *
7  *    Based on Intel version
8  * 
9  *  Copyright (C) 1991, 1992  Linus Torvalds
10  *
11  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
12  */
13
14 #include <linux/config.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/kernel.h>
20 #include <linux/signal.h>
21 #include <linux/errno.h>
22 #include <linux/wait.h>
23 #include <linux/ptrace.h>
24 #include <linux/unistd.h>
25 #include <linux/stddef.h>
26 #include <linux/tty.h>
27 #include <linux/personality.h>
28 #include <linux/binfmts.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31 #include <asm/lowcore.h>
32
33 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34
35
36 typedef struct 
37 {
38         __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
39         struct sigcontext sc;
40         _sigregs sregs;
41         int signo;
42         __u8 retcode[S390_SYSCALL_SIZE];
43 } sigframe;
44
45 typedef struct 
46 {
47         __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
48         __u8 retcode[S390_SYSCALL_SIZE];
49         struct siginfo info;
50         struct ucontext uc;
51 } rt_sigframe;
52
53 /*
54  * Atomically swap in the new signal mask, and wait for a signal.
55  */
56 asmlinkage int
57 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
58 {
59         mask &= _BLOCKABLE;
60         spin_lock_irq(&current->sighand->siglock);
61         current->saved_sigmask = current->blocked;
62         siginitset(&current->blocked, mask);
63         recalc_sigpending();
64         spin_unlock_irq(&current->sighand->siglock);
65
66         current->state = TASK_INTERRUPTIBLE;
67         schedule();
68         set_thread_flag(TIF_RESTORE_SIGMASK);
69
70         return -ERESTARTNOHAND;
71 }
72
73 asmlinkage long
74 sys_sigaction(int sig, const struct old_sigaction __user *act,
75               struct old_sigaction __user *oact)
76 {
77         struct k_sigaction new_ka, old_ka;
78         int ret;
79
80         if (act) {
81                 old_sigset_t mask;
82                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
83                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
84                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
85                         return -EFAULT;
86                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
87                 __get_user(mask, &act->sa_mask);
88                 siginitset(&new_ka.sa.sa_mask, mask);
89         }
90
91         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
92
93         if (!ret && oact) {
94                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
95                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
96                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
97                         return -EFAULT;
98                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
99                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
100         }
101
102         return ret;
103 }
104
105 asmlinkage long
106 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
107                                         struct pt_regs *regs)
108 {
109         return do_sigaltstack(uss, uoss, regs->gprs[15]);
110 }
111
112
113
114 /* Returns non-zero on fault. */
115 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
116 {
117         unsigned long old_mask = regs->psw.mask;
118         int err;
119   
120         save_access_regs(current->thread.acrs);
121
122         /* Copy a 'clean' PSW mask to the user to avoid leaking
123            information about whether PER is currently on.  */
124         regs->psw.mask = PSW_MASK_MERGE(PSW_USER_BITS, regs->psw.mask);
125         err = __copy_to_user(&sregs->regs.psw, &regs->psw,
126                              sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
127         regs->psw.mask = old_mask;
128         if (err != 0)
129                 return err;
130         err = __copy_to_user(&sregs->regs.acrs, current->thread.acrs,
131                              sizeof(sregs->regs.acrs));
132         if (err != 0)
133                 return err;
134         /* 
135          * We have to store the fp registers to current->thread.fp_regs
136          * to merge them with the emulated registers.
137          */
138         save_fp_regs(&current->thread.fp_regs);
139         return __copy_to_user(&sregs->fpregs, &current->thread.fp_regs,
140                               sizeof(s390_fp_regs));
141 }
142
143 /* Returns positive number on error */
144 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
145 {
146         unsigned long old_mask = regs->psw.mask;
147         int err;
148
149         /* Alwys make any pending restarted system call return -EINTR */
150         current_thread_info()->restart_block.fn = do_no_restart_syscall;
151
152         err = __copy_from_user(&regs->psw, &sregs->regs.psw,
153                                sizeof(sregs->regs.psw)+sizeof(sregs->regs.gprs));
154         regs->psw.mask = PSW_MASK_MERGE(old_mask, regs->psw.mask);
155         regs->psw.addr |= PSW_ADDR_AMODE;
156         if (err)
157                 return err;
158         err = __copy_from_user(&current->thread.acrs, &sregs->regs.acrs,
159                                sizeof(sregs->regs.acrs));
160         if (err)
161                 return err;
162         restore_access_regs(current->thread.acrs);
163
164         err = __copy_from_user(&current->thread.fp_regs, &sregs->fpregs,
165                                sizeof(s390_fp_regs));
166         current->thread.fp_regs.fpc &= FPC_VALID_MASK;
167         if (err)
168                 return err;
169
170         restore_fp_regs(&current->thread.fp_regs);
171         regs->trap = -1;        /* disable syscall checks */
172         return 0;
173 }
174
175 asmlinkage long sys_sigreturn(struct pt_regs *regs)
176 {
177         sigframe __user *frame = (sigframe __user *)regs->gprs[15];
178         sigset_t set;
179
180         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
181                 goto badframe;
182         if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
183                 goto badframe;
184
185         sigdelsetmask(&set, ~_BLOCKABLE);
186         spin_lock_irq(&current->sighand->siglock);
187         current->blocked = set;
188         recalc_sigpending();
189         spin_unlock_irq(&current->sighand->siglock);
190
191         if (restore_sigregs(regs, &frame->sregs))
192                 goto badframe;
193
194         return regs->gprs[2];
195
196 badframe:
197         force_sig(SIGSEGV, current);
198         return 0;
199 }
200
201 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
202 {
203         rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
204         sigset_t set;
205
206         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
207                 goto badframe;
208         if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
209                 goto badframe;
210
211         sigdelsetmask(&set, ~_BLOCKABLE);
212         spin_lock_irq(&current->sighand->siglock);
213         current->blocked = set;
214         recalc_sigpending();
215         spin_unlock_irq(&current->sighand->siglock);
216
217         if (restore_sigregs(regs, &frame->uc.uc_mcontext))
218                 goto badframe;
219
220         if (do_sigaltstack(&frame->uc.uc_stack, NULL,
221                            regs->gprs[15]) == -EFAULT)
222                 goto badframe;
223         return regs->gprs[2];
224
225 badframe:
226         force_sig(SIGSEGV, current);
227         return 0;
228 }
229
230 /*
231  * Set up a signal frame.
232  */
233
234
235 /*
236  * Determine which stack to use..
237  */
238 static inline void __user *
239 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
240 {
241         unsigned long sp;
242
243         /* Default to using normal stack */
244         sp = regs->gprs[15];
245
246         /* This is the X/Open sanctioned signal stack switching.  */
247         if (ka->sa.sa_flags & SA_ONSTACK) {
248                 if (! sas_ss_flags(sp))
249                         sp = current->sas_ss_sp + current->sas_ss_size;
250         }
251
252         /* This is the legacy signal stack switching. */
253         else if (!user_mode(regs) &&
254                  !(ka->sa.sa_flags & SA_RESTORER) &&
255                  ka->sa.sa_restorer) {
256                 sp = (unsigned long) ka->sa.sa_restorer;
257         }
258
259         return (void __user *)((sp - frame_size) & -8ul);
260 }
261
262 static inline int map_signal(int sig)
263 {
264         if (current_thread_info()->exec_domain
265             && current_thread_info()->exec_domain->signal_invmap
266             && sig < 32)
267                 return current_thread_info()->exec_domain->signal_invmap[sig];
268         else
269                 return sig;
270 }
271
272 static int setup_frame(int sig, struct k_sigaction *ka,
273                        sigset_t *set, struct pt_regs * regs)
274 {
275         sigframe __user *frame;
276
277         frame = get_sigframe(ka, regs, sizeof(sigframe));
278         if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
279                 goto give_sigsegv;
280
281         if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
282                 goto give_sigsegv;
283
284         if (save_sigregs(regs, &frame->sregs))
285                 goto give_sigsegv;
286         if (__put_user(&frame->sregs, &frame->sc.sregs))
287                 goto give_sigsegv;
288
289         /* Set up to return from userspace.  If provided, use a stub
290            already in userspace.  */
291         if (ka->sa.sa_flags & SA_RESTORER) {
292                 regs->gprs[14] = (unsigned long)
293                         ka->sa.sa_restorer | PSW_ADDR_AMODE;
294         } else {
295                 regs->gprs[14] = (unsigned long)
296                         frame->retcode | PSW_ADDR_AMODE;
297                 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
298                                (u16 __user *)(frame->retcode)))
299                         goto give_sigsegv;
300         }
301
302         /* Set up backchain. */
303         if (__put_user(regs->gprs[15], (addr_t __user *) frame))
304                 goto give_sigsegv;
305
306         /* Set up registers for signal handler */
307         regs->gprs[15] = (unsigned long) frame;
308         regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
309
310         regs->gprs[2] = map_signal(sig);
311         regs->gprs[3] = (unsigned long) &frame->sc;
312
313         /* We forgot to include these in the sigcontext.
314            To avoid breaking binary compatibility, they are passed as args. */
315         regs->gprs[4] = current->thread.trap_no;
316         regs->gprs[5] = current->thread.prot_addr;
317
318         /* Place signal number on stack to allow backtrace from handler.  */
319         if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
320                 goto give_sigsegv;
321         return 0;
322
323 give_sigsegv:
324         force_sigsegv(sig, current);
325         return -EFAULT;
326 }
327
328 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
329                            sigset_t *set, struct pt_regs * regs)
330 {
331         int err = 0;
332         rt_sigframe __user *frame;
333
334         frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
335         if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
336                 goto give_sigsegv;
337
338         if (copy_siginfo_to_user(&frame->info, info))
339                 goto give_sigsegv;
340
341         /* Create the ucontext.  */
342         err |= __put_user(0, &frame->uc.uc_flags);
343         err |= __put_user(NULL, &frame->uc.uc_link);
344         err |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
345         err |= __put_user(sas_ss_flags(regs->gprs[15]),
346                           &frame->uc.uc_stack.ss_flags);
347         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
348         err |= save_sigregs(regs, &frame->uc.uc_mcontext);
349         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
350         if (err)
351                 goto give_sigsegv;
352
353         /* Set up to return from userspace.  If provided, use a stub
354            already in userspace.  */
355         if (ka->sa.sa_flags & SA_RESTORER) {
356                 regs->gprs[14] = (unsigned long)
357                         ka->sa.sa_restorer | PSW_ADDR_AMODE;
358         } else {
359                 regs->gprs[14] = (unsigned long)
360                         frame->retcode | PSW_ADDR_AMODE;
361                 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
362                                   (u16 __user *)(frame->retcode));
363         }
364
365         /* Set up backchain. */
366         if (__put_user(regs->gprs[15], (addr_t __user *) frame))
367                 goto give_sigsegv;
368
369         /* Set up registers for signal handler */
370         regs->gprs[15] = (unsigned long) frame;
371         regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
372
373         regs->gprs[2] = map_signal(sig);
374         regs->gprs[3] = (unsigned long) &frame->info;
375         regs->gprs[4] = (unsigned long) &frame->uc;
376         return 0;
377
378 give_sigsegv:
379         force_sigsegv(sig, current);
380         return -EFAULT;
381 }
382
383 /*
384  * OK, we're invoking a handler
385  */     
386
387 static int
388 handle_signal(unsigned long sig, struct k_sigaction *ka,
389               siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
390 {
391         int ret;
392
393         /* Set up the stack frame */
394         if (ka->sa.sa_flags & SA_SIGINFO)
395                 ret = setup_rt_frame(sig, ka, info, oldset, regs);
396         else
397                 ret = setup_frame(sig, ka, oldset, regs);
398
399         if (ret == 0) {
400                 spin_lock_irq(&current->sighand->siglock);
401                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
402                 if (!(ka->sa.sa_flags & SA_NODEFER))
403                         sigaddset(&current->blocked,sig);
404                 recalc_sigpending();
405                 spin_unlock_irq(&current->sighand->siglock);
406         }
407
408         return ret;
409 }
410
411 /*
412  * Note that 'init' is a special process: it doesn't get signals it doesn't
413  * want to handle. Thus you cannot kill init even with a SIGKILL even by
414  * mistake.
415  *
416  * Note that we go through the signals twice: once to check the signals that
417  * the kernel can handle, and then we build all the user-level signal handling
418  * stack-frames in one go after that.
419  */
420 void do_signal(struct pt_regs *regs)
421 {
422         unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
423         siginfo_t info;
424         int signr;
425         struct k_sigaction ka;
426         sigset_t *oldset;
427
428         /*
429          * We want the common case to go fast, which
430          * is why we may in certain cases get here from
431          * kernel mode. Just return without doing anything
432          * if so.
433          */
434         if (!user_mode(regs))
435                 return;
436
437         if (test_thread_flag(TIF_RESTORE_SIGMASK))
438                 oldset = &current->saved_sigmask;
439         else
440                 oldset = &current->blocked;
441
442         /* Are we from a system call? */
443         if (regs->trap == __LC_SVC_OLD_PSW) {
444                 continue_addr = regs->psw.addr;
445                 restart_addr = continue_addr - regs->ilc;
446                 retval = regs->gprs[2];
447
448                 /* Prepare for system call restart.  We do this here so that a
449                    debugger will see the already changed PSW. */
450                 switch (retval) {
451                 case -ERESTARTNOHAND:
452                 case -ERESTARTSYS:
453                 case -ERESTARTNOINTR:
454                         regs->gprs[2] = regs->orig_gpr2;
455                         regs->psw.addr = restart_addr;
456                         break;
457                 case -ERESTART_RESTARTBLOCK:
458                         regs->gprs[2] = -EINTR;
459                 }
460         }
461
462         /* Get signal to deliver.  When running under ptrace, at this point
463            the debugger may change all our registers ... */
464         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
465
466         /* Depending on the signal settings we may need to revert the
467            decision to restart the system call. */
468         if (signr > 0 && regs->psw.addr == restart_addr) {
469                 if (retval == -ERESTARTNOHAND
470                     || (retval == -ERESTARTSYS
471                          && !(current->sighand->action[signr-1].sa.sa_flags
472                               & SA_RESTART))) {
473                         regs->gprs[2] = -EINTR;
474                         regs->psw.addr = continue_addr;
475                 }
476         }
477
478         if (signr > 0) {
479                 /* Whee!  Actually deliver the signal.  */
480 #ifdef CONFIG_COMPAT
481                 if (test_thread_flag(TIF_31BIT)) {
482                         extern int handle_signal32(unsigned long sig,
483                                                    struct k_sigaction *ka,
484                                                    siginfo_t *info,
485                                                    sigset_t *oldset,
486                                                    struct pt_regs *regs);
487                         if (handle_signal32(
488                                     signr, &ka, &info, oldset, regs) == 0) {
489                                 if (test_thread_flag(TIF_RESTORE_SIGMASK))
490                                         clear_thread_flag(TIF_RESTORE_SIGMASK);
491                         }
492                         return;
493                 }
494 #endif
495                 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
496                         /*
497                          * A signal was successfully delivered; the saved
498                          * sigmask will have been stored in the signal frame,
499                          * and will be restored by sigreturn, so we can simply
500                          * clear the TIF_RESTORE_SIGMASK flag.
501                          */
502                         if (test_thread_flag(TIF_RESTORE_SIGMASK))
503                                 clear_thread_flag(TIF_RESTORE_SIGMASK);
504                 }
505                 return;
506         }
507
508         /*
509          * If there's no signal to deliver, we just put the saved sigmask back.
510          */
511         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
512                 clear_thread_flag(TIF_RESTORE_SIGMASK);
513                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
514         }
515
516         /* Restart a different system call. */
517         if (retval == -ERESTART_RESTARTBLOCK
518             && regs->psw.addr == continue_addr) {
519                 regs->gprs[2] = __NR_restart_syscall;
520                 set_thread_flag(TIF_RESTART_SVC);
521         }
522 }