process.c 17 KB

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