sparc64: Move reboot handling into seperate file and kill power reg programming.
[linux-2.6] / arch / sparc64 / kernel / process.c
1 /*  arch/sparc64/kernel/process.c
2  *
3  *  Copyright (C) 1995, 1996, 2008 David S. Miller (davem@davemloft.net)
4  *  Copyright (C) 1996       Eddie C. Dost   (ecd@skynet.be)
5  *  Copyright (C) 1997, 1998 Jakub Jelinek   (jj@sunsite.mff.cuni.cz)
6  */
7
8 /*
9  * This file handles the architecture-dependent parts of process handling..
10  */
11
12 #include <stdarg.h>
13
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/fs.h>
20 #include <linux/smp.h>
21 #include <linux/stddef.h>
22 #include <linux/ptrace.h>
23 #include <linux/slab.h>
24 #include <linux/user.h>
25 #include <linux/delay.h>
26 #include <linux/compat.h>
27 #include <linux/tick.h>
28 #include <linux/init.h>
29 #include <linux/cpu.h>
30 #include <linux/elfcore.h>
31 #include <linux/sysrq.h>
32
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <asm/page.h>
36 #include <asm/pgalloc.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
39 #include <asm/pstate.h>
40 #include <asm/elf.h>
41 #include <asm/fpumacro.h>
42 #include <asm/head.h>
43 #include <asm/cpudata.h>
44 #include <asm/mmu_context.h>
45 #include <asm/unistd.h>
46 #include <asm/hypervisor.h>
47 #include <asm/syscalls.h>
48 #include <asm/irq_regs.h>
49 #include <asm/smp.h>
50
51 #include "kstack.h"
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(1);
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 #ifdef CONFIG_COMPAT
115 static void show_regwindow32(struct pt_regs *regs)
116 {
117         struct reg_window32 __user *rw;
118         struct reg_window32 r_w;
119         mm_segment_t old_fs;
120         
121         __asm__ __volatile__ ("flushw");
122         rw = compat_ptr((unsigned)regs->u_regs[14]);
123         old_fs = get_fs();
124         set_fs (USER_DS);
125         if (copy_from_user (&r_w, rw, sizeof(r_w))) {
126                 set_fs (old_fs);
127                 return;
128         }
129
130         set_fs (old_fs);                        
131         printk("l0: %08x l1: %08x l2: %08x l3: %08x "
132                "l4: %08x l5: %08x l6: %08x l7: %08x\n",
133                r_w.locals[0], r_w.locals[1], r_w.locals[2], r_w.locals[3],
134                r_w.locals[4], r_w.locals[5], r_w.locals[6], r_w.locals[7]);
135         printk("i0: %08x i1: %08x i2: %08x i3: %08x "
136                "i4: %08x i5: %08x i6: %08x i7: %08x\n",
137                r_w.ins[0], r_w.ins[1], r_w.ins[2], r_w.ins[3],
138                r_w.ins[4], r_w.ins[5], r_w.ins[6], r_w.ins[7]);
139 }
140 #else
141 #define show_regwindow32(regs)  do { } while (0)
142 #endif
143
144 static void show_regwindow(struct pt_regs *regs)
145 {
146         struct reg_window __user *rw;
147         struct reg_window *rwk;
148         struct reg_window r_w;
149         mm_segment_t old_fs;
150
151         if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
152                 __asm__ __volatile__ ("flushw");
153                 rw = (struct reg_window __user *)
154                         (regs->u_regs[14] + STACK_BIAS);
155                 rwk = (struct reg_window *)
156                         (regs->u_regs[14] + STACK_BIAS);
157                 if (!(regs->tstate & TSTATE_PRIV)) {
158                         old_fs = get_fs();
159                         set_fs (USER_DS);
160                         if (copy_from_user (&r_w, rw, sizeof(r_w))) {
161                                 set_fs (old_fs);
162                                 return;
163                         }
164                         rwk = &r_w;
165                         set_fs (old_fs);                        
166                 }
167         } else {
168                 show_regwindow32(regs);
169                 return;
170         }
171         printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
172                rwk->locals[0], rwk->locals[1], rwk->locals[2], rwk->locals[3]);
173         printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
174                rwk->locals[4], rwk->locals[5], rwk->locals[6], rwk->locals[7]);
175         printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
176                rwk->ins[0], rwk->ins[1], rwk->ins[2], rwk->ins[3]);
177         printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
178                rwk->ins[4], rwk->ins[5], rwk->ins[6], rwk->ins[7]);
179         if (regs->tstate & TSTATE_PRIV)
180                 printk("I7: <%pS>\n", (void *) rwk->ins[7]);
181 }
182
183 void show_regs(struct pt_regs *regs)
184 {
185         printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x    %s\n", regs->tstate,
186                regs->tpc, regs->tnpc, regs->y, print_tainted());
187         printk("TPC: <%pS>\n", (void *) regs->tpc);
188         printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
189                regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
190                regs->u_regs[3]);
191         printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
192                regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
193                regs->u_regs[7]);
194         printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
195                regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
196                regs->u_regs[11]);
197         printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
198                regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
199                regs->u_regs[15]);
200         printk("RPC: <%pS>\n", (void *) regs->u_regs[15]);
201         show_regwindow(regs);
202 }
203
204 struct global_reg_snapshot global_reg_snapshot[NR_CPUS];
205 static DEFINE_SPINLOCK(global_reg_snapshot_lock);
206
207 static void __global_reg_self(struct thread_info *tp, struct pt_regs *regs,
208                               int this_cpu)
209 {
210         flushw_all();
211
212         global_reg_snapshot[this_cpu].tstate = regs->tstate;
213         global_reg_snapshot[this_cpu].tpc = regs->tpc;
214         global_reg_snapshot[this_cpu].tnpc = regs->tnpc;
215         global_reg_snapshot[this_cpu].o7 = regs->u_regs[UREG_I7];
216
217         if (regs->tstate & TSTATE_PRIV) {
218                 struct thread_info *tp = current_thread_info();
219                 struct reg_window *rw;
220
221                 rw = (struct reg_window *)
222                         (regs->u_regs[UREG_FP] + STACK_BIAS);
223                 if (kstack_valid(tp, (unsigned long) rw)) {
224                         global_reg_snapshot[this_cpu].i7 = rw->ins[7];
225                         rw = (struct reg_window *)
226                                 (rw->ins[6] + STACK_BIAS);
227                         if (kstack_valid(tp, (unsigned long) rw))
228                                 global_reg_snapshot[this_cpu].rpc = rw->ins[7];
229                 }
230         } else {
231                 global_reg_snapshot[this_cpu].i7 = 0;
232                 global_reg_snapshot[this_cpu].rpc = 0;
233         }
234         global_reg_snapshot[this_cpu].thread = tp;
235 }
236
237 /* In order to avoid hangs we do not try to synchronize with the
238  * global register dump client cpus.  The last store they make is to
239  * the thread pointer, so do a short poll waiting for that to become
240  * non-NULL.
241  */
242 static void __global_reg_poll(struct global_reg_snapshot *gp)
243 {
244         int limit = 0;
245
246         while (!gp->thread && ++limit < 100) {
247                 barrier();
248                 udelay(1);
249         }
250 }
251
252 void __trigger_all_cpu_backtrace(void)
253 {
254         struct thread_info *tp = current_thread_info();
255         struct pt_regs *regs = get_irq_regs();
256         unsigned long flags;
257         int this_cpu, cpu;
258
259         if (!regs)
260                 regs = tp->kregs;
261
262         spin_lock_irqsave(&global_reg_snapshot_lock, flags);
263
264         memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
265
266         this_cpu = raw_smp_processor_id();
267
268         __global_reg_self(tp, regs, this_cpu);
269
270         smp_fetch_global_regs();
271
272         for_each_online_cpu(cpu) {
273                 struct global_reg_snapshot *gp = &global_reg_snapshot[cpu];
274                 struct thread_info *tp;
275
276                 __global_reg_poll(gp);
277
278                 tp = gp->thread;
279                 printk("%c CPU[%3d]: TSTATE[%016lx] TPC[%016lx] TNPC[%016lx] TASK[%s:%d]\n",
280                        (cpu == this_cpu ? '*' : ' '), cpu,
281                        gp->tstate, gp->tpc, gp->tnpc,
282                        ((tp && tp->task) ? tp->task->comm : "NULL"),
283                        ((tp && tp->task) ? tp->task->pid : -1));
284
285                 if (gp->tstate & TSTATE_PRIV) {
286                         printk("             TPC[%pS] O7[%pS] I7[%pS] RPC[%pS]\n",
287                                (void *) gp->tpc,
288                                (void *) gp->o7,
289                                (void *) gp->i7,
290                                (void *) gp->rpc);
291                 } else {
292                         printk("             TPC[%lx] O7[%lx] I7[%lx] RPC[%lx]\n",
293                                gp->tpc, gp->o7, gp->i7, gp->rpc);
294                 }
295         }
296
297         memset(global_reg_snapshot, 0, sizeof(global_reg_snapshot));
298
299         spin_unlock_irqrestore(&global_reg_snapshot_lock, flags);
300 }
301
302 #ifdef CONFIG_MAGIC_SYSRQ
303
304 static void sysrq_handle_globreg(int key, struct tty_struct *tty)
305 {
306         __trigger_all_cpu_backtrace();
307 }
308
309 static struct sysrq_key_op sparc_globalreg_op = {
310         .handler        = sysrq_handle_globreg,
311         .help_msg       = "Globalregs",
312         .action_msg     = "Show Global CPU Regs",
313 };
314
315 static int __init sparc_globreg_init(void)
316 {
317         return register_sysrq_key('y', &sparc_globalreg_op);
318 }
319
320 core_initcall(sparc_globreg_init);
321
322 #endif
323
324 unsigned long thread_saved_pc(struct task_struct *tsk)
325 {
326         struct thread_info *ti = task_thread_info(tsk);
327         unsigned long ret = 0xdeadbeefUL;
328         
329         if (ti && ti->ksp) {
330                 unsigned long *sp;
331                 sp = (unsigned long *)(ti->ksp + STACK_BIAS);
332                 if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
333                     sp[14]) {
334                         unsigned long *fp;
335                         fp = (unsigned long *)(sp[14] + STACK_BIAS);
336                         if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
337                                 ret = fp[15];
338                 }
339         }
340         return ret;
341 }
342
343 /* Free current thread data structures etc.. */
344 void exit_thread(void)
345 {
346         struct thread_info *t = current_thread_info();
347
348         if (t->utraps) {
349                 if (t->utraps[0] < 2)
350                         kfree (t->utraps);
351                 else
352                         t->utraps[0]--;
353         }
354
355         if (test_and_clear_thread_flag(TIF_PERFCTR)) {
356                 t->user_cntd0 = t->user_cntd1 = NULL;
357                 t->pcr_reg = 0;
358                 write_pcr(0);
359         }
360 }
361
362 void flush_thread(void)
363 {
364         struct thread_info *t = current_thread_info();
365         struct mm_struct *mm;
366
367         if (test_ti_thread_flag(t, TIF_ABI_PENDING)) {
368                 clear_ti_thread_flag(t, TIF_ABI_PENDING);
369                 if (test_ti_thread_flag(t, TIF_32BIT))
370                         clear_ti_thread_flag(t, TIF_32BIT);
371                 else
372                         set_ti_thread_flag(t, TIF_32BIT);
373         }
374
375         mm = t->task->mm;
376         if (mm)
377                 tsb_context_switch(mm);
378
379         set_thread_wsaved(0);
380
381         /* Turn off performance counters if on. */
382         if (test_and_clear_thread_flag(TIF_PERFCTR)) {
383                 t->user_cntd0 = t->user_cntd1 = NULL;
384                 t->pcr_reg = 0;
385                 write_pcr(0);
386         }
387
388         /* Clear FPU register state. */
389         t->fpsaved[0] = 0;
390         
391         if (get_thread_current_ds() != ASI_AIUS)
392                 set_fs(USER_DS);
393 }
394
395 /* It's a bit more tricky when 64-bit tasks are involved... */
396 static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
397 {
398         unsigned long fp, distance, rval;
399
400         if (!(test_thread_flag(TIF_32BIT))) {
401                 csp += STACK_BIAS;
402                 psp += STACK_BIAS;
403                 __get_user(fp, &(((struct reg_window __user *)psp)->ins[6]));
404                 fp += STACK_BIAS;
405         } else
406                 __get_user(fp, &(((struct reg_window32 __user *)psp)->ins[6]));
407
408         /* Now 8-byte align the stack as this is mandatory in the
409          * Sparc ABI due to how register windows work.  This hides
410          * the restriction from thread libraries etc.  -DaveM
411          */
412         csp &= ~7UL;
413
414         distance = fp - psp;
415         rval = (csp - distance);
416         if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
417                 rval = 0;
418         else if (test_thread_flag(TIF_32BIT)) {
419                 if (put_user(((u32)csp),
420                              &(((struct reg_window32 __user *)rval)->ins[6])))
421                         rval = 0;
422         } else {
423                 if (put_user(((u64)csp - STACK_BIAS),
424                              &(((struct reg_window __user *)rval)->ins[6])))
425                         rval = 0;
426                 else
427                         rval = rval - STACK_BIAS;
428         }
429
430         return rval;
431 }
432
433 /* Standard stuff. */
434 static inline void shift_window_buffer(int first_win, int last_win,
435                                        struct thread_info *t)
436 {
437         int i;
438
439         for (i = first_win; i < last_win; i++) {
440                 t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
441                 memcpy(&t->reg_window[i], &t->reg_window[i+1],
442                        sizeof(struct reg_window));
443         }
444 }
445
446 void synchronize_user_stack(void)
447 {
448         struct thread_info *t = current_thread_info();
449         unsigned long window;
450
451         flush_user_windows();
452         if ((window = get_thread_wsaved()) != 0) {
453                 int winsize = sizeof(struct reg_window);
454                 int bias = 0;
455
456                 if (test_thread_flag(TIF_32BIT))
457                         winsize = sizeof(struct reg_window32);
458                 else
459                         bias = STACK_BIAS;
460
461                 window -= 1;
462                 do {
463                         unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
464                         struct reg_window *rwin = &t->reg_window[window];
465
466                         if (!copy_to_user((char __user *)sp, rwin, winsize)) {
467                                 shift_window_buffer(window, get_thread_wsaved() - 1, t);
468                                 set_thread_wsaved(get_thread_wsaved() - 1);
469                         }
470                 } while (window--);
471         }
472 }
473
474 static void stack_unaligned(unsigned long sp)
475 {
476         siginfo_t info;
477
478         info.si_signo = SIGBUS;
479         info.si_errno = 0;
480         info.si_code = BUS_ADRALN;
481         info.si_addr = (void __user *) sp;
482         info.si_trapno = 0;
483         force_sig_info(SIGBUS, &info, current);
484 }
485
486 void fault_in_user_windows(void)
487 {
488         struct thread_info *t = current_thread_info();
489         unsigned long window;
490         int winsize = sizeof(struct reg_window);
491         int bias = 0;
492
493         if (test_thread_flag(TIF_32BIT))
494                 winsize = sizeof(struct reg_window32);
495         else
496                 bias = STACK_BIAS;
497
498         flush_user_windows();
499         window = get_thread_wsaved();
500
501         if (likely(window != 0)) {
502                 window -= 1;
503                 do {
504                         unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
505                         struct reg_window *rwin = &t->reg_window[window];
506
507                         if (unlikely(sp & 0x7UL))
508                                 stack_unaligned(sp);
509
510                         if (unlikely(copy_to_user((char __user *)sp,
511                                                   rwin, winsize)))
512                                 goto barf;
513                 } while (window--);
514         }
515         set_thread_wsaved(0);
516         return;
517
518 barf:
519         set_thread_wsaved(window + 1);
520         do_exit(SIGILL);
521 }
522
523 asmlinkage long sparc_do_fork(unsigned long clone_flags,
524                               unsigned long stack_start,
525                               struct pt_regs *regs,
526                               unsigned long stack_size)
527 {
528         int __user *parent_tid_ptr, *child_tid_ptr;
529         unsigned long orig_i1 = regs->u_regs[UREG_I1];
530         long ret;
531
532 #ifdef CONFIG_COMPAT
533         if (test_thread_flag(TIF_32BIT)) {
534                 parent_tid_ptr = compat_ptr(regs->u_regs[UREG_I2]);
535                 child_tid_ptr = compat_ptr(regs->u_regs[UREG_I4]);
536         } else
537 #endif
538         {
539                 parent_tid_ptr = (int __user *) regs->u_regs[UREG_I2];
540                 child_tid_ptr = (int __user *) regs->u_regs[UREG_I4];
541         }
542
543         ret = do_fork(clone_flags, stack_start,
544                       regs, stack_size,
545                       parent_tid_ptr, child_tid_ptr);
546
547         /* If we get an error and potentially restart the system
548          * call, we're screwed because copy_thread() clobbered
549          * the parent's %o1.  So detect that case and restore it
550          * here.
551          */
552         if ((unsigned long)ret >= -ERESTART_RESTARTBLOCK)
553                 regs->u_regs[UREG_I1] = orig_i1;
554
555         return ret;
556 }
557
558 /* Copy a Sparc thread.  The fork() return value conventions
559  * under SunOS are nothing short of bletcherous:
560  * Parent -->  %o0 == childs  pid, %o1 == 0
561  * Child  -->  %o0 == parents pid, %o1 == 1
562  */
563 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
564                 unsigned long unused,
565                 struct task_struct *p, struct pt_regs *regs)
566 {
567         struct thread_info *t = task_thread_info(p);
568         struct sparc_stackf *parent_sf;
569         unsigned long child_stack_sz;
570         char *child_trap_frame;
571         int kernel_thread;
572
573         kernel_thread = (regs->tstate & TSTATE_PRIV) ? 1 : 0;
574         parent_sf = ((struct sparc_stackf *) regs) - 1;
575
576         /* Calculate offset to stack_frame & pt_regs */
577         child_stack_sz = ((STACKFRAME_SZ + TRACEREG_SZ) +
578                           (kernel_thread ? STACKFRAME_SZ : 0));
579         child_trap_frame = (task_stack_page(p) +
580                             (THREAD_SIZE - child_stack_sz));
581         memcpy(child_trap_frame, parent_sf, child_stack_sz);
582
583         t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) |
584                                  (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
585                 (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
586         t->new_child = 1;
587         t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
588         t->kregs = (struct pt_regs *) (child_trap_frame +
589                                        sizeof(struct sparc_stackf));
590         t->fpsaved[0] = 0;
591
592         if (kernel_thread) {
593                 struct sparc_stackf *child_sf = (struct sparc_stackf *)
594                         (child_trap_frame + (STACKFRAME_SZ + TRACEREG_SZ));
595
596                 /* Zero terminate the stack backtrace.  */
597                 child_sf->fp = NULL;
598                 t->kregs->u_regs[UREG_FP] =
599                   ((unsigned long) child_sf) - STACK_BIAS;
600
601                 /* Special case, if we are spawning a kernel thread from
602                  * a userspace task (usermode helper, NFS or similar), we
603                  * must disable performance counters in the child because
604                  * the address space and protection realm are changing.
605                  */
606                 if (t->flags & _TIF_PERFCTR) {
607                         t->user_cntd0 = t->user_cntd1 = NULL;
608                         t->pcr_reg = 0;
609                         t->flags &= ~_TIF_PERFCTR;
610                 }
611                 t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
612                 t->kregs->u_regs[UREG_G6] = (unsigned long) t;
613                 t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
614         } else {
615                 if (t->flags & _TIF_32BIT) {
616                         sp &= 0x00000000ffffffffUL;
617                         regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
618                 }
619                 t->kregs->u_regs[UREG_FP] = sp;
620                 t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
621                 if (sp != regs->u_regs[UREG_FP]) {
622                         unsigned long csp;
623
624                         csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
625                         if (!csp)
626                                 return -EFAULT;
627                         t->kregs->u_regs[UREG_FP] = csp;
628                 }
629                 if (t->utraps)
630                         t->utraps[0]++;
631         }
632
633         /* Set the return value for the child. */
634         t->kregs->u_regs[UREG_I0] = current->pid;
635         t->kregs->u_regs[UREG_I1] = 1;
636
637         /* Set the second return value for the parent. */
638         regs->u_regs[UREG_I1] = 0;
639
640         if (clone_flags & CLONE_SETTLS)
641                 t->kregs->u_regs[UREG_G7] = regs->u_regs[UREG_I3];
642
643         return 0;
644 }
645
646 /*
647  * This is the mechanism for creating a new kernel thread.
648  *
649  * NOTE! Only a kernel-only process(ie the swapper or direct descendants
650  * who haven't done an "execve()") should use this: it will work within
651  * a system call from a "real" process, but the process memory space will
652  * not be freed until both the parent and the child have exited.
653  */
654 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
655 {
656         long retval;
657
658         /* If the parent runs before fn(arg) is called by the child,
659          * the input registers of this function can be clobbered.
660          * So we stash 'fn' and 'arg' into global registers which
661          * will not be modified by the parent.
662          */
663         __asm__ __volatile__("mov %4, %%g2\n\t"    /* Save FN into global */
664                              "mov %5, %%g3\n\t"    /* Save ARG into global */
665                              "mov %1, %%g1\n\t"    /* Clone syscall nr. */
666                              "mov %2, %%o0\n\t"    /* Clone flags. */
667                              "mov 0, %%o1\n\t"     /* usp arg == 0 */
668                              "t 0x6d\n\t"          /* Linux/Sparc clone(). */
669                              "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
670                              " mov %%o0, %0\n\t"
671                              "jmpl %%g2, %%o7\n\t"   /* Call the function. */
672                              " mov %%g3, %%o0\n\t"   /* Set arg in delay. */
673                              "mov %3, %%g1\n\t"
674                              "t 0x6d\n\t"          /* Linux/Sparc exit(). */
675                              /* Notreached by child. */
676                              "1:" :
677                              "=r" (retval) :
678                              "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
679                              "i" (__NR_exit),  "r" (fn), "r" (arg) :
680                              "g1", "g2", "g3", "o0", "o1", "memory", "cc");
681         return retval;
682 }
683
684 typedef struct {
685         union {
686                 unsigned int    pr_regs[32];
687                 unsigned long   pr_dregs[16];
688         } pr_fr;
689         unsigned int __unused;
690         unsigned int    pr_fsr;
691         unsigned char   pr_qcnt;
692         unsigned char   pr_q_entrysize;
693         unsigned char   pr_en;
694         unsigned int    pr_q[64];
695 } elf_fpregset_t32;
696
697 /*
698  * fill in the fpu structure for a core dump.
699  */
700 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
701 {
702         unsigned long *kfpregs = current_thread_info()->fpregs;
703         unsigned long fprs = current_thread_info()->fpsaved[0];
704
705         if (test_thread_flag(TIF_32BIT)) {
706                 elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
707
708                 if (fprs & FPRS_DL)
709                         memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
710                                sizeof(unsigned int) * 32);
711                 else
712                         memset(&fpregs32->pr_fr.pr_regs[0], 0,
713                                sizeof(unsigned int) * 32);
714                 fpregs32->pr_qcnt = 0;
715                 fpregs32->pr_q_entrysize = 8;
716                 memset(&fpregs32->pr_q[0], 0,
717                        (sizeof(unsigned int) * 64));
718                 if (fprs & FPRS_FEF) {
719                         fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
720                         fpregs32->pr_en = 1;
721                 } else {
722                         fpregs32->pr_fsr = 0;
723                         fpregs32->pr_en = 0;
724                 }
725         } else {
726                 if(fprs & FPRS_DL)
727                         memcpy(&fpregs->pr_regs[0], kfpregs,
728                                sizeof(unsigned int) * 32);
729                 else
730                         memset(&fpregs->pr_regs[0], 0,
731                                sizeof(unsigned int) * 32);
732                 if(fprs & FPRS_DU)
733                         memcpy(&fpregs->pr_regs[16], kfpregs+16,
734                                sizeof(unsigned int) * 32);
735                 else
736                         memset(&fpregs->pr_regs[16], 0,
737                                sizeof(unsigned int) * 32);
738                 if(fprs & FPRS_FEF) {
739                         fpregs->pr_fsr = current_thread_info()->xfsr[0];
740                         fpregs->pr_gsr = current_thread_info()->gsr[0];
741                 } else {
742                         fpregs->pr_fsr = fpregs->pr_gsr = 0;
743                 }
744                 fpregs->pr_fprs = fprs;
745         }
746         return 1;
747 }
748
749 /*
750  * sparc_execve() executes a new program after the asm stub has set
751  * things up for us.  This should basically do what I want it to.
752  */
753 asmlinkage int sparc_execve(struct pt_regs *regs)
754 {
755         int error, base = 0;
756         char *filename;
757
758         /* User register window flush is done by entry.S */
759
760         /* Check for indirect call. */
761         if (regs->u_regs[UREG_G1] == 0)
762                 base = 1;
763
764         filename = getname((char __user *)regs->u_regs[base + UREG_I0]);
765         error = PTR_ERR(filename);
766         if (IS_ERR(filename))
767                 goto out;
768         error = do_execve(filename,
769                           (char __user * __user *)
770                           regs->u_regs[base + UREG_I1],
771                           (char __user * __user *)
772                           regs->u_regs[base + UREG_I2], regs);
773         putname(filename);
774         if (!error) {
775                 fprs_write(0);
776                 current_thread_info()->xfsr[0] = 0;
777                 current_thread_info()->fpsaved[0] = 0;
778                 regs->tstate &= ~TSTATE_PEF;
779         }
780 out:
781         return error;
782 }
783
784 unsigned long get_wchan(struct task_struct *task)
785 {
786         unsigned long pc, fp, bias = 0;
787         struct thread_info *tp;
788         struct reg_window *rw;
789         unsigned long ret = 0;
790         int count = 0; 
791
792         if (!task || task == current ||
793             task->state == TASK_RUNNING)
794                 goto out;
795
796         tp = task_thread_info(task);
797         bias = STACK_BIAS;
798         fp = task_thread_info(task)->ksp + bias;
799
800         do {
801                 if (!kstack_valid(tp, fp))
802                         break;
803                 rw = (struct reg_window *) fp;
804                 pc = rw->ins[7];
805                 if (!in_sched_functions(pc)) {
806                         ret = pc;
807                         goto out;
808                 }
809                 fp = rw->ins[6] + bias;
810         } while (++count < 16);
811
812 out:
813         return ret;
814 }