blob: d0f60b97bbc5d82b1855e73145ce0b809124200f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031asmlinkage int
32sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
33 unsigned long r2, unsigned long r3, unsigned long r4,
Hirokazu Takata6ced13c2006-02-24 13:03:51 -080034 unsigned long r5, unsigned long r6, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Hirokazu Takata6ced13c2006-02-24 13:03:51 -080036 return do_sigaltstack(uss, uoss, regs->spu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39
40/*
41 * Do a signal return; undo the signal stack.
42 */
43
44struct rt_sigframe
45{
46 int sig;
Al Viro870e75a2006-10-11 17:24:45 +010047 struct siginfo __user *pinfo;
48 void __user *puc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct siginfo info;
50 struct ucontext uc;
51// struct _fpstate fpstate;
52};
53
54static int
55restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
56 int *r0_p)
57{
58 unsigned int err = 0;
59
60 /* Always make any pending restarted system calls return -EINTR */
61 current_thread_info()->restart_block.fn = do_no_restart_syscall;
62
63#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
64 COPY(r4);
65 COPY(r5);
66 COPY(r6);
67 COPY(pt_regs);
68 /* COPY(r0); Skip r0 */
69 COPY(r1);
70 COPY(r2);
71 COPY(r3);
72 COPY(r7);
73 COPY(r8);
74 COPY(r9);
75 COPY(r10);
76 COPY(r11);
77 COPY(r12);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 COPY(acc0h);
79 COPY(acc0l);
Hirokazu Takata9674dcf2007-02-10 01:43:35 -080080 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
81 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 COPY(psw);
83 COPY(bpc);
84 COPY(bbpsw);
85 COPY(bbpc);
86 COPY(spu);
87 COPY(fp);
88 COPY(lr);
89 COPY(spi);
90#undef COPY
91
92 regs->syscall_nr = -1; /* disable syscall checks */
93 err |= __get_user(*r0_p, &sc->sc_r0);
94
95 return err;
96}
97
98asmlinkage int
99sys_rt_sigreturn(unsigned long r0, unsigned long r1,
100 unsigned long r2, unsigned long r3, unsigned long r4,
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800101 unsigned long r5, unsigned long r6, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800103 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int result;
106
107 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
108 goto badframe;
109 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
110 goto badframe;
111
Matt Fleming3883e302012-05-11 10:57:59 +1000112 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800114 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 goto badframe;
116
Hirokazu Takata6ced13c2006-02-24 13:03:51 -0800117 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return result;
121
122badframe:
123 force_sig(SIGSEGV, current);
124 return 0;
125}
126
127/*
128 * Set up a signal frame.
129 */
130
131static int
132setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
133 unsigned long mask)
134{
135 int err = 0;
136
137#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
138 COPY(r4);
139 COPY(r5);
140 COPY(r6);
141 COPY(pt_regs);
142 COPY(r0);
143 COPY(r1);
144 COPY(r2);
145 COPY(r3);
146 COPY(r7);
147 COPY(r8);
148 COPY(r9);
149 COPY(r10);
150 COPY(r11);
151 COPY(r12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 COPY(acc0h);
153 COPY(acc0l);
Hirokazu Takata9674dcf2007-02-10 01:43:35 -0800154 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
155 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 COPY(psw);
157 COPY(bpc);
158 COPY(bbpsw);
159 COPY(bbpc);
160 COPY(spu);
161 COPY(fp);
162 COPY(lr);
163 COPY(spi);
164#undef COPY
165
166 err |= __put_user(mask, &sc->oldmask);
167
168 return err;
169}
170
171/*
172 * Determine which stack to use..
173 */
174static inline void __user *
175get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
176{
177 /* This is the X/Open sanctioned signal stack switching. */
178 if (ka->sa.sa_flags & SA_ONSTACK) {
179 if (sas_ss_flags(sp) == 0)
180 sp = current->sas_ss_sp + current->sas_ss_size;
181 }
182
183 return (void __user *)((sp - frame_size) & -8ul);
184}
185
Al Viroa05c4e12010-09-24 06:23:57 +0100186static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 sigset_t *set, struct pt_regs *regs)
188{
189 struct rt_sigframe __user *frame;
190 int err = 0;
191 int signal;
192
193 frame = get_sigframe(ka, regs->spu, sizeof(*frame));
194
195 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
196 goto give_sigsegv;
197
198 signal = current_thread_info()->exec_domain
199 && current_thread_info()->exec_domain->signal_invmap
200 && sig < 32
201 ? current_thread_info()->exec_domain->signal_invmap[sig]
202 : sig;
203
204 err |= __put_user(signal, &frame->sig);
205 if (err)
206 goto give_sigsegv;
207
208 err |= __put_user(&frame->info, &frame->pinfo);
209 err |= __put_user(&frame->uc, &frame->puc);
210 err |= copy_siginfo_to_user(&frame->info, info);
211 if (err)
212 goto give_sigsegv;
213
214 /* Create the ucontext. */
215 err |= __put_user(0, &frame->uc.uc_flags);
216 err |= __put_user(0, &frame->uc.uc_link);
217 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
218 err |= __put_user(sas_ss_flags(regs->spu),
219 &frame->uc.uc_stack.ss_flags);
220 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
221 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
222 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
223 if (err)
224 goto give_sigsegv;
225
226 /* Set up to return from userspace. */
227 regs->lr = (unsigned long)ka->sa.sa_restorer;
228
229 /* Set up registers for signal handler */
230 regs->spu = (unsigned long)frame;
231 regs->r0 = signal; /* Arg for signal handler */
232 regs->r1 = (unsigned long)&frame->info;
233 regs->r2 = (unsigned long)&frame->uc;
234 regs->bpc = (unsigned long)ka->sa.sa_handler;
235
236 set_fs(USER_DS);
237
238#if DEBUG_SIG
239 printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
240 current->comm, current->pid, frame, regs->pc);
241#endif
242
Al Viroa05c4e12010-09-24 06:23:57 +0100243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245give_sigsegv:
246 force_sigsegv(sig, current);
Al Viroa05c4e12010-09-24 06:23:57 +0100247 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Al Virobb9c8612010-09-24 06:24:53 +0100250static int prev_insn(struct pt_regs *regs)
251{
252 u16 inst;
Kyle McMartin388d1482010-10-15 21:17:09 -0400253 if (get_user(inst, (u16 __user *)(regs->bpc - 2)))
Al Virobb9c8612010-09-24 06:24:53 +0100254 return -EFAULT;
255 if ((inst & 0xfff0) == 0x10f0) /* trap ? */
256 regs->bpc -= 2;
257 else
258 regs->bpc -= 4;
259 regs->syscall_nr = -1;
260 return 0;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
264 * OK, we're invoking a handler
265 */
266
Al Viroa610d6e2012-05-21 23:42:15 -0400267static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
Al Virob7f9a112012-05-02 09:59:21 -0400269 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Are we from a system call? */
272 if (regs->syscall_nr >= 0) {
273 /* If so, check system call restarting.. */
274 switch (regs->r0) {
275 case -ERESTART_RESTARTBLOCK:
276 case -ERESTARTNOHAND:
277 regs->r0 = -EINTR;
278 break;
279
280 case -ERESTARTSYS:
281 if (!(ka->sa.sa_flags & SA_RESTART)) {
282 regs->r0 = -EINTR;
283 break;
284 }
285 /* fallthrough */
286 case -ERESTARTNOINTR:
287 regs->r0 = regs->orig_r0;
Al Virobb9c8612010-09-24 06:24:53 +0100288 if (prev_insn(regs) < 0)
Geert Uytterhoevenf9717f32012-07-17 15:47:59 -0700289 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291 }
292
293 /* Set up the stack frame */
Al Virob7f9a112012-05-02 09:59:21 -0400294 if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs))
Al Viroa610d6e2012-05-21 23:42:15 -0400295 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Al Viroefee9842012-04-28 02:04:15 -0400297 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
301 * Note that 'init' is a special process: it doesn't get signals it doesn't
302 * want to handle. Thus you cannot kill init even with a SIGKILL even by
303 * mistake.
304 */
Al Viroa05c4e12010-09-24 06:23:57 +0100305static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 siginfo_t info;
308 int signr;
309 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /*
312 * We want the common case to go fast, which
313 * is why we may in certain cases get here from
314 * kernel mode. Just return without doing anything
315 * if so.
316 */
317 if (!user_mode(regs))
Al Viroa05c4e12010-09-24 06:23:57 +0100318 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
321 if (signr > 0) {
Simon Arlott5aa8b6c2007-10-20 01:14:39 +0200322 /* Re-enable any watchpoints before delivering the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * signal to user space. The processor register will
324 * have been cleared if the watchpoint triggered
325 * inside the kernel.
326 */
327
328 /* Whee! Actually deliver the signal. */
Al Viroa610d6e2012-05-21 23:42:15 -0400329 handle_signal(signr, &ka, &info, regs);
Al Viroa05c4e12010-09-24 06:23:57 +0100330
331 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* Did we come from a system call? */
335 if (regs->syscall_nr >= 0) {
336 /* Restart the system call - no handlers present */
337 if (regs->r0 == -ERESTARTNOHAND ||
338 regs->r0 == -ERESTARTSYS ||
339 regs->r0 == -ERESTARTNOINTR) {
340 regs->r0 = regs->orig_r0;
Al Virobb9c8612010-09-24 06:24:53 +0100341 prev_insn(regs);
Al Viroa7481022010-09-24 06:22:30 +0100342 } else if (regs->r0 == -ERESTART_RESTARTBLOCK){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 regs->r0 = regs->orig_r0;
344 regs->r7 = __NR_restart_syscall;
Al Virobb9c8612010-09-24 06:24:53 +0100345 prev_insn(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 }
Al Viro51a7b442012-05-21 23:33:55 -0400348 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351/*
352 * notification of userspace execution resumption
353 * - triggered by current->work.notify_resume
354 */
Al Viroa7f83882010-09-24 06:20:35 +0100355void do_notify_resume(struct pt_regs *regs, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 /* Pending single-step? */
358 if (thread_info_flags & _TIF_SINGLESTEP)
359 clear_thread_flag(TIF_SINGLESTEP);
360
361 /* deal with pending signal delivery */
362 if (thread_info_flags & _TIF_SIGPENDING)
Al Viroa7f83882010-09-24 06:20:35 +0100363 do_signal(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
David Howellsd0420c82009-09-02 09:14:16 +0100365 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
366 clear_thread_flag(TIF_NOTIFY_RESUME);
367 tracehook_notify_resume(regs);
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 clear_thread_flag(TIF_IRET);
371}