1 // TODO coprocessor stuff
3 * linux/arch/xtensa/kernel/signal.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * Joe Taylor <joe@tensilica.com>
9 * Chris Zankel <chris@zankel.net>
15 #include <asm/variant/core.h>
16 #include <asm/coprocessor.h>
17 #include <linux/sched.h>
19 #include <linux/smp.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27 #include <linux/personality.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/cacheflush.h>
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options,
39 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
41 extern struct task_struct *coproc_owners[];
45 * Atomically swap in the new signal mask, and wait for a signal.
48 int xtensa_sigsuspend(struct pt_regs *regs)
50 old_sigset_t mask = (old_sigset_t) regs->areg[3];
54 spin_lock_irq(¤t->sighand->siglock);
55 saveset = current->blocked;
56 siginitset(¤t->blocked, mask);
58 spin_unlock_irq(¤t->sighand->siglock);
60 regs->areg[2] = -EINTR;
62 current->state = TASK_INTERRUPTIBLE;
64 if (do_signal(regs, &saveset))
70 xtensa_rt_sigsuspend(struct pt_regs *regs)
72 sigset_t *unewset = (sigset_t *) regs->areg[4];
73 size_t sigsetsize = (size_t) regs->areg[3];
74 sigset_t saveset, newset;
75 /* XXX: Don't preclude handling different sized sigset_t's. */
76 if (sigsetsize != sizeof(sigset_t))
79 if (copy_from_user(&newset, unewset, sizeof(newset)))
81 sigdelsetmask(&newset, ~_BLOCKABLE);
82 spin_lock_irq(¤t->sighand->siglock);
83 saveset = current->blocked;
84 current->blocked = newset;
86 spin_unlock_irq(¤t->sighand->siglock);
88 regs->areg[2] = -EINTR;
90 current->state = TASK_INTERRUPTIBLE;
92 if (do_signal(regs, &saveset))
98 xtensa_sigaction(int sig, const struct old_sigaction *act,
99 struct old_sigaction *oact)
101 struct k_sigaction new_ka, old_ka;
106 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
107 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
108 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
110 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
111 __get_user(mask, &act->sa_mask);
112 siginitset(&new_ka.sa.sa_mask, mask);
115 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
118 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
119 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
120 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
122 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
123 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
130 xtensa_sigaltstack(struct pt_regs *regs)
132 const stack_t *uss = (stack_t *) regs->areg[4];
133 stack_t *uoss = (stack_t *) regs->areg[3];
136 panic ("Double exception sys_sigreturn\n");
139 return do_sigaltstack(uss, uoss, regs->areg[1]);
144 * Do a signal return; undo the signal stack.
149 struct sigcontext sc;
150 struct _cpstate cpstate;
151 unsigned long extramask[_NSIG_WORDS-1];
152 unsigned char retcode[6];
153 unsigned int reserved[4]; /* Reserved area for chaining */
154 unsigned int window[4]; /* Window of 4 registers for initial context */
161 struct _cpstate cpstate;
162 unsigned char retcode[6];
163 unsigned int reserved[4]; /* Reserved area for chaining */
164 unsigned int window[4]; /* Window of 4 registers for initial context */
167 extern void release_all_cp (struct task_struct *);
170 // FIXME restore_cpextra
172 restore_cpextra (struct _cpstate *buf)
175 /* The signal handler may have used coprocessors in which
176 * case they are still enabled. We disable them to force a
177 * reloading of the original task's CP state by the lazy
178 * context-switching mechanisms of CP exception handling.
179 * Also, we essentially discard any coprocessor state that the
180 * signal handler created. */
182 struct task_struct *tsk = current;
184 return __copy_from_user(tsk->thread.cpextra, buf, XTENSA_CP_EXTRA_SIZE);
189 /* Note: We don't copy double exception 'tregs', we have to finish double exc. first before we return to signal handler! This dbl.exc.handler might cause another double exception, but I think we are fine as the situation is the same as if we had returned to the signal handerl and got an interrupt immediately...
194 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
196 struct thread_struct *thread;
197 unsigned int err = 0;
199 struct _cpstate *buf;
201 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
213 /* For PS, restore only PS.CALLINC.
214 * Assume that all other bits are either the same as for the signal
215 * handler, or the user mode value doesn't matter (e.g. PS.OWB).
217 err |= __get_user(ps, &sc->sc_ps);
218 regs->ps = (regs->ps & ~PS_CALLINC_MASK)
219 | (ps & PS_CALLINC_MASK);
221 /* Additional corruption checks */
223 if ((regs->windowbase >= (XCHAL_NUM_AREGS/4))
224 || ((regs->windowstart & ~((1<<(XCHAL_NUM_AREGS/4)) - 1)) != 0) )
226 if ((regs->lcount > 0)
227 && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
230 /* Restore extended register state.
231 * See struct thread_struct in processor.h.
233 thread = ¤t->thread;
235 err |= __copy_from_user (regs->areg, sc->sc_areg, XCHAL_NUM_AREGS*4);
236 err |= __get_user(buf, &sc->sc_cpstate);
238 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
240 err |= restore_cpextra(buf);
243 regs->syscall = -1; /* disable syscall checks */
251 flush_my_cpstate(struct task_struct *tsk)
254 local_irq_save(flags);
257 for (i = 0; i < XCHAL_CP_NUM; i++) {
258 if (tsk == coproc_owners[i]) {
259 xthal_validate_cp(i);
260 xthal_save_cpregs(tsk->thread.cpregs_ptr[i], i);
262 /* Invalidate and "disown" the cp to allow
263 * callers the chance to reset cp state in the
266 xthal_invalidate_cp(i);
267 coproc_owners[i] = 0;
271 local_irq_restore(flags);
276 1: stuff to save, successful
277 -1: stuff to save, error happened
280 save_cpextra (struct _cpstate *buf)
282 #if XCHAL_CP_NUM == 0
286 /* FIXME: If a task has never used a coprocessor, there is
287 * no need to save and restore anything. Tracking this
288 * information would allow us to optimize this section.
289 * Perhaps we can use current->used_math or (current->flags &
290 * PF_USEDFPU) or define a new field in the thread
293 /* We flush any live, task-owned cp state to the task_struct,
294 * then copy it all to the sigframe. Then we clear all
295 * cp/extra state in the task_struct, effectively
296 * clearing/resetting all cp/extra state for the signal
297 * handler (cp-exception handling will load these new values
298 * into the cp/extra registers.) This step is important for
299 * things like a floating-point cp, where the OS must reset
300 * the FCR to the default rounding mode. */
303 struct task_struct *tsk = current;
305 flush_my_cpstate(tsk);
306 /* Note that we just copy everything: 'extra' and 'cp' state together.*/
307 err |= __copy_to_user(buf, tsk->thread.cp_save, XTENSA_CP_EXTRA_SIZE);
308 memset(tsk->thread.cp_save, 0, XTENSA_CP_EXTRA_SIZE);
310 #if (XTENSA_CP_EXTRA_SIZE == 0)
311 #error Sanity check on memset above, cpextra_size should not be zero.
319 setup_sigcontext(struct sigcontext *sc, struct _cpstate *cpstate,
320 struct pt_regs *regs, unsigned long mask)
322 struct thread_struct *thread;
325 //printk("setup_sigcontext\n");
326 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
339 /* Save extended register state.
340 * See struct thread_struct in processor.h.
342 thread = ¤t->thread;
343 err |= __copy_to_user (sc->sc_areg, regs->areg, XCHAL_NUM_AREGS * 4);
344 err |= save_cpextra(cpstate);
345 err |= __put_user(err ? NULL : cpstate, &sc->sc_cpstate);
346 /* non-iBCS2 extensions.. */
347 err |= __put_user(mask, &sc->oldmask);
352 asmlinkage int xtensa_sigreturn(struct pt_regs *regs)
354 struct sigframe *frame = (struct sigframe *)regs->areg[1];
357 panic ("Double exception sys_sigreturn\n");
359 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
362 if (__get_user(set.sig[0], &frame->sc.oldmask)
364 && __copy_from_user(&set.sig[1], &frame->extramask,
365 sizeof(frame->extramask))))
368 sigdelsetmask(&set, ~_BLOCKABLE);
370 spin_lock_irq(¤t->sighand->siglock);
371 current->blocked = set;
373 spin_unlock_irq(¤t->sighand->siglock);
375 if (restore_sigcontext(regs, &frame->sc))
377 return regs->areg[2];
380 force_sig(SIGSEGV, current);
384 asmlinkage int xtensa_rt_sigreturn(struct pt_regs *regs)
386 struct rt_sigframe *frame = (struct rt_sigframe *)regs->areg[1];
392 printk("!!!!!!! DEPC !!!!!!!\n");
396 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
399 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
402 sigdelsetmask(&set, ~_BLOCKABLE);
403 spin_lock_irq(¤t->sighand->siglock);
404 current->blocked = set;
406 spin_unlock_irq(¤t->sighand->siglock);
408 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
412 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
414 /* It is more difficult to avoid calling this function than to
415 call it and ignore errors. */
416 do_sigaltstack(&st, NULL, regs->areg[1]);
421 force_sig(SIGSEGV, current);
426 * Set up a signal frame.
430 * Determine which stack to use..
433 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
435 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! sas_ss_flags(sp))
436 sp = current->sas_ss_sp + current->sas_ss_size;
438 return (void *)((sp - frame_size) & -16ul);
441 #define USE_SIGRETURN 0
442 #define USE_RT_SIGRETURN 1
445 gen_return_code(unsigned char *codemem, unsigned int use_rt_sigreturn)
447 unsigned int retcall;
451 /* Ignoring SA_RESTORER for now; it's supposed to be obsolete,
452 * and the xtensa glibc doesn't use it.
454 if (ka->sa.sa_flags & SA_RESTORER) {
455 regs->pr = (unsigned long) ka->sa.sa_restorer;
460 #if (__NR_sigreturn > 255) || (__NR_rt_sigreturn > 255)
462 /* The 12-bit immediate is really split up within the 24-bit MOVI
463 * instruction. As long as the above system call numbers fit within
464 * 8-bits, the following code works fine. See the Xtensa ISA for
468 #error Generating the MOVI instruction below breaks!
471 retcall = use_rt_sigreturn ? __NR_rt_sigreturn : __NR_sigreturn;
473 #ifdef __XTENSA_EB__ /* Big Endian version */
474 /* Generate instruction: MOVI a2, retcall */
475 err |= __put_user(0x22, &codemem[0]);
476 err |= __put_user(0x0a, &codemem[1]);
477 err |= __put_user(retcall, &codemem[2]);
478 /* Generate instruction: SYSCALL */
479 err |= __put_user(0x00, &codemem[3]);
480 err |= __put_user(0x05, &codemem[4]);
481 err |= __put_user(0x00, &codemem[5]);
483 #elif defined __XTENSA_EL__ /* Little Endian version */
484 /* Generate instruction: MOVI a2, retcall */
485 err |= __put_user(0x22, &codemem[0]);
486 err |= __put_user(0xa0, &codemem[1]);
487 err |= __put_user(retcall, &codemem[2]);
488 /* Generate instruction: SYSCALL */
489 err |= __put_user(0x00, &codemem[3]);
490 err |= __put_user(0x50, &codemem[4]);
491 err |= __put_user(0x00, &codemem[5]);
493 #error Must use compiler for Xtensa processors.
497 /* Flush generated code out of the data cache */
500 __invalidate_icache_range((unsigned long)codemem, 6UL);
501 __flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
508 set_thread_state(struct pt_regs *regs, void *stack, unsigned char *retaddr,
509 void *handler, unsigned long arg1, void *arg2, void *arg3)
511 /* Set up registers for signal handler */
512 start_thread(regs, (unsigned long) handler, (unsigned long) stack);
514 /* Set up a stack frame for a call4
515 * Note: PS.CALLINC is set to one by start_thread
517 regs->areg[4] = (((unsigned long) retaddr) & 0x3fffffff) | 0x40000000;
518 regs->areg[6] = arg1;
519 regs->areg[7] = (unsigned long) arg2;
520 regs->areg[8] = (unsigned long) arg3;
523 static void setup_frame(int sig, struct k_sigaction *ka,
524 sigset_t *set, struct pt_regs *regs)
526 struct sigframe *frame;
530 frame = get_sigframe(ka, regs->areg[1], sizeof(*frame));
533 printk("!!!!!!! DEPC !!!!!!!\n");
538 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
541 signal = current_thread_info()->exec_domain
542 && current_thread_info()->exec_domain->signal_invmap
544 ? current_thread_info()->exec_domain->signal_invmap[sig]
547 err |= setup_sigcontext(&frame->sc, &frame->cpstate, regs, set->sig[0]);
549 if (_NSIG_WORDS > 1) {
550 err |= __copy_to_user(frame->extramask, &set->sig[1],
551 sizeof(frame->extramask));
554 /* Create sys_sigreturn syscall in stack frame */
555 err |= gen_return_code(frame->retcode, USE_SIGRETURN);
560 /* Create signal handler execution context.
561 * Return context not modified until this point.
563 set_thread_state(regs, frame, frame->retcode,
564 ka->sa.sa_handler, signal, &frame->sc, NULL);
566 /* Set access mode to USER_DS. Nomenclature is outdated, but
567 * functionality is used in uaccess.h
573 printk("SIG deliver (%s:%d): signal=%d sp=%p pc=%08x\n",
574 current->comm, current->pid, signal, frame, regs->pc);
581 ka->sa.sa_handler = SIG_DFL;
582 force_sig(SIGSEGV, current);
585 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
586 sigset_t *set, struct pt_regs *regs)
588 struct rt_sigframe *frame;
592 frame = get_sigframe(ka, regs->areg[1], sizeof(*frame));
594 panic ("Double exception sys_sigreturn\n");
596 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
599 signal = current_thread_info()->exec_domain
600 && current_thread_info()->exec_domain->signal_invmap
602 ? current_thread_info()->exec_domain->signal_invmap[sig]
605 err |= copy_siginfo_to_user(&frame->info, info);
607 /* Create the ucontext. */
608 err |= __put_user(0, &frame->uc.uc_flags);
609 err |= __put_user(0, &frame->uc.uc_link);
610 err |= __put_user((void *)current->sas_ss_sp,
611 &frame->uc.uc_stack.ss_sp);
612 err |= __put_user(sas_ss_flags(regs->areg[1]),
613 &frame->uc.uc_stack.ss_flags);
614 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
615 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->cpstate,
617 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
619 /* Create sys_rt_sigreturn syscall in stack frame */
620 err |= gen_return_code(frame->retcode, USE_RT_SIGRETURN);
625 /* Create signal handler execution context.
626 * Return context not modified until this point.
628 set_thread_state(regs, frame, frame->retcode,
629 ka->sa.sa_handler, signal, &frame->info, &frame->uc);
631 /* Set access mode to USER_DS. Nomenclature is outdated, but
632 * functionality is used in uaccess.h
637 printk("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08x\n",
638 current->comm, current->pid, signal, frame, regs->pc);
645 ka->sa.sa_handler = SIG_DFL;
646 force_sig(SIGSEGV, current);
652 * Note that 'init' is a special process: it doesn't get signals it doesn't
653 * want to handle. Thus you cannot kill init even with a SIGKILL even by
656 * Note that we go through the signals twice: once to check the signals that
657 * the kernel can handle, and then we build all the user-level signal handling
658 * stack-frames in one go after that.
660 int do_signal(struct pt_regs *regs, sigset_t *oldset)
664 struct k_sigaction ka;
667 oldset = ¤t->blocked;
669 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
671 /* Are we from a system call? */
672 if (regs->syscall >= 0) {
673 /* If so, check system call restarting.. */
674 switch (regs->areg[2]) {
676 case ERESTART_RESTARTBLOCK:
677 regs->areg[2] = -EINTR;
681 if (!(ka.sa.sa_flags & SA_RESTART)) {
682 regs->areg[2] = -EINTR;
687 regs->areg[2] = regs->syscall;
693 return 0; /* no signals delivered */
695 /* Whee! Actually deliver the signal. */
697 /* Set up the stack frame */
698 if (ka.sa.sa_flags & SA_SIGINFO)
699 setup_rt_frame(signr, &ka, &info, oldset, regs);
701 setup_frame(signr, &ka, oldset, regs);
703 if (ka.sa.sa_flags & SA_ONESHOT)
704 ka.sa.sa_handler = SIG_DFL;
706 spin_lock_irq(¤t->sighand->siglock);
707 sigorsets(¤t->blocked, ¤t->blocked, &ka.sa.sa_mask);
708 if (!(ka.sa.sa_flags & SA_NODEFER))
709 sigaddset(¤t->blocked, signr);
711 spin_unlock_irq(¤t->sighand->siglock);