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