sparc64: Make global reg dumping even more useful.
[linux-2.6.git] / arch / sparc64 / kernel / process.c
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/module.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/fs.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/reboot.h>
26 #include <linux/delay.h>
27 #include <linux/compat.h>
28 #include <linux/tick.h>
29 #include <linux/init.h>
30 #include <linux/cpu.h>
31 #include <linux/elfcore.h>
32 #include <linux/sysrq.h>
33
34 #include <asm/oplib.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #include <asm/page.h>
38 #include <asm/pgalloc.h>
39 #include <asm/pgtable.h>
40 #include <asm/processor.h>
41 #include <asm/pstate.h>
42 #include <asm/elf.h>
43 #include <asm/fpumacro.h>
44 #include <asm/head.h>
45 #include <asm/cpudata.h>
46 #include <asm/mmu_context.h>
47 #include <asm/unistd.h>
48 #include <asm/hypervisor.h>
49 #include <asm/sstate.h>
50 #include <asm/reboot.h>
51 #include <asm/syscalls.h>
52 #include <asm/irq_regs.h>
53 #include <asm/smp.h>
54
55 /* #define VERBOSE_SHOWREGS */
56
57 static void sparc64_yield(int cpu)
58 {
59         if (tlb_type != hypervisor)
60                 return;
61
62         clear_thread_flag(TIF_POLLING_NRFLAG);
63         smp_mb__after_clear_bit();
64
65         while (!need_resched() && !cpu_is_offline(cpu)) {
66                 unsigned long pstate;
67
68                 /* Disable interrupts. */
69                 __asm__ __volatile__(
70                         "rdpr %%pstate, %0\n\t"
71                         "andn %0, %1, %0\n\t"
72                         "wrpr %0, %%g0, %%pstate"
73                         : "=&r" (pstate)
74                         : "i" (PSTATE_IE));
75
76                 if (!need_resched() && !cpu_is_offline(cpu))
77                         sun4v_cpu_yield();
78
79                 /* Re-enable interrupts. */
80                 __asm__ __volatile__(
81                         "rdpr %%pstate, %0\n\t"
82                         "or %0, %1, %0\n\t"
83                         "wrpr %0, %%g0, %%pstate"
84                         : "=&r" (pstate)
85                         : "i" (PSTATE_IE));
86         }
87
88         set_thread_flag(TIF_POLLING_NRFLAG);
89 }
90
91 /* The idle loop on sparc64. */
92 void cpu_idle(void)
93 {
94         int cpu = smp_processor_id();
95
96         set_thread_flag(TIF_POLLING_NRFLAG);
97
98         while(1) {
99                 tick_nohz_stop_sched_tick(1);
100
101                 while (!need_resched() && !cpu_is_offline(cpu))
102                         sparc64_yield(cpu);
103
104                 tick_nohz_restart_sched_tick();
105
106                 preempt_enable_no_resched();
107
108 #ifdef CONFIG_HOTPLUG_CPU
109                 if (cpu_is_offline(cpu))
110                         cpu_play_dead();
111 #endif
112
113                 schedule();
114                 preempt_disable();
115         }
116 }
117
118 void machine_halt(void)
119 {
120         sstate_halt();
121         prom_halt();
122         panic("Halt failed!");
123 }
124
125 void machine_alt_power_off(void)
126 {
127         sstate_poweroff();
128         prom_halt_power_off();
129         panic("Power-off failed!");
130 }
131
132 void machine_restart(char * cmd)
133 {
134         char *p;
135         
136         sstate_reboot();
137         p = strchr (reboot_command, '\n');
138         if (p) *p = 0;
139         if (cmd)
140                 prom_reboot(cmd);
141         if (*reboot_command)
142                 prom_reboot(reboot_command);
143         prom_reboot("");
144         panic("Reboot failed!");
145 }
146
147 #ifdef CONFIG_COMPAT
148 static void show_regwindow32(struct pt_regs *regs)
149 {
150         struct reg_window32 __user *rw;
151         struct reg_window32 r_w;
152         mm_segment_t old_fs;
153         
154         __asm__ __volatile__ ("flushw");
155         rw = compat_ptr((unsigned)regs->u_regs[14]);
156         old_fs = get_fs();
157         set_fs (USER_DS);
158         if (copy_from_user (&r_w, rw, sizeof(r_w))) {
159                 set_fs (old_fs);
160                 return;
161         }
162
163         set_fs (old_fs);                        
164         printk("l0: %08x l1: %08x l2: %08x l3: %08x "
165                "l4: %08x l5: %08x l6: %08x l7: %08x\n",
166                r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
167                r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
168         printk("i0: %08x i1: %08x i2: %08x i3: %08x "
169                "i4: %08x i5: %08x i6: %08x i7: %08x\n",
170                r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
171                r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
172 }
173 #else
174 #define show_regwindow32(regs)  do { } while (0)
175 #endif
176
177 static void show_regwindow(struct pt_regs *regs)
178 {
179         struct reg_window __user *rw;
180         struct reg_window *rwk;
181         struct reg_window r_w;
182         mm_segment_t old_fs;
183
184         if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
185                 __asm__ __volatile__ ("flushw");
186                 rw = (struct reg_window __user *)
187                         (regs->u_regs[14] + STACK_BIAS);
188                 rwk = (struct reg_window *)
189                         (regs->u_regs[14] + STACK_BIAS);
190                 if (!(regs->tstate & TSTATE_PRIV)) {
191                         old_fs = get_fs();
192                         set_fs (USER_DS);
193                         if (copy_from_user (&r_w, rw, sizeof(r_w))) {
194                                 set_fs (old_fs);
195                                 return;
196                         }
197                         rwk = &r_w;
198                         set_fs (old_fs);                        
199                 }
200         } else {
201                 show_regwindow32(regs);
202                 return;
203         }
204         printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
205                rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
206         printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
207                rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
208         printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
209                rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
210         printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
211                rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
212         if (regs->tstate & TSTATE_PRIV)
213                 printk("I7: <%pS>\n", (void *) rwk->ins[7]);
214 }
215
216 #ifdef CONFIG_SMP
217 static DEFINE_SPINLOCK(regdump_lock);
218 #endif
219
220 void __show_regs(struct pt_regs * regs)
221 {
222 #ifdef CONFIG_SMP
223         unsigned long flags;
224
225         /* Protect against xcall ipis which might lead to livelock on the lock */
226         __asm__ __volatile__("rdpr      %%pstate, %0\n\t"
227                              "wrpr      %0, %1, %%pstate"
228                              : "=r" (flags)
229                              : "i" (PSTATE_IE));
230         spin_lock(&regdump_lock);
231 #endif
232         printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x    %s\n", regs->tstate,
233                regs->tpc, regs->tnpc, regs->y, print_tainted());
234         printk("TPC: <%pS>\n", (void *) regs->tpc);
235         printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
236                regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
237                regs->u_regs[3]);
238         printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
239                regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
240                regs->u_regs[7]);
241         printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
242                regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
243                regs->u_regs[11]);
244         printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
245                regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
246                regs->u_regs[15]);
247         printk("RPC: <%pS>\n", (void *) regs->u_regs[15]);
248         show_regwindow(regs);
249 #ifdef CONFIG_SMP
250         spin_unlock(&regdump_lock);
251         __asm__ __volatile__("wrpr      %0, 0, %%pstate"
252                              : : "r" (flags));
253 #endif
254 }
255
256 #ifdef VERBOSE_SHOWREGS
257 static void idump_from_user (unsigned int *pc)
258 {
259         int i;
260         int code;
261         
262         if((((unsigned long) pc) & 3))
263                 return;
264         
265         pc -= 3;
266         for(i = -3; i < 6; i++) {
267                 get_user(code, pc);
268                 printk("%c%08x%c",i?' ':'<',code,i?' ':'>');
269                 pc++;
270         }
271         printk("\n");
272 }
273 #endif
274
275 void show_regs(struct pt_regs *regs)
276 {
277 #ifdef VERBOSE_SHOWREGS
278         extern long etrap, etraptl1;
279 #endif
280         __show_regs(regs);
281 #if 0
282 #ifdef CONFIG_SMP
283         {
284                 extern void smp_report_regs(void);
285
286                 smp_report_regs();
287         }
288 #endif
289 #endif
290
291 #ifdef VERBOSE_SHOWREGS 
292         if (regs->tpc >= &etrap && regs->tpc < &etraptl1 &&
293             regs->u_regs[14] >= (long)current - PAGE_SIZE &&
294             regs->u_regs[14] < (long)current + 6 * PAGE_SIZE) {
295                 printk ("*********parent**********\n");
296                 __show_regs((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF));
297                 idump_from_user(((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF))->tpc);
298                 printk ("*********endpar**********\n");
299         }
300 #endif
301 }
302
303 #ifdef CONFIG_MAGIC_SYSRQ
304 struct global_reg_snapshot global_reg_snapshot[NR_CPUS];
305 static DEFINE_SPINLOCK(global_reg_snapshot_lock);
306
307 static bool kstack_valid(struct thread_info *tp, struct reg_window *rw)
308 {
309         unsigned long thread_base, fp;
310
311         thread_base = (unsigned long) tp;
312         fp = (unsigned long) rw;
313
314         if (fp < (thread_base + sizeof(struct thread_info)) ||
315             fp >= (thread_base + THREAD_SIZE))
316                 return false;
317         return true;
318 }
319
320 static void __global_reg_self(struct thread_info *tp, struct pt_regs *regs,
321                               int this_cpu)
322 {
323         flushw_all();
324
325         global_reg_snapshot[this_cpu].tstate = regs->tstate;
326         global_reg_snapshot[this_cpu].tpc = regs->tpc;
327         global_reg_snapshot[this_cpu].tnpc = regs->tnpc;
328         global_reg_snapshot[this_cpu].o7 = regs->u_regs[UREG_I7];
329
330         if (regs->tstate & TSTATE_PRIV) {
331                 struct thread_info *tp = current_thread_info();
332                 struct reg_window *rw;
333
334                 rw = (struct reg_window *)
335                         (regs->u_regs[UREG_FP] + STACK_BIAS);
336                 if (kstack_valid(tp, rw)) {
337                         global_reg_snapshot[this_cpu].i7 = rw->ins[7];
338                         rw = (struct reg_window *)
339                                 (rw->ins[6] + STACK_BIAS);
340                         if (kstack_valid(tp, rw))
341                                 global_reg_snapshot[this_cpu].rpc = rw->ins[7];
342                 }
343         } else {
344                 global_reg_snapshot[this_cpu].i7 = 0;
345                 global_reg_snapshot[this_cpu].rpc = 0;
346         }
347         global_reg_snapshot[this_cpu].thread = tp;
348 }
349
350 /* In order to avoid hangs we do not try to synchronize with the
351  * global register dump client cpus.  The last store they make is to
352  * the thread pointer, so do a short poll waiting for that to become
353  * non-NULL.
354  */
355 static void __global_reg_poll(struct global_reg_snapshot *gp)
356 {
357         int limit = 0;
358
359         while (!gp->thread && ++limit < 100) {
360                 barrier();
361                 udelay(1);
362         }
363 }
364
365 static void sysrq_handle_globreg(int key, struct tty_struct *tty)
366 {
367         struct thread_info *tp = current_thread_info();
368         struct pt_regs *regs = get_irq_regs();
369         unsigned long flags;
370         int this_cpu, cpu;
371
372         if (!regs)
373                 regs = tp->kregs;
374
375         spin_lock_irqsave(&global_reg_snapshot_lock, flags);
376
377         memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
378
379         this_cpu = raw_smp_processor_id();
380
381         __global_reg_self(tp, regs, this_cpu);
382
383         smp_fetch_global_regs();
384
385         for_each_online_cpu(cpu) {
386                 struct global_reg_snapshot *gp = &global_reg_snapshot[cpu];
387                 struct thread_info *tp;
388
389                 __global_reg_poll(gp);
390
391                 tp = gp->thread;
392                 printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n",
393                        (cpu == this_cpu ? '*' : ' '), cpu,
394                        gp->tstate, gp->tpc, gp->tnpc,
395                        ((tp && tp->task) ? tp->task->comm : "NULL"),
396                        ((tp && tp->task) ? tp->task->pid : -1));
397
398                 if (gp->tstate & TSTATE_PRIV) {
399                         printk("             TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n",
400                                (void *) gp->tpc,
401                                (void *) gp->o7,
402                                (void *) gp->i7,
403                                (void *) gp->rpc);
404                 } else {
405                         printk("             TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n",
406                                gp->tpc, gp->o7, gp->i7, gp->rpc);
407                 }
408         }
409
410         memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
411
412         spin_unlock_irqrestore(&global_reg_snapshot_lock, flags);
413 }
414
415 static struct sysrq_key_op sparc_globalreg_op = {
416         .handler        = sysrq_handle_globreg,
417         .help_msg       = "Globalregs",
418         .action_msg     = "Show Global CPU Regs",
419 };
420
421 static int __init sparc_globreg_init(void)
422 {
423         return register_sysrq_key('y', &sparc_globalreg_op);
424 }
425
426 core_initcall(sparc_globreg_init);
427
428 #endif
429
430 unsigned long thread_saved_pc(struct task_struct *tsk)
431 {
432         struct thread_info *ti = task_thread_info(tsk);
433         unsigned long ret = 0xdeadbeefUL;
434         
435         if (ti && ti->ksp) {
436                 unsigned long *sp;
437                 sp = (unsigned long *)(ti->ksp + STACK_BIAS);
438                 if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
439                     sp[14]) {
440                         unsigned long *fp;
441                         fp = (unsigned long *)(sp[14] + STACK_BIAS);
442                         if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
443                                 ret = fp[15];
444                 }
445         }
446         return ret;
447 }
448
449 /* Free current thread data structures etc.. */
450 void exit_thread(void)
451 {
452         struct thread_info *t = current_thread_info();
453
454         if (t->utraps) {
455                 if (t->utraps[0] < 2)
456                         kfree (t->utraps);
457                 else
458                         t->utraps[0]--;
459         }
460
461         if (test_and_clear_thread_flag(TIF_PERFCTR)) {
462                 t->user_cntd0 = t->user_cntd1 = NULL;
463                 t->pcr_reg = 0;
464                 write_pcr(0);
465         }
466 }
467
468 void flush_thread(void)
469 {
470         struct thread_info *t = current_thread_info();
471         struct mm_struct *mm;
472
473         if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
474                 clear_ti_thread_flag(t, TIF_ABI_PENDING);
475                 if (test_ti_thread_flag(t, TIF_32BIT))
476                         clear_ti_thread_flag(t, TIF_32BIT);
477                 else
478                         set_ti_thread_flag(t, TIF_32BIT);
479         }
480
481         mm = t->task->mm;
482         if (mm)
483                 tsb_context_switch(mm);
484
485         set_thread_wsaved(0);
486
487         /* Turn off performance counters if on. */
488         if (test_and_clear_thread_flag(TIF_PERFCTR)) {
489                 t->user_cntd0 = t->user_cntd1 = NULL;
490                 t->pcr_reg = 0;
491                 write_pcr(0);
492         }
493
494         /* Clear FPU register state. */
495         t->fpsaved[0] = 0;
496         
497         if (get_thread_current_ds() != ASI_AIUS)
498                 set_fs(USER_DS);
499 }
500
501 /* It's a bit more tricky when 64-bit tasks are involved... */
502 static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
503 {
504         unsigned long fp, distance, rval;
505
506         if (!(test_thread_flag(TIF_32BIT))) {
507                 csp += STACK_BIAS;
508                 psp += STACK_BIAS;
509                 __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
510                 fp += STACK_BIAS;
511         } else
512                 __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
513
514         /* Now 8-byte align the stack as this is mandatory in the
515          * Sparc ABI due to how register windows work.  This hides
516          * the restriction from thread libraries etc.  -DaveM
517          */
518         csp &= ~7UL;
519
520         distance = fp - psp;
521         rval = (csp - distance);
522         if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
523                 rval = 0;
524         else if (test_thread_flag(TIF_32BIT)) {
525                 if (put_user(((u32)csp),
526                              &(((struct reg_window32 __user *)rval)->ins[6])))
527                         rval = 0;
528         } else {
529                 if (put_user(((u64)csp - STACK_BIAS),
530                              &(((struct reg_window __user *)rval)->ins[6])))
531                         rval = 0;
532                 else
533                         rval = rval - STACK_BIAS;
534         }
535
536         return rval;
537 }
538
539 /* Standard stuff. */
540 static inline void shift_window_buffer(int first_win, int last_win,
541                                        struct thread_info *t)
542 {
543         int i;
544
545         for (i = first_win; i < last_win; i++) {
546                 t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
547                 memcpy(&t->reg_window[i], &t->reg_window[i+1],
548                        sizeof(struct reg_window));
549         }
550 }
551
552 void synchronize_user_stack(void)
553 {
554         struct thread_info *t = current_thread_info();
555         unsigned long window;
556
557         flush_user_windows();
558         if ((window = get_thread_wsaved()) != 0) {
559                 int winsize = sizeof(struct reg_window);
560                 int bias = 0;
561
562                 if (test_thread_flag(TIF_32BIT))
563                         winsize = sizeof(struct reg_window32);
564                 else
565                         bias = STACK_BIAS;
566
567                 window -= 1;
568                 do {
569                         unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
570                         struct reg_window *rwin = &t->reg_window[window];
571
572                         if (!copy_to_user((char __user *)sp, rwin, winsize)) {
573                                 shift_window_buffer(window, get_thread_wsaved() - 1, t);
574                                 set_thread_wsaved(get_thread_wsaved() - 1);
575                         }
576                 } while (window--);
577         }
578 }
579
580 static void stack_unaligned(unsigned long sp)
581 {
582         siginfo_t info;
583
584         info.si_signo = SIGBUS;
585         info.si_errno = 0;
586         info.si_code = BUS_ADRALN;
587         info.si_addr = (void __user *) sp;
588         info.si_trapno = 0;
589         force_sig_info(SIGBUS, &info, current);
590 }
591
592 void fault_in_user_windows(void)
593 {
594         struct thread_info *t = current_thread_info();
595         unsigned long window;
596         int winsize = sizeof(struct reg_window);
597         int bias = 0;
598
599         if (test_thread_flag(TIF_32BIT))
600                 winsize = sizeof(struct reg_window32);
601         else
602                 bias = STACK_BIAS;
603
604         flush_user_windows();
605         window = get_thread_wsaved();
606
607         if (likely(window != 0)) {
608                 window -= 1;
609                 do {
610                         unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
611                         struct reg_window *rwin = &t->reg_window[window];
612
613                         if (unlikely(sp & 0x7UL))
614                                 stack_unaligned(sp);
615
616                         if (unlikely(copy_to_user((char __user *)sp,
617                                                   rwin, winsize)))
618                                 goto barf;
619                 } while (window--);
620         }
621         set_thread_wsaved(0);
622         return;
623
624 barf:
625         set_thread_wsaved(window + 1);
626         do_exit(SIGILL);
627 }
628
629 asmlinkage long sparc_do_fork(unsigned long clone_flags,
630                               unsigned long stack_start,
631                               struct pt_regs *regs,
632                               unsigned long stack_size)
633 {
634         int __user *parent_tid_ptr, *child_tid_ptr;
635         unsigned long orig_i1 = regs->u_regs[UREG_I1];
636         long ret;
637
638 #ifdef CONFIG_COMPAT
639         if (test_thread_flag(TIF_32BIT)) {
640                 parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
641                 child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
642         } else
643 #endif
644         {
645                 parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
646                 child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
647         }
648
649         ret = do_fork(clone_flags, stack_start,
650                       regs, stack_size,
651                       parent_tid_ptr, child_tid_ptr);
652
653         /* If we get an error and potentially restart the system
654          * call, we're screwed because copy_thread() clobbered
655          * the parent's %o1.  So detect that case and restore it
656          * here.
657          */
658         if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
659                 regs->u_regs[UREG_I1] = orig_i1;
660
661         return ret;
662 }
663
664 /* Copy a Sparc thread.  The fork() return value conventions
665  * under SunOS are nothing short of bletcherous:
666  * Parent -->  %o0 == childs  pid, %o1 == 0
667  * Child  -->  %o0 == parents pid, %o1 == 1
668  */
669 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
670                 unsigned long unused,
671                 struct task_struct *p, struct pt_regs *regs)
672 {
673         struct thread_info *t = task_thread_info(p);
674         struct sparc_stackf *parent_sf;
675         unsigned long child_stack_sz;
676         char *child_trap_frame;
677         int kernel_thread;
678
679         kernel_thread = (regs->tstate & TSTATE_PRIV) ? 1 : 0;
680         parent_sf = ((struct sparc_stackf *) regs) - 1;
681
682         /* Calculate offset to stack_frame & pt_regs */
683         child_stack_sz = ((STACKFRAME_SZ + TRACEREG_SZ) +
684                           (kernel_thread ? STACKFRAME_SZ : 0));
685         child_trap_frame = (task_stack_page(p) +
686                             (THREAD_SIZE - child_stack_sz));
687         memcpy(child_trap_frame, parent_sf, child_stack_sz);
688
689         t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) |
690                                  (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
691                 (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
692         t->new_child = 1;
693         t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
694         t->kregs = (struct pt_regs *) (child_trap_frame +
695                                        sizeof(struct sparc_stackf));
696         t->fpsaved[0] = 0;
697
698         if (kernel_thread) {
699                 struct sparc_stackf *child_sf = (struct sparc_stackf *)
700                         (child_trap_frame + (STACKFRAME_SZ + TRACEREG_SZ));
701
702                 /* Zero terminate the stack backtrace.  */
703                 child_sf->fp = NULL;
704                 t->kregs->u_regs[UREG_FP] =
705                   ((unsigned long) child_sf) - STACK_BIAS;
706
707                 /* Special case, if we are spawning a kernel thread from
708                  * a userspace task (usermode helper, NFS or similar), we
709                  * must disable performance counters in the child because
710                  * the address space and protection realm are changing.
711                  */
712                 if (t->flags & _TIF_PERFCTR) {
713                         t->user_cntd0 = t->user_cntd1 = NULL;
714                         t->pcr_reg = 0;
715                         t->flags &= ~_TIF_PERFCTR;
716                 }
717                 t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
718                 t->kregs->u_regs[UREG_G6] = (unsigned long) t;
719                 t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
720         } else {
721                 if (t->flags & _TIF_32BIT) {
722                         sp &= 0x00000000ffffffffUL;
723                         regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
724                 }
725                 t->kregs->u_regs[UREG_FP] = sp;
726                 t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
727                 if (sp != regs->u_regs[UREG_FP]) {
728                         unsigned long csp;
729
730                         csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
731                         if (!csp)
732                                 return -EFAULT;
733                         t->kregs->u_regs[UREG_FP] = csp;
734                 }
735                 if (t->utraps)
736                         t->utraps[0]++;
737         }
738
739         /* Set the return value for the child. */
740         t->kregs->u_regs[UREG_I0] = current->pid;
741         t->kregs->u_regs[UREG_I1] = 1;
742
743         /* Set the second return value for the parent. */
744         regs->u_regs[UREG_I1] = 0;
745
746         if (clone_flags & CLONE_SETTLS)
747                 t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
748
749         return 0;
750 }
751
752 /*
753  * This is the mechanism for creating a new kernel thread.
754  *
755  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
756  * who haven't done an "execve()") should use this: it will work within
757  * a system call from a "real" process, but the process memory space will
758  * not be freed until both the parent and the child have exited.
759  */
760 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
761 {
762         long retval;
763
764         /* If the parent runs before fn(arg) is called by the child,
765          * the input registers of this function can be clobbered.
766          * So we stash 'fn' and 'arg' into global registers which
767          * will not be modified by the parent.
768          */
769         __asm__ __volatile__("mov %4, %%g2\n\t"    /* Save FN into global */
770                              "mov %5, %%g3\n\t"    /* Save ARG into global */
771                              "mov %1, %%g1\n\t"    /* Clone syscall nr. */
772                              "mov %2, %%o0\n\t"    /* Clone flags. */
773                              "mov 0, %%o1\n\t"     /* usp arg == 0 */
774                              "t 0x6d\n\t"          /* Linux/Sparc clone(). */
775                              "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
776                              " mov %%o0, %0\n\t"
777                              "jmpl %%g2, %%o7\n\t"   /* Call the function. */
778                              " mov %%g3, %%o0\n\t"   /* Set arg in delay. */
779                              "mov %3, %%g1\n\t"
780                              "t 0x6d\n\t"          /* Linux/Sparc exit(). */
781                              /* Notreached by child. */
782                              "1:" :
783                              "=r" (retval) :
784                              "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
785                              "i" (__NR_exit),  "r" (fn), "r" (arg) :
786                              "g1", "g2", "g3", "o0", "o1", "memory", "cc");
787         return retval;
788 }
789
790 typedef struct {
791         union {
792                 unsigned int    pr_regs[32];
793                 unsigned long   pr_dregs[16];
794         } pr_fr;
795         unsigned int __unused;
796         unsigned int    pr_fsr;
797         unsigned char   pr_qcnt;
798         unsigned char   pr_q_entrysize;
799         unsigned char   pr_en;
800         unsigned int    pr_q[64];
801 } elf_fpregset_t32;
802
803 /*
804  * fill in the fpu structure for a core dump.
805  */
806 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
807 {
808         unsigned long *kfpregs = current_thread_info()->fpregs;
809         unsigned long fprs = current_thread_info()->fpsaved[0];
810
811         if (test_thread_flag(TIF_32BIT)) {
812                 elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
813
814                 if (fprs & FPRS_DL)
815                         memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
816                                sizeof(unsigned int) * 32);
817                 else
818                         memset(&fpregs32->pr_fr.pr_regs[0], 0,
819                                sizeof(unsigned int) * 32);
820                 fpregs32->pr_qcnt = 0;
821                 fpregs32->pr_q_entrysize = 8;
822                 memset(&fpregs32->pr_q[0], 0,
823                        (sizeof(unsigned int) * 64));
824                 if (fprs & FPRS_FEF) {
825                         fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
826                         fpregs32->pr_en = 1;
827                 } else {
828                         fpregs32->pr_fsr = 0;
829                         fpregs32->pr_en = 0;
830                 }
831         } else {
832                 if(fprs & FPRS_DL)
833                         memcpy(&fpregs->pr_regs[0], kfpregs,
834                                sizeof(unsigned int) * 32);
835                 else
836                         memset(&fpregs->pr_regs[0], 0,
837                                sizeof(unsigned int) * 32);
838                 if(fprs & FPRS_DU)
839                         memcpy(&fpregs->pr_regs[16], kfpregs+16,
840                                sizeof(unsigned int) * 32);
841                 else
842                         memset(&fpregs->pr_regs[16], 0,
843                                sizeof(unsigned int) * 32);
844                 if(fprs & FPRS_FEF) {
845                         fpregs->pr_fsr = current_thread_info()->xfsr[0];
846                         fpregs->pr_gsr = current_thread_info()->gsr[0];
847                 } else {
848                         fpregs->pr_fsr = fpregs->pr_gsr = 0;
849                 }
850                 fpregs->pr_fprs = fprs;
851         }
852         return 1;
853 }
854
855 /*
856  * sparc_execve() executes a new program after the asm stub has set
857  * things up for us.  This should basically do what I want it to.
858  */
859 asmlinkage int sparc_execve(struct pt_regs *regs)
860 {
861         int error, base = 0;
862         char *filename;
863
864         /* User register window flush is done by entry.S */
865
866         /* Check for indirect call. */
867         if (regs->u_regs[UREG_G1] == 0)
868                 base = 1;
869
870         filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
871         error = PTR_ERR(filename);
872         if (IS_ERR(filename))
873                 goto out;
874         error = do_execve(filename,
875                           (char __user * __user *)
876                           regs->u_regs[base + UREG_I1],
877                           (char __user * __user *)
878                           regs->u_regs[base + UREG_I2], regs);
879         putname(filename);
880         if (!error) {
881                 fprs_write(0);
882                 current_thread_info()->xfsr[0] = 0;
883                 current_thread_info()->fpsaved[0] = 0;
884                 regs->tstate &= ~TSTATE_PEF;
885         }
886 out:
887         return error;
888 }
889
890 unsigned long get_wchan(struct task_struct *task)
891 {
892         unsigned long pc, fp, bias = 0;
893         unsigned long thread_info_base;
894         struct reg_window *rw;
895         unsigned long ret = 0;
896         int count = 0; 
897
898         if (!task || task == current ||
899             task->state == TASK_RUNNING)
900                 goto out;
901
902         thread_info_base = (unsigned long) task_stack_page(task);
903         bias = STACK_BIAS;
904         fp = task_thread_info(task)->ksp + bias;
905
906         do {
907                 /* Bogus frame pointer? */
908                 if (fp < (thread_info_base + sizeof(struct thread_info)) ||
909                     fp >= (thread_info_base + THREAD_SIZE))
910                         break;
911                 rw = (struct reg_window *) fp;
912                 pc = rw->ins[7];
913                 if (!in_sched_functions(pc)) {
914                         ret = pc;
915                         goto out;
916                 }
917                 fp = rw->ins[6] + bias;
918         } while (++count < 16);
919
920 out:
921         return ret;
922 }