2 * Copyright IBM Corp. 1999, 2006
3 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
5 * Based on Intel version
7 * Copyright (C) 1991, 1992 Linus Torvalds
9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/personality.h>
24 #include <linux/binfmts.h>
25 #include <linux/tracehook.h>
26 #include <linux/syscalls.h>
27 #include <linux/compat.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/lowcore.h>
31 #include <asm/switch_to.h>
36 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
40 __u8 retcode
[S390_SYSCALL_SIZE
];
45 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
46 __u8 retcode
[S390_SYSCALL_SIZE
];
52 * Atomically swap in the new signal mask, and wait for a signal.
54 SYSCALL_DEFINE3(sigsuspend
, int, history0
, int, history1
, old_sigset_t
, mask
)
57 siginitset(&blocked
, mask
);
58 return sigsuspend(&blocked
);
61 SYSCALL_DEFINE3(sigaction
, int, sig
, const struct old_sigaction __user
*, act
,
62 struct old_sigaction __user
*, oact
)
64 struct k_sigaction new_ka
, old_ka
;
69 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
70 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
71 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
) ||
72 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
) ||
73 __get_user(mask
, &act
->sa_mask
))
75 siginitset(&new_ka
.sa
.sa_mask
, mask
);
78 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
81 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
82 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
83 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
) ||
84 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
) ||
85 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
))
92 SYSCALL_DEFINE2(sigaltstack
, const stack_t __user
*, uss
,
93 stack_t __user
*, uoss
)
95 struct pt_regs
*regs
= task_pt_regs(current
);
96 return do_sigaltstack(uss
, uoss
, regs
->gprs
[15]);
99 /* Returns non-zero on fault. */
100 static int save_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
104 save_access_regs(current
->thread
.acrs
);
106 /* Copy a 'clean' PSW mask to the user to avoid leaking
107 information about whether PER is currently on. */
108 user_sregs
.regs
.psw
.mask
= psw_user_bits
|
109 (regs
->psw
.mask
& PSW_MASK_USER
);
110 user_sregs
.regs
.psw
.addr
= regs
->psw
.addr
;
111 memcpy(&user_sregs
.regs
.gprs
, ®s
->gprs
, sizeof(sregs
->regs
.gprs
));
112 memcpy(&user_sregs
.regs
.acrs
, current
->thread
.acrs
,
113 sizeof(sregs
->regs
.acrs
));
115 * We have to store the fp registers to current->thread.fp_regs
116 * to merge them with the emulated registers.
118 save_fp_regs(¤t
->thread
.fp_regs
);
119 memcpy(&user_sregs
.fpregs
, ¤t
->thread
.fp_regs
,
120 sizeof(s390_fp_regs
));
121 return __copy_to_user(sregs
, &user_sregs
, sizeof(_sigregs
));
124 /* Returns positive number on error */
125 static int restore_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
130 /* Alwys make any pending restarted system call return -EINTR */
131 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
133 err
= __copy_from_user(&user_sregs
, sregs
, sizeof(_sigregs
));
136 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
137 regs
->psw
.mask
= (regs
->psw
.mask
& ~PSW_MASK_USER
) |
138 (user_sregs
.regs
.psw
.mask
& PSW_MASK_USER
);
139 /* Check for invalid amode */
140 if (regs
->psw
.mask
& PSW_MASK_EA
)
141 regs
->psw
.mask
|= PSW_MASK_BA
;
142 regs
->psw
.addr
= user_sregs
.regs
.psw
.addr
;
143 memcpy(®s
->gprs
, &user_sregs
.regs
.gprs
, sizeof(sregs
->regs
.gprs
));
144 memcpy(¤t
->thread
.acrs
, &user_sregs
.regs
.acrs
,
145 sizeof(sregs
->regs
.acrs
));
146 restore_access_regs(current
->thread
.acrs
);
148 memcpy(¤t
->thread
.fp_regs
, &user_sregs
.fpregs
,
149 sizeof(s390_fp_regs
));
150 current
->thread
.fp_regs
.fpc
&= FPC_VALID_MASK
;
152 restore_fp_regs(¤t
->thread
.fp_regs
);
153 clear_thread_flag(TIF_SYSCALL
); /* No longer in a system call */
157 SYSCALL_DEFINE0(sigreturn
)
159 struct pt_regs
*regs
= task_pt_regs(current
);
160 sigframe __user
*frame
= (sigframe __user
*)regs
->gprs
[15];
163 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
165 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE
))
167 set_current_blocked(&set
);
168 if (restore_sigregs(regs
, &frame
->sregs
))
170 return regs
->gprs
[2];
172 force_sig(SIGSEGV
, current
);
176 SYSCALL_DEFINE0(rt_sigreturn
)
178 struct pt_regs
*regs
= task_pt_regs(current
);
179 rt_sigframe __user
*frame
= (rt_sigframe __user
*)regs
->gprs
[15];
182 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
184 if (__copy_from_user(&set
.sig
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
186 set_current_blocked(&set
);
187 if (restore_sigregs(regs
, &frame
->uc
.uc_mcontext
))
189 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
,
190 regs
->gprs
[15]) == -EFAULT
)
192 return regs
->gprs
[2];
194 force_sig(SIGSEGV
, current
);
199 * Set up a signal frame.
204 * Determine which stack to use..
206 static inline void __user
*
207 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
211 /* Default to using normal stack */
214 /* Overflow on alternate signal stack gives SIGSEGV. */
215 if (on_sig_stack(sp
) && !on_sig_stack((sp
- frame_size
) & -8UL))
216 return (void __user
*) -1UL;
218 /* This is the X/Open sanctioned signal stack switching. */
219 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
220 if (! sas_ss_flags(sp
))
221 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
224 return (void __user
*)((sp
- frame_size
) & -8ul);
227 static inline int map_signal(int sig
)
229 if (current_thread_info()->exec_domain
230 && current_thread_info()->exec_domain
->signal_invmap
232 return current_thread_info()->exec_domain
->signal_invmap
[sig
];
237 static int setup_frame(int sig
, struct k_sigaction
*ka
,
238 sigset_t
*set
, struct pt_regs
* regs
)
240 sigframe __user
*frame
;
242 frame
= get_sigframe(ka
, regs
, sizeof(sigframe
));
243 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(sigframe
)))
246 if (frame
== (void __user
*) -1UL)
249 if (__copy_to_user(&frame
->sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE
))
252 if (save_sigregs(regs
, &frame
->sregs
))
254 if (__put_user(&frame
->sregs
, &frame
->sc
.sregs
))
257 /* Set up to return from userspace. If provided, use a stub
258 already in userspace. */
259 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
260 regs
->gprs
[14] = (unsigned long)
261 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
263 regs
->gprs
[14] = (unsigned long)
264 frame
->retcode
| PSW_ADDR_AMODE
;
265 if (__put_user(S390_SYSCALL_OPCODE
| __NR_sigreturn
,
266 (u16 __user
*)(frame
->retcode
)))
270 /* Set up backchain. */
271 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
274 /* Set up registers for signal handler */
275 regs
->gprs
[15] = (unsigned long) frame
;
276 regs
->psw
.mask
|= PSW_MASK_EA
| PSW_MASK_BA
; /* 64 bit amode */
277 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
279 regs
->gprs
[2] = map_signal(sig
);
280 regs
->gprs
[3] = (unsigned long) &frame
->sc
;
282 /* We forgot to include these in the sigcontext.
283 To avoid breaking binary compatibility, they are passed as args. */
284 if (sig
== SIGSEGV
|| sig
== SIGBUS
|| sig
== SIGILL
||
285 sig
== SIGTRAP
|| sig
== SIGFPE
) {
286 /* set extra registers only for synchronous signals */
287 regs
->gprs
[4] = regs
->int_code
& 127;
288 regs
->gprs
[5] = regs
->int_parm_long
;
289 regs
->gprs
[6] = task_thread_info(current
)->last_break
;
292 /* Place signal number on stack to allow backtrace from handler. */
293 if (__put_user(regs
->gprs
[2], (int __user
*) &frame
->signo
))
298 force_sigsegv(sig
, current
);
302 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
303 sigset_t
*set
, struct pt_regs
* regs
)
306 rt_sigframe __user
*frame
;
308 frame
= get_sigframe(ka
, regs
, sizeof(rt_sigframe
));
309 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(rt_sigframe
)))
312 if (frame
== (void __user
*) -1UL)
315 if (copy_siginfo_to_user(&frame
->info
, info
))
318 /* Create the ucontext. */
319 err
|= __put_user(0, &frame
->uc
.uc_flags
);
320 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
321 err
|= __put_user((void __user
*)current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
322 err
|= __put_user(sas_ss_flags(regs
->gprs
[15]),
323 &frame
->uc
.uc_stack
.ss_flags
);
324 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
325 err
|= save_sigregs(regs
, &frame
->uc
.uc_mcontext
);
326 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
330 /* Set up to return from userspace. If provided, use a stub
331 already in userspace. */
332 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
333 regs
->gprs
[14] = (unsigned long)
334 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
336 regs
->gprs
[14] = (unsigned long)
337 frame
->retcode
| PSW_ADDR_AMODE
;
338 if (__put_user(S390_SYSCALL_OPCODE
| __NR_rt_sigreturn
,
339 (u16 __user
*)(frame
->retcode
)))
343 /* Set up backchain. */
344 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
347 /* Set up registers for signal handler */
348 regs
->gprs
[15] = (unsigned long) frame
;
349 regs
->psw
.mask
|= PSW_MASK_EA
| PSW_MASK_BA
; /* 64 bit amode */
350 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
352 regs
->gprs
[2] = map_signal(sig
);
353 regs
->gprs
[3] = (unsigned long) &frame
->info
;
354 regs
->gprs
[4] = (unsigned long) &frame
->uc
;
355 regs
->gprs
[5] = task_thread_info(current
)->last_break
;
359 force_sigsegv(sig
, current
);
363 static void handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
364 siginfo_t
*info
, sigset_t
*oldset
,
365 struct pt_regs
*regs
)
369 /* Set up the stack frame */
370 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
371 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
373 ret
= setup_frame(sig
, ka
, oldset
, regs
);
376 signal_delivered(sig
, info
, ka
, regs
,
377 test_thread_flag(TIF_SINGLE_STEP
));
381 * Note that 'init' is a special process: it doesn't get signals it doesn't
382 * want to handle. Thus you cannot kill init even with a SIGKILL even by
385 * Note that we go through the signals twice: once to check the signals that
386 * the kernel can handle, and then we build all the user-level signal handling
387 * stack-frames in one go after that.
389 void do_signal(struct pt_regs
*regs
)
393 struct k_sigaction ka
;
394 sigset_t
*oldset
= sigmask_to_save();
397 * Get signal to deliver. When running under ptrace, at this point
398 * the debugger may change all our registers, including the system
401 current_thread_info()->system_call
=
402 test_thread_flag(TIF_SYSCALL
) ? regs
->int_code
: 0;
403 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
406 /* Whee! Actually deliver the signal. */
407 if (current_thread_info()->system_call
) {
408 regs
->int_code
= current_thread_info()->system_call
;
409 /* Check for system call restarting. */
410 switch (regs
->gprs
[2]) {
411 case -ERESTART_RESTARTBLOCK
:
412 case -ERESTARTNOHAND
:
413 regs
->gprs
[2] = -EINTR
;
416 if (!(ka
.sa
.sa_flags
& SA_RESTART
)) {
417 regs
->gprs
[2] = -EINTR
;
421 case -ERESTARTNOINTR
:
422 regs
->gprs
[2] = regs
->orig_gpr2
;
424 __rewind_psw(regs
->psw
,
425 regs
->int_code
>> 16);
429 /* No longer in a system call */
430 clear_thread_flag(TIF_SYSCALL
);
432 if (is_compat_task())
433 handle_signal32(signr
, &ka
, &info
, oldset
, regs
);
435 handle_signal(signr
, &ka
, &info
, oldset
, regs
);
439 /* No handlers present - check for system call restart */
440 clear_thread_flag(TIF_SYSCALL
);
441 if (current_thread_info()->system_call
) {
442 regs
->int_code
= current_thread_info()->system_call
;
443 switch (regs
->gprs
[2]) {
444 case -ERESTART_RESTARTBLOCK
:
445 /* Restart with sys_restart_syscall */
446 regs
->int_code
= __NR_restart_syscall
;
448 case -ERESTARTNOHAND
:
450 case -ERESTARTNOINTR
:
451 /* Restart system call with magic TIF bit. */
452 regs
->gprs
[2] = regs
->orig_gpr2
;
453 set_thread_flag(TIF_SYSCALL
);
459 * If there's no signal to deliver, we just put the saved sigmask back.
461 restore_saved_sigmask();
464 void do_notify_resume(struct pt_regs
*regs
)
466 clear_thread_flag(TIF_NOTIFY_RESUME
);
467 tracehook_notify_resume(regs
);