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