Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[linux-2.6] / arch / sparc / kernel / signal.c
1 /*  $Id: signal.c,v 1.110 2002/02/08 03:57:14 davem Exp $
2  *  linux/arch/sparc/kernel/signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6  *  Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
7  *  Copyright (C) 1997 Eddie C. Dost   (ecd@skynet.be)
8  */
9
10 #include <linux/config.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/mm.h>
19 #include <linux/tty.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/binfmts.h>      /* do_coredum */
23 #include <linux/bitops.h>
24
25 #include <asm/uaccess.h>
26 #include <asm/ptrace.h>
27 #include <asm/svr4.h>
28 #include <asm/pgalloc.h>
29 #include <asm/pgtable.h>
30 #include <asm/cacheflush.h>     /* flush_sig_insns */
31
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
34 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
35                    void *fpqueue, unsigned long *fpqdepth);
36 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
37
38 /* Signal frames: the original one (compatible with SunOS):
39  *
40  * Set up a signal frame... Make the stack look the way SunOS
41  * expects it to look which is basically:
42  *
43  * ---------------------------------- <-- %sp at signal time
44  * Struct sigcontext
45  * Signal address
46  * Ptr to sigcontext area above
47  * Signal code
48  * The signal number itself
49  * One register window
50  * ---------------------------------- <-- New %sp
51  */
52 struct signal_sframe {
53         struct reg_window       sig_window;
54         int                     sig_num;
55         int                     sig_code;
56         struct sigcontext __user *sig_scptr;
57         int                     sig_address;
58         struct sigcontext       sig_context;
59         unsigned int            extramask[_NSIG_WORDS - 1];
60 };
61
62 /* 
63  * And the new one, intended to be used for Linux applications only
64  * (we have enough in there to work with clone).
65  * All the interesting bits are in the info field.
66  */
67
68 struct new_signal_frame {
69         struct sparc_stackf     ss;
70         __siginfo_t             info;
71         __siginfo_fpu_t __user  *fpu_save;
72         unsigned long           insns[2] __attribute__ ((aligned (8)));
73         unsigned int            extramask[_NSIG_WORDS - 1];
74         unsigned int            extra_size; /* Should be 0 */
75         __siginfo_fpu_t         fpu_state;
76 };
77
78 struct rt_signal_frame {
79         struct sparc_stackf     ss;
80         siginfo_t               info;
81         struct pt_regs          regs;
82         sigset_t                mask;
83         __siginfo_fpu_t __user  *fpu_save;
84         unsigned int            insns[2];
85         stack_t                 stack;
86         unsigned int            extra_size; /* Should be 0 */
87         __siginfo_fpu_t         fpu_state;
88 };
89
90 /* Align macros */
91 #define SF_ALIGNEDSZ  (((sizeof(struct signal_sframe) + 7) & (~7)))
92 #define NF_ALIGNEDSZ  (((sizeof(struct new_signal_frame) + 7) & (~7)))
93 #define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame) + 7) & (~7)))
94
95 static int _sigpause_common(old_sigset_t set)
96 {
97         set &= _BLOCKABLE;
98         spin_lock_irq(&current->sighand->siglock);
99         current->saved_sigmask = current->blocked;
100         siginitset(&current->blocked, set);
101         recalc_sigpending();
102         spin_unlock_irq(&current->sighand->siglock);
103
104         current->state = TASK_INTERRUPTIBLE;
105         schedule();
106         set_thread_flag(TIF_RESTORE_SIGMASK);
107
108         return -ERESTARTNOHAND;
109 }
110
111 asmlinkage int sys_sigpause(unsigned int set)
112 {
113         return _sigpause_common(set);
114 }
115
116 asmlinkage int sys_sigsuspend(old_sigset_t set)
117 {
118         return _sigpause_common(set);
119 }
120
121 static inline int
122 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
123 {
124         int err;
125 #ifdef CONFIG_SMP
126         if (test_tsk_thread_flag(current, TIF_USEDFPU))
127                 regs->psr &= ~PSR_EF;
128 #else
129         if (current == last_task_used_math) {
130                 last_task_used_math = NULL;
131                 regs->psr &= ~PSR_EF;
132         }
133 #endif
134         set_used_math();
135         clear_tsk_thread_flag(current, TIF_USEDFPU);
136
137         if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
138                 return -EFAULT;
139
140         err = __copy_from_user(&current->thread.float_regs[0], &fpu->si_float_regs[0],
141                                (sizeof(unsigned long) * 32));
142         err |= __get_user(current->thread.fsr, &fpu->si_fsr);
143         err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
144         if (current->thread.fpqdepth != 0)
145                 err |= __copy_from_user(&current->thread.fpqueue[0],
146                                         &fpu->si_fpqueue[0],
147                                         ((sizeof(unsigned long) +
148                                         (sizeof(unsigned long *)))*16));
149         return err;
150 }
151
152 static inline void do_new_sigreturn (struct pt_regs *regs)
153 {
154         struct new_signal_frame __user *sf;
155         unsigned long up_psr, pc, npc;
156         sigset_t set;
157         __siginfo_fpu_t __user *fpu_save;
158         int err;
159
160         sf = (struct new_signal_frame __user *) regs->u_regs[UREG_FP];
161
162         /* 1. Make sure we are not getting garbage from the user */
163         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
164                 goto segv_and_exit;
165
166         if (((unsigned long) sf) & 3)
167                 goto segv_and_exit;
168
169         err = __get_user(pc,  &sf->info.si_regs.pc);
170         err |= __get_user(npc, &sf->info.si_regs.npc);
171
172         if ((pc | npc) & 3)
173                 goto segv_and_exit;
174
175         /* 2. Restore the state */
176         up_psr = regs->psr;
177         err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
178
179         /* User can only change condition codes and FPU enabling in %psr. */
180         regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
181                   | (regs->psr & (PSR_ICC | PSR_EF));
182
183         err |= __get_user(fpu_save, &sf->fpu_save);
184
185         if (fpu_save)
186                 err |= restore_fpu_state(regs, fpu_save);
187
188         /* This is pretty much atomic, no amount locking would prevent
189          * the races which exist anyways.
190          */
191         err |= __get_user(set.sig[0], &sf->info.si_mask);
192         err |= __copy_from_user(&set.sig[1], &sf->extramask,
193                                 (_NSIG_WORDS-1) * sizeof(unsigned int));
194                            
195         if (err)
196                 goto segv_and_exit;
197
198         sigdelsetmask(&set, ~_BLOCKABLE);
199         spin_lock_irq(&current->sighand->siglock);
200         current->blocked = set;
201         recalc_sigpending();
202         spin_unlock_irq(&current->sighand->siglock);
203         return;
204
205 segv_and_exit:
206         force_sig(SIGSEGV, current);
207 }
208
209 asmlinkage void do_sigreturn(struct pt_regs *regs)
210 {
211         struct sigcontext __user *scptr;
212         unsigned long pc, npc, psr;
213         sigset_t set;
214         int err;
215
216         /* Always make any pending restarted system calls return -EINTR */
217         current_thread_info()->restart_block.fn = do_no_restart_syscall;
218
219         synchronize_user_stack();
220
221         if (current->thread.new_signal) {
222                 do_new_sigreturn(regs);
223                 return;
224         }
225
226         scptr = (struct sigcontext __user *) regs->u_regs[UREG_I0];
227
228         /* Check sanity of the user arg. */
229         if (!access_ok(VERIFY_READ, scptr, sizeof(struct sigcontext)) ||
230             (((unsigned long) scptr) & 3))
231                 goto segv_and_exit;
232
233         err = __get_user(pc, &scptr->sigc_pc);
234         err |= __get_user(npc, &scptr->sigc_npc);
235
236         if ((pc | npc) & 3)
237                 goto segv_and_exit;
238
239         /* This is pretty much atomic, no amount locking would prevent
240          * the races which exist anyways.
241          */
242         err |= __get_user(set.sig[0], &scptr->sigc_mask);
243         /* Note that scptr + 1 points to extramask */
244         err |= __copy_from_user(&set.sig[1], scptr + 1,
245                                 (_NSIG_WORDS - 1) * sizeof(unsigned int));
246         
247         if (err)
248                 goto segv_and_exit;
249
250         sigdelsetmask(&set, ~_BLOCKABLE);
251         spin_lock_irq(&current->sighand->siglock);
252         current->blocked = set;
253         recalc_sigpending();
254         spin_unlock_irq(&current->sighand->siglock);
255
256         regs->pc = pc;
257         regs->npc = npc;
258
259         err = __get_user(regs->u_regs[UREG_FP], &scptr->sigc_sp);
260         err |= __get_user(regs->u_regs[UREG_I0], &scptr->sigc_o0);
261         err |= __get_user(regs->u_regs[UREG_G1], &scptr->sigc_g1);
262
263         /* User can only change condition codes in %psr. */
264         err |= __get_user(psr, &scptr->sigc_psr);
265         if (err)
266                 goto segv_and_exit;
267                 
268         regs->psr &= ~(PSR_ICC);
269         regs->psr |= (psr & PSR_ICC);
270         return;
271
272 segv_and_exit:
273         force_sig(SIGSEGV, current);
274 }
275
276 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
277 {
278         struct rt_signal_frame __user *sf;
279         unsigned int psr, pc, npc;
280         __siginfo_fpu_t __user *fpu_save;
281         mm_segment_t old_fs;
282         sigset_t set;
283         stack_t st;
284         int err;
285
286         synchronize_user_stack();
287         sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
288         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
289             (((unsigned long) sf) & 0x03))
290                 goto segv;
291
292         err = __get_user(pc, &sf->regs.pc);
293         err |= __get_user(npc, &sf->regs.npc);
294         err |= ((pc | npc) & 0x03);
295
296         err |= __get_user(regs->y, &sf->regs.y);
297         err |= __get_user(psr, &sf->regs.psr);
298
299         err |= __copy_from_user(&regs->u_regs[UREG_G1],
300                                 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
301
302         regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
303
304         err |= __get_user(fpu_save, &sf->fpu_save);
305
306         if (fpu_save)
307                 err |= restore_fpu_state(regs, fpu_save);
308         err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
309         
310         err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
311         
312         if (err)
313                 goto segv;
314                 
315         regs->pc = pc;
316         regs->npc = npc;
317         
318         /* It is more difficult to avoid calling this function than to
319          * call it and ignore errors.
320          */
321         old_fs = get_fs();
322         set_fs(KERNEL_DS);
323         do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
324         set_fs(old_fs);
325
326         sigdelsetmask(&set, ~_BLOCKABLE);
327         spin_lock_irq(&current->sighand->siglock);
328         current->blocked = set;
329         recalc_sigpending();
330         spin_unlock_irq(&current->sighand->siglock);
331         return;
332 segv:
333         force_sig(SIGSEGV, current);
334 }
335
336 /* Checks if the fp is valid */
337 static inline int invalid_frame_pointer(void __user *fp, int fplen)
338 {
339         if ((((unsigned long) fp) & 7) ||
340             !__access_ok((unsigned long)fp, fplen) ||
341             ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
342              ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
343                 return 1;
344         
345         return 0;
346 }
347
348 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
349 {
350         unsigned long sp;
351
352         sp = regs->u_regs[UREG_FP];
353
354         /* This is the X/Open sanctioned signal stack switching.  */
355         if (sa->sa_flags & SA_ONSTACK) {
356                 if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
357                         sp = current->sas_ss_sp + current->sas_ss_size;
358         }
359         return (void __user *)(sp - framesize);
360 }
361
362 static inline void
363 setup_frame(struct sigaction *sa, struct pt_regs *regs, int signr, sigset_t *oldset, siginfo_t *info)
364 {
365         struct signal_sframe __user *sframep;
366         struct sigcontext __user *sc;
367         int window = 0, err;
368         unsigned long pc = regs->pc;
369         unsigned long npc = regs->npc;
370         struct thread_info *tp = current_thread_info();
371         void __user *sig_address;
372         int sig_code;
373
374         synchronize_user_stack();
375         sframep = (struct signal_sframe __user *)
376                 get_sigframe(sa, regs, SF_ALIGNEDSZ);
377         if (invalid_frame_pointer(sframep, sizeof(*sframep))){
378                 /* Don't change signal code and address, so that
379                  * post mortem debuggers can have a look.
380                  */
381                 goto sigill_and_return;
382         }
383
384         sc = &sframep->sig_context;
385
386         /* We've already made sure frame pointer isn't in kernel space... */
387         err  = __put_user((sas_ss_flags(regs->u_regs[UREG_FP]) == SS_ONSTACK),
388                          &sc->sigc_onstack);
389         err |= __put_user(oldset->sig[0], &sc->sigc_mask);
390         err |= __copy_to_user(sframep->extramask, &oldset->sig[1],
391                               (_NSIG_WORDS - 1) * sizeof(unsigned int));
392         err |= __put_user(regs->u_regs[UREG_FP], &sc->sigc_sp);
393         err |= __put_user(pc, &sc->sigc_pc);
394         err |= __put_user(npc, &sc->sigc_npc);
395         err |= __put_user(regs->psr, &sc->sigc_psr);
396         err |= __put_user(regs->u_regs[UREG_G1], &sc->sigc_g1);
397         err |= __put_user(regs->u_regs[UREG_I0], &sc->sigc_o0);
398         err |= __put_user(tp->w_saved, &sc->sigc_oswins);
399         if (tp->w_saved)
400                 for (window = 0; window < tp->w_saved; window++) {
401                         put_user((char *)tp->rwbuf_stkptrs[window],
402                                  &sc->sigc_spbuf[window]);
403                         err |= __copy_to_user(&sc->sigc_wbuf[window],
404                                               &tp->reg_window[window],
405                                               sizeof(struct reg_window));
406                 }
407         else
408                 err |= __copy_to_user(sframep, (char *) regs->u_regs[UREG_FP],
409                                       sizeof(struct reg_window));
410
411         tp->w_saved = 0; /* So process is allowed to execute. */
412
413         err |= __put_user(signr, &sframep->sig_num);
414         sig_address = NULL;
415         sig_code = 0;
416         if (SI_FROMKERNEL (info) && (info->si_code & __SI_MASK) == __SI_FAULT) {
417                 sig_address = info->si_addr;
418                 switch (signr) {
419                 case SIGSEGV:
420                         switch (info->si_code) {
421                         case SEGV_MAPERR: sig_code = SUBSIG_NOMAPPING; break;
422                         default: sig_code = SUBSIG_PROTECTION; break;
423                         }
424                         break;
425                 case SIGILL:
426                         switch (info->si_code) {
427                         case ILL_ILLOPC: sig_code = SUBSIG_ILLINST; break;
428                         case ILL_PRVOPC: sig_code = SUBSIG_PRIVINST; break;
429                         case ILL_ILLTRP: sig_code = SUBSIG_BADTRAP(info->si_trapno); break;
430                         default: sig_code = SUBSIG_STACK; break;
431                         }
432                         break;
433                 case SIGFPE:
434                         switch (info->si_code) {
435                         case FPE_INTDIV: sig_code = SUBSIG_IDIVZERO; break;
436                         case FPE_INTOVF: sig_code = SUBSIG_FPINTOVFL; break;
437                         case FPE_FLTDIV: sig_code = SUBSIG_FPDIVZERO; break;
438                         case FPE_FLTOVF: sig_code = SUBSIG_FPOVFLOW; break;
439                         case FPE_FLTUND: sig_code = SUBSIG_FPUNFLOW; break;
440                         case FPE_FLTRES: sig_code = SUBSIG_FPINEXACT; break;
441                         case FPE_FLTINV: sig_code = SUBSIG_FPOPERROR; break;
442                         default: sig_code = SUBSIG_FPERROR; break;
443                         }
444                         break;
445                 case SIGBUS:
446                         switch (info->si_code) {
447                         case BUS_ADRALN: sig_code = SUBSIG_ALIGNMENT; break;
448                         case BUS_ADRERR: sig_code = SUBSIG_MISCERROR; break;
449                         default: sig_code = SUBSIG_BUSTIMEOUT; break;
450                         }
451                         break;
452                 case SIGEMT:
453                         switch (info->si_code) {
454                         case EMT_TAGOVF: sig_code = SUBSIG_TAG; break;
455                         }
456                         break;
457                 case SIGSYS:
458                         if (info->si_code == (__SI_FAULT|0x100)) {
459                                 /* See sys_sunos.c */
460                                 sig_code = info->si_trapno;
461                                 break;
462                         }
463                 default:
464                         sig_address = NULL;
465                 }
466         }
467         err |= __put_user((unsigned long)sig_address, &sframep->sig_address);
468         err |= __put_user(sig_code, &sframep->sig_code);
469         err |= __put_user(sc, &sframep->sig_scptr);
470         if (err)
471                 goto sigsegv;
472
473         regs->u_regs[UREG_FP] = (unsigned long) sframep;
474         regs->pc = (unsigned long) sa->sa_handler;
475         regs->npc = (regs->pc + 4);
476         return;
477
478 sigill_and_return:
479         do_exit(SIGILL);
480 sigsegv:
481         force_sigsegv(signr, current);
482 }
483
484
485 static inline int
486 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
487 {
488         int err = 0;
489 #ifdef CONFIG_SMP
490         if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
491                 put_psr(get_psr() | PSR_EF);
492                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
493                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
494                 regs->psr &= ~(PSR_EF);
495                 clear_tsk_thread_flag(current, TIF_USEDFPU);
496         }
497 #else
498         if (current == last_task_used_math) {
499                 put_psr(get_psr() | PSR_EF);
500                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
501                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
502                 last_task_used_math = NULL;
503                 regs->psr &= ~(PSR_EF);
504         }
505 #endif
506         err |= __copy_to_user(&fpu->si_float_regs[0],
507                               &current->thread.float_regs[0],
508                               (sizeof(unsigned long) * 32));
509         err |= __put_user(current->thread.fsr, &fpu->si_fsr);
510         err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
511         if (current->thread.fpqdepth != 0)
512                 err |= __copy_to_user(&fpu->si_fpqueue[0],
513                                       &current->thread.fpqueue[0],
514                                       ((sizeof(unsigned long) +
515                                       (sizeof(unsigned long *)))*16));
516         clear_used_math();
517         return err;
518 }
519
520 static inline void
521 new_setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
522                 int signo, sigset_t *oldset)
523 {
524         struct new_signal_frame __user *sf;
525         int sigframe_size, err;
526
527         /* 1. Make sure everything is clean */
528         synchronize_user_stack();
529
530         sigframe_size = NF_ALIGNEDSZ;
531         if (!used_math())
532                 sigframe_size -= sizeof(__siginfo_fpu_t);
533
534         sf = (struct new_signal_frame __user *)
535                 get_sigframe(&ka->sa, regs, sigframe_size);
536
537         if (invalid_frame_pointer(sf, sigframe_size))
538                 goto sigill_and_return;
539
540         if (current_thread_info()->w_saved != 0)
541                 goto sigill_and_return;
542
543         /* 2. Save the current process state */
544         err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
545         
546         err |= __put_user(0, &sf->extra_size);
547
548         if (used_math()) {
549                 err |= save_fpu_state(regs, &sf->fpu_state);
550                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
551         } else {
552                 err |= __put_user(0, &sf->fpu_save);
553         }
554
555         err |= __put_user(oldset->sig[0], &sf->info.si_mask);
556         err |= __copy_to_user(sf->extramask, &oldset->sig[1],
557                               (_NSIG_WORDS - 1) * sizeof(unsigned int));
558         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
559                               sizeof(struct reg_window));
560         if (err)
561                 goto sigsegv;
562         
563         /* 3. signal handler back-trampoline and parameters */
564         regs->u_regs[UREG_FP] = (unsigned long) sf;
565         regs->u_regs[UREG_I0] = signo;
566         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
567         regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
568
569         /* 4. signal handler */
570         regs->pc = (unsigned long) ka->sa.sa_handler;
571         regs->npc = (regs->pc + 4);
572
573         /* 5. return to kernel instructions */
574         if (ka->ka_restorer)
575                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
576         else {
577                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
578
579                 /* mov __NR_sigreturn, %g1 */
580                 err |= __put_user(0x821020d8, &sf->insns[0]);
581
582                 /* t 0x10 */
583                 err |= __put_user(0x91d02010, &sf->insns[1]);
584                 if (err)
585                         goto sigsegv;
586
587                 /* Flush instruction space. */
588                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
589         }
590         return;
591
592 sigill_and_return:
593         do_exit(SIGILL);
594 sigsegv:
595         force_sigsegv(signo, current);
596 }
597
598 static inline void
599 new_setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
600                    int signo, sigset_t *oldset, siginfo_t *info)
601 {
602         struct rt_signal_frame __user *sf;
603         int sigframe_size;
604         unsigned int psr;
605         int err;
606
607         synchronize_user_stack();
608         sigframe_size = RT_ALIGNEDSZ;
609         if (!used_math())
610                 sigframe_size -= sizeof(__siginfo_fpu_t);
611         sf = (struct rt_signal_frame __user *)
612                 get_sigframe(&ka->sa, regs, sigframe_size);
613         if (invalid_frame_pointer(sf, sigframe_size))
614                 goto sigill;
615         if (current_thread_info()->w_saved != 0)
616                 goto sigill;
617
618         err  = __put_user(regs->pc, &sf->regs.pc);
619         err |= __put_user(regs->npc, &sf->regs.npc);
620         err |= __put_user(regs->y, &sf->regs.y);
621         psr = regs->psr;
622         if (used_math())
623                 psr |= PSR_EF;
624         err |= __put_user(psr, &sf->regs.psr);
625         err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
626         err |= __put_user(0, &sf->extra_size);
627
628         if (psr & PSR_EF) {
629                 err |= save_fpu_state(regs, &sf->fpu_state);
630                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
631         } else {
632                 err |= __put_user(0, &sf->fpu_save);
633         }
634         err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
635         
636         /* Setup sigaltstack */
637         err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
638         err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
639         err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
640         
641         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
642                               sizeof(struct reg_window));       
643
644         err |= copy_siginfo_to_user(&sf->info, info);
645
646         if (err)
647                 goto sigsegv;
648
649         regs->u_regs[UREG_FP] = (unsigned long) sf;
650         regs->u_regs[UREG_I0] = signo;
651         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
652         regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
653
654         regs->pc = (unsigned long) ka->sa.sa_handler;
655         regs->npc = (regs->pc + 4);
656
657         if (ka->ka_restorer)
658                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
659         else {
660                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
661
662                 /* mov __NR_sigreturn, %g1 */
663                 err |= __put_user(0x821020d8, &sf->insns[0]);
664
665                 /* t 0x10 */
666                 err |= __put_user(0x91d02010, &sf->insns[1]);
667                 if (err)
668                         goto sigsegv;
669
670                 /* Flush instruction space. */
671                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
672         }
673         return;
674
675 sigill:
676         do_exit(SIGILL);
677 sigsegv:
678         force_sigsegv(signo, current);
679 }
680
681 /* Setup a Solaris stack frame */
682 static inline void
683 setup_svr4_frame(struct sigaction *sa, unsigned long pc, unsigned long npc,
684                  struct pt_regs *regs, int signr, sigset_t *oldset)
685 {
686         svr4_signal_frame_t __user *sfp;
687         svr4_gregset_t  __user *gr;
688         svr4_siginfo_t  __user *si;
689         svr4_mcontext_t __user *mc;
690         svr4_gwindows_t __user *gw;
691         svr4_ucontext_t __user *uc;
692         svr4_sigset_t   setv;
693         struct thread_info *tp = current_thread_info();
694         int window = 0, err;
695
696         synchronize_user_stack();
697         sfp = (svr4_signal_frame_t __user *)
698                 get_sigframe(sa, regs, SVR4_SF_ALIGNED + sizeof(struct reg_window));
699
700         if (invalid_frame_pointer(sfp, sizeof(*sfp)))
701                 goto sigill_and_return;
702
703         /* Start with a clean frame pointer and fill it */
704         err = __clear_user(sfp, sizeof(*sfp));
705
706         /* Setup convenience variables */
707         si = &sfp->si;
708         uc = &sfp->uc;
709         gw = &sfp->gw;
710         mc = &uc->mcontext;
711         gr = &mc->greg;
712         
713         /* FIXME: where am I supposed to put this?
714          * sc->sigc_onstack = old_status;
715          * anyways, it does not look like it is used for anything at all.
716          */
717         setv.sigbits[0] = oldset->sig[0];
718         setv.sigbits[1] = oldset->sig[1];
719         if (_NSIG_WORDS >= 4) {
720                 setv.sigbits[2] = oldset->sig[2];
721                 setv.sigbits[3] = oldset->sig[3];
722                 err |= __copy_to_user(&uc->sigmask, &setv, sizeof(svr4_sigset_t));
723         } else
724                 err |= __copy_to_user(&uc->sigmask, &setv,
725                                       2 * sizeof(unsigned int));
726
727         /* Store registers */
728         err |= __put_user(regs->pc, &((*gr)[SVR4_PC]));
729         err |= __put_user(regs->npc, &((*gr)[SVR4_NPC]));
730         err |= __put_user(regs->psr, &((*gr)[SVR4_PSR]));
731         err |= __put_user(regs->y, &((*gr)[SVR4_Y]));
732         
733         /* Copy g[1..7] and o[0..7] registers */
734         err |= __copy_to_user(&(*gr)[SVR4_G1], &regs->u_regs[UREG_G1],
735                               sizeof(long) * 7);
736         err |= __copy_to_user(&(*gr)[SVR4_O0], &regs->u_regs[UREG_I0],
737                               sizeof(long) * 8);
738
739         /* Setup sigaltstack */
740         err |= __put_user(current->sas_ss_sp, &uc->stack.sp);
741         err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &uc->stack.flags);
742         err |= __put_user(current->sas_ss_size, &uc->stack.size);
743
744         /* Save the currently window file: */
745
746         /* 1. Link sfp->uc->gwins to our windows */
747         err |= __put_user(gw, &mc->gwin);
748             
749         /* 2. Number of windows to restore at setcontext(): */
750         err |= __put_user(tp->w_saved, &gw->count);
751
752         /* 3. Save each valid window
753          *    Currently, it makes a copy of the windows from the kernel copy.
754          *    David's code for SunOS, makes the copy but keeps the pointer to
755          *    the kernel.  My version makes the pointer point to a userland 
756          *    copy of those.  Mhm, I wonder if I shouldn't just ignore those
757          *    on setcontext and use those that are on the kernel, the signal
758          *    handler should not be modyfing those, mhm.
759          *
760          *    These windows are just used in case synchronize_user_stack failed
761          *    to flush the user windows.
762          */
763         for (window = 0; window < tp->w_saved; window++) {
764                 err |= __put_user((int __user *) &(gw->win[window]), &gw->winptr[window]);
765                 err |= __copy_to_user(&gw->win[window],
766                                       &tp->reg_window[window],
767                                       sizeof(svr4_rwindow_t));
768                 err |= __put_user(0, gw->winptr[window]);
769         }
770
771         /* 4. We just pay attention to the gw->count field on setcontext */
772         tp->w_saved = 0; /* So process is allowed to execute. */
773
774         /* Setup the signal information.  Solaris expects a bunch of
775          * information to be passed to the signal handler, we don't provide
776          * that much currently, should use siginfo.
777          */
778         err |= __put_user(signr, &si->siginfo.signo);
779         err |= __put_user(SVR4_SINOINFO, &si->siginfo.code);
780         if (err)
781                 goto sigsegv;
782
783         regs->u_regs[UREG_FP] = (unsigned long) sfp;
784         regs->pc = (unsigned long) sa->sa_handler;
785         regs->npc = (regs->pc + 4);
786
787         /* Arguments passed to signal handler */
788         if (regs->u_regs[14]){
789                 struct reg_window __user *rw = (struct reg_window __user *)
790                         regs->u_regs[14];
791
792                 err |= __put_user(signr, &rw->ins[0]);
793                 err |= __put_user(si, &rw->ins[1]);
794                 err |= __put_user(uc, &rw->ins[2]);
795                 err |= __put_user(sfp, &rw->ins[6]);    /* frame pointer */
796                 if (err)
797                         goto sigsegv;
798
799                 regs->u_regs[UREG_I0] = signr;
800                 regs->u_regs[UREG_I1] = (unsigned long) si;
801                 regs->u_regs[UREG_I2] = (unsigned long) uc;
802         }
803         return;
804
805 sigill_and_return:
806         do_exit(SIGILL);
807 sigsegv:
808         force_sigsegv(signr, current);
809 }
810
811 asmlinkage int svr4_getcontext(svr4_ucontext_t __user *uc, struct pt_regs *regs)
812 {
813         svr4_gregset_t  __user *gr;
814         svr4_mcontext_t __user *mc;
815         svr4_sigset_t   setv;
816         int err = 0;
817
818         synchronize_user_stack();
819
820         if (current_thread_info()->w_saved)
821                 return -EFAULT;
822
823         err = clear_user(uc, sizeof(*uc));
824         if (err)
825                 return -EFAULT;
826
827         /* Setup convenience variables */
828         mc = &uc->mcontext;
829         gr = &mc->greg;
830
831         setv.sigbits[0] = current->blocked.sig[0];
832         setv.sigbits[1] = current->blocked.sig[1];
833         if (_NSIG_WORDS >= 4) {
834                 setv.sigbits[2] = current->blocked.sig[2];
835                 setv.sigbits[3] = current->blocked.sig[3];
836                 err |= __copy_to_user(&uc->sigmask, &setv, sizeof(svr4_sigset_t));
837         } else
838                 err |= __copy_to_user(&uc->sigmask, &setv,
839                                       2 * sizeof(unsigned int));
840
841         /* Store registers */
842         err |= __put_user(regs->pc, &uc->mcontext.greg[SVR4_PC]);
843         err |= __put_user(regs->npc, &uc->mcontext.greg[SVR4_NPC]);
844         err |= __put_user(regs->psr, &uc->mcontext.greg[SVR4_PSR]);
845         err |= __put_user(regs->y, &uc->mcontext.greg[SVR4_Y]);
846         
847         /* Copy g[1..7] and o[0..7] registers */
848         err |= __copy_to_user(&(*gr)[SVR4_G1], &regs->u_regs[UREG_G1],
849                               sizeof(uint) * 7);
850         err |= __copy_to_user(&(*gr)[SVR4_O0], &regs->u_regs[UREG_I0],
851                               sizeof(uint) * 8);
852
853         /* Setup sigaltstack */
854         err |= __put_user(current->sas_ss_sp, &uc->stack.sp);
855         err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &uc->stack.flags);
856         err |= __put_user(current->sas_ss_size, &uc->stack.size);
857
858         /* The register file is not saved
859          * we have already stuffed all of it with sync_user_stack
860          */
861         return (err ? -EFAULT : 0);
862 }
863
864 /* Set the context for a svr4 application, this is Solaris way to sigreturn */
865 asmlinkage int svr4_setcontext(svr4_ucontext_t __user *c, struct pt_regs *regs)
866 {
867         svr4_gregset_t  __user *gr;
868         unsigned long pc, npc, psr;
869         mm_segment_t old_fs;
870         sigset_t set;
871         svr4_sigset_t setv;
872         int err;
873         stack_t st;
874         
875         /* Fixme: restore windows, or is this already taken care of in
876          * svr4_setup_frame when sync_user_windows is done?
877          */
878         flush_user_windows();
879
880         if (current_thread_info()->w_saved)
881                 goto sigsegv_and_return;
882
883         if (((unsigned long) c) & 3)
884                 goto sigsegv_and_return;
885
886         if (!__access_ok((unsigned long)c, sizeof(*c)))
887                 goto sigsegv_and_return;
888
889         /* Check for valid PC and nPC */
890         gr = &c->mcontext.greg;
891         err = __get_user(pc, &((*gr)[SVR4_PC]));
892         err |= __get_user(npc, &((*gr)[SVR4_NPC]));
893
894         if ((pc | npc) & 3)
895                 goto sigsegv_and_return;
896
897         /* Retrieve information from passed ucontext */
898         /* note that nPC is ored a 1, this is used to inform entry.S */
899         /* that we don't want it to mess with our PC and nPC */
900
901         /* This is pretty much atomic, no amount locking would prevent
902          * the races which exist anyways.
903          */
904         err |= __copy_from_user(&setv, &c->sigmask, sizeof(svr4_sigset_t));
905         
906         err |= __get_user(st.ss_sp, &c->stack.sp);
907         err |= __get_user(st.ss_flags, &c->stack.flags);
908         err |= __get_user(st.ss_size, &c->stack.size);
909         
910         if (err)
911                 goto sigsegv_and_return;
912                 
913         /* It is more difficult to avoid calling this function than to
914            call it and ignore errors.  */
915         old_fs = get_fs();
916         set_fs(KERNEL_DS);
917         do_sigaltstack((const stack_t __user *) &st, NULL,
918                        regs->u_regs[UREG_I6]);
919         set_fs(old_fs);
920         
921         set.sig[0] = setv.sigbits[0];
922         set.sig[1] = setv.sigbits[1];
923         if (_NSIG_WORDS >= 4) {
924                 set.sig[2] = setv.sigbits[2];
925                 set.sig[3] = setv.sigbits[3];
926         }
927         sigdelsetmask(&set, ~_BLOCKABLE);
928         spin_lock_irq(&current->sighand->siglock);
929         current->blocked = set;
930         recalc_sigpending();
931         spin_unlock_irq(&current->sighand->siglock);
932         regs->pc = pc;
933         regs->npc = npc | 1;
934         err |= __get_user(regs->y, &((*gr)[SVR4_Y]));
935         err |= __get_user(psr, &((*gr)[SVR4_PSR]));
936         regs->psr &= ~(PSR_ICC);
937         regs->psr |= (psr & PSR_ICC);
938
939         /* Restore g[1..7] and o[0..7] registers */
940         err |= __copy_from_user(&regs->u_regs[UREG_G1], &(*gr)[SVR4_G1],
941                               sizeof(long) * 7);
942         err |= __copy_from_user(&regs->u_regs[UREG_I0], &(*gr)[SVR4_O0],
943                               sizeof(long) * 8);
944         return (err ? -EFAULT : 0);
945
946 sigsegv_and_return:
947         force_sig(SIGSEGV, current);
948         return -EFAULT;
949 }
950
951 static inline void
952 handle_signal(unsigned long signr, struct k_sigaction *ka,
953               siginfo_t *info, sigset_t *oldset, struct pt_regs *regs,
954               int svr4_signal)
955 {
956         if (svr4_signal)
957                 setup_svr4_frame(&ka->sa, regs->pc, regs->npc, regs, signr, oldset);
958         else {
959                 if (ka->sa.sa_flags & SA_SIGINFO)
960                         new_setup_rt_frame(ka, regs, signr, oldset, info);
961                 else if (current->thread.new_signal)
962                         new_setup_frame(ka, regs, signr, oldset);
963                 else
964                         setup_frame(&ka->sa, regs, signr, oldset, info);
965         }
966         spin_lock_irq(&current->sighand->siglock);
967         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
968         if (!(ka->sa.sa_flags & SA_NOMASK))
969                 sigaddset(&current->blocked, signr);
970         recalc_sigpending();
971         spin_unlock_irq(&current->sighand->siglock);
972 }
973
974 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
975                                    struct sigaction *sa)
976 {
977         switch(regs->u_regs[UREG_I0]) {
978         case ERESTART_RESTARTBLOCK:
979         case ERESTARTNOHAND:
980         no_system_call_restart:
981                 regs->u_regs[UREG_I0] = EINTR;
982                 regs->psr |= PSR_C;
983                 break;
984         case ERESTARTSYS:
985                 if (!(sa->sa_flags & SA_RESTART))
986                         goto no_system_call_restart;
987                 /* fallthrough */
988         case ERESTARTNOINTR:
989                 regs->u_regs[UREG_I0] = orig_i0;
990                 regs->pc -= 4;
991                 regs->npc -= 4;
992         }
993 }
994
995 /* Note that 'init' is a special process: it doesn't get signals it doesn't
996  * want to handle. Thus you cannot kill init even with a SIGKILL even by
997  * mistake.
998  */
999 asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0, int restart_syscall)
1000 {
1001         siginfo_t info;
1002         struct sparc_deliver_cookie cookie;
1003         struct k_sigaction ka;
1004         int signr;
1005         sigset_t *oldset;
1006
1007         /*
1008          * XXX Disable svr4 signal handling until solaris emulation works.
1009          * It is buggy - Anton
1010          */
1011 #define SVR4_SIGNAL_BROKEN 1
1012 #ifdef SVR4_SIGNAL_BROKEN
1013         int svr4_signal = 0;
1014 #else
1015         int svr4_signal = current->personality == PER_SVR4;
1016 #endif
1017
1018         cookie.restart_syscall = restart_syscall;
1019         cookie.orig_i0 = orig_i0;
1020
1021         if (test_thread_flag(TIF_RESTORE_SIGMASK))
1022                 oldset = &current->saved_sigmask;
1023         else
1024                 oldset = &current->blocked;
1025
1026         signr = get_signal_to_deliver(&info, &ka, regs, &cookie);
1027         if (signr > 0) {
1028                 if (cookie.restart_syscall)
1029                         syscall_restart(cookie.orig_i0, regs, &ka.sa);
1030                 handle_signal(signr, &ka, &info, oldset,
1031                               regs, svr4_signal);
1032                 /* a signal was successfully delivered; the saved
1033                  * sigmask will have been stored in the signal frame,
1034                  * and will be restored by sigreturn, so we can simply
1035                  * clear the TIF_RESTORE_SIGMASK flag.
1036                  */
1037                 if (test_thread_flag(TIF_RESTORE_SIGMASK))
1038                         clear_thread_flag(TIF_RESTORE_SIGMASK);
1039                 return;
1040         }
1041         if (cookie.restart_syscall &&
1042             (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
1043              regs->u_regs[UREG_I0] == ERESTARTSYS ||
1044              regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
1045                 /* replay the system call when we are done */
1046                 regs->u_regs[UREG_I0] = cookie.orig_i0;
1047                 regs->pc -= 4;
1048                 regs->npc -= 4;
1049         }
1050         if (cookie.restart_syscall &&
1051             regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
1052                 regs->u_regs[UREG_G1] = __NR_restart_syscall;
1053                 regs->pc -= 4;
1054                 regs->npc -= 4;
1055         }
1056
1057         /* if there's no signal to deliver, we just put the saved sigmask
1058          * back
1059          */
1060         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
1061                 clear_thread_flag(TIF_RESTORE_SIGMASK);
1062                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
1063         }
1064 }
1065
1066 asmlinkage int
1067 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
1068                 unsigned long sp)
1069 {
1070         int ret = -EFAULT;
1071
1072         /* First see if old state is wanted. */
1073         if (ossptr) {
1074                 if (put_user(current->sas_ss_sp + current->sas_ss_size,
1075                              &ossptr->the_stack) ||
1076                     __put_user(on_sig_stack(sp), &ossptr->cur_status))
1077                         goto out;
1078         }
1079
1080         /* Now see if we want to update the new state. */
1081         if (ssptr) {
1082                 char *ss_sp;
1083
1084                 if (get_user(ss_sp, &ssptr->the_stack))
1085                         goto out;
1086                 /* If the current stack was set with sigaltstack, don't
1087                    swap stacks while we are on it.  */
1088                 ret = -EPERM;
1089                 if (current->sas_ss_sp && on_sig_stack(sp))
1090                         goto out;
1091
1092                 /* Since we don't know the extent of the stack, and we don't
1093                    track onstack-ness, but rather calculate it, we must
1094                    presume a size.  Ho hum this interface is lossy.  */
1095                 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
1096                 current->sas_ss_size = SIGSTKSZ;
1097         }
1098         ret = 0;
1099 out:
1100         return ret;
1101 }
1102
1103 void ptrace_signal_deliver(struct pt_regs *regs, void *cookie)
1104 {
1105         struct sparc_deliver_cookie *cp = cookie;
1106
1107         if (cp->restart_syscall &&
1108             (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
1109              regs->u_regs[UREG_I0] == ERESTARTSYS ||
1110              regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
1111                 /* replay the system call when we are done */
1112                 regs->u_regs[UREG_I0] = cp->orig_i0;
1113                 regs->pc -= 4;
1114                 regs->npc -= 4;
1115                 cp->restart_syscall = 0;
1116         }
1117
1118         if (cp->restart_syscall &&
1119             regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
1120                 regs->u_regs[UREG_G1] = __NR_restart_syscall;
1121                 regs->pc -= 4;
1122                 regs->npc -= 4;
1123                 cp->restart_syscall = 0;
1124         }
1125 }