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/sched.h>
16 #include <linux/smp.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/stddef.h>
24 #include <linux/tty.h>
25 #include <linux/personality.h>
26 #include <linux/binfmts.h>
27 #include <linux/tracehook.h>
28 #include <linux/syscalls.h>
29 #include <linux/compat.h>
30 #include <asm/ucontext.h>
31 #include <asm/uaccess.h>
32 #include <asm/lowcore.h>
33 #include <asm/compat.h>
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
45 __u8 retcode
[S390_SYSCALL_SIZE
];
50 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
51 __u8 retcode
[S390_SYSCALL_SIZE
];
57 * Atomically swap in the new signal mask, and wait for a signal.
59 SYSCALL_DEFINE3(sigsuspend
, int, history0
, int, history1
, old_sigset_t
, mask
)
63 current
->saved_sigmask
= current
->blocked
;
65 siginitset(&blocked
, mask
);
66 set_current_blocked(&blocked
);
67 set_current_state(TASK_INTERRUPTIBLE
);
69 set_restore_sigmask();
70 return -ERESTARTNOHAND
;
73 SYSCALL_DEFINE3(sigaction
, int, sig
, const struct old_sigaction __user
*, act
,
74 struct old_sigaction __user
*, oact
)
76 struct k_sigaction new_ka
, old_ka
;
81 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
82 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
83 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
) ||
84 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
) ||
85 __get_user(mask
, &act
->sa_mask
))
87 siginitset(&new_ka
.sa
.sa_mask
, mask
);
90 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
93 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
94 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
95 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
) ||
96 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
) ||
97 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
))
104 SYSCALL_DEFINE2(sigaltstack
, const stack_t __user
*, uss
,
105 stack_t __user
*, uoss
)
107 struct pt_regs
*regs
= task_pt_regs(current
);
108 return do_sigaltstack(uss
, uoss
, regs
->gprs
[15]);
111 /* Returns non-zero on fault. */
112 static int save_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
116 save_access_regs(current
->thread
.acrs
);
118 /* Copy a 'clean' PSW mask to the user to avoid leaking
119 information about whether PER is currently on. */
120 user_sregs
.regs
.psw
.mask
= psw_user_bits
|
121 (regs
->psw
.mask
& PSW_MASK_USER
);
122 user_sregs
.regs
.psw
.addr
= regs
->psw
.addr
;
123 memcpy(&user_sregs
.regs
.gprs
, ®s
->gprs
, sizeof(sregs
->regs
.gprs
));
124 memcpy(&user_sregs
.regs
.acrs
, current
->thread
.acrs
,
125 sizeof(sregs
->regs
.acrs
));
127 * We have to store the fp registers to current->thread.fp_regs
128 * to merge them with the emulated registers.
130 save_fp_regs(¤t
->thread
.fp_regs
);
131 memcpy(&user_sregs
.fpregs
, ¤t
->thread
.fp_regs
,
132 sizeof(s390_fp_regs
));
133 return __copy_to_user(sregs
, &user_sregs
, sizeof(_sigregs
));
136 /* Returns positive number on error */
137 static int restore_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
142 /* Alwys make any pending restarted system call return -EINTR */
143 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
145 err
= __copy_from_user(&user_sregs
, sregs
, sizeof(_sigregs
));
148 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
149 regs
->psw
.mask
= (regs
->psw
.mask
& ~PSW_MASK_USER
) |
150 (user_sregs
.regs
.psw
.mask
& PSW_MASK_USER
);
151 /* Check for invalid amode */
152 if (regs
->psw
.mask
& PSW_MASK_EA
)
153 regs
->psw
.mask
|= PSW_MASK_BA
;
154 regs
->psw
.addr
= user_sregs
.regs
.psw
.addr
;
155 memcpy(®s
->gprs
, &user_sregs
.regs
.gprs
, sizeof(sregs
->regs
.gprs
));
156 memcpy(¤t
->thread
.acrs
, &user_sregs
.regs
.acrs
,
157 sizeof(sregs
->regs
.acrs
));
158 restore_access_regs(current
->thread
.acrs
);
160 memcpy(¤t
->thread
.fp_regs
, &user_sregs
.fpregs
,
161 sizeof(s390_fp_regs
));
162 current
->thread
.fp_regs
.fpc
&= FPC_VALID_MASK
;
164 restore_fp_regs(¤t
->thread
.fp_regs
);
165 clear_thread_flag(TIF_SYSCALL
); /* No longer in a system call */
169 SYSCALL_DEFINE0(sigreturn
)
171 struct pt_regs
*regs
= task_pt_regs(current
);
172 sigframe __user
*frame
= (sigframe __user
*)regs
->gprs
[15];
175 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
177 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE
))
179 sigdelsetmask(&set
, ~_BLOCKABLE
);
180 set_current_blocked(&set
);
181 if (restore_sigregs(regs
, &frame
->sregs
))
183 return regs
->gprs
[2];
185 force_sig(SIGSEGV
, current
);
189 SYSCALL_DEFINE0(rt_sigreturn
)
191 struct pt_regs
*regs
= task_pt_regs(current
);
192 rt_sigframe __user
*frame
= (rt_sigframe __user
*)regs
->gprs
[15];
195 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
197 if (__copy_from_user(&set
.sig
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
199 sigdelsetmask(&set
, ~_BLOCKABLE
);
200 set_current_blocked(&set
);
201 if (restore_sigregs(regs
, &frame
->uc
.uc_mcontext
))
203 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
,
204 regs
->gprs
[15]) == -EFAULT
)
206 return regs
->gprs
[2];
208 force_sig(SIGSEGV
, current
);
213 * Set up a signal frame.
218 * Determine which stack to use..
220 static inline void __user
*
221 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
225 /* Default to using normal stack */
228 /* Overflow on alternate signal stack gives SIGSEGV. */
229 if (on_sig_stack(sp
) && !on_sig_stack((sp
- frame_size
) & -8UL))
230 return (void __user
*) -1UL;
232 /* This is the X/Open sanctioned signal stack switching. */
233 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
234 if (! sas_ss_flags(sp
))
235 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
238 /* This is the legacy signal stack switching. */
239 else if (!user_mode(regs
) &&
240 !(ka
->sa
.sa_flags
& SA_RESTORER
) &&
241 ka
->sa
.sa_restorer
) {
242 sp
= (unsigned long) ka
->sa
.sa_restorer
;
245 return (void __user
*)((sp
- frame_size
) & -8ul);
248 static inline int map_signal(int sig
)
250 if (current_thread_info()->exec_domain
251 && current_thread_info()->exec_domain
->signal_invmap
253 return current_thread_info()->exec_domain
->signal_invmap
[sig
];
258 static int setup_frame(int sig
, struct k_sigaction
*ka
,
259 sigset_t
*set
, struct pt_regs
* regs
)
261 sigframe __user
*frame
;
263 frame
= get_sigframe(ka
, regs
, sizeof(sigframe
));
264 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(sigframe
)))
267 if (frame
== (void __user
*) -1UL)
270 if (__copy_to_user(&frame
->sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE
))
273 if (save_sigregs(regs
, &frame
->sregs
))
275 if (__put_user(&frame
->sregs
, &frame
->sc
.sregs
))
278 /* Set up to return from userspace. If provided, use a stub
279 already in userspace. */
280 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
281 regs
->gprs
[14] = (unsigned long)
282 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
284 regs
->gprs
[14] = (unsigned long)
285 frame
->retcode
| PSW_ADDR_AMODE
;
286 if (__put_user(S390_SYSCALL_OPCODE
| __NR_sigreturn
,
287 (u16 __user
*)(frame
->retcode
)))
291 /* Set up backchain. */
292 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
295 /* Set up registers for signal handler */
296 regs
->gprs
[15] = (unsigned long) frame
;
297 regs
->psw
.mask
|= PSW_MASK_EA
| PSW_MASK_BA
; /* 64 bit amode */
298 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
300 regs
->gprs
[2] = map_signal(sig
);
301 regs
->gprs
[3] = (unsigned long) &frame
->sc
;
303 /* We forgot to include these in the sigcontext.
304 To avoid breaking binary compatibility, they are passed as args. */
305 regs
->gprs
[4] = current
->thread
.trap_no
;
306 regs
->gprs
[5] = current
->thread
.prot_addr
;
307 regs
->gprs
[6] = task_thread_info(current
)->last_break
;
309 /* Place signal number on stack to allow backtrace from handler. */
310 if (__put_user(regs
->gprs
[2], (int __user
*) &frame
->signo
))
315 force_sigsegv(sig
, current
);
319 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
320 sigset_t
*set
, struct pt_regs
* regs
)
323 rt_sigframe __user
*frame
;
325 frame
= get_sigframe(ka
, regs
, sizeof(rt_sigframe
));
326 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(rt_sigframe
)))
329 if (frame
== (void __user
*) -1UL)
332 if (copy_siginfo_to_user(&frame
->info
, info
))
335 /* Create the ucontext. */
336 err
|= __put_user(0, &frame
->uc
.uc_flags
);
337 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
338 err
|= __put_user((void __user
*)current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
339 err
|= __put_user(sas_ss_flags(regs
->gprs
[15]),
340 &frame
->uc
.uc_stack
.ss_flags
);
341 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
342 err
|= save_sigregs(regs
, &frame
->uc
.uc_mcontext
);
343 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
347 /* Set up to return from userspace. If provided, use a stub
348 already in userspace. */
349 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
350 regs
->gprs
[14] = (unsigned long)
351 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
353 regs
->gprs
[14] = (unsigned long)
354 frame
->retcode
| PSW_ADDR_AMODE
;
355 if (__put_user(S390_SYSCALL_OPCODE
| __NR_rt_sigreturn
,
356 (u16 __user
*)(frame
->retcode
)))
360 /* Set up backchain. */
361 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
364 /* Set up registers for signal handler */
365 regs
->gprs
[15] = (unsigned long) frame
;
366 regs
->psw
.mask
|= PSW_MASK_EA
| PSW_MASK_BA
; /* 64 bit amode */
367 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
369 regs
->gprs
[2] = map_signal(sig
);
370 regs
->gprs
[3] = (unsigned long) &frame
->info
;
371 regs
->gprs
[4] = (unsigned long) &frame
->uc
;
372 regs
->gprs
[5] = task_thread_info(current
)->last_break
;
376 force_sigsegv(sig
, current
);
380 static int handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
381 siginfo_t
*info
, sigset_t
*oldset
,
382 struct pt_regs
*regs
)
387 /* Set up the stack frame */
388 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
389 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
391 ret
= setup_frame(sig
, ka
, oldset
, regs
);
394 sigorsets(&blocked
, ¤t
->blocked
, &ka
->sa
.sa_mask
);
395 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
396 sigaddset(&blocked
, sig
);
397 set_current_blocked(&blocked
);
402 * Note that 'init' is a special process: it doesn't get signals it doesn't
403 * want to handle. Thus you cannot kill init even with a SIGKILL even by
406 * Note that we go through the signals twice: once to check the signals that
407 * the kernel can handle, and then we build all the user-level signal handling
408 * stack-frames in one go after that.
410 void do_signal(struct pt_regs
*regs
)
414 struct k_sigaction ka
;
418 * We want the common case to go fast, which
419 * is why we may in certain cases get here from
420 * kernel mode. Just return without doing anything
423 if (!user_mode(regs
))
426 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
427 oldset
= ¤t
->saved_sigmask
;
429 oldset
= ¤t
->blocked
;
432 * Get signal to deliver. When running under ptrace, at this point
433 * the debugger may change all our registers, including the system
436 current_thread_info()->system_call
=
437 test_thread_flag(TIF_SYSCALL
) ? regs
->svc_code
: 0;
438 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
441 /* Whee! Actually deliver the signal. */
442 if (current_thread_info()->system_call
) {
443 regs
->svc_code
= current_thread_info()->system_call
;
444 /* Check for system call restarting. */
445 switch (regs
->gprs
[2]) {
446 case -ERESTART_RESTARTBLOCK
:
447 case -ERESTARTNOHAND
:
448 regs
->gprs
[2] = -EINTR
;
451 if (!(ka
.sa
.sa_flags
& SA_RESTART
)) {
452 regs
->gprs
[2] = -EINTR
;
456 case -ERESTARTNOINTR
:
457 regs
->gprs
[2] = regs
->orig_gpr2
;
459 __rewind_psw(regs
->psw
,
460 regs
->svc_code
>> 16);
464 /* No longer in a system call */
465 clear_thread_flag(TIF_SYSCALL
);
467 if ((is_compat_task() ?
468 handle_signal32(signr
, &ka
, &info
, oldset
, regs
) :
469 handle_signal(signr
, &ka
, &info
, oldset
, regs
)) == 0) {
471 * A signal was successfully delivered; the saved
472 * sigmask will have been stored in the signal frame,
473 * and will be restored by sigreturn, so we can simply
474 * clear the TIF_RESTORE_SIGMASK flag.
476 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
477 clear_thread_flag(TIF_RESTORE_SIGMASK
);
480 * Let tracing know that we've done the handler setup.
482 tracehook_signal_handler(signr
, &info
, &ka
, regs
,
483 test_thread_flag(TIF_SINGLE_STEP
));
488 /* No handlers present - check for system call restart */
489 clear_thread_flag(TIF_SYSCALL
);
490 if (current_thread_info()->system_call
) {
491 regs
->svc_code
= current_thread_info()->system_call
;
492 switch (regs
->gprs
[2]) {
493 case -ERESTART_RESTARTBLOCK
:
494 /* Restart with sys_restart_syscall */
495 regs
->svc_code
= __NR_restart_syscall
;
497 case -ERESTARTNOHAND
:
499 case -ERESTARTNOINTR
:
500 /* Restart system call with magic TIF bit. */
501 regs
->gprs
[2] = regs
->orig_gpr2
;
502 set_thread_flag(TIF_SYSCALL
);
508 * If there's no signal to deliver, we just put the saved sigmask back.
510 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
511 clear_thread_flag(TIF_RESTORE_SIGMASK
);
512 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
516 void do_notify_resume(struct pt_regs
*regs
)
518 clear_thread_flag(TIF_NOTIFY_RESUME
);
519 tracehook_notify_resume(regs
);
520 if (current
->replacement_session_keyring
)
521 key_replace_session_keyring();