2 * linux/arch/sh/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/tty.h>
22 #include <linux/elf.h>
23 #include <linux/personality.h>
24 #include <linux/binfmts.h>
25 #include <linux/freezer.h>
27 #include <linux/tracehook.h>
28 #include <asm/system.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/cacheflush.h>
33 #include <asm/syscalls.h>
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 struct fdpic_func_descriptor
{
44 * The following define adds a 64 byte gap between the signal
45 * stack frame and previous contents of the stack. This allows
46 * frame unwinding in a function epilogue but only if a frame
47 * pointer is used in the function. This is necessary because
48 * current gcc compilers (<4.3) do not generate unwind info on
49 * SH for function epilogues.
51 #define UNWINDGUARD 64
54 * Atomically swap in the new signal mask, and wait for a signal.
57 sys_sigsuspend(old_sigset_t mask
,
58 unsigned long r5
, unsigned long r6
, unsigned long r7
,
59 struct pt_regs __regs
)
63 current
->saved_sigmask
= current
->blocked
;
66 siginitset(&blocked
, mask
);
67 set_current_blocked(&blocked
);
69 current
->state
= TASK_INTERRUPTIBLE
;
71 set_restore_sigmask();
73 return -ERESTARTNOHAND
;
77 sys_sigaction(int sig
, const struct old_sigaction __user
*act
,
78 struct old_sigaction __user
*oact
)
80 struct k_sigaction new_ka
, old_ka
;
85 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
86 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
87 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
89 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
90 __get_user(mask
, &act
->sa_mask
);
91 siginitset(&new_ka
.sa
.sa_mask
, mask
);
94 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
97 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
98 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
99 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
101 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
102 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
109 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
,
110 unsigned long r6
, unsigned long r7
,
111 struct pt_regs __regs
)
113 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
115 return do_sigaltstack(uss
, uoss
, regs
->regs
[15]);
120 * Do a signal return; undo the signal stack.
123 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
124 #if defined(CONFIG_CPU_SH2)
125 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
127 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
129 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
133 struct sigcontext sc
;
134 unsigned long extramask
[_NSIG_WORDS
-1];
146 static inline int restore_sigcontext_fpu(struct sigcontext __user
*sc
)
148 struct task_struct
*tsk
= current
;
150 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
154 return __copy_from_user(&tsk
->thread
.xstate
->hardfpu
, &sc
->sc_fpregs
[0],
155 sizeof(long)*(16*2+2));
158 static inline int save_sigcontext_fpu(struct sigcontext __user
*sc
,
159 struct pt_regs
*regs
)
161 struct task_struct
*tsk
= current
;
163 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
167 __put_user(0, &sc
->sc_ownedfp
);
171 __put_user(1, &sc
->sc_ownedfp
);
173 /* This will cause a "finit" to be triggered by the next
174 attempted FPU operation by the 'current' process.
178 unlazy_fpu(tsk
, regs
);
179 return __copy_to_user(&sc
->sc_fpregs
[0], &tsk
->thread
.xstate
->hardfpu
,
180 sizeof(long)*(16*2+2));
182 #endif /* CONFIG_SH_FPU */
185 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
, int *r0_p
)
187 unsigned int err
= 0;
189 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
191 COPY(regs
[2]); COPY(regs
[3]);
192 COPY(regs
[4]); COPY(regs
[5]);
193 COPY(regs
[6]); COPY(regs
[7]);
194 COPY(regs
[8]); COPY(regs
[9]);
195 COPY(regs
[10]); COPY(regs
[11]);
196 COPY(regs
[12]); COPY(regs
[13]);
197 COPY(regs
[14]); COPY(regs
[15]);
198 COPY(gbr
); COPY(mach
);
199 COPY(macl
); COPY(pr
);
204 if (boot_cpu_data
.flags
& CPU_HAS_FPU
) {
206 struct task_struct
*tsk
= current
;
208 regs
->sr
|= SR_FD
; /* Release FPU */
209 clear_fpu(tsk
, regs
);
211 __get_user (owned_fp
, &sc
->sc_ownedfp
);
213 err
|= restore_sigcontext_fpu(sc
);
217 regs
->tra
= -1; /* disable syscall checks */
218 err
|= __get_user(*r0_p
, &sc
->sc_regs
[0]);
222 asmlinkage
int sys_sigreturn(unsigned long r4
, unsigned long r5
,
223 unsigned long r6
, unsigned long r7
,
224 struct pt_regs __regs
)
226 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
227 struct sigframe __user
*frame
= (struct sigframe __user
*)regs
->regs
[15];
231 /* Always make any pending restarted system calls return -EINTR */
232 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
234 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
237 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
239 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
240 sizeof(frame
->extramask
))))
243 sigdelsetmask(&set
, ~_BLOCKABLE
);
244 set_current_blocked(&set
);
246 if (restore_sigcontext(regs
, &frame
->sc
, &r0
))
251 force_sig(SIGSEGV
, current
);
255 asmlinkage
int sys_rt_sigreturn(unsigned long r4
, unsigned long r5
,
256 unsigned long r6
, unsigned long r7
,
257 struct pt_regs __regs
)
259 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
260 struct rt_sigframe __user
*frame
= (struct rt_sigframe __user
*)regs
->regs
[15];
264 /* Always make any pending restarted system calls return -EINTR */
265 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
267 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
270 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
273 sigdelsetmask(&set
, ~_BLOCKABLE
);
274 set_current_blocked(&set
);
276 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &r0
))
279 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
,
280 regs
->regs
[15]) == -EFAULT
)
286 force_sig(SIGSEGV
, current
);
291 * Set up a signal frame.
295 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
300 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
301 COPY(regs
[0]); COPY(regs
[1]);
302 COPY(regs
[2]); COPY(regs
[3]);
303 COPY(regs
[4]); COPY(regs
[5]);
304 COPY(regs
[6]); COPY(regs
[7]);
305 COPY(regs
[8]); COPY(regs
[9]);
306 COPY(regs
[10]); COPY(regs
[11]);
307 COPY(regs
[12]); COPY(regs
[13]);
308 COPY(regs
[14]); COPY(regs
[15]);
309 COPY(gbr
); COPY(mach
);
310 COPY(macl
); COPY(pr
);
315 err
|= save_sigcontext_fpu(sc
, regs
);
318 /* non-iBCS2 extensions.. */
319 err
|= __put_user(mask
, &sc
->oldmask
);
325 * Determine which stack to use..
327 static inline void __user
*
328 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
330 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
331 if (sas_ss_flags(sp
) == 0)
332 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
335 return (void __user
*)((sp
- (frame_size
+UNWINDGUARD
)) & -8ul);
338 /* These symbols are defined with the addresses in the vsyscall page.
339 See vsyscall-trapa.S. */
340 extern void __kernel_sigreturn(void);
341 extern void __kernel_rt_sigreturn(void);
343 static int setup_frame(int sig
, struct k_sigaction
*ka
,
344 sigset_t
*set
, struct pt_regs
*regs
)
346 struct sigframe __user
*frame
;
350 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
352 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
355 signal
= current_thread_info()->exec_domain
356 && current_thread_info()->exec_domain
->signal_invmap
358 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
361 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
364 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
365 sizeof(frame
->extramask
));
367 /* Set up to return from userspace. If provided, use a stub
368 already in userspace. */
369 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
370 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
371 #ifdef CONFIG_VSYSCALL
372 } else if (likely(current
->mm
->context
.vdso
)) {
373 regs
->pr
= VDSO_SYM(&__kernel_sigreturn
);
376 /* Generate return code (system call to sigreturn) */
377 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
378 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
379 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
380 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
381 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
382 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
383 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
384 err
|= __put_user((__NR_sigreturn
), &frame
->retcode
[7]);
385 regs
->pr
= (unsigned long) frame
->retcode
;
386 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
392 /* Set up registers for signal handler */
393 regs
->regs
[15] = (unsigned long) frame
;
394 regs
->regs
[4] = signal
; /* Arg for signal handler */
396 regs
->regs
[6] = (unsigned long) &frame
->sc
;
398 if (current
->personality
& FDPIC_FUNCPTRS
) {
399 struct fdpic_func_descriptor __user
*funcptr
=
400 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
402 __get_user(regs
->pc
, &funcptr
->text
);
403 __get_user(regs
->regs
[12], &funcptr
->GOT
);
405 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
409 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
410 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
415 force_sigsegv(sig
, current
);
419 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
420 sigset_t
*set
, struct pt_regs
*regs
)
422 struct rt_sigframe __user
*frame
;
426 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
428 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
431 signal
= current_thread_info()->exec_domain
432 && current_thread_info()->exec_domain
->signal_invmap
434 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
437 err
|= copy_siginfo_to_user(&frame
->info
, info
);
439 /* Create the ucontext. */
440 err
|= __put_user(0, &frame
->uc
.uc_flags
);
441 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
442 err
|= __put_user((void *)current
->sas_ss_sp
,
443 &frame
->uc
.uc_stack
.ss_sp
);
444 err
|= __put_user(sas_ss_flags(regs
->regs
[15]),
445 &frame
->uc
.uc_stack
.ss_flags
);
446 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
447 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
449 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
451 /* Set up to return from userspace. If provided, use a stub
452 already in userspace. */
453 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
454 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
455 #ifdef CONFIG_VSYSCALL
456 } else if (likely(current
->mm
->context
.vdso
)) {
457 regs
->pr
= VDSO_SYM(&__kernel_rt_sigreturn
);
460 /* Generate return code (system call to rt_sigreturn) */
461 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
462 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
463 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
464 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
465 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
466 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
467 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
468 err
|= __put_user((__NR_rt_sigreturn
), &frame
->retcode
[7]);
469 regs
->pr
= (unsigned long) frame
->retcode
;
470 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
476 /* Set up registers for signal handler */
477 regs
->regs
[15] = (unsigned long) frame
;
478 regs
->regs
[4] = signal
; /* Arg for signal handler */
479 regs
->regs
[5] = (unsigned long) &frame
->info
;
480 regs
->regs
[6] = (unsigned long) &frame
->uc
;
482 if (current
->personality
& FDPIC_FUNCPTRS
) {
483 struct fdpic_func_descriptor __user
*funcptr
=
484 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
486 __get_user(regs
->pc
, &funcptr
->text
);
487 __get_user(regs
->regs
[12], &funcptr
->GOT
);
489 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
493 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
494 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
499 force_sigsegv(sig
, current
);
504 handle_syscall_restart(unsigned long save_r0
, struct pt_regs
*regs
,
505 struct sigaction
*sa
)
507 /* If we're not from a syscall, bail out */
511 /* check for system call restart.. */
512 switch (regs
->regs
[0]) {
513 case -ERESTART_RESTARTBLOCK
:
514 case -ERESTARTNOHAND
:
515 no_system_call_restart
:
516 regs
->regs
[0] = -EINTR
;
520 if (!(sa
->sa_flags
& SA_RESTART
))
521 goto no_system_call_restart
;
523 case -ERESTARTNOINTR
:
524 regs
->regs
[0] = save_r0
;
525 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
531 * OK, we're invoking a handler
534 handle_signal(unsigned long sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
535 sigset_t
*oldset
, struct pt_regs
*regs
, unsigned int save_r0
)
539 /* Set up the stack frame */
540 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
541 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
543 ret
= setup_frame(sig
, ka
, oldset
, regs
);
546 block_sigmask(ka
, sig
);
552 * Note that 'init' is a special process: it doesn't get signals it doesn't
553 * want to handle. Thus you cannot kill init even with a SIGKILL even by
556 * Note that we go through the signals twice: once to check the signals that
557 * the kernel can handle, and then we build all the user-level signal handling
558 * stack-frames in one go after that.
560 static void do_signal(struct pt_regs
*regs
, unsigned int save_r0
)
564 struct k_sigaction ka
;
568 * We want the common case to go fast, which
569 * is why we may in certain cases get here from
570 * kernel mode. Just return without doing anything
573 if (!user_mode(regs
))
579 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
)
580 oldset
= ¤t
->saved_sigmask
;
582 oldset
= ¤t
->blocked
;
584 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
586 handle_syscall_restart(save_r0
, regs
, &ka
.sa
);
588 /* Whee! Actually deliver the signal. */
589 if (handle_signal(signr
, &ka
, &info
, oldset
,
590 regs
, save_r0
) == 0) {
592 * A signal was successfully delivered; the saved
593 * sigmask will have been stored in the signal frame,
594 * and will be restored by sigreturn, so we can simply
595 * clear the TS_RESTORE_SIGMASK flag
597 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
599 tracehook_signal_handler(signr
, &info
, &ka
, regs
,
600 test_thread_flag(TIF_SINGLESTEP
));
607 /* Did we come from a system call? */
608 if (regs
->tra
>= 0) {
609 /* Restart the system call - no handlers present */
610 if (regs
->regs
[0] == -ERESTARTNOHAND
||
611 regs
->regs
[0] == -ERESTARTSYS
||
612 regs
->regs
[0] == -ERESTARTNOINTR
) {
613 regs
->regs
[0] = save_r0
;
614 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
615 } else if (regs
->regs
[0] == -ERESTART_RESTARTBLOCK
) {
616 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
617 regs
->regs
[3] = __NR_restart_syscall
;
622 * If there's no signal to deliver, we just put the saved sigmask
625 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
) {
626 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
627 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
631 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, unsigned int save_r0
,
632 unsigned long thread_info_flags
)
634 /* deal with pending signal delivery */
635 if (thread_info_flags
& _TIF_SIGPENDING
)
636 do_signal(regs
, save_r0
);
638 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
639 clear_thread_flag(TIF_NOTIFY_RESUME
);
640 tracehook_notify_resume(regs
);
641 if (current
->replacement_session_keyring
)
642 key_replace_session_keyring();