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