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