process.c 14 KB

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