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
)
62 spin_lock_irq(¤t
->sighand
->siglock
);
63 current
->saved_sigmask
= current
->blocked
;
64 siginitset(¤t
->blocked
, mask
);
66 spin_unlock_irq(¤t
->sighand
->siglock
);
68 current
->state
= TASK_INTERRUPTIBLE
;
70 set_restore_sigmask();
72 return -ERESTARTNOHAND
;
76 sys_sigaction(int sig
, const struct old_sigaction __user
*act
,
77 struct old_sigaction __user
*oact
)
79 struct k_sigaction new_ka
, old_ka
;
84 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
85 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
86 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
88 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
89 __get_user(mask
, &act
->sa_mask
);
90 siginitset(&new_ka
.sa
.sa_mask
, mask
);
93 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
96 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
97 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
98 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
100 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
101 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
108 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
,
109 unsigned long r6
, unsigned long r7
,
110 struct pt_regs __regs
)
112 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
114 return do_sigaltstack(uss
, uoss
, regs
->regs
[15]);
119 * Do a signal return; undo the signal stack.
122 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
123 #if defined(CONFIG_CPU_SH2)
124 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
126 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
128 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
132 struct sigcontext sc
;
133 unsigned long extramask
[_NSIG_WORDS
-1];
145 static inline int restore_sigcontext_fpu(struct sigcontext __user
*sc
)
147 struct task_struct
*tsk
= current
;
149 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
153 return __copy_from_user(&tsk
->thread
.xstate
->hardfpu
, &sc
->sc_fpregs
[0],
154 sizeof(long)*(16*2+2));
157 static inline int save_sigcontext_fpu(struct sigcontext __user
*sc
,
158 struct pt_regs
*regs
)
160 struct task_struct
*tsk
= current
;
162 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
166 __put_user(0, &sc
->sc_ownedfp
);
170 __put_user(1, &sc
->sc_ownedfp
);
172 /* This will cause a "finit" to be triggered by the next
173 attempted FPU operation by the 'current' process.
177 unlazy_fpu(tsk
, regs
);
178 return __copy_to_user(&sc
->sc_fpregs
[0], &tsk
->thread
.xstate
->hardfpu
,
179 sizeof(long)*(16*2+2));
181 #endif /* CONFIG_SH_FPU */
184 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
, int *r0_p
)
186 unsigned int err
= 0;
188 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
190 COPY(regs
[2]); COPY(regs
[3]);
191 COPY(regs
[4]); COPY(regs
[5]);
192 COPY(regs
[6]); COPY(regs
[7]);
193 COPY(regs
[8]); COPY(regs
[9]);
194 COPY(regs
[10]); COPY(regs
[11]);
195 COPY(regs
[12]); COPY(regs
[13]);
196 COPY(regs
[14]); COPY(regs
[15]);
197 COPY(gbr
); COPY(mach
);
198 COPY(macl
); COPY(pr
);
203 if (boot_cpu_data
.flags
& CPU_HAS_FPU
) {
205 struct task_struct
*tsk
= current
;
207 regs
->sr
|= SR_FD
; /* Release FPU */
208 clear_fpu(tsk
, regs
);
210 __get_user (owned_fp
, &sc
->sc_ownedfp
);
212 err
|= restore_sigcontext_fpu(sc
);
216 regs
->tra
= -1; /* disable syscall checks */
217 err
|= __get_user(*r0_p
, &sc
->sc_regs
[0]);
221 asmlinkage
int sys_sigreturn(unsigned long r4
, unsigned long r5
,
222 unsigned long r6
, unsigned long r7
,
223 struct pt_regs __regs
)
225 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
226 struct sigframe __user
*frame
= (struct sigframe __user
*)regs
->regs
[15];
230 /* Always make any pending restarted system calls return -EINTR */
231 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
233 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
236 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
238 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
239 sizeof(frame
->extramask
))))
242 sigdelsetmask(&set
, ~_BLOCKABLE
);
244 spin_lock_irq(¤t
->sighand
->siglock
);
245 current
->blocked
= set
;
247 spin_unlock_irq(¤t
->sighand
->siglock
);
249 if (restore_sigcontext(regs
, &frame
->sc
, &r0
))
254 force_sig(SIGSEGV
, current
);
258 asmlinkage
int sys_rt_sigreturn(unsigned long r4
, unsigned long r5
,
259 unsigned long r6
, unsigned long r7
,
260 struct pt_regs __regs
)
262 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
263 struct rt_sigframe __user
*frame
= (struct rt_sigframe __user
*)regs
->regs
[15];
267 /* Always make any pending restarted system calls return -EINTR */
268 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
270 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
273 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
276 sigdelsetmask(&set
, ~_BLOCKABLE
);
277 spin_lock_irq(¤t
->sighand
->siglock
);
278 current
->blocked
= set
;
280 spin_unlock_irq(¤t
->sighand
->siglock
);
282 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &r0
))
285 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
,
286 regs
->regs
[15]) == -EFAULT
)
292 force_sig(SIGSEGV
, current
);
297 * Set up a signal frame.
301 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
306 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
307 COPY(regs
[0]); COPY(regs
[1]);
308 COPY(regs
[2]); COPY(regs
[3]);
309 COPY(regs
[4]); COPY(regs
[5]);
310 COPY(regs
[6]); COPY(regs
[7]);
311 COPY(regs
[8]); COPY(regs
[9]);
312 COPY(regs
[10]); COPY(regs
[11]);
313 COPY(regs
[12]); COPY(regs
[13]);
314 COPY(regs
[14]); COPY(regs
[15]);
315 COPY(gbr
); COPY(mach
);
316 COPY(macl
); COPY(pr
);
321 err
|= save_sigcontext_fpu(sc
, regs
);
324 /* non-iBCS2 extensions.. */
325 err
|= __put_user(mask
, &sc
->oldmask
);
331 * Determine which stack to use..
333 static inline void __user
*
334 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
336 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
337 if (sas_ss_flags(sp
) == 0)
338 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
341 return (void __user
*)((sp
- (frame_size
+UNWINDGUARD
)) & -8ul);
344 /* These symbols are defined with the addresses in the vsyscall page.
345 See vsyscall-trapa.S. */
346 extern void __kernel_sigreturn(void);
347 extern void __kernel_rt_sigreturn(void);
349 static int setup_frame(int sig
, struct k_sigaction
*ka
,
350 sigset_t
*set
, struct pt_regs
*regs
)
352 struct sigframe __user
*frame
;
356 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
358 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
361 signal
= current_thread_info()->exec_domain
362 && current_thread_info()->exec_domain
->signal_invmap
364 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
367 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
370 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
371 sizeof(frame
->extramask
));
373 /* Set up to return from userspace. If provided, use a stub
374 already in userspace. */
375 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
376 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
377 #ifdef CONFIG_VSYSCALL
378 } else if (likely(current
->mm
->context
.vdso
)) {
379 regs
->pr
= VDSO_SYM(&__kernel_sigreturn
);
382 /* Generate return code (system call to sigreturn) */
383 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
384 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
385 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
386 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
387 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
388 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
389 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
390 err
|= __put_user((__NR_sigreturn
), &frame
->retcode
[7]);
391 regs
->pr
= (unsigned long) frame
->retcode
;
392 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
398 /* Set up registers for signal handler */
399 regs
->regs
[15] = (unsigned long) frame
;
400 regs
->regs
[4] = signal
; /* Arg for signal handler */
402 regs
->regs
[6] = (unsigned long) &frame
->sc
;
404 if (current
->personality
& FDPIC_FUNCPTRS
) {
405 struct fdpic_func_descriptor __user
*funcptr
=
406 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
408 __get_user(regs
->pc
, &funcptr
->text
);
409 __get_user(regs
->regs
[12], &funcptr
->GOT
);
411 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
415 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
416 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
421 force_sigsegv(sig
, current
);
425 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
426 sigset_t
*set
, struct pt_regs
*regs
)
428 struct rt_sigframe __user
*frame
;
432 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
434 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
437 signal
= current_thread_info()->exec_domain
438 && current_thread_info()->exec_domain
->signal_invmap
440 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
443 err
|= copy_siginfo_to_user(&frame
->info
, info
);
445 /* Create the ucontext. */
446 err
|= __put_user(0, &frame
->uc
.uc_flags
);
447 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
448 err
|= __put_user((void *)current
->sas_ss_sp
,
449 &frame
->uc
.uc_stack
.ss_sp
);
450 err
|= __put_user(sas_ss_flags(regs
->regs
[15]),
451 &frame
->uc
.uc_stack
.ss_flags
);
452 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
453 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
455 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
457 /* Set up to return from userspace. If provided, use a stub
458 already in userspace. */
459 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
460 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
461 #ifdef CONFIG_VSYSCALL
462 } else if (likely(current
->mm
->context
.vdso
)) {
463 regs
->pr
= VDSO_SYM(&__kernel_rt_sigreturn
);
466 /* Generate return code (system call to rt_sigreturn) */
467 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
468 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
469 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
470 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
471 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
472 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
473 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
474 err
|= __put_user((__NR_rt_sigreturn
), &frame
->retcode
[7]);
475 regs
->pr
= (unsigned long) frame
->retcode
;
476 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
482 /* Set up registers for signal handler */
483 regs
->regs
[15] = (unsigned long) frame
;
484 regs
->regs
[4] = signal
; /* Arg for signal handler */
485 regs
->regs
[5] = (unsigned long) &frame
->info
;
486 regs
->regs
[6] = (unsigned long) &frame
->uc
;
488 if (current
->personality
& FDPIC_FUNCPTRS
) {
489 struct fdpic_func_descriptor __user
*funcptr
=
490 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
492 __get_user(regs
->pc
, &funcptr
->text
);
493 __get_user(regs
->regs
[12], &funcptr
->GOT
);
495 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
499 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
500 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
505 force_sigsegv(sig
, current
);
510 handle_syscall_restart(unsigned long save_r0
, struct pt_regs
*regs
,
511 struct sigaction
*sa
)
513 /* If we're not from a syscall, bail out */
517 /* check for system call restart.. */
518 switch (regs
->regs
[0]) {
519 case -ERESTART_RESTARTBLOCK
:
520 case -ERESTARTNOHAND
:
521 no_system_call_restart
:
522 regs
->regs
[0] = -EINTR
;
526 if (!(sa
->sa_flags
& SA_RESTART
))
527 goto no_system_call_restart
;
529 case -ERESTARTNOINTR
:
530 regs
->regs
[0] = save_r0
;
531 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
537 * OK, we're invoking a handler
540 handle_signal(unsigned long sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
541 sigset_t
*oldset
, struct pt_regs
*regs
, unsigned int save_r0
)
545 /* Set up the stack frame */
546 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
547 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
549 ret
= setup_frame(sig
, ka
, oldset
, regs
);
551 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
552 ka
->sa
.sa_handler
= SIG_DFL
;
555 spin_lock_irq(¤t
->sighand
->siglock
);
556 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
557 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
558 sigaddset(¤t
->blocked
,sig
);
560 spin_unlock_irq(¤t
->sighand
->siglock
);
567 * Note that 'init' is a special process: it doesn't get signals it doesn't
568 * want to handle. Thus you cannot kill init even with a SIGKILL even by
571 * Note that we go through the signals twice: once to check the signals that
572 * the kernel can handle, and then we build all the user-level signal handling
573 * stack-frames in one go after that.
575 static void do_signal(struct pt_regs
*regs
, unsigned int save_r0
)
579 struct k_sigaction ka
;
583 * We want the common case to go fast, which
584 * is why we may in certain cases get here from
585 * kernel mode. Just return without doing anything
588 if (!user_mode(regs
))
594 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
)
595 oldset
= ¤t
->saved_sigmask
;
597 oldset
= ¤t
->blocked
;
599 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
601 handle_syscall_restart(save_r0
, regs
, &ka
.sa
);
603 /* Whee! Actually deliver the signal. */
604 if (handle_signal(signr
, &ka
, &info
, oldset
,
605 regs
, save_r0
) == 0) {
607 * A signal was successfully delivered; the saved
608 * sigmask will have been stored in the signal frame,
609 * and will be restored by sigreturn, so we can simply
610 * clear the TS_RESTORE_SIGMASK flag
612 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
614 tracehook_signal_handler(signr
, &info
, &ka
, regs
,
615 test_thread_flag(TIF_SINGLESTEP
));
622 /* Did we come from a system call? */
623 if (regs
->tra
>= 0) {
624 /* Restart the system call - no handlers present */
625 if (regs
->regs
[0] == -ERESTARTNOHAND
||
626 regs
->regs
[0] == -ERESTARTSYS
||
627 regs
->regs
[0] == -ERESTARTNOINTR
) {
628 regs
->regs
[0] = save_r0
;
629 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
630 } else if (regs
->regs
[0] == -ERESTART_RESTARTBLOCK
) {
631 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
632 regs
->regs
[3] = __NR_restart_syscall
;
637 * If there's no signal to deliver, we just put the saved sigmask
640 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
) {
641 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
642 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
646 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, unsigned int save_r0
,
647 unsigned long thread_info_flags
)
649 /* deal with pending signal delivery */
650 if (thread_info_flags
& _TIF_SIGPENDING
)
651 do_signal(regs
, save_r0
);
653 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
654 clear_thread_flag(TIF_NOTIFY_RESUME
);
655 tracehook_notify_resume(regs
);
656 if (current
->replacement_session_keyring
)
657 key_replace_session_keyring();