entry.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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_CONFIG5 16, 5
  53. #define C0_DDATA_LO 28, 3
  54. #define C0_ERROREPC 30, 0
  55. #define CALLFRAME_SIZ 32
  56. #ifdef CONFIG_64BIT
  57. #define ST0_KX_IF_64 ST0_KX
  58. #else
  59. #define ST0_KX_IF_64 0
  60. #endif
  61. static unsigned int scratch_vcpu[2] = { C0_DDATA_LO };
  62. static unsigned int scratch_tmp[2] = { C0_ERROREPC };
  63. enum label_id {
  64. label_fpu_1 = 1,
  65. label_msa_1,
  66. label_return_to_host,
  67. label_kernel_asid,
  68. label_exit_common,
  69. };
  70. UASM_L_LA(_fpu_1)
  71. UASM_L_LA(_msa_1)
  72. UASM_L_LA(_return_to_host)
  73. UASM_L_LA(_kernel_asid)
  74. UASM_L_LA(_exit_common)
  75. static void *kvm_mips_build_enter_guest(void *addr);
  76. static void *kvm_mips_build_ret_from_exit(void *addr);
  77. static void *kvm_mips_build_ret_to_guest(void *addr);
  78. static void *kvm_mips_build_ret_to_host(void *addr);
  79. /**
  80. * kvm_mips_entry_setup() - Perform global setup for entry code.
  81. *
  82. * Perform global setup for entry code, such as choosing a scratch register.
  83. *
  84. * Returns: 0 on success.
  85. * -errno on failure.
  86. */
  87. int kvm_mips_entry_setup(void)
  88. {
  89. /*
  90. * We prefer to use KScratchN registers if they are available over the
  91. * defaults above, which may not work on all cores.
  92. */
  93. unsigned int kscratch_mask = cpu_data[0].kscratch_mask & 0xfc;
  94. /* Pick a scratch register for storing VCPU */
  95. if (kscratch_mask) {
  96. scratch_vcpu[0] = 31;
  97. scratch_vcpu[1] = ffs(kscratch_mask) - 1;
  98. kscratch_mask &= ~BIT(scratch_vcpu[1]);
  99. }
  100. /* Pick a scratch register to use as a temp for saving state */
  101. if (kscratch_mask) {
  102. scratch_tmp[0] = 31;
  103. scratch_tmp[1] = ffs(kscratch_mask) - 1;
  104. kscratch_mask &= ~BIT(scratch_tmp[1]);
  105. }
  106. return 0;
  107. }
  108. static void kvm_mips_build_save_scratch(u32 **p, unsigned int tmp,
  109. unsigned int frame)
  110. {
  111. /* Save the VCPU scratch register value in cp0_epc of the stack frame */
  112. UASM_i_MFC0(p, tmp, scratch_vcpu[0], scratch_vcpu[1]);
  113. UASM_i_SW(p, tmp, offsetof(struct pt_regs, cp0_epc), frame);
  114. /* Save the temp scratch register value in cp0_cause of stack frame */
  115. if (scratch_tmp[0] == 31) {
  116. UASM_i_MFC0(p, tmp, scratch_tmp[0], scratch_tmp[1]);
  117. UASM_i_SW(p, tmp, offsetof(struct pt_regs, cp0_cause), frame);
  118. }
  119. }
  120. static void kvm_mips_build_restore_scratch(u32 **p, unsigned int tmp,
  121. unsigned int frame)
  122. {
  123. /*
  124. * Restore host scratch register values saved by
  125. * kvm_mips_build_save_scratch().
  126. */
  127. UASM_i_LW(p, tmp, offsetof(struct pt_regs, cp0_epc), frame);
  128. UASM_i_MTC0(p, tmp, scratch_vcpu[0], scratch_vcpu[1]);
  129. if (scratch_tmp[0] == 31) {
  130. UASM_i_LW(p, tmp, offsetof(struct pt_regs, cp0_cause), frame);
  131. UASM_i_MTC0(p, tmp, scratch_tmp[0], scratch_tmp[1]);
  132. }
  133. }
  134. /**
  135. * build_set_exc_base() - Assemble code to write exception base address.
  136. * @p: Code buffer pointer.
  137. * @reg: Source register (generated code may set WG bit in @reg).
  138. *
  139. * Assemble code to modify the exception base address in the EBase register,
  140. * using the appropriately sized access and setting the WG bit if necessary.
  141. */
  142. static inline void build_set_exc_base(u32 **p, unsigned int reg)
  143. {
  144. if (cpu_has_ebase_wg) {
  145. /* Set WG so that all the bits get written */
  146. uasm_i_ori(p, reg, reg, MIPS_EBASE_WG);
  147. UASM_i_MTC0(p, reg, C0_EBASE);
  148. } else {
  149. uasm_i_mtc0(p, reg, C0_EBASE);
  150. }
  151. }
  152. /**
  153. * kvm_mips_build_vcpu_run() - Assemble function to start running a guest VCPU.
  154. * @addr: Address to start writing code.
  155. *
  156. * Assemble the start of the vcpu_run function to run a guest VCPU. The function
  157. * conforms to the following prototype:
  158. *
  159. * int vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu);
  160. *
  161. * The exit from the guest and return to the caller is handled by the code
  162. * generated by kvm_mips_build_ret_to_host().
  163. *
  164. * Returns: Next address after end of written function.
  165. */
  166. void *kvm_mips_build_vcpu_run(void *addr)
  167. {
  168. u32 *p = addr;
  169. unsigned int i;
  170. /*
  171. * A0: run
  172. * A1: vcpu
  173. */
  174. /* k0/k1 not being used in host kernel context */
  175. UASM_i_ADDIU(&p, K1, SP, -(int)sizeof(struct pt_regs));
  176. for (i = 16; i < 32; ++i) {
  177. if (i == 24)
  178. i = 28;
  179. UASM_i_SW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
  180. }
  181. /* Save host status */
  182. uasm_i_mfc0(&p, V0, C0_STATUS);
  183. UASM_i_SW(&p, V0, offsetof(struct pt_regs, cp0_status), K1);
  184. /* Save scratch registers, will be used to store pointer to vcpu etc */
  185. kvm_mips_build_save_scratch(&p, V1, K1);
  186. /* VCPU scratch register has pointer to vcpu */
  187. UASM_i_MTC0(&p, A1, scratch_vcpu[0], scratch_vcpu[1]);
  188. /* Offset into vcpu->arch */
  189. UASM_i_ADDIU(&p, K1, A1, offsetof(struct kvm_vcpu, arch));
  190. /*
  191. * Save the host stack to VCPU, used for exception processing
  192. * when we exit from the Guest
  193. */
  194. UASM_i_SW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
  195. /* Save the kernel gp as well */
  196. UASM_i_SW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
  197. /*
  198. * Setup status register for running the guest in UM, interrupts
  199. * are disabled
  200. */
  201. UASM_i_LA(&p, K0, ST0_EXL | KSU_USER | ST0_BEV | ST0_KX_IF_64);
  202. uasm_i_mtc0(&p, K0, C0_STATUS);
  203. uasm_i_ehb(&p);
  204. /* load up the new EBASE */
  205. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
  206. build_set_exc_base(&p, K0);
  207. /*
  208. * Now that the new EBASE has been loaded, unset BEV, set
  209. * interrupt mask as it was but make sure that timer interrupts
  210. * are enabled
  211. */
  212. uasm_i_addiu(&p, K0, ZERO, ST0_EXL | KSU_USER | ST0_IE | ST0_KX_IF_64);
  213. uasm_i_andi(&p, V0, V0, ST0_IM);
  214. uasm_i_or(&p, K0, K0, V0);
  215. uasm_i_mtc0(&p, K0, C0_STATUS);
  216. uasm_i_ehb(&p);
  217. p = kvm_mips_build_enter_guest(p);
  218. return p;
  219. }
  220. /**
  221. * kvm_mips_build_enter_guest() - Assemble code to resume guest execution.
  222. * @addr: Address to start writing code.
  223. *
  224. * Assemble the code to resume guest execution. This code is common between the
  225. * initial entry into the guest from the host, and returning from the exit
  226. * handler back to the guest.
  227. *
  228. * Returns: Next address after end of written function.
  229. */
  230. static void *kvm_mips_build_enter_guest(void *addr)
  231. {
  232. u32 *p = addr;
  233. unsigned int i;
  234. struct uasm_label labels[2];
  235. struct uasm_reloc relocs[2];
  236. struct uasm_label *l = labels;
  237. struct uasm_reloc *r = relocs;
  238. memset(labels, 0, sizeof(labels));
  239. memset(relocs, 0, sizeof(relocs));
  240. /* Set Guest EPC */
  241. UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, pc), K1);
  242. UASM_i_MTC0(&p, T0, C0_EPC);
  243. /* Set the ASID for the Guest Kernel */
  244. UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, cop0), K1);
  245. UASM_i_LW(&p, T0, offsetof(struct mips_coproc, reg[MIPS_CP0_STATUS][0]),
  246. T0);
  247. uasm_i_andi(&p, T0, T0, KSU_USER | ST0_ERL | ST0_EXL);
  248. uasm_i_xori(&p, T0, T0, KSU_USER);
  249. uasm_il_bnez(&p, &r, T0, label_kernel_asid);
  250. UASM_i_ADDIU(&p, T1, K1,
  251. offsetof(struct kvm_vcpu_arch, guest_kernel_asid));
  252. /* else user */
  253. UASM_i_ADDIU(&p, T1, K1,
  254. offsetof(struct kvm_vcpu_arch, guest_user_asid));
  255. uasm_l_kernel_asid(&l, p);
  256. /* t1: contains the base of the ASID array, need to get the cpu id */
  257. /* smp_processor_id */
  258. uasm_i_lw(&p, T2, offsetof(struct thread_info, cpu), GP);
  259. /* x4 */
  260. uasm_i_sll(&p, T2, T2, 2);
  261. UASM_i_ADDU(&p, T3, T1, T2);
  262. uasm_i_lw(&p, K0, 0, T3);
  263. #ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
  264. /* x sizeof(struct cpuinfo_mips)/4 */
  265. uasm_i_addiu(&p, T3, ZERO, sizeof(struct cpuinfo_mips)/4);
  266. uasm_i_mul(&p, T2, T2, T3);
  267. UASM_i_LA_mostly(&p, AT, (long)&cpu_data[0].asid_mask);
  268. UASM_i_ADDU(&p, AT, AT, T2);
  269. UASM_i_LW(&p, T2, uasm_rel_lo((long)&cpu_data[0].asid_mask), AT);
  270. uasm_i_and(&p, K0, K0, T2);
  271. #else
  272. uasm_i_andi(&p, K0, K0, MIPS_ENTRYHI_ASID);
  273. #endif
  274. uasm_i_mtc0(&p, K0, C0_ENTRYHI);
  275. uasm_i_ehb(&p);
  276. /* Disable RDHWR access */
  277. uasm_i_mtc0(&p, ZERO, C0_HWRENA);
  278. /* load the guest context from VCPU and return */
  279. for (i = 1; i < 32; ++i) {
  280. /* Guest k0/k1 loaded later */
  281. if (i == K0 || i == K1)
  282. continue;
  283. UASM_i_LW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
  284. }
  285. #ifndef CONFIG_CPU_MIPSR6
  286. /* Restore hi/lo */
  287. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, hi), K1);
  288. uasm_i_mthi(&p, K0);
  289. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, lo), K1);
  290. uasm_i_mtlo(&p, K0);
  291. #endif
  292. /* Restore the guest's k0/k1 registers */
  293. UASM_i_LW(&p, K0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
  294. UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
  295. /* Jump to guest */
  296. uasm_i_eret(&p);
  297. uasm_resolve_relocs(relocs, labels);
  298. return p;
  299. }
  300. /**
  301. * kvm_mips_build_exception() - Assemble first level guest exception handler.
  302. * @addr: Address to start writing code.
  303. * @handler: Address of common handler (within range of @addr).
  304. *
  305. * Assemble exception vector code for guest execution. The generated vector will
  306. * branch to the common exception handler generated by kvm_mips_build_exit().
  307. *
  308. * Returns: Next address after end of written function.
  309. */
  310. void *kvm_mips_build_exception(void *addr, void *handler)
  311. {
  312. u32 *p = addr;
  313. struct uasm_label labels[2];
  314. struct uasm_reloc relocs[2];
  315. struct uasm_label *l = labels;
  316. struct uasm_reloc *r = relocs;
  317. memset(labels, 0, sizeof(labels));
  318. memset(relocs, 0, sizeof(relocs));
  319. /* Save guest k1 into scratch register */
  320. UASM_i_MTC0(&p, K1, scratch_tmp[0], scratch_tmp[1]);
  321. /* Get the VCPU pointer from the VCPU scratch register */
  322. UASM_i_MFC0(&p, K1, scratch_vcpu[0], scratch_vcpu[1]);
  323. UASM_i_ADDIU(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
  324. /* Save guest k0 into VCPU structure */
  325. UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, gprs[K0]), K1);
  326. /* Branch to the common handler */
  327. uasm_il_b(&p, &r, label_exit_common);
  328. uasm_i_nop(&p);
  329. uasm_l_exit_common(&l, handler);
  330. uasm_resolve_relocs(relocs, labels);
  331. return p;
  332. }
  333. /**
  334. * kvm_mips_build_exit() - Assemble common guest exit handler.
  335. * @addr: Address to start writing code.
  336. *
  337. * Assemble the generic guest exit handling code. This is called by the
  338. * exception vectors (generated by kvm_mips_build_exception()), and calls
  339. * kvm_mips_handle_exit(), then either resumes the guest or returns to the host
  340. * depending on the return value.
  341. *
  342. * Returns: Next address after end of written function.
  343. */
  344. void *kvm_mips_build_exit(void *addr)
  345. {
  346. u32 *p = addr;
  347. unsigned int i;
  348. struct uasm_label labels[3];
  349. struct uasm_reloc relocs[3];
  350. struct uasm_label *l = labels;
  351. struct uasm_reloc *r = relocs;
  352. memset(labels, 0, sizeof(labels));
  353. memset(relocs, 0, sizeof(relocs));
  354. /*
  355. * Generic Guest exception handler. We end up here when the guest
  356. * does something that causes a trap to kernel mode.
  357. *
  358. * Both k0/k1 registers will have already been saved (k0 into the vcpu
  359. * structure, and k1 into the scratch_tmp register).
  360. *
  361. * The k1 register will already contain the kvm_vcpu_arch pointer.
  362. */
  363. /* Start saving Guest context to VCPU */
  364. for (i = 0; i < 32; ++i) {
  365. /* Guest k0/k1 saved later */
  366. if (i == K0 || i == K1)
  367. continue;
  368. UASM_i_SW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), K1);
  369. }
  370. #ifndef CONFIG_CPU_MIPSR6
  371. /* We need to save hi/lo and restore them on the way out */
  372. uasm_i_mfhi(&p, T0);
  373. UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, hi), K1);
  374. uasm_i_mflo(&p, T0);
  375. UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, lo), K1);
  376. #endif
  377. /* Finally save guest k1 to VCPU */
  378. uasm_i_ehb(&p);
  379. UASM_i_MFC0(&p, T0, scratch_tmp[0], scratch_tmp[1]);
  380. UASM_i_SW(&p, T0, offsetof(struct kvm_vcpu_arch, gprs[K1]), K1);
  381. /* Now that context has been saved, we can use other registers */
  382. /* Restore vcpu */
  383. UASM_i_MFC0(&p, A1, scratch_vcpu[0], scratch_vcpu[1]);
  384. uasm_i_move(&p, S1, A1);
  385. /* Restore run (vcpu->run) */
  386. UASM_i_LW(&p, A0, offsetof(struct kvm_vcpu, run), A1);
  387. /* Save pointer to run in s0, will be saved by the compiler */
  388. uasm_i_move(&p, S0, A0);
  389. /*
  390. * Save Host level EPC, BadVaddr and Cause to VCPU, useful to process
  391. * the exception
  392. */
  393. UASM_i_MFC0(&p, K0, C0_EPC);
  394. UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, pc), K1);
  395. UASM_i_MFC0(&p, K0, C0_BADVADDR);
  396. UASM_i_SW(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_badvaddr),
  397. K1);
  398. uasm_i_mfc0(&p, K0, C0_CAUSE);
  399. uasm_i_sw(&p, K0, offsetof(struct kvm_vcpu_arch, host_cp0_cause), K1);
  400. /* Now restore the host state just enough to run the handlers */
  401. /* Switch EBASE to the one used by Linux */
  402. /* load up the host EBASE */
  403. uasm_i_mfc0(&p, V0, C0_STATUS);
  404. uasm_i_lui(&p, AT, ST0_BEV >> 16);
  405. uasm_i_or(&p, K0, V0, AT);
  406. uasm_i_mtc0(&p, K0, C0_STATUS);
  407. uasm_i_ehb(&p);
  408. UASM_i_LA_mostly(&p, K0, (long)&ebase);
  409. UASM_i_LW(&p, K0, uasm_rel_lo((long)&ebase), K0);
  410. build_set_exc_base(&p, K0);
  411. if (raw_cpu_has_fpu) {
  412. /*
  413. * If FPU is enabled, save FCR31 and clear it so that later
  414. * ctc1's don't trigger FPE for pending exceptions.
  415. */
  416. uasm_i_lui(&p, AT, ST0_CU1 >> 16);
  417. uasm_i_and(&p, V1, V0, AT);
  418. uasm_il_beqz(&p, &r, V1, label_fpu_1);
  419. uasm_i_nop(&p);
  420. uasm_i_cfc1(&p, T0, 31);
  421. uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.fcr31),
  422. K1);
  423. uasm_i_ctc1(&p, ZERO, 31);
  424. uasm_l_fpu_1(&l, p);
  425. }
  426. if (cpu_has_msa) {
  427. /*
  428. * If MSA is enabled, save MSACSR and clear it so that later
  429. * instructions don't trigger MSAFPE for pending exceptions.
  430. */
  431. uasm_i_mfc0(&p, T0, C0_CONFIG5);
  432. uasm_i_ext(&p, T0, T0, 27, 1); /* MIPS_CONF5_MSAEN */
  433. uasm_il_beqz(&p, &r, T0, label_msa_1);
  434. uasm_i_nop(&p);
  435. uasm_i_cfcmsa(&p, T0, MSA_CSR);
  436. uasm_i_sw(&p, T0, offsetof(struct kvm_vcpu_arch, fpu.msacsr),
  437. K1);
  438. uasm_i_ctcmsa(&p, MSA_CSR, ZERO);
  439. uasm_l_msa_1(&l, p);
  440. }
  441. /* Now that the new EBASE has been loaded, unset BEV and KSU_USER */
  442. uasm_i_addiu(&p, AT, ZERO, ~(ST0_EXL | KSU_USER | ST0_IE));
  443. uasm_i_and(&p, V0, V0, AT);
  444. uasm_i_lui(&p, AT, ST0_CU0 >> 16);
  445. uasm_i_or(&p, V0, V0, AT);
  446. #ifdef CONFIG_64BIT
  447. uasm_i_ori(&p, V0, V0, ST0_SX | ST0_UX);
  448. #endif
  449. uasm_i_mtc0(&p, V0, C0_STATUS);
  450. uasm_i_ehb(&p);
  451. /* Load up host GP */
  452. UASM_i_LW(&p, GP, offsetof(struct kvm_vcpu_arch, host_gp), K1);
  453. /* Need a stack before we can jump to "C" */
  454. UASM_i_LW(&p, SP, offsetof(struct kvm_vcpu_arch, host_stack), K1);
  455. /* Saved host state */
  456. UASM_i_ADDIU(&p, SP, SP, -(int)sizeof(struct pt_regs));
  457. /*
  458. * XXXKYMA do we need to load the host ASID, maybe not because the
  459. * kernel entries are marked GLOBAL, need to verify
  460. */
  461. /* Restore host scratch registers, as we'll have clobbered them */
  462. kvm_mips_build_restore_scratch(&p, K0, SP);
  463. /* Restore RDHWR access */
  464. UASM_i_LA_mostly(&p, K0, (long)&hwrena);
  465. uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
  466. uasm_i_mtc0(&p, K0, C0_HWRENA);
  467. /* Jump to handler */
  468. /*
  469. * XXXKYMA: not sure if this is safe, how large is the stack??
  470. * Now jump to the kvm_mips_handle_exit() to see if we can deal
  471. * with this in the kernel
  472. */
  473. UASM_i_LA(&p, T9, (unsigned long)kvm_mips_handle_exit);
  474. uasm_i_jalr(&p, RA, T9);
  475. UASM_i_ADDIU(&p, SP, SP, -CALLFRAME_SIZ);
  476. uasm_resolve_relocs(relocs, labels);
  477. p = kvm_mips_build_ret_from_exit(p);
  478. return p;
  479. }
  480. /**
  481. * kvm_mips_build_ret_from_exit() - Assemble guest exit return handler.
  482. * @addr: Address to start writing code.
  483. *
  484. * Assemble the code to handle the return from kvm_mips_handle_exit(), either
  485. * resuming the guest or returning to the host depending on the return value.
  486. *
  487. * Returns: Next address after end of written function.
  488. */
  489. static void *kvm_mips_build_ret_from_exit(void *addr)
  490. {
  491. u32 *p = addr;
  492. struct uasm_label labels[2];
  493. struct uasm_reloc relocs[2];
  494. struct uasm_label *l = labels;
  495. struct uasm_reloc *r = relocs;
  496. memset(labels, 0, sizeof(labels));
  497. memset(relocs, 0, sizeof(relocs));
  498. /* Return from handler Make sure interrupts are disabled */
  499. uasm_i_di(&p, ZERO);
  500. uasm_i_ehb(&p);
  501. /*
  502. * XXXKYMA: k0/k1 could have been blown away if we processed
  503. * an exception while we were handling the exception from the
  504. * guest, reload k1
  505. */
  506. uasm_i_move(&p, K1, S1);
  507. UASM_i_ADDIU(&p, K1, K1, offsetof(struct kvm_vcpu, arch));
  508. /*
  509. * Check return value, should tell us if we are returning to the
  510. * host (handle I/O etc)or resuming the guest
  511. */
  512. uasm_i_andi(&p, T0, V0, RESUME_HOST);
  513. uasm_il_bnez(&p, &r, T0, label_return_to_host);
  514. uasm_i_nop(&p);
  515. p = kvm_mips_build_ret_to_guest(p);
  516. uasm_l_return_to_host(&l, p);
  517. p = kvm_mips_build_ret_to_host(p);
  518. uasm_resolve_relocs(relocs, labels);
  519. return p;
  520. }
  521. /**
  522. * kvm_mips_build_ret_to_guest() - Assemble code to return to the guest.
  523. * @addr: Address to start writing code.
  524. *
  525. * Assemble the code to handle return from the guest exit handler
  526. * (kvm_mips_handle_exit()) back to the guest.
  527. *
  528. * Returns: Next address after end of written function.
  529. */
  530. static void *kvm_mips_build_ret_to_guest(void *addr)
  531. {
  532. u32 *p = addr;
  533. /* Put the saved pointer to vcpu (s1) back into the scratch register */
  534. UASM_i_MTC0(&p, S1, scratch_vcpu[0], scratch_vcpu[1]);
  535. /* Load up the Guest EBASE to minimize the window where BEV is set */
  536. UASM_i_LW(&p, T0, offsetof(struct kvm_vcpu_arch, guest_ebase), K1);
  537. /* Switch EBASE back to the one used by KVM */
  538. uasm_i_mfc0(&p, V1, C0_STATUS);
  539. uasm_i_lui(&p, AT, ST0_BEV >> 16);
  540. uasm_i_or(&p, K0, V1, AT);
  541. uasm_i_mtc0(&p, K0, C0_STATUS);
  542. uasm_i_ehb(&p);
  543. build_set_exc_base(&p, T0);
  544. /* Setup status register for running guest in UM */
  545. uasm_i_ori(&p, V1, V1, ST0_EXL | KSU_USER | ST0_IE);
  546. UASM_i_LA(&p, AT, ~(ST0_CU0 | ST0_MX | ST0_SX | ST0_UX));
  547. uasm_i_and(&p, V1, V1, AT);
  548. uasm_i_mtc0(&p, V1, C0_STATUS);
  549. uasm_i_ehb(&p);
  550. p = kvm_mips_build_enter_guest(p);
  551. return p;
  552. }
  553. /**
  554. * kvm_mips_build_ret_to_host() - Assemble code to return to the host.
  555. * @addr: Address to start writing code.
  556. *
  557. * Assemble the code to handle return from the guest exit handler
  558. * (kvm_mips_handle_exit()) back to the host, i.e. to the caller of the vcpu_run
  559. * function generated by kvm_mips_build_vcpu_run().
  560. *
  561. * Returns: Next address after end of written function.
  562. */
  563. static void *kvm_mips_build_ret_to_host(void *addr)
  564. {
  565. u32 *p = addr;
  566. unsigned int i;
  567. /* EBASE is already pointing to Linux */
  568. UASM_i_LW(&p, K1, offsetof(struct kvm_vcpu_arch, host_stack), K1);
  569. UASM_i_ADDIU(&p, K1, K1, -(int)sizeof(struct pt_regs));
  570. /*
  571. * r2/v0 is the return code, shift it down by 2 (arithmetic)
  572. * to recover the err code
  573. */
  574. uasm_i_sra(&p, K0, V0, 2);
  575. uasm_i_move(&p, V0, K0);
  576. /* Load context saved on the host stack */
  577. for (i = 16; i < 31; ++i) {
  578. if (i == 24)
  579. i = 28;
  580. UASM_i_LW(&p, i, offsetof(struct pt_regs, regs[i]), K1);
  581. }
  582. /* Restore RDHWR access */
  583. UASM_i_LA_mostly(&p, K0, (long)&hwrena);
  584. uasm_i_lw(&p, K0, uasm_rel_lo((long)&hwrena), K0);
  585. uasm_i_mtc0(&p, K0, C0_HWRENA);
  586. /* Restore RA, which is the address we will return to */
  587. UASM_i_LW(&p, RA, offsetof(struct pt_regs, regs[RA]), K1);
  588. uasm_i_jr(&p, RA);
  589. uasm_i_nop(&p);
  590. return p;
  591. }