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 (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
121 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
123 set_current_blocked(&set);
124 if (restore_sigregs(regs, &frame->sregs))
126 return regs->gprs[2];
128 force_sig(SIGSEGV, current);
132 SYSCALL_DEFINE0(rt_sigreturn)
134 struct pt_regs *regs = task_pt_regs(current);
135 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
138 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
140 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
142 set_current_blocked(&set);
143 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
145 if (restore_altstack(&frame->uc.uc_stack))
147 return regs->gprs[2];
149 force_sig(SIGSEGV, current);
154 * Set up a signal frame.
159 * Determine which stack to use..
161 static inline void __user *
162 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
166 /* Default to using normal stack */
169 /* Overflow on alternate signal stack gives SIGSEGV. */
170 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
171 return (void __user *) -1UL;
173 /* This is the X/Open sanctioned signal stack switching. */
174 if (ka->sa.sa_flags & SA_ONSTACK) {
175 if (! sas_ss_flags(sp))
176 sp = current->sas_ss_sp + current->sas_ss_size;
179 return (void __user *)((sp - frame_size) & -8ul);
182 static inline int map_signal(int sig)
184 if (current_thread_info()->exec_domain
185 && current_thread_info()->exec_domain->signal_invmap
187 return current_thread_info()->exec_domain->signal_invmap[sig];
192 static int setup_frame(int sig, struct k_sigaction *ka,
193 sigset_t *set, struct pt_regs * regs)
195 sigframe __user *frame;
197 frame = get_sigframe(ka, regs, sizeof(sigframe));
198 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
201 if (frame == (void __user *) -1UL)
204 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
207 if (save_sigregs(regs, &frame->sregs))
209 if (__put_user(&frame->sregs, &frame->sc.sregs))
212 /* Set up to return from userspace. If provided, use a stub
213 already in userspace. */
214 if (ka->sa.sa_flags & SA_RESTORER) {
215 regs->gprs[14] = (unsigned long)
216 ka->sa.sa_restorer | PSW_ADDR_AMODE;
218 regs->gprs[14] = (unsigned long)
219 frame->retcode | PSW_ADDR_AMODE;
220 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
221 (u16 __user *)(frame->retcode)))
225 /* Set up backchain. */
226 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
229 /* Set up registers for signal handler */
230 regs->gprs[15] = (unsigned long) frame;
231 /* Force default amode and default user address space control. */
232 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
233 (psw_user_bits & PSW_MASK_ASC) |
234 (regs->psw.mask & ~PSW_MASK_ASC);
235 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
237 regs->gprs[2] = map_signal(sig);
238 regs->gprs[3] = (unsigned long) &frame->sc;
240 /* We forgot to include these in the sigcontext.
241 To avoid breaking binary compatibility, they are passed as args. */
242 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
243 sig == SIGTRAP || sig == SIGFPE) {
244 /* set extra registers only for synchronous signals */
245 regs->gprs[4] = regs->int_code & 127;
246 regs->gprs[5] = regs->int_parm_long;
247 regs->gprs[6] = task_thread_info(current)->last_break;
250 /* Place signal number on stack to allow backtrace from handler. */
251 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
256 force_sigsegv(sig, current);
260 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
261 sigset_t *set, struct pt_regs * regs)
264 rt_sigframe __user *frame;
266 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
267 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
270 if (frame == (void __user *) -1UL)
273 if (copy_siginfo_to_user(&frame->info, info))
276 /* Create the ucontext. */
277 err |= __put_user(0, &frame->uc.uc_flags);
278 err |= __put_user(NULL, &frame->uc.uc_link);
279 err |= __save_altstack(&frame->uc.uc_stack, regs->gprs[15]);
280 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
281 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
285 /* Set up to return from userspace. If provided, use a stub
286 already in userspace. */
287 if (ka->sa.sa_flags & SA_RESTORER) {
288 regs->gprs[14] = (unsigned long)
289 ka->sa.sa_restorer | PSW_ADDR_AMODE;
291 regs->gprs[14] = (unsigned long)
292 frame->retcode | PSW_ADDR_AMODE;
293 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
294 (u16 __user *)(frame->retcode)))
298 /* Set up backchain. */
299 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
302 /* Set up registers for signal handler */
303 regs->gprs[15] = (unsigned long) frame;
304 /* Force default amode and default user address space control. */
305 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
306 (psw_user_bits & PSW_MASK_ASC) |
307 (regs->psw.mask & ~PSW_MASK_ASC);
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->info;
312 regs->gprs[4] = (unsigned long) &frame->uc;
313 regs->gprs[5] = task_thread_info(current)->last_break;
317 force_sigsegv(sig, current);
321 static void handle_signal(unsigned long sig, struct k_sigaction *ka,
322 siginfo_t *info, sigset_t *oldset,
323 struct pt_regs *regs)
327 /* Set up the stack frame */
328 if (ka->sa.sa_flags & SA_SIGINFO)
329 ret = setup_rt_frame(sig, ka, info, oldset, regs);
331 ret = setup_frame(sig, ka, oldset, regs);
334 signal_delivered(sig, info, ka, regs,
335 test_thread_flag(TIF_SINGLE_STEP));
339 * Note that 'init' is a special process: it doesn't get signals it doesn't
340 * want to handle. Thus you cannot kill init even with a SIGKILL even by
343 * Note that we go through the signals twice: once to check the signals that
344 * the kernel can handle, and then we build all the user-level signal handling
345 * stack-frames in one go after that.
347 void do_signal(struct pt_regs *regs)
351 struct k_sigaction ka;
352 sigset_t *oldset = sigmask_to_save();
355 * Get signal to deliver. When running under ptrace, at this point
356 * the debugger may change all our registers, including the system
359 current_thread_info()->system_call =
360 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
361 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
364 /* Whee! Actually deliver the signal. */
365 if (current_thread_info()->system_call) {
366 regs->int_code = current_thread_info()->system_call;
367 /* Check for system call restarting. */
368 switch (regs->gprs[2]) {
369 case -ERESTART_RESTARTBLOCK:
370 case -ERESTARTNOHAND:
371 regs->gprs[2] = -EINTR;
374 if (!(ka.sa.sa_flags & SA_RESTART)) {
375 regs->gprs[2] = -EINTR;
379 case -ERESTARTNOINTR:
380 regs->gprs[2] = regs->orig_gpr2;
382 __rewind_psw(regs->psw,
383 regs->int_code >> 16);
387 /* No longer in a system call */
388 clear_thread_flag(TIF_SYSCALL);
390 if (is_compat_task())
391 handle_signal32(signr, &ka, &info, oldset, regs);
393 handle_signal(signr, &ka, &info, oldset, regs);
397 /* No handlers present - check for system call restart */
398 clear_thread_flag(TIF_SYSCALL);
399 if (current_thread_info()->system_call) {
400 regs->int_code = current_thread_info()->system_call;
401 switch (regs->gprs[2]) {
402 case -ERESTART_RESTARTBLOCK:
403 /* Restart with sys_restart_syscall */
404 regs->int_code = __NR_restart_syscall;
406 case -ERESTARTNOHAND:
408 case -ERESTARTNOINTR:
409 /* Restart system call with magic TIF bit. */
410 regs->gprs[2] = regs->orig_gpr2;
411 set_thread_flag(TIF_SYSCALL);
412 if (test_thread_flag(TIF_SINGLE_STEP))
413 set_thread_flag(TIF_PER_TRAP);
419 * If there's no signal to deliver, we just put the saved sigmask back.
421 restore_saved_sigmask();
424 void do_notify_resume(struct pt_regs *regs)
426 clear_thread_flag(TIF_NOTIFY_RESUME);
427 tracehook_notify_resume(regs);