2 * arch/xtensa/kernel/signal.c
4 * Default platform functions.
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
10 * Copyright (C) 2005, 2006 Tensilica Inc.
11 * Copyright (C) 1991, 1992 Linus Torvalds
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 * Chris Zankel <chris@zankel.net>
15 * Joe Taylor <joe@tensilica.com>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/personality.h>
22 #include <linux/tracehook.h>
23 #include <linux/sched/task_stack.h>
25 #include <asm/ucontext.h>
26 #include <linux/uaccess.h>
27 #include <asm/cacheflush.h>
28 #include <asm/coprocessor.h>
29 #include <asm/unistd.h>
31 extern struct task_struct *coproc_owners[];
40 #if XTENSA_HAVE_COPROCESSORS
41 xtregs_coprocessor_t cp;
44 unsigned char retcode[6];
45 unsigned int window[4];
48 #if defined(USER_SUPPORT_WINDOWED)
50 * Flush register windows stored in pt_regs to stack.
51 * Returns 1 for errors.
55 flush_window_regs_user(struct pt_regs *regs)
57 const unsigned long ws = regs->windowstart;
58 const unsigned long wb = regs->windowbase;
64 /* Return if no other frames. */
69 /* Rotate windowmask and skip empty frames. */
71 wm = (ws >> wb) | (ws << (XCHAL_NUM_AREGS / 4 - wb));
72 base = (XCHAL_NUM_AREGS / 4) - (regs->wmask >> 4);
74 /* For call8 or call12 frames, we need the previous stack pointer. */
76 if ((regs->wmask & 2) == 0)
77 if (__get_user(sp, (int*)(regs->areg[base * 4 + 1] - 12)))
80 /* Spill frames to stack. */
82 while (base < XCHAL_NUM_AREGS / 4) {
87 /* Save registers a4..a7 (call8) or a4...a11 (call12) */
89 if (m & 2) { /* call4 */
92 } else if (m & 4) { /* call8 */
93 if (copy_to_user(&SPILL_SLOT_CALL8(sp, 4),
94 ®s->areg[(base + 1) * 4], 16))
98 } else if (m & 8) { /* call12 */
99 if (copy_to_user(&SPILL_SLOT_CALL12(sp, 4),
100 ®s->areg[(base + 1) * 4], 32))
105 /* Save current frame a0..a3 under next SP */
107 sp = regs->areg[((base + inc) * 4 + 1) % XCHAL_NUM_AREGS];
108 if (copy_to_user(&SPILL_SLOT(sp, 0), ®s->areg[base * 4], 16))
111 /* Get current stack pointer for next loop iteration. */
113 sp = regs->areg[base * 4 + 1];
118 regs->windowstart = 1 << wb;
127 flush_window_regs_user(struct pt_regs *regs)
134 * Note: We don't copy double exception 'regs', we have to finish double exc.
135 * first before we return to signal handler! This dbl.exc.handler might cause
136 * another double exception, but I think we are fine as the situation is the
137 * same as if we had returned to the signal handerl and got an interrupt
142 setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs)
144 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
145 struct thread_info *ti = current_thread_info();
148 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
157 err |= flush_window_regs_user(regs);
158 err |= __copy_to_user (sc->sc_a, regs->areg, 16 * 4);
159 err |= __put_user(0, &sc->sc_xtregs);
164 #if XTENSA_HAVE_COPROCESSORS
165 coprocessor_flush_all(ti);
166 coprocessor_release_all(ti);
167 err |= __copy_to_user(&frame->xtregs.cp, &ti->xtregs_cp,
168 sizeof (frame->xtregs.cp));
170 err |= __copy_to_user(&frame->xtregs.opt, ®s->xtregs_opt,
171 sizeof (xtregs_opt_t));
172 err |= __copy_to_user(&frame->xtregs.user, &ti->xtregs_user,
173 sizeof (xtregs_user_t));
175 err |= __put_user(err ? NULL : &frame->xtregs, &sc->sc_xtregs);
181 restore_sigcontext(struct pt_regs *regs, struct rt_sigframe __user *frame)
183 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
184 struct thread_info *ti = current_thread_info();
185 unsigned int err = 0;
188 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
196 /* All registers were flushed to stack. Start with a pristine frame. */
199 regs->windowbase = 0;
200 regs->windowstart = 1;
202 regs->syscall = NO_SYSCALL; /* disable syscall checks */
204 /* For PS, restore only PS.CALLINC.
205 * Assume that all other bits are either the same as for the signal
206 * handler, or the user mode value doesn't matter (e.g. PS.OWB).
208 err |= __get_user(ps, &sc->sc_ps);
209 regs->ps = (regs->ps & ~PS_CALLINC_MASK) | (ps & PS_CALLINC_MASK);
211 /* Additional corruption checks */
213 if ((regs->lcount > 0)
214 && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
217 err |= __copy_from_user(regs->areg, sc->sc_a, 16 * 4);
222 /* The signal handler may have used coprocessors in which
223 * case they are still enabled. We disable them to force a
224 * reloading of the original task's CP state by the lazy
225 * context-switching mechanisms of CP exception handling.
226 * Also, we essentially discard any coprocessor state that the
227 * signal handler created. */
229 #if XTENSA_HAVE_COPROCESSORS
230 coprocessor_release_all(ti);
231 err |= __copy_from_user(&ti->xtregs_cp, &frame->xtregs.cp,
232 sizeof (frame->xtregs.cp));
234 err |= __copy_from_user(&ti->xtregs_user, &frame->xtregs.user,
235 sizeof (xtregs_user_t));
236 err |= __copy_from_user(®s->xtregs_opt, &frame->xtregs.opt,
237 sizeof (xtregs_opt_t));
244 * Do a signal return; undo the signal stack.
247 asmlinkage long xtensa_rt_sigreturn(void)
249 struct pt_regs *regs = current_pt_regs();
250 struct rt_sigframe __user *frame;
254 /* Always make any pending restarted system calls return -EINTR */
255 current->restart_block.fn = do_no_restart_syscall;
258 panic("rt_sigreturn in double exception!\n");
260 frame = (struct rt_sigframe __user *) regs->areg[1];
262 if (!access_ok(frame, sizeof(*frame)))
265 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
268 set_current_blocked(&set);
270 if (restore_sigcontext(regs, frame))
275 if (restore_altstack(&frame->uc.uc_stack))
288 * Set up a signal frame.
292 gen_return_code(unsigned char *codemem)
297 * The 12-bit immediate is really split up within the 24-bit MOVI
298 * instruction. As long as the above system call numbers fit within
299 * 8-bits, the following code works fine. See the Xtensa ISA for
303 #if __NR_rt_sigreturn > 255
304 # error Generating the MOVI instruction below breaks!
307 #ifdef __XTENSA_EB__ /* Big Endian version */
308 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
309 err |= __put_user(0x22, &codemem[0]);
310 err |= __put_user(0x0a, &codemem[1]);
311 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
312 /* Generate instruction: SYSCALL */
313 err |= __put_user(0x00, &codemem[3]);
314 err |= __put_user(0x05, &codemem[4]);
315 err |= __put_user(0x00, &codemem[5]);
317 #elif defined __XTENSA_EL__ /* Little Endian version */
318 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
319 err |= __put_user(0x22, &codemem[0]);
320 err |= __put_user(0xa0, &codemem[1]);
321 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
322 /* Generate instruction: SYSCALL */
323 err |= __put_user(0x00, &codemem[3]);
324 err |= __put_user(0x50, &codemem[4]);
325 err |= __put_user(0x00, &codemem[5]);
327 # error Must use compiler for Xtensa processors.
330 /* Flush generated code out of the data cache */
333 __invalidate_icache_range((unsigned long)codemem, 6UL);
334 __flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
341 static int setup_frame(struct ksignal *ksig, sigset_t *set,
342 struct pt_regs *regs)
344 struct rt_sigframe *frame;
345 int err = 0, sig = ksig->sig;
346 unsigned long sp, ra, tp, ps;
351 if ((ksig->ka.sa.sa_flags & SA_ONSTACK) != 0 && sas_ss_flags(sp) == 0) {
352 sp = current->sas_ss_sp + current->sas_ss_size;
355 frame = (void *)((sp - sizeof(*frame)) & -16ul);
358 panic ("Double exception sys_sigreturn\n");
360 if (!access_ok(frame, sizeof(*frame))) {
364 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
365 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
368 /* Create the user context. */
370 err |= __put_user(0, &frame->uc.uc_flags);
371 err |= __put_user(0, &frame->uc.uc_link);
372 err |= __save_altstack(&frame->uc.uc_stack, regs->areg[1]);
373 err |= setup_sigcontext(frame, regs);
374 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
376 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
377 ra = (unsigned long)ksig->ka.sa.sa_restorer;
380 /* Create sys_rt_sigreturn syscall in stack frame */
382 err |= gen_return_code(frame->retcode);
387 ra = (unsigned long) frame->retcode;
391 * Create signal handler execution context.
392 * Return context not modified until this point.
395 /* Set up registers for signal handler; preserve the threadptr */
396 tp = regs->threadptr;
398 start_thread(regs, (unsigned long) ksig->ka.sa.sa_handler,
399 (unsigned long) frame);
401 /* Set up a stack frame for a call4 if userspace uses windowed ABI */
402 if (ps & PS_WOE_MASK) {
405 (((unsigned long) ra) & 0x3fffffff) | 0x40000000;
406 ps = (ps & ~(PS_CALLINC_MASK | PS_OWB_MASK)) |
407 (1 << PS_CALLINC_SHIFT);
410 regs->areg[base] = (unsigned long) ra;
412 regs->areg[base + 2] = (unsigned long) sig;
413 regs->areg[base + 3] = (unsigned long) &frame->info;
414 regs->areg[base + 4] = (unsigned long) &frame->uc;
415 regs->threadptr = tp;
418 pr_debug("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08lx\n",
419 current->comm, current->pid, sig, frame, regs->pc);
425 * Note that 'init' is a special process: it doesn't get signals it doesn't
426 * want to handle. Thus you cannot kill init even with a SIGKILL even by
429 * Note that we go through the signals twice: once to check the signals that
430 * the kernel can handle, and then we build all the user-level signal handling
431 * stack-frames in one go after that.
433 static void do_signal(struct pt_regs *regs)
437 task_pt_regs(current)->icountlevel = 0;
439 if (get_signal(&ksig)) {
442 /* Are we from a system call? */
444 if (regs->syscall != NO_SYSCALL) {
446 /* If so, check system call restarting.. */
448 switch (regs->areg[2]) {
449 case -ERESTARTNOHAND:
450 case -ERESTART_RESTARTBLOCK:
451 regs->areg[2] = -EINTR;
455 if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
456 regs->areg[2] = -EINTR;
460 case -ERESTARTNOINTR:
461 regs->areg[2] = regs->syscall;
467 if (regs->areg[2] != 0)
472 /* Whee! Actually deliver the signal. */
473 /* Set up the stack frame */
474 ret = setup_frame(&ksig, sigmask_to_save(), regs);
475 signal_setup_done(ret, &ksig, 0);
476 if (current->ptrace & PT_SINGLESTEP)
477 task_pt_regs(current)->icountlevel = 1;
482 /* Did we come from a system call? */
483 if (regs->syscall != NO_SYSCALL) {
484 /* Restart the system call - no handlers present */
485 switch (regs->areg[2]) {
486 case -ERESTARTNOHAND:
488 case -ERESTARTNOINTR:
489 regs->areg[2] = regs->syscall;
492 case -ERESTART_RESTARTBLOCK:
493 regs->areg[2] = __NR_restart_syscall;
499 /* If there's no signal to deliver, we just restore the saved mask. */
500 restore_saved_sigmask();
502 if (current->ptrace & PT_SINGLESTEP)
503 task_pt_regs(current)->icountlevel = 1;
507 void do_notify_resume(struct pt_regs *regs)
509 if (test_thread_flag(TIF_SIGPENDING) ||
510 test_thread_flag(TIF_NOTIFY_SIGNAL))
513 if (test_thread_flag(TIF_NOTIFY_RESUME))
514 tracehook_notify_resume(regs);