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