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 * Do a signal return; undo the signal stack.
53 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
54 #if defined(CONFIG_CPU_SH2)
55 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
57 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
59 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
64 unsigned long extramask
[_NSIG_WORDS
-1];
76 static inline int restore_sigcontext_fpu(struct sigcontext __user
*sc
)
78 struct task_struct
*tsk
= current
;
80 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
84 return __copy_from_user(&tsk
->thread
.xstate
->hardfpu
, &sc
->sc_fpregs
[0],
85 sizeof(long)*(16*2+2));
88 static inline int save_sigcontext_fpu(struct sigcontext __user
*sc
,
91 struct task_struct
*tsk
= current
;
93 if (!(boot_cpu_data
.flags
& CPU_HAS_FPU
))
97 return __put_user(0, &sc
->sc_ownedfp
);
99 if (__put_user(1, &sc
->sc_ownedfp
))
102 /* This will cause a "finit" to be triggered by the next
103 attempted FPU operation by the 'current' process.
107 unlazy_fpu(tsk
, regs
);
108 return __copy_to_user(&sc
->sc_fpregs
[0], &tsk
->thread
.xstate
->hardfpu
,
109 sizeof(long)*(16*2+2));
111 #endif /* CONFIG_SH_FPU */
114 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
, int *r0_p
)
116 unsigned int err
= 0;
118 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
120 COPY(regs
[2]); COPY(regs
[3]);
121 COPY(regs
[4]); COPY(regs
[5]);
122 COPY(regs
[6]); COPY(regs
[7]);
123 COPY(regs
[8]); COPY(regs
[9]);
124 COPY(regs
[10]); COPY(regs
[11]);
125 COPY(regs
[12]); COPY(regs
[13]);
126 COPY(regs
[14]); COPY(regs
[15]);
127 COPY(gbr
); COPY(mach
);
128 COPY(macl
); COPY(pr
);
133 if (boot_cpu_data
.flags
& CPU_HAS_FPU
) {
135 struct task_struct
*tsk
= current
;
137 regs
->sr
|= SR_FD
; /* Release FPU */
138 clear_fpu(tsk
, regs
);
140 err
|= __get_user (owned_fp
, &sc
->sc_ownedfp
);
142 err
|= restore_sigcontext_fpu(sc
);
146 regs
->tra
= -1; /* disable syscall checks */
147 err
|= __get_user(*r0_p
, &sc
->sc_regs
[0]);
151 asmlinkage
int sys_sigreturn(void)
153 struct pt_regs
*regs
= current_pt_regs();
154 struct sigframe __user
*frame
= (struct sigframe __user
*)regs
->regs
[15];
158 /* Always make any pending restarted system calls return -EINTR */
159 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
161 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
164 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
166 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
167 sizeof(frame
->extramask
))))
170 set_current_blocked(&set
);
172 if (restore_sigcontext(regs
, &frame
->sc
, &r0
))
177 force_sig(SIGSEGV
, current
);
181 asmlinkage
int sys_rt_sigreturn(void)
183 struct pt_regs
*regs
= current_pt_regs();
184 struct rt_sigframe __user
*frame
= (struct rt_sigframe __user
*)regs
->regs
[15];
188 /* Always make any pending restarted system calls return -EINTR */
189 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
191 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
194 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
197 set_current_blocked(&set
);
199 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &r0
))
202 if (restore_altstack(&frame
->uc
.uc_stack
))
208 force_sig(SIGSEGV
, current
);
213 * Set up a signal frame.
217 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
222 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
223 COPY(regs
[0]); COPY(regs
[1]);
224 COPY(regs
[2]); COPY(regs
[3]);
225 COPY(regs
[4]); COPY(regs
[5]);
226 COPY(regs
[6]); COPY(regs
[7]);
227 COPY(regs
[8]); COPY(regs
[9]);
228 COPY(regs
[10]); COPY(regs
[11]);
229 COPY(regs
[12]); COPY(regs
[13]);
230 COPY(regs
[14]); COPY(regs
[15]);
231 COPY(gbr
); COPY(mach
);
232 COPY(macl
); COPY(pr
);
237 err
|= save_sigcontext_fpu(sc
, regs
);
240 /* non-iBCS2 extensions.. */
241 err
|= __put_user(mask
, &sc
->oldmask
);
247 * Determine which stack to use..
249 static inline void __user
*
250 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
252 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
253 if (sas_ss_flags(sp
) == 0)
254 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
257 return (void __user
*)((sp
- (frame_size
+UNWINDGUARD
)) & -8ul);
260 /* These symbols are defined with the addresses in the vsyscall page.
261 See vsyscall-trapa.S. */
262 extern void __kernel_sigreturn(void);
263 extern void __kernel_rt_sigreturn(void);
265 static int setup_frame(int sig
, struct k_sigaction
*ka
,
266 sigset_t
*set
, struct pt_regs
*regs
)
268 struct sigframe __user
*frame
;
272 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
274 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
277 signal
= current_thread_info()->exec_domain
278 && current_thread_info()->exec_domain
->signal_invmap
280 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
283 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
286 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
287 sizeof(frame
->extramask
));
289 /* Set up to return from userspace. If provided, use a stub
290 already in userspace. */
291 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
292 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
293 #ifdef CONFIG_VSYSCALL
294 } else if (likely(current
->mm
->context
.vdso
)) {
295 regs
->pr
= VDSO_SYM(&__kernel_sigreturn
);
298 /* Generate return code (system call to sigreturn) */
299 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
300 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
301 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
302 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
303 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
304 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
305 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
306 err
|= __put_user((__NR_sigreturn
), &frame
->retcode
[7]);
307 regs
->pr
= (unsigned long) frame
->retcode
;
308 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
314 /* Set up registers for signal handler */
315 regs
->regs
[15] = (unsigned long) frame
;
316 regs
->regs
[4] = signal
; /* Arg for signal handler */
318 regs
->regs
[6] = (unsigned long) &frame
->sc
;
320 if (current
->personality
& FDPIC_FUNCPTRS
) {
321 struct fdpic_func_descriptor __user
*funcptr
=
322 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
324 err
|= __get_user(regs
->pc
, &funcptr
->text
);
325 err
|= __get_user(regs
->regs
[12], &funcptr
->GOT
);
327 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
334 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
335 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
340 force_sigsegv(sig
, current
);
344 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
345 sigset_t
*set
, struct pt_regs
*regs
)
347 struct rt_sigframe __user
*frame
;
351 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
353 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
356 signal
= current_thread_info()->exec_domain
357 && current_thread_info()->exec_domain
->signal_invmap
359 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
362 err
|= copy_siginfo_to_user(&frame
->info
, info
);
364 /* Create the ucontext. */
365 err
|= __put_user(0, &frame
->uc
.uc_flags
);
366 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
367 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->regs
[15]);
368 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
370 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
372 /* Set up to return from userspace. If provided, use a stub
373 already in userspace. */
374 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
375 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
376 #ifdef CONFIG_VSYSCALL
377 } else if (likely(current
->mm
->context
.vdso
)) {
378 regs
->pr
= VDSO_SYM(&__kernel_rt_sigreturn
);
381 /* Generate return code (system call to rt_sigreturn) */
382 err
|= __put_user(MOVW(7), &frame
->retcode
[0]);
383 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
384 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[2]);
385 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[3]);
386 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[4]);
387 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[5]);
388 err
|= __put_user(OR_R0_R0
, &frame
->retcode
[6]);
389 err
|= __put_user((__NR_rt_sigreturn
), &frame
->retcode
[7]);
390 regs
->pr
= (unsigned long) frame
->retcode
;
391 flush_icache_range(regs
->pr
, regs
->pr
+ sizeof(frame
->retcode
));
397 /* Set up registers for signal handler */
398 regs
->regs
[15] = (unsigned long) frame
;
399 regs
->regs
[4] = signal
; /* Arg for signal handler */
400 regs
->regs
[5] = (unsigned long) &frame
->info
;
401 regs
->regs
[6] = (unsigned long) &frame
->uc
;
403 if (current
->personality
& FDPIC_FUNCPTRS
) {
404 struct fdpic_func_descriptor __user
*funcptr
=
405 (struct fdpic_func_descriptor __user
*)ka
->sa
.sa_handler
;
407 err
|= __get_user(regs
->pc
, &funcptr
->text
);
408 err
|= __get_user(regs
->regs
[12], &funcptr
->GOT
);
410 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
;
417 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
418 current
->comm
, task_pid_nr(current
), frame
, regs
->pc
, regs
->pr
);
423 force_sigsegv(sig
, current
);
428 handle_syscall_restart(unsigned long save_r0
, struct pt_regs
*regs
,
429 struct sigaction
*sa
)
431 /* If we're not from a syscall, bail out */
435 /* check for system call restart.. */
436 switch (regs
->regs
[0]) {
437 case -ERESTART_RESTARTBLOCK
:
438 case -ERESTARTNOHAND
:
439 no_system_call_restart
:
440 regs
->regs
[0] = -EINTR
;
444 if (!(sa
->sa_flags
& SA_RESTART
))
445 goto no_system_call_restart
;
447 case -ERESTARTNOINTR
:
448 regs
->regs
[0] = save_r0
;
449 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
455 * OK, we're invoking a handler
458 handle_signal(unsigned long sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
459 struct pt_regs
*regs
, unsigned int save_r0
)
461 sigset_t
*oldset
= sigmask_to_save();
464 /* Set up the stack frame */
465 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
466 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
468 ret
= setup_frame(sig
, ka
, oldset
, regs
);
472 signal_delivered(sig
, info
, ka
, regs
,
473 test_thread_flag(TIF_SINGLESTEP
));
477 * Note that 'init' is a special process: it doesn't get signals it doesn't
478 * want to handle. Thus you cannot kill init even with a SIGKILL even by
481 * Note that we go through the signals twice: once to check the signals that
482 * the kernel can handle, and then we build all the user-level signal handling
483 * stack-frames in one go after that.
485 static void do_signal(struct pt_regs
*regs
, unsigned int save_r0
)
489 struct k_sigaction ka
;
492 * We want the common case to go fast, which
493 * is why we may in certain cases get here from
494 * kernel mode. Just return without doing anything
497 if (!user_mode(regs
))
500 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
502 handle_syscall_restart(save_r0
, regs
, &ka
.sa
);
504 /* Whee! Actually deliver the signal. */
505 handle_signal(signr
, &ka
, &info
, regs
, save_r0
);
509 /* Did we come from a system call? */
510 if (regs
->tra
>= 0) {
511 /* Restart the system call - no handlers present */
512 if (regs
->regs
[0] == -ERESTARTNOHAND
||
513 regs
->regs
[0] == -ERESTARTSYS
||
514 regs
->regs
[0] == -ERESTARTNOINTR
) {
515 regs
->regs
[0] = save_r0
;
516 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
517 } else if (regs
->regs
[0] == -ERESTART_RESTARTBLOCK
) {
518 regs
->pc
-= instruction_size(__raw_readw(regs
->pc
- 4));
519 regs
->regs
[3] = __NR_restart_syscall
;
524 * If there's no signal to deliver, we just put the saved sigmask
527 restore_saved_sigmask();
530 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, unsigned int save_r0
,
531 unsigned long thread_info_flags
)
533 /* deal with pending signal delivery */
534 if (thread_info_flags
& _TIF_SIGPENDING
)
535 do_signal(regs
, save_r0
);
537 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
538 clear_thread_flag(TIF_NOTIFY_RESUME
);
539 tracehook_notify_resume(regs
);