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