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