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