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
];
51 /* Returns non-zero on fault. */
52 static int save_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
56 save_access_regs(current
->thread
.acrs
);
58 /* Copy a 'clean' PSW mask to the user to avoid leaking
59 information about whether PER is currently on. */
60 user_sregs
.regs
.psw
.mask
= psw_user_bits
|
61 (regs
->psw
.mask
& PSW_MASK_USER
);
62 user_sregs
.regs
.psw
.addr
= regs
->psw
.addr
;
63 memcpy(&user_sregs
.regs
.gprs
, ®s
->gprs
, sizeof(sregs
->regs
.gprs
));
64 memcpy(&user_sregs
.regs
.acrs
, current
->thread
.acrs
,
65 sizeof(sregs
->regs
.acrs
));
67 * We have to store the fp registers to current->thread.fp_regs
68 * to merge them with the emulated registers.
70 save_fp_regs(¤t
->thread
.fp_regs
);
71 memcpy(&user_sregs
.fpregs
, ¤t
->thread
.fp_regs
,
72 sizeof(s390_fp_regs
));
73 return __copy_to_user(sregs
, &user_sregs
, sizeof(_sigregs
));
76 /* Returns positive number on error */
77 static int restore_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
82 /* Alwys make any pending restarted system call return -EINTR */
83 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
85 err
= __copy_from_user(&user_sregs
, sregs
, sizeof(_sigregs
));
88 /* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
89 regs
->psw
.mask
= (regs
->psw
.mask
& ~PSW_MASK_USER
) |
90 (user_sregs
.regs
.psw
.mask
& PSW_MASK_USER
);
91 /* Check for invalid user address space control. */
92 if ((regs
->psw
.mask
& PSW_MASK_ASC
) >= (psw_kernel_bits
& PSW_MASK_ASC
))
93 regs
->psw
.mask
= (psw_user_bits
& PSW_MASK_ASC
) |
94 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
95 /* Check for invalid amode */
96 if (regs
->psw
.mask
& PSW_MASK_EA
)
97 regs
->psw
.mask
|= PSW_MASK_BA
;
98 regs
->psw
.addr
= user_sregs
.regs
.psw
.addr
;
99 memcpy(®s
->gprs
, &user_sregs
.regs
.gprs
, sizeof(sregs
->regs
.gprs
));
100 memcpy(¤t
->thread
.acrs
, &user_sregs
.regs
.acrs
,
101 sizeof(sregs
->regs
.acrs
));
102 restore_access_regs(current
->thread
.acrs
);
104 memcpy(¤t
->thread
.fp_regs
, &user_sregs
.fpregs
,
105 sizeof(s390_fp_regs
));
106 current
->thread
.fp_regs
.fpc
&= FPC_VALID_MASK
;
108 restore_fp_regs(¤t
->thread
.fp_regs
);
109 clear_thread_flag(TIF_SYSCALL
); /* No longer in a system call */
113 SYSCALL_DEFINE0(sigreturn
)
115 struct pt_regs
*regs
= task_pt_regs(current
);
116 sigframe __user
*frame
= (sigframe __user
*)regs
->gprs
[15];
119 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE
))
121 set_current_blocked(&set
);
122 if (restore_sigregs(regs
, &frame
->sregs
))
124 return regs
->gprs
[2];
126 force_sig(SIGSEGV
, current
);
130 SYSCALL_DEFINE0(rt_sigreturn
)
132 struct pt_regs
*regs
= task_pt_regs(current
);
133 rt_sigframe __user
*frame
= (rt_sigframe __user
*)regs
->gprs
[15];
136 if (__copy_from_user(&set
.sig
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
138 set_current_blocked(&set
);
139 if (restore_sigregs(regs
, &frame
->uc
.uc_mcontext
))
141 if (restore_altstack(&frame
->uc
.uc_stack
))
143 return regs
->gprs
[2];
145 force_sig(SIGSEGV
, current
);
150 * Set up a signal frame.
155 * Determine which stack to use..
157 static inline void __user
*
158 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
162 /* Default to using normal stack */
165 /* Overflow on alternate signal stack gives SIGSEGV. */
166 if (on_sig_stack(sp
) && !on_sig_stack((sp
- frame_size
) & -8UL))
167 return (void __user
*) -1UL;
169 /* This is the X/Open sanctioned signal stack switching. */
170 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
171 if (! sas_ss_flags(sp
))
172 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
175 return (void __user
*)((sp
- frame_size
) & -8ul);
178 static inline int map_signal(int sig
)
180 if (current_thread_info()->exec_domain
181 && current_thread_info()->exec_domain
->signal_invmap
183 return current_thread_info()->exec_domain
->signal_invmap
[sig
];
188 static int setup_frame(int sig
, struct k_sigaction
*ka
,
189 sigset_t
*set
, struct pt_regs
* regs
)
191 sigframe __user
*frame
;
193 frame
= get_sigframe(ka
, regs
, sizeof(sigframe
));
195 if (frame
== (void __user
*) -1UL)
198 if (__copy_to_user(&frame
->sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE
))
201 if (save_sigregs(regs
, &frame
->sregs
))
203 if (__put_user(&frame
->sregs
, &frame
->sc
.sregs
))
206 /* Set up to return from userspace. If provided, use a stub
207 already in userspace. */
208 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
209 regs
->gprs
[14] = (unsigned long)
210 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
212 regs
->gprs
[14] = (unsigned long)
213 frame
->retcode
| PSW_ADDR_AMODE
;
214 if (__put_user(S390_SYSCALL_OPCODE
| __NR_sigreturn
,
215 (u16 __user
*)(frame
->retcode
)))
219 /* Set up backchain. */
220 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
223 /* Set up registers for signal handler */
224 regs
->gprs
[15] = (unsigned long) frame
;
225 /* Force default amode and default user address space control. */
226 regs
->psw
.mask
= PSW_MASK_EA
| PSW_MASK_BA
|
227 (psw_user_bits
& PSW_MASK_ASC
) |
228 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
229 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
231 regs
->gprs
[2] = map_signal(sig
);
232 regs
->gprs
[3] = (unsigned long) &frame
->sc
;
234 /* We forgot to include these in the sigcontext.
235 To avoid breaking binary compatibility, they are passed as args. */
236 if (sig
== SIGSEGV
|| sig
== SIGBUS
|| sig
== SIGILL
||
237 sig
== SIGTRAP
|| sig
== SIGFPE
) {
238 /* set extra registers only for synchronous signals */
239 regs
->gprs
[4] = regs
->int_code
& 127;
240 regs
->gprs
[5] = regs
->int_parm_long
;
241 regs
->gprs
[6] = task_thread_info(current
)->last_break
;
244 /* Place signal number on stack to allow backtrace from handler. */
245 if (__put_user(regs
->gprs
[2], (int __user
*) &frame
->signo
))
250 force_sigsegv(sig
, current
);
254 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
255 sigset_t
*set
, struct pt_regs
* regs
)
258 rt_sigframe __user
*frame
;
260 frame
= get_sigframe(ka
, regs
, sizeof(rt_sigframe
));
262 if (frame
== (void __user
*) -1UL)
265 if (copy_siginfo_to_user(&frame
->info
, info
))
268 /* Create the ucontext. */
269 err
|= __put_user(0, &frame
->uc
.uc_flags
);
270 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
271 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->gprs
[15]);
272 err
|= save_sigregs(regs
, &frame
->uc
.uc_mcontext
);
273 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
277 /* Set up to return from userspace. If provided, use a stub
278 already in userspace. */
279 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
280 regs
->gprs
[14] = (unsigned long)
281 ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
283 regs
->gprs
[14] = (unsigned long)
284 frame
->retcode
| PSW_ADDR_AMODE
;
285 if (__put_user(S390_SYSCALL_OPCODE
| __NR_rt_sigreturn
,
286 (u16 __user
*)(frame
->retcode
)))
290 /* Set up backchain. */
291 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
294 /* Set up registers for signal handler */
295 regs
->gprs
[15] = (unsigned long) frame
;
296 /* Force default amode and default user address space control. */
297 regs
->psw
.mask
= PSW_MASK_EA
| PSW_MASK_BA
|
298 (psw_user_bits
& PSW_MASK_ASC
) |
299 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
300 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
302 regs
->gprs
[2] = map_signal(sig
);
303 regs
->gprs
[3] = (unsigned long) &frame
->info
;
304 regs
->gprs
[4] = (unsigned long) &frame
->uc
;
305 regs
->gprs
[5] = task_thread_info(current
)->last_break
;
309 force_sigsegv(sig
, current
);
313 static void handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
314 siginfo_t
*info
, sigset_t
*oldset
,
315 struct pt_regs
*regs
)
319 /* Set up the stack frame */
320 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
321 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
323 ret
= setup_frame(sig
, ka
, oldset
, regs
);
326 signal_delivered(sig
, info
, ka
, regs
,
327 test_thread_flag(TIF_SINGLE_STEP
));
331 * Note that 'init' is a special process: it doesn't get signals it doesn't
332 * want to handle. Thus you cannot kill init even with a SIGKILL even by
335 * Note that we go through the signals twice: once to check the signals that
336 * the kernel can handle, and then we build all the user-level signal handling
337 * stack-frames in one go after that.
339 void do_signal(struct pt_regs
*regs
)
343 struct k_sigaction ka
;
344 sigset_t
*oldset
= sigmask_to_save();
347 * Get signal to deliver. When running under ptrace, at this point
348 * the debugger may change all our registers, including the system
351 current_thread_info()->system_call
=
352 test_thread_flag(TIF_SYSCALL
) ? regs
->int_code
: 0;
353 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
356 /* Whee! Actually deliver the signal. */
357 if (current_thread_info()->system_call
) {
358 regs
->int_code
= current_thread_info()->system_call
;
359 /* Check for system call restarting. */
360 switch (regs
->gprs
[2]) {
361 case -ERESTART_RESTARTBLOCK
:
362 case -ERESTARTNOHAND
:
363 regs
->gprs
[2] = -EINTR
;
366 if (!(ka
.sa
.sa_flags
& SA_RESTART
)) {
367 regs
->gprs
[2] = -EINTR
;
371 case -ERESTARTNOINTR
:
372 regs
->gprs
[2] = regs
->orig_gpr2
;
374 __rewind_psw(regs
->psw
,
375 regs
->int_code
>> 16);
379 /* No longer in a system call */
380 clear_thread_flag(TIF_SYSCALL
);
382 if (is_compat_task())
383 handle_signal32(signr
, &ka
, &info
, oldset
, regs
);
385 handle_signal(signr
, &ka
, &info
, oldset
, regs
);
389 /* No handlers present - check for system call restart */
390 clear_thread_flag(TIF_SYSCALL
);
391 if (current_thread_info()->system_call
) {
392 regs
->int_code
= current_thread_info()->system_call
;
393 switch (regs
->gprs
[2]) {
394 case -ERESTART_RESTARTBLOCK
:
395 /* Restart with sys_restart_syscall */
396 regs
->int_code
= __NR_restart_syscall
;
398 case -ERESTARTNOHAND
:
400 case -ERESTARTNOINTR
:
401 /* Restart system call with magic TIF bit. */
402 regs
->gprs
[2] = regs
->orig_gpr2
;
403 set_thread_flag(TIF_SYSCALL
);
404 if (test_thread_flag(TIF_SINGLE_STEP
))
405 set_thread_flag(TIF_PER_TRAP
);
411 * If there's no signal to deliver, we just put the saved sigmask back.
413 restore_saved_sigmask();
416 void do_notify_resume(struct pt_regs
*regs
)
418 clear_thread_flag(TIF_NOTIFY_RESUME
);
419 tracehook_notify_resume(regs
);