pre-2.3.4..
[davej-history.git] / arch / ppc / kernel / process.c
blob0147d427de7f771e384bff91eb719dd616387910
1 /*
2 * $Id: process.c,v 1.85 1999/05/16 21:27:08 cort Exp $
4 * linux/arch/ppc/kernel/process.c
6 * Derived from "arch/i386/kernel/process.c"
7 * Copyright (C) 1995 Linus Torvalds
9 * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
10 * Paul Mackerras (paulus@cs.anu.edu.au)
12 * PowerPC version
13 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
22 #include <linux/errno.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28 #include <linux/stddef.h>
29 #include <linux/unistd.h>
30 #include <linux/ptrace.h>
31 #include <linux/malloc.h>
32 #include <linux/user.h>
33 #include <linux/elf.h>
34 #include <linux/elf.h>
35 #include <linux/init.h>
37 #include <asm/pgtable.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/processor.h>
42 #include <asm/mmu.h>
43 #include <asm/prom.h>
45 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs);
46 extern unsigned long _get_SP(void);
48 struct task_struct *last_task_used_math = NULL;
49 static struct vm_area_struct init_mmap = INIT_MMAP;
50 static struct fs_struct init_fs = INIT_FS;
51 static struct file * init_fd_array[NR_OPEN] = { NULL, };
52 static struct files_struct init_files = INIT_FILES;
53 static struct signal_struct init_signals = INIT_SIGNALS;
54 struct mm_struct init_mm = INIT_MM(init_mm);
55 union task_union init_task_union = { INIT_TASK(init_task_union.task) };
56 /* only used to get secondary processor up */
57 struct task_struct *current_set[NR_CPUS] = {&init_task, };
59 #undef SHOW_TASK_SWITCHES 1
60 #undef CHECK_STACK 1
62 unsigned long
63 kernel_stack_top(struct task_struct *tsk)
65 return ((unsigned long)tsk) + sizeof(union task_union);
68 unsigned long
69 task_top(struct task_struct *tsk)
71 return ((unsigned long)tsk) + sizeof(struct task_struct);
74 int
75 dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
77 if (regs->msr & MSR_FP)
78 giveup_fpu(current);
79 memcpy(fpregs, &current->tss.fpr[0], sizeof(*fpregs));
80 return 1;
83 void
84 enable_kernel_fp(void)
86 #ifdef __SMP__
87 if (current->tss.regs && (current->tss.regs->msr & MSR_FP))
88 giveup_fpu(current);
89 else
90 giveup_fpu(NULL); /* just enables FP for kernel */
91 #else
92 giveup_fpu(last_task_used_math);
93 #endif /* __SMP__ */
96 /* check to make sure the kernel stack is healthy */
97 int check_stack(struct task_struct *tsk)
99 unsigned long stack_top = kernel_stack_top(tsk);
100 unsigned long tsk_top = task_top(tsk);
101 int ret = 0;
103 #if 0
104 /* check tss magic */
105 if ( tsk->tss.magic != TSS_MAGIC )
107 ret |= 1;
108 printk("tss.magic bad: %08x\n", tsk->tss.magic);
110 #endif
112 if ( !tsk )
113 printk("check_stack(): tsk bad tsk %p\n",tsk);
115 /* check if stored ksp is bad */
116 if ( (tsk->tss.ksp > stack_top) || (tsk->tss.ksp < tsk_top) )
118 printk("stack out of bounds: %s/%d\n"
119 " tsk_top %08lx ksp %08lx stack_top %08lx\n",
120 tsk->comm,tsk->pid,
121 tsk_top, tsk->tss.ksp, stack_top);
122 ret |= 2;
125 /* check if stack ptr RIGHT NOW is bad */
126 if ( (tsk == current) && ((_get_SP() > stack_top ) || (_get_SP() < tsk_top)) )
128 printk("current stack ptr out of bounds: %s/%d\n"
129 " tsk_top %08lx sp %08lx stack_top %08lx\n",
130 current->comm,current->pid,
131 tsk_top, _get_SP(), stack_top);
132 ret |= 4;
135 #if 0
136 /* check amount of free stack */
137 for ( i = (unsigned long *)task_top(tsk) ; i < kernel_stack_top(tsk) ; i++ )
139 if ( !i )
140 printk("check_stack(): i = %p\n", i);
141 if ( *i != 0 )
143 /* only notify if it's less than 900 bytes */
144 if ( (i - (unsigned long *)task_top(tsk)) < 900 )
145 printk("%d bytes free on stack\n",
146 i - task_top(tsk));
147 break;
150 #endif
152 if (ret)
154 panic("bad kernel stack");
156 return(ret);
159 void
160 _switch_to(struct task_struct *prev, struct task_struct *new,
161 struct task_struct **last)
163 struct thread_struct *new_tss, *old_tss;
164 int s = _disable_interrupts();
165 #if CHECK_STACK
166 check_stack(prev);
167 check_stack(new);
168 #endif
170 #ifdef SHOW_TASK_SWITCHES
171 printk("%s/%d -> %s/%d NIP %08lx cpu %d root %x/%x\n",
172 prev->comm,prev->pid,
173 new->comm,new->pid,new->tss.regs->nip,new->processor,
174 new->fs->root,prev->fs->root);
175 #endif
176 #ifdef __SMP__
177 /* avoid complexity of lazy save/restore of fpu
178 * by just saving it every time we switch out if
179 * this task used the fpu during the last quantum.
181 * If it tries to use the fpu again, it'll trap and
182 * reload its fp regs. So we don't have to do a restore
183 * every switch, just a save.
184 * -- Cort
186 if (prev->tss.regs && (prev->tss.regs->msr & MSR_FP))
187 giveup_fpu(prev);
189 prev->last_processor = prev->processor;
190 current_set[smp_processor_id()] = new;
191 #endif /* __SMP__ */
192 new_tss = &new->tss;
193 old_tss = &current->tss;
194 *last = _switch(old_tss, new_tss, new->mm->context);
195 _enable_interrupts(s);
198 void show_regs(struct pt_regs * regs)
200 int i;
202 printk("NIP: %08lX XER: %08lX LR: %08lX REGS: %p TRAP: %04lx\n",
203 regs->nip, regs->xer, regs->link, regs,regs->trap);
204 printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
205 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
206 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
207 regs->msr&MSR_IR ? 1 : 0,
208 regs->msr&MSR_DR ? 1 : 0);
209 printk("TASK = %p[%d] '%s' mm->pgd %p ",
210 current, current->pid, current->comm, current->mm->pgd);
211 printk("Last syscall: %ld ", current->tss.last_syscall);
212 printk("\nlast math %p", last_task_used_math);
214 #ifdef __SMP__
215 printk(" CPU: %d last CPU: %d", current->processor,current->last_processor);
216 #endif /* __SMP__ */
218 printk("\n");
219 for (i = 0; i < 32; i++)
221 long r;
222 if ((i % 8) == 0)
224 printk("GPR%02d: ", i);
227 if ( __get_user(r, &(regs->gpr[i])) )
228 goto out;
229 printk("%08lX ", r);
230 if ((i % 8) == 7)
232 printk("\n");
235 out:
238 void exit_thread(void)
240 if (last_task_used_math == current)
241 last_task_used_math = NULL;
244 void flush_thread(void)
246 if (last_task_used_math == current)
247 last_task_used_math = NULL;
250 void
251 release_thread(struct task_struct *t)
256 * Copy a thread..
259 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
260 struct task_struct * p, struct pt_regs * regs)
262 struct pt_regs * childregs, *kregs;
263 #ifdef __SMP__
264 extern void ret_from_smpfork(void);
265 #else
266 extern void ret_from_syscall(void);
267 #endif
268 /* Copy registers */
269 childregs = ((struct pt_regs *)
270 ((unsigned long)p + sizeof(union task_union)
271 - STACK_FRAME_OVERHEAD)) - 2;
272 *childregs = *regs;
273 if ((childregs->msr & MSR_PR) == 0)
274 childregs->gpr[2] = (unsigned long) p; /* `current' in new task */
275 childregs->gpr[3] = 0; /* Result from fork() */
276 p->tss.regs = childregs;
277 p->tss.ksp = (unsigned long) childregs - STACK_FRAME_OVERHEAD;
278 p->tss.ksp -= sizeof(struct pt_regs ) + STACK_FRAME_OVERHEAD;
279 kregs = (struct pt_regs *)(p->tss.ksp + STACK_FRAME_OVERHEAD);
280 #ifdef __SMP__
281 kregs->nip = (unsigned long)ret_from_smpfork;
282 #else
283 kregs->nip = (unsigned long)ret_from_syscall;
284 #endif
285 kregs->msr = MSR_KERNEL;
286 kregs->gpr[1] = (unsigned long)childregs - STACK_FRAME_OVERHEAD;
287 kregs->gpr[2] = (unsigned long)p;
289 if (usp >= (unsigned long) regs) {
290 /* Stack is in kernel space - must adjust */
291 childregs->gpr[1] = (unsigned long)(childregs + 1);
292 } else {
293 /* Provided stack is in user space */
294 childregs->gpr[1] = usp;
296 p->tss.last_syscall = -1;
299 * copy fpu info - assume lazy fpu switch now always
300 * -- Cort
302 if (regs->msr & MSR_FP)
303 giveup_fpu(current);
305 memcpy(&p->tss.fpr, &current->tss.fpr, sizeof(p->tss.fpr));
306 p->tss.fpscr = current->tss.fpscr;
307 childregs->msr &= ~MSR_FP;
309 #ifdef __SMP__
310 p->last_processor = NO_PROC_ID;
311 #endif /* __SMP__ */
312 return 0;
316 * XXX ld.so expects the auxiliary table to start on
317 * a 16-byte boundary, so we have to find it and
318 * move it up. :-(
320 static inline void shove_aux_table(unsigned long sp)
322 int argc;
323 char *p;
324 unsigned long e;
325 unsigned long aux_start, offset;
327 if (__get_user(argc, (int *)sp))
328 return;
329 sp += sizeof(int) + (argc + 1) * sizeof(char *);
330 /* skip over the environment pointers */
331 do {
332 if (__get_user(p, (char **)sp))
333 return;
334 sp += sizeof(char *);
335 } while (p != NULL);
336 aux_start = sp;
337 /* skip to the end of the auxiliary table */
338 do {
339 if (__get_user(e, (unsigned long *)sp))
340 return;
341 sp += 2 * sizeof(unsigned long);
342 } while (e != AT_NULL);
343 offset = ((aux_start + 15) & ~15) - aux_start;
344 if (offset != 0) {
345 do {
346 sp -= sizeof(unsigned long);
347 if (__get_user(e, (unsigned long *)sp)
348 || __put_user(e, (unsigned long *)(sp + offset)))
349 return;
350 } while (sp > aux_start);
355 * Set up a thread for executing a new program
357 void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
359 set_fs(USER_DS);
360 regs->nip = nip;
361 regs->gpr[1] = sp;
362 regs->msr = MSR_USER;
363 shove_aux_table(sp);
364 if (last_task_used_math == current)
365 last_task_used_math = 0;
366 current->tss.fpscr = 0;
369 asmlinkage int sys_clone(int p1, int p2, int p3, int p4, int p5, int p6,
370 struct pt_regs *regs)
372 unsigned long clone_flags = p1;
373 int res;
374 lock_kernel();
375 res = do_fork(clone_flags, regs->gpr[1], regs);
376 #ifdef __SMP__
377 /* When we clone the idle task we keep the same pid but
378 * the return value of 0 for both causes problems.
379 * -- Cort
381 if ((current->pid == 0) && (current == &init_task))
382 res = 1;
383 #endif /* __SMP__ */
384 unlock_kernel();
385 return res;
388 asmlinkage int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
389 struct pt_regs *regs)
392 int res;
394 res = do_fork(SIGCHLD, regs->gpr[1], regs);
395 #ifdef __SMP__
396 /* When we clone the idle task we keep the same pid but
397 * the return value of 0 for both causes problems.
398 * -- Cort
400 if ((current->pid == 0) && (current == &init_task))
401 res = 1;
402 #endif /* __SMP__ */
403 return res;
406 asmlinkage int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
407 struct pt_regs *regs)
409 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs);
412 asmlinkage int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
413 unsigned long a3, unsigned long a4, unsigned long a5,
414 struct pt_regs *regs)
416 int error;
417 char * filename;
418 lock_kernel();
419 filename = getname((char *) a0);
420 error = PTR_ERR(filename);
421 if (IS_ERR(filename))
422 goto out;
423 if (regs->msr & MSR_FP)
424 giveup_fpu(current);
425 error = do_execve(filename, (char **) a1, (char **) a2, regs);
426 putname(filename);
427 out:
428 unlock_kernel();
430 return error;
433 void
434 print_backtrace(unsigned long *sp)
436 int cnt = 0;
437 unsigned long i;
439 printk("Call backtrace: ");
440 while (sp) {
441 if (__get_user( i, &sp[1] ))
442 break;
443 if (cnt++ % 7 == 0)
444 printk("\n");
445 printk("%08lX ", i);
446 if (cnt > 32) break;
447 if (__get_user(sp, (unsigned long **)sp))
448 break;
450 printk("\n");
453 #if 0
455 * Low level print for debugging - Cort
457 __initfunc(int ll_printk(const char *fmt, ...))
459 va_list args;
460 char buf[256];
461 int i;
463 va_start(args, fmt);
464 i=vsprintf(buf,fmt,args);
465 ll_puts(buf);
466 va_end(args);
467 return i;
470 int lines = 24, cols = 80;
471 int orig_x = 0, orig_y = 0;
473 void puthex(unsigned long val)
475 unsigned char buf[10];
476 int i;
477 for (i = 7; i >= 0; i--)
479 buf[i] = "0123456789ABCDEF"[val & 0x0F];
480 val >>= 4;
482 buf[8] = '\0';
483 prom_print(buf);
486 __initfunc(void ll_puts(const char *s))
488 int x,y;
489 char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
490 char c;
491 extern int mem_init_done;
493 if ( mem_init_done ) /* assume this means we can printk */
495 printk(s);
496 return;
499 #if 0
500 if ( have_of )
502 prom_print(s);
503 return;
505 #endif
508 * can't ll_puts on chrp without openfirmware yet.
509 * vidmem just needs to be setup for it.
510 * -- Cort
512 if ( ! is_prep )
513 return;
514 x = orig_x;
515 y = orig_y;
517 while ( ( c = *s++ ) != '\0' ) {
518 if ( c == '\n' ) {
519 x = 0;
520 if ( ++y >= lines ) {
521 /*scroll();*/
522 /*y--;*/
523 y = 0;
525 } else {
526 vidmem [ ( x + cols * y ) * 2 ] = c;
527 if ( ++x >= cols ) {
528 x = 0;
529 if ( ++y >= lines ) {
530 /*scroll();*/
531 /*y--;*/
532 y = 0;
538 orig_x = x;
539 orig_y = y;
541 #endif