2 * Copyright (C) 2003, Axis Communications AB.
5 #include <linux/sched.h>
7 #include <linux/slab.h>
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>
19 #include <asm/processor.h>
20 #include <asm/ucontext.h>
21 #include <asm/uaccess.h>
22 #include <arch/ptrace.h>
23 #include <arch/hwregs/cpu_vect.h>
25 extern unsigned long cris_signal_return_page
;
27 /* Flag to check if a signal is blockable. */
28 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
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.
35 * This relies on that PC points to the instruction after the break call.
37 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
42 unsigned long extramask
[_NSIG_WORDS
- 1];
43 unsigned char retcode
[8]; /* Trampoline code. */
46 struct rt_signal_frame
{
47 struct siginfo
*pinfo
;
51 unsigned char retcode
[8]; /* Trampoline code. */
54 void do_signal(int restart
, struct pt_regs
*regs
);
55 void keep_debug_flags(unsigned long oldccs
, unsigned long oldspc
,
56 struct pt_regs
*regs
);
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.
62 sys_sigsuspend(old_sigset_t mask
, long r11
, long r12
, long r13
, long mof
,
63 long srp
, struct pt_regs
*regs
)
67 current
->saved_sigmask
= current
->blocked
;
70 siginitset(&blocked
, mask
);
71 set_current_blocked(&blocked
);
73 current
->state
= TASK_INTERRUPTIBLE
;
75 set_thread_flag(TIF_RESTORE_SIGMASK
);
76 return -ERESTARTNOHAND
;
80 sys_sigaction(int signal
, const struct old_sigaction
*act
,
81 struct old_sigaction
*oact
)
84 struct k_sigaction newk
;
85 struct k_sigaction oldk
;
90 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
91 __get_user(newk
.sa
.sa_handler
, &act
->sa_handler
) ||
92 __get_user(newk
.sa
.sa_restorer
, &act
->sa_restorer
))
95 __get_user(newk
.sa
.sa_flags
, &act
->sa_flags
);
96 __get_user(mask
, &act
->sa_mask
);
97 siginitset(&newk
.sa
.sa_mask
, mask
);
100 retval
= do_sigaction(signal
, act
? &newk
: NULL
, oact
? &oldk
: NULL
);
102 if (!retval
&& oact
) {
103 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
104 __put_user(oldk
.sa
.sa_handler
, &oact
->sa_handler
) ||
105 __put_user(oldk
.sa
.sa_restorer
, &oact
->sa_restorer
))
108 __put_user(oldk
.sa
.sa_flags
, &oact
->sa_flags
);
109 __put_user(oldk
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
116 sys_sigaltstack(const stack_t __user
*uss
, stack_t __user
*uoss
)
118 return do_sigaltstack(uss
, uoss
, rdusp());
122 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
124 unsigned int err
= 0;
125 unsigned long old_usp
;
127 /* Always make any pending restarted system calls return -EINTR */
128 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
131 * Restore the registers from &sc->regs. sc is already checked
132 * for VERIFY_READ since the signal_frame was previously
133 * checked in sys_sigreturn().
135 if (__copy_from_user(regs
, sc
, sizeof(struct pt_regs
)))
138 /* Make that the user-mode flag is set. */
139 regs
->ccs
|= (1 << (U_CCS_BITNR
+ CCS_SHIFT
));
141 /* Restore the old USP. */
142 err
|= __get_user(old_usp
, &sc
->usp
);
151 /* Define some dummy arguments to be able to reach the regs argument. */
153 sys_sigreturn(long r10
, long r11
, long r12
, long r13
, long mof
, long srp
,
154 struct pt_regs
*regs
)
157 struct signal_frame __user
*frame
;
158 unsigned long oldspc
= regs
->spc
;
159 unsigned long oldccs
= regs
->ccs
;
161 frame
= (struct signal_frame
*) rdusp();
164 * Since the signal is stacked on a dword boundary, the frame
165 * should be dword aligned here as well. It it's not, then the
166 * user is trying some funny business.
168 if (((long)frame
) & 3)
171 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
174 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
) ||
175 (_NSIG_WORDS
> 1 && __copy_from_user(&set
.sig
[1],
177 sizeof(frame
->extramask
))))
180 sigdelsetmask(&set
, ~_BLOCKABLE
);
181 set_current_blocked(&set
);
183 if (restore_sigcontext(regs
, &frame
->sc
))
186 keep_debug_flags(oldccs
, oldspc
, regs
);
191 force_sig(SIGSEGV
, current
);
195 /* Define some dummy variables to be able to reach the regs argument. */
197 sys_rt_sigreturn(long r10
, long r11
, long r12
, long r13
, long mof
, long srp
,
198 struct pt_regs
*regs
)
201 struct rt_signal_frame __user
*frame
;
202 unsigned long oldspc
= regs
->spc
;
203 unsigned long oldccs
= regs
->ccs
;
205 frame
= (struct rt_signal_frame
*) rdusp();
208 * Since the signal is stacked on a dword boundary, the frame
209 * should be dword aligned here as well. It it's not, then the
210 * user is trying some funny business.
212 if (((long)frame
) & 3)
215 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
218 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
221 sigdelsetmask(&set
, ~_BLOCKABLE
);
222 set_current_blocked(&set
);
224 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
227 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, rdusp()) == -EFAULT
)
230 keep_debug_flags(oldccs
, oldspc
, regs
);
235 force_sig(SIGSEGV
, current
);
239 /* Setup a signal frame. */
241 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
251 * Copy the registers. They are located first in sc, so it's
252 * possible to use sc directly.
254 err
|= __copy_to_user(sc
, regs
, sizeof(struct pt_regs
));
256 err
|= __put_user(mask
, &sc
->oldmask
);
257 err
|= __put_user(usp
, &sc
->usp
);
262 /* Figure out where to put the new signal frame - usually on the stack. */
263 static inline void __user
*
264 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
270 /* This is the X/Open sanctioned signal stack switching. */
271 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
272 if (!on_sig_stack(sp
))
273 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
276 /* Make sure the frame is dword-aligned. */
279 return (void __user
*)(sp
- frame_size
);
282 /* Grab and setup a signal frame.
284 * Basically a lot of state-info is stacked, and arranged for the
285 * user-mode program to return to the kernel using either a trampiline
286 * which performs the syscall sigreturn(), or a provided user-mode
290 setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
291 struct pt_regs
* regs
)
294 unsigned long return_ip
;
295 struct signal_frame __user
*frame
;
298 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
300 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
303 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
308 if (_NSIG_WORDS
> 1) {
309 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
310 sizeof(frame
->extramask
));
317 * Set up to return from user-space. If provided, use a stub
318 * already located in user-space.
320 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
321 return_ip
= (unsigned long)ka
->sa
.sa_restorer
;
323 /* Trampoline - the desired return ip is in the signal return page. */
324 return_ip
= cris_signal_return_page
;
327 * This is movu.w __NR_sigreturn, r9; break 13;
329 * WE DO NOT USE IT ANY MORE! It's only left here for historical
330 * reasons and because gdb uses it as a signature to notice
331 * signal handler stack frames.
333 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
334 err
|= __put_user(__NR_sigreturn
, (short __user
*)(frame
->retcode
+2));
335 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
342 * Set up registers for signal handler.
344 * Where the code enters now.
345 * Where the code enter later.
346 * First argument, signo.
348 regs
->erp
= (unsigned long) ka
->sa
.sa_handler
;
349 regs
->srp
= return_ip
;
352 /* Actually move the USP to reflect the stacked frame. */
353 wrusp((unsigned long)frame
);
359 ka
->sa
.sa_handler
= SIG_DFL
;
361 force_sig(SIGSEGV
, current
);
366 setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
367 sigset_t
*set
, struct pt_regs
* regs
)
370 unsigned long return_ip
;
371 struct rt_signal_frame __user
*frame
;
374 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
376 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
379 /* TODO: what is the current->exec_domain stuff and invmap ? */
381 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
382 err
|= __put_user(&frame
->uc
, &frame
->puc
);
383 err
|= copy_siginfo_to_user(&frame
->info
, info
);
388 /* Clear all the bits of the ucontext we don't use. */
389 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
390 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, set
->sig
[0]);
391 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
397 * Set up to return from user-space. If provided, use a stub
398 * already located in user-space.
400 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
401 return_ip
= (unsigned long) ka
->sa
.sa_restorer
;
403 /* Trampoline - the desired return ip is in the signal return page. */
404 return_ip
= cris_signal_return_page
+ 6;
407 * This is movu.w __NR_rt_sigreturn, r9; break 13;
409 * WE DO NOT USE IT ANY MORE! It's only left here for historical
410 * reasons and because gdb uses it as a signature to notice
411 * signal handler stack frames.
413 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
415 err
|= __put_user(__NR_rt_sigreturn
,
416 (short __user
*)(frame
->retcode
+2));
418 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
425 * Set up registers for signal handler.
427 * Where the code enters now.
428 * Where the code enters later.
429 * First argument is signo.
430 * Second argument is (siginfo_t *).
431 * Third argument is unused.
433 regs
->erp
= (unsigned long) ka
->sa
.sa_handler
;
434 regs
->srp
= return_ip
;
436 regs
->r11
= (unsigned long) &frame
->info
;
439 /* Actually move the usp to reflect the stacked frame. */
440 wrusp((unsigned long)frame
);
446 ka
->sa
.sa_handler
= SIG_DFL
;
448 force_sig(SIGSEGV
, current
);
452 /* Invoke a signal handler to, well, handle the signal. */
454 handle_signal(int canrestart
, unsigned long sig
,
455 siginfo_t
*info
, struct k_sigaction
*ka
,
456 sigset_t
*oldset
, struct pt_regs
* regs
)
460 /* Check if this got called from a system call. */
462 /* If so, check system call restarting. */
464 case -ERESTART_RESTARTBLOCK
:
465 case -ERESTARTNOHAND
:
467 * This means that the syscall should
468 * only be restarted if there was no
469 * handler for the signal, and since
470 * this point isn't reached unless
471 * there is a handler, there's no need
479 * This means restart the syscall if
480 * there is no handler, or the handler
481 * was registered with SA_RESTART.
483 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
490 case -ERESTARTNOINTR
:
492 * This means that the syscall should
493 * be called again after the signal
496 RESTART_CRIS_SYS(regs
);
501 /* Set up the stack frame. */
502 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
503 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
505 ret
= setup_frame(sig
, ka
, oldset
, regs
);
507 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
508 ka
->sa
.sa_handler
= SIG_DFL
;
511 block_sigmask(ka
, sig
);
517 * Note that 'init' is a special process: it doesn't get signals it doesn't
518 * want to handle. Thus you cannot kill init even with a SIGKILL even by
521 * Also note that the regs structure given here as an argument, is the latest
522 * pushed pt_regs. It may or may not be the same as the first pushed registers
523 * when the initial usermode->kernelmode transition took place. Therefore
524 * we can use user_mode(regs) to see if we came directly from kernel or user
528 do_signal(int canrestart
, struct pt_regs
*regs
)
532 struct k_sigaction ka
;
536 * The common case should go fast, which is why this point is
537 * reached from kernel-mode. If that's the case, just return
538 * without doing anything.
540 if (!user_mode(regs
))
543 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
544 oldset
= ¤t
->saved_sigmask
;
546 oldset
= ¤t
->blocked
;
548 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
551 /* Whee! Actually deliver the signal. */
552 if (handle_signal(canrestart
, signr
, &info
, &ka
,
554 /* a signal was successfully delivered; the saved
555 * sigmask will have been stored in the signal frame,
556 * and will be restored by sigreturn, so we can simply
557 * clear the TIF_RESTORE_SIGMASK flag */
558 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
559 clear_thread_flag(TIF_RESTORE_SIGMASK
);
565 /* Got here from a system call? */
567 /* Restart the system call - no handlers present. */
568 if (regs
->r10
== -ERESTARTNOHAND
||
569 regs
->r10
== -ERESTARTSYS
||
570 regs
->r10
== -ERESTARTNOINTR
) {
571 RESTART_CRIS_SYS(regs
);
574 if (regs
->r10
== -ERESTART_RESTARTBLOCK
){
575 regs
->r9
= __NR_restart_syscall
;
580 /* if there's no signal to deliver, we just put the saved sigmask
582 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
583 clear_thread_flag(TIF_RESTORE_SIGMASK
);
584 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
589 ugdb_trap_user(struct thread_info
*ti
, int sig
)
591 if (((user_regs(ti
)->exs
& 0xff00) >> 8) != SINGLE_STEP_INTR_VECT
) {
592 /* Zero single-step PC if the reason we stopped wasn't a single
593 step exception. This is to avoid relying on it when it isn't
595 user_regs(ti
)->spc
= 0;
597 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
598 not within any configured h/w breakpoint range). Synchronize with
599 what already exists for kernel debugging. */
600 if (((user_regs(ti
)->exs
& 0xff00) >> 8) == BREAK_8_INTR_VECT
) {
601 /* Break 8: subtract 2 from ERP unless in a delay slot. */
602 if (!(user_regs(ti
)->erp
& 0x1))
603 user_regs(ti
)->erp
-= 2;
605 sys_kill(ti
->task
->pid
, sig
);
609 keep_debug_flags(unsigned long oldccs
, unsigned long oldspc
,
610 struct pt_regs
*regs
)
612 if (oldccs
& (1 << Q_CCS_BITNR
)) {
613 /* Pending single step due to single-stepping the break 13
614 in the signal trampoline: keep the Q flag. */
615 regs
->ccs
|= (1 << Q_CCS_BITNR
);
616 /* S flag should be set - complain if it's not. */
617 if (!(oldccs
& (1 << (S_CCS_BITNR
+ CCS_SHIFT
)))) {
618 printk("Q flag but no S flag?");
620 regs
->ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
621 /* Assume the SPC is valid and interesting. */
624 } else if (oldccs
& (1 << (S_CCS_BITNR
+ CCS_SHIFT
))) {
625 /* If a h/w bp was set in the signal handler we need
626 to keep the S flag. */
627 regs
->ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
628 /* Don't keep the old SPC though; if we got here due to
629 a single-step, the Q flag should have been set. */
630 } else if (regs
->spc
) {
631 /* If we were single-stepping *before* the signal was taken,
632 we don't want to restore that state now, because GDB will
633 have forgotten all about it. */
635 regs
->ccs
&= ~(1 << (S_CCS_BITNR
+ CCS_SHIFT
));
639 /* Set up the trampolines on the signal return page. */
641 cris_init_signal(void)
643 u16
* data
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
645 /* This is movu.w __NR_sigreturn, r9; break 13; */
647 data
[1] = __NR_sigreturn
;
649 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
651 data
[4] = __NR_rt_sigreturn
;
654 /* Map to userspace with appropriate permissions (no write access...) */
655 cris_signal_return_page
= (unsigned long)
656 __ioremap_prot(virt_to_phys(data
), PAGE_SIZE
, PAGE_SIGNAL_TRAMPOLINE
);
661 __initcall(cris_init_signal
);