patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / sparc / kernel / process.c
1 /*  $Id: process.c,v 1.161 2002/01/23 11:27:32 davem Exp $
2  *  linux/arch/sparc/kernel/process.c
3  *
4  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  *  Copyright (C) 1996 Eddie C. Dost   (ecd@skynet.be)
6  */
7
8 /*
9  * This file handles the architecture-dependent parts of process handling..
10  */
11
12 #include <stdarg.h>
13
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/kallsyms.h>
19 #include <linux/mm.h>
20 #include <linux/stddef.h>
21 #include <linux/ptrace.h>
22 #include <linux/slab.h>
23 #include <linux/user.h>
24 #include <linux/a.out.h>
25 #include <linux/config.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28 #include <linux/reboot.h>
29 #include <linux/delay.h>
30 #include <linux/pm.h>
31 #include <linux/init.h>
32
33 #include <asm/auxio.h>
34 #include <asm/oplib.h>
35 #include <asm/uaccess.h>
36 #include <asm/system.h>
37 #include <asm/page.h>
38 #include <asm/pgalloc.h>
39 #include <asm/pgtable.h>
40 #include <asm/delay.h>
41 #include <asm/processor.h>
42 #include <asm/psr.h>
43 #include <asm/elf.h>
44 #include <asm/unistd.h>
45
46 /* 
47  * Power management idle function 
48  * Set in pm platform drivers
49  */
50 void (*pm_idle)(void);
51
52 /* 
53  * Power-off handler instantiation for pm.h compliance
54  * This is done via auxio, but could be used as a fallback
55  * handler when auxio is not present-- unused for now...
56  */
57 void (*pm_power_off)(void);
58
59 /*
60  * sysctl - toggle power-off restriction for serial console 
61  * systems in machine_power_off()
62  */
63 int scons_pwroff = 1;
64
65 extern void fpsave(unsigned long *, unsigned long *, void *, unsigned long *);
66
67 struct task_struct *last_task_used_math = NULL;
68 struct thread_info *current_set[NR_CPUS];
69
70 /*
71  * default_idle is new in 2.5. XXX Review, currently stolen from sparc64.
72  */
73 void default_idle(void)
74 {
75 }
76
77 #ifndef CONFIG_SMP
78
79 #define SUN4C_FAULT_HIGH 100
80
81 /*
82  * the idle loop on a Sparc... ;)
83  */
84 int cpu_idle(void)
85 {
86         int ret = -EPERM;
87
88         if (current->pid != 0)
89                 goto out;
90
91         /* endless idle loop with no priority at all */
92         for (;;) {
93                 if (ARCH_SUN4C_SUN4) {
94                         static int count = HZ;
95                         static unsigned long last_jiffies;
96                         static unsigned long last_faults;
97                         static unsigned long fps;
98                         unsigned long now;
99                         unsigned long faults;
100                         unsigned long flags;
101
102                         extern unsigned long sun4c_kernel_faults;
103                         extern void sun4c_grow_kernel_ring(void);
104
105                         local_irq_save(flags);
106                         now = jiffies;
107                         count -= (now - last_jiffies);
108                         last_jiffies = now;
109                         if (count < 0) {
110                                 count += HZ;
111                                 faults = sun4c_kernel_faults;
112                                 fps = (fps + (faults - last_faults)) >> 1;
113                                 last_faults = faults;
114 #if 0
115                                 printk("kernel faults / second = %ld\n", fps);
116 #endif
117                                 if (fps >= SUN4C_FAULT_HIGH) {
118                                         sun4c_grow_kernel_ring();
119                                 }
120                         }
121                         local_irq_restore(flags);
122                 }
123
124                 while((!need_resched()) && pm_idle) {
125                         (*pm_idle)();           /* XXX Huh? On sparc?! */
126                 }
127
128                 schedule();
129                 check_pgt_cache();
130         }
131         ret = 0;
132 out:
133         return ret;
134 }
135
136 #else
137
138 /* This is being executed in task 0 'user space'. */
139 int cpu_idle(void)
140 {
141         /* endless idle loop with no priority at all */
142         while(1) {
143                 if(need_resched()) {
144                         schedule();
145                         check_pgt_cache();
146                 }
147                 barrier(); /* or else gcc optimizes... */
148         }
149 }
150
151 #endif
152
153 extern char reboot_command [];
154
155 extern void (*prom_palette)(int);
156
157 /* XXX cli/sti -> local_irq_xxx here, check this works once SMP is fixed. */
158 void machine_halt(void)
159 {
160         local_irq_enable();
161         mdelay(8);
162         local_irq_disable();
163         if (!serial_console && prom_palette)
164                 prom_palette (1);
165         prom_halt();
166         panic("Halt failed!");
167 }
168
169 EXPORT_SYMBOL(machine_halt);
170
171 void machine_restart(char * cmd)
172 {
173         char *p;
174         
175         local_irq_enable();
176         mdelay(8);
177         local_irq_disable();
178
179         p = strchr (reboot_command, '\n');
180         if (p) *p = 0;
181         if (!serial_console && prom_palette)
182                 prom_palette (1);
183         if (cmd)
184                 prom_reboot(cmd);
185         if (*reboot_command)
186                 prom_reboot(reboot_command);
187         prom_feval ("reset");
188         panic("Reboot failed!");
189 }
190
191 EXPORT_SYMBOL(machine_restart);
192
193 void machine_power_off(void)
194 {
195 #ifdef CONFIG_SUN_AUXIO
196         if (auxio_power_register && (!serial_console || scons_pwroff))
197                 *auxio_power_register |= AUXIO_POWER_OFF;
198 #endif
199         machine_halt();
200 }
201
202 EXPORT_SYMBOL(machine_power_off);
203
204 static spinlock_t sparc_backtrace_lock = SPIN_LOCK_UNLOCKED;
205
206 void __show_backtrace(unsigned long fp)
207 {
208         struct reg_window *rw;
209         unsigned long flags;
210         int cpu = smp_processor_id();
211
212         spin_lock_irqsave(&sparc_backtrace_lock, flags);
213
214         rw = (struct reg_window *)fp;
215         while(rw && (((unsigned long) rw) >= PAGE_OFFSET) &&
216             !(((unsigned long) rw) & 0x7)) {
217                 printk("CPU[%d]: ARGS[%08lx,%08lx,%08lx,%08lx,%08lx,%08lx] "
218                        "FP[%08lx] CALLER[%08lx]: ", cpu,
219                        rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
220                        rw->ins[4], rw->ins[5],
221                        rw->ins[6],
222                        rw->ins[7]);
223                 print_symbol("%s\n", rw->ins[7]);
224                 rw = (struct reg_window *) rw->ins[6];
225         }
226         spin_unlock_irqrestore(&sparc_backtrace_lock, flags);
227 }
228
229 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
230 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
231 #define __GET_FP(fp) __asm__ __volatile__("mov %%i6, %0" : "=r" (fp))
232
233 void show_backtrace(void)
234 {
235         unsigned long fp;
236
237         __SAVE; __SAVE; __SAVE; __SAVE;
238         __SAVE; __SAVE; __SAVE; __SAVE;
239         __RESTORE; __RESTORE; __RESTORE; __RESTORE;
240         __RESTORE; __RESTORE; __RESTORE; __RESTORE;
241
242         __GET_FP(fp);
243
244         __show_backtrace(fp);
245 }
246
247 #ifdef CONFIG_SMP
248 void smp_show_backtrace_all_cpus(void)
249 {
250         xc0((smpfunc_t) show_backtrace);
251         show_backtrace();
252 }
253 #endif
254
255 #if 0
256 void show_stackframe(struct sparc_stackf *sf)
257 {
258         unsigned long size;
259         unsigned long *stk;
260         int i;
261
262         printk("l0: %08lx l1: %08lx l2: %08lx l3: %08lx "
263                "l4: %08lx l5: %08lx l6: %08lx l7: %08lx\n",
264                sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3],
265                sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
266         printk("i0: %08lx i1: %08lx i2: %08lx i3: %08lx "
267                "i4: %08lx i5: %08lx fp: %08lx i7: %08lx\n",
268                sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3],
269                sf->ins[4], sf->ins[5], (unsigned long)sf->fp, sf->callers_pc);
270         printk("sp: %08lx x0: %08lx x1: %08lx x2: %08lx "
271                "x3: %08lx x4: %08lx x5: %08lx xx: %08lx\n",
272                (unsigned long)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 -= STACKFRAME_SZ;
277         stk = (unsigned long *)((unsigned long)sf + STACKFRAME_SZ);
278         i = 0;
279         do {
280                 printk("s%d: %08lx\n", i++, *stk++);
281         } while ((size -= sizeof(unsigned long)));
282 }
283 #endif
284
285 void show_regs(struct pt_regs *r)
286 {
287         struct reg_window *rw = (struct reg_window *) r->u_regs[14];
288
289         printk("PSR: %08lx PC: %08lx NPC: %08lx Y: %08lx    %s\n",
290                r->psr, r->pc, r->npc, r->y, print_tainted());
291         print_symbol("PC: <%s>\n", r->pc);
292         printk("%%G: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
293                r->u_regs[0], r->u_regs[1], r->u_regs[2], r->u_regs[3],
294                r->u_regs[4], r->u_regs[5], r->u_regs[6], r->u_regs[7]);
295         printk("%%O: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
296                r->u_regs[8], r->u_regs[9], r->u_regs[10], r->u_regs[11],
297                r->u_regs[12], r->u_regs[13], r->u_regs[14], r->u_regs[15]);
298         print_symbol("RPC: <%s>\n", r->u_regs[15]);
299
300         printk("%%L: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
301                rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3],
302                rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
303         printk("%%I: %08lx %08lx  %08lx %08lx  %08lx %08lx  %08lx %08lx\n",
304                rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
305                rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
306 }
307
308 /*
309  * The show_stack is an external API which we do not use ourselves.
310  * The oops is printed in die_if_kernel.
311  */
312 void show_stack(struct task_struct *tsk, unsigned long *_ksp)
313 {
314         unsigned long pc, fp;
315         unsigned long task_base;
316         struct reg_window *rw;
317         int count = 0;
318
319         if (tsk != NULL)
320                 task_base = (unsigned long) tsk->thread_info;
321         else
322                 task_base = (unsigned long) current_thread_info();
323
324         fp = (unsigned long) _ksp;
325         do {
326                 /* Bogus frame pointer? */
327                 if (fp < (task_base + sizeof(struct thread_info)) ||
328                     fp >= (task_base + (PAGE_SIZE << 1)))
329                         break;
330                 rw = (struct reg_window *) fp;
331                 pc = rw->ins[7];
332                 printk("[%08lx : ", pc);
333                 print_symbol("%s ] ", pc);
334                 fp = rw->ins[6];
335         } while (++count < 16);
336         printk("\n");
337 }
338
339 /*
340  * Note: sparc64 has a pretty intricated thread_saved_pc, check it out.
341  */
342 unsigned long thread_saved_pc(struct task_struct *tsk)
343 {
344         return tsk->thread_info->kpc;
345 }
346
347 /*
348  * Free current thread data structures etc..
349  */
350 void exit_thread(void)
351 {
352 #ifndef CONFIG_SMP
353         if(last_task_used_math == current) {
354 #else
355         if(current_thread_info()->flags & _TIF_USEDFPU) {
356 #endif
357                 /* Keep process from leaving FPU in a bogon state. */
358                 put_psr(get_psr() | PSR_EF);
359                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
360                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
361 #ifndef CONFIG_SMP
362                 last_task_used_math = NULL;
363 #else
364                 current_thread_info()->flags &= ~_TIF_USEDFPU;
365 #endif
366         }
367 }
368
369 void flush_thread(void)
370 {
371         current_thread_info()->w_saved = 0;
372
373         /* No new signal delivery by default */
374         current->thread.new_signal = 0;
375 #ifndef CONFIG_SMP
376         if(last_task_used_math == current) {
377 #else
378         if(current_thread_info()->flags & _TIF_USEDFPU) {
379 #endif
380                 /* Clean the fpu. */
381                 put_psr(get_psr() | PSR_EF);
382                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
383                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
384 #ifndef CONFIG_SMP
385                 last_task_used_math = NULL;
386 #else
387                 current_thread_info()->flags &= ~_TIF_USEDFPU;
388 #endif
389         }
390
391         /* Now, this task is no longer a kernel thread. */
392         current->thread.current_ds = USER_DS;
393         if (current->thread.flags & SPARC_FLAG_KTHREAD) {
394                 current->thread.flags &= ~SPARC_FLAG_KTHREAD;
395
396                 /* We must fixup kregs as well. */
397                 /* XXX This was not fixed for ti for a while, worked. Unused? */
398                 current->thread.kregs = (struct pt_regs *)
399                     ((char *)current->thread_info + (THREAD_SIZE - TRACEREG_SZ));
400         }
401 }
402
403 static __inline__ struct sparc_stackf __user *
404 clone_stackframe(struct sparc_stackf __user *dst,
405                  struct sparc_stackf __user *src)
406 {
407         unsigned long size, fp;
408         struct sparc_stackf *tmp;
409         struct sparc_stackf __user *sp;
410
411         if (get_user(tmp, &src->fp))
412                 return NULL;
413
414         fp = (unsigned long) tmp;
415         size = (fp - ((unsigned long) src));
416         fp = (unsigned long) dst;
417         sp = (struct sparc_stackf __user *)(fp - size); 
418
419         /* do_fork() grabs the parent semaphore, we must release it
420          * temporarily so we can build the child clone stack frame
421          * without deadlocking.
422          */
423         if (__copy_user(sp, src, size))
424                 sp = NULL;
425         else if (put_user(fp, &sp->fp))
426                 sp = NULL;
427
428         return sp;
429 }
430
431 asmlinkage int sparc_do_fork(unsigned long clone_flags,
432                              unsigned long stack_start,
433                              struct pt_regs *regs,
434                              unsigned long stack_size)
435 {
436         unsigned long parent_tid_ptr, child_tid_ptr;
437
438         clone_flags &= ~CLONE_IDLETASK;
439
440         parent_tid_ptr = regs->u_regs[UREG_I2];
441         child_tid_ptr = regs->u_regs[UREG_I4];
442
443         return do_fork(clone_flags, stack_start,
444                        regs, stack_size,
445                        (int __user *) parent_tid_ptr,
446                        (int __user *) child_tid_ptr);
447 }
448
449 /* Copy a Sparc thread.  The fork() return value conventions
450  * under SunOS are nothing short of bletcherous:
451  * Parent -->  %o0 == childs  pid, %o1 == 0
452  * Child  -->  %o0 == parents pid, %o1 == 1
453  *
454  * NOTE: We have a separate fork kpsr/kwim because
455  *       the parent could change these values between
456  *       sys_fork invocation and when we reach here
457  *       if the parent should sleep while trying to
458  *       allocate the task_struct and kernel stack in
459  *       do_fork().
460  * XXX See comment above sys_vfork in sparc64. todo.
461  */
462 extern void ret_from_fork(void);
463
464 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
465                 unsigned long unused,
466                 struct task_struct *p, struct pt_regs *regs)
467 {
468         struct thread_info *ti = p->thread_info;
469         struct pt_regs *childregs;
470         char *new_stack;
471
472 #ifndef CONFIG_SMP
473         if(last_task_used_math == current) {
474 #else
475         if(current_thread_info()->flags & _TIF_USEDFPU) {
476 #endif
477                 put_psr(get_psr() | PSR_EF);
478                 fpsave(&p->thread.float_regs[0], &p->thread.fsr,
479                        &p->thread.fpqueue[0], &p->thread.fpqdepth);
480 #ifdef CONFIG_SMP
481                 current_thread_info()->flags &= ~_TIF_USEDFPU;
482 #endif
483         }
484
485         p->set_child_tid = p->clear_child_tid = NULL;
486
487         /*
488          *  p->thread_info         new_stack   childregs
489          *  !                      !           !             {if(PSR_PS) }
490          *  V                      V (stk.fr.) V  (pt_regs)  { (stk.fr.) }
491          *  +----- - - - - - ------+===========+============={+==========}+
492          */
493         new_stack = (char*)ti + THREAD_SIZE;
494         if (regs->psr & PSR_PS)
495                 new_stack -= STACKFRAME_SZ;
496         new_stack -= STACKFRAME_SZ + TRACEREG_SZ;
497         memcpy(new_stack, (char *)regs - STACKFRAME_SZ, STACKFRAME_SZ + TRACEREG_SZ);
498         childregs = (struct pt_regs *) (new_stack + STACKFRAME_SZ);
499
500         /*
501          * A new process must start with interrupts closed in 2.5,
502          * because this is how Mingo's scheduler works (see schedule_tail
503          * and finish_arch_switch). If we do not do it, a timer interrupt hits
504          * before we unlock, attempts to re-take the rq->lock, and then we die.
505          * Thus, kpsr|=PSR_PIL.
506          */
507         ti->ksp = (unsigned long) new_stack;
508         ti->kpc = (((unsigned long) ret_from_fork) - 0x8);
509         ti->kpsr = current->thread.fork_kpsr | PSR_PIL;
510         ti->kwim = current->thread.fork_kwim;
511
512         if(regs->psr & PSR_PS) {
513                 extern struct pt_regs fake_swapper_regs;
514
515                 p->thread.kregs = &fake_swapper_regs;
516                 new_stack += STACKFRAME_SZ + TRACEREG_SZ;
517                 childregs->u_regs[UREG_FP] = (unsigned long) new_stack;
518                 p->thread.flags |= SPARC_FLAG_KTHREAD;
519                 p->thread.current_ds = KERNEL_DS;
520                 memcpy(new_stack, (void *)regs->u_regs[UREG_FP], STACKFRAME_SZ);
521                 childregs->u_regs[UREG_G6] = (unsigned long) ti;
522         } else {
523                 p->thread.kregs = childregs;
524                 childregs->u_regs[UREG_FP] = sp;
525                 p->thread.flags &= ~SPARC_FLAG_KTHREAD;
526                 p->thread.current_ds = USER_DS;
527
528                 if (sp != regs->u_regs[UREG_FP]) {
529                         struct sparc_stackf __user *childstack;
530                         struct sparc_stackf __user *parentstack;
531
532                         /*
533                          * This is a clone() call with supplied user stack.
534                          * Set some valid stack frames to give to the child.
535                          */
536                         childstack = (struct sparc_stackf __user *)
537                                 (sp & ~0x7UL);
538                         parentstack = (struct sparc_stackf __user *)
539                                 regs->u_regs[UREG_FP];
540
541 #if 0
542                         printk("clone: parent stack:\n");
543                         show_stackframe(parentstack);
544 #endif
545
546                         childstack = clone_stackframe(childstack, parentstack);
547                         if (!childstack)
548                                 return -EFAULT;
549
550 #if 0
551                         printk("clone: child stack:\n");
552                         show_stackframe(childstack);
553 #endif
554
555                         childregs->u_regs[UREG_FP] = (unsigned long)childstack;
556                 }
557         }
558
559         /* Set the return value for the child. */
560         childregs->u_regs[UREG_I0] = current->pid;
561         childregs->u_regs[UREG_I1] = 1;
562
563         /* Set the return value for the parent. */
564         regs->u_regs[UREG_I1] = 0;
565
566         if (clone_flags & CLONE_SETTLS)
567                 childregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
568
569         return 0;
570 }
571
572 /*
573  * fill in the user structure for a core dump..
574  */
575 void dump_thread(struct pt_regs * regs, struct user * dump)
576 {
577         unsigned long first_stack_page;
578
579         dump->magic = SUNOS_CORE_MAGIC;
580         dump->len = sizeof(struct user);
581         dump->regs.psr = regs->psr;
582         dump->regs.pc = regs->pc;
583         dump->regs.npc = regs->npc;
584         dump->regs.y = regs->y;
585         /* fuck me plenty */
586         memcpy(&dump->regs.regs[0], &regs->u_regs[1], (sizeof(unsigned long) * 15));
587         dump->uexec = current->thread.core_exec;
588         dump->u_tsize = (((unsigned long) current->mm->end_code) -
589                 ((unsigned long) current->mm->start_code)) & ~(PAGE_SIZE - 1);
590         dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1)));
591         dump->u_dsize -= dump->u_tsize;
592         dump->u_dsize &= ~(PAGE_SIZE - 1);
593         first_stack_page = (regs->u_regs[UREG_FP] & ~(PAGE_SIZE - 1));
594         dump->u_ssize = (TASK_SIZE - first_stack_page) & ~(PAGE_SIZE - 1);
595         memcpy(&dump->fpu.fpstatus.fregs.regs[0], &current->thread.float_regs[0], (sizeof(unsigned long) * 32));
596         dump->fpu.fpstatus.fsr = current->thread.fsr;
597         dump->fpu.fpstatus.flags = dump->fpu.fpstatus.extra = 0;
598         dump->fpu.fpstatus.fpq_count = current->thread.fpqdepth;
599         memcpy(&dump->fpu.fpstatus.fpq[0], &current->thread.fpqueue[0],
600                ((sizeof(unsigned long) * 2) * 16));
601         dump->sigcode = 0;
602 }
603
604 /*
605  * fill in the fpu structure for a core dump.
606  */
607 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
608 {
609         if (current->used_math == 0) {
610                 memset(fpregs, 0, sizeof(*fpregs));
611                 fpregs->pr_q_entrysize = 8;
612                 return 1;
613         }
614 #ifdef CONFIG_SMP
615         if (current_thread_info()->flags & _TIF_USEDFPU) {
616                 put_psr(get_psr() | PSR_EF);
617                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
618                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
619                 if (regs != NULL) {
620                         regs->psr &= ~(PSR_EF);
621                         current_thread_info()->flags &= ~(_TIF_USEDFPU);
622                 }
623         }
624 #else
625         if (current == last_task_used_math) {
626                 put_psr(get_psr() | PSR_EF);
627                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
628                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
629                 if (regs != NULL) {
630                         regs->psr &= ~(PSR_EF);
631                         last_task_used_math = 0;
632                 }
633         }
634 #endif
635         memcpy(&fpregs->pr_fr.pr_regs[0],
636                &current->thread.float_regs[0],
637                (sizeof(unsigned long) * 32));
638         fpregs->pr_fsr = current->thread.fsr;
639         fpregs->pr_qcnt = current->thread.fpqdepth;
640         fpregs->pr_q_entrysize = 8;
641         fpregs->pr_en = 1;
642         if(fpregs->pr_qcnt != 0) {
643                 memcpy(&fpregs->pr_q[0],
644                        &current->thread.fpqueue[0],
645                        sizeof(struct fpq) * fpregs->pr_qcnt);
646         }
647         /* Zero out the rest. */
648         memset(&fpregs->pr_q[fpregs->pr_qcnt], 0,
649                sizeof(struct fpq) * (32 - fpregs->pr_qcnt));
650         return 1;
651 }
652
653 /*
654  * sparc_execve() executes a new program after the asm stub has set
655  * things up for us.  This should basically do what I want it to.
656  */
657 asmlinkage int sparc_execve(struct pt_regs *regs)
658 {
659         int error, base = 0;
660         char *filename;
661
662         /* Check for indirect call. */
663         if(regs->u_regs[UREG_G1] == 0)
664                 base = 1;
665
666         filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
667         error = PTR_ERR(filename);
668         if(IS_ERR(filename))
669                 goto out;
670         error = do_execve(filename,
671                           (char __user * __user *)regs->u_regs[base + UREG_I1],
672                           (char __user * __user *)regs->u_regs[base + UREG_I2],
673                           regs);
674         putname(filename);
675         if (error == 0)
676                 current->ptrace &= ~PT_DTRACE;
677 out:
678         return error;
679 }
680
681 /*
682  * This is the mechanism for creating a new kernel thread.
683  *
684  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
685  * who haven't done an "execve()") should use this: it will work within
686  * a system call from a "real" process, but the process memory space will
687  * not be free'd until both the parent and the child have exited.
688  */
689 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
690 {
691         long retval;
692
693         __asm__ __volatile__("mov %4, %%g2\n\t"    /* Set aside fn ptr... */
694                              "mov %5, %%g3\n\t"    /* and arg. */
695                              "mov %1, %%g1\n\t"
696                              "mov %2, %%o0\n\t"    /* Clone flags. */
697                              "mov 0, %%o1\n\t"     /* usp arg == 0 */
698                              "t 0x10\n\t"          /* Linux/Sparc clone(). */
699                              "cmp %%o1, 0\n\t"
700                              "be 1f\n\t"           /* The parent, just return. */
701                              " nop\n\t"            /* Delay slot. */
702                              "jmpl %%g2, %%o7\n\t" /* Call the function. */
703                              " mov %%g3, %%o0\n\t" /* Get back the arg in delay. */
704                              "mov %3, %%g1\n\t"
705                              "t 0x10\n\t"          /* Linux/Sparc exit(). */
706                              /* Notreached by child. */
707                              "1: mov %%o0, %0\n\t" :
708                              "=r" (retval) :
709                              "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
710                              "i" (__NR_exit),  "r" (fn), "r" (arg) :
711                              "g1", "g2", "g3", "o0", "o1", "memory", "cc");
712         return retval;
713 }
714
715 unsigned long get_wchan(struct task_struct *task)
716 {
717         unsigned long pc, fp, bias = 0;
718         unsigned long task_base = (unsigned long) task;
719         unsigned long ret = 0;
720         struct reg_window *rw;
721         int count = 0;
722
723         if (!task || task == current ||
724             task->state == TASK_RUNNING)
725                 goto out;
726
727         fp = task->thread_info->ksp + bias;
728         do {
729                 /* Bogus frame pointer? */
730                 if (fp < (task_base + sizeof(struct thread_info)) ||
731                     fp >= (task_base + (2 * PAGE_SIZE)))
732                         break;
733                 rw = (struct reg_window *) fp;
734                 pc = rw->ins[7];
735                 if (!in_sched_functions(pc)) {
736                         ret = pc;
737                         goto out;
738                 }
739                 fp = rw->ins[6] + bias;
740         } while (++count < 16);
741
742 out:
743         return ret;
744 }
745