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