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/tick.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/stddef.h>
  18. #include <linux/unistd.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/mman.h>
  21. #include <linux/personality.h>
  22. #include <linux/sys.h>
  23. #include <linux/user.h>
  24. #include <linux/init.h>
  25. #include <linux/completion.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/random.h>
  28. #include <asm/asm.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. void __noreturn cpu_idle(void)
  49. {
  50. int cpu;
  51. /* CPU is going idle. */
  52. cpu = smp_processor_id();
  53. /* endless idle loop with no priority at all */
  54. while (1) {
  55. tick_nohz_stop_sched_tick(1);
  56. while (!need_resched() && cpu_online(cpu)) {
  57. #ifdef CONFIG_MIPS_MT_SMTC
  58. extern void smtc_idle_loop_hook(void);
  59. smtc_idle_loop_hook();
  60. #endif
  61. if (cpu_wait) {
  62. /* Don't trace irqs off for idle */
  63. stop_critical_timings();
  64. (*cpu_wait)();
  65. start_critical_timings();
  66. }
  67. }
  68. #ifdef CONFIG_HOTPLUG_CPU
  69. if (!cpu_online(cpu) && !cpu_isset(cpu, cpu_callin_map) &&
  70. (system_state == SYSTEM_RUNNING ||
  71. system_state == SYSTEM_BOOTING))
  72. play_dead();
  73. #endif
  74. tick_nohz_restart_sched_tick();
  75. preempt_enable_no_resched();
  76. schedule();
  77. preempt_disable();
  78. }
  79. }
  80. asmlinkage void ret_from_fork(void);
  81. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  82. {
  83. unsigned long status;
  84. /* New thread loses kernel privileges. */
  85. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK);
  86. #ifdef CONFIG_64BIT
  87. status |= test_thread_flag(TIF_32BIT_REGS) ? 0 : ST0_FR;
  88. #endif
  89. status |= KU_USER;
  90. regs->cp0_status = status;
  91. clear_used_math();
  92. clear_fpu_owner();
  93. if (cpu_has_dsp)
  94. __init_dsp();
  95. regs->cp0_epc = pc;
  96. regs->regs[29] = sp;
  97. }
  98. void exit_thread(void)
  99. {
  100. }
  101. void flush_thread(void)
  102. {
  103. }
  104. int copy_thread(unsigned long clone_flags, unsigned long usp,
  105. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  106. {
  107. struct thread_info *ti = task_thread_info(p);
  108. struct pt_regs *childregs;
  109. unsigned long childksp;
  110. p->set_child_tid = p->clear_child_tid = NULL;
  111. childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
  112. preempt_disable();
  113. if (is_fpu_owner())
  114. save_fp(p);
  115. if (cpu_has_dsp)
  116. save_dsp(p);
  117. preempt_enable();
  118. /* set up new TSS. */
  119. childregs = (struct pt_regs *) childksp - 1;
  120. /* Put the stack after the struct pt_regs. */
  121. childksp = (unsigned long) childregs;
  122. *childregs = *regs;
  123. childregs->regs[7] = 0; /* Clear error flag */
  124. childregs->regs[2] = 0; /* Child gets zero as return value */
  125. if (childregs->cp0_status & ST0_CU0) {
  126. childregs->regs[28] = (unsigned long) ti;
  127. childregs->regs[29] = childksp;
  128. ti->addr_limit = KERNEL_DS;
  129. } else {
  130. childregs->regs[29] = usp;
  131. ti->addr_limit = USER_DS;
  132. }
  133. p->thread.reg29 = (unsigned long) childregs;
  134. p->thread.reg31 = (unsigned long) ret_from_fork;
  135. /*
  136. * New tasks lose permission to use the fpu. This accelerates context
  137. * switching for most programs since they don't use the fpu.
  138. */
  139. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  140. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  141. #ifdef CONFIG_MIPS_MT_SMTC
  142. /*
  143. * SMTC restores TCStatus after Status, and the CU bits
  144. * are aliased there.
  145. */
  146. childregs->cp0_tcstatus &= ~(ST0_CU2|ST0_CU1);
  147. #endif
  148. clear_tsk_thread_flag(p, TIF_USEDFPU);
  149. #ifdef CONFIG_MIPS_MT_FPAFF
  150. clear_tsk_thread_flag(p, TIF_FPUBOUND);
  151. #endif /* CONFIG_MIPS_MT_FPAFF */
  152. if (clone_flags & CLONE_SETTLS)
  153. ti->tp_value = regs->regs[7];
  154. return 0;
  155. }
  156. /* Fill in the fpu structure for a core dump.. */
  157. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  158. {
  159. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  160. return 1;
  161. }
  162. void elf_dump_regs(elf_greg_t *gp, struct pt_regs *regs)
  163. {
  164. int i;
  165. for (i = 0; i < EF_R0; i++)
  166. gp[i] = 0;
  167. gp[EF_R0] = 0;
  168. for (i = 1; i <= 31; i++)
  169. gp[EF_R0 + i] = regs->regs[i];
  170. gp[EF_R26] = 0;
  171. gp[EF_R27] = 0;
  172. gp[EF_LO] = regs->lo;
  173. gp[EF_HI] = regs->hi;
  174. gp[EF_CP0_EPC] = regs->cp0_epc;
  175. gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  176. gp[EF_CP0_STATUS] = regs->cp0_status;
  177. gp[EF_CP0_CAUSE] = regs->cp0_cause;
  178. #ifdef EF_UNUSED0
  179. gp[EF_UNUSED0] = 0;
  180. #endif
  181. }
  182. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  183. {
  184. elf_dump_regs(*regs, task_pt_regs(tsk));
  185. return 1;
  186. }
  187. int dump_task_fpu(struct task_struct *t, elf_fpregset_t *fpr)
  188. {
  189. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  190. return 1;
  191. }
  192. /*
  193. * Create a kernel thread
  194. */
  195. static void __noreturn kernel_thread_helper(void *arg, int (*fn)(void *))
  196. {
  197. do_exit(fn(arg));
  198. }
  199. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  200. {
  201. struct pt_regs regs;
  202. memset(&regs, 0, sizeof(regs));
  203. regs.regs[4] = (unsigned long) arg;
  204. regs.regs[5] = (unsigned long) fn;
  205. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  206. regs.cp0_status = read_c0_status();
  207. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  208. regs.cp0_status = (regs.cp0_status & ~(ST0_KUP | ST0_IEP | ST0_IEC)) |
  209. ((regs.cp0_status & (ST0_KUC | ST0_IEC)) << 2);
  210. #else
  211. regs.cp0_status |= ST0_EXL;
  212. #endif
  213. /* Ok, create the new process.. */
  214. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  215. }
  216. /*
  217. *
  218. */
  219. struct mips_frame_info {
  220. void *func;
  221. unsigned long func_size;
  222. int frame_size;
  223. int pc_offset;
  224. };
  225. static inline int is_ra_save_ins(union mips_instruction *ip)
  226. {
  227. /* sw / sd $ra, offset($sp) */
  228. return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
  229. ip->i_format.rs == 29 &&
  230. ip->i_format.rt == 31;
  231. }
  232. static inline int is_jal_jalr_jr_ins(union mips_instruction *ip)
  233. {
  234. if (ip->j_format.opcode == jal_op)
  235. return 1;
  236. if (ip->r_format.opcode != spec_op)
  237. return 0;
  238. return ip->r_format.func == jalr_op || ip->r_format.func == jr_op;
  239. }
  240. static inline int is_sp_move_ins(union mips_instruction *ip)
  241. {
  242. /* addiu/daddiu sp,sp,-imm */
  243. if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
  244. return 0;
  245. if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
  246. return 1;
  247. return 0;
  248. }
  249. static int get_frame_info(struct mips_frame_info *info)
  250. {
  251. union mips_instruction *ip = info->func;
  252. unsigned max_insns = info->func_size / sizeof(union mips_instruction);
  253. unsigned i;
  254. info->pc_offset = -1;
  255. info->frame_size = 0;
  256. if (!ip)
  257. goto err;
  258. if (max_insns == 0)
  259. max_insns = 128U; /* unknown function size */
  260. max_insns = min(128U, max_insns);
  261. for (i = 0; i < max_insns; i++, ip++) {
  262. if (is_jal_jalr_jr_ins(ip))
  263. break;
  264. if (!info->frame_size) {
  265. if (is_sp_move_ins(ip))
  266. info->frame_size = - ip->i_format.simmediate;
  267. continue;
  268. }
  269. if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
  270. info->pc_offset =
  271. ip->i_format.simmediate / sizeof(long);
  272. break;
  273. }
  274. }
  275. if (info->frame_size && info->pc_offset >= 0) /* nested */
  276. return 0;
  277. if (info->pc_offset < 0) /* leaf */
  278. return 1;
  279. /* prologue seems boggus... */
  280. err:
  281. return -1;
  282. }
  283. static struct mips_frame_info schedule_mfi __read_mostly;
  284. static int __init frame_info_init(void)
  285. {
  286. unsigned long size = 0;
  287. #ifdef CONFIG_KALLSYMS
  288. unsigned long ofs;
  289. kallsyms_lookup_size_offset((unsigned long)schedule, &size, &ofs);
  290. #endif
  291. schedule_mfi.func = schedule;
  292. schedule_mfi.func_size = size;
  293. get_frame_info(&schedule_mfi);
  294. /*
  295. * Without schedule() frame info, result given by
  296. * thread_saved_pc() and get_wchan() are not reliable.
  297. */
  298. if (schedule_mfi.pc_offset < 0)
  299. printk("Can't analyze schedule() prologue at %p\n", schedule);
  300. return 0;
  301. }
  302. arch_initcall(frame_info_init);
  303. /*
  304. * Return saved PC of a blocked thread.
  305. */
  306. unsigned long thread_saved_pc(struct task_struct *tsk)
  307. {
  308. struct thread_struct *t = &tsk->thread;
  309. /* New born processes are a special case */
  310. if (t->reg31 == (unsigned long) ret_from_fork)
  311. return t->reg31;
  312. if (schedule_mfi.pc_offset < 0)
  313. return 0;
  314. return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
  315. }
  316. #ifdef CONFIG_KALLSYMS
  317. /* generic stack unwinding function */
  318. unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
  319. unsigned long *sp,
  320. unsigned long pc,
  321. unsigned long *ra)
  322. {
  323. struct mips_frame_info info;
  324. unsigned long size, ofs;
  325. int leaf;
  326. extern void ret_from_irq(void);
  327. extern void ret_from_exception(void);
  328. if (!stack_page)
  329. return 0;
  330. /*
  331. * If we reached the bottom of interrupt context,
  332. * return saved pc in pt_regs.
  333. */
  334. if (pc == (unsigned long)ret_from_irq ||
  335. pc == (unsigned long)ret_from_exception) {
  336. struct pt_regs *regs;
  337. if (*sp >= stack_page &&
  338. *sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) {
  339. regs = (struct pt_regs *)*sp;
  340. pc = regs->cp0_epc;
  341. if (__kernel_text_address(pc)) {
  342. *sp = regs->regs[29];
  343. *ra = regs->regs[31];
  344. return pc;
  345. }
  346. }
  347. return 0;
  348. }
  349. if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
  350. return 0;
  351. /*
  352. * Return ra if an exception occurred at the first instruction
  353. */
  354. if (unlikely(ofs == 0)) {
  355. pc = *ra;
  356. *ra = 0;
  357. return pc;
  358. }
  359. info.func = (void *)(pc - ofs);
  360. info.func_size = ofs; /* analyze from start to ofs */
  361. leaf = get_frame_info(&info);
  362. if (leaf < 0)
  363. return 0;
  364. if (*sp < stack_page ||
  365. *sp + info.frame_size > stack_page + THREAD_SIZE - 32)
  366. return 0;
  367. if (leaf)
  368. /*
  369. * For some extreme cases, get_frame_info() can
  370. * consider wrongly a nested function as a leaf
  371. * one. In that cases avoid to return always the
  372. * same value.
  373. */
  374. pc = pc != *ra ? *ra : 0;
  375. else
  376. pc = ((unsigned long *)(*sp))[info.pc_offset];
  377. *sp += info.frame_size;
  378. *ra = 0;
  379. return __kernel_text_address(pc) ? pc : 0;
  380. }
  381. EXPORT_SYMBOL(unwind_stack_by_address);
  382. /* used by show_backtrace() */
  383. unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
  384. unsigned long pc, unsigned long *ra)
  385. {
  386. unsigned long stack_page = (unsigned long)task_stack_page(task);
  387. return unwind_stack_by_address(stack_page, sp, pc, ra);
  388. }
  389. #endif
  390. /*
  391. * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
  392. */
  393. unsigned long get_wchan(struct task_struct *task)
  394. {
  395. unsigned long pc = 0;
  396. #ifdef CONFIG_KALLSYMS
  397. unsigned long sp;
  398. unsigned long ra = 0;
  399. #endif
  400. if (!task || task == current || task->state == TASK_RUNNING)
  401. goto out;
  402. if (!task_stack_page(task))
  403. goto out;
  404. pc = thread_saved_pc(task);
  405. #ifdef CONFIG_KALLSYMS
  406. sp = task->thread.reg29 + schedule_mfi.frame_size;
  407. while (in_sched_functions(pc))
  408. pc = unwind_stack(task, &sp, pc, &ra);
  409. #endif
  410. out:
  411. return pc;
  412. }
  413. /*
  414. * Don't forget that the stack pointer must be aligned on a 8 bytes
  415. * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
  416. */
  417. unsigned long arch_align_stack(unsigned long sp)
  418. {
  419. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  420. sp -= get_random_int() & ~PAGE_MASK;
  421. return sp & ALMASK;
  422. }