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