blob: 8223eb371e65d7a63ee27344ec45f902e6bf008f [file] [log] [blame]
David Howellsb920de12008-02-08 04:19:31 -08001/* MN10300 Signal handling
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
David Howellsb920de12008-02-08 04:19:31 -080015#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>
23#include <linux/personality.h>
24#include <linux/suspend.h>
David Howells5d289962009-06-11 13:08:37 +010025#include <linux/tracehook.h>
David Howellsb920de12008-02-08 04:19:31 -080026#include <asm/cacheflush.h>
27#include <asm/ucontext.h>
28#include <asm/uaccess.h>
29#include <asm/fpu.h>
30#include "sigframe.h"
31
32#define DEBUG_SIG 0
33
David Howellsb920de12008-02-08 04:19:31 -080034/*
35 * atomically swap in the new signal mask, and wait for a signal.
36 */
37asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
38{
Al Viro68f3f162012-05-21 21:42:32 -040039 sigset_t blocked;
40 siginitset(&blocked, mask);
41 return sigsuspend(&blocked);
David Howellsb920de12008-02-08 04:19:31 -080042}
43
44/*
45 * set signal action syscall
46 */
47asmlinkage long sys_sigaction(int sig,
48 const struct old_sigaction __user *act,
49 struct old_sigaction __user *oact)
50{
51 struct k_sigaction new_ka, old_ka;
52 int ret;
53
54 if (act) {
55 old_sigset_t mask;
56 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
57 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
Al Viro60bdb722010-09-26 19:29:12 +010058 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
59 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
60 __get_user(mask, &act->sa_mask))
David Howellsb920de12008-02-08 04:19:31 -080061 return -EFAULT;
David Howellsb920de12008-02-08 04:19:31 -080062 siginitset(&new_ka.sa.sa_mask, mask);
63 }
64
65 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
66
67 if (!ret && oact) {
68 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
69 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
Al Viro60bdb722010-09-26 19:29:12 +010070 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
71 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
72 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
David Howellsb920de12008-02-08 04:19:31 -080073 return -EFAULT;
David Howellsb920de12008-02-08 04:19:31 -080074 }
75
76 return ret;
77}
78
79/*
David Howellsb920de12008-02-08 04:19:31 -080080 * do a signal return; undo the signal stack.
81 */
82static int restore_sigcontext(struct pt_regs *regs,
83 struct sigcontext __user *sc, long *_d0)
84{
85 unsigned int err = 0;
86
Al Viroc05628b2010-09-26 19:28:52 +010087 /* Always make any pending restarted system calls return -EINTR */
88 current_thread_info()->restart_block.fn = do_no_restart_syscall;
89
David Howellsb920de12008-02-08 04:19:31 -080090 if (is_using_fpu(current))
91 fpu_kill_state(current);
92
93#define COPY(x) err |= __get_user(regs->x, &sc->x)
94 COPY(d1); COPY(d2); COPY(d3);
95 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
96 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
97 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
98 COPY(lar); COPY(lir);
99 COPY(mdr); COPY(mdrq);
100 COPY(mcvf); COPY(mcrl); COPY(mcrh);
101 COPY(sp); COPY(pc);
102#undef COPY
103
104 {
105 unsigned int tmpflags;
106#ifndef CONFIG_MN10300_USING_JTAG
107#define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
108 EPSW_T | EPSW_nAR)
109#else
110#define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
111 EPSW_nAR)
112#endif
113 err |= __get_user(tmpflags, &sc->epsw);
114 regs->epsw = (regs->epsw & ~USER_EPSW) |
115 (tmpflags & USER_EPSW);
116 regs->orig_d0 = -1; /* disable syscall checks */
117 }
118
119 {
120 struct fpucontext *buf;
121 err |= __get_user(buf, &sc->fpucontext);
122 if (buf) {
123 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
124 goto badframe;
125 err |= fpu_restore_sigcontext(buf);
126 }
127 }
128
129 err |= __get_user(*_d0, &sc->d0);
130 return err;
131
132badframe:
133 return 1;
134}
135
136/*
137 * standard signal return syscall
138 */
139asmlinkage long sys_sigreturn(void)
140{
David Howells7c7fcf72010-10-27 17:29:01 +0100141 struct sigframe __user *frame;
David Howellsb920de12008-02-08 04:19:31 -0800142 sigset_t set;
143 long d0;
144
David Howells7c7fcf72010-10-27 17:29:01 +0100145 frame = (struct sigframe __user *) current_frame()->sp;
David Howellsb920de12008-02-08 04:19:31 -0800146 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
147 goto badframe;
148 if (__get_user(set.sig[0], &frame->sc.oldmask))
149 goto badframe;
150
151 if (_NSIG_WORDS > 1 &&
152 __copy_from_user(&set.sig[1], &frame->extramask,
153 sizeof(frame->extramask)))
154 goto badframe;
155
Matt Fleming00f35782012-05-11 10:57:59 +1000156 set_current_blocked(&set);
David Howellsb920de12008-02-08 04:19:31 -0800157
David Howells7c7fcf72010-10-27 17:29:01 +0100158 if (restore_sigcontext(current_frame(), &frame->sc, &d0))
David Howellsb920de12008-02-08 04:19:31 -0800159 goto badframe;
160
161 return d0;
162
163badframe:
164 force_sig(SIGSEGV, current);
165 return 0;
166}
167
168/*
169 * realtime signal return syscall
170 */
171asmlinkage long sys_rt_sigreturn(void)
172{
David Howells7c7fcf72010-10-27 17:29:01 +0100173 struct rt_sigframe __user *frame;
David Howellsb920de12008-02-08 04:19:31 -0800174 sigset_t set;
David Howells7c7fcf72010-10-27 17:29:01 +0100175 long d0;
David Howellsb920de12008-02-08 04:19:31 -0800176
David Howells7c7fcf72010-10-27 17:29:01 +0100177 frame = (struct rt_sigframe __user *) current_frame()->sp;
David Howellsb920de12008-02-08 04:19:31 -0800178 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
179 goto badframe;
180 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
181 goto badframe;
182
Matt Fleming00f35782012-05-11 10:57:59 +1000183 set_current_blocked(&set);
David Howellsb920de12008-02-08 04:19:31 -0800184
David Howells7c7fcf72010-10-27 17:29:01 +0100185 if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
David Howellsb920de12008-02-08 04:19:31 -0800186 goto badframe;
187
Al Virob30c7d52012-12-23 03:17:06 -0500188 if (restore_altstack(&frame->uc.uc_stack))
David Howellsb920de12008-02-08 04:19:31 -0800189 goto badframe;
190
191 return d0;
192
193badframe:
194 force_sig(SIGSEGV, current);
195 return 0;
196}
197
198/*
199 * store the userspace context into a signal frame
200 */
201static int setup_sigcontext(struct sigcontext __user *sc,
202 struct fpucontext *fpuctx,
203 struct pt_regs *regs,
204 unsigned long mask)
205{
206 int tmp, err = 0;
207
208#define COPY(x) err |= __put_user(regs->x, &sc->x)
209 COPY(d0); COPY(d1); COPY(d2); COPY(d3);
210 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
211 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
212 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
213 COPY(lar); COPY(lir);
214 COPY(mdr); COPY(mdrq);
215 COPY(mcvf); COPY(mcrl); COPY(mcrh);
216 COPY(sp); COPY(epsw); COPY(pc);
217#undef COPY
218
219 tmp = fpu_setup_sigcontext(fpuctx);
220 if (tmp < 0)
221 err = 1;
222 else
223 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
224
225 /* non-iBCS2 extensions.. */
226 err |= __put_user(mask, &sc->oldmask);
227
228 return err;
229}
230
231/*
232 * determine which stack to use..
233 */
234static inline void __user *get_sigframe(struct k_sigaction *ka,
235 struct pt_regs *regs,
236 size_t frame_size)
237{
238 unsigned long sp;
239
240 /* default to using normal stack */
241 sp = regs->sp;
242
243 /* this is the X/Open sanctioned signal stack switching. */
244 if (ka->sa.sa_flags & SA_ONSTACK) {
Mark Salter4c03ee72010-01-08 14:43:14 -0800245 if (sas_ss_flags(sp) == 0)
David Howellsb920de12008-02-08 04:19:31 -0800246 sp = current->sas_ss_sp + current->sas_ss_size;
247 }
248
249 return (void __user *) ((sp - frame_size) & ~7UL);
250}
251
252/*
253 * set up a normal signal frame
254 */
255static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
256 struct pt_regs *regs)
257{
258 struct sigframe __user *frame;
259 int rsig;
260
261 frame = get_sigframe(ka, regs, sizeof(*frame));
262
263 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
264 goto give_sigsegv;
265
266 rsig = sig;
267 if (sig < 32 &&
268 current_thread_info()->exec_domain &&
269 current_thread_info()->exec_domain->signal_invmap)
270 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
271
272 if (__put_user(rsig, &frame->sig) < 0 ||
273 __put_user(&frame->sc, &frame->psc) < 0)
274 goto give_sigsegv;
275
276 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
277 goto give_sigsegv;
278
279 if (_NSIG_WORDS > 1) {
280 if (__copy_to_user(frame->extramask, &set->sig[1],
281 sizeof(frame->extramask)))
282 goto give_sigsegv;
283 }
284
285 /* set up to return from userspace. If provided, use a stub already in
286 * userspace */
287 if (ka->sa.sa_flags & SA_RESTORER) {
288 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
289 goto give_sigsegv;
290 } else {
291 if (__put_user((void (*)(void))frame->retcode,
292 &frame->pretcode))
293 goto give_sigsegv;
294 /* this is mov $,d0; syscall 0 */
295 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
296 __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
297 __put_user(0x00, (char *)(frame->retcode + 2)) ||
298 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
299 __put_user(0xe0, (char *)(frame->retcode + 4)))
300 goto give_sigsegv;
301 flush_icache_range((unsigned long) frame->retcode,
302 (unsigned long) frame->retcode + 5);
303 }
304
305 /* set up registers for signal handler */
306 regs->sp = (unsigned long) frame;
307 regs->pc = (unsigned long) ka->sa.sa_handler;
308 regs->d0 = sig;
309 regs->d1 = (unsigned long) &frame->sc;
310
David Howellsb920de12008-02-08 04:19:31 -0800311#if DEBUG_SIG
312 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
313 sig, current->comm, current->pid, frame, regs->pc,
314 frame->pretcode);
315#endif
316
317 return 0;
318
319give_sigsegv:
Al Viroe46924d2010-09-26 19:28:32 +0100320 force_sigsegv(sig, current);
David Howellsb920de12008-02-08 04:19:31 -0800321 return -EFAULT;
322}
323
324/*
325 * set up a realtime signal frame
326 */
327static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
328 sigset_t *set, struct pt_regs *regs)
329{
330 struct rt_sigframe __user *frame;
331 int rsig;
332
333 frame = get_sigframe(ka, regs, sizeof(*frame));
334
335 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
336 goto give_sigsegv;
337
338 rsig = sig;
339 if (sig < 32 &&
340 current_thread_info()->exec_domain &&
341 current_thread_info()->exec_domain->signal_invmap)
342 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
343
344 if (__put_user(rsig, &frame->sig) ||
345 __put_user(&frame->info, &frame->pinfo) ||
346 __put_user(&frame->uc, &frame->puc) ||
347 copy_siginfo_to_user(&frame->info, info))
348 goto give_sigsegv;
349
350 /* create the ucontext. */
351 if (__put_user(0, &frame->uc.uc_flags) ||
352 __put_user(0, &frame->uc.uc_link) ||
Al Virob30c7d52012-12-23 03:17:06 -0500353 __save_altstack(&frame->uc.uc_stack, regs->sp) ||
David Howellsb920de12008-02-08 04:19:31 -0800354 setup_sigcontext(&frame->uc.uc_mcontext,
355 &frame->fpuctx, regs, set->sig[0]) ||
356 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
357 goto give_sigsegv;
358
359 /* set up to return from userspace. If provided, use a stub already in
360 * userspace */
361 if (ka->sa.sa_flags & SA_RESTORER) {
362 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
363 goto give_sigsegv;
364 } else {
365 if (__put_user((void(*)(void))frame->retcode,
366 &frame->pretcode) ||
367 /* This is mov $,d0; syscall 0 */
368 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
369 __put_user(__NR_rt_sigreturn,
370 (char *)(frame->retcode + 1)) ||
371 __put_user(0x00, (char *)(frame->retcode + 2)) ||
372 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
373 __put_user(0xe0, (char *)(frame->retcode + 4)))
374 goto give_sigsegv;
375
376 flush_icache_range((u_long) frame->retcode,
377 (u_long) frame->retcode + 5);
378 }
379
380 /* Set up registers for signal handler */
381 regs->sp = (unsigned long) frame;
382 regs->pc = (unsigned long) ka->sa.sa_handler;
383 regs->d0 = sig;
384 regs->d1 = (long) &frame->info;
385
David Howellsb920de12008-02-08 04:19:31 -0800386#if DEBUG_SIG
387 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
388 sig, current->comm, current->pid, frame, regs->pc,
389 frame->pretcode);
390#endif
391
392 return 0;
393
394give_sigsegv:
Al Viroe46924d2010-09-26 19:28:32 +0100395 force_sigsegv(sig, current);
David Howellsb920de12008-02-08 04:19:31 -0800396 return -EFAULT;
397}
398
Al Viro00cbf602010-09-26 19:28:42 +0100399static inline void stepback(struct pt_regs *regs)
400{
401 regs->pc -= 2;
402 regs->orig_d0 = -1;
403}
404
David Howellsb920de12008-02-08 04:19:31 -0800405/*
406 * handle the actual delivery of a signal to userspace
407 */
408static int handle_signal(int sig,
409 siginfo_t *info, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400410 struct pt_regs *regs)
David Howellsb920de12008-02-08 04:19:31 -0800411{
Al Virob7f9a112012-05-02 09:59:21 -0400412 sigset_t *oldset = sigmask_to_save();
David Howellsb920de12008-02-08 04:19:31 -0800413 int ret;
414
415 /* Are we from a system call? */
416 if (regs->orig_d0 >= 0) {
417 /* If so, check system call restarting.. */
418 switch (regs->d0) {
419 case -ERESTART_RESTARTBLOCK:
420 case -ERESTARTNOHAND:
421 regs->d0 = -EINTR;
422 break;
423
424 case -ERESTARTSYS:
425 if (!(ka->sa.sa_flags & SA_RESTART)) {
426 regs->d0 = -EINTR;
427 break;
428 }
429
430 /* fallthrough */
431 case -ERESTARTNOINTR:
432 regs->d0 = regs->orig_d0;
Al Viro00cbf602010-09-26 19:28:42 +0100433 stepback(regs);
David Howellsb920de12008-02-08 04:19:31 -0800434 }
435 }
436
437 /* Set up the stack frame */
438 if (ka->sa.sa_flags & SA_SIGINFO)
439 ret = setup_rt_frame(sig, ka, info, oldset, regs);
440 else
441 ret = setup_frame(sig, ka, oldset, regs);
Al Viroa610d6e2012-05-21 23:42:15 -0400442 if (ret)
Geert Uytterhoevenb45f9332012-07-17 15:47:54 -0700443 return ret;
David Howellsb920de12008-02-08 04:19:31 -0800444
Al Viroefee9842012-04-28 02:04:15 -0400445 signal_delivered(sig, info, ka, regs,
Geert Uytterhoevenb45f9332012-07-17 15:47:54 -0700446 test_thread_flag(TIF_SINGLESTEP));
447 return 0;
David Howellsb920de12008-02-08 04:19:31 -0800448}
449
450/*
451 * handle a potential signal
452 */
453static void do_signal(struct pt_regs *regs)
454{
455 struct k_sigaction ka;
456 siginfo_t info;
David Howellsb920de12008-02-08 04:19:31 -0800457 int signr;
458
David Howellsb920de12008-02-08 04:19:31 -0800459 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
460 if (signr > 0) {
Al Virob7f9a112012-05-02 09:59:21 -0400461 if (handle_signal(signr, &info, &ka, regs) == 0) {
David Howellsb920de12008-02-08 04:19:31 -0800462 }
463
464 return;
465 }
466
467 /* did we come from a system call? */
468 if (regs->orig_d0 >= 0) {
469 /* restart the system call - no handlers present */
470 switch (regs->d0) {
471 case -ERESTARTNOHAND:
472 case -ERESTARTSYS:
473 case -ERESTARTNOINTR:
474 regs->d0 = regs->orig_d0;
Al Viro00cbf602010-09-26 19:28:42 +0100475 stepback(regs);
David Howellsb920de12008-02-08 04:19:31 -0800476 break;
477
478 case -ERESTART_RESTARTBLOCK:
479 regs->d0 = __NR_restart_syscall;
Al Viro00cbf602010-09-26 19:28:42 +0100480 stepback(regs);
David Howellsb920de12008-02-08 04:19:31 -0800481 break;
482 }
483 }
484
485 /* if there's no signal to deliver, we just put the saved sigmask
486 * back */
Al Viro51a7b442012-05-21 23:33:55 -0400487 restore_saved_sigmask();
David Howellsb920de12008-02-08 04:19:31 -0800488}
489
490/*
491 * notification of userspace execution resumption
492 * - triggered by current->work.notify_resume
493 */
494asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
495{
496 /* Pending single-step? */
497 if (thread_info_flags & _TIF_SINGLESTEP) {
498#ifndef CONFIG_MN10300_USING_JTAG
499 regs->epsw |= EPSW_T;
500 clear_thread_flag(TIF_SINGLESTEP);
501#else
502 BUG(); /* no h/w single-step if using JTAG unit */
503#endif
504 }
505
506 /* deal with pending signal delivery */
Al Viro6fd84c02012-05-23 15:28:58 -0400507 if (thread_info_flags & _TIF_SIGPENDING)
David Howellsb920de12008-02-08 04:19:31 -0800508 do_signal(regs);
David Howells5d289962009-06-11 13:08:37 +0100509
510 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
511 clear_thread_flag(TIF_NOTIFY_RESUME);
David Howells7c7fcf72010-10-27 17:29:01 +0100512 tracehook_notify_resume(current_frame());
David Howells5d289962009-06-11 13:08:37 +0100513 }
David Howellsb920de12008-02-08 04:19:31 -0800514}