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)))
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
;
63 spin_lock_irq(¤t
->sighand
->siglock
);
64 saveset
= current
->blocked
;
65 siginitset(¤t
->blocked
, mask
);
67 spin_unlock_irq(¤t
->sighand
->siglock
);
71 current
->state
= TASK_INTERRUPTIBLE
;
73 if (do_signal(regs
, &saveset
))
79 do_rt_sigsuspend(struct pt_regs
*regs
)
81 sigset_t
*unewset
= (sigset_t
*)regs
->er1
;
82 size_t sigsetsize
= (size_t)regs
->er2
;
83 sigset_t saveset
, newset
;
85 /* XXX: Don't preclude handling different sized sigset_t's. */
86 if (sigsetsize
!= sizeof(sigset_t
))
89 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
91 sigdelsetmask(&newset
, ~_BLOCKABLE
);
93 spin_lock_irq(¤t
->sighand
->siglock
);
94 saveset
= current
->blocked
;
95 current
->blocked
= newset
;
97 spin_unlock_irq(¤t
->sighand
->siglock
);
101 current
->state
= TASK_INTERRUPTIBLE
;
103 if (do_signal(regs
, &saveset
))
109 sys_sigaction(int sig
, const struct old_sigaction
*act
,
110 struct old_sigaction
*oact
)
112 struct k_sigaction new_ka
, old_ka
;
117 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
118 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
119 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
121 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
122 __get_user(mask
, &act
->sa_mask
);
123 siginitset(&new_ka
.sa
.sa_mask
, mask
);
126 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
129 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
130 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
131 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
133 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
134 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
141 sys_sigaltstack(const stack_t
*uss
, stack_t
*uoss
)
143 return do_sigaltstack(uss
, uoss
, rdusp());
148 * Do a signal return; undo the signal stack.
150 * Keep the return code on the stack quadword aligned!
151 * That makes the cache flush below easier.
158 #if defined(CONFIG_CPU_H8S)
163 unsigned char retcode
[8];
164 unsigned long extramask
[_NSIG_WORDS
-1];
165 struct sigcontext sc
;
167 } __attribute__((aligned(2),packed
));
173 #if defined(CONFIG_CPU_H8S)
178 struct siginfo
*pinfo
;
180 unsigned char retcode
[8];
184 } __attribute__((aligned(2),packed
));
187 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*usc
,
195 /* Always make any pending restarted system calls return -EINTR */
196 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
198 #define COPY(r) err |= __get_user(regs->r, &usc->sc_##r) /* restore passed registers */
204 ccr
= regs
->ccr
& 0x10;
209 regs
->orig_er0
= -1; /* disable syscall checks */
210 err
|= __get_user(usp
, &usc
->sc_usp
);
213 err
|= __get_user(er0
, &usc
->sc_er0
);
218 asmlinkage
int do_sigreturn(unsigned long __unused
,...)
220 struct pt_regs
*regs
= (struct pt_regs
*) (&__unused
- 1);
221 unsigned long usp
= rdusp();
222 struct sigframe
*frame
= (struct sigframe
*)(usp
- 4);
226 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
228 if (__get_user(set
.sig
[0], &frame
->sc
.sc_mask
) ||
230 __copy_from_user(&set
.sig
[1], &frame
->extramask
,
231 sizeof(frame
->extramask
))))
234 sigdelsetmask(&set
, ~_BLOCKABLE
);
235 spin_lock_irq(¤t
->sighand
->siglock
);
236 current
->blocked
= set
;
238 spin_unlock_irq(¤t
->sighand
->siglock
);
240 if (restore_sigcontext(regs
, &frame
->sc
, &er0
))
245 force_sig(SIGSEGV
, current
);
249 asmlinkage
int do_rt_sigreturn(unsigned long __unused
,...)
251 struct pt_regs
*regs
= (struct pt_regs
*) &__unused
;
252 unsigned long usp
= rdusp();
253 struct rt_sigframe
*frame
= (struct rt_sigframe
*)(usp
- 4);
257 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
259 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
262 sigdelsetmask(&set
, ~_BLOCKABLE
);
263 spin_unlock_irq(¤t
->sighand
->siglock
);
264 current
->blocked
= set
;
266 spin_lock_irq(¤t
->sighand
->siglock
);
268 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &er0
))
271 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, usp
) == -EFAULT
)
277 force_sig(SIGSEGV
, current
);
281 static int setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
286 err
|= __put_user(regs
->er0
, &sc
->sc_er0
);
287 err
|= __put_user(regs
->er1
, &sc
->sc_er1
);
288 err
|= __put_user(regs
->er2
, &sc
->sc_er2
);
289 err
|= __put_user(regs
->er3
, &sc
->sc_er3
);
290 err
|= __put_user(regs
->er4
, &sc
->sc_er4
);
291 err
|= __put_user(regs
->er5
, &sc
->sc_er5
);
292 err
|= __put_user(regs
->er6
, &sc
->sc_er6
);
293 err
|= __put_user(rdusp(), &sc
->sc_usp
);
294 err
|= __put_user(regs
->pc
, &sc
->sc_pc
);
295 err
|= __put_user(regs
->ccr
, &sc
->sc_ccr
);
296 err
|= __put_user(mask
, &sc
->sc_mask
);
302 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, size_t frame_size
)
306 /* Default to using normal stack. */
309 /* This is the X/Open sanctioned signal stack switching. */
310 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
311 if (!sas_ss_flags(usp
))
312 usp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
314 return (void *)((usp
- frame_size
) & -8UL);
317 static void setup_frame (int sig
, struct k_sigaction
*ka
,
318 sigset_t
*set
, struct pt_regs
*regs
)
320 struct sigframe
*frame
;
325 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
327 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
330 usig
= current_thread_info()->exec_domain
331 && current_thread_info()->exec_domain
->signal_invmap
333 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
336 err
|= __put_user(usig
, &frame
->sig
);
340 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
344 if (_NSIG_WORDS
> 1) {
345 err
|= copy_to_user(frame
->extramask
, &set
->sig
[1],
346 sizeof(frame
->extramask
));
351 ret
= frame
->retcode
;
352 if (ka
->sa
.sa_flags
& SA_RESTORER
)
353 ret
= (unsigned char *)(ka
->sa
.sa_restorer
);
355 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
356 err
|= __put_user(0x1a80f800 + (__NR_sigreturn
& 0xff),
357 (unsigned long *)(frame
->retcode
+ 0));
358 err
|= __put_user(0x5700, (unsigned short *)(frame
->retcode
+ 4));
361 /* Set up to return from userspace. */
362 err
|= __put_user(ret
, &frame
->pretcode
);
367 /* Set up registers for signal handler */
368 wrusp ((unsigned long) frame
);
369 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
370 regs
->er0
= (current_thread_info()->exec_domain
371 && current_thread_info()->exec_domain
->signal_invmap
373 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
375 regs
->er1
= (unsigned long)&(frame
->sc
);
376 regs
->er5
= current
->mm
->start_data
; /* GOT base */
381 force_sigsegv(sig
, current
);
384 static void setup_rt_frame (int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
385 sigset_t
*set
, struct pt_regs
*regs
)
387 struct rt_sigframe
*frame
;
392 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
394 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
397 usig
= current_thread_info()->exec_domain
398 && current_thread_info()->exec_domain
->signal_invmap
400 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
403 err
|= __put_user(usig
, &frame
->sig
);
407 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
408 err
|= __put_user(&frame
->uc
, &frame
->puc
);
409 err
|= copy_siginfo_to_user(&frame
->info
, info
);
413 /* Create the ucontext. */
414 err
|= __put_user(0, &frame
->uc
.uc_flags
);
415 err
|= __put_user(0, &frame
->uc
.uc_link
);
416 err
|= __put_user((void *)current
->sas_ss_sp
,
417 &frame
->uc
.uc_stack
.ss_sp
);
418 err
|= __put_user(sas_ss_flags(rdusp()),
419 &frame
->uc
.uc_stack
.ss_flags
);
420 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
421 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, set
->sig
[0]);
422 err
|= copy_to_user (&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
426 /* Set up to return from userspace. */
427 ret
= frame
->retcode
;
428 if (ka
->sa
.sa_flags
& SA_RESTORER
)
429 ret
= (unsigned char *)(ka
->sa
.sa_restorer
);
431 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
432 err
|= __put_user(0x1a80f800 + (__NR_sigreturn
& 0xff),
433 (unsigned long *)(frame
->retcode
+ 0));
434 err
|= __put_user(0x5700, (unsigned short *)(frame
->retcode
+ 4));
436 err
|= __put_user(ret
, &frame
->pretcode
);
441 /* Set up registers for signal handler */
442 wrusp ((unsigned long) frame
);
443 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
444 regs
->er0
= (current_thread_info()->exec_domain
445 && current_thread_info()->exec_domain
->signal_invmap
447 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
449 regs
->er1
= (unsigned long)&(frame
->info
);
450 regs
->er2
= (unsigned long)&frame
->uc
;
451 regs
->er5
= current
->mm
->start_data
; /* GOT base */
456 force_sigsegv(sig
, current
);
460 * OK, we're invoking a handler
463 handle_signal(unsigned long sig
, siginfo_t
*info
, struct k_sigaction
*ka
,
464 sigset_t
*oldset
, struct pt_regs
* regs
)
466 /* are we from a system call? */
467 if (regs
->orig_er0
>= 0) {
469 case -ERESTART_RESTARTBLOCK
:
470 case -ERESTARTNOHAND
:
475 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
480 case -ERESTARTNOINTR
:
481 regs
->er0
= regs
->orig_er0
;
486 /* set up the stack frame */
487 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
488 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
490 setup_frame(sig
, ka
, oldset
, regs
);
492 spin_lock_irq(¤t
->sighand
->siglock
);
493 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
494 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
495 sigaddset(¤t
->blocked
,sig
);
497 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))
523 current
->thread
.esp0
= (unsigned long) regs
;
526 oldset
= ¤t
->blocked
;
528 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
530 /* Whee! Actually deliver the signal. */
531 handle_signal(signr
, &info
, &ka
, oldset
, regs
);
535 /* Did we come from a system call? */
536 if (regs
->orig_er0
>= 0) {
537 /* Restart the system call - no handlers present */
538 if (regs
->er0
== -ERESTARTNOHAND
||
539 regs
->er0
== -ERESTARTSYS
||
540 regs
->er0
== -ERESTARTNOINTR
) {
541 regs
->er0
= regs
->orig_er0
;
544 if (regs
->er0
== -ERESTART_RESTARTBLOCK
){
545 regs
->er0
= __NR_restart_syscall
;
552 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, u32 thread_info_flags
)
554 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_RESTORE_SIGMASK
))
555 do_signal(regs
, NULL
);
557 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
558 clear_thread_flag(TIF_NOTIFY_RESUME
);
559 tracehook_notify_resume(regs
);
560 if (current
->replacement_session_keyring
)
561 key_replace_session_keyring();