blob: 2ad7c4587669a2c455fd8078aa884e7ad42850bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m32r/kernel/signal.c
3 *
4 * Copyright (c) 2003 Hitoshi Yamamoto
5 *
6 * Taken from i386 version.
7 * Copyright (C) 1991, 1992 Linus Torvalds
8 *
9 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
10 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/sched.h>
14#include <linux/mm.h>
15#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/signal.h>
18#include <linux/errno.h>
19#include <linux/wait.h>
20#include <linux/unistd.h>
21#include <linux/stddef.h>
22#include <linux/personality.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
David Howells733e5e42009-09-09 08:30:21 +010024#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/cacheflush.h>
26#include <asm/ucontext.h>
27#include <asm/uaccess.h>
28
29#define DEBUG_SIG 0
30
Kyle McMartin99d67342010-10-15 21:16:45 -040031#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033asmlinkage int
34sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
35 unsigned long r2, unsigned long r3, unsigned long r4,
Hirokazu Takata6ced13c2006-02-24 13:03:51 -080036 unsigned long r5, unsigned long r6, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Hirokazu Takata6ced13c2006-02-24 13:03:51 -080038 return do_sigaltstack(uss, uoss, regs->spu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41
42/*
43 * Do a signal return; undo the signal stack.
44 */
45
46struct rt_sigframe
47{
48 int sig;
Al Viro870e75a2006-10-11 17:24:45 +010049 struct siginfo __user *pinfo;
50 void __user *puc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct siginfo info;
52 struct ucontext uc;
53// struct _fpstate fpstate;
54};
55
56static int
57restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
58 int *r0_p)
59{
60 unsigned int err = 0;
61
62 /* Always make any pending restarted system calls return -EINTR */
63 current_thread_info()->restart_block.fn = do_no_restart_syscall;
64
65#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
66 COPY(r4);
67 COPY(r5);
68 COPY(r6);
69 COPY(pt_regs);
70 /* COPY(r0); Skip r0 */
71 COPY(r1);
72 COPY(r2);
73 COPY(r3);
74 COPY(r7);
75 COPY(r8);
76 COPY(r9);
77 COPY(r10);
78 COPY(r11);
79 COPY(r12);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 COPY(acc0h);
81 COPY(acc0l);
Hirokazu Takata9674dcf2007-02-10 01:43:35 -080082 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
83 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 COPY(psw);
85 COPY(bpc);
86 COPY(bbpsw);
87 COPY(bbpc);
88 COPY(spu);
89 COPY(fp);
90 COPY(lr);
91 COPY(spi);
92#undef COPY
93
94 regs->syscall_nr = -1; /* disable syscall checks */
95 err |= __get_user(*r0_p, &sc->sc_r0);
96
97 return err;
98}
99
100asmlinkage int
101sys_rt_sigreturn(unsigned long r0, unsigned long r1,
102 unsigned long r2, unsigned long r3, unsigned long r4,
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800103 unsigned long r5, unsigned long r6, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800105 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int result;
108
109 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
110 goto badframe;
111 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
112 goto badframe;
113
114 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Fleming3883e302012-05-11 10:57:59 +1000115 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800117 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 goto badframe;
119
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800120 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return result;
124
125badframe:
126 force_sig(SIGSEGV, current);
127 return 0;
128}
129
130/*
131 * Set up a signal frame.
132 */
133
134static int
135setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
136 unsigned long mask)
137{
138 int err = 0;
139
140#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
141 COPY(r4);
142 COPY(r5);
143 COPY(r6);
144 COPY(pt_regs);
145 COPY(r0);
146 COPY(r1);
147 COPY(r2);
148 COPY(r3);
149 COPY(r7);
150 COPY(r8);
151 COPY(r9);
152 COPY(r10);
153 COPY(r11);
154 COPY(r12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 COPY(acc0h);
156 COPY(acc0l);
Hirokazu Takata9674dcf2007-02-10 01:43:35 -0800157 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
158 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 COPY(psw);
160 COPY(bpc);
161 COPY(bbpsw);
162 COPY(bbpc);
163 COPY(spu);
164 COPY(fp);
165 COPY(lr);
166 COPY(spi);
167#undef COPY
168
169 err |= __put_user(mask, &sc->oldmask);
170
171 return err;
172}
173
174/*
175 * Determine which stack to use..
176 */
177static inline void __user *
178get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
179{
180 /* This is the X/Open sanctioned signal stack switching. */
181 if (ka->sa.sa_flags & SA_ONSTACK) {
182 if (sas_ss_flags(sp) == 0)
183 sp = current->sas_ss_sp + current->sas_ss_size;
184 }
185
186 return (void __user *)((sp - frame_size) & -8ul);
187}
188
Al Viroa05c4e12010-09-24 06:23:57 +0100189static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 sigset_t *set, struct pt_regs *regs)
191{
192 struct rt_sigframe __user *frame;
193 int err = 0;
194 int signal;
195
196 frame = get_sigframe(ka, regs->spu, sizeof(*frame));
197
198 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
199 goto give_sigsegv;
200
201 signal = current_thread_info()->exec_domain
202 && current_thread_info()->exec_domain->signal_invmap
203 && sig < 32
204 ? current_thread_info()->exec_domain->signal_invmap[sig]
205 : sig;
206
207 err |= __put_user(signal, &frame->sig);
208 if (err)
209 goto give_sigsegv;
210
211 err |= __put_user(&frame->info, &frame->pinfo);
212 err |= __put_user(&frame->uc, &frame->puc);
213 err |= copy_siginfo_to_user(&frame->info, info);
214 if (err)
215 goto give_sigsegv;
216
217 /* Create the ucontext. */
218 err |= __put_user(0, &frame->uc.uc_flags);
219 err |= __put_user(0, &frame->uc.uc_link);
220 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
221 err |= __put_user(sas_ss_flags(regs->spu),
222 &frame->uc.uc_stack.ss_flags);
223 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
224 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
225 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
226 if (err)
227 goto give_sigsegv;
228
229 /* Set up to return from userspace. */
230 regs->lr = (unsigned long)ka->sa.sa_restorer;
231
232 /* Set up registers for signal handler */
233 regs->spu = (unsigned long)frame;
234 regs->r0 = signal; /* Arg for signal handler */
235 regs->r1 = (unsigned long)&frame->info;
236 regs->r2 = (unsigned long)&frame->uc;
237 regs->bpc = (unsigned long)ka->sa.sa_handler;
238
239 set_fs(USER_DS);
240
241#if DEBUG_SIG
242 printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
243 current->comm, current->pid, frame, regs->pc);
244#endif
245
Al Viroa05c4e12010-09-24 06:23:57 +0100246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248give_sigsegv:
249 force_sigsegv(sig, current);
Al Viroa05c4e12010-09-24 06:23:57 +0100250 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Al Virobb9c8612010-09-24 06:24:53 +0100253static int prev_insn(struct pt_regs *regs)
254{
255 u16 inst;
Kyle McMartin388d1482010-10-15 21:17:09 -0400256 if (get_user(inst, (u16 __user *)(regs->bpc - 2)))
Al Virobb9c8612010-09-24 06:24:53 +0100257 return -EFAULT;
258 if ((inst & 0xfff0) == 0x10f0) /* trap ? */
259 regs->bpc -= 2;
260 else
261 regs->bpc -= 4;
262 regs->syscall_nr = -1;
263 return 0;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
267 * OK, we're invoking a handler
268 */
269
Al Viroa05c4e12010-09-24 06:23:57 +0100270static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
272 sigset_t *oldset, struct pt_regs *regs)
273{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Are we from a system call? */
275 if (regs->syscall_nr >= 0) {
276 /* If so, check system call restarting.. */
277 switch (regs->r0) {
278 case -ERESTART_RESTARTBLOCK:
279 case -ERESTARTNOHAND:
280 regs->r0 = -EINTR;
281 break;
282
283 case -ERESTARTSYS:
284 if (!(ka->sa.sa_flags & SA_RESTART)) {
285 regs->r0 = -EINTR;
286 break;
287 }
288 /* fallthrough */
289 case -ERESTARTNOINTR:
290 regs->r0 = regs->orig_r0;
Al Virobb9c8612010-09-24 06:24:53 +0100291 if (prev_insn(regs) < 0)
292 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 }
295
296 /* Set up the stack frame */
Al Viroa05c4e12010-09-24 06:23:57 +0100297 if (setup_rt_frame(sig, ka, info, oldset, regs))
298 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Matt Fleming3883e302012-05-11 10:57:59 +1000300 block_sigmask(ka, sig);
Al Viroa05c4e12010-09-24 06:23:57 +0100301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304/*
305 * Note that 'init' is a special process: it doesn't get signals it doesn't
306 * want to handle. Thus you cannot kill init even with a SIGKILL even by
307 * mistake.
308 */
Al Viroa05c4e12010-09-24 06:23:57 +0100309static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 siginfo_t info;
312 int signr;
313 struct k_sigaction ka;
Al Viroa7f83882010-09-24 06:20:35 +0100314 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /*
317 * We want the common case to go fast, which
318 * is why we may in certain cases get here from
319 * kernel mode. Just return without doing anything
320 * if so.
321 */
322 if (!user_mode(regs))
Al Viroa05c4e12010-09-24 06:23:57 +0100323 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700325 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto no_signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Al Viroa7f83882010-09-24 06:20:35 +0100328 if (test_thread_flag(TIF_RESTORE_SIGMASK))
329 oldset = &current->saved_sigmask;
330 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 oldset = &current->blocked;
332
333 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
334 if (signr > 0) {
Simon Arlott5aa8b6c2007-10-20 01:14:39 +0200335 /* Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * signal to user space. The processor register will
337 * have been cleared if the watchpoint triggered
338 * inside the kernel.
339 */
340
341 /* Whee! Actually deliver the signal. */
Al Viroa05c4e12010-09-24 06:23:57 +0100342 if (handle_signal(signr, &ka, &info, oldset, regs) == 0)
343 clear_thread_flag(TIF_RESTORE_SIGMASK);
344
345 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 no_signal:
349 /* Did we come from a system call? */
350 if (regs->syscall_nr >= 0) {
351 /* Restart the system call - no handlers present */
352 if (regs->r0 == -ERESTARTNOHAND ||
353 regs->r0 == -ERESTARTSYS ||
354 regs->r0 == -ERESTARTNOINTR) {
355 regs->r0 = regs->orig_r0;
Al Virobb9c8612010-09-24 06:24:53 +0100356 prev_insn(regs);
Al Viroa7481022010-09-24 06:22:30 +0100357 } else if (regs->r0 == -ERESTART_RESTARTBLOCK){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 regs->r0 = regs->orig_r0;
359 regs->r7 = __NR_restart_syscall;
Al Virobb9c8612010-09-24 06:24:53 +0100360 prev_insn(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 }
Al Viro51a7b442012-05-21 23:33:55 -0400363 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/*
367 * notification of userspace execution resumption
368 * - triggered by current->work.notify_resume
369 */
Al Viroa7f83882010-09-24 06:20:35 +0100370void do_notify_resume(struct pt_regs *regs, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 /* Pending single-step? */
373 if (thread_info_flags & _TIF_SINGLESTEP)
374 clear_thread_flag(TIF_SINGLESTEP);
375
376 /* deal with pending signal delivery */
377 if (thread_info_flags & _TIF_SIGPENDING)
Al Viroa7f83882010-09-24 06:20:35 +0100378 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Howellsd0420c82009-09-02 09:14:16 +0100380 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
381 clear_thread_flag(TIF_NOTIFY_RESUME);
382 tracehook_notify_resume(regs);
383 }
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 clear_thread_flag(TIF_IRET);
386}