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/freezer.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/cacheflush.h>
27 #include <asm/coprocessor.h>
28 #include <asm/unistd.h>
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34 asmlinkage
int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
);
36 extern struct task_struct
*coproc_owners
[];
45 #if XTENSA_HAVE_COPROCESSORS
46 xtregs_coprocessor_t cp
;
49 unsigned char retcode
[6];
50 unsigned int window
[4];
54 * Flush register windows stored in pt_regs to stack.
55 * Returns 1 for errors.
59 flush_window_regs_user(struct pt_regs
*regs
)
61 const unsigned long ws
= regs
->windowstart
;
62 const unsigned long wb
= regs
->windowbase
;
68 /* Return if no other frames. */
73 /* Rotate windowmask and skip empty frames. */
75 wm
= (ws
>> wb
) | (ws
<< (XCHAL_NUM_AREGS
/ 4 - wb
));
76 base
= (XCHAL_NUM_AREGS
/ 4) - (regs
->wmask
>> 4);
78 /* For call8 or call12 frames, we need the previous stack pointer. */
80 if ((regs
->wmask
& 2) == 0)
81 if (__get_user(sp
, (int*)(regs
->areg
[base
* 4 + 1] - 12)))
84 /* Spill frames to stack. */
86 while (base
< XCHAL_NUM_AREGS
/ 4) {
91 /* Save registers a4..a7 (call8) or a4...a11 (call12) */
93 if (m
& 2) { /* call4 */
96 } else if (m
& 4) { /* call8 */
97 if (copy_to_user((void*)(sp
- 32),
98 ®s
->areg
[(base
+ 1) * 4], 16))
102 } else if (m
& 8) { /* call12 */
103 if (copy_to_user((void*)(sp
- 48),
104 ®s
->areg
[(base
+ 1) * 4], 32))
109 /* Save current frame a0..a3 under next SP */
111 sp
= regs
->areg
[((base
+ inc
) * 4 + 1) % XCHAL_NUM_AREGS
];
112 if (copy_to_user((void*)(sp
- 16), ®s
->areg
[base
* 4], 16))
115 /* Get current stack pointer for next loop iteration. */
117 sp
= regs
->areg
[base
* 4 + 1];
122 regs
->windowstart
= 1 << wb
;
131 * Note: We don't copy double exception 'regs', we have to finish double exc.
132 * first before we return to signal handler! This dbl.exc.handler might cause
133 * another double exception, but I think we are fine as the situation is the
134 * same as if we had returned to the signal handerl and got an interrupt
139 setup_sigcontext(struct rt_sigframe __user
*frame
, struct pt_regs
*regs
)
141 struct sigcontext __user
*sc
= &frame
->uc
.uc_mcontext
;
142 struct thread_info
*ti
= current_thread_info();
145 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
154 err
|= flush_window_regs_user(regs
);
155 err
|= __copy_to_user (sc
->sc_a
, regs
->areg
, 16 * 4);
156 err
|= __put_user(0, &sc
->sc_xtregs
);
161 #if XTENSA_HAVE_COPROCESSORS
162 coprocessor_flush_all(ti
);
163 coprocessor_release_all(ti
);
164 err
|= __copy_to_user(&frame
->xtregs
.cp
, &ti
->xtregs_cp
,
165 sizeof (frame
->xtregs
.cp
));
167 err
|= __copy_to_user(&frame
->xtregs
.opt
, ®s
->xtregs_opt
,
168 sizeof (xtregs_opt_t
));
169 err
|= __copy_to_user(&frame
->xtregs
.user
, &ti
->xtregs_user
,
170 sizeof (xtregs_user_t
));
172 err
|= __put_user(err
? NULL
: &frame
->xtregs
, &sc
->sc_xtregs
);
178 restore_sigcontext(struct pt_regs
*regs
, struct rt_sigframe __user
*frame
)
180 struct sigcontext __user
*sc
= &frame
->uc
.uc_mcontext
;
181 struct thread_info
*ti
= current_thread_info();
182 unsigned int err
= 0;
185 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
193 /* All registers were flushed to stack. Start with a prestine frame. */
196 regs
->windowbase
= 0;
197 regs
->windowstart
= 1;
199 regs
->syscall
= -1; /* disable syscall checks */
201 /* For PS, restore only PS.CALLINC.
202 * Assume that all other bits are either the same as for the signal
203 * handler, or the user mode value doesn't matter (e.g. PS.OWB).
205 err
|= __get_user(ps
, &sc
->sc_ps
);
206 regs
->ps
= (regs
->ps
& ~PS_CALLINC_MASK
) | (ps
& PS_CALLINC_MASK
);
208 /* Additional corruption checks */
210 if ((regs
->lcount
> 0)
211 && ((regs
->lbeg
> TASK_SIZE
) || (regs
->lend
> TASK_SIZE
)) )
214 err
|= __copy_from_user(regs
->areg
, sc
->sc_a
, 16 * 4);
219 /* The signal handler may have used coprocessors in which
220 * case they are still enabled. We disable them to force a
221 * reloading of the original task's CP state by the lazy
222 * context-switching mechanisms of CP exception handling.
223 * Also, we essentially discard any coprocessor state that the
224 * signal handler created. */
226 #if XTENSA_HAVE_COPROCESSORS
227 coprocessor_release_all(ti
);
228 err
|= __copy_from_user(&ti
->xtregs_cp
, &frame
->xtregs
.cp
,
229 sizeof (frame
->xtregs
.cp
));
231 err
|= __copy_from_user(&ti
->xtregs_user
, &frame
->xtregs
.user
,
232 sizeof (xtregs_user_t
));
233 err
|= __copy_from_user(®s
->xtregs_opt
, &frame
->xtregs
.opt
,
234 sizeof (xtregs_opt_t
));
241 * Do a signal return; undo the signal stack.
244 asmlinkage
long xtensa_rt_sigreturn(long a0
, long a1
, long a2
, long a3
,
245 long a4
, long a5
, struct pt_regs
*regs
)
247 struct rt_sigframe __user
*frame
;
252 panic("rt_sigreturn in double exception!\n");
254 frame
= (struct rt_sigframe __user
*) regs
->areg
[1];
256 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
259 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
262 sigdelsetmask(&set
, ~_BLOCKABLE
);
263 set_current_blocked(&set
);
265 if (restore_sigcontext(regs
, frame
))
270 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, regs
->areg
[1]) == -EFAULT
)
276 force_sig(SIGSEGV
, current
);
283 * Set up a signal frame.
287 gen_return_code(unsigned char *codemem
)
292 * The 12-bit immediate is really split up within the 24-bit MOVI
293 * instruction. As long as the above system call numbers fit within
294 * 8-bits, the following code works fine. See the Xtensa ISA for
298 #if __NR_rt_sigreturn > 255
299 # error Generating the MOVI instruction below breaks!
302 #ifdef __XTENSA_EB__ /* Big Endian version */
303 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
304 err
|= __put_user(0x22, &codemem
[0]);
305 err
|= __put_user(0x0a, &codemem
[1]);
306 err
|= __put_user(__NR_rt_sigreturn
, &codemem
[2]);
307 /* Generate instruction: SYSCALL */
308 err
|= __put_user(0x00, &codemem
[3]);
309 err
|= __put_user(0x05, &codemem
[4]);
310 err
|= __put_user(0x00, &codemem
[5]);
312 #elif defined __XTENSA_EL__ /* Little Endian version */
313 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
314 err
|= __put_user(0x22, &codemem
[0]);
315 err
|= __put_user(0xa0, &codemem
[1]);
316 err
|= __put_user(__NR_rt_sigreturn
, &codemem
[2]);
317 /* Generate instruction: SYSCALL */
318 err
|= __put_user(0x00, &codemem
[3]);
319 err
|= __put_user(0x50, &codemem
[4]);
320 err
|= __put_user(0x00, &codemem
[5]);
322 # error Must use compiler for Xtensa processors.
325 /* Flush generated code out of the data cache */
328 __invalidate_icache_range((unsigned long)codemem
, 6UL);
329 __flush_invalidate_dcache_range((unsigned long)codemem
, 6UL);
336 static int setup_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
337 sigset_t
*set
, struct pt_regs
*regs
)
339 struct rt_sigframe
*frame
;
342 unsigned long sp
, ra
;
346 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && ! on_sig_stack(sp
)) {
347 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
350 frame
= (void *)((sp
- sizeof(*frame
)) & -16ul);
353 panic ("Double exception sys_sigreturn\n");
355 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
))) {
359 signal
= current_thread_info()->exec_domain
360 && current_thread_info()->exec_domain
->signal_invmap
362 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
365 if (ka
->sa
.sa_flags
& SA_SIGINFO
) {
366 err
|= copy_siginfo_to_user(&frame
->info
, info
);
369 /* Create the user context. */
371 err
|= __put_user(0, &frame
->uc
.uc_flags
);
372 err
|= __put_user(0, &frame
->uc
.uc_link
);
373 err
|= __put_user((void *)current
->sas_ss_sp
,
374 &frame
->uc
.uc_stack
.ss_sp
);
375 err
|= __put_user(sas_ss_flags(regs
->areg
[1]),
376 &frame
->uc
.uc_stack
.ss_flags
);
377 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
378 err
|= setup_sigcontext(frame
, regs
);
379 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
381 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
382 ra
= (unsigned long)ka
->sa
.sa_restorer
;
385 /* Create sys_rt_sigreturn syscall in stack frame */
387 err
|= gen_return_code(frame
->retcode
);
392 ra
= (unsigned long) frame
->retcode
;
396 * Create signal handler execution context.
397 * Return context not modified until this point.
400 /* Set up registers for signal handler */
401 start_thread(regs
, (unsigned long) ka
->sa
.sa_handler
,
402 (unsigned long) frame
);
404 /* Set up a stack frame for a call4
405 * Note: PS.CALLINC is set to one by start_thread
407 regs
->areg
[4] = (((unsigned long) ra
) & 0x3fffffff) | 0x40000000;
408 regs
->areg
[6] = (unsigned long) signal
;
409 regs
->areg
[7] = (unsigned long) &frame
->info
;
410 regs
->areg
[8] = (unsigned long) &frame
->uc
;
412 /* Set access mode to USER_DS. Nomenclature is outdated, but
413 * functionality is used in uaccess.h
418 printk("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08x\n",
419 current
->comm
, current
->pid
, signal
, frame
, regs
->pc
);
425 force_sigsegv(sig
, current
);
430 * Atomically swap in the new signal mask, and wait for a signal.
433 asmlinkage
long xtensa_rt_sigsuspend(sigset_t __user
*unewset
,
435 long a2
, long a3
, long a4
, long a5
,
436 struct pt_regs
*regs
)
438 sigset_t saveset
, newset
;
440 /* XXX: Don't preclude handling different sized sigset_t's. */
441 if (sigsetsize
!= sizeof(sigset_t
))
444 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
447 sigdelsetmask(&newset
, ~_BLOCKABLE
);
448 saveset
= current
->blocked
;
449 set_current_blocked(&newset
);
451 regs
->areg
[2] = -EINTR
;
453 current
->state
= TASK_INTERRUPTIBLE
;
455 if (do_signal(regs
, &saveset
))
460 asmlinkage
long xtensa_sigaltstack(const stack_t __user
*uss
,
461 stack_t __user
*uoss
,
462 long a2
, long a3
, long a4
, long a5
,
463 struct pt_regs
*regs
)
465 return do_sigaltstack(uss
, uoss
, regs
->areg
[1]);
471 * Note that 'init' is a special process: it doesn't get signals it doesn't
472 * want to handle. Thus you cannot kill init even with a SIGKILL even by
475 * Note that we go through the signals twice: once to check the signals that
476 * the kernel can handle, and then we build all the user-level signal handling
477 * stack-frames in one go after that.
479 int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
)
483 struct k_sigaction ka
;
485 if (!user_mode(regs
))
492 oldset
= ¤t
->blocked
;
494 task_pt_regs(current
)->icountlevel
= 0;
496 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
500 /* Are we from a system call? */
502 if ((signed)regs
->syscall
>= 0) {
504 /* If so, check system call restarting.. */
506 switch (regs
->areg
[2]) {
507 case -ERESTARTNOHAND
:
508 case -ERESTART_RESTARTBLOCK
:
509 regs
->areg
[2] = -EINTR
;
513 if (!(ka
.sa
.sa_flags
& SA_RESTART
)) {
514 regs
->areg
[2] = -EINTR
;
518 case -ERESTARTNOINTR
:
519 regs
->areg
[2] = regs
->syscall
;
525 if (regs
->areg
[2] != 0)
530 /* Whee! Actually deliver the signal. */
531 /* Set up the stack frame */
532 ret
= setup_frame(signr
, &ka
, &info
, oldset
, regs
);
536 block_sigmask(&ka
, signr
);
537 if (current
->ptrace
& PT_SINGLESTEP
)
538 task_pt_regs(current
)->icountlevel
= 1;
544 /* Did we come from a system call? */
545 if ((signed) regs
->syscall
>= 0) {
546 /* Restart the system call - no handlers present */
547 switch (regs
->areg
[2]) {
548 case -ERESTARTNOHAND
:
550 case -ERESTARTNOINTR
:
551 regs
->areg
[2] = regs
->syscall
;
554 case -ERESTART_RESTARTBLOCK
:
555 regs
->areg
[2] = __NR_restart_syscall
;
560 if (current
->ptrace
& PT_SINGLESTEP
)
561 task_pt_regs(current
)->icountlevel
= 1;