Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux-2.6.git] / arch / sh / kernel / signal.c
blob706d81ccd10149e15e28ffbfeed70e69a656e009
1 /*
2 * linux/arch/sh/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/tty.h>
22 #include <linux/elf.h>
23 #include <linux/personality.h>
24 #include <linux/binfmts.h>
25 #include <linux/freezer.h>
26 #include <linux/io.h>
27 #include <asm/system.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/cacheflush.h>
33 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 * Atomically swap in the new signal mask, and wait for a signal.
38 asmlinkage int
39 sys_sigsuspend(old_sigset_t mask,
40 unsigned long r5, unsigned long r6, unsigned long r7,
41 struct pt_regs __regs)
43 mask &= _BLOCKABLE;
44 spin_lock_irq(&current->sighand->siglock);
45 current->saved_sigmask = current->blocked;
46 siginitset(&current->blocked, mask);
47 recalc_sigpending();
48 spin_unlock_irq(&current->sighand->siglock);
50 current->state = TASK_INTERRUPTIBLE;
51 schedule();
52 set_thread_flag(TIF_RESTORE_SIGMASK);
53 return -ERESTARTNOHAND;
56 asmlinkage int
57 sys_sigaction(int sig, const struct old_sigaction __user *act,
58 struct old_sigaction __user *oact)
60 struct k_sigaction new_ka, old_ka;
61 int ret;
63 if (act) {
64 old_sigset_t mask;
65 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
66 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
67 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
68 return -EFAULT;
69 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
70 __get_user(mask, &act->sa_mask);
71 siginitset(&new_ka.sa.sa_mask, mask);
74 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
76 if (!ret && oact) {
77 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
78 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
79 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
80 return -EFAULT;
81 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
82 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
85 return ret;
88 asmlinkage int
89 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
90 unsigned long r6, unsigned long r7,
91 struct pt_regs __regs)
93 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
95 return do_sigaltstack(uss, uoss, regs->regs[15]);
100 * Do a signal return; undo the signal stack.
103 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
104 #if defined(CONFIG_CPU_SH2)
105 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
106 #else
107 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
108 #endif
109 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
111 struct sigframe
113 struct sigcontext sc;
114 unsigned long extramask[_NSIG_WORDS-1];
115 u16 retcode[8];
118 struct rt_sigframe
120 struct siginfo info;
121 struct ucontext uc;
122 u16 retcode[8];
125 #ifdef CONFIG_SH_FPU
126 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
128 struct task_struct *tsk = current;
130 if (!(current_cpu_data.flags & CPU_HAS_FPU))
131 return 0;
133 set_used_math();
134 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
135 sizeof(long)*(16*2+2));
138 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
139 struct pt_regs *regs)
141 struct task_struct *tsk = current;
143 if (!(current_cpu_data.flags & CPU_HAS_FPU))
144 return 0;
146 if (!used_math()) {
147 __put_user(0, &sc->sc_ownedfp);
148 return 0;
151 __put_user(1, &sc->sc_ownedfp);
153 /* This will cause a "finit" to be triggered by the next
154 attempted FPU operation by the 'current' process.
156 clear_used_math();
158 unlazy_fpu(tsk, regs);
159 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
160 sizeof(long)*(16*2+2));
162 #endif /* CONFIG_SH_FPU */
164 static int
165 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
167 unsigned int err = 0;
169 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
170 COPY(regs[1]);
171 COPY(regs[2]); COPY(regs[3]);
172 COPY(regs[4]); COPY(regs[5]);
173 COPY(regs[6]); COPY(regs[7]);
174 COPY(regs[8]); COPY(regs[9]);
175 COPY(regs[10]); COPY(regs[11]);
176 COPY(regs[12]); COPY(regs[13]);
177 COPY(regs[14]); COPY(regs[15]);
178 COPY(gbr); COPY(mach);
179 COPY(macl); COPY(pr);
180 COPY(sr); COPY(pc);
181 #undef COPY
183 #ifdef CONFIG_SH_FPU
184 if (current_cpu_data.flags & CPU_HAS_FPU) {
185 int owned_fp;
186 struct task_struct *tsk = current;
188 regs->sr |= SR_FD; /* Release FPU */
189 clear_fpu(tsk, regs);
190 clear_used_math();
191 __get_user (owned_fp, &sc->sc_ownedfp);
192 if (owned_fp)
193 err |= restore_sigcontext_fpu(sc);
195 #endif
197 regs->tra = -1; /* disable syscall checks */
198 err |= __get_user(*r0_p, &sc->sc_regs[0]);
199 return err;
202 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
203 unsigned long r6, unsigned long r7,
204 struct pt_regs __regs)
206 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
207 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
208 sigset_t set;
209 int r0;
211 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
212 goto badframe;
214 if (__get_user(set.sig[0], &frame->sc.oldmask)
215 || (_NSIG_WORDS > 1
216 && __copy_from_user(&set.sig[1], &frame->extramask,
217 sizeof(frame->extramask))))
218 goto badframe;
220 sigdelsetmask(&set, ~_BLOCKABLE);
222 spin_lock_irq(&current->sighand->siglock);
223 current->blocked = set;
224 recalc_sigpending();
225 spin_unlock_irq(&current->sighand->siglock);
227 if (restore_sigcontext(regs, &frame->sc, &r0))
228 goto badframe;
229 return r0;
231 badframe:
232 force_sig(SIGSEGV, current);
233 return 0;
236 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
237 unsigned long r6, unsigned long r7,
238 struct pt_regs __regs)
240 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
241 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
242 sigset_t set;
243 stack_t st;
244 int r0;
246 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
247 goto badframe;
249 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
250 goto badframe;
252 sigdelsetmask(&set, ~_BLOCKABLE);
253 spin_lock_irq(&current->sighand->siglock);
254 current->blocked = set;
255 recalc_sigpending();
256 spin_unlock_irq(&current->sighand->siglock);
258 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
259 goto badframe;
261 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
262 goto badframe;
263 /* It is more difficult to avoid calling this function than to
264 call it and ignore errors. */
265 do_sigaltstack((const stack_t __user *)&st, NULL, (unsigned long)frame);
267 return r0;
269 badframe:
270 force_sig(SIGSEGV, current);
271 return 0;
275 * Set up a signal frame.
278 static int
279 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
280 unsigned long mask)
282 int err = 0;
284 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
285 COPY(regs[0]); COPY(regs[1]);
286 COPY(regs[2]); COPY(regs[3]);
287 COPY(regs[4]); COPY(regs[5]);
288 COPY(regs[6]); COPY(regs[7]);
289 COPY(regs[8]); COPY(regs[9]);
290 COPY(regs[10]); COPY(regs[11]);
291 COPY(regs[12]); COPY(regs[13]);
292 COPY(regs[14]); COPY(regs[15]);
293 COPY(gbr); COPY(mach);
294 COPY(macl); COPY(pr);
295 COPY(sr); COPY(pc);
296 #undef COPY
298 #ifdef CONFIG_SH_FPU
299 err |= save_sigcontext_fpu(sc, regs);
300 #endif
302 /* non-iBCS2 extensions.. */
303 err |= __put_user(mask, &sc->oldmask);
305 return err;
309 * Determine which stack to use..
311 static inline void __user *
312 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
314 if (ka->sa.sa_flags & SA_ONSTACK) {
315 if (sas_ss_flags(sp) == 0)
316 sp = current->sas_ss_sp + current->sas_ss_size;
319 return (void __user *)((sp - frame_size) & -8ul);
322 /* These symbols are defined with the addresses in the vsyscall page.
323 See vsyscall-trapa.S. */
324 extern void __user __kernel_sigreturn;
325 extern void __user __kernel_rt_sigreturn;
327 static int setup_frame(int sig, struct k_sigaction *ka,
328 sigset_t *set, struct pt_regs *regs)
330 struct sigframe __user *frame;
331 int err = 0;
332 int signal;
334 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
336 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
337 goto give_sigsegv;
339 signal = current_thread_info()->exec_domain
340 && current_thread_info()->exec_domain->signal_invmap
341 && sig < 32
342 ? current_thread_info()->exec_domain->signal_invmap[sig]
343 : sig;
345 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
347 if (_NSIG_WORDS > 1)
348 err |= __copy_to_user(frame->extramask, &set->sig[1],
349 sizeof(frame->extramask));
351 /* Set up to return from userspace. If provided, use a stub
352 already in userspace. */
353 if (ka->sa.sa_flags & SA_RESTORER) {
354 regs->pr = (unsigned long) ka->sa.sa_restorer;
355 #ifdef CONFIG_VSYSCALL
356 } else if (likely(current->mm->context.vdso)) {
357 regs->pr = VDSO_SYM(&__kernel_sigreturn);
358 #endif
359 } else {
360 /* Generate return code (system call to sigreturn) */
361 err |= __put_user(MOVW(7), &frame->retcode[0]);
362 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
363 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
364 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
365 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
366 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
367 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
368 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
369 regs->pr = (unsigned long) frame->retcode;
372 if (err)
373 goto give_sigsegv;
375 /* Set up registers for signal handler */
376 regs->regs[15] = (unsigned long) frame;
377 regs->regs[4] = signal; /* Arg for signal handler */
378 regs->regs[5] = 0;
379 regs->regs[6] = (unsigned long) &frame->sc;
380 regs->pc = (unsigned long) ka->sa.sa_handler;
382 set_fs(USER_DS);
384 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
385 current->comm, current->pid, frame, regs->pc, regs->pr);
387 flush_cache_sigtramp(regs->pr);
389 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
390 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
392 return 0;
394 give_sigsegv:
395 force_sigsegv(sig, current);
396 return -EFAULT;
399 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
400 sigset_t *set, struct pt_regs *regs)
402 struct rt_sigframe __user *frame;
403 int err = 0;
404 int signal;
406 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
408 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
409 goto give_sigsegv;
411 signal = current_thread_info()->exec_domain
412 && current_thread_info()->exec_domain->signal_invmap
413 && sig < 32
414 ? current_thread_info()->exec_domain->signal_invmap[sig]
415 : sig;
417 err |= copy_siginfo_to_user(&frame->info, info);
419 /* Create the ucontext. */
420 err |= __put_user(0, &frame->uc.uc_flags);
421 err |= __put_user(0, &frame->uc.uc_link);
422 err |= __put_user((void *)current->sas_ss_sp,
423 &frame->uc.uc_stack.ss_sp);
424 err |= __put_user(sas_ss_flags(regs->regs[15]),
425 &frame->uc.uc_stack.ss_flags);
426 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
427 err |= setup_sigcontext(&frame->uc.uc_mcontext,
428 regs, set->sig[0]);
429 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
431 /* Set up to return from userspace. If provided, use a stub
432 already in userspace. */
433 if (ka->sa.sa_flags & SA_RESTORER) {
434 regs->pr = (unsigned long) ka->sa.sa_restorer;
435 #ifdef CONFIG_VSYSCALL
436 } else if (likely(current->mm->context.vdso)) {
437 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
438 #endif
439 } else {
440 /* Generate return code (system call to rt_sigreturn) */
441 err |= __put_user(MOVW(7), &frame->retcode[0]);
442 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
443 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
444 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
445 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
446 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
447 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
448 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
449 regs->pr = (unsigned long) frame->retcode;
452 if (err)
453 goto give_sigsegv;
455 /* Set up registers for signal handler */
456 regs->regs[15] = (unsigned long) frame;
457 regs->regs[4] = signal; /* Arg for signal handler */
458 regs->regs[5] = (unsigned long) &frame->info;
459 regs->regs[6] = (unsigned long) &frame->uc;
460 regs->pc = (unsigned long) ka->sa.sa_handler;
462 set_fs(USER_DS);
464 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
465 current->comm, current->pid, frame, regs->pc, regs->pr);
467 flush_cache_sigtramp(regs->pr);
469 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
470 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
472 return 0;
474 give_sigsegv:
475 force_sigsegv(sig, current);
476 return -EFAULT;
480 * OK, we're invoking a handler
483 static int
484 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
485 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
487 int ret;
489 /* Are we from a system call? */
490 if (regs->tra >= 0) {
491 /* If so, check system call restarting.. */
492 switch (regs->regs[0]) {
493 case -ERESTART_RESTARTBLOCK:
494 case -ERESTARTNOHAND:
495 regs->regs[0] = -EINTR;
496 break;
498 case -ERESTARTSYS:
499 if (!(ka->sa.sa_flags & SA_RESTART)) {
500 regs->regs[0] = -EINTR;
501 break;
503 /* fallthrough */
504 case -ERESTARTNOINTR:
505 regs->regs[0] = save_r0;
506 regs->pc -= instruction_size(
507 ctrl_inw(regs->pc - 4));
508 break;
510 } else {
511 /* gUSA handling */
512 #ifdef CONFIG_PREEMPT
513 unsigned long flags;
515 local_irq_save(flags);
516 #endif
517 if (regs->regs[15] >= 0xc0000000) {
518 int offset = (int)regs->regs[15];
520 /* Reset stack pointer: clear critical region mark */
521 regs->regs[15] = regs->regs[1];
522 if (regs->pc < regs->regs[0])
523 /* Go to rewind point #1 */
524 regs->pc = regs->regs[0] + offset -
525 instruction_size(ctrl_inw(regs->pc-4));
527 #ifdef CONFIG_PREEMPT
528 local_irq_restore(flags);
529 #endif
532 /* Set up the stack frame */
533 if (ka->sa.sa_flags & SA_SIGINFO)
534 ret = setup_rt_frame(sig, ka, info, oldset, regs);
535 else
536 ret = setup_frame(sig, ka, oldset, regs);
538 if (ka->sa.sa_flags & SA_ONESHOT)
539 ka->sa.sa_handler = SIG_DFL;
541 if (ret == 0) {
542 spin_lock_irq(&current->sighand->siglock);
543 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
544 if (!(ka->sa.sa_flags & SA_NODEFER))
545 sigaddset(&current->blocked,sig);
546 recalc_sigpending();
547 spin_unlock_irq(&current->sighand->siglock);
550 return ret;
554 * Note that 'init' is a special process: it doesn't get signals it doesn't
555 * want to handle. Thus you cannot kill init even with a SIGKILL even by
556 * mistake.
558 * Note that we go through the signals twice: once to check the signals that
559 * the kernel can handle, and then we build all the user-level signal handling
560 * stack-frames in one go after that.
562 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
564 siginfo_t info;
565 int signr;
566 struct k_sigaction ka;
567 sigset_t *oldset;
570 * We want the common case to go fast, which
571 * is why we may in certain cases get here from
572 * kernel mode. Just return without doing anything
573 * if so.
575 if (!user_mode(regs))
576 return;
578 if (try_to_freeze())
579 goto no_signal;
581 if (test_thread_flag(TIF_RESTORE_SIGMASK))
582 oldset = &current->saved_sigmask;
583 else
584 oldset = &current->blocked;
586 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
587 if (signr > 0) {
588 /* Whee! Actually deliver the signal. */
589 if (handle_signal(signr, &ka, &info, oldset,
590 regs, save_r0) == 0) {
591 /* a signal was successfully delivered; the saved
592 * sigmask will have been stored in the signal frame,
593 * and will be restored by sigreturn, so we can simply
594 * clear the TIF_RESTORE_SIGMASK flag */
595 if (test_thread_flag(TIF_RESTORE_SIGMASK))
596 clear_thread_flag(TIF_RESTORE_SIGMASK);
599 return;
602 no_signal:
603 /* Did we come from a system call? */
604 if (regs->tra >= 0) {
605 /* Restart the system call - no handlers present */
606 if (regs->regs[0] == -ERESTARTNOHAND ||
607 regs->regs[0] == -ERESTARTSYS ||
608 regs->regs[0] == -ERESTARTNOINTR) {
609 regs->regs[0] = save_r0;
610 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
611 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
612 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
613 regs->regs[3] = __NR_restart_syscall;
617 /* if there's no signal to deliver, we just put the saved sigmask
618 * back */
619 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
620 clear_thread_flag(TIF_RESTORE_SIGMASK);
621 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
625 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
626 __u32 thread_info_flags)
628 /* deal with pending signal delivery */
629 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
630 do_signal(regs, save_r0);