This client driver allows you to use a GPIO pin as a source for PPS
[linux-2.6/next.git] / arch / sh / kernel / signal_32.c
blob68ecb698a444b41645085b93b58b2265065fffa2
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 * The following define adds a 64 byte gap between the signal
45 * stack frame and previous contents of the stack. This allows
46 * frame unwinding in a function epilogue but only if a frame
47 * pointer is used in the function. This is necessary because
48 * current gcc compilers (<4.3) do not generate unwind info on
49 * SH for function epilogues.
51 #define UNWINDGUARD 64
54 * Atomically swap in the new signal mask, and wait for a signal.
56 asmlinkage int
57 sys_sigsuspend(old_sigset_t mask,
58 unsigned long r5, unsigned long r6, unsigned long r7,
59 struct pt_regs __regs)
61 sigset_t blocked;
63 current->saved_sigmask = current->blocked;
65 mask &= _BLOCKABLE;
66 siginitset(&blocked, mask);
67 set_current_blocked(&blocked);
69 current->state = TASK_INTERRUPTIBLE;
70 schedule();
71 set_restore_sigmask();
73 return -ERESTARTNOHAND;
76 asmlinkage int
77 sys_sigaction(int sig, const struct old_sigaction __user *act,
78 struct old_sigaction __user *oact)
80 struct k_sigaction new_ka, old_ka;
81 int ret;
83 if (act) {
84 old_sigset_t mask;
85 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
86 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
87 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
88 return -EFAULT;
89 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
90 __get_user(mask, &act->sa_mask);
91 siginitset(&new_ka.sa.sa_mask, mask);
94 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
96 if (!ret && oact) {
97 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
98 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
99 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
100 return -EFAULT;
101 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
102 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
105 return ret;
108 asmlinkage int
109 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
110 unsigned long r6, unsigned long r7,
111 struct pt_regs __regs)
113 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
115 return do_sigaltstack(uss, uoss, regs->regs[15]);
120 * Do a signal return; undo the signal stack.
123 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
124 #if defined(CONFIG_CPU_SH2)
125 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
126 #else
127 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
128 #endif
129 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
131 struct sigframe
133 struct sigcontext sc;
134 unsigned long extramask[_NSIG_WORDS-1];
135 u16 retcode[8];
138 struct rt_sigframe
140 struct siginfo info;
141 struct ucontext uc;
142 u16 retcode[8];
145 #ifdef CONFIG_SH_FPU
146 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
148 struct task_struct *tsk = current;
150 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
151 return 0;
153 set_used_math();
154 return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
155 sizeof(long)*(16*2+2));
158 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
159 struct pt_regs *regs)
161 struct task_struct *tsk = current;
163 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
164 return 0;
166 if (!used_math()) {
167 __put_user(0, &sc->sc_ownedfp);
168 return 0;
171 __put_user(1, &sc->sc_ownedfp);
173 /* This will cause a "finit" to be triggered by the next
174 attempted FPU operation by the 'current' process.
176 clear_used_math();
178 unlazy_fpu(tsk, regs);
179 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
180 sizeof(long)*(16*2+2));
182 #endif /* CONFIG_SH_FPU */
184 static int
185 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
187 unsigned int err = 0;
189 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
190 COPY(regs[1]);
191 COPY(regs[2]); COPY(regs[3]);
192 COPY(regs[4]); COPY(regs[5]);
193 COPY(regs[6]); COPY(regs[7]);
194 COPY(regs[8]); COPY(regs[9]);
195 COPY(regs[10]); COPY(regs[11]);
196 COPY(regs[12]); COPY(regs[13]);
197 COPY(regs[14]); COPY(regs[15]);
198 COPY(gbr); COPY(mach);
199 COPY(macl); COPY(pr);
200 COPY(sr); COPY(pc);
201 #undef COPY
203 #ifdef CONFIG_SH_FPU
204 if (boot_cpu_data.flags & CPU_HAS_FPU) {
205 int owned_fp;
206 struct task_struct *tsk = current;
208 regs->sr |= SR_FD; /* Release FPU */
209 clear_fpu(tsk, regs);
210 clear_used_math();
211 __get_user (owned_fp, &sc->sc_ownedfp);
212 if (owned_fp)
213 err |= restore_sigcontext_fpu(sc);
215 #endif
217 regs->tra = -1; /* disable syscall checks */
218 err |= __get_user(*r0_p, &sc->sc_regs[0]);
219 return err;
222 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
223 unsigned long r6, unsigned long r7,
224 struct pt_regs __regs)
226 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
227 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
228 sigset_t set;
229 int r0;
231 /* Always make any pending restarted system calls return -EINTR */
232 current_thread_info()->restart_block.fn = do_no_restart_syscall;
234 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
235 goto badframe;
237 if (__get_user(set.sig[0], &frame->sc.oldmask)
238 || (_NSIG_WORDS > 1
239 && __copy_from_user(&set.sig[1], &frame->extramask,
240 sizeof(frame->extramask))))
241 goto badframe;
243 sigdelsetmask(&set, ~_BLOCKABLE);
244 set_current_blocked(&set);
246 if (restore_sigcontext(regs, &frame->sc, &r0))
247 goto badframe;
248 return r0;
250 badframe:
251 force_sig(SIGSEGV, current);
252 return 0;
255 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
256 unsigned long r6, unsigned long r7,
257 struct pt_regs __regs)
259 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
260 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
261 sigset_t set;
262 int r0;
264 /* Always make any pending restarted system calls return -EINTR */
265 current_thread_info()->restart_block.fn = do_no_restart_syscall;
267 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
268 goto badframe;
270 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
271 goto badframe;
273 sigdelsetmask(&set, ~_BLOCKABLE);
274 set_current_blocked(&set);
276 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
277 goto badframe;
279 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
280 regs->regs[15]) == -EFAULT)
281 goto badframe;
283 return r0;
285 badframe:
286 force_sig(SIGSEGV, current);
287 return 0;
291 * Set up a signal frame.
294 static int
295 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
296 unsigned long mask)
298 int err = 0;
300 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
301 COPY(regs[0]); COPY(regs[1]);
302 COPY(regs[2]); COPY(regs[3]);
303 COPY(regs[4]); COPY(regs[5]);
304 COPY(regs[6]); COPY(regs[7]);
305 COPY(regs[8]); COPY(regs[9]);
306 COPY(regs[10]); COPY(regs[11]);
307 COPY(regs[12]); COPY(regs[13]);
308 COPY(regs[14]); COPY(regs[15]);
309 COPY(gbr); COPY(mach);
310 COPY(macl); COPY(pr);
311 COPY(sr); COPY(pc);
312 #undef COPY
314 #ifdef CONFIG_SH_FPU
315 err |= save_sigcontext_fpu(sc, regs);
316 #endif
318 /* non-iBCS2 extensions.. */
319 err |= __put_user(mask, &sc->oldmask);
321 return err;
325 * Determine which stack to use..
327 static inline void __user *
328 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
330 if (ka->sa.sa_flags & SA_ONSTACK) {
331 if (sas_ss_flags(sp) == 0)
332 sp = current->sas_ss_sp + current->sas_ss_size;
335 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
338 /* These symbols are defined with the addresses in the vsyscall page.
339 See vsyscall-trapa.S. */
340 extern void __kernel_sigreturn(void);
341 extern void __kernel_rt_sigreturn(void);
343 static int setup_frame(int sig, struct k_sigaction *ka,
344 sigset_t *set, struct pt_regs *regs)
346 struct sigframe __user *frame;
347 int err = 0;
348 int signal;
350 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
352 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
353 goto give_sigsegv;
355 signal = current_thread_info()->exec_domain
356 && current_thread_info()->exec_domain->signal_invmap
357 && sig < 32
358 ? current_thread_info()->exec_domain->signal_invmap[sig]
359 : sig;
361 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
363 if (_NSIG_WORDS > 1)
364 err |= __copy_to_user(frame->extramask, &set->sig[1],
365 sizeof(frame->extramask));
367 /* Set up to return from userspace. If provided, use a stub
368 already in userspace. */
369 if (ka->sa.sa_flags & SA_RESTORER) {
370 regs->pr = (unsigned long) ka->sa.sa_restorer;
371 #ifdef CONFIG_VSYSCALL
372 } else if (likely(current->mm->context.vdso)) {
373 regs->pr = VDSO_SYM(&__kernel_sigreturn);
374 #endif
375 } else {
376 /* Generate return code (system call to sigreturn) */
377 err |= __put_user(MOVW(7), &frame->retcode[0]);
378 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
379 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
380 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
381 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
382 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
383 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
384 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
385 regs->pr = (unsigned long) frame->retcode;
386 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
389 if (err)
390 goto give_sigsegv;
392 /* Set up registers for signal handler */
393 regs->regs[15] = (unsigned long) frame;
394 regs->regs[4] = signal; /* Arg for signal handler */
395 regs->regs[5] = 0;
396 regs->regs[6] = (unsigned long) &frame->sc;
398 if (current->personality & FDPIC_FUNCPTRS) {
399 struct fdpic_func_descriptor __user *funcptr =
400 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
402 __get_user(regs->pc, &funcptr->text);
403 __get_user(regs->regs[12], &funcptr->GOT);
404 } else
405 regs->pc = (unsigned long)ka->sa.sa_handler;
407 set_fs(USER_DS);
409 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
410 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
412 return 0;
414 give_sigsegv:
415 force_sigsegv(sig, current);
416 return -EFAULT;
419 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
420 sigset_t *set, struct pt_regs *regs)
422 struct rt_sigframe __user *frame;
423 int err = 0;
424 int signal;
426 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
428 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
429 goto give_sigsegv;
431 signal = current_thread_info()->exec_domain
432 && current_thread_info()->exec_domain->signal_invmap
433 && sig < 32
434 ? current_thread_info()->exec_domain->signal_invmap[sig]
435 : sig;
437 err |= copy_siginfo_to_user(&frame->info, info);
439 /* Create the ucontext. */
440 err |= __put_user(0, &frame->uc.uc_flags);
441 err |= __put_user(NULL, &frame->uc.uc_link);
442 err |= __put_user((void *)current->sas_ss_sp,
443 &frame->uc.uc_stack.ss_sp);
444 err |= __put_user(sas_ss_flags(regs->regs[15]),
445 &frame->uc.uc_stack.ss_flags);
446 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
447 err |= setup_sigcontext(&frame->uc.uc_mcontext,
448 regs, set->sig[0]);
449 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
451 /* Set up to return from userspace. If provided, use a stub
452 already in userspace. */
453 if (ka->sa.sa_flags & SA_RESTORER) {
454 regs->pr = (unsigned long) ka->sa.sa_restorer;
455 #ifdef CONFIG_VSYSCALL
456 } else if (likely(current->mm->context.vdso)) {
457 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
458 #endif
459 } else {
460 /* Generate return code (system call to rt_sigreturn) */
461 err |= __put_user(MOVW(7), &frame->retcode[0]);
462 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
463 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
464 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
465 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
466 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
467 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
468 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
469 regs->pr = (unsigned long) frame->retcode;
470 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
473 if (err)
474 goto give_sigsegv;
476 /* Set up registers for signal handler */
477 regs->regs[15] = (unsigned long) frame;
478 regs->regs[4] = signal; /* Arg for signal handler */
479 regs->regs[5] = (unsigned long) &frame->info;
480 regs->regs[6] = (unsigned long) &frame->uc;
482 if (current->personality & FDPIC_FUNCPTRS) {
483 struct fdpic_func_descriptor __user *funcptr =
484 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
486 __get_user(regs->pc, &funcptr->text);
487 __get_user(regs->regs[12], &funcptr->GOT);
488 } else
489 regs->pc = (unsigned long)ka->sa.sa_handler;
491 set_fs(USER_DS);
493 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
494 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
496 return 0;
498 give_sigsegv:
499 force_sigsegv(sig, current);
500 return -EFAULT;
503 static inline void
504 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
505 struct sigaction *sa)
507 /* If we're not from a syscall, bail out */
508 if (regs->tra < 0)
509 return;
511 /* check for system call restart.. */
512 switch (regs->regs[0]) {
513 case -ERESTART_RESTARTBLOCK:
514 case -ERESTARTNOHAND:
515 no_system_call_restart:
516 regs->regs[0] = -EINTR;
517 break;
519 case -ERESTARTSYS:
520 if (!(sa->sa_flags & SA_RESTART))
521 goto no_system_call_restart;
522 /* fallthrough */
523 case -ERESTARTNOINTR:
524 regs->regs[0] = save_r0;
525 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
526 break;
531 * OK, we're invoking a handler
533 static int
534 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
535 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
537 int ret;
539 /* Set up the stack frame */
540 if (ka->sa.sa_flags & SA_SIGINFO)
541 ret = setup_rt_frame(sig, ka, info, oldset, regs);
542 else
543 ret = setup_frame(sig, ka, oldset, regs);
545 if (ret == 0)
546 block_sigmask(ka, sig);
548 return ret;
552 * Note that 'init' is a special process: it doesn't get signals it doesn't
553 * want to handle. Thus you cannot kill init even with a SIGKILL even by
554 * mistake.
556 * Note that we go through the signals twice: once to check the signals that
557 * the kernel can handle, and then we build all the user-level signal handling
558 * stack-frames in one go after that.
560 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
562 siginfo_t info;
563 int signr;
564 struct k_sigaction ka;
565 sigset_t *oldset;
568 * We want the common case to go fast, which
569 * is why we may in certain cases get here from
570 * kernel mode. Just return without doing anything
571 * if so.
573 if (!user_mode(regs))
574 return;
576 if (try_to_freeze())
577 goto no_signal;
579 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
580 oldset = &current->saved_sigmask;
581 else
582 oldset = &current->blocked;
584 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
585 if (signr > 0) {
586 handle_syscall_restart(save_r0, regs, &ka.sa);
588 /* Whee! Actually deliver the signal. */
589 if (handle_signal(signr, &ka, &info, oldset,
590 regs, save_r0) == 0) {
592 * A signal was successfully delivered; the saved
593 * sigmask will have been stored in the signal frame,
594 * and will be restored by sigreturn, so we can simply
595 * clear the TS_RESTORE_SIGMASK flag
597 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
599 tracehook_signal_handler(signr, &info, &ka, regs,
600 test_thread_flag(TIF_SINGLESTEP));
603 return;
606 no_signal:
607 /* Did we come from a system call? */
608 if (regs->tra >= 0) {
609 /* Restart the system call - no handlers present */
610 if (regs->regs[0] == -ERESTARTNOHAND ||
611 regs->regs[0] == -ERESTARTSYS ||
612 regs->regs[0] == -ERESTARTNOINTR) {
613 regs->regs[0] = save_r0;
614 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
615 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
616 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
617 regs->regs[3] = __NR_restart_syscall;
622 * If there's no signal to deliver, we just put the saved sigmask
623 * back.
625 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
626 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
627 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
631 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
632 unsigned long thread_info_flags)
634 /* deal with pending signal delivery */
635 if (thread_info_flags & _TIF_SIGPENDING)
636 do_signal(regs, save_r0);
638 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
639 clear_thread_flag(TIF_NOTIFY_RESUME);
640 tracehook_notify_resume(regs);
641 if (current->replacement_session_keyring)
642 key_replace_session_keyring();