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 if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs)))
78 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 if (__copy_from_user(&user_sregs, sregs, sizeof(_sigregs)))
87 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
88 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
89 (user_sregs.regs.psw.mask & PSW_MASK_USER);
90 /* Check for invalid user address space control. */
91 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
92 regs->psw.mask = PSW_ASC_PRIMARY |
93 (regs->psw.mask & ~PSW_MASK_ASC);
94 /* Check for invalid amode */
95 if (regs->psw.mask & PSW_MASK_EA)
96 regs->psw.mask |= PSW_MASK_BA;
97 regs->psw.addr = user_sregs.regs.psw.addr;
98 memcpy(®s->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
99 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs,
100 sizeof(sregs->regs.acrs));
101 restore_access_regs(current->thread.acrs);
103 memcpy(¤t->thread.fp_regs, &user_sregs.fpregs,
104 sizeof(s390_fp_regs));
105 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
107 restore_fp_regs(¤t->thread.fp_regs);
108 clear_thread_flag(TIF_SYSCALL); /* No longer in a system call */
112 SYSCALL_DEFINE0(sigreturn)
114 struct pt_regs *regs = task_pt_regs(current);
115 sigframe __user *frame = (sigframe __user *)regs->gprs[15];
118 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
120 set_current_blocked(&set);
121 if (restore_sigregs(regs, &frame->sregs))
123 return regs->gprs[2];
125 force_sig(SIGSEGV, current);
129 SYSCALL_DEFINE0(rt_sigreturn)
131 struct pt_regs *regs = task_pt_regs(current);
132 rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
135 if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
137 set_current_blocked(&set);
138 if (restore_sigregs(regs, &frame->uc.uc_mcontext))
140 if (restore_altstack(&frame->uc.uc_stack))
142 return regs->gprs[2];
144 force_sig(SIGSEGV, current);
149 * Set up a signal frame.
154 * Determine which stack to use..
156 static inline void __user *
157 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
161 /* Default to using normal stack */
164 /* Overflow on alternate signal stack gives SIGSEGV. */
165 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
166 return (void __user *) -1UL;
168 /* This is the X/Open sanctioned signal stack switching. */
169 if (ka->sa.sa_flags & SA_ONSTACK) {
170 if (! sas_ss_flags(sp))
171 sp = current->sas_ss_sp + current->sas_ss_size;
174 return (void __user *)((sp - frame_size) & -8ul);
177 static inline int map_signal(int sig)
179 if (current_thread_info()->exec_domain
180 && current_thread_info()->exec_domain->signal_invmap
182 return current_thread_info()->exec_domain->signal_invmap[sig];
187 static int setup_frame(int sig, struct k_sigaction *ka,
188 sigset_t *set, struct pt_regs * regs)
190 sigframe __user *frame;
192 frame = get_sigframe(ka, regs, sizeof(sigframe));
194 if (frame == (void __user *) -1UL)
197 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
200 if (save_sigregs(regs, &frame->sregs))
202 if (__put_user(&frame->sregs, &frame->sc.sregs))
205 /* Set up to return from userspace. If provided, use a stub
206 already in userspace. */
207 if (ka->sa.sa_flags & SA_RESTORER) {
208 regs->gprs[14] = (unsigned long)
209 ka->sa.sa_restorer | PSW_ADDR_AMODE;
211 regs->gprs[14] = (unsigned long)
212 frame->retcode | PSW_ADDR_AMODE;
213 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
214 (u16 __user *)(frame->retcode)))
218 /* Set up backchain. */
219 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
222 /* Set up registers for signal handler */
223 regs->gprs[15] = (unsigned long) frame;
224 /* Force default amode and default user address space control. */
225 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
226 (PSW_USER_BITS & PSW_MASK_ASC) |
227 (regs->psw.mask & ~PSW_MASK_ASC);
228 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
230 regs->gprs[2] = map_signal(sig);
231 regs->gprs[3] = (unsigned long) &frame->sc;
233 /* We forgot to include these in the sigcontext.
234 To avoid breaking binary compatibility, they are passed as args. */
235 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
236 sig == SIGTRAP || sig == SIGFPE) {
237 /* set extra registers only for synchronous signals */
238 regs->gprs[4] = regs->int_code & 127;
239 regs->gprs[5] = regs->int_parm_long;
240 regs->gprs[6] = task_thread_info(current)->last_break;
243 /* Place signal number on stack to allow backtrace from handler. */
244 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
249 force_sigsegv(sig, current);
253 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
254 sigset_t *set, struct pt_regs * regs)
257 rt_sigframe __user *frame;
259 frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
261 if (frame == (void __user *) -1UL)
264 if (copy_siginfo_to_user(&frame->info, info))
267 /* Create the ucontext. */
268 err |= __put_user(0, &frame->uc.uc_flags);
269 err |= __put_user(NULL, &frame->uc.uc_link);
270 err |= __save_altstack(&frame->uc.uc_stack, regs->gprs[15]);
271 err |= save_sigregs(regs, &frame->uc.uc_mcontext);
272 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
276 /* Set up to return from userspace. If provided, use a stub
277 already in userspace. */
278 if (ka->sa.sa_flags & SA_RESTORER) {
279 regs->gprs[14] = (unsigned long)
280 ka->sa.sa_restorer | PSW_ADDR_AMODE;
282 regs->gprs[14] = (unsigned long)
283 frame->retcode | PSW_ADDR_AMODE;
284 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
285 (u16 __user *)(frame->retcode)))
289 /* Set up backchain. */
290 if (__put_user(regs->gprs[15], (addr_t __user *) frame))
293 /* Set up registers for signal handler */
294 regs->gprs[15] = (unsigned long) frame;
295 /* Force default amode and default user address space control. */
296 regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
297 (PSW_USER_BITS & PSW_MASK_ASC) |
298 (regs->psw.mask & ~PSW_MASK_ASC);
299 regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
301 regs->gprs[2] = map_signal(sig);
302 regs->gprs[3] = (unsigned long) &frame->info;
303 regs->gprs[4] = (unsigned long) &frame->uc;
304 regs->gprs[5] = task_thread_info(current)->last_break;
308 force_sigsegv(sig, current);
312 static void handle_signal(unsigned long sig, struct k_sigaction *ka,
313 siginfo_t *info, sigset_t *oldset,
314 struct pt_regs *regs)
318 /* Set up the stack frame */
319 if (ka->sa.sa_flags & SA_SIGINFO)
320 ret = setup_rt_frame(sig, ka, info, oldset, regs);
322 ret = setup_frame(sig, ka, oldset, regs);
325 signal_delivered(sig, info, ka, regs,
326 test_thread_flag(TIF_SINGLE_STEP));
330 * Note that 'init' is a special process: it doesn't get signals it doesn't
331 * want to handle. Thus you cannot kill init even with a SIGKILL even by
334 * Note that we go through the signals twice: once to check the signals that
335 * the kernel can handle, and then we build all the user-level signal handling
336 * stack-frames in one go after that.
338 void do_signal(struct pt_regs *regs)
342 struct k_sigaction ka;
343 sigset_t *oldset = sigmask_to_save();
346 * Get signal to deliver. When running under ptrace, at this point
347 * the debugger may change all our registers, including the system
350 current_thread_info()->system_call =
351 test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
352 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
355 /* Whee! Actually deliver the signal. */
356 if (current_thread_info()->system_call) {
357 regs->int_code = current_thread_info()->system_call;
358 /* Check for system call restarting. */
359 switch (regs->gprs[2]) {
360 case -ERESTART_RESTARTBLOCK:
361 case -ERESTARTNOHAND:
362 regs->gprs[2] = -EINTR;
365 if (!(ka.sa.sa_flags & SA_RESTART)) {
366 regs->gprs[2] = -EINTR;
370 case -ERESTARTNOINTR:
371 regs->gprs[2] = regs->orig_gpr2;
373 __rewind_psw(regs->psw,
374 regs->int_code >> 16);
378 /* No longer in a system call */
379 clear_thread_flag(TIF_SYSCALL);
381 if (is_compat_task())
382 handle_signal32(signr, &ka, &info, oldset, regs);
384 handle_signal(signr, &ka, &info, oldset, regs);
388 /* No handlers present - check for system call restart */
389 clear_thread_flag(TIF_SYSCALL);
390 if (current_thread_info()->system_call) {
391 regs->int_code = current_thread_info()->system_call;
392 switch (regs->gprs[2]) {
393 case -ERESTART_RESTARTBLOCK:
394 /* Restart with sys_restart_syscall */
395 regs->int_code = __NR_restart_syscall;
397 case -ERESTARTNOHAND:
399 case -ERESTARTNOINTR:
400 /* Restart system call with magic TIF bit. */
401 regs->gprs[2] = regs->orig_gpr2;
402 set_thread_flag(TIF_SYSCALL);
403 if (test_thread_flag(TIF_SINGLE_STEP))
404 set_thread_flag(TIF_PER_TRAP);
410 * If there's no signal to deliver, we just put the saved sigmask back.
412 restore_saved_sigmask();
415 void do_notify_resume(struct pt_regs *regs)
417 clear_thread_flag(TIF_NOTIFY_RESUME);
418 tracehook_notify_resume(regs);