process.c 13 KB

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