[PATCH] s390: bug in setup_rt_frame
[linux-2.6/verdex.git] / arch / s390 / kernel / signal.c
blobd48cfc726b68d9e2fbe3b7968c2283dd476e71de
1 /*
2 * arch/s390/kernel/signal.c
4 * Copyright (C) IBM Corp. 1999,2006
5 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
7 * Based on Intel version
8 *
9 * Copyright (C) 1991, 1992 Linus Torvalds
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
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>
33 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 typedef struct
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;
45 typedef struct
47 __u8 callee_used_stack[__SIGNAL_FRAMESIZE];
48 __u8 retcode[S390_SYSCALL_SIZE];
49 struct siginfo info;
50 struct ucontext uc;
51 } rt_sigframe;
54 * Atomically swap in the new signal mask, and wait for a signal.
56 asmlinkage int
57 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
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);
66 current->state = TASK_INTERRUPTIBLE;
67 schedule();
68 set_thread_flag(TIF_RESTORE_SIGMASK);
70 return -ERESTARTNOHAND;
73 asmlinkage long
74 sys_sigaction(int sig, const struct old_sigaction __user *act,
75 struct old_sigaction __user *oact)
77 struct k_sigaction new_ka, old_ka;
78 int ret;
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);
91 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
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);
102 return ret;
105 asmlinkage long
106 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
107 struct pt_regs *regs)
109 return do_sigaltstack(uss, uoss, regs->gprs[15]);
114 /* Returns non-zero on fault. */
115 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
117 unsigned long old_mask = regs->psw.mask;
118 int err;
120 save_access_regs(current->thread.acrs);
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;
135 * We have to store the fp registers to current->thread.fp_regs
136 * to merge them with the emulated registers.
138 save_fp_regs(&current->thread.fp_regs);
139 return __copy_to_user(&sregs->fpregs, &current->thread.fp_regs,
140 sizeof(s390_fp_regs));
143 /* Returns positive number on error */
144 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
146 unsigned long old_mask = regs->psw.mask;
147 int err;
149 /* Alwys make any pending restarted system call return -EINTR */
150 current_thread_info()->restart_block.fn = do_no_restart_syscall;
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);
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;
170 restore_fp_regs(&current->thread.fp_regs);
171 regs->trap = -1; /* disable syscall checks */
172 return 0;
175 asmlinkage long sys_sigreturn(struct pt_regs *regs)
177 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
178 sigset_t set;
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;
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);
191 if (restore_sigregs(regs, &frame->sregs))
192 goto badframe;
194 return regs->gprs[2];
196 badframe:
197 force_sig(SIGSEGV, current);
198 return 0;
201 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
203 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
204 sigset_t set;
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;
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);
217 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
218 goto badframe;
220 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
221 regs->gprs[15]) == -EFAULT)
222 goto badframe;
223 return regs->gprs[2];
225 badframe:
226 force_sig(SIGSEGV, current);
227 return 0;
231 * Set up a signal frame.
236 * Determine which stack to use..
238 static inline void __user *
239 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
241 unsigned long sp;
243 /* Default to using normal stack */
244 sp = regs->gprs[15];
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;
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;
259 return (void __user *)((sp - frame_size) & -8ul);
262 static inline int map_signal(int sig)
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;
272 static int setup_frame(int sig, struct k_sigaction *ka,
273 sigset_t *set, struct pt_regs * regs)
275 sigframe __user *frame;
277 frame = get_sigframe(ka, regs, sizeof(sigframe));
278 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
279 goto give_sigsegv;
281 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
282 goto give_sigsegv;
284 if (save_sigregs(regs, &frame->sregs))
285 goto give_sigsegv;
286 if (__put_user(&frame->sregs, &frame->sc.sregs))
287 goto give_sigsegv;
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;
302 /* Set up backchain. */
303 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
304 goto give_sigsegv;
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;
310 regs->gprs[2] = map_signal(sig);
311 regs->gprs[3] = (unsigned long) &frame->sc;
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;
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;
323 give_sigsegv:
324 force_sigsegv(sig, current);
325 return -EFAULT;
328 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
329 sigset_t *set, struct pt_regs * regs)
331 int err = 0;
332 rt_sigframe __user *frame;
334 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
335 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
336 goto give_sigsegv;
338 if (copy_siginfo_to_user(&frame->info, info))
339 goto give_sigsegv;
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;
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 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
362 (u16 __user *)(frame->retcode)))
363 goto give_sigsegv;
366 /* Set up backchain. */
367 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
368 goto give_sigsegv;
370 /* Set up registers for signal handler */
371 regs->gprs[15] = (unsigned long) frame;
372 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
374 regs->gprs[2] = map_signal(sig);
375 regs->gprs[3] = (unsigned long) &frame->info;
376 regs->gprs[4] = (unsigned long) &frame->uc;
377 return 0;
379 give_sigsegv:
380 force_sigsegv(sig, current);
381 return -EFAULT;
385 * OK, we're invoking a handler
388 static int
389 handle_signal(unsigned long sig, struct k_sigaction *ka,
390 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
392 int ret;
394 /* Set up the stack frame */
395 if (ka->sa.sa_flags & SA_SIGINFO)
396 ret = setup_rt_frame(sig, ka, info, oldset, regs);
397 else
398 ret = setup_frame(sig, ka, oldset, regs);
400 if (ret == 0) {
401 spin_lock_irq(&current->sighand->siglock);
402 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
403 if (!(ka->sa.sa_flags & SA_NODEFER))
404 sigaddset(&current->blocked,sig);
405 recalc_sigpending();
406 spin_unlock_irq(&current->sighand->siglock);
409 return ret;
413 * Note that 'init' is a special process: it doesn't get signals it doesn't
414 * want to handle. Thus you cannot kill init even with a SIGKILL even by
415 * mistake.
417 * Note that we go through the signals twice: once to check the signals that
418 * the kernel can handle, and then we build all the user-level signal handling
419 * stack-frames in one go after that.
421 void do_signal(struct pt_regs *regs)
423 unsigned long retval = 0, continue_addr = 0, restart_addr = 0;
424 siginfo_t info;
425 int signr;
426 struct k_sigaction ka;
427 sigset_t *oldset;
430 * We want the common case to go fast, which
431 * is why we may in certain cases get here from
432 * kernel mode. Just return without doing anything
433 * if so.
435 if (!user_mode(regs))
436 return;
438 if (test_thread_flag(TIF_RESTORE_SIGMASK))
439 oldset = &current->saved_sigmask;
440 else
441 oldset = &current->blocked;
443 /* Are we from a system call? */
444 if (regs->trap == __LC_SVC_OLD_PSW) {
445 continue_addr = regs->psw.addr;
446 restart_addr = continue_addr - regs->ilc;
447 retval = regs->gprs[2];
449 /* Prepare for system call restart. We do this here so that a
450 debugger will see the already changed PSW. */
451 switch (retval) {
452 case -ERESTARTNOHAND:
453 case -ERESTARTSYS:
454 case -ERESTARTNOINTR:
455 regs->gprs[2] = regs->orig_gpr2;
456 regs->psw.addr = restart_addr;
457 break;
458 case -ERESTART_RESTARTBLOCK:
459 regs->gprs[2] = -EINTR;
463 /* Get signal to deliver. When running under ptrace, at this point
464 the debugger may change all our registers ... */
465 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
467 /* Depending on the signal settings we may need to revert the
468 decision to restart the system call. */
469 if (signr > 0 && regs->psw.addr == restart_addr) {
470 if (retval == -ERESTARTNOHAND
471 || (retval == -ERESTARTSYS
472 && !(current->sighand->action[signr-1].sa.sa_flags
473 & SA_RESTART))) {
474 regs->gprs[2] = -EINTR;
475 regs->psw.addr = continue_addr;
479 if (signr > 0) {
480 /* Whee! Actually deliver the signal. */
481 #ifdef CONFIG_COMPAT
482 if (test_thread_flag(TIF_31BIT)) {
483 extern int handle_signal32(unsigned long sig,
484 struct k_sigaction *ka,
485 siginfo_t *info,
486 sigset_t *oldset,
487 struct pt_regs *regs);
488 if (handle_signal32(
489 signr, &ka, &info, oldset, regs) == 0) {
490 if (test_thread_flag(TIF_RESTORE_SIGMASK))
491 clear_thread_flag(TIF_RESTORE_SIGMASK);
493 return;
495 #endif
496 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
498 * A signal was successfully delivered; the saved
499 * sigmask will have been stored in the signal frame,
500 * and will be restored by sigreturn, so we can simply
501 * clear the TIF_RESTORE_SIGMASK flag.
503 if (test_thread_flag(TIF_RESTORE_SIGMASK))
504 clear_thread_flag(TIF_RESTORE_SIGMASK);
506 return;
510 * If there's no signal to deliver, we just put the saved sigmask back.
512 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
513 clear_thread_flag(TIF_RESTORE_SIGMASK);
514 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
517 /* Restart a different system call. */
518 if (retval == -ERESTART_RESTARTBLOCK
519 && regs->psw.addr == continue_addr) {
520 regs->gprs[2] = __NR_restart_syscall;
521 set_thread_flag(TIF_RESTART_SVC);