Merge master.kernel.org:/pub/scm/linux/kernel/git/sam/kbuild
[linux-2.6] / arch / sh / kernel / signal.c
1 /*
2  *  linux/arch/sh/kernel/signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
7  *
8  *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
9  *
10  */
11
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/tty.h>
24 #include <linux/personality.h>
25 #include <linux/binfmts.h>
26
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/cacheflush.h>
31
32 #define DEBUG_SIG 0
33
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
36 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
37
38 /*
39  * Atomically swap in the new signal mask, and wait for a signal.
40  */
41 asmlinkage int
42 sys_sigsuspend(old_sigset_t mask,
43                unsigned long r5, unsigned long r6, unsigned long r7,
44                struct pt_regs regs)
45 {
46         sigset_t saveset;
47
48         mask &= _BLOCKABLE;
49         spin_lock_irq(&current->sighand->siglock);
50         saveset = current->blocked;
51         siginitset(&current->blocked, mask);
52         recalc_sigpending();
53         spin_unlock_irq(&current->sighand->siglock);
54
55         regs.regs[0] = -EINTR;
56         while (1) {
57                 current->state = TASK_INTERRUPTIBLE;
58                 schedule();
59                 if (do_signal(&regs, &saveset))
60                         return -EINTR;
61         }
62 }
63
64 asmlinkage int
65 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
66                   unsigned long r6, unsigned long r7,
67                   struct pt_regs regs)
68 {
69         sigset_t saveset, newset;
70
71         /* XXX: Don't preclude handling different sized sigset_t's.  */
72         if (sigsetsize != sizeof(sigset_t))
73                 return -EINVAL;
74
75         if (copy_from_user(&newset, unewset, sizeof(newset)))
76                 return -EFAULT;
77         sigdelsetmask(&newset, ~_BLOCKABLE);
78         spin_lock_irq(&current->sighand->siglock);
79         saveset = current->blocked;
80         current->blocked = newset;
81         recalc_sigpending();
82         spin_unlock_irq(&current->sighand->siglock);
83
84         regs.regs[0] = -EINTR;
85         while (1) {
86                 current->state = TASK_INTERRUPTIBLE;
87                 schedule();
88                 if (do_signal(&regs, &saveset))
89                         return -EINTR;
90         }
91 }
92
93 asmlinkage int 
94 sys_sigaction(int sig, const struct old_sigaction __user *act,
95               struct old_sigaction __user *oact)
96 {
97         struct k_sigaction new_ka, old_ka;
98         int ret;
99
100         if (act) {
101                 old_sigset_t mask;
102                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
103                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
104                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
105                         return -EFAULT;
106                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
107                 __get_user(mask, &act->sa_mask);
108                 siginitset(&new_ka.sa.sa_mask, mask);
109         }
110
111         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
112
113         if (!ret && oact) {
114                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
115                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
116                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
117                         return -EFAULT;
118                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
119                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
120         }
121
122         return ret;
123 }
124
125 asmlinkage int
126 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
127                 unsigned long r6, unsigned long r7,
128                 struct pt_regs regs)
129 {
130         return do_sigaltstack(uss, uoss, regs.regs[15]);
131 }
132
133
134 /*
135  * Do a signal return; undo the signal stack.
136  */
137
138 #define MOVW(n)  (0x9300|((n)-2))       /* Move mem word at PC+n to R3 */
139 #define TRAP16   0xc310                 /* Syscall w/no args (NR in R3) */
140 #define OR_R0_R0 0x200b                 /* or r0,r0 (insert to avoid hardware bug) */
141
142 struct sigframe
143 {
144         struct sigcontext sc;
145         unsigned long extramask[_NSIG_WORDS-1];
146         u16 retcode[8];
147 };
148
149 struct rt_sigframe
150 {
151         struct siginfo info;
152         struct ucontext uc;
153         u16 retcode[8];
154 };
155
156 #ifdef CONFIG_SH_FPU
157 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
158 {
159         struct task_struct *tsk = current;
160
161         if (!(cpu_data->flags & CPU_HAS_FPU))
162                 return 0;
163
164         set_used_math();
165         return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
166                                 sizeof(long)*(16*2+2));
167 }
168
169 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
170                                       struct pt_regs *regs)
171 {
172         struct task_struct *tsk = current;
173
174         if (!(cpu_data->flags & CPU_HAS_FPU))
175                 return 0;
176
177         if (!used_math()) {
178                 __put_user(0, &sc->sc_ownedfp);
179                 return 0;
180         }
181
182         __put_user(1, &sc->sc_ownedfp);
183
184         /* This will cause a "finit" to be triggered by the next
185            attempted FPU operation by the 'current' process.
186            */
187         clear_used_math();
188
189         unlazy_fpu(tsk, regs);
190         return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
191                               sizeof(long)*(16*2+2));
192 }
193 #endif /* CONFIG_SH_FPU */
194
195 static int
196 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
197 {
198         unsigned int err = 0;
199
200 #define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
201                         COPY(regs[1]);
202         COPY(regs[2]);  COPY(regs[3]);
203         COPY(regs[4]);  COPY(regs[5]);
204         COPY(regs[6]);  COPY(regs[7]);
205         COPY(regs[8]);  COPY(regs[9]);
206         COPY(regs[10]); COPY(regs[11]);
207         COPY(regs[12]); COPY(regs[13]);
208         COPY(regs[14]); COPY(regs[15]);
209         COPY(gbr);      COPY(mach);
210         COPY(macl);     COPY(pr);
211         COPY(sr);       COPY(pc);
212 #undef COPY
213
214 #ifdef CONFIG_SH_FPU
215         if (cpu_data->flags & CPU_HAS_FPU) {
216                 int owned_fp;
217                 struct task_struct *tsk = current;
218
219                 regs->sr |= SR_FD; /* Release FPU */
220                 clear_fpu(tsk, regs);
221                 clear_used_math();
222                 __get_user (owned_fp, &sc->sc_ownedfp);
223                 if (owned_fp)
224                         err |= restore_sigcontext_fpu(sc);
225         }
226 #endif
227
228         regs->tra = -1;         /* disable syscall checks */
229         err |= __get_user(*r0_p, &sc->sc_regs[0]);
230         return err;
231 }
232
233 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
234                              unsigned long r6, unsigned long r7,
235                              struct pt_regs regs)
236 {
237         struct sigframe __user *frame = (struct sigframe __user *)regs.regs[15];
238         sigset_t set;
239         int r0;
240
241         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
242                 goto badframe;
243
244         if (__get_user(set.sig[0], &frame->sc.oldmask)
245             || (_NSIG_WORDS > 1
246                 && __copy_from_user(&set.sig[1], &frame->extramask,
247                                     sizeof(frame->extramask))))
248                 goto badframe;
249
250         sigdelsetmask(&set, ~_BLOCKABLE);
251
252         spin_lock_irq(&current->sighand->siglock);
253         current->blocked = set;
254         recalc_sigpending();
255         spin_unlock_irq(&current->sighand->siglock);
256
257         if (restore_sigcontext(&regs, &frame->sc, &r0))
258                 goto badframe;
259         return r0;
260
261 badframe:
262         force_sig(SIGSEGV, current);
263         return 0;
264 }
265
266 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
267                                 unsigned long r6, unsigned long r7,
268                                 struct pt_regs regs)
269 {
270         struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs.regs[15];
271         sigset_t set;
272         stack_t st;
273         int r0;
274
275         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
276                 goto badframe;
277
278         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
279                 goto badframe;
280
281         sigdelsetmask(&set, ~_BLOCKABLE);
282         spin_lock_irq(&current->sighand->siglock);
283         current->blocked = set;
284         recalc_sigpending();
285         spin_unlock_irq(&current->sighand->siglock);
286
287         if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &r0))
288                 goto badframe;
289
290         if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
291                 goto badframe;
292         /* It is more difficult to avoid calling this function than to
293            call it and ignore errors.  */
294         do_sigaltstack(&st, NULL, regs.regs[15]);
295
296         return r0;
297
298 badframe:
299         force_sig(SIGSEGV, current);
300         return 0;
301 }       
302
303 /*
304  * Set up a signal frame.
305  */
306
307 static int
308 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
309                  unsigned long mask)
310 {
311         int err = 0;
312
313 #define COPY(x)         err |= __put_user(regs->x, &sc->sc_##x)
314         COPY(regs[0]);  COPY(regs[1]);
315         COPY(regs[2]);  COPY(regs[3]);
316         COPY(regs[4]);  COPY(regs[5]);
317         COPY(regs[6]);  COPY(regs[7]);
318         COPY(regs[8]);  COPY(regs[9]);
319         COPY(regs[10]); COPY(regs[11]);
320         COPY(regs[12]); COPY(regs[13]);
321         COPY(regs[14]); COPY(regs[15]);
322         COPY(gbr);      COPY(mach);
323         COPY(macl);     COPY(pr);
324         COPY(sr);       COPY(pc);
325 #undef COPY
326
327 #ifdef CONFIG_SH_FPU
328         err |= save_sigcontext_fpu(sc, regs);
329 #endif
330
331         /* non-iBCS2 extensions.. */
332         err |= __put_user(mask, &sc->oldmask);
333
334         return err;
335 }
336
337 /*
338  * Determine which stack to use..
339  */
340 static inline void __user *
341 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
342 {
343         if (ka->sa.sa_flags & SA_ONSTACK) {
344                 if (sas_ss_flags(sp) == 0)
345                         sp = current->sas_ss_sp + current->sas_ss_size;
346         }
347
348         return (void __user *)((sp - frame_size) & -8ul);
349 }
350
351 static void setup_frame(int sig, struct k_sigaction *ka,
352                         sigset_t *set, struct pt_regs *regs)
353 {
354         struct sigframe __user *frame;
355         int err = 0;
356         int signal;
357
358         frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
359
360         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
361                 goto give_sigsegv;
362
363         signal = current_thread_info()->exec_domain
364                 && current_thread_info()->exec_domain->signal_invmap
365                 && sig < 32
366                 ? current_thread_info()->exec_domain->signal_invmap[sig]
367                 : sig;
368
369         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
370
371         if (_NSIG_WORDS > 1) {
372                 err |= __copy_to_user(frame->extramask, &set->sig[1],
373                                       sizeof(frame->extramask));
374         }
375
376         /* Set up to return from userspace.  If provided, use a stub
377            already in userspace.  */
378         if (ka->sa.sa_flags & SA_RESTORER) {
379                 regs->pr = (unsigned long) ka->sa.sa_restorer;
380         } else {
381                 /* Generate return code (system call to sigreturn) */
382                 err |= __put_user(MOVW(7), &frame->retcode[0]);
383                 err |= __put_user(TRAP16, &frame->retcode[1]);
384                 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
385                 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
386                 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
387                 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
388                 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
389                 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
390                 regs->pr = (unsigned long) frame->retcode;
391         }
392
393         if (err)
394                 goto give_sigsegv;
395
396         /* Set up registers for signal handler */
397         regs->regs[15] = (unsigned long) frame;
398         regs->regs[4] = signal; /* Arg for signal handler */
399         regs->regs[5] = 0;
400         regs->regs[6] = (unsigned long) &frame->sc;
401         regs->pc = (unsigned long) ka->sa.sa_handler;
402
403         set_fs(USER_DS);
404
405 #if DEBUG_SIG
406         printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
407                 current->comm, current->pid, frame, regs->pc, regs->pr);
408 #endif
409
410         flush_cache_sigtramp(regs->pr);
411         if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
412                 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
413         return;
414
415 give_sigsegv:
416         force_sigsegv(sig, current);
417 }
418
419 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
420                            sigset_t *set, struct pt_regs *regs)
421 {
422         struct rt_sigframe __user *frame;
423         int err = 0;
424         int signal;
425
426         frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
427
428         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
429                 goto give_sigsegv;
430
431         signal = current_thread_info()->exec_domain
432                 && current_thread_info()->exec_domain->signal_invmap
433                 && sig < 32
434                 ? current_thread_info()->exec_domain->signal_invmap[sig]
435                 : sig;
436
437         err |= copy_siginfo_to_user(&frame->info, info);
438
439         /* Create the ucontext.  */
440         err |= __put_user(0, &frame->uc.uc_flags);
441         err |= __put_user(0, &frame->uc.uc_link);
442         err |= __put_user((void *)current->sas_ss_sp,
443                           &frame->uc.uc_stack.ss_sp);
444         err |= __put_user(sas_ss_flags(regs->regs[15]),
445                           &frame->uc.uc_stack.ss_flags);
446         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
447         err |= setup_sigcontext(&frame->uc.uc_mcontext,
448                                 regs, set->sig[0]);
449         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
450
451         /* Set up to return from userspace.  If provided, use a stub
452            already in userspace.  */
453         if (ka->sa.sa_flags & SA_RESTORER) {
454                 regs->pr = (unsigned long) ka->sa.sa_restorer;
455         } else {
456                 /* Generate return code (system call to rt_sigreturn) */
457                 err |= __put_user(MOVW(7), &frame->retcode[0]);
458                 err |= __put_user(TRAP16, &frame->retcode[1]);
459                 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
460                 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
461                 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
462                 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
463                 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
464                 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
465                 regs->pr = (unsigned long) frame->retcode;
466         }
467
468         if (err)
469                 goto give_sigsegv;
470
471         /* Set up registers for signal handler */
472         regs->regs[15] = (unsigned long) frame;
473         regs->regs[4] = signal; /* Arg for signal handler */
474         regs->regs[5] = (unsigned long) &frame->info;
475         regs->regs[6] = (unsigned long) &frame->uc;
476         regs->pc = (unsigned long) ka->sa.sa_handler;
477
478         set_fs(USER_DS);
479
480 #if DEBUG_SIG
481         printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
482                 current->comm, current->pid, frame, regs->pc, regs->pr);
483 #endif
484
485         flush_cache_sigtramp(regs->pr);
486         if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
487                 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
488         return;
489
490 give_sigsegv:
491         force_sigsegv(sig, current);
492 }
493
494 /*
495  * OK, we're invoking a handler
496  */
497
498 static void
499 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
500               sigset_t *oldset, struct pt_regs *regs)
501 {
502         /* Are we from a system call? */
503         if (regs->tra >= 0) {
504                 /* If so, check system call restarting.. */
505                 switch (regs->regs[0]) {
506                         case -ERESTARTNOHAND:
507                                 regs->regs[0] = -EINTR;
508                                 break;
509
510                         case -ERESTARTSYS:
511                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
512                                         regs->regs[0] = -EINTR;
513                                         break;
514                                 }
515                         /* fallthrough */
516                         case -ERESTARTNOINTR:
517                                 regs->pc -= 2;
518                 }
519         } else {
520                 /* gUSA handling */
521 #ifdef CONFIG_PREEMPT
522                 unsigned long flags;
523
524                 local_irq_save(flags);
525 #endif
526                 if (regs->regs[15] >= 0xc0000000) {
527                         int offset = (int)regs->regs[15];
528
529                         /* Reset stack pointer: clear critical region mark */
530                         regs->regs[15] = regs->regs[1];
531                         if (regs->pc < regs->regs[0])
532                                 /* Go to rewind point #1 */
533                                 regs->pc = regs->regs[0] + offset - 2;
534                 }
535 #ifdef CONFIG_PREEMPT
536                 local_irq_restore(flags);
537 #endif
538         }
539
540         /* Set up the stack frame */
541         if (ka->sa.sa_flags & SA_SIGINFO)
542                 setup_rt_frame(sig, ka, info, oldset, regs);
543         else
544                 setup_frame(sig, ka, oldset, regs);
545
546         if (ka->sa.sa_flags & SA_ONESHOT)
547                 ka->sa.sa_handler = SIG_DFL;
548
549         spin_lock_irq(&current->sighand->siglock);
550         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
551         if (!(ka->sa.sa_flags & SA_NODEFER))
552                 sigaddset(&current->blocked,sig);
553         recalc_sigpending();
554         spin_unlock_irq(&current->sighand->siglock);
555 }
556
557 /*
558  * Note that 'init' is a special process: it doesn't get signals it doesn't
559  * want to handle. Thus you cannot kill init even with a SIGKILL even by
560  * mistake.
561  *
562  * Note that we go through the signals twice: once to check the signals that
563  * the kernel can handle, and then we build all the user-level signal handling
564  * stack-frames in one go after that.
565  */
566 int do_signal(struct pt_regs *regs, sigset_t *oldset)
567 {
568         siginfo_t info;
569         int signr;
570         struct k_sigaction ka;
571
572         /*
573          * We want the common case to go fast, which
574          * is why we may in certain cases get here from
575          * kernel mode. Just return without doing anything
576          * if so.
577          */
578         if (!user_mode(regs))
579                 return 1;
580
581         if (try_to_freeze())
582                 goto no_signal;
583
584         if (!oldset)
585                 oldset = &current->blocked;
586
587         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
588         if (signr > 0) {
589                 /* Whee!  Actually deliver the signal.  */
590                 handle_signal(signr, &ka, &info, oldset, regs);
591                 return 1;
592         }
593
594  no_signal:
595         /* Did we come from a system call? */
596         if (regs->tra >= 0) {
597                 /* Restart the system call - no handlers present */
598                 if (regs->regs[0] == -ERESTARTNOHAND ||
599                     regs->regs[0] == -ERESTARTSYS ||
600                     regs->regs[0] == -ERESTARTNOINTR ||
601                     regs->regs[0] == -ERESTART_RESTARTBLOCK) {
602                         regs->pc -= 2;
603                 }
604         }
605         return 0;
606 }