process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. * Copyright (C) 2013 Imagination Technologies Ltd.
  11. */
  12. #include <linux/errno.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/export.h>
  20. #include <linux/ptrace.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/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_idle_enter();
  56. rcu_idle_enter();
  57. while (!need_resched() && cpu_online(cpu)) {
  58. #ifdef CONFIG_MIPS_MT_SMTC
  59. extern void smtc_idle_loop_hook(void);
  60. smtc_idle_loop_hook();
  61. #endif
  62. if (cpu_wait) {
  63. /* Don't trace irqs off for idle */
  64. stop_critical_timings();
  65. (*cpu_wait)();
  66. start_critical_timings();
  67. }
  68. }
  69. #ifdef CONFIG_HOTPLUG_CPU
  70. if (!cpu_online(cpu) && !cpu_isset(cpu, cpu_callin_map))
  71. play_dead();
  72. #endif
  73. rcu_idle_exit();
  74. tick_nohz_idle_exit();
  75. schedule_preempt_disabled();
  76. }
  77. }
  78. asmlinkage void ret_from_fork(void);
  79. asmlinkage void ret_from_kernel_thread(void);
  80. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  81. {
  82. unsigned long status;
  83. /* New thread loses kernel privileges. */
  84. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK);
  85. #ifdef CONFIG_64BIT
  86. status |= test_thread_flag(TIF_32BIT_REGS) ? 0 : ST0_FR;
  87. #endif
  88. status |= KU_USER;
  89. regs->cp0_status = status;
  90. clear_used_math();
  91. clear_fpu_owner();
  92. if (cpu_has_dsp)
  93. __init_dsp();
  94. regs->cp0_epc = pc;
  95. regs->regs[29] = sp;
  96. }
  97. void exit_thread(void)
  98. {
  99. }
  100. void flush_thread(void)
  101. {
  102. }
  103. int copy_thread(unsigned long clone_flags, unsigned long usp,
  104. unsigned long arg, struct task_struct *p)
  105. {
  106. struct thread_info *ti = task_thread_info(p);
  107. struct pt_regs *childregs, *regs = current_pt_regs();
  108. unsigned long childksp;
  109. p->set_child_tid = p->clear_child_tid = NULL;
  110. childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
  111. preempt_disable();
  112. if (is_fpu_owner())
  113. save_fp(p);
  114. if (cpu_has_dsp)
  115. save_dsp(p);
  116. preempt_enable();
  117. /* set up new TSS. */
  118. childregs = (struct pt_regs *) childksp - 1;
  119. /* Put the stack after the struct pt_regs. */
  120. childksp = (unsigned long) childregs;
  121. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  122. if (unlikely(p->flags & PF_KTHREAD)) {
  123. unsigned long status = p->thread.cp0_status;
  124. memset(childregs, 0, sizeof(struct pt_regs));
  125. ti->addr_limit = KERNEL_DS;
  126. p->thread.reg16 = usp; /* fn */
  127. p->thread.reg17 = arg;
  128. p->thread.reg29 = childksp;
  129. p->thread.reg31 = (unsigned long) ret_from_kernel_thread;
  130. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  131. status = (status & ~(ST0_KUP | ST0_IEP | ST0_IEC)) |
  132. ((status & (ST0_KUC | ST0_IEC)) << 2);
  133. #else
  134. status |= ST0_EXL;
  135. #endif
  136. childregs->cp0_status = status;
  137. return 0;
  138. }
  139. *childregs = *regs;
  140. childregs->regs[7] = 0; /* Clear error flag */
  141. childregs->regs[2] = 0; /* Child gets zero as return value */
  142. if (usp)
  143. childregs->regs[29] = usp;
  144. ti->addr_limit = USER_DS;
  145. p->thread.reg29 = (unsigned long) childregs;
  146. p->thread.reg31 = (unsigned long) ret_from_fork;
  147. /*
  148. * New tasks lose permission to use the fpu. This accelerates context
  149. * switching for most programs since they don't use the fpu.
  150. */
  151. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  152. #ifdef CONFIG_MIPS_MT_SMTC
  153. /*
  154. * SMTC restores TCStatus after Status, and the CU bits
  155. * are aliased there.
  156. */
  157. childregs->cp0_tcstatus &= ~(ST0_CU2|ST0_CU1);
  158. #endif
  159. clear_tsk_thread_flag(p, TIF_USEDFPU);
  160. #ifdef CONFIG_MIPS_MT_FPAFF
  161. clear_tsk_thread_flag(p, TIF_FPUBOUND);
  162. #endif /* CONFIG_MIPS_MT_FPAFF */
  163. if (clone_flags & CLONE_SETTLS)
  164. ti->tp_value = regs->regs[7];
  165. return 0;
  166. }
  167. /* Fill in the fpu structure for a core dump.. */
  168. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  169. {
  170. memcpy(r, &current->thread.fpu, sizeof(current->thread.fpu));
  171. return 1;
  172. }
  173. void elf_dump_regs(elf_greg_t *gp, struct pt_regs *regs)
  174. {
  175. int i;
  176. for (i = 0; i < EF_R0; i++)
  177. gp[i] = 0;
  178. gp[EF_R0] = 0;
  179. for (i = 1; i <= 31; i++)
  180. gp[EF_R0 + i] = regs->regs[i];
  181. gp[EF_R26] = 0;
  182. gp[EF_R27] = 0;
  183. gp[EF_LO] = regs->lo;
  184. gp[EF_HI] = regs->hi;
  185. gp[EF_CP0_EPC] = regs->cp0_epc;
  186. gp[EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  187. gp[EF_CP0_STATUS] = regs->cp0_status;
  188. gp[EF_CP0_CAUSE] = regs->cp0_cause;
  189. #ifdef EF_UNUSED0
  190. gp[EF_UNUSED0] = 0;
  191. #endif
  192. }
  193. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  194. {
  195. elf_dump_regs(*regs, task_pt_regs(tsk));
  196. return 1;
  197. }
  198. int dump_task_fpu(struct task_struct *t, elf_fpregset_t *fpr)
  199. {
  200. memcpy(fpr, &t->thread.fpu, sizeof(current->thread.fpu));
  201. return 1;
  202. }
  203. /*
  204. *
  205. */
  206. struct mips_frame_info {
  207. void *func;
  208. unsigned long func_size;
  209. int frame_size;
  210. int pc_offset;
  211. };
  212. static inline int is_ra_save_ins(union mips_instruction *ip)
  213. {
  214. #ifdef CONFIG_CPU_MICROMIPS
  215. union mips_instruction mmi;
  216. /*
  217. * swsp ra,offset
  218. * swm16 reglist,offset(sp)
  219. * swm32 reglist,offset(sp)
  220. * sw32 ra,offset(sp)
  221. * jradiussp - NOT SUPPORTED
  222. *
  223. * microMIPS is way more fun...
  224. */
  225. if (mm_insn_16bit(ip->halfword[0])) {
  226. mmi.word = (ip->halfword[0] << 16);
  227. return ((mmi.mm16_r5_format.opcode == mm_swsp16_op &&
  228. mmi.mm16_r5_format.rt == 31) ||
  229. (mmi.mm16_m_format.opcode == mm_pool16c_op &&
  230. mmi.mm16_m_format.func == mm_swm16_op));
  231. }
  232. else {
  233. mmi.halfword[0] = ip->halfword[1];
  234. mmi.halfword[1] = ip->halfword[0];
  235. return ((mmi.mm_m_format.opcode == mm_pool32b_op &&
  236. mmi.mm_m_format.rd > 9 &&
  237. mmi.mm_m_format.base == 29 &&
  238. mmi.mm_m_format.func == mm_swm32_func) ||
  239. (mmi.i_format.opcode == mm_sw32_op &&
  240. mmi.i_format.rs == 29 &&
  241. mmi.i_format.rt == 31));
  242. }
  243. #else
  244. /* sw / sd $ra, offset($sp) */
  245. return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
  246. ip->i_format.rs == 29 &&
  247. ip->i_format.rt == 31;
  248. #endif
  249. }
  250. static inline int is_jal_jalr_jr_ins(union mips_instruction *ip)
  251. {
  252. #ifdef CONFIG_CPU_MICROMIPS
  253. /*
  254. * jr16,jrc,jalr16,jalr16
  255. * jal
  256. * jalr/jr,jalr.hb/jr.hb,jalrs,jalrs.hb
  257. * jraddiusp - NOT SUPPORTED
  258. *
  259. * microMIPS is kind of more fun...
  260. */
  261. union mips_instruction mmi;
  262. mmi.word = (ip->halfword[0] << 16);
  263. if ((mmi.mm16_r5_format.opcode == mm_pool16c_op &&
  264. (mmi.mm16_r5_format.rt & mm_jr16_op) == mm_jr16_op) ||
  265. ip->j_format.opcode == mm_jal32_op)
  266. return 1;
  267. if (ip->r_format.opcode != mm_pool32a_op ||
  268. ip->r_format.func != mm_pool32axf_op)
  269. return 0;
  270. return (((ip->u_format.uimmediate >> 6) & mm_jalr_op) == mm_jalr_op);
  271. #else
  272. if (ip->j_format.opcode == jal_op)
  273. return 1;
  274. if (ip->r_format.opcode != spec_op)
  275. return 0;
  276. return ip->r_format.func == jalr_op || ip->r_format.func == jr_op;
  277. #endif
  278. }
  279. static inline int is_sp_move_ins(union mips_instruction *ip)
  280. {
  281. #ifdef CONFIG_CPU_MICROMIPS
  282. /*
  283. * addiusp -imm
  284. * addius5 sp,-imm
  285. * addiu32 sp,sp,-imm
  286. * jradiussp - NOT SUPPORTED
  287. *
  288. * microMIPS is not more fun...
  289. */
  290. if (mm_insn_16bit(ip->halfword[0])) {
  291. union mips_instruction mmi;
  292. mmi.word = (ip->halfword[0] << 16);
  293. return ((mmi.mm16_r3_format.opcode == mm_pool16d_op &&
  294. mmi.mm16_r3_format.simmediate && mm_addiusp_func) ||
  295. (mmi.mm16_r5_format.opcode == mm_pool16d_op &&
  296. mmi.mm16_r5_format.rt == 29));
  297. }
  298. return (ip->mm_i_format.opcode == mm_addiu32_op &&
  299. ip->mm_i_format.rt == 29 && ip->mm_i_format.rs == 29);
  300. #else
  301. /* addiu/daddiu sp,sp,-imm */
  302. if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
  303. return 0;
  304. if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
  305. return 1;
  306. #endif
  307. return 0;
  308. }
  309. static int get_frame_info(struct mips_frame_info *info)
  310. {
  311. #ifdef CONFIG_CPU_MICROMIPS
  312. union mips_instruction *ip = (void *) (((char *) info->func) - 1);
  313. #else
  314. union mips_instruction *ip = info->func;
  315. #endif
  316. unsigned max_insns = info->func_size / sizeof(union mips_instruction);
  317. unsigned i;
  318. info->pc_offset = -1;
  319. info->frame_size = 0;
  320. if (!ip)
  321. goto err;
  322. if (max_insns == 0)
  323. max_insns = 128U; /* unknown function size */
  324. max_insns = min(128U, max_insns);
  325. for (i = 0; i < max_insns; i++, ip++) {
  326. if (is_jal_jalr_jr_ins(ip))
  327. break;
  328. if (!info->frame_size) {
  329. if (is_sp_move_ins(ip))
  330. {
  331. #ifdef CONFIG_CPU_MICROMIPS
  332. if (mm_insn_16bit(ip->halfword[0]))
  333. {
  334. unsigned short tmp;
  335. if (ip->halfword[0] & mm_addiusp_func)
  336. {
  337. tmp = (((ip->halfword[0] >> 1) & 0x1ff) << 2);
  338. info->frame_size = -(signed short)(tmp | ((tmp & 0x100) ? 0xfe00 : 0));
  339. } else {
  340. tmp = (ip->halfword[0] >> 1);
  341. info->frame_size = -(signed short)(tmp & 0xf);
  342. }
  343. ip = (void *) &ip->halfword[1];
  344. ip--;
  345. } else
  346. #endif
  347. info->frame_size = - ip->i_format.simmediate;
  348. }
  349. continue;
  350. }
  351. if (info->pc_offset == -1 && is_ra_save_ins(ip)) {
  352. info->pc_offset =
  353. ip->i_format.simmediate / sizeof(long);
  354. break;
  355. }
  356. }
  357. if (info->frame_size && info->pc_offset >= 0) /* nested */
  358. return 0;
  359. if (info->pc_offset < 0) /* leaf */
  360. return 1;
  361. /* prologue seems boggus... */
  362. err:
  363. return -1;
  364. }
  365. static struct mips_frame_info schedule_mfi __read_mostly;
  366. static int __init frame_info_init(void)
  367. {
  368. unsigned long size = 0;
  369. #ifdef CONFIG_KALLSYMS
  370. unsigned long ofs;
  371. kallsyms_lookup_size_offset((unsigned long)schedule, &size, &ofs);
  372. #endif
  373. schedule_mfi.func = schedule;
  374. schedule_mfi.func_size = size;
  375. get_frame_info(&schedule_mfi);
  376. /*
  377. * Without schedule() frame info, result given by
  378. * thread_saved_pc() and get_wchan() are not reliable.
  379. */
  380. if (schedule_mfi.pc_offset < 0)
  381. printk("Can't analyze schedule() prologue at %p\n", schedule);
  382. return 0;
  383. }
  384. arch_initcall(frame_info_init);
  385. /*
  386. * Return saved PC of a blocked thread.
  387. */
  388. unsigned long thread_saved_pc(struct task_struct *tsk)
  389. {
  390. struct thread_struct *t = &tsk->thread;
  391. /* New born processes are a special case */
  392. if (t->reg31 == (unsigned long) ret_from_fork)
  393. return t->reg31;
  394. if (schedule_mfi.pc_offset < 0)
  395. return 0;
  396. return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
  397. }
  398. #ifdef CONFIG_KALLSYMS
  399. /* generic stack unwinding function */
  400. unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
  401. unsigned long *sp,
  402. unsigned long pc,
  403. unsigned long *ra)
  404. {
  405. struct mips_frame_info info;
  406. unsigned long size, ofs;
  407. int leaf;
  408. extern void ret_from_irq(void);
  409. extern void ret_from_exception(void);
  410. if (!stack_page)
  411. return 0;
  412. /*
  413. * If we reached the bottom of interrupt context,
  414. * return saved pc in pt_regs.
  415. */
  416. if (pc == (unsigned long)ret_from_irq ||
  417. pc == (unsigned long)ret_from_exception) {
  418. struct pt_regs *regs;
  419. if (*sp >= stack_page &&
  420. *sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) {
  421. regs = (struct pt_regs *)*sp;
  422. pc = regs->cp0_epc;
  423. if (__kernel_text_address(pc)) {
  424. *sp = regs->regs[29];
  425. *ra = regs->regs[31];
  426. return pc;
  427. }
  428. }
  429. return 0;
  430. }
  431. if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
  432. return 0;
  433. /*
  434. * Return ra if an exception occurred at the first instruction
  435. */
  436. if (unlikely(ofs == 0)) {
  437. pc = *ra;
  438. *ra = 0;
  439. return pc;
  440. }
  441. info.func = (void *)(pc - ofs);
  442. info.func_size = ofs; /* analyze from start to ofs */
  443. leaf = get_frame_info(&info);
  444. if (leaf < 0)
  445. return 0;
  446. if (*sp < stack_page ||
  447. *sp + info.frame_size > stack_page + THREAD_SIZE - 32)
  448. return 0;
  449. if (leaf)
  450. /*
  451. * For some extreme cases, get_frame_info() can
  452. * consider wrongly a nested function as a leaf
  453. * one. In that cases avoid to return always the
  454. * same value.
  455. */
  456. pc = pc != *ra ? *ra : 0;
  457. else
  458. pc = ((unsigned long *)(*sp))[info.pc_offset];
  459. *sp += info.frame_size;
  460. *ra = 0;
  461. return __kernel_text_address(pc) ? pc : 0;
  462. }
  463. EXPORT_SYMBOL(unwind_stack_by_address);
  464. /* used by show_backtrace() */
  465. unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
  466. unsigned long pc, unsigned long *ra)
  467. {
  468. unsigned long stack_page = (unsigned long)task_stack_page(task);
  469. return unwind_stack_by_address(stack_page, sp, pc, ra);
  470. }
  471. #endif
  472. /*
  473. * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
  474. */
  475. unsigned long get_wchan(struct task_struct *task)
  476. {
  477. unsigned long pc = 0;
  478. #ifdef CONFIG_KALLSYMS
  479. unsigned long sp;
  480. unsigned long ra = 0;
  481. #endif
  482. if (!task || task == current || task->state == TASK_RUNNING)
  483. goto out;
  484. if (!task_stack_page(task))
  485. goto out;
  486. pc = thread_saved_pc(task);
  487. #ifdef CONFIG_KALLSYMS
  488. sp = task->thread.reg29 + schedule_mfi.frame_size;
  489. while (in_sched_functions(pc))
  490. pc = unwind_stack(task, &sp, pc, &ra);
  491. #endif
  492. out:
  493. return pc;
  494. }
  495. /*
  496. * Don't forget that the stack pointer must be aligned on a 8 bytes
  497. * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
  498. */
  499. unsigned long arch_align_stack(unsigned long sp)
  500. {
  501. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  502. sp -= get_random_int() & ~PAGE_MASK;
  503. return sp & ALMASK;
  504. }