gianfar: Fix build with CONFIG_PM enabled
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sh / kernel / signal_32.c
blob17784e19ae340ec40dbc8c97e79e3790877c98de
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 <linux/tracehook.h>
28 #include <asm/system.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/cacheflush.h>
33 #include <asm/syscalls.h>
34 #include <asm/fpu.h>
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
38 struct fdpic_func_descriptor {
39 unsigned long text;
40 unsigned long GOT;
44 * Atomically swap in the new signal mask, and wait for a signal.
46 asmlinkage int
47 sys_sigsuspend(old_sigset_t mask,
48 unsigned long r5, unsigned long r6, unsigned long r7,
49 struct pt_regs __regs)
51 mask &= _BLOCKABLE;
52 spin_lock_irq(&current->sighand->siglock);
53 current->saved_sigmask = current->blocked;
54 siginitset(&current->blocked, mask);
55 recalc_sigpending();
56 spin_unlock_irq(&current->sighand->siglock);
58 current->state = TASK_INTERRUPTIBLE;
59 schedule();
60 set_thread_flag(TIF_RESTORE_SIGMASK);
61 return -ERESTARTNOHAND;
64 asmlinkage int
65 sys_sigaction(int sig, const struct old_sigaction __user *act,
66 struct old_sigaction __user *oact)
68 struct k_sigaction new_ka, old_ka;
69 int ret;
71 if (act) {
72 old_sigset_t mask;
73 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
74 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
75 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
76 return -EFAULT;
77 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
78 __get_user(mask, &act->sa_mask);
79 siginitset(&new_ka.sa.sa_mask, mask);
82 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
84 if (!ret && oact) {
85 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
86 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
87 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
88 return -EFAULT;
89 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
90 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
93 return ret;
96 asmlinkage int
97 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
98 unsigned long r6, unsigned long r7,
99 struct pt_regs __regs)
101 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
103 return do_sigaltstack(uss, uoss, regs->regs[15]);
108 * Do a signal return; undo the signal stack.
111 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
112 #if defined(CONFIG_CPU_SH2)
113 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
114 #else
115 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
116 #endif
117 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
119 struct sigframe
121 struct sigcontext sc;
122 unsigned long extramask[_NSIG_WORDS-1];
123 u16 retcode[8];
126 struct rt_sigframe
128 struct siginfo info;
129 struct ucontext uc;
130 u16 retcode[8];
133 #ifdef CONFIG_SH_FPU
134 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
136 struct task_struct *tsk = current;
138 if (!(current_cpu_data.flags & CPU_HAS_FPU))
139 return 0;
141 set_used_math();
142 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
143 sizeof(long)*(16*2+2));
146 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
147 struct pt_regs *regs)
149 struct task_struct *tsk = current;
151 if (!(current_cpu_data.flags & CPU_HAS_FPU))
152 return 0;
154 if (!used_math()) {
155 __put_user(0, &sc->sc_ownedfp);
156 return 0;
159 __put_user(1, &sc->sc_ownedfp);
161 /* This will cause a "finit" to be triggered by the next
162 attempted FPU operation by the 'current' process.
164 clear_used_math();
166 unlazy_fpu(tsk, regs);
167 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
168 sizeof(long)*(16*2+2));
170 #endif /* CONFIG_SH_FPU */
172 static int
173 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
175 unsigned int err = 0;
177 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
178 COPY(regs[1]);
179 COPY(regs[2]); COPY(regs[3]);
180 COPY(regs[4]); COPY(regs[5]);
181 COPY(regs[6]); COPY(regs[7]);
182 COPY(regs[8]); COPY(regs[9]);
183 COPY(regs[10]); COPY(regs[11]);
184 COPY(regs[12]); COPY(regs[13]);
185 COPY(regs[14]); COPY(regs[15]);
186 COPY(gbr); COPY(mach);
187 COPY(macl); COPY(pr);
188 COPY(sr); COPY(pc);
189 #undef COPY
191 #ifdef CONFIG_SH_FPU
192 if (current_cpu_data.flags & CPU_HAS_FPU) {
193 int owned_fp;
194 struct task_struct *tsk = current;
196 regs->sr |= SR_FD; /* Release FPU */
197 clear_fpu(tsk, regs);
198 clear_used_math();
199 __get_user (owned_fp, &sc->sc_ownedfp);
200 if (owned_fp)
201 err |= restore_sigcontext_fpu(sc);
203 #endif
205 regs->tra = -1; /* disable syscall checks */
206 err |= __get_user(*r0_p, &sc->sc_regs[0]);
207 return err;
210 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
211 unsigned long r6, unsigned long r7,
212 struct pt_regs __regs)
214 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
215 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
216 sigset_t set;
217 int r0;
219 /* Always make any pending restarted system calls return -EINTR */
220 current_thread_info()->restart_block.fn = do_no_restart_syscall;
222 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
223 goto badframe;
225 if (__get_user(set.sig[0], &frame->sc.oldmask)
226 || (_NSIG_WORDS > 1
227 && __copy_from_user(&set.sig[1], &frame->extramask,
228 sizeof(frame->extramask))))
229 goto badframe;
231 sigdelsetmask(&set, ~_BLOCKABLE);
233 spin_lock_irq(&current->sighand->siglock);
234 current->blocked = set;
235 recalc_sigpending();
236 spin_unlock_irq(&current->sighand->siglock);
238 if (restore_sigcontext(regs, &frame->sc, &r0))
239 goto badframe;
240 return r0;
242 badframe:
243 force_sig(SIGSEGV, current);
244 return 0;
247 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
248 unsigned long r6, unsigned long r7,
249 struct pt_regs __regs)
251 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
252 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
253 sigset_t set;
254 int r0;
256 /* Always make any pending restarted system calls return -EINTR */
257 current_thread_info()->restart_block.fn = do_no_restart_syscall;
259 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
260 goto badframe;
262 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
263 goto badframe;
265 sigdelsetmask(&set, ~_BLOCKABLE);
266 spin_lock_irq(&current->sighand->siglock);
267 current->blocked = set;
268 recalc_sigpending();
269 spin_unlock_irq(&current->sighand->siglock);
271 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
272 goto badframe;
274 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
275 regs->regs[15]) == -EFAULT)
276 goto badframe;
278 return r0;
280 badframe:
281 force_sig(SIGSEGV, current);
282 return 0;
286 * Set up a signal frame.
289 static int
290 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
291 unsigned long mask)
293 int err = 0;
295 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
296 COPY(regs[0]); COPY(regs[1]);
297 COPY(regs[2]); COPY(regs[3]);
298 COPY(regs[4]); COPY(regs[5]);
299 COPY(regs[6]); COPY(regs[7]);
300 COPY(regs[8]); COPY(regs[9]);
301 COPY(regs[10]); COPY(regs[11]);
302 COPY(regs[12]); COPY(regs[13]);
303 COPY(regs[14]); COPY(regs[15]);
304 COPY(gbr); COPY(mach);
305 COPY(macl); COPY(pr);
306 COPY(sr); COPY(pc);
307 #undef COPY
309 #ifdef CONFIG_SH_FPU
310 err |= save_sigcontext_fpu(sc, regs);
311 #endif
313 /* non-iBCS2 extensions.. */
314 err |= __put_user(mask, &sc->oldmask);
316 return err;
320 * Determine which stack to use..
322 static inline void __user *
323 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
325 if (ka->sa.sa_flags & SA_ONSTACK) {
326 if (sas_ss_flags(sp) == 0)
327 sp = current->sas_ss_sp + current->sas_ss_size;
330 return (void __user *)((sp - frame_size) & -8ul);
333 /* These symbols are defined with the addresses in the vsyscall page.
334 See vsyscall-trapa.S. */
335 extern void __user __kernel_sigreturn;
336 extern void __user __kernel_rt_sigreturn;
338 static int setup_frame(int sig, struct k_sigaction *ka,
339 sigset_t *set, struct pt_regs *regs)
341 struct sigframe __user *frame;
342 int err = 0;
343 int signal;
345 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
347 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
348 goto give_sigsegv;
350 signal = current_thread_info()->exec_domain
351 && current_thread_info()->exec_domain->signal_invmap
352 && sig < 32
353 ? current_thread_info()->exec_domain->signal_invmap[sig]
354 : sig;
356 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
358 if (_NSIG_WORDS > 1)
359 err |= __copy_to_user(frame->extramask, &set->sig[1],
360 sizeof(frame->extramask));
362 /* Set up to return from userspace. If provided, use a stub
363 already in userspace. */
364 if (ka->sa.sa_flags & SA_RESTORER) {
365 regs->pr = (unsigned long) ka->sa.sa_restorer;
366 #ifdef CONFIG_VSYSCALL
367 } else if (likely(current->mm->context.vdso)) {
368 regs->pr = VDSO_SYM(&__kernel_sigreturn);
369 #endif
370 } else {
371 /* Generate return code (system call to sigreturn) */
372 err |= __put_user(MOVW(7), &frame->retcode[0]);
373 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
374 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
375 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
376 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
377 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
378 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
379 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
380 regs->pr = (unsigned long) frame->retcode;
381 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
384 if (err)
385 goto give_sigsegv;
387 /* Set up registers for signal handler */
388 regs->regs[15] = (unsigned long) frame;
389 regs->regs[4] = signal; /* Arg for signal handler */
390 regs->regs[5] = 0;
391 regs->regs[6] = (unsigned long) &frame->sc;
393 if (current->personality & FDPIC_FUNCPTRS) {
394 struct fdpic_func_descriptor __user *funcptr =
395 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
397 __get_user(regs->pc, &funcptr->text);
398 __get_user(regs->regs[12], &funcptr->GOT);
399 } else
400 regs->pc = (unsigned long)ka->sa.sa_handler;
402 set_fs(USER_DS);
404 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
405 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
407 return 0;
409 give_sigsegv:
410 force_sigsegv(sig, current);
411 return -EFAULT;
414 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
415 sigset_t *set, struct pt_regs *regs)
417 struct rt_sigframe __user *frame;
418 int err = 0;
419 int signal;
421 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
423 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
424 goto give_sigsegv;
426 signal = current_thread_info()->exec_domain
427 && current_thread_info()->exec_domain->signal_invmap
428 && sig < 32
429 ? current_thread_info()->exec_domain->signal_invmap[sig]
430 : sig;
432 err |= copy_siginfo_to_user(&frame->info, info);
434 /* Create the ucontext. */
435 err |= __put_user(0, &frame->uc.uc_flags);
436 err |= __put_user(NULL, &frame->uc.uc_link);
437 err |= __put_user((void *)current->sas_ss_sp,
438 &frame->uc.uc_stack.ss_sp);
439 err |= __put_user(sas_ss_flags(regs->regs[15]),
440 &frame->uc.uc_stack.ss_flags);
441 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
442 err |= setup_sigcontext(&frame->uc.uc_mcontext,
443 regs, set->sig[0]);
444 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
446 /* Set up to return from userspace. If provided, use a stub
447 already in userspace. */
448 if (ka->sa.sa_flags & SA_RESTORER) {
449 regs->pr = (unsigned long) ka->sa.sa_restorer;
450 #ifdef CONFIG_VSYSCALL
451 } else if (likely(current->mm->context.vdso)) {
452 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
453 #endif
454 } else {
455 /* Generate return code (system call to rt_sigreturn) */
456 err |= __put_user(MOVW(7), &frame->retcode[0]);
457 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
458 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
459 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
460 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
461 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
462 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
463 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
464 regs->pr = (unsigned long) frame->retcode;
467 if (err)
468 goto give_sigsegv;
470 /* Set up registers for signal handler */
471 regs->regs[15] = (unsigned long) frame;
472 regs->regs[4] = signal; /* Arg for signal handler */
473 regs->regs[5] = (unsigned long) &frame->info;
474 regs->regs[6] = (unsigned long) &frame->uc;
476 if (current->personality & FDPIC_FUNCPTRS) {
477 struct fdpic_func_descriptor __user *funcptr =
478 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
480 __get_user(regs->pc, &funcptr->text);
481 __get_user(regs->regs[12], &funcptr->GOT);
482 } else
483 regs->pc = (unsigned long)ka->sa.sa_handler;
485 set_fs(USER_DS);
487 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
488 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
490 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
492 return 0;
494 give_sigsegv:
495 force_sigsegv(sig, current);
496 return -EFAULT;
499 static inline void
500 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
501 struct sigaction *sa)
503 /* If we're not from a syscall, bail out */
504 if (regs->tra < 0)
505 return;
507 /* check for system call restart.. */
508 switch (regs->regs[0]) {
509 case -ERESTART_RESTARTBLOCK:
510 case -ERESTARTNOHAND:
511 no_system_call_restart:
512 regs->regs[0] = -EINTR;
513 break;
515 case -ERESTARTSYS:
516 if (!(sa->sa_flags & SA_RESTART))
517 goto no_system_call_restart;
518 /* fallthrough */
519 case -ERESTARTNOINTR:
520 regs->regs[0] = save_r0;
521 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
522 break;
527 * OK, we're invoking a handler
529 static int
530 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
531 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
533 int ret;
535 /* Set up the stack frame */
536 if (ka->sa.sa_flags & SA_SIGINFO)
537 ret = setup_rt_frame(sig, ka, info, oldset, regs);
538 else
539 ret = setup_frame(sig, ka, oldset, regs);
541 if (ka->sa.sa_flags & SA_ONESHOT)
542 ka->sa.sa_handler = SIG_DFL;
544 if (ret == 0) {
545 spin_lock_irq(&current->sighand->siglock);
546 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
547 if (!(ka->sa.sa_flags & SA_NODEFER))
548 sigaddset(&current->blocked,sig);
549 recalc_sigpending();
550 spin_unlock_irq(&current->sighand->siglock);
553 return ret;
557 * Note that 'init' is a special process: it doesn't get signals it doesn't
558 * want to handle. Thus you cannot kill init even with a SIGKILL even by
559 * mistake.
561 * Note that we go through the signals twice: once to check the signals that
562 * the kernel can handle, and then we build all the user-level signal handling
563 * stack-frames in one go after that.
565 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
567 siginfo_t info;
568 int signr;
569 struct k_sigaction ka;
570 sigset_t *oldset;
573 * We want the common case to go fast, which
574 * is why we may in certain cases get here from
575 * kernel mode. Just return without doing anything
576 * if so.
578 if (!user_mode(regs))
579 return;
581 if (try_to_freeze())
582 goto no_signal;
584 if (test_thread_flag(TIF_RESTORE_SIGMASK))
585 oldset = &current->saved_sigmask;
586 else
587 oldset = &current->blocked;
589 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
590 if (signr > 0) {
591 handle_syscall_restart(save_r0, regs, &ka.sa);
593 /* Whee! Actually deliver the signal. */
594 if (handle_signal(signr, &ka, &info, oldset,
595 regs, save_r0) == 0) {
596 /* a signal was successfully delivered; the saved
597 * sigmask will have been stored in the signal frame,
598 * and will be restored by sigreturn, so we can simply
599 * clear the TIF_RESTORE_SIGMASK flag */
600 if (test_thread_flag(TIF_RESTORE_SIGMASK))
601 clear_thread_flag(TIF_RESTORE_SIGMASK);
603 tracehook_signal_handler(signr, &info, &ka, regs,
604 test_thread_flag(TIF_SINGLESTEP));
607 return;
610 no_signal:
611 /* Did we come from a system call? */
612 if (regs->tra >= 0) {
613 /* Restart the system call - no handlers present */
614 if (regs->regs[0] == -ERESTARTNOHAND ||
615 regs->regs[0] == -ERESTARTSYS ||
616 regs->regs[0] == -ERESTARTNOINTR) {
617 regs->regs[0] = save_r0;
618 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
619 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
620 regs->pc -= instruction_size(ctrl_inw(regs->pc - 4));
621 regs->regs[3] = __NR_restart_syscall;
625 /* if there's no signal to deliver, we just put the saved sigmask
626 * back */
627 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
628 clear_thread_flag(TIF_RESTORE_SIGMASK);
629 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
633 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
634 unsigned long thread_info_flags)
636 /* deal with pending signal delivery */
637 if (thread_info_flags & _TIF_SIGPENDING)
638 do_signal(regs, save_r0);
640 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
641 clear_thread_flag(TIF_NOTIFY_RESUME);
642 tracehook_notify_resume(regs);