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