Merge with 2.5.75.
[linux-2.6/linux-mips.git] / arch / sparc64 / kernel / process.c
blob46d473c1041796d98205695080fa6e1f505d1aaa
1 /* $Id: process.c,v 1.131 2002/02/09 19:49:30 davem Exp $
2 * arch/sparc64/kernel/process.c
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 */
9 /*
10 * This file handles the architecture-dependent parts of process handling..
13 #define __KERNEL_SYSCALLS__
14 #include <stdarg.h>
16 #include <linux/errno.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/smp_lock.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.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>
33 #include <asm/oplib.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <asm/page.h>
37 #include <asm/pgalloc.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
40 #include <asm/pstate.h>
41 #include <asm/elf.h>
42 #include <asm/fpumacro.h>
43 #include <asm/head.h>
45 /* #define VERBOSE_SHOWREGS */
48 * Nothing special yet...
50 void default_idle(void)
54 #ifndef CONFIG_SMP
57 * the idle loop on a Sparc... ;)
59 int cpu_idle(void)
61 if (current->pid != 0)
62 return -EPERM;
64 /* endless idle loop with no priority at all */
65 for (;;) {
66 /* If current->work.need_resched is zero we should really
67 * setup for a system wakup event and execute a shutdown
68 * instruction.
70 * But this requires writing back the contents of the
71 * L2 cache etc. so implement this later. -DaveM
73 while (!need_resched())
74 barrier();
76 schedule();
77 check_pgt_cache();
79 return 0;
82 #else
85 * the idle loop on a UltraMultiPenguin...
87 #define idle_me_harder() (cpu_data[smp_processor_id()].idle_volume += 1)
88 #define unidle_me() (cpu_data[smp_processor_id()].idle_volume = 0)
89 int cpu_idle(void)
91 set_thread_flag(TIF_POLLING_NRFLAG);
92 while(1) {
93 if (need_resched()) {
94 unidle_me();
95 clear_thread_flag(TIF_POLLING_NRFLAG);
96 schedule();
97 set_thread_flag(TIF_POLLING_NRFLAG);
98 check_pgt_cache();
100 idle_me_harder();
102 /* The store ordering is so that IRQ handlers on
103 * other cpus see our increasing idleness for the buddy
104 * redistribution algorithm. -DaveM
106 membar("#StoreStore | #StoreLoad");
110 #endif
112 extern char reboot_command [];
114 extern void (*prom_palette)(int);
115 extern void (*prom_keyboard)(void);
117 void machine_halt(void)
119 if (!serial_console && prom_palette)
120 prom_palette (1);
121 if (prom_keyboard)
122 prom_keyboard();
123 prom_halt();
124 panic("Halt failed!");
127 void machine_alt_power_off(void)
129 if (!serial_console && prom_palette)
130 prom_palette(1);
131 if (prom_keyboard)
132 prom_keyboard();
133 prom_halt_power_off();
134 panic("Power-off failed!");
137 void machine_restart(char * cmd)
139 char *p;
141 p = strchr (reboot_command, '\n');
142 if (p) *p = 0;
143 if (!serial_console && prom_palette)
144 prom_palette (1);
145 if (prom_keyboard)
146 prom_keyboard();
147 if (cmd)
148 prom_reboot(cmd);
149 if (*reboot_command)
150 prom_reboot(reboot_command);
151 prom_reboot("");
152 panic("Reboot failed!");
155 static void show_regwindow32(struct pt_regs *regs)
157 struct reg_window32 *rw;
158 struct reg_window32 r_w;
159 mm_segment_t old_fs;
161 __asm__ __volatile__ ("flushw");
162 rw = (struct reg_window32 *)((long)(unsigned)regs->u_regs[14]);
163 old_fs = get_fs();
164 set_fs (USER_DS);
165 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
166 set_fs (old_fs);
167 return;
169 rw = &r_w;
170 set_fs (old_fs);
171 printk("l0: %08x l1: %08x l2: %08x l3: %08x "
172 "l4: %08x l5: %08x l6: %08x l7: %08x\n",
173 rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3],
174 rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
175 printk("i0: %08x i1: %08x i2: %08x i3: %08x "
176 "i4: %08x i5: %08x i6: %08x i7: %08x\n",
177 rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
178 rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
181 static void show_regwindow(struct pt_regs *regs)
183 struct reg_window *rw;
184 struct reg_window r_w;
185 mm_segment_t old_fs;
187 if ((regs->tstate & TSTATE_PRIV) || !(test_thread_flag(TIF_32BIT))) {
188 __asm__ __volatile__ ("flushw");
189 rw = (struct reg_window *)(regs->u_regs[14] + STACK_BIAS);
190 if (!(regs->tstate & TSTATE_PRIV)) {
191 old_fs = get_fs();
192 set_fs (USER_DS);
193 if (copy_from_user (&r_w, rw, sizeof(r_w))) {
194 set_fs (old_fs);
195 return;
197 rw = &r_w;
198 set_fs (old_fs);
200 } else {
201 show_regwindow32(regs);
202 return;
204 printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n",
205 rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3]);
206 printk("l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
207 rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
208 printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n",
209 rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3]);
210 printk("i4: %016lx i5: %016lx i6: %016lx i7: %016lx\n",
211 rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
214 void show_stackframe(struct sparc_stackf *sf)
216 unsigned long size;
217 unsigned long *stk;
218 int i;
220 printk("l0: %016lx l1: %016lx l2: %016lx l3: %016lx\n"
221 "l4: %016lx l5: %016lx l6: %016lx l7: %016lx\n",
222 sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3],
223 sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
224 printk("i0: %016lx i1: %016lx i2: %016lx i3: %016lx\n"
225 "i4: %016lx i5: %016lx fp: %016lx ret_pc: %016lx\n",
226 sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3],
227 sf->ins[4], sf->ins[5], (unsigned long)sf->fp, sf->callers_pc);
228 printk("sp: %016lx x0: %016lx x1: %016lx x2: %016lx\n"
229 "x3: %016lx x4: %016lx x5: %016lx xx: %016lx\n",
230 (unsigned long)sf->structptr, sf->xargs[0], sf->xargs[1],
231 sf->xargs[2], sf->xargs[3], sf->xargs[4], sf->xargs[5],
232 sf->xxargs[0]);
233 size = ((unsigned long)sf->fp) - ((unsigned long)sf);
234 size -= STACKFRAME_SZ;
235 stk = (unsigned long *)((unsigned long)sf + STACKFRAME_SZ);
236 i = 0;
237 do {
238 printk("s%d: %016lx\n", i++, *stk++);
239 } while ((size -= sizeof(unsigned long)));
242 void show_stackframe32(struct sparc_stackf32 *sf)
244 unsigned long size;
245 unsigned *stk;
246 int i;
248 printk("l0: %08x l1: %08x l2: %08x l3: %08x\n",
249 sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3]);
250 printk("l4: %08x l5: %08x l6: %08x l7: %08x\n",
251 sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
252 printk("i0: %08x i1: %08x i2: %08x i3: %08x\n",
253 sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3]);
254 printk("i4: %08x i5: %08x fp: %08x ret_pc: %08x\n",
255 sf->ins[4], sf->ins[5], sf->fp, sf->callers_pc);
256 printk("sp: %08x x0: %08x x1: %08x x2: %08x\n"
257 "x3: %08x x4: %08x x5: %08x xx: %08x\n",
258 sf->structptr, sf->xargs[0], sf->xargs[1],
259 sf->xargs[2], sf->xargs[3], sf->xargs[4], sf->xargs[5],
260 sf->xxargs[0]);
261 size = ((unsigned long)sf->fp) - ((unsigned long)sf);
262 size -= STACKFRAME32_SZ;
263 stk = (unsigned *)((unsigned long)sf + STACKFRAME32_SZ);
264 i = 0;
265 do {
266 printk("s%d: %08x\n", i++, *stk++);
267 } while ((size -= sizeof(unsigned)));
270 #ifdef CONFIG_SMP
271 static spinlock_t regdump_lock = SPIN_LOCK_UNLOCKED;
272 #endif
274 void __show_regs(struct pt_regs * regs)
276 #ifdef CONFIG_SMP
277 unsigned long flags;
279 /* Protect against xcall ipis which might lead to livelock on the lock */
280 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
281 "wrpr %0, %1, %%pstate"
282 : "=r" (flags)
283 : "i" (PSTATE_IE));
284 spin_lock(&regdump_lock);
285 #endif
286 printk("TSTATE: %016lx TPC: %016lx TNPC: %016lx Y: %08x %s\n", regs->tstate,
287 regs->tpc, regs->tnpc, regs->y, print_tainted());
288 print_symbol("TPC: <%s>\n", regs->tpc);
289 printk("g0: %016lx g1: %016lx g2: %016lx g3: %016lx\n",
290 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
291 regs->u_regs[3]);
292 printk("g4: %016lx g5: %016lx g6: %016lx g7: %016lx\n",
293 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
294 regs->u_regs[7]);
295 printk("o0: %016lx o1: %016lx o2: %016lx o3: %016lx\n",
296 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
297 regs->u_regs[11]);
298 printk("o4: %016lx o5: %016lx sp: %016lx ret_pc: %016lx\n",
299 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
300 regs->u_regs[15]);
301 show_regwindow(regs);
302 #ifdef CONFIG_SMP
303 spin_unlock(&regdump_lock);
304 __asm__ __volatile__("wrpr %0, 0, %%pstate"
305 : : "r" (flags));
306 #endif
309 #ifdef VERBOSE_SHOWREGS
310 static void idump_from_user (unsigned int *pc)
312 int i;
313 int code;
315 if((((unsigned long) pc) & 3))
316 return;
318 pc -= 3;
319 for(i = -3; i < 6; i++) {
320 get_user(code, pc);
321 printk("%c%08x%c",i?' ':'<',code,i?' ':'>');
322 pc++;
324 printk("\n");
326 #endif
328 void show_regs(struct pt_regs *regs)
330 #ifdef VERBOSE_SHOWREGS
331 extern long etrap, etraptl1;
332 #endif
333 __show_regs(regs);
334 #ifdef CONFIG_SMP
336 extern void smp_report_regs(void);
338 smp_report_regs();
340 #endif
342 #ifdef VERBOSE_SHOWREGS
343 if (regs->tpc >= &etrap && regs->tpc < &etraptl1 &&
344 regs->u_regs[14] >= (long)current - PAGE_SIZE &&
345 regs->u_regs[14] < (long)current + 6 * PAGE_SIZE) {
346 printk ("*********parent**********\n");
347 __show_regs((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF));
348 idump_from_user(((struct pt_regs *)(regs->u_regs[14] + PTREGS_OFF))->tpc);
349 printk ("*********endpar**********\n");
351 #endif
354 void show_regs32(struct pt_regs32 *regs)
356 printk("PSR: %08x PC: %08x NPC: %08x Y: %08x %s\n", regs->psr,
357 regs->pc, regs->npc, regs->y, print_tainted());
358 printk("g0: %08x g1: %08x g2: %08x g3: %08x ",
359 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
360 regs->u_regs[3]);
361 printk("g4: %08x g5: %08x g6: %08x g7: %08x\n",
362 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
363 regs->u_regs[7]);
364 printk("o0: %08x o1: %08x o2: %08x o3: %08x ",
365 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
366 regs->u_regs[11]);
367 printk("o4: %08x o5: %08x sp: %08x ret_pc: %08x\n",
368 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
369 regs->u_regs[15]);
372 unsigned long thread_saved_pc(struct task_struct *tsk)
374 struct thread_info *ti = tsk->thread_info;
375 unsigned long ret = 0xdeadbeefUL;
377 if (ti && ti->ksp) {
378 unsigned long *sp;
379 sp = (unsigned long *)(ti->ksp + STACK_BIAS);
380 if (((unsigned long)sp & (sizeof(long) - 1)) == 0UL &&
381 sp[14]) {
382 unsigned long *fp;
383 fp = (unsigned long *)(sp[14] + STACK_BIAS);
384 if (((unsigned long)fp & (sizeof(long) - 1)) == 0UL)
385 ret = fp[15];
388 return ret;
391 /* Free current thread data structures etc.. */
392 void exit_thread(void)
394 struct thread_info *t = current_thread_info();
396 if (t->utraps) {
397 if (t->utraps[0] < 2)
398 kfree (t->utraps);
399 else
400 t->utraps[0]--;
403 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
404 t->user_cntd0 = t->user_cntd1 = NULL;
405 t->pcr_reg = 0;
406 write_pcr(0);
410 void flush_thread(void)
412 struct thread_info *t = current_thread_info();
414 if (t->flags & _TIF_ABI_PENDING)
415 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
417 if (t->task->mm) {
418 unsigned long pgd_cache = 0UL;
419 if (test_thread_flag(TIF_32BIT)) {
420 struct mm_struct *mm = t->task->mm;
421 pgd_t *pgd0 = &mm->pgd[0];
423 if (pgd_none(*pgd0)) {
424 pmd_t *page = pmd_alloc_one_fast(NULL, 0);
425 if (!page)
426 page = pmd_alloc_one(NULL, 0);
427 pgd_set(pgd0, page);
429 pgd_cache = pgd_val(*pgd0) << 11UL;
431 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
432 "membar #Sync"
433 : /* no outputs */
434 : "r" (pgd_cache),
435 "r" (TSB_REG),
436 "i" (ASI_DMMU));
438 set_thread_wsaved(0);
440 /* Turn off performance counters if on. */
441 if (test_and_clear_thread_flag(TIF_PERFCTR)) {
442 t->user_cntd0 = t->user_cntd1 = NULL;
443 t->pcr_reg = 0;
444 write_pcr(0);
447 /* Clear FPU register state. */
448 t->fpsaved[0] = 0;
450 if (get_thread_current_ds() != ASI_AIUS)
451 set_fs(USER_DS);
453 /* Init new signal delivery disposition. */
454 clear_thread_flag(TIF_NEWSIGNALS);
457 /* It's a bit more tricky when 64-bit tasks are involved... */
458 static unsigned long clone_stackframe(unsigned long csp, unsigned long psp)
460 unsigned long fp, distance, rval;
462 if (!(test_thread_flag(TIF_32BIT))) {
463 csp += STACK_BIAS;
464 psp += STACK_BIAS;
465 __get_user(fp, &(((struct reg_window *)psp)->ins[6]));
466 fp += STACK_BIAS;
467 } else
468 __get_user(fp, &(((struct reg_window32 *)psp)->ins[6]));
470 /* Now 8-byte align the stack as this is mandatory in the
471 * Sparc ABI due to how register windows work. This hides
472 * the restriction from thread libraries etc. -DaveM
474 csp &= ~7UL;
476 distance = fp - psp;
477 rval = (csp - distance);
478 if (copy_in_user((void __user *) rval, (void __user *) psp, distance))
479 rval = 0;
480 else if (test_thread_flag(TIF_32BIT)) {
481 if (put_user(((u32)csp), &(((struct reg_window32 *)rval)->ins[6])))
482 rval = 0;
483 } else {
484 if (put_user(((u64)csp - STACK_BIAS),
485 &(((struct reg_window *)rval)->ins[6])))
486 rval = 0;
487 else
488 rval = rval - STACK_BIAS;
491 return rval;
494 /* Standard stuff. */
495 static inline void shift_window_buffer(int first_win, int last_win,
496 struct thread_info *t)
498 int i;
500 for (i = first_win; i < last_win; i++) {
501 t->rwbuf_stkptrs[i] = t->rwbuf_stkptrs[i+1];
502 memcpy(&t->reg_window[i], &t->reg_window[i+1],
503 sizeof(struct reg_window));
507 void synchronize_user_stack(void)
509 struct thread_info *t = current_thread_info();
510 unsigned long window;
512 flush_user_windows();
513 if ((window = get_thread_wsaved()) != 0) {
514 int winsize = sizeof(struct reg_window);
515 int bias = 0;
517 if (test_thread_flag(TIF_32BIT))
518 winsize = sizeof(struct reg_window32);
519 else
520 bias = STACK_BIAS;
522 window -= 1;
523 do {
524 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
525 struct reg_window *rwin = &t->reg_window[window];
527 if (!copy_to_user((char *)sp, rwin, winsize)) {
528 shift_window_buffer(window, get_thread_wsaved() - 1, t);
529 set_thread_wsaved(get_thread_wsaved() - 1);
531 } while (window--);
535 void fault_in_user_windows(void)
537 struct thread_info *t = current_thread_info();
538 unsigned long window;
539 int winsize = sizeof(struct reg_window);
540 int bias = 0;
542 if (test_thread_flag(TIF_32BIT))
543 winsize = sizeof(struct reg_window32);
544 else
545 bias = STACK_BIAS;
547 flush_user_windows();
548 window = get_thread_wsaved();
550 if (window != 0) {
551 window -= 1;
552 do {
553 unsigned long sp = (t->rwbuf_stkptrs[window] + bias);
554 struct reg_window *rwin = &t->reg_window[window];
556 if (copy_to_user((char *)sp, rwin, winsize))
557 goto barf;
558 } while (window--);
560 set_thread_wsaved(0);
561 return;
563 barf:
564 set_thread_wsaved(window + 1);
565 do_exit(SIGILL);
568 asmlinkage int sparc_do_fork(unsigned long clone_flags,
569 unsigned long stack_start,
570 struct pt_regs *regs,
571 unsigned long stack_size)
573 unsigned long parent_tid_ptr = 0;
574 unsigned long child_tid_ptr = 0;
576 clone_flags &= ~CLONE_IDLETASK;
578 if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
579 parent_tid_ptr = regs->u_regs[UREG_G2];
580 child_tid_ptr = regs->u_regs[UREG_G3];
581 if (test_thread_flag(TIF_32BIT)) {
582 parent_tid_ptr &= 0xffffffff;
583 child_tid_ptr &= 0xffffffff;
587 return do_fork(clone_flags, stack_start,
588 regs, stack_size,
589 (int *) parent_tid_ptr,
590 (int *) child_tid_ptr);
593 /* Copy a Sparc thread. The fork() return value conventions
594 * under SunOS are nothing short of bletcherous:
595 * Parent --> %o0 == childs pid, %o1 == 0
596 * Child --> %o0 == parents pid, %o1 == 1
598 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
599 unsigned long unused,
600 struct task_struct *p, struct pt_regs *regs)
602 struct thread_info *t = p->thread_info;
603 char *child_trap_frame;
605 #ifdef CONFIG_DEBUG_SPINLOCK
606 p->thread.smp_lock_count = 0;
607 p->thread.smp_lock_pc = 0;
608 #endif
610 p->set_child_tid = p->clear_child_tid = NULL;
612 /* Calculate offset to stack_frame & pt_regs */
613 child_trap_frame = ((char *)t) + (THREAD_SIZE - (TRACEREG_SZ+STACKFRAME_SZ));
614 memcpy(child_trap_frame, (((struct sparc_stackf *)regs)-1), (TRACEREG_SZ+STACKFRAME_SZ));
616 t->flags = (t->flags & ~((0xffUL << TI_FLAG_CWP_SHIFT) | (0xffUL << TI_FLAG_CURRENT_DS_SHIFT))) |
617 _TIF_NEWCHILD |
618 (((regs->tstate + 1) & TSTATE_CWP) << TI_FLAG_CWP_SHIFT);
619 t->ksp = ((unsigned long) child_trap_frame) - STACK_BIAS;
620 t->kregs = (struct pt_regs *)(child_trap_frame+sizeof(struct sparc_stackf));
621 t->fpsaved[0] = 0;
623 if (regs->tstate & TSTATE_PRIV) {
624 /* Special case, if we are spawning a kernel thread from
625 * a userspace task (via KMOD, NFS, or similar) we must
626 * disable performance counters in the child because the
627 * address space and protection realm are changing.
629 if (t->flags & _TIF_PERFCTR) {
630 t->user_cntd0 = t->user_cntd1 = NULL;
631 t->pcr_reg = 0;
632 t->flags &= ~_TIF_PERFCTR;
634 t->kregs->u_regs[UREG_FP] = t->ksp;
635 t->flags |= ((long)ASI_P << TI_FLAG_CURRENT_DS_SHIFT);
636 flush_register_windows();
637 memcpy((void *)(t->ksp + STACK_BIAS),
638 (void *)(regs->u_regs[UREG_FP] + STACK_BIAS),
639 sizeof(struct sparc_stackf));
640 t->kregs->u_regs[UREG_G6] = (unsigned long) t;
641 t->kregs->u_regs[UREG_G4] = (unsigned long) t->task;
642 } else {
643 if (t->flags & _TIF_32BIT) {
644 sp &= 0x00000000ffffffffUL;
645 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
647 t->kregs->u_regs[UREG_FP] = sp;
648 t->flags |= ((long)ASI_AIUS << TI_FLAG_CURRENT_DS_SHIFT);
649 if (sp != regs->u_regs[UREG_FP]) {
650 unsigned long csp;
652 csp = clone_stackframe(sp, regs->u_regs[UREG_FP]);
653 if (!csp)
654 return -EFAULT;
655 t->kregs->u_regs[UREG_FP] = csp;
657 if (t->utraps)
658 t->utraps[0]++;
661 /* Set the return value for the child. */
662 t->kregs->u_regs[UREG_I0] = current->pid;
663 t->kregs->u_regs[UREG_I1] = 1;
665 /* Set the second return value for the parent. */
666 regs->u_regs[UREG_I1] = 0;
668 return 0;
672 * This is the mechanism for creating a new kernel thread.
674 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
675 * who haven't done an "execve()") should use this: it will work within
676 * a system call from a "real" process, but the process memory space will
677 * not be free'd until both the parent and the child have exited.
679 pid_t kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
681 long retval;
683 /* If the parent runs before fn(arg) is called by the child,
684 * the input registers of this function can be clobbered.
685 * So we stash 'fn' and 'arg' into global registers which
686 * will not be modified by the parent.
688 __asm__ __volatile("mov %4, %%g2\n\t" /* Save FN into global */
689 "mov %5, %%g3\n\t" /* Save ARG into global */
690 "mov %1, %%g1\n\t" /* Clone syscall nr. */
691 "mov %2, %%o0\n\t" /* Clone flags. */
692 "mov 0, %%o1\n\t" /* usp arg == 0 */
693 "t 0x6d\n\t" /* Linux/Sparc clone(). */
694 "brz,a,pn %%o1, 1f\n\t" /* Parent, just return. */
695 " mov %%o0, %0\n\t"
696 "jmpl %%g2, %%o7\n\t" /* Call the function. */
697 " mov %%g3, %%o0\n\t" /* Set arg in delay. */
698 "mov %3, %%g1\n\t"
699 "t 0x6d\n\t" /* Linux/Sparc exit(). */
700 /* Notreached by child. */
701 "1:" :
702 "=r" (retval) :
703 "i" (__NR_clone), "r" (flags | CLONE_VM | CLONE_UNTRACED),
704 "i" (__NR_exit), "r" (fn), "r" (arg) :
705 "g1", "g2", "g3", "o0", "o1", "memory", "cc");
706 return retval;
710 * fill in the user structure for a core dump..
712 void dump_thread(struct pt_regs * regs, struct user * dump)
714 /* Only should be used for SunOS and ancient a.out
715 * SparcLinux binaries... Not worth implementing.
717 memset(dump, 0, sizeof(struct user));
720 typedef struct {
721 union {
722 unsigned int pr_regs[32];
723 unsigned long pr_dregs[16];
724 } pr_fr;
725 unsigned int __unused;
726 unsigned int pr_fsr;
727 unsigned char pr_qcnt;
728 unsigned char pr_q_entrysize;
729 unsigned char pr_en;
730 unsigned int pr_q[64];
731 } elf_fpregset_t32;
734 * fill in the fpu structure for a core dump.
736 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
738 unsigned long *kfpregs = current_thread_info()->fpregs;
739 unsigned long fprs = current_thread_info()->fpsaved[0];
741 if (test_thread_flag(TIF_32BIT)) {
742 elf_fpregset_t32 *fpregs32 = (elf_fpregset_t32 *)fpregs;
744 if (fprs & FPRS_DL)
745 memcpy(&fpregs32->pr_fr.pr_regs[0], kfpregs,
746 sizeof(unsigned int) * 32);
747 else
748 memset(&fpregs32->pr_fr.pr_regs[0], 0,
749 sizeof(unsigned int) * 32);
750 fpregs32->pr_qcnt = 0;
751 fpregs32->pr_q_entrysize = 8;
752 memset(&fpregs32->pr_q[0], 0,
753 (sizeof(unsigned int) * 64));
754 if (fprs & FPRS_FEF) {
755 fpregs32->pr_fsr = (unsigned int) current_thread_info()->xfsr[0];
756 fpregs32->pr_en = 1;
757 } else {
758 fpregs32->pr_fsr = 0;
759 fpregs32->pr_en = 0;
761 } else {
762 if(fprs & FPRS_DL)
763 memcpy(&fpregs->pr_regs[0], kfpregs,
764 sizeof(unsigned int) * 32);
765 else
766 memset(&fpregs->pr_regs[0], 0,
767 sizeof(unsigned int) * 32);
768 if(fprs & FPRS_DU)
769 memcpy(&fpregs->pr_regs[16], kfpregs+16,
770 sizeof(unsigned int) * 32);
771 else
772 memset(&fpregs->pr_regs[16], 0,
773 sizeof(unsigned int) * 32);
774 if(fprs & FPRS_FEF) {
775 fpregs->pr_fsr = current_thread_info()->xfsr[0];
776 fpregs->pr_gsr = current_thread_info()->gsr[0];
777 } else {
778 fpregs->pr_fsr = fpregs->pr_gsr = 0;
780 fpregs->pr_fprs = fprs;
782 return 1;
786 * sparc_execve() executes a new program after the asm stub has set
787 * things up for us. This should basically do what I want it to.
789 asmlinkage int sparc_execve(struct pt_regs *regs)
791 int error, base = 0;
792 char *filename;
794 /* User register window flush is done by entry.S */
796 /* Check for indirect call. */
797 if (regs->u_regs[UREG_G1] == 0)
798 base = 1;
800 filename = getname((char *)regs->u_regs[base + UREG_I0]);
801 error = PTR_ERR(filename);
802 if (IS_ERR(filename))
803 goto out;
804 error = do_execve(filename, (char **) regs->u_regs[base + UREG_I1],
805 (char **) regs->u_regs[base + UREG_I2], regs);
806 putname(filename);
807 if (!error) {
808 fprs_write(0);
809 current_thread_info()->xfsr[0] = 0;
810 current_thread_info()->fpsaved[0] = 0;
811 regs->tstate &= ~TSTATE_PEF;
812 current->ptrace &= ~PT_DTRACE;
814 out:
815 return error;
818 extern void scheduling_functions_start_here(void);
819 extern void scheduling_functions_end_here(void);
821 unsigned long get_wchan(struct task_struct *task)
823 unsigned long pc, fp, bias = 0;
824 unsigned long thread_info_base;
825 struct reg_window *rw;
826 unsigned long ret = 0;
827 int count = 0;
829 if (!task || task == current ||
830 task->state == TASK_RUNNING)
831 goto out;
833 thread_info_base = (unsigned long) task->thread_info;
834 bias = STACK_BIAS;
835 fp = task->thread_info->ksp + bias;
837 do {
838 /* Bogus frame pointer? */
839 if (fp < (thread_info_base + sizeof(struct thread_info)) ||
840 fp >= (thread_info_base + THREAD_SIZE))
841 break;
842 rw = (struct reg_window *) fp;
843 pc = rw->ins[7];
844 if (pc < ((unsigned long) scheduling_functions_start_here) ||
845 pc >= ((unsigned long) scheduling_functions_end_here)) {
846 ret = pc;
847 goto out;
849 fp = rw->ins[6] + bias;
850 } while (++count < 16);
852 out:
853 return ret;