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
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>
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)))
38 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
42 __u8 retcode
[S390_SYSCALL_SIZE
];
47 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
48 __u8 retcode
[S390_SYSCALL_SIZE
];
54 * Atomically swap in the new signal mask, and wait for a signal.
57 sys_sigsuspend(int history0
, int history1
, old_sigset_t mask
)
60 spin_lock_irq(¤t
->sighand
->siglock
);
61 current
->saved_sigmask
= current
->blocked
;
62 siginitset(¤t
->blocked
, mask
);
64 spin_unlock_irq(¤t
->sighand
->siglock
);
66 current
->state
= TASK_INTERRUPTIBLE
;
68 set_thread_flag(TIF_RESTORE_SIGMASK
);
70 return -ERESTARTNOHAND
;
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
;
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
))
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
);
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
))
98 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
99 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
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
;
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
, ®s
->psw
,
126 sizeof(sregs
->regs
.psw
)+sizeof(sregs
->regs
.gprs
));
127 regs
->psw
.mask
= old_mask
;
130 err
= __copy_to_user(&sregs
->regs
.acrs
, current
->thread
.acrs
,
131 sizeof(sregs
->regs
.acrs
));
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(¤t
->thread
.fp_regs
);
139 return __copy_to_user(&sregs
->fpregs
, ¤t
->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
;
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(®s
->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
;
158 err
= __copy_from_user(¤t
->thread
.acrs
, &sregs
->regs
.acrs
,
159 sizeof(sregs
->regs
.acrs
));
162 restore_access_regs(current
->thread
.acrs
);
164 err
= __copy_from_user(¤t
->thread
.fp_regs
, &sregs
->fpregs
,
165 sizeof(s390_fp_regs
));
166 current
->thread
.fp_regs
.fpc
&= FPC_VALID_MASK
;
170 restore_fp_regs(¤t
->thread
.fp_regs
);
171 regs
->trap
= -1; /* disable syscall checks */
175 asmlinkage
long sys_sigreturn(struct pt_regs
*regs
)
177 sigframe __user
*frame
= (sigframe __user
*)regs
->gprs
[15];
180 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
182 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE
))
185 sigdelsetmask(&set
, ~_BLOCKABLE
);
186 spin_lock_irq(¤t
->sighand
->siglock
);
187 current
->blocked
= set
;
189 spin_unlock_irq(¤t
->sighand
->siglock
);
191 if (restore_sigregs(regs
, &frame
->sregs
))
194 return regs
->gprs
[2];
197 force_sig(SIGSEGV
, current
);
201 asmlinkage
long sys_rt_sigreturn(struct pt_regs
*regs
)
203 rt_sigframe __user
*frame
= (rt_sigframe __user
*)regs
->gprs
[15];
206 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
208 if (__copy_from_user(&set
.sig
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
211 sigdelsetmask(&set
, ~_BLOCKABLE
);
212 spin_lock_irq(¤t
->sighand
->siglock
);
213 current
->blocked
= set
;
215 spin_unlock_irq(¤t
->sighand
->siglock
);
217 if (restore_sigregs(regs
, &frame
->uc
.uc_mcontext
))
220 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
,
221 regs
->gprs
[15]) == -EFAULT
)
223 return regs
->gprs
[2];
226 force_sig(SIGSEGV
, current
);
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
)
243 /* Default to using normal stack */
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
267 return current_thread_info()->exec_domain
->signal_invmap
[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
)))
281 if (__copy_to_user(&frame
->sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE
))
284 if (save_sigregs(regs
, &frame
->sregs
))
286 if (__put_user(&frame
->sregs
, &frame
->sc
.sregs
))
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
;
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
)))
302 /* Set up backchain. */
303 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
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
))
324 force_sigsegv(sig
, current
);
328 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
329 sigset_t
*set
, struct pt_regs
* regs
)
332 rt_sigframe __user
*frame
;
334 frame
= get_sigframe(ka
, regs
, sizeof(rt_sigframe
));
335 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(rt_sigframe
)))
338 if (copy_siginfo_to_user(&frame
->info
, info
))
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
));
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
;
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
)))
366 /* Set up backchain. */
367 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
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
;
380 force_sigsegv(sig
, current
);
385 * OK, we're invoking a handler
389 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
390 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
* regs
)
394 /* Set up the stack frame */
395 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
396 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
398 ret
= setup_frame(sig
, ka
, oldset
, regs
);
401 spin_lock_irq(¤t
->sighand
->siglock
);
402 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
403 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
404 sigaddset(¤t
->blocked
,sig
);
406 spin_unlock_irq(¤t
->sighand
->siglock
);
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
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;
426 struct k_sigaction ka
;
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
435 if (!user_mode(regs
))
438 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
439 oldset
= ¤t
->saved_sigmask
;
441 oldset
= ¤t
->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. */
452 case -ERESTARTNOHAND
:
454 case -ERESTARTNOINTR
:
455 regs
->gprs
[2] = regs
->orig_gpr2
;
456 regs
->psw
.addr
= restart_addr
;
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
474 regs
->gprs
[2] = -EINTR
;
475 regs
->psw
.addr
= continue_addr
;
480 /* Whee! Actually deliver the signal. */
482 if (test_thread_flag(TIF_31BIT
)) {
483 extern int handle_signal32(unsigned long sig
,
484 struct k_sigaction
*ka
,
487 struct pt_regs
*regs
);
489 signr
, &ka
, &info
, oldset
, regs
) == 0) {
490 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
491 clear_thread_flag(TIF_RESTORE_SIGMASK
);
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
);
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
, ¤t
->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
);