Staging: Panel: panel: Fixed checkpatch line length warnings
[linux/fpc-iii.git] / arch / cris / arch-v32 / kernel / signal.c
blob01d1375c9004cdaa89dc867c5b00c303ac618ff5
1 /*
2 * Copyright (C) 2003, Axis Communications AB.
3 */
5 #include <linux/sched.h>
6 #include <linux/mm.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>
18 #include <asm/io.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;
36 /* Signal frames. */
37 struct signal_frame {
38 struct sigcontext sc;
39 unsigned long extramask[_NSIG_WORDS - 1];
40 unsigned char retcode[8]; /* Trampoline code. */
43 struct rt_signal_frame {
44 struct siginfo *pinfo;
45 void *puc;
46 struct siginfo info;
47 struct ucontext uc;
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);
55 static int
56 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
58 unsigned int err = 0;
59 unsigned long old_usp;
61 /* Always make any pending restarted system calls return -EINTR */
62 current_thread_info()->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)))
70 goto badframe;
72 /* Make that the user-mode flag is set. */
73 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
75 /* Restore the old USP. */
76 err |= __get_user(old_usp, &sc->usp);
77 wrusp(old_usp);
79 return err;
81 badframe:
82 return 1;
85 asmlinkage int sys_sigreturn(void)
87 struct pt_regs *regs = current_pt_regs();
88 sigset_t set;
89 struct signal_frame __user *frame;
90 unsigned long oldspc = regs->spc;
91 unsigned long oldccs = regs->ccs;
93 frame = (struct signal_frame *) rdusp();
96 * Since the signal is stacked on a dword boundary, the frame
97 * should be dword aligned here as well. It it's not, then the
98 * user is trying some funny business.
100 if (((long)frame) & 3)
101 goto badframe;
103 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
104 goto badframe;
106 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
107 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
108 frame->extramask,
109 sizeof(frame->extramask))))
110 goto badframe;
112 set_current_blocked(&set);
114 if (restore_sigcontext(regs, &frame->sc))
115 goto badframe;
117 keep_debug_flags(oldccs, oldspc, regs);
119 return regs->r10;
121 badframe:
122 force_sig(SIGSEGV, current);
123 return 0;
126 asmlinkage int sys_rt_sigreturn(void)
128 struct pt_regs *regs = current_pt_regs();
129 sigset_t set;
130 struct rt_signal_frame __user *frame;
131 unsigned long oldspc = regs->spc;
132 unsigned long oldccs = regs->ccs;
134 frame = (struct rt_signal_frame *) rdusp();
137 * Since the signal is stacked on a dword boundary, the frame
138 * should be dword aligned here as well. It it's not, then the
139 * user is trying some funny business.
141 if (((long)frame) & 3)
142 goto badframe;
144 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
145 goto badframe;
147 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
148 goto badframe;
150 set_current_blocked(&set);
152 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
153 goto badframe;
155 if (restore_altstack(&frame->uc.uc_stack))
156 goto badframe;
158 keep_debug_flags(oldccs, oldspc, regs);
160 return regs->r10;
162 badframe:
163 force_sig(SIGSEGV, current);
164 return 0;
167 /* Setup a signal frame. */
168 static int
169 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
170 unsigned long mask)
172 int err;
173 unsigned long usp;
175 err = 0;
176 usp = rdusp();
179 * Copy the registers. They are located first in sc, so it's
180 * possible to use sc directly.
182 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
184 err |= __put_user(mask, &sc->oldmask);
185 err |= __put_user(usp, &sc->usp);
187 return err;
190 /* Figure out where to put the new signal frame - usually on the stack. */
191 static inline void __user *
192 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
194 unsigned long sp;
196 sp = rdusp();
198 /* This is the X/Open sanctioned signal stack switching. */
199 if (ka->sa.sa_flags & SA_ONSTACK) {
200 if (!on_sig_stack(sp))
201 sp = current->sas_ss_sp + current->sas_ss_size;
204 /* Make sure the frame is dword-aligned. */
205 sp &= ~3;
207 return (void __user *)(sp - frame_size);
210 /* Grab and setup a signal frame.
212 * Basically a lot of state-info is stacked, and arranged for the
213 * user-mode program to return to the kernel using either a trampiline
214 * which performs the syscall sigreturn(), or a provided user-mode
215 * trampoline.
217 static int
218 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
219 struct pt_regs * regs)
221 int err;
222 unsigned long return_ip;
223 struct signal_frame __user *frame;
225 err = 0;
226 frame = get_sigframe(ka, regs, sizeof(*frame));
228 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
229 goto give_sigsegv;
231 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
233 if (err)
234 goto give_sigsegv;
236 if (_NSIG_WORDS > 1) {
237 err |= __copy_to_user(frame->extramask, &set->sig[1],
238 sizeof(frame->extramask));
241 if (err)
242 goto give_sigsegv;
245 * Set up to return from user-space. If provided, use a stub
246 * already located in user-space.
248 if (ka->sa.sa_flags & SA_RESTORER) {
249 return_ip = (unsigned long)ka->sa.sa_restorer;
250 } else {
251 /* Trampoline - the desired return ip is in the signal return page. */
252 return_ip = cris_signal_return_page;
255 * This is movu.w __NR_sigreturn, r9; break 13;
257 * WE DO NOT USE IT ANY MORE! It's only left here for historical
258 * reasons and because gdb uses it as a signature to notice
259 * signal handler stack frames.
261 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
262 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
263 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
266 if (err)
267 goto give_sigsegv;
270 * Set up registers for signal handler.
272 * Where the code enters now.
273 * Where the code enter later.
274 * First argument, signo.
276 regs->erp = (unsigned long) ka->sa.sa_handler;
277 regs->srp = return_ip;
278 regs->r10 = sig;
280 /* Actually move the USP to reflect the stacked frame. */
281 wrusp((unsigned long)frame);
283 return 0;
285 give_sigsegv:
286 force_sigsegv(sig, current);
287 return -EFAULT;
290 static int
291 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
292 sigset_t *set, struct pt_regs * regs)
294 int err;
295 unsigned long return_ip;
296 struct rt_signal_frame __user *frame;
298 err = 0;
299 frame = get_sigframe(ka, regs, sizeof(*frame));
301 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
302 goto give_sigsegv;
304 /* TODO: what is the current->exec_domain stuff and invmap ? */
306 err |= __put_user(&frame->info, &frame->pinfo);
307 err |= __put_user(&frame->uc, &frame->puc);
308 err |= copy_siginfo_to_user(&frame->info, info);
310 if (err)
311 goto give_sigsegv;
313 /* Clear all the bits of the ucontext we don't use. */
314 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
315 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
316 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
317 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
319 if (err)
320 goto give_sigsegv;
323 * Set up to return from user-space. If provided, use a stub
324 * already located in user-space.
326 if (ka->sa.sa_flags & SA_RESTORER) {
327 return_ip = (unsigned long) ka->sa.sa_restorer;
328 } else {
329 /* Trampoline - the desired return ip is in the signal return page. */
330 return_ip = cris_signal_return_page + 6;
333 * This is movu.w __NR_rt_sigreturn, r9; break 13;
335 * WE DO NOT USE IT ANY MORE! It's only left here for historical
336 * reasons and because gdb uses it as a signature to notice
337 * signal handler stack frames.
339 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
341 err |= __put_user(__NR_rt_sigreturn,
342 (short __user*)(frame->retcode+2));
344 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
347 if (err)
348 goto give_sigsegv;
351 * Set up registers for signal handler.
353 * Where the code enters now.
354 * Where the code enters later.
355 * First argument is signo.
356 * Second argument is (siginfo_t *).
357 * Third argument is unused.
359 regs->erp = (unsigned long) ka->sa.sa_handler;
360 regs->srp = return_ip;
361 regs->r10 = sig;
362 regs->r11 = (unsigned long) &frame->info;
363 regs->r12 = 0;
365 /* Actually move the usp to reflect the stacked frame. */
366 wrusp((unsigned long)frame);
368 return 0;
370 give_sigsegv:
371 force_sigsegv(sig, current);
372 return -EFAULT;
375 /* Invoke a signal handler to, well, handle the signal. */
376 static inline void
377 handle_signal(int canrestart, unsigned long sig,
378 siginfo_t *info, struct k_sigaction *ka,
379 struct pt_regs * regs)
381 sigset_t *oldset = sigmask_to_save();
382 int ret;
384 /* Check if this got called from a system call. */
385 if (canrestart) {
386 /* If so, check system call restarting. */
387 switch (regs->r10) {
388 case -ERESTART_RESTARTBLOCK:
389 case -ERESTARTNOHAND:
391 * This means that the syscall should
392 * only be restarted if there was no
393 * handler for the signal, and since
394 * this point isn't reached unless
395 * there is a handler, there's no need
396 * to restart.
398 regs->r10 = -EINTR;
399 break;
401 case -ERESTARTSYS:
403 * This means restart the syscall if
404 * there is no handler, or the handler
405 * was registered with SA_RESTART.
407 if (!(ka->sa.sa_flags & SA_RESTART)) {
408 regs->r10 = -EINTR;
409 break;
412 /* Fall through. */
414 case -ERESTARTNOINTR:
416 * This means that the syscall should
417 * be called again after the signal
418 * handler returns.
420 RESTART_CRIS_SYS(regs);
421 break;
425 /* Set up the stack frame. */
426 if (ka->sa.sa_flags & SA_SIGINFO)
427 ret = setup_rt_frame(sig, ka, info, oldset, regs);
428 else
429 ret = setup_frame(sig, ka, oldset, regs);
431 if (ret == 0)
432 signal_delivered(sig, info, ka, regs, 0);
436 * Note that 'init' is a special process: it doesn't get signals it doesn't
437 * want to handle. Thus you cannot kill init even with a SIGKILL even by
438 * mistake.
440 * Also note that the regs structure given here as an argument, is the latest
441 * pushed pt_regs. It may or may not be the same as the first pushed registers
442 * when the initial usermode->kernelmode transition took place. Therefore
443 * we can use user_mode(regs) to see if we came directly from kernel or user
444 * mode below.
446 void
447 do_signal(int canrestart, struct pt_regs *regs)
449 int signr;
450 siginfo_t info;
451 struct k_sigaction ka;
454 * The common case should go fast, which is why this point is
455 * reached from kernel-mode. If that's the case, just return
456 * without doing anything.
458 if (!user_mode(regs))
459 return;
461 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
463 if (signr > 0) {
464 /* Whee! Actually deliver the signal. */
465 handle_signal(canrestart, signr, &info, &ka, regs);
466 return;
469 /* Got here from a system call? */
470 if (canrestart) {
471 /* Restart the system call - no handlers present. */
472 if (regs->r10 == -ERESTARTNOHAND ||
473 regs->r10 == -ERESTARTSYS ||
474 regs->r10 == -ERESTARTNOINTR) {
475 RESTART_CRIS_SYS(regs);
478 if (regs->r10 == -ERESTART_RESTARTBLOCK){
479 regs->r9 = __NR_restart_syscall;
480 regs->erp -= 2;
484 /* if there's no signal to deliver, we just put the saved sigmask
485 * back */
486 restore_saved_sigmask();
489 asmlinkage void
490 ugdb_trap_user(struct thread_info *ti, int sig)
492 if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
493 /* Zero single-step PC if the reason we stopped wasn't a single
494 step exception. This is to avoid relying on it when it isn't
495 reliable. */
496 user_regs(ti)->spc = 0;
498 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
499 not within any configured h/w breakpoint range). Synchronize with
500 what already exists for kernel debugging. */
501 if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
502 /* Break 8: subtract 2 from ERP unless in a delay slot. */
503 if (!(user_regs(ti)->erp & 0x1))
504 user_regs(ti)->erp -= 2;
506 sys_kill(ti->task->pid, sig);
509 void
510 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
511 struct pt_regs *regs)
513 if (oldccs & (1 << Q_CCS_BITNR)) {
514 /* Pending single step due to single-stepping the break 13
515 in the signal trampoline: keep the Q flag. */
516 regs->ccs |= (1 << Q_CCS_BITNR);
517 /* S flag should be set - complain if it's not. */
518 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
519 printk("Q flag but no S flag?");
521 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
522 /* Assume the SPC is valid and interesting. */
523 regs->spc = oldspc;
525 } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
526 /* If a h/w bp was set in the signal handler we need
527 to keep the S flag. */
528 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
529 /* Don't keep the old SPC though; if we got here due to
530 a single-step, the Q flag should have been set. */
531 } else if (regs->spc) {
532 /* If we were single-stepping *before* the signal was taken,
533 we don't want to restore that state now, because GDB will
534 have forgotten all about it. */
535 regs->spc = 0;
536 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
540 /* Set up the trampolines on the signal return page. */
541 int __init
542 cris_init_signal(void)
544 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
546 /* This is movu.w __NR_sigreturn, r9; break 13; */
547 data[0] = 0x9c5f;
548 data[1] = __NR_sigreturn;
549 data[2] = 0xe93d;
550 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
551 data[3] = 0x9c5f;
552 data[4] = __NR_rt_sigreturn;
553 data[5] = 0xe93d;
555 /* Map to userspace with appropriate permissions (no write access...) */
556 cris_signal_return_page = (unsigned long)
557 __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
559 return 0;
562 __initcall(cris_init_signal);