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