blob: 08bce11badc6a83f643302fa512c9ab11f67b7c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/sh/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 *
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
9 *
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010012#include <linux/sched/task_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
14#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/signal.h>
17#include <linux/errno.h>
18#include <linux/wait.h>
19#include <linux/ptrace.h>
20#include <linux/unistd.h>
21#include <linux/stddef.h>
22#include <linux/tty.h>
Paul Mundt19f9a342006-09-27 18:33:49 +090023#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/binfmts.h>
Paul Mundtb66d51c2007-06-01 17:26:13 +090026#include <linux/io.h>
Paul Mundtab99c732008-07-30 19:55:30 +090027#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/ucontext.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/pgtable.h>
31#include <asm/cacheflush.h>
Paul Mundtfa439722008-09-04 18:53:58 +090032#include <asm/syscalls.h>
Paul Mundt9bbafce2008-03-26 19:02:47 +090033#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Paul Mundt3bc24a12008-05-19 13:40:12 +090035struct fdpic_func_descriptor {
36 unsigned long text;
37 unsigned long GOT;
38};
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Carl Shaw2fc742f2009-08-24 15:07:08 +090041 * The following define adds a 64 byte gap between the signal
42 * stack frame and previous contents of the stack. This allows
43 * frame unwinding in a function epilogue but only if a frame
44 * pointer is used in the function. This is necessary because
45 * current gcc compilers (<4.3) do not generate unwind info on
46 * SH for function epilogues.
47 */
48#define UNWINDGUARD 64
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * Do a signal return; undo the signal stack.
52 */
53
54#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
Yoshinori Sato11cbb702006-12-07 18:07:27 +090055#if defined(CONFIG_CPU_SH2)
Yoshinori Sato9d4436a2006-11-05 15:40:13 +090056#define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
57#else
58#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
59#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
61
62struct sigframe
63{
64 struct sigcontext sc;
65 unsigned long extramask[_NSIG_WORDS-1];
66 u16 retcode[8];
67};
68
69struct rt_sigframe
70{
71 struct siginfo info;
72 struct ucontext uc;
73 u16 retcode[8];
74};
75
76#ifdef CONFIG_SH_FPU
77static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
78{
79 struct task_struct *tsk = current;
80
Paul Mundteaa47702009-10-14 14:12:41 +090081 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83
84 set_used_math();
Paul Mundt0ea820c2010-01-13 12:51:40 +090085 return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 sizeof(long)*(16*2+2));
87}
88
89static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
90 struct pt_regs *regs)
91{
92 struct task_struct *tsk = current;
93
Paul Mundteaa47702009-10-14 14:12:41 +090094 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 0;
96
Al Viroa46808e2012-04-22 16:59:56 -040097 if (!used_math())
98 return __put_user(0, &sc->sc_ownedfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Al Viroa46808e2012-04-22 16:59:56 -0400100 if (__put_user(1, &sc->sc_ownedfp))
101 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* This will cause a "finit" to be triggered by the next
104 attempted FPU operation by the 'current' process.
105 */
106 clear_used_math();
107
108 unlazy_fpu(tsk, regs);
Paul Mundt0ea820c2010-01-13 12:51:40 +0900109 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 sizeof(long)*(16*2+2));
111}
112#endif /* CONFIG_SH_FPU */
113
114static int
115restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
116{
117 unsigned int err = 0;
118
119#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
120 COPY(regs[1]);
121 COPY(regs[2]); COPY(regs[3]);
122 COPY(regs[4]); COPY(regs[5]);
123 COPY(regs[6]); COPY(regs[7]);
124 COPY(regs[8]); COPY(regs[9]);
125 COPY(regs[10]); COPY(regs[11]);
126 COPY(regs[12]); COPY(regs[13]);
127 COPY(regs[14]); COPY(regs[15]);
128 COPY(gbr); COPY(mach);
129 COPY(macl); COPY(pr);
130 COPY(sr); COPY(pc);
131#undef COPY
132
133#ifdef CONFIG_SH_FPU
Paul Mundteaa47702009-10-14 14:12:41 +0900134 if (boot_cpu_data.flags & CPU_HAS_FPU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int owned_fp;
136 struct task_struct *tsk = current;
137
138 regs->sr |= SR_FD; /* Release FPU */
139 clear_fpu(tsk, regs);
140 clear_used_math();
Al Viroa46808e2012-04-22 16:59:56 -0400141 err |= __get_user (owned_fp, &sc->sc_ownedfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (owned_fp)
143 err |= restore_sigcontext_fpu(sc);
144 }
145#endif
146
147 regs->tra = -1; /* disable syscall checks */
148 err |= __get_user(*r0_p, &sc->sc_regs[0]);
149 return err;
150}
151
Bobby Binghamabafe5d2014-04-03 14:46:39 -0700152asmlinkage int sys_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Bobby Binghamabafe5d2014-04-03 14:46:39 -0700154 struct pt_regs *regs = current_pt_regs();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900155 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sigset_t set;
157 int r0;
158
Paul Mundt1bec1572008-09-24 14:37:35 +0900159 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800160 current->restart_block.fn = do_no_restart_syscall;
Paul Mundt1bec1572008-09-24 14:37:35 +0900161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
163 goto badframe;
164
165 if (__get_user(set.sig[0], &frame->sc.oldmask)
166 || (_NSIG_WORDS > 1
167 && __copy_from_user(&set.sig[1], &frame->extramask,
168 sizeof(frame->extramask))))
169 goto badframe;
170
Matt Fleming5e047fa2012-03-28 15:16:32 -0700171 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900173 if (restore_sigcontext(regs, &frame->sc, &r0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto badframe;
175 return r0;
176
177badframe:
178 force_sig(SIGSEGV, current);
179 return 0;
180}
181
Bobby Binghamabafe5d2014-04-03 14:46:39 -0700182asmlinkage int sys_rt_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Bobby Binghamabafe5d2014-04-03 14:46:39 -0700184 struct pt_regs *regs = current_pt_regs();
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900185 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int r0;
188
Paul Mundt1bec1572008-09-24 14:37:35 +0900189 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800190 current->restart_block.fn = do_no_restart_syscall;
Paul Mundt1bec1572008-09-24 14:37:35 +0900191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
193 goto badframe;
194
195 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
196 goto badframe;
197
Matt Fleming5e047fa2012-03-28 15:16:32 -0700198 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Stuart Menefyf0bc8142006-11-21 11:16:57 +0900200 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto badframe;
202
Al Viro7a879a92012-12-23 03:46:05 -0500203 if (restore_altstack(&frame->uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 return r0;
207
208badframe:
209 force_sig(SIGSEGV, current);
210 return 0;
Paul Mundt3aeb8842007-06-19 12:33:21 +0900211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/*
214 * Set up a signal frame.
215 */
216
217static int
218setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
219 unsigned long mask)
220{
221 int err = 0;
222
223#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
224 COPY(regs[0]); COPY(regs[1]);
225 COPY(regs[2]); COPY(regs[3]);
226 COPY(regs[4]); COPY(regs[5]);
227 COPY(regs[6]); COPY(regs[7]);
228 COPY(regs[8]); COPY(regs[9]);
229 COPY(regs[10]); COPY(regs[11]);
230 COPY(regs[12]); COPY(regs[13]);
231 COPY(regs[14]); COPY(regs[15]);
232 COPY(gbr); COPY(mach);
233 COPY(macl); COPY(pr);
234 COPY(sr); COPY(pc);
235#undef COPY
236
237#ifdef CONFIG_SH_FPU
238 err |= save_sigcontext_fpu(sc, regs);
239#endif
240
241 /* non-iBCS2 extensions.. */
242 err |= __put_user(mask, &sc->oldmask);
243
244 return err;
245}
246
247/*
248 * Determine which stack to use..
249 */
250static inline void __user *
251get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
252{
253 if (ka->sa.sa_flags & SA_ONSTACK) {
254 if (sas_ss_flags(sp) == 0)
255 sp = current->sas_ss_sp + current->sas_ss_size;
256 }
257
Carl Shaw2fc742f2009-08-24 15:07:08 +0900258 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Paul Mundt19f9a342006-09-27 18:33:49 +0900261/* These symbols are defined with the addresses in the vsyscall page.
262 See vsyscall-trapa.S. */
Matt Fleming94455712009-06-18 10:03:33 +0100263extern void __kernel_sigreturn(void);
264extern void __kernel_rt_sigreturn(void);
Paul Mundt19f9a342006-09-27 18:33:49 +0900265
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200266static int setup_frame(struct ksignal *ksig, sigset_t *set,
267 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct sigframe __user *frame;
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200270 int err = 0, sig = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200272 frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200275 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
278
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900279 if (_NSIG_WORDS > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 err |= __copy_to_user(frame->extramask, &set->sig[1],
281 sizeof(frame->extramask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* Set up to return from userspace. If provided, use a stub
284 already in userspace. */
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200285 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
286 regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
Paul Mundt19f9a342006-09-27 18:33:49 +0900287#ifdef CONFIG_VSYSCALL
288 } else if (likely(current->mm->context.vdso)) {
289 regs->pr = VDSO_SYM(&__kernel_sigreturn);
290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 } else {
292 /* Generate return code (system call to sigreturn) */
293 err |= __put_user(MOVW(7), &frame->retcode[0]);
Yoshinori Sato9d4436a2006-11-05 15:40:13 +0900294 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
296 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
297 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
298 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
299 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
300 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
301 regs->pr = (unsigned long) frame->retcode;
Stuart Menefyf2fb4e42008-07-02 17:51:23 +0900302 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305 if (err)
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200306 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* Set up registers for signal handler */
309 regs->regs[15] = (unsigned long) frame;
Richard Weinbergerdaea906d2014-07-13 17:37:45 +0200310 regs->regs[4] = sig; /* Arg for signal handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 regs->regs[5] = 0;
312 regs->regs[6] = (unsigned long) &frame->sc;
Paul Mundt3bc24a12008-05-19 13:40:12 +0900313
314 if (current->personality & FDPIC_FUNCPTRS) {
315 struct fdpic_func_descriptor __user *funcptr =
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200316 (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
Paul Mundt3bc24a12008-05-19 13:40:12 +0900317
Al Viroa46808e2012-04-22 16:59:56 -0400318 err |= __get_user(regs->pc, &funcptr->text);
319 err |= __get_user(regs->regs[12], &funcptr->GOT);
Paul Mundt3bc24a12008-05-19 13:40:12 +0900320 } else
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200321 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Al Viroa46808e2012-04-22 16:59:56 -0400323 if (err)
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200324 return -EFAULT;
Al Viroa46808e2012-04-22 16:59:56 -0400325
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900326 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700327 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200332static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
333 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct rt_sigframe __user *frame;
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200336 int err = 0, sig = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200338 frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200341 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200343 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Create the ucontext. */
346 err |= __put_user(0, &frame->uc.uc_flags);
Paul Mundtfa439722008-09-04 18:53:58 +0900347 err |= __put_user(NULL, &frame->uc.uc_link);
Al Viro7a879a92012-12-23 03:46:05 -0500348 err |= __save_altstack(&frame->uc.uc_stack, regs->regs[15]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 err |= setup_sigcontext(&frame->uc.uc_mcontext,
350 regs, set->sig[0]);
351 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
352
353 /* Set up to return from userspace. If provided, use a stub
354 already in userspace. */
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200355 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
356 regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
Paul Mundt19f9a342006-09-27 18:33:49 +0900357#ifdef CONFIG_VSYSCALL
358 } else if (likely(current->mm->context.vdso)) {
359 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
360#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 } else {
362 /* Generate return code (system call to rt_sigreturn) */
363 err |= __put_user(MOVW(7), &frame->retcode[0]);
Yoshinori Sato9d4436a2006-11-05 15:40:13 +0900364 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
366 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
367 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
368 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
369 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
370 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
371 regs->pr = (unsigned long) frame->retcode;
Paul Mundt4d2947f2009-10-14 14:21:40 +0900372 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 if (err)
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200376 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* Set up registers for signal handler */
379 regs->regs[15] = (unsigned long) frame;
Richard Weinbergerdaea906d2014-07-13 17:37:45 +0200380 regs->regs[4] = sig; /* Arg for signal handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 regs->regs[5] = (unsigned long) &frame->info;
382 regs->regs[6] = (unsigned long) &frame->uc;
Paul Mundt3bc24a12008-05-19 13:40:12 +0900383
384 if (current->personality & FDPIC_FUNCPTRS) {
385 struct fdpic_func_descriptor __user *funcptr =
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200386 (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
Paul Mundt3bc24a12008-05-19 13:40:12 +0900387
Al Viroa46808e2012-04-22 16:59:56 -0400388 err |= __get_user(regs->pc, &funcptr->text);
389 err |= __get_user(regs->regs[12], &funcptr->GOT);
Paul Mundt3bc24a12008-05-19 13:40:12 +0900390 } else
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200391 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Al Viroa46808e2012-04-22 16:59:56 -0400393 if (err)
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200394 return -EFAULT;
Al Viroa46808e2012-04-22 16:59:56 -0400395
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900396 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700397 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Paul Mundtf8b890a2008-09-12 22:08:20 +0900402static inline void
403handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
404 struct sigaction *sa)
405{
406 /* If we're not from a syscall, bail out */
407 if (regs->tra < 0)
408 return;
409
410 /* check for system call restart.. */
411 switch (regs->regs[0]) {
412 case -ERESTART_RESTARTBLOCK:
413 case -ERESTARTNOHAND:
414 no_system_call_restart:
415 regs->regs[0] = -EINTR;
Paul Mundtf8b890a2008-09-12 22:08:20 +0900416 break;
417
418 case -ERESTARTSYS:
419 if (!(sa->sa_flags & SA_RESTART))
420 goto no_system_call_restart;
421 /* fallthrough */
422 case -ERESTARTNOINTR:
423 regs->regs[0] = save_r0;
Paul Mundt9d56dd32010-01-26 12:58:40 +0900424 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
Paul Mundtf8b890a2008-09-12 22:08:20 +0900425 break;
426 }
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/*
430 * OK, we're invoking a handler
431 */
Al Viroa610d6e2012-05-21 23:42:15 -0400432static void
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200433handle_signal(struct ksignal *ksig, struct pt_regs *regs, unsigned int save_r0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Al Virob7f9a112012-05-02 09:59:21 -0400435 sigset_t *oldset = sigmask_to_save();
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900436 int ret;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* Set up the stack frame */
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200439 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
440 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 else
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200442 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200444 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/*
448 * Note that 'init' is a special process: it doesn't get signals it doesn't
449 * want to handle. Thus you cannot kill init even with a SIGKILL even by
450 * mistake.
451 *
452 * Note that we go through the signals twice: once to check the signals that
453 * the kernel can handle, and then we build all the user-level signal handling
454 * stack-frames in one go after that.
455 */
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900456static void do_signal(struct pt_regs *regs, unsigned int save_r0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200458 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * We want the common case to go fast, which
462 * is why we may in certain cases get here from
463 * kernel mode. Just return without doing anything
464 * if so.
465 */
466 if (!user_mode(regs))
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200469 if (get_signal(&ksig)) {
470 handle_syscall_restart(save_r0, regs, &ksig.ka.sa);
Paul Mundtf8b890a2008-09-12 22:08:20 +0900471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* Whee! Actually deliver the signal. */
Richard Weinbergerb46e8482013-10-07 14:51:14 +0200473 handle_signal(&ksig, regs, save_r0);
Ryusuke Sakatoc8bfa1f2007-02-23 13:22:56 +0900474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* Did we come from a system call? */
478 if (regs->tra >= 0) {
479 /* Restart the system call - no handlers present */
480 if (regs->regs[0] == -ERESTARTNOHAND ||
481 regs->regs[0] == -ERESTARTSYS ||
Paul Mundt0b892932006-09-27 17:22:49 +0900482 regs->regs[0] == -ERESTARTNOINTR) {
Ryusuke Sakatoc8bfa1f2007-02-23 13:22:56 +0900483 regs->regs[0] = save_r0;
Paul Mundt9d56dd32010-01-26 12:58:40 +0900484 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
Paul Mundt0b892932006-09-27 17:22:49 +0900485 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
Paul Mundt9d56dd32010-01-26 12:58:40 +0900486 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
Paul Mundt0b892932006-09-27 17:22:49 +0900487 regs->regs[3] = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 }
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900490
Paul Mundt56bfc422009-10-14 16:05:42 +0900491 /*
492 * If there's no signal to deliver, we just put the saved sigmask
493 * back.
494 */
Al Viro51a7b442012-05-21 23:33:55 -0400495 restore_saved_sigmask();
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900496}
497
498asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
Paul Mundtab99c732008-07-30 19:55:30 +0900499 unsigned long thread_info_flags)
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900500{
501 /* deal with pending signal delivery */
Paul Mundtab99c732008-07-30 19:55:30 +0900502 if (thread_info_flags & _TIF_SIGPENDING)
Paul Mundt9f23e7e2006-09-27 17:27:00 +0900503 do_signal(regs, save_r0);
Paul Mundtab99c732008-07-30 19:55:30 +0900504
505 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
506 clear_thread_flag(TIF_NOTIFY_RESUME);
507 tracehook_notify_resume(regs);
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}