process.c 21 KB

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