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>
26 #include <linux/tracehook.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/cacheflush.h>
31 #include <asm/syscalls.h>
34 struct fdpic_func_descriptor
{
40 * The following define adds a 64 byte gap between the signal
41 * stack frame and previous contents of the stack. This allows
42 * frame unwinding in a function epilogue but only if a frame
43 * pointer is used in the function. This is necessary because
44 * current gcc compilers (<4.3) do not generate unwind info on
45 * SH for function epilogues.
47 #define UNWINDGUARD 64
50 * Atomically swap in the new signal mask, and wait for a signal.
53 sys_sigsuspend(old_sigset_t mask
)
56 siginitset(&blocked
, mask
);
57 return sigsuspend(&blocked
);
61 sys_sigaction(int sig
, const struct old_sigaction __user
*act
,
62 struct old_sigaction __user
*oact
)
64 struct k_sigaction new_ka
, old_ka
;
69 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
70 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
71 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
) ||
72 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
) ||
73 __get_user(mask
, &act
->sa_mask
))
75 siginitset(&new_ka
.sa
.sa_mask
, mask
);
78 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
81 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
82 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
83 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
) ||
84 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
) ||
85 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
))
93 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
,
94 unsigned long r6
, unsigned long r7
,
95 struct pt_regs __regs
)
97 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
99 return do_sigaltstack(uss
, uoss
, regs
->regs
[15]);
104 * Do a signal return; undo the signal stack.
107 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
108 #if defined(CONFIG_CPU_SH2)
109 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
111 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
113 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
117 struct sigcontext sc
;
118 unsigned long extramask
[_NSIG_WORDS
-1];
130 static inline int restore_sigcontext_fpu(struct sigcontext __user
*sc
)
132 struct task_struct
*tsk
= current
;
134 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
138 return __copy_from_user(&tsk
->thread
.xstate
->hardfpu
, &sc
->sc_fpregs
[0],
139 sizeof(long)*(16*2+2));
142 static inline int save_sigcontext_fpu(struct sigcontext __user
*sc
,
143 struct pt_regs
*regs
)
145 struct task_struct
*tsk
= current
;
147 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
151 return __put_user(0, &sc
->sc_ownedfp
);
153 if (__put_user(1, &sc
->sc_ownedfp
))
156 /* This will cause a "finit" to be triggered by the next
157 attempted FPU operation by the 'current' process.
161 unlazy_fpu(tsk
, regs
);
162 return __copy_to_user(&sc
->sc_fpregs
[0], &tsk
->thread
.xstate
->hardfpu
,
163 sizeof(long)*(16*2+2));
165 #endif /* CONFIG_SH_FPU */
168 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
, int *r0_p
)
170 unsigned int err
= 0;
172 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
174 COPY(regs
[2]); COPY(regs
[3]);
175 COPY(regs
[4]); COPY(regs
[5]);
176 COPY(regs
[6]); COPY(regs
[7]);
177 COPY(regs
[8]); COPY(regs
[9]);
178 COPY(regs
[10]); COPY(regs
[11]);
179 COPY(regs
[12]); COPY(regs
[13]);
180 COPY(regs
[14]); COPY(regs
[15]);
181 COPY(gbr
); COPY(mach
);
182 COPY(macl
); COPY(pr
);
187 if (boot_cpu_data
.flags
& CPU_HAS_FPU
) {
189 struct task_struct
*tsk
= current
;
191 regs
->sr
|= SR_FD
; /* Release FPU */
192 clear_fpu(tsk
, regs
);
194 err
|= __get_user (owned_fp
, &sc
->sc_ownedfp
);
196 err
|= restore_sigcontext_fpu(sc
);
200 regs
->tra
= -1; /* disable syscall checks */
201 err
|= __get_user(*r0_p
, &sc
->sc_regs
[0]);
205 asmlinkage
int sys_sigreturn(unsigned long r4
, unsigned long r5
,
206 unsigned long r6
, unsigned long r7
,
207 struct pt_regs __regs
)
209 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
210 struct sigframe __user
*frame
= (struct sigframe __user
*)regs
->regs
[15];
214 /* Always make any pending restarted system calls return -EINTR */
215 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
217 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
220 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
222 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
223 sizeof(frame
->extramask
))))
226 set_current_blocked(&set
);
228 if (restore_sigcontext(regs
, &frame
->sc
, &r0
))
233 force_sig(SIGSEGV
, current
);
237 asmlinkage
int sys_rt_sigreturn(unsigned long r4
, unsigned long r5
,
238 unsigned long r6
, unsigned long r7
,
239 struct pt_regs __regs
)
241 struct pt_regs
*regs
= RELOC_HIDE(&__regs
, 0);
242 struct rt_sigframe __user
*frame
= (struct rt_sigframe __user
*)regs
->regs
[15];
246 /* Always make any pending restarted system calls return -EINTR */
247 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
249 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
252 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
255 set_current_blocked(&set
);
257 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &r0
))
260 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
,
261 regs
->regs
[15]) == -EFAULT
)
267 force_sig(SIGSEGV
, current
);
272 * Set up a signal frame.
276 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
281 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
282 COPY(regs
[0]); COPY(regs
[1]);
283 COPY(regs
[2]); COPY(regs
[3]);
284 COPY(regs
[4]); COPY(regs
[5]);
285 COPY(regs
[6]); COPY(regs
[7]);
286 COPY(regs
[8]); COPY(regs
[9]);
287 COPY(regs
[10]); COPY(regs
[11]);
288 COPY(regs
[12]); COPY(regs
[13]);
289 COPY(regs
[14]); COPY(regs
[15]);
290 COPY(gbr
); COPY(mach
);
291 COPY(macl
); COPY(pr
);
296 err
|= save_sigcontext_fpu(sc
, regs
);
299 /* non-iBCS2 extensions.. */
300 err
|= __put_user(mask
, &sc
->oldmask
);
306 * Determine which stack to use..
308 static inline void __user
*
309 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
311 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
312 if (sas_ss_flags(sp
) == 0)
313 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
316 return (void __user
*)((sp
- (frame_size
+UNWINDGUARD
)) & -8ul);
319 /* These symbols are defined with the addresses in the vsyscall page.
320 See vsyscall-trapa.S. */
321 extern void __kernel_sigreturn(void);
322 extern void __kernel_rt_sigreturn(void);
324 static int setup_frame(int sig
, struct k_sigaction
*ka
,
325 sigset_t
*set
, struct pt_regs
*regs
)
327 struct sigframe __user
*frame
;
331 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
333 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
336 signal
= current_thread_info()->exec_domain
337 && current_thread_info()->exec_domain
->signal_invmap
339 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
342 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
345 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
346 sizeof(frame
->extramask
));
348 /* Set up to return from userspace. If provided, use a stub
349 already in userspace. */
350 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
351 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
352 #ifdef CONFIG_VSYSCALL
353 } else if (likely(current
->mm
->context
.vdso
)) {
354 regs
->pr
= VDSO_SYM(&__kernel_sigreturn
);
357 /* Generate return code (system call to sigreturn) */
358 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
359 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
360 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
361 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
362 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
363 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
364 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
365 err
|= __put_user((__NR_sigreturn
), &frame
->retcode
[7]);
366 regs
->pr
= (unsigned long) frame
->retcode
;
367 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
373 /* Set up registers for signal handler */
374 regs
->regs
[15] = (unsigned long) frame
;
375 regs
->regs
[4] = signal
; /* Arg for signal handler */
377 regs
->regs
[6] = (unsigned long) &frame
->sc
;
379 if (current
->personality
& FDPIC_FUNCPTRS
) {
380 struct fdpic_func_descriptor __user
*funcptr
=
381 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
383 err
|= __get_user(regs
->pc
, &funcptr
->text
);
384 err
|= __get_user(regs
->regs
[12], &funcptr
->GOT
);
386 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
393 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
394 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
399 force_sigsegv(sig
, current
);
403 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
404 sigset_t
*set
, struct pt_regs
*regs
)
406 struct rt_sigframe __user
*frame
;
410 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
412 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
415 signal
= current_thread_info()->exec_domain
416 && current_thread_info()->exec_domain
->signal_invmap
418 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
421 err
|= copy_siginfo_to_user(&frame
->info
, info
);
423 /* Create the ucontext. */
424 err
|= __put_user(0, &frame
->uc
.uc_flags
);
425 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
426 err
|= __put_user((void *)current
->sas_ss_sp
,
427 &frame
->uc
.uc_stack
.ss_sp
);
428 err
|= __put_user(sas_ss_flags(regs
->regs
[15]),
429 &frame
->uc
.uc_stack
.ss_flags
);
430 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
431 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
433 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
435 /* Set up to return from userspace. If provided, use a stub
436 already in userspace. */
437 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
438 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
439 #ifdef CONFIG_VSYSCALL
440 } else if (likely(current
->mm
->context
.vdso
)) {
441 regs
->pr
= VDSO_SYM(&__kernel_rt_sigreturn
);
444 /* Generate return code (system call to rt_sigreturn) */
445 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
446 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
447 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
448 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
449 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
450 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
451 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
452 err
|= __put_user((__NR_rt_sigreturn
), &frame
->retcode
[7]);
453 regs
->pr
= (unsigned long) frame
->retcode
;
454 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
460 /* Set up registers for signal handler */
461 regs
->regs
[15] = (unsigned long) frame
;
462 regs
->regs
[4] = signal
; /* Arg for signal handler */
463 regs
->regs
[5] = (unsigned long) &frame
->info
;
464 regs
->regs
[6] = (unsigned long) &frame
->uc
;
466 if (current
->personality
& FDPIC_FUNCPTRS
) {
467 struct fdpic_func_descriptor __user
*funcptr
=
468 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
470 err
|= __get_user(regs
->pc
, &funcptr
->text
);
471 err
|= __get_user(regs
->regs
[12], &funcptr
->GOT
);
473 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
480 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
481 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
486 force_sigsegv(sig
, current
);
491 handle_syscall_restart(unsigned long save_r0
, struct pt_regs
*regs
,
492 struct sigaction
*sa
)
494 /* If we're not from a syscall, bail out */
498 /* check for system call restart.. */
499 switch (regs
->regs
[0]) {
500 case -ERESTART_RESTARTBLOCK
:
501 case -ERESTARTNOHAND
:
502 no_system_call_restart
:
503 regs
->regs
[0] = -EINTR
;
507 if (!(sa
->sa_flags
& SA_RESTART
))
508 goto no_system_call_restart
;
510 case -ERESTARTNOINTR
:
511 regs
->regs
[0] = save_r0
;
512 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
518 * OK, we're invoking a handler
521 handle_signal(unsigned long sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
522 struct pt_regs
*regs
, unsigned int save_r0
)
524 sigset_t
*oldset
= sigmask_to_save();
527 /* Set up the stack frame */
528 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
529 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
531 ret
= setup_frame(sig
, ka
, oldset
, regs
);
535 signal_delivered(sig
, info
, ka
, regs
,
536 test_thread_flag(TIF_SINGLESTEP
));
540 * Note that 'init' is a special process: it doesn't get signals it doesn't
541 * want to handle. Thus you cannot kill init even with a SIGKILL even by
544 * Note that we go through the signals twice: once to check the signals that
545 * the kernel can handle, and then we build all the user-level signal handling
546 * stack-frames in one go after that.
548 static void do_signal(struct pt_regs
*regs
, unsigned int save_r0
)
552 struct k_sigaction ka
;
555 * We want the common case to go fast, which
556 * is why we may in certain cases get here from
557 * kernel mode. Just return without doing anything
560 if (!user_mode(regs
))
563 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
565 handle_syscall_restart(save_r0
, regs
, &ka
.sa
);
567 /* Whee! Actually deliver the signal. */
568 handle_signal(signr
, &ka
, &info
, regs
, save_r0
);
572 /* Did we come from a system call? */
573 if (regs
->tra
>= 0) {
574 /* Restart the system call - no handlers present */
575 if (regs
->regs
[0] == -ERESTARTNOHAND
||
576 regs
->regs
[0] == -ERESTARTSYS
||
577 regs
->regs
[0] == -ERESTARTNOINTR
) {
578 regs
->regs
[0] = save_r0
;
579 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
580 } else if (regs
->regs
[0] == -ERESTART_RESTARTBLOCK
) {
581 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
582 regs
->regs
[3] = __NR_restart_syscall
;
587 * If there's no signal to deliver, we just put the saved sigmask
590 restore_saved_sigmask();
593 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, unsigned int save_r0
,
594 unsigned long thread_info_flags
)
596 /* deal with pending signal delivery */
597 if (thread_info_flags
& _TIF_SIGPENDING
)
598 do_signal(regs
, save_r0
);
600 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
601 clear_thread_flag(TIF_NOTIFY_RESUME
);
602 tracehook_notify_resume(regs
);