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