process.c 11 KB

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