process.c 19 KB

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