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