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