sparc: Use tracehook routines in syscall_trace().
[linux-2.6.git] / arch / sparc / kernel / signal.c
1 /*  linux/arch/sparc/kernel/signal.c
2  *
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  *  Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6  *  Copyright (C) 1997 Eddie C. Dost   (ecd@skynet.be)
7  */
8
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/wait.h>
14 #include <linux/ptrace.h>
15 #include <linux/unistd.h>
16 #include <linux/mm.h>
17 #include <linux/tty.h>
18 #include <linux/smp.h>
19 #include <linux/binfmts.h>      /* do_coredum */
20 #include <linux/bitops.h>
21
22 #include <asm/uaccess.h>
23 #include <asm/ptrace.h>
24 #include <asm/pgalloc.h>
25 #include <asm/pgtable.h>
26 #include <asm/cacheflush.h>     /* flush_sig_insns */
27
28 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
29
30 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
31                    void *fpqueue, unsigned long *fpqdepth);
32 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
33
34 struct signal_frame {
35         struct sparc_stackf     ss;
36         __siginfo_t             info;
37         __siginfo_fpu_t __user  *fpu_save;
38         unsigned long           insns[2] __attribute__ ((aligned (8)));
39         unsigned int            extramask[_NSIG_WORDS - 1];
40         unsigned int            extra_size; /* Should be 0 */
41         __siginfo_fpu_t         fpu_state;
42 };
43
44 struct rt_signal_frame {
45         struct sparc_stackf     ss;
46         siginfo_t               info;
47         struct pt_regs          regs;
48         sigset_t                mask;
49         __siginfo_fpu_t __user  *fpu_save;
50         unsigned int            insns[2];
51         stack_t                 stack;
52         unsigned int            extra_size; /* Should be 0 */
53         __siginfo_fpu_t         fpu_state;
54 };
55
56 /* Align macros */
57 #define SF_ALIGNEDSZ  (((sizeof(struct signal_frame) + 7) & (~7)))
58 #define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame) + 7) & (~7)))
59
60 static int _sigpause_common(old_sigset_t set)
61 {
62         set &= _BLOCKABLE;
63         spin_lock_irq(&current->sighand->siglock);
64         current->saved_sigmask = current->blocked;
65         siginitset(&current->blocked, set);
66         recalc_sigpending();
67         spin_unlock_irq(&current->sighand->siglock);
68
69         current->state = TASK_INTERRUPTIBLE;
70         schedule();
71         set_thread_flag(TIF_RESTORE_SIGMASK);
72
73         return -ERESTARTNOHAND;
74 }
75
76 asmlinkage int sys_sigsuspend(old_sigset_t set)
77 {
78         return _sigpause_common(set);
79 }
80
81 static inline int
82 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
83 {
84         int err;
85 #ifdef CONFIG_SMP
86         if (test_tsk_thread_flag(current, TIF_USEDFPU))
87                 regs->psr &= ~PSR_EF;
88 #else
89         if (current == last_task_used_math) {
90                 last_task_used_math = NULL;
91                 regs->psr &= ~PSR_EF;
92         }
93 #endif
94         set_used_math();
95         clear_tsk_thread_flag(current, TIF_USEDFPU);
96
97         if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
98                 return -EFAULT;
99
100         err = __copy_from_user(&current->thread.float_regs[0], &fpu->si_float_regs[0],
101                                (sizeof(unsigned long) * 32));
102         err |= __get_user(current->thread.fsr, &fpu->si_fsr);
103         err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
104         if (current->thread.fpqdepth != 0)
105                 err |= __copy_from_user(&current->thread.fpqueue[0],
106                                         &fpu->si_fpqueue[0],
107                                         ((sizeof(unsigned long) +
108                                         (sizeof(unsigned long *)))*16));
109         return err;
110 }
111
112 asmlinkage void do_sigreturn(struct pt_regs *regs)
113 {
114         struct signal_frame __user *sf;
115         unsigned long up_psr, pc, npc;
116         sigset_t set;
117         __siginfo_fpu_t __user *fpu_save;
118         int err;
119
120         /* Always make any pending restarted system calls return -EINTR */
121         current_thread_info()->restart_block.fn = do_no_restart_syscall;
122
123         synchronize_user_stack();
124
125         sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
126
127         /* 1. Make sure we are not getting garbage from the user */
128         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
129                 goto segv_and_exit;
130
131         if (((unsigned long) sf) & 3)
132                 goto segv_and_exit;
133
134         err = __get_user(pc,  &sf->info.si_regs.pc);
135         err |= __get_user(npc, &sf->info.si_regs.npc);
136
137         if ((pc | npc) & 3)
138                 goto segv_and_exit;
139
140         /* 2. Restore the state */
141         up_psr = regs->psr;
142         err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
143
144         /* User can only change condition codes and FPU enabling in %psr. */
145         regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
146                   | (regs->psr & (PSR_ICC | PSR_EF));
147
148         /* Prevent syscall restart.  */
149         pt_regs_clear_syscall(regs);
150
151         err |= __get_user(fpu_save, &sf->fpu_save);
152
153         if (fpu_save)
154                 err |= restore_fpu_state(regs, fpu_save);
155
156         /* This is pretty much atomic, no amount locking would prevent
157          * the races which exist anyways.
158          */
159         err |= __get_user(set.sig[0], &sf->info.si_mask);
160         err |= __copy_from_user(&set.sig[1], &sf->extramask,
161                                 (_NSIG_WORDS-1) * sizeof(unsigned int));
162                            
163         if (err)
164                 goto segv_and_exit;
165
166         sigdelsetmask(&set, ~_BLOCKABLE);
167         spin_lock_irq(&current->sighand->siglock);
168         current->blocked = set;
169         recalc_sigpending();
170         spin_unlock_irq(&current->sighand->siglock);
171         return;
172
173 segv_and_exit:
174         force_sig(SIGSEGV, current);
175 }
176
177 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
178 {
179         struct rt_signal_frame __user *sf;
180         unsigned int psr, pc, npc;
181         __siginfo_fpu_t __user *fpu_save;
182         mm_segment_t old_fs;
183         sigset_t set;
184         stack_t st;
185         int err;
186
187         synchronize_user_stack();
188         sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
189         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
190             (((unsigned long) sf) & 0x03))
191                 goto segv;
192
193         err = __get_user(pc, &sf->regs.pc);
194         err |= __get_user(npc, &sf->regs.npc);
195         err |= ((pc | npc) & 0x03);
196
197         err |= __get_user(regs->y, &sf->regs.y);
198         err |= __get_user(psr, &sf->regs.psr);
199
200         err |= __copy_from_user(&regs->u_regs[UREG_G1],
201                                 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
202
203         regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
204
205         /* Prevent syscall restart.  */
206         pt_regs_clear_syscall(regs);
207
208         err |= __get_user(fpu_save, &sf->fpu_save);
209
210         if (fpu_save)
211                 err |= restore_fpu_state(regs, fpu_save);
212         err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
213         
214         err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
215         
216         if (err)
217                 goto segv;
218                 
219         regs->pc = pc;
220         regs->npc = npc;
221         
222         /* It is more difficult to avoid calling this function than to
223          * call it and ignore errors.
224          */
225         old_fs = get_fs();
226         set_fs(KERNEL_DS);
227         do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
228         set_fs(old_fs);
229
230         sigdelsetmask(&set, ~_BLOCKABLE);
231         spin_lock_irq(&current->sighand->siglock);
232         current->blocked = set;
233         recalc_sigpending();
234         spin_unlock_irq(&current->sighand->siglock);
235         return;
236 segv:
237         force_sig(SIGSEGV, current);
238 }
239
240 /* Checks if the fp is valid */
241 static inline int invalid_frame_pointer(void __user *fp, int fplen)
242 {
243         if ((((unsigned long) fp) & 7) ||
244             !__access_ok((unsigned long)fp, fplen) ||
245             ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
246              ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
247                 return 1;
248         
249         return 0;
250 }
251
252 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
253 {
254         unsigned long sp = regs->u_regs[UREG_FP];
255
256         /*
257          * If we are on the alternate signal stack and would overflow it, don't.
258          * Return an always-bogus address instead so we will die with SIGSEGV.
259          */
260         if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
261                 return (void __user *) -1L;
262
263         /* This is the X/Open sanctioned signal stack switching.  */
264         if (sa->sa_flags & SA_ONSTACK) {
265                 if (sas_ss_flags(sp) == 0)
266                         sp = current->sas_ss_sp + current->sas_ss_size;
267         }
268
269         /* Always align the stack frame.  This handles two cases.  First,
270          * sigaltstack need not be mindful of platform specific stack
271          * alignment.  Second, if we took this signal because the stack
272          * is not aligned properly, we'd like to take the signal cleanly
273          * and report that.
274          */
275         sp &= ~7UL;
276
277         return (void __user *)(sp - framesize);
278 }
279
280 static inline int
281 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
282 {
283         int err = 0;
284 #ifdef CONFIG_SMP
285         if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
286                 put_psr(get_psr() | PSR_EF);
287                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
288                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
289                 regs->psr &= ~(PSR_EF);
290                 clear_tsk_thread_flag(current, TIF_USEDFPU);
291         }
292 #else
293         if (current == last_task_used_math) {
294                 put_psr(get_psr() | PSR_EF);
295                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
296                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
297                 last_task_used_math = NULL;
298                 regs->psr &= ~(PSR_EF);
299         }
300 #endif
301         err |= __copy_to_user(&fpu->si_float_regs[0],
302                               &current->thread.float_regs[0],
303                               (sizeof(unsigned long) * 32));
304         err |= __put_user(current->thread.fsr, &fpu->si_fsr);
305         err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
306         if (current->thread.fpqdepth != 0)
307                 err |= __copy_to_user(&fpu->si_fpqueue[0],
308                                       &current->thread.fpqueue[0],
309                                       ((sizeof(unsigned long) +
310                                       (sizeof(unsigned long *)))*16));
311         clear_used_math();
312         return err;
313 }
314
315 static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
316                         int signo, sigset_t *oldset)
317 {
318         struct signal_frame __user *sf;
319         int sigframe_size, err;
320
321         /* 1. Make sure everything is clean */
322         synchronize_user_stack();
323
324         sigframe_size = SF_ALIGNEDSZ;
325         if (!used_math())
326                 sigframe_size -= sizeof(__siginfo_fpu_t);
327
328         sf = (struct signal_frame __user *)
329                 get_sigframe(&ka->sa, regs, sigframe_size);
330
331         if (invalid_frame_pointer(sf, sigframe_size))
332                 goto sigill_and_return;
333
334         if (current_thread_info()->w_saved != 0)
335                 goto sigill_and_return;
336
337         /* 2. Save the current process state */
338         err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
339         
340         err |= __put_user(0, &sf->extra_size);
341
342         if (used_math()) {
343                 err |= save_fpu_state(regs, &sf->fpu_state);
344                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
345         } else {
346                 err |= __put_user(0, &sf->fpu_save);
347         }
348
349         err |= __put_user(oldset->sig[0], &sf->info.si_mask);
350         err |= __copy_to_user(sf->extramask, &oldset->sig[1],
351                               (_NSIG_WORDS - 1) * sizeof(unsigned int));
352         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
353                               sizeof(struct reg_window));
354         if (err)
355                 goto sigsegv;
356         
357         /* 3. signal handler back-trampoline and parameters */
358         regs->u_regs[UREG_FP] = (unsigned long) sf;
359         regs->u_regs[UREG_I0] = signo;
360         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
361         regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
362
363         /* 4. signal handler */
364         regs->pc = (unsigned long) ka->sa.sa_handler;
365         regs->npc = (regs->pc + 4);
366
367         /* 5. return to kernel instructions */
368         if (ka->ka_restorer)
369                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
370         else {
371                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
372
373                 /* mov __NR_sigreturn, %g1 */
374                 err |= __put_user(0x821020d8, &sf->insns[0]);
375
376                 /* t 0x10 */
377                 err |= __put_user(0x91d02010, &sf->insns[1]);
378                 if (err)
379                         goto sigsegv;
380
381                 /* Flush instruction space. */
382                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
383         }
384         return;
385
386 sigill_and_return:
387         do_exit(SIGILL);
388 sigsegv:
389         force_sigsegv(signo, current);
390 }
391
392 static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
393                            int signo, sigset_t *oldset, siginfo_t *info)
394 {
395         struct rt_signal_frame __user *sf;
396         int sigframe_size;
397         unsigned int psr;
398         int err;
399
400         synchronize_user_stack();
401         sigframe_size = RT_ALIGNEDSZ;
402         if (!used_math())
403                 sigframe_size -= sizeof(__siginfo_fpu_t);
404         sf = (struct rt_signal_frame __user *)
405                 get_sigframe(&ka->sa, regs, sigframe_size);
406         if (invalid_frame_pointer(sf, sigframe_size))
407                 goto sigill;
408         if (current_thread_info()->w_saved != 0)
409                 goto sigill;
410
411         err  = __put_user(regs->pc, &sf->regs.pc);
412         err |= __put_user(regs->npc, &sf->regs.npc);
413         err |= __put_user(regs->y, &sf->regs.y);
414         psr = regs->psr;
415         if (used_math())
416                 psr |= PSR_EF;
417         err |= __put_user(psr, &sf->regs.psr);
418         err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
419         err |= __put_user(0, &sf->extra_size);
420
421         if (psr & PSR_EF) {
422                 err |= save_fpu_state(regs, &sf->fpu_state);
423                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
424         } else {
425                 err |= __put_user(0, &sf->fpu_save);
426         }
427         err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
428         
429         /* Setup sigaltstack */
430         err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
431         err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
432         err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
433         
434         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
435                               sizeof(struct reg_window));       
436
437         err |= copy_siginfo_to_user(&sf->info, info);
438
439         if (err)
440                 goto sigsegv;
441
442         regs->u_regs[UREG_FP] = (unsigned long) sf;
443         regs->u_regs[UREG_I0] = signo;
444         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
445         regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
446
447         regs->pc = (unsigned long) ka->sa.sa_handler;
448         regs->npc = (regs->pc + 4);
449
450         if (ka->ka_restorer)
451                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
452         else {
453                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
454
455                 /* mov __NR_sigreturn, %g1 */
456                 err |= __put_user(0x821020d8, &sf->insns[0]);
457
458                 /* t 0x10 */
459                 err |= __put_user(0x91d02010, &sf->insns[1]);
460                 if (err)
461                         goto sigsegv;
462
463                 /* Flush instruction space. */
464                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
465         }
466         return;
467
468 sigill:
469         do_exit(SIGILL);
470 sigsegv:
471         force_sigsegv(signo, current);
472 }
473
474 static inline void
475 handle_signal(unsigned long signr, struct k_sigaction *ka,
476               siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
477 {
478         if (ka->sa.sa_flags & SA_SIGINFO)
479                 setup_rt_frame(ka, regs, signr, oldset, info);
480         else
481                 setup_frame(ka, regs, signr, oldset);
482
483         spin_lock_irq(&current->sighand->siglock);
484         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
485         if (!(ka->sa.sa_flags & SA_NOMASK))
486                 sigaddset(&current->blocked, signr);
487         recalc_sigpending();
488         spin_unlock_irq(&current->sighand->siglock);
489 }
490
491 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
492                                    struct sigaction *sa)
493 {
494         switch(regs->u_regs[UREG_I0]) {
495         case ERESTART_RESTARTBLOCK:
496         case ERESTARTNOHAND:
497         no_system_call_restart:
498                 regs->u_regs[UREG_I0] = EINTR;
499                 regs->psr |= PSR_C;
500                 break;
501         case ERESTARTSYS:
502                 if (!(sa->sa_flags & SA_RESTART))
503                         goto no_system_call_restart;
504                 /* fallthrough */
505         case ERESTARTNOINTR:
506                 regs->u_regs[UREG_I0] = orig_i0;
507                 regs->pc -= 4;
508                 regs->npc -= 4;
509         }
510 }
511
512 /* Note that 'init' is a special process: it doesn't get signals it doesn't
513  * want to handle. Thus you cannot kill init even with a SIGKILL even by
514  * mistake.
515  */
516 asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0)
517 {
518         struct k_sigaction ka;
519         int restart_syscall;
520         sigset_t *oldset;
521         siginfo_t info;
522         int signr;
523
524         if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
525                 restart_syscall = 1;
526         else
527                 restart_syscall = 0;
528
529         if (test_thread_flag(TIF_RESTORE_SIGMASK))
530                 oldset = &current->saved_sigmask;
531         else
532                 oldset = &current->blocked;
533
534         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
535
536         /* If the debugger messes with the program counter, it clears
537          * the software "in syscall" bit, directing us to not perform
538          * a syscall restart.
539          */
540         if (restart_syscall && !pt_regs_is_syscall(regs))
541                 restart_syscall = 0;
542
543         if (signr > 0) {
544                 if (restart_syscall)
545                         syscall_restart(orig_i0, regs, &ka.sa);
546                 handle_signal(signr, &ka, &info, oldset, regs);
547
548                 /* a signal was successfully delivered; the saved
549                  * sigmask will have been stored in the signal frame,
550                  * and will be restored by sigreturn, so we can simply
551                  * clear the TIF_RESTORE_SIGMASK flag.
552                  */
553                 if (test_thread_flag(TIF_RESTORE_SIGMASK))
554                         clear_thread_flag(TIF_RESTORE_SIGMASK);
555                 return;
556         }
557         if (restart_syscall &&
558             (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
559              regs->u_regs[UREG_I0] == ERESTARTSYS ||
560              regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
561                 /* replay the system call when we are done */
562                 regs->u_regs[UREG_I0] = orig_i0;
563                 regs->pc -= 4;
564                 regs->npc -= 4;
565         }
566         if (restart_syscall &&
567             regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
568                 regs->u_regs[UREG_G1] = __NR_restart_syscall;
569                 regs->pc -= 4;
570                 regs->npc -= 4;
571         }
572
573         /* if there's no signal to deliver, we just put the saved sigmask
574          * back
575          */
576         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
577                 clear_thread_flag(TIF_RESTORE_SIGMASK);
578                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
579         }
580 }
581
582 asmlinkage int
583 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
584                 unsigned long sp)
585 {
586         int ret = -EFAULT;
587
588         /* First see if old state is wanted. */
589         if (ossptr) {
590                 if (put_user(current->sas_ss_sp + current->sas_ss_size,
591                              &ossptr->the_stack) ||
592                     __put_user(on_sig_stack(sp), &ossptr->cur_status))
593                         goto out;
594         }
595
596         /* Now see if we want to update the new state. */
597         if (ssptr) {
598                 char *ss_sp;
599
600                 if (get_user(ss_sp, &ssptr->the_stack))
601                         goto out;
602                 /* If the current stack was set with sigaltstack, don't
603                    swap stacks while we are on it.  */
604                 ret = -EPERM;
605                 if (current->sas_ss_sp && on_sig_stack(sp))
606                         goto out;
607
608                 /* Since we don't know the extent of the stack, and we don't
609                    track onstack-ness, but rather calculate it, we must
610                    presume a size.  Ho hum this interface is lossy.  */
611                 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
612                 current->sas_ss_size = SIGSTKSZ;
613         }
614         ret = 0;
615 out:
616         return ret;
617 }