vserver 1.9.3
[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         parent_tid_ptr = regs->u_regs[UREG_I2];
439         child_tid_ptr = regs->u_regs[UREG_I4];
440
441         return do_fork(clone_flags, stack_start,
442                        regs, stack_size,
443                        (int __user *) parent_tid_ptr,
444                        (int __user *) child_tid_ptr);
445 }
446
447 /* Copy a Sparc thread.  The fork() return value conventions
448  * under SunOS are nothing short of bletcherous:
449  * Parent -->  %o0 == childs  pid, %o1 == 0
450  * Child  -->  %o0 == parents pid, %o1 == 1
451  *
452  * NOTE: We have a separate fork kpsr/kwim because
453  *       the parent could change these values between
454  *       sys_fork invocation and when we reach here
455  *       if the parent should sleep while trying to
456  *       allocate the task_struct and kernel stack in
457  *       do_fork().
458  * XXX See comment above sys_vfork in sparc64. todo.
459  */
460 extern void ret_from_fork(void);
461
462 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
463                 unsigned long unused,
464                 struct task_struct *p, struct pt_regs *regs)
465 {
466         struct thread_info *ti = p->thread_info;
467         struct pt_regs *childregs;
468         char *new_stack;
469
470 #ifndef CONFIG_SMP
471         if(last_task_used_math == current) {
472 #else
473         if(current_thread_info()->flags & _TIF_USEDFPU) {
474 #endif
475                 put_psr(get_psr() | PSR_EF);
476                 fpsave(&p->thread.float_regs[0], &p->thread.fsr,
477                        &p->thread.fpqueue[0], &p->thread.fpqdepth);
478 #ifdef CONFIG_SMP
479                 current_thread_info()->flags &= ~_TIF_USEDFPU;
480 #endif
481         }
482
483         p->set_child_tid = p->clear_child_tid = NULL;
484
485         /*
486          *  p->thread_info         new_stack   childregs
487          *  !                      !           !             {if(PSR_PS) }
488          *  V                      V (stk.fr.) V  (pt_regs)  { (stk.fr.) }
489          *  +----- - - - - - ------+===========+============={+==========}+
490          */
491         new_stack = (char*)ti + THREAD_SIZE;
492         if (regs->psr & PSR_PS)
493                 new_stack -= STACKFRAME_SZ;
494         new_stack -= STACKFRAME_SZ + TRACEREG_SZ;
495         memcpy(new_stack, (char *)regs - STACKFRAME_SZ, STACKFRAME_SZ + TRACEREG_SZ);
496         childregs = (struct pt_regs *) (new_stack + STACKFRAME_SZ);
497
498         /*
499          * A new process must start with interrupts closed in 2.5,
500          * because this is how Mingo's scheduler works (see schedule_tail
501          * and finish_arch_switch). If we do not do it, a timer interrupt hits
502          * before we unlock, attempts to re-take the rq->lock, and then we die.
503          * Thus, kpsr|=PSR_PIL.
504          */
505         ti->ksp = (unsigned long) new_stack;
506         ti->kpc = (((unsigned long) ret_from_fork) - 0x8);
507         ti->kpsr = current->thread.fork_kpsr | PSR_PIL;
508         ti->kwim = current->thread.fork_kwim;
509
510         if(regs->psr & PSR_PS) {
511                 extern struct pt_regs fake_swapper_regs;
512
513                 p->thread.kregs = &fake_swapper_regs;
514                 new_stack += STACKFRAME_SZ + TRACEREG_SZ;
515                 childregs->u_regs[UREG_FP] = (unsigned long) new_stack;
516                 p->thread.flags |= SPARC_FLAG_KTHREAD;
517                 p->thread.current_ds = KERNEL_DS;
518                 memcpy(new_stack, (void *)regs->u_regs[UREG_FP], STACKFRAME_SZ);
519                 childregs->u_regs[UREG_G6] = (unsigned long) ti;
520         } else {
521                 p->thread.kregs = childregs;
522                 childregs->u_regs[UREG_FP] = sp;
523                 p->thread.flags &= ~SPARC_FLAG_KTHREAD;
524                 p->thread.current_ds = USER_DS;
525
526                 if (sp != regs->u_regs[UREG_FP]) {
527                         struct sparc_stackf __user *childstack;
528                         struct sparc_stackf __user *parentstack;
529
530                         /*
531                          * This is a clone() call with supplied user stack.
532                          * Set some valid stack frames to give to the child.
533                          */
534                         childstack = (struct sparc_stackf __user *)
535                                 (sp & ~0x7UL);
536                         parentstack = (struct sparc_stackf __user *)
537                                 regs->u_regs[UREG_FP];
538
539 #if 0
540                         printk("clone: parent stack:\n");
541                         show_stackframe(parentstack);
542 #endif
543
544                         childstack = clone_stackframe(childstack, parentstack);
545                         if (!childstack)
546                                 return -EFAULT;
547
548 #if 0
549                         printk("clone: child stack:\n");
550                         show_stackframe(childstack);
551 #endif
552
553                         childregs->u_regs[UREG_FP] = (unsigned long)childstack;
554                 }
555         }
556
557         /* Set the return value for the child. */
558         childregs->u_regs[UREG_I0] = current->pid;
559         childregs->u_regs[UREG_I1] = 1;
560
561         /* Set the return value for the parent. */
562         regs->u_regs[UREG_I1] = 0;
563
564         if (clone_flags & CLONE_SETTLS)
565                 childregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
566
567         return 0;
568 }
569
570 /*
571  * fill in the user structure for a core dump..
572  */
573 void dump_thread(struct pt_regs * regs, struct user * dump)
574 {
575         unsigned long first_stack_page;
576
577         dump->magic = SUNOS_CORE_MAGIC;
578         dump->len = sizeof(struct user);
579         dump->regs.psr = regs->psr;
580         dump->regs.pc = regs->pc;
581         dump->regs.npc = regs->npc;
582         dump->regs.y = regs->y;
583         /* fuck me plenty */
584         memcpy(&dump->regs.regs[0], &regs->u_regs[1], (sizeof(unsigned long) * 15));
585         dump->uexec = current->thread.core_exec;
586         dump->u_tsize = (((unsigned long) current->mm->end_code) -
587                 ((unsigned long) current->mm->start_code)) & ~(PAGE_SIZE - 1);
588         dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1)));
589         dump->u_dsize -= dump->u_tsize;
590         dump->u_dsize &= ~(PAGE_SIZE - 1);
591         first_stack_page = (regs->u_regs[UREG_FP] & ~(PAGE_SIZE - 1));
592         dump->u_ssize = (TASK_SIZE - first_stack_page) & ~(PAGE_SIZE - 1);
593         memcpy(&dump->fpu.fpstatus.fregs.regs[0], &current->thread.float_regs[0], (sizeof(unsigned long) * 32));
594         dump->fpu.fpstatus.fsr = current->thread.fsr;
595         dump->fpu.fpstatus.flags = dump->fpu.fpstatus.extra = 0;
596         dump->fpu.fpstatus.fpq_count = current->thread.fpqdepth;
597         memcpy(&dump->fpu.fpstatus.fpq[0], &current->thread.fpqueue[0],
598                ((sizeof(unsigned long) * 2) * 16));
599         dump->sigcode = 0;
600 }
601
602 /*
603  * fill in the fpu structure for a core dump.
604  */
605 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
606 {
607         if (current->used_math == 0) {
608                 memset(fpregs, 0, sizeof(*fpregs));
609                 fpregs->pr_q_entrysize = 8;
610                 return 1;
611         }
612 #ifdef CONFIG_SMP
613         if (current_thread_info()->flags & _TIF_USEDFPU) {
614                 put_psr(get_psr() | PSR_EF);
615                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
616                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
617                 if (regs != NULL) {
618                         regs->psr &= ~(PSR_EF);
619                         current_thread_info()->flags &= ~(_TIF_USEDFPU);
620                 }
621         }
622 #else
623         if (current == last_task_used_math) {
624                 put_psr(get_psr() | PSR_EF);
625                 fpsave(&current->thread.float_regs[0], &current->thread.fsr,
626                        &current->thread.fpqueue[0], &current->thread.fpqdepth);
627                 if (regs != NULL) {
628                         regs->psr &= ~(PSR_EF);
629                         last_task_used_math = NULL;
630                 }
631         }
632 #endif
633         memcpy(&fpregs->pr_fr.pr_regs[0],
634                &current->thread.float_regs[0],
635                (sizeof(unsigned long) * 32));
636         fpregs->pr_fsr = current->thread.fsr;
637         fpregs->pr_qcnt = current->thread.fpqdepth;
638         fpregs->pr_q_entrysize = 8;
639         fpregs->pr_en = 1;
640         if(fpregs->pr_qcnt != 0) {
641                 memcpy(&fpregs->pr_q[0],
642                        &current->thread.fpqueue[0],
643                        sizeof(struct fpq) * fpregs->pr_qcnt);
644         }
645         /* Zero out the rest. */
646         memset(&fpregs->pr_q[fpregs->pr_qcnt], 0,
647                sizeof(struct fpq) * (32 - fpregs->pr_qcnt));
648         return 1;
649 }
650
651 /*
652  * sparc_execve() executes a new program after the asm stub has set
653  * things up for us.  This should basically do what I want it to.
654  */
655 asmlinkage int sparc_execve(struct pt_regs *regs)
656 {
657         int error, base = 0;
658         char *filename;
659
660         /* Check for indirect call. */
661         if(regs->u_regs[UREG_G1] == 0)
662                 base = 1;
663
664         filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
665         error = PTR_ERR(filename);
666         if(IS_ERR(filename))
667                 goto out;
668         error = do_execve(filename,
669                           (char __user * __user *)regs->u_regs[base + UREG_I1],
670                           (char __user * __user *)regs->u_regs[base + UREG_I2],
671                           regs);
672         putname(filename);
673         if (error == 0)
674                 current->ptrace &= ~PT_DTRACE;
675 out:
676         return error;
677 }
678
679 /*
680  * This is the mechanism for creating a new kernel thread.
681  *
682  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
683  * who haven't done an "execve()") should use this: it will work within
684  * a system call from a "real" process, but the process memory space will
685  * not be free'd until both the parent and the child have exited.
686  */
687 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
688 {
689         long retval;
690
691         __asm__ __volatile__("mov %4, %%g2\n\t"    /* Set aside fn ptr... */
692                              "mov %5, %%g3\n\t"    /* and arg. */
693                              "mov %1, %%g1\n\t"
694                              "mov %2, %%o0\n\t"    /* Clone flags. */
695                              "mov 0, %%o1\n\t"     /* usp arg == 0 */
696                              "t 0x10\n\t"          /* Linux/Sparc clone(). */
697                              "cmp %%o1, 0\n\t"
698                              "be 1f\n\t"           /* The parent, just return. */
699                              " nop\n\t"            /* Delay slot. */
700                              "jmpl %%g2, %%o7\n\t" /* Call the function. */
701                              " mov %%g3, %%o0\n\t" /* Get back the arg in delay. */
702                              "mov %3, %%g1\n\t"
703                              "t 0x10\n\t"          /* Linux/Sparc exit(). */
704                              /* Notreached by child. */
705                              "1: mov %%o0, %0\n\t" :
706                              "=r" (retval) :
707                              "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
708                              "i" (__NR_exit),  "r" (fn), "r" (arg) :
709                              "g1", "g2", "g3", "o0", "o1", "memory", "cc");
710         return retval;
711 }
712
713 unsigned long get_wchan(struct task_struct *task)
714 {
715         unsigned long pc, fp, bias = 0;
716         unsigned long task_base = (unsigned long) task;
717         unsigned long ret = 0;
718         struct reg_window *rw;
719         int count = 0;
720
721         if (!task || task == current ||
722             task->state == TASK_RUNNING)
723                 goto out;
724
725         fp = task->thread_info->ksp + bias;
726         do {
727                 /* Bogus frame pointer? */
728                 if (fp < (task_base + sizeof(struct thread_info)) ||
729                     fp >= (task_base + (2 * PAGE_SIZE)))
730                         break;
731                 rw = (struct reg_window *) fp;
732                 pc = rw->ins[7];
733                 if (!in_sched_functions(pc)) {
734                         ret = pc;
735                         goto out;
736                 }
737                 fp = rw->ins[6] + bias;
738         } while (++count < 16);
739
740 out:
741         return ret;
742 }
743