Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[linux-2.6/linux-loongson.git] / arch / microblaze / kernel / signal.c
blob3889cf45fa71451b904dc5e880f2a54c783b4b23
1 /*
2 * Signal handling
4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2008-2009 PetaLogix
6 * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
7 * Copyright (C) 2001 NEC Corporation
8 * Copyright (C) 2001 Miles Bader <miles@gnu.org>
9 * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
10 * Copyright (C) 1991,1992 Linus Torvalds
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 * This file was was derived from the sh version, arch/sh/kernel/signal.c
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file COPYING in the main directory of this
18 * archive for more details.
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/kernel.h>
26 #include <linux/signal.h>
27 #include <linux/errno.h>
28 #include <linux/wait.h>
29 #include <linux/ptrace.h>
30 #include <linux/unistd.h>
31 #include <linux/stddef.h>
32 #include <linux/personality.h>
33 #include <linux/percpu.h>
34 #include <linux/linkage.h>
35 #include <asm/entry.h>
36 #include <asm/ucontext.h>
37 #include <linux/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/pgalloc.h>
40 #include <linux/syscalls.h>
41 #include <asm/cacheflush.h>
42 #include <asm/syscalls.h>
44 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
46 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_sycall);
49 * Atomically swap in the new signal mask, and wait for a signal.
51 asmlinkage int
52 sys_sigsuspend(old_sigset_t mask, struct pt_regs *regs)
54 sigset_t saveset;
56 mask &= _BLOCKABLE;
57 spin_lock_irq(&current->sighand->siglock);
58 saveset = current->blocked;
59 siginitset(&current->blocked, mask);
60 recalc_sigpending();
61 spin_unlock_irq(&current->sighand->siglock);
63 regs->r3 = -EINTR;
64 while (1) {
65 current->state = TASK_INTERRUPTIBLE;
66 schedule();
67 if (do_signal(regs, &saveset, 1))
68 return -EINTR;
72 asmlinkage int
73 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize,
74 struct pt_regs *regs)
76 sigset_t saveset, newset;
78 /* XXX: Don't preclude handling different sized sigset_t's. */
79 if (sigsetsize != sizeof(sigset_t))
80 return -EINVAL;
82 if (copy_from_user(&newset, unewset, sizeof(newset)))
83 return -EFAULT;
84 sigdelsetmask(&newset, ~_BLOCKABLE);
85 spin_lock_irq(&current->sighand->siglock);
86 saveset = current->blocked;
87 current->blocked = newset;
88 recalc_sigpending();
89 spin_unlock_irq(&current->sighand->siglock);
91 regs->r3 = -EINTR;
92 while (1) {
93 current->state = TASK_INTERRUPTIBLE;
94 schedule();
95 if (do_signal(regs, &saveset, 1))
96 return -EINTR;
100 asmlinkage int
101 sys_sigaction(int sig, const struct old_sigaction *act,
102 struct old_sigaction *oact)
104 struct k_sigaction new_ka, old_ka;
105 int ret;
107 if (act) {
108 old_sigset_t mask;
109 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
110 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
111 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
112 return -EFAULT;
113 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
114 __get_user(mask, &act->sa_mask);
115 siginitset(&new_ka.sa.sa_mask, mask);
118 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
120 if (!ret && oact) {
121 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
122 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
123 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
124 return -EFAULT;
125 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
126 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
129 return ret;
132 asmlinkage int
133 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
134 struct pt_regs *regs)
136 return do_sigaltstack(uss, uoss, regs->r1);
140 * Do a signal return; undo the signal stack.
143 struct sigframe {
144 struct sigcontext sc;
145 unsigned long extramask[_NSIG_WORDS-1];
146 unsigned long tramp[2]; /* signal trampoline */
149 struct rt_sigframe {
150 struct siginfo info;
151 struct ucontext uc;
152 unsigned long tramp[2]; /* signal trampoline */
155 static int
156 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, int *rval_p)
158 unsigned int err = 0;
160 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
161 COPY(r0);
162 COPY(r1);
163 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
164 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
165 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
166 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
167 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
168 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
169 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
170 COPY(r30); COPY(r31);
171 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
172 #undef COPY
174 *rval_p = regs->r3;
176 return err;
179 asmlinkage int sys_sigreturn(struct pt_regs *regs)
181 struct sigframe *frame = (struct sigframe *)regs->r1;
182 sigset_t set;
183 int rval;
185 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
186 goto badframe;
188 if (__get_user(set.sig[0], &frame->sc.oldmask)
189 || (_NSIG_WORDS > 1
190 && __copy_from_user(&set.sig[1], &frame->extramask,
191 sizeof(frame->extramask))))
192 goto badframe;
194 sigdelsetmask(&set, ~_BLOCKABLE);
196 spin_lock_irq(&current->sighand->siglock);
197 current->blocked = set;
198 recalc_sigpending();
199 spin_unlock_irq(&current->sighand->siglock);
201 if (restore_sigcontext(regs, &frame->sc, &rval))
202 goto badframe;
203 return rval;
205 badframe:
206 force_sig(SIGSEGV, current);
207 return 0;
210 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
212 struct rt_sigframe *frame = (struct rt_sigframe *)regs->r1;
213 sigset_t set;
214 stack_t st;
215 int rval;
217 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
218 goto badframe;
220 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
221 goto badframe;
223 sigdelsetmask(&set, ~_BLOCKABLE);
224 spin_lock_irq(&current->sighand->siglock);
225 current->blocked = set;
226 recalc_sigpending();
227 spin_unlock_irq(&current->sighand->siglock);
229 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
230 goto badframe;
232 if (__copy_from_user((void *)&st, &frame->uc.uc_stack, sizeof(st)))
233 goto badframe;
234 /* It is more difficult to avoid calling this function than to
235 call it and ignore errors. */
236 do_sigaltstack(&st, NULL, regs->r1);
238 return rval;
240 badframe:
241 force_sig(SIGSEGV, current);
242 return 0;
246 * Set up a signal frame.
249 static int
250 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
251 unsigned long mask)
253 int err = 0;
255 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
256 COPY(r0);
257 COPY(r1);
258 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
259 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
260 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
261 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
262 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
263 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
264 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
265 COPY(r30); COPY(r31);
266 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
267 #undef COPY
269 err |= __put_user(mask, &sc->oldmask);
271 return err;
275 * Determine which stack to use..
277 static inline void *
278 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
280 /* Default to using normal stack */
281 unsigned long sp = regs->r1;
283 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
284 sp = current->sas_ss_sp + current->sas_ss_size;
286 return (void *)((sp - frame_size) & -8UL);
289 static void setup_frame(int sig, struct k_sigaction *ka,
290 sigset_t *set, struct pt_regs *regs)
292 struct sigframe *frame;
293 int err = 0;
294 int signal;
296 frame = get_sigframe(ka, regs, sizeof(*frame));
298 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
299 goto give_sigsegv;
301 signal = current_thread_info()->exec_domain
302 && current_thread_info()->exec_domain->signal_invmap
303 && sig < 32
304 ? current_thread_info()->exec_domain->signal_invmap[sig]
305 : sig;
307 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
309 if (_NSIG_WORDS > 1) {
310 err |= __copy_to_user(frame->extramask, &set->sig[1],
311 sizeof(frame->extramask));
314 /* Set up to return from userspace. If provided, use a stub
315 already in userspace. */
316 /* minus 8 is offset to cater for "rtsd r15,8" offset */
317 if (ka->sa.sa_flags & SA_RESTORER) {
318 regs->r15 = ((unsigned long)ka->sa.sa_restorer)-8;
319 } else {
320 /* Note, these encodings are _big endian_! */
322 /* addi r12, r0, __NR_sigreturn */
323 err |= __put_user(0x31800000 | __NR_sigreturn ,
324 frame->tramp + 0);
325 /* brki r14, 0x8 */
326 err |= __put_user(0xb9cc0008, frame->tramp + 1);
328 /* Return from sighandler will jump to the tramp.
329 Negative 8 offset because return is rtsd r15, 8 */
330 regs->r15 = ((unsigned long)frame->tramp)-8;
332 __invalidate_cache_sigtramp((unsigned long)frame->tramp);
335 if (err)
336 goto give_sigsegv;
338 /* Set up registers for signal handler */
339 regs->r1 = (unsigned long) frame;
340 /* Signal handler args: */
341 regs->r5 = signal; /* Arg 0: signum */
342 regs->r6 = (unsigned long) &frame->sc; /* arg 1: sigcontext */
344 /* Offset of 4 to handle microblaze rtid r14, 0 */
345 regs->pc = (unsigned long)ka->sa.sa_handler;
347 set_fs(USER_DS);
349 #ifdef DEBUG_SIG
350 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
351 current->comm, current->pid, frame, regs->pc);
352 #endif
354 return;
356 give_sigsegv:
357 if (sig == SIGSEGV)
358 ka->sa.sa_handler = SIG_DFL;
359 force_sig(SIGSEGV, current);
362 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
363 sigset_t *set, struct pt_regs *regs)
365 struct rt_sigframe *frame;
366 int err = 0;
367 int signal;
369 frame = get_sigframe(ka, regs, sizeof(*frame));
371 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
372 goto give_sigsegv;
374 signal = current_thread_info()->exec_domain
375 && current_thread_info()->exec_domain->signal_invmap
376 && sig < 32
377 ? current_thread_info()->exec_domain->signal_invmap[sig]
378 : sig;
380 err |= copy_siginfo_to_user(&frame->info, info);
382 /* Create the ucontext. */
383 err |= __put_user(0, &frame->uc.uc_flags);
384 err |= __put_user(0, &frame->uc.uc_link);
385 err |= __put_user((void *)current->sas_ss_sp,
386 &frame->uc.uc_stack.ss_sp);
387 err |= __put_user(sas_ss_flags(regs->r1),
388 &frame->uc.uc_stack.ss_flags);
389 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
390 err |= setup_sigcontext(&frame->uc.uc_mcontext,
391 regs, set->sig[0]);
392 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
394 /* Set up to return from userspace. If provided, use a stub
395 already in userspace. */
396 /* minus 8 is offset to cater for "rtsd r15,8" */
397 if (ka->sa.sa_flags & SA_RESTORER) {
398 regs->r15 = ((unsigned long)ka->sa.sa_restorer)-8;
399 } else {
400 /* addi r12, r0, __NR_sigreturn */
401 err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
402 frame->tramp + 0);
403 /* brki r14, 0x8 */
404 err |= __put_user(0xb9cc0008, frame->tramp + 1);
406 /* Return from sighandler will jump to the tramp.
407 Negative 8 offset because return is rtsd r15, 8 */
408 regs->r15 = ((unsigned long)frame->tramp)-8;
410 __invalidate_cache_sigtramp((unsigned long)frame->tramp);
413 if (err)
414 goto give_sigsegv;
416 /* Set up registers for signal handler */
417 regs->r1 = (unsigned long) frame;
418 /* Signal handler args: */
419 regs->r5 = signal; /* arg 0: signum */
420 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
421 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
422 /* Offset to handle microblaze rtid r14, 0 */
423 regs->pc = (unsigned long)ka->sa.sa_handler;
425 set_fs(USER_DS);
427 #ifdef DEBUG_SIG
428 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
429 current->comm, current->pid, frame, regs->pc);
430 #endif
432 return;
434 give_sigsegv:
435 if (sig == SIGSEGV)
436 ka->sa.sa_handler = SIG_DFL;
437 force_sig(SIGSEGV, current);
440 /* Handle restarting system calls */
441 static inline void
442 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
444 switch (regs->r3) {
445 case -ERESTART_RESTARTBLOCK:
446 case -ERESTARTNOHAND:
447 if (!has_handler)
448 goto do_restart;
449 regs->r3 = -EINTR;
450 break;
451 case -ERESTARTSYS:
452 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
453 regs->r3 = -EINTR;
454 break;
456 /* fallthrough */
457 case -ERESTARTNOINTR:
458 do_restart:
459 /* offset of 4 bytes to re-execute trap (brki) instruction */
460 regs->pc -= 4;
461 break;
466 * OK, we're invoking a handler
469 static void
470 handle_signal(unsigned long sig, struct k_sigaction *ka,
471 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
473 /* Set up the stack frame */
474 if (ka->sa.sa_flags & SA_SIGINFO)
475 setup_rt_frame(sig, ka, info, oldset, regs);
476 else
477 setup_frame(sig, ka, oldset, regs);
479 if (ka->sa.sa_flags & SA_ONESHOT)
480 ka->sa.sa_handler = SIG_DFL;
482 if (!(ka->sa.sa_flags & SA_NODEFER)) {
483 spin_lock_irq(&current->sighand->siglock);
484 sigorsets(&current->blocked,
485 &current->blocked, &ka->sa.sa_mask);
486 sigaddset(&current->blocked, sig);
487 recalc_sigpending();
488 spin_unlock_irq(&current->sighand->siglock);
493 * Note that 'init' is a special process: it doesn't get signals it doesn't
494 * want to handle. Thus you cannot kill init even with a SIGKILL even by
495 * mistake.
497 * Note that we go through the signals twice: once to check the signals that
498 * the kernel can handle, and then we build all the user-level signal handling
499 * stack-frames in one go after that.
501 int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_syscall)
503 siginfo_t info;
504 int signr;
505 struct k_sigaction ka;
506 #ifdef DEBUG_SIG
507 printk(KERN_INFO "do signal: %p %p %d\n", regs, oldset, in_syscall);
508 printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
509 regs->r12, current_thread_info()->flags);
510 #endif
512 * We want the common case to go fast, which
513 * is why we may in certain cases get here from
514 * kernel mode. Just return without doing anything
515 * if so.
517 if (kernel_mode(regs))
518 return 1;
520 if (!oldset)
521 oldset = &current->blocked;
523 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
524 if (signr > 0) {
525 /* Whee! Actually deliver the signal. */
526 if (in_syscall)
527 handle_restart(regs, &ka, 1);
528 handle_signal(signr, &ka, &info, oldset, regs);
529 return 1;
532 if (in_syscall)
533 handle_restart(regs, NULL, 0);
535 /* Did we come from a system call? */
536 return 0;