1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/sh/kernel/signal.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
9 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
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/elf.h>
25 #include <linux/personality.h>
26 #include <linux/binfmts.h>
28 #include <linux/tracehook.h>
29 #include <asm/ucontext.h>
30 #include <linux/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/cacheflush.h>
33 #include <asm/syscalls.h>
36 struct fdpic_func_descriptor {
42 * The following define adds a 64 byte gap between the signal
43 * stack frame and previous contents of the stack. This allows
44 * frame unwinding in a function epilogue but only if a frame
45 * pointer is used in the function. This is necessary because
46 * current gcc compilers (<4.3) do not generate unwind info on
47 * SH for function epilogues.
49 #define UNWINDGUARD 64
52 * Do a signal return; undo the signal stack.
55 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
56 #if defined(CONFIG_CPU_SH2)
57 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
59 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
61 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
66 unsigned long extramask[_NSIG_WORDS-1];
78 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
80 struct task_struct *tsk = current;
82 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
86 return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
87 sizeof(long)*(16*2+2));
90 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
93 struct task_struct *tsk = current;
95 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
99 return __put_user(0, &sc->sc_ownedfp);
101 if (__put_user(1, &sc->sc_ownedfp))
104 /* This will cause a "finit" to be triggered by the next
105 attempted FPU operation by the 'current' process.
109 unlazy_fpu(tsk, regs);
110 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
111 sizeof(long)*(16*2+2));
113 #endif /* CONFIG_SH_FPU */
116 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
118 unsigned int err = 0;
119 unsigned int sr = regs->sr & ~SR_USER_MASK;
121 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
123 COPY(regs[2]); COPY(regs[3]);
124 COPY(regs[4]); COPY(regs[5]);
125 COPY(regs[6]); COPY(regs[7]);
126 COPY(regs[8]); COPY(regs[9]);
127 COPY(regs[10]); COPY(regs[11]);
128 COPY(regs[12]); COPY(regs[13]);
129 COPY(regs[14]); COPY(regs[15]);
130 COPY(gbr); COPY(mach);
131 COPY(macl); COPY(pr);
135 regs->sr = (regs->sr & SR_USER_MASK) | sr;
138 if (boot_cpu_data.flags & CPU_HAS_FPU) {
140 struct task_struct *tsk = current;
142 regs->sr |= SR_FD; /* Release FPU */
143 clear_fpu(tsk, regs);
145 err |= __get_user (owned_fp, &sc->sc_ownedfp);
147 err |= restore_sigcontext_fpu(sc);
151 regs->tra = -1; /* disable syscall checks */
152 err |= __get_user(*r0_p, &sc->sc_regs[0]);
156 asmlinkage int sys_sigreturn(void)
158 struct pt_regs *regs = current_pt_regs();
159 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
163 /* Always make any pending restarted system calls return -EINTR */
164 current->restart_block.fn = do_no_restart_syscall;
166 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
169 if (__get_user(set.sig[0], &frame->sc.oldmask)
171 && __copy_from_user(&set.sig[1], &frame->extramask,
172 sizeof(frame->extramask))))
175 set_current_blocked(&set);
177 if (restore_sigcontext(regs, &frame->sc, &r0))
182 force_sig(SIGSEGV, current);
186 asmlinkage int sys_rt_sigreturn(void)
188 struct pt_regs *regs = current_pt_regs();
189 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
193 /* Always make any pending restarted system calls return -EINTR */
194 current->restart_block.fn = do_no_restart_syscall;
196 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
199 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
202 set_current_blocked(&set);
204 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
207 if (restore_altstack(&frame->uc.uc_stack))
213 force_sig(SIGSEGV, current);
218 * Set up a signal frame.
222 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
227 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
228 COPY(regs[0]); COPY(regs[1]);
229 COPY(regs[2]); COPY(regs[3]);
230 COPY(regs[4]); COPY(regs[5]);
231 COPY(regs[6]); COPY(regs[7]);
232 COPY(regs[8]); COPY(regs[9]);
233 COPY(regs[10]); COPY(regs[11]);
234 COPY(regs[12]); COPY(regs[13]);
235 COPY(regs[14]); COPY(regs[15]);
236 COPY(gbr); COPY(mach);
237 COPY(macl); COPY(pr);
242 err |= save_sigcontext_fpu(sc, regs);
245 /* non-iBCS2 extensions.. */
246 err |= __put_user(mask, &sc->oldmask);
252 * Determine which stack to use..
254 static inline void __user *
255 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
257 if (ka->sa.sa_flags & SA_ONSTACK) {
258 if (sas_ss_flags(sp) == 0)
259 sp = current->sas_ss_sp + current->sas_ss_size;
262 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
265 /* These symbols are defined with the addresses in the vsyscall page.
266 See vsyscall-trapa.S. */
267 extern void __kernel_sigreturn(void);
268 extern void __kernel_rt_sigreturn(void);
270 static int setup_frame(struct ksignal *ksig, sigset_t *set,
271 struct pt_regs *regs)
273 struct sigframe __user *frame;
274 int err = 0, sig = ksig->sig;
276 frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
278 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
281 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
284 err |= __copy_to_user(frame->extramask, &set->sig[1],
285 sizeof(frame->extramask));
287 /* Set up to return from userspace. If provided, use a stub
288 already in userspace. */
289 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
290 regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
291 #ifdef CONFIG_VSYSCALL
292 } else if (likely(current->mm->context.vdso)) {
293 regs->pr = VDSO_SYM(&__kernel_sigreturn);
296 /* Generate return code (system call to sigreturn) */
297 err |= __put_user(MOVW(7), &frame->retcode[0]);
298 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
299 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
300 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
301 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
302 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
303 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
304 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
305 regs->pr = (unsigned long) frame->retcode;
306 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
312 /* Set up registers for signal handler */
313 regs->regs[15] = (unsigned long) frame;
314 regs->regs[4] = sig; /* Arg for signal handler */
316 regs->regs[6] = (unsigned long) &frame->sc;
318 if (current->personality & FDPIC_FUNCPTRS) {
319 struct fdpic_func_descriptor __user *funcptr =
320 (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
322 err |= __get_user(regs->pc, &funcptr->text);
323 err |= __get_user(regs->regs[12], &funcptr->GOT);
325 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
330 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
331 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
336 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
337 struct pt_regs *regs)
339 struct rt_sigframe __user *frame;
340 int err = 0, sig = ksig->sig;
342 frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
344 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
347 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
349 /* Create the ucontext. */
350 err |= __put_user(0, &frame->uc.uc_flags);
351 err |= __put_user(NULL, &frame->uc.uc_link);
352 err |= __save_altstack(&frame->uc.uc_stack, regs->regs[15]);
353 err |= setup_sigcontext(&frame->uc.uc_mcontext,
355 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
357 /* Set up to return from userspace. If provided, use a stub
358 already in userspace. */
359 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
360 regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
361 #ifdef CONFIG_VSYSCALL
362 } else if (likely(current->mm->context.vdso)) {
363 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
366 /* Generate return code (system call to rt_sigreturn) */
367 err |= __put_user(MOVW(7), &frame->retcode[0]);
368 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
369 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
370 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
371 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
372 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
373 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
374 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
375 regs->pr = (unsigned long) frame->retcode;
376 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
382 /* Set up registers for signal handler */
383 regs->regs[15] = (unsigned long) frame;
384 regs->regs[4] = sig; /* Arg for signal handler */
385 regs->regs[5] = (unsigned long) &frame->info;
386 regs->regs[6] = (unsigned long) &frame->uc;
388 if (current->personality & FDPIC_FUNCPTRS) {
389 struct fdpic_func_descriptor __user *funcptr =
390 (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
392 err |= __get_user(regs->pc, &funcptr->text);
393 err |= __get_user(regs->regs[12], &funcptr->GOT);
395 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
400 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
401 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
407 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
408 struct sigaction *sa)
410 /* If we're not from a syscall, bail out */
414 /* check for system call restart.. */
415 switch (regs->regs[0]) {
416 case -ERESTART_RESTARTBLOCK:
417 case -ERESTARTNOHAND:
418 no_system_call_restart:
419 regs->regs[0] = -EINTR;
423 if (!(sa->sa_flags & SA_RESTART))
424 goto no_system_call_restart;
426 case -ERESTARTNOINTR:
427 regs->regs[0] = save_r0;
428 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
434 * OK, we're invoking a handler
437 handle_signal(struct ksignal *ksig, struct pt_regs *regs, unsigned int save_r0)
439 sigset_t *oldset = sigmask_to_save();
442 /* Set up the stack frame */
443 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
444 ret = setup_rt_frame(ksig, oldset, regs);
446 ret = setup_frame(ksig, oldset, regs);
448 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
452 * Note that 'init' is a special process: it doesn't get signals it doesn't
453 * want to handle. Thus you cannot kill init even with a SIGKILL even by
456 * Note that we go through the signals twice: once to check the signals that
457 * the kernel can handle, and then we build all the user-level signal handling
458 * stack-frames in one go after that.
460 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
465 * We want the common case to go fast, which
466 * is why we may in certain cases get here from
467 * kernel mode. Just return without doing anything
470 if (!user_mode(regs))
473 if (get_signal(&ksig)) {
474 handle_syscall_restart(save_r0, regs, &ksig.ka.sa);
476 /* Whee! Actually deliver the signal. */
477 handle_signal(&ksig, regs, save_r0);
481 /* Did we come from a system call? */
482 if (regs->tra >= 0) {
483 /* Restart the system call - no handlers present */
484 if (regs->regs[0] == -ERESTARTNOHAND ||
485 regs->regs[0] == -ERESTARTSYS ||
486 regs->regs[0] == -ERESTARTNOINTR) {
487 regs->regs[0] = save_r0;
488 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
489 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
490 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
491 regs->regs[3] = __NR_restart_syscall;
496 * If there's no signal to deliver, we just put the saved sigmask
499 restore_saved_sigmask();
502 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
503 unsigned long thread_info_flags)
505 /* deal with pending signal delivery */
506 if (thread_info_flags & _TIF_SIGPENDING)
507 do_signal(regs, save_r0);
509 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
510 clear_thread_flag(TIF_NOTIFY_RESUME);
511 tracehook_notify_resume(regs);