process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org)
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. * Copyright (C) 2004 Thiemo Seufer
  10. */
  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 <linux/kallsyms.h>
  28. #include <asm/bootinfo.h>
  29. #include <asm/cpu.h>
  30. #include <asm/dsp.h>
  31. #include <asm/fpu.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/system.h>
  34. #include <asm/mipsregs.h>
  35. #include <asm/processor.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. #include <asm/elf.h>
  39. #include <asm/isadep.h>
  40. #include <asm/inst.h>
  41. #include <asm/stacktrace.h>
  42. /*
  43. * The idle thread. There's no useful work to be done, so just try to conserve
  44. * power and have a low exit latency (ie sit in a loop waiting for somebody to
  45. * say that they'd like to reschedule)
  46. */
  47. ATTRIB_NORET void cpu_idle(void)
  48. {
  49. /* endless idle loop with no priority at all */
  50. while (1) {
  51. while (!need_resched()) {
  52. #ifdef CONFIG_MIPS_MT_SMTC
  53. extern void smtc_idle_loop_hook(void);
  54. smtc_idle_loop_hook();
  55. #endif /* CONFIG_MIPS_MT_SMTC */
  56. if (cpu_wait)
  57. (*cpu_wait)();
  58. }
  59. preempt_enable_no_resched();
  60. schedule();
  61. preempt_disable();
  62. }
  63. }
  64. asmlinkage void ret_from_fork(void);
  65. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  66. {
  67. unsigned long status;
  68. /* New thread loses kernel privileges. */
  69. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
  70. #ifdef CONFIG_64BIT
  71. status &= ~ST0_FR;
  72. status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
  73. #endif
  74. status |= KU_USER;
  75. regs->cp0_status = status;
  76. clear_used_math();
  77. clear_fpu_owner();
  78. if (cpu_has_dsp)
  79. __init_dsp();
  80. regs->cp0_epc = pc;
  81. regs->regs[29] = sp;
  82. current_thread_info()->addr_limit = USER_DS;
  83. }
  84. void exit_thread(void)
  85. {
  86. }
  87. void flush_thread(void)
  88. {
  89. }
  90. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  91. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  92. {
  93. struct thread_info *ti = task_thread_info(p);
  94. struct pt_regs *childregs;
  95. long childksp;
  96. p->set_child_tid = p->clear_child_tid = NULL;
  97. childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
  98. preempt_disable();
  99. if (is_fpu_owner())
  100. save_fp(p);
  101. if (cpu_has_dsp)
  102. save_dsp(p);
  103. preempt_enable();
  104. /* set up new TSS. */
  105. childregs = (struct pt_regs *) childksp - 1;
  106. *childregs = *regs;
  107. childregs->regs[7] = 0; /* Clear error flag */
  108. #if defined(CONFIG_BINFMT_IRIX)
  109. if (current->personality != PER_LINUX) {
  110. /* Under IRIX things are a little different. */
  111. childregs->regs[3] = 1;
  112. regs->regs[3] = 0;
  113. }
  114. #endif
  115. childregs->regs[2] = 0; /* Child gets zero as return value */
  116. regs->regs[2] = p->pid;
  117. if (childregs->cp0_status & ST0_CU0) {
  118. childregs->regs[28] = (unsigned long) ti;
  119. childregs->regs[29] = childksp;
  120. ti->addr_limit = KERNEL_DS;
  121. } else {
  122. childregs->regs[29] = usp;
  123. ti->addr_limit = USER_DS;
  124. }
  125. p->thread.reg29 = (unsigned long) childregs;
  126. p->thread.reg31 = (unsigned long) ret_from_fork;
  127. /*
  128. * New tasks lose permission to use the fpu. This accelerates context
  129. * switching for most programs since they don't use the fpu.
  130. */
  131. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  132. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  133. clear_tsk_thread_flag(p, TIF_USEDFPU);
  134. #ifdef CONFIG_MIPS_MT_FPAFF
  135. /*
  136. * FPU affinity support is cleaner if we track the
  137. * user-visible CPU affinity from the very beginning.
  138. * The generic cpus_allowed mask will already have
  139. * been copied from the parent before copy_thread
  140. * is invoked.
  141. */
  142. p->thread.user_cpus_allowed = p->cpus_allowed;
  143. #endif /* CONFIG_MIPS_MT_FPAFF */
  144. if (clone_flags & CLONE_SETTLS)
  145. ti->tp_value = regs->regs[7];
  146. return 0;
  147. }
  148. /* Fill in the fpu structure for a core dump.. */
  149. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  150. {
  151. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  152. return 1;
  153. }
  154. void elf_dump_regs(elf_greg_t *gp, struct pt_regs *regs)
  155. {
  156. int i;
  157. for (i = 0; i < EF_R0; i++)
  158. gp[i] = 0;
  159. gp[EF_R0] = 0;
  160. for (i = 1; i <= 31; i++)
  161. gp[EF_R0 + i] = regs->regs[i];
  162. gp[EF_R26] = 0;
  163. gp[EF_R27] = 0;
  164. gp[EF_LO] = regs->lo;
  165. gp[EF_HI] = regs->hi;
  166. gp[EF_CP0_EPC] = regs->cp0_epc;
  167. gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  168. gp[EF_CP0_STATUS] = regs->cp0_status;
  169. gp[EF_CP0_CAUSE] = regs->cp0_cause;
  170. #ifdef EF_UNUSED0
  171. gp[EF_UNUSED0] = 0;
  172. #endif
  173. }
  174. int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs)
  175. {
  176. elf_dump_regs(*regs, task_pt_regs(tsk));
  177. return 1;
  178. }
  179. int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
  180. {
  181. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  182. return 1;
  183. }
  184. /*
  185. * Create a kernel thread
  186. */
  187. ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
  188. {
  189. do_exit(fn(arg));
  190. }
  191. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  192. {
  193. struct pt_regs regs;
  194. memset(&regs, 0, sizeof(regs));
  195. regs.regs[4] = (unsigned long) arg;
  196. regs.regs[5] = (unsigned long) fn;
  197. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  198. regs.cp0_status = read_c0_status();
  199. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  200. regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
  201. regs.cp0_status |= ST0_IEP;
  202. #else
  203. regs.cp0_status |= ST0_EXL;
  204. #endif
  205. /* Ok, create the new process.. */
  206. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  207. }
  208. /*
  209. *
  210. */
  211. struct mips_frame_info {
  212. void *func;
  213. unsigned long func_size;
  214. int frame_size;
  215. int pc_offset;
  216. };
  217. static inline int is_ra_save_ins(union mips_instruction *ip)
  218. {
  219. /* sw / sd $ra, offset($sp) */
  220. return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
  221. ip->i_format.rs == 29 &&
  222. ip->i_format.rt == 31;
  223. }
  224. static inline int is_jal_jalr_jr_ins(union mips_instruction *ip)
  225. {
  226. if (ip->j_format.opcode == jal_op)
  227. return 1;
  228. if (ip->r_format.opcode != spec_op)
  229. return 0;
  230. return ip->r_format.func == jalr_op || ip->r_format.func == jr_op;
  231. }
  232. static inline int is_sp_move_ins(union mips_instruction *ip)
  233. {
  234. /* addiu/daddiu sp,sp,-imm */
  235. if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
  236. return 0;
  237. if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
  238. return 1;
  239. return 0;
  240. }
  241. static int get_frame_info(struct mips_frame_info *info)
  242. {
  243. union mips_instruction *ip = info->func;
  244. unsigned max_insns = info->func_size / sizeof(union mips_instruction);
  245. unsigned i;
  246. info->pc_offset = -1;
  247. info->frame_size = 0;
  248. if (!ip)
  249. goto err;
  250. if (max_insns == 0)
  251. max_insns = 128U; /* unknown function size */
  252. max_insns = min(128U, max_insns);
  253. for (i = 0; i < max_insns; i++, ip++) {
  254. if (is_jal_jalr_jr_ins(ip))
  255. break;
  256. if (!info->frame_size) {
  257. if (is_sp_move_ins(ip))
  258. info->frame_size = - ip->i_format.simmediate;
  259. continue;
  260. }
  261. if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
  262. info->pc_offset =
  263. ip->i_format.simmediate / sizeof(long);
  264. break;
  265. }
  266. }
  267. if (info->frame_size && info->pc_offset >= 0) /* nested */
  268. return 0;
  269. if (info->pc_offset < 0) /* leaf */
  270. return 1;
  271. /* prologue seems boggus... */
  272. err:
  273. return -1;
  274. }
  275. static struct mips_frame_info schedule_mfi __read_mostly;
  276. static int __init frame_info_init(void)
  277. {
  278. unsigned long size = 0;
  279. #ifdef CONFIG_KALLSYMS
  280. unsigned long ofs;
  281. kallsyms_lookup_size_offset((unsigned long)schedule, &size, &ofs);
  282. #endif
  283. schedule_mfi.func = schedule;
  284. schedule_mfi.func_size = size;
  285. get_frame_info(&schedule_mfi);
  286. /*
  287. * Without schedule() frame info, result given by
  288. * thread_saved_pc() and get_wchan() are not reliable.
  289. */
  290. if (schedule_mfi.pc_offset < 0)
  291. printk("Can't analyze schedule() prologue at %p\n", schedule);
  292. return 0;
  293. }
  294. arch_initcall(frame_info_init);
  295. /*
  296. * Return saved PC of a blocked thread.
  297. */
  298. unsigned long thread_saved_pc(struct task_struct *tsk)
  299. {
  300. struct thread_struct *t = &tsk->thread;
  301. /* New born processes are a special case */
  302. if (t->reg31 == (unsigned long) ret_from_fork)
  303. return t->reg31;
  304. if (schedule_mfi.pc_offset < 0)
  305. return 0;
  306. return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
  307. }
  308. #ifdef CONFIG_KALLSYMS
  309. /* used by show_backtrace() */
  310. unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
  311. unsigned long pc, unsigned long *ra)
  312. {
  313. unsigned long stack_page;
  314. struct mips_frame_info info;
  315. unsigned long size, ofs;
  316. int leaf;
  317. extern void ret_from_irq(void);
  318. extern void ret_from_exception(void);
  319. stack_page = (unsigned long)task_stack_page(task);
  320. if (!stack_page)
  321. return 0;
  322. /*
  323. * If we reached the bottom of interrupt context,
  324. * return saved pc in pt_regs.
  325. */
  326. if (pc == (unsigned long)ret_from_irq ||
  327. pc == (unsigned long)ret_from_exception) {
  328. struct pt_regs *regs;
  329. if (*sp >= stack_page &&
  330. *sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) {
  331. regs = (struct pt_regs *)*sp;
  332. pc = regs->cp0_epc;
  333. if (__kernel_text_address(pc)) {
  334. *sp = regs->regs[29];
  335. *ra = regs->regs[31];
  336. return pc;
  337. }
  338. }
  339. return 0;
  340. }
  341. if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
  342. return 0;
  343. /*
  344. * Return ra if an exception occured at the first instruction
  345. */
  346. if (unlikely(ofs == 0)) {
  347. pc = *ra;
  348. *ra = 0;
  349. return pc;
  350. }
  351. info.func = (void *)(pc - ofs);
  352. info.func_size = ofs; /* analyze from start to ofs */
  353. leaf = get_frame_info(&info);
  354. if (leaf < 0)
  355. return 0;
  356. if (*sp < stack_page ||
  357. *sp + info.frame_size > stack_page + THREAD_SIZE - 32)
  358. return 0;
  359. if (leaf)
  360. /*
  361. * For some extreme cases, get_frame_info() can
  362. * consider wrongly a nested function as a leaf
  363. * one. In that cases avoid to return always the
  364. * same value.
  365. */
  366. pc = pc != *ra ? *ra : 0;
  367. else
  368. pc = ((unsigned long *)(*sp))[info.pc_offset];
  369. *sp += info.frame_size;
  370. *ra = 0;
  371. return __kernel_text_address(pc) ? pc : 0;
  372. }
  373. #endif
  374. /*
  375. * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
  376. */
  377. unsigned long get_wchan(struct task_struct *task)
  378. {
  379. unsigned long pc = 0;
  380. #ifdef CONFIG_KALLSYMS
  381. unsigned long sp;
  382. unsigned long ra = 0;
  383. #endif
  384. if (!task || task == current || task->state == TASK_RUNNING)
  385. goto out;
  386. if (!task_stack_page(task))
  387. goto out;
  388. pc = thread_saved_pc(task);
  389. #ifdef CONFIG_KALLSYMS
  390. sp = task->thread.reg29 + schedule_mfi.frame_size;
  391. while (in_sched_functions(pc))
  392. pc = unwind_stack(task, &sp, pc, &ra);
  393. #endif
  394. out:
  395. return pc;
  396. }