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