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];
49 * Flush register windows stored in pt_regs to stack.
50 * Returns 1 for errors.
54 flush_window_regs_user(struct pt_regs
*regs
)
56 const unsigned long ws
= regs
->windowstart
;
57 const unsigned long wb
= regs
->windowbase
;
63 /* Return if no other frames. */
68 /* Rotate windowmask and skip empty frames. */
70 wm
= (ws
>> wb
) | (ws
<< (XCHAL_NUM_AREGS
/ 4 - wb
));
71 base
= (XCHAL_NUM_AREGS
/ 4) - (regs
->wmask
>> 4);
73 /* For call8 or call12 frames, we need the previous stack pointer. */
75 if ((regs
->wmask
& 2) == 0)
76 if (__get_user(sp
, (int*)(regs
->areg
[base
* 4 + 1] - 12)))
79 /* Spill frames to stack. */
81 while (base
< XCHAL_NUM_AREGS
/ 4) {
86 /* Save registers a4..a7 (call8) or a4...a11 (call12) */
88 if (m
& 2) { /* call4 */
91 } else if (m
& 4) { /* call8 */
92 if (copy_to_user(&SPILL_SLOT_CALL8(sp
, 4),
93 ®s
->areg
[(base
+ 1) * 4], 16))
97 } else if (m
& 8) { /* call12 */
98 if (copy_to_user(&SPILL_SLOT_CALL12(sp
, 4),
99 ®s
->areg
[(base
+ 1) * 4], 32))
104 /* Save current frame a0..a3 under next SP */
106 sp
= regs
->areg
[((base
+ inc
) * 4 + 1) % XCHAL_NUM_AREGS
];
107 if (copy_to_user(&SPILL_SLOT(sp
, 0), ®s
->areg
[base
* 4], 16))
110 /* Get current stack pointer for next loop iteration. */
112 sp
= regs
->areg
[base
* 4 + 1];
117 regs
->windowstart
= 1 << wb
;
126 * Note: We don't copy double exception 'regs', we have to finish double exc.
127 * first before we return to signal handler! This dbl.exc.handler might cause
128 * another double exception, but I think we are fine as the situation is the
129 * same as if we had returned to the signal handerl and got an interrupt
134 setup_sigcontext(struct rt_sigframe __user
*frame
, struct pt_regs
*regs
)
136 struct sigcontext __user
*sc
= &frame
->uc
.uc_mcontext
;
137 struct thread_info
*ti
= current_thread_info();
140 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
149 err
|= flush_window_regs_user(regs
);
150 err
|= __copy_to_user (sc
->sc_a
, regs
->areg
, 16 * 4);
151 err
|= __put_user(0, &sc
->sc_xtregs
);
156 #if XTENSA_HAVE_COPROCESSORS
157 coprocessor_flush_all(ti
);
158 coprocessor_release_all(ti
);
159 err
|= __copy_to_user(&frame
->xtregs
.cp
, &ti
->xtregs_cp
,
160 sizeof (frame
->xtregs
.cp
));
162 err
|= __copy_to_user(&frame
->xtregs
.opt
, ®s
->xtregs_opt
,
163 sizeof (xtregs_opt_t
));
164 err
|= __copy_to_user(&frame
->xtregs
.user
, &ti
->xtregs_user
,
165 sizeof (xtregs_user_t
));
167 err
|= __put_user(err
? NULL
: &frame
->xtregs
, &sc
->sc_xtregs
);
173 restore_sigcontext(struct pt_regs
*regs
, struct rt_sigframe __user
*frame
)
175 struct sigcontext __user
*sc
= &frame
->uc
.uc_mcontext
;
176 struct thread_info
*ti
= current_thread_info();
177 unsigned int err
= 0;
180 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
188 /* All registers were flushed to stack. Start with a prestine frame. */
191 regs
->windowbase
= 0;
192 regs
->windowstart
= 1;
194 regs
->syscall
= -1; /* disable syscall checks */
196 /* For PS, restore only PS.CALLINC.
197 * Assume that all other bits are either the same as for the signal
198 * handler, or the user mode value doesn't matter (e.g. PS.OWB).
200 err
|= __get_user(ps
, &sc
->sc_ps
);
201 regs
->ps
= (regs
->ps
& ~PS_CALLINC_MASK
) | (ps
& PS_CALLINC_MASK
);
203 /* Additional corruption checks */
205 if ((regs
->lcount
> 0)
206 && ((regs
->lbeg
> TASK_SIZE
) || (regs
->lend
> TASK_SIZE
)) )
209 err
|= __copy_from_user(regs
->areg
, sc
->sc_a
, 16 * 4);
214 /* The signal handler may have used coprocessors in which
215 * case they are still enabled. We disable them to force a
216 * reloading of the original task's CP state by the lazy
217 * context-switching mechanisms of CP exception handling.
218 * Also, we essentially discard any coprocessor state that the
219 * signal handler created. */
221 #if XTENSA_HAVE_COPROCESSORS
222 coprocessor_release_all(ti
);
223 err
|= __copy_from_user(&ti
->xtregs_cp
, &frame
->xtregs
.cp
,
224 sizeof (frame
->xtregs
.cp
));
226 err
|= __copy_from_user(&ti
->xtregs_user
, &frame
->xtregs
.user
,
227 sizeof (xtregs_user_t
));
228 err
|= __copy_from_user(®s
->xtregs_opt
, &frame
->xtregs
.opt
,
229 sizeof (xtregs_opt_t
));
236 * Do a signal return; undo the signal stack.
239 asmlinkage
long xtensa_rt_sigreturn(long a0
, long a1
, long a2
, long a3
,
240 long a4
, long a5
, struct pt_regs
*regs
)
242 struct rt_sigframe __user
*frame
;
246 /* Always make any pending restarted system calls return -EINTR */
247 current
->restart_block
.fn
= do_no_restart_syscall
;
250 panic("rt_sigreturn in double exception!\n");
252 frame
= (struct rt_sigframe __user
*) regs
->areg
[1];
254 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
257 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
260 set_current_blocked(&set
);
262 if (restore_sigcontext(regs
, frame
))
267 if (restore_altstack(&frame
->uc
.uc_stack
))
273 force_sig(SIGSEGV
, current
);
280 * Set up a signal frame.
284 gen_return_code(unsigned char *codemem
)
289 * The 12-bit immediate is really split up within the 24-bit MOVI
290 * instruction. As long as the above system call numbers fit within
291 * 8-bits, the following code works fine. See the Xtensa ISA for
295 #if __NR_rt_sigreturn > 255
296 # error Generating the MOVI instruction below breaks!
299 #ifdef __XTENSA_EB__ /* Big Endian version */
300 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
301 err
|= __put_user(0x22, &codemem
[0]);
302 err
|= __put_user(0x0a, &codemem
[1]);
303 err
|= __put_user(__NR_rt_sigreturn
, &codemem
[2]);
304 /* Generate instruction: SYSCALL */
305 err
|= __put_user(0x00, &codemem
[3]);
306 err
|= __put_user(0x05, &codemem
[4]);
307 err
|= __put_user(0x00, &codemem
[5]);
309 #elif defined __XTENSA_EL__ /* Little Endian version */
310 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
311 err
|= __put_user(0x22, &codemem
[0]);
312 err
|= __put_user(0xa0, &codemem
[1]);
313 err
|= __put_user(__NR_rt_sigreturn
, &codemem
[2]);
314 /* Generate instruction: SYSCALL */
315 err
|= __put_user(0x00, &codemem
[3]);
316 err
|= __put_user(0x50, &codemem
[4]);
317 err
|= __put_user(0x00, &codemem
[5]);
319 # error Must use compiler for Xtensa processors.
322 /* Flush generated code out of the data cache */
325 __invalidate_icache_range((unsigned long)codemem
, 6UL);
326 __flush_invalidate_dcache_range((unsigned long)codemem
, 6UL);
333 static int setup_frame(struct ksignal
*ksig
, sigset_t
*set
,
334 struct pt_regs
*regs
)
336 struct rt_sigframe
*frame
;
337 int err
= 0, sig
= ksig
->sig
;
338 unsigned long sp
, ra
, tp
;
342 if ((ksig
->ka
.sa
.sa_flags
& SA_ONSTACK
) != 0 && sas_ss_flags(sp
) == 0) {
343 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
346 frame
= (void *)((sp
- sizeof(*frame
)) & -16ul);
349 panic ("Double exception sys_sigreturn\n");
351 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
))) {
355 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
) {
356 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
359 /* Create the user context. */
361 err
|= __put_user(0, &frame
->uc
.uc_flags
);
362 err
|= __put_user(0, &frame
->uc
.uc_link
);
363 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->areg
[1]);
364 err
|= setup_sigcontext(frame
, regs
);
365 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
367 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
368 ra
= (unsigned long)ksig
->ka
.sa
.sa_restorer
;
371 /* Create sys_rt_sigreturn syscall in stack frame */
373 err
|= gen_return_code(frame
->retcode
);
378 ra
= (unsigned long) frame
->retcode
;
382 * Create signal handler execution context.
383 * Return context not modified until this point.
386 /* Set up registers for signal handler; preserve the threadptr */
387 tp
= regs
->threadptr
;
388 start_thread(regs
, (unsigned long) ksig
->ka
.sa
.sa_handler
,
389 (unsigned long) frame
);
391 /* Set up a stack frame for a call4
392 * Note: PS.CALLINC is set to one by start_thread
394 regs
->areg
[4] = (((unsigned long) ra
) & 0x3fffffff) | 0x40000000;
395 regs
->areg
[6] = (unsigned long) sig
;
396 regs
->areg
[7] = (unsigned long) &frame
->info
;
397 regs
->areg
[8] = (unsigned long) &frame
->uc
;
398 regs
->threadptr
= tp
;
400 pr_debug("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08lx\n",
401 current
->comm
, current
->pid
, sig
, frame
, regs
->pc
);
407 * Note that 'init' is a special process: it doesn't get signals it doesn't
408 * want to handle. Thus you cannot kill init even with a SIGKILL even by
411 * Note that we go through the signals twice: once to check the signals that
412 * the kernel can handle, and then we build all the user-level signal handling
413 * stack-frames in one go after that.
415 static void do_signal(struct pt_regs
*regs
)
419 task_pt_regs(current
)->icountlevel
= 0;
421 if (get_signal(&ksig
)) {
424 /* Are we from a system call? */
426 if ((signed)regs
->syscall
>= 0) {
428 /* If so, check system call restarting.. */
430 switch (regs
->areg
[2]) {
431 case -ERESTARTNOHAND
:
432 case -ERESTART_RESTARTBLOCK
:
433 regs
->areg
[2] = -EINTR
;
437 if (!(ksig
.ka
.sa
.sa_flags
& SA_RESTART
)) {
438 regs
->areg
[2] = -EINTR
;
442 case -ERESTARTNOINTR
:
443 regs
->areg
[2] = regs
->syscall
;
449 if (regs
->areg
[2] != 0)
454 /* Whee! Actually deliver the signal. */
455 /* Set up the stack frame */
456 ret
= setup_frame(&ksig
, sigmask_to_save(), regs
);
457 signal_setup_done(ret
, &ksig
, 0);
458 if (current
->ptrace
& PT_SINGLESTEP
)
459 task_pt_regs(current
)->icountlevel
= 1;
464 /* Did we come from a system call? */
465 if ((signed) regs
->syscall
>= 0) {
466 /* Restart the system call - no handlers present */
467 switch (regs
->areg
[2]) {
468 case -ERESTARTNOHAND
:
470 case -ERESTARTNOINTR
:
471 regs
->areg
[2] = regs
->syscall
;
474 case -ERESTART_RESTARTBLOCK
:
475 regs
->areg
[2] = __NR_restart_syscall
;
481 /* If there's no signal to deliver, we just restore the saved mask. */
482 restore_saved_sigmask();
484 if (current
->ptrace
& PT_SINGLESTEP
)
485 task_pt_regs(current
)->icountlevel
= 1;
489 void do_notify_resume(struct pt_regs
*regs
)
491 if (test_thread_flag(TIF_SIGPENDING
))
494 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME
))
495 tracehook_notify_resume(regs
);