xref: /openbmc/linux/arch/sparc/kernel/process_64.c (revision 6aa7de05)
1 /*  arch/sparc64/kernel/process.c
2  *
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)
6  */
7 
8 /*
9  * This file handles the architecture-dependent parts of process handling..
10  */
11 
12 #include <stdarg.h>
13 
14 #include <linux/errno.h>
15 #include <linux/export.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched/task.h>
19 #include <linux/sched/task_stack.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/fs.h>
23 #include <linux/smp.h>
24 #include <linux/stddef.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/user.h>
28 #include <linux/delay.h>
29 #include <linux/compat.h>
30 #include <linux/tick.h>
31 #include <linux/init.h>
32 #include <linux/cpu.h>
33 #include <linux/perf_event.h>
34 #include <linux/elfcore.h>
35 #include <linux/sysrq.h>
36 #include <linux/nmi.h>
37 #include <linux/context_tracking.h>
38 
39 #include <linux/uaccess.h>
40 #include <asm/page.h>
41 #include <asm/pgalloc.h>
42 #include <asm/pgtable.h>
43 #include <asm/processor.h>
44 #include <asm/pstate.h>
45 #include <asm/elf.h>
46 #include <asm/fpumacro.h>
47 #include <asm/head.h>
48 #include <asm/cpudata.h>
49 #include <asm/mmu_context.h>
50 #include <asm/unistd.h>
51 #include <asm/hypervisor.h>
52 #include <asm/syscalls.h>
53 #include <asm/irq_regs.h>
54 #include <asm/smp.h>
55 #include <asm/pcr.h>
56 
57 #include "kstack.h"
58 
59 /* Idle loop support on sparc64. */
60 void arch_cpu_idle(void)
61 {
62 	if (tlb_type != hypervisor) {
63 		touch_nmi_watchdog();
64 		local_irq_enable();
65 	} else {
66 		unsigned long pstate;
67 
68 		local_irq_enable();
69 
70                 /* The sun4v sleeping code requires that we have PSTATE.IE cleared over
71                  * the cpu sleep hypervisor call.
72                  */
73 		__asm__ __volatile__(
74 			"rdpr %%pstate, %0\n\t"
75 			"andn %0, %1, %0\n\t"
76 			"wrpr %0, %%g0, %%pstate"
77 			: "=&r" (pstate)
78 			: "i" (PSTATE_IE));
79 
80 		if (!need_resched() && !cpu_is_offline(smp_processor_id())) {
81 			sun4v_cpu_yield();
82 			/* If resumed by cpu_poke then we need to explicitly
83 			 * call scheduler_ipi().
84 			 */
85 			scheduler_poke();
86 		}
87 
88 		/* Re-enable interrupts. */
89 		__asm__ __volatile__(
90 			"rdpr %%pstate, %0\n\t"
91 			"or %0, %1, %0\n\t"
92 			"wrpr %0, %%g0, %%pstate"
93 			: "=&r" (pstate)
94 			: "i" (PSTATE_IE));
95 	}
96 }
97 
98 #ifdef CONFIG_HOTPLUG_CPU
99 void arch_cpu_idle_dead(void)
100 {
101 	sched_preempt_enable_no_resched();
102 	cpu_play_dead();
103 }
104 #endif
105 
106 #ifdef CONFIG_COMPAT
107 static void show_regwindow32(struct pt_regs *regs)
108 {
109 	struct reg_window32 __user *rw;
110 	struct reg_window32 r_w;
111 	mm_segment_t old_fs;
112 
113 	__asm__ __volatile__ ("flushw");
114 	rw = compat_ptr((unsigned int)regs->u_regs[14]);
115 	old_fs = get_fs();
116 	set_fs (USER_DS);
117 	if (copy_from_user (&r_w, rw, sizeof(r_w))) {
118 		set_fs (old_fs);
119 		return;
120 	}
121 
122 	set_fs (old_fs);
123 	printk("l0: %08x l1: %08x l2: %08x l3: %08x "
124 	       "l4: %08x l5: %08x l6: %08x l7: %08x\n",
125 	       r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
126 	       r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
127 	printk("i0: %08x i1: %08x i2: %08x i3: %08x "
128 	       "i4: %08x i5: %08x i6: %08x i7: %08x\n",
129 	       r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
130 	       r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
131 }
132 #else
133 #define show_regwindow32(regs)	do { } while (0)
134 #endif
135 
136 static void show_regwindow(struct pt_regs *regs)
137 {
138 	struct reg_window __user *rw;
139 	struct reg_window *rwk;
140 	struct reg_window r_w;
141 	mm_segment_t old_fs;
142 
143 	if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
144 		__asm__ __volatile__ ("flushw");
145 		rw = (struct reg_window __user *)
146 			(regs->u_regs[14] + STACK_BIAS);
147 		rwk = (struct reg_window *)
148 			(regs->u_regs[14] + STACK_BIAS);
149 		if (!(regs->tstate & TSTATE_PRIV)) {
150 			old_fs = get_fs();
151 			set_fs (USER_DS);
152 			if (copy_from_user (&r_w, rw, sizeof(r_w))) {
153 				set_fs (old_fs);
154 				return;
155 			}
156 			rwk = &r_w;
157 			set_fs (old_fs);
158 		}
159 	} else {
160 		show_regwindow32(regs);
161 		return;
162 	}
163 	printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
164 	       rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
165 	printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
166 	       rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
167 	printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
168 	       rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
169 	printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
170 	       rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
171 	if (regs->tstate & TSTATE_PRIV)
172 		printk("I7: <%pS>\n", (void *) rwk->ins[7]);
173 }
174 
175 void show_regs(struct pt_regs *regs)
176 {
177 	show_regs_print_info(KERN_DEFAULT);
178 
179 	printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x    %s\n", regs->tstate,
180 	       regs->tpc, regs->tnpc, regs->y, print_tainted());
181 	printk("TPC: <%pS>\n", (void *) regs->tpc);
182 	printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
183 	       regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
184 	       regs->u_regs[3]);
185 	printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
186 	       regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
187 	       regs->u_regs[7]);
188 	printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
189 	       regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
190 	       regs->u_regs[11]);
191 	printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
192 	       regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
193 	       regs->u_regs[15]);
194 	printk("RPC: <%pS>\n", (void *) regs->u_regs[15]);
195 	show_regwindow(regs);
196 	show_stack(current, (unsigned long *) regs->u_regs[UREG_FP]);
197 }
198 
199 union global_cpu_snapshot global_cpu_snapshot[NR_CPUS];
200 static DEFINE_SPINLOCK(global_cpu_snapshot_lock);
201 
202 static void __global_reg_self(struct thread_info *tp, struct pt_regs *regs,
203 			      int this_cpu)
204 {
205 	struct global_reg_snapshot *rp;
206 
207 	flushw_all();
208 
209 	rp = &global_cpu_snapshot[this_cpu].reg;
210 
211 	rp->tstate = regs->tstate;
212 	rp->tpc = regs->tpc;
213 	rp->tnpc = regs->tnpc;
214 	rp->o7 = regs->u_regs[UREG_I7];
215 
216 	if (regs->tstate & TSTATE_PRIV) {
217 		struct reg_window *rw;
218 
219 		rw = (struct reg_window *)
220 			(regs->u_regs[UREG_FP] + STACK_BIAS);
221 		if (kstack_valid(tp, (unsigned long) rw)) {
222 			rp->i7 = rw->ins[7];
223 			rw = (struct reg_window *)
224 				(rw->ins[6] + STACK_BIAS);
225 			if (kstack_valid(tp, (unsigned long) rw))
226 				rp->rpc = rw->ins[7];
227 		}
228 	} else {
229 		rp->i7 = 0;
230 		rp->rpc = 0;
231 	}
232 	rp->thread = tp;
233 }
234 
235 /* In order to avoid hangs we do not try to synchronize with the
236  * global register dump client cpus.  The last store they make is to
237  * the thread pointer, so do a short poll waiting for that to become
238  * non-NULL.
239  */
240 static void __global_reg_poll(struct global_reg_snapshot *gp)
241 {
242 	int limit = 0;
243 
244 	while (!gp->thread && ++limit < 100) {
245 		barrier();
246 		udelay(1);
247 	}
248 }
249 
250 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
251 {
252 	struct thread_info *tp = current_thread_info();
253 	struct pt_regs *regs = get_irq_regs();
254 	unsigned long flags;
255 	int this_cpu, cpu;
256 
257 	if (!regs)
258 		regs = tp->kregs;
259 
260 	spin_lock_irqsave(&global_cpu_snapshot_lock, flags);
261 
262 	this_cpu = raw_smp_processor_id();
263 
264 	memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
265 
266 	if (cpumask_test_cpu(this_cpu, mask) && !exclude_self)
267 		__global_reg_self(tp, regs, this_cpu);
268 
269 	smp_fetch_global_regs();
270 
271 	for_each_cpu(cpu, mask) {
272 		struct global_reg_snapshot *gp;
273 
274 		if (exclude_self && cpu == this_cpu)
275 			continue;
276 
277 		gp = &global_cpu_snapshot[cpu].reg;
278 
279 		__global_reg_poll(gp);
280 
281 		tp = gp->thread;
282 		printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n",
283 		       (cpu == this_cpu ? '*' : ' '), cpu,
284 		       gp->tstate, gp->tpc, gp->tnpc,
285 		       ((tp && tp->task) ? tp->task->comm : "NULL"),
286 		       ((tp && tp->task) ? tp->task->pid : -1));
287 
288 		if (gp->tstate & TSTATE_PRIV) {
289 			printk("             TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n",
290 			       (void *) gp->tpc,
291 			       (void *) gp->o7,
292 			       (void *) gp->i7,
293 			       (void *) gp->rpc);
294 		} else {
295 			printk("             TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n",
296 			       gp->tpc, gp->o7, gp->i7, gp->rpc);
297 		}
298 
299 		touch_nmi_watchdog();
300 	}
301 
302 	memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
303 
304 	spin_unlock_irqrestore(&global_cpu_snapshot_lock, flags);
305 }
306 
307 #ifdef CONFIG_MAGIC_SYSRQ
308 
309 static void sysrq_handle_globreg(int key)
310 {
311 	trigger_all_cpu_backtrace();
312 }
313 
314 static struct sysrq_key_op sparc_globalreg_op = {
315 	.handler	= sysrq_handle_globreg,
316 	.help_msg	= "global-regs(y)",
317 	.action_msg	= "Show Global CPU Regs",
318 };
319 
320 static void __global_pmu_self(int this_cpu)
321 {
322 	struct global_pmu_snapshot *pp;
323 	int i, num;
324 
325 	if (!pcr_ops)
326 		return;
327 
328 	pp = &global_cpu_snapshot[this_cpu].pmu;
329 
330 	num = 1;
331 	if (tlb_type == hypervisor &&
332 	    sun4v_chip_type >= SUN4V_CHIP_NIAGARA4)
333 		num = 4;
334 
335 	for (i = 0; i < num; i++) {
336 		pp->pcr[i] = pcr_ops->read_pcr(i);
337 		pp->pic[i] = pcr_ops->read_pic(i);
338 	}
339 }
340 
341 static void __global_pmu_poll(struct global_pmu_snapshot *pp)
342 {
343 	int limit = 0;
344 
345 	while (!pp->pcr[0] && ++limit < 100) {
346 		barrier();
347 		udelay(1);
348 	}
349 }
350 
351 static void pmu_snapshot_all_cpus(void)
352 {
353 	unsigned long flags;
354 	int this_cpu, cpu;
355 
356 	spin_lock_irqsave(&global_cpu_snapshot_lock, flags);
357 
358 	memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
359 
360 	this_cpu = raw_smp_processor_id();
361 
362 	__global_pmu_self(this_cpu);
363 
364 	smp_fetch_global_pmu();
365 
366 	for_each_online_cpu(cpu) {
367 		struct global_pmu_snapshot *pp = &global_cpu_snapshot[cpu].pmu;
368 
369 		__global_pmu_poll(pp);
370 
371 		printk("%c CPU[%3d]: PCR[%08lx:%08lx:%08lx:%08lx] PIC[%08lx:%08lx:%08lx:%08lx]\n",
372 		       (cpu == this_cpu ? '*' : ' '), cpu,
373 		       pp->pcr[0], pp->pcr[1], pp->pcr[2], pp->pcr[3],
374 		       pp->pic[0], pp->pic[1], pp->pic[2], pp->pic[3]);
375 
376 		touch_nmi_watchdog();
377 	}
378 
379 	memset(global_cpu_snapshot, 0, sizeof(global_cpu_snapshot));
380 
381 	spin_unlock_irqrestore(&global_cpu_snapshot_lock, flags);
382 }
383 
384 static void sysrq_handle_globpmu(int key)
385 {
386 	pmu_snapshot_all_cpus();
387 }
388 
389 static struct sysrq_key_op sparc_globalpmu_op = {
390 	.handler	= sysrq_handle_globpmu,
391 	.help_msg	= "global-pmu(x)",
392 	.action_msg	= "Show Global PMU Regs",
393 };
394 
395 static int __init sparc_sysrq_init(void)
396 {
397 	int ret = register_sysrq_key('y', &sparc_globalreg_op);
398 
399 	if (!ret)
400 		ret = register_sysrq_key('x', &sparc_globalpmu_op);
401 	return ret;
402 }
403 
404 core_initcall(sparc_sysrq_init);
405 
406 #endif
407 
408 /* Free current thread data structures etc.. */
409 void exit_thread(struct task_struct *tsk)
410 {
411 	struct thread_info *t = task_thread_info(tsk);
412 
413 	if (t->utraps) {
414 		if (t->utraps[0] < 2)
415 			kfree (t->utraps);
416 		else
417 			t->utraps[0]--;
418 	}
419 }
420 
421 void flush_thread(void)
422 {
423 	struct thread_info *t = current_thread_info();
424 	struct mm_struct *mm;
425 
426 	mm = t->task->mm;
427 	if (mm)
428 		tsb_context_switch(mm);
429 
430 	set_thread_wsaved(0);
431 
432 	/* Clear FPU register state. */
433 	t->fpsaved[0] = 0;
434 }
435 
436 /* It's a bit more tricky when 64-bit tasks are involved... */
437 static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
438 {
439 	bool stack_64bit = test_thread_64bit_stack(psp);
440 	unsigned long fp, distance, rval;
441 
442 	if (stack_64bit) {
443 		csp += STACK_BIAS;
444 		psp += STACK_BIAS;
445 		__get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
446 		fp += STACK_BIAS;
447 		if (test_thread_flag(TIF_32BIT))
448 			fp &= 0xffffffff;
449 	} else
450 		__get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
451 
452 	/* Now align the stack as this is mandatory in the Sparc ABI
453 	 * due to how register windows work.  This hides the
454 	 * restriction from thread libraries etc.
455 	 */
456 	csp &= ~15UL;
457 
458 	distance = fp - psp;
459 	rval = (csp - distance);
460 	if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
461 		rval = 0;
462 	else if (!stack_64bit) {
463 		if (put_user(((u32)csp),
464 			     &(((struct reg_window32 __user *)rval)->ins[6])))
465 			rval = 0;
466 	} else {
467 		if (put_user(((u64)csp - STACK_BIAS),
468 			     &(((struct reg_window __user *)rval)->ins[6])))
469 			rval = 0;
470 		else
471 			rval = rval - STACK_BIAS;
472 	}
473 
474 	return rval;
475 }
476 
477 /* Standard stuff. */
478 static inline void shift_window_buffer(int first_win, int last_win,
479 				       struct thread_info *t)
480 {
481 	int i;
482 
483 	for (i = first_win; i < last_win; i++) {
484 		t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
485 		memcpy(&t->reg_window[i], &t->reg_window[i+1],
486 		       sizeof(struct reg_window));
487 	}
488 }
489 
490 void synchronize_user_stack(void)
491 {
492 	struct thread_info *t = current_thread_info();
493 	unsigned long window;
494 
495 	flush_user_windows();
496 	if ((window = get_thread_wsaved()) != 0) {
497 		window -= 1;
498 		do {
499 			struct reg_window *rwin = &t->reg_window[window];
500 			int winsize = sizeof(struct reg_window);
501 			unsigned long sp;
502 
503 			sp = t->rwbuf_stkptrs[window];
504 
505 			if (test_thread_64bit_stack(sp))
506 				sp += STACK_BIAS;
507 			else
508 				winsize = sizeof(struct reg_window32);
509 
510 			if (!copy_to_user((char __user *)sp, rwin, winsize)) {
511 				shift_window_buffer(window, get_thread_wsaved() - 1, t);
512 				set_thread_wsaved(get_thread_wsaved() - 1);
513 			}
514 		} while (window--);
515 	}
516 }
517 
518 static void stack_unaligned(unsigned long sp)
519 {
520 	siginfo_t info;
521 
522 	info.si_signo = SIGBUS;
523 	info.si_errno = 0;
524 	info.si_code = BUS_ADRALN;
525 	info.si_addr = (void __user *) sp;
526 	info.si_trapno = 0;
527 	force_sig_info(SIGBUS, &info, current);
528 }
529 
530 void fault_in_user_windows(void)
531 {
532 	struct thread_info *t = current_thread_info();
533 	unsigned long window;
534 
535 	flush_user_windows();
536 	window = get_thread_wsaved();
537 
538 	if (likely(window != 0)) {
539 		window -= 1;
540 		do {
541 			struct reg_window *rwin = &t->reg_window[window];
542 			int winsize = sizeof(struct reg_window);
543 			unsigned long sp;
544 
545 			sp = t->rwbuf_stkptrs[window];
546 
547 			if (test_thread_64bit_stack(sp))
548 				sp += STACK_BIAS;
549 			else
550 				winsize = sizeof(struct reg_window32);
551 
552 			if (unlikely(sp & 0x7UL))
553 				stack_unaligned(sp);
554 
555 			if (unlikely(copy_to_user((char __user *)sp,
556 						  rwin, winsize)))
557 				goto barf;
558 		} while (window--);
559 	}
560 	set_thread_wsaved(0);
561 	return;
562 
563 barf:
564 	set_thread_wsaved(window + 1);
565 	user_exit();
566 	do_exit(SIGILL);
567 }
568 
569 asmlinkage long sparc_do_fork(unsigned long clone_flags,
570 			      unsigned long stack_start,
571 			      struct pt_regs *regs,
572 			      unsigned long stack_size)
573 {
574 	int __user *parent_tid_ptr, *child_tid_ptr;
575 	unsigned long orig_i1 = regs->u_regs[UREG_I1];
576 	long ret;
577 
578 #ifdef CONFIG_COMPAT
579 	if (test_thread_flag(TIF_32BIT)) {
580 		parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
581 		child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
582 	} else
583 #endif
584 	{
585 		parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
586 		child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
587 	}
588 
589 	ret = do_fork(clone_flags, stack_start, stack_size,
590 		      parent_tid_ptr, child_tid_ptr);
591 
592 	/* If we get an error and potentially restart the system
593 	 * call, we're screwed because copy_thread() clobbered
594 	 * the parent's %o1.  So detect that case and restore it
595 	 * here.
596 	 */
597 	if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
598 		regs->u_regs[UREG_I1] = orig_i1;
599 
600 	return ret;
601 }
602 
603 /* Copy a Sparc thread.  The fork() return value conventions
604  * under SunOS are nothing short of bletcherous:
605  * Parent -->  %o0 == childs  pid, %o1 == 0
606  * Child  -->  %o0 == parents pid, %o1 == 1
607  */
608 int copy_thread(unsigned long clone_flags, unsigned long sp,
609 		unsigned long arg, struct task_struct *p)
610 {
611 	struct thread_info *t = task_thread_info(p);
612 	struct pt_regs *regs = current_pt_regs();
613 	struct sparc_stackf *parent_sf;
614 	unsigned long child_stack_sz;
615 	char *child_trap_frame;
616 
617 	/* Calculate offset to stack_frame & pt_regs */
618 	child_stack_sz = (STACKFRAME_SZ + TRACEREG_SZ);
619 	child_trap_frame = (task_stack_page(p) +
620 			    (THREAD_SIZE - child_stack_sz));
621 
622 	t->new_child = 1;
623 	t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
624 	t->kregs = (struct pt_regs *) (child_trap_frame +
625 				       sizeof(struct sparc_stackf));
626 	t->fpsaved[0] = 0;
627 
628 	if (unlikely(p->flags & PF_KTHREAD)) {
629 		memset(child_trap_frame, 0, child_stack_sz);
630 		__thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP] =
631 			(current_pt_regs()->tstate + 1) & TSTATE_CWP;
632 		t->current_ds = ASI_P;
633 		t->kregs->u_regs[UREG_G1] = sp; /* function */
634 		t->kregs->u_regs[UREG_G2] = arg;
635 		return 0;
636 	}
637 
638 	parent_sf = ((struct sparc_stackf *) regs) - 1;
639 	memcpy(child_trap_frame, parent_sf, child_stack_sz);
640 	if (t->flags & _TIF_32BIT) {
641 		sp &= 0x00000000ffffffffUL;
642 		regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
643 	}
644 	t->kregs->u_regs[UREG_FP] = sp;
645 	__thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP] =
646 		(regs->tstate + 1) & TSTATE_CWP;
647 	t->current_ds = ASI_AIUS;
648 	if (sp != regs->u_regs[UREG_FP]) {
649 		unsigned long csp;
650 
651 		csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
652 		if (!csp)
653 			return -EFAULT;
654 		t->kregs->u_regs[UREG_FP] = csp;
655 	}
656 	if (t->utraps)
657 		t->utraps[0]++;
658 
659 	/* Set the return value for the child. */
660 	t->kregs->u_regs[UREG_I0] = current->pid;
661 	t->kregs->u_regs[UREG_I1] = 1;
662 
663 	/* Set the second return value for the parent. */
664 	regs->u_regs[UREG_I1] = 0;
665 
666 	if (clone_flags & CLONE_SETTLS)
667 		t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
668 
669 	return 0;
670 }
671 
672 typedef struct {
673 	union {
674 		unsigned int	pr_regs[32];
675 		unsigned long	pr_dregs[16];
676 	} pr_fr;
677 	unsigned int __unused;
678 	unsigned int	pr_fsr;
679 	unsigned char	pr_qcnt;
680 	unsigned char	pr_q_entrysize;
681 	unsigned char	pr_en;
682 	unsigned int	pr_q[64];
683 } elf_fpregset_t32;
684 
685 /*
686  * fill in the fpu structure for a core dump.
687  */
688 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
689 {
690 	unsigned long *kfpregs = current_thread_info()->fpregs;
691 	unsigned long fprs = current_thread_info()->fpsaved[0];
692 
693 	if (test_thread_flag(TIF_32BIT)) {
694 		elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
695 
696 		if (fprs & FPRS_DL)
697 			memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
698 			       sizeof(unsigned int) * 32);
699 		else
700 			memset(&fpregs32->pr_fr.pr_regs[0], 0,
701 			       sizeof(unsigned int) * 32);
702 		fpregs32->pr_qcnt = 0;
703 		fpregs32->pr_q_entrysize = 8;
704 		memset(&fpregs32->pr_q[0], 0,
705 		       (sizeof(unsigned int) * 64));
706 		if (fprs & FPRS_FEF) {
707 			fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
708 			fpregs32->pr_en = 1;
709 		} else {
710 			fpregs32->pr_fsr = 0;
711 			fpregs32->pr_en = 0;
712 		}
713 	} else {
714 		if(fprs & FPRS_DL)
715 			memcpy(&fpregs->pr_regs[0], kfpregs,
716 			       sizeof(unsigned int) * 32);
717 		else
718 			memset(&fpregs->pr_regs[0], 0,
719 			       sizeof(unsigned int) * 32);
720 		if(fprs & FPRS_DU)
721 			memcpy(&fpregs->pr_regs[16], kfpregs+16,
722 			       sizeof(unsigned int) * 32);
723 		else
724 			memset(&fpregs->pr_regs[16], 0,
725 			       sizeof(unsigned int) * 32);
726 		if(fprs & FPRS_FEF) {
727 			fpregs->pr_fsr = current_thread_info()->xfsr[0];
728 			fpregs->pr_gsr = current_thread_info()->gsr[0];
729 		} else {
730 			fpregs->pr_fsr = fpregs->pr_gsr = 0;
731 		}
732 		fpregs->pr_fprs = fprs;
733 	}
734 	return 1;
735 }
736 EXPORT_SYMBOL(dump_fpu);
737 
738 unsigned long get_wchan(struct task_struct *task)
739 {
740 	unsigned long pc, fp, bias = 0;
741 	struct thread_info *tp;
742 	struct reg_window *rw;
743         unsigned long ret = 0;
744 	int count = 0;
745 
746 	if (!task || task == current ||
747             task->state == TASK_RUNNING)
748 		goto out;
749 
750 	tp = task_thread_info(task);
751 	bias = STACK_BIAS;
752 	fp = task_thread_info(task)->ksp + bias;
753 
754 	do {
755 		if (!kstack_valid(tp, fp))
756 			break;
757 		rw = (struct reg_window *) fp;
758 		pc = rw->ins[7];
759 		if (!in_sched_functions(pc)) {
760 			ret = pc;
761 			goto out;
762 		}
763 		fp = rw->ins[6] + bias;
764 	} while (++count < 16);
765 
766 out:
767 	return ret;
768 }
769