Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / 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         err |= __get_user(fpu_save, &sf->fpu_save);
149
150         if (fpu_save)
151                 err |= restore_fpu_state(regs, fpu_save);
152
153         /* This is pretty much atomic, no amount locking would prevent
154          * the races which exist anyways.
155          */
156         err |= __get_user(set.sig[0], &sf->info.si_mask);
157         err |= __copy_from_user(&set.sig[1], &sf->extramask,
158                                 (_NSIG_WORDS-1) * sizeof(unsigned int));
159                            
160         if (err)
161                 goto segv_and_exit;
162
163         sigdelsetmask(&set, ~_BLOCKABLE);
164         spin_lock_irq(&current->sighand->siglock);
165         current->blocked = set;
166         recalc_sigpending();
167         spin_unlock_irq(&current->sighand->siglock);
168         return;
169
170 segv_and_exit:
171         force_sig(SIGSEGV, current);
172 }
173
174 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
175 {
176         struct rt_signal_frame __user *sf;
177         unsigned int psr, pc, npc;
178         __siginfo_fpu_t __user *fpu_save;
179         mm_segment_t old_fs;
180         sigset_t set;
181         stack_t st;
182         int err;
183
184         synchronize_user_stack();
185         sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
186         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
187             (((unsigned long) sf) & 0x03))
188                 goto segv;
189
190         err = __get_user(pc, &sf->regs.pc);
191         err |= __get_user(npc, &sf->regs.npc);
192         err |= ((pc | npc) & 0x03);
193
194         err |= __get_user(regs->y, &sf->regs.y);
195         err |= __get_user(psr, &sf->regs.psr);
196
197         err |= __copy_from_user(&regs->u_regs[UREG_G1],
198                                 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
199
200         regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
201
202         err |= __get_user(fpu_save, &sf->fpu_save);
203
204         if (fpu_save)
205                 err |= restore_fpu_state(regs, fpu_save);
206         err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
207         
208         err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
209         
210         if (err)
211                 goto segv;
212                 
213         regs->pc = pc;
214         regs->npc = npc;
215         
216         /* It is more difficult to avoid calling this function than to
217          * call it and ignore errors.
218          */
219         old_fs = get_fs();
220         set_fs(KERNEL_DS);
221         do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
222         set_fs(old_fs);
223
224         sigdelsetmask(&set, ~_BLOCKABLE);
225         spin_lock_irq(&current->sighand->siglock);
226         current->blocked = set;
227         recalc_sigpending();
228         spin_unlock_irq(&current->sighand->siglock);
229         return;
230 segv:
231         force_sig(SIGSEGV, current);
232 }
233
234 /* Checks if the fp is valid */
235 static inline int invalid_frame_pointer(void __user *fp, int fplen)
236 {
237         if ((((unsigned long) fp) & 7) ||
238             !__access_ok((unsigned long)fp, fplen) ||
239             ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
240              ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
241                 return 1;
242         
243         return 0;
244 }
245
246 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
247 {
248         unsigned long sp;
249
250         sp = regs->u_regs[UREG_FP];
251
252         /* This is the X/Open sanctioned signal stack switching.  */
253         if (sa->sa_flags & SA_ONSTACK) {
254                 if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
255                         sp = current->sas_ss_sp + current->sas_ss_size;
256         }
257         return (void __user *)(sp - framesize);
258 }
259
260 static inline int
261 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
262 {
263         int err = 0;
264 #ifdef CONFIG_SMP
265         if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
266                 put_psr(get_psr() | PSR_EF);
267                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
268                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
269                 regs->psr &= ~(PSR_EF);
270                 clear_tsk_thread_flag(current, TIF_USEDFPU);
271         }
272 #else
273         if (current == last_task_used_math) {
274                 put_psr(get_psr() | PSR_EF);
275                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
276                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
277                 last_task_used_math = NULL;
278                 regs->psr &= ~(PSR_EF);
279         }
280 #endif
281         err |= __copy_to_user(&fpu->si_float_regs[0],
282                               &current->thread.float_regs[0],
283                               (sizeof(unsigned long) * 32));
284         err |= __put_user(current->thread.fsr, &fpu->si_fsr);
285         err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
286         if (current->thread.fpqdepth != 0)
287                 err |= __copy_to_user(&fpu->si_fpqueue[0],
288                                       &current->thread.fpqueue[0],
289                                       ((sizeof(unsigned long) +
290                                       (sizeof(unsigned long *)))*16));
291         clear_used_math();
292         return err;
293 }
294
295 static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
296                         int signo, sigset_t *oldset)
297 {
298         struct signal_frame __user *sf;
299         int sigframe_size, err;
300
301         /* 1. Make sure everything is clean */
302         synchronize_user_stack();
303
304         sigframe_size = SF_ALIGNEDSZ;
305         if (!used_math())
306                 sigframe_size -= sizeof(__siginfo_fpu_t);
307
308         sf = (struct signal_frame __user *)
309                 get_sigframe(&ka->sa, regs, sigframe_size);
310
311         if (invalid_frame_pointer(sf, sigframe_size))
312                 goto sigill_and_return;
313
314         if (current_thread_info()->w_saved != 0)
315                 goto sigill_and_return;
316
317         /* 2. Save the current process state */
318         err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
319         
320         err |= __put_user(0, &sf->extra_size);
321
322         if (used_math()) {
323                 err |= save_fpu_state(regs, &sf->fpu_state);
324                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
325         } else {
326                 err |= __put_user(0, &sf->fpu_save);
327         }
328
329         err |= __put_user(oldset->sig[0], &sf->info.si_mask);
330         err |= __copy_to_user(sf->extramask, &oldset->sig[1],
331                               (_NSIG_WORDS - 1) * sizeof(unsigned int));
332         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
333                               sizeof(struct reg_window));
334         if (err)
335                 goto sigsegv;
336         
337         /* 3. signal handler back-trampoline and parameters */
338         regs->u_regs[UREG_FP] = (unsigned long) sf;
339         regs->u_regs[UREG_I0] = signo;
340         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
341         regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
342
343         /* 4. signal handler */
344         regs->pc = (unsigned long) ka->sa.sa_handler;
345         regs->npc = (regs->pc + 4);
346
347         /* 5. return to kernel instructions */
348         if (ka->ka_restorer)
349                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
350         else {
351                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
352
353                 /* mov __NR_sigreturn, %g1 */
354                 err |= __put_user(0x821020d8, &sf->insns[0]);
355
356                 /* t 0x10 */
357                 err |= __put_user(0x91d02010, &sf->insns[1]);
358                 if (err)
359                         goto sigsegv;
360
361                 /* Flush instruction space. */
362                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
363         }
364         return;
365
366 sigill_and_return:
367         do_exit(SIGILL);
368 sigsegv:
369         force_sigsegv(signo, current);
370 }
371
372 static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
373                            int signo, sigset_t *oldset, siginfo_t *info)
374 {
375         struct rt_signal_frame __user *sf;
376         int sigframe_size;
377         unsigned int psr;
378         int err;
379
380         synchronize_user_stack();
381         sigframe_size = RT_ALIGNEDSZ;
382         if (!used_math())
383                 sigframe_size -= sizeof(__siginfo_fpu_t);
384         sf = (struct rt_signal_frame __user *)
385                 get_sigframe(&ka->sa, regs, sigframe_size);
386         if (invalid_frame_pointer(sf, sigframe_size))
387                 goto sigill;
388         if (current_thread_info()->w_saved != 0)
389                 goto sigill;
390
391         err  = __put_user(regs->pc, &sf->regs.pc);
392         err |= __put_user(regs->npc, &sf->regs.npc);
393         err |= __put_user(regs->y, &sf->regs.y);
394         psr = regs->psr;
395         if (used_math())
396                 psr |= PSR_EF;
397         err |= __put_user(psr, &sf->regs.psr);
398         err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
399         err |= __put_user(0, &sf->extra_size);
400
401         if (psr & PSR_EF) {
402                 err |= save_fpu_state(regs, &sf->fpu_state);
403                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
404         } else {
405                 err |= __put_user(0, &sf->fpu_save);
406         }
407         err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
408         
409         /* Setup sigaltstack */
410         err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
411         err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
412         err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
413         
414         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
415                               sizeof(struct reg_window));       
416
417         err |= copy_siginfo_to_user(&sf->info, info);
418
419         if (err)
420                 goto sigsegv;
421
422         regs->u_regs[UREG_FP] = (unsigned long) sf;
423         regs->u_regs[UREG_I0] = signo;
424         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
425         regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
426
427         regs->pc = (unsigned long) ka->sa.sa_handler;
428         regs->npc = (regs->pc + 4);
429
430         if (ka->ka_restorer)
431                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
432         else {
433                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
434
435                 /* mov __NR_sigreturn, %g1 */
436                 err |= __put_user(0x821020d8, &sf->insns[0]);
437
438                 /* t 0x10 */
439                 err |= __put_user(0x91d02010, &sf->insns[1]);
440                 if (err)
441                         goto sigsegv;
442
443                 /* Flush instruction space. */
444                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
445         }
446         return;
447
448 sigill:
449         do_exit(SIGILL);
450 sigsegv:
451         force_sigsegv(signo, current);
452 }
453
454 static inline void
455 handle_signal(unsigned long signr, struct k_sigaction *ka,
456               siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
457 {
458         if (ka->sa.sa_flags & SA_SIGINFO)
459                 setup_rt_frame(ka, regs, signr, oldset, info);
460         else
461                 setup_frame(ka, regs, signr, oldset);
462
463         spin_lock_irq(&current->sighand->siglock);
464         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
465         if (!(ka->sa.sa_flags & SA_NOMASK))
466                 sigaddset(&current->blocked, signr);
467         recalc_sigpending();
468         spin_unlock_irq(&current->sighand->siglock);
469 }
470
471 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
472                                    struct sigaction *sa)
473 {
474         switch(regs->u_regs[UREG_I0]) {
475         case ERESTART_RESTARTBLOCK:
476         case ERESTARTNOHAND:
477         no_system_call_restart:
478                 regs->u_regs[UREG_I0] = EINTR;
479                 regs->psr |= PSR_C;
480                 break;
481         case ERESTARTSYS:
482                 if (!(sa->sa_flags & SA_RESTART))
483                         goto no_system_call_restart;
484                 /* fallthrough */
485         case ERESTARTNOINTR:
486                 regs->u_regs[UREG_I0] = orig_i0;
487                 regs->pc -= 4;
488                 regs->npc -= 4;
489         }
490 }
491
492 /* Note that 'init' is a special process: it doesn't get signals it doesn't
493  * want to handle. Thus you cannot kill init even with a SIGKILL even by
494  * mistake.
495  */
496 asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0, int restart_syscall)
497 {
498         siginfo_t info;
499         struct sparc_deliver_cookie cookie;
500         struct k_sigaction ka;
501         int signr;
502         sigset_t *oldset;
503
504         cookie.restart_syscall = restart_syscall;
505         cookie.orig_i0 = orig_i0;
506
507         if (test_thread_flag(TIF_RESTORE_SIGMASK))
508                 oldset = &current->saved_sigmask;
509         else
510                 oldset = &current->blocked;
511
512         signr = get_signal_to_deliver(&info, &ka, regs, &cookie);
513         if (signr > 0) {
514                 if (cookie.restart_syscall)
515                         syscall_restart(cookie.orig_i0, regs, &ka.sa);
516                 handle_signal(signr, &ka, &info, oldset, regs);
517
518                 /* a signal was successfully delivered; the saved
519                  * sigmask will have been stored in the signal frame,
520                  * and will be restored by sigreturn, so we can simply
521                  * clear the TIF_RESTORE_SIGMASK flag.
522                  */
523                 if (test_thread_flag(TIF_RESTORE_SIGMASK))
524                         clear_thread_flag(TIF_RESTORE_SIGMASK);
525                 return;
526         }
527         if (cookie.restart_syscall &&
528             (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
529              regs->u_regs[UREG_I0] == ERESTARTSYS ||
530              regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
531                 /* replay the system call when we are done */
532                 regs->u_regs[UREG_I0] = cookie.orig_i0;
533                 regs->pc -= 4;
534                 regs->npc -= 4;
535         }
536         if (cookie.restart_syscall &&
537             regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
538                 regs->u_regs[UREG_G1] = __NR_restart_syscall;
539                 regs->pc -= 4;
540                 regs->npc -= 4;
541         }
542
543         /* if there's no signal to deliver, we just put the saved sigmask
544          * back
545          */
546         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
547                 clear_thread_flag(TIF_RESTORE_SIGMASK);
548                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
549         }
550 }
551
552 asmlinkage int
553 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
554                 unsigned long sp)
555 {
556         int ret = -EFAULT;
557
558         /* First see if old state is wanted. */
559         if (ossptr) {
560                 if (put_user(current->sas_ss_sp + current->sas_ss_size,
561                              &ossptr->the_stack) ||
562                     __put_user(on_sig_stack(sp), &ossptr->cur_status))
563                         goto out;
564         }
565
566         /* Now see if we want to update the new state. */
567         if (ssptr) {
568                 char *ss_sp;
569
570                 if (get_user(ss_sp, &ssptr->the_stack))
571                         goto out;
572                 /* If the current stack was set with sigaltstack, don't
573                    swap stacks while we are on it.  */
574                 ret = -EPERM;
575                 if (current->sas_ss_sp && on_sig_stack(sp))
576                         goto out;
577
578                 /* Since we don't know the extent of the stack, and we don't
579                    track onstack-ness, but rather calculate it, we must
580                    presume a size.  Ho hum this interface is lossy.  */
581                 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
582                 current->sas_ss_size = SIGSTKSZ;
583         }
584         ret = 0;
585 out:
586         return ret;
587 }
588
589 void ptrace_signal_deliver(struct pt_regs *regs, void *cookie)
590 {
591         struct sparc_deliver_cookie *cp = cookie;
592
593         if (cp->restart_syscall &&
594             (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
595              regs->u_regs[UREG_I0] == ERESTARTSYS ||
596              regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
597                 /* replay the system call when we are done */
598                 regs->u_regs[UREG_I0] = cp->orig_i0;
599                 regs->pc -= 4;
600                 regs->npc -= 4;
601                 cp->restart_syscall = 0;
602         }
603
604         if (cp->restart_syscall &&
605             regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
606                 regs->u_regs[UREG_G1] = __NR_restart_syscall;
607                 regs->pc -= 4;
608                 regs->npc -= 4;
609                 cp->restart_syscall = 0;
610         }
611 }