Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / h8300 / kernel / signal.c
blobcd63f5a6c8fe9d3ad6a77f05d63a12d7cecd612a
1 /*
2 * linux/arch/h8300/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
8 * for more details.
9 */
12 * uClinux H8/300 support by Yoshinori Sato <ysato@users.sourceforge.jp>
13 * and David McCullough <davidm@snapgear.com>
15 * Based on
16 * Linux/m68k by Hamish Macdonald
20 * ++roman (07/09/96): implemented signal stacks (specially for tosemu on
21 * Atari :-) Current limitation: Only one sigstack can be active at one time.
22 * If a second signal with SA_ONSTACK set arrives while working on a sigstack,
23 * SA_ONSTACK is ignored. This behaviour avoids lots of trouble with nested
24 * signal handlers!
27 #include <linux/sched.h>
28 #include <linux/mm.h>
29 #include <linux/kernel.h>
30 #include <linux/signal.h>
31 #include <linux/syscalls.h>
32 #include <linux/errno.h>
33 #include <linux/wait.h>
34 #include <linux/ptrace.h>
35 #include <linux/unistd.h>
36 #include <linux/stddef.h>
37 #include <linux/highuid.h>
38 #include <linux/personality.h>
39 #include <linux/tty.h>
40 #include <linux/binfmts.h>
41 #include <linux/freezer.h>
42 #include <linux/tracehook.h>
44 #include <asm/setup.h>
45 #include <asm/uaccess.h>
46 #include <asm/pgtable.h>
47 #include <asm/traps.h>
48 #include <asm/ucontext.h>
50 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
52 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
55 * Atomically swap in the new signal mask, and wait for a signal.
57 asmlinkage int do_sigsuspend(struct pt_regs *regs)
59 old_sigset_t mask = regs->er3;
60 sigset_t saveset, blocked;
62 saveset = current->blocked;
64 mask &= _BLOCKABLE;
65 siginitset(&blocked, mask);
66 set_current_blocked(&blocked);
68 regs->er0 = -EINTR;
69 while (1) {
70 current->state = TASK_INTERRUPTIBLE;
71 schedule();
72 if (do_signal(regs, &saveset))
73 return -EINTR;
77 asmlinkage int
78 do_rt_sigsuspend(struct pt_regs *regs)
80 sigset_t *unewset = (sigset_t *)regs->er1;
81 size_t sigsetsize = (size_t)regs->er2;
82 sigset_t saveset, newset;
84 /* XXX: Don't preclude handling different sized sigset_t's. */
85 if (sigsetsize != sizeof(sigset_t))
86 return -EINVAL;
88 if (copy_from_user(&newset, unewset, sizeof(newset)))
89 return -EFAULT;
90 sigdelsetmask(&newset, ~_BLOCKABLE);
92 saveset = current->blocked;
93 set_current_blocked(&newset);
95 regs->er0 = -EINTR;
96 while (1) {
97 current->state = TASK_INTERRUPTIBLE;
98 schedule();
99 if (do_signal(regs, &saveset))
100 return -EINTR;
104 asmlinkage int
105 sys_sigaction(int sig, const struct old_sigaction *act,
106 struct old_sigaction *oact)
108 struct k_sigaction new_ka, old_ka;
109 int ret;
111 if (act) {
112 old_sigset_t mask;
113 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
114 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
115 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
116 return -EFAULT;
117 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
118 __get_user(mask, &act->sa_mask);
119 siginitset(&new_ka.sa.sa_mask, mask);
122 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
124 if (!ret && oact) {
125 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
126 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
127 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
128 return -EFAULT;
129 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
130 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
133 return ret;
136 asmlinkage int
137 sys_sigaltstack(const stack_t *uss, stack_t *uoss)
139 return do_sigaltstack(uss, uoss, rdusp());
144 * Do a signal return; undo the signal stack.
146 * Keep the return code on the stack quadword aligned!
147 * That makes the cache flush below easier.
150 struct sigframe
152 long dummy_er0;
153 long dummy_vector;
154 #if defined(CONFIG_CPU_H8S)
155 short dummy_exr;
156 #endif
157 long dummy_pc;
158 char *pretcode;
159 unsigned char retcode[8];
160 unsigned long extramask[_NSIG_WORDS-1];
161 struct sigcontext sc;
162 int sig;
163 } __attribute__((aligned(2),packed));
165 struct rt_sigframe
167 long dummy_er0;
168 long dummy_vector;
169 #if defined(CONFIG_CPU_H8S)
170 short dummy_exr;
171 #endif
172 long dummy_pc;
173 char *pretcode;
174 struct siginfo *pinfo;
175 void *puc;
176 unsigned char retcode[8];
177 struct siginfo info;
178 struct ucontext uc;
179 int sig;
180 } __attribute__((aligned(2),packed));
182 static inline int
183 restore_sigcontext(struct pt_regs *regs, struct sigcontext *usc,
184 int *pd0)
186 int err = 0;
187 unsigned int ccr;
188 unsigned int usp;
189 unsigned int er0;
191 /* Always make any pending restarted system calls return -EINTR */
192 current_thread_info()->restart_block.fn = do_no_restart_syscall;
194 #define COPY(r) err |= __get_user(regs->r, &usc->sc_##r) /* restore passed registers */
195 COPY(er1);
196 COPY(er2);
197 COPY(er3);
198 COPY(er5);
199 COPY(pc);
200 ccr = regs->ccr & 0x10;
201 COPY(ccr);
202 #undef COPY
203 regs->ccr &= 0xef;
204 regs->ccr |= ccr;
205 regs->orig_er0 = -1; /* disable syscall checks */
206 err |= __get_user(usp, &usc->sc_usp);
207 wrusp(usp);
209 err |= __get_user(er0, &usc->sc_er0);
210 *pd0 = er0;
211 return err;
214 asmlinkage int do_sigreturn(unsigned long __unused,...)
216 struct pt_regs *regs = (struct pt_regs *) (&__unused - 1);
217 unsigned long usp = rdusp();
218 struct sigframe *frame = (struct sigframe *)(usp - 4);
219 sigset_t set;
220 int er0;
222 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
223 goto badframe;
224 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
225 (_NSIG_WORDS > 1 &&
226 __copy_from_user(&set.sig[1], &frame->extramask,
227 sizeof(frame->extramask))))
228 goto badframe;
230 sigdelsetmask(&set, ~_BLOCKABLE);
231 set_current_blocked(&set);
233 if (restore_sigcontext(regs, &frame->sc, &er0))
234 goto badframe;
235 return er0;
237 badframe:
238 force_sig(SIGSEGV, current);
239 return 0;
242 asmlinkage int do_rt_sigreturn(unsigned long __unused,...)
244 struct pt_regs *regs = (struct pt_regs *) &__unused;
245 unsigned long usp = rdusp();
246 struct rt_sigframe *frame = (struct rt_sigframe *)(usp - 4);
247 sigset_t set;
248 int er0;
250 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
251 goto badframe;
252 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
253 goto badframe;
255 sigdelsetmask(&set, ~_BLOCKABLE);
256 set_current_blocked(&set);
258 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &er0))
259 goto badframe;
261 if (do_sigaltstack(&frame->uc.uc_stack, NULL, usp) == -EFAULT)
262 goto badframe;
264 return er0;
266 badframe:
267 force_sig(SIGSEGV, current);
268 return 0;
271 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
272 unsigned long mask)
274 int err = 0;
276 err |= __put_user(regs->er0, &sc->sc_er0);
277 err |= __put_user(regs->er1, &sc->sc_er1);
278 err |= __put_user(regs->er2, &sc->sc_er2);
279 err |= __put_user(regs->er3, &sc->sc_er3);
280 err |= __put_user(regs->er4, &sc->sc_er4);
281 err |= __put_user(regs->er5, &sc->sc_er5);
282 err |= __put_user(regs->er6, &sc->sc_er6);
283 err |= __put_user(rdusp(), &sc->sc_usp);
284 err |= __put_user(regs->pc, &sc->sc_pc);
285 err |= __put_user(regs->ccr, &sc->sc_ccr);
286 err |= __put_user(mask, &sc->sc_mask);
288 return err;
291 static inline void *
292 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
294 unsigned long usp;
296 /* Default to using normal stack. */
297 usp = rdusp();
299 /* This is the X/Open sanctioned signal stack switching. */
300 if (ka->sa.sa_flags & SA_ONSTACK) {
301 if (!sas_ss_flags(usp))
302 usp = current->sas_ss_sp + current->sas_ss_size;
304 return (void *)((usp - frame_size) & -8UL);
307 static void setup_frame (int sig, struct k_sigaction *ka,
308 sigset_t *set, struct pt_regs *regs)
310 struct sigframe *frame;
311 int err = 0;
312 int usig;
313 unsigned char *ret;
315 frame = get_sigframe(ka, regs, sizeof(*frame));
317 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
318 goto give_sigsegv;
320 usig = current_thread_info()->exec_domain
321 && current_thread_info()->exec_domain->signal_invmap
322 && sig < 32
323 ? current_thread_info()->exec_domain->signal_invmap[sig]
324 : sig;
326 err |= __put_user(usig, &frame->sig);
327 if (err)
328 goto give_sigsegv;
330 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
331 if (err)
332 goto give_sigsegv;
334 if (_NSIG_WORDS > 1) {
335 err |= copy_to_user(frame->extramask, &set->sig[1],
336 sizeof(frame->extramask));
337 if (err)
338 goto give_sigsegv;
341 ret = frame->retcode;
342 if (ka->sa.sa_flags & SA_RESTORER)
343 ret = (unsigned char *)(ka->sa.sa_restorer);
344 else {
345 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
346 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
347 (unsigned long *)(frame->retcode + 0));
348 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
351 /* Set up to return from userspace. */
352 err |= __put_user(ret, &frame->pretcode);
354 if (err)
355 goto give_sigsegv;
357 /* Set up registers for signal handler */
358 wrusp ((unsigned long) frame);
359 regs->pc = (unsigned long) ka->sa.sa_handler;
360 regs->er0 = (current_thread_info()->exec_domain
361 && current_thread_info()->exec_domain->signal_invmap
362 && sig < 32
363 ? current_thread_info()->exec_domain->signal_invmap[sig]
364 : sig);
365 regs->er1 = (unsigned long)&(frame->sc);
366 regs->er5 = current->mm->start_data; /* GOT base */
368 return;
370 give_sigsegv:
371 force_sigsegv(sig, current);
374 static void setup_rt_frame (int sig, struct k_sigaction *ka, siginfo_t *info,
375 sigset_t *set, struct pt_regs *regs)
377 struct rt_sigframe *frame;
378 int err = 0;
379 int usig;
380 unsigned char *ret;
382 frame = get_sigframe(ka, regs, sizeof(*frame));
384 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
385 goto give_sigsegv;
387 usig = current_thread_info()->exec_domain
388 && current_thread_info()->exec_domain->signal_invmap
389 && sig < 32
390 ? current_thread_info()->exec_domain->signal_invmap[sig]
391 : sig;
393 err |= __put_user(usig, &frame->sig);
394 if (err)
395 goto give_sigsegv;
397 err |= __put_user(&frame->info, &frame->pinfo);
398 err |= __put_user(&frame->uc, &frame->puc);
399 err |= copy_siginfo_to_user(&frame->info, info);
400 if (err)
401 goto give_sigsegv;
403 /* Create the ucontext. */
404 err |= __put_user(0, &frame->uc.uc_flags);
405 err |= __put_user(0, &frame->uc.uc_link);
406 err |= __put_user((void *)current->sas_ss_sp,
407 &frame->uc.uc_stack.ss_sp);
408 err |= __put_user(sas_ss_flags(rdusp()),
409 &frame->uc.uc_stack.ss_flags);
410 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
411 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
412 err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
413 if (err)
414 goto give_sigsegv;
416 /* Set up to return from userspace. */
417 ret = frame->retcode;
418 if (ka->sa.sa_flags & SA_RESTORER)
419 ret = (unsigned char *)(ka->sa.sa_restorer);
420 else {
421 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
422 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
423 (unsigned long *)(frame->retcode + 0));
424 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
426 err |= __put_user(ret, &frame->pretcode);
428 if (err)
429 goto give_sigsegv;
431 /* Set up registers for signal handler */
432 wrusp ((unsigned long) frame);
433 regs->pc = (unsigned long) ka->sa.sa_handler;
434 regs->er0 = (current_thread_info()->exec_domain
435 && current_thread_info()->exec_domain->signal_invmap
436 && sig < 32
437 ? current_thread_info()->exec_domain->signal_invmap[sig]
438 : sig);
439 regs->er1 = (unsigned long)&(frame->info);
440 regs->er2 = (unsigned long)&frame->uc;
441 regs->er5 = current->mm->start_data; /* GOT base */
443 return;
445 give_sigsegv:
446 force_sigsegv(sig, current);
450 * OK, we're invoking a handler
452 static void
453 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
454 sigset_t *oldset, struct pt_regs * regs)
456 /* are we from a system call? */
457 if (regs->orig_er0 >= 0) {
458 switch (regs->er0) {
459 case -ERESTART_RESTARTBLOCK:
460 case -ERESTARTNOHAND:
461 regs->er0 = -EINTR;
462 break;
464 case -ERESTARTSYS:
465 if (!(ka->sa.sa_flags & SA_RESTART)) {
466 regs->er0 = -EINTR;
467 break;
469 /* fallthrough */
470 case -ERESTARTNOINTR:
471 regs->er0 = regs->orig_er0;
472 regs->pc -= 2;
476 /* set up the stack frame */
477 if (ka->sa.sa_flags & SA_SIGINFO)
478 setup_rt_frame(sig, ka, info, oldset, regs);
479 else
480 setup_frame(sig, ka, oldset, regs);
482 block_sigmask(ka, sig);
486 * Note that 'init' is a special process: it doesn't get signals it doesn't
487 * want to handle. Thus you cannot kill init even with a SIGKILL even by
488 * mistake.
490 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset)
492 siginfo_t info;
493 int signr;
494 struct k_sigaction ka;
497 * We want the common case to go fast, which
498 * is why we may in certain cases get here from
499 * kernel mode. Just return without doing anything
500 * if so.
502 if ((regs->ccr & 0x10))
503 return 1;
505 if (try_to_freeze())
506 goto no_signal;
508 current->thread.esp0 = (unsigned long) regs;
510 if (!oldset)
511 oldset = &current->blocked;
513 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
514 if (signr > 0) {
515 /* Whee! Actually deliver the signal. */
516 handle_signal(signr, &info, &ka, oldset, regs);
517 return 1;
519 no_signal:
520 /* Did we come from a system call? */
521 if (regs->orig_er0 >= 0) {
522 /* Restart the system call - no handlers present */
523 if (regs->er0 == -ERESTARTNOHAND ||
524 regs->er0 == -ERESTARTSYS ||
525 regs->er0 == -ERESTARTNOINTR) {
526 regs->er0 = regs->orig_er0;
527 regs->pc -= 2;
529 if (regs->er0 == -ERESTART_RESTARTBLOCK){
530 regs->er0 = __NR_restart_syscall;
531 regs->pc -= 2;
534 return 0;
537 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
539 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
540 do_signal(regs, NULL);
542 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
543 clear_thread_flag(TIF_NOTIFY_RESUME);
544 tracehook_notify_resume(regs);
545 if (current->replacement_session_keyring)
546 key_replace_session_keyring();