1 /*
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 * VMX/Altivec port from ppc32 (c) IBM 2003
14 * Denis Joseph Barrow (dj@de.ibm.com,barrow_dj@yahoo.com)
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 */
21
22 #include <linux/config.h>
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/smp.h>
28 #include <linux/smp_lock.h>
29 #include <linux/stddef.h>
30 #include <linux/unistd.h>
31 #include <linux/ptrace.h>
32 #include <linux/slab.h>
33 #include <linux/user.h>
34 #include <linux/elf.h>
35 #include <linux/init.h>
36
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/mmu_context.h>
44 #include <asm/prom.h>
45 #include <asm/ppcdebug.h>
46 #include <asm/machdep.h>
47 #include <asm/iSeries/HvCallHpt.h>
48 #include <asm/cputable.h>
49
50 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs);
51
52 #ifndef CONFIG_SMP
53 struct task_struct *last_task_used_math = NULL;
54 struct task_struct *last_task_used_altivec = NULL;
55 #endif /* CONFIG_SMP */
56 static struct fs_struct init_fs = INIT_FS;
57 static struct files_struct init_files = INIT_FILES;
58 static struct signal_struct init_signals = INIT_SIGNALS;
59 struct mm_struct init_mm = INIT_MM(init_mm);
60
61 struct mm_struct ioremap_mm = { pgd : ioremap_dir
62 ,page_table_lock : SPIN_LOCK_UNLOCKED };
63
64 /* this is 16-byte aligned because it has a stack in it */
65 union task_union __attribute((aligned(16))) init_task_union = {
66 INIT_TASK(init_task_union.task)
67 };
68
69 #ifdef CONFIG_SMP
70 struct current_set_struct current_set[NR_CPUS] = {{&init_task, 0}, };
71 #endif
72
73 char *sysmap = NULL;
74 unsigned long sysmap_size = 0;
75
76 extern char __toc_start;
77
78 #undef SHOW_TASK_SWITCHES
79
80 void
enable_kernel_fp(void)81 enable_kernel_fp(void)
82 {
83 #ifdef CONFIG_SMP
84 if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
85 giveup_fpu(current);
86 else
87 giveup_fpu(NULL); /* just enables FP for kernel */
88 #else
89 giveup_fpu(last_task_used_math);
90 #endif /* CONFIG_SMP */
91 }
92
93 int
dump_fpu(struct pt_regs * regs,elf_fpregset_t * fpregs)94 dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
95 {
96 if (regs->msr & MSR_FP)
97 giveup_fpu(current);
98 memcpy(fpregs, ¤t->thread.fpr[0], sizeof(*fpregs));
99 return 1;
100 }
101
102 #ifdef CONFIG_ALTIVEC
103 int
dump_altivec(struct pt_regs * regs,elf_vrregset_t * vrregs)104 dump_altivec(struct pt_regs *regs, elf_vrregset_t *vrregs)
105 {
106 if (regs->msr & MSR_VEC)
107 giveup_altivec(current);
108 memcpy(vrregs, ¤t->thread.vr[0], sizeof(*vrregs));
109 return 1;
110 }
111
112
113 void
enable_kernel_altivec(void)114 enable_kernel_altivec(void)
115 {
116 #ifdef CONFIG_SMP
117 if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
118 giveup_altivec(current);
119 else
120 giveup_altivec(NULL); /* just enable AltiVec for kernel - force */
121 #else
122 giveup_altivec(last_task_used_altivec);
123 #endif /* __SMP __ */
124 }
125 #endif /* CONFIG_ALTIVEC */
126
127
128 void
_switch_to(struct task_struct * prev,struct task_struct * new,struct task_struct ** last)129 _switch_to(struct task_struct *prev, struct task_struct *new,
130 struct task_struct **last)
131 {
132 struct thread_struct *new_thread, *old_thread;
133 unsigned long s;
134
135 __save_flags(s);
136 __cli();
137
138 #ifdef SHOW_TASK_SWITCHES
139 printk("%s/%d -> %s/%d NIP %08lx cpu %d root %x/%x\n",
140 prev->comm,prev->pid,
141 new->comm,new->pid,new->thread.regs->nip,new->processor,
142 new->fs->root,prev->fs->root);
143 #endif
144 #ifdef CONFIG_SMP
145 /* avoid complexity of lazy save/restore of fpu
146 * by just saving it every time we switch out if
147 * this task used the fpu during the last quantum.
148 *
149 * If it tries to use the fpu again, it'll trap and
150 * reload its fp regs. So we don't have to do a restore
151 * every switch, just a save.
152 * -- Cort
153 */
154 if ( prev->thread.regs && (prev->thread.regs->msr & MSR_FP) )
155 giveup_fpu(prev);
156 #ifdef CONFIG_ALTIVEC
157 /*
158 * If the previous thread used altivec in the last quantum
159 * (thus changing altivec regs) then save them.
160 * We used to check the VRSAVE register but not all apps
161 * set it, so we don't rely on it now (and in fact we need
162 * to save & restore VSCR even if VRSAVE == 0). -- paulus
163 *
164 * On SMP we always save/restore altivec regs just to avoid the
165 * complexity of changing processors.
166 * -- Cort
167 */
168 if ((prev->thread.regs && (prev->thread.regs->msr & MSR_VEC)))
169 giveup_altivec(prev);
170 #endif /* CONFIG_ALTIVEC */
171 /* prev->last_processor = prev->processor; */
172 current_set[smp_processor_id()].task = new;
173 #endif /* CONFIG_SMP */
174 new_thread = &new->thread;
175 old_thread = ¤t->thread;
176 *last = _switch(old_thread, new_thread);
177 __restore_flags(s);
178 }
179
show_regs(struct pt_regs * regs)180 void show_regs(struct pt_regs * regs)
181 {
182 int i;
183
184 printk("NIP: %016lX XER: %016lX LR: %016lX REGS: %p TRAP: %04lx %s\n",
185 regs->nip, regs->xer, regs->link, regs,regs->trap, print_tainted());
186 printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
187 regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
188 regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
189 regs->msr&MSR_IR ? 1 : 0,
190 regs->msr&MSR_DR ? 1 : 0);
191 printk("TASK = %p[%d] '%s' ",
192 current, current->pid, current->comm);
193 printk("Last syscall: %ld ", current->thread.last_syscall);
194 #ifndef CONFIG_SMP
195 printk("\nlast math %p last altivec %p", last_task_used_math,
196 last_task_used_altivec);
197 #endif
198
199 #ifdef CONFIG_SMP
200 /* printk(" CPU: %d last CPU: %d", current->processor,current->last_processor); */
201 #endif /* CONFIG_SMP */
202
203 printk("\n");
204 for (i = 0; i < 32; i++)
205 {
206 long r;
207 if ((i % 4) == 0)
208 {
209 printk("GPR%02d: ", i);
210 }
211
212 if ( __get_user(r, &(regs->gpr[i])) )
213 return;
214
215 printk("%016lX ", r);
216 if ((i % 4) == 3)
217 {
218 printk("\n");
219 }
220 }
221 }
222
exit_thread(void)223 void exit_thread(void)
224 {
225 #ifndef CONFIG_SMP
226 if (last_task_used_math == current)
227 last_task_used_math = NULL;
228 if (last_task_used_altivec == current)
229 last_task_used_altivec = NULL;
230 #endif
231 }
232
flush_thread(void)233 void flush_thread(void)
234 {
235 #ifndef CONFIG_SMP
236 if (last_task_used_math == current)
237 last_task_used_math = NULL;
238 if (last_task_used_altivec == current)
239 last_task_used_altivec = NULL;
240 #endif
241 }
242
243 void
release_thread(struct task_struct * t)244 release_thread(struct task_struct *t)
245 {
246 }
247
248 /*
249 * Copy a thread..
250 */
251 int
copy_thread(int nr,unsigned long clone_flags,unsigned long usp,unsigned long unused,struct task_struct * p,struct pt_regs * regs)252 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
253 unsigned long unused,
254 struct task_struct * p, struct pt_regs * regs)
255 {
256 unsigned long msr;
257 struct pt_regs * childregs, *kregs;
258 extern void ret_from_fork(void);
259
260 /* Copy registers */
261 childregs = ((struct pt_regs *)
262 ((unsigned long)p + sizeof(union task_union)
263 - STACK_FRAME_OVERHEAD)) - 2;
264 *childregs = *regs;
265 childregs->gpr[3] = 0; /* Result from fork() */
266 p->thread.regs = childregs;
267 p->thread.ksp = (unsigned long) childregs - STACK_FRAME_OVERHEAD;
268 p->thread.ksp -= sizeof(struct pt_regs ) + STACK_FRAME_OVERHEAD;
269 kregs = (struct pt_regs *)(p->thread.ksp + STACK_FRAME_OVERHEAD);
270 /* The PPC64 compiler makes use of a TOC to contain function
271 * pointers. The function (ret_from_except) is actually a pointer
272 * to the TOC entry. The first entry is a pointer to the actual
273 * function.
274 */
275 kregs->nip = *((unsigned long *)ret_from_fork);
276 asm volatile("mfmsr %0" : "=r" (msr):);
277 kregs->msr = msr;
278 kregs->gpr[1] = (unsigned long)childregs - STACK_FRAME_OVERHEAD;
279 kregs->gpr[2] = (((unsigned long)&__toc_start) + 0x8000);
280
281 if (usp >= (unsigned long) regs) {
282 /* Stack is in kernel space - must adjust */
283 childregs->gpr[1] = (unsigned long)(childregs + 1);
284 *((unsigned long *) childregs->gpr[1]) = 0;
285 } else {
286 /* Provided stack is in user space */
287 childregs->gpr[1] = usp;
288 }
289 p->thread.last_syscall = -1;
290
291 /*
292 * copy fpu info - assume lazy fpu switch now always
293 * -- Cort
294 */
295 if (regs->msr & MSR_FP) {
296 giveup_fpu(current);
297 childregs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
298 }
299 memcpy(&p->thread.fpr, ¤t->thread.fpr, sizeof(p->thread.fpr));
300 p->thread.fpscr = current->thread.fpscr;
301 p->thread.fpexc_mode = current->thread.fpexc_mode;
302
303 #ifdef CONFIG_ALTIVEC
304 /*
305 * copy altiVec info - assume lazy altiVec switch
306 * - kumar
307 */
308 if (regs->msr & MSR_VEC)
309 giveup_altivec(current);
310 memcpy(&p->thread.vr, ¤t->thread.vr, sizeof(p->thread.vr));
311 p->thread.vscr = current->thread.vscr;
312 childregs->msr &= ~MSR_VEC;
313 #endif /* CONFIG_ALTIVEC */
314
315 return 0;
316 }
317
318 /*
319 * Set up a thread for executing a new program
320 */
start_thread(struct pt_regs * regs,unsigned long fdptr,unsigned long sp)321 void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
322 {
323 unsigned long entry, toc, load_addr = regs->gpr[2];
324
325 /* fdptr is a relocated pointer to the function descriptor for
326 * the elf _start routine. The first entry in the function
327 * descriptor is the entry address of _start and the second
328 * entry is the TOC value we need to use.
329 */
330 set_fs(USER_DS);
331 __get_user(entry, (unsigned long *)fdptr);
332 __get_user(toc, (unsigned long *)fdptr+1);
333
334 /* Check whether the e_entry function descriptor entries
335 * need to be relocated before we can use them.
336 */
337 if ( load_addr != 0 ) {
338 entry += load_addr;
339 toc += load_addr;
340 }
341
342 regs->nip = entry;
343 regs->gpr[1] = sp;
344 regs->gpr[2] = toc;
345 regs->msr = MSR_USER64;
346 #ifndef CONFIG_SMP
347 if (last_task_used_math == current)
348 last_task_used_math = 0;
349 if (last_task_used_altivec == current)
350 last_task_used_altivec = 0;
351 #endif /* CONFIG_SMP */
352 memset(current->thread.fpr, 0, sizeof(current->thread.fpr));
353 current->thread.fpscr = 0;
354 #ifdef CONFIG_ALTIVEC
355 memset(¤t->thread.vr[0], 0,offsetof(struct thread_struct,vrsave[2])-
356 offsetof(struct thread_struct,vr[0]));
357 current->thread.vscr.u[3] = 0x00010000; /* Java mode disabled */
358 #endif /* CONFIG_ALTIVEC */
359 }
360
361 # define PR_FP_EXC_DISABLED 0 /* FP exceptions disabled */
362 # define PR_FP_EXC_NONRECOV 1 /* async non-recoverable exc. mode */
363 # define PR_FP_EXC_ASYNC 2 /* async recoverable exception mode */
364 # define PR_FP_EXC_PRECISE 3 /* precise exception mode */
365
set_fpexc_mode(struct task_struct * tsk,unsigned int val)366 int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
367 {
368 struct pt_regs *regs = tsk->thread.regs;
369
370 if (val > PR_FP_EXC_PRECISE)
371 return -EINVAL;
372 tsk->thread.fpexc_mode = __pack_fe01(val);
373 if (regs != NULL && (regs->msr & MSR_FP) != 0)
374 regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
375 | tsk->thread.fpexc_mode;
376 return 0;
377 }
378
get_fpexc_mode(struct task_struct * tsk,unsigned long adr)379 int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
380 {
381 unsigned int val;
382
383 val = __unpack_fe01(tsk->thread.fpexc_mode);
384 return put_user(val, (unsigned int *) adr);
385 }
386
sys_clone(int p1,int p2,int p3,int p4,int p5,int p6,struct pt_regs * regs)387 int sys_clone(int p1, int p2, int p3, int p4, int p5, int p6,
388 struct pt_regs *regs)
389 {
390 return do_fork(p1, regs->gpr[1], regs, 0);
391 }
392
sys_fork(int p1,int p2,int p3,int p4,int p5,int p6,struct pt_regs * regs)393 int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
394 struct pt_regs *regs)
395 {
396 return do_fork(SIGCHLD, regs->gpr[1], regs, 0);
397 }
398
sys_vfork(int p1,int p2,int p3,int p4,int p5,int p6,struct pt_regs * regs)399 int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
400 struct pt_regs *regs)
401 {
402 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0);
403 }
404
sys_execve(unsigned long a0,unsigned long a1,unsigned long a2,unsigned long a3,unsigned long a4,unsigned long a5,struct pt_regs * regs)405 int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
406 unsigned long a3, unsigned long a4, unsigned long a5,
407 struct pt_regs *regs)
408 {
409 int error;
410 char * filename;
411
412 filename = getname((char *) a0);
413 error = PTR_ERR(filename);
414 if (IS_ERR(filename))
415 goto out;
416 if (regs->msr & MSR_FP)
417 giveup_fpu(current);
418 #ifdef CONFIG_ALTIVEC
419 if (regs->msr & MSR_VEC)
420 giveup_altivec(current);
421 #endif /* CONFIG_ALTIVEC */
422 error = do_execve(filename, (char **) a1, (char **) a2, regs);
423
424 if (error == 0)
425 current->ptrace &= ~PT_DTRACE;
426 putname(filename);
427
428 out:
429 return error;
430 }
431
alloc_task_struct(void)432 struct task_struct * alloc_task_struct(void)
433 {
434 struct task_struct * new_task_ptr;
435
436 new_task_ptr = ((struct task_struct *)
437 __get_free_pages(GFP_KERNEL, get_order(THREAD_SIZE)));
438
439 return new_task_ptr;
440 }
441
free_task_struct(struct task_struct * task_ptr)442 void free_task_struct(struct task_struct * task_ptr)
443 {
444 free_pages((unsigned long)(task_ptr), get_order(THREAD_SIZE));
445 }
446
initialize_paca_hardware_interrupt_stack(void)447 void initialize_paca_hardware_interrupt_stack(void)
448 {
449 extern struct systemcfg *systemcfg;
450
451 int i;
452 unsigned long stack;
453 unsigned long end_of_stack =0;
454
455 for (i=1; i < systemcfg->processorCount; i++) {
456 /* Carve out storage for the hardware interrupt stack */
457 stack = __get_free_pages(GFP_KERNEL, get_order(8*PAGE_SIZE));
458
459 if ( !stack ) {
460 printk("ERROR, cannot find space for hardware stack.\n");
461 panic(" no hardware stack ");
462 }
463
464
465 /* Store the stack value in the PACA for the processor */
466 paca[i].xHrdIntStack = stack + (8*PAGE_SIZE) - STACK_FRAME_OVERHEAD;
467 paca[i].xHrdIntCount = 0;
468
469 }
470
471 /*
472 * __get_free_pages() might give us a page > KERNBASE+256M which
473 * is mapped with large ptes so we can't set up the guard page.
474 */
475 if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
476 return;
477
478 for (i=0; i < systemcfg->processorCount; i++) {
479 /* set page at the top of stack to be protected - prevent overflow */
480 end_of_stack = paca[i].xHrdIntStack - (8*PAGE_SIZE - STACK_FRAME_OVERHEAD);
481 ppc_md.hpte_updateboltedpp(PP_RXRX,end_of_stack);
482 }
483 }
484
485 extern char _stext[], _etext[];
486
ppc_find_proc_name(unsigned * p,char * buf,unsigned buflen)487 char * ppc_find_proc_name( unsigned * p, char * buf, unsigned buflen )
488 {
489 unsigned long tb_flags;
490 unsigned short name_len;
491 unsigned long tb_start, code_start, code_ptr, code_offset;
492 unsigned code_len;
493 strcpy( buf, "Unknown" );
494 code_ptr = (unsigned long)p;
495 code_offset = 0;
496 if ( ( (unsigned long)p >= (unsigned long)_stext ) && ( (unsigned long)p <= (unsigned long)_etext ) ) {
497 while ( (unsigned long)p <= (unsigned long)_etext ) {
498 if ( *p == 0 ) {
499 tb_start = (unsigned long)p;
500 ++p; /* Point to traceback flags */
501 tb_flags = *((unsigned long *)p);
502 p += 2; /* Skip over traceback flags */
503 if ( tb_flags & TB_NAME_PRESENT ) {
504 if ( tb_flags & TB_PARMINFO )
505 ++p; /* skip over parminfo data */
506 if ( tb_flags & TB_HAS_TBOFF ) {
507 code_len = *p; /* get code length */
508 code_start = tb_start - code_len;
509 code_offset = code_ptr - code_start + 1;
510 if ( code_offset > 0x100000 )
511 break;
512 ++p; /* skip over code size */
513 }
514 name_len = *((unsigned short *)p);
515 if ( name_len > (buflen-20) )
516 name_len = buflen-20;
517 memcpy( buf, ((char *)p)+2, name_len );
518 buf[name_len] = 0;
519 if ( code_offset )
520 sprintf( buf+name_len, "+0x%lx", code_offset-1 );
521 }
522 break;
523 }
524 ++p;
525 }
526 }
527 return buf;
528 }
529
530 void
print_backtrace(unsigned long * sp)531 print_backtrace(unsigned long *sp)
532 {
533 int cnt = 0;
534 unsigned long i;
535 char name_buf[256];
536
537 printk("Call backtrace: \n");
538 while (sp) {
539 if (__get_user(i, &sp[2]))
540 break;
541 printk("%016lX ", i);
542 printk("%s\n", ppc_find_proc_name((unsigned *)i, name_buf, 256));
543 if (cnt > 32) break;
544 if (__get_user(sp, (unsigned long **)sp))
545 break;
546 }
547 printk("\n");
548 }
549
550 /*
551 * These bracket the sleeping functions..
552 */
553 extern void scheduling_functions_start_here(void);
554 extern void scheduling_functions_end_here(void);
555 #define first_sched (*(unsigned long *)scheduling_functions_start_here)
556 #define last_sched (*(unsigned long *)scheduling_functions_end_here)
557
get_wchan(struct task_struct * p)558 unsigned long get_wchan(struct task_struct *p)
559 {
560 unsigned long ip, sp;
561 unsigned long stack_page = (unsigned long)p;
562 int count = 0;
563 if (!p || p == current || p->state == TASK_RUNNING)
564 return 0;
565 sp = p->thread.ksp;
566 do {
567 sp = *(unsigned long *)sp;
568 if (sp < (stack_page + (2 * PAGE_SIZE)) ||
569 sp >= (stack_page + THREAD_SIZE))
570 return 0;
571 if (count > 0) {
572 ip = *(unsigned long *)(sp + 16);
573 /*
574 * XXX we mask the upper 32 bits until procps
575 * gets fixed.
576 */
577 if (ip < first_sched || ip >= last_sched)
578 return (ip);
579 }
580 } while (count++ < 16);
581 return 0;
582 }
583
show_trace_task(struct task_struct * p)584 void show_trace_task(struct task_struct *p)
585 {
586 unsigned long ip, sp;
587 unsigned long stack_page = (unsigned long)p;
588 int count = 0;
589
590 if (!p)
591 return;
592
593 printk("Call Trace: ");
594 sp = p->thread.ksp;
595 do {
596 sp = *(unsigned long *)sp;
597 if (sp < (stack_page + (2 * PAGE_SIZE)) ||
598 sp >= (stack_page + THREAD_SIZE))
599 break;
600 if (count > 0) {
601 ip = *(unsigned long *)(sp + 16);
602 printk("[%016lx] ", ip);
603 }
604 } while (count++ < 16);
605 printk("\n");
606 }
607