process.c 11 KB

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