process.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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/sched/debug.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/tick.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/stddef.h>
  21. #include <linux/unistd.h>
  22. #include <linux/export.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/mman.h>
  25. #include <linux/personality.h>
  26. #include <linux/sys.h>
  27. #include <linux/init.h>
  28. #include <linux/completion.h>
  29. #include <linux/kallsyms.h>
  30. #include <linux/random.h>
  31. #include <linux/prctl.h>
  32. #include <linux/nmi.h>
  33. #include <linux/cpu.h>
  34. #include <asm/asm.h>
  35. #include <asm/bootinfo.h>
  36. #include <asm/cpu.h>
  37. #include <asm/dsemul.h>
  38. #include <asm/dsp.h>
  39. #include <asm/fpu.h>
  40. #include <asm/irq.h>
  41. #include <asm/msa.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/mipsregs.h>
  44. #include <asm/processor.h>
  45. #include <asm/reg.h>
  46. #include <linux/uaccess.h>
  47. #include <asm/io.h>
  48. #include <asm/elf.h>
  49. #include <asm/isadep.h>
  50. #include <asm/inst.h>
  51. #include <asm/stacktrace.h>
  52. #include <asm/irq_regs.h>
  53. #ifdef CONFIG_HOTPLUG_CPU
  54. void arch_cpu_idle_dead(void)
  55. {
  56. play_dead();
  57. }
  58. #endif
  59. asmlinkage void ret_from_fork(void);
  60. asmlinkage void ret_from_kernel_thread(void);
  61. void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  62. {
  63. unsigned long status;
  64. /* New thread loses kernel privileges. */
  65. status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK);
  66. status |= KU_USER;
  67. regs->cp0_status = status;
  68. lose_fpu(0);
  69. clear_thread_flag(TIF_MSA_CTX_LIVE);
  70. clear_used_math();
  71. atomic_set(&current->thread.bd_emu_frame, BD_EMUFRAME_NONE);
  72. init_dsp();
  73. regs->cp0_epc = pc;
  74. regs->regs[29] = sp;
  75. }
  76. void exit_thread(struct task_struct *tsk)
  77. {
  78. /*
  79. * User threads may have allocated a delay slot emulation frame.
  80. * If so, clean up that allocation.
  81. */
  82. if (!(current->flags & PF_KTHREAD))
  83. dsemul_thread_cleanup(tsk);
  84. }
  85. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  86. {
  87. /*
  88. * Save any process state which is live in hardware registers to the
  89. * parent context prior to duplication. This prevents the new child
  90. * state becoming stale if the parent is preempted before copy_thread()
  91. * gets a chance to save the parent's live hardware registers to the
  92. * child context.
  93. */
  94. preempt_disable();
  95. if (is_msa_enabled())
  96. save_msa(current);
  97. else if (is_fpu_owner())
  98. _save_fp(current);
  99. save_dsp(current);
  100. preempt_enable();
  101. *dst = *src;
  102. return 0;
  103. }
  104. /*
  105. * Copy architecture-specific thread state
  106. */
  107. int copy_thread_tls(unsigned long clone_flags, unsigned long usp,
  108. unsigned long kthread_arg, struct task_struct *p, unsigned long tls)
  109. {
  110. struct thread_info *ti = task_thread_info(p);
  111. struct pt_regs *childregs, *regs = current_pt_regs();
  112. unsigned long childksp;
  113. childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
  114. /* set up new TSS. */
  115. childregs = (struct pt_regs *) childksp - 1;
  116. /* Put the stack after the struct pt_regs. */
  117. childksp = (unsigned long) childregs;
  118. p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
  119. if (unlikely(p->flags & PF_KTHREAD)) {
  120. /* kernel thread */
  121. unsigned long status = p->thread.cp0_status;
  122. memset(childregs, 0, sizeof(struct pt_regs));
  123. ti->addr_limit = KERNEL_DS;
  124. p->thread.reg16 = usp; /* fn */
  125. p->thread.reg17 = kthread_arg;
  126. p->thread.reg29 = childksp;
  127. p->thread.reg31 = (unsigned long) ret_from_kernel_thread;
  128. #if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
  129. status = (status & ~(ST0_KUP | ST0_IEP | ST0_IEC)) |
  130. ((status & (ST0_KUC | ST0_IEC)) << 2);
  131. #else
  132. status |= ST0_EXL;
  133. #endif
  134. childregs->cp0_status = status;
  135. return 0;
  136. }
  137. /* user thread */
  138. *childregs = *regs;
  139. childregs->regs[7] = 0; /* Clear error flag */
  140. childregs->regs[2] = 0; /* Child gets zero as return value */
  141. if (usp)
  142. childregs->regs[29] = usp;
  143. ti->addr_limit = USER_DS;
  144. p->thread.reg29 = (unsigned long) childregs;
  145. p->thread.reg31 = (unsigned long) ret_from_fork;
  146. /*
  147. * New tasks lose permission to use the fpu. This accelerates context
  148. * switching for most programs since they don't use the fpu.
  149. */
  150. childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
  151. clear_tsk_thread_flag(p, TIF_USEDFPU);
  152. clear_tsk_thread_flag(p, TIF_USEDMSA);
  153. clear_tsk_thread_flag(p, TIF_MSA_CTX_LIVE);
  154. #ifdef CONFIG_MIPS_MT_FPAFF
  155. clear_tsk_thread_flag(p, TIF_FPUBOUND);
  156. #endif /* CONFIG_MIPS_MT_FPAFF */
  157. atomic_set(&p->thread.bd_emu_frame, BD_EMUFRAME_NONE);
  158. if (clone_flags & CLONE_SETTLS)
  159. ti->tp_value = tls;
  160. return 0;
  161. }
  162. #ifdef CONFIG_STACKPROTECTOR
  163. #include <linux/stackprotector.h>
  164. unsigned long __stack_chk_guard __read_mostly;
  165. EXPORT_SYMBOL(__stack_chk_guard);
  166. #endif
  167. struct mips_frame_info {
  168. void *func;
  169. unsigned long func_size;
  170. int frame_size;
  171. int pc_offset;
  172. };
  173. #define J_TARGET(pc,target) \
  174. (((unsigned long)(pc) & 0xf0000000) | ((target) << 2))
  175. static inline int is_ra_save_ins(union mips_instruction *ip, int *poff)
  176. {
  177. #ifdef CONFIG_CPU_MICROMIPS
  178. /*
  179. * swsp ra,offset
  180. * swm16 reglist,offset(sp)
  181. * swm32 reglist,offset(sp)
  182. * sw32 ra,offset(sp)
  183. * jradiussp - NOT SUPPORTED
  184. *
  185. * microMIPS is way more fun...
  186. */
  187. if (mm_insn_16bit(ip->word >> 16)) {
  188. switch (ip->mm16_r5_format.opcode) {
  189. case mm_swsp16_op:
  190. if (ip->mm16_r5_format.rt != 31)
  191. return 0;
  192. *poff = ip->mm16_r5_format.imm;
  193. *poff = (*poff << 2) / sizeof(ulong);
  194. return 1;
  195. case mm_pool16c_op:
  196. switch (ip->mm16_m_format.func) {
  197. case mm_swm16_op:
  198. *poff = ip->mm16_m_format.imm;
  199. *poff += 1 + ip->mm16_m_format.rlist;
  200. *poff = (*poff << 2) / sizeof(ulong);
  201. return 1;
  202. default:
  203. return 0;
  204. }
  205. default:
  206. return 0;
  207. }
  208. }
  209. switch (ip->i_format.opcode) {
  210. case mm_sw32_op:
  211. if (ip->i_format.rs != 29)
  212. return 0;
  213. if (ip->i_format.rt != 31)
  214. return 0;
  215. *poff = ip->i_format.simmediate / sizeof(ulong);
  216. return 1;
  217. case mm_pool32b_op:
  218. switch (ip->mm_m_format.func) {
  219. case mm_swm32_func:
  220. if (ip->mm_m_format.rd < 0x10)
  221. return 0;
  222. if (ip->mm_m_format.base != 29)
  223. return 0;
  224. *poff = ip->mm_m_format.simmediate;
  225. *poff += (ip->mm_m_format.rd & 0xf) * sizeof(u32);
  226. *poff /= sizeof(ulong);
  227. return 1;
  228. default:
  229. return 0;
  230. }
  231. default:
  232. return 0;
  233. }
  234. #else
  235. /* sw / sd $ra, offset($sp) */
  236. if ((ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
  237. ip->i_format.rs == 29 && ip->i_format.rt == 31) {
  238. *poff = ip->i_format.simmediate / sizeof(ulong);
  239. return 1;
  240. }
  241. return 0;
  242. #endif
  243. }
  244. static inline int is_jump_ins(union mips_instruction *ip)
  245. {
  246. #ifdef CONFIG_CPU_MICROMIPS
  247. /*
  248. * jr16,jrc,jalr16,jalr16
  249. * jal
  250. * jalr/jr,jalr.hb/jr.hb,jalrs,jalrs.hb
  251. * jraddiusp - NOT SUPPORTED
  252. *
  253. * microMIPS is kind of more fun...
  254. */
  255. if (mm_insn_16bit(ip->word >> 16)) {
  256. if ((ip->mm16_r5_format.opcode == mm_pool16c_op &&
  257. (ip->mm16_r5_format.rt & mm_jr16_op) == mm_jr16_op))
  258. return 1;
  259. return 0;
  260. }
  261. if (ip->j_format.opcode == mm_j32_op)
  262. return 1;
  263. if (ip->j_format.opcode == mm_jal32_op)
  264. return 1;
  265. if (ip->r_format.opcode != mm_pool32a_op ||
  266. ip->r_format.func != mm_pool32axf_op)
  267. return 0;
  268. return ((ip->u_format.uimmediate >> 6) & mm_jalr_op) == mm_jalr_op;
  269. #else
  270. if (ip->j_format.opcode == j_op)
  271. return 1;
  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, int *frame_size)
  280. {
  281. #ifdef CONFIG_CPU_MICROMIPS
  282. unsigned short tmp;
  283. /*
  284. * addiusp -imm
  285. * addius5 sp,-imm
  286. * addiu32 sp,sp,-imm
  287. * jradiussp - NOT SUPPORTED
  288. *
  289. * microMIPS is not more fun...
  290. */
  291. if (mm_insn_16bit(ip->word >> 16)) {
  292. if (ip->mm16_r3_format.opcode == mm_pool16d_op &&
  293. ip->mm16_r3_format.simmediate & mm_addiusp_func) {
  294. tmp = ip->mm_b0_format.simmediate >> 1;
  295. tmp = ((tmp & 0x1ff) ^ 0x100) - 0x100;
  296. if ((tmp + 2) < 4) /* 0x0,0x1,0x1fe,0x1ff are special */
  297. tmp ^= 0x100;
  298. *frame_size = -(signed short)(tmp << 2);
  299. return 1;
  300. }
  301. if (ip->mm16_r5_format.opcode == mm_pool16d_op &&
  302. ip->mm16_r5_format.rt == 29) {
  303. tmp = ip->mm16_r5_format.imm >> 1;
  304. *frame_size = -(signed short)(tmp & 0xf);
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. if (ip->mm_i_format.opcode == mm_addiu32_op &&
  310. ip->mm_i_format.rt == 29 && ip->mm_i_format.rs == 29) {
  311. *frame_size = -ip->i_format.simmediate;
  312. return 1;
  313. }
  314. #else
  315. /* addiu/daddiu sp,sp,-imm */
  316. if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
  317. return 0;
  318. if (ip->i_format.opcode == addiu_op ||
  319. ip->i_format.opcode == daddiu_op) {
  320. *frame_size = -ip->i_format.simmediate;
  321. return 1;
  322. }
  323. #endif
  324. return 0;
  325. }
  326. static int get_frame_info(struct mips_frame_info *info)
  327. {
  328. bool is_mmips = IS_ENABLED(CONFIG_CPU_MICROMIPS);
  329. union mips_instruction insn, *ip, *ip_end;
  330. const unsigned int max_insns = 128;
  331. unsigned int last_insn_size = 0;
  332. unsigned int i;
  333. bool saw_jump = false;
  334. info->pc_offset = -1;
  335. info->frame_size = 0;
  336. ip = (void *)msk_isa16_mode((ulong)info->func);
  337. if (!ip)
  338. goto err;
  339. ip_end = (void *)ip + info->func_size;
  340. for (i = 0; i < max_insns && ip < ip_end; i++) {
  341. ip = (void *)ip + last_insn_size;
  342. if (is_mmips && mm_insn_16bit(ip->halfword[0])) {
  343. insn.word = ip->halfword[0] << 16;
  344. last_insn_size = 2;
  345. } else if (is_mmips) {
  346. insn.word = ip->halfword[0] << 16 | ip->halfword[1];
  347. last_insn_size = 4;
  348. } else {
  349. insn.word = ip->word;
  350. last_insn_size = 4;
  351. }
  352. if (!info->frame_size) {
  353. is_sp_move_ins(&insn, &info->frame_size);
  354. continue;
  355. } else if (!saw_jump && is_jump_ins(ip)) {
  356. /*
  357. * If we see a jump instruction, we are finished
  358. * with the frame save.
  359. *
  360. * Some functions can have a shortcut return at
  361. * the beginning of the function, so don't start
  362. * looking for jump instruction until we see the
  363. * frame setup.
  364. *
  365. * The RA save instruction can get put into the
  366. * delay slot of the jump instruction, so look
  367. * at the next instruction, too.
  368. */
  369. saw_jump = true;
  370. continue;
  371. }
  372. if (info->pc_offset == -1 &&
  373. is_ra_save_ins(&insn, &info->pc_offset))
  374. break;
  375. if (saw_jump)
  376. break;
  377. }
  378. if (info->frame_size && info->pc_offset >= 0) /* nested */
  379. return 0;
  380. if (info->pc_offset < 0) /* leaf */
  381. return 1;
  382. /* prologue seems bogus... */
  383. err:
  384. return -1;
  385. }
  386. static struct mips_frame_info schedule_mfi __read_mostly;
  387. #ifdef CONFIG_KALLSYMS
  388. static unsigned long get___schedule_addr(void)
  389. {
  390. return kallsyms_lookup_name("__schedule");
  391. }
  392. #else
  393. static unsigned long get___schedule_addr(void)
  394. {
  395. union mips_instruction *ip = (void *)schedule;
  396. int max_insns = 8;
  397. int i;
  398. for (i = 0; i < max_insns; i++, ip++) {
  399. if (ip->j_format.opcode == j_op)
  400. return J_TARGET(ip, ip->j_format.target);
  401. }
  402. return 0;
  403. }
  404. #endif
  405. static int __init frame_info_init(void)
  406. {
  407. unsigned long size = 0;
  408. #ifdef CONFIG_KALLSYMS
  409. unsigned long ofs;
  410. #endif
  411. unsigned long addr;
  412. addr = get___schedule_addr();
  413. if (!addr)
  414. addr = (unsigned long)schedule;
  415. #ifdef CONFIG_KALLSYMS
  416. kallsyms_lookup_size_offset(addr, &size, &ofs);
  417. #endif
  418. schedule_mfi.func = (void *)addr;
  419. schedule_mfi.func_size = size;
  420. get_frame_info(&schedule_mfi);
  421. /*
  422. * Without schedule() frame info, result given by
  423. * thread_saved_pc() and get_wchan() are not reliable.
  424. */
  425. if (schedule_mfi.pc_offset < 0)
  426. printk("Can't analyze schedule() prologue at %p\n", schedule);
  427. return 0;
  428. }
  429. arch_initcall(frame_info_init);
  430. /*
  431. * Return saved PC of a blocked thread.
  432. */
  433. static unsigned long thread_saved_pc(struct task_struct *tsk)
  434. {
  435. struct thread_struct *t = &tsk->thread;
  436. /* New born processes are a special case */
  437. if (t->reg31 == (unsigned long) ret_from_fork)
  438. return t->reg31;
  439. if (schedule_mfi.pc_offset < 0)
  440. return 0;
  441. return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
  442. }
  443. #ifdef CONFIG_KALLSYMS
  444. /* generic stack unwinding function */
  445. unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
  446. unsigned long *sp,
  447. unsigned long pc,
  448. unsigned long *ra)
  449. {
  450. unsigned long low, high, irq_stack_high;
  451. struct mips_frame_info info;
  452. unsigned long size, ofs;
  453. struct pt_regs *regs;
  454. int leaf;
  455. if (!stack_page)
  456. return 0;
  457. /*
  458. * IRQ stacks start at IRQ_STACK_START
  459. * task stacks at THREAD_SIZE - 32
  460. */
  461. low = stack_page;
  462. if (!preemptible() && on_irq_stack(raw_smp_processor_id(), *sp)) {
  463. high = stack_page + IRQ_STACK_START;
  464. irq_stack_high = high;
  465. } else {
  466. high = stack_page + THREAD_SIZE - 32;
  467. irq_stack_high = 0;
  468. }
  469. /*
  470. * If we reached the top of the interrupt stack, start unwinding
  471. * the interrupted task stack.
  472. */
  473. if (unlikely(*sp == irq_stack_high)) {
  474. unsigned long task_sp = *(unsigned long *)*sp;
  475. /*
  476. * Check that the pointer saved in the IRQ stack head points to
  477. * something within the stack of the current task
  478. */
  479. if (!object_is_on_stack((void *)task_sp))
  480. return 0;
  481. /*
  482. * Follow pointer to tasks kernel stack frame where interrupted
  483. * state was saved.
  484. */
  485. regs = (struct pt_regs *)task_sp;
  486. pc = regs->cp0_epc;
  487. if (!user_mode(regs) && __kernel_text_address(pc)) {
  488. *sp = regs->regs[29];
  489. *ra = regs->regs[31];
  490. return pc;
  491. }
  492. return 0;
  493. }
  494. if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
  495. return 0;
  496. /*
  497. * Return ra if an exception occurred at the first instruction
  498. */
  499. if (unlikely(ofs == 0)) {
  500. pc = *ra;
  501. *ra = 0;
  502. return pc;
  503. }
  504. info.func = (void *)(pc - ofs);
  505. info.func_size = ofs; /* analyze from start to ofs */
  506. leaf = get_frame_info(&info);
  507. if (leaf < 0)
  508. return 0;
  509. if (*sp < low || *sp + info.frame_size > high)
  510. return 0;
  511. if (leaf)
  512. /*
  513. * For some extreme cases, get_frame_info() can
  514. * consider wrongly a nested function as a leaf
  515. * one. In that cases avoid to return always the
  516. * same value.
  517. */
  518. pc = pc != *ra ? *ra : 0;
  519. else
  520. pc = ((unsigned long *)(*sp))[info.pc_offset];
  521. *sp += info.frame_size;
  522. *ra = 0;
  523. return __kernel_text_address(pc) ? pc : 0;
  524. }
  525. EXPORT_SYMBOL(unwind_stack_by_address);
  526. /* used by show_backtrace() */
  527. unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
  528. unsigned long pc, unsigned long *ra)
  529. {
  530. unsigned long stack_page = 0;
  531. int cpu;
  532. for_each_possible_cpu(cpu) {
  533. if (on_irq_stack(cpu, *sp)) {
  534. stack_page = (unsigned long)irq_stack[cpu];
  535. break;
  536. }
  537. }
  538. if (!stack_page)
  539. stack_page = (unsigned long)task_stack_page(task);
  540. return unwind_stack_by_address(stack_page, sp, pc, ra);
  541. }
  542. #endif
  543. /*
  544. * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
  545. */
  546. unsigned long get_wchan(struct task_struct *task)
  547. {
  548. unsigned long pc = 0;
  549. #ifdef CONFIG_KALLSYMS
  550. unsigned long sp;
  551. unsigned long ra = 0;
  552. #endif
  553. if (!task || task == current || task->state == TASK_RUNNING)
  554. goto out;
  555. if (!task_stack_page(task))
  556. goto out;
  557. pc = thread_saved_pc(task);
  558. #ifdef CONFIG_KALLSYMS
  559. sp = task->thread.reg29 + schedule_mfi.frame_size;
  560. while (in_sched_functions(pc))
  561. pc = unwind_stack(task, &sp, pc, &ra);
  562. #endif
  563. out:
  564. return pc;
  565. }
  566. /*
  567. * Don't forget that the stack pointer must be aligned on a 8 bytes
  568. * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
  569. */
  570. unsigned long arch_align_stack(unsigned long sp)
  571. {
  572. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  573. sp -= get_random_int() & ~PAGE_MASK;
  574. return sp & ALMASK;
  575. }
  576. static DEFINE_PER_CPU(call_single_data_t, backtrace_csd);
  577. static struct cpumask backtrace_csd_busy;
  578. static void handle_backtrace(void *info)
  579. {
  580. nmi_cpu_backtrace(get_irq_regs());
  581. cpumask_clear_cpu(smp_processor_id(), &backtrace_csd_busy);
  582. }
  583. static void raise_backtrace(cpumask_t *mask)
  584. {
  585. call_single_data_t *csd;
  586. int cpu;
  587. for_each_cpu(cpu, mask) {
  588. /*
  589. * If we previously sent an IPI to the target CPU & it hasn't
  590. * cleared its bit in the busy cpumask then it didn't handle
  591. * our previous IPI & it's not safe for us to reuse the
  592. * call_single_data_t.
  593. */
  594. if (cpumask_test_and_set_cpu(cpu, &backtrace_csd_busy)) {
  595. pr_warn("Unable to send backtrace IPI to CPU%u - perhaps it hung?\n",
  596. cpu);
  597. continue;
  598. }
  599. csd = &per_cpu(backtrace_csd, cpu);
  600. csd->func = handle_backtrace;
  601. smp_call_function_single_async(cpu, csd);
  602. }
  603. }
  604. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
  605. {
  606. nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace);
  607. }
  608. int mips_get_process_fp_mode(struct task_struct *task)
  609. {
  610. int value = 0;
  611. if (!test_tsk_thread_flag(task, TIF_32BIT_FPREGS))
  612. value |= PR_FP_MODE_FR;
  613. if (test_tsk_thread_flag(task, TIF_HYBRID_FPREGS))
  614. value |= PR_FP_MODE_FRE;
  615. return value;
  616. }
  617. static long prepare_for_fp_mode_switch(void *unused)
  618. {
  619. /*
  620. * This is icky, but we use this to simply ensure that all CPUs have
  621. * context switched, regardless of whether they were previously running
  622. * kernel or user code. This ensures that no CPU currently has its FPU
  623. * enabled, or is about to attempt to enable it through any path other
  624. * than enable_restore_fp_context() which will wait appropriately for
  625. * fp_mode_switching to be zero.
  626. */
  627. return 0;
  628. }
  629. int mips_set_process_fp_mode(struct task_struct *task, unsigned int value)
  630. {
  631. const unsigned int known_bits = PR_FP_MODE_FR | PR_FP_MODE_FRE;
  632. struct task_struct *t;
  633. struct cpumask process_cpus;
  634. int cpu;
  635. /* If nothing to change, return right away, successfully. */
  636. if (value == mips_get_process_fp_mode(task))
  637. return 0;
  638. /* Only accept a mode change if 64-bit FP enabled for o32. */
  639. if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  640. return -EOPNOTSUPP;
  641. /* And only for o32 tasks. */
  642. if (IS_ENABLED(CONFIG_64BIT) && !test_thread_flag(TIF_32BIT_REGS))
  643. return -EOPNOTSUPP;
  644. /* Check the value is valid */
  645. if (value & ~known_bits)
  646. return -EOPNOTSUPP;
  647. /* Setting FRE without FR is not supported. */
  648. if ((value & (PR_FP_MODE_FR | PR_FP_MODE_FRE)) == PR_FP_MODE_FRE)
  649. return -EOPNOTSUPP;
  650. /* Avoid inadvertently triggering emulation */
  651. if ((value & PR_FP_MODE_FR) && raw_cpu_has_fpu &&
  652. !(raw_current_cpu_data.fpu_id & MIPS_FPIR_F64))
  653. return -EOPNOTSUPP;
  654. if ((value & PR_FP_MODE_FRE) && raw_cpu_has_fpu && !cpu_has_fre)
  655. return -EOPNOTSUPP;
  656. /* FR = 0 not supported in MIPS R6 */
  657. if (!(value & PR_FP_MODE_FR) && raw_cpu_has_fpu && cpu_has_mips_r6)
  658. return -EOPNOTSUPP;
  659. /* Indicate the new FP mode in each thread */
  660. for_each_thread(task, t) {
  661. /* Update desired FP register width */
  662. if (value & PR_FP_MODE_FR) {
  663. clear_tsk_thread_flag(t, TIF_32BIT_FPREGS);
  664. } else {
  665. set_tsk_thread_flag(t, TIF_32BIT_FPREGS);
  666. clear_tsk_thread_flag(t, TIF_MSA_CTX_LIVE);
  667. }
  668. /* Update desired FP single layout */
  669. if (value & PR_FP_MODE_FRE)
  670. set_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
  671. else
  672. clear_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
  673. }
  674. /*
  675. * We need to ensure that all threads in the process have switched mode
  676. * before returning, in order to allow userland to not worry about
  677. * races. We can do this by forcing all CPUs that any thread in the
  678. * process may be running on to schedule something else - in this case
  679. * prepare_for_fp_mode_switch().
  680. *
  681. * We begin by generating a mask of all CPUs that any thread in the
  682. * process may be running on.
  683. */
  684. cpumask_clear(&process_cpus);
  685. for_each_thread(task, t)
  686. cpumask_set_cpu(task_cpu(t), &process_cpus);
  687. /*
  688. * Now we schedule prepare_for_fp_mode_switch() on each of those CPUs.
  689. *
  690. * The CPUs may have rescheduled already since we switched mode or
  691. * generated the cpumask, but that doesn't matter. If the task in this
  692. * process is scheduled out then our scheduling
  693. * prepare_for_fp_mode_switch() will simply be redundant. If it's
  694. * scheduled in then it will already have picked up the new FP mode
  695. * whilst doing so.
  696. */
  697. get_online_cpus();
  698. for_each_cpu_and(cpu, &process_cpus, cpu_online_mask)
  699. work_on_cpu(cpu, prepare_for_fp_mode_switch, NULL);
  700. put_online_cpus();
  701. wake_up_var(&task->mm->context.fp_mode_switching);
  702. return 0;
  703. }
  704. #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
  705. void mips_dump_regs32(u32 *uregs, const struct pt_regs *regs)
  706. {
  707. unsigned int i;
  708. for (i = MIPS32_EF_R1; i <= MIPS32_EF_R31; i++) {
  709. /* k0/k1 are copied as zero. */
  710. if (i == MIPS32_EF_R26 || i == MIPS32_EF_R27)
  711. uregs[i] = 0;
  712. else
  713. uregs[i] = regs->regs[i - MIPS32_EF_R0];
  714. }
  715. uregs[MIPS32_EF_LO] = regs->lo;
  716. uregs[MIPS32_EF_HI] = regs->hi;
  717. uregs[MIPS32_EF_CP0_EPC] = regs->cp0_epc;
  718. uregs[MIPS32_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  719. uregs[MIPS32_EF_CP0_STATUS] = regs->cp0_status;
  720. uregs[MIPS32_EF_CP0_CAUSE] = regs->cp0_cause;
  721. }
  722. #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
  723. #ifdef CONFIG_64BIT
  724. void mips_dump_regs64(u64 *uregs, const struct pt_regs *regs)
  725. {
  726. unsigned int i;
  727. for (i = MIPS64_EF_R1; i <= MIPS64_EF_R31; i++) {
  728. /* k0/k1 are copied as zero. */
  729. if (i == MIPS64_EF_R26 || i == MIPS64_EF_R27)
  730. uregs[i] = 0;
  731. else
  732. uregs[i] = regs->regs[i - MIPS64_EF_R0];
  733. }
  734. uregs[MIPS64_EF_LO] = regs->lo;
  735. uregs[MIPS64_EF_HI] = regs->hi;
  736. uregs[MIPS64_EF_CP0_EPC] = regs->cp0_epc;
  737. uregs[MIPS64_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  738. uregs[MIPS64_EF_CP0_STATUS] = regs->cp0_status;
  739. uregs[MIPS64_EF_CP0_CAUSE] = regs->cp0_cause;
  740. }
  741. #endif /* CONFIG_64BIT */