process.c 11 KB

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