Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / cris / arch-v32 / kernel / signal.c
blob4f2e3ba3bf40c63457c7aa49f62593ec71988174
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2003, Axis Communications AB.
4 */
6 #include <linux/sched.h>
7 #include <linux/sched/task_stack.h>
8 #include <linux/mm.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>
20 #include <asm/io.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;
37 /* Signal frames. */
38 struct signal_frame {
39 struct sigcontext sc;
40 unsigned long extramask[_NSIG_WORDS - 1];
41 unsigned char retcode[8]; /* Trampoline code. */
44 struct rt_signal_frame {
45 struct siginfo *pinfo;
46 void *puc;
47 struct siginfo info;
48 struct ucontext uc;
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);
56 static int
57 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
59 unsigned int err = 0;
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)))
71 goto badframe;
73 /* Make that the user-mode flag is set. */
74 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
76 /* Don't perform syscall restarting */
77 regs->exs = -1;
79 /* Restore the old USP. */
80 err |= __get_user(old_usp, &sc->usp);
81 wrusp(old_usp);
83 return err;
85 badframe:
86 return 1;
89 asmlinkage int sys_sigreturn(void)
91 struct pt_regs *regs = current_pt_regs();
92 sigset_t set;
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)
105 goto badframe;
107 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
108 goto badframe;
110 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
111 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
112 frame->extramask,
113 sizeof(frame->extramask))))
114 goto badframe;
116 set_current_blocked(&set);
118 if (restore_sigcontext(regs, &frame->sc))
119 goto badframe;
121 keep_debug_flags(oldccs, oldspc, regs);
123 return regs->r10;
125 badframe:
126 force_sig(SIGSEGV, current);
127 return 0;
130 asmlinkage int sys_rt_sigreturn(void)
132 struct pt_regs *regs = current_pt_regs();
133 sigset_t set;
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)
146 goto badframe;
148 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
149 goto badframe;
151 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
152 goto badframe;
154 set_current_blocked(&set);
156 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
157 goto badframe;
159 if (restore_altstack(&frame->uc.uc_stack))
160 goto badframe;
162 keep_debug_flags(oldccs, oldspc, regs);
164 return regs->r10;
166 badframe:
167 force_sig(SIGSEGV, current);
168 return 0;
171 /* Setup a signal frame. */
172 static int
173 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
174 unsigned long mask)
176 int err;
177 unsigned long usp;
179 err = 0;
180 usp = rdusp();
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);
191 return err;
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. */
201 sp &= ~3;
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
211 * trampoline.
213 static int
214 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
216 int err;
217 unsigned long return_ip;
218 struct signal_frame __user *frame;
220 err = 0;
221 frame = get_sigframe(ksig, sizeof(*frame));
223 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
224 return -EFAULT;
226 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
228 if (err)
229 return -EFAULT;
231 if (_NSIG_WORDS > 1) {
232 err |= __copy_to_user(frame->extramask, &set->sig[1],
233 sizeof(frame->extramask));
236 if (err)
237 return -EFAULT;
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;
245 } else {
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));
261 if (err)
262 return -EFAULT;
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);
278 return 0;
281 static int
282 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
284 int err;
285 unsigned long return_ip;
286 struct rt_signal_frame __user *frame;
288 err = 0;
289 frame = get_sigframe(ksig, sizeof(*frame));
291 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
292 return -EFAULT;
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);
298 if (err)
299 return -EFAULT;
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());
307 if (err)
308 return -EFAULT;
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;
316 } else {
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));
335 if (err)
336 return -EFAULT;
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;
351 regs->r12 = 0;
353 /* Actually move the usp to reflect the stacked frame. */
354 wrusp((unsigned long)frame);
356 return 0;
359 /* Invoke a signal handler to, well, handle the signal. */
360 static inline void
361 handle_signal(int canrestart, struct ksignal *ksig, struct pt_regs *regs)
363 sigset_t *oldset = sigmask_to_save();
364 int ret;
366 /* Check if this got called from a system call. */
367 if (canrestart) {
368 /* If so, check system call restarting. */
369 switch (regs->r10) {
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
378 * to restart.
380 regs->r10 = -EINTR;
381 break;
383 case -ERESTARTSYS:
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)) {
390 regs->r10 = -EINTR;
391 break;
394 /* Fall through. */
396 case -ERESTARTNOINTR:
398 * This means that the syscall should
399 * be called again after the signal
400 * handler returns.
402 RESTART_CRIS_SYS(regs);
403 break;
407 /* Set up the stack frame. */
408 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
409 ret = setup_rt_frame(ksig, oldset, regs);
410 else
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
419 * mistake.
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
425 * mode below.
427 void
428 do_signal(int canrestart, struct pt_regs *regs)
430 struct ksignal ksig;
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))
440 return;
442 if (get_signal(&ksig)) {
443 /* Whee! Actually deliver the signal. */
444 handle_signal(canrestart, &ksig, regs);
445 return;
448 /* Got here from a system call? */
449 if (canrestart) {
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;
459 regs->erp -= 2;
463 /* if there's no signal to deliver, we just put the saved sigmask
464 * back */
465 restore_saved_sigmask();
468 asmlinkage void
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
474 reliable. */
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);
488 void
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. */
502 regs->spc = oldspc;
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. */
514 regs->spc = 0;
515 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
519 /* Set up the trampolines on the signal return page. */
520 int __init
521 cris_init_signal(void)
523 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
525 /* This is movu.w __NR_sigreturn, r9; break 13; */
526 data[0] = 0x9c5f;
527 data[1] = __NR_sigreturn;
528 data[2] = 0xe93d;
529 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
530 data[3] = 0x9c5f;
531 data[4] = __NR_rt_sigreturn;
532 data[5] = 0xe93d;
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);
538 return 0;
541 __initcall(cris_init_signal);