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