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