process.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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;
  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. for (i = 0; i < max_insns; i++) {
  342. ip = (void *)ip + last_insn_size;
  343. if (is_mmips && mm_insn_16bit(ip->halfword[0])) {
  344. insn.word = ip->halfword[0] << 16;
  345. last_insn_size = 2;
  346. } else if (is_mmips) {
  347. insn.word = ip->halfword[0] << 16 | ip->halfword[1];
  348. last_insn_size = 4;
  349. } else {
  350. insn.word = ip->word;
  351. last_insn_size = 4;
  352. }
  353. if (!info->frame_size) {
  354. is_sp_move_ins(&insn, &info->frame_size);
  355. continue;
  356. } else if (!saw_jump && is_jump_ins(ip)) {
  357. /*
  358. * If we see a jump instruction, we are finished
  359. * with the frame save.
  360. *
  361. * Some functions can have a shortcut return at
  362. * the beginning of the function, so don't start
  363. * looking for jump instruction until we see the
  364. * frame setup.
  365. *
  366. * The RA save instruction can get put into the
  367. * delay slot of the jump instruction, so look
  368. * at the next instruction, too.
  369. */
  370. saw_jump = true;
  371. continue;
  372. }
  373. if (info->pc_offset == -1 &&
  374. is_ra_save_ins(&insn, &info->pc_offset))
  375. break;
  376. if (saw_jump)
  377. break;
  378. }
  379. if (info->frame_size && info->pc_offset >= 0) /* nested */
  380. return 0;
  381. if (info->pc_offset < 0) /* leaf */
  382. return 1;
  383. /* prologue seems bogus... */
  384. err:
  385. return -1;
  386. }
  387. static struct mips_frame_info schedule_mfi __read_mostly;
  388. #ifdef CONFIG_KALLSYMS
  389. static unsigned long get___schedule_addr(void)
  390. {
  391. return kallsyms_lookup_name("__schedule");
  392. }
  393. #else
  394. static unsigned long get___schedule_addr(void)
  395. {
  396. union mips_instruction *ip = (void *)schedule;
  397. int max_insns = 8;
  398. int i;
  399. for (i = 0; i < max_insns; i++, ip++) {
  400. if (ip->j_format.opcode == j_op)
  401. return J_TARGET(ip, ip->j_format.target);
  402. }
  403. return 0;
  404. }
  405. #endif
  406. static int __init frame_info_init(void)
  407. {
  408. unsigned long size = 0;
  409. #ifdef CONFIG_KALLSYMS
  410. unsigned long ofs;
  411. #endif
  412. unsigned long addr;
  413. addr = get___schedule_addr();
  414. if (!addr)
  415. addr = (unsigned long)schedule;
  416. #ifdef CONFIG_KALLSYMS
  417. kallsyms_lookup_size_offset(addr, &size, &ofs);
  418. #endif
  419. schedule_mfi.func = (void *)addr;
  420. schedule_mfi.func_size = size;
  421. get_frame_info(&schedule_mfi);
  422. /*
  423. * Without schedule() frame info, result given by
  424. * thread_saved_pc() and get_wchan() are not reliable.
  425. */
  426. if (schedule_mfi.pc_offset < 0)
  427. printk("Can't analyze schedule() prologue at %p\n", schedule);
  428. return 0;
  429. }
  430. arch_initcall(frame_info_init);
  431. /*
  432. * Return saved PC of a blocked thread.
  433. */
  434. static unsigned long thread_saved_pc(struct task_struct *tsk)
  435. {
  436. struct thread_struct *t = &tsk->thread;
  437. /* New born processes are a special case */
  438. if (t->reg31 == (unsigned long) ret_from_fork)
  439. return t->reg31;
  440. if (schedule_mfi.pc_offset < 0)
  441. return 0;
  442. return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
  443. }
  444. #ifdef CONFIG_KALLSYMS
  445. /* generic stack unwinding function */
  446. unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
  447. unsigned long *sp,
  448. unsigned long pc,
  449. unsigned long *ra)
  450. {
  451. unsigned long low, high, irq_stack_high;
  452. struct mips_frame_info info;
  453. unsigned long size, ofs;
  454. struct pt_regs *regs;
  455. int leaf;
  456. if (!stack_page)
  457. return 0;
  458. /*
  459. * IRQ stacks start at IRQ_STACK_START
  460. * task stacks at THREAD_SIZE - 32
  461. */
  462. low = stack_page;
  463. if (!preemptible() && on_irq_stack(raw_smp_processor_id(), *sp)) {
  464. high = stack_page + IRQ_STACK_START;
  465. irq_stack_high = high;
  466. } else {
  467. high = stack_page + THREAD_SIZE - 32;
  468. irq_stack_high = 0;
  469. }
  470. /*
  471. * If we reached the top of the interrupt stack, start unwinding
  472. * the interrupted task stack.
  473. */
  474. if (unlikely(*sp == irq_stack_high)) {
  475. unsigned long task_sp = *(unsigned long *)*sp;
  476. /*
  477. * Check that the pointer saved in the IRQ stack head points to
  478. * something within the stack of the current task
  479. */
  480. if (!object_is_on_stack((void *)task_sp))
  481. return 0;
  482. /*
  483. * Follow pointer to tasks kernel stack frame where interrupted
  484. * state was saved.
  485. */
  486. regs = (struct pt_regs *)task_sp;
  487. pc = regs->cp0_epc;
  488. if (!user_mode(regs) && __kernel_text_address(pc)) {
  489. *sp = regs->regs[29];
  490. *ra = regs->regs[31];
  491. return pc;
  492. }
  493. return 0;
  494. }
  495. if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
  496. return 0;
  497. /*
  498. * Return ra if an exception occurred at the first instruction
  499. */
  500. if (unlikely(ofs == 0)) {
  501. pc = *ra;
  502. *ra = 0;
  503. return pc;
  504. }
  505. info.func = (void *)(pc - ofs);
  506. info.func_size = ofs; /* analyze from start to ofs */
  507. leaf = get_frame_info(&info);
  508. if (leaf < 0)
  509. return 0;
  510. if (*sp < low || *sp + info.frame_size > high)
  511. return 0;
  512. if (leaf)
  513. /*
  514. * For some extreme cases, get_frame_info() can
  515. * consider wrongly a nested function as a leaf
  516. * one. In that cases avoid to return always the
  517. * same value.
  518. */
  519. pc = pc != *ra ? *ra : 0;
  520. else
  521. pc = ((unsigned long *)(*sp))[info.pc_offset];
  522. *sp += info.frame_size;
  523. *ra = 0;
  524. return __kernel_text_address(pc) ? pc : 0;
  525. }
  526. EXPORT_SYMBOL(unwind_stack_by_address);
  527. /* used by show_backtrace() */
  528. unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
  529. unsigned long pc, unsigned long *ra)
  530. {
  531. unsigned long stack_page = 0;
  532. int cpu;
  533. for_each_possible_cpu(cpu) {
  534. if (on_irq_stack(cpu, *sp)) {
  535. stack_page = (unsigned long)irq_stack[cpu];
  536. break;
  537. }
  538. }
  539. if (!stack_page)
  540. stack_page = (unsigned long)task_stack_page(task);
  541. return unwind_stack_by_address(stack_page, sp, pc, ra);
  542. }
  543. #endif
  544. /*
  545. * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
  546. */
  547. unsigned long get_wchan(struct task_struct *task)
  548. {
  549. unsigned long pc = 0;
  550. #ifdef CONFIG_KALLSYMS
  551. unsigned long sp;
  552. unsigned long ra = 0;
  553. #endif
  554. if (!task || task == current || task->state == TASK_RUNNING)
  555. goto out;
  556. if (!task_stack_page(task))
  557. goto out;
  558. pc = thread_saved_pc(task);
  559. #ifdef CONFIG_KALLSYMS
  560. sp = task->thread.reg29 + schedule_mfi.frame_size;
  561. while (in_sched_functions(pc))
  562. pc = unwind_stack(task, &sp, pc, &ra);
  563. #endif
  564. out:
  565. return pc;
  566. }
  567. unsigned long mips_stack_top(void)
  568. {
  569. unsigned long top = TASK_SIZE & PAGE_MASK;
  570. /* One page for branch delay slot "emulation" */
  571. top -= PAGE_SIZE;
  572. /* Space for the VDSO, data page & GIC user page */
  573. top -= PAGE_ALIGN(current->thread.abi->vdso->size);
  574. top -= PAGE_SIZE;
  575. top -= mips_gic_present() ? PAGE_SIZE : 0;
  576. /* Space for cache colour alignment */
  577. if (cpu_has_dc_aliases)
  578. top -= shm_align_mask + 1;
  579. /* Space to randomize the VDSO base */
  580. if (current->flags & PF_RANDOMIZE)
  581. top -= VDSO_RANDOMIZE_SIZE;
  582. return top;
  583. }
  584. /*
  585. * Don't forget that the stack pointer must be aligned on a 8 bytes
  586. * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
  587. */
  588. unsigned long arch_align_stack(unsigned long sp)
  589. {
  590. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  591. sp -= get_random_int() & ~PAGE_MASK;
  592. return sp & ALMASK;
  593. }
  594. static DEFINE_PER_CPU(call_single_data_t, backtrace_csd);
  595. static struct cpumask backtrace_csd_busy;
  596. static void handle_backtrace(void *info)
  597. {
  598. nmi_cpu_backtrace(get_irq_regs());
  599. cpumask_clear_cpu(smp_processor_id(), &backtrace_csd_busy);
  600. }
  601. static void raise_backtrace(cpumask_t *mask)
  602. {
  603. call_single_data_t *csd;
  604. int cpu;
  605. for_each_cpu(cpu, mask) {
  606. /*
  607. * If we previously sent an IPI to the target CPU & it hasn't
  608. * cleared its bit in the busy cpumask then it didn't handle
  609. * our previous IPI & it's not safe for us to reuse the
  610. * call_single_data_t.
  611. */
  612. if (cpumask_test_and_set_cpu(cpu, &backtrace_csd_busy)) {
  613. pr_warn("Unable to send backtrace IPI to CPU%u - perhaps it hung?\n",
  614. cpu);
  615. continue;
  616. }
  617. csd = &per_cpu(backtrace_csd, cpu);
  618. csd->func = handle_backtrace;
  619. smp_call_function_single_async(cpu, csd);
  620. }
  621. }
  622. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
  623. {
  624. nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace);
  625. }
  626. int mips_get_process_fp_mode(struct task_struct *task)
  627. {
  628. int value = 0;
  629. if (!test_tsk_thread_flag(task, TIF_32BIT_FPREGS))
  630. value |= PR_FP_MODE_FR;
  631. if (test_tsk_thread_flag(task, TIF_HYBRID_FPREGS))
  632. value |= PR_FP_MODE_FRE;
  633. return value;
  634. }
  635. static long prepare_for_fp_mode_switch(void *unused)
  636. {
  637. /*
  638. * This is icky, but we use this to simply ensure that all CPUs have
  639. * context switched, regardless of whether they were previously running
  640. * kernel or user code. This ensures that no CPU currently has its FPU
  641. * enabled, or is about to attempt to enable it through any path other
  642. * than enable_restore_fp_context() which will wait appropriately for
  643. * fp_mode_switching to be zero.
  644. */
  645. return 0;
  646. }
  647. int mips_set_process_fp_mode(struct task_struct *task, unsigned int value)
  648. {
  649. const unsigned int known_bits = PR_FP_MODE_FR | PR_FP_MODE_FRE;
  650. struct task_struct *t;
  651. struct cpumask process_cpus;
  652. int cpu;
  653. /* If nothing to change, return right away, successfully. */
  654. if (value == mips_get_process_fp_mode(task))
  655. return 0;
  656. /* Only accept a mode change if 64-bit FP enabled for o32. */
  657. if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  658. return -EOPNOTSUPP;
  659. /* And only for o32 tasks. */
  660. if (IS_ENABLED(CONFIG_64BIT) && !test_thread_flag(TIF_32BIT_REGS))
  661. return -EOPNOTSUPP;
  662. /* Check the value is valid */
  663. if (value & ~known_bits)
  664. return -EOPNOTSUPP;
  665. /* Setting FRE without FR is not supported. */
  666. if ((value & (PR_FP_MODE_FR | PR_FP_MODE_FRE)) == PR_FP_MODE_FRE)
  667. return -EOPNOTSUPP;
  668. /* Avoid inadvertently triggering emulation */
  669. if ((value & PR_FP_MODE_FR) && raw_cpu_has_fpu &&
  670. !(raw_current_cpu_data.fpu_id & MIPS_FPIR_F64))
  671. return -EOPNOTSUPP;
  672. if ((value & PR_FP_MODE_FRE) && raw_cpu_has_fpu && !cpu_has_fre)
  673. return -EOPNOTSUPP;
  674. /* FR = 0 not supported in MIPS R6 */
  675. if (!(value & PR_FP_MODE_FR) && raw_cpu_has_fpu && cpu_has_mips_r6)
  676. return -EOPNOTSUPP;
  677. /* Indicate the new FP mode in each thread */
  678. for_each_thread(task, t) {
  679. /* Update desired FP register width */
  680. if (value & PR_FP_MODE_FR) {
  681. clear_tsk_thread_flag(t, TIF_32BIT_FPREGS);
  682. } else {
  683. set_tsk_thread_flag(t, TIF_32BIT_FPREGS);
  684. clear_tsk_thread_flag(t, TIF_MSA_CTX_LIVE);
  685. }
  686. /* Update desired FP single layout */
  687. if (value & PR_FP_MODE_FRE)
  688. set_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
  689. else
  690. clear_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
  691. }
  692. /*
  693. * We need to ensure that all threads in the process have switched mode
  694. * before returning, in order to allow userland to not worry about
  695. * races. We can do this by forcing all CPUs that any thread in the
  696. * process may be running on to schedule something else - in this case
  697. * prepare_for_fp_mode_switch().
  698. *
  699. * We begin by generating a mask of all CPUs that any thread in the
  700. * process may be running on.
  701. */
  702. cpumask_clear(&process_cpus);
  703. for_each_thread(task, t)
  704. cpumask_set_cpu(task_cpu(t), &process_cpus);
  705. /*
  706. * Now we schedule prepare_for_fp_mode_switch() on each of those CPUs.
  707. *
  708. * The CPUs may have rescheduled already since we switched mode or
  709. * generated the cpumask, but that doesn't matter. If the task in this
  710. * process is scheduled out then our scheduling
  711. * prepare_for_fp_mode_switch() will simply be redundant. If it's
  712. * scheduled in then it will already have picked up the new FP mode
  713. * whilst doing so.
  714. */
  715. get_online_cpus();
  716. for_each_cpu_and(cpu, &process_cpus, cpu_online_mask)
  717. work_on_cpu(cpu, prepare_for_fp_mode_switch, NULL);
  718. put_online_cpus();
  719. wake_up_var(&task->mm->context.fp_mode_switching);
  720. return 0;
  721. }
  722. #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
  723. void mips_dump_regs32(u32 *uregs, const struct pt_regs *regs)
  724. {
  725. unsigned int i;
  726. for (i = MIPS32_EF_R1; i <= MIPS32_EF_R31; i++) {
  727. /* k0/k1 are copied as zero. */
  728. if (i == MIPS32_EF_R26 || i == MIPS32_EF_R27)
  729. uregs[i] = 0;
  730. else
  731. uregs[i] = regs->regs[i - MIPS32_EF_R0];
  732. }
  733. uregs[MIPS32_EF_LO] = regs->lo;
  734. uregs[MIPS32_EF_HI] = regs->hi;
  735. uregs[MIPS32_EF_CP0_EPC] = regs->cp0_epc;
  736. uregs[MIPS32_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  737. uregs[MIPS32_EF_CP0_STATUS] = regs->cp0_status;
  738. uregs[MIPS32_EF_CP0_CAUSE] = regs->cp0_cause;
  739. }
  740. #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
  741. #ifdef CONFIG_64BIT
  742. void mips_dump_regs64(u64 *uregs, const struct pt_regs *regs)
  743. {
  744. unsigned int i;
  745. for (i = MIPS64_EF_R1; i <= MIPS64_EF_R31; i++) {
  746. /* k0/k1 are copied as zero. */
  747. if (i == MIPS64_EF_R26 || i == MIPS64_EF_R27)
  748. uregs[i] = 0;
  749. else
  750. uregs[i] = regs->regs[i - MIPS64_EF_R0];
  751. }
  752. uregs[MIPS64_EF_LO] = regs->lo;
  753. uregs[MIPS64_EF_HI] = regs->hi;
  754. uregs[MIPS64_EF_CP0_EPC] = regs->cp0_epc;
  755. uregs[MIPS64_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  756. uregs[MIPS64_EF_CP0_STATUS] = regs->cp0_status;
  757. uregs[MIPS64_EF_CP0_CAUSE] = regs->cp0_cause;
  758. }
  759. #endif /* CONFIG_64BIT */