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