Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / arch / h8300 / kernel / signal.c
blobaf842c369d24301e4b38861d29ffae2df0f863e7
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;
62 mask &= _BLOCKABLE;
63 spin_lock_irq(&current->sighand->siglock);
64 saveset = current->blocked;
65 siginitset(&current->blocked, mask);
66 recalc_sigpending();
67 spin_unlock_irq(&current->sighand->siglock);
69 regs->er0 = -EINTR;
70 while (1) {
71 current->state = TASK_INTERRUPTIBLE;
72 schedule();
73 if (do_signal(regs, &saveset))
74 return -EINTR;
78 asmlinkage int
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))
87 return -EINVAL;
89 if (copy_from_user(&newset, unewset, sizeof(newset)))
90 return -EFAULT;
91 sigdelsetmask(&newset, ~_BLOCKABLE);
93 spin_lock_irq(&current->sighand->siglock);
94 saveset = current->blocked;
95 current->blocked = newset;
96 recalc_sigpending();
97 spin_unlock_irq(&current->sighand->siglock);
99 regs->er0 = -EINTR;
100 while (1) {
101 current->state = TASK_INTERRUPTIBLE;
102 schedule();
103 if (do_signal(regs, &saveset))
104 return -EINTR;
108 asmlinkage int
109 sys_sigaction(int sig, const struct old_sigaction *act,
110 struct old_sigaction *oact)
112 struct k_sigaction new_ka, old_ka;
113 int ret;
115 if (act) {
116 old_sigset_t mask;
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))
120 return -EFAULT;
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);
128 if (!ret && oact) {
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))
132 return -EFAULT;
133 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
134 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
137 return ret;
140 asmlinkage int
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.
154 struct sigframe
156 long dummy_er0;
157 long dummy_vector;
158 #if defined(CONFIG_CPU_H8S)
159 short dummy_exr;
160 #endif
161 long dummy_pc;
162 char *pretcode;
163 unsigned char retcode[8];
164 unsigned long extramask[_NSIG_WORDS-1];
165 struct sigcontext sc;
166 int sig;
167 } __attribute__((aligned(2),packed));
169 struct rt_sigframe
171 long dummy_er0;
172 long dummy_vector;
173 #if defined(CONFIG_CPU_H8S)
174 short dummy_exr;
175 #endif
176 long dummy_pc;
177 char *pretcode;
178 struct siginfo *pinfo;
179 void *puc;
180 unsigned char retcode[8];
181 struct siginfo info;
182 struct ucontext uc;
183 int sig;
184 } __attribute__((aligned(2),packed));
186 static inline int
187 restore_sigcontext(struct pt_regs *regs, struct sigcontext *usc,
188 int *pd0)
190 int err = 0;
191 unsigned int ccr;
192 unsigned int usp;
193 unsigned int er0;
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 */
199 COPY(er1);
200 COPY(er2);
201 COPY(er3);
202 COPY(er5);
203 COPY(pc);
204 ccr = regs->ccr & 0x10;
205 COPY(ccr);
206 #undef COPY
207 regs->ccr &= 0xef;
208 regs->ccr |= ccr;
209 regs->orig_er0 = -1; /* disable syscall checks */
210 err |= __get_user(usp, &usc->sc_usp);
211 wrusp(usp);
213 err |= __get_user(er0, &usc->sc_er0);
214 *pd0 = er0;
215 return err;
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);
223 sigset_t set;
224 int er0;
226 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
227 goto badframe;
228 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
229 (_NSIG_WORDS > 1 &&
230 __copy_from_user(&set.sig[1], &frame->extramask,
231 sizeof(frame->extramask))))
232 goto badframe;
234 sigdelsetmask(&set, ~_BLOCKABLE);
235 spin_lock_irq(&current->sighand->siglock);
236 current->blocked = set;
237 recalc_sigpending();
238 spin_unlock_irq(&current->sighand->siglock);
240 if (restore_sigcontext(regs, &frame->sc, &er0))
241 goto badframe;
242 return er0;
244 badframe:
245 force_sig(SIGSEGV, current);
246 return 0;
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);
254 sigset_t set;
255 int er0;
257 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
258 goto badframe;
259 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
260 goto badframe;
262 sigdelsetmask(&set, ~_BLOCKABLE);
263 spin_unlock_irq(&current->sighand->siglock);
264 current->blocked = set;
265 recalc_sigpending();
266 spin_lock_irq(&current->sighand->siglock);
268 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &er0))
269 goto badframe;
271 if (do_sigaltstack(&frame->uc.uc_stack, NULL, usp) == -EFAULT)
272 goto badframe;
274 return er0;
276 badframe:
277 force_sig(SIGSEGV, current);
278 return 0;
281 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
282 unsigned long mask)
284 int err = 0;
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);
298 return err;
301 static inline void *
302 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
304 unsigned long usp;
306 /* Default to using normal stack. */
307 usp = rdusp();
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;
321 int err = 0;
322 int usig;
323 unsigned char *ret;
325 frame = get_sigframe(ka, regs, sizeof(*frame));
327 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
328 goto give_sigsegv;
330 usig = current_thread_info()->exec_domain
331 && current_thread_info()->exec_domain->signal_invmap
332 && sig < 32
333 ? current_thread_info()->exec_domain->signal_invmap[sig]
334 : sig;
336 err |= __put_user(usig, &frame->sig);
337 if (err)
338 goto give_sigsegv;
340 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
341 if (err)
342 goto give_sigsegv;
344 if (_NSIG_WORDS > 1) {
345 err |= copy_to_user(frame->extramask, &set->sig[1],
346 sizeof(frame->extramask));
347 if (err)
348 goto give_sigsegv;
351 ret = frame->retcode;
352 if (ka->sa.sa_flags & SA_RESTORER)
353 ret = (unsigned char *)(ka->sa.sa_restorer);
354 else {
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);
364 if (err)
365 goto give_sigsegv;
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
372 && sig < 32
373 ? current_thread_info()->exec_domain->signal_invmap[sig]
374 : sig);
375 regs->er1 = (unsigned long)&(frame->sc);
376 regs->er5 = current->mm->start_data; /* GOT base */
378 return;
380 give_sigsegv:
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;
388 int err = 0;
389 int usig;
390 unsigned char *ret;
392 frame = get_sigframe(ka, regs, sizeof(*frame));
394 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
395 goto give_sigsegv;
397 usig = current_thread_info()->exec_domain
398 && current_thread_info()->exec_domain->signal_invmap
399 && sig < 32
400 ? current_thread_info()->exec_domain->signal_invmap[sig]
401 : sig;
403 err |= __put_user(usig, &frame->sig);
404 if (err)
405 goto give_sigsegv;
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);
410 if (err)
411 goto give_sigsegv;
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));
423 if (err)
424 goto give_sigsegv;
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);
430 else {
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);
438 if (err)
439 goto give_sigsegv;
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
446 && sig < 32
447 ? current_thread_info()->exec_domain->signal_invmap[sig]
448 : sig);
449 regs->er1 = (unsigned long)&(frame->info);
450 regs->er2 = (unsigned long)&frame->uc;
451 regs->er5 = current->mm->start_data; /* GOT base */
453 return;
455 give_sigsegv:
456 force_sigsegv(sig, current);
460 * OK, we're invoking a handler
462 static void
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) {
468 switch (regs->er0) {
469 case -ERESTART_RESTARTBLOCK:
470 case -ERESTARTNOHAND:
471 regs->er0 = -EINTR;
472 break;
474 case -ERESTARTSYS:
475 if (!(ka->sa.sa_flags & SA_RESTART)) {
476 regs->er0 = -EINTR;
477 break;
479 /* fallthrough */
480 case -ERESTARTNOINTR:
481 regs->er0 = regs->orig_er0;
482 regs->pc -= 2;
486 /* set up the stack frame */
487 if (ka->sa.sa_flags & SA_SIGINFO)
488 setup_rt_frame(sig, ka, info, oldset, regs);
489 else
490 setup_frame(sig, ka, oldset, regs);
492 spin_lock_irq(&current->sighand->siglock);
493 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
494 if (!(ka->sa.sa_flags & SA_NODEFER))
495 sigaddset(&current->blocked,sig);
496 recalc_sigpending();
497 spin_unlock_irq(&current->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
503 * mistake.
505 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset)
507 siginfo_t info;
508 int signr;
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
515 * if so.
517 if ((regs->ccr & 0x10))
518 return 1;
520 if (try_to_freeze())
521 goto no_signal;
523 current->thread.esp0 = (unsigned long) regs;
525 if (!oldset)
526 oldset = &current->blocked;
528 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
529 if (signr > 0) {
530 /* Whee! Actually deliver the signal. */
531 handle_signal(signr, &info, &ka, oldset, regs);
532 return 1;
534 no_signal:
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;
542 regs->pc -= 2;
544 if (regs->er0 == -ERESTART_RESTARTBLOCK){
545 regs->er0 = __NR_restart_syscall;
546 regs->pc -= 2;
549 return 0;
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();