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