Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[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/pgalloc.h>
26 #include <asm/pgtable.h>
27 #include <asm/cacheflush.h>     /* flush_sig_insns */
28
29 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
30
31 extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
32                    void *fpqueue, unsigned long *fpqdepth);
33 extern void fpload(unsigned long *fpregs, unsigned long *fsr);
34
35 /* Signal frames: the original one (compatible with SunOS):
36  *
37  * Set up a signal frame... Make the stack look the way SunOS
38  * expects it to look which is basically:
39  *
40  * ---------------------------------- <-- %sp at signal time
41  * Struct sigcontext
42  * Signal address
43  * Ptr to sigcontext area above
44  * Signal code
45  * The signal number itself
46  * One register window
47  * ---------------------------------- <-- New %sp
48  */
49 struct signal_sframe {
50         struct reg_window       sig_window;
51         int                     sig_num;
52         int                     sig_code;
53         struct sigcontext __user *sig_scptr;
54         int                     sig_address;
55         struct sigcontext       sig_context;
56         unsigned int            extramask[_NSIG_WORDS - 1];
57 };
58
59 /* 
60  * And the new one, intended to be used for Linux applications only
61  * (we have enough in there to work with clone).
62  * All the interesting bits are in the info field.
63  */
64
65 struct new_signal_frame {
66         struct sparc_stackf     ss;
67         __siginfo_t             info;
68         __siginfo_fpu_t __user  *fpu_save;
69         unsigned long           insns[2] __attribute__ ((aligned (8)));
70         unsigned int            extramask[_NSIG_WORDS - 1];
71         unsigned int            extra_size; /* Should be 0 */
72         __siginfo_fpu_t         fpu_state;
73 };
74
75 struct rt_signal_frame {
76         struct sparc_stackf     ss;
77         siginfo_t               info;
78         struct pt_regs          regs;
79         sigset_t                mask;
80         __siginfo_fpu_t __user  *fpu_save;
81         unsigned int            insns[2];
82         stack_t                 stack;
83         unsigned int            extra_size; /* Should be 0 */
84         __siginfo_fpu_t         fpu_state;
85 };
86
87 /* Align macros */
88 #define SF_ALIGNEDSZ  (((sizeof(struct signal_sframe) + 7) & (~7)))
89 #define NF_ALIGNEDSZ  (((sizeof(struct new_signal_frame) + 7) & (~7)))
90 #define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame) + 7) & (~7)))
91
92 static int _sigpause_common(old_sigset_t set)
93 {
94         set &= _BLOCKABLE;
95         spin_lock_irq(&current->sighand->siglock);
96         current->saved_sigmask = current->blocked;
97         siginitset(&current->blocked, set);
98         recalc_sigpending();
99         spin_unlock_irq(&current->sighand->siglock);
100
101         current->state = TASK_INTERRUPTIBLE;
102         schedule();
103         set_thread_flag(TIF_RESTORE_SIGMASK);
104
105         return -ERESTARTNOHAND;
106 }
107
108 asmlinkage int sys_sigpause(unsigned int set)
109 {
110         return _sigpause_common(set);
111 }
112
113 asmlinkage int sys_sigsuspend(old_sigset_t set)
114 {
115         return _sigpause_common(set);
116 }
117
118 static inline int
119 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
120 {
121         int err;
122 #ifdef CONFIG_SMP
123         if (test_tsk_thread_flag(current, TIF_USEDFPU))
124                 regs->psr &= ~PSR_EF;
125 #else
126         if (current == last_task_used_math) {
127                 last_task_used_math = NULL;
128                 regs->psr &= ~PSR_EF;
129         }
130 #endif
131         set_used_math();
132         clear_tsk_thread_flag(current, TIF_USEDFPU);
133
134         if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
135                 return -EFAULT;
136
137         err = __copy_from_user(&current->thread.float_regs[0], &fpu->si_float_regs[0],
138                                (sizeof(unsigned long) * 32));
139         err |= __get_user(current->thread.fsr, &fpu->si_fsr);
140         err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
141         if (current->thread.fpqdepth != 0)
142                 err |= __copy_from_user(&current->thread.fpqueue[0],
143                                         &fpu->si_fpqueue[0],
144                                         ((sizeof(unsigned long) +
145                                         (sizeof(unsigned long *)))*16));
146         return err;
147 }
148
149 static inline void do_new_sigreturn (struct pt_regs *regs)
150 {
151         struct new_signal_frame __user *sf;
152         unsigned long up_psr, pc, npc;
153         sigset_t set;
154         __siginfo_fpu_t __user *fpu_save;
155         int err;
156
157         sf = (struct new_signal_frame __user *) regs->u_regs[UREG_FP];
158
159         /* 1. Make sure we are not getting garbage from the user */
160         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
161                 goto segv_and_exit;
162
163         if (((unsigned long) sf) & 3)
164                 goto segv_and_exit;
165
166         err = __get_user(pc,  &sf->info.si_regs.pc);
167         err |= __get_user(npc, &sf->info.si_regs.npc);
168
169         if ((pc | npc) & 3)
170                 goto segv_and_exit;
171
172         /* 2. Restore the state */
173         up_psr = regs->psr;
174         err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
175
176         /* User can only change condition codes and FPU enabling in %psr. */
177         regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
178                   | (regs->psr & (PSR_ICC | PSR_EF));
179
180         err |= __get_user(fpu_save, &sf->fpu_save);
181
182         if (fpu_save)
183                 err |= restore_fpu_state(regs, fpu_save);
184
185         /* This is pretty much atomic, no amount locking would prevent
186          * the races which exist anyways.
187          */
188         err |= __get_user(set.sig[0], &sf->info.si_mask);
189         err |= __copy_from_user(&set.sig[1], &sf->extramask,
190                                 (_NSIG_WORDS-1) * sizeof(unsigned int));
191                            
192         if (err)
193                 goto segv_and_exit;
194
195         sigdelsetmask(&set, ~_BLOCKABLE);
196         spin_lock_irq(&current->sighand->siglock);
197         current->blocked = set;
198         recalc_sigpending();
199         spin_unlock_irq(&current->sighand->siglock);
200         return;
201
202 segv_and_exit:
203         force_sig(SIGSEGV, current);
204 }
205
206 asmlinkage void do_sigreturn(struct pt_regs *regs)
207 {
208         struct sigcontext __user *scptr;
209         unsigned long pc, npc, psr;
210         sigset_t set;
211         int err;
212
213         /* Always make any pending restarted system calls return -EINTR */
214         current_thread_info()->restart_block.fn = do_no_restart_syscall;
215
216         synchronize_user_stack();
217
218         if (current->thread.new_signal) {
219                 do_new_sigreturn(regs);
220                 return;
221         }
222
223         scptr = (struct sigcontext __user *) regs->u_regs[UREG_I0];
224
225         /* Check sanity of the user arg. */
226         if (!access_ok(VERIFY_READ, scptr, sizeof(struct sigcontext)) ||
227             (((unsigned long) scptr) & 3))
228                 goto segv_and_exit;
229
230         err = __get_user(pc, &scptr->sigc_pc);
231         err |= __get_user(npc, &scptr->sigc_npc);
232
233         if ((pc | npc) & 3)
234                 goto segv_and_exit;
235
236         /* This is pretty much atomic, no amount locking would prevent
237          * the races which exist anyways.
238          */
239         err |= __get_user(set.sig[0], &scptr->sigc_mask);
240         /* Note that scptr + 1 points to extramask */
241         err |= __copy_from_user(&set.sig[1], scptr + 1,
242                                 (_NSIG_WORDS - 1) * sizeof(unsigned int));
243         
244         if (err)
245                 goto segv_and_exit;
246
247         sigdelsetmask(&set, ~_BLOCKABLE);
248         spin_lock_irq(&current->sighand->siglock);
249         current->blocked = set;
250         recalc_sigpending();
251         spin_unlock_irq(&current->sighand->siglock);
252
253         regs->pc = pc;
254         regs->npc = npc;
255
256         err = __get_user(regs->u_regs[UREG_FP], &scptr->sigc_sp);
257         err |= __get_user(regs->u_regs[UREG_I0], &scptr->sigc_o0);
258         err |= __get_user(regs->u_regs[UREG_G1], &scptr->sigc_g1);
259
260         /* User can only change condition codes in %psr. */
261         err |= __get_user(psr, &scptr->sigc_psr);
262         if (err)
263                 goto segv_and_exit;
264                 
265         regs->psr &= ~(PSR_ICC);
266         regs->psr |= (psr & PSR_ICC);
267         return;
268
269 segv_and_exit:
270         force_sig(SIGSEGV, current);
271 }
272
273 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
274 {
275         struct rt_signal_frame __user *sf;
276         unsigned int psr, pc, npc;
277         __siginfo_fpu_t __user *fpu_save;
278         mm_segment_t old_fs;
279         sigset_t set;
280         stack_t st;
281         int err;
282
283         synchronize_user_stack();
284         sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
285         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
286             (((unsigned long) sf) & 0x03))
287                 goto segv;
288
289         err = __get_user(pc, &sf->regs.pc);
290         err |= __get_user(npc, &sf->regs.npc);
291         err |= ((pc | npc) & 0x03);
292
293         err |= __get_user(regs->y, &sf->regs.y);
294         err |= __get_user(psr, &sf->regs.psr);
295
296         err |= __copy_from_user(&regs->u_regs[UREG_G1],
297                                 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
298
299         regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
300
301         err |= __get_user(fpu_save, &sf->fpu_save);
302
303         if (fpu_save)
304                 err |= restore_fpu_state(regs, fpu_save);
305         err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
306         
307         err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
308         
309         if (err)
310                 goto segv;
311                 
312         regs->pc = pc;
313         regs->npc = npc;
314         
315         /* It is more difficult to avoid calling this function than to
316          * call it and ignore errors.
317          */
318         old_fs = get_fs();
319         set_fs(KERNEL_DS);
320         do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
321         set_fs(old_fs);
322
323         sigdelsetmask(&set, ~_BLOCKABLE);
324         spin_lock_irq(&current->sighand->siglock);
325         current->blocked = set;
326         recalc_sigpending();
327         spin_unlock_irq(&current->sighand->siglock);
328         return;
329 segv:
330         force_sig(SIGSEGV, current);
331 }
332
333 /* Checks if the fp is valid */
334 static inline int invalid_frame_pointer(void __user *fp, int fplen)
335 {
336         if ((((unsigned long) fp) & 7) ||
337             !__access_ok((unsigned long)fp, fplen) ||
338             ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
339              ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
340                 return 1;
341         
342         return 0;
343 }
344
345 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
346 {
347         unsigned long sp;
348
349         sp = regs->u_regs[UREG_FP];
350
351         /* This is the X/Open sanctioned signal stack switching.  */
352         if (sa->sa_flags & SA_ONSTACK) {
353                 if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
354                         sp = current->sas_ss_sp + current->sas_ss_size;
355         }
356         return (void __user *)(sp - framesize);
357 }
358
359 static inline void
360 setup_frame(struct sigaction *sa, struct pt_regs *regs, int signr, sigset_t *oldset, siginfo_t *info)
361 {
362         struct signal_sframe __user *sframep;
363         struct sigcontext __user *sc;
364         int window = 0, err;
365         unsigned long pc = regs->pc;
366         unsigned long npc = regs->npc;
367         struct thread_info *tp = current_thread_info();
368         void __user *sig_address;
369         int sig_code;
370
371         synchronize_user_stack();
372         sframep = (struct signal_sframe __user *)
373                 get_sigframe(sa, regs, SF_ALIGNEDSZ);
374         if (invalid_frame_pointer(sframep, sizeof(*sframep))){
375                 /* Don't change signal code and address, so that
376                  * post mortem debuggers can have a look.
377                  */
378                 goto sigill_and_return;
379         }
380
381         sc = &sframep->sig_context;
382
383         /* We've already made sure frame pointer isn't in kernel space... */
384         err  = __put_user((sas_ss_flags(regs->u_regs[UREG_FP]) == SS_ONSTACK),
385                          &sc->sigc_onstack);
386         err |= __put_user(oldset->sig[0], &sc->sigc_mask);
387         err |= __copy_to_user(sframep->extramask, &oldset->sig[1],
388                               (_NSIG_WORDS - 1) * sizeof(unsigned int));
389         err |= __put_user(regs->u_regs[UREG_FP], &sc->sigc_sp);
390         err |= __put_user(pc, &sc->sigc_pc);
391         err |= __put_user(npc, &sc->sigc_npc);
392         err |= __put_user(regs->psr, &sc->sigc_psr);
393         err |= __put_user(regs->u_regs[UREG_G1], &sc->sigc_g1);
394         err |= __put_user(regs->u_regs[UREG_I0], &sc->sigc_o0);
395         err |= __put_user(tp->w_saved, &sc->sigc_oswins);
396         if (tp->w_saved)
397                 for (window = 0; window < tp->w_saved; window++) {
398                         put_user((char *)tp->rwbuf_stkptrs[window],
399                                  &sc->sigc_spbuf[window]);
400                         err |= __copy_to_user(&sc->sigc_wbuf[window],
401                                               &tp->reg_window[window],
402                                               sizeof(struct reg_window));
403                 }
404         else
405                 err |= __copy_to_user(sframep, (char *) regs->u_regs[UREG_FP],
406                                       sizeof(struct reg_window));
407
408         tp->w_saved = 0; /* So process is allowed to execute. */
409
410         err |= __put_user(signr, &sframep->sig_num);
411         sig_address = NULL;
412         sig_code = 0;
413         if (SI_FROMKERNEL (info) && (info->si_code & __SI_MASK) == __SI_FAULT) {
414                 sig_address = info->si_addr;
415                 switch (signr) {
416                 case SIGSEGV:
417                         switch (info->si_code) {
418                         case SEGV_MAPERR: sig_code = SUBSIG_NOMAPPING; break;
419                         default: sig_code = SUBSIG_PROTECTION; break;
420                         }
421                         break;
422                 case SIGILL:
423                         switch (info->si_code) {
424                         case ILL_ILLOPC: sig_code = SUBSIG_ILLINST; break;
425                         case ILL_PRVOPC: sig_code = SUBSIG_PRIVINST; break;
426                         case ILL_ILLTRP: sig_code = SUBSIG_BADTRAP(info->si_trapno); break;
427                         default: sig_code = SUBSIG_STACK; break;
428                         }
429                         break;
430                 case SIGFPE:
431                         switch (info->si_code) {
432                         case FPE_INTDIV: sig_code = SUBSIG_IDIVZERO; break;
433                         case FPE_INTOVF: sig_code = SUBSIG_FPINTOVFL; break;
434                         case FPE_FLTDIV: sig_code = SUBSIG_FPDIVZERO; break;
435                         case FPE_FLTOVF: sig_code = SUBSIG_FPOVFLOW; break;
436                         case FPE_FLTUND: sig_code = SUBSIG_FPUNFLOW; break;
437                         case FPE_FLTRES: sig_code = SUBSIG_FPINEXACT; break;
438                         case FPE_FLTINV: sig_code = SUBSIG_FPOPERROR; break;
439                         default: sig_code = SUBSIG_FPERROR; break;
440                         }
441                         break;
442                 case SIGBUS:
443                         switch (info->si_code) {
444                         case BUS_ADRALN: sig_code = SUBSIG_ALIGNMENT; break;
445                         case BUS_ADRERR: sig_code = SUBSIG_MISCERROR; break;
446                         default: sig_code = SUBSIG_BUSTIMEOUT; break;
447                         }
448                         break;
449                 case SIGEMT:
450                         switch (info->si_code) {
451                         case EMT_TAGOVF: sig_code = SUBSIG_TAG; break;
452                         }
453                         break;
454                 case SIGSYS:
455                         if (info->si_code == (__SI_FAULT|0x100)) {
456                                 sig_code = info->si_trapno;
457                                 break;
458                         }
459                 default:
460                         sig_address = NULL;
461                 }
462         }
463         err |= __put_user((unsigned long)sig_address, &sframep->sig_address);
464         err |= __put_user(sig_code, &sframep->sig_code);
465         err |= __put_user(sc, &sframep->sig_scptr);
466         if (err)
467                 goto sigsegv;
468
469         regs->u_regs[UREG_FP] = (unsigned long) sframep;
470         regs->pc = (unsigned long) sa->sa_handler;
471         regs->npc = (regs->pc + 4);
472         return;
473
474 sigill_and_return:
475         do_exit(SIGILL);
476 sigsegv:
477         force_sigsegv(signr, current);
478 }
479
480
481 static inline int
482 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
483 {
484         int err = 0;
485 #ifdef CONFIG_SMP
486         if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
487                 put_psr(get_psr() | PSR_EF);
488                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
489                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
490                 regs->psr &= ~(PSR_EF);
491                 clear_tsk_thread_flag(current, TIF_USEDFPU);
492         }
493 #else
494         if (current == last_task_used_math) {
495                 put_psr(get_psr() | PSR_EF);
496                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
497                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
498                 last_task_used_math = NULL;
499                 regs->psr &= ~(PSR_EF);
500         }
501 #endif
502         err |= __copy_to_user(&fpu->si_float_regs[0],
503                               &current->thread.float_regs[0],
504                               (sizeof(unsigned long) * 32));
505         err |= __put_user(current->thread.fsr, &fpu->si_fsr);
506         err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
507         if (current->thread.fpqdepth != 0)
508                 err |= __copy_to_user(&fpu->si_fpqueue[0],
509                                       &current->thread.fpqueue[0],
510                                       ((sizeof(unsigned long) +
511                                       (sizeof(unsigned long *)))*16));
512         clear_used_math();
513         return err;
514 }
515
516 static inline void
517 new_setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
518                 int signo, sigset_t *oldset)
519 {
520         struct new_signal_frame __user *sf;
521         int sigframe_size, err;
522
523         /* 1. Make sure everything is clean */
524         synchronize_user_stack();
525
526         sigframe_size = NF_ALIGNEDSZ;
527         if (!used_math())
528                 sigframe_size -= sizeof(__siginfo_fpu_t);
529
530         sf = (struct new_signal_frame __user *)
531                 get_sigframe(&ka->sa, regs, sigframe_size);
532
533         if (invalid_frame_pointer(sf, sigframe_size))
534                 goto sigill_and_return;
535
536         if (current_thread_info()->w_saved != 0)
537                 goto sigill_and_return;
538
539         /* 2. Save the current process state */
540         err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
541         
542         err |= __put_user(0, &sf->extra_size);
543
544         if (used_math()) {
545                 err |= save_fpu_state(regs, &sf->fpu_state);
546                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
547         } else {
548                 err |= __put_user(0, &sf->fpu_save);
549         }
550
551         err |= __put_user(oldset->sig[0], &sf->info.si_mask);
552         err |= __copy_to_user(sf->extramask, &oldset->sig[1],
553                               (_NSIG_WORDS - 1) * sizeof(unsigned int));
554         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
555                               sizeof(struct reg_window));
556         if (err)
557                 goto sigsegv;
558         
559         /* 3. signal handler back-trampoline and parameters */
560         regs->u_regs[UREG_FP] = (unsigned long) sf;
561         regs->u_regs[UREG_I0] = signo;
562         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
563         regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
564
565         /* 4. signal handler */
566         regs->pc = (unsigned long) ka->sa.sa_handler;
567         regs->npc = (regs->pc + 4);
568
569         /* 5. return to kernel instructions */
570         if (ka->ka_restorer)
571                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
572         else {
573                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
574
575                 /* mov __NR_sigreturn, %g1 */
576                 err |= __put_user(0x821020d8, &sf->insns[0]);
577
578                 /* t 0x10 */
579                 err |= __put_user(0x91d02010, &sf->insns[1]);
580                 if (err)
581                         goto sigsegv;
582
583                 /* Flush instruction space. */
584                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
585         }
586         return;
587
588 sigill_and_return:
589         do_exit(SIGILL);
590 sigsegv:
591         force_sigsegv(signo, current);
592 }
593
594 static inline void
595 new_setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
596                    int signo, sigset_t *oldset, siginfo_t *info)
597 {
598         struct rt_signal_frame __user *sf;
599         int sigframe_size;
600         unsigned int psr;
601         int err;
602
603         synchronize_user_stack();
604         sigframe_size = RT_ALIGNEDSZ;
605         if (!used_math())
606                 sigframe_size -= sizeof(__siginfo_fpu_t);
607         sf = (struct rt_signal_frame __user *)
608                 get_sigframe(&ka->sa, regs, sigframe_size);
609         if (invalid_frame_pointer(sf, sigframe_size))
610                 goto sigill;
611         if (current_thread_info()->w_saved != 0)
612                 goto sigill;
613
614         err  = __put_user(regs->pc, &sf->regs.pc);
615         err |= __put_user(regs->npc, &sf->regs.npc);
616         err |= __put_user(regs->y, &sf->regs.y);
617         psr = regs->psr;
618         if (used_math())
619                 psr |= PSR_EF;
620         err |= __put_user(psr, &sf->regs.psr);
621         err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
622         err |= __put_user(0, &sf->extra_size);
623
624         if (psr & PSR_EF) {
625                 err |= save_fpu_state(regs, &sf->fpu_state);
626                 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
627         } else {
628                 err |= __put_user(0, &sf->fpu_save);
629         }
630         err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
631         
632         /* Setup sigaltstack */
633         err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
634         err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
635         err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
636         
637         err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
638                               sizeof(struct reg_window));       
639
640         err |= copy_siginfo_to_user(&sf->info, info);
641
642         if (err)
643                 goto sigsegv;
644
645         regs->u_regs[UREG_FP] = (unsigned long) sf;
646         regs->u_regs[UREG_I0] = signo;
647         regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
648         regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
649
650         regs->pc = (unsigned long) ka->sa.sa_handler;
651         regs->npc = (regs->pc + 4);
652
653         if (ka->ka_restorer)
654                 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
655         else {
656                 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
657
658                 /* mov __NR_sigreturn, %g1 */
659                 err |= __put_user(0x821020d8, &sf->insns[0]);
660
661                 /* t 0x10 */
662                 err |= __put_user(0x91d02010, &sf->insns[1]);
663                 if (err)
664                         goto sigsegv;
665
666                 /* Flush instruction space. */
667                 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
668         }
669         return;
670
671 sigill:
672         do_exit(SIGILL);
673 sigsegv:
674         force_sigsegv(signo, current);
675 }
676
677 static inline void
678 handle_signal(unsigned long signr, struct k_sigaction *ka,
679               siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
680 {
681         if (ka->sa.sa_flags & SA_SIGINFO)
682                 new_setup_rt_frame(ka, regs, signr, oldset, info);
683         else if (current->thread.new_signal)
684                 new_setup_frame(ka, regs, signr, oldset);
685         else
686                 setup_frame(&ka->sa, regs, signr, oldset, info);
687
688         spin_lock_irq(&current->sighand->siglock);
689         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
690         if (!(ka->sa.sa_flags & SA_NOMASK))
691                 sigaddset(&current->blocked, signr);
692         recalc_sigpending();
693         spin_unlock_irq(&current->sighand->siglock);
694 }
695
696 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
697                                    struct sigaction *sa)
698 {
699         switch(regs->u_regs[UREG_I0]) {
700         case ERESTART_RESTARTBLOCK:
701         case ERESTARTNOHAND:
702         no_system_call_restart:
703                 regs->u_regs[UREG_I0] = EINTR;
704                 regs->psr |= PSR_C;
705                 break;
706         case ERESTARTSYS:
707                 if (!(sa->sa_flags & SA_RESTART))
708                         goto no_system_call_restart;
709                 /* fallthrough */
710         case ERESTARTNOINTR:
711                 regs->u_regs[UREG_I0] = orig_i0;
712                 regs->pc -= 4;
713                 regs->npc -= 4;
714         }
715 }
716
717 /* Note that 'init' is a special process: it doesn't get signals it doesn't
718  * want to handle. Thus you cannot kill init even with a SIGKILL even by
719  * mistake.
720  */
721 asmlinkage void do_signal(struct pt_regs * regs, unsigned long orig_i0, int restart_syscall)
722 {
723         siginfo_t info;
724         struct sparc_deliver_cookie cookie;
725         struct k_sigaction ka;
726         int signr;
727         sigset_t *oldset;
728
729         cookie.restart_syscall = restart_syscall;
730         cookie.orig_i0 = orig_i0;
731
732         if (test_thread_flag(TIF_RESTORE_SIGMASK))
733                 oldset = &current->saved_sigmask;
734         else
735                 oldset = &current->blocked;
736
737         signr = get_signal_to_deliver(&info, &ka, regs, &cookie);
738         if (signr > 0) {
739                 if (cookie.restart_syscall)
740                         syscall_restart(cookie.orig_i0, regs, &ka.sa);
741                 handle_signal(signr, &ka, &info, oldset, regs);
742
743                 /* a signal was successfully delivered; the saved
744                  * sigmask will have been stored in the signal frame,
745                  * and will be restored by sigreturn, so we can simply
746                  * clear the TIF_RESTORE_SIGMASK flag.
747                  */
748                 if (test_thread_flag(TIF_RESTORE_SIGMASK))
749                         clear_thread_flag(TIF_RESTORE_SIGMASK);
750                 return;
751         }
752         if (cookie.restart_syscall &&
753             (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
754              regs->u_regs[UREG_I0] == ERESTARTSYS ||
755              regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
756                 /* replay the system call when we are done */
757                 regs->u_regs[UREG_I0] = cookie.orig_i0;
758                 regs->pc -= 4;
759                 regs->npc -= 4;
760         }
761         if (cookie.restart_syscall &&
762             regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
763                 regs->u_regs[UREG_G1] = __NR_restart_syscall;
764                 regs->pc -= 4;
765                 regs->npc -= 4;
766         }
767
768         /* if there's no signal to deliver, we just put the saved sigmask
769          * back
770          */
771         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
772                 clear_thread_flag(TIF_RESTORE_SIGMASK);
773                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
774         }
775 }
776
777 asmlinkage int
778 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
779                 unsigned long sp)
780 {
781         int ret = -EFAULT;
782
783         /* First see if old state is wanted. */
784         if (ossptr) {
785                 if (put_user(current->sas_ss_sp + current->sas_ss_size,
786                              &ossptr->the_stack) ||
787                     __put_user(on_sig_stack(sp), &ossptr->cur_status))
788                         goto out;
789         }
790
791         /* Now see if we want to update the new state. */
792         if (ssptr) {
793                 char *ss_sp;
794
795                 if (get_user(ss_sp, &ssptr->the_stack))
796                         goto out;
797                 /* If the current stack was set with sigaltstack, don't
798                    swap stacks while we are on it.  */
799                 ret = -EPERM;
800                 if (current->sas_ss_sp && on_sig_stack(sp))
801                         goto out;
802
803                 /* Since we don't know the extent of the stack, and we don't
804                    track onstack-ness, but rather calculate it, we must
805                    presume a size.  Ho hum this interface is lossy.  */
806                 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
807                 current->sas_ss_size = SIGSTKSZ;
808         }
809         ret = 0;
810 out:
811         return ret;
812 }
813
814 void ptrace_signal_deliver(struct pt_regs *regs, void *cookie)
815 {
816         struct sparc_deliver_cookie *cp = cookie;
817
818         if (cp->restart_syscall &&
819             (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
820              regs->u_regs[UREG_I0] == ERESTARTSYS ||
821              regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
822                 /* replay the system call when we are done */
823                 regs->u_regs[UREG_I0] = cp->orig_i0;
824                 regs->pc -= 4;
825                 regs->npc -= 4;
826                 cp->restart_syscall = 0;
827         }
828
829         if (cp->restart_syscall &&
830             regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
831                 regs->u_regs[UREG_G1] = __NR_restart_syscall;
832                 regs->pc -= 4;
833                 regs->npc -= 4;
834                 cp->restart_syscall = 0;
835         }
836 }