blob: 9f7572a0f5784a30d9ff16bbcfa407d10fc8a066 [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>
15#include <linux/smp_lock.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/personality.h>
25#include <linux/suspend.h>
David Howells5d289962009-06-11 13:08:37 +010026#include <linux/tracehook.h>
David Howellsb920de12008-02-08 04:19:31 -080027#include <asm/cacheflush.h>
28#include <asm/ucontext.h>
29#include <asm/uaccess.h>
30#include <asm/fpu.h>
31#include "sigframe.h"
32
33#define DEBUG_SIG 0
34
35#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36
37/*
38 * atomically swap in the new signal mask, and wait for a signal.
39 */
40asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
41{
42 mask &= _BLOCKABLE;
43 spin_lock_irq(&current->sighand->siglock);
44 current->saved_sigmask = current->blocked;
45 siginitset(&current->blocked, mask);
46 recalc_sigpending();
47 spin_unlock_irq(&current->sighand->siglock);
48
49 current->state = TASK_INTERRUPTIBLE;
50 schedule();
51 set_thread_flag(TIF_RESTORE_SIGMASK);
52 return -ERESTARTNOHAND;
53}
54
55/*
56 * set signal action syscall
57 */
58asmlinkage long sys_sigaction(int sig,
59 const struct old_sigaction __user *act,
60 struct old_sigaction __user *oact)
61{
62 struct k_sigaction new_ka, old_ka;
63 int ret;
64
65 if (act) {
66 old_sigset_t mask;
67 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
68 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
69 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
70 return -EFAULT;
71 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
72 __get_user(mask, &act->sa_mask);
73 siginitset(&new_ka.sa.sa_mask, mask);
74 }
75
76 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
77
78 if (!ret && oact) {
79 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
80 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
81 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
82 return -EFAULT;
83 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
84 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
85 }
86
87 return ret;
88}
89
90/*
91 * set alternate signal stack syscall
92 */
93asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
94{
95 return do_sigaltstack(uss, uoss, __frame->sp);
96}
97
98/*
99 * do a signal return; undo the signal stack.
100 */
101static int restore_sigcontext(struct pt_regs *regs,
102 struct sigcontext __user *sc, long *_d0)
103{
104 unsigned int err = 0;
105
106 if (is_using_fpu(current))
107 fpu_kill_state(current);
108
109#define COPY(x) err |= __get_user(regs->x, &sc->x)
110 COPY(d1); COPY(d2); COPY(d3);
111 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
112 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
113 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
114 COPY(lar); COPY(lir);
115 COPY(mdr); COPY(mdrq);
116 COPY(mcvf); COPY(mcrl); COPY(mcrh);
117 COPY(sp); COPY(pc);
118#undef COPY
119
120 {
121 unsigned int tmpflags;
122#ifndef CONFIG_MN10300_USING_JTAG
123#define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
124 EPSW_T | EPSW_nAR)
125#else
126#define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
127 EPSW_nAR)
128#endif
129 err |= __get_user(tmpflags, &sc->epsw);
130 regs->epsw = (regs->epsw & ~USER_EPSW) |
131 (tmpflags & USER_EPSW);
132 regs->orig_d0 = -1; /* disable syscall checks */
133 }
134
135 {
136 struct fpucontext *buf;
137 err |= __get_user(buf, &sc->fpucontext);
138 if (buf) {
139 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
140 goto badframe;
141 err |= fpu_restore_sigcontext(buf);
142 }
143 }
144
145 err |= __get_user(*_d0, &sc->d0);
146 return err;
147
148badframe:
149 return 1;
150}
151
152/*
153 * standard signal return syscall
154 */
155asmlinkage long sys_sigreturn(void)
156{
157 struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
158 sigset_t set;
159 long d0;
160
161 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
162 goto badframe;
163 if (__get_user(set.sig[0], &frame->sc.oldmask))
164 goto badframe;
165
166 if (_NSIG_WORDS > 1 &&
167 __copy_from_user(&set.sig[1], &frame->extramask,
168 sizeof(frame->extramask)))
169 goto badframe;
170
171 sigdelsetmask(&set, ~_BLOCKABLE);
172 spin_lock_irq(&current->sighand->siglock);
173 current->blocked = set;
174 recalc_sigpending();
175 spin_unlock_irq(&current->sighand->siglock);
176
177 if (restore_sigcontext(__frame, &frame->sc, &d0))
178 goto badframe;
179
180 return d0;
181
182badframe:
183 force_sig(SIGSEGV, current);
184 return 0;
185}
186
187/*
188 * realtime signal return syscall
189 */
190asmlinkage long sys_rt_sigreturn(void)
191{
192 struct rt_sigframe __user *frame =
193 (struct rt_sigframe __user *) __frame->sp;
194 sigset_t set;
195 unsigned long d0;
196
197 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
198 goto badframe;
199 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
200 goto badframe;
201
202 sigdelsetmask(&set, ~_BLOCKABLE);
203 spin_lock_irq(&current->sighand->siglock);
204 current->blocked = set;
205 recalc_sigpending();
206 spin_unlock_irq(&current->sighand->siglock);
207
208 if (restore_sigcontext(__frame, &frame->uc.uc_mcontext, &d0))
209 goto badframe;
210
211 if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
212 goto badframe;
213
214 return d0;
215
216badframe:
217 force_sig(SIGSEGV, current);
218 return 0;
219}
220
221/*
222 * store the userspace context into a signal frame
223 */
224static int setup_sigcontext(struct sigcontext __user *sc,
225 struct fpucontext *fpuctx,
226 struct pt_regs *regs,
227 unsigned long mask)
228{
229 int tmp, err = 0;
230
231#define COPY(x) err |= __put_user(regs->x, &sc->x)
232 COPY(d0); COPY(d1); COPY(d2); COPY(d3);
233 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
234 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
235 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
236 COPY(lar); COPY(lir);
237 COPY(mdr); COPY(mdrq);
238 COPY(mcvf); COPY(mcrl); COPY(mcrh);
239 COPY(sp); COPY(epsw); COPY(pc);
240#undef COPY
241
242 tmp = fpu_setup_sigcontext(fpuctx);
243 if (tmp < 0)
244 err = 1;
245 else
246 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
247
248 /* non-iBCS2 extensions.. */
249 err |= __put_user(mask, &sc->oldmask);
250
251 return err;
252}
253
254/*
255 * determine which stack to use..
256 */
257static inline void __user *get_sigframe(struct k_sigaction *ka,
258 struct pt_regs *regs,
259 size_t frame_size)
260{
261 unsigned long sp;
262
263 /* default to using normal stack */
264 sp = regs->sp;
265
266 /* this is the X/Open sanctioned signal stack switching. */
267 if (ka->sa.sa_flags & SA_ONSTACK) {
268 if (!on_sig_stack(sp))
269 sp = current->sas_ss_sp + current->sas_ss_size;
270 }
271
272 return (void __user *) ((sp - frame_size) & ~7UL);
273}
274
275/*
276 * set up a normal signal frame
277 */
278static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
279 struct pt_regs *regs)
280{
281 struct sigframe __user *frame;
282 int rsig;
283
284 frame = get_sigframe(ka, regs, sizeof(*frame));
285
286 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
287 goto give_sigsegv;
288
289 rsig = sig;
290 if (sig < 32 &&
291 current_thread_info()->exec_domain &&
292 current_thread_info()->exec_domain->signal_invmap)
293 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
294
295 if (__put_user(rsig, &frame->sig) < 0 ||
296 __put_user(&frame->sc, &frame->psc) < 0)
297 goto give_sigsegv;
298
299 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
300 goto give_sigsegv;
301
302 if (_NSIG_WORDS > 1) {
303 if (__copy_to_user(frame->extramask, &set->sig[1],
304 sizeof(frame->extramask)))
305 goto give_sigsegv;
306 }
307
308 /* set up to return from userspace. If provided, use a stub already in
309 * userspace */
310 if (ka->sa.sa_flags & SA_RESTORER) {
311 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
312 goto give_sigsegv;
313 } else {
314 if (__put_user((void (*)(void))frame->retcode,
315 &frame->pretcode))
316 goto give_sigsegv;
317 /* this is mov $,d0; syscall 0 */
318 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
319 __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
320 __put_user(0x00, (char *)(frame->retcode + 2)) ||
321 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
322 __put_user(0xe0, (char *)(frame->retcode + 4)))
323 goto give_sigsegv;
324 flush_icache_range((unsigned long) frame->retcode,
325 (unsigned long) frame->retcode + 5);
326 }
327
328 /* set up registers for signal handler */
329 regs->sp = (unsigned long) frame;
330 regs->pc = (unsigned long) ka->sa.sa_handler;
331 regs->d0 = sig;
332 regs->d1 = (unsigned long) &frame->sc;
333
334 set_fs(USER_DS);
335
336 /* the tracer may want to single-step inside the handler */
337 if (test_thread_flag(TIF_SINGLESTEP))
338 ptrace_notify(SIGTRAP);
339
340#if DEBUG_SIG
341 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
342 sig, current->comm, current->pid, frame, regs->pc,
343 frame->pretcode);
344#endif
345
346 return 0;
347
348give_sigsegv:
349 force_sig(SIGSEGV, current);
350 return -EFAULT;
351}
352
353/*
354 * set up a realtime signal frame
355 */
356static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
357 sigset_t *set, struct pt_regs *regs)
358{
359 struct rt_sigframe __user *frame;
360 int rsig;
361
362 frame = get_sigframe(ka, regs, sizeof(*frame));
363
364 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
365 goto give_sigsegv;
366
367 rsig = sig;
368 if (sig < 32 &&
369 current_thread_info()->exec_domain &&
370 current_thread_info()->exec_domain->signal_invmap)
371 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
372
373 if (__put_user(rsig, &frame->sig) ||
374 __put_user(&frame->info, &frame->pinfo) ||
375 __put_user(&frame->uc, &frame->puc) ||
376 copy_siginfo_to_user(&frame->info, info))
377 goto give_sigsegv;
378
379 /* create the ucontext. */
380 if (__put_user(0, &frame->uc.uc_flags) ||
381 __put_user(0, &frame->uc.uc_link) ||
382 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
383 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
384 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
385 setup_sigcontext(&frame->uc.uc_mcontext,
386 &frame->fpuctx, regs, set->sig[0]) ||
387 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
388 goto give_sigsegv;
389
390 /* set up to return from userspace. If provided, use a stub already in
391 * userspace */
392 if (ka->sa.sa_flags & SA_RESTORER) {
393 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
394 goto give_sigsegv;
395 } else {
396 if (__put_user((void(*)(void))frame->retcode,
397 &frame->pretcode) ||
398 /* This is mov $,d0; syscall 0 */
399 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
400 __put_user(__NR_rt_sigreturn,
401 (char *)(frame->retcode + 1)) ||
402 __put_user(0x00, (char *)(frame->retcode + 2)) ||
403 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
404 __put_user(0xe0, (char *)(frame->retcode + 4)))
405 goto give_sigsegv;
406
407 flush_icache_range((u_long) frame->retcode,
408 (u_long) frame->retcode + 5);
409 }
410
411 /* Set up registers for signal handler */
412 regs->sp = (unsigned long) frame;
413 regs->pc = (unsigned long) ka->sa.sa_handler;
414 regs->d0 = sig;
415 regs->d1 = (long) &frame->info;
416
417 set_fs(USER_DS);
418
419 /* the tracer may want to single-step inside the handler */
420 if (test_thread_flag(TIF_SINGLESTEP))
421 ptrace_notify(SIGTRAP);
422
423#if DEBUG_SIG
424 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
425 sig, current->comm, current->pid, frame, regs->pc,
426 frame->pretcode);
427#endif
428
429 return 0;
430
431give_sigsegv:
432 force_sig(SIGSEGV, current);
433 return -EFAULT;
434}
435
436/*
437 * handle the actual delivery of a signal to userspace
438 */
439static int handle_signal(int sig,
440 siginfo_t *info, struct k_sigaction *ka,
441 sigset_t *oldset, struct pt_regs *regs)
442{
443 int ret;
444
445 /* Are we from a system call? */
446 if (regs->orig_d0 >= 0) {
447 /* If so, check system call restarting.. */
448 switch (regs->d0) {
449 case -ERESTART_RESTARTBLOCK:
450 case -ERESTARTNOHAND:
451 regs->d0 = -EINTR;
452 break;
453
454 case -ERESTARTSYS:
455 if (!(ka->sa.sa_flags & SA_RESTART)) {
456 regs->d0 = -EINTR;
457 break;
458 }
459
460 /* fallthrough */
461 case -ERESTARTNOINTR:
462 regs->d0 = regs->orig_d0;
463 regs->pc -= 2;
464 }
465 }
466
467 /* Set up the stack frame */
468 if (ka->sa.sa_flags & SA_SIGINFO)
469 ret = setup_rt_frame(sig, ka, info, oldset, regs);
470 else
471 ret = setup_frame(sig, ka, oldset, regs);
472
473 if (ret == 0) {
474 spin_lock_irq(&current->sighand->siglock);
475 sigorsets(&current->blocked, &current->blocked,
476 &ka->sa.sa_mask);
477 if (!(ka->sa.sa_flags & SA_NODEFER))
478 sigaddset(&current->blocked, sig);
479 recalc_sigpending();
480 spin_unlock_irq(&current->sighand->siglock);
481 }
482
483 return ret;
484}
485
486/*
487 * handle a potential signal
488 */
489static void do_signal(struct pt_regs *regs)
490{
491 struct k_sigaction ka;
492 siginfo_t info;
493 sigset_t *oldset;
494 int signr;
495
496 /* we want the common case to go fast, which is why we may in certain
497 * cases get here from kernel mode */
498 if (!user_mode(regs))
499 return;
500
501 if (test_thread_flag(TIF_RESTORE_SIGMASK))
502 oldset = &current->saved_sigmask;
503 else
504 oldset = &current->blocked;
505
506 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
507 if (signr > 0) {
508 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
509 /* a signal was successfully delivered; the saved
510 * sigmask will have been stored in the signal frame,
511 * and will be restored by sigreturn, so we can simply
512 * clear the TIF_RESTORE_SIGMASK flag */
513 if (test_thread_flag(TIF_RESTORE_SIGMASK))
514 clear_thread_flag(TIF_RESTORE_SIGMASK);
David Howells5d289962009-06-11 13:08:37 +0100515
516 tracehook_signal_handler(signr, &info, &ka, regs,
517 test_thread_flag(TIF_SINGLESTEP));
David Howellsb920de12008-02-08 04:19:31 -0800518 }
519
520 return;
521 }
522
523 /* did we come from a system call? */
524 if (regs->orig_d0 >= 0) {
525 /* restart the system call - no handlers present */
526 switch (regs->d0) {
527 case -ERESTARTNOHAND:
528 case -ERESTARTSYS:
529 case -ERESTARTNOINTR:
530 regs->d0 = regs->orig_d0;
531 regs->pc -= 2;
532 break;
533
534 case -ERESTART_RESTARTBLOCK:
535 regs->d0 = __NR_restart_syscall;
536 regs->pc -= 2;
537 break;
538 }
539 }
540
541 /* if there's no signal to deliver, we just put the saved sigmask
542 * back */
543 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
544 clear_thread_flag(TIF_RESTORE_SIGMASK);
545 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
546 }
547}
548
549/*
550 * notification of userspace execution resumption
551 * - triggered by current->work.notify_resume
552 */
553asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
554{
555 /* Pending single-step? */
556 if (thread_info_flags & _TIF_SINGLESTEP) {
557#ifndef CONFIG_MN10300_USING_JTAG
558 regs->epsw |= EPSW_T;
559 clear_thread_flag(TIF_SINGLESTEP);
560#else
561 BUG(); /* no h/w single-step if using JTAG unit */
562#endif
563 }
564
565 /* deal with pending signal delivery */
566 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
567 do_signal(regs);
David Howells5d289962009-06-11 13:08:37 +0100568
569 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
570 clear_thread_flag(TIF_NOTIFY_RESUME);
571 tracehook_notify_resume(__frame);
572 }
David Howellsb920de12008-02-08 04:19:31 -0800573}