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