1 /*
2 * linux/arch/ppc/kernel/process.c
3 *
4 * Derived from "arch/i386/kernel/process.c"
5 * Copyright (C) 1995 Linus Torvalds
6 *
7 * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
8 * Paul Mackerras (paulus@cs.anu.edu.au)
9 *
10 * PowerPC version
11 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 */
19
20 #include <linux/config.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/stddef.h>
28 #include <linux/unistd.h>
29 #include <linux/ptrace.h>
30 #include <linux/slab.h>
31 #include <linux/user.h>
32 #include <linux/elf.h>
33 #include <linux/init.h>
34 #include <linux/prctl.h>
35
36 #include <asm/pgtable.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <asm/io.h>
40 #include <asm/processor.h>
41 #include <asm/mmu.h>
42 #include <asm/prom.h>
43
44 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs);
45 extern unsigned long _get_SP(void);
46
47 struct task_struct *last_task_used_math = NULL;
48 struct task_struct *last_task_used_altivec = NULL;
49 static struct fs_struct init_fs = INIT_FS;
50 static struct files_struct init_files = INIT_FILES;
51 static struct signal_struct init_signals = INIT_SIGNALS;
52 struct mm_struct init_mm = INIT_MM(init_mm);
53 /* this is 16-byte aligned because it has a stack in it */
54 union task_union __attribute((aligned(16))) init_task_union = {
55 INIT_TASK(init_task_union.task)
56 };
57 /* only used to get secondary processor up */
58 struct task_struct *current_set[NR_CPUS] = {&init_task, };
59
60 #undef SHOW_TASK_SWITCHES
61 #undef CHECK_STACK
62
63 #if defined(CHECK_STACK)
64 unsigned long
kernel_stack_top(struct task_struct * tsk)65 kernel_stack_top(struct task_struct *tsk)
66 {
67 return ((unsigned long)tsk) + sizeof(union task_union);
68 }
69
70 unsigned long
task_top(struct task_struct * tsk)71 task_top(struct task_struct *tsk)
72 {
73 return ((unsigned long)tsk) + sizeof(struct task_struct);
74 }
75
76 /* check to make sure the kernel stack is healthy */
check_stack(struct task_struct * tsk)77 int check_stack(struct task_struct *tsk)
78 {
79 unsigned long stack_top = kernel_stack_top(tsk);
80 unsigned long tsk_top = task_top(tsk);
81 int ret = 0;
82
83 #if 0
84 /* check thread magic */
85 if ( tsk->thread.magic != THREAD_MAGIC )
86 {
87 ret |= 1;
88 printk("thread.magic bad: %08x\n", tsk->thread.magic);
89 }
90 #endif
91
92 if ( !tsk )
93 printk("check_stack(): tsk bad tsk %p\n",tsk);
94
95 /* check if stored ksp is bad */
96 if ( (tsk->thread.ksp > stack_top) || (tsk->thread.ksp < tsk_top) )
97 {
98 printk("stack out of bounds: %s/%d\n"
99 " tsk_top %08lx ksp %08lx stack_top %08lx\n",
100 tsk->comm,tsk->pid,
101 tsk_top, tsk->thread.ksp, stack_top);
102 ret |= 2;
103 }
104
105 /* check if stack ptr RIGHT NOW is bad */
106 if ( (tsk == current) && ((_get_SP() > stack_top ) || (_get_SP() < tsk_top)) )
107 {
108 printk("current stack ptr out of bounds: %s/%d\n"
109 " tsk_top %08lx sp %08lx stack_top %08lx\n",
110 current->comm,current->pid,
111 tsk_top, _get_SP(), stack_top);
112 ret |= 4;
113 }
114
115 #if 0
116 /* check amount of free stack */
117 for ( i = (unsigned long *)task_top(tsk) ; i < kernel_stack_top(tsk) ; i++ )
118 {
119 if ( !i )
120 printk("check_stack(): i = %p\n", i);
121 if ( *i != 0 )
122 {
123 /* only notify if it's less than 900 bytes */
124 if ( (i - (unsigned long *)task_top(tsk)) < 900 )
125 printk("%d bytes free on stack\n",
126 i - task_top(tsk));
127 break;
128 }
129 }
130 #endif
131
132 if (ret)
133 {
134 panic("bad kernel stack");
135 }
136 return(ret);
137 }
138 #endif /* defined(CHECK_STACK) */
139
140 #ifdef CONFIG_ALTIVEC
141 int
dump_altivec(struct pt_regs * regs,elf_vrregset_t * vrregs)142 dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
143 {
144 if (regs->msr & MSR_VEC)
145 giveup_altivec(current);
146 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
147 return 1;
148 }
149
150 void
enable_kernel_altivec(void)151 enable_kernel_altivec(void)
152 {
153 #ifdef CONFIG_SMP
154 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
155 giveup_altivec(current);
156 else
157 giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
158 #else
159 giveup_altivec(last_task_used_altivec);
160 #endif /* __SMP __ */
161 }
162 #endif /* CONFIG_ALTIVEC */
163
164 void
enable_kernel_fp(void)165 enable_kernel_fp(void)
166 {
167 #ifdef CONFIG_SMP
168 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
169 giveup_fpu(current);
170 else
171 giveup_fpu(NULL); /* just enables FP for kernel */
172 #else
173 giveup_fpu(last_task_used_math);
174 #endif /* CONFIG_SMP */
175 }
176
177 int
dump_fpu(struct pt_regs * regs,elf_fpregset_t * fpregs)178 dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
179 {
180 if (regs->msr & MSR_FP)
181 giveup_fpu(current);
182 memcpy(fpregs, ¤t->thread.fpr[0], sizeof(*fpregs));
183 return 1;
184 }
185
186 void
_switch_to(struct task_struct * prev,struct task_struct * new,struct task_struct ** last)187 _switch_to(struct task_struct *prev, struct task_struct *new,
188 struct task_struct **last)
189 {
190 struct thread_struct *new_thread, *old_thread;
191 unsigned long s;
192
193 __save_flags(s);
194 __cli();
195 #if CHECK_STACK
196 check_stack(prev);
197 check_stack(new);
198 #endif
199
200 #ifdef CONFIG_SMP
201 /* avoid complexity of lazy save/restore of fpu
202 * by just saving it every time we switch out if
203 * this task used the fpu during the last quantum.
204 *
205 * If it tries to use the fpu again, it'll trap and
206 * reload its fp regs. So we don't have to do a restore
207 * every switch, just a save.
208 * -- Cort
209 */
210 if ( prev->thread.regs && (prev->thread.regs->msr & MSR_FP) )
211 giveup_fpu(prev);
212 #ifdef CONFIG_ALTIVEC
213 /*
214 * If the previous thread used altivec in the last quantum
215 * (thus changing altivec regs) then save them.
216 * We used to check the VRSAVE register but not all apps
217 * set it, so we don't rely on it now (and in fact we need
218 * to save & restore VSCR even if VRSAVE == 0). -- paulus
219 *
220 * On SMP we always save/restore altivec regs just to avoid the
221 * complexity of changing processors.
222 * -- Cort
223 */
224 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
225 giveup_altivec(prev);
226 #endif /* CONFIG_ALTIVEC */
227 #endif /* CONFIG_SMP */
228
229 current_set[smp_processor_id()] = new;
230
231 /* Avoid the trap. On smp this this never happens since
232 * we don't set last_task_used_altivec -- Cort
233 */
234 if (new->thread.regs && last_task_used_altivec == new)
235 new->thread.regs->msr |= MSR_VEC;
236 new_thread = &new->thread;
237 old_thread = ¤t->thread;
238 *last = _switch(old_thread, new_thread);
239 __restore_flags(s);
240 }
241
show_regs(struct pt_regs * regs)242 void show_regs(struct pt_regs * regs)
243 {
244 int i;
245
246 printk("NIP: %08lX XER: %08lX LR: %08lX SP: %08lX REGS: %p TRAP: %04lx %s\n",
247 regs->nip, regs->xer, regs->link, regs->gpr[1], regs,regs->trap, print_tainted());
248 printk("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
249 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
250 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
251 regs->msr&MSR_IR ? 1 : 0,
252 regs->msr&MSR_DR ? 1 : 0);
253 #ifdef CONFIG_4xx
254 /*
255 * TRAP 0x800 is the hijacked FPU unavailable exception vector
256 * on 40x used to implement the heavyweight data access
257 * functionality. It is an emulated value (like all trap
258 * vectors) on 440.
259 */
260 if (regs->trap == 0x300 || regs->trap == 0x600 || regs->trap == 0x800)
261 printk("DEAR: %08lX, ESR: %08lX\n", regs->dar, regs->dsisr);
262 #else
263 if (regs->trap == 0x300 || regs->trap == 0x600)
264 printk("DAR: %08lX, DSISR: %08lX\n", regs->dar, regs->dsisr);
265 #endif
266 printk("TASK = %p[%d] '%s' ",
267 current, current->pid, current->comm);
268 printk("Last syscall: %ld ", current->thread.last_syscall);
269 printk("\nlast math %p last altivec %p", last_task_used_math,
270 last_task_used_altivec);
271
272 #if defined(CONFIG_4xx) && defined(DCRN_PLB0_BEAR)
273 printk("\nPLB0: bear= 0x%8.8x acr= 0x%8.8x besr= 0x%8.8x\n",
274 mfdcr(DCRN_PLB0_BEAR), mfdcr(DCRN_PLB0_ACR),
275 mfdcr(DCRN_PLB0_BESR));
276 #endif
277 #if defined(CONFIG_4xx) && defined(DCRN_POB0_BEAR)
278 printk("PLB0 to OPB: bear= 0x%8.8x besr0= 0x%8.8x besr1= 0x%8.8x\n",
279 mfdcr(DCRN_POB0_BEAR), mfdcr(DCRN_POB0_BESR0),
280 mfdcr(DCRN_POB0_BESR1));
281 #endif
282
283 #ifdef CONFIG_SMP
284 printk(" CPU: %d", current->processor);
285 #endif /* CONFIG_SMP */
286
287 printk("\n");
288 for (i = 0; i < 32; i++)
289 {
290 long r;
291 if ((i % 8) == 0)
292 {
293 printk("GPR%02d: ", i);
294 }
295
296 if ( __get_user(r, &(regs->gpr[i])) )
297 goto out;
298 printk("%08lX ", r);
299 if ((i % 8) == 7)
300 {
301 printk("\n");
302 }
303 }
304 out:
305 print_backtrace((unsigned long *)regs->gpr[1]);
306 }
307
exit_thread(void)308 void exit_thread(void)
309 {
310 if (last_task_used_math == current)
311 last_task_used_math = NULL;
312 if (last_task_used_altivec == current)
313 last_task_used_altivec = NULL;
314 }
315
flush_thread(void)316 void flush_thread(void)
317 {
318 if (last_task_used_math == current)
319 last_task_used_math = NULL;
320 if (last_task_used_altivec == current)
321 last_task_used_altivec = NULL;
322 }
323
324 void
release_thread(struct task_struct * t)325 release_thread(struct task_struct *t)
326 {
327 }
328
329 /*
330 * Copy a thread..
331 */
332 int
copy_thread(int nr,unsigned long clone_flags,unsigned long usp,unsigned long unused,struct task_struct * p,struct pt_regs * regs)333 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
334 unsigned long unused,
335 struct task_struct *p, struct pt_regs *regs)
336 {
337 struct pt_regs *childregs, *kregs;
338 extern void ret_from_fork(void);
339 unsigned long sp = (unsigned long)p + sizeof(union task_union);
340 unsigned long childframe;
341
342 /* Copy registers */
343 sp -= sizeof(struct pt_regs);
344 childregs = (struct pt_regs *) sp;
345 *childregs = *regs;
346 if ((childregs->msr & MSR_PR) == 0) {
347 /* for kernel thread, set `current' and stackptr in new task */
348 childregs->gpr[1] = sp + sizeof(struct pt_regs);
349 childregs->gpr[2] = (unsigned long) p;
350 p->thread.regs = NULL; /* no user register state */
351 } else
352 p->thread.regs = childregs;
353 childregs->gpr[3] = 0; /* Result from fork() */
354 sp -= STACK_FRAME_OVERHEAD;
355 childframe = sp;
356
357 /*
358 * The way this works is that at some point in the future
359 * some task will call _switch to switch to the new task.
360 * That will pop off the stack frame created below and start
361 * the new task running at ret_from_fork. The new task will
362 * do some house keeping and then return from the fork or clone
363 * system call, using the stack frame created above.
364 */
365 sp -= sizeof(struct pt_regs);
366 kregs = (struct pt_regs *) sp;
367 sp -= STACK_FRAME_OVERHEAD;
368 p->thread.ksp = sp;
369 kregs->nip = (unsigned long)ret_from_fork;
370
371 /*
372 * copy fpu info - assume lazy fpu switch now always
373 * -- Cort
374 */
375 if (regs->msr & MSR_FP) {
376 giveup_fpu(current);
377 childregs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
378 }
379 memcpy(&p->thread.fpr, ¤t->thread.fpr, sizeof(p->thread.fpr));
380 p->thread.fpscr = current->thread.fpscr;
381
382 #ifdef CONFIG_ALTIVEC
383 /*
384 * copy altiVec info - assume lazy altiVec switch
385 * - kumar
386 */
387 if (regs->msr & MSR_VEC)
388 giveup_altivec(current);
389 memcpy(&p->thread.vr, ¤t->thread.vr, sizeof(p->thread.vr));
390 p->thread.vscr = current->thread.vscr;
391 childregs->msr &= ~MSR_VEC;
392 #endif /* CONFIG_ALTIVEC */
393
394 p->thread.last_syscall = -1;
395
396 return 0;
397 }
398
399 /*
400 * Set up a thread for executing a new program
401 */
start_thread(struct pt_regs * regs,unsigned long nip,unsigned long sp)402 void start_thread(struct pt_regs *regs, unsigned long nip, unsigned long sp)
403 {
404 set_fs(USER_DS);
405 memset(regs->gpr, 0, sizeof(regs->gpr));
406 memset(®s->ctr, 0, 5 * sizeof(regs->ctr));
407 regs->nip = nip;
408 regs->gpr[1] = sp;
409 regs->msr = MSR_USER;
410 if (last_task_used_math == current)
411 last_task_used_math = 0;
412 if (last_task_used_altivec == current)
413 last_task_used_altivec = 0;
414 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
415 current->thread.fpscr = 0;
416 #ifdef CONFIG_ALTIVEC
417 memset(current->thread.vr, 0, sizeof(current->thread.vr));
418 memset(¤t->thread.vscr, 0, sizeof(current->thread.vscr));
419 current->thread.vrsave = 0;
420 current->thread.used_vr = 0;
421 #endif /* CONFIG_ALTIVEC */
422 }
423
424 /*
425 * Support for the PR_GET/SET_FPEXC prctl() calls.
426 */
__unpack_fe01(unsigned int msr_bits)427 static inline unsigned int __unpack_fe01(unsigned int msr_bits)
428 {
429 return ((msr_bits & MSR_FE0) >> 10) | ((msr_bits & MSR_FE1) >> 8);
430 }
431
__pack_fe01(unsigned int fpmode)432 static inline unsigned int __pack_fe01(unsigned int fpmode)
433 {
434 return ((fpmode << 10) & MSR_FE0) | ((fpmode << 8) & MSR_FE1);
435 }
436
set_fpexc_mode(struct task_struct * tsk,unsigned int val)437 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
438 {
439 struct pt_regs *regs = tsk->thread.regs;
440
441 if (val > PR_FP_EXC_PRECISE)
442 return -EINVAL;
443 tsk->thread.fpexc_mode = __pack_fe01(val);
444 if (regs != NULL && (regs->msr & MSR_FP) != 0)
445 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
446 | tsk->thread.fpexc_mode;
447 return 0;
448 }
449
get_fpexc_mode(struct task_struct * tsk,unsigned long adr)450 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
451 {
452 unsigned int val;
453
454 val = __unpack_fe01(tsk->thread.fpexc_mode);
455 return put_user(val, (unsigned int *) adr);
456 }
457
sys_clone(int p1,int p2,int p3,int p4,int p5,int p6,struct pt_regs * regs)458 int sys_clone(int p1, int p2, int p3, int p4, int p5, int p6,
459 struct pt_regs *regs)
460 {
461 return do_fork(p1, p2, regs, 0);
462 }
463
sys_fork(int p1,int p2,int p3,int p4,int p5,int p6,struct pt_regs * regs)464 int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
465 struct pt_regs *regs)
466 {
467 return do_fork(SIGCHLD, regs->gpr[1], regs, 0);
468 }
469
sys_vfork(int p1,int p2,int p3,int p4,int p5,int p6,struct pt_regs * regs)470 int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
471 struct pt_regs *regs)
472 {
473 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0);
474 }
475
sys_execve(unsigned long a0,unsigned long a1,unsigned long a2,unsigned long a3,unsigned long a4,unsigned long a5,struct pt_regs * regs)476 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
477 unsigned long a3, unsigned long a4, unsigned long a5,
478 struct pt_regs *regs)
479 {
480 int error;
481 char * filename;
482
483 filename = getname((char *) a0);
484 error = PTR_ERR(filename);
485 if (IS_ERR(filename))
486 goto out;
487 if (regs->msr & MSR_FP)
488 giveup_fpu(current);
489 #ifdef CONFIG_ALTIVEC
490 if (regs->msr & MSR_VEC)
491 giveup_altivec(current);
492 #endif /* CONFIG_ALTIVEC */
493 error = do_execve(filename, (char **) a1, (char **) a2, regs);
494 if (error == 0)
495 current->ptrace &= ~PT_DTRACE;
496 putname(filename);
497 out:
498 return error;
499 }
500
501 void
print_backtrace(unsigned long * sp)502 print_backtrace(unsigned long *sp)
503 {
504 int cnt = 0;
505 unsigned long i;
506
507 if (sp == NULL)
508 asm("mr %0,1" : "=r" (sp));
509 printk("Call backtrace: ");
510 while (sp) {
511 if (__get_user( i, &sp[1] ))
512 break;
513 if (cnt++ % 7 == 0)
514 printk("\n");
515 printk("%08lX ", i);
516 if (cnt > 32) break;
517 if (__get_user(sp, (unsigned long **)sp))
518 break;
519 }
520 printk("\n");
521 }
522
show_trace_task(struct task_struct * tsk)523 void show_trace_task(struct task_struct *tsk)
524 {
525 unsigned long stack_top = (unsigned long) tsk + THREAD_SIZE;
526 unsigned long sp, prev_sp;
527 int count = 0;
528
529 if (tsk == NULL)
530 return;
531 sp = (unsigned long) &tsk->thread.ksp;
532 do {
533 prev_sp = sp;
534 sp = *(unsigned long *)sp;
535 if (sp <= prev_sp || sp >= stack_top || (sp & 3) != 0)
536 break;
537 if (count > 0)
538 printk("[%08lx] ", *(unsigned long *)(sp + 4));
539 } while (++count < 16);
540 if (count > 1)
541 printk("\n");
542 }
543
544 #if 0
545 /*
546 * Low level print for debugging - Cort
547 */
548 int __init ll_printk(const char *fmt, ...)
549 {
550 va_list args;
551 char buf[256];
552 int i;
553
554 va_start(args, fmt);
555 i=vsprintf(buf,fmt,args);
556 ll_puts(buf);
557 va_end(args);
558 return i;
559 }
560
561 int lines = 24, cols = 80;
562 int orig_x = 0, orig_y = 0;
563
564 void puthex(unsigned long val)
565 {
566 unsigned char buf[10];
567 int i;
568 for (i = 7; i >= 0; i--)
569 {
570 buf[i] = "0123456789ABCDEF"[val & 0x0F];
571 val >>= 4;
572 }
573 buf[8] = '\0';
574 prom_print(buf);
575 }
576
577 void __init ll_puts(const char *s)
578 {
579 int x,y;
580 char *vidmem = (char *)/*(_ISA_MEM_BASE + 0xB8000) */0xD00B8000;
581 char c;
582 extern int mem_init_done;
583
584 if ( mem_init_done ) /* assume this means we can printk */
585 {
586 printk(s);
587 return;
588 }
589
590 #if 0
591 if ( have_of )
592 {
593 prom_print(s);
594 return;
595 }
596 #endif
597
598 /*
599 * can't ll_puts on chrp without openfirmware yet.
600 * vidmem just needs to be setup for it.
601 * -- Cort
602 */
603 if ( _machine != _MACH_prep )
604 return;
605 x = orig_x;
606 y = orig_y;
607
608 while ( ( c = *s++ ) != '\0' ) {
609 if ( c == '\n' ) {
610 x = 0;
611 if ( ++y >= lines ) {
612 /*scroll();*/
613 /*y--;*/
614 y = 0;
615 }
616 } else {
617 vidmem [ ( x + cols * y ) * 2 ] = c;
618 if ( ++x >= cols ) {
619 x = 0;
620 if ( ++y >= lines ) {
621 /*scroll();*/
622 /*y--;*/
623 y = 0;
624 }
625 }
626 }
627 }
628
629 orig_x = x;
630 orig_y = y;
631 }
632 #endif
633
634 /*
635 * These bracket the sleeping functions..
636 */
637 extern void scheduling_functions_start_here(void);
638 extern void scheduling_functions_end_here(void);
639 #define first_sched ((unsigned long) scheduling_functions_start_here)
640 #define last_sched ((unsigned long) scheduling_functions_end_here)
641
get_wchan(struct task_struct * p)642 unsigned long get_wchan(struct task_struct *p)
643 {
644 unsigned long ip, sp;
645 unsigned long stack_page = (unsigned long) p;
646 int count = 0;
647 if (!p || p == current || p->state == TASK_RUNNING)
648 return 0;
649 sp = p->thread.ksp;
650 do {
651 sp = *(unsigned long *)sp;
652 if (sp < stack_page || sp >= stack_page + 8188)
653 return 0;
654 if (count > 0) {
655 ip = *(unsigned long *)(sp + 4);
656 if (ip < first_sched || ip >= last_sched)
657 return ip;
658 }
659 } while (count++ < 16);
660 return 0;
661 }
662