arm: introduce little-endian bitops
[linux-2.6/kvm.git] / arch / sparc / kernel / signal_32.c
blob5e5c5fd03783c997f5c344025e8f4784182a0ddc
1 /* linux/arch/sparc/kernel/signal.c
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 */
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/wait.h>
14 #include <linux/ptrace.h>
15 #include <linux/unistd.h>
16 #include <linux/mm.h>
17 #include <linux/tty.h>
18 #include <linux/smp.h>
19 #include <linux/binfmts.h> /* do_coredum */
20 #include <linux/bitops.h>
21 #include <linux/tracehook.h>
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 */
29 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
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);
35 struct signal_frame {
36 struct sparc_stackf ss;
37 __siginfo32_t info;
38 __siginfo_fpu_t __user *fpu_save;
39 unsigned long insns[2] __attribute__ ((aligned (8)));
40 unsigned int extramask[_NSIG_WORDS - 1];
41 unsigned int extra_size; /* Should be 0 */
42 __siginfo_fpu_t fpu_state;
45 struct rt_signal_frame {
46 struct sparc_stackf ss;
47 siginfo_t info;
48 struct pt_regs regs;
49 sigset_t mask;
50 __siginfo_fpu_t __user *fpu_save;
51 unsigned int insns[2];
52 stack_t stack;
53 unsigned int extra_size; /* Should be 0 */
54 __siginfo_fpu_t fpu_state;
57 /* Align macros */
58 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
59 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
61 static int _sigpause_common(old_sigset_t set)
63 set &= _BLOCKABLE;
64 spin_lock_irq(&current->sighand->siglock);
65 current->saved_sigmask = current->blocked;
66 siginitset(&current->blocked, set);
67 recalc_sigpending();
68 spin_unlock_irq(&current->sighand->siglock);
70 current->state = TASK_INTERRUPTIBLE;
71 schedule();
72 set_thread_flag(TIF_RESTORE_SIGMASK);
74 return -ERESTARTNOHAND;
77 asmlinkage int sys_sigsuspend(old_sigset_t set)
79 return _sigpause_common(set);
82 static inline int
83 restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
85 int err;
86 #ifdef CONFIG_SMP
87 if (test_tsk_thread_flag(current, TIF_USEDFPU))
88 regs->psr &= ~PSR_EF;
89 #else
90 if (current == last_task_used_math) {
91 last_task_used_math = NULL;
92 regs->psr &= ~PSR_EF;
94 #endif
95 set_used_math();
96 clear_tsk_thread_flag(current, TIF_USEDFPU);
98 if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
99 return -EFAULT;
101 err = __copy_from_user(&current->thread.float_regs[0], &fpu->si_float_regs[0],
102 (sizeof(unsigned long) * 32));
103 err |= __get_user(current->thread.fsr, &fpu->si_fsr);
104 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
105 if (current->thread.fpqdepth != 0)
106 err |= __copy_from_user(&current->thread.fpqueue[0],
107 &fpu->si_fpqueue[0],
108 ((sizeof(unsigned long) +
109 (sizeof(unsigned long *)))*16));
110 return err;
113 asmlinkage void do_sigreturn(struct pt_regs *regs)
115 struct signal_frame __user *sf;
116 unsigned long up_psr, pc, npc;
117 sigset_t set;
118 __siginfo_fpu_t __user *fpu_save;
119 int err;
121 /* Always make any pending restarted system calls return -EINTR */
122 current_thread_info()->restart_block.fn = do_no_restart_syscall;
124 synchronize_user_stack();
126 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
128 /* 1. Make sure we are not getting garbage from the user */
129 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
130 goto segv_and_exit;
132 if (((unsigned long) sf) & 3)
133 goto segv_and_exit;
135 err = __get_user(pc, &sf->info.si_regs.pc);
136 err |= __get_user(npc, &sf->info.si_regs.npc);
138 if ((pc | npc) & 3)
139 goto segv_and_exit;
141 /* 2. Restore the state */
142 up_psr = regs->psr;
143 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
145 /* User can only change condition codes and FPU enabling in %psr. */
146 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
147 | (regs->psr & (PSR_ICC | PSR_EF));
149 /* Prevent syscall restart. */
150 pt_regs_clear_syscall(regs);
152 err |= __get_user(fpu_save, &sf->fpu_save);
154 if (fpu_save)
155 err |= restore_fpu_state(regs, fpu_save);
157 /* This is pretty much atomic, no amount locking would prevent
158 * the races which exist anyways.
160 err |= __get_user(set.sig[0], &sf->info.si_mask);
161 err |= __copy_from_user(&set.sig[1], &sf->extramask,
162 (_NSIG_WORDS-1) * sizeof(unsigned int));
164 if (err)
165 goto segv_and_exit;
167 sigdelsetmask(&set, ~_BLOCKABLE);
168 spin_lock_irq(&current->sighand->siglock);
169 current->blocked = set;
170 recalc_sigpending();
171 spin_unlock_irq(&current->sighand->siglock);
172 return;
174 segv_and_exit:
175 force_sig(SIGSEGV, current);
178 asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
180 struct rt_signal_frame __user *sf;
181 unsigned int psr, pc, npc;
182 __siginfo_fpu_t __user *fpu_save;
183 mm_segment_t old_fs;
184 sigset_t set;
185 stack_t st;
186 int err;
188 synchronize_user_stack();
189 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
190 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
191 (((unsigned long) sf) & 0x03))
192 goto segv;
194 err = __get_user(pc, &sf->regs.pc);
195 err |= __get_user(npc, &sf->regs.npc);
196 err |= ((pc | npc) & 0x03);
198 err |= __get_user(regs->y, &sf->regs.y);
199 err |= __get_user(psr, &sf->regs.psr);
201 err |= __copy_from_user(&regs->u_regs[UREG_G1],
202 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
204 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
206 /* Prevent syscall restart. */
207 pt_regs_clear_syscall(regs);
209 err |= __get_user(fpu_save, &sf->fpu_save);
211 if (fpu_save)
212 err |= restore_fpu_state(regs, fpu_save);
213 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
215 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t));
217 if (err)
218 goto segv;
220 regs->pc = pc;
221 regs->npc = npc;
223 /* It is more difficult to avoid calling this function than to
224 * call it and ignore errors.
226 old_fs = get_fs();
227 set_fs(KERNEL_DS);
228 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf);
229 set_fs(old_fs);
231 sigdelsetmask(&set, ~_BLOCKABLE);
232 spin_lock_irq(&current->sighand->siglock);
233 current->blocked = set;
234 recalc_sigpending();
235 spin_unlock_irq(&current->sighand->siglock);
236 return;
237 segv:
238 force_sig(SIGSEGV, current);
241 /* Checks if the fp is valid */
242 static inline int invalid_frame_pointer(void __user *fp, int fplen)
244 if ((((unsigned long) fp) & 7) ||
245 !__access_ok((unsigned long)fp, fplen) ||
246 ((sparc_cpu_model == sun4 || sparc_cpu_model == sun4c) &&
247 ((unsigned long) fp < 0xe0000000 && (unsigned long) fp >= 0x20000000)))
248 return 1;
250 return 0;
253 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
255 unsigned long sp = regs->u_regs[UREG_FP];
258 * If we are on the alternate signal stack and would overflow it, don't.
259 * Return an always-bogus address instead so we will die with SIGSEGV.
261 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
262 return (void __user *) -1L;
264 /* This is the X/Open sanctioned signal stack switching. */
265 if (sa->sa_flags & SA_ONSTACK) {
266 if (sas_ss_flags(sp) == 0)
267 sp = current->sas_ss_sp + current->sas_ss_size;
270 sp -= framesize;
272 /* Always align the stack frame. This handles two cases. First,
273 * sigaltstack need not be mindful of platform specific stack
274 * alignment. Second, if we took this signal because the stack
275 * is not aligned properly, we'd like to take the signal cleanly
276 * and report that.
278 sp &= ~15UL;
280 return (void __user *) sp;
283 static inline int
284 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
286 int err = 0;
287 #ifdef CONFIG_SMP
288 if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
289 put_psr(get_psr() | PSR_EF);
290 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
291 &current->thread.fpqueue[0], &current->thread.fpqdepth);
292 regs->psr &= ~(PSR_EF);
293 clear_tsk_thread_flag(current, TIF_USEDFPU);
295 #else
296 if (current == last_task_used_math) {
297 put_psr(get_psr() | PSR_EF);
298 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
299 &current->thread.fpqueue[0], &current->thread.fpqdepth);
300 last_task_used_math = NULL;
301 regs->psr &= ~(PSR_EF);
303 #endif
304 err |= __copy_to_user(&fpu->si_float_regs[0],
305 &current->thread.float_regs[0],
306 (sizeof(unsigned long) * 32));
307 err |= __put_user(current->thread.fsr, &fpu->si_fsr);
308 err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
309 if (current->thread.fpqdepth != 0)
310 err |= __copy_to_user(&fpu->si_fpqueue[0],
311 &current->thread.fpqueue[0],
312 ((sizeof(unsigned long) +
313 (sizeof(unsigned long *)))*16));
314 clear_used_math();
315 return err;
318 static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
319 int signo, sigset_t *oldset)
321 struct signal_frame __user *sf;
322 int sigframe_size, err;
324 /* 1. Make sure everything is clean */
325 synchronize_user_stack();
327 sigframe_size = SF_ALIGNEDSZ;
328 if (!used_math())
329 sigframe_size -= sizeof(__siginfo_fpu_t);
331 sf = (struct signal_frame __user *)
332 get_sigframe(&ka->sa, regs, sigframe_size);
334 if (invalid_frame_pointer(sf, sigframe_size))
335 goto sigill_and_return;
337 if (current_thread_info()->w_saved != 0)
338 goto sigill_and_return;
340 /* 2. Save the current process state */
341 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
343 err |= __put_user(0, &sf->extra_size);
345 if (used_math()) {
346 err |= save_fpu_state(regs, &sf->fpu_state);
347 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
348 } else {
349 err |= __put_user(0, &sf->fpu_save);
352 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
353 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
354 (_NSIG_WORDS - 1) * sizeof(unsigned int));
355 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
356 sizeof(struct reg_window32));
357 if (err)
358 goto sigsegv;
360 /* 3. signal handler back-trampoline and parameters */
361 regs->u_regs[UREG_FP] = (unsigned long) sf;
362 regs->u_regs[UREG_I0] = signo;
363 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
364 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
366 /* 4. signal handler */
367 regs->pc = (unsigned long) ka->sa.sa_handler;
368 regs->npc = (regs->pc + 4);
370 /* 5. return to kernel instructions */
371 if (ka->ka_restorer)
372 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
373 else {
374 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
376 /* mov __NR_sigreturn, %g1 */
377 err |= __put_user(0x821020d8, &sf->insns[0]);
379 /* t 0x10 */
380 err |= __put_user(0x91d02010, &sf->insns[1]);
381 if (err)
382 goto sigsegv;
384 /* Flush instruction space. */
385 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
387 return 0;
389 sigill_and_return:
390 do_exit(SIGILL);
391 return -EINVAL;
393 sigsegv:
394 force_sigsegv(signo, current);
395 return -EFAULT;
398 static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
399 int signo, sigset_t *oldset, siginfo_t *info)
401 struct rt_signal_frame __user *sf;
402 int sigframe_size;
403 unsigned int psr;
404 int err;
406 synchronize_user_stack();
407 sigframe_size = RT_ALIGNEDSZ;
408 if (!used_math())
409 sigframe_size -= sizeof(__siginfo_fpu_t);
410 sf = (struct rt_signal_frame __user *)
411 get_sigframe(&ka->sa, regs, sigframe_size);
412 if (invalid_frame_pointer(sf, sigframe_size))
413 goto sigill;
414 if (current_thread_info()->w_saved != 0)
415 goto sigill;
417 err = __put_user(regs->pc, &sf->regs.pc);
418 err |= __put_user(regs->npc, &sf->regs.npc);
419 err |= __put_user(regs->y, &sf->regs.y);
420 psr = regs->psr;
421 if (used_math())
422 psr |= PSR_EF;
423 err |= __put_user(psr, &sf->regs.psr);
424 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
425 err |= __put_user(0, &sf->extra_size);
427 if (psr & PSR_EF) {
428 err |= save_fpu_state(regs, &sf->fpu_state);
429 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
430 } else {
431 err |= __put_user(0, &sf->fpu_save);
433 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
435 /* Setup sigaltstack */
436 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
437 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
438 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
440 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
441 sizeof(struct reg_window32));
443 err |= copy_siginfo_to_user(&sf->info, info);
445 if (err)
446 goto sigsegv;
448 regs->u_regs[UREG_FP] = (unsigned long) sf;
449 regs->u_regs[UREG_I0] = signo;
450 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
451 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
453 regs->pc = (unsigned long) ka->sa.sa_handler;
454 regs->npc = (regs->pc + 4);
456 if (ka->ka_restorer)
457 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
458 else {
459 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
461 /* mov __NR_sigreturn, %g1 */
462 err |= __put_user(0x821020d8, &sf->insns[0]);
464 /* t 0x10 */
465 err |= __put_user(0x91d02010, &sf->insns[1]);
466 if (err)
467 goto sigsegv;
469 /* Flush instruction space. */
470 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
472 return 0;
474 sigill:
475 do_exit(SIGILL);
476 return -EINVAL;
478 sigsegv:
479 force_sigsegv(signo, current);
480 return -EFAULT;
483 static inline int
484 handle_signal(unsigned long signr, struct k_sigaction *ka,
485 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
487 int err;
489 if (ka->sa.sa_flags & SA_SIGINFO)
490 err = setup_rt_frame(ka, regs, signr, oldset, info);
491 else
492 err = setup_frame(ka, regs, signr, oldset);
494 if (err)
495 return err;
497 spin_lock_irq(&current->sighand->siglock);
498 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
499 if (!(ka->sa.sa_flags & SA_NOMASK))
500 sigaddset(&current->blocked, signr);
501 recalc_sigpending();
502 spin_unlock_irq(&current->sighand->siglock);
504 tracehook_signal_handler(signr, info, ka, regs, 0);
506 return 0;
509 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
510 struct sigaction *sa)
512 switch(regs->u_regs[UREG_I0]) {
513 case ERESTART_RESTARTBLOCK:
514 case ERESTARTNOHAND:
515 no_system_call_restart:
516 regs->u_regs[UREG_I0] = EINTR;
517 regs->psr |= PSR_C;
518 break;
519 case ERESTARTSYS:
520 if (!(sa->sa_flags & SA_RESTART))
521 goto no_system_call_restart;
522 /* fallthrough */
523 case ERESTARTNOINTR:
524 regs->u_regs[UREG_I0] = orig_i0;
525 regs->pc -= 4;
526 regs->npc -= 4;
530 /* Note that 'init' is a special process: it doesn't get signals it doesn't
531 * want to handle. Thus you cannot kill init even with a SIGKILL even by
532 * mistake.
534 static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
536 struct k_sigaction ka;
537 int restart_syscall;
538 sigset_t *oldset;
539 siginfo_t info;
540 int signr;
542 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
543 restart_syscall = 1;
544 else
545 restart_syscall = 0;
547 if (test_thread_flag(TIF_RESTORE_SIGMASK))
548 oldset = &current->saved_sigmask;
549 else
550 oldset = &current->blocked;
552 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
554 /* If the debugger messes with the program counter, it clears
555 * the software "in syscall" bit, directing us to not perform
556 * a syscall restart.
558 if (restart_syscall && !pt_regs_is_syscall(regs))
559 restart_syscall = 0;
561 if (signr > 0) {
562 if (restart_syscall)
563 syscall_restart(orig_i0, regs, &ka.sa);
564 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
565 /* a signal was successfully delivered; the saved
566 * sigmask will have been stored in the signal frame,
567 * and will be restored by sigreturn, so we can simply
568 * clear the TIF_RESTORE_SIGMASK flag.
570 if (test_thread_flag(TIF_RESTORE_SIGMASK))
571 clear_thread_flag(TIF_RESTORE_SIGMASK);
573 return;
575 if (restart_syscall &&
576 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
577 regs->u_regs[UREG_I0] == ERESTARTSYS ||
578 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
579 /* replay the system call when we are done */
580 regs->u_regs[UREG_I0] = orig_i0;
581 regs->pc -= 4;
582 regs->npc -= 4;
583 pt_regs_clear_syscall(regs);
585 if (restart_syscall &&
586 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
587 regs->u_regs[UREG_G1] = __NR_restart_syscall;
588 regs->pc -= 4;
589 regs->npc -= 4;
590 pt_regs_clear_syscall(regs);
593 /* if there's no signal to deliver, we just put the saved sigmask
594 * back
596 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
597 clear_thread_flag(TIF_RESTORE_SIGMASK);
598 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
602 void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
603 unsigned long thread_info_flags)
605 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
606 do_signal(regs, orig_i0);
607 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
608 clear_thread_flag(TIF_NOTIFY_RESUME);
609 tracehook_notify_resume(regs);
610 if (current->replacement_session_keyring)
611 key_replace_session_keyring();
615 asmlinkage int
616 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
617 unsigned long sp)
619 int ret = -EFAULT;
621 /* First see if old state is wanted. */
622 if (ossptr) {
623 if (put_user(current->sas_ss_sp + current->sas_ss_size,
624 &ossptr->the_stack) ||
625 __put_user(on_sig_stack(sp), &ossptr->cur_status))
626 goto out;
629 /* Now see if we want to update the new state. */
630 if (ssptr) {
631 char *ss_sp;
633 if (get_user(ss_sp, &ssptr->the_stack))
634 goto out;
635 /* If the current stack was set with sigaltstack, don't
636 swap stacks while we are on it. */
637 ret = -EPERM;
638 if (current->sas_ss_sp && on_sig_stack(sp))
639 goto out;
641 /* Since we don't know the extent of the stack, and we don't
642 track onstack-ness, but rather calculate it, we must
643 presume a size. Ho hum this interface is lossy. */
644 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
645 current->sas_ss_size = SIGSTKSZ;
647 ret = 0;
648 out:
649 return ret;