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 reg_window *rw;
220 rw = (struct reg_window *)
221 (regs->u_regs[UREG_FP] + STACK_BIAS);
222 if (kstack_valid(tp, (unsigned long) rw)) {
223 global_reg_snapshot[this_cpu].i7 = rw->ins[7];
224 rw = (struct reg_window *)
225 (rw->ins[6] + STACK_BIAS);
226 if (kstack_valid(tp, (unsigned long) rw))
227 global_reg_snapshot[this_cpu].rpc = rw->ins[7];
230 global_reg_snapshot[this_cpu].i7 = 0;
231 global_reg_snapshot[this_cpu].rpc = 0;
233 global_reg_snapshot[this_cpu].thread = tp;
236 /* In order to avoid hangs we do not try to synchronize with the
237 * global register dump client cpus. The last store they make is to
238 * the thread pointer, so do a short poll waiting for that to become
241 static void __global_reg_poll(struct global_reg_snapshot *gp)
245 while (!gp->thread && ++limit < 100) {
251 void __trigger_all_cpu_backtrace(void)
253 struct thread_info *tp = current_thread_info();
254 struct pt_regs *regs = get_irq_regs();
261 spin_lock_irqsave(&global_reg_snapshot_lock, flags);
263 memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
265 this_cpu = raw_smp_processor_id();
267 __global_reg_self(tp, regs, this_cpu);
269 smp_fetch_global_regs();
271 for_each_online_cpu(cpu) {
272 struct global_reg_snapshot *gp = &global_reg_snapshot[cpu];
274 __global_reg_poll(gp);
277 printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n",
278 (cpu == this_cpu ? '*' : ' '), cpu,
279 gp->tstate, gp->tpc, gp->tnpc,
280 ((tp && tp->task) ? tp->task->comm : "NULL"),
281 ((tp && tp->task) ? tp->task->pid : -1));
283 if (gp->tstate & TSTATE_PRIV) {
284 printk(" TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n",
290 printk(" TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n",
291 gp->tpc, gp->o7, gp->i7, gp->rpc);
295 memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
297 spin_unlock_irqrestore(&global_reg_snapshot_lock, flags);
300 #ifdef CONFIG_MAGIC_SYSRQ
302 static void sysrq_handle_globreg(int key, struct tty_struct *tty)
304 __trigger_all_cpu_backtrace();
307 static struct sysrq_key_op sparc_globalreg_op = {
308 .handler = sysrq_handle_globreg,
309 .help_msg = "Globalregs",
310 .action_msg = "Show Global CPU Regs",
313 static int __init sparc_globreg_init(void)
315 return register_sysrq_key('y', &sparc_globalreg_op);
318 core_initcall(sparc_globreg_init);
322 unsigned long thread_saved_pc(struct task_struct *tsk)
324 struct thread_info *ti = task_thread_info(tsk);
325 unsigned long ret = 0xdeadbeefUL;
329 sp = (unsigned long *)(ti->ksp + STACK_BIAS);
330 if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
333 fp = (unsigned long *)(sp[14] + STACK_BIAS);
334 if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
341 /* Free current thread data structures etc.. */
342 void exit_thread(void)
344 struct thread_info *t = current_thread_info();
347 if (t->utraps[0] < 2)
353 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
354 t->user_cntd0 = t->user_cntd1 = NULL;
360 void flush_thread(void)
362 struct thread_info *t = current_thread_info();
363 struct mm_struct *mm;
365 if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
366 clear_ti_thread_flag(t, TIF_ABI_PENDING);
367 if (test_ti_thread_flag(t, TIF_32BIT))
368 clear_ti_thread_flag(t, TIF_32BIT);
370 set_ti_thread_flag(t, TIF_32BIT);
375 tsb_context_switch(mm);
377 set_thread_wsaved(0);
379 /* Turn off performance counters if on. */
380 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
381 t->user_cntd0 = t->user_cntd1 = NULL;
386 /* Clear FPU register state. */
389 if (get_thread_current_ds() != ASI_AIUS)
393 /* It's a bit more tricky when 64-bit tasks are involved... */
394 static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
396 unsigned long fp, distance, rval;
398 if (!(test_thread_flag(TIF_32BIT))) {
401 __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
404 __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
406 /* Now 8-byte align the stack as this is mandatory in the
407 * Sparc ABI due to how register windows work. This hides
408 * the restriction from thread libraries etc. -DaveM
413 rval = (csp - distance);
414 if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
416 else if (test_thread_flag(TIF_32BIT)) {
417 if (put_user(((u32)csp),
418 &(((struct reg_window32 __user *)rval)->ins[6])))
421 if (put_user(((u64)csp - STACK_BIAS),
422 &(((struct reg_window __user *)rval)->ins[6])))
425 rval = rval - STACK_BIAS;
431 /* Standard stuff. */
432 static inline void shift_window_buffer(int first_win, int last_win,
433 struct thread_info *t)
437 for (i = first_win; i < last_win; i++) {
438 t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
439 memcpy(&t->reg_window[i], &t->reg_window[i+1],
440 sizeof(struct reg_window));
444 void synchronize_user_stack(void)
446 struct thread_info *t = current_thread_info();
447 unsigned long window;
449 flush_user_windows();
450 if ((window = get_thread_wsaved()) != 0) {
451 int winsize = sizeof(struct reg_window);
454 if (test_thread_flag(TIF_32BIT))
455 winsize = sizeof(struct reg_window32);
461 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
462 struct reg_window *rwin = &t->reg_window[window];
464 if (!copy_to_user((char __user *)sp, rwin, winsize)) {
465 shift_window_buffer(window, get_thread_wsaved() - 1, t);
466 set_thread_wsaved(get_thread_wsaved() - 1);
472 static void stack_unaligned(unsigned long sp)
476 info.si_signo = SIGBUS;
478 info.si_code = BUS_ADRALN;
479 info.si_addr = (void __user *) sp;
481 force_sig_info(SIGBUS, &info, current);
484 void fault_in_user_windows(void)
486 struct thread_info *t = current_thread_info();
487 unsigned long window;
488 int winsize = sizeof(struct reg_window);
491 if (test_thread_flag(TIF_32BIT))
492 winsize = sizeof(struct reg_window32);
496 flush_user_windows();
497 window = get_thread_wsaved();
499 if (likely(window != 0)) {
502 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
503 struct reg_window *rwin = &t->reg_window[window];
505 if (unlikely(sp & 0x7UL))
508 if (unlikely(copy_to_user((char __user *)sp,
513 set_thread_wsaved(0);
517 set_thread_wsaved(window + 1);
521 asmlinkage long sparc_do_fork(unsigned long clone_flags,
522 unsigned long stack_start,
523 struct pt_regs *regs,
524 unsigned long stack_size)
526 int __user *parent_tid_ptr, *child_tid_ptr;
527 unsigned long orig_i1 = regs->u_regs[UREG_I1];
531 if (test_thread_flag(TIF_32BIT)) {
532 parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
533 child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
537 parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
538 child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
541 ret = do_fork(clone_flags, stack_start,
543 parent_tid_ptr, child_tid_ptr);
545 /* If we get an error and potentially restart the system
546 * call, we're screwed because copy_thread() clobbered
547 * the parent's %o1. So detect that case and restore it
550 if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
551 regs->u_regs[UREG_I1] = orig_i1;
556 /* Copy a Sparc thread. The fork() return value conventions
557 * under SunOS are nothing short of bletcherous:
558 * Parent --> %o0 == childs pid, %o1 == 0
559 * Child --> %o0 == parents pid, %o1 == 1
561 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
562 unsigned long unused,
563 struct task_struct *p, struct pt_regs *regs)
565 struct thread_info *t = task_thread_info(p);
566 struct sparc_stackf *parent_sf;
567 unsigned long child_stack_sz;
568 char *child_trap_frame;
571 kernel_thread = (regs->tstate & TSTATE_PRIV) ? 1 : 0;
572 parent_sf = ((struct sparc_stackf *) regs) - 1;
574 /* Calculate offset to stack_frame & pt_regs */
575 child_stack_sz = ((STACKFRAME_SZ + TRACEREG_SZ) +
576 (kernel_thread ? STACKFRAME_SZ : 0));
577 child_trap_frame = (task_stack_page(p) +
578 (THREAD_SIZE - child_stack_sz));
579 memcpy(child_trap_frame, parent_sf, child_stack_sz);
581 t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) |
582 (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
583 (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
585 t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
586 t->kregs = (struct pt_regs *) (child_trap_frame +
587 sizeof(struct sparc_stackf));
591 struct sparc_stackf *child_sf = (struct sparc_stackf *)
592 (child_trap_frame + (STACKFRAME_SZ + TRACEREG_SZ));
594 /* Zero terminate the stack backtrace. */
596 t->kregs->u_regs[UREG_FP] =
597 ((unsigned long) child_sf) - STACK_BIAS;
599 /* Special case, if we are spawning a kernel thread from
600 * a userspace task (usermode helper, NFS or similar), we
601 * must disable performance counters in the child because
602 * the address space and protection realm are changing.
604 if (t->flags & _TIF_PERFCTR) {
605 t->user_cntd0 = t->user_cntd1 = NULL;
607 t->flags &= ~_TIF_PERFCTR;
609 t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
610 t->kregs->u_regs[UREG_G6] = (unsigned long) t;
611 t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
613 if (t->flags & _TIF_32BIT) {
614 sp &= 0x00000000ffffffffUL;
615 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
617 t->kregs->u_regs[UREG_FP] = sp;
618 t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
619 if (sp != regs->u_regs[UREG_FP]) {
622 csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
625 t->kregs->u_regs[UREG_FP] = csp;
631 /* Set the return value for the child. */
632 t->kregs->u_regs[UREG_I0] = current->pid;
633 t->kregs->u_regs[UREG_I1] = 1;
635 /* Set the second return value for the parent. */
636 regs->u_regs[UREG_I1] = 0;
638 if (clone_flags & CLONE_SETTLS)
639 t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
645 * This is the mechanism for creating a new kernel thread.
647 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
648 * who haven't done an "execve()") should use this: it will work within
649 * a system call from a "real" process, but the process memory space will
650 * not be freed until both the parent and the child have exited.
652 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
656 /* If the parent runs before fn(arg) is called by the child,
657 * the input registers of this function can be clobbered.
658 * So we stash 'fn' and 'arg' into global registers which
659 * will not be modified by the parent.
661 __asm__ __volatile__("mov %4, %%g2\n\t" /* Save FN into global */
662 "mov %5, %%g3\n\t" /* Save ARG into global */
663 "mov %1, %%g1\n\t" /* Clone syscall nr. */
664 "mov %2, %%o0\n\t" /* Clone flags. */
665 "mov 0, %%o1\n\t" /* usp arg == 0 */
666 "t 0x6d\n\t" /* Linux/Sparc clone(). */
667 "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
669 "jmpl %%g2, %%o7\n\t" /* Call the function. */
670 " mov %%g3, %%o0\n\t" /* Set arg in delay. */
672 "t 0x6d\n\t" /* Linux/Sparc exit(). */
673 /* Notreached by child. */
676 "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
677 "i" (__NR_exit), "r" (fn), "r" (arg) :
678 "g1", "g2", "g3", "o0", "o1", "memory", "cc");
684 unsigned int pr_regs[32];
685 unsigned long pr_dregs[16];
687 unsigned int __unused;
689 unsigned char pr_qcnt;
690 unsigned char pr_q_entrysize;
692 unsigned int pr_q[64];
696 * fill in the fpu structure for a core dump.
698 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
700 unsigned long *kfpregs = current_thread_info()->fpregs;
701 unsigned long fprs = current_thread_info()->fpsaved[0];
703 if (test_thread_flag(TIF_32BIT)) {
704 elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
707 memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
708 sizeof(unsigned int) * 32);
710 memset(&fpregs32->pr_fr.pr_regs[0], 0,
711 sizeof(unsigned int) * 32);
712 fpregs32->pr_qcnt = 0;
713 fpregs32->pr_q_entrysize = 8;
714 memset(&fpregs32->pr_q[0], 0,
715 (sizeof(unsigned int) * 64));
716 if (fprs & FPRS_FEF) {
717 fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
720 fpregs32->pr_fsr = 0;
725 memcpy(&fpregs->pr_regs[0], kfpregs,
726 sizeof(unsigned int) * 32);
728 memset(&fpregs->pr_regs[0], 0,
729 sizeof(unsigned int) * 32);
731 memcpy(&fpregs->pr_regs[16], kfpregs+16,
732 sizeof(unsigned int) * 32);
734 memset(&fpregs->pr_regs[16], 0,
735 sizeof(unsigned int) * 32);
736 if(fprs & FPRS_FEF) {
737 fpregs->pr_fsr = current_thread_info()->xfsr[0];
738 fpregs->pr_gsr = current_thread_info()->gsr[0];
740 fpregs->pr_fsr = fpregs->pr_gsr = 0;
742 fpregs->pr_fprs = fprs;
748 * sparc_execve() executes a new program after the asm stub has set
749 * things up for us. This should basically do what I want it to.
751 asmlinkage int sparc_execve(struct pt_regs *regs)
756 /* User register window flush is done by entry.S */
758 /* Check for indirect call. */
759 if (regs->u_regs[UREG_G1] == 0)
762 filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
763 error = PTR_ERR(filename);
764 if (IS_ERR(filename))
766 error = do_execve(filename,
767 (char __user * __user *)
768 regs->u_regs[base + UREG_I1],
769 (char __user * __user *)
770 regs->u_regs[base + UREG_I2], regs);
774 current_thread_info()->xfsr[0] = 0;
775 current_thread_info()->fpsaved[0] = 0;
776 regs->tstate &= ~TSTATE_PEF;
782 unsigned long get_wchan(struct task_struct *task)
784 unsigned long pc, fp, bias = 0;
785 struct thread_info *tp;
786 struct reg_window *rw;
787 unsigned long ret = 0;
790 if (!task || task == current ||
791 task->state == TASK_RUNNING)
794 tp = task_thread_info(task);
796 fp = task_thread_info(task)->ksp + bias;
799 if (!kstack_valid(tp, fp))
801 rw = (struct reg_window *) fp;
803 if (!in_sched_functions(pc)) {
807 fp = rw->ins[6] + bias;
808 } while (++count < 16);