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
12 * uClinux H8/300 support by Yoshinori Sato <ysato@users.sourceforge.jp>
13 * and David McCullough <davidm@snapgear.com>
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
27 #include <linux/sched.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/suspend.h>
43 #include <asm/setup.h>
44 #include <asm/uaccess.h>
45 #include <asm/pgtable.h>
46 #include <asm/traps.h>
47 #include <asm/ucontext.h>
49 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
51 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
54 * Atomically swap in the new signal mask, and wait for a signal.
56 asmlinkage int do_sigsuspend(struct pt_regs *regs)
58 old_sigset_t mask = regs->er3;
62 spin_lock_irq(¤t->sighand->siglock);
63 saveset = current->blocked;
64 siginitset(¤t->blocked, mask);
66 spin_unlock_irq(¤t->sighand->siglock);
70 current->state = TASK_INTERRUPTIBLE;
72 if (do_signal(regs, &saveset))
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))
88 if (copy_from_user(&newset, unewset, sizeof(newset)))
90 sigdelsetmask(&newset, ~_BLOCKABLE);
92 spin_lock_irq(¤t->sighand->siglock);
93 saveset = current->blocked;
94 current->blocked = newset;
96 spin_unlock_irq(¤t->sighand->siglock);
100 current->state = TASK_INTERRUPTIBLE;
102 if (do_signal(regs, &saveset))
108 sys_sigaction(int sig, const struct old_sigaction *act,
109 struct old_sigaction *oact)
111 struct k_sigaction new_ka, old_ka;
116 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
117 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
118 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
120 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
121 __get_user(mask, &act->sa_mask);
122 siginitset(&new_ka.sa.sa_mask, mask);
125 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
128 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
129 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
130 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
132 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
133 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
140 sys_sigaltstack(const stack_t *uss, stack_t *uoss)
142 return do_sigaltstack(uss, uoss, rdusp());
147 * Do a signal return; undo the signal stack.
149 * Keep the return code on the stack quadword aligned!
150 * That makes the cache flush below easier.
157 #if defined(CONFIG_CPU_H8S)
162 unsigned char retcode[8];
163 unsigned long extramask[_NSIG_WORDS-1];
164 struct sigcontext sc;
166 } __attribute__((aligned(2),packed));
172 #if defined(CONFIG_CPU_H8S)
177 struct siginfo *pinfo;
179 unsigned char retcode[8];
183 } __attribute__((aligned(2),packed));
186 restore_sigcontext(struct pt_regs *regs, struct sigcontext *usc,
194 /* Always make any pending restarted system calls return -EINTR */
195 current_thread_info()->restart_block.fn = do_no_restart_syscall;
197 #define COPY(r) err |= __get_user(regs->r, &usc->sc_##r) /* restore passed registers */
203 ccr = regs->ccr & 0x10;
208 regs->orig_er0 = -1; /* disable syscall checks */
209 err |= __get_user(usp, &usc->sc_usp);
212 err |= __get_user(er0, &usc->sc_er0);
217 asmlinkage int do_sigreturn(unsigned long __unused,...)
219 struct pt_regs *regs = (struct pt_regs *) (&__unused - 1);
220 unsigned long usp = rdusp();
221 struct sigframe *frame = (struct sigframe *)(usp - 4);
225 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
227 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
229 __copy_from_user(&set.sig[1], &frame->extramask,
230 sizeof(frame->extramask))))
233 sigdelsetmask(&set, ~_BLOCKABLE);
234 spin_lock_irq(¤t->sighand->siglock);
235 current->blocked = set;
237 spin_unlock_irq(¤t->sighand->siglock);
239 if (restore_sigcontext(regs, &frame->sc, &er0))
244 force_sig(SIGSEGV, current);
248 asmlinkage int do_rt_sigreturn(unsigned long __unused,...)
250 struct pt_regs *regs = (struct pt_regs *) &__unused;
251 unsigned long usp = rdusp();
252 struct rt_sigframe *frame = (struct rt_sigframe *)(usp - 4);
256 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
258 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
261 sigdelsetmask(&set, ~_BLOCKABLE);
262 spin_unlock_irq(¤t->sighand->siglock);
263 current->blocked = set;
265 spin_lock_irq(¤t->sighand->siglock);
267 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &er0))
270 if (do_sigaltstack(&frame->uc.uc_stack, NULL, usp) == -EFAULT)
276 force_sig(SIGSEGV, current);
280 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
285 err |= __put_user(regs->er0, &sc->sc_er0);
286 err |= __put_user(regs->er1, &sc->sc_er1);
287 err |= __put_user(regs->er2, &sc->sc_er2);
288 err |= __put_user(regs->er3, &sc->sc_er3);
289 err |= __put_user(regs->er4, &sc->sc_er4);
290 err |= __put_user(regs->er5, &sc->sc_er5);
291 err |= __put_user(regs->er6, &sc->sc_er6);
292 err |= __put_user(rdusp(), &sc->sc_usp);
293 err |= __put_user(regs->pc, &sc->sc_pc);
294 err |= __put_user(regs->ccr, &sc->sc_ccr);
295 err |= __put_user(mask, &sc->sc_mask);
301 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
305 /* Default to using normal stack. */
308 /* This is the X/Open sanctioned signal stack switching. */
309 if (ka->sa.sa_flags & SA_ONSTACK) {
310 if (!on_sig_stack(usp))
311 usp = current->sas_ss_sp + current->sas_ss_size;
313 return (void *)((usp - frame_size) & -8UL);
316 static void setup_frame (int sig, struct k_sigaction *ka,
317 sigset_t *set, struct pt_regs *regs)
319 struct sigframe *frame;
324 frame = get_sigframe(ka, regs, sizeof(*frame));
326 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
329 usig = current_thread_info()->exec_domain
330 && current_thread_info()->exec_domain->signal_invmap
332 ? current_thread_info()->exec_domain->signal_invmap[sig]
335 err |= __put_user(usig, &frame->sig);
339 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
343 if (_NSIG_WORDS > 1) {
344 err |= copy_to_user(frame->extramask, &set->sig[1],
345 sizeof(frame->extramask));
350 ret = frame->retcode;
351 if (ka->sa.sa_flags & SA_RESTORER)
352 ret = (unsigned char *)(ka->sa.sa_restorer);
354 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
355 err != __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
356 (unsigned long *)(frame->retcode + 0));
357 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
360 /* Set up to return from userspace. */
361 err |= __put_user(ret, &frame->pretcode);
366 /* Set up registers for signal handler */
367 wrusp ((unsigned long) frame);
368 regs->pc = (unsigned long) ka->sa.sa_handler;
369 regs->er0 = (current_thread_info()->exec_domain
370 && current_thread_info()->exec_domain->signal_invmap
372 ? current_thread_info()->exec_domain->signal_invmap[sig]
374 regs->er1 = (unsigned long)&(frame->sc);
375 regs->er5 = current->mm->start_data; /* GOT base */
380 force_sigsegv(sig, current);
383 static void setup_rt_frame (int sig, struct k_sigaction *ka, siginfo_t *info,
384 sigset_t *set, struct pt_regs *regs)
386 struct rt_sigframe *frame;
391 frame = get_sigframe(ka, regs, sizeof(*frame));
393 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
396 usig = current_thread_info()->exec_domain
397 && current_thread_info()->exec_domain->signal_invmap
399 ? current_thread_info()->exec_domain->signal_invmap[sig]
402 err |= __put_user(usig, &frame->sig);
406 err |= __put_user(&frame->info, &frame->pinfo);
407 err |= __put_user(&frame->uc, &frame->puc);
408 err |= copy_siginfo_to_user(&frame->info, info);
412 /* Create the ucontext. */
413 err |= __put_user(0, &frame->uc.uc_flags);
414 err |= __put_user(0, &frame->uc.uc_link);
415 err |= __put_user((void *)current->sas_ss_sp,
416 &frame->uc.uc_stack.ss_sp);
417 err |= __put_user(sas_ss_flags(rdusp()),
418 &frame->uc.uc_stack.ss_flags);
419 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
420 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
421 err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
425 /* Set up to return from userspace. */
426 ret = frame->retcode;
427 if (ka->sa.sa_flags & SA_RESTORER)
428 ret = (unsigned char *)(ka->sa.sa_restorer);
430 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
431 err != __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
432 (unsigned long *)(frame->retcode + 0));
433 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
435 err |= __put_user(ret, &frame->pretcode);
440 /* Set up registers for signal handler */
441 wrusp ((unsigned long) frame);
442 regs->pc = (unsigned long) ka->sa.sa_handler;
443 regs->er0 = (current_thread_info()->exec_domain
444 && current_thread_info()->exec_domain->signal_invmap
446 ? current_thread_info()->exec_domain->signal_invmap[sig]
448 regs->er1 = (unsigned long)&(frame->info);
449 regs->er2 = (unsigned long)&frame->uc;
450 regs->er5 = current->mm->start_data; /* GOT base */
455 force_sigsegv(sig, current);
459 * OK, we're invoking a handler
462 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
463 sigset_t *oldset, struct pt_regs * regs)
465 /* are we from a system call? */
466 if (regs->orig_er0 >= 0) {
468 case -ERESTART_RESTARTBLOCK:
469 case -ERESTARTNOHAND:
474 if (!(ka->sa.sa_flags & SA_RESTART)) {
479 case -ERESTARTNOINTR:
480 regs->er0 = regs->orig_er0;
485 /* set up the stack frame */
486 if (ka->sa.sa_flags & SA_SIGINFO)
487 setup_rt_frame(sig, ka, info, oldset, regs);
489 setup_frame(sig, ka, oldset, regs);
491 if (!(ka->sa.sa_flags & SA_NODEFER)) {
492 spin_lock_irq(¤t->sighand->siglock);
493 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
494 sigaddset(¤t->blocked,sig);
496 spin_unlock_irq(¤t->sighand->siglock);
501 * Note that 'init' is a special process: it doesn't get signals it doesn't
502 * want to handle. Thus you cannot kill init even with a SIGKILL even by
505 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset)
509 struct k_sigaction ka;
512 * We want the common case to go fast, which
513 * is why we may in certain cases get here from
514 * kernel mode. Just return without doing anything
517 if ((regs->ccr & 0x10))
520 if (current->flags & PF_FREEZE) {
525 current->thread.esp0 = (unsigned long) regs;
528 oldset = ¤t->blocked;
530 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
532 /* Whee! Actually deliver the signal. */
533 handle_signal(signr, &info, &ka, oldset, regs);
537 /* Did we come from a system call? */
538 if (regs->orig_er0 >= 0) {
539 /* Restart the system call - no handlers present */
540 if (regs->er0 == -ERESTARTNOHAND ||
541 regs->er0 == -ERESTARTSYS ||
542 regs->er0 == -ERESTARTNOINTR) {
543 regs->er0 = regs->orig_er0;
546 if (regs->er0 == -ERESTART_RESTARTBLOCK){
547 regs->er0 = __NR_restart_syscall;