1 /* arch/sparc64/kernel/process.c
3 * Copyright (C) 1995, 1996, 2008 David S. Miller (davem@davemloft.net)
4 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1997, 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 * This file handles the architecture-dependent parts of process handling..
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
20 #include <linux/smp.h>
21 #include <linux/stddef.h>
22 #include <linux/ptrace.h>
23 #include <linux/slab.h>
24 #include <linux/user.h>
25 #include <linux/delay.h>
26 #include <linux/compat.h>
27 #include <linux/tick.h>
28 #include <linux/init.h>
29 #include <linux/cpu.h>
30 #include <linux/elfcore.h>
31 #include <linux/sysrq.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
36 #include <asm/pgalloc.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
39 #include <asm/pstate.h>
41 #include <asm/fpumacro.h>
43 #include <asm/cpudata.h>
44 #include <asm/mmu_context.h>
45 #include <asm/unistd.h>
46 #include <asm/hypervisor.h>
47 #include <asm/syscalls.h>
48 #include <asm/irq_regs.h>
53 static void sparc64_yield(int cpu)
55 if (tlb_type != hypervisor)
58 clear_thread_flag(TIF_POLLING_NRFLAG);
59 smp_mb__after_clear_bit();
61 while (!need_resched() && !cpu_is_offline(cpu)) {
64 /* Disable interrupts. */
66 "rdpr %%pstate, %0\n\t"
68 "wrpr %0, %%g0, %%pstate"
72 if (!need_resched() && !cpu_is_offline(cpu))
75 /* Re-enable interrupts. */
77 "rdpr %%pstate, %0\n\t"
79 "wrpr %0, %%g0, %%pstate"
84 set_thread_flag(TIF_POLLING_NRFLAG);
87 /* The idle loop on sparc64. */
90 int cpu = smp_processor_id();
92 set_thread_flag(TIF_POLLING_NRFLAG);
95 tick_nohz_stop_sched_tick(1);
97 while (!need_resched() && !cpu_is_offline(cpu))
100 tick_nohz_restart_sched_tick();
102 preempt_enable_no_resched();
104 #ifdef CONFIG_HOTPLUG_CPU
105 if (cpu_is_offline(cpu))
115 static void show_regwindow32(struct pt_regs *regs)
117 struct reg_window32 __user *rw;
118 struct reg_window32 r_w;
121 __asm__ __volatile__ ("flushw");
122 rw = compat_ptr((unsigned)regs->u_regs[14]);
125 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
131 printk("l0: %08x l1: %08x l2: %08x l3: %08x "
132 "l4: %08x l5: %08x l6: %08x l7: %08x\n",
133 r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
134 r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
135 printk("i0: %08x i1: %08x i2: %08x i3: %08x "
136 "i4: %08x i5: %08x i6: %08x i7: %08x\n",
137 r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
138 r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
141 #define show_regwindow32(regs) do { } while (0)
144 static void show_regwindow(struct pt_regs *regs)
146 struct reg_window __user *rw;
147 struct reg_window *rwk;
148 struct reg_window r_w;
151 if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
152 __asm__ __volatile__ ("flushw");
153 rw = (struct reg_window __user *)
154 (regs->u_regs[14] + STACK_BIAS);
155 rwk = (struct reg_window *)
156 (regs->u_regs[14] + STACK_BIAS);
157 if (!(regs->tstate & TSTATE_PRIV)) {
160 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
168 show_regwindow32(regs);
171 printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
172 rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
173 printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
174 rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
175 printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
176 rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
177 printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
178 rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
179 if (regs->tstate & TSTATE_PRIV)
180 printk("I7: <%pS>\n", (void *) rwk->ins[7]);
183 void show_regs(struct pt_regs *regs)
185 printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x %s\n", regs->tstate,
186 regs->tpc, regs->tnpc, regs->y, print_tainted());
187 printk("TPC: <%pS>\n", (void *) regs->tpc);
188 printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
189 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
191 printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
192 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
194 printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
195 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
197 printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
198 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
200 printk("RPC: <%pS>\n", (void *) regs->u_regs[15]);
201 show_regwindow(regs);
204 struct global_reg_snapshot global_reg_snapshot[NR_CPUS];
205 static DEFINE_SPINLOCK(global_reg_snapshot_lock);
207 static void __global_reg_self(struct thread_info *tp, struct pt_regs *regs,
212 global_reg_snapshot[this_cpu].tstate = regs->tstate;
213 global_reg_snapshot[this_cpu].tpc = regs->tpc;
214 global_reg_snapshot[this_cpu].tnpc = regs->tnpc;
215 global_reg_snapshot[this_cpu].o7 = regs->u_regs[UREG_I7];
217 if (regs->tstate & TSTATE_PRIV) {
218 struct thread_info *tp = current_thread_info();
219 struct reg_window *rw;
221 rw = (struct reg_window *)
222 (regs->u_regs[UREG_FP] + STACK_BIAS);
223 if (kstack_valid(tp, (unsigned long) rw)) {
224 global_reg_snapshot[this_cpu].i7 = rw->ins[7];
225 rw = (struct reg_window *)
226 (rw->ins[6] + STACK_BIAS);
227 if (kstack_valid(tp, (unsigned long) rw))
228 global_reg_snapshot[this_cpu].rpc = rw->ins[7];
231 global_reg_snapshot[this_cpu].i7 = 0;
232 global_reg_snapshot[this_cpu].rpc = 0;
234 global_reg_snapshot[this_cpu].thread = tp;
237 /* In order to avoid hangs we do not try to synchronize with the
238 * global register dump client cpus. The last store they make is to
239 * the thread pointer, so do a short poll waiting for that to become
242 static void __global_reg_poll(struct global_reg_snapshot *gp)
246 while (!gp->thread && ++limit < 100) {
252 void __trigger_all_cpu_backtrace(void)
254 struct thread_info *tp = current_thread_info();
255 struct pt_regs *regs = get_irq_regs();
262 spin_lock_irqsave(&global_reg_snapshot_lock, flags);
264 memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
266 this_cpu = raw_smp_processor_id();
268 __global_reg_self(tp, regs, this_cpu);
270 smp_fetch_global_regs();
272 for_each_online_cpu(cpu) {
273 struct global_reg_snapshot *gp = &global_reg_snapshot[cpu];
274 struct thread_info *tp;
276 __global_reg_poll(gp);
279 printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n",
280 (cpu == this_cpu ? '*' : ' '), cpu,
281 gp->tstate, gp->tpc, gp->tnpc,
282 ((tp && tp->task) ? tp->task->comm : "NULL"),
283 ((tp && tp->task) ? tp->task->pid : -1));
285 if (gp->tstate & TSTATE_PRIV) {
286 printk(" TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n",
292 printk(" TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n",
293 gp->tpc, gp->o7, gp->i7, gp->rpc);
297 memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
299 spin_unlock_irqrestore(&global_reg_snapshot_lock, flags);
302 #ifdef CONFIG_MAGIC_SYSRQ
304 static void sysrq_handle_globreg(int key, struct tty_struct *tty)
306 __trigger_all_cpu_backtrace();
309 static struct sysrq_key_op sparc_globalreg_op = {
310 .handler = sysrq_handle_globreg,
311 .help_msg = "Globalregs",
312 .action_msg = "Show Global CPU Regs",
315 static int __init sparc_globreg_init(void)
317 return register_sysrq_key('y', &sparc_globalreg_op);
320 core_initcall(sparc_globreg_init);
324 unsigned long thread_saved_pc(struct task_struct *tsk)
326 struct thread_info *ti = task_thread_info(tsk);
327 unsigned long ret = 0xdeadbeefUL;
331 sp = (unsigned long *)(ti->ksp + STACK_BIAS);
332 if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
335 fp = (unsigned long *)(sp[14] + STACK_BIAS);
336 if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
343 /* Free current thread data structures etc.. */
344 void exit_thread(void)
346 struct thread_info *t = current_thread_info();
349 if (t->utraps[0] < 2)
355 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
356 t->user_cntd0 = t->user_cntd1 = NULL;
362 void flush_thread(void)
364 struct thread_info *t = current_thread_info();
365 struct mm_struct *mm;
367 if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
368 clear_ti_thread_flag(t, TIF_ABI_PENDING);
369 if (test_ti_thread_flag(t, TIF_32BIT))
370 clear_ti_thread_flag(t, TIF_32BIT);
372 set_ti_thread_flag(t, TIF_32BIT);
377 tsb_context_switch(mm);
379 set_thread_wsaved(0);
381 /* Turn off performance counters if on. */
382 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
383 t->user_cntd0 = t->user_cntd1 = NULL;
388 /* Clear FPU register state. */
391 if (get_thread_current_ds() != ASI_AIUS)
395 /* It's a bit more tricky when 64-bit tasks are involved... */
396 static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
398 unsigned long fp, distance, rval;
400 if (!(test_thread_flag(TIF_32BIT))) {
403 __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
406 __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
408 /* Now 8-byte align the stack as this is mandatory in the
409 * Sparc ABI due to how register windows work. This hides
410 * the restriction from thread libraries etc. -DaveM
415 rval = (csp - distance);
416 if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
418 else if (test_thread_flag(TIF_32BIT)) {
419 if (put_user(((u32)csp),
420 &(((struct reg_window32 __user *)rval)->ins[6])))
423 if (put_user(((u64)csp - STACK_BIAS),
424 &(((struct reg_window __user *)rval)->ins[6])))
427 rval = rval - STACK_BIAS;
433 /* Standard stuff. */
434 static inline void shift_window_buffer(int first_win, int last_win,
435 struct thread_info *t)
439 for (i = first_win; i < last_win; i++) {
440 t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
441 memcpy(&t->reg_window[i], &t->reg_window[i+1],
442 sizeof(struct reg_window));
446 void synchronize_user_stack(void)
448 struct thread_info *t = current_thread_info();
449 unsigned long window;
451 flush_user_windows();
452 if ((window = get_thread_wsaved()) != 0) {
453 int winsize = sizeof(struct reg_window);
456 if (test_thread_flag(TIF_32BIT))
457 winsize = sizeof(struct reg_window32);
463 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
464 struct reg_window *rwin = &t->reg_window[window];
466 if (!copy_to_user((char __user *)sp, rwin, winsize)) {
467 shift_window_buffer(window, get_thread_wsaved() - 1, t);
468 set_thread_wsaved(get_thread_wsaved() - 1);
474 static void stack_unaligned(unsigned long sp)
478 info.si_signo = SIGBUS;
480 info.si_code = BUS_ADRALN;
481 info.si_addr = (void __user *) sp;
483 force_sig_info(SIGBUS, &info, current);
486 void fault_in_user_windows(void)
488 struct thread_info *t = current_thread_info();
489 unsigned long window;
490 int winsize = sizeof(struct reg_window);
493 if (test_thread_flag(TIF_32BIT))
494 winsize = sizeof(struct reg_window32);
498 flush_user_windows();
499 window = get_thread_wsaved();
501 if (likely(window != 0)) {
504 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
505 struct reg_window *rwin = &t->reg_window[window];
507 if (unlikely(sp & 0x7UL))
510 if (unlikely(copy_to_user((char __user *)sp,
515 set_thread_wsaved(0);
519 set_thread_wsaved(window + 1);
523 asmlinkage long sparc_do_fork(unsigned long clone_flags,
524 unsigned long stack_start,
525 struct pt_regs *regs,
526 unsigned long stack_size)
528 int __user *parent_tid_ptr, *child_tid_ptr;
529 unsigned long orig_i1 = regs->u_regs[UREG_I1];
533 if (test_thread_flag(TIF_32BIT)) {
534 parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
535 child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
539 parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
540 child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
543 ret = do_fork(clone_flags, stack_start,
545 parent_tid_ptr, child_tid_ptr);
547 /* If we get an error and potentially restart the system
548 * call, we're screwed because copy_thread() clobbered
549 * the parent's %o1. So detect that case and restore it
552 if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
553 regs->u_regs[UREG_I1] = orig_i1;
558 /* Copy a Sparc thread. The fork() return value conventions
559 * under SunOS are nothing short of bletcherous:
560 * Parent --> %o0 == childs pid, %o1 == 0
561 * Child --> %o0 == parents pid, %o1 == 1
563 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
564 unsigned long unused,
565 struct task_struct *p, struct pt_regs *regs)
567 struct thread_info *t = task_thread_info(p);
568 struct sparc_stackf *parent_sf;
569 unsigned long child_stack_sz;
570 char *child_trap_frame;
573 kernel_thread = (regs->tstate & TSTATE_PRIV) ? 1 : 0;
574 parent_sf = ((struct sparc_stackf *) regs) - 1;
576 /* Calculate offset to stack_frame & pt_regs */
577 child_stack_sz = ((STACKFRAME_SZ + TRACEREG_SZ) +
578 (kernel_thread ? STACKFRAME_SZ : 0));
579 child_trap_frame = (task_stack_page(p) +
580 (THREAD_SIZE - child_stack_sz));
581 memcpy(child_trap_frame, parent_sf, child_stack_sz);
583 t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) |
584 (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
585 (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
587 t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
588 t->kregs = (struct pt_regs *) (child_trap_frame +
589 sizeof(struct sparc_stackf));
593 struct sparc_stackf *child_sf = (struct sparc_stackf *)
594 (child_trap_frame + (STACKFRAME_SZ + TRACEREG_SZ));
596 /* Zero terminate the stack backtrace. */
598 t->kregs->u_regs[UREG_FP] =
599 ((unsigned long) child_sf) - STACK_BIAS;
601 /* Special case, if we are spawning a kernel thread from
602 * a userspace task (usermode helper, NFS or similar), we
603 * must disable performance counters in the child because
604 * the address space and protection realm are changing.
606 if (t->flags & _TIF_PERFCTR) {
607 t->user_cntd0 = t->user_cntd1 = NULL;
609 t->flags &= ~_TIF_PERFCTR;
611 t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
612 t->kregs->u_regs[UREG_G6] = (unsigned long) t;
613 t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
615 if (t->flags & _TIF_32BIT) {
616 sp &= 0x00000000ffffffffUL;
617 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
619 t->kregs->u_regs[UREG_FP] = sp;
620 t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
621 if (sp != regs->u_regs[UREG_FP]) {
624 csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
627 t->kregs->u_regs[UREG_FP] = csp;
633 /* Set the return value for the child. */
634 t->kregs->u_regs[UREG_I0] = current->pid;
635 t->kregs->u_regs[UREG_I1] = 1;
637 /* Set the second return value for the parent. */
638 regs->u_regs[UREG_I1] = 0;
640 if (clone_flags & CLONE_SETTLS)
641 t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
647 * This is the mechanism for creating a new kernel thread.
649 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
650 * who haven't done an "execve()") should use this: it will work within
651 * a system call from a "real" process, but the process memory space will
652 * not be freed until both the parent and the child have exited.
654 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
658 /* If the parent runs before fn(arg) is called by the child,
659 * the input registers of this function can be clobbered.
660 * So we stash 'fn' and 'arg' into global registers which
661 * will not be modified by the parent.
663 __asm__ __volatile__("mov %4, %%g2\n\t" /* Save FN into global */
664 "mov %5, %%g3\n\t" /* Save ARG into global */
665 "mov %1, %%g1\n\t" /* Clone syscall nr. */
666 "mov %2, %%o0\n\t" /* Clone flags. */
667 "mov 0, %%o1\n\t" /* usp arg == 0 */
668 "t 0x6d\n\t" /* Linux/Sparc clone(). */
669 "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
671 "jmpl %%g2, %%o7\n\t" /* Call the function. */
672 " mov %%g3, %%o0\n\t" /* Set arg in delay. */
674 "t 0x6d\n\t" /* Linux/Sparc exit(). */
675 /* Notreached by child. */
678 "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
679 "i" (__NR_exit), "r" (fn), "r" (arg) :
680 "g1", "g2", "g3", "o0", "o1", "memory", "cc");
686 unsigned int pr_regs[32];
687 unsigned long pr_dregs[16];
689 unsigned int __unused;
691 unsigned char pr_qcnt;
692 unsigned char pr_q_entrysize;
694 unsigned int pr_q[64];
698 * fill in the fpu structure for a core dump.
700 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
702 unsigned long *kfpregs = current_thread_info()->fpregs;
703 unsigned long fprs = current_thread_info()->fpsaved[0];
705 if (test_thread_flag(TIF_32BIT)) {
706 elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
709 memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
710 sizeof(unsigned int) * 32);
712 memset(&fpregs32->pr_fr.pr_regs[0], 0,
713 sizeof(unsigned int) * 32);
714 fpregs32->pr_qcnt = 0;
715 fpregs32->pr_q_entrysize = 8;
716 memset(&fpregs32->pr_q[0], 0,
717 (sizeof(unsigned int) * 64));
718 if (fprs & FPRS_FEF) {
719 fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
722 fpregs32->pr_fsr = 0;
727 memcpy(&fpregs->pr_regs[0], kfpregs,
728 sizeof(unsigned int) * 32);
730 memset(&fpregs->pr_regs[0], 0,
731 sizeof(unsigned int) * 32);
733 memcpy(&fpregs->pr_regs[16], kfpregs+16,
734 sizeof(unsigned int) * 32);
736 memset(&fpregs->pr_regs[16], 0,
737 sizeof(unsigned int) * 32);
738 if(fprs & FPRS_FEF) {
739 fpregs->pr_fsr = current_thread_info()->xfsr[0];
740 fpregs->pr_gsr = current_thread_info()->gsr[0];
742 fpregs->pr_fsr = fpregs->pr_gsr = 0;
744 fpregs->pr_fprs = fprs;
750 * sparc_execve() executes a new program after the asm stub has set
751 * things up for us. This should basically do what I want it to.
753 asmlinkage int sparc_execve(struct pt_regs *regs)
758 /* User register window flush is done by entry.S */
760 /* Check for indirect call. */
761 if (regs->u_regs[UREG_G1] == 0)
764 filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
765 error = PTR_ERR(filename);
766 if (IS_ERR(filename))
768 error = do_execve(filename,
769 (char __user * __user *)
770 regs->u_regs[base + UREG_I1],
771 (char __user * __user *)
772 regs->u_regs[base + UREG_I2], regs);
776 current_thread_info()->xfsr[0] = 0;
777 current_thread_info()->fpsaved[0] = 0;
778 regs->tstate &= ~TSTATE_PEF;
784 unsigned long get_wchan(struct task_struct *task)
786 unsigned long pc, fp, bias = 0;
787 struct thread_info *tp;
788 struct reg_window *rw;
789 unsigned long ret = 0;
792 if (!task || task == current ||
793 task->state == TASK_RUNNING)
796 tp = task_thread_info(task);
798 fp = task_thread_info(task)->ksp + bias;
801 if (!kstack_valid(tp, fp))
803 rw = (struct reg_window *) fp;
805 if (!in_sched_functions(pc)) {
809 fp = rw->ins[6] + bias;
810 } while (++count < 16);