OMAP3: PM: Ensure MUSB block can idle when driver not loaded
[linux-ginger.git] / arch / sparc / kernel / signal_32.c
blob181d069a2d44bdeb160125053150b894e550598b
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 /* Always align the stack frame. This handles two cases. First,
271 * sigaltstack need not be mindful of platform specific stack
272 * alignment. Second, if we took this signal because the stack
273 * is not aligned properly, we'd like to take the signal cleanly
274 * and report that.
276 sp &= ~7UL;
278 return (void __user *)(sp - framesize);
281 static inline int
282 save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
284 int err = 0;
285 #ifdef CONFIG_SMP
286 if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
287 put_psr(get_psr() | PSR_EF);
288 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
289 &current->thread.fpqueue[0], &current->thread.fpqdepth);
290 regs->psr &= ~(PSR_EF);
291 clear_tsk_thread_flag(current, TIF_USEDFPU);
293 #else
294 if (current == last_task_used_math) {
295 put_psr(get_psr() | PSR_EF);
296 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
297 &current->thread.fpqueue[0], &current->thread.fpqdepth);
298 last_task_used_math = NULL;
299 regs->psr &= ~(PSR_EF);
301 #endif
302 err |= __copy_to_user(&fpu->si_float_regs[0],
303 &current->thread.float_regs[0],
304 (sizeof(unsigned long) * 32));
305 err |= __put_user(current->thread.fsr, &fpu->si_fsr);
306 err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
307 if (current->thread.fpqdepth != 0)
308 err |= __copy_to_user(&fpu->si_fpqueue[0],
309 &current->thread.fpqueue[0],
310 ((sizeof(unsigned long) +
311 (sizeof(unsigned long *)))*16));
312 clear_used_math();
313 return err;
316 static void setup_frame(struct k_sigaction *ka, struct pt_regs *regs,
317 int signo, sigset_t *oldset)
319 struct signal_frame __user *sf;
320 int sigframe_size, err;
322 /* 1. Make sure everything is clean */
323 synchronize_user_stack();
325 sigframe_size = SF_ALIGNEDSZ;
326 if (!used_math())
327 sigframe_size -= sizeof(__siginfo_fpu_t);
329 sf = (struct signal_frame __user *)
330 get_sigframe(&ka->sa, regs, sigframe_size);
332 if (invalid_frame_pointer(sf, sigframe_size))
333 goto sigill_and_return;
335 if (current_thread_info()->w_saved != 0)
336 goto sigill_and_return;
338 /* 2. Save the current process state */
339 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
341 err |= __put_user(0, &sf->extra_size);
343 if (used_math()) {
344 err |= save_fpu_state(regs, &sf->fpu_state);
345 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
346 } else {
347 err |= __put_user(0, &sf->fpu_save);
350 err |= __put_user(oldset->sig[0], &sf->info.si_mask);
351 err |= __copy_to_user(sf->extramask, &oldset->sig[1],
352 (_NSIG_WORDS - 1) * sizeof(unsigned int));
353 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
354 sizeof(struct reg_window32));
355 if (err)
356 goto sigsegv;
358 /* 3. signal handler back-trampoline and parameters */
359 regs->u_regs[UREG_FP] = (unsigned long) sf;
360 regs->u_regs[UREG_I0] = signo;
361 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
362 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
364 /* 4. signal handler */
365 regs->pc = (unsigned long) ka->sa.sa_handler;
366 regs->npc = (regs->pc + 4);
368 /* 5. return to kernel instructions */
369 if (ka->ka_restorer)
370 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
371 else {
372 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
374 /* mov __NR_sigreturn, %g1 */
375 err |= __put_user(0x821020d8, &sf->insns[0]);
377 /* t 0x10 */
378 err |= __put_user(0x91d02010, &sf->insns[1]);
379 if (err)
380 goto sigsegv;
382 /* Flush instruction space. */
383 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
385 return;
387 sigill_and_return:
388 do_exit(SIGILL);
389 sigsegv:
390 force_sigsegv(signo, current);
393 static void setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
394 int signo, sigset_t *oldset, siginfo_t *info)
396 struct rt_signal_frame __user *sf;
397 int sigframe_size;
398 unsigned int psr;
399 int err;
401 synchronize_user_stack();
402 sigframe_size = RT_ALIGNEDSZ;
403 if (!used_math())
404 sigframe_size -= sizeof(__siginfo_fpu_t);
405 sf = (struct rt_signal_frame __user *)
406 get_sigframe(&ka->sa, regs, sigframe_size);
407 if (invalid_frame_pointer(sf, sigframe_size))
408 goto sigill;
409 if (current_thread_info()->w_saved != 0)
410 goto sigill;
412 err = __put_user(regs->pc, &sf->regs.pc);
413 err |= __put_user(regs->npc, &sf->regs.npc);
414 err |= __put_user(regs->y, &sf->regs.y);
415 psr = regs->psr;
416 if (used_math())
417 psr |= PSR_EF;
418 err |= __put_user(psr, &sf->regs.psr);
419 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
420 err |= __put_user(0, &sf->extra_size);
422 if (psr & PSR_EF) {
423 err |= save_fpu_state(regs, &sf->fpu_state);
424 err |= __put_user(&sf->fpu_state, &sf->fpu_save);
425 } else {
426 err |= __put_user(0, &sf->fpu_save);
428 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
430 /* Setup sigaltstack */
431 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
432 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
433 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
435 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
436 sizeof(struct reg_window32));
438 err |= copy_siginfo_to_user(&sf->info, info);
440 if (err)
441 goto sigsegv;
443 regs->u_regs[UREG_FP] = (unsigned long) sf;
444 regs->u_regs[UREG_I0] = signo;
445 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
446 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
448 regs->pc = (unsigned long) ka->sa.sa_handler;
449 regs->npc = (regs->pc + 4);
451 if (ka->ka_restorer)
452 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
453 else {
454 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
456 /* mov __NR_sigreturn, %g1 */
457 err |= __put_user(0x821020d8, &sf->insns[0]);
459 /* t 0x10 */
460 err |= __put_user(0x91d02010, &sf->insns[1]);
461 if (err)
462 goto sigsegv;
464 /* Flush instruction space. */
465 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
467 return;
469 sigill:
470 do_exit(SIGILL);
471 sigsegv:
472 force_sigsegv(signo, current);
475 static inline void
476 handle_signal(unsigned long signr, struct k_sigaction *ka,
477 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
479 if (ka->sa.sa_flags & SA_SIGINFO)
480 setup_rt_frame(ka, regs, signr, oldset, info);
481 else
482 setup_frame(ka, regs, signr, oldset);
484 spin_lock_irq(&current->sighand->siglock);
485 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
486 if (!(ka->sa.sa_flags & SA_NOMASK))
487 sigaddset(&current->blocked, signr);
488 recalc_sigpending();
489 spin_unlock_irq(&current->sighand->siglock);
492 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
493 struct sigaction *sa)
495 switch(regs->u_regs[UREG_I0]) {
496 case ERESTART_RESTARTBLOCK:
497 case ERESTARTNOHAND:
498 no_system_call_restart:
499 regs->u_regs[UREG_I0] = EINTR;
500 regs->psr |= PSR_C;
501 break;
502 case ERESTARTSYS:
503 if (!(sa->sa_flags & SA_RESTART))
504 goto no_system_call_restart;
505 /* fallthrough */
506 case ERESTARTNOINTR:
507 regs->u_regs[UREG_I0] = orig_i0;
508 regs->pc -= 4;
509 regs->npc -= 4;
513 /* Note that 'init' is a special process: it doesn't get signals it doesn't
514 * want to handle. Thus you cannot kill init even with a SIGKILL even by
515 * mistake.
517 static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
519 struct k_sigaction ka;
520 int restart_syscall;
521 sigset_t *oldset;
522 siginfo_t info;
523 int signr;
525 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
526 restart_syscall = 1;
527 else
528 restart_syscall = 0;
530 if (test_thread_flag(TIF_RESTORE_SIGMASK))
531 oldset = &current->saved_sigmask;
532 else
533 oldset = &current->blocked;
535 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
537 /* If the debugger messes with the program counter, it clears
538 * the software "in syscall" bit, directing us to not perform
539 * a syscall restart.
541 if (restart_syscall && !pt_regs_is_syscall(regs))
542 restart_syscall = 0;
544 if (signr > 0) {
545 if (restart_syscall)
546 syscall_restart(orig_i0, regs, &ka.sa);
547 handle_signal(signr, &ka, &info, oldset, regs);
549 /* a signal was successfully delivered; the saved
550 * sigmask will have been stored in the signal frame,
551 * and will be restored by sigreturn, so we can simply
552 * clear the TIF_RESTORE_SIGMASK flag.
554 if (test_thread_flag(TIF_RESTORE_SIGMASK))
555 clear_thread_flag(TIF_RESTORE_SIGMASK);
557 tracehook_signal_handler(signr, &info, &ka, regs, 0);
558 return;
560 if (restart_syscall &&
561 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
562 regs->u_regs[UREG_I0] == ERESTARTSYS ||
563 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
564 /* replay the system call when we are done */
565 regs->u_regs[UREG_I0] = orig_i0;
566 regs->pc -= 4;
567 regs->npc -= 4;
569 if (restart_syscall &&
570 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
571 regs->u_regs[UREG_G1] = __NR_restart_syscall;
572 regs->pc -= 4;
573 regs->npc -= 4;
576 /* if there's no signal to deliver, we just put the saved sigmask
577 * back
579 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
580 clear_thread_flag(TIF_RESTORE_SIGMASK);
581 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
585 void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
586 unsigned long thread_info_flags)
588 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
589 do_signal(regs, orig_i0);
590 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
591 clear_thread_flag(TIF_NOTIFY_RESUME);
592 tracehook_notify_resume(regs);
596 asmlinkage int
597 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr,
598 unsigned long sp)
600 int ret = -EFAULT;
602 /* First see if old state is wanted. */
603 if (ossptr) {
604 if (put_user(current->sas_ss_sp + current->sas_ss_size,
605 &ossptr->the_stack) ||
606 __put_user(on_sig_stack(sp), &ossptr->cur_status))
607 goto out;
610 /* Now see if we want to update the new state. */
611 if (ssptr) {
612 char *ss_sp;
614 if (get_user(ss_sp, &ssptr->the_stack))
615 goto out;
616 /* If the current stack was set with sigaltstack, don't
617 swap stacks while we are on it. */
618 ret = -EPERM;
619 if (current->sas_ss_sp && on_sig_stack(sp))
620 goto out;
622 /* Since we don't know the extent of the stack, and we don't
623 track onstack-ness, but rather calculate it, we must
624 presume a size. Ho hum this interface is lossy. */
625 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
626 current->sas_ss_size = SIGSTKSZ;
628 ret = 0;
629 out:
630 return ret;