Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / h8300 / kernel / signal.c
blob4b490473a58e3536367579b3d88e7ba0562e7642
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>
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;
59 sigset_t saveset;
61 mask &= _BLOCKABLE;
62 spin_lock_irq(&current->sighand->siglock);
63 saveset = current->blocked;
64 siginitset(&current->blocked, mask);
65 recalc_sigpending();
66 spin_unlock_irq(&current->sighand->siglock);
68 regs->er0 = -EINTR;
69 while (1) {
70 current->state = TASK_INTERRUPTIBLE;
71 schedule();
72 if (do_signal(regs, &saveset))
73 return -EINTR;
77 asmlinkage int
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))
86 return -EINVAL;
88 if (copy_from_user(&newset, unewset, sizeof(newset)))
89 return -EFAULT;
90 sigdelsetmask(&newset, ~_BLOCKABLE);
92 spin_lock_irq(&current->sighand->siglock);
93 saveset = current->blocked;
94 current->blocked = newset;
95 recalc_sigpending();
96 spin_unlock_irq(&current->sighand->siglock);
98 regs->er0 = -EINTR;
99 while (1) {
100 current->state = TASK_INTERRUPTIBLE;
101 schedule();
102 if (do_signal(regs, &saveset))
103 return -EINTR;
107 asmlinkage int
108 sys_sigaction(int sig, const struct old_sigaction *act,
109 struct old_sigaction *oact)
111 struct k_sigaction new_ka, old_ka;
112 int ret;
114 if (act) {
115 old_sigset_t mask;
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))
119 return -EFAULT;
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);
127 if (!ret && oact) {
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))
131 return -EFAULT;
132 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
133 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
136 return ret;
139 asmlinkage int
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.
153 struct sigframe
155 long dummy_er0;
156 long dummy_vector;
157 #if defined(CONFIG_CPU_H8S)
158 short dummy_exr;
159 #endif
160 long dummy_pc;
161 char *pretcode;
162 unsigned char retcode[8];
163 unsigned long extramask[_NSIG_WORDS-1];
164 struct sigcontext sc;
165 int sig;
166 } __attribute__((aligned(2),packed));
168 struct rt_sigframe
170 long dummy_er0;
171 long dummy_vector;
172 #if defined(CONFIG_CPU_H8S)
173 short dummy_exr;
174 #endif
175 long dummy_pc;
176 char *pretcode;
177 struct siginfo *pinfo;
178 void *puc;
179 unsigned char retcode[8];
180 struct siginfo info;
181 struct ucontext uc;
182 int sig;
183 } __attribute__((aligned(2),packed));
185 static inline int
186 restore_sigcontext(struct pt_regs *regs, struct sigcontext *usc,
187 int *pd0)
189 int err = 0;
190 unsigned int ccr;
191 unsigned int usp;
192 unsigned int er0;
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 */
198 COPY(er1);
199 COPY(er2);
200 COPY(er3);
201 COPY(er5);
202 COPY(pc);
203 ccr = regs->ccr & 0x10;
204 COPY(ccr);
205 #undef COPY
206 regs->ccr &= 0xef;
207 regs->ccr |= ccr;
208 regs->orig_er0 = -1; /* disable syscall checks */
209 err |= __get_user(usp, &usc->sc_usp);
210 wrusp(usp);
212 err |= __get_user(er0, &usc->sc_er0);
213 *pd0 = er0;
214 return err;
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);
222 sigset_t set;
223 int er0;
225 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
226 goto badframe;
227 if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
228 (_NSIG_WORDS > 1 &&
229 __copy_from_user(&set.sig[1], &frame->extramask,
230 sizeof(frame->extramask))))
231 goto badframe;
233 sigdelsetmask(&set, ~_BLOCKABLE);
234 spin_lock_irq(&current->sighand->siglock);
235 current->blocked = set;
236 recalc_sigpending();
237 spin_unlock_irq(&current->sighand->siglock);
239 if (restore_sigcontext(regs, &frame->sc, &er0))
240 goto badframe;
241 return er0;
243 badframe:
244 force_sig(SIGSEGV, current);
245 return 0;
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);
253 sigset_t set;
254 int er0;
256 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
257 goto badframe;
258 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
259 goto badframe;
261 sigdelsetmask(&set, ~_BLOCKABLE);
262 spin_unlock_irq(&current->sighand->siglock);
263 current->blocked = set;
264 recalc_sigpending();
265 spin_lock_irq(&current->sighand->siglock);
267 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &er0))
268 goto badframe;
270 if (do_sigaltstack(&frame->uc.uc_stack, NULL, usp) == -EFAULT)
271 goto badframe;
273 return er0;
275 badframe:
276 force_sig(SIGSEGV, current);
277 return 0;
280 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
281 unsigned long mask)
283 int err = 0;
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);
297 return err;
300 static inline void *
301 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
303 unsigned long usp;
305 /* Default to using normal stack. */
306 usp = rdusp();
308 /* This is the X/Open sanctioned signal stack switching. */
309 if (ka->sa.sa_flags & SA_ONSTACK) {
310 if (!sas_ss_flags(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;
320 int err = 0;
321 int usig;
322 unsigned char *ret;
324 frame = get_sigframe(ka, regs, sizeof(*frame));
326 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
327 goto give_sigsegv;
329 usig = current_thread_info()->exec_domain
330 && current_thread_info()->exec_domain->signal_invmap
331 && sig < 32
332 ? current_thread_info()->exec_domain->signal_invmap[sig]
333 : sig;
335 err |= __put_user(usig, &frame->sig);
336 if (err)
337 goto give_sigsegv;
339 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
340 if (err)
341 goto give_sigsegv;
343 if (_NSIG_WORDS > 1) {
344 err |= copy_to_user(frame->extramask, &set->sig[1],
345 sizeof(frame->extramask));
346 if (err)
347 goto give_sigsegv;
350 ret = frame->retcode;
351 if (ka->sa.sa_flags & SA_RESTORER)
352 ret = (unsigned char *)(ka->sa.sa_restorer);
353 else {
354 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
355 <<<<<<< HEAD:arch/h8300/kernel/signal.c
356 err != __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
357 =======
358 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
359 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/h8300/kernel/signal.c
360 (unsigned long *)(frame->retcode + 0));
361 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
364 /* Set up to return from userspace. */
365 err |= __put_user(ret, &frame->pretcode);
367 if (err)
368 goto give_sigsegv;
370 /* Set up registers for signal handler */
371 wrusp ((unsigned long) frame);
372 regs->pc = (unsigned long) ka->sa.sa_handler;
373 regs->er0 = (current_thread_info()->exec_domain
374 && current_thread_info()->exec_domain->signal_invmap
375 && sig < 32
376 ? current_thread_info()->exec_domain->signal_invmap[sig]
377 : sig);
378 regs->er1 = (unsigned long)&(frame->sc);
379 regs->er5 = current->mm->start_data; /* GOT base */
381 return;
383 give_sigsegv:
384 force_sigsegv(sig, current);
387 static void setup_rt_frame (int sig, struct k_sigaction *ka, siginfo_t *info,
388 sigset_t *set, struct pt_regs *regs)
390 struct rt_sigframe *frame;
391 int err = 0;
392 int usig;
393 unsigned char *ret;
395 frame = get_sigframe(ka, regs, sizeof(*frame));
397 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
398 goto give_sigsegv;
400 usig = current_thread_info()->exec_domain
401 && current_thread_info()->exec_domain->signal_invmap
402 && sig < 32
403 ? current_thread_info()->exec_domain->signal_invmap[sig]
404 : sig;
406 err |= __put_user(usig, &frame->sig);
407 if (err)
408 goto give_sigsegv;
410 err |= __put_user(&frame->info, &frame->pinfo);
411 err |= __put_user(&frame->uc, &frame->puc);
412 err |= copy_siginfo_to_user(&frame->info, info);
413 if (err)
414 goto give_sigsegv;
416 /* Create the ucontext. */
417 err |= __put_user(0, &frame->uc.uc_flags);
418 err |= __put_user(0, &frame->uc.uc_link);
419 err |= __put_user((void *)current->sas_ss_sp,
420 &frame->uc.uc_stack.ss_sp);
421 err |= __put_user(sas_ss_flags(rdusp()),
422 &frame->uc.uc_stack.ss_flags);
423 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
424 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
425 err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
426 if (err)
427 goto give_sigsegv;
429 /* Set up to return from userspace. */
430 ret = frame->retcode;
431 if (ka->sa.sa_flags & SA_RESTORER)
432 ret = (unsigned char *)(ka->sa.sa_restorer);
433 else {
434 /* sub.l er0,er0; mov.b #__NR_sigreturn,r0l; trapa #0 */
435 <<<<<<< HEAD:arch/h8300/kernel/signal.c
436 err != __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
437 =======
438 err |= __put_user(0x1a80f800 + (__NR_sigreturn & 0xff),
439 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/h8300/kernel/signal.c
440 (unsigned long *)(frame->retcode + 0));
441 err |= __put_user(0x5700, (unsigned short *)(frame->retcode + 4));
443 err |= __put_user(ret, &frame->pretcode);
445 if (err)
446 goto give_sigsegv;
448 /* Set up registers for signal handler */
449 wrusp ((unsigned long) frame);
450 regs->pc = (unsigned long) ka->sa.sa_handler;
451 regs->er0 = (current_thread_info()->exec_domain
452 && current_thread_info()->exec_domain->signal_invmap
453 && sig < 32
454 ? current_thread_info()->exec_domain->signal_invmap[sig]
455 : sig);
456 regs->er1 = (unsigned long)&(frame->info);
457 regs->er2 = (unsigned long)&frame->uc;
458 regs->er5 = current->mm->start_data; /* GOT base */
460 return;
462 give_sigsegv:
463 force_sigsegv(sig, current);
467 * OK, we're invoking a handler
469 static void
470 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
471 sigset_t *oldset, struct pt_regs * regs)
473 /* are we from a system call? */
474 if (regs->orig_er0 >= 0) {
475 switch (regs->er0) {
476 case -ERESTART_RESTARTBLOCK:
477 case -ERESTARTNOHAND:
478 regs->er0 = -EINTR;
479 break;
481 case -ERESTARTSYS:
482 if (!(ka->sa.sa_flags & SA_RESTART)) {
483 regs->er0 = -EINTR;
484 break;
486 /* fallthrough */
487 case -ERESTARTNOINTR:
488 regs->er0 = regs->orig_er0;
489 regs->pc -= 2;
493 /* set up the stack frame */
494 if (ka->sa.sa_flags & SA_SIGINFO)
495 setup_rt_frame(sig, ka, info, oldset, regs);
496 else
497 setup_frame(sig, ka, oldset, regs);
499 spin_lock_irq(&current->sighand->siglock);
500 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
501 if (!(ka->sa.sa_flags & SA_NODEFER))
502 sigaddset(&current->blocked,sig);
503 recalc_sigpending();
504 spin_unlock_irq(&current->sighand->siglock);
508 * Note that 'init' is a special process: it doesn't get signals it doesn't
509 * want to handle. Thus you cannot kill init even with a SIGKILL even by
510 * mistake.
512 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset)
514 siginfo_t info;
515 int signr;
516 struct k_sigaction ka;
519 * We want the common case to go fast, which
520 * is why we may in certain cases get here from
521 * kernel mode. Just return without doing anything
522 * if so.
524 if ((regs->ccr & 0x10))
525 return 1;
527 if (try_to_freeze())
528 goto no_signal;
530 current->thread.esp0 = (unsigned long) regs;
532 if (!oldset)
533 oldset = &current->blocked;
535 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
536 if (signr > 0) {
537 /* Whee! Actually deliver the signal. */
538 handle_signal(signr, &info, &ka, oldset, regs);
539 return 1;
541 no_signal:
542 /* Did we come from a system call? */
543 if (regs->orig_er0 >= 0) {
544 /* Restart the system call - no handlers present */
545 if (regs->er0 == -ERESTARTNOHAND ||
546 regs->er0 == -ERESTARTSYS ||
547 regs->er0 == -ERESTARTNOINTR) {
548 regs->er0 = regs->orig_er0;
549 regs->pc -= 2;
551 if (regs->er0 == -ERESTART_RESTARTBLOCK){
552 regs->er0 = __NR_restart_syscall;
553 regs->pc -= 2;
556 return 0;
559 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
561 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
562 do_signal(regs, NULL);