process.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:13k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  *  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. #include <linux/config.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/stddef.h>
  26. #include <linux/unistd.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/user.h>
  30. #include <linux/elf.h>
  31. #include <linux/init.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/system.h>
  35. #include <asm/io.h>
  36. #include <asm/processor.h>
  37. #include <asm/mmu.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/prom.h>
  40. #include <asm/ppcdebug.h>
  41. #include <asm/machdep.h>
  42. #include <asm/iSeries/HvCallHpt.h>
  43. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs);
  44. struct task_struct *last_task_used_math = NULL;
  45. static struct fs_struct init_fs = INIT_FS;
  46. static struct files_struct init_files = INIT_FILES;
  47. static struct signal_struct init_signals = INIT_SIGNALS;
  48. struct mm_struct init_mm = INIT_MM(init_mm);
  49. struct mm_struct ioremap_mm = { pgd             : ioremap_dir  
  50.                                ,page_table_lock : SPIN_LOCK_UNLOCKED };
  51. /* this is 16-byte aligned because it has a stack in it */
  52. union task_union __attribute((aligned(16))) init_task_union = {
  53. INIT_TASK(init_task_union.task)
  54. };
  55. #ifdef CONFIG_SMP
  56. struct current_set_struct current_set[NR_CPUS] = {{&init_task, 0}, };
  57. #endif
  58. char *sysmap = NULL; 
  59. unsigned long sysmap_size = 0;
  60. extern char __toc_start;
  61. #undef SHOW_TASK_SWITCHES
  62. void
  63. enable_kernel_fp(void)
  64. {
  65. #ifdef CONFIG_SMP
  66. if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
  67. giveup_fpu(current);
  68. else
  69. giveup_fpu(NULL); /* just enables FP for kernel */
  70. #else
  71. giveup_fpu(last_task_used_math);
  72. #endif /* CONFIG_SMP */
  73. }
  74. int
  75. dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
  76. {
  77. if (regs->msr & MSR_FP)
  78. giveup_fpu(current);
  79. memcpy(fpregs, &current->thread.fpr[0], sizeof(*fpregs));
  80. return 1;
  81. }
  82. void
  83. _switch_to(struct task_struct *prev, struct task_struct *new,
  84.   struct task_struct **last)
  85. {
  86. struct thread_struct *new_thread, *old_thread;
  87. unsigned long s;
  88. __save_flags(s);
  89. __cli();
  90. #ifdef SHOW_TASK_SWITCHES
  91. printk("%s/%d -> %s/%d NIP %08lx cpu %d root %x/%xn",
  92.        prev->comm,prev->pid,
  93.        new->comm,new->pid,new->thread.regs->nip,new->processor,
  94.        new->fs->root,prev->fs->root);
  95. #endif
  96. #ifdef CONFIG_SMP
  97. /* avoid complexity of lazy save/restore of fpu
  98.  * by just saving it every time we switch out if
  99.  * this task used the fpu during the last quantum.
  100.  * 
  101.  * If it tries to use the fpu again, it'll trap and
  102.  * reload its fp regs.  So we don't have to do a restore
  103.  * every switch, just a save.
  104.  *  -- Cort
  105.  */
  106. if ( prev->thread.regs && (prev->thread.regs->msr & MSR_FP) )
  107. giveup_fpu(prev);
  108. /* prev->last_processor = prev->processor; */
  109. current_set[smp_processor_id()].task = new;
  110. #endif /* CONFIG_SMP */
  111. new_thread = &new->thread;
  112. old_thread = &current->thread;
  113. *last = _switch(old_thread, new_thread);
  114. __restore_flags(s);
  115. }
  116. void show_regs(struct pt_regs * regs)
  117. {
  118. int i;
  119. printk("NIP: %016lX XER: %016lX LR: %016lX REGS: %p TRAP: %04lx    %sn",
  120.        regs->nip, regs->xer, regs->link, regs,regs->trap, print_tainted());
  121. printk("MSR: %016lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01xn",
  122.        regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
  123.        regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
  124.        regs->msr&MSR_IR ? 1 : 0,
  125.        regs->msr&MSR_DR ? 1 : 0);
  126. printk("TASK = %p[%d] '%s' ",
  127.        current, current->pid, current->comm);
  128. printk("Last syscall: %ld ", current->thread.last_syscall);
  129. printk("nlast math %p ", last_task_used_math);
  130. #ifdef CONFIG_SMP
  131. /* printk(" CPU: %d last CPU: %d", current->processor,current->last_processor); */
  132. #endif /* CONFIG_SMP */
  133. printk("n");
  134. for (i = 0;  i < 32;  i++)
  135. {
  136. long r;
  137. if ((i % 4) == 0)
  138. {
  139. printk("GPR%02d: ", i);
  140. }
  141. if ( __get_user(r, &(regs->gpr[i])) )
  142.     return;
  143. printk("%016lX ", r);
  144. if ((i % 4) == 3)
  145. {
  146. printk("n");
  147. }
  148. }
  149. }
  150. void exit_thread(void)
  151. {
  152. if (last_task_used_math == current)
  153. last_task_used_math = NULL;
  154. }
  155. void flush_thread(void)
  156. {
  157. if (last_task_used_math == current)
  158. last_task_used_math = NULL;
  159. }
  160. void
  161. release_thread(struct task_struct *t)
  162. {
  163. }
  164. /*
  165.  * Copy a thread..
  166.  */
  167. int
  168. copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  169.     unsigned long unused,
  170.     struct task_struct * p, struct pt_regs * regs)
  171. {
  172. unsigned long msr;
  173. struct pt_regs * childregs, *kregs;
  174. extern void ret_from_fork(void);
  175. /* Copy registers */
  176. childregs = ((struct pt_regs *)
  177.      ((unsigned long)p + sizeof(union task_union)
  178.       - STACK_FRAME_OVERHEAD)) - 2;
  179. *childregs = *regs;
  180. childregs->gpr[3] = 0;  /* Result from fork() */
  181. p->thread.regs = childregs;
  182. p->thread.ksp = (unsigned long) childregs - STACK_FRAME_OVERHEAD;
  183. p->thread.ksp -= sizeof(struct pt_regs ) + STACK_FRAME_OVERHEAD;
  184. kregs = (struct pt_regs *)(p->thread.ksp + STACK_FRAME_OVERHEAD);
  185. /* The PPC64 compiler makes use of a TOC to contain function 
  186.  * pointers.  The function (ret_from_except) is actually a pointer
  187.  * to the TOC entry.  The first entry is a pointer to the actual
  188.  * function.
  189.    */
  190. kregs->nip = *((unsigned long *)ret_from_fork);
  191. asm volatile("mfmsr %0" : "=r" (msr):);
  192. kregs->msr = msr;
  193. kregs->gpr[1] = (unsigned long)childregs - STACK_FRAME_OVERHEAD;
  194. kregs->gpr[2] = (((unsigned long)&__toc_start) + 0x8000);
  195. if (usp >= (unsigned long) regs) {
  196. /* Stack is in kernel space - must adjust */
  197. childregs->gpr[1] = (unsigned long)(childregs + 1);
  198. *((unsigned long *) childregs->gpr[1]) = 0;
  199. childregs->gpr[13] = (unsigned long) p;
  200. } else {
  201. /* Provided stack is in user space */
  202. childregs->gpr[1] = usp;
  203. }
  204. p->thread.last_syscall = -1;
  205.   
  206. /*
  207.  * copy fpu info - assume lazy fpu switch now always
  208.  *  -- Cort
  209.  */
  210. if (regs->msr & MSR_FP) {
  211. giveup_fpu(current);
  212. childregs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  213. }
  214. memcpy(&p->thread.fpr, &current->thread.fpr, sizeof(p->thread.fpr));
  215. p->thread.fpscr = current->thread.fpscr;
  216. return 0;
  217. }
  218. /*
  219.  * Set up a thread for executing a new program
  220.  */
  221. void start_thread(struct pt_regs *regs, unsigned long fdptr, unsigned long sp)
  222. {
  223. unsigned long entry, toc, load_addr = regs->gpr[2];
  224. /* fdptr is a relocated pointer to the function descriptor for
  225.          * the elf _start routine.  The first entry in the function
  226.          * descriptor is the entry address of _start and the second
  227.          * entry is the TOC value we need to use.
  228.          */
  229. set_fs(USER_DS);
  230. __get_user(entry, (unsigned long *)fdptr);
  231. __get_user(toc, (unsigned long *)fdptr+1);
  232. /* Check whether the e_entry function descriptor entries
  233.  * need to be relocated before we can use them.
  234.  */
  235. if ( load_addr != 0 ) {
  236. entry += load_addr;
  237. toc   += load_addr;
  238. }
  239. regs->nip = entry;
  240. regs->gpr[1] = sp;
  241. regs->gpr[2] = toc;
  242. regs->msr = MSR_USER64;
  243. if (last_task_used_math == current)
  244. last_task_used_math = 0;
  245. current->thread.fpscr = 0;
  246. }
  247. int sys_clone(int p1, int p2, int p3, int p4, int p5, int p6,
  248.       struct pt_regs *regs)
  249. {
  250. return do_fork(p1, regs->gpr[1], regs, 0);
  251. }
  252. int sys_fork(int p1, int p2, int p3, int p4, int p5, int p6,
  253.      struct pt_regs *regs)
  254. {
  255. return do_fork(SIGCHLD, regs->gpr[1], regs, 0);
  256. }
  257. int sys_vfork(int p1, int p2, int p3, int p4, int p5, int p6,
  258.  struct pt_regs *regs)
  259. {
  260. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gpr[1], regs, 0);
  261. }
  262. int sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
  263.        unsigned long a3, unsigned long a4, unsigned long a5,
  264.        struct pt_regs *regs)
  265. {
  266. int error;
  267. char * filename;
  268. filename = getname((char *) a0);
  269. error = PTR_ERR(filename);
  270. if (IS_ERR(filename))
  271. goto out;
  272. if (regs->msr & MSR_FP)
  273. giveup_fpu(current);
  274.   
  275. error = do_execve(filename, (char **) a1, (char **) a2, regs);
  276.   
  277. if (error == 0)
  278. current->ptrace &= ~PT_DTRACE;
  279. putname(filename);
  280. out:
  281. return error;
  282. }
  283. struct task_struct * alloc_task_struct(void)
  284. {
  285. struct task_struct * new_task_ptr;
  286.     
  287. new_task_ptr = ((struct task_struct *) 
  288. __get_free_pages(GFP_KERNEL, get_order(THREAD_SIZE)));
  289.     
  290. return new_task_ptr;
  291. }
  292. void free_task_struct(struct task_struct * task_ptr)
  293. {
  294. free_pages((unsigned long)(task_ptr), get_order(THREAD_SIZE));
  295. }
  296. void initialize_paca_hardware_interrupt_stack(void)
  297. {
  298. extern struct naca_struct *naca;
  299. int i;
  300. unsigned long stack;
  301. unsigned long end_of_stack =0;
  302. for (i=1; i < naca->processorCount; i++) {
  303. /* Carve out storage for the hardware interrupt stack */
  304. stack = __get_free_pages(GFP_KERNEL, get_order(8*PAGE_SIZE));
  305. if ( !stack ) {     
  306. printk("ERROR, cannot find space for hardware stack.n");
  307. panic(" no hardware stack ");
  308. }
  309. /* Store the stack value in the PACA for the processor */
  310. paca[i].xHrdIntStack = stack + (8*PAGE_SIZE) - STACK_FRAME_OVERHEAD;
  311. paca[i].xHrdIntCount = 0;
  312. }
  313. /*
  314.  * __get_free_pages() might give us a page > KERNBASE+256M which
  315.  * is mapped with large ptes so we can't set up the guard page.
  316.  */
  317. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  318. return;
  319. for (i=0; i < naca->processorCount; i++) {
  320. /* set page at the top of stack to be protected - prevent overflow */
  321. end_of_stack = paca[i].xHrdIntStack - (8*PAGE_SIZE - STACK_FRAME_OVERHEAD);
  322. ppc_md.hpte_updateboltedpp(PP_RXRX,end_of_stack);
  323. }
  324. }
  325. extern char _stext[], _etext[];
  326. char * ppc_find_proc_name( unsigned * p, char * buf, unsigned buflen )
  327. {
  328. unsigned long tb_flags;
  329. unsigned short name_len;
  330. unsigned long tb_start, code_start, code_ptr, code_offset;
  331. unsigned code_len;
  332. strcpy( buf, "Unknown" );
  333. code_ptr = (unsigned long)p;
  334. code_offset = 0;
  335. if ( ( (unsigned long)p >= (unsigned long)_stext ) && ( (unsigned long)p <= (unsigned long)_etext ) ) {
  336. while ( (unsigned long)p <= (unsigned long)_etext ) {
  337. if ( *p == 0 ) {
  338. tb_start = (unsigned long)p;
  339. ++p; /* Point to traceback flags */
  340. tb_flags = *((unsigned long *)p);
  341. p += 2; /* Skip over traceback flags */
  342. if ( tb_flags & TB_NAME_PRESENT ) {
  343. if ( tb_flags & TB_PARMINFO )
  344. ++p; /* skip over parminfo data */
  345. if ( tb_flags & TB_HAS_TBOFF ) {
  346. code_len = *p; /* get code length */
  347. code_start = tb_start - code_len;
  348. code_offset = code_ptr - code_start + 1;
  349. if ( code_offset > 0x100000 )
  350. break;
  351. ++p; /* skip over code size */
  352. }
  353. name_len = *((unsigned short *)p);
  354. if ( name_len > (buflen-20) )
  355. name_len = buflen-20;
  356. memcpy( buf, ((char *)p)+2, name_len );
  357. buf[name_len] = 0;
  358. if ( code_offset )
  359. sprintf( buf+name_len, "+0x%lx", code_offset-1 ); 
  360. }
  361. break;
  362. }
  363. ++p;
  364. }
  365. }
  366. return buf;
  367. }
  368. void
  369. print_backtrace(unsigned long *sp)
  370. {
  371. int cnt = 0;
  372. unsigned long i;
  373. char name_buf[256];
  374. printk("Call backtrace: n");
  375. while (sp) {
  376. if (__get_user(i, &sp[2]))
  377. break;
  378. printk("%016lX ", i);
  379. printk("%sn", ppc_find_proc_name((unsigned *)i, name_buf, 256));
  380. if (cnt > 32) break;
  381. if (__get_user(sp, (unsigned long **)sp))
  382. break;
  383. }
  384. printk("n");
  385. }
  386. /*
  387.  * These bracket the sleeping functions..
  388.  */
  389. extern void scheduling_functions_start_here(void);
  390. extern void scheduling_functions_end_here(void);
  391. #define first_sched    (*(unsigned long *)scheduling_functions_start_here)
  392. #define last_sched     (*(unsigned long *)scheduling_functions_end_here)
  393. unsigned long get_wchan(struct task_struct *p)
  394. {
  395. unsigned long ip, sp;
  396. unsigned long stack_page = (unsigned long)p;
  397. int count = 0;
  398. if (!p || p == current || p->state == TASK_RUNNING)
  399. return 0;
  400. sp = p->thread.ksp;
  401. do {
  402. sp = *(unsigned long *)sp;
  403. if (sp < (stack_page + (2 * PAGE_SIZE)) ||
  404.     sp >= (stack_page + THREAD_SIZE))
  405. return 0;
  406. if (count > 0) {
  407. ip = *(unsigned long *)(sp + 16);
  408. if (ip < first_sched || ip >= last_sched)
  409. return (ip & 0xFFFFFFFF);
  410. }
  411. } while (count++ < 16);
  412. return 0;
  413. }
  414. void show_trace_task(struct task_struct *p)
  415. {
  416. unsigned long ip, sp;
  417. unsigned long stack_page = (unsigned long)p;
  418. int count = 0;
  419. if (!p)
  420. return;
  421. printk("Call Trace: ");
  422. sp = p->thread.ksp;
  423. do {
  424. sp = *(unsigned long *)sp;
  425. if (sp < (stack_page + (2 * PAGE_SIZE)) ||
  426.     sp >= (stack_page + THREAD_SIZE))
  427. break;
  428. if (count > 0) {
  429. ip = *(unsigned long *)(sp + 16);
  430. printk("[%016lx] ", ip);
  431. }
  432. } while (count++ < 16);
  433. printk("n");
  434. }