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>
42 #include <asm/setup.h>
43 #include <asm/uaccess.h>
44 #include <asm/pgtable.h>
45 #include <asm/traps.h>
46 #include <asm/ucontext.h>
48 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
50 asmlinkage
int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
);
53 * Atomically swap in the new signal mask, and wait for a signal.
55 asmlinkage
int do_sigsuspend(struct pt_regs
*regs
)
57 old_sigset_t mask
= regs
->er3
;
61 spin_lock_irq(¤t
->sighand
->siglock
);
62 saveset
= current
->blocked
;
63 siginitset(¤t
->blocked
, mask
);
65 spin_unlock_irq(¤t
->sighand
->siglock
);
69 current
->state
= TASK_INTERRUPTIBLE
;
71 if (do_signal(&saveset
, regs
))
77 do_rt_sigsuspend(struct pt_regs
*regs
)
79 sigset_t
*unewset
= (sigset_t
*)regs
->er1
;
80 size_t sigsetsize
= (size_t)regs
->er2
;
81 sigset_t saveset
, newset
;
83 /* XXX: Don't preclude handling different sized sigset_t's. */
84 if (sigsetsize
!= sizeof(sigset_t
))
87 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
89 sigdelsetmask(&newset
, ~_BLOCKABLE
);
91 spin_lock_irq(¤t
->sighand
->siglock
);
92 saveset
= current
->blocked
;
93 current
->blocked
= newset
;
95 spin_unlock_irq(¤t
->sighand
->siglock
);
99 current
->state
= TASK_INTERRUPTIBLE
;
101 if (do_signal(&saveset
, regs
))
107 sys_sigaction(int sig
, const struct old_sigaction
*act
,
108 struct old_sigaction
*oact
)
110 struct k_sigaction new_ka
, old_ka
;
115 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
116 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
117 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
119 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
120 __get_user(mask
, &act
->sa_mask
);
121 siginitset(&new_ka
.sa
.sa_mask
, mask
);
124 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
127 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
128 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
129 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
131 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
132 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
139 sys_sigaltstack(const stack_t
*uss
, stack_t
*uoss
)
141 return do_sigaltstack(uss
, uoss
, rdusp());
146 * Do a signal return; undo the signal stack.
148 * Keep the return code on the stack quadword aligned!
149 * That makes the cache flush below easier.
156 #if defined(CONFIG_CPU_H8S)
161 unsigned char retcode
[8];
162 unsigned long extramask
[_NSIG_WORDS
-1];
163 struct sigcontext sc
;
164 } __attribute__((aligned(2),packed
));
170 #if defined(CONFIG_CPU_H8S)
175 unsigned char retcode
[8];
178 } __attribute__((aligned(2),packed
));
181 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*usc
, void *fp
,
184 struct sigcontext context
;
187 /* get previous context */
188 if (copy_from_user(&context
, usc
, sizeof(context
)))
191 /* restore passed registers */
192 regs
->er1
= context
.sc_er1
;
193 regs
->er2
= context
.sc_er2
;
194 regs
->er3
= context
.sc_er3
;
195 regs
->er5
= context
.sc_er5
;
196 regs
->ccr
= (regs
->ccr
& 0x10)|(context
.sc_ccr
& 0xef);
197 regs
->pc
= context
.sc_pc
;
198 regs
->orig_er0
= -1; /* disable syscall checks */
199 wrusp(context
.sc_usp
);
201 *pd0
= context
.sc_er0
;
209 rt_restore_ucontext(struct pt_regs
*regs
, struct ucontext
*uc
, int *pd0
)
212 greg_t
*gregs
= uc
->uc_mcontext
.gregs
;
216 err
= __get_user(temp
, &uc
->uc_mcontext
.version
);
217 if (temp
!= MCONTEXT_VERSION
)
219 /* restore passed registers */
220 err
|= __get_user(regs
->er0
, &gregs
[0]);
221 err
|= __get_user(regs
->er1
, &gregs
[1]);
222 err
|= __get_user(regs
->er2
, &gregs
[2]);
223 err
|= __get_user(regs
->er3
, &gregs
[3]);
224 err
|= __get_user(regs
->er4
, &gregs
[4]);
225 err
|= __get_user(regs
->er5
, &gregs
[5]);
226 err
|= __get_user(regs
->er6
, &gregs
[6]);
227 err
|= __get_user(usp
, &gregs
[7]);
229 err
|= __get_user(regs
->pc
, &gregs
[8]);
230 err
|= __get_user(temp
, &gregs
[9]);
231 regs
->ccr
= (regs
->ccr
& 0x10) | (temp
& 0xef);
232 regs
->orig_er0
= -1; /* disable syscall checks */
234 if (do_sigaltstack(&uc
->uc_stack
, NULL
, usp
) == -EFAULT
)
244 asmlinkage
int do_sigreturn(unsigned long __unused
,...)
246 struct pt_regs
*regs
= (struct pt_regs
*) (&__unused
- 1);
247 unsigned long usp
= rdusp();
248 struct sigframe
*frame
= (struct sigframe
*)(usp
- 4);
252 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
254 if (__get_user(set
.sig
[0], &frame
->sc
.sc_mask
) ||
256 __copy_from_user(&set
.sig
[1], &frame
->extramask
,
257 sizeof(frame
->extramask
))))
260 sigdelsetmask(&set
, ~_BLOCKABLE
);
261 spin_lock_irq(¤t
->sighand
->siglock
);
262 current
->blocked
= set
;
264 spin_unlock_irq(¤t
->sighand
->siglock
);
266 if (restore_sigcontext(regs
, &frame
->sc
, frame
+ 1, &er0
))
271 force_sig(SIGSEGV
, current
);
275 asmlinkage
int do_rt_sigreturn(unsigned long __unused
,...)
277 struct pt_regs
*regs
= (struct pt_regs
*) &__unused
;
278 unsigned long usp
= rdusp();
279 struct rt_sigframe
*frame
= (struct rt_sigframe
*)(usp
- 4);
283 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
285 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
288 sigdelsetmask(&set
, ~_BLOCKABLE
);
289 spin_unlock_irq(¤t
->sighand
->siglock
);
290 current
->blocked
= set
;
292 spin_lock_irq(¤t
->sighand
->siglock
);
294 if (rt_restore_ucontext(regs
, &frame
->uc
, &er0
))
299 force_sig(SIGSEGV
, current
);
303 static void setup_sigcontext(struct sigcontext
*sc
, struct pt_regs
*regs
,
307 sc
->sc_usp
= rdusp();
308 sc
->sc_er0
= regs
->er0
;
309 sc
->sc_er1
= regs
->er1
;
310 sc
->sc_er2
= regs
->er2
;
311 sc
->sc_er3
= regs
->er3
;
312 sc
->sc_er5
= regs
->er5
;
313 sc
->sc_ccr
= regs
->ccr
;
314 sc
->sc_pc
= regs
->pc
;
317 static inline int rt_setup_ucontext(struct ucontext
*uc
, struct pt_regs
*regs
)
319 greg_t
*gregs
= uc
->uc_mcontext
.gregs
;
322 err
|= __put_user(MCONTEXT_VERSION
, &uc
->uc_mcontext
.version
);
323 err
|= __put_user(regs
->er0
, &gregs
[0]);
324 err
|= __put_user(regs
->er1
, &gregs
[1]);
325 err
|= __put_user(regs
->er2
, &gregs
[2]);
326 err
|= __put_user(regs
->er3
, &gregs
[3]);
327 err
|= __put_user(regs
->er4
, &gregs
[4]);
328 err
|= __put_user(regs
->er5
, &gregs
[5]);
329 err
|= __put_user(regs
->er6
, &gregs
[6]);
330 err
|= __put_user(rdusp(), &gregs
[7]);
331 err
|= __put_user(regs
->pc
, &gregs
[8]);
332 err
|= __put_user(regs
->ccr
, &gregs
[9]);
337 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, size_t frame_size
)
341 /* Default to using normal stack. */
344 /* This is the X/Open sanctioned signal stack switching. */
345 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
346 if (!on_sig_stack(usp
))
347 usp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
349 return (void *)((usp
- frame_size
) & -8UL);
352 static void setup_frame (int sig
, struct k_sigaction
*ka
,
353 sigset_t
*set
, struct pt_regs
*regs
)
355 struct sigframe
*frame
;
356 struct sigcontext context
;
359 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
362 err
|= copy_to_user(frame
->extramask
, &set
->sig
[1],
363 sizeof(frame
->extramask
));
365 setup_sigcontext(&context
, regs
, set
->sig
[0]);
366 err
|= copy_to_user (&frame
->sc
, &context
, sizeof(context
));
368 /* Set up to return from userspace. */
369 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
371 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
372 err
!= __put_user(0x1a80f800 + (__NR_sigreturn
& 0xff),
373 (unsigned long *)(frame
->retcode
+ 0));
374 err
|= __put_user(0x5700, (unsigned short *)(frame
->retcode
+ 4));
380 /* Set up registers for signal handler */
381 wrusp ((unsigned long) frame
);
382 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
383 regs
->er0
= (current_thread_info()->exec_domain
384 && current_thread_info()->exec_domain
->signal_invmap
386 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
388 regs
->er1
= (unsigned long)&(frame
->sc
);
389 regs
->er5
= current
->mm
->start_data
; /* GOT base */
394 force_sigsegv(sig
, current
);
397 static void setup_rt_frame (int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
398 sigset_t
*set
, struct pt_regs
*regs
)
400 struct rt_sigframe
*frame
;
403 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
405 err
|= copy_siginfo_to_user(&frame
->info
, info
);
407 /* Create the ucontext. */
408 err
|= __put_user(0, &frame
->uc
.uc_flags
);
409 err
|= __put_user(0, &frame
->uc
.uc_link
);
410 err
|= __put_user((void *)current
->sas_ss_sp
,
411 &frame
->uc
.uc_stack
.ss_sp
);
412 err
|= __put_user(sas_ss_flags(rdusp()),
413 &frame
->uc
.uc_stack
.ss_flags
);
414 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
415 err
|= rt_setup_ucontext(&frame
->uc
, regs
);
416 err
|= copy_to_user (&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
418 /* Set up to return from userspace. */
419 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
421 /* sub.l er0,er0; mov.b #__NR_rt_sigreturn,r0l; trapa #0 */
422 err
!= __put_user(0x1a80f800 + (__NR_rt_sigreturn
& 0xff),
423 (long *)(frame
->retcode
+ 0));
424 err
|= __put_user(0x5700, (short *)(frame
->retcode
+ 4));
429 /* Set up registers for signal handler */
430 wrusp ((unsigned long) frame
);
431 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
432 regs
->er0
= (current_thread_info()->exec_domain
433 && current_thread_info()->exec_domain
->signal_invmap
435 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
437 regs
->er1
= (unsigned long)&(frame
->info
);
438 regs
->er2
= (unsigned long)&frame
->uc
;
439 regs
->er5
= current
->mm
->start_data
; /* GOT base */
444 force_sigsegv(sig
, current
);
448 handle_restart(struct pt_regs
*regs
, struct k_sigaction
*ka
, int has_handler
)
451 case -ERESTARTNOHAND
:
458 if (has_handler
&& !(ka
->sa
.sa_flags
& SA_RESTART
)) {
463 case -ERESTARTNOINTR
:
465 regs
->er0
= regs
->orig_er0
;
472 * OK, we're invoking a handler
475 handle_signal(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
476 sigset_t
*oldset
, struct pt_regs
*regs
)
478 /* are we from a system call? */
479 if (regs
->orig_er0
>= 0)
480 /* If so, check system call restarting.. */
481 handle_restart(regs
, ka
, 1);
483 /* set up the stack frame */
484 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
485 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
487 setup_frame(sig
, ka
, oldset
, regs
);
489 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
490 ka
->sa
.sa_handler
= SIG_DFL
;
492 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
493 spin_lock_irq(¤t
->sighand
->siglock
);
494 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
495 sigaddset(¤t
->blocked
,sig
);
497 spin_unlock_irq(¤t
->sighand
->siglock
);
502 * Note that 'init' is a special process: it doesn't get signals it doesn't
503 * want to handle. Thus you cannot kill init even with a SIGKILL even by
506 * Note that we go through the signals twice: once to check the signals
507 * that the kernel can handle, and then we build all the user-level signal
508 * handling stack-frames in one go after that.
510 asmlinkage
int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
)
513 struct k_sigaction
*ka
;
515 current
->thread
.esp0
= (unsigned long) regs
;
518 oldset
= ¤t
->blocked
;
523 signr
= get_signal_to_deliver(&info
, regs
, NULL
);
528 if ((current
->ptrace
& PT_PTRACED
) && signr
!= SIGKILL
) {
529 current
->exit_code
= signr
;
530 current
->state
= TASK_STOPPED
;
532 /* Did we come from a system call? */
533 if (regs
->orig_er0
>= 0) {
534 /* Restart the system call the same way as
535 if the process were not traced. */
536 struct k_sigaction
*ka
=
537 ¤t
->sighand
->action
[signr
-1];
539 (ka
->sa
.sa_handler
!= SIG_IGN
&&
540 ka
->sa
.sa_handler
!= SIG_DFL
);
541 handle_restart(regs
, ka
, has_handler
);
543 notify_parent(current
, SIGCHLD
);
546 /* We're back. Did the debugger cancel the sig? */
547 if (!(signr
= current
->exit_code
)) {
551 current
->exit_code
= 0;
553 /* The debugger continued. Ignore SIGSTOP. */
554 if (signr
== SIGSTOP
)
557 /* Update the siginfo structure. Is this good? */
558 if (signr
!= info
.si_signo
) {
559 info
.si_signo
= signr
;
561 info
.si_code
= SI_USER
;
562 info
.si_pid
= current
->parent
->pid
;
563 info
.si_uid
= current
->parent
->uid
;
566 /* If the (new) signal is now blocked, requeue it. */
567 if (sigismember(¤t
->blocked
, signr
)) {
568 send_sig_info(signr
, &info
, current
);
573 ka
= ¤t
->sighand
->action
[signr
-1];
574 if (ka
->sa
.sa_handler
== SIG_IGN
) {
575 if (signr
!= SIGCHLD
)
577 /* Check for SIGCHLD: it's special. */
578 while (sys_wait4(-1, NULL
, WNOHANG
, NULL
) > 0)
583 if (ka
->sa
.sa_handler
== SIG_DFL
) {
584 int exit_code
= signr
;
586 if (current
->pid
== 1)
590 case SIGCONT
: case SIGCHLD
:
591 case SIGWINCH
: case SIGURG
:
594 case SIGTSTP
: case SIGTTIN
: case SIGTTOU
:
595 if (is_orphaned_pgrp(process_group(current
)))
600 struct sighand_struct
*sig
;
601 current
->state
= TASK_STOPPED
;
602 current
->exit_code
= signr
;
603 sig
= current
->parent
->sighand
;
604 if (sig
&& !(sig
->action
[SIGCHLD
-1].sa
.sa_flags
606 notify_parent(current
, SIGCHLD
);
611 case SIGQUIT
: case SIGILL
: case SIGTRAP
:
612 case SIGIOT
: case SIGFPE
: case SIGSEGV
:
613 case SIGBUS
: case SIGSYS
: case SIGXCPU
: case SIGXFSZ
:
614 if (do_coredump(signr
, exit_code
, regs
))
619 sigaddset(¤t
->pending
.signal
, signr
);
621 current
->flags
|= PF_SIGNALED
;
627 /* Whee! Actually deliver the signal. */
628 handle_signal(signr
, ka
, &info
, oldset
, regs
);
632 /* Did we come from a system call? */
633 if (regs
->orig_er0
>= 0)
634 /* Restart the system call - no handlers present */
635 handle_restart(regs
, NULL
, 0);