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/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)))
53 * Atomically swap in the new signal mask, and wait for a signal.
56 sys_sigsuspend(int unused1, int unused2, old_sigset_t mask)
59 siginitset(&blocked, mask);
60 return sigsuspend(&blocked);
64 sys_sigaction(int sig, const struct old_sigaction *act,
65 struct old_sigaction *oact)
67 struct k_sigaction new_ka, old_ka;
72 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
73 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
74 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
76 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
77 __get_user(mask, &act->sa_mask);
78 siginitset(&new_ka.sa.sa_mask, mask);
81 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
84 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
85 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
86 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
88 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
89 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
96 sys_sigaltstack(const stack_t *uss, stack_t *uoss)
98 return do_sigaltstack(uss, uoss, rdusp());
103 * Do a signal return; undo the signal stack.
105 * Keep the return code on the stack quadword aligned!
106 * That makes the cache flush below easier.
113 #if defined(CONFIG_CPU_H8S)
118 unsigned char retcode[8];
119 unsigned long extramask[_NSIG_WORDS-1];
120 struct sigcontext sc;
122 } __attribute__((aligned(2),packed));
128 #if defined(CONFIG_CPU_H8S)
133 struct siginfo *pinfo;
135 unsigned char retcode[8];
139 } __attribute__((aligned(2),packed));
142 restore_sigcontext(struct pt_regs *regs, struct sigcontext *usc,
150 /* Always make any pending restarted system calls return -EINTR */
151 current_thread_info()->restart_block.fn = do_no_restart_syscall;
153 #define COPY(r) err |= __get_user(regs->r, &usc->sc_##r) /* restore passed registers */
159 ccr = regs->ccr & 0x10;
164 regs->orig_er0 = -1; /* disable syscall checks */
165 err |= __get_user(usp, &usc->sc_usp);
168 err |= __get_user(er0, &usc->sc_er0);
173 asmlinkage int do_sigreturn(unsigned long __unused,...)
175 struct pt_regs *regs = (struct pt_regs *) (&__unused - 1);
176 unsigned long usp = rdusp();
177 struct sigframe *frame = (struct sigframe *)(usp - 4);
181 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
183 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
185 __copy_from_user(&set.sig[1], &frame->extramask,
186 sizeof(frame->extramask))))
189 sigdelsetmask(&set, ~_BLOCKABLE);
190 set_current_blocked(&set);
192 if (restore_sigcontext(regs, &frame->sc, &er0))
197 force_sig(SIGSEGV, current);
201 asmlinkage int do_rt_sigreturn(unsigned long __unused,...)
203 struct pt_regs *regs = (struct pt_regs *) &__unused;
204 unsigned long usp = rdusp();
205 struct rt_sigframe *frame = (struct rt_sigframe *)(usp - 4);
209 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
211 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
214 sigdelsetmask(&set, ~_BLOCKABLE);
215 set_current_blocked(&set);
217 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &er0))
220 if (do_sigaltstack(&frame->uc.uc_stack, NULL, usp) == -EFAULT)
226 force_sig(SIGSEGV, current);
230 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
235 err |= __put_user(regs->er0, &sc->sc_er0);
236 err |= __put_user(regs->er1, &sc->sc_er1);
237 err |= __put_user(regs->er2, &sc->sc_er2);
238 err |= __put_user(regs->er3, &sc->sc_er3);
239 err |= __put_user(regs->er4, &sc->sc_er4);
240 err |= __put_user(regs->er5, &sc->sc_er5);
241 err |= __put_user(regs->er6, &sc->sc_er6);
242 err |= __put_user(rdusp(), &sc->sc_usp);
243 err |= __put_user(regs->pc, &sc->sc_pc);
244 err |= __put_user(regs->ccr, &sc->sc_ccr);
245 err |= __put_user(mask, &sc->sc_mask);
251 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
255 /* Default to using normal stack. */
258 /* This is the X/Open sanctioned signal stack switching. */
259 if (ka->sa.sa_flags & SA_ONSTACK) {
260 if (!sas_ss_flags(usp))
261 usp = current->sas_ss_sp + current->sas_ss_size;
263 return (void *)((usp - frame_size) & -8UL);
266 static int setup_frame (int sig, struct k_sigaction *ka,
267 sigset_t *set, struct pt_regs *regs)
269 struct sigframe *frame;
274 frame = get_sigframe(ka, regs, sizeof(*frame));
276 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
279 usig = current_thread_info()->exec_domain
280 && current_thread_info()->exec_domain->signal_invmap
282 ? current_thread_info()->exec_domain->signal_invmap[sig]
285 err |= __put_user(usig, &frame->sig);
289 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
293 if (_NSIG_WORDS > 1) {
294 err |= copy_to_user(frame->extramask, &set->sig[1],
295 sizeof(frame->extramask));
300 ret = frame->retcode;
301 if (ka->sa.sa_flags & SA_RESTORER)
302 ret = (unsigned char *)(ka->sa.sa_restorer);
304 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
305 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
306 (unsigned long *)(frame->retcode + 0));
307 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
310 /* Set up to return from userspace. */
311 err |= __put_user(ret, &frame->pretcode);
316 /* Set up registers for signal handler */
317 wrusp ((unsigned long) frame);
318 regs->pc = (unsigned long) ka->sa.sa_handler;
319 regs->er0 = (current_thread_info()->exec_domain
320 && current_thread_info()->exec_domain->signal_invmap
322 ? current_thread_info()->exec_domain->signal_invmap[sig]
324 regs->er1 = (unsigned long)&(frame->sc);
325 regs->er5 = current->mm->start_data; /* GOT base */
330 force_sigsegv(sig, current);
334 static int setup_rt_frame (int sig, struct k_sigaction *ka, siginfo_t *info,
335 sigset_t *set, struct pt_regs *regs)
337 struct rt_sigframe *frame;
342 frame = get_sigframe(ka, regs, sizeof(*frame));
344 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
347 usig = current_thread_info()->exec_domain
348 && current_thread_info()->exec_domain->signal_invmap
350 ? current_thread_info()->exec_domain->signal_invmap[sig]
353 err |= __put_user(usig, &frame->sig);
357 err |= __put_user(&frame->info, &frame->pinfo);
358 err |= __put_user(&frame->uc, &frame->puc);
359 err |= copy_siginfo_to_user(&frame->info, info);
363 /* Create the ucontext. */
364 err |= __put_user(0, &frame->uc.uc_flags);
365 err |= __put_user(0, &frame->uc.uc_link);
366 err |= __put_user((void *)current->sas_ss_sp,
367 &frame->uc.uc_stack.ss_sp);
368 err |= __put_user(sas_ss_flags(rdusp()),
369 &frame->uc.uc_stack.ss_flags);
370 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
371 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
372 err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
376 /* Set up to return from userspace. */
377 ret = frame->retcode;
378 if (ka->sa.sa_flags & SA_RESTORER)
379 ret = (unsigned char *)(ka->sa.sa_restorer);
381 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
382 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
383 (unsigned long *)(frame->retcode + 0));
384 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
386 err |= __put_user(ret, &frame->pretcode);
391 /* Set up registers for signal handler */
392 wrusp ((unsigned long) frame);
393 regs->pc = (unsigned long) ka->sa.sa_handler;
394 regs->er0 = (current_thread_info()->exec_domain
395 && current_thread_info()->exec_domain->signal_invmap
397 ? current_thread_info()->exec_domain->signal_invmap[sig]
399 regs->er1 = (unsigned long)&(frame->info);
400 regs->er2 = (unsigned long)&frame->uc;
401 regs->er5 = current->mm->start_data; /* GOT base */
406 force_sigsegv(sig, current);
411 * OK, we're invoking a handler
414 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
415 sigset_t *oldset, struct pt_regs * regs)
418 /* are we from a system call? */
419 if (regs->orig_er0 >= 0) {
421 case -ERESTART_RESTARTBLOCK:
422 case -ERESTARTNOHAND:
427 if (!(ka->sa.sa_flags & SA_RESTART)) {
432 case -ERESTARTNOINTR:
433 regs->er0 = regs->orig_er0;
438 /* set up the stack frame */
439 if (ka->sa.sa_flags & SA_SIGINFO)
440 ret = setup_rt_frame(sig, ka, info, oldset, regs);
442 ret = setup_frame(sig, ka, oldset, regs);
445 block_sigmask(ka, sig);
446 clear_thread_flag(TIF_RESTORE_SIGMASK);
451 * Note that 'init' is a special process: it doesn't get signals it doesn't
452 * want to handle. Thus you cannot kill init even with a SIGKILL even by
455 statis void do_signal(struct pt_regs *regs)
459 struct k_sigaction ka;
463 * We want the common case to go fast, which
464 * is why we may in certain cases get here from
465 * kernel mode. Just return without doing anything
468 if ((regs->ccr & 0x10))
474 current->thread.esp0 = (unsigned long) regs;
476 if (test_thread_flag(TIF_RESTORE_SIGMASK))
477 oldset = ¤t->saved_sigmask;
479 oldset = ¤t->blocked;
481 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
483 /* Whee! Actually deliver the signal. */
484 handle_signal(signr, &info, &ka, oldset, regs);
488 /* Did we come from a system call? */
489 if (regs->orig_er0 >= 0) {
490 /* Restart the system call - no handlers present */
491 if (regs->er0 == -ERESTARTNOHAND ||
492 regs->er0 == -ERESTARTSYS ||
493 regs->er0 == -ERESTARTNOINTR) {
494 regs->er0 = regs->orig_er0;
497 if (regs->er0 == -ERESTART_RESTARTBLOCK){
498 regs->er0 = __NR_restart_syscall;
503 /* If there's no signal to deliver, we just restore the saved mask. */
504 if (test_and_clear_thread_flag(TIF_RESTORE_SIGMASK))
505 set_current_blocked(¤t->saved_sigmask);
508 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
510 if (thread_info_flags & _TIF_SIGPENDING)
513 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
514 clear_thread_flag(TIF_NOTIFY_RESUME);
515 tracehook_notify_resume(regs);
516 if (current->replacement_session_keyring)
517 key_replace_session_keyring();