]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/cris/arch-v32/kernel/signal.c
d8d13beafc02045303b7af6dfd859a17f57c5996
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
;
28 * A syscall in CRIS is really a "break 13" instruction, which is 2
29 * bytes. The registers is manipulated so upon return the instruction
30 * will be executed again.
32 * This relies on that PC points to the instruction after the break call.
34 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
39 unsigned long extramask
[_NSIG_WORDS
- 1];
40 unsigned char retcode
[8]; /* Trampoline code. */
43 struct rt_signal_frame
{
44 struct siginfo
*pinfo
;
48 unsigned char retcode
[8]; /* Trampoline code. */
51 void do_signal(int restart
, struct pt_regs
*regs
);
52 void keep_debug_flags(unsigned long oldccs
, unsigned long oldspc
,
53 struct pt_regs
*regs
);
56 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
59 unsigned long old_usp
;
61 /* Always make any pending restarted system calls return -EINTR */
62 current
->restart_block
.fn
= do_no_restart_syscall
;
65 * Restore the registers from &sc->regs. sc is already checked
66 * for VERIFY_READ since the signal_frame was previously
67 * checked in sys_sigreturn().
69 if (__copy_from_user(regs
, sc
, sizeof(struct pt_regs
)))
72 /* Make that the user-mode flag is set. */
73 regs
->ccs
|= (1 << (U_CCS_BITNR
+ CCS_SHIFT
));
75 /* Don't perform syscall restarting */
78 /* Restore the old USP. */
79 err
|= __get_user(old_usp
, &sc
->usp
);
88 asmlinkage
int sys_sigreturn(void)
90 struct pt_regs
*regs
= current_pt_regs();
92 struct signal_frame __user
*frame
;
93 unsigned long oldspc
= regs
->spc
;
94 unsigned long oldccs
= regs
->ccs
;
96 frame
= (struct signal_frame
*) rdusp();
99 * Since the signal is stacked on a dword boundary, the frame
100 * should be dword aligned here as well. It it's not, then the
101 * user is trying some funny business.
103 if (((long)frame
) & 3)
106 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
109 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
) ||
110 (_NSIG_WORDS
> 1 && __copy_from_user(&set
.sig
[1],
112 sizeof(frame
->extramask
))))
115 set_current_blocked(&set
);
117 if (restore_sigcontext(regs
, &frame
->sc
))
120 keep_debug_flags(oldccs
, oldspc
, regs
);
125 force_sig(SIGSEGV
, current
);
129 asmlinkage
int sys_rt_sigreturn(void)
131 struct pt_regs
*regs
= current_pt_regs();
133 struct rt_signal_frame __user
*frame
;
134 unsigned long oldspc
= regs
->spc
;
135 unsigned long oldccs
= regs
->ccs
;
137 frame
= (struct rt_signal_frame
*) rdusp();
140 * Since the signal is stacked on a dword boundary, the frame
141 * should be dword aligned here as well. It it's not, then the
142 * user is trying some funny business.
144 if (((long)frame
) & 3)
147 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
150 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
153 set_current_blocked(&set
);
155 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
158 if (restore_altstack(&frame
->uc
.uc_stack
))
161 keep_debug_flags(oldccs
, oldspc
, regs
);
166 force_sig(SIGSEGV
, current
);
170 /* Setup a signal frame. */
172 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
182 * Copy the registers. They are located first in sc, so it's
183 * possible to use sc directly.
185 err
|= __copy_to_user(sc
, regs
, sizeof(struct pt_regs
));
187 err
|= __put_user(mask
, &sc
->oldmask
);
188 err
|= __put_user(usp
, &sc
->usp
);
193 /* Figure out where to put the new signal frame - usually on the stack. */
194 static inline void __user
*
195 get_sigframe(struct ksignal
*ksig
, size_t frame_size
)
197 unsigned long sp
= sigsp(rdusp(), ksig
);
199 /* Make sure the frame is dword-aligned. */
202 return (void __user
*)(sp
- frame_size
);
205 /* Grab and setup a signal frame.
207 * Basically a lot of state-info is stacked, and arranged for the
208 * user-mode program to return to the kernel using either a trampiline
209 * which performs the syscall sigreturn(), or a provided user-mode
213 setup_frame(struct ksignal
*ksig
, sigset_t
*set
, struct pt_regs
*regs
)
216 unsigned long return_ip
;
217 struct signal_frame __user
*frame
;
220 frame
= get_sigframe(ksig
, sizeof(*frame
));
222 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
225 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
230 if (_NSIG_WORDS
> 1) {
231 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
232 sizeof(frame
->extramask
));
239 * Set up to return from user-space. If provided, use a stub
240 * already located in user-space.
242 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
243 return_ip
= (unsigned long)ksig
->ka
.sa
.sa_restorer
;
245 /* Trampoline - the desired return ip is in the signal return page. */
246 return_ip
= cris_signal_return_page
;
249 * This is movu.w __NR_sigreturn, r9; break 13;
251 * WE DO NOT USE IT ANY MORE! It's only left here for historical
252 * reasons and because gdb uses it as a signature to notice
253 * signal handler stack frames.
255 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
256 err
|= __put_user(__NR_sigreturn
, (short __user
*)(frame
->retcode
+2));
257 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
264 * Set up registers for signal handler.
266 * Where the code enters now.
267 * Where the code enter later.
268 * First argument, signo.
270 regs
->erp
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
271 regs
->srp
= return_ip
;
272 regs
->r10
= ksig
->sig
;
274 /* Actually move the USP to reflect the stacked frame. */
275 wrusp((unsigned long)frame
);
281 setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
, struct pt_regs
*regs
)
284 unsigned long return_ip
;
285 struct rt_signal_frame __user
*frame
;
288 frame
= get_sigframe(ksig
, sizeof(*frame
));
290 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
293 /* TODO: what is the current->exec_domain stuff and invmap ? */
295 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
296 err
|= __put_user(&frame
->uc
, &frame
->puc
);
297 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
302 /* Clear all the bits of the ucontext we don't use. */
303 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
304 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, set
->sig
[0]);
305 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
306 err
|= __save_altstack(&frame
->uc
.uc_stack
, rdusp());
312 * Set up to return from user-space. If provided, use a stub
313 * already located in user-space.
315 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
316 return_ip
= (unsigned long) ksig
->ka
.sa
.sa_restorer
;
318 /* Trampoline - the desired return ip is in the signal return page. */
319 return_ip
= cris_signal_return_page
+ 6;
322 * This is movu.w __NR_rt_sigreturn, r9; break 13;
324 * WE DO NOT USE IT ANY MORE! It's only left here for historical
325 * reasons and because gdb uses it as a signature to notice
326 * signal handler stack frames.
328 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
330 err
|= __put_user(__NR_rt_sigreturn
,
331 (short __user
*)(frame
->retcode
+2));
333 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
340 * Set up registers for signal handler.
342 * Where the code enters now.
343 * Where the code enters later.
344 * First argument is signo.
345 * Second argument is (siginfo_t *).
346 * Third argument is unused.
348 regs
->erp
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
349 regs
->srp
= return_ip
;
350 regs
->r10
= ksig
->sig
;
351 regs
->r11
= (unsigned long) &frame
->info
;
354 /* Actually move the usp to reflect the stacked frame. */
355 wrusp((unsigned long)frame
);
360 /* Invoke a signal handler to, well, handle the signal. */
362 handle_signal(int canrestart
, struct ksignal
*ksig
, struct pt_regs
*regs
)
364 sigset_t
*oldset
= sigmask_to_save();
367 /* Check if this got called from a system call. */
369 /* If so, check system call restarting. */
371 case -ERESTART_RESTARTBLOCK
:
372 case -ERESTARTNOHAND
:
374 * This means that the syscall should
375 * only be restarted if there was no
376 * handler for the signal, and since
377 * this point isn't reached unless
378 * there is a handler, there's no need
386 * This means restart the syscall if
387 * there is no handler, or the handler
388 * was registered with SA_RESTART.
390 if (!(ksig
->ka
.sa
.sa_flags
& SA_RESTART
)) {
397 case -ERESTARTNOINTR
:
399 * This means that the syscall should
400 * be called again after the signal
403 RESTART_CRIS_SYS(regs
);
408 /* Set up the stack frame. */
409 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
410 ret
= setup_rt_frame(ksig
, oldset
, regs
);
412 ret
= setup_frame(ksig
, oldset
, regs
);
414 signal_setup_done(ret
, ksig
, 0);
418 * Note that 'init' is a special process: it doesn't get signals it doesn't
419 * want to handle. Thus you cannot kill init even with a SIGKILL even by
422 * Also note that the regs structure given here as an argument, is the latest
423 * pushed pt_regs. It may or may not be the same as the first pushed registers
424 * when the initial usermode->kernelmode transition took place. Therefore
425 * we can use user_mode(regs) to see if we came directly from kernel or user
429 do_signal(int canrestart
, struct pt_regs
*regs
)
433 canrestart
= canrestart
&& ((int)regs
->exs
>= 0);
436 * The common case should go fast, which is why this point is
437 * reached from kernel-mode. If that's the case, just return
438 * without doing anything.
440 if (!user_mode(regs
))
443 if (get_signal(&ksig
)) {
444 /* Whee! Actually deliver the signal. */
445 handle_signal(canrestart
, &ksig
, regs
);
449 /* Got here from a system call? */
451 /* Restart the system call - no handlers present. */
452 if (regs
->r10
== -ERESTARTNOHAND
||
453 regs
->r10
== -ERESTARTSYS
||
454 regs
->r10
== -ERESTARTNOINTR
) {
455 RESTART_CRIS_SYS(regs
);
458 if (regs
->r10
== -ERESTART_RESTARTBLOCK
){
459 regs
->r9
= __NR_restart_syscall
;
464 /* if there's no signal to deliver, we just put the saved sigmask
466 restore_saved_sigmask();
470 ugdb_trap_user(struct thread_info
*ti
, int sig
)
472 if (((user_regs(ti
)->exs
& 0xff00) >> 8) != SINGLE_STEP_INTR_VECT
) {
473 /* Zero single-step PC if the reason we stopped wasn't a single
474 step exception. This is to avoid relying on it when it isn't
476 user_regs(ti
)->spc
= 0;
478 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
479 not within any configured h/w breakpoint range). Synchronize with
480 what already exists for kernel debugging. */
481 if (((user_regs(ti
)->exs
& 0xff00) >> 8) == BREAK_8_INTR_VECT
) {
482 /* Break 8: subtract 2 from ERP unless in a delay slot. */
483 if (!(user_regs(ti
)->erp
& 0x1))
484 user_regs(ti
)->erp
-= 2;
486 sys_kill(ti
->task
->pid
, sig
);
490 keep_debug_flags(unsigned long oldccs
, unsigned long oldspc
,
491 struct pt_regs
*regs
)
493 if (oldccs
& (1 << Q_CCS_BITNR
)) {
494 /* Pending single step due to single-stepping the break 13
495 in the signal trampoline: keep the Q flag. */
496 regs
->ccs
|= (1 << Q_CCS_BITNR
);
497 /* S flag should be set - complain if it's not. */
498 if (!(oldccs
& (1 << (S_CCS_BITNR
+ CCS_SHIFT
)))) {
499 printk("Q flag but no S flag?");
501 regs
->ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
502 /* Assume the SPC is valid and interesting. */
505 } else if (oldccs
& (1 << (S_CCS_BITNR
+ CCS_SHIFT
))) {
506 /* If a h/w bp was set in the signal handler we need
507 to keep the S flag. */
508 regs
->ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
509 /* Don't keep the old SPC though; if we got here due to
510 a single-step, the Q flag should have been set. */
511 } else if (regs
->spc
) {
512 /* If we were single-stepping *before* the signal was taken,
513 we don't want to restore that state now, because GDB will
514 have forgotten all about it. */
516 regs
->ccs
&= ~(1 << (S_CCS_BITNR
+ CCS_SHIFT
));
520 /* Set up the trampolines on the signal return page. */
522 cris_init_signal(void)
524 u16
* data
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
526 /* This is movu.w __NR_sigreturn, r9; break 13; */
528 data
[1] = __NR_sigreturn
;
530 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
532 data
[4] = __NR_rt_sigreturn
;
535 /* Map to userspace with appropriate permissions (no write access...) */
536 cris_signal_return_page
= (unsigned long)
537 __ioremap_prot(virt_to_phys(data
), PAGE_SIZE
, PAGE_SIGNAL_TRAMPOLINE
);
542 __initcall(cris_init_signal
);