blob: 9bad983333b1258c31dc826a91bab0699ffdaeff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/ppc64/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#include <linux/config.h>
20#include <linux/module.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/slab.h>
30#include <linux/user.h>
31#include <linux/elf.h>
32#include <linux/init.h>
33#include <linux/init_task.h>
34#include <linux/prctl.h>
35#include <linux/ptrace.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/utsname.h>
Rusty Lynch97f79432005-06-27 15:17:15 -070039#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/pgtable.h>
42#include <asm/uaccess.h>
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/processor.h>
46#include <asm/mmu.h>
47#include <asm/mmu_context.h>
48#include <asm/prom.h>
49#include <asm/ppcdebug.h>
50#include <asm/machdep.h>
51#include <asm/iSeries/HvCallHpt.h>
52#include <asm/cputable.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100053#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/sections.h>
55#include <asm/tlbflush.h>
56#include <asm/time.h>
57
58#ifndef CONFIG_SMP
59struct task_struct *last_task_used_math = NULL;
60struct task_struct *last_task_used_altivec = NULL;
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Make sure the floating-point register state in the
65 * the thread_struct is up to date for task tsk.
66 */
67void flush_fp_to_thread(struct task_struct *tsk)
68{
69 if (tsk->thread.regs) {
70 /*
71 * We need to disable preemption here because if we didn't,
72 * another process could get scheduled after the regs->msr
73 * test but before we have finished saving the FP registers
74 * to the thread_struct. That process could take over the
75 * FPU, and then when we get scheduled again we would store
76 * bogus values for the remaining FP registers.
77 */
78 preempt_disable();
79 if (tsk->thread.regs->msr & MSR_FP) {
80#ifdef CONFIG_SMP
81 /*
82 * This should only ever be called for current or
83 * for a stopped child process. Since we save away
84 * the FP register state on context switch on SMP,
85 * there is something wrong if a stopped child appears
86 * to still have its FP state in the CPU registers.
87 */
88 BUG_ON(tsk != current);
89#endif
90 giveup_fpu(current);
91 }
92 preempt_enable();
93 }
94}
95
96void enable_kernel_fp(void)
97{
98 WARN_ON(preemptible());
99
100#ifdef CONFIG_SMP
101 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
102 giveup_fpu(current);
103 else
104 giveup_fpu(NULL); /* just enables FP for kernel */
105#else
106 giveup_fpu(last_task_used_math);
107#endif /* CONFIG_SMP */
108}
109EXPORT_SYMBOL(enable_kernel_fp);
110
111int dump_task_fpu(struct task_struct *tsk, elf_fpregset_t *fpregs)
112{
113 if (!tsk->thread.regs)
114 return 0;
115 flush_fp_to_thread(current);
116
117 memcpy(fpregs, &tsk->thread.fpr[0], sizeof(*fpregs));
118
119 return 1;
120}
121
122#ifdef CONFIG_ALTIVEC
123
124void enable_kernel_altivec(void)
125{
126 WARN_ON(preemptible());
127
128#ifdef CONFIG_SMP
129 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
130 giveup_altivec(current);
131 else
132 giveup_altivec(NULL); /* just enables FP for kernel */
133#else
134 giveup_altivec(last_task_used_altivec);
135#endif /* CONFIG_SMP */
136}
137EXPORT_SYMBOL(enable_kernel_altivec);
138
139/*
140 * Make sure the VMX/Altivec register state in the
141 * the thread_struct is up to date for task tsk.
142 */
143void flush_altivec_to_thread(struct task_struct *tsk)
144{
145 if (tsk->thread.regs) {
146 preempt_disable();
147 if (tsk->thread.regs->msr & MSR_VEC) {
148#ifdef CONFIG_SMP
149 BUG_ON(tsk != current);
150#endif
151 giveup_altivec(current);
152 }
153 preempt_enable();
154 }
155}
156
157int dump_task_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
158{
159 flush_altivec_to_thread(current);
160 memcpy(vrregs, &current->thread.vr[0], sizeof(*vrregs));
161 return 1;
162}
163
164#endif /* CONFIG_ALTIVEC */
165
166DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
167
168struct task_struct *__switch_to(struct task_struct *prev,
169 struct task_struct *new)
170{
171 struct thread_struct *new_thread, *old_thread;
172 unsigned long flags;
173 struct task_struct *last;
174
175#ifdef CONFIG_SMP
176 /* avoid complexity of lazy save/restore of fpu
177 * by just saving it every time we switch out if
178 * this task used the fpu during the last quantum.
179 *
180 * If it tries to use the fpu again, it'll trap and
181 * reload its fp regs. So we don't have to do a restore
182 * every switch, just a save.
183 * -- Cort
184 */
185 if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
186 giveup_fpu(prev);
187#ifdef CONFIG_ALTIVEC
188 if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
189 giveup_altivec(prev);
190#endif /* CONFIG_ALTIVEC */
191#endif /* CONFIG_SMP */
192
193#if defined(CONFIG_ALTIVEC) && !defined(CONFIG_SMP)
194 /* Avoid the trap. On smp this this never happens since
195 * we don't set last_task_used_altivec -- Cort
196 */
197 if (new->thread.regs && last_task_used_altivec == new)
198 new->thread.regs->msr |= MSR_VEC;
199#endif /* CONFIG_ALTIVEC */
200
201 flush_tlb_pending();
202
203 new_thread = &new->thread;
204 old_thread = &current->thread;
205
206/* Collect purr utilization data per process and per processor wise */
207/* purr is nothing but processor time base */
208
209#if defined(CONFIG_PPC_PSERIES)
Stephen Rothwell1ababe12005-08-03 14:35:25 +1000210 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
212 long unsigned start_tb, current_tb;
213 start_tb = old_thread->start_tb;
214 cu->current_tb = current_tb = mfspr(SPRN_PURR);
215 old_thread->accum_tb += (current_tb - start_tb);
216 new_thread->start_tb = current_tb;
217 }
218#endif
219
220
221 local_irq_save(flags);
222 last = _switch(old_thread, new_thread);
223
224 local_irq_restore(flags);
225
226 return last;
227}
228
229static int instructions_to_print = 16;
230
231static void show_instructions(struct pt_regs *regs)
232{
233 int i;
234 unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
235 sizeof(int));
236
237 printk("Instruction dump:");
238
239 for (i = 0; i < instructions_to_print; i++) {
240 int instr;
241
242 if (!(i % 8))
243 printk("\n");
244
245 if (((REGION_ID(pc) != KERNEL_REGION_ID) &&
246 (REGION_ID(pc) != VMALLOC_REGION_ID)) ||
247 __get_user(instr, (unsigned int *)pc)) {
248 printk("XXXXXXXX ");
249 } else {
250 if (regs->nip == pc)
251 printk("<%08x> ", instr);
252 else
253 printk("%08x ", instr);
254 }
255
256 pc += sizeof(int);
257 }
258
259 printk("\n");
260}
261
262void show_regs(struct pt_regs * regs)
263{
264 int i;
265 unsigned long trap;
266
267 printk("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
268 regs->nip, (unsigned int)regs->xer, regs->link, regs->ctr);
269 printk("REGS: %p TRAP: %04lx %s (%s)\n",
270 regs, regs->trap, print_tainted(), system_utsname.release);
271 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x "
272 "IR/DR: %01x%01x CR: %08X\n",
273 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
274 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
275 regs->msr&MSR_IR ? 1 : 0,
276 regs->msr&MSR_DR ? 1 : 0,
277 (unsigned int)regs->ccr);
278 trap = TRAP(regs);
279 printk("DAR: %016lx DSISR: %016lx\n", regs->dar, regs->dsisr);
280 printk("TASK: %p[%d] '%s' THREAD: %p",
281 current, current->pid, current->comm, current->thread_info);
282
283#ifdef CONFIG_SMP
284 printk(" CPU: %d", smp_processor_id());
285#endif /* CONFIG_SMP */
286
287 for (i = 0; i < 32; i++) {
288 if ((i % 4) == 0) {
289 printk("\n" KERN_INFO "GPR%02d: ", i);
290 }
291
292 printk("%016lX ", regs->gpr[i]);
293 if (i == 13 && !FULL_REGS(regs))
294 break;
295 }
296 printk("\n");
297 /*
298 * Lookup NIP late so we have the best change of getting the
299 * above info out without failing
300 */
301 printk("NIP [%016lx] ", regs->nip);
302 print_symbol("%s\n", regs->nip);
303 printk("LR [%016lx] ", regs->link);
304 print_symbol("%s\n", regs->link);
305 show_stack(current, (unsigned long *)regs->gpr[1]);
306 if (!user_mode(regs))
307 show_instructions(regs);
308}
309
310void exit_thread(void)
311{
Rusty Lynch97f79432005-06-27 15:17:15 -0700312 kprobe_flush_task(current);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#ifndef CONFIG_SMP
315 if (last_task_used_math == current)
316 last_task_used_math = NULL;
317#ifdef CONFIG_ALTIVEC
318 if (last_task_used_altivec == current)
319 last_task_used_altivec = NULL;
320#endif /* CONFIG_ALTIVEC */
321#endif /* CONFIG_SMP */
322}
323
324void flush_thread(void)
325{
326 struct thread_info *t = current_thread_info();
327
Rusty Lynch97f79432005-06-27 15:17:15 -0700328 kprobe_flush_task(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (t->flags & _TIF_ABI_PENDING)
330 t->flags ^= (_TIF_ABI_PENDING | _TIF_32BIT);
331
332#ifndef CONFIG_SMP
333 if (last_task_used_math == current)
334 last_task_used_math = NULL;
335#ifdef CONFIG_ALTIVEC
336 if (last_task_used_altivec == current)
337 last_task_used_altivec = NULL;
338#endif /* CONFIG_ALTIVEC */
339#endif /* CONFIG_SMP */
340}
341
342void
343release_thread(struct task_struct *t)
344{
345}
346
347
348/*
349 * This gets called before we allocate a new thread and copy
350 * the current task into it.
351 */
352void prepare_to_copy(struct task_struct *tsk)
353{
354 flush_fp_to_thread(current);
355 flush_altivec_to_thread(current);
356}
357
358/*
359 * Copy a thread..
360 */
361int
362copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
363 unsigned long unused, struct task_struct *p, struct pt_regs *regs)
364{
365 struct pt_regs *childregs, *kregs;
366 extern void ret_from_fork(void);
367 unsigned long sp = (unsigned long)p->thread_info + THREAD_SIZE;
368
369 /* Copy registers */
370 sp -= sizeof(struct pt_regs);
371 childregs = (struct pt_regs *) sp;
372 *childregs = *regs;
373 if ((childregs->msr & MSR_PR) == 0) {
374 /* for kernel thread, set stackptr in new task */
375 childregs->gpr[1] = sp + sizeof(struct pt_regs);
376 p->thread.regs = NULL; /* no user register state */
377 clear_ti_thread_flag(p->thread_info, TIF_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 } else {
379 childregs->gpr[1] = usp;
380 p->thread.regs = childregs;
381 if (clone_flags & CLONE_SETTLS) {
382 if (test_thread_flag(TIF_32BIT))
383 childregs->gpr[2] = childregs->gpr[6];
384 else
385 childregs->gpr[13] = childregs->gpr[6];
386 }
387 }
388 childregs->gpr[3] = 0; /* Result from fork() */
389 sp -= STACK_FRAME_OVERHEAD;
390
391 /*
392 * The way this works is that at some point in the future
393 * some task will call _switch to switch to the new task.
394 * That will pop off the stack frame created below and start
395 * the new task running at ret_from_fork. The new task will
396 * do some house keeping and then return from the fork or clone
397 * system call, using the stack frame created above.
398 */
399 sp -= sizeof(struct pt_regs);
400 kregs = (struct pt_regs *) sp;
401 sp -= STACK_FRAME_OVERHEAD;
402 p->thread.ksp = sp;
403 if (cpu_has_feature(CPU_FTR_SLB)) {
404 unsigned long sp_vsid = get_kernel_vsid(sp);
405
406 sp_vsid <<= SLB_VSID_SHIFT;
407 sp_vsid |= SLB_VSID_KERNEL;
408 if (cpu_has_feature(CPU_FTR_16M_PAGE))
409 sp_vsid |= SLB_VSID_L;
410
411 p->thread.ksp_vsid = sp_vsid;
412 }
413
414 /*
415 * The PPC64 ABI makes use of a TOC to contain function
416 * pointers. The function (ret_from_except) is actually a pointer
417 * to the TOC entry. The first entry is a pointer to the actual
418 * function.
419 */
420 kregs->nip = *((unsigned long *)ret_from_fork);
421
422 return 0;
423}
424
425/*
426 * Set up a thread for executing a new program
427 */
428void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
429{
430 unsigned long entry, toc, load_addr = regs->gpr[2];
431
432 /* fdptr is a relocated pointer to the function descriptor for
433 * the elf _start routine. The first entry in the function
434 * descriptor is the entry address of _start and the second
435 * entry is the TOC value we need to use.
436 */
437 set_fs(USER_DS);
438 __get_user(entry, (unsigned long __user *)fdptr);
439 __get_user(toc, (unsigned long __user *)fdptr+1);
440
441 /* Check whether the e_entry function descriptor entries
442 * need to be relocated before we can use them.
443 */
444 if (load_addr != 0) {
445 entry += load_addr;
446 toc += load_addr;
447 }
448
449 /*
450 * If we exec out of a kernel thread then thread.regs will not be
451 * set. Do it now.
452 */
453 if (!current->thread.regs) {
454 unsigned long childregs = (unsigned long)current->thread_info +
455 THREAD_SIZE;
456 childregs -= sizeof(struct pt_regs);
457 current->thread.regs = (struct pt_regs *)childregs;
458 }
459
460 regs->nip = entry;
461 regs->gpr[1] = sp;
462 regs->gpr[2] = toc;
463 regs->msr = MSR_USER64;
464#ifndef CONFIG_SMP
465 if (last_task_used_math == current)
466 last_task_used_math = 0;
467#endif /* CONFIG_SMP */
468 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
469 current->thread.fpscr = 0;
470#ifdef CONFIG_ALTIVEC
471#ifndef CONFIG_SMP
472 if (last_task_used_altivec == current)
473 last_task_used_altivec = 0;
474#endif /* CONFIG_SMP */
475 memset(current->thread.vr, 0, sizeof(current->thread.vr));
476 current->thread.vscr.u[0] = 0;
477 current->thread.vscr.u[1] = 0;
478 current->thread.vscr.u[2] = 0;
479 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
480 current->thread.vrsave = 0;
481 current->thread.used_vr = 0;
482#endif /* CONFIG_ALTIVEC */
483}
484EXPORT_SYMBOL(start_thread);
485
486int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
487{
488 struct pt_regs *regs = tsk->thread.regs;
489
490 if (val > PR_FP_EXC_PRECISE)
491 return -EINVAL;
492 tsk->thread.fpexc_mode = __pack_fe01(val);
493 if (regs != NULL && (regs->msr & MSR_FP) != 0)
494 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
495 | tsk->thread.fpexc_mode;
496 return 0;
497}
498
499int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
500{
501 unsigned int val;
502
503 val = __unpack_fe01(tsk->thread.fpexc_mode);
504 return put_user(val, (unsigned int __user *) adr);
505}
506
507int sys_clone(unsigned long clone_flags, unsigned long p2, unsigned long p3,
508 unsigned long p4, unsigned long p5, unsigned long p6,
509 struct pt_regs *regs)
510{
511 unsigned long parent_tidptr = 0;
512 unsigned long child_tidptr = 0;
513
514 if (p2 == 0)
515 p2 = regs->gpr[1]; /* stack pointer for child */
516
517 if (clone_flags & (CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
518 CLONE_CHILD_CLEARTID)) {
519 parent_tidptr = p3;
520 child_tidptr = p5;
521 if (test_thread_flag(TIF_32BIT)) {
522 parent_tidptr &= 0xffffffff;
523 child_tidptr &= 0xffffffff;
524 }
525 }
526
527 return do_fork(clone_flags, p2, regs, 0,
528 (int __user *)parent_tidptr, (int __user *)child_tidptr);
529}
530
531int sys_fork(unsigned long p1, unsigned long p2, unsigned long p3,
532 unsigned long p4, unsigned long p5, unsigned long p6,
533 struct pt_regs *regs)
534{
535 return do_fork(SIGCHLD, regs->gpr[1], regs, 0, NULL, NULL);
536}
537
538int sys_vfork(unsigned long p1, unsigned long p2, unsigned long p3,
539 unsigned long p4, unsigned long p5, unsigned long p6,
540 struct pt_regs *regs)
541{
542 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0,
543 NULL, NULL);
544}
545
546int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
547 unsigned long a3, unsigned long a4, unsigned long a5,
548 struct pt_regs *regs)
549{
550 int error;
551 char * filename;
552
553 filename = getname((char __user *) a0);
554 error = PTR_ERR(filename);
555 if (IS_ERR(filename))
556 goto out;
557 flush_fp_to_thread(current);
558 flush_altivec_to_thread(current);
559 error = do_execve(filename, (char __user * __user *) a1,
560 (char __user * __user *) a2, regs);
561
562 if (error == 0) {
563 task_lock(current);
564 current->ptrace &= ~PT_DTRACE;
565 task_unlock(current);
566 }
567 putname(filename);
568
569out:
570 return error;
571}
572
573static int kstack_depth_to_print = 64;
574
575static int validate_sp(unsigned long sp, struct task_struct *p,
576 unsigned long nbytes)
577{
578 unsigned long stack_page = (unsigned long)p->thread_info;
579
580 if (sp >= stack_page + sizeof(struct thread_struct)
581 && sp <= stack_page + THREAD_SIZE - nbytes)
582 return 1;
583
584#ifdef CONFIG_IRQSTACKS
585 stack_page = (unsigned long) hardirq_ctx[task_cpu(p)];
586 if (sp >= stack_page + sizeof(struct thread_struct)
587 && sp <= stack_page + THREAD_SIZE - nbytes)
588 return 1;
589
590 stack_page = (unsigned long) softirq_ctx[task_cpu(p)];
591 if (sp >= stack_page + sizeof(struct thread_struct)
592 && sp <= stack_page + THREAD_SIZE - nbytes)
593 return 1;
594#endif
595
596 return 0;
597}
598
599unsigned long get_wchan(struct task_struct *p)
600{
601 unsigned long ip, sp;
602 int count = 0;
603
604 if (!p || p == current || p->state == TASK_RUNNING)
605 return 0;
606
607 sp = p->thread.ksp;
608 if (!validate_sp(sp, p, 112))
609 return 0;
610
611 do {
612 sp = *(unsigned long *)sp;
613 if (!validate_sp(sp, p, 112))
614 return 0;
615 if (count > 0) {
616 ip = *(unsigned long *)(sp + 16);
617 if (!in_sched_functions(ip))
618 return ip;
619 }
620 } while (count++ < 16);
621 return 0;
622}
623EXPORT_SYMBOL(get_wchan);
624
625void show_stack(struct task_struct *p, unsigned long *_sp)
626{
627 unsigned long ip, newsp, lr;
628 int count = 0;
629 unsigned long sp = (unsigned long)_sp;
630 int firstframe = 1;
631
632 if (sp == 0) {
633 if (p) {
634 sp = p->thread.ksp;
635 } else {
636 sp = __get_SP();
637 p = current;
638 }
639 }
640
641 lr = 0;
642 printk("Call Trace:\n");
643 do {
644 if (!validate_sp(sp, p, 112))
645 return;
646
647 _sp = (unsigned long *) sp;
648 newsp = _sp[0];
649 ip = _sp[2];
650 if (!firstframe || ip != lr) {
651 printk("[%016lx] [%016lx] ", sp, ip);
652 print_symbol("%s", ip);
653 if (firstframe)
654 printk(" (unreliable)");
655 printk("\n");
656 }
657 firstframe = 0;
658
659 /*
660 * See if this is an exception frame.
661 * We look for the "regshere" marker in the current frame.
662 */
663 if (validate_sp(sp, p, sizeof(struct pt_regs) + 400)
664 && _sp[12] == 0x7265677368657265ul) {
665 struct pt_regs *regs = (struct pt_regs *)
666 (sp + STACK_FRAME_OVERHEAD);
667 printk("--- Exception: %lx", regs->trap);
668 print_symbol(" at %s\n", regs->nip);
669 lr = regs->link;
670 print_symbol(" LR = %s\n", lr);
671 firstframe = 1;
672 }
673
674 sp = newsp;
675 } while (count++ < kstack_depth_to_print);
676}
677
678void dump_stack(void)
679{
680 show_stack(current, (unsigned long *)__get_SP());
681}
682EXPORT_SYMBOL(dump_stack);