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