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