entry.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. * Generation of main entry point for the guest, exception handling.
  7. *
  8. * Copyright (C) 2012 MIPS Technologies, Inc.
  9. * Authors: Sanjay Lal <sanjayl@kymasys.com>
  10. *
  11. * Copyright (C) 2016 Imagination Technologies Ltd.
  12. */
  13. #include <linux/kvm_host.h>
  14. #include <asm/msa.h>
  15. #include <asm/setup.h>
  16. #include <asm/uasm.h>
  17. /* Register names */
  18. #define ZERO 0
  19. #define AT 1
  20. #define V0 2
  21. #define V1 3
  22. #define A0 4
  23. #define A1 5
  24. #if _MIPS_SIM == _MIPS_SIM_ABI32
  25. #define T0 8
  26. #define T1 9
  27. #define T2 10
  28. #define T3 11
  29. #endif /* _MIPS_SIM == _MIPS_SIM_ABI32 */
  30. #if _MIPS_SIM == _MIPS_SIM_ABI64 || _MIPS_SIM == _MIPS_SIM_NABI32
  31. #define T0 12
  32. #define T1 13
  33. #define T2 14
  34. #define T3 15
  35. #endif /* _MIPS_SIM == _MIPS_SIM_ABI64 || _MIPS_SIM == _MIPS_SIM_NABI32 */
  36. #define S0 16
  37. #define S1 17
  38. #define T9 25
  39. #define K0 26
  40. #define K1 27
  41. #define GP 28
  42. #define SP 29
  43. #define RA 31
  44. /* Some CP0 registers */
  45. #define C0_HWRENA 7, 0
  46. #define C0_BADVADDR 8, 0
  47. #define C0_ENTRYHI 10, 0
  48. #define C0_STATUS 12, 0
  49. #define C0_CAUSE 13, 0
  50. #define C0_EPC 14, 0
  51. #define C0_EBASE 15, 1
  52. #define C0_CONFIG3 16, 3
  53. #define C0_CONFIG5 16, 5
  54. #define C0_DDATA_LO 28, 3
  55. #define C0_ERROREPC 30, 0
  56. #define CALLFRAME_SIZ 32
  57. enum label_id {
  58. label_fpu_1 = 1,
  59. label_msa_1,
  60. label_return_to_host,
  61. label_kernel_asid,
  62. };
  63. UASM_L_LA(_fpu_1)
  64. UASM_L_LA(_msa_1)
  65. UASM_L_LA(_return_to_host)
  66. UASM_L_LA(_kernel_asid)
  67. static void *kvm_mips_build_enter_guest(void *addr);
  68. static void *kvm_mips_build_ret_from_exit(void *addr);
  69. static void *kvm_mips_build_ret_to_guest(void *addr);
  70. static void *kvm_mips_build_ret_to_host(void *addr);
  71. /**
  72. * kvm_mips_build_vcpu_run() - Assemble function to start running a guest VCPU.
  73. * @addr: Address to start writing code.
  74. *
  75. * Assemble the start of the vcpu_run function to run a guest VCPU. The function
  76. * conforms to the following prototype:
  77. *
  78. * int vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu);
  79. *
  80. * The exit from the guest and return to the caller is handled by the code
  81. * generated by kvm_mips_build_ret_to_host().
  82. *
  83. * Returns: Next address after end of written function.
  84. */
  85. void *kvm_mips_build_vcpu_run(void *addr)
  86. {
  87. u32 *p = addr;
  88. unsigned int i;
  89. /*
  90. * A0: run
  91. * A1: vcpu
  92. */
  93. /* k0/k1 not being used in host kernel context */
  94. uasm_i_addiu(&p, K1, SP, -(int)sizeof(struct pt_regs));
  95. for (i = 16; i < 32; ++i) {
  96. if (i == 24)
  97. i = 28;
  98. UASM_i_SW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
  99. }
  100. /* Save hi/lo */
  101. uasm_i_mflo(&p, V0);
  102. UASM_i_SW(&p, V0, offsetof(struct pt_regs, lo), K1);
  103. uasm_i_mfhi(&p, V1);
  104. UASM_i_SW(&p, V1, offsetof(struct pt_regs, hi), K1);
  105. /* Save host status */
  106. uasm_i_mfc0(&p, V0, C0_STATUS);
  107. UASM_i_SW(&p, V0, offsetof(struct pt_regs, cp0_status), K1);
  108. /* Save DDATA_LO, will be used to store pointer to vcpu */
  109. uasm_i_mfc0(&p, V1, C0_DDATA_LO);
  110. UASM_i_SW(&p, V1, offsetof(struct pt_regs, cp0_epc), K1);
  111. /* DDATA_LO has pointer to vcpu */
  112. uasm_i_mtc0(&p, A1, C0_DDATA_LO);
  113. /* Offset into vcpu->arch */
  114. uasm_i_addiu(&p, K1, A1, offsetof(struct kvm_vcpu, arch));
  115. /*
  116. * Save the host stack to VCPU, used for exception processing
  117. * when we exit from the Guest
  118. */
  119. UASM_i_SW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
  120. /* Save the kernel gp as well */
  121. UASM_i_SW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
  122. /*
  123. * Setup status register for running the guest in UM, interrupts
  124. * are disabled
  125. */
  126. UASM_i_LA(&p, K0, ST0_EXL | KSU_USER | ST0_BEV);
  127. uasm_i_mtc0(&p, K0, C0_STATUS);
  128. uasm_i_ehb(&p);
  129. /* load up the new EBASE */
  130. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
  131. uasm_i_mtc0(&p, K0, C0_EBASE);
  132. /*
  133. * Now that the new EBASE has been loaded, unset BEV, set
  134. * interrupt mask as it was but make sure that timer interrupts
  135. * are enabled
  136. */
  137. uasm_i_addiu(&p, K0, ZERO, ST0_EXL | KSU_USER | ST0_IE);
  138. uasm_i_andi(&p, V0, V0, ST0_IM);
  139. uasm_i_or(&p, K0, K0, V0);
  140. uasm_i_mtc0(&p, K0, C0_STATUS);
  141. uasm_i_ehb(&p);
  142. p = kvm_mips_build_enter_guest(p);
  143. return p;
  144. }
  145. /**
  146. * kvm_mips_build_enter_guest() - Assemble code to resume guest execution.
  147. * @addr: Address to start writing code.
  148. *
  149. * Assemble the code to resume guest execution. This code is common between the
  150. * initial entry into the guest from the host, and returning from the exit
  151. * handler back to the guest.
  152. *
  153. * Returns: Next address after end of written function.
  154. */
  155. static void *kvm_mips_build_enter_guest(void *addr)
  156. {
  157. u32 *p = addr;
  158. unsigned int i;
  159. struct uasm_label labels[2];
  160. struct uasm_reloc relocs[2];
  161. struct uasm_label *l = labels;
  162. struct uasm_reloc *r = relocs;
  163. memset(labels, 0, sizeof(labels));
  164. memset(relocs, 0, sizeof(relocs));
  165. /* Set Guest EPC */
  166. UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, pc), K1);
  167. uasm_i_mtc0(&p, T0, C0_EPC);
  168. /* Set the ASID for the Guest Kernel */
  169. UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, cop0), K1);
  170. UASM_i_LW(&p, T0, offsetof(struct mips_coproc, reg[MIPS_CP0_STATUS][0]),
  171. T0);
  172. uasm_i_andi(&p, T0, T0, KSU_USER | ST0_ERL | ST0_EXL);
  173. uasm_i_xori(&p, T0, T0, KSU_USER);
  174. uasm_il_bnez(&p, &r, T0, label_kernel_asid);
  175. uasm_i_addiu(&p, T1, K1,
  176. offsetof(struct kvm_vcpu_arch, guest_kernel_asid));
  177. /* else user */
  178. uasm_i_addiu(&p, T1, K1,
  179. offsetof(struct kvm_vcpu_arch, guest_user_asid));
  180. uasm_l_kernel_asid(&l, p);
  181. /* t1: contains the base of the ASID array, need to get the cpu id */
  182. /* smp_processor_id */
  183. UASM_i_LW(&p, T2, offsetof(struct thread_info, cpu), GP);
  184. /* x4 */
  185. uasm_i_sll(&p, T2, T2, 2);
  186. UASM_i_ADDU(&p, T3, T1, T2);
  187. UASM_i_LW(&p, K0, 0, T3);
  188. #ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
  189. /* x sizeof(struct cpuinfo_mips)/4 */
  190. uasm_i_addiu(&p, T3, ZERO, sizeof(struct cpuinfo_mips)/4);
  191. uasm_i_mul(&p, T2, T2, T3);
  192. UASM_i_LA_mostly(&p, AT, (long)&cpu_data[0].asid_mask);
  193. UASM_i_ADDU(&p, AT, AT, T2);
  194. UASM_i_LW(&p, T2, uasm_rel_lo((long)&cpu_data[0].asid_mask), AT);
  195. uasm_i_and(&p, K0, K0, T2);
  196. #else
  197. uasm_i_andi(&p, K0, K0, MIPS_ENTRYHI_ASID);
  198. #endif
  199. uasm_i_mtc0(&p, K0, C0_ENTRYHI);
  200. uasm_i_ehb(&p);
  201. /* Disable RDHWR access */
  202. uasm_i_mtc0(&p, ZERO, C0_HWRENA);
  203. /* load the guest context from VCPU and return */
  204. for (i = 1; i < 32; ++i) {
  205. /* Guest k0/k1 loaded later */
  206. if (i == K0 || i == K1)
  207. continue;
  208. UASM_i_LW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
  209. }
  210. /* Restore hi/lo */
  211. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, hi), K1);
  212. uasm_i_mthi(&p, K0);
  213. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, lo), K1);
  214. uasm_i_mtlo(&p, K0);
  215. /* Restore the guest's k0/k1 registers */
  216. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
  217. UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
  218. /* Jump to guest */
  219. uasm_i_eret(&p);
  220. uasm_resolve_relocs(relocs, labels);
  221. return p;
  222. }
  223. /**
  224. * kvm_mips_build_exception() - Assemble first level guest exception handler.
  225. * @addr: Address to start writing code.
  226. *
  227. * Assemble exception vector code for guest execution. The generated vector will
  228. * jump to the common exception handler generated by kvm_mips_build_exit().
  229. *
  230. * Returns: Next address after end of written function.
  231. */
  232. void *kvm_mips_build_exception(void *addr)
  233. {
  234. u32 *p = addr;
  235. /* Save guest k0 */
  236. uasm_i_mtc0(&p, K0, C0_ERROREPC);
  237. uasm_i_ehb(&p);
  238. /* Get EBASE */
  239. uasm_i_mfc0(&p, K0, C0_EBASE);
  240. /* Get rid of CPUNum */
  241. uasm_i_srl(&p, K0, K0, 10);
  242. uasm_i_sll(&p, K0, K0, 10);
  243. /* Save k1 @ offset 0x3000 */
  244. UASM_i_SW(&p, K1, 0x3000, K0);
  245. /* Exception handler is installed @ offset 0x2000 */
  246. uasm_i_addiu(&p, K0, K0, 0x2000);
  247. /* Jump to the function */
  248. uasm_i_jr(&p, K0);
  249. uasm_i_nop(&p);
  250. return p;
  251. }
  252. /**
  253. * kvm_mips_build_exit() - Assemble common guest exit handler.
  254. * @addr: Address to start writing code.
  255. *
  256. * Assemble the generic guest exit handling code. This is called by the
  257. * exception vectors (generated by kvm_mips_build_exception()), and calls
  258. * kvm_mips_handle_exit(), then either resumes the guest or returns to the host
  259. * depending on the return value.
  260. *
  261. * Returns: Next address after end of written function.
  262. */
  263. void *kvm_mips_build_exit(void *addr)
  264. {
  265. u32 *p = addr;
  266. unsigned int i;
  267. struct uasm_label labels[3];
  268. struct uasm_reloc relocs[3];
  269. struct uasm_label *l = labels;
  270. struct uasm_reloc *r = relocs;
  271. memset(labels, 0, sizeof(labels));
  272. memset(relocs, 0, sizeof(relocs));
  273. /*
  274. * Generic Guest exception handler. We end up here when the guest
  275. * does something that causes a trap to kernel mode.
  276. */
  277. /* Get the VCPU pointer from DDATA_LO */
  278. uasm_i_mfc0(&p, K1, C0_DDATA_LO);
  279. uasm_i_addiu(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
  280. /* Start saving Guest context to VCPU */
  281. for (i = 0; i < 32; ++i) {
  282. /* Guest k0/k1 saved later */
  283. if (i == K0 || i == K1)
  284. continue;
  285. UASM_i_SW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
  286. }
  287. /* We need to save hi/lo and restore them on the way out */
  288. uasm_i_mfhi(&p, T0);
  289. UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, hi), K1);
  290. uasm_i_mflo(&p, T0);
  291. UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, lo), K1);
  292. /* Finally save guest k0/k1 to VCPU */
  293. uasm_i_mfc0(&p, T0, C0_ERROREPC);
  294. UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
  295. /* Get GUEST k1 and save it in VCPU */
  296. uasm_i_addiu(&p, T1, ZERO, ~0x2ff);
  297. uasm_i_mfc0(&p, T0, C0_EBASE);
  298. uasm_i_and(&p, T0, T0, T1);
  299. UASM_i_LW(&p, T0, 0x3000, T0);
  300. UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
  301. /* Now that context has been saved, we can use other registers */
  302. /* Restore vcpu */
  303. uasm_i_mfc0(&p, A1, C0_DDATA_LO);
  304. uasm_i_move(&p, S1, A1);
  305. /* Restore run (vcpu->run) */
  306. UASM_i_LW(&p, A0, offsetof(struct kvm_vcpu, run), A1);
  307. /* Save pointer to run in s0, will be saved by the compiler */
  308. uasm_i_move(&p, S0, A0);
  309. /*
  310. * Save Host level EPC, BadVaddr and Cause to VCPU, useful to process
  311. * the exception
  312. */
  313. uasm_i_mfc0(&p, K0, C0_EPC);
  314. UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, pc), K1);
  315. uasm_i_mfc0(&p, K0, C0_BADVADDR);
  316. UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_badvaddr),
  317. K1);
  318. uasm_i_mfc0(&p, K0, C0_CAUSE);
  319. uasm_i_sw(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_cause), K1);
  320. /* Now restore the host state just enough to run the handlers */
  321. /* Switch EBASE to the one used by Linux */
  322. /* load up the host EBASE */
  323. uasm_i_mfc0(&p, V0, C0_STATUS);
  324. uasm_i_lui(&p, AT, ST0_BEV >> 16);
  325. uasm_i_or(&p, K0, V0, AT);
  326. uasm_i_mtc0(&p, K0, C0_STATUS);
  327. uasm_i_ehb(&p);
  328. UASM_i_LA_mostly(&p, K0, (long)&ebase);
  329. UASM_i_LW(&p, K0, uasm_rel_lo((long)&ebase), K0);
  330. uasm_i_mtc0(&p, K0, C0_EBASE);
  331. /*
  332. * If FPU is enabled, save FCR31 and clear it so that later ctc1's don't
  333. * trigger FPE for pending exceptions.
  334. */
  335. uasm_i_lui(&p, AT, ST0_CU1 >> 16);
  336. uasm_i_and(&p, V1, V0, AT);
  337. uasm_il_beqz(&p, &r, V1, label_fpu_1);
  338. uasm_i_nop(&p);
  339. uasm_i_cfc1(&p, T0, 31);
  340. uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.fcr31), K1);
  341. uasm_i_ctc1(&p, ZERO, 31);
  342. uasm_l_fpu_1(&l, p);
  343. #ifdef CONFIG_CPU_HAS_MSA
  344. /*
  345. * If MSA is enabled, save MSACSR and clear it so that later
  346. * instructions don't trigger MSAFPE for pending exceptions.
  347. */
  348. uasm_i_mfc0(&p, T0, C0_CONFIG3);
  349. uasm_i_ext(&p, T0, T0, 28, 1); /* MIPS_CONF3_MSAP */
  350. uasm_il_beqz(&p, &r, T0, label_msa_1);
  351. uasm_i_nop(&p);
  352. uasm_i_mfc0(&p, T0, C0_CONFIG5);
  353. uasm_i_ext(&p, T0, T0, 27, 1); /* MIPS_CONF5_MSAEN */
  354. uasm_il_beqz(&p, &r, T0, label_msa_1);
  355. uasm_i_nop(&p);
  356. uasm_i_cfcmsa(&p, T0, MSA_CSR);
  357. uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.msacsr),
  358. K1);
  359. uasm_i_ctcmsa(&p, MSA_CSR, ZERO);
  360. uasm_l_msa_1(&l, p);
  361. #endif
  362. /* Now that the new EBASE has been loaded, unset BEV and KSU_USER */
  363. uasm_i_addiu(&p, AT, ZERO, ~(ST0_EXL | KSU_USER | ST0_IE));
  364. uasm_i_and(&p, V0, V0, AT);
  365. uasm_i_lui(&p, AT, ST0_CU0 >> 16);
  366. uasm_i_or(&p, V0, V0, AT);
  367. uasm_i_mtc0(&p, V0, C0_STATUS);
  368. uasm_i_ehb(&p);
  369. /* Load up host GP */
  370. UASM_i_LW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
  371. /* Need a stack before we can jump to "C" */
  372. UASM_i_LW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
  373. /* Saved host state */
  374. uasm_i_addiu(&p, SP, SP, -(int)sizeof(struct pt_regs));
  375. /*
  376. * XXXKYMA do we need to load the host ASID, maybe not because the
  377. * kernel entries are marked GLOBAL, need to verify
  378. */
  379. /* Restore host DDATA_LO */
  380. UASM_i_LW(&p, K0, offsetof(struct pt_regs, cp0_epc), SP);
  381. uasm_i_mtc0(&p, K0, C0_DDATA_LO);
  382. /* Restore RDHWR access */
  383. UASM_i_LA_mostly(&p, K0, (long)&hwrena);
  384. uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
  385. uasm_i_mtc0(&p, K0, C0_HWRENA);
  386. /* Jump to handler */
  387. /*
  388. * XXXKYMA: not sure if this is safe, how large is the stack??
  389. * Now jump to the kvm_mips_handle_exit() to see if we can deal
  390. * with this in the kernel
  391. */
  392. UASM_i_LA(&p, T9, (unsigned long)kvm_mips_handle_exit);
  393. uasm_i_jalr(&p, RA, T9);
  394. uasm_i_addiu(&p, SP, SP, -CALLFRAME_SIZ);
  395. uasm_resolve_relocs(relocs, labels);
  396. p = kvm_mips_build_ret_from_exit(p);
  397. return p;
  398. }
  399. /**
  400. * kvm_mips_build_ret_from_exit() - Assemble guest exit return handler.
  401. * @addr: Address to start writing code.
  402. *
  403. * Assemble the code to handle the return from kvm_mips_handle_exit(), either
  404. * resuming the guest or returning to the host depending on the return value.
  405. *
  406. * Returns: Next address after end of written function.
  407. */
  408. static void *kvm_mips_build_ret_from_exit(void *addr)
  409. {
  410. u32 *p = addr;
  411. struct uasm_label labels[2];
  412. struct uasm_reloc relocs[2];
  413. struct uasm_label *l = labels;
  414. struct uasm_reloc *r = relocs;
  415. memset(labels, 0, sizeof(labels));
  416. memset(relocs, 0, sizeof(relocs));
  417. /* Return from handler Make sure interrupts are disabled */
  418. uasm_i_di(&p, ZERO);
  419. uasm_i_ehb(&p);
  420. /*
  421. * XXXKYMA: k0/k1 could have been blown away if we processed
  422. * an exception while we were handling the exception from the
  423. * guest, reload k1
  424. */
  425. uasm_i_move(&p, K1, S1);
  426. uasm_i_addiu(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
  427. /*
  428. * Check return value, should tell us if we are returning to the
  429. * host (handle I/O etc)or resuming the guest
  430. */
  431. uasm_i_andi(&p, T0, V0, RESUME_HOST);
  432. uasm_il_bnez(&p, &r, T0, label_return_to_host);
  433. uasm_i_nop(&p);
  434. p = kvm_mips_build_ret_to_guest(p);
  435. uasm_l_return_to_host(&l, p);
  436. p = kvm_mips_build_ret_to_host(p);
  437. uasm_resolve_relocs(relocs, labels);
  438. return p;
  439. }
  440. /**
  441. * kvm_mips_build_ret_to_guest() - Assemble code to return to the guest.
  442. * @addr: Address to start writing code.
  443. *
  444. * Assemble the code to handle return from the guest exit handler
  445. * (kvm_mips_handle_exit()) back to the guest.
  446. *
  447. * Returns: Next address after end of written function.
  448. */
  449. static void *kvm_mips_build_ret_to_guest(void *addr)
  450. {
  451. u32 *p = addr;
  452. /* Put the saved pointer to vcpu (s1) back into the DDATA_LO Register */
  453. uasm_i_mtc0(&p, S1, C0_DDATA_LO);
  454. /* Load up the Guest EBASE to minimize the window where BEV is set */
  455. UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
  456. /* Switch EBASE back to the one used by KVM */
  457. uasm_i_mfc0(&p, V1, C0_STATUS);
  458. uasm_i_lui(&p, AT, ST0_BEV >> 16);
  459. uasm_i_or(&p, K0, V1, AT);
  460. uasm_i_mtc0(&p, K0, C0_STATUS);
  461. uasm_i_ehb(&p);
  462. uasm_i_mtc0(&p, T0, C0_EBASE);
  463. /* Setup status register for running guest in UM */
  464. uasm_i_ori(&p, V1, V1, ST0_EXL | KSU_USER | ST0_IE);
  465. UASM_i_LA(&p, AT, ~(ST0_CU0 | ST0_MX));
  466. uasm_i_and(&p, V1, V1, AT);
  467. uasm_i_mtc0(&p, V1, C0_STATUS);
  468. uasm_i_ehb(&p);
  469. p = kvm_mips_build_enter_guest(p);
  470. return p;
  471. }
  472. /**
  473. * kvm_mips_build_ret_to_host() - Assemble code to return to the host.
  474. * @addr: Address to start writing code.
  475. *
  476. * Assemble the code to handle return from the guest exit handler
  477. * (kvm_mips_handle_exit()) back to the host, i.e. to the caller of the vcpu_run
  478. * function generated by kvm_mips_build_vcpu_run().
  479. *
  480. * Returns: Next address after end of written function.
  481. */
  482. static void *kvm_mips_build_ret_to_host(void *addr)
  483. {
  484. u32 *p = addr;
  485. unsigned int i;
  486. /* EBASE is already pointing to Linux */
  487. UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, host_stack), K1);
  488. uasm_i_addiu(&p, K1, K1, -(int)sizeof(struct pt_regs));
  489. /* Restore host DDATA_LO */
  490. UASM_i_LW(&p, K0, offsetof(struct pt_regs, cp0_epc), K1);
  491. uasm_i_mtc0(&p, K0, C0_DDATA_LO);
  492. /*
  493. * r2/v0 is the return code, shift it down by 2 (arithmetic)
  494. * to recover the err code
  495. */
  496. uasm_i_sra(&p, K0, V0, 2);
  497. uasm_i_move(&p, V0, K0);
  498. /* Load context saved on the host stack */
  499. for (i = 16; i < 31; ++i) {
  500. if (i == 24)
  501. i = 28;
  502. UASM_i_LW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
  503. }
  504. UASM_i_LW(&p, K0, offsetof(struct pt_regs, hi), K1);
  505. uasm_i_mthi(&p, K0);
  506. UASM_i_LW(&p, K0, offsetof(struct pt_regs, lo), K1);
  507. uasm_i_mtlo(&p, K0);
  508. /* Restore RDHWR access */
  509. UASM_i_LA_mostly(&p, K0, (long)&hwrena);
  510. uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
  511. uasm_i_mtc0(&p, K0, C0_HWRENA);
  512. /* Restore RA, which is the address we will return to */
  513. UASM_i_LW(&p, RA, offsetof(struct pt_regs, regs[RA]), K1);
  514. uasm_i_jr(&p, RA);
  515. uasm_i_nop(&p);
  516. return p;
  517. }