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/smp_lock.h>
18 #include <linux/kernel.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/wait.h>
22 #include <linux/ptrace.h>
23 #include <linux/unistd.h>
24 #include <linux/stddef.h>
25 #include <linux/tty.h>
26 #include <linux/personality.h>
27 #include <linux/binfmts.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/lowcore.h>
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
41 __u8 retcode
[S390_SYSCALL_SIZE
];
46 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
47 __u8 retcode
[S390_SYSCALL_SIZE
];
53 * Atomically swap in the new signal mask, and wait for a signal.
56 sys_sigsuspend(int history0
, int history1
, old_sigset_t mask
)
59 spin_lock_irq(¤t
->sighand
->siglock
);
60 current
->saved_sigmask
= current
->blocked
;
61 siginitset(¤t
->blocked
, mask
);
63 spin_unlock_irq(¤t
->sighand
->siglock
);
65 current
->state
= TASK_INTERRUPTIBLE
;
67 set_thread_flag(TIF_RESTORE_SIGMASK
);
69 return -ERESTARTNOHAND
;
73 sys_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
))
85 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
86 __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
))
97 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
98 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
105 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
,
106 struct pt_regs
*regs
)
108 return do_sigaltstack(uss
, uoss
, regs
->gprs
[15]);
113 /* Returns non-zero on fault. */
114 static int save_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
116 unsigned long old_mask
= regs
->psw
.mask
;
119 save_access_regs(current
->thread
.acrs
);
121 /* Copy a 'clean' PSW mask to the user to avoid leaking
122 information about whether PER is currently on. */
123 regs
->psw
.mask
= PSW_MASK_MERGE(PSW_USER_BITS
, regs
->psw
.mask
);
124 err
= __copy_to_user(&sregs
->regs
.psw
, ®s
->psw
,
125 sizeof(sregs
->regs
.psw
)+sizeof(sregs
->regs
.gprs
));
126 regs
->psw
.mask
= old_mask
;
129 err
= __copy_to_user(&sregs
->regs
.acrs
, current
->thread
.acrs
,
130 sizeof(sregs
->regs
.acrs
));
134 * We have to store the fp registers to current->thread.fp_regs
135 * to merge them with the emulated registers.
137 save_fp_regs(¤t
->thread
.fp_regs
);
138 return __copy_to_user(&sregs
->fpregs
, ¤t
->thread
.fp_regs
,
139 sizeof(s390_fp_regs
));
142 /* Returns positive number on error */
143 static int restore_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
145 unsigned long old_mask
= regs
->psw
.mask
;
148 /* Alwys make any pending restarted system call return -EINTR */
149 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
151 err
= __copy_from_user(®s
->psw
, &sregs
->regs
.psw
,
152 sizeof(sregs
->regs
.psw
)+sizeof(sregs
->regs
.gprs
));
153 regs
->psw
.mask
= PSW_MASK_MERGE(old_mask
, regs
->psw
.mask
);
154 regs
->psw
.addr
|= PSW_ADDR_AMODE
;
157 err
= __copy_from_user(¤t
->thread
.acrs
, &sregs
->regs
.acrs
,
158 sizeof(sregs
->regs
.acrs
));
161 restore_access_regs(current
->thread
.acrs
);
163 err
= __copy_from_user(¤t
->thread
.fp_regs
, &sregs
->fpregs
,
164 sizeof(s390_fp_regs
));
165 current
->thread
.fp_regs
.fpc
&= FPC_VALID_MASK
;
169 restore_fp_regs(¤t
->thread
.fp_regs
);
170 regs
->trap
= -1; /* disable syscall checks */
174 asmlinkage
long sys_sigreturn(struct pt_regs
*regs
)
176 sigframe __user
*frame
= (sigframe __user
*)regs
->gprs
[15];
179 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
181 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE
))
184 sigdelsetmask(&set
, ~_BLOCKABLE
);
185 spin_lock_irq(¤t
->sighand
->siglock
);
186 current
->blocked
= set
;
188 spin_unlock_irq(¤t
->sighand
->siglock
);
190 if (restore_sigregs(regs
, &frame
->sregs
))
193 return regs
->gprs
[2];
196 force_sig(SIGSEGV
, current
);
200 asmlinkage
long sys_rt_sigreturn(struct pt_regs
*regs
)
202 rt_sigframe __user
*frame
= (rt_sigframe __user
*)regs
->gprs
[15];
205 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
207 if (__copy_from_user(&set
.sig
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
210 sigdelsetmask(&set
, ~_BLOCKABLE
);
211 spin_lock_irq(¤t
->sighand
->siglock
);
212 current
->blocked
= set
;
214 spin_unlock_irq(¤t
->sighand
->siglock
);
216 if (restore_sigregs(regs
, &frame
->uc
.uc_mcontext
))
219 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
,
220 regs
->gprs
[15]) == -EFAULT
)
222 return regs
->gprs
[2];
225 force_sig(SIGSEGV
, current
);
230 * Set up a signal frame.
235 * Determine which stack to use..
237 static inline void __user
*
238 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
242 /* Default to using normal stack */
245 /* This is the X/Open sanctioned signal stack switching. */
246 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
247 if (! sas_ss_flags(sp
))
248 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
251 /* This is the legacy signal stack switching. */
252 else if (!user_mode(regs
) &&
253 !(ka
->sa
.sa_flags
& SA_RESTORER
) &&
254 ka
->sa
.sa_restorer
) {
255 sp
= (unsigned long) ka
->sa
.sa_restorer
;
258 return (void __user
*)((sp
- frame_size
) & -8ul);
261 static inline int map_signal(int sig
)
263 if (current_thread_info()->exec_domain
264 && current_thread_info()->exec_domain
->signal_invmap
266 return current_thread_info()->exec_domain
->signal_invmap
[sig
];
271 static int setup_frame(int sig
, struct k_sigaction
*ka
,
272 sigset_t
*set
, struct pt_regs
* regs
)
274 sigframe __user
*frame
;
276 frame
= get_sigframe(ka
, regs
, sizeof(sigframe
));
277 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(sigframe
)))
280 if (__copy_to_user(&frame
->sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE
))
283 if (save_sigregs(regs
, &frame
->sregs
))
285 if (__put_user(&frame
->sregs
, &frame
->sc
.sregs
))
288 /* Set up to return from userspace. If provided, use a stub
289 already in userspace. */
290 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
291 regs
->gprs
[14] = (unsigned long)
292 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
294 regs
->gprs
[14] = (unsigned long)
295 frame
->retcode
| PSW_ADDR_AMODE
;
296 if (__put_user(S390_SYSCALL_OPCODE
| __NR_sigreturn
,
297 (u16 __user
*)(frame
->retcode
)))
301 /* Set up backchain. */
302 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
305 /* Set up registers for signal handler */
306 regs
->gprs
[15] = (unsigned long) frame
;
307 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
309 regs
->gprs
[2] = map_signal(sig
);
310 regs
->gprs
[3] = (unsigned long) &frame
->sc
;
312 /* We forgot to include these in the sigcontext.
313 To avoid breaking binary compatibility, they are passed as args. */
314 regs
->gprs
[4] = current
->thread
.trap_no
;
315 regs
->gprs
[5] = current
->thread
.prot_addr
;
317 /* Place signal number on stack to allow backtrace from handler. */
318 if (__put_user(regs
->gprs
[2], (int __user
*) &frame
->signo
))
323 force_sigsegv(sig
, current
);
327 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
328 sigset_t
*set
, struct pt_regs
* regs
)
331 rt_sigframe __user
*frame
;
333 frame
= get_sigframe(ka
, regs
, sizeof(rt_sigframe
));
334 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(rt_sigframe
)))
337 if (copy_siginfo_to_user(&frame
->info
, info
))
340 /* Create the ucontext. */
341 err
|= __put_user(0, &frame
->uc
.uc_flags
);
342 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
343 err
|= __put_user((void __user
*)current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
344 err
|= __put_user(sas_ss_flags(regs
->gprs
[15]),
345 &frame
->uc
.uc_stack
.ss_flags
);
346 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
347 err
|= save_sigregs(regs
, &frame
->uc
.uc_mcontext
);
348 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
352 /* Set up to return from userspace. If provided, use a stub
353 already in userspace. */
354 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
355 regs
->gprs
[14] = (unsigned long)
356 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
358 regs
->gprs
[14] = (unsigned long)
359 frame
->retcode
| PSW_ADDR_AMODE
;
360 if (__put_user(S390_SYSCALL_OPCODE
| __NR_rt_sigreturn
,
361 (u16 __user
*)(frame
->retcode
)))
365 /* Set up backchain. */
366 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
369 /* Set up registers for signal handler */
370 regs
->gprs
[15] = (unsigned long) frame
;
371 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
373 regs
->gprs
[2] = map_signal(sig
);
374 regs
->gprs
[3] = (unsigned long) &frame
->info
;
375 regs
->gprs
[4] = (unsigned long) &frame
->uc
;
379 force_sigsegv(sig
, current
);
384 * OK, we're invoking a handler
388 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
389 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
* regs
)
393 /* Set up the stack frame */
394 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
395 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
397 ret
= setup_frame(sig
, ka
, oldset
, regs
);
400 spin_lock_irq(¤t
->sighand
->siglock
);
401 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
402 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
403 sigaddset(¤t
->blocked
,sig
);
405 spin_unlock_irq(¤t
->sighand
->siglock
);
412 * Note that 'init' is a special process: it doesn't get signals it doesn't
413 * want to handle. Thus you cannot kill init even with a SIGKILL even by
416 * Note that we go through the signals twice: once to check the signals that
417 * the kernel can handle, and then we build all the user-level signal handling
418 * stack-frames in one go after that.
420 void do_signal(struct pt_regs
*regs
)
422 unsigned long retval
= 0, continue_addr
= 0, restart_addr
= 0;
425 struct k_sigaction ka
;
429 * We want the common case to go fast, which
430 * is why we may in certain cases get here from
431 * kernel mode. Just return without doing anything
434 if (!user_mode(regs
))
437 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
438 oldset
= ¤t
->saved_sigmask
;
440 oldset
= ¤t
->blocked
;
442 /* Are we from a system call? */
443 if (regs
->trap
== __LC_SVC_OLD_PSW
) {
444 continue_addr
= regs
->psw
.addr
;
445 restart_addr
= continue_addr
- regs
->ilc
;
446 retval
= regs
->gprs
[2];
448 /* Prepare for system call restart. We do this here so that a
449 debugger will see the already changed PSW. */
451 case -ERESTARTNOHAND
:
453 case -ERESTARTNOINTR
:
454 regs
->gprs
[2] = regs
->orig_gpr2
;
455 regs
->psw
.addr
= restart_addr
;
457 case -ERESTART_RESTARTBLOCK
:
458 regs
->gprs
[2] = -EINTR
;
462 /* Get signal to deliver. When running under ptrace, at this point
463 the debugger may change all our registers ... */
464 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
466 /* Depending on the signal settings we may need to revert the
467 decision to restart the system call. */
468 if (signr
> 0 && regs
->psw
.addr
== restart_addr
) {
469 if (retval
== -ERESTARTNOHAND
470 || (retval
== -ERESTARTSYS
471 && !(current
->sighand
->action
[signr
-1].sa
.sa_flags
473 regs
->gprs
[2] = -EINTR
;
474 regs
->psw
.addr
= continue_addr
;
479 /* Whee! Actually deliver the signal. */
481 if (test_thread_flag(TIF_31BIT
)) {
482 extern int handle_signal32(unsigned long sig
,
483 struct k_sigaction
*ka
,
486 struct pt_regs
*regs
);
488 signr
, &ka
, &info
, oldset
, regs
) == 0) {
489 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
490 clear_thread_flag(TIF_RESTORE_SIGMASK
);
495 if (handle_signal(signr
, &ka
, &info
, oldset
, regs
) == 0) {
497 * A signal was successfully delivered; the saved
498 * sigmask will have been stored in the signal frame,
499 * and will be restored by sigreturn, so we can simply
500 * clear the TIF_RESTORE_SIGMASK flag.
502 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
503 clear_thread_flag(TIF_RESTORE_SIGMASK
);
509 * If there's no signal to deliver, we just put the saved sigmask back.
511 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
512 clear_thread_flag(TIF_RESTORE_SIGMASK
);
513 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
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
);