]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/cris/arch-v32/kernel/signal.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / arch / cris / arch-v32 / kernel / signal.c
CommitLineData
51533b61
MS
1/*
2 * Copyright (C) 2003, Axis Communications AB.
3 */
4
5#include <linux/sched.h>
6#include <linux/mm.h>
5a0e3ad6 7#include <linux/slab.h>
51533b61
MS
8#include <linux/kernel.h>
9#include <linux/signal.h>
10#include <linux/errno.h>
11#include <linux/wait.h>
12#include <linux/ptrace.h>
13#include <linux/unistd.h>
14#include <linux/stddef.h>
15#include <linux/syscalls.h>
16#include <linux/vmalloc.h>
17
18#include <asm/io.h>
19#include <asm/processor.h>
20#include <asm/ucontext.h>
21#include <asm/uaccess.h>
556dcee7
JN
22#include <arch/ptrace.h>
23#include <arch/hwregs/cpu_vect.h>
51533b61
MS
24
25extern unsigned long cris_signal_return_page;
26
27/* Flag to check if a signal is blockable. */
28#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
29
30/*
31 * A syscall in CRIS is really a "break 13" instruction, which is 2
32 * bytes. The registers is manipulated so upon return the instruction
33 * will be executed again.
34 *
35 * This relies on that PC points to the instruction after the break call.
36 */
37#define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
38
39/* Signal frames. */
40struct signal_frame {
41 struct sigcontext sc;
42 unsigned long extramask[_NSIG_WORDS - 1];
43 unsigned char retcode[8]; /* Trampoline code. */
44};
45
46struct rt_signal_frame {
47 struct siginfo *pinfo;
48 void *puc;
49 struct siginfo info;
50 struct ucontext uc;
51 unsigned char retcode[8]; /* Trampoline code. */
52};
53
574852a2 54void do_signal(int restart, struct pt_regs *regs);
51533b61
MS
55void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
56 struct pt_regs *regs);
57/*
58 * Swap in the new signal mask, and wait for a signal. Define some
59 * dummy arguments to be able to reach the regs argument.
60 */
61int
62sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
63 long srp, struct pt_regs *regs)
64{
51533b61 65 mask &= _BLOCKABLE;
51533b61 66 spin_lock_irq(&current->sighand->siglock);
574852a2 67 current->saved_sigmask = current->blocked;
51533b61 68 siginitset(&current->blocked, mask);
51533b61
MS
69 recalc_sigpending();
70 spin_unlock_irq(&current->sighand->siglock);
574852a2
JN
71 current->state = TASK_INTERRUPTIBLE;
72 schedule();
73 set_thread_flag(TIF_RESTORE_SIGMASK);
74 return -ERESTARTNOHAND;
51533b61
MS
75}
76
77int
78sys_sigaction(int signal, const struct old_sigaction *act,
79 struct old_sigaction *oact)
80{
81 int retval;
82 struct k_sigaction newk;
83 struct k_sigaction oldk;
84
85 if (act) {
86 old_sigset_t mask;
87
88 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
89 __get_user(newk.sa.sa_handler, &act->sa_handler) ||
90 __get_user(newk.sa.sa_restorer, &act->sa_restorer))
91 return -EFAULT;
92
93 __get_user(newk.sa.sa_flags, &act->sa_flags);
94 __get_user(mask, &act->sa_mask);
95 siginitset(&newk.sa.sa_mask, mask);
96 }
97
98 retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
99
100 if (!retval && oact) {
101 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
102 __put_user(oldk.sa.sa_handler, &oact->sa_handler) ||
103 __put_user(oldk.sa.sa_restorer, &oact->sa_restorer))
104 return -EFAULT;
105
106 __put_user(oldk.sa.sa_flags, &oact->sa_flags);
107 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
108 }
109
110 return retval;
111}
112
113int
114sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
115{
116 return do_sigaltstack(uss, uoss, rdusp());
117}
118
119static int
120restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
121{
122 unsigned int err = 0;
123 unsigned long old_usp;
124
125 /* Always make any pending restarted system calls return -EINTR */
126 current_thread_info()->restart_block.fn = do_no_restart_syscall;
127
128 /*
129 * Restore the registers from &sc->regs. sc is already checked
130 * for VERIFY_READ since the signal_frame was previously
131 * checked in sys_sigreturn().
132 */
133 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
134 goto badframe;
135
136 /* Make that the user-mode flag is set. */
137 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
138
139 /* Restore the old USP. */
140 err |= __get_user(old_usp, &sc->usp);
141 wrusp(old_usp);
142
143 return err;
144
145badframe:
146 return 1;
147}
148
149/* Define some dummy arguments to be able to reach the regs argument. */
150asmlinkage int
151sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
152 struct pt_regs *regs)
153{
154 sigset_t set;
155 struct signal_frame __user *frame;
156 unsigned long oldspc = regs->spc;
157 unsigned long oldccs = regs->ccs;
158
159 frame = (struct signal_frame *) rdusp();
160
161 /*
162 * Since the signal is stacked on a dword boundary, the frame
163 * should be dword aligned here as well. It it's not, then the
164 * user is trying some funny business.
165 */
166 if (((long)frame) & 3)
167 goto badframe;
168
169 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
170 goto badframe;
171
172 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
173 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
174 frame->extramask,
175 sizeof(frame->extramask))))
176 goto badframe;
177
178 sigdelsetmask(&set, ~_BLOCKABLE);
179 spin_lock_irq(&current->sighand->siglock);
180
181 current->blocked = set;
182
183 recalc_sigpending();
184 spin_unlock_irq(&current->sighand->siglock);
185
186 if (restore_sigcontext(regs, &frame->sc))
187 goto badframe;
188
189 keep_debug_flags(oldccs, oldspc, regs);
190
191 return regs->r10;
192
193badframe:
194 force_sig(SIGSEGV, current);
195 return 0;
196}
197
198/* Define some dummy variables to be able to reach the regs argument. */
199asmlinkage int
200sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
201 struct pt_regs *regs)
202{
203 sigset_t set;
204 struct rt_signal_frame __user *frame;
205 unsigned long oldspc = regs->spc;
206 unsigned long oldccs = regs->ccs;
207
208 frame = (struct rt_signal_frame *) rdusp();
209
210 /*
211 * Since the signal is stacked on a dword boundary, the frame
212 * should be dword aligned here as well. It it's not, then the
213 * user is trying some funny business.
214 */
215 if (((long)frame) & 3)
216 goto badframe;
217
218 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
219 goto badframe;
220
221 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
222 goto badframe;
223
224 sigdelsetmask(&set, ~_BLOCKABLE);
225 spin_lock_irq(&current->sighand->siglock);
226
227 current->blocked = set;
228
229 recalc_sigpending();
230 spin_unlock_irq(&current->sighand->siglock);
231
232 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
233 goto badframe;
234
235 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
574852a2 236 goto badframe;
51533b61
MS
237
238 keep_debug_flags(oldccs, oldspc, regs);
239
240 return regs->r10;
241
242badframe:
243 force_sig(SIGSEGV, current);
244 return 0;
245}
246
247/* Setup a signal frame. */
248static int
249setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
250 unsigned long mask)
251{
252 int err;
253 unsigned long usp;
254
255 err = 0;
256 usp = rdusp();
257
258 /*
259 * Copy the registers. They are located first in sc, so it's
260 * possible to use sc directly.
261 */
262 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
263
264 err |= __put_user(mask, &sc->oldmask);
265 err |= __put_user(usp, &sc->usp);
266
267 return err;
268}
269
270/* Figure out where to put the new signal frame - usually on the stack. */
271static inline void __user *
272get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
273{
274 unsigned long sp;
275
276 sp = rdusp();
277
278 /* This is the X/Open sanctioned signal stack switching. */
279 if (ka->sa.sa_flags & SA_ONSTACK) {
280 if (!on_sig_stack(sp))
281 sp = current->sas_ss_sp + current->sas_ss_size;
282 }
283
284 /* Make sure the frame is dword-aligned. */
285 sp &= ~3;
286
287 return (void __user *)(sp - frame_size);
288}
289
290/* Grab and setup a signal frame.
291 *
292 * Basically a lot of state-info is stacked, and arranged for the
574852a2 293 * user-mode program to return to the kernel using either a trampiline
51533b61
MS
294 * which performs the syscall sigreturn(), or a provided user-mode
295 * trampoline.
296 */
574852a2 297static int
51533b61
MS
298setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
299 struct pt_regs * regs)
300{
301 int err;
302 unsigned long return_ip;
303 struct signal_frame __user *frame;
304
305 err = 0;
306 frame = get_sigframe(ka, regs, sizeof(*frame));
307
308 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
309 goto give_sigsegv;
310
311 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
312
313 if (err)
314 goto give_sigsegv;
315
316 if (_NSIG_WORDS > 1) {
317 err |= __copy_to_user(frame->extramask, &set->sig[1],
318 sizeof(frame->extramask));
319 }
320
321 if (err)
322 goto give_sigsegv;
323
324 /*
325 * Set up to return from user-space. If provided, use a stub
326 * already located in user-space.
327 */
328 if (ka->sa.sa_flags & SA_RESTORER) {
329 return_ip = (unsigned long)ka->sa.sa_restorer;
330 } else {
331 /* Trampoline - the desired return ip is in the signal return page. */
332 return_ip = cris_signal_return_page;
333
334 /*
335 * This is movu.w __NR_sigreturn, r9; break 13;
336 *
337 * WE DO NOT USE IT ANY MORE! It's only left here for historical
338 * reasons and because gdb uses it as a signature to notice
339 * signal handler stack frames.
340 */
341 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
342 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
343 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
344 }
345
346 if (err)
347 goto give_sigsegv;
348
349 /*
350 * Set up registers for signal handler.
351 *
352 * Where the code enters now.
353 * Where the code enter later.
354 * First argument, signo.
355 */
356 regs->erp = (unsigned long) ka->sa.sa_handler;
357 regs->srp = return_ip;
358 regs->r10 = sig;
359
360 /* Actually move the USP to reflect the stacked frame. */
361 wrusp((unsigned long)frame);
362
574852a2 363 return 0;
51533b61
MS
364
365give_sigsegv:
366 if (sig == SIGSEGV)
367 ka->sa.sa_handler = SIG_DFL;
368
369 force_sig(SIGSEGV, current);
574852a2 370 return -EFAULT;
51533b61
MS
371}
372
574852a2 373static int
51533b61
MS
374setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
375 sigset_t *set, struct pt_regs * regs)
376{
377 int err;
378 unsigned long return_ip;
379 struct rt_signal_frame __user *frame;
380
381 err = 0;
382 frame = get_sigframe(ka, regs, sizeof(*frame));
383
384 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
385 goto give_sigsegv;
386
387 /* TODO: what is the current->exec_domain stuff and invmap ? */
388
389 err |= __put_user(&frame->info, &frame->pinfo);
390 err |= __put_user(&frame->uc, &frame->puc);
391 err |= copy_siginfo_to_user(&frame->info, info);
392
393 if (err)
394 goto give_sigsegv;
395
396 /* Clear all the bits of the ucontext we don't use. */
397 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
398 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
399 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
400
401 if (err)
402 goto give_sigsegv;
403
404 /*
405 * Set up to return from user-space. If provided, use a stub
406 * already located in user-space.
407 */
408 if (ka->sa.sa_flags & SA_RESTORER) {
409 return_ip = (unsigned long) ka->sa.sa_restorer;
410 } else {
411 /* Trampoline - the desired return ip is in the signal return page. */
412 return_ip = cris_signal_return_page + 6;
413
414 /*
415 * This is movu.w __NR_rt_sigreturn, r9; break 13;
416 *
417 * WE DO NOT USE IT ANY MORE! It's only left here for historical
418 * reasons and because gdb uses it as a signature to notice
419 * signal handler stack frames.
420 */
421 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
422
423 err |= __put_user(__NR_rt_sigreturn,
424 (short __user*)(frame->retcode+2));
425
426 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
427 }
428
429 if (err)
430 goto give_sigsegv;
431
432 /*
433 * Set up registers for signal handler.
434 *
435 * Where the code enters now.
436 * Where the code enters later.
437 * First argument is signo.
438 * Second argument is (siginfo_t *).
439 * Third argument is unused.
440 */
441 regs->erp = (unsigned long) ka->sa.sa_handler;
442 regs->srp = return_ip;
443 regs->r10 = sig;
444 regs->r11 = (unsigned long) &frame->info;
445 regs->r12 = 0;
446
447 /* Actually move the usp to reflect the stacked frame. */
448 wrusp((unsigned long)frame);
449
574852a2 450 return 0;
51533b61
MS
451
452give_sigsegv:
453 if (sig == SIGSEGV)
454 ka->sa.sa_handler = SIG_DFL;
455
456 force_sig(SIGSEGV, current);
574852a2 457 return -EFAULT;
51533b61
MS
458}
459
c03264a7 460/* Invoke a signal handler to, well, handle the signal. */
574852a2 461static inline int
51533b61
MS
462handle_signal(int canrestart, unsigned long sig,
463 siginfo_t *info, struct k_sigaction *ka,
464 sigset_t *oldset, struct pt_regs * regs)
465{
574852a2
JN
466 int ret;
467
51533b61
MS
468 /* Check if this got called from a system call. */
469 if (canrestart) {
470 /* If so, check system call restarting. */
471 switch (regs->r10) {
472 case -ERESTART_RESTARTBLOCK:
473 case -ERESTARTNOHAND:
474 /*
475 * This means that the syscall should
476 * only be restarted if there was no
477 * handler for the signal, and since
478 * this point isn't reached unless
479 * there is a handler, there's no need
480 * to restart.
481 */
482 regs->r10 = -EINTR;
483 break;
484
485 case -ERESTARTSYS:
486 /*
487 * This means restart the syscall if
488 * there is no handler, or the handler
489 * was registered with SA_RESTART.
490 */
491 if (!(ka->sa.sa_flags & SA_RESTART)) {
492 regs->r10 = -EINTR;
493 break;
494 }
495
496 /* Fall through. */
497
498 case -ERESTARTNOINTR:
499 /*
500 * This means that the syscall should
501 * be called again after the signal
502 * handler returns.
503 */
504 RESTART_CRIS_SYS(regs);
505 break;
506 }
507 }
508
509 /* Set up the stack frame. */
510 if (ka->sa.sa_flags & SA_SIGINFO)
574852a2 511 ret = setup_rt_frame(sig, ka, info, oldset, regs);
51533b61 512 else
574852a2 513 ret = setup_frame(sig, ka, oldset, regs);
51533b61
MS
514
515 if (ka->sa.sa_flags & SA_ONESHOT)
516 ka->sa.sa_handler = SIG_DFL;
517
574852a2
JN
518 if (ret == 0) {
519 spin_lock_irq(&current->sighand->siglock);
520 sigorsets(&current->blocked, &current->blocked,
521 &ka->sa.sa_mask);
522 if (!(ka->sa.sa_flags & SA_NODEFER))
523 sigaddset(&current->blocked, sig);
524 recalc_sigpending();
525 spin_unlock_irq(&current->sighand->siglock);
526 }
527
528 return ret;
51533b61
MS
529}
530
531/*
532 * Note that 'init' is a special process: it doesn't get signals it doesn't
533 * want to handle. Thus you cannot kill init even with a SIGKILL even by
534 * mistake.
535 *
536 * Also note that the regs structure given here as an argument, is the latest
537 * pushed pt_regs. It may or may not be the same as the first pushed registers
538 * when the initial usermode->kernelmode transition took place. Therefore
539 * we can use user_mode(regs) to see if we came directly from kernel or user
540 * mode below.
541 */
574852a2
JN
542void
543do_signal(int canrestart, struct pt_regs *regs)
51533b61
MS
544{
545 int signr;
546 siginfo_t info;
547 struct k_sigaction ka;
574852a2 548 sigset_t *oldset;
51533b61
MS
549
550 /*
551 * The common case should go fast, which is why this point is
552 * reached from kernel-mode. If that's the case, just return
553 * without doing anything.
554 */
555 if (!user_mode(regs))
574852a2 556 return;
51533b61 557
574852a2
JN
558 if (test_thread_flag(TIF_RESTORE_SIGMASK))
559 oldset = &current->saved_sigmask;
560 else
51533b61
MS
561 oldset = &current->blocked;
562
563 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
564
565 if (signr > 0) {
574852a2
JN
566 /* Whee! Actually deliver the signal. */
567 if (handle_signal(canrestart, signr, &info, &ka,
568 oldset, regs)) {
569 /* a signal was successfully delivered; the saved
570 * sigmask will have been stored in the signal frame,
571 * and will be restored by sigreturn, so we can simply
572 * clear the TIF_RESTORE_SIGMASK flag */
573 if (test_thread_flag(TIF_RESTORE_SIGMASK))
574 clear_thread_flag(TIF_RESTORE_SIGMASK);
575 }
576
577 return;
51533b61
MS
578 }
579
580 /* Got here from a system call? */
581 if (canrestart) {
582 /* Restart the system call - no handlers present. */
583 if (regs->r10 == -ERESTARTNOHAND ||
584 regs->r10 == -ERESTARTSYS ||
585 regs->r10 == -ERESTARTNOINTR) {
586 RESTART_CRIS_SYS(regs);
587 }
588
589 if (regs->r10 == -ERESTART_RESTARTBLOCK){
590 regs->r10 = __NR_restart_syscall;
591 regs->erp -= 2;
592 }
593 }
594
574852a2
JN
595 /* if there's no signal to deliver, we just put the saved sigmask
596 * back */
597 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
598 clear_thread_flag(TIF_RESTORE_SIGMASK);
599 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
600 }
51533b61
MS
601}
602
603asmlinkage void
604ugdb_trap_user(struct thread_info *ti, int sig)
605{
606 if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
607 /* Zero single-step PC if the reason we stopped wasn't a single
608 step exception. This is to avoid relying on it when it isn't
609 reliable. */
610 user_regs(ti)->spc = 0;
611 }
612 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
574852a2 613 not withing any configured h/w breakpoint range). Synchronize with
51533b61
MS
614 what already exists for kernel debugging. */
615 if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
616 /* Break 8: subtract 2 from ERP unless in a delay slot. */
617 if (!(user_regs(ti)->erp & 0x1))
618 user_regs(ti)->erp -= 2;
619 }
620 sys_kill(ti->task->pid, sig);
621}
622
623void
624keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
625 struct pt_regs *regs)
626{
627 if (oldccs & (1 << Q_CCS_BITNR)) {
628 /* Pending single step due to single-stepping the break 13
629 in the signal trampoline: keep the Q flag. */
630 regs->ccs |= (1 << Q_CCS_BITNR);
631 /* S flag should be set - complain if it's not. */
632 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
633 printk("Q flag but no S flag?");
634 }
635 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
636 /* Assume the SPC is valid and interesting. */
637 regs->spc = oldspc;
638
639 } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
640 /* If a h/w bp was set in the signal handler we need
641 to keep the S flag. */
642 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
643 /* Don't keep the old SPC though; if we got here due to
644 a single-step, the Q flag should have been set. */
645 } else if (regs->spc) {
646 /* If we were single-stepping *before* the signal was taken,
647 we don't want to restore that state now, because GDB will
648 have forgotten all about it. */
649 regs->spc = 0;
650 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
651 }
652}
653
654/* Set up the trampolines on the signal return page. */
655int __init
656cris_init_signal(void)
657{
5cbded58 658 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
51533b61
MS
659
660 /* This is movu.w __NR_sigreturn, r9; break 13; */
661 data[0] = 0x9c5f;
662 data[1] = __NR_sigreturn;
663 data[2] = 0xe93d;
664 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
665 data[3] = 0x9c5f;
666 data[4] = __NR_rt_sigreturn;
667 data[5] = 0xe93d;
668
669 /* Map to userspace with appropriate permissions (no write access...) */
670 cris_signal_return_page = (unsigned long)
671 __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
672
673 return 0;
674}
675
676__initcall(cris_init_signal);