process.c 19 KB

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