x86, efi: Set runtime_version to the EFI spec revision
[linux/fpc-iii.git] / arch / mn10300 / kernel / signal.c
blobf570b3085ef90004d2150eca7f86107ea232cf1e
1 /* MN10300 Signal handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #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>
25 #include <linux/tracehook.h>
26 #include <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/fpu.h>
30 #include "sigframe.h"
32 #define DEBUG_SIG 0
35 * atomically swap in the new signal mask, and wait for a signal.
37 asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
39 sigset_t blocked;
40 siginitset(&blocked, mask);
41 return sigsuspend(&blocked);
45 * set signal action syscall
47 asmlinkage long sys_sigaction(int sig,
48 const struct old_sigaction __user *act,
49 struct old_sigaction __user *oact)
51 struct k_sigaction new_ka, old_ka;
52 int ret;
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) ||
58 __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))
61 return -EFAULT;
62 siginitset(&new_ka.sa.sa_mask, mask);
65 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
67 if (!ret && oact) {
68 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
69 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
70 __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))
73 return -EFAULT;
76 return ret;
80 * set alternate signal stack syscall
82 asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
84 return do_sigaltstack(uss, uoss, current_frame()->sp);
88 * do a signal return; undo the signal stack.
90 static int restore_sigcontext(struct pt_regs *regs,
91 struct sigcontext __user *sc, long *_d0)
93 unsigned int err = 0;
95 /* Always make any pending restarted system calls return -EINTR */
96 current_thread_info()->restart_block.fn = do_no_restart_syscall;
98 if (is_using_fpu(current))
99 fpu_kill_state(current);
101 #define COPY(x) err |= __get_user(regs->x, &sc->x)
102 COPY(d1); COPY(d2); COPY(d3);
103 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
104 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
105 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
106 COPY(lar); COPY(lir);
107 COPY(mdr); COPY(mdrq);
108 COPY(mcvf); COPY(mcrl); COPY(mcrh);
109 COPY(sp); COPY(pc);
110 #undef COPY
113 unsigned int tmpflags;
114 #ifndef CONFIG_MN10300_USING_JTAG
115 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
116 EPSW_T | EPSW_nAR)
117 #else
118 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
119 EPSW_nAR)
120 #endif
121 err |= __get_user(tmpflags, &sc->epsw);
122 regs->epsw = (regs->epsw & ~USER_EPSW) |
123 (tmpflags & USER_EPSW);
124 regs->orig_d0 = -1; /* disable syscall checks */
128 struct fpucontext *buf;
129 err |= __get_user(buf, &sc->fpucontext);
130 if (buf) {
131 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
132 goto badframe;
133 err |= fpu_restore_sigcontext(buf);
137 err |= __get_user(*_d0, &sc->d0);
138 return err;
140 badframe:
141 return 1;
145 * standard signal return syscall
147 asmlinkage long sys_sigreturn(void)
149 struct sigframe __user *frame;
150 sigset_t set;
151 long d0;
153 frame = (struct sigframe __user *) current_frame()->sp;
154 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
155 goto badframe;
156 if (__get_user(set.sig[0], &frame->sc.oldmask))
157 goto badframe;
159 if (_NSIG_WORDS > 1 &&
160 __copy_from_user(&set.sig[1], &frame->extramask,
161 sizeof(frame->extramask)))
162 goto badframe;
164 set_current_blocked(&set);
166 if (restore_sigcontext(current_frame(), &frame->sc, &d0))
167 goto badframe;
169 return d0;
171 badframe:
172 force_sig(SIGSEGV, current);
173 return 0;
177 * realtime signal return syscall
179 asmlinkage long sys_rt_sigreturn(void)
181 struct rt_sigframe __user *frame;
182 sigset_t set;
183 long d0;
185 frame = (struct rt_sigframe __user *) current_frame()->sp;
186 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
187 goto badframe;
188 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
189 goto badframe;
191 set_current_blocked(&set);
193 if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
194 goto badframe;
196 if (do_sigaltstack(&frame->uc.uc_stack, NULL, current_frame()->sp) ==
197 -EFAULT)
198 goto badframe;
200 return d0;
202 badframe:
203 force_sig(SIGSEGV, current);
204 return 0;
208 * store the userspace context into a signal frame
210 static int setup_sigcontext(struct sigcontext __user *sc,
211 struct fpucontext *fpuctx,
212 struct pt_regs *regs,
213 unsigned long mask)
215 int tmp, err = 0;
217 #define COPY(x) err |= __put_user(regs->x, &sc->x)
218 COPY(d0); COPY(d1); COPY(d2); COPY(d3);
219 COPY(a0); COPY(a1); COPY(a2); COPY(a3);
220 COPY(e0); COPY(e1); COPY(e2); COPY(e3);
221 COPY(e4); COPY(e5); COPY(e6); COPY(e7);
222 COPY(lar); COPY(lir);
223 COPY(mdr); COPY(mdrq);
224 COPY(mcvf); COPY(mcrl); COPY(mcrh);
225 COPY(sp); COPY(epsw); COPY(pc);
226 #undef COPY
228 tmp = fpu_setup_sigcontext(fpuctx);
229 if (tmp < 0)
230 err = 1;
231 else
232 err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
234 /* non-iBCS2 extensions.. */
235 err |= __put_user(mask, &sc->oldmask);
237 return err;
241 * determine which stack to use..
243 static inline void __user *get_sigframe(struct k_sigaction *ka,
244 struct pt_regs *regs,
245 size_t frame_size)
247 unsigned long sp;
249 /* default to using normal stack */
250 sp = regs->sp;
252 /* this is the X/Open sanctioned signal stack switching. */
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;
258 return (void __user *) ((sp - frame_size) & ~7UL);
262 * set up a normal signal frame
264 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
265 struct pt_regs *regs)
267 struct sigframe __user *frame;
268 int rsig;
270 frame = get_sigframe(ka, regs, sizeof(*frame));
272 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
273 goto give_sigsegv;
275 rsig = sig;
276 if (sig < 32 &&
277 current_thread_info()->exec_domain &&
278 current_thread_info()->exec_domain->signal_invmap)
279 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
281 if (__put_user(rsig, &frame->sig) < 0 ||
282 __put_user(&frame->sc, &frame->psc) < 0)
283 goto give_sigsegv;
285 if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
286 goto give_sigsegv;
288 if (_NSIG_WORDS > 1) {
289 if (__copy_to_user(frame->extramask, &set->sig[1],
290 sizeof(frame->extramask)))
291 goto give_sigsegv;
294 /* set up to return from userspace. If provided, use a stub already in
295 * userspace */
296 if (ka->sa.sa_flags & SA_RESTORER) {
297 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
298 goto give_sigsegv;
299 } else {
300 if (__put_user((void (*)(void))frame->retcode,
301 &frame->pretcode))
302 goto give_sigsegv;
303 /* this is mov $,d0; syscall 0 */
304 if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
305 __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
306 __put_user(0x00, (char *)(frame->retcode + 2)) ||
307 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
308 __put_user(0xe0, (char *)(frame->retcode + 4)))
309 goto give_sigsegv;
310 flush_icache_range((unsigned long) frame->retcode,
311 (unsigned long) frame->retcode + 5);
314 /* set up registers for signal handler */
315 regs->sp = (unsigned long) frame;
316 regs->pc = (unsigned long) ka->sa.sa_handler;
317 regs->d0 = sig;
318 regs->d1 = (unsigned long) &frame->sc;
320 #if DEBUG_SIG
321 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
322 sig, current->comm, current->pid, frame, regs->pc,
323 frame->pretcode);
324 #endif
326 return 0;
328 give_sigsegv:
329 force_sigsegv(sig, current);
330 return -EFAULT;
334 * set up a realtime signal frame
336 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
337 sigset_t *set, struct pt_regs *regs)
339 struct rt_sigframe __user *frame;
340 int rsig;
342 frame = get_sigframe(ka, regs, sizeof(*frame));
344 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
345 goto give_sigsegv;
347 rsig = sig;
348 if (sig < 32 &&
349 current_thread_info()->exec_domain &&
350 current_thread_info()->exec_domain->signal_invmap)
351 rsig = current_thread_info()->exec_domain->signal_invmap[sig];
353 if (__put_user(rsig, &frame->sig) ||
354 __put_user(&frame->info, &frame->pinfo) ||
355 __put_user(&frame->uc, &frame->puc) ||
356 copy_siginfo_to_user(&frame->info, info))
357 goto give_sigsegv;
359 /* create the ucontext. */
360 if (__put_user(0, &frame->uc.uc_flags) ||
361 __put_user(0, &frame->uc.uc_link) ||
362 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
363 __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
364 __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
365 setup_sigcontext(&frame->uc.uc_mcontext,
366 &frame->fpuctx, regs, set->sig[0]) ||
367 __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
368 goto give_sigsegv;
370 /* set up to return from userspace. If provided, use a stub already in
371 * userspace */
372 if (ka->sa.sa_flags & SA_RESTORER) {
373 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
374 goto give_sigsegv;
375 } else {
376 if (__put_user((void(*)(void))frame->retcode,
377 &frame->pretcode) ||
378 /* This is mov $,d0; syscall 0 */
379 __put_user(0x2c, (char *)(frame->retcode + 0)) ||
380 __put_user(__NR_rt_sigreturn,
381 (char *)(frame->retcode + 1)) ||
382 __put_user(0x00, (char *)(frame->retcode + 2)) ||
383 __put_user(0xf0, (char *)(frame->retcode + 3)) ||
384 __put_user(0xe0, (char *)(frame->retcode + 4)))
385 goto give_sigsegv;
387 flush_icache_range((u_long) frame->retcode,
388 (u_long) frame->retcode + 5);
391 /* Set up registers for signal handler */
392 regs->sp = (unsigned long) frame;
393 regs->pc = (unsigned long) ka->sa.sa_handler;
394 regs->d0 = sig;
395 regs->d1 = (long) &frame->info;
397 #if DEBUG_SIG
398 printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
399 sig, current->comm, current->pid, frame, regs->pc,
400 frame->pretcode);
401 #endif
403 return 0;
405 give_sigsegv:
406 force_sigsegv(sig, current);
407 return -EFAULT;
410 static inline void stepback(struct pt_regs *regs)
412 regs->pc -= 2;
413 regs->orig_d0 = -1;
417 * handle the actual delivery of a signal to userspace
419 static int handle_signal(int sig,
420 siginfo_t *info, struct k_sigaction *ka,
421 struct pt_regs *regs)
423 sigset_t *oldset = sigmask_to_save();
424 int ret;
426 /* Are we from a system call? */
427 if (regs->orig_d0 >= 0) {
428 /* If so, check system call restarting.. */
429 switch (regs->d0) {
430 case -ERESTART_RESTARTBLOCK:
431 case -ERESTARTNOHAND:
432 regs->d0 = -EINTR;
433 break;
435 case -ERESTARTSYS:
436 if (!(ka->sa.sa_flags & SA_RESTART)) {
437 regs->d0 = -EINTR;
438 break;
441 /* fallthrough */
442 case -ERESTARTNOINTR:
443 regs->d0 = regs->orig_d0;
444 stepback(regs);
448 /* Set up the stack frame */
449 if (ka->sa.sa_flags & SA_SIGINFO)
450 ret = setup_rt_frame(sig, ka, info, oldset, regs);
451 else
452 ret = setup_frame(sig, ka, oldset, regs);
453 if (ret)
454 return ret;
456 signal_delivered(sig, info, ka, regs,
457 test_thread_flag(TIF_SINGLESTEP));
458 return 0;
462 * handle a potential signal
464 static void do_signal(struct pt_regs *regs)
466 struct k_sigaction ka;
467 siginfo_t info;
468 int signr;
470 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
471 if (signr > 0) {
472 if (handle_signal(signr, &info, &ka, regs) == 0) {
475 return;
478 /* did we come from a system call? */
479 if (regs->orig_d0 >= 0) {
480 /* restart the system call - no handlers present */
481 switch (regs->d0) {
482 case -ERESTARTNOHAND:
483 case -ERESTARTSYS:
484 case -ERESTARTNOINTR:
485 regs->d0 = regs->orig_d0;
486 stepback(regs);
487 break;
489 case -ERESTART_RESTARTBLOCK:
490 regs->d0 = __NR_restart_syscall;
491 stepback(regs);
492 break;
496 /* if there's no signal to deliver, we just put the saved sigmask
497 * back */
498 restore_saved_sigmask();
502 * notification of userspace execution resumption
503 * - triggered by current->work.notify_resume
505 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
507 /* Pending single-step? */
508 if (thread_info_flags & _TIF_SINGLESTEP) {
509 #ifndef CONFIG_MN10300_USING_JTAG
510 regs->epsw |= EPSW_T;
511 clear_thread_flag(TIF_SINGLESTEP);
512 #else
513 BUG(); /* no h/w single-step if using JTAG unit */
514 #endif
517 /* deal with pending signal delivery */
518 if (thread_info_flags & _TIF_SIGPENDING)
519 do_signal(regs);
521 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
522 clear_thread_flag(TIF_NOTIFY_RESUME);
523 tracehook_notify_resume(current_frame());