]> Pileus Git - ~andy/linux/blob - arch/cris/arch-v32/kernel/signal.c
Merge branch 'akpm' (incoming from Andrew)
[~andy/linux] / arch / cris / arch-v32 / kernel / signal.c
1 /*
2  * Copyright (C) 2003, Axis Communications AB.
3  */
4
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>
17
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>
24
25 extern unsigned long cris_signal_return_page;
26
27 /*
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.
31  *
32  * This relies on that PC points to the instruction after the break call.
33  */
34 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
35
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. */
41 };
42
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. */
49 };
50
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);
54 /*
55  * Swap in the new signal mask, and wait for a signal. Define some
56  * dummy arguments to be able to reach the regs argument.
57  */
58 int
59 sys_sigsuspend(old_sigset_t mask)
60 {
61         sigset_t blocked;
62         siginitset(&blocked, mask);
63         return sigsuspend(&blocked);
64 }
65
66 int
67 sys_sigaction(int signal, const struct old_sigaction *act,
68               struct old_sigaction *oact)
69 {
70         int retval;
71         struct k_sigaction newk;
72         struct k_sigaction oldk;
73
74         if (act) {
75                 old_sigset_t mask;
76
77                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
78                     __get_user(newk.sa.sa_handler, &act->sa_handler) ||
79                     __get_user(newk.sa.sa_restorer, &act->sa_restorer) ||
80                     __get_user(newk.sa.sa_flags, &act->sa_flags) ||
81                     __get_user(mask, &act->sa_mask))
82                         return -EFAULT;
83
84                 siginitset(&newk.sa.sa_mask, mask);
85         }
86
87         retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
88
89         if (!retval && oact) {
90                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
91                     __put_user(oldk.sa.sa_handler, &oact->sa_handler) ||
92                     __put_user(oldk.sa.sa_restorer, &oact->sa_restorer) ||
93                     __put_user(oldk.sa.sa_flags, &oact->sa_flags) ||
94                     __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask))
95                         return -EFAULT;
96
97         }
98
99         return retval;
100 }
101
102 int
103 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
104 {
105         return do_sigaltstack(uss, uoss, rdusp());
106 }
107
108 static int
109 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
110 {
111         unsigned int err = 0;
112         unsigned long old_usp;
113
114         /* Always make any pending restarted system calls return -EINTR */
115         current_thread_info()->restart_block.fn = do_no_restart_syscall;
116
117         /*
118          * Restore the registers from &sc->regs. sc is already checked
119          * for VERIFY_READ since the signal_frame was previously
120          * checked in sys_sigreturn().
121          */
122         if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
123                 goto badframe;
124
125         /* Make that the user-mode flag is set. */
126         regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
127
128         /* Restore the old USP. */
129         err |= __get_user(old_usp, &sc->usp);
130         wrusp(old_usp);
131
132         return err;
133
134 badframe:
135         return 1;
136 }
137
138 /* Define some dummy arguments to be able to reach the regs argument. */
139 asmlinkage int
140 sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
141               struct pt_regs *regs)
142 {
143         sigset_t set;
144         struct signal_frame __user *frame;
145         unsigned long oldspc = regs->spc;
146         unsigned long oldccs = regs->ccs;
147
148         frame = (struct signal_frame *) rdusp();
149
150         /*
151          * Since the signal is stacked on a dword boundary, the frame
152          * should be dword aligned here as well. It it's not, then the
153          * user is trying some funny business.
154          */
155         if (((long)frame) & 3)
156                 goto badframe;
157
158         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
159                 goto badframe;
160
161         if (__get_user(set.sig[0], &frame->sc.oldmask) ||
162             (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
163                                                  frame->extramask,
164                                                  sizeof(frame->extramask))))
165                 goto badframe;
166
167         set_current_blocked(&set);
168
169         if (restore_sigcontext(regs, &frame->sc))
170                 goto badframe;
171
172         keep_debug_flags(oldccs, oldspc, regs);
173
174         return regs->r10;
175
176 badframe:
177         force_sig(SIGSEGV, current);
178         return 0;
179 }
180
181 /* Define some dummy variables to be able to reach the regs argument. */
182 asmlinkage int
183 sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
184                  struct pt_regs *regs)
185 {
186         sigset_t set;
187         struct rt_signal_frame __user *frame;
188         unsigned long oldspc = regs->spc;
189         unsigned long oldccs = regs->ccs;
190
191         frame = (struct rt_signal_frame *) rdusp();
192
193         /*
194          * Since the signal is stacked on a dword boundary, the frame
195          * should be dword aligned here as well. It it's not, then the
196          * user is trying some funny business.
197          */
198         if (((long)frame) & 3)
199                 goto badframe;
200
201         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
202                 goto badframe;
203
204         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
205                 goto badframe;
206
207         set_current_blocked(&set);
208
209         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
210                 goto badframe;
211
212         if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
213                 goto badframe;
214
215         keep_debug_flags(oldccs, oldspc, regs);
216
217         return regs->r10;
218
219 badframe:
220         force_sig(SIGSEGV, current);
221         return 0;
222 }
223
224 /* Setup a signal frame. */
225 static int
226 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
227                  unsigned long mask)
228 {
229         int err;
230         unsigned long usp;
231
232         err = 0;
233         usp = rdusp();
234
235         /*
236          * Copy the registers. They are located first in sc, so it's
237          * possible to use sc directly.
238          */
239         err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
240
241         err |= __put_user(mask, &sc->oldmask);
242         err |= __put_user(usp, &sc->usp);
243
244         return err;
245 }
246
247 /* Figure out where to put the new signal frame - usually on the stack. */
248 static inline void __user *
249 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
250 {
251         unsigned long sp;
252
253         sp = rdusp();
254
255         /* This is the X/Open sanctioned signal stack switching. */
256         if (ka->sa.sa_flags & SA_ONSTACK) {
257                 if (!on_sig_stack(sp))
258                         sp = current->sas_ss_sp + current->sas_ss_size;
259         }
260
261         /* Make sure the frame is dword-aligned. */
262         sp &= ~3;
263
264         return (void __user *)(sp - frame_size);
265 }
266
267 /* Grab and setup a signal frame.
268  *
269  * Basically a lot of state-info is stacked, and arranged for the
270  * user-mode program to return to the kernel using either a trampiline
271  * which performs the syscall sigreturn(), or a provided user-mode
272  * trampoline.
273   */
274 static int
275 setup_frame(int sig, struct k_sigaction *ka,  sigset_t *set,
276             struct pt_regs * regs)
277 {
278         int err;
279         unsigned long return_ip;
280         struct signal_frame __user *frame;
281
282         err = 0;
283         frame = get_sigframe(ka, regs, sizeof(*frame));
284
285         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
286                 goto give_sigsegv;
287
288         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
289
290         if (err)
291                 goto give_sigsegv;
292
293         if (_NSIG_WORDS > 1) {
294                 err |= __copy_to_user(frame->extramask, &set->sig[1],
295                                       sizeof(frame->extramask));
296         }
297
298         if (err)
299                 goto give_sigsegv;
300
301         /*
302          * Set up to return from user-space. If provided, use a stub
303          * already located in user-space.
304          */
305         if (ka->sa.sa_flags & SA_RESTORER) {
306                 return_ip = (unsigned long)ka->sa.sa_restorer;
307         } else {
308                 /* Trampoline - the desired return ip is in the signal return page. */
309                 return_ip = cris_signal_return_page;
310
311                 /*
312                  * This is movu.w __NR_sigreturn, r9; break 13;
313                  *
314                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
315                  * reasons and because gdb uses it as a signature to notice
316                  * signal handler stack frames.
317                  */
318                 err |= __put_user(0x9c5f,         (short __user*)(frame->retcode+0));
319                 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
320                 err |= __put_user(0xe93d,         (short __user*)(frame->retcode+4));
321         }
322
323         if (err)
324                 goto give_sigsegv;
325
326         /*
327          * Set up registers for signal handler.
328          *
329          * Where the code enters now.
330          * Where the code enter later.
331          * First argument, signo.
332          */
333         regs->erp = (unsigned long) ka->sa.sa_handler;
334         regs->srp = return_ip;
335         regs->r10 = sig;
336
337         /* Actually move the USP to reflect the stacked frame. */
338         wrusp((unsigned long)frame);
339
340         return 0;
341
342 give_sigsegv:
343         force_sigsegv(sig, current);
344         return -EFAULT;
345 }
346
347 static int
348 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
349                sigset_t *set, struct pt_regs * regs)
350 {
351         int err;
352         unsigned long return_ip;
353         struct rt_signal_frame __user *frame;
354
355         err = 0;
356         frame = get_sigframe(ka, regs, sizeof(*frame));
357
358         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
359                 goto give_sigsegv;
360
361         /* TODO: what is the current->exec_domain stuff and invmap ? */
362
363         err |= __put_user(&frame->info, &frame->pinfo);
364         err |= __put_user(&frame->uc, &frame->puc);
365         err |= copy_siginfo_to_user(&frame->info, info);
366
367         if (err)
368                 goto give_sigsegv;
369
370         /* Clear all the bits of the ucontext we don't use.  */
371         err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
372         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
373         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
374
375         if (err)
376                 goto give_sigsegv;
377
378         /*
379          * Set up to return from user-space. If provided, use a stub
380          * already located in user-space.
381          */
382         if (ka->sa.sa_flags & SA_RESTORER) {
383                 return_ip = (unsigned long) ka->sa.sa_restorer;
384         } else {
385                 /* Trampoline - the desired return ip is in the signal return page. */
386                 return_ip = cris_signal_return_page + 6;
387
388                 /*
389                  * This is movu.w __NR_rt_sigreturn, r9; break 13;
390                  *
391                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
392                  * reasons and because gdb uses it as a signature to notice
393                  * signal handler stack frames.
394                  */
395                 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
396
397                 err |= __put_user(__NR_rt_sigreturn,
398                                   (short __user*)(frame->retcode+2));
399
400                 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
401         }
402
403         if (err)
404                 goto give_sigsegv;
405
406         /*
407          * Set up registers for signal handler.
408          *
409          * Where the code enters now.
410          * Where the code enters later.
411          * First argument is signo.
412          * Second argument is (siginfo_t *).
413          * Third argument is unused.
414          */
415         regs->erp = (unsigned long) ka->sa.sa_handler;
416         regs->srp = return_ip;
417         regs->r10 = sig;
418         regs->r11 = (unsigned long) &frame->info;
419         regs->r12 = 0;
420
421         /* Actually move the usp to reflect the stacked frame. */
422         wrusp((unsigned long)frame);
423
424         return 0;
425
426 give_sigsegv:
427         force_sigsegv(sig, current);
428         return -EFAULT;
429 }
430
431 /* Invoke a signal handler to, well, handle the signal. */
432 static inline void
433 handle_signal(int canrestart, unsigned long sig,
434               siginfo_t *info, struct k_sigaction *ka,
435               struct pt_regs * regs)
436 {
437         sigset_t *oldset = sigmask_to_save();
438         int ret;
439
440         /* Check if this got called from a system call. */
441         if (canrestart) {
442                 /* If so, check system call restarting. */
443                 switch (regs->r10) {
444                         case -ERESTART_RESTARTBLOCK:
445                         case -ERESTARTNOHAND:
446                                 /*
447                                  * This means that the syscall should
448                                  * only be restarted if there was no
449                                  * handler for the signal, and since
450                                  * this point isn't reached unless
451                                  * there is a handler, there's no need
452                                  * to restart.
453                                  */
454                                 regs->r10 = -EINTR;
455                                 break;
456
457                         case -ERESTARTSYS:
458                                 /*
459                                  * This means restart the syscall if
460                                  * there is no handler, or the handler
461                                  * was registered with SA_RESTART.
462                                  */
463                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
464                                         regs->r10 = -EINTR;
465                                         break;
466                                 }
467
468                                 /* Fall through. */
469
470                         case -ERESTARTNOINTR:
471                                 /*
472                                  * This means that the syscall should
473                                  * be called again after the signal
474                                  * handler returns.
475                                  */
476                                 RESTART_CRIS_SYS(regs);
477                                 break;
478                 }
479         }
480
481         /* Set up the stack frame. */
482         if (ka->sa.sa_flags & SA_SIGINFO)
483                 ret = setup_rt_frame(sig, ka, info, oldset, regs);
484         else
485                 ret = setup_frame(sig, ka, oldset, regs);
486
487         if (ret == 0)
488                 signal_delivered(sig, info, ka, regs, 0);
489 }
490
491 /*
492  * Note that 'init' is a special process: it doesn't get signals it doesn't
493  * want to handle. Thus you cannot kill init even with a SIGKILL even by
494  * mistake.
495  *
496  * Also note that the regs structure given here as an argument, is the latest
497  * pushed pt_regs. It may or may not be the same as the first pushed registers
498  * when the initial usermode->kernelmode transition took place. Therefore
499  * we can use user_mode(regs) to see if we came directly from kernel or user
500  * mode below.
501  */
502 void
503 do_signal(int canrestart, struct pt_regs *regs)
504 {
505         int signr;
506         siginfo_t info;
507         struct k_sigaction ka;
508
509         /*
510          * The common case should go fast, which is why this point is
511          * reached from kernel-mode. If that's the case, just return
512          * without doing anything.
513          */
514         if (!user_mode(regs))
515                 return;
516
517         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
518
519         if (signr > 0) {
520                 /* Whee!  Actually deliver the signal.  */
521                 handle_signal(canrestart, signr, &info, &ka, regs);
522                 return;
523         }
524
525         /* Got here from a system call? */
526         if (canrestart) {
527                 /* Restart the system call - no handlers present. */
528                 if (regs->r10 == -ERESTARTNOHAND ||
529                     regs->r10 == -ERESTARTSYS ||
530                     regs->r10 == -ERESTARTNOINTR) {
531                         RESTART_CRIS_SYS(regs);
532                 }
533
534                 if (regs->r10 == -ERESTART_RESTARTBLOCK){
535                         regs->r9 = __NR_restart_syscall;
536                         regs->erp -= 2;
537                 }
538         }
539
540         /* if there's no signal to deliver, we just put the saved sigmask
541          * back */
542         restore_saved_sigmask();
543 }
544
545 asmlinkage void
546 ugdb_trap_user(struct thread_info *ti, int sig)
547 {
548         if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
549                 /* Zero single-step PC if the reason we stopped wasn't a single
550                    step exception. This is to avoid relying on it when it isn't
551                    reliable. */
552                 user_regs(ti)->spc = 0;
553         }
554         /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
555            not within any configured h/w breakpoint range). Synchronize with
556            what already exists for kernel debugging.  */
557         if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
558                 /* Break 8: subtract 2 from ERP unless in a delay slot. */
559                 if (!(user_regs(ti)->erp & 0x1))
560                         user_regs(ti)->erp -= 2;
561         }
562         sys_kill(ti->task->pid, sig);
563 }
564
565 void
566 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
567                  struct pt_regs *regs)
568 {
569         if (oldccs & (1 << Q_CCS_BITNR)) {
570                 /* Pending single step due to single-stepping the break 13
571                    in the signal trampoline: keep the Q flag. */
572                 regs->ccs |= (1 << Q_CCS_BITNR);
573                 /* S flag should be set - complain if it's not. */
574                 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
575                         printk("Q flag but no S flag?");
576                 }
577                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
578                 /* Assume the SPC is valid and interesting. */
579                 regs->spc = oldspc;
580
581         } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
582                 /* If a h/w bp was set in the signal handler we need
583                    to keep the S flag. */
584                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
585                 /* Don't keep the old SPC though; if we got here due to
586                    a single-step, the Q flag should have been set. */
587         } else if (regs->spc) {
588                 /* If we were single-stepping *before* the signal was taken,
589                    we don't want to restore that state now, because GDB will
590                    have forgotten all about it. */
591                 regs->spc = 0;
592                 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
593         }
594 }
595
596 /* Set up the trampolines on the signal return page. */
597 int __init
598 cris_init_signal(void)
599 {
600         u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
601
602         /* This is movu.w __NR_sigreturn, r9; break 13; */
603         data[0] = 0x9c5f;
604         data[1] = __NR_sigreturn;
605         data[2] = 0xe93d;
606         /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
607         data[3] = 0x9c5f;
608         data[4] = __NR_rt_sigreturn;
609         data[5] = 0xe93d;
610
611         /* Map to userspace with appropriate permissions (no write access...) */
612         cris_signal_return_page = (unsigned long)
613           __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
614
615         return 0;
616 }
617
618 __initcall(cris_init_signal);