Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[linux-2.6-block.git] / arch / xtensa / kernel / signal.c
CommitLineData
5a0015d6 1/*
29c4dfd9 2 * arch/xtensa/kernel/signal.c
5a0015d6 3 *
29c4dfd9 4 * Default platform functions.
5a0015d6 5 *
29c4dfd9
CZ
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.
5a0015d6 9 *
29c4dfd9
CZ
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
5a0015d6 13 *
29c4dfd9
CZ
14 * Chris Zankel <chris@zankel.net>
15 * Joe Taylor <joe@tensilica.com>
5a0015d6
CZ
16 */
17
5a0015d6
CZ
18#include <linux/signal.h>
19#include <linux/errno.h>
5a0015d6 20#include <linux/ptrace.h>
5a0015d6 21#include <linux/personality.h>
29c4dfd9 22#include <linux/freezer.h>
a53bb24e 23#include <linux/tracehook.h>
29c4dfd9 24
5a0015d6
CZ
25#include <asm/ucontext.h>
26#include <asm/uaccess.h>
5a0015d6 27#include <asm/cacheflush.h>
29c4dfd9
CZ
28#include <asm/coprocessor.h>
29#include <asm/unistd.h>
5a0015d6
CZ
30
31#define DEBUG_SIG 0
32
33#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34
5a0015d6
CZ
35extern struct task_struct *coproc_owners[];
36
29c4dfd9
CZ
37struct rt_sigframe
38{
39 struct siginfo info;
40 struct ucontext uc;
c658eac6
CZ
41 struct {
42 xtregs_opt_t opt;
43 xtregs_user_t user;
44#if XTENSA_HAVE_COPROCESSORS
45 xtregs_coprocessor_t cp;
46#endif
47 } xtregs;
29c4dfd9
CZ
48 unsigned char retcode[6];
49 unsigned int window[4];
50};
51
52/*
53 * Flush register windows stored in pt_regs to stack.
54 * Returns 1 for errors.
5a0015d6
CZ
55 */
56
29c4dfd9
CZ
57int
58flush_window_regs_user(struct pt_regs *regs)
5a0015d6 59{
29c4dfd9
CZ
60 const unsigned long ws = regs->windowstart;
61 const unsigned long wb = regs->windowbase;
62 unsigned long sp = 0;
63 unsigned long wm;
64 int err = 1;
65 int base;
5a0015d6 66
29c4dfd9 67 /* Return if no other frames. */
5a0015d6 68
29c4dfd9
CZ
69 if (regs->wmask == 1)
70 return 0;
5a0015d6 71
29c4dfd9 72 /* Rotate windowmask and skip empty frames. */
5a0015d6 73
29c4dfd9
CZ
74 wm = (ws >> wb) | (ws << (XCHAL_NUM_AREGS / 4 - wb));
75 base = (XCHAL_NUM_AREGS / 4) - (regs->wmask >> 4);
76
77 /* For call8 or call12 frames, we need the previous stack pointer. */
5a0015d6 78
29c4dfd9
CZ
79 if ((regs->wmask & 2) == 0)
80 if (__get_user(sp, (int*)(regs->areg[base * 4 + 1] - 12)))
81 goto errout;
5a0015d6 82
29c4dfd9 83 /* Spill frames to stack. */
5a0015d6 84
29c4dfd9 85 while (base < XCHAL_NUM_AREGS / 4) {
5a0015d6 86
29c4dfd9
CZ
87 int m = (wm >> base);
88 int inc = 0;
5a0015d6 89
29c4dfd9 90 /* Save registers a4..a7 (call8) or a4...a11 (call12) */
5a0015d6 91
29c4dfd9
CZ
92 if (m & 2) { /* call4 */
93 inc = 1;
5a0015d6 94
29c4dfd9
CZ
95 } else if (m & 4) { /* call8 */
96 if (copy_to_user((void*)(sp - 32),
97 &regs->areg[(base + 1) * 4], 16))
98 goto errout;
99 inc = 2;
5a0015d6 100
29c4dfd9
CZ
101 } else if (m & 8) { /* call12 */
102 if (copy_to_user((void*)(sp - 48),
103 &regs->areg[(base + 1) * 4], 32))
104 goto errout;
105 inc = 3;
106 }
5a0015d6 107
29c4dfd9 108 /* Save current frame a0..a3 under next SP */
5a0015d6 109
29c4dfd9
CZ
110 sp = regs->areg[((base + inc) * 4 + 1) % XCHAL_NUM_AREGS];
111 if (copy_to_user((void*)(sp - 16), &regs->areg[base * 4], 16))
112 goto errout;
113
114 /* Get current stack pointer for next loop iteration. */
115
116 sp = regs->areg[base * 4 + 1];
117 base += inc;
118 }
119
3befce8f
CZ
120 regs->wmask = 1;
121 regs->windowstart = 1 << wb;
122
29c4dfd9 123 return 0;
5a0015d6 124
29c4dfd9
CZ
125errout:
126 return err;
127}
5a0015d6
CZ
128
129/*
29c4dfd9
CZ
130 * Note: We don't copy double exception 'regs', we have to finish double exc.
131 * first before we return to signal handler! This dbl.exc.handler might cause
132 * another double exception, but I think we are fine as the situation is the
133 * same as if we had returned to the signal handerl and got an interrupt
134 * immediately...
5a0015d6
CZ
135 */
136
29c4dfd9 137static int
c658eac6 138setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs)
5a0015d6 139{
c658eac6
CZ
140 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
141 struct thread_info *ti = current_thread_info();
29c4dfd9 142 int err = 0;
5a0015d6 143
29c4dfd9
CZ
144#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
145 COPY(pc);
146 COPY(ps);
147 COPY(lbeg);
148 COPY(lend);
149 COPY(lcount);
150 COPY(sar);
151#undef COPY
5a0015d6 152
29c4dfd9
CZ
153 err |= flush_window_regs_user(regs);
154 err |= __copy_to_user (sc->sc_a, regs->areg, 16 * 4);
c658eac6 155 err |= __put_user(0, &sc->sc_xtregs);
5a0015d6 156
c658eac6
CZ
157 if (err)
158 return err;
5a0015d6 159
c658eac6
CZ
160#if XTENSA_HAVE_COPROCESSORS
161 coprocessor_flush_all(ti);
162 coprocessor_release_all(ti);
163 err |= __copy_to_user(&frame->xtregs.cp, &ti->xtregs_cp,
164 sizeof (frame->xtregs.cp));
5a0015d6 165#endif
c658eac6
CZ
166 err |= __copy_to_user(&frame->xtregs.opt, &regs->xtregs_opt,
167 sizeof (xtregs_opt_t));
168 err |= __copy_to_user(&frame->xtregs.user, &ti->xtregs_user,
169 sizeof (xtregs_user_t));
170
171 err |= __put_user(err ? NULL : &frame->xtregs, &sc->sc_xtregs);
5a0015d6 172
29c4dfd9
CZ
173 return err;
174}
5a0015d6
CZ
175
176static int
c658eac6 177restore_sigcontext(struct pt_regs *regs, struct rt_sigframe __user *frame)
5a0015d6 178{
c658eac6
CZ
179 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
180 struct thread_info *ti = current_thread_info();
5a0015d6
CZ
181 unsigned int err = 0;
182 unsigned long ps;
5a0015d6
CZ
183
184#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
185 COPY(pc);
5a0015d6
CZ
186 COPY(lbeg);
187 COPY(lend);
188 COPY(lcount);
189 COPY(sar);
5a0015d6
CZ
190#undef COPY
191
29c4dfd9
CZ
192 /* All registers were flushed to stack. Start with a prestine frame. */
193
194 regs->wmask = 1;
195 regs->windowbase = 0;
196 regs->windowstart = 1;
197
c658eac6
CZ
198 regs->syscall = -1; /* disable syscall checks */
199
5a0015d6
CZ
200 /* For PS, restore only PS.CALLINC.
201 * Assume that all other bits are either the same as for the signal
202 * handler, or the user mode value doesn't matter (e.g. PS.OWB).
203 */
204 err |= __get_user(ps, &sc->sc_ps);
29c4dfd9 205 regs->ps = (regs->ps & ~PS_CALLINC_MASK) | (ps & PS_CALLINC_MASK);
5a0015d6
CZ
206
207 /* Additional corruption checks */
208
5a0015d6 209 if ((regs->lcount > 0)
29c4dfd9 210 && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
5a0015d6
CZ
211 err = 1;
212
29c4dfd9 213 err |= __copy_from_user(regs->areg, sc->sc_a, 16 * 4);
5a0015d6 214
c658eac6
CZ
215 if (err)
216 return err;
217
29c4dfd9
CZ
218 /* The signal handler may have used coprocessors in which
219 * case they are still enabled. We disable them to force a
220 * reloading of the original task's CP state by the lazy
221 * context-switching mechanisms of CP exception handling.
222 * Also, we essentially discard any coprocessor state that the
223 * signal handler created. */
5a0015d6 224
c658eac6
CZ
225#if XTENSA_HAVE_COPROCESSORS
226 coprocessor_release_all(ti);
227 err |= __copy_from_user(&ti->xtregs_cp, &frame->xtregs.cp,
228 sizeof (frame->xtregs.cp));
5a0015d6 229#endif
c658eac6
CZ
230 err |= __copy_from_user(&ti->xtregs_user, &frame->xtregs.user,
231 sizeof (xtregs_user_t));
232 err |= __copy_from_user(&regs->xtregs_opt, &frame->xtregs.opt,
233 sizeof (xtregs_opt_t));
5a0015d6
CZ
234
235 return err;
236}
237
5a0015d6 238
29c4dfd9
CZ
239/*
240 * Do a signal return; undo the signal stack.
241 */
5a0015d6 242
29c4dfd9
CZ
243asmlinkage long xtensa_rt_sigreturn(long a0, long a1, long a2, long a3,
244 long a4, long a5, struct pt_regs *regs)
5a0015d6 245{
29c4dfd9 246 struct rt_sigframe __user *frame;
5a0015d6 247 sigset_t set;
5a0015d6 248 int ret;
29c4dfd9 249
188f677f
AV
250 /* Always make any pending restarted system calls return -EINTR */
251 current_thread_info()->restart_block.fn = do_no_restart_syscall;
252
5a0015d6 253 if (regs->depc > 64)
29c4dfd9
CZ
254 panic("rt_sigreturn in double exception!\n");
255
256 frame = (struct rt_sigframe __user *) regs->areg[1];
5a0015d6 257
b9e122c8 258 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
5a0015d6
CZ
259 goto badframe;
260
261 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
262 goto badframe;
263
264 sigdelsetmask(&set, ~_BLOCKABLE);
d12f7c4a 265 set_current_blocked(&set);
5a0015d6 266
c658eac6 267 if (restore_sigcontext(regs, frame))
5a0015d6 268 goto badframe;
29c4dfd9 269
5a0015d6
CZ
270 ret = regs->areg[2];
271
29c4dfd9 272 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->areg[1]) == -EFAULT)
5a0015d6 273 goto badframe;
5a0015d6
CZ
274
275 return ret;
276
277badframe:
278 force_sig(SIGSEGV, current);
279 return 0;
280}
281
29c4dfd9 282
5a0015d6
CZ
283
284/*
29c4dfd9 285 * Set up a signal frame.
5a0015d6 286 */
5a0015d6
CZ
287
288static int
29c4dfd9 289gen_return_code(unsigned char *codemem)
5a0015d6 290{
5a0015d6
CZ
291 int err = 0;
292
29c4dfd9
CZ
293 /*
294 * The 12-bit immediate is really split up within the 24-bit MOVI
295 * instruction. As long as the above system call numbers fit within
296 * 8-bits, the following code works fine. See the Xtensa ISA for
297 * details.
5a0015d6 298 */
5a0015d6 299
29c4dfd9
CZ
300#if __NR_rt_sigreturn > 255
301# error Generating the MOVI instruction below breaks!
5a0015d6
CZ
302#endif
303
5a0015d6 304#ifdef __XTENSA_EB__ /* Big Endian version */
29c4dfd9
CZ
305 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
306 err |= __put_user(0x22, &codemem[0]);
307 err |= __put_user(0x0a, &codemem[1]);
308 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
309 /* Generate instruction: SYSCALL */
310 err |= __put_user(0x00, &codemem[3]);
311 err |= __put_user(0x05, &codemem[4]);
312 err |= __put_user(0x00, &codemem[5]);
5a0015d6
CZ
313
314#elif defined __XTENSA_EL__ /* Little Endian version */
29c4dfd9
CZ
315 /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
316 err |= __put_user(0x22, &codemem[0]);
317 err |= __put_user(0xa0, &codemem[1]);
318 err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
319 /* Generate instruction: SYSCALL */
320 err |= __put_user(0x00, &codemem[3]);
321 err |= __put_user(0x50, &codemem[4]);
322 err |= __put_user(0x00, &codemem[5]);
5a0015d6 323#else
29c4dfd9 324# error Must use compiler for Xtensa processors.
5a0015d6 325#endif
5a0015d6
CZ
326
327 /* Flush generated code out of the data cache */
328
173d6681
CZ
329 if (err == 0) {
330 __invalidate_icache_range((unsigned long)codemem, 6UL);
331 __flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
332 }
5a0015d6
CZ
333
334 return err;
335}
336
5a0015d6 337
3785006a
MF
338static int setup_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
339 sigset_t *set, struct pt_regs *regs)
5a0015d6 340{
29c4dfd9 341 struct rt_sigframe *frame;
5a0015d6
CZ
342 int err = 0;
343 int signal;
29c4dfd9 344 unsigned long sp, ra;
5a0015d6 345
29c4dfd9 346 sp = regs->areg[1];
5a0015d6 347
29c4dfd9
CZ
348 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp)) {
349 sp = current->sas_ss_sp + current->sas_ss_size;
5a0015d6
CZ
350 }
351
29c4dfd9 352 frame = (void *)((sp - sizeof(*frame)) & -16ul);
5a0015d6 353
5a0015d6
CZ
354 if (regs->depc > 64)
355 panic ("Double exception sys_sigreturn\n");
356
29c4dfd9 357 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
5a0015d6 358 goto give_sigsegv;
29c4dfd9 359 }
5a0015d6
CZ
360
361 signal = current_thread_info()->exec_domain
362 && current_thread_info()->exec_domain->signal_invmap
363 && sig < 32
364 ? current_thread_info()->exec_domain->signal_invmap[sig]
365 : sig;
366
29c4dfd9
CZ
367 if (ka->sa.sa_flags & SA_SIGINFO) {
368 err |= copy_siginfo_to_user(&frame->info, info);
369 }
370
371 /* Create the user context. */
5a0015d6 372
5a0015d6
CZ
373 err |= __put_user(0, &frame->uc.uc_flags);
374 err |= __put_user(0, &frame->uc.uc_link);
375 err |= __put_user((void *)current->sas_ss_sp,
376 &frame->uc.uc_stack.ss_sp);
377 err |= __put_user(sas_ss_flags(regs->areg[1]),
378 &frame->uc.uc_stack.ss_flags);
379 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
c658eac6 380 err |= setup_sigcontext(frame, regs);
5a0015d6
CZ
381 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
382
44c64e6b
CZ
383 if (ka->sa.sa_flags & SA_RESTORER) {
384 ra = (unsigned long)ka->sa.sa_restorer;
385 } else {
5a0015d6 386
44c64e6b 387 /* Create sys_rt_sigreturn syscall in stack frame */
29c4dfd9 388
44c64e6b
CZ
389 err |= gen_return_code(frame->retcode);
390
391 if (err) {
392 goto give_sigsegv;
393 }
394 ra = (unsigned long) frame->retcode;
29c4dfd9 395 }
5a0015d6 396
29c4dfd9
CZ
397 /*
398 * Create signal handler execution context.
5a0015d6
CZ
399 * Return context not modified until this point.
400 */
29c4dfd9
CZ
401
402 /* Set up registers for signal handler */
403 start_thread(regs, (unsigned long) ka->sa.sa_handler,
404 (unsigned long) frame);
405
406 /* Set up a stack frame for a call4
407 * Note: PS.CALLINC is set to one by start_thread
408 */
29c4dfd9
CZ
409 regs->areg[4] = (((unsigned long) ra) & 0x3fffffff) | 0x40000000;
410 regs->areg[6] = (unsigned long) signal;
411 regs->areg[7] = (unsigned long) &frame->info;
412 regs->areg[8] = (unsigned long) &frame->uc;
5a0015d6
CZ
413
414 /* Set access mode to USER_DS. Nomenclature is outdated, but
415 * functionality is used in uaccess.h
416 */
417 set_fs(USER_DS);
418
419#if DEBUG_SIG
420 printk("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08x\n",
421 current->comm, current->pid, signal, frame, regs->pc);
422#endif
423
3785006a 424 return 0;
5a0015d6
CZ
425
426give_sigsegv:
fa47ac59 427 force_sigsegv(sig, current);
3785006a 428 return -EFAULT;
5a0015d6
CZ
429}
430
29c4dfd9
CZ
431asmlinkage long xtensa_sigaltstack(const stack_t __user *uss,
432 stack_t __user *uoss,
433 long a2, long a3, long a4, long a5,
434 struct pt_regs *regs)
435{
436 return do_sigaltstack(uss, uoss, regs->areg[1]);
437}
438
5a0015d6
CZ
439
440
441/*
442 * Note that 'init' is a special process: it doesn't get signals it doesn't
443 * want to handle. Thus you cannot kill init even with a SIGKILL even by
444 * mistake.
445 *
446 * Note that we go through the signals twice: once to check the signals that
447 * the kernel can handle, and then we build all the user-level signal handling
448 * stack-frames in one go after that.
449 */
a53bb24e 450static void do_signal(struct pt_regs *regs)
5a0015d6
CZ
451{
452 siginfo_t info;
453 int signr;
454 struct k_sigaction ka;
9ccc9c75 455 sigset_t oldset;
5a0015d6 456
29c4dfd9
CZ
457 if (try_to_freeze())
458 goto no_signal;
459
9ccc9c75
AV
460 if (test_thread_flag(TIF_RESTORE_SIGMASK))
461 oldset = &current->saved_sigmask;
462 else
5a0015d6
CZ
463 oldset = &current->blocked;
464
29c4dfd9
CZ
465 task_pt_regs(current)->icountlevel = 0;
466
5a0015d6
CZ
467 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
468
29c4dfd9 469 if (signr > 0) {
9112a6b2 470 int ret;
29c4dfd9
CZ
471
472 /* Are we from a system call? */
473
474 if ((signed)regs->syscall >= 0) {
5a0015d6 475
29c4dfd9
CZ
476 /* If so, check system call restarting.. */
477
478 switch (regs->areg[2]) {
479 case -ERESTARTNOHAND:
480 case -ERESTART_RESTARTBLOCK:
5a0015d6
CZ
481 regs->areg[2] = -EINTR;
482 break;
29c4dfd9
CZ
483
484 case -ERESTARTSYS:
485 if (!(ka.sa.sa_flags & SA_RESTART)) {
486 regs->areg[2] = -EINTR;
487 break;
488 }
489 /* fallthrough */
490 case -ERESTARTNOINTR:
491 regs->areg[2] = regs->syscall;
492 regs->pc -= 3;
493 break;
494
495 default:
496 /* nothing to do */
497 if (regs->areg[2] != 0)
498 break;
499 }
5a0015d6 500 }
5a0015d6 501
29c4dfd9
CZ
502 /* Whee! Actually deliver the signal. */
503 /* Set up the stack frame */
3785006a
MF
504 ret = setup_frame(signr, &ka, &info, oldset, regs);
505 if (ret)
9ccc9c75 506 return;
5a0015d6 507
9ccc9c75 508 clear_thread_flag(TIF_RESTORE_SIGMASK);
d12f7c4a 509 block_sigmask(&ka, signr);
29c4dfd9
CZ
510 if (current->ptrace & PT_SINGLESTEP)
511 task_pt_regs(current)->icountlevel = 1;
5a0015d6 512
9ccc9c75 513 return;
29c4dfd9 514 }
5a0015d6 515
29c4dfd9
CZ
516no_signal:
517 /* Did we come from a system call? */
518 if ((signed) regs->syscall >= 0) {
519 /* Restart the system call - no handlers present */
520 switch (regs->areg[2]) {
521 case -ERESTARTNOHAND:
522 case -ERESTARTSYS:
523 case -ERESTARTNOINTR:
524 regs->areg[2] = regs->syscall;
525 regs->pc -= 3;
526 break;
527 case -ERESTART_RESTARTBLOCK:
528 regs->areg[2] = __NR_restart_syscall;
529 regs->pc -= 3;
530 break;
531 }
532 }
9ccc9c75
AV
533
534 /* If there's no signal to deliver, we just restore the saved mask. */
535 if (test_and_clear_thread_flag(TIF_RESTORE_SIGMASK))
536 set_current_blocked(&current->saved_sigmask);
537
29c4dfd9
CZ
538 if (current->ptrace & PT_SINGLESTEP)
539 task_pt_regs(current)->icountlevel = 1;
9ccc9c75 540 return;
5a0015d6 541}
29c4dfd9 542
a53bb24e
AV
543void do_notify_resume(struct pt_regs *regs)
544{
545 if (!user_mode(regs))
546 return;
547
548 if (test_thread_flag(TIF_SIGPENDING))
549 do_signal(regs);
550
a42c6ded 551 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
a53bb24e 552 tracehook_notify_resume(regs);
a53bb24e 553}