1 /* $Id: process.c,v 1.158 2001/11/26 23:45:00 davem Exp $
2 * linux/arch/sparc/kernel/process.c
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
9 * This file handles the architecture-dependent parts of process handling..
12 #define __KERNEL_SYSCALLS__
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
19 #include <linux/stddef.h>
20 #include <linux/unistd.h>
21 #include <linux/ptrace.h>
22 #include <linux/slab.h>
23 #include <linux/user.h>
24 #include <linux/a.out.h>
25 #include <linux/config.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28 #include <linux/reboot.h>
29 #include <linux/delay.h>
32 #include <asm/auxio.h>
33 #include <asm/oplib.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
37 #include <asm/pgalloc.h>
38 #include <asm/pgtable.h>
39 #include <asm/delay.h>
40 #include <asm/processor.h>
45 * Power management idle function
46 * Set in pm platform drivers
48 void (*pm_idle)(void);
51 * Power-off handler instantiation for pm.h compliance
52 * This is done via auxio, but could be used as a fallback
53 * handler when auxio is not present-- unused for now...
55 void (*pm_power_off)(void);
58 * sysctl - toggle power-off restriction for serial console
59 * systems in machine_power_off()
63 extern void fpsave(unsigned long *, unsigned long *, void *, unsigned long *);
65 struct task_struct *last_task_used_math = NULL;
66 struct task_struct *current_set[NR_CPUS] = {&init_task, };
70 #define SUN4C_FAULT_HIGH 100
73 * the idle loop on a Sparc... ;)
79 if (current->pid != 0)
82 /* endless idle loop with no priority at all */
84 current->counter = -100;
88 if (ARCH_SUN4C_SUN4) {
89 static int count = HZ;
90 static unsigned long last_jiffies;
91 static unsigned long last_faults;
92 static unsigned long fps;
97 extern unsigned long sun4c_kernel_faults;
98 extern void sun4c_grow_kernel_ring(void);
102 count -= (now - last_jiffies);
106 faults = sun4c_kernel_faults;
107 fps = (fps + (faults - last_faults)) >> 1;
108 last_faults = faults;
110 printk("kernel faults / second = %ld\n", fps);
112 if (fps >= SUN4C_FAULT_HIGH) {
113 sun4c_grow_kernel_ring();
116 restore_flags(flags);
119 while((!current->need_resched) && pm_idle) {
133 /* This is being executed in task 0 'user space'. */
136 /* endless idle loop with no priority at all */
138 current->counter = -100;
142 if(current->need_resched) {
146 barrier(); /* or else gcc optimizes... */
152 extern char reboot_command [];
154 extern int serial_console;
156 #ifdef CONFIG_SUN_CONSOLE
157 extern void (*prom_palette)(int);
160 void machine_halt(void)
165 #ifdef CONFIG_SUN_CONSOLE
166 if (!serial_console && prom_palette)
170 panic("Halt failed!");
173 void machine_restart(char * cmd)
181 p = strchr (reboot_command, '\n');
183 #ifdef CONFIG_SUN_CONSOLE
184 if (!serial_console && prom_palette)
190 prom_reboot(reboot_command);
191 prom_feval ("reset");
192 panic("Reboot failed!");
195 void machine_power_off(void)
197 #ifdef CONFIG_SUN_AUXIO
198 if (auxio_power_register && (!serial_console || scons_pwroff))
199 *auxio_power_register |= AUXIO_POWER_OFF;
204 void show_regwindow(struct reg_window *rw)
206 printk("l0: %08lx l1: %08lx l2: %08lx l3: %08lx "
207 "l4: %08lx l5: %08lx l6: %08lx l7: %08lx\n",
208 rw->locals[0], rw->locals[1], rw->locals[2], rw->locals[3],
209 rw->locals[4], rw->locals[5], rw->locals[6], rw->locals[7]);
210 printk("i0: %08lx i1: %08lx i2: %08lx i3: %08lx "
211 "i4: %08lx i5: %08lx fp: %08lx i7: %08lx\n",
212 rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
213 rw->ins[4], rw->ins[5], rw->ins[6], rw->ins[7]);
216 static spinlock_t sparc_backtrace_lock = SPIN_LOCK_UNLOCKED;
218 void __show_backtrace(unsigned long fp)
220 struct reg_window *rw;
222 int cpu = smp_processor_id();
224 spin_lock_irqsave(&sparc_backtrace_lock, flags);
226 rw = (struct reg_window *)fp;
227 while(rw && (((unsigned long) rw) >= PAGE_OFFSET) &&
228 !(((unsigned long) rw) & 0x7)) {
229 printk("CPU[%d]: ARGS[%08lx,%08lx,%08lx,%08lx,%08lx,%08lx] "
230 "FP[%08lx] CALLER[%08lx]\n", cpu,
231 rw->ins[0], rw->ins[1], rw->ins[2], rw->ins[3],
232 rw->ins[4], rw->ins[5],
235 rw = (struct reg_window *) rw->ins[6];
237 spin_unlock_irqrestore(&sparc_backtrace_lock, flags);
240 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
241 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
242 #define __GET_FP(fp) __asm__ __volatile__("mov %%i6, %0" : "=r" (fp))
244 void show_backtrace(void)
248 __SAVE; __SAVE; __SAVE; __SAVE;
249 __SAVE; __SAVE; __SAVE; __SAVE;
250 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
251 __RESTORE; __RESTORE; __RESTORE; __RESTORE;
255 __show_backtrace(fp);
259 void smp_show_backtrace_all_cpus(void)
261 xc0((smpfunc_t) show_backtrace);
266 void show_stackframe(struct sparc_stackf *sf)
272 printk("l0: %08lx l1: %08lx l2: %08lx l3: %08lx "
273 "l4: %08lx l5: %08lx l6: %08lx l7: %08lx\n",
274 sf->locals[0], sf->locals[1], sf->locals[2], sf->locals[3],
275 sf->locals[4], sf->locals[5], sf->locals[6], sf->locals[7]);
276 printk("i0: %08lx i1: %08lx i2: %08lx i3: %08lx "
277 "i4: %08lx i5: %08lx fp: %08lx i7: %08lx\n",
278 sf->ins[0], sf->ins[1], sf->ins[2], sf->ins[3],
279 sf->ins[4], sf->ins[5], (unsigned long)sf->fp, sf->callers_pc);
280 printk("sp: %08lx x0: %08lx x1: %08lx x2: %08lx "
281 "x3: %08lx x4: %08lx x5: %08lx xx: %08lx\n",
282 (unsigned long)sf->structptr, sf->xargs[0], sf->xargs[1],
283 sf->xargs[2], sf->xargs[3], sf->xargs[4], sf->xargs[5],
285 size = ((unsigned long)sf->fp) - ((unsigned long)sf);
286 size -= STACKFRAME_SZ;
287 stk = (unsigned long *)((unsigned long)sf + STACKFRAME_SZ);
290 printk("s%d: %08lx\n", i++, *stk++);
291 } while ((size -= sizeof(unsigned long)));
294 void show_regs(struct pt_regs * regs)
296 printk("PSR: %08lx PC: %08lx NPC: %08lx Y: %08lx %s\n", regs->psr,
297 regs->pc, regs->npc, regs->y, print_tainted());
298 printk("g0: %08lx g1: %08lx g2: %08lx g3: %08lx ",
299 regs->u_regs[0], regs->u_regs[1], regs->u_regs[2],
301 printk("g4: %08lx g5: %08lx g6: %08lx g7: %08lx\n",
302 regs->u_regs[4], regs->u_regs[5], regs->u_regs[6],
304 printk("o0: %08lx o1: %08lx o2: %08lx o3: %08lx ",
305 regs->u_regs[8], regs->u_regs[9], regs->u_regs[10],
307 printk("o4: %08lx o5: %08lx sp: %08lx o7: %08lx\n",
308 regs->u_regs[12], regs->u_regs[13], regs->u_regs[14],
310 show_regwindow((struct reg_window *)regs->u_regs[14]);
313 void show_trace_task(struct task_struct *tsk)
315 unsigned long pc, fp;
316 unsigned long task_base = (unsigned long) tsk;
317 struct reg_window *rw;
323 fp = tsk->thread.ksp;
325 /* Bogus frame pointer? */
326 if (fp < (task_base + sizeof(struct task_struct)) ||
327 fp >= (task_base + (PAGE_SIZE << 1)))
329 rw = (struct reg_window *) fp;
331 printk("[%08lx] ", pc);
333 } while (++count < 16);
338 * Free current thread data structures etc..
340 void exit_thread(void)
343 if(last_task_used_math == current) {
345 if(current->flags & PF_USEDFPU) {
347 /* Keep process from leaving FPU in a bogon state. */
348 put_psr(get_psr() | PSR_EF);
349 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
350 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
352 last_task_used_math = NULL;
354 current->flags &= ~PF_USEDFPU;
359 void flush_thread(void)
361 current->thread.w_saved = 0;
363 /* No new signal delivery by default */
364 current->thread.new_signal = 0;
366 if(last_task_used_math == current) {
368 if(current->flags & PF_USEDFPU) {
371 put_psr(get_psr() | PSR_EF);
372 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
373 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
375 last_task_used_math = NULL;
377 current->flags &= ~PF_USEDFPU;
381 /* Now, this task is no longer a kernel thread. */
382 current->thread.current_ds = USER_DS;
383 if (current->thread.flags & SPARC_FLAG_KTHREAD) {
384 current->thread.flags &= ~SPARC_FLAG_KTHREAD;
386 /* We must fixup kregs as well. */
387 current->thread.kregs = (struct pt_regs *)
388 (((unsigned long)current) +
389 (TASK_UNION_SIZE - TRACEREG_SZ));
393 static __inline__ struct sparc_stackf *
394 clone_stackframe(struct sparc_stackf *dst, struct sparc_stackf *src)
397 struct sparc_stackf *sp;
399 size = ((unsigned long)src->fp) - ((unsigned long)src);
400 sp = (struct sparc_stackf *)(((unsigned long)dst) - size);
402 /* do_fork() grabs the parent semaphore, we must release it
403 * temporarily so we can build the child clone stack frame
404 * without deadlocking.
406 if (copy_to_user(sp, src, size))
407 sp = (struct sparc_stackf *) 0;
408 else if (put_user(dst, &sp->fp))
409 sp = (struct sparc_stackf *) 0;
415 /* Copy a Sparc thread. The fork() return value conventions
416 * under SunOS are nothing short of bletcherous:
417 * Parent --> %o0 == childs pid, %o1 == 0
418 * Child --> %o0 == parents pid, %o1 == 1
420 * NOTE: We have a separate fork kpsr/kwim because
421 * the parent could change these values between
422 * sys_fork invocation and when we reach here
423 * if the parent should sleep while trying to
424 * allocate the task_struct and kernel stack in
427 extern void ret_from_fork(void);
429 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
430 unsigned long unused,
431 struct task_struct *p, struct pt_regs *regs)
433 struct pt_regs *childregs;
437 if(last_task_used_math == current) {
439 if(current->flags & PF_USEDFPU) {
441 put_psr(get_psr() | PSR_EF);
442 fpsave(&p->thread.float_regs[0], &p->thread.fsr,
443 &p->thread.fpqueue[0], &p->thread.fpqdepth);
445 current->flags &= ~PF_USEDFPU;
450 * p new_stack childregs
451 * ! ! ! {if(PSR_PS) }
452 * V V (stk.fr.) V (pt_regs) { (stk.fr.) }
453 * +----- - - - - - ------+===========+============={+==========}+
455 new_stack = (char*)p + TASK_UNION_SIZE;
456 if (regs->psr & PSR_PS)
457 new_stack -= STACKFRAME_SZ;
458 new_stack -= STACKFRAME_SZ + TRACEREG_SZ;
459 memcpy(new_stack, (char *)regs - STACKFRAME_SZ, STACKFRAME_SZ + TRACEREG_SZ);
460 childregs = (struct pt_regs *) (new_stack + STACKFRAME_SZ);
462 p->thread.ksp = (unsigned long) new_stack;
463 p->thread.kpc = (((unsigned long) ret_from_fork) - 0x8);
464 p->thread.kpsr = current->thread.fork_kpsr;
465 p->thread.kwim = current->thread.fork_kwim;
467 /* This is used for sun4c only */
468 atomic_set(&p->thread.refcount, 1);
470 if(regs->psr & PSR_PS) {
471 extern struct pt_regs fake_swapper_regs;
473 p->thread.kregs = &fake_swapper_regs;
474 new_stack += STACKFRAME_SZ + TRACEREG_SZ;
475 childregs->u_regs[UREG_FP] = (unsigned long) new_stack;
476 p->thread.flags |= SPARC_FLAG_KTHREAD;
477 p->thread.current_ds = KERNEL_DS;
478 memcpy(new_stack, (void *)regs->u_regs[UREG_FP], STACKFRAME_SZ);
479 childregs->u_regs[UREG_G6] = (unsigned long) p;
481 p->thread.kregs = childregs;
482 childregs->u_regs[UREG_FP] = sp;
483 p->thread.flags &= ~SPARC_FLAG_KTHREAD;
484 p->thread.current_ds = USER_DS;
486 if (sp != regs->u_regs[UREG_FP]) {
487 struct sparc_stackf *childstack;
488 struct sparc_stackf *parentstack;
491 * This is a clone() call with supplied user stack.
492 * Set some valid stack frames to give to the child.
494 childstack = (struct sparc_stackf *) (sp & ~0x7UL);
495 parentstack = (struct sparc_stackf *) regs->u_regs[UREG_FP];
498 printk("clone: parent stack:\n");
499 show_stackframe(parentstack);
502 childstack = clone_stackframe(childstack, parentstack);
507 printk("clone: child stack:\n");
508 show_stackframe(childstack);
511 childregs->u_regs[UREG_FP] = (unsigned long)childstack;
516 /* FPU must be disabled on SMP. */
517 childregs->psr &= ~PSR_EF;
520 /* Set the return value for the child. */
521 childregs->u_regs[UREG_I0] = current->pid;
522 childregs->u_regs[UREG_I1] = 1;
524 /* Set the return value for the parent. */
525 regs->u_regs[UREG_I1] = 0;
531 * fill in the user structure for a core dump..
533 void dump_thread(struct pt_regs * regs, struct user * dump)
535 unsigned long first_stack_page;
537 dump->magic = SUNOS_CORE_MAGIC;
538 dump->len = sizeof(struct user);
539 dump->regs.psr = regs->psr;
540 dump->regs.pc = regs->pc;
541 dump->regs.npc = regs->npc;
542 dump->regs.y = regs->y;
544 memcpy(&dump->regs.regs[0], ®s->u_regs[1], (sizeof(unsigned long) * 15));
545 dump->uexec = current->thread.core_exec;
546 dump->u_tsize = (((unsigned long) current->mm->end_code) -
547 ((unsigned long) current->mm->start_code)) & ~(PAGE_SIZE - 1);
548 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1)));
549 dump->u_dsize -= dump->u_tsize;
550 dump->u_dsize &= ~(PAGE_SIZE - 1);
551 first_stack_page = (regs->u_regs[UREG_FP] & ~(PAGE_SIZE - 1));
552 dump->u_ssize = (TASK_SIZE - first_stack_page) & ~(PAGE_SIZE - 1);
553 memcpy(&dump->fpu.fpstatus.fregs.regs[0], ¤t->thread.float_regs[0], (sizeof(unsigned long) * 32));
554 dump->fpu.fpstatus.fsr = current->thread.fsr;
555 dump->fpu.fpstatus.flags = dump->fpu.fpstatus.extra = 0;
556 dump->fpu.fpstatus.fpq_count = current->thread.fpqdepth;
557 memcpy(&dump->fpu.fpstatus.fpq[0], ¤t->thread.fpqueue[0],
558 ((sizeof(unsigned long) * 2) * 16));
563 * fill in the fpu structure for a core dump.
565 int dump_fpu (struct pt_regs * regs, elf_fpregset_t * fpregs)
567 if (current->used_math == 0) {
568 memset(fpregs, 0, sizeof(*fpregs));
569 fpregs->pr_q_entrysize = 8;
573 if (current->flags & PF_USEDFPU) {
574 put_psr(get_psr() | PSR_EF);
575 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
576 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
577 regs->psr &= ~(PSR_EF);
578 current->flags &= ~(PF_USEDFPU);
581 if (current == last_task_used_math) {
582 put_psr(get_psr() | PSR_EF);
583 fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
584 ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
585 last_task_used_math = 0;
586 regs->psr &= ~(PSR_EF);
589 memcpy(&fpregs->pr_fr.pr_regs[0],
590 ¤t->thread.float_regs[0],
591 (sizeof(unsigned long) * 32));
592 fpregs->pr_fsr = current->thread.fsr;
593 fpregs->pr_qcnt = current->thread.fpqdepth;
594 fpregs->pr_q_entrysize = 8;
596 if(fpregs->pr_qcnt != 0) {
597 memcpy(&fpregs->pr_q[0],
598 ¤t->thread.fpqueue[0],
599 sizeof(struct fpq) * fpregs->pr_qcnt);
601 /* Zero out the rest. */
602 memset(&fpregs->pr_q[fpregs->pr_qcnt], 0,
603 sizeof(struct fpq) * (32 - fpregs->pr_qcnt));
608 * sparc_execve() executes a new program after the asm stub has set
609 * things up for us. This should basically do what I want it to.
611 asmlinkage int sparc_execve(struct pt_regs *regs)
616 /* Check for indirect call. */
617 if(regs->u_regs[UREG_G1] == 0)
620 filename = getname((char *)regs->u_regs[base + UREG_I0]);
621 error = PTR_ERR(filename);
624 error = do_execve(filename, (char **) regs->u_regs[base + UREG_I1],
625 (char **) regs->u_regs[base + UREG_I2], regs);
632 * This is the mechanism for creating a new kernel thread.
634 * NOTE! Only a kernel-only process(ie the swapper or direct descendants
635 * who haven't done an "execve()") should use this: it will work within
636 * a system call from a "real" process, but the process memory space will
637 * not be free'd until both the parent and the child have exited.
639 pid_t arch_kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
643 __asm__ __volatile("mov %4, %%g2\n\t" /* Set aside fn ptr... */
644 "mov %5, %%g3\n\t" /* and arg. */
646 "mov %2, %%o0\n\t" /* Clone flags. */
647 "mov 0, %%o1\n\t" /* usp arg == 0 */
648 "t 0x10\n\t" /* Linux/Sparc clone(). */
650 "be 1f\n\t" /* The parent, just return. */
651 " nop\n\t" /* Delay slot. */
652 "jmpl %%g2, %%o7\n\t" /* Call the function. */
653 " mov %%g3, %%o0\n\t" /* Get back the arg in delay. */
655 "t 0x10\n\t" /* Linux/Sparc exit(). */
656 /* Notreached by child. */
657 "1: mov %%o0, %0\n\t" :
659 "i" (__NR_clone), "r" (flags | CLONE_VM),
660 "i" (__NR_exit), "r" (fn), "r" (arg) :
661 "g1", "g2", "g3", "o0", "o1", "memory", "cc");