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