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