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