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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * This file is subject to the terms and conditions of the GNU General Public
  3.  * License.  See the file "COPYING" in the main directory of this archive
  4.  * for more details.
  5.  *
  6.  * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others.
  7.  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8.  */
  9. #include <linux/errno.h>
  10. #include <linux/sched.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/stddef.h>
  14. #include <linux/unistd.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/slab.h>
  17. #include <linux/mman.h>
  18. #include <linux/personality.h>
  19. #include <linux/sys.h>
  20. #include <linux/user.h>
  21. #include <linux/a.out.h>
  22. #include <asm/bootinfo.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system.h>
  25. #include <asm/mipsregs.h>
  26. #include <asm/processor.h>
  27. #include <asm/stackframe.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/io.h>
  30. #include <asm/elf.h>
  31. #include <asm/cpu.h>
  32. ATTRIB_NORET void cpu_idle(void)
  33. {
  34. /* endless idle loop with no priority at all */
  35. init_idle();
  36. current->nice = 20;
  37. current->counter = -100;
  38. while (1) {
  39. while (!current->need_resched)
  40. if (cpu_wait)
  41. (*cpu_wait)();
  42. schedule();
  43. check_pgt_cache();
  44. }
  45. }
  46. struct task_struct *last_task_used_math = NULL;
  47. asmlinkage void ret_from_fork(void);
  48. void exit_thread(void)
  49. {
  50. /* Forget lazy fpu state */
  51. if (IS_FPU_OWNER()) {
  52. if (mips_cpu.options & MIPS_CPU_FPU) {
  53. __enable_fpu();
  54. __asm__ __volatile__("cfc1t$0,$31");
  55. }
  56. CLEAR_FPU_OWNER();
  57. }
  58. }
  59. void flush_thread(void)
  60. {
  61. /* Forget lazy fpu state */
  62. if (IS_FPU_OWNER()) {
  63. if (mips_cpu.options & MIPS_CPU_FPU) {
  64. __enable_fpu();
  65. __asm__ __volatile__("cfc1t$0,$31");
  66. }
  67. CLEAR_FPU_OWNER();
  68. }
  69. }
  70. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  71. unsigned long unused, struct task_struct *p,
  72. struct pt_regs *regs)
  73. {
  74. struct pt_regs *childregs;
  75. long childksp;
  76. childksp = (unsigned long)p + KERNEL_STACK_SIZE - 32;
  77. if (IS_FPU_OWNER()) {
  78. if (mips_cpu.options & MIPS_CPU_FPU)
  79. save_fp(p);
  80. }
  81. /* set up new TSS. */
  82. childregs = (struct pt_regs *) childksp - 1;
  83. *childregs = *regs;
  84. childregs->regs[7] = 0; /* Clear error flag */
  85. childregs->regs[2] = 0; /* Child gets zero as return value */
  86. regs->regs[2] = p->pid;
  87. if (childregs->cp0_status & ST0_CU0) {
  88. childregs->regs[28] = (unsigned long) p;
  89. childregs->regs[29] = childksp;
  90. p->thread.current_ds = KERNEL_DS;
  91. } else {
  92. childregs->regs[29] = usp;
  93. p->thread.current_ds = USER_DS;
  94. }
  95. p->thread.reg29 = (unsigned long) childregs;
  96. p->thread.reg31 = (unsigned long) ret_from_fork;
  97. /*
  98.  * New tasks loose permission to use the fpu. This accelerates context
  99.  * switching for most programs since they don't use the fpu.
  100.  */
  101. p->thread.cp0_status = read_32bit_cp0_register(CP0_STATUS) &
  102.                             ~(ST0_CU3|ST0_CU2|ST0_CU1|ST0_KSU);
  103. childregs->cp0_status &= ~(ST0_CU3|ST0_CU2|ST0_CU1);
  104. return 0;
  105. }
  106. /* Fill in the fpu structure for a core dump.. */
  107. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  108. {
  109. /* We actually store the FPU info in the task->thread
  110.  * area.
  111.  */
  112. if (regs->cp0_status & ST0_CU1) {
  113. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  114. return 1;
  115. }
  116. return 0; /* Task didn't use the fpu at all. */
  117. }
  118. /* Fill in the user structure for a core dump.. */
  119. void dump_thread(struct pt_regs *regs, struct user *dump)
  120. {
  121. dump->magic = CMAGIC;
  122. dump->start_code  = current->mm->start_code;
  123. dump->start_data  = current->mm->start_data;
  124. dump->start_stack = regs->regs[29] & ~(PAGE_SIZE - 1);
  125. dump->u_tsize = (current->mm->end_code - dump->start_code)
  126.                 >> PAGE_SHIFT;
  127. dump->u_dsize = (current->mm->brk + (PAGE_SIZE - 1) - dump->start_data)
  128.                 >> PAGE_SHIFT;
  129. dump->u_ssize = (current->mm->start_stack - dump->start_stack +
  130.                  PAGE_SIZE - 1) >> PAGE_SHIFT;
  131. memcpy(&dump->regs[0], regs, sizeof(struct pt_regs));
  132. memcpy(&dump->regs[EF_SIZE/4], &current->thread.fpu,
  133.        sizeof(current->thread.fpu));
  134. }
  135. /*
  136.  * Create a kernel thread
  137.  */
  138. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  139. {
  140. int retval;
  141. __asm__ __volatile__(
  142. "movet$6, $spnt"
  143. "movet$4, %5nt"
  144. "lit$2, %1nt"
  145. "syscallnt"
  146. "beqt$6, $sp, 1fnt"
  147. "movet$4, %3nt"
  148. "jalrt%4nt"
  149. "movet$4, $2nt"
  150. "lit$2, %2nt"
  151. "syscalln"
  152. "1:tmovet%0, $2"
  153. :"=r" (retval)
  154. :"i" (__NR_clone), "i" (__NR_exit), "r" (arg), "r" (fn),
  155.  "r" (flags | CLONE_VM)
  156.  /* The called subroutine might have destroyed any of the
  157.   * at, result, argument or temporary registers ...  */
  158. : "$2", "$3", "$4", "$5", "$6", "$7", "$8",
  159.   "$9","$10","$11","$12","$13","$14","$15","$24","$25");
  160. return retval;
  161. }
  162. /*
  163.  * These bracket the sleeping functions..
  164.  */
  165. extern void scheduling_functions_start_here(void);
  166. extern void scheduling_functions_end_here(void);
  167. #define first_sched ((unsigned long) scheduling_functions_start_here)
  168. #define last_sched ((unsigned long) scheduling_functions_end_here)
  169. /* get_wchan - a maintenance nightmare ...  */
  170. unsigned long get_wchan(struct task_struct *p)
  171. {
  172. unsigned long frame, pc;
  173. if (!p || p == current || p->state == TASK_RUNNING)
  174. return 0;
  175. pc = thread_saved_pc(&p->thread);
  176. if (pc < first_sched || pc >= last_sched)
  177. goto out;
  178. if (pc >= (unsigned long) sleep_on_timeout)
  179. goto schedule_timeout_caller;
  180. if (pc >= (unsigned long) sleep_on)
  181. goto schedule_caller;
  182. if (pc >= (unsigned long) interruptible_sleep_on_timeout)
  183. goto schedule_timeout_caller;
  184. if (pc >= (unsigned long)interruptible_sleep_on)
  185. goto schedule_caller;
  186. goto schedule_timeout_caller;
  187. schedule_caller:
  188. frame = ((unsigned long *)p->thread.reg30)[10];
  189. pc    = ((unsigned long *)frame)[7];
  190. goto out;
  191. schedule_timeout_caller:
  192. /* Must be schedule_timeout ...  */
  193. pc    = ((unsigned long *)p->thread.reg30)[11];
  194. frame = ((unsigned long *)p->thread.reg30)[10];
  195. /* The schedule_timeout frame ...  */
  196. pc    = ((unsigned long *)frame)[9];
  197. frame = ((unsigned long *)frame)[8];
  198. if (pc >= first_sched && pc < last_sched) {
  199. /* schedule_timeout called by interruptible_sleep_on_timeout */
  200. pc    = ((unsigned long *)frame)[7];
  201. frame = ((unsigned long *)frame)[6];
  202. }
  203. out:
  204. if (current->thread.mflags & MF_32BIT) /* Kludge for 32-bit ps  */
  205. pc &= 0xffffffff;
  206. return pc;
  207. }