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