process.c 14 KB

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