1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2003, Axis Communications AB.
6 #include <linux/sched.h>
7 #include <linux/sched/task_stack.h>
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/wait.h>
14 #include <linux/ptrace.h>
15 #include <linux/unistd.h>
16 #include <linux/stddef.h>
17 #include <linux/syscalls.h>
18 #include <linux/vmalloc.h>
21 #include <asm/processor.h>
22 #include <asm/ucontext.h>
23 #include <linux/uaccess.h>
24 #include <arch/hwregs/cpu_vect.h>
26 extern unsigned long cris_signal_return_page
;
29 * A syscall in CRIS is really a "break 13" instruction, which is 2
30 * bytes. The registers is manipulated so upon return the instruction
31 * will be executed again.
33 * This relies on that PC points to the instruction after the break call.
35 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
40 unsigned long extramask
[_NSIG_WORDS
- 1];
41 unsigned char retcode
[8]; /* Trampoline code. */
44 struct rt_signal_frame
{
45 struct siginfo
*pinfo
;
49 unsigned char retcode
[8]; /* Trampoline code. */
52 void do_signal(int restart
, struct pt_regs
*regs
);
53 void keep_debug_flags(unsigned long oldccs
, unsigned long oldspc
,
54 struct pt_regs
*regs
);
57 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
60 unsigned long old_usp
;
62 /* Always make any pending restarted system calls return -EINTR */
63 current
->restart_block
.fn
= do_no_restart_syscall
;
66 * Restore the registers from &sc->regs. sc is already checked
67 * for VERIFY_READ since the signal_frame was previously
68 * checked in sys_sigreturn().
70 if (__copy_from_user(regs
, sc
, sizeof(struct pt_regs
)))
73 /* Make that the user-mode flag is set. */
74 regs
->ccs
|= (1 << (U_CCS_BITNR
+ CCS_SHIFT
));
76 /* Don't perform syscall restarting */
79 /* Restore the old USP. */
80 err
|= __get_user(old_usp
, &sc
->usp
);
89 asmlinkage
int sys_sigreturn(void)
91 struct pt_regs
*regs
= current_pt_regs();
93 struct signal_frame __user
*frame
;
94 unsigned long oldspc
= regs
->spc
;
95 unsigned long oldccs
= regs
->ccs
;
97 frame
= (struct signal_frame
*) rdusp();
100 * Since the signal is stacked on a dword boundary, the frame
101 * should be dword aligned here as well. It it's not, then the
102 * user is trying some funny business.
104 if (((long)frame
) & 3)
107 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
110 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
) ||
111 (_NSIG_WORDS
> 1 && __copy_from_user(&set
.sig
[1],
113 sizeof(frame
->extramask
))))
116 set_current_blocked(&set
);
118 if (restore_sigcontext(regs
, &frame
->sc
))
121 keep_debug_flags(oldccs
, oldspc
, regs
);
126 force_sig(SIGSEGV
, current
);
130 asmlinkage
int sys_rt_sigreturn(void)
132 struct pt_regs
*regs
= current_pt_regs();
134 struct rt_signal_frame __user
*frame
;
135 unsigned long oldspc
= regs
->spc
;
136 unsigned long oldccs
= regs
->ccs
;
138 frame
= (struct rt_signal_frame
*) rdusp();
141 * Since the signal is stacked on a dword boundary, the frame
142 * should be dword aligned here as well. It it's not, then the
143 * user is trying some funny business.
145 if (((long)frame
) & 3)
148 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
151 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
154 set_current_blocked(&set
);
156 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
159 if (restore_altstack(&frame
->uc
.uc_stack
))
162 keep_debug_flags(oldccs
, oldspc
, regs
);
167 force_sig(SIGSEGV
, current
);
171 /* Setup a signal frame. */
173 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
183 * Copy the registers. They are located first in sc, so it's
184 * possible to use sc directly.
186 err
|= __copy_to_user(sc
, regs
, sizeof(struct pt_regs
));
188 err
|= __put_user(mask
, &sc
->oldmask
);
189 err
|= __put_user(usp
, &sc
->usp
);
194 /* Figure out where to put the new signal frame - usually on the stack. */
195 static inline void __user
*
196 get_sigframe(struct ksignal
*ksig
, size_t frame_size
)
198 unsigned long sp
= sigsp(rdusp(), ksig
);
200 /* Make sure the frame is dword-aligned. */
203 return (void __user
*)(sp
- frame_size
);
206 /* Grab and setup a signal frame.
208 * Basically a lot of state-info is stacked, and arranged for the
209 * user-mode program to return to the kernel using either a trampiline
210 * which performs the syscall sigreturn(), or a provided user-mode
214 setup_frame(struct ksignal
*ksig
, sigset_t
*set
, struct pt_regs
*regs
)
217 unsigned long return_ip
;
218 struct signal_frame __user
*frame
;
221 frame
= get_sigframe(ksig
, sizeof(*frame
));
223 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
226 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
231 if (_NSIG_WORDS
> 1) {
232 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
233 sizeof(frame
->extramask
));
240 * Set up to return from user-space. If provided, use a stub
241 * already located in user-space.
243 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
244 return_ip
= (unsigned long)ksig
->ka
.sa
.sa_restorer
;
246 /* Trampoline - the desired return ip is in the signal return page. */
247 return_ip
= cris_signal_return_page
;
250 * This is movu.w __NR_sigreturn, r9; break 13;
252 * WE DO NOT USE IT ANY MORE! It's only left here for historical
253 * reasons and because gdb uses it as a signature to notice
254 * signal handler stack frames.
256 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
257 err
|= __put_user(__NR_sigreturn
, (short __user
*)(frame
->retcode
+2));
258 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
265 * Set up registers for signal handler.
267 * Where the code enters now.
268 * Where the code enter later.
269 * First argument, signo.
271 regs
->erp
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
272 regs
->srp
= return_ip
;
273 regs
->r10
= ksig
->sig
;
275 /* Actually move the USP to reflect the stacked frame. */
276 wrusp((unsigned long)frame
);
282 setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
, struct pt_regs
*regs
)
285 unsigned long return_ip
;
286 struct rt_signal_frame __user
*frame
;
289 frame
= get_sigframe(ksig
, sizeof(*frame
));
291 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
294 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
295 err
|= __put_user(&frame
->uc
, &frame
->puc
);
296 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
301 /* Clear all the bits of the ucontext we don't use. */
302 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
303 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, set
->sig
[0]);
304 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
305 err
|= __save_altstack(&frame
->uc
.uc_stack
, rdusp());
311 * Set up to return from user-space. If provided, use a stub
312 * already located in user-space.
314 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
315 return_ip
= (unsigned long) ksig
->ka
.sa
.sa_restorer
;
317 /* Trampoline - the desired return ip is in the signal return page. */
318 return_ip
= cris_signal_return_page
+ 6;
321 * This is movu.w __NR_rt_sigreturn, r9; break 13;
323 * WE DO NOT USE IT ANY MORE! It's only left here for historical
324 * reasons and because gdb uses it as a signature to notice
325 * signal handler stack frames.
327 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
329 err
|= __put_user(__NR_rt_sigreturn
,
330 (short __user
*)(frame
->retcode
+2));
332 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
339 * Set up registers for signal handler.
341 * Where the code enters now.
342 * Where the code enters later.
343 * First argument is signo.
344 * Second argument is (siginfo_t *).
345 * Third argument is unused.
347 regs
->erp
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
348 regs
->srp
= return_ip
;
349 regs
->r10
= ksig
->sig
;
350 regs
->r11
= (unsigned long) &frame
->info
;
353 /* Actually move the usp to reflect the stacked frame. */
354 wrusp((unsigned long)frame
);
359 /* Invoke a signal handler to, well, handle the signal. */
361 handle_signal(int canrestart
, struct ksignal
*ksig
, struct pt_regs
*regs
)
363 sigset_t
*oldset
= sigmask_to_save();
366 /* Check if this got called from a system call. */
368 /* If so, check system call restarting. */
370 case -ERESTART_RESTARTBLOCK
:
371 case -ERESTARTNOHAND
:
373 * This means that the syscall should
374 * only be restarted if there was no
375 * handler for the signal, and since
376 * this point isn't reached unless
377 * there is a handler, there's no need
385 * This means restart the syscall if
386 * there is no handler, or the handler
387 * was registered with SA_RESTART.
389 if (!(ksig
->ka
.sa
.sa_flags
& SA_RESTART
)) {
396 case -ERESTARTNOINTR
:
398 * This means that the syscall should
399 * be called again after the signal
402 RESTART_CRIS_SYS(regs
);
407 /* Set up the stack frame. */
408 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
409 ret
= setup_rt_frame(ksig
, oldset
, regs
);
411 ret
= setup_frame(ksig
, oldset
, regs
);
413 signal_setup_done(ret
, ksig
, 0);
417 * Note that 'init' is a special process: it doesn't get signals it doesn't
418 * want to handle. Thus you cannot kill init even with a SIGKILL even by
421 * Also note that the regs structure given here as an argument, is the latest
422 * pushed pt_regs. It may or may not be the same as the first pushed registers
423 * when the initial usermode->kernelmode transition took place. Therefore
424 * we can use user_mode(regs) to see if we came directly from kernel or user
428 do_signal(int canrestart
, struct pt_regs
*regs
)
432 canrestart
= canrestart
&& ((int)regs
->exs
>= 0);
435 * The common case should go fast, which is why this point is
436 * reached from kernel-mode. If that's the case, just return
437 * without doing anything.
439 if (!user_mode(regs
))
442 if (get_signal(&ksig
)) {
443 /* Whee! Actually deliver the signal. */
444 handle_signal(canrestart
, &ksig
, regs
);
448 /* Got here from a system call? */
450 /* Restart the system call - no handlers present. */
451 if (regs
->r10
== -ERESTARTNOHAND
||
452 regs
->r10
== -ERESTARTSYS
||
453 regs
->r10
== -ERESTARTNOINTR
) {
454 RESTART_CRIS_SYS(regs
);
457 if (regs
->r10
== -ERESTART_RESTARTBLOCK
){
458 regs
->r9
= __NR_restart_syscall
;
463 /* if there's no signal to deliver, we just put the saved sigmask
465 restore_saved_sigmask();
469 ugdb_trap_user(struct thread_info
*ti
, int sig
)
471 if (((user_regs(ti
)->exs
& 0xff00) >> 8) != SINGLE_STEP_INTR_VECT
) {
472 /* Zero single-step PC if the reason we stopped wasn't a single
473 step exception. This is to avoid relying on it when it isn't
475 user_regs(ti
)->spc
= 0;
477 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
478 not within any configured h/w breakpoint range). Synchronize with
479 what already exists for kernel debugging. */
480 if (((user_regs(ti
)->exs
& 0xff00) >> 8) == BREAK_8_INTR_VECT
) {
481 /* Break 8: subtract 2 from ERP unless in a delay slot. */
482 if (!(user_regs(ti
)->erp
& 0x1))
483 user_regs(ti
)->erp
-= 2;
485 sys_kill(ti
->task
->pid
, sig
);
489 keep_debug_flags(unsigned long oldccs
, unsigned long oldspc
,
490 struct pt_regs
*regs
)
492 if (oldccs
& (1 << Q_CCS_BITNR
)) {
493 /* Pending single step due to single-stepping the break 13
494 in the signal trampoline: keep the Q flag. */
495 regs
->ccs
|= (1 << Q_CCS_BITNR
);
496 /* S flag should be set - complain if it's not. */
497 if (!(oldccs
& (1 << (S_CCS_BITNR
+ CCS_SHIFT
)))) {
498 printk("Q flag but no S flag?");
500 regs
->ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
501 /* Assume the SPC is valid and interesting. */
504 } else if (oldccs
& (1 << (S_CCS_BITNR
+ CCS_SHIFT
))) {
505 /* If a h/w bp was set in the signal handler we need
506 to keep the S flag. */
507 regs
->ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
508 /* Don't keep the old SPC though; if we got here due to
509 a single-step, the Q flag should have been set. */
510 } else if (regs
->spc
) {
511 /* If we were single-stepping *before* the signal was taken,
512 we don't want to restore that state now, because GDB will
513 have forgotten all about it. */
515 regs
->ccs
&= ~(1 << (S_CCS_BITNR
+ CCS_SHIFT
));
519 /* Set up the trampolines on the signal return page. */
521 cris_init_signal(void)
523 u16
* data
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
525 /* This is movu.w __NR_sigreturn, r9; break 13; */
527 data
[1] = __NR_sigreturn
;
529 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
531 data
[4] = __NR_rt_sigreturn
;
534 /* Map to userspace with appropriate permissions (no write access...) */
535 cris_signal_return_page
= (unsigned long)
536 __ioremap_prot(virt_to_phys(data
), PAGE_SIZE
, PAGE_SIGNAL_TRAMPOLINE
);
541 __initcall(cris_init_signal
);