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>
35 * Layout of an old-style signal-frame:
36 * -----------------------------------------
37 * | save area (_SIGNAL_FRAMESIZE) |
38 * -----------------------------------------
39 * | struct sigcontext |
42 * -----------------------------------------
44 * | _s390_regs_common |
46 * -----------------------------------------
48 * -----------------------------------------
49 * | _sigregs_ext with |
50 * | gprs_high 64 byte (opt) |
51 * | vxrs_low 128 byte (opt) |
52 * | vxrs_high 256 byte (opt) |
53 * | reserved 128 byte (opt) |
54 * -----------------------------------------
56 * -----------------------------------------
57 * The svc_insn entry with the sigreturn system call opcode does not
58 * have a fixed position and moves if gprs_high or vxrs exist.
59 * Future extensions will be added to _sigregs_ext.
63 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
67 _sigregs_ext sregs_ext
;
68 __u16 svc_insn
; /* Offset of svc_insn is NOT fixed! */
72 * Layout of an rt signal-frame:
73 * -----------------------------------------
74 * | save area (_SIGNAL_FRAMESIZE) |
75 * -----------------------------------------
76 * | svc __NR_rt_sigreturn 2 byte |
77 * -----------------------------------------
79 * -----------------------------------------
80 * | struct ucontext_extended with |
81 * | unsigned long uc_flags |
82 * | struct ucontext *uc_link |
83 * | stack_t uc_stack |
84 * | _sigregs uc_mcontext with |
85 * | _s390_regs_common |
87 * | sigset_t uc_sigmask |
88 * | _sigregs_ext uc_mcontext_ext |
89 * | gprs_high 64 byte (opt) |
90 * | vxrs_low 128 byte (opt) |
91 * | vxrs_high 256 byte (opt)|
92 * | reserved 128 byte (opt) |
93 * -----------------------------------------
94 * Future extensions will be added to _sigregs_ext.
98 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
101 struct ucontext_extended uc
;
104 /* Store registers needed to create the signal frame */
105 static void store_sigregs(void)
107 save_access_regs(current
->thread
.acrs
);
108 save_fp_ctl(¤t
->thread
.fp_regs
.fpc
);
110 if (current
->thread
.vxrs
) {
113 save_vx_regs(current
->thread
.vxrs
);
114 for (i
= 0; i
< __NUM_FPRS
; i
++)
115 current
->thread
.fp_regs
.fprs
[i
] =
116 *(freg_t
*)(current
->thread
.vxrs
+ i
);
119 save_fp_regs(current
->thread
.fp_regs
.fprs
);
122 /* Load registers after signal return */
123 static void load_sigregs(void)
125 restore_access_regs(current
->thread
.acrs
);
126 /* restore_fp_ctl is done in restore_sigregs */
128 if (current
->thread
.vxrs
) {
131 for (i
= 0; i
< __NUM_FPRS
; i
++)
132 *(freg_t
*)(current
->thread
.vxrs
+ i
) =
133 current
->thread
.fp_regs
.fprs
[i
];
134 restore_vx_regs(current
->thread
.vxrs
);
137 restore_fp_regs(current
->thread
.fp_regs
.fprs
);
140 /* Returns non-zero on fault. */
141 static int save_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
145 /* Copy a 'clean' PSW mask to the user to avoid leaking
146 information about whether PER is currently on. */
147 user_sregs
.regs
.psw
.mask
= PSW_USER_BITS
|
148 (regs
->psw
.mask
& (PSW_MASK_USER
| PSW_MASK_RI
));
149 user_sregs
.regs
.psw
.addr
= regs
->psw
.addr
;
150 memcpy(&user_sregs
.regs
.gprs
, ®s
->gprs
, sizeof(sregs
->regs
.gprs
));
151 memcpy(&user_sregs
.regs
.acrs
, current
->thread
.acrs
,
152 sizeof(user_sregs
.regs
.acrs
));
153 memcpy(&user_sregs
.fpregs
, ¤t
->thread
.fp_regs
,
154 sizeof(user_sregs
.fpregs
));
155 if (__copy_to_user(sregs
, &user_sregs
, sizeof(_sigregs
)))
160 static int restore_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
164 /* Alwys make any pending restarted system call return -EINTR */
165 current
->restart_block
.fn
= do_no_restart_syscall
;
167 if (__copy_from_user(&user_sregs
, sregs
, sizeof(user_sregs
)))
170 if (!is_ri_task(current
) && (user_sregs
.regs
.psw
.mask
& PSW_MASK_RI
))
173 /* Loading the floating-point-control word can fail. Do that first. */
174 if (restore_fp_ctl(&user_sregs
.fpregs
.fpc
))
177 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
178 regs
->psw
.mask
= (regs
->psw
.mask
& ~(PSW_MASK_USER
| PSW_MASK_RI
)) |
179 (user_sregs
.regs
.psw
.mask
& (PSW_MASK_USER
| PSW_MASK_RI
));
180 /* Check for invalid user address space control. */
181 if ((regs
->psw
.mask
& PSW_MASK_ASC
) == PSW_ASC_HOME
)
182 regs
->psw
.mask
= PSW_ASC_PRIMARY
|
183 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
184 /* Check for invalid amode */
185 if (regs
->psw
.mask
& PSW_MASK_EA
)
186 regs
->psw
.mask
|= PSW_MASK_BA
;
187 regs
->psw
.addr
= user_sregs
.regs
.psw
.addr
;
188 memcpy(®s
->gprs
, &user_sregs
.regs
.gprs
, sizeof(sregs
->regs
.gprs
));
189 memcpy(¤t
->thread
.acrs
, &user_sregs
.regs
.acrs
,
190 sizeof(current
->thread
.acrs
));
192 memcpy(¤t
->thread
.fp_regs
, &user_sregs
.fpregs
,
193 sizeof(current
->thread
.fp_regs
));
195 clear_pt_regs_flag(regs
, PIF_SYSCALL
); /* No longer in a system call */
199 /* Returns non-zero on fault. */
200 static int save_sigregs_ext(struct pt_regs
*regs
,
201 _sigregs_ext __user
*sregs_ext
)
204 __u64 vxrs
[__NUM_VXRS_LOW
];
207 /* Save vector registers to signal stack */
208 if (current
->thread
.vxrs
) {
209 for (i
= 0; i
< __NUM_VXRS_LOW
; i
++)
210 vxrs
[i
] = *((__u64
*)(current
->thread
.vxrs
+ i
) + 1);
211 if (__copy_to_user(&sregs_ext
->vxrs_low
, vxrs
,
212 sizeof(sregs_ext
->vxrs_low
)) ||
213 __copy_to_user(&sregs_ext
->vxrs_high
,
214 current
->thread
.vxrs
+ __NUM_VXRS_LOW
,
215 sizeof(sregs_ext
->vxrs_high
)))
222 static int restore_sigregs_ext(struct pt_regs
*regs
,
223 _sigregs_ext __user
*sregs_ext
)
226 __u64 vxrs
[__NUM_VXRS_LOW
];
229 /* Restore vector registers from signal stack */
230 if (current
->thread
.vxrs
) {
231 if (__copy_from_user(vxrs
, &sregs_ext
->vxrs_low
,
232 sizeof(sregs_ext
->vxrs_low
)) ||
233 __copy_from_user(current
->thread
.vxrs
+ __NUM_VXRS_LOW
,
234 &sregs_ext
->vxrs_high
,
235 sizeof(sregs_ext
->vxrs_high
)))
237 for (i
= 0; i
< __NUM_VXRS_LOW
; i
++)
238 *((__u64
*)(current
->thread
.vxrs
+ i
) + 1) = vxrs
[i
];
244 SYSCALL_DEFINE0(sigreturn
)
246 struct pt_regs
*regs
= task_pt_regs(current
);
247 struct sigframe __user
*frame
=
248 (struct sigframe __user
*) regs
->gprs
[15];
251 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE
))
253 set_current_blocked(&set
);
254 if (restore_sigregs(regs
, &frame
->sregs
))
256 if (restore_sigregs_ext(regs
, &frame
->sregs_ext
))
259 return regs
->gprs
[2];
261 force_sig(SIGSEGV
, current
);
265 SYSCALL_DEFINE0(rt_sigreturn
)
267 struct pt_regs
*regs
= task_pt_regs(current
);
268 struct rt_sigframe __user
*frame
=
269 (struct rt_sigframe __user
*)regs
->gprs
[15];
272 if (__copy_from_user(&set
.sig
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
274 set_current_blocked(&set
);
275 if (restore_altstack(&frame
->uc
.uc_stack
))
277 if (restore_sigregs(regs
, &frame
->uc
.uc_mcontext
))
279 if (restore_sigregs_ext(regs
, &frame
->uc
.uc_mcontext_ext
))
282 return regs
->gprs
[2];
284 force_sig(SIGSEGV
, current
);
289 * Determine which stack to use..
291 static inline void __user
*
292 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
296 /* Default to using normal stack */
299 /* Overflow on alternate signal stack gives SIGSEGV. */
300 if (on_sig_stack(sp
) && !on_sig_stack((sp
- frame_size
) & -8UL))
301 return (void __user
*) -1UL;
303 /* This is the X/Open sanctioned signal stack switching. */
304 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
305 if (! sas_ss_flags(sp
))
306 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
309 return (void __user
*)((sp
- frame_size
) & -8ul);
312 static int setup_frame(int sig
, struct k_sigaction
*ka
,
313 sigset_t
*set
, struct pt_regs
* regs
)
315 struct sigframe __user
*frame
;
316 struct sigcontext sc
;
317 unsigned long restorer
;
321 * gprs_high are only present for a 31-bit task running on
322 * a 64-bit kernel (see compat_signal.c) but the space for
323 * gprs_high need to be allocated if vector registers are
324 * included in the signal frame on a 31-bit system.
326 frame_size
= sizeof(*frame
) - sizeof(frame
->sregs_ext
);
328 frame_size
+= sizeof(frame
->sregs_ext
);
329 frame
= get_sigframe(ka
, regs
, frame_size
);
330 if (frame
== (void __user
*) -1UL)
333 /* Set up backchain. */
334 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
337 /* Create struct sigcontext on the signal stack */
338 memcpy(&sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE
);
339 sc
.sregs
= (_sigregs __user __force
*) &frame
->sregs
;
340 if (__copy_to_user(&frame
->sc
, &sc
, sizeof(frame
->sc
)))
343 /* Store registers needed to create the signal frame */
346 /* Create _sigregs on the signal stack */
347 if (save_sigregs(regs
, &frame
->sregs
))
350 /* Place signal number on stack to allow backtrace from handler. */
351 if (__put_user(regs
->gprs
[2], (int __user
*) &frame
->signo
))
354 /* Create _sigregs_ext on the signal stack */
355 if (save_sigregs_ext(regs
, &frame
->sregs_ext
))
358 /* Set up to return from userspace. If provided, use a stub
359 already in userspace. */
360 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
361 restorer
= (unsigned long) ka
->sa
.sa_restorer
| PSW_ADDR_AMODE
;
363 /* Signal frame without vector registers are short ! */
364 __u16 __user
*svc
= (void __user
*) frame
+ frame_size
- 2;
365 if (__put_user(S390_SYSCALL_OPCODE
| __NR_sigreturn
, svc
))
367 restorer
= (unsigned long) svc
| PSW_ADDR_AMODE
;
370 /* Set up registers for signal handler */
371 regs
->gprs
[14] = restorer
;
372 regs
->gprs
[15] = (unsigned long) frame
;
373 /* Force default amode and default user address space control. */
374 regs
->psw
.mask
= PSW_MASK_EA
| PSW_MASK_BA
|
375 (PSW_USER_BITS
& PSW_MASK_ASC
) |
376 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
377 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
| PSW_ADDR_AMODE
;
380 regs
->gprs
[3] = (unsigned long) &frame
->sc
;
382 /* We forgot to include these in the sigcontext.
383 To avoid breaking binary compatibility, they are passed as args. */
384 if (sig
== SIGSEGV
|| sig
== SIGBUS
|| sig
== SIGILL
||
385 sig
== SIGTRAP
|| sig
== SIGFPE
) {
386 /* set extra registers only for synchronous signals */
387 regs
->gprs
[4] = regs
->int_code
& 127;
388 regs
->gprs
[5] = regs
->int_parm_long
;
389 regs
->gprs
[6] = task_thread_info(current
)->last_break
;
394 static int setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
,
395 struct pt_regs
*regs
)
397 struct rt_sigframe __user
*frame
;
398 unsigned long uc_flags
, restorer
;
401 frame_size
= sizeof(struct rt_sigframe
) - sizeof(_sigregs_ext
);
403 * gprs_high are only present for a 31-bit task running on
404 * a 64-bit kernel (see compat_signal.c) but the space for
405 * gprs_high need to be allocated if vector registers are
406 * included in the signal frame on a 31-bit system.
410 if (MACHINE_HAS_VX
) {
411 frame_size
+= sizeof(_sigregs_ext
);
412 if (current
->thread
.vxrs
)
416 frame
= get_sigframe(&ksig
->ka
, regs
, frame_size
);
417 if (frame
== (void __user
*) -1UL)
420 /* Set up backchain. */
421 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
424 /* Set up to return from userspace. If provided, use a stub
425 already in userspace. */
426 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
427 restorer
= (unsigned long)
428 ksig
->ka
.sa
.sa_restorer
| PSW_ADDR_AMODE
;
430 __u16 __user
*svc
= &frame
->svc_insn
;
431 if (__put_user(S390_SYSCALL_OPCODE
| __NR_rt_sigreturn
, svc
))
433 restorer
= (unsigned long) svc
| PSW_ADDR_AMODE
;
436 /* Create siginfo on the signal stack */
437 if (copy_siginfo_to_user(&frame
->info
, &ksig
->info
))
440 /* Store registers needed to create the signal frame */
443 /* Create ucontext on the signal stack. */
444 if (__put_user(uc_flags
, &frame
->uc
.uc_flags
) ||
445 __put_user(NULL
, &frame
->uc
.uc_link
) ||
446 __save_altstack(&frame
->uc
.uc_stack
, regs
->gprs
[15]) ||
447 save_sigregs(regs
, &frame
->uc
.uc_mcontext
) ||
448 __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
)) ||
449 save_sigregs_ext(regs
, &frame
->uc
.uc_mcontext_ext
))
452 /* Set up registers for signal handler */
453 regs
->gprs
[14] = restorer
;
454 regs
->gprs
[15] = (unsigned long) frame
;
455 /* Force default amode and default user address space control. */
456 regs
->psw
.mask
= PSW_MASK_EA
| PSW_MASK_BA
|
457 (PSW_USER_BITS
& PSW_MASK_ASC
) |
458 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
459 regs
->psw
.addr
= (unsigned long) ksig
->ka
.sa
.sa_handler
| PSW_ADDR_AMODE
;
461 regs
->gprs
[2] = ksig
->sig
;
462 regs
->gprs
[3] = (unsigned long) &frame
->info
;
463 regs
->gprs
[4] = (unsigned long) &frame
->uc
;
464 regs
->gprs
[5] = task_thread_info(current
)->last_break
;
468 static void handle_signal(struct ksignal
*ksig
, sigset_t
*oldset
,
469 struct pt_regs
*regs
)
473 /* Set up the stack frame */
474 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
475 ret
= setup_rt_frame(ksig
, oldset
, regs
);
477 ret
= setup_frame(ksig
->sig
, &ksig
->ka
, oldset
, regs
);
479 signal_setup_done(ret
, ksig
, test_thread_flag(TIF_SINGLE_STEP
));
483 * Note that 'init' is a special process: it doesn't get signals it doesn't
484 * want to handle. Thus you cannot kill init even with a SIGKILL even by
487 * Note that we go through the signals twice: once to check the signals that
488 * the kernel can handle, and then we build all the user-level signal handling
489 * stack-frames in one go after that.
491 void do_signal(struct pt_regs
*regs
)
494 sigset_t
*oldset
= sigmask_to_save();
497 * Get signal to deliver. When running under ptrace, at this point
498 * the debugger may change all our registers, including the system
501 current_thread_info()->system_call
=
502 test_pt_regs_flag(regs
, PIF_SYSCALL
) ? regs
->int_code
: 0;
504 if (get_signal(&ksig
)) {
505 /* Whee! Actually deliver the signal. */
506 if (current_thread_info()->system_call
) {
507 regs
->int_code
= current_thread_info()->system_call
;
508 /* Check for system call restarting. */
509 switch (regs
->gprs
[2]) {
510 case -ERESTART_RESTARTBLOCK
:
511 case -ERESTARTNOHAND
:
512 regs
->gprs
[2] = -EINTR
;
515 if (!(ksig
.ka
.sa
.sa_flags
& SA_RESTART
)) {
516 regs
->gprs
[2] = -EINTR
;
520 case -ERESTARTNOINTR
:
521 regs
->gprs
[2] = regs
->orig_gpr2
;
523 __rewind_psw(regs
->psw
,
524 regs
->int_code
>> 16);
528 /* No longer in a system call */
529 clear_pt_regs_flag(regs
, PIF_SYSCALL
);
531 if (is_compat_task())
532 handle_signal32(&ksig
, oldset
, regs
);
534 handle_signal(&ksig
, oldset
, regs
);
538 /* No handlers present - check for system call restart */
539 clear_pt_regs_flag(regs
, PIF_SYSCALL
);
540 if (current_thread_info()->system_call
) {
541 regs
->int_code
= current_thread_info()->system_call
;
542 switch (regs
->gprs
[2]) {
543 case -ERESTART_RESTARTBLOCK
:
544 /* Restart with sys_restart_syscall */
545 regs
->int_code
= __NR_restart_syscall
;
547 case -ERESTARTNOHAND
:
549 case -ERESTARTNOINTR
:
550 /* Restart system call with magic TIF bit. */
551 regs
->gprs
[2] = regs
->orig_gpr2
;
552 set_pt_regs_flag(regs
, PIF_SYSCALL
);
553 if (test_thread_flag(TIF_SINGLE_STEP
))
554 clear_pt_regs_flag(regs
, PIF_PER_TRAP
);
560 * If there's no signal to deliver, we just put the saved sigmask back.
562 restore_saved_sigmask();
565 void do_notify_resume(struct pt_regs
*regs
)
567 clear_thread_flag(TIF_NOTIFY_RESUME
);
568 tracehook_notify_resume(regs
);