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