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