process.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. * The idle thread. There's no useful work to be done, so just try to conserve
  41. * power and have a low exit latency (ie sit in a loop waiting for somebody to
  42. * say that they'd like to reschedule)
  43. */
  44. ATTRIB_NORET void cpu_idle(void)
  45. {
  46. /* endless idle loop with no priority at all */
  47. while (1) {
  48. while (!need_resched())
  49. if (cpu_wait)
  50. (*cpu_wait)();
  51. schedule();
  52. }
  53. }
  54. asmlinkage void ret_from_fork(void);
  55. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  56. {
  57. unsigned long status;
  58. /* New thread loses kernel privileges. */
  59. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
  60. #ifdef CONFIG_64BIT
  61. status &= ~ST0_FR;
  62. status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
  63. #endif
  64. status |= KU_USER;
  65. regs->cp0_status = status;
  66. clear_used_math();
  67. lose_fpu();
  68. regs->cp0_epc = pc;
  69. regs->regs[29] = sp;
  70. current_thread_info()->addr_limit = USER_DS;
  71. }
  72. void exit_thread(void)
  73. {
  74. }
  75. void flush_thread(void)
  76. {
  77. }
  78. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  79. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  80. {
  81. struct thread_info *ti = p->thread_info;
  82. struct pt_regs *childregs;
  83. long childksp;
  84. p->set_child_tid = p->clear_child_tid = NULL;
  85. childksp = (unsigned long)ti + THREAD_SIZE - 32;
  86. preempt_disable();
  87. if (is_fpu_owner()) {
  88. save_fp(p);
  89. }
  90. preempt_enable();
  91. /* set up new TSS. */
  92. childregs = (struct pt_regs *) childksp - 1;
  93. *childregs = *regs;
  94. childregs->regs[7] = 0; /* Clear error flag */
  95. #if defined(CONFIG_BINFMT_IRIX)
  96. if (current->personality != PER_LINUX) {
  97. /* Under IRIX things are a little different. */
  98. childregs->regs[3] = 1;
  99. regs->regs[3] = 0;
  100. }
  101. #endif
  102. childregs->regs[2] = 0; /* Child gets zero as return value */
  103. regs->regs[2] = p->pid;
  104. if (childregs->cp0_status & ST0_CU0) {
  105. childregs->regs[28] = (unsigned long) ti;
  106. childregs->regs[29] = childksp;
  107. ti->addr_limit = KERNEL_DS;
  108. } else {
  109. childregs->regs[29] = usp;
  110. ti->addr_limit = USER_DS;
  111. }
  112. p->thread.reg29 = (unsigned long) childregs;
  113. p->thread.reg31 = (unsigned long) ret_from_fork;
  114. /*
  115. * New tasks lose permission to use the fpu. This accelerates context
  116. * switching for most programs since they don't use the fpu.
  117. */
  118. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  119. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  120. clear_tsk_thread_flag(p, TIF_USEDFPU);
  121. if (clone_flags & CLONE_SETTLS)
  122. ti->tp_value = regs->regs[7];
  123. return 0;
  124. }
  125. /* Fill in the fpu structure for a core dump.. */
  126. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  127. {
  128. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  129. return 1;
  130. }
  131. void dump_regs(elf_greg_t *gp, struct pt_regs *regs)
  132. {
  133. int i;
  134. for (i = 0; i < EF_R0; i++)
  135. gp[i] = 0;
  136. gp[EF_R0] = 0;
  137. for (i = 1; i <= 31; i++)
  138. gp[EF_R0 + i] = regs->regs[i];
  139. gp[EF_R26] = 0;
  140. gp[EF_R27] = 0;
  141. gp[EF_LO] = regs->lo;
  142. gp[EF_HI] = regs->hi;
  143. gp[EF_CP0_EPC] = regs->cp0_epc;
  144. gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  145. gp[EF_CP0_STATUS] = regs->cp0_status;
  146. gp[EF_CP0_CAUSE] = regs->cp0_cause;
  147. #ifdef EF_UNUSED0
  148. gp[EF_UNUSED0] = 0;
  149. #endif
  150. }
  151. int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs)
  152. {
  153. struct thread_info *ti = tsk->thread_info;
  154. long ksp = (unsigned long)ti + THREAD_SIZE - 32;
  155. dump_regs(&(*regs)[0], (struct pt_regs *) ksp - 1);
  156. return 1;
  157. }
  158. int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
  159. {
  160. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  161. return 1;
  162. }
  163. /*
  164. * Create a kernel thread
  165. */
  166. ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
  167. {
  168. do_exit(fn(arg));
  169. }
  170. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  171. {
  172. struct pt_regs regs;
  173. memset(&regs, 0, sizeof(regs));
  174. regs.regs[4] = (unsigned long) arg;
  175. regs.regs[5] = (unsigned long) fn;
  176. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  177. regs.cp0_status = read_c0_status();
  178. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  179. regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
  180. regs.cp0_status |= ST0_IEP;
  181. #else
  182. regs.cp0_status |= ST0_EXL;
  183. #endif
  184. /* Ok, create the new process.. */
  185. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  186. }
  187. static struct mips_frame_info {
  188. void *func;
  189. int omit_fp; /* compiled without fno-omit-frame-pointer */
  190. int frame_offset;
  191. int pc_offset;
  192. } schedule_frame, mfinfo[] = {
  193. { schedule, 0 }, /* must be first */
  194. /* arch/mips/kernel/semaphore.c */
  195. { __down, 1 },
  196. { __down_interruptible, 1 },
  197. /* kernel/sched.c */
  198. #ifdef CONFIG_PREEMPT
  199. { preempt_schedule, 0 },
  200. #endif
  201. { wait_for_completion, 0 },
  202. { interruptible_sleep_on, 0 },
  203. { interruptible_sleep_on_timeout, 0 },
  204. { sleep_on, 0 },
  205. { sleep_on_timeout, 0 },
  206. { yield, 0 },
  207. { io_schedule, 0 },
  208. { io_schedule_timeout, 0 },
  209. #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
  210. { __preempt_spin_lock, 0 },
  211. { __preempt_write_lock, 0 },
  212. #endif
  213. /* kernel/timer.c */
  214. { schedule_timeout, 1 },
  215. /* { nanosleep_restart, 1 }, */
  216. /* lib/rwsem-spinlock.c */
  217. { __down_read, 1 },
  218. { __down_write, 1 },
  219. };
  220. static int mips_frame_info_initialized;
  221. static int __init get_frame_info(struct mips_frame_info *info)
  222. {
  223. int i;
  224. void *func = info->func;
  225. union mips_instruction *ip = (union mips_instruction *)func;
  226. info->pc_offset = -1;
  227. info->frame_offset = info->omit_fp ? 0 : -1;
  228. for (i = 0; i < 128; i++, ip++) {
  229. /* if jal, jalr, jr, stop. */
  230. if (ip->j_format.opcode == jal_op ||
  231. (ip->r_format.opcode == spec_op &&
  232. (ip->r_format.func == jalr_op ||
  233. ip->r_format.func == jr_op)))
  234. break;
  235. if (
  236. #ifdef CONFIG_32BIT
  237. ip->i_format.opcode == sw_op &&
  238. #endif
  239. #ifdef CONFIG_64BIT
  240. ip->i_format.opcode == sd_op &&
  241. #endif
  242. ip->i_format.rs == 29)
  243. {
  244. /* sw / sd $ra, offset($sp) */
  245. if (ip->i_format.rt == 31) {
  246. if (info->pc_offset != -1)
  247. continue;
  248. info->pc_offset =
  249. ip->i_format.simmediate / sizeof(long);
  250. }
  251. /* sw / sd $s8, offset($sp) */
  252. if (ip->i_format.rt == 30) {
  253. //#if 0 /* gcc 3.4 does aggressive optimization... */
  254. if (info->frame_offset != -1)
  255. continue;
  256. //#endif
  257. info->frame_offset =
  258. ip->i_format.simmediate / sizeof(long);
  259. }
  260. }
  261. }
  262. if (info->pc_offset == -1 || info->frame_offset == -1) {
  263. printk("Can't analyze prologue code at %p\n", func);
  264. info->pc_offset = -1;
  265. info->frame_offset = -1;
  266. return -1;
  267. }
  268. return 0;
  269. }
  270. static int __init frame_info_init(void)
  271. {
  272. int i, found;
  273. for (i = 0; i < ARRAY_SIZE(mfinfo); i++)
  274. if (get_frame_info(&mfinfo[i]))
  275. return -1;
  276. schedule_frame = mfinfo[0];
  277. /* bubble sort */
  278. do {
  279. struct mips_frame_info tmp;
  280. found = 0;
  281. for (i = 1; i < ARRAY_SIZE(mfinfo); i++) {
  282. if (mfinfo[i-1].func > mfinfo[i].func) {
  283. tmp = mfinfo[i];
  284. mfinfo[i] = mfinfo[i-1];
  285. mfinfo[i-1] = tmp;
  286. found = 1;
  287. }
  288. }
  289. } while (found);
  290. mips_frame_info_initialized = 1;
  291. return 0;
  292. }
  293. arch_initcall(frame_info_init);
  294. /*
  295. * Return saved PC of a blocked thread.
  296. */
  297. unsigned long thread_saved_pc(struct task_struct *tsk)
  298. {
  299. struct thread_struct *t = &tsk->thread;
  300. /* New born processes are a special case */
  301. if (t->reg31 == (unsigned long) ret_from_fork)
  302. return t->reg31;
  303. if (schedule_frame.pc_offset < 0)
  304. return 0;
  305. return ((unsigned long *)t->reg29)[schedule_frame.pc_offset];
  306. }
  307. /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
  308. unsigned long get_wchan(struct task_struct *p)
  309. {
  310. unsigned long stack_page;
  311. unsigned long frame, pc;
  312. if (!p || p == current || p->state == TASK_RUNNING)
  313. return 0;
  314. stack_page = (unsigned long)p->thread_info;
  315. if (!stack_page || !mips_frame_info_initialized)
  316. return 0;
  317. pc = thread_saved_pc(p);
  318. if (!in_sched_functions(pc))
  319. return pc;
  320. frame = ((unsigned long *)p->thread.reg30)[schedule_frame.frame_offset];
  321. do {
  322. int i;
  323. if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32)
  324. return 0;
  325. for (i = ARRAY_SIZE(mfinfo) - 1; i >= 0; i--) {
  326. if (pc >= (unsigned long) mfinfo[i].func)
  327. break;
  328. }
  329. if (i < 0)
  330. break;
  331. if (mfinfo[i].omit_fp)
  332. break;
  333. pc = ((unsigned long *)frame)[mfinfo[i].pc_offset];
  334. frame = ((unsigned long *)frame)[mfinfo[i].frame_offset];
  335. } while (in_sched_functions(pc));
  336. return pc;
  337. }
  338. EXPORT_SYMBOL(get_wchan);