process.c 22 KB

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