process.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * Copyright (C) 2004 Thiemo Seufer
  9. */
  10. #include <linux/config.h>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/stddef.h>
  17. #include <linux/unistd.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/slab.h>
  20. #include <linux/mman.h>
  21. #include <linux/personality.h>
  22. #include <linux/sys.h>
  23. #include <linux/user.h>
  24. #include <linux/a.out.h>
  25. #include <linux/init.h>
  26. #include <linux/completion.h>
  27. #include <asm/bootinfo.h>
  28. #include <asm/cpu.h>
  29. #include <asm/fpu.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/system.h>
  32. #include <asm/mipsregs.h>
  33. #include <asm/processor.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/io.h>
  36. #include <asm/elf.h>
  37. #include <asm/isadep.h>
  38. #include <asm/inst.h>
  39. /*
  40. * We use this if we don't have any better idle routine..
  41. * (This to kill: kernel/platform.c.
  42. */
  43. void default_idle (void)
  44. {
  45. }
  46. /*
  47. * The idle thread. There's no useful work to be done, so just try to conserve
  48. * power and have a low exit latency (ie sit in a loop waiting for somebody to
  49. * say that they'd like to reschedule)
  50. */
  51. ATTRIB_NORET void cpu_idle(void)
  52. {
  53. /* endless idle loop with no priority at all */
  54. while (1) {
  55. while (!need_resched())
  56. if (cpu_wait)
  57. (*cpu_wait)();
  58. schedule();
  59. }
  60. }
  61. asmlinkage void ret_from_fork(void);
  62. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  63. {
  64. unsigned long status;
  65. /* New thread loses kernel privileges. */
  66. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
  67. #ifdef CONFIG_64BIT
  68. status &= ~ST0_FR;
  69. status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
  70. #endif
  71. status |= KU_USER;
  72. regs->cp0_status = status;
  73. clear_used_math();
  74. lose_fpu();
  75. regs->cp0_epc = pc;
  76. regs->regs[29] = sp;
  77. current_thread_info()->addr_limit = USER_DS;
  78. }
  79. void exit_thread(void)
  80. {
  81. }
  82. void flush_thread(void)
  83. {
  84. }
  85. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  86. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  87. {
  88. struct thread_info *ti = p->thread_info;
  89. struct pt_regs *childregs;
  90. long childksp;
  91. childksp = (unsigned long)ti + THREAD_SIZE - 32;
  92. preempt_disable();
  93. if (is_fpu_owner()) {
  94. save_fp(p);
  95. }
  96. preempt_enable();
  97. /* set up new TSS. */
  98. childregs = (struct pt_regs *) childksp - 1;
  99. *childregs = *regs;
  100. childregs->regs[7] = 0; /* Clear error flag */
  101. #if defined(CONFIG_BINFMT_IRIX)
  102. if (current->personality != PER_LINUX) {
  103. /* Under IRIX things are a little different. */
  104. childregs->regs[3] = 1;
  105. regs->regs[3] = 0;
  106. }
  107. #endif
  108. childregs->regs[2] = 0; /* Child gets zero as return value */
  109. regs->regs[2] = p->pid;
  110. if (childregs->cp0_status & ST0_CU0) {
  111. childregs->regs[28] = (unsigned long) ti;
  112. childregs->regs[29] = childksp;
  113. ti->addr_limit = KERNEL_DS;
  114. } else {
  115. childregs->regs[29] = usp;
  116. ti->addr_limit = USER_DS;
  117. }
  118. p->thread.reg29 = (unsigned long) childregs;
  119. p->thread.reg31 = (unsigned long) ret_from_fork;
  120. /*
  121. * New tasks lose permission to use the fpu. This accelerates context
  122. * switching for most programs since they don't use the fpu.
  123. */
  124. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  125. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  126. clear_tsk_thread_flag(p, TIF_USEDFPU);
  127. return 0;
  128. }
  129. /* Fill in the fpu structure for a core dump.. */
  130. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  131. {
  132. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  133. return 1;
  134. }
  135. void dump_regs(elf_greg_t *gp, struct pt_regs *regs)
  136. {
  137. int i;
  138. for (i = 0; i < EF_R0; i++)
  139. gp[i] = 0;
  140. gp[EF_R0] = 0;
  141. for (i = 1; i <= 31; i++)
  142. gp[EF_R0 + i] = regs->regs[i];
  143. gp[EF_R26] = 0;
  144. gp[EF_R27] = 0;
  145. gp[EF_LO] = regs->lo;
  146. gp[EF_HI] = regs->hi;
  147. gp[EF_CP0_EPC] = regs->cp0_epc;
  148. gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  149. gp[EF_CP0_STATUS] = regs->cp0_status;
  150. gp[EF_CP0_CAUSE] = regs->cp0_cause;
  151. #ifdef EF_UNUSED0
  152. gp[EF_UNUSED0] = 0;
  153. #endif
  154. }
  155. int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
  156. {
  157. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  158. return 1;
  159. }
  160. /*
  161. * Create a kernel thread
  162. */
  163. ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
  164. {
  165. do_exit(fn(arg));
  166. }
  167. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  168. {
  169. struct pt_regs regs;
  170. memset(&regs, 0, sizeof(regs));
  171. regs.regs[4] = (unsigned long) arg;
  172. regs.regs[5] = (unsigned long) fn;
  173. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  174. regs.cp0_status = read_c0_status();
  175. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  176. regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
  177. regs.cp0_status |= ST0_IEP;
  178. #else
  179. regs.cp0_status |= ST0_EXL;
  180. #endif
  181. /* Ok, create the new process.. */
  182. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  183. }
  184. struct mips_frame_info {
  185. int frame_offset;
  186. int pc_offset;
  187. };
  188. static struct mips_frame_info schedule_frame;
  189. static struct mips_frame_info schedule_timeout_frame;
  190. static struct mips_frame_info sleep_on_frame;
  191. static struct mips_frame_info sleep_on_timeout_frame;
  192. static struct mips_frame_info wait_for_completion_frame;
  193. static int mips_frame_info_initialized;
  194. static int __init get_frame_info(struct mips_frame_info *info, void *func)
  195. {
  196. int i;
  197. union mips_instruction *ip = (union mips_instruction *)func;
  198. info->pc_offset = -1;
  199. info->frame_offset = -1;
  200. for (i = 0; i < 128; i++, ip++) {
  201. /* if jal, jalr, jr, stop. */
  202. if (ip->j_format.opcode == jal_op ||
  203. (ip->r_format.opcode == spec_op &&
  204. (ip->r_format.func == jalr_op ||
  205. ip->r_format.func == jr_op)))
  206. break;
  207. if (
  208. #ifdef CONFIG_32BIT
  209. ip->i_format.opcode == sw_op &&
  210. #endif
  211. #ifdef CONFIG_64BIT
  212. ip->i_format.opcode == sd_op &&
  213. #endif
  214. ip->i_format.rs == 29)
  215. {
  216. /* sw / sd $ra, offset($sp) */
  217. if (ip->i_format.rt == 31) {
  218. if (info->pc_offset != -1)
  219. break;
  220. info->pc_offset =
  221. ip->i_format.simmediate / sizeof(long);
  222. }
  223. /* sw / sd $s8, offset($sp) */
  224. if (ip->i_format.rt == 30) {
  225. if (info->frame_offset != -1)
  226. break;
  227. info->frame_offset =
  228. ip->i_format.simmediate / sizeof(long);
  229. }
  230. }
  231. }
  232. if (info->pc_offset == -1 || info->frame_offset == -1) {
  233. printk("Can't analyze prologue code at %p\n", func);
  234. info->pc_offset = -1;
  235. info->frame_offset = -1;
  236. return -1;
  237. }
  238. return 0;
  239. }
  240. static int __init frame_info_init(void)
  241. {
  242. mips_frame_info_initialized =
  243. !get_frame_info(&schedule_frame, schedule) &&
  244. !get_frame_info(&schedule_timeout_frame, schedule_timeout) &&
  245. !get_frame_info(&sleep_on_frame, sleep_on) &&
  246. !get_frame_info(&sleep_on_timeout_frame, sleep_on_timeout) &&
  247. !get_frame_info(&wait_for_completion_frame, wait_for_completion);
  248. return 0;
  249. }
  250. arch_initcall(frame_info_init);
  251. /*
  252. * Return saved PC of a blocked thread.
  253. */
  254. unsigned long thread_saved_pc(struct task_struct *tsk)
  255. {
  256. struct thread_struct *t = &tsk->thread;
  257. /* New born processes are a special case */
  258. if (t->reg31 == (unsigned long) ret_from_fork)
  259. return t->reg31;
  260. if (schedule_frame.pc_offset < 0)
  261. return 0;
  262. return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
  263. }
  264. /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
  265. unsigned long get_wchan(struct task_struct *p)
  266. {
  267. unsigned long frame, pc;
  268. if (!p || p == current || p->state == TASK_RUNNING)
  269. return 0;
  270. if (!mips_frame_info_initialized)
  271. return 0;
  272. pc = thread_saved_pc(p);
  273. if (!in_sched_functions(pc))
  274. goto out;
  275. if (pc >= (unsigned long) sleep_on_timeout)
  276. goto schedule_timeout_caller;
  277. if (pc >= (unsigned long) sleep_on)
  278. goto schedule_caller;
  279. if (pc >= (unsigned long) interruptible_sleep_on_timeout)
  280. goto schedule_timeout_caller;
  281. if (pc >= (unsigned long)interruptible_sleep_on)
  282. goto schedule_caller;
  283. if (pc >= (unsigned long)wait_for_completion)
  284. goto schedule_caller;
  285. goto schedule_timeout_caller;
  286. schedule_caller:
  287. frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
  288. if (pc >= (unsigned long) sleep_on)
  289. pc = ((unsigned long *)frame)[sleep_on_frame.pc_offset];
  290. else
  291. pc = ((unsigned long *)frame)[wait_for_completion_frame.pc_offset];
  292. goto out;
  293. schedule_timeout_caller:
  294. /*
  295. * The schedule_timeout frame
  296. */
  297. frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
  298. /*
  299. * frame now points to sleep_on_timeout's frame
  300. */
  301. pc = ((unsigned long *)frame)[schedule_timeout_frame.pc_offset];
  302. if (in_sched_functions(pc)) {
  303. /* schedule_timeout called by [interruptible_]sleep_on_timeout */
  304. frame = ((unsigned long *)frame)[schedule_timeout_frame.frame_offset];
  305. pc = ((unsigned long *)frame)[sleep_on_timeout_frame.pc_offset];
  306. }
  307. out:
  308. #ifdef CONFIG_64BIT
  309. if (current->thread.mflags & MF_32BIT_REGS) /* Kludge for 32-bit ps */
  310. pc &= 0xffffffffUL;
  311. #endif
  312. return pc;
  313. }
  314. EXPORT_SYMBOL(get_wchan);