process.c 13 KB

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