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