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>
13 #include <linux/sched/task_stack.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/tty.h>
24 #include <linux/personality.h>
25 #include <linux/binfmts.h>
26 #include <linux/tracehook.h>
27 #include <linux/syscalls.h>
28 #include <linux/compat.h>
29 #include <asm/ucontext.h>
30 #include <linux/uaccess.h>
31 #include <asm/lowcore.h>
32 #include <asm/switch_to.h>
36 * Layout of an old-style signal-frame:
37 * -----------------------------------------
38 * | save area (_SIGNAL_FRAMESIZE) |
39 * -----------------------------------------
40 * | struct sigcontext |
43 * -----------------------------------------
45 * | _s390_regs_common |
47 * -----------------------------------------
49 * -----------------------------------------
50 * | _sigregs_ext with |
51 * | gprs_high 64 byte (opt) |
52 * | vxrs_low 128 byte (opt) |
53 * | vxrs_high 256 byte (opt) |
54 * | reserved 128 byte (opt) |
55 * -----------------------------------------
57 * -----------------------------------------
58 * The svc_insn entry with the sigreturn system call opcode does not
59 * have a fixed position and moves if gprs_high or vxrs exist.
60 * Future extensions will be added to _sigregs_ext.
64 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
68 _sigregs_ext sregs_ext
;
69 __u16 svc_insn
; /* Offset of svc_insn is NOT fixed! */
73 * Layout of an rt signal-frame:
74 * -----------------------------------------
75 * | save area (_SIGNAL_FRAMESIZE) |
76 * -----------------------------------------
77 * | svc __NR_rt_sigreturn 2 byte |
78 * -----------------------------------------
80 * -----------------------------------------
81 * | struct ucontext_extended with |
82 * | unsigned long uc_flags |
83 * | struct ucontext *uc_link |
84 * | stack_t uc_stack |
85 * | _sigregs uc_mcontext with |
86 * | _s390_regs_common |
88 * | sigset_t uc_sigmask |
89 * | _sigregs_ext uc_mcontext_ext |
90 * | gprs_high 64 byte (opt) |
91 * | vxrs_low 128 byte (opt) |
92 * | vxrs_high 256 byte (opt)|
93 * | reserved 128 byte (opt) |
94 * -----------------------------------------
95 * Future extensions will be added to _sigregs_ext.
99 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE
];
102 struct ucontext_extended uc
;
105 /* Store registers needed to create the signal frame */
106 static void store_sigregs(void)
108 save_access_regs(current
->thread
.acrs
);
112 /* Load registers after signal return */
113 static void load_sigregs(void)
115 restore_access_regs(current
->thread
.acrs
);
118 /* Returns non-zero on fault. */
119 static int save_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
123 /* Copy a 'clean' PSW mask to the user to avoid leaking
124 information about whether PER is currently on. */
125 user_sregs
.regs
.psw
.mask
= PSW_USER_BITS
|
126 (regs
->psw
.mask
& (PSW_MASK_USER
| PSW_MASK_RI
));
127 user_sregs
.regs
.psw
.addr
= regs
->psw
.addr
;
128 memcpy(&user_sregs
.regs
.gprs
, ®s
->gprs
, sizeof(sregs
->regs
.gprs
));
129 memcpy(&user_sregs
.regs
.acrs
, current
->thread
.acrs
,
130 sizeof(user_sregs
.regs
.acrs
));
131 fpregs_store(&user_sregs
.fpregs
, ¤t
->thread
.fpu
);
132 if (__copy_to_user(sregs
, &user_sregs
, sizeof(_sigregs
)))
137 static int restore_sigregs(struct pt_regs
*regs
, _sigregs __user
*sregs
)
141 /* Alwys make any pending restarted system call return -EINTR */
142 current
->restart_block
.fn
= do_no_restart_syscall
;
144 if (__copy_from_user(&user_sregs
, sregs
, sizeof(user_sregs
)))
147 if (!is_ri_task(current
) && (user_sregs
.regs
.psw
.mask
& PSW_MASK_RI
))
150 /* Test the floating-point-control word. */
151 if (test_fp_ctl(user_sregs
.fpregs
.fpc
))
154 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
155 regs
->psw
.mask
= (regs
->psw
.mask
& ~(PSW_MASK_USER
| PSW_MASK_RI
)) |
156 (user_sregs
.regs
.psw
.mask
& (PSW_MASK_USER
| PSW_MASK_RI
));
157 /* Check for invalid user address space control. */
158 if ((regs
->psw
.mask
& PSW_MASK_ASC
) == PSW_ASC_HOME
)
159 regs
->psw
.mask
= PSW_ASC_PRIMARY
|
160 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
161 /* Check for invalid amode */
162 if (regs
->psw
.mask
& PSW_MASK_EA
)
163 regs
->psw
.mask
|= PSW_MASK_BA
;
164 regs
->psw
.addr
= user_sregs
.regs
.psw
.addr
;
165 memcpy(®s
->gprs
, &user_sregs
.regs
.gprs
, sizeof(sregs
->regs
.gprs
));
166 memcpy(¤t
->thread
.acrs
, &user_sregs
.regs
.acrs
,
167 sizeof(current
->thread
.acrs
));
169 fpregs_load(&user_sregs
.fpregs
, ¤t
->thread
.fpu
);
171 clear_pt_regs_flag(regs
, PIF_SYSCALL
); /* No longer in a system call */
175 /* Returns non-zero on fault. */
176 static int save_sigregs_ext(struct pt_regs
*regs
,
177 _sigregs_ext __user
*sregs_ext
)
179 __u64 vxrs
[__NUM_VXRS_LOW
];
182 /* Save vector registers to signal stack */
183 if (MACHINE_HAS_VX
) {
184 for (i
= 0; i
< __NUM_VXRS_LOW
; i
++)
185 vxrs
[i
] = *((__u64
*)(current
->thread
.fpu
.vxrs
+ i
) + 1);
186 if (__copy_to_user(&sregs_ext
->vxrs_low
, vxrs
,
187 sizeof(sregs_ext
->vxrs_low
)) ||
188 __copy_to_user(&sregs_ext
->vxrs_high
,
189 current
->thread
.fpu
.vxrs
+ __NUM_VXRS_LOW
,
190 sizeof(sregs_ext
->vxrs_high
)))
196 static int restore_sigregs_ext(struct pt_regs
*regs
,
197 _sigregs_ext __user
*sregs_ext
)
199 __u64 vxrs
[__NUM_VXRS_LOW
];
202 /* Restore vector registers from signal stack */
203 if (MACHINE_HAS_VX
) {
204 if (__copy_from_user(vxrs
, &sregs_ext
->vxrs_low
,
205 sizeof(sregs_ext
->vxrs_low
)) ||
206 __copy_from_user(current
->thread
.fpu
.vxrs
+ __NUM_VXRS_LOW
,
207 &sregs_ext
->vxrs_high
,
208 sizeof(sregs_ext
->vxrs_high
)))
210 for (i
= 0; i
< __NUM_VXRS_LOW
; i
++)
211 *((__u64
*)(current
->thread
.fpu
.vxrs
+ i
) + 1) = vxrs
[i
];
216 SYSCALL_DEFINE0(sigreturn
)
218 struct pt_regs
*regs
= task_pt_regs(current
);
219 struct sigframe __user
*frame
=
220 (struct sigframe __user
*) regs
->gprs
[15];
223 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE
))
225 set_current_blocked(&set
);
227 if (restore_sigregs(regs
, &frame
->sregs
))
229 if (restore_sigregs_ext(regs
, &frame
->sregs_ext
))
232 return regs
->gprs
[2];
234 force_sig(SIGSEGV
, current
);
238 SYSCALL_DEFINE0(rt_sigreturn
)
240 struct pt_regs
*regs
= task_pt_regs(current
);
241 struct rt_sigframe __user
*frame
=
242 (struct rt_sigframe __user
*)regs
->gprs
[15];
245 if (__copy_from_user(&set
.sig
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
247 set_current_blocked(&set
);
248 if (restore_altstack(&frame
->uc
.uc_stack
))
251 if (restore_sigregs(regs
, &frame
->uc
.uc_mcontext
))
253 if (restore_sigregs_ext(regs
, &frame
->uc
.uc_mcontext_ext
))
256 return regs
->gprs
[2];
258 force_sig(SIGSEGV
, current
);
263 * Determine which stack to use..
265 static inline void __user
*
266 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
270 /* Default to using normal stack */
273 /* Overflow on alternate signal stack gives SIGSEGV. */
274 if (on_sig_stack(sp
) && !on_sig_stack((sp
- frame_size
) & -8UL))
275 return (void __user
*) -1UL;
277 /* This is the X/Open sanctioned signal stack switching. */
278 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
279 if (! sas_ss_flags(sp
))
280 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
283 return (void __user
*)((sp
- frame_size
) & -8ul);
286 static int setup_frame(int sig
, struct k_sigaction
*ka
,
287 sigset_t
*set
, struct pt_regs
* regs
)
289 struct sigframe __user
*frame
;
290 struct sigcontext sc
;
291 unsigned long restorer
;
295 * gprs_high are only present for a 31-bit task running on
296 * a 64-bit kernel (see compat_signal.c) but the space for
297 * gprs_high need to be allocated if vector registers are
298 * included in the signal frame on a 31-bit system.
300 frame_size
= sizeof(*frame
) - sizeof(frame
->sregs_ext
);
302 frame_size
+= sizeof(frame
->sregs_ext
);
303 frame
= get_sigframe(ka
, regs
, frame_size
);
304 if (frame
== (void __user
*) -1UL)
307 /* Set up backchain. */
308 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
311 /* Create struct sigcontext on the signal stack */
312 memcpy(&sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE
);
313 sc
.sregs
= (_sigregs __user __force
*) &frame
->sregs
;
314 if (__copy_to_user(&frame
->sc
, &sc
, sizeof(frame
->sc
)))
317 /* Store registers needed to create the signal frame */
320 /* Create _sigregs on the signal stack */
321 if (save_sigregs(regs
, &frame
->sregs
))
324 /* Place signal number on stack to allow backtrace from handler. */
325 if (__put_user(regs
->gprs
[2], (int __user
*) &frame
->signo
))
328 /* Create _sigregs_ext on the signal stack */
329 if (save_sigregs_ext(regs
, &frame
->sregs_ext
))
332 /* Set up to return from userspace. If provided, use a stub
333 already in userspace. */
334 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
335 restorer
= (unsigned long) ka
->sa
.sa_restorer
;
337 /* Signal frame without vector registers are short ! */
338 __u16 __user
*svc
= (void __user
*) frame
+ frame_size
- 2;
339 if (__put_user(S390_SYSCALL_OPCODE
| __NR_sigreturn
, svc
))
341 restorer
= (unsigned long) svc
;
344 /* Set up registers for signal handler */
345 regs
->gprs
[14] = restorer
;
346 regs
->gprs
[15] = (unsigned long) frame
;
347 /* Force default amode and default user address space control. */
348 regs
->psw
.mask
= PSW_MASK_EA
| PSW_MASK_BA
|
349 (PSW_USER_BITS
& PSW_MASK_ASC
) |
350 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
351 regs
->psw
.addr
= (unsigned long) ka
->sa
.sa_handler
;
354 regs
->gprs
[3] = (unsigned long) &frame
->sc
;
356 /* We forgot to include these in the sigcontext.
357 To avoid breaking binary compatibility, they are passed as args. */
358 if (sig
== SIGSEGV
|| sig
== SIGBUS
|| sig
== SIGILL
||
359 sig
== SIGTRAP
|| sig
== SIGFPE
) {
360 /* set extra registers only for synchronous signals */
361 regs
->gprs
[4] = regs
->int_code
& 127;
362 regs
->gprs
[5] = regs
->int_parm_long
;
363 regs
->gprs
[6] = current
->thread
.last_break
;
368 static int setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
,
369 struct pt_regs
*regs
)
371 struct rt_sigframe __user
*frame
;
372 unsigned long uc_flags
, restorer
;
375 frame_size
= sizeof(struct rt_sigframe
) - sizeof(_sigregs_ext
);
377 * gprs_high are only present for a 31-bit task running on
378 * a 64-bit kernel (see compat_signal.c) but the space for
379 * gprs_high need to be allocated if vector registers are
380 * included in the signal frame on a 31-bit system.
383 if (MACHINE_HAS_VX
) {
384 frame_size
+= sizeof(_sigregs_ext
);
387 frame
= get_sigframe(&ksig
->ka
, regs
, frame_size
);
388 if (frame
== (void __user
*) -1UL)
391 /* Set up backchain. */
392 if (__put_user(regs
->gprs
[15], (addr_t __user
*) frame
))
395 /* Set up to return from userspace. If provided, use a stub
396 already in userspace. */
397 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
398 restorer
= (unsigned long) ksig
->ka
.sa
.sa_restorer
;
400 __u16 __user
*svc
= &frame
->svc_insn
;
401 if (__put_user(S390_SYSCALL_OPCODE
| __NR_rt_sigreturn
, svc
))
403 restorer
= (unsigned long) svc
;
406 /* Create siginfo on the signal stack */
407 if (copy_siginfo_to_user(&frame
->info
, &ksig
->info
))
410 /* Store registers needed to create the signal frame */
413 /* Create ucontext on the signal stack. */
414 if (__put_user(uc_flags
, &frame
->uc
.uc_flags
) ||
415 __put_user(NULL
, &frame
->uc
.uc_link
) ||
416 __save_altstack(&frame
->uc
.uc_stack
, regs
->gprs
[15]) ||
417 save_sigregs(regs
, &frame
->uc
.uc_mcontext
) ||
418 __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
)) ||
419 save_sigregs_ext(regs
, &frame
->uc
.uc_mcontext_ext
))
422 /* Set up registers for signal handler */
423 regs
->gprs
[14] = restorer
;
424 regs
->gprs
[15] = (unsigned long) frame
;
425 /* Force default amode and default user address space control. */
426 regs
->psw
.mask
= PSW_MASK_EA
| PSW_MASK_BA
|
427 (PSW_USER_BITS
& PSW_MASK_ASC
) |
428 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
429 regs
->psw
.addr
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
431 regs
->gprs
[2] = ksig
->sig
;
432 regs
->gprs
[3] = (unsigned long) &frame
->info
;
433 regs
->gprs
[4] = (unsigned long) &frame
->uc
;
434 regs
->gprs
[5] = current
->thread
.last_break
;
438 static void handle_signal(struct ksignal
*ksig
, sigset_t
*oldset
,
439 struct pt_regs
*regs
)
443 /* Set up the stack frame */
444 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
445 ret
= setup_rt_frame(ksig
, oldset
, regs
);
447 ret
= setup_frame(ksig
->sig
, &ksig
->ka
, oldset
, regs
);
449 signal_setup_done(ret
, ksig
, test_thread_flag(TIF_SINGLE_STEP
));
453 * Note that 'init' is a special process: it doesn't get signals it doesn't
454 * want to handle. Thus you cannot kill init even with a SIGKILL even by
457 * Note that we go through the signals twice: once to check the signals that
458 * the kernel can handle, and then we build all the user-level signal handling
459 * stack-frames in one go after that.
461 void do_signal(struct pt_regs
*regs
)
464 sigset_t
*oldset
= sigmask_to_save();
467 * Get signal to deliver. When running under ptrace, at this point
468 * the debugger may change all our registers, including the system
471 current
->thread
.system_call
=
472 test_pt_regs_flag(regs
, PIF_SYSCALL
) ? regs
->int_code
: 0;
474 if (get_signal(&ksig
)) {
475 /* Whee! Actually deliver the signal. */
476 if (current
->thread
.system_call
) {
477 regs
->int_code
= current
->thread
.system_call
;
478 /* Check for system call restarting. */
479 switch (regs
->gprs
[2]) {
480 case -ERESTART_RESTARTBLOCK
:
481 case -ERESTARTNOHAND
:
482 regs
->gprs
[2] = -EINTR
;
485 if (!(ksig
.ka
.sa
.sa_flags
& SA_RESTART
)) {
486 regs
->gprs
[2] = -EINTR
;
490 case -ERESTARTNOINTR
:
491 regs
->gprs
[2] = regs
->orig_gpr2
;
493 __rewind_psw(regs
->psw
,
494 regs
->int_code
>> 16);
498 /* No longer in a system call */
499 clear_pt_regs_flag(regs
, PIF_SYSCALL
);
501 if (is_compat_task())
502 handle_signal32(&ksig
, oldset
, regs
);
504 handle_signal(&ksig
, oldset
, regs
);
508 /* No handlers present - check for system call restart */
509 clear_pt_regs_flag(regs
, PIF_SYSCALL
);
510 if (current
->thread
.system_call
) {
511 regs
->int_code
= current
->thread
.system_call
;
512 switch (regs
->gprs
[2]) {
513 case -ERESTART_RESTARTBLOCK
:
514 /* Restart with sys_restart_syscall */
515 regs
->int_code
= __NR_restart_syscall
;
517 case -ERESTARTNOHAND
:
519 case -ERESTARTNOINTR
:
520 /* Restart system call with magic TIF bit. */
521 regs
->gprs
[2] = regs
->orig_gpr2
;
522 set_pt_regs_flag(regs
, PIF_SYSCALL
);
523 if (test_thread_flag(TIF_SINGLE_STEP
))
524 clear_pt_regs_flag(regs
, PIF_PER_TRAP
);
530 * If there's no signal to deliver, we just put the saved sigmask back.
532 restore_saved_sigmask();
535 void do_notify_resume(struct pt_regs
*regs
)
537 clear_thread_flag(TIF_NOTIFY_RESUME
);
538 tracehook_notify_resume(regs
);