Merge 3.4-rc6 into usb-next
[deliverable/linux.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
CZ
22#include <linux/freezer.h>
23
5a0015d6
CZ
24#include <asm/ucontext.h>
25#include <asm/uaccess.h>
5a0015d6 26#include <asm/cacheflush.h>
29c4dfd9
CZ
27#include <asm/coprocessor.h>
28#include <asm/unistd.h>
5a0015d6
CZ
29
30#define DEBUG_SIG 0
31
32#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
5a0015d6
CZ
34asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
35
36extern struct task_struct *coproc_owners[];
37
29c4dfd9
CZ
38struct rt_sigframe
39{
40 struct siginfo info;
41 struct ucontext uc;
c658eac6
CZ
42 struct {
43 xtregs_opt_t opt;
44 xtregs_user_t user;
45#if XTENSA_HAVE_COPROCESSORS
46 xtregs_coprocessor_t cp;
47#endif
48 } xtregs;
29c4dfd9
CZ
49 unsigned char retcode[6];
50 unsigned int window[4];
51};
52
53/*
54 * Flush register windows stored in pt_regs to stack.
55 * Returns 1 for errors.
5a0015d6
CZ
56 */
57
29c4dfd9
CZ
58int
59flush_window_regs_user(struct pt_regs *regs)
5a0015d6 60{
29c4dfd9
CZ
61 const unsigned long ws = regs->windowstart;
62 const unsigned long wb = regs->windowbase;
63 unsigned long sp = 0;
64 unsigned long wm;
65 int err = 1;
66 int base;
5a0015d6 67
29c4dfd9 68 /* Return if no other frames. */
5a0015d6 69
29c4dfd9
CZ
70 if (regs->wmask == 1)
71 return 0;
5a0015d6 72
29c4dfd9 73 /* Rotate windowmask and skip empty frames. */
5a0015d6 74
29c4dfd9
CZ
75 wm = (ws >> wb) | (ws << (XCHAL_NUM_AREGS / 4 - wb));
76 base = (XCHAL_NUM_AREGS / 4) - (regs->wmask >> 4);
77
78 /* For call8 or call12 frames, we need the previous stack pointer. */
5a0015d6 79
29c4dfd9
CZ
80 if ((regs->wmask & 2) == 0)
81 if (__get_user(sp, (int*)(regs->areg[base * 4 + 1] - 12)))
82 goto errout;
5a0015d6 83
29c4dfd9 84 /* Spill frames to stack. */
5a0015d6 85
29c4dfd9 86 while (base < XCHAL_NUM_AREGS / 4) {
5a0015d6 87
29c4dfd9
CZ
88 int m = (wm >> base);
89 int inc = 0;
5a0015d6 90
29c4dfd9 91 /* Save registers a4..a7 (call8) or a4...a11 (call12) */
5a0015d6 92
29c4dfd9
CZ
93 if (m & 2) { /* call4 */
94 inc = 1;
5a0015d6 95
29c4dfd9
CZ
96 } else if (m & 4) { /* call8 */
97 if (copy_to_user((void*)(sp - 32),
98 &regs->areg[(base + 1) * 4], 16))
99 goto errout;
100 inc = 2;
5a0015d6 101
29c4dfd9
CZ
102 } else if (m & 8) { /* call12 */
103 if (copy_to_user((void*)(sp - 48),
104 &regs->areg[(base + 1) * 4], 32))
105 goto errout;
106 inc = 3;
107 }
5a0015d6 108
29c4dfd9 109 /* Save current frame a0..a3 under next SP */
5a0015d6 110
29c4dfd9
CZ
111 sp = regs->areg[((base + inc) * 4 + 1) % XCHAL_NUM_AREGS];
112 if (copy_to_user((void*)(sp - 16), &regs->areg[base * 4], 16))
113 goto errout;
114
115 /* Get current stack pointer for next loop iteration. */
116
117 sp = regs->areg[base * 4 + 1];
118 base += inc;
119 }
120
3befce8f
CZ
121 regs->wmask = 1;
122 regs->windowstart = 1 << wb;
123
29c4dfd9 124 return 0;
5a0015d6 125
29c4dfd9
CZ
126errout:
127 return err;
128}
5a0015d6
CZ
129
130/*
29c4dfd9
CZ
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
135 * immediately...
5a0015d6
CZ
136 */
137
29c4dfd9 138static int
c658eac6 139setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs)
5a0015d6 140{
c658eac6
CZ
141 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
142 struct thread_info *ti = current_thread_info();
29c4dfd9 143 int err = 0;
5a0015d6 144
29c4dfd9
CZ
145#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
146 COPY(pc);
147 COPY(ps);
148 COPY(lbeg);
149 COPY(lend);
150 COPY(lcount);
151 COPY(sar);
152#undef COPY
5a0015d6 153
29c4dfd9
CZ
154 err |= flush_window_regs_user(regs);
155 err |= __copy_to_user (sc->sc_a, regs->areg, 16 * 4);
c658eac6 156 err |= __put_user(0, &sc->sc_xtregs);
5a0015d6 157
c658eac6
CZ
158 if (err)
159 return err;
5a0015d6 160
c658eac6
CZ
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));
5a0015d6 166#endif
c658eac6
CZ
167 err |= __copy_to_user(&frame->xtregs.opt, &regs->xtregs_opt,
168 sizeof (xtregs_opt_t));
169 err |= __copy_to_user(&frame->xtregs.user, &ti->xtregs_user,
170 sizeof (xtregs_user_t));
171
172 err |= __put_user(err ? NULL : &frame->xtregs, &sc->sc_xtregs);
5a0015d6 173
29c4dfd9
CZ
174 return err;
175}
5a0015d6
CZ
176
177static int
c658eac6 178restore_sigcontext(struct pt_regs *regs, struct rt_sigframe __user *frame)
5a0015d6 179{
c658eac6
CZ
180 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
181 struct thread_info *ti = current_thread_info();
5a0015d6
CZ
182 unsigned int err = 0;
183 unsigned long ps;
5a0015d6
CZ
184
185#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
186 COPY(pc);
5a0015d6
CZ
187 COPY(lbeg);
188 COPY(lend);
189 COPY(lcount);
190 COPY(sar);
5a0015d6
CZ
191#undef COPY
192
29c4dfd9
CZ
193 /* All registers were flushed to stack. Start with a prestine frame. */
194
195 regs->wmask = 1;
196 regs->windowbase = 0;
197 regs->windowstart = 1;
198
c658eac6
CZ
199 regs->syscall = -1; /* disable syscall checks */
200
5a0015d6
CZ
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).
204 */
205 err |= __get_user(ps, &sc->sc_ps);
29c4dfd9 206 regs->ps = (regs->ps & ~PS_CALLINC_MASK) | (ps & PS_CALLINC_MASK);
5a0015d6
CZ
207
208 /* Additional corruption checks */
209
5a0015d6 210 if ((regs->lcount > 0)
29c4dfd9 211 && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
5a0015d6
CZ
212 err = 1;
213
29c4dfd9 214 err |= __copy_from_user(regs->areg, sc->sc_a, 16 * 4);
5a0015d6 215
c658eac6
CZ
216 if (err)
217 return err;
218
29c4dfd9
CZ
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. */
5a0015d6 225
c658eac6
CZ
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));
5a0015d6 230#endif
c658eac6
CZ
231 err |= __copy_from_user(&ti->xtregs_user, &frame->xtregs.user,
232 sizeof (xtregs_user_t));
233 err |= __copy_from_user(&regs->xtregs_opt, &frame->xtregs.opt,
234 sizeof (xtregs_opt_t));
5a0015d6
CZ
235
236 return err;
237}
238
5a0015d6 239
29c4dfd9
CZ
240/*
241 * Do a signal return; undo the signal stack.
242 */
5a0015d6 243
29c4dfd9
CZ
244asmlinkage long xtensa_rt_sigreturn(long a0, long a1, long a2, long a3,
245 long a4, long a5, struct pt_regs *regs)
5a0015d6 246{
29c4dfd9 247 struct rt_sigframe __user *frame;
5a0015d6 248 sigset_t set;
5a0015d6 249 int ret;
29c4dfd9 250
5a0015d6 251 if (regs->depc > 64)
29c4dfd9
CZ
252 panic("rt_sigreturn in double exception!\n");
253
254 frame = (struct rt_sigframe __user *) regs->areg[1];
5a0015d6 255
b9e122c8 256 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
5a0015d6
CZ
257 goto badframe;
258
259 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
260 goto badframe;
261
262 sigdelsetmask(&set, ~_BLOCKABLE);
d12f7c4a 263 set_current_blocked(&set);
5a0015d6 264
c658eac6 265 if (restore_sigcontext(regs, frame))
5a0015d6 266 goto badframe;
29c4dfd9 267
5a0015d6
CZ
268 ret = regs->areg[2];
269
29c4dfd9 270 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->areg[1]) == -EFAULT)
5a0015d6 271 goto badframe;
5a0015d6
CZ
272
273 return ret;
274
275badframe:
276 force_sig(SIGSEGV, current);
277 return 0;
278}
279
29c4dfd9 280
5a0015d6
CZ
281
282/*
29c4dfd9 283 * Set up a signal frame.
5a0015d6 284 */
5a0015d6
CZ
285
286static int
29c4dfd9 287gen_return_code(unsigned char *codemem)
5a0015d6 288{
5a0015d6
CZ
289 int err = 0;
290
29c4dfd9
CZ
291 /*
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
295 * details.
5a0015d6 296 */
5a0015d6 297
29c4dfd9
CZ
298#if __NR_rt_sigreturn > 255
299# error Generating the MOVI instruction below breaks!
5a0015d6
CZ
300#endif
301
5a0015d6 302#ifdef __XTENSA_EB__ /* Big Endian version */
29c4dfd9
CZ
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]);
5a0015d6
CZ
311
312#elif defined __XTENSA_EL__ /* Little Endian version */
29c4dfd9
CZ
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]);
5a0015d6 321#else
29c4dfd9 322# error Must use compiler for Xtensa processors.
5a0015d6 323#endif
5a0015d6
CZ
324
325 /* Flush generated code out of the data cache */
326
173d6681
CZ
327 if (err == 0) {
328 __invalidate_icache_range((unsigned long)codemem, 6UL);
329 __flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
330 }
5a0015d6
CZ
331
332 return err;
333}
334
5a0015d6 335
3785006a
MF
336static int setup_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
337 sigset_t *set, struct pt_regs *regs)
5a0015d6 338{
29c4dfd9 339 struct rt_sigframe *frame;
5a0015d6
CZ
340 int err = 0;
341 int signal;
29c4dfd9 342 unsigned long sp, ra;
5a0015d6 343
29c4dfd9 344 sp = regs->areg[1];
5a0015d6 345
29c4dfd9
CZ
346 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp)) {
347 sp = current->sas_ss_sp + current->sas_ss_size;
5a0015d6
CZ
348 }
349
29c4dfd9 350 frame = (void *)((sp - sizeof(*frame)) & -16ul);
5a0015d6 351
5a0015d6
CZ
352 if (regs->depc > 64)
353 panic ("Double exception sys_sigreturn\n");
354
29c4dfd9 355 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
5a0015d6 356 goto give_sigsegv;
29c4dfd9 357 }
5a0015d6
CZ
358
359 signal = current_thread_info()->exec_domain
360 && current_thread_info()->exec_domain->signal_invmap
361 && sig < 32
362 ? current_thread_info()->exec_domain->signal_invmap[sig]
363 : sig;
364
29c4dfd9
CZ
365 if (ka->sa.sa_flags & SA_SIGINFO) {
366 err |= copy_siginfo_to_user(&frame->info, info);
367 }
368
369 /* Create the user context. */
5a0015d6 370
5a0015d6
CZ
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);
c658eac6 378 err |= setup_sigcontext(frame, regs);
5a0015d6
CZ
379 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
380
44c64e6b
CZ
381 if (ka->sa.sa_flags & SA_RESTORER) {
382 ra = (unsigned long)ka->sa.sa_restorer;
383 } else {
5a0015d6 384
44c64e6b 385 /* Create sys_rt_sigreturn syscall in stack frame */
29c4dfd9 386
44c64e6b
CZ
387 err |= gen_return_code(frame->retcode);
388
389 if (err) {
390 goto give_sigsegv;
391 }
392 ra = (unsigned long) frame->retcode;
29c4dfd9 393 }
5a0015d6 394
29c4dfd9
CZ
395 /*
396 * Create signal handler execution context.
5a0015d6
CZ
397 * Return context not modified until this point.
398 */
29c4dfd9
CZ
399
400 /* Set up registers for signal handler */
401 start_thread(regs, (unsigned long) ka->sa.sa_handler,
402 (unsigned long) frame);
403
404 /* Set up a stack frame for a call4
405 * Note: PS.CALLINC is set to one by start_thread
406 */
29c4dfd9
CZ
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;
5a0015d6
CZ
411
412 /* Set access mode to USER_DS. Nomenclature is outdated, but
413 * functionality is used in uaccess.h
414 */
415 set_fs(USER_DS);
416
417#if DEBUG_SIG
418 printk("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08x\n",
419 current->comm, current->pid, signal, frame, regs->pc);
420#endif
421
3785006a 422 return 0;
5a0015d6
CZ
423
424give_sigsegv:
fa47ac59 425 force_sigsegv(sig, current);
3785006a 426 return -EFAULT;
5a0015d6
CZ
427}
428
29c4dfd9
CZ
429/*
430 * Atomically swap in the new signal mask, and wait for a signal.
431 */
432
433asmlinkage long xtensa_rt_sigsuspend(sigset_t __user *unewset,
434 size_t sigsetsize,
435 long a2, long a3, long a4, long a5,
436 struct pt_regs *regs)
437{
438 sigset_t saveset, newset;
439
440 /* XXX: Don't preclude handling different sized sigset_t's. */
441 if (sigsetsize != sizeof(sigset_t))
442 return -EINVAL;
443
444 if (copy_from_user(&newset, unewset, sizeof(newset)))
445 return -EFAULT;
446
447 sigdelsetmask(&newset, ~_BLOCKABLE);
29c4dfd9 448 saveset = current->blocked;
d12f7c4a 449 set_current_blocked(&newset);
29c4dfd9
CZ
450
451 regs->areg[2] = -EINTR;
452 while (1) {
453 current->state = TASK_INTERRUPTIBLE;
454 schedule();
455 if (do_signal(regs, &saveset))
456 return -EINTR;
457 }
458}
459
460asmlinkage 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)
464{
465 return do_sigaltstack(uss, uoss, regs->areg[1]);
466}
467
5a0015d6
CZ
468
469
470/*
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
473 * mistake.
474 *
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.
478 */
479int do_signal(struct pt_regs *regs, sigset_t *oldset)
480{
481 siginfo_t info;
482 int signr;
483 struct k_sigaction ka;
484
29c4dfd9
CZ
485 if (!user_mode(regs))
486 return 0;
487
488 if (try_to_freeze())
489 goto no_signal;
490
5a0015d6
CZ
491 if (!oldset)
492 oldset = &current->blocked;
493
29c4dfd9
CZ
494 task_pt_regs(current)->icountlevel = 0;
495
5a0015d6
CZ
496 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
497
29c4dfd9 498 if (signr > 0) {
9112a6b2 499 int ret;
29c4dfd9
CZ
500
501 /* Are we from a system call? */
502
503 if ((signed)regs->syscall >= 0) {
5a0015d6 504
29c4dfd9
CZ
505 /* If so, check system call restarting.. */
506
507 switch (regs->areg[2]) {
508 case -ERESTARTNOHAND:
509 case -ERESTART_RESTARTBLOCK:
5a0015d6
CZ
510 regs->areg[2] = -EINTR;
511 break;
29c4dfd9
CZ
512
513 case -ERESTARTSYS:
514 if (!(ka.sa.sa_flags & SA_RESTART)) {
515 regs->areg[2] = -EINTR;
516 break;
517 }
518 /* fallthrough */
519 case -ERESTARTNOINTR:
520 regs->areg[2] = regs->syscall;
521 regs->pc -= 3;
522 break;
523
524 default:
525 /* nothing to do */
526 if (regs->areg[2] != 0)
527 break;
528 }
5a0015d6 529 }
5a0015d6 530
29c4dfd9
CZ
531 /* Whee! Actually deliver the signal. */
532 /* Set up the stack frame */
3785006a
MF
533 ret = setup_frame(signr, &ka, &info, oldset, regs);
534 if (ret)
535 return ret;
5a0015d6 536
d12f7c4a 537 block_sigmask(&ka, signr);
29c4dfd9
CZ
538 if (current->ptrace & PT_SINGLESTEP)
539 task_pt_regs(current)->icountlevel = 1;
5a0015d6 540
29c4dfd9
CZ
541 return 1;
542 }
5a0015d6 543
29c4dfd9
CZ
544no_signal:
545 /* Did we come from a system call? */
546 if ((signed) regs->syscall >= 0) {
547 /* Restart the system call - no handlers present */
548 switch (regs->areg[2]) {
549 case -ERESTARTNOHAND:
550 case -ERESTARTSYS:
551 case -ERESTARTNOINTR:
552 regs->areg[2] = regs->syscall;
553 regs->pc -= 3;
554 break;
555 case -ERESTART_RESTARTBLOCK:
556 regs->areg[2] = __NR_restart_syscall;
557 regs->pc -= 3;
558 break;
559 }
560 }
561 if (current->ptrace & PT_SINGLESTEP)
562 task_pt_regs(current)->icountlevel = 1;
563 return 0;
5a0015d6 564}
29c4dfd9 565
This page took 0.614161 seconds and 5 git commands to generate.