Replace <asm/uaccess.h> with <linux/uaccess.h> globally
[linux-2.6/btrfs-unstable.git] / arch / xtensa / kernel / signal.c
blobc41294745731eece5bcb20f8e523c2ed8464971d
1 /*
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
8 * for more details.
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>
24 #include <asm/ucontext.h>
25 #include <linux/uaccess.h>
26 #include <asm/cacheflush.h>
27 #include <asm/coprocessor.h>
28 #include <asm/unistd.h>
30 #define DEBUG_SIG 0
32 extern struct task_struct *coproc_owners[];
34 struct rt_sigframe
36 struct siginfo info;
37 struct ucontext uc;
38 struct {
39 xtregs_opt_t opt;
40 xtregs_user_t user;
41 #if XTENSA_HAVE_COPROCESSORS
42 xtregs_coprocessor_t cp;
43 #endif
44 } xtregs;
45 unsigned char retcode[6];
46 unsigned int window[4];
49 /*
50 * Flush register windows stored in pt_regs to stack.
51 * Returns 1 for errors.
54 int
55 flush_window_regs_user(struct pt_regs *regs)
57 const unsigned long ws = regs->windowstart;
58 const unsigned long wb = regs->windowbase;
59 unsigned long sp = 0;
60 unsigned long wm;
61 int err = 1;
62 int base;
64 /* Return if no other frames. */
66 if (regs->wmask == 1)
67 return 0;
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)))
78 goto errout;
80 /* Spill frames to stack. */
82 while (base < XCHAL_NUM_AREGS / 4) {
84 int m = (wm >> base);
85 int inc = 0;
87 /* Save registers a4..a7 (call8) or a4...a11 (call12) */
89 if (m & 2) { /* call4 */
90 inc = 1;
92 } else if (m & 4) { /* call8 */
93 if (copy_to_user((void*)(sp - 32),
94 &regs->areg[(base + 1) * 4], 16))
95 goto errout;
96 inc = 2;
98 } else if (m & 8) { /* call12 */
99 if (copy_to_user((void*)(sp - 48),
100 &regs->areg[(base + 1) * 4], 32))
101 goto errout;
102 inc = 3;
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((void*)(sp - 16), &regs->areg[base * 4], 16))
109 goto errout;
111 /* Get current stack pointer for next loop iteration. */
113 sp = regs->areg[base * 4 + 1];
114 base += inc;
117 regs->wmask = 1;
118 regs->windowstart = 1 << wb;
120 return 0;
122 errout:
123 return err;
127 * Note: We don't copy double exception 'regs', we have to finish double exc.
128 * first before we return to signal handler! This dbl.exc.handler might cause
129 * another double exception, but I think we are fine as the situation is the
130 * same as if we had returned to the signal handerl and got an interrupt
131 * immediately...
134 static int
135 setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs)
137 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
138 struct thread_info *ti = current_thread_info();
139 int err = 0;
141 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
142 COPY(pc);
143 COPY(ps);
144 COPY(lbeg);
145 COPY(lend);
146 COPY(lcount);
147 COPY(sar);
148 #undef COPY
150 err |= flush_window_regs_user(regs);
151 err |= __copy_to_user (sc->sc_a, regs->areg, 16 * 4);
152 err |= __put_user(0, &sc->sc_xtregs);
154 if (err)
155 return err;
157 #if XTENSA_HAVE_COPROCESSORS
158 coprocessor_flush_all(ti);
159 coprocessor_release_all(ti);
160 err |= __copy_to_user(&frame->xtregs.cp, &ti->xtregs_cp,
161 sizeof (frame->xtregs.cp));
162 #endif
163 err |= __copy_to_user(&frame->xtregs.opt, &regs->xtregs_opt,
164 sizeof (xtregs_opt_t));
165 err |= __copy_to_user(&frame->xtregs.user, &ti->xtregs_user,
166 sizeof (xtregs_user_t));
168 err |= __put_user(err ? NULL : &frame->xtregs, &sc->sc_xtregs);
170 return err;
173 static int
174 restore_sigcontext(struct pt_regs *regs, struct rt_sigframe __user *frame)
176 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
177 struct thread_info *ti = current_thread_info();
178 unsigned int err = 0;
179 unsigned long ps;
181 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
182 COPY(pc);
183 COPY(lbeg);
184 COPY(lend);
185 COPY(lcount);
186 COPY(sar);
187 #undef COPY
189 /* All registers were flushed to stack. Start with a prestine frame. */
191 regs->wmask = 1;
192 regs->windowbase = 0;
193 regs->windowstart = 1;
195 regs->syscall = -1; /* disable syscall checks */
197 /* For PS, restore only PS.CALLINC.
198 * Assume that all other bits are either the same as for the signal
199 * handler, or the user mode value doesn't matter (e.g. PS.OWB).
201 err |= __get_user(ps, &sc->sc_ps);
202 regs->ps = (regs->ps & ~PS_CALLINC_MASK) | (ps & PS_CALLINC_MASK);
204 /* Additional corruption checks */
206 if ((regs->lcount > 0)
207 && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
208 err = 1;
210 err |= __copy_from_user(regs->areg, sc->sc_a, 16 * 4);
212 if (err)
213 return err;
215 /* The signal handler may have used coprocessors in which
216 * case they are still enabled. We disable them to force a
217 * reloading of the original task's CP state by the lazy
218 * context-switching mechanisms of CP exception handling.
219 * Also, we essentially discard any coprocessor state that the
220 * signal handler created. */
222 #if XTENSA_HAVE_COPROCESSORS
223 coprocessor_release_all(ti);
224 err |= __copy_from_user(&ti->xtregs_cp, &frame->xtregs.cp,
225 sizeof (frame->xtregs.cp));
226 #endif
227 err |= __copy_from_user(&ti->xtregs_user, &frame->xtregs.user,
228 sizeof (xtregs_user_t));
229 err |= __copy_from_user(&regs->xtregs_opt, &frame->xtregs.opt,
230 sizeof (xtregs_opt_t));
232 return err;
237 * Do a signal return; undo the signal stack.
240 asmlinkage long xtensa_rt_sigreturn(long a0, long a1, long a2, long a3,
241 long a4, long a5, struct pt_regs *regs)
243 struct rt_sigframe __user *frame;
244 sigset_t set;
245 int ret;
247 /* Always make any pending restarted system calls return -EINTR */
248 current->restart_block.fn = do_no_restart_syscall;
250 if (regs->depc > 64)
251 panic("rt_sigreturn in double exception!\n");
253 frame = (struct rt_sigframe __user *) regs->areg[1];
255 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
256 goto badframe;
258 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
259 goto badframe;
261 set_current_blocked(&set);
263 if (restore_sigcontext(regs, frame))
264 goto badframe;
266 ret = regs->areg[2];
268 if (restore_altstack(&frame->uc.uc_stack))
269 goto badframe;
271 return ret;
273 badframe:
274 force_sig(SIGSEGV, current);
275 return 0;
281 * Set up a signal frame.
284 static int
285 gen_return_code(unsigned char *codemem)
287 int err = 0;
290 * The 12-bit immediate is really split up within the 24-bit MOVI
291 * instruction. As long as the above system call numbers fit within
292 * 8-bits, the following code works fine. See the Xtensa ISA for
293 * details.
296 #if __NR_rt_sigreturn > 255
297 # error Generating the MOVI instruction below breaks!
298 #endif
300 #ifdef __XTENSA_EB__ /* Big Endian version */
301 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
302 err |= __put_user(0x22, &codemem[0]);
303 err |= __put_user(0x0a, &codemem[1]);
304 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
305 /* Generate instruction: SYSCALL */
306 err |= __put_user(0x00, &codemem[3]);
307 err |= __put_user(0x05, &codemem[4]);
308 err |= __put_user(0x00, &codemem[5]);
310 #elif defined __XTENSA_EL__ /* Little Endian version */
311 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
312 err |= __put_user(0x22, &codemem[0]);
313 err |= __put_user(0xa0, &codemem[1]);
314 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
315 /* Generate instruction: SYSCALL */
316 err |= __put_user(0x00, &codemem[3]);
317 err |= __put_user(0x50, &codemem[4]);
318 err |= __put_user(0x00, &codemem[5]);
319 #else
320 # error Must use compiler for Xtensa processors.
321 #endif
323 /* Flush generated code out of the data cache */
325 if (err == 0) {
326 __invalidate_icache_range((unsigned long)codemem, 6UL);
327 __flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
330 return err;
334 static int setup_frame(struct ksignal *ksig, sigset_t *set,
335 struct pt_regs *regs)
337 struct rt_sigframe *frame;
338 int err = 0, sig = ksig->sig;
339 unsigned long sp, ra, tp;
341 sp = regs->areg[1];
343 if ((ksig->ka.sa.sa_flags & SA_ONSTACK) != 0 && sas_ss_flags(sp) == 0) {
344 sp = current->sas_ss_sp + current->sas_ss_size;
347 frame = (void *)((sp - sizeof(*frame)) & -16ul);
349 if (regs->depc > 64)
350 panic ("Double exception sys_sigreturn\n");
352 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
353 return -EFAULT;
356 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
357 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
360 /* Create the user context. */
362 err |= __put_user(0, &frame->uc.uc_flags);
363 err |= __put_user(0, &frame->uc.uc_link);
364 err |= __save_altstack(&frame->uc.uc_stack, regs->areg[1]);
365 err |= setup_sigcontext(frame, regs);
366 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
368 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
369 ra = (unsigned long)ksig->ka.sa.sa_restorer;
370 } else {
372 /* Create sys_rt_sigreturn syscall in stack frame */
374 err |= gen_return_code(frame->retcode);
376 if (err) {
377 return -EFAULT;
379 ra = (unsigned long) frame->retcode;
383 * Create signal handler execution context.
384 * Return context not modified until this point.
387 /* Set up registers for signal handler; preserve the threadptr */
388 tp = regs->threadptr;
389 start_thread(regs, (unsigned long) ksig->ka.sa.sa_handler,
390 (unsigned long) frame);
392 /* Set up a stack frame for a call4
393 * Note: PS.CALLINC is set to one by start_thread
395 regs->areg[4] = (((unsigned long) ra) & 0x3fffffff) | 0x40000000;
396 regs->areg[6] = (unsigned long) sig;
397 regs->areg[7] = (unsigned long) &frame->info;
398 regs->areg[8] = (unsigned long) &frame->uc;
399 regs->threadptr = tp;
401 #if DEBUG_SIG
402 printk("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08x\n",
403 current->comm, current->pid, sig, frame, regs->pc);
404 #endif
406 return 0;
410 * Note that 'init' is a special process: it doesn't get signals it doesn't
411 * want to handle. Thus you cannot kill init even with a SIGKILL even by
412 * mistake.
414 * Note that we go through the signals twice: once to check the signals that
415 * the kernel can handle, and then we build all the user-level signal handling
416 * stack-frames in one go after that.
418 static void do_signal(struct pt_regs *regs)
420 struct ksignal ksig;
422 task_pt_regs(current)->icountlevel = 0;
424 if (get_signal(&ksig)) {
425 int ret;
427 /* Are we from a system call? */
429 if ((signed)regs->syscall >= 0) {
431 /* If so, check system call restarting.. */
433 switch (regs->areg[2]) {
434 case -ERESTARTNOHAND:
435 case -ERESTART_RESTARTBLOCK:
436 regs->areg[2] = -EINTR;
437 break;
439 case -ERESTARTSYS:
440 if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
441 regs->areg[2] = -EINTR;
442 break;
444 /* fallthrough */
445 case -ERESTARTNOINTR:
446 regs->areg[2] = regs->syscall;
447 regs->pc -= 3;
448 break;
450 default:
451 /* nothing to do */
452 if (regs->areg[2] != 0)
453 break;
457 /* Whee! Actually deliver the signal. */
458 /* Set up the stack frame */
459 ret = setup_frame(&ksig, sigmask_to_save(), regs);
460 signal_setup_done(ret, &ksig, 0);
461 if (current->ptrace & PT_SINGLESTEP)
462 task_pt_regs(current)->icountlevel = 1;
464 return;
467 /* Did we come from a system call? */
468 if ((signed) regs->syscall >= 0) {
469 /* Restart the system call - no handlers present */
470 switch (regs->areg[2]) {
471 case -ERESTARTNOHAND:
472 case -ERESTARTSYS:
473 case -ERESTARTNOINTR:
474 regs->areg[2] = regs->syscall;
475 regs->pc -= 3;
476 break;
477 case -ERESTART_RESTARTBLOCK:
478 regs->areg[2] = __NR_restart_syscall;
479 regs->pc -= 3;
480 break;
484 /* If there's no signal to deliver, we just restore the saved mask. */
485 restore_saved_sigmask();
487 if (current->ptrace & PT_SINGLESTEP)
488 task_pt_regs(current)->icountlevel = 1;
489 return;
492 void do_notify_resume(struct pt_regs *regs)
494 if (test_thread_flag(TIF_SIGPENDING))
495 do_signal(regs);
497 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
498 tracehook_notify_resume(regs);