process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #ifdef CONFIG_MIPS_MT_SMTC
  43. #include <asm/mipsmtregs.h>
  44. extern void smtc_idle_loop_hook(void);
  45. #endif /* CONFIG_MIPS_MT_SMTC */
  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. #ifdef CONFIG_MIPS_MT_SMTC
  57. smtc_idle_loop_hook();
  58. #endif /* CONFIG_MIPS_MT_SMTC */
  59. if (cpu_wait)
  60. (*cpu_wait)();
  61. }
  62. preempt_enable_no_resched();
  63. schedule();
  64. preempt_disable();
  65. }
  66. }
  67. /*
  68. * Native o32 and N64 ABI without DSP ASE
  69. */
  70. struct mips_abi mips_abi = {
  71. .do_signal = do_signal,
  72. #ifdef CONFIG_TRAD_SIGNALS
  73. .setup_frame = setup_frame,
  74. #endif
  75. .setup_rt_frame = setup_rt_frame
  76. };
  77. #ifdef CONFIG_MIPS32_O32
  78. /*
  79. * o32 compatibility on 64-bit kernels, without DSP ASE
  80. */
  81. struct mips_abi mips_abi_32 = {
  82. .do_signal = do_signal32,
  83. .setup_frame = setup_frame_32,
  84. .setup_rt_frame = setup_rt_frame_32
  85. };
  86. #endif /* CONFIG_MIPS32_O32 */
  87. #ifdef CONFIG_MIPS32_N32
  88. /*
  89. * N32 on 64-bit kernels, without DSP ASE
  90. */
  91. struct mips_abi mips_abi_n32 = {
  92. .do_signal = do_signal,
  93. .setup_rt_frame = setup_rt_frame_n32
  94. };
  95. #endif /* CONFIG_MIPS32_N32 */
  96. asmlinkage void ret_from_fork(void);
  97. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  98. {
  99. unsigned long status;
  100. /* New thread loses kernel privileges. */
  101. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|KU_MASK);
  102. #ifdef CONFIG_64BIT
  103. status &= ~ST0_FR;
  104. status |= (current->thread.mflags & MF_32BIT_REGS) ? 0 : ST0_FR;
  105. #endif
  106. status |= KU_USER;
  107. regs->cp0_status = status;
  108. clear_used_math();
  109. lose_fpu();
  110. if (cpu_has_dsp)
  111. __init_dsp();
  112. regs->cp0_epc = pc;
  113. regs->regs[29] = sp;
  114. current_thread_info()->addr_limit = USER_DS;
  115. }
  116. void exit_thread(void)
  117. {
  118. }
  119. void flush_thread(void)
  120. {
  121. }
  122. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  123. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  124. {
  125. struct thread_info *ti = task_thread_info(p);
  126. struct pt_regs *childregs;
  127. long childksp;
  128. p->set_child_tid = p->clear_child_tid = NULL;
  129. childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
  130. preempt_disable();
  131. if (is_fpu_owner())
  132. save_fp(p);
  133. if (cpu_has_dsp)
  134. save_dsp(p);
  135. preempt_enable();
  136. /* set up new TSS. */
  137. childregs = (struct pt_regs *) childksp - 1;
  138. *childregs = *regs;
  139. childregs->regs[7] = 0; /* Clear error flag */
  140. #if defined(CONFIG_BINFMT_IRIX)
  141. if (current->personality != PER_LINUX) {
  142. /* Under IRIX things are a little different. */
  143. childregs->regs[3] = 1;
  144. regs->regs[3] = 0;
  145. }
  146. #endif
  147. childregs->regs[2] = 0; /* Child gets zero as return value */
  148. regs->regs[2] = p->pid;
  149. if (childregs->cp0_status & ST0_CU0) {
  150. childregs->regs[28] = (unsigned long) ti;
  151. childregs->regs[29] = childksp;
  152. ti->addr_limit = KERNEL_DS;
  153. } else {
  154. childregs->regs[29] = usp;
  155. ti->addr_limit = USER_DS;
  156. }
  157. p->thread.reg29 = (unsigned long) childregs;
  158. p->thread.reg31 = (unsigned long) ret_from_fork;
  159. /*
  160. * New tasks lose permission to use the fpu. This accelerates context
  161. * switching for most programs since they don't use the fpu.
  162. */
  163. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  164. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  165. clear_tsk_thread_flag(p, TIF_USEDFPU);
  166. #ifdef CONFIG_MIPS_MT_FPAFF
  167. /*
  168. * FPU affinity support is cleaner if we track the
  169. * user-visible CPU affinity from the very beginning.
  170. * The generic cpus_allowed mask will already have
  171. * been copied from the parent before copy_thread
  172. * is invoked.
  173. */
  174. p->thread.user_cpus_allowed = p->cpus_allowed;
  175. #endif /* CONFIG_MIPS_MT_FPAFF */
  176. if (clone_flags & CLONE_SETTLS)
  177. ti->tp_value = regs->regs[7];
  178. return 0;
  179. }
  180. /* Fill in the fpu structure for a core dump.. */
  181. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  182. {
  183. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  184. return 1;
  185. }
  186. void elf_dump_regs(elf_greg_t *gp, struct pt_regs *regs)
  187. {
  188. int i;
  189. for (i = 0; i < EF_R0; i++)
  190. gp[i] = 0;
  191. gp[EF_R0] = 0;
  192. for (i = 1; i <= 31; i++)
  193. gp[EF_R0 + i] = regs->regs[i];
  194. gp[EF_R26] = 0;
  195. gp[EF_R27] = 0;
  196. gp[EF_LO] = regs->lo;
  197. gp[EF_HI] = regs->hi;
  198. gp[EF_CP0_EPC] = regs->cp0_epc;
  199. gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  200. gp[EF_CP0_STATUS] = regs->cp0_status;
  201. gp[EF_CP0_CAUSE] = regs->cp0_cause;
  202. #ifdef EF_UNUSED0
  203. gp[EF_UNUSED0] = 0;
  204. #endif
  205. }
  206. int dump_task_regs (struct task_struct *tsk, elf_gregset_t *regs)
  207. {
  208. elf_dump_regs(*regs, task_pt_regs(tsk));
  209. return 1;
  210. }
  211. int dump_task_fpu (struct task_struct *t, elf_fpregset_t *fpr)
  212. {
  213. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  214. return 1;
  215. }
  216. /*
  217. * Create a kernel thread
  218. */
  219. ATTRIB_NORET void kernel_thread_helper(void *arg, int (*fn)(void *))
  220. {
  221. do_exit(fn(arg));
  222. }
  223. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  224. {
  225. struct pt_regs regs;
  226. memset(&regs, 0, sizeof(regs));
  227. regs.regs[4] = (unsigned long) arg;
  228. regs.regs[5] = (unsigned long) fn;
  229. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  230. regs.cp0_status = read_c0_status();
  231. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  232. regs.cp0_status &= ~(ST0_KUP | ST0_IEC);
  233. regs.cp0_status |= ST0_IEP;
  234. #else
  235. regs.cp0_status |= ST0_EXL;
  236. #endif
  237. /* Ok, create the new process.. */
  238. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  239. }
  240. static struct mips_frame_info {
  241. void *func;
  242. unsigned long func_size;
  243. int frame_size;
  244. int pc_offset;
  245. } *schedule_frame, mfinfo[64];
  246. static int mfinfo_num;
  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. int i, max_insns =
  275. min(128UL, info->func_size / sizeof(union mips_instruction));
  276. info->pc_offset = -1;
  277. info->frame_size = 0;
  278. for (i = 0; i < max_insns; i++, ip++) {
  279. if (is_jal_jalr_jr_ins(ip))
  280. break;
  281. if (!info->frame_size) {
  282. if (is_sp_move_ins(ip))
  283. info->frame_size = - ip->i_format.simmediate;
  284. continue;
  285. }
  286. if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
  287. info->pc_offset =
  288. ip->i_format.simmediate / sizeof(long);
  289. break;
  290. }
  291. }
  292. if (info->frame_size && info->pc_offset >= 0) /* nested */
  293. return 0;
  294. if (info->pc_offset < 0) /* leaf */
  295. return 1;
  296. /* prologue seems boggus... */
  297. return -1;
  298. }
  299. static int __init frame_info_init(void)
  300. {
  301. int i;
  302. #ifdef CONFIG_KALLSYMS
  303. char *modname;
  304. char namebuf[KSYM_NAME_LEN + 1];
  305. unsigned long start, size, ofs;
  306. extern char __sched_text_start[], __sched_text_end[];
  307. extern char __lock_text_start[], __lock_text_end[];
  308. start = (unsigned long)__sched_text_start;
  309. for (i = 0; i < ARRAY_SIZE(mfinfo); i++) {
  310. if (start == (unsigned long)schedule)
  311. schedule_frame = &mfinfo[i];
  312. if (!kallsyms_lookup(start, &size, &ofs, &modname, namebuf))
  313. break;
  314. mfinfo[i].func = (void *)(start + ofs);
  315. mfinfo[i].func_size = size;
  316. start += size - ofs;
  317. if (start >= (unsigned long)__lock_text_end)
  318. break;
  319. if (start == (unsigned long)__sched_text_end)
  320. start = (unsigned long)__lock_text_start;
  321. }
  322. #else
  323. mfinfo[0].func = schedule;
  324. schedule_frame = &mfinfo[0];
  325. #endif
  326. for (i = 0; i < ARRAY_SIZE(mfinfo) && mfinfo[i].func; i++)
  327. get_frame_info(mfinfo + i);
  328. /*
  329. * Without schedule() frame info, result given by
  330. * thread_saved_pc() and get_wchan() are not reliable.
  331. */
  332. if (schedule_frame->pc_offset < 0)
  333. printk("Can't analyze schedule() prologue at %p\n", schedule);
  334. mfinfo_num = i;
  335. return 0;
  336. }
  337. arch_initcall(frame_info_init);
  338. /*
  339. * Return saved PC of a blocked thread.
  340. */
  341. unsigned long thread_saved_pc(struct task_struct *tsk)
  342. {
  343. struct thread_struct *t = &tsk->thread;
  344. /* New born processes are a special case */
  345. if (t->reg31 == (unsigned long) ret_from_fork)
  346. return t->reg31;
  347. if (!schedule_frame || schedule_frame->pc_offset < 0)
  348. return 0;
  349. return ((unsigned long *)t->reg29)[schedule_frame->pc_offset];
  350. }
  351. /* get_wchan - a maintenance nightmare^W^Wpain in the ass ... */
  352. unsigned long get_wchan(struct task_struct *p)
  353. {
  354. unsigned long stack_page;
  355. unsigned long pc;
  356. #ifdef CONFIG_KALLSYMS
  357. unsigned long frame;
  358. #endif
  359. if (!p || p == current || p->state == TASK_RUNNING)
  360. return 0;
  361. stack_page = (unsigned long)task_stack_page(p);
  362. if (!stack_page || !mfinfo_num)
  363. return 0;
  364. pc = thread_saved_pc(p);
  365. #ifdef CONFIG_KALLSYMS
  366. if (!in_sched_functions(pc))
  367. return pc;
  368. frame = p->thread.reg29 + schedule_frame->frame_size;
  369. do {
  370. int i;
  371. if (frame < stack_page || frame > stack_page + THREAD_SIZE - 32)
  372. return 0;
  373. for (i = mfinfo_num - 1; i >= 0; i--) {
  374. if (pc >= (unsigned long) mfinfo[i].func)
  375. break;
  376. }
  377. if (i < 0)
  378. break;
  379. if (mfinfo[i].pc_offset < 0)
  380. break;
  381. pc = ((unsigned long *)frame)[mfinfo[i].pc_offset];
  382. if (!mfinfo[i].frame_size)
  383. break;
  384. frame += mfinfo[i].frame_size;
  385. } while (in_sched_functions(pc));
  386. #endif
  387. return pc;
  388. }
  389. #ifdef CONFIG_KALLSYMS
  390. /* used by show_frametrace() */
  391. unsigned long unwind_stack(struct task_struct *task,
  392. unsigned long **sp, unsigned long pc)
  393. {
  394. unsigned long stack_page;
  395. struct mips_frame_info info;
  396. char *modname;
  397. char namebuf[KSYM_NAME_LEN + 1];
  398. unsigned long size, ofs;
  399. stack_page = (unsigned long)task_stack_page(task);
  400. if (!stack_page)
  401. return 0;
  402. if (!kallsyms_lookup(pc, &size, &ofs, &modname, namebuf))
  403. return 0;
  404. if (ofs == 0)
  405. return 0;
  406. info.func = (void *)(pc - ofs);
  407. info.func_size = ofs; /* analyze from start to ofs */
  408. if (get_frame_info(&info)) {
  409. /* leaf or unknown */
  410. *sp += info.frame_size / sizeof(long);
  411. return 0;
  412. }
  413. if ((unsigned long)*sp < stack_page ||
  414. (unsigned long)*sp + info.frame_size / sizeof(long) >
  415. stack_page + THREAD_SIZE - 32)
  416. return 0;
  417. pc = (*sp)[info.pc_offset];
  418. *sp += info.frame_size / sizeof(long);
  419. return pc;
  420. }
  421. #endif