ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / arch-v10 / kernel / signal.c
1 /*
2  *  linux/arch/cris/kernel/signal.c
3  *
4  *  Based on arch/i386/kernel/signal.c by
5  *     Copyright (C) 1991, 1992  Linus Torvalds
6  *     1997-11-28  Modified for POSIX.1b signals by Richard Henderson *
7  *
8  *  Ideas also taken from arch/arm.
9  *
10  *  Copyright (C) 2000, 2001 Axis Communications AB
11  *
12  *  Authors:  Bjorn Wesen (bjornw@axis.com)
13  *
14  */
15
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27
28 #include <asm/processor.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31
32 #define DEBUG_SIG 0
33
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
36 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
37 /* manipulate regs so that upon return, it will be re-executed */
38
39 /* We rely on that pc points to the instruction after "break 13", so the
40  * library must never do strange things like putting it in a delay slot.
41  */
42 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
43
44 int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs);
45
46 /*
47  * Atomically swap in the new signal mask, and wait for a signal.  Define 
48  * dummy arguments to be able to reach the regs argument.  (Note that this
49  * arrangement relies on old_sigset_t occupying one register.)
50  */
51 int
52 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof, 
53                long srp, struct pt_regs *regs)
54 {
55         sigset_t saveset;
56
57         mask &= _BLOCKABLE;
58         spin_lock_irq(&current->sighand->siglock);
59         saveset = current->blocked;
60         siginitset(&current->blocked, mask);
61         recalc_sigpending();
62         spin_unlock_irq(&current->sighand->siglock);
63
64         regs->r10 = -EINTR;
65         while (1) {
66                 current->state = TASK_INTERRUPTIBLE;
67                 schedule();
68                 if (do_signal(0, &saveset, regs))
69                         /* We will get here twice: once to call the signal
70                            handler, then again to return from the
71                            sigsuspend system call.  When calling the
72                            signal handler, R10 holds the signal number as
73                            set through do_signal.  The sigsuspend call
74                            will return with the restored value set above;
75                            always -EINTR.  */
76                         return regs->r10;
77         }
78 }
79
80 /* Define dummy arguments to be able to reach the regs argument.  (Note that
81  * this arrangement relies on size_t occupying one register.)
82  */
83 int
84 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, long r12, long r13, 
85                   long mof, long srp, struct pt_regs *regs)
86 {
87         sigset_t saveset, newset;
88
89         /* XXX: Don't preclude handling different sized sigset_t's.  */
90         if (sigsetsize != sizeof(sigset_t))
91                 return -EINVAL;
92
93         if (copy_from_user(&newset, unewset, sizeof(newset)))
94                 return -EFAULT;
95         sigdelsetmask(&newset, ~_BLOCKABLE);
96
97         spin_lock_irq(&current->sighand->siglock);
98         saveset = current->blocked;
99         current->blocked = newset;
100         recalc_sigpending();
101         spin_unlock_irq(&current->sighand->siglock);
102
103         regs->r10 = -EINTR;
104         while (1) {
105                 current->state = TASK_INTERRUPTIBLE;
106                 schedule();
107                 if (do_signal(0, &saveset, regs))
108                         /* We will get here twice: once to call the signal
109                            handler, then again to return from the
110                            sigsuspend system call.  When calling the
111                            signal handler, R10 holds the signal number as
112                            set through do_signal.  The sigsuspend call
113                            will return with the restored value set above;
114                            always -EINTR.  */
115                         return regs->r10;
116         }
117 }
118
119 int 
120 sys_sigaction(int sig, const struct old_sigaction __user *act,
121               struct old_sigaction *oact)
122 {
123         struct k_sigaction new_ka, old_ka;
124         int ret;
125
126         if (act) {
127                 old_sigset_t mask;
128                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
129                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
130                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
131                         return -EFAULT;
132                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
133                 __get_user(mask, &act->sa_mask);
134                 siginitset(&new_ka.sa.sa_mask, mask);
135         }
136
137         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
138
139         if (!ret && oact) {
140                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
141                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
142                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
143                         return -EFAULT;
144                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
145                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
146         }
147
148         return ret;
149 }
150
151 int
152 sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
153 {
154         return do_sigaltstack(uss, uoss, rdusp());
155 }
156
157
158 /*
159  * Do a signal return; undo the signal stack.
160  */
161
162 struct sigframe {
163         struct sigcontext sc;
164         unsigned long extramask[_NSIG_WORDS-1];
165         unsigned char retcode[8];  /* trampoline code */
166 };
167
168 struct rt_sigframe {
169         struct siginfo *pinfo;
170         void *puc;
171         struct siginfo info;
172         struct ucontext uc;
173         unsigned char retcode[8];  /* trampoline code */
174 };
175
176
177 static int
178 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
179 {
180         unsigned int err = 0;
181         unsigned long old_usp;
182
183         /* restore the regs from &sc->regs (same as sc, since regs is first)
184          * (sc is already checked for VERIFY_READ since the sigframe was
185          *  checked in sys_sigreturn previously)
186          */
187
188         if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
189                 goto badframe;
190
191         /* make sure the U-flag is set so user-mode cannot fool us */
192
193         regs->dccr |= 1 << 8;
194
195         /* restore the old USP as it was before we stacked the sc etc.
196          * (we cannot just pop the sigcontext since we aligned the sp and
197          *  stuff after pushing it)
198          */
199
200         err |= __get_user(old_usp, &sc->usp);
201
202         wrusp(old_usp);
203
204         /* TODO: the other ports use regs->orig_XX to disable syscall checks
205          * after this completes, but we don't use that mechanism. maybe we can
206          * use it now ? 
207          */
208
209         return err;
210
211 badframe:
212         return 1;
213 }
214
215 /* Define dummy arguments to be able to reach the regs argument.  */
216
217 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof, 
218                              long srp, struct pt_regs *regs)
219 {
220         struct sigframe __user *frame = (struct sigframe *)rdusp();
221         sigset_t set;
222
223         /*
224          * Since we stacked the signal on a dword boundary,
225          * then frame should be dword aligned here.  If it's
226          * not, then the user is trying to mess with us.
227          */
228         if (((long)frame) & 3)
229                 goto badframe;
230
231         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
232                 goto badframe;
233         if (__get_user(set.sig[0], &frame->sc.oldmask)
234             || (_NSIG_WORDS > 1
235                 && __copy_from_user(&set.sig[1], frame->extramask,
236                                     sizeof(frame->extramask))))
237                 goto badframe;
238
239         sigdelsetmask(&set, ~_BLOCKABLE);
240         spin_lock_irq(&current->sighand->siglock);
241         current->blocked = set;
242         recalc_sigpending();
243         spin_unlock_irq(&current->sighand->siglock);
244         
245         if (restore_sigcontext(regs, &frame->sc))
246                 goto badframe;
247
248         /* TODO: SIGTRAP when single-stepping as in arm ? */
249
250         return regs->r10;
251
252 badframe:
253         force_sig(SIGSEGV, current);
254         return 0;
255 }       
256
257 /* Define dummy arguments to be able to reach the regs argument.  */
258
259 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13, 
260                                 long mof, long srp, struct pt_regs *regs)
261 {
262         struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
263         sigset_t set;
264         stack_t st;
265
266         /*
267          * Since we stacked the signal on a dword boundary,
268          * then frame should be dword aligned here.  If it's
269          * not, then the user is trying to mess with us.
270          */
271         if (((long)frame) & 3)
272                 goto badframe;
273
274         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
275                 goto badframe;
276         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
277                 goto badframe;
278
279         sigdelsetmask(&set, ~_BLOCKABLE);
280         spin_lock_irq(&current->sighand->siglock);
281         current->blocked = set;
282         recalc_sigpending();
283         spin_unlock_irq(&current->sighand->siglock);
284         
285         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
286                 goto badframe;
287
288         if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
289                 goto badframe;
290         /* It is more difficult to avoid calling this function than to
291            call it and ignore errors.  */
292         do_sigaltstack(&st, NULL, rdusp());
293
294         return regs->r10;
295
296 badframe:
297         force_sig(SIGSEGV, current);
298         return 0;
299 }       
300
301 /*
302  * Set up a signal frame.
303  */
304
305 static int
306 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask)
307 {
308         int err = 0;
309         unsigned long usp = rdusp();
310
311         /* copy the regs. they are first in sc so we can use sc directly */
312
313         err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
314
315         /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
316            the signal handler. The frametype will be restored to its previous
317            value in restore_sigcontext. */
318         regs->frametype = CRIS_FRAME_NORMAL;
319
320         /* then some other stuff */
321
322         err |= __put_user(mask, &sc->oldmask);
323
324         err |= __put_user(usp, &sc->usp);
325
326         return err;
327 }
328
329 /* figure out where we want to put the new signal frame - usually on the stack */
330
331 static inline void __user *
332 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
333 {
334         unsigned long sp = rdusp();
335
336         /* This is the X/Open sanctioned signal stack switching.  */
337         if (ka->sa.sa_flags & SA_ONSTACK) {
338                 if (! on_sig_stack(sp))
339                         sp = current->sas_ss_sp + current->sas_ss_size;
340         }
341
342         /* make sure the frame is dword-aligned */
343
344         sp &= ~3;
345
346         return (void __user*)(sp - frame_size);
347 }
348
349 /* grab and setup a signal frame.
350  * 
351  * basically we stack a lot of state info, and arrange for the
352  * user-mode program to return to the kernel using either a
353  * trampoline which performs the syscall sigreturn, or a provided
354  * user-mode trampoline.
355  */
356
357 static void setup_frame(int sig, struct k_sigaction *ka,
358                         sigset_t *set, struct pt_regs * regs)
359 {
360         struct sigframe __user *frame;
361         unsigned long return_ip;
362         int err = 0;
363
364         frame = get_sigframe(ka, regs, sizeof(*frame));
365
366         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
367                 goto give_sigsegv;
368
369         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
370         if (err)
371                 goto give_sigsegv;
372
373         if (_NSIG_WORDS > 1) {
374                 err |= __copy_to_user(frame->extramask, &set->sig[1],
375                                       sizeof(frame->extramask));
376         }
377         if (err)
378                 goto give_sigsegv;
379
380         /* Set up to return from userspace.  If provided, use a stub
381            already in userspace.  */
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 the retcode itself */
386                 return_ip = (unsigned long)&frame->retcode;
387                 /* This is movu.w __NR_sigreturn, r9; break 13; */
388                 err |= __put_user(0x9c5f,         (short *)(frame->retcode+0));
389                 err |= __put_user(__NR_sigreturn, (short *)(frame->retcode+2));
390                 err |= __put_user(0xe93d,         (short *)(frame->retcode+4));
391         }
392
393         if (err)
394                 goto give_sigsegv;
395
396         /* Set up registers for signal handler */
397
398         regs->irp = (unsigned long) ka->sa.sa_handler;  /* what we enter NOW   */
399         regs->srp = return_ip;                          /* what we enter LATER */
400         regs->r10 = sig;                                /* first argument is signo */
401
402         /* actually move the usp to reflect the stacked frame */
403
404         wrusp((unsigned long)frame);
405
406         return;
407
408 give_sigsegv:
409         if (sig == SIGSEGV)
410                 ka->sa.sa_handler = SIG_DFL;
411         force_sig(SIGSEGV, current);
412 }
413
414 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
415                            sigset_t *set, struct pt_regs * regs)
416 {
417         struct rt_sigframe __user *frame;
418         unsigned long return_ip;
419         int err = 0;
420
421         frame = get_sigframe(ka, regs, sizeof(*frame));
422
423         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
424                 goto give_sigsegv;
425
426         err |= __put_user(&frame->info, &frame->pinfo);
427         err |= __put_user(&frame->uc, &frame->puc);
428         err |= copy_siginfo_to_user(&frame->info, info);
429         if (err)
430                 goto give_sigsegv;
431
432         /* Clear all the bits of the ucontext we don't use.  */
433         err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
434
435         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
436
437         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
438
439         if (err)
440                 goto give_sigsegv;
441
442         /* Set up to return from userspace.  If provided, use a stub
443            already in userspace.  */
444         if (ka->sa.sa_flags & SA_RESTORER) {
445                 return_ip = (unsigned long)ka->sa.sa_restorer;
446         } else {
447                 /* trampoline - the desired return ip is the retcode itself */
448                 return_ip = (unsigned long)&frame->retcode;
449                 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
450                 err |= __put_user(0x9c5f,            (short *)(frame->retcode+0));
451                 err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode+2));
452                 err |= __put_user(0xe93d,            (short *)(frame->retcode+4));
453         }
454
455         if (err)
456                 goto give_sigsegv;
457
458         /* TODO what is the current->exec_domain stuff and invmap ? */
459
460         /* Set up registers for signal handler */
461
462         regs->irp = (unsigned long) ka->sa.sa_handler;  /* what we enter NOW   */
463         regs->srp = return_ip;                          /* what we enter LATER */
464         regs->r10 = sig;                                /* first argument is signo */
465         regs->r11 = (unsigned long) &frame->info;       /* second argument is (siginfo_t *) */
466         regs->r12 = 0;                                  /* third argument is unused */
467
468         /* actually move the usp to reflect the stacked frame */
469
470         wrusp((unsigned long)frame);
471
472         return;
473
474 give_sigsegv:
475         if (sig == SIGSEGV)
476                 ka->sa.sa_handler = SIG_DFL;
477         force_sig(SIGSEGV, current);
478 }
479
480 /*
481  * OK, we're invoking a handler
482  */     
483
484 extern inline void
485 handle_signal(int canrestart, unsigned long sig,
486               siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
487 {
488         struct k_sigaction *ka = &current->sighand->action[sig-1];
489
490         /* Are we from a system call? */
491         if (canrestart) {
492                 /* If so, check system call restarting.. */
493                 switch (regs->r10) {
494                         case -ERESTART_RESTARTBLOCK:
495                                 current_thread_info()->restart_block.fn = do_no_restart_syscall;
496                         case -ERESTARTNOHAND:
497                                 /* ERESTARTNOHAND means that the syscall should only be
498                                    restarted if there was no handler for the signal, and since
499                                    we only get here if there is a handler, we don't restart */
500                                 regs->r10 = -EINTR;
501                                 break;
502
503                         case -ERESTARTSYS:
504                                 /* ERESTARTSYS means to restart the syscall if there is no
505                                    handler or the handler was registered with SA_RESTART */
506                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
507                                         regs->r10 = -EINTR;
508                                         break;
509                                 }
510                         /* fallthrough */
511                         case -ERESTARTNOINTR:
512                                 /* ERESTARTNOINTR means that the syscall should be called again
513                                    after the signal handler returns. */
514                                 RESTART_CRIS_SYS(regs);
515                 }
516         }
517
518         /* Set up the stack frame */
519         if (ka->sa.sa_flags & SA_SIGINFO)
520                 setup_rt_frame(sig, ka, info, oldset, regs);
521         else
522                 setup_frame(sig, ka, oldset, regs);
523
524         if (ka->sa.sa_flags & SA_ONESHOT)
525                 ka->sa.sa_handler = SIG_DFL;
526
527         if (!(ka->sa.sa_flags & SA_NODEFER)) {
528                 spin_lock_irq(&current->sighand->siglock);
529                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
530                 sigaddset(&current->blocked,sig);
531                 recalc_sigpending();
532                 spin_unlock_irq(&current->sighand->siglock);
533         }
534 }
535
536 /*
537  * Note that 'init' is a special process: it doesn't get signals it doesn't
538  * want to handle. Thus you cannot kill init even with a SIGKILL even by
539  * mistake.
540  *
541  * Also note that the regs structure given here as an argument, is the latest
542  * pushed pt_regs. It may or may not be the same as the first pushed registers
543  * when the initial usermode->kernelmode transition took place. Therefore
544  * we can use user_mode(regs) to see if we came directly from kernel or user
545  * mode below.
546  */
547
548 int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
549 {
550         siginfo_t info;
551         int signr;
552
553         /*
554          * We want the common case to go fast, which
555          * is why we may in certain cases get here from
556          * kernel mode. Just return without doing anything
557          * if so.
558          */
559         if (!user_mode(regs))
560                 return 1;
561
562         if (!oldset)
563                 oldset = &current->blocked;
564
565         signr = get_signal_to_deliver(&info, regs, NULL);
566         if (signr > 0) {
567                 /* Whee!  Actually deliver the signal.  */
568                 handle_signal(canrestart, signr, &info, oldset, regs);
569                 return 1;
570         }
571
572         /* Did we come from a system call? */
573         if (canrestart) {
574                 /* Restart the system call - no handlers present */
575                 if (regs->r10 == -ERESTARTNOHAND ||
576                     regs->r10 == -ERESTARTSYS ||
577                     regs->r10 == -ERESTARTNOINTR) {
578                         RESTART_CRIS_SYS(regs);
579                 }
580                 if (regs->r10 == -ERESTART_RESTARTBLOCK){
581                         regs->r10 = __NR_restart_syscall;
582                         regs->irp -= 2;
583                 }
584         }
585         return 0;
586 }