entry.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 <linux/log2.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/msa.h>
  18. #include <asm/regdef.h>
  19. #include <asm/setup.h>
  20. #include <asm/tlbex.h>
  21. #include <asm/uasm.h>
  22. #define CALLFRAME_SIZ 32
  23. static unsigned int scratch_vcpu[2] = { C0_DDATALO };
  24. static unsigned int scratch_tmp[2] = { C0_ERROREPC };
  25. enum label_id {
  26. label_fpu_1 = 1,
  27. label_msa_1,
  28. label_return_to_host,
  29. label_kernel_asid,
  30. label_exit_common,
  31. };
  32. UASM_L_LA(_fpu_1)
  33. UASM_L_LA(_msa_1)
  34. UASM_L_LA(_return_to_host)
  35. UASM_L_LA(_kernel_asid)
  36. UASM_L_LA(_exit_common)
  37. static void *kvm_mips_build_enter_guest(void *addr);
  38. static void *kvm_mips_build_ret_from_exit(void *addr);
  39. static void *kvm_mips_build_ret_to_guest(void *addr);
  40. static void *kvm_mips_build_ret_to_host(void *addr);
  41. /*
  42. * The version of this function in tlbex.c uses current_cpu_type(), but for KVM
  43. * we assume symmetry.
  44. */
  45. static int c0_kscratch(void)
  46. {
  47. return 31;
  48. }
  49. /**
  50. * kvm_mips_entry_setup() - Perform global setup for entry code.
  51. *
  52. * Perform global setup for entry code, such as choosing a scratch register.
  53. *
  54. * Returns: 0 on success.
  55. * -errno on failure.
  56. */
  57. int kvm_mips_entry_setup(void)
  58. {
  59. /*
  60. * We prefer to use KScratchN registers if they are available over the
  61. * defaults above, which may not work on all cores.
  62. */
  63. unsigned int kscratch_mask = cpu_data[0].kscratch_mask;
  64. if (pgd_reg != -1)
  65. kscratch_mask &= ~BIT(pgd_reg);
  66. /* Pick a scratch register for storing VCPU */
  67. if (kscratch_mask) {
  68. scratch_vcpu[0] = c0_kscratch();
  69. scratch_vcpu[1] = ffs(kscratch_mask) - 1;
  70. kscratch_mask &= ~BIT(scratch_vcpu[1]);
  71. }
  72. /* Pick a scratch register to use as a temp for saving state */
  73. if (kscratch_mask) {
  74. scratch_tmp[0] = c0_kscratch();
  75. scratch_tmp[1] = ffs(kscratch_mask) - 1;
  76. kscratch_mask &= ~BIT(scratch_tmp[1]);
  77. }
  78. return 0;
  79. }
  80. static void kvm_mips_build_save_scratch(u32 **p, unsigned int tmp,
  81. unsigned int frame)
  82. {
  83. /* Save the VCPU scratch register value in cp0_epc of the stack frame */
  84. UASM_i_MFC0(p, tmp, scratch_vcpu[0], scratch_vcpu[1]);
  85. UASM_i_SW(p, tmp, offsetof(struct pt_regs, cp0_epc), frame);
  86. /* Save the temp scratch register value in cp0_cause of stack frame */
  87. if (scratch_tmp[0] == c0_kscratch()) {
  88. UASM_i_MFC0(p, tmp, scratch_tmp[0], scratch_tmp[1]);
  89. UASM_i_SW(p, tmp, offsetof(struct pt_regs, cp0_cause), frame);
  90. }
  91. }
  92. static void kvm_mips_build_restore_scratch(u32 **p, unsigned int tmp,
  93. unsigned int frame)
  94. {
  95. /*
  96. * Restore host scratch register values saved by
  97. * kvm_mips_build_save_scratch().
  98. */
  99. UASM_i_LW(p, tmp, offsetof(struct pt_regs, cp0_epc), frame);
  100. UASM_i_MTC0(p, tmp, scratch_vcpu[0], scratch_vcpu[1]);
  101. if (scratch_tmp[0] == c0_kscratch()) {
  102. UASM_i_LW(p, tmp, offsetof(struct pt_regs, cp0_cause), frame);
  103. UASM_i_MTC0(p, tmp, scratch_tmp[0], scratch_tmp[1]);
  104. }
  105. }
  106. /**
  107. * build_set_exc_base() - Assemble code to write exception base address.
  108. * @p: Code buffer pointer.
  109. * @reg: Source register (generated code may set WG bit in @reg).
  110. *
  111. * Assemble code to modify the exception base address in the EBase register,
  112. * using the appropriately sized access and setting the WG bit if necessary.
  113. */
  114. static inline void build_set_exc_base(u32 **p, unsigned int reg)
  115. {
  116. if (cpu_has_ebase_wg) {
  117. /* Set WG so that all the bits get written */
  118. uasm_i_ori(p, reg, reg, MIPS_EBASE_WG);
  119. UASM_i_MTC0(p, reg, C0_EBASE);
  120. } else {
  121. uasm_i_mtc0(p, reg, C0_EBASE);
  122. }
  123. }
  124. /**
  125. * kvm_mips_build_vcpu_run() - Assemble function to start running a guest VCPU.
  126. * @addr: Address to start writing code.
  127. *
  128. * Assemble the start of the vcpu_run function to run a guest VCPU. The function
  129. * conforms to the following prototype:
  130. *
  131. * int vcpu_run(struct kvm_vcpu *vcpu);
  132. *
  133. * The exit from the guest and return to the caller is handled by the code
  134. * generated by kvm_mips_build_ret_to_host().
  135. *
  136. * Returns: Next address after end of written function.
  137. */
  138. void *kvm_mips_build_vcpu_run(void *addr)
  139. {
  140. u32 *p = addr;
  141. unsigned int i;
  142. /*
  143. * GPR_A0: vcpu
  144. */
  145. /* k0/k1 not being used in host kernel context */
  146. UASM_i_ADDIU(&p, GPR_K1, GPR_SP, -(int)sizeof(struct pt_regs));
  147. for (i = 16; i < 32; ++i) {
  148. if (i == 24)
  149. i = 28;
  150. UASM_i_SW(&p, i, offsetof(struct pt_regs, regs[i]), GPR_K1);
  151. }
  152. /* Save host status */
  153. uasm_i_mfc0(&p, GPR_V0, C0_STATUS);
  154. UASM_i_SW(&p, GPR_V0, offsetof(struct pt_regs, cp0_status), GPR_K1);
  155. /* Save scratch registers, will be used to store pointer to vcpu etc */
  156. kvm_mips_build_save_scratch(&p, GPR_V1, GPR_K1);
  157. /* VCPU scratch register has pointer to vcpu */
  158. UASM_i_MTC0(&p, GPR_A0, scratch_vcpu[0], scratch_vcpu[1]);
  159. /* Offset into vcpu->arch */
  160. UASM_i_ADDIU(&p, GPR_K1, GPR_A0, offsetof(struct kvm_vcpu, arch));
  161. /*
  162. * Save the host stack to VCPU, used for exception processing
  163. * when we exit from the Guest
  164. */
  165. UASM_i_SW(&p, GPR_SP, offsetof(struct kvm_vcpu_arch, host_stack), GPR_K1);
  166. /* Save the kernel gp as well */
  167. UASM_i_SW(&p, GPR_GP, offsetof(struct kvm_vcpu_arch, host_gp), GPR_K1);
  168. /*
  169. * Setup status register for running the guest in UM, interrupts
  170. * are disabled
  171. */
  172. UASM_i_LA(&p, GPR_K0, ST0_EXL | KSU_USER | ST0_BEV | ST0_KX_IF_64);
  173. uasm_i_mtc0(&p, GPR_K0, C0_STATUS);
  174. uasm_i_ehb(&p);
  175. /* load up the new EBASE */
  176. UASM_i_LW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, guest_ebase), GPR_K1);
  177. build_set_exc_base(&p, GPR_K0);
  178. /*
  179. * Now that the new EBASE has been loaded, unset BEV, set
  180. * interrupt mask as it was but make sure that timer interrupts
  181. * are enabled
  182. */
  183. uasm_i_addiu(&p, GPR_K0, GPR_ZERO, ST0_EXL | KSU_USER | ST0_IE | ST0_KX_IF_64);
  184. uasm_i_andi(&p, GPR_V0, GPR_V0, ST0_IM);
  185. uasm_i_or(&p, GPR_K0, GPR_K0, GPR_V0);
  186. uasm_i_mtc0(&p, GPR_K0, C0_STATUS);
  187. uasm_i_ehb(&p);
  188. p = kvm_mips_build_enter_guest(p);
  189. return p;
  190. }
  191. /**
  192. * kvm_mips_build_enter_guest() - Assemble code to resume guest execution.
  193. * @addr: Address to start writing code.
  194. *
  195. * Assemble the code to resume guest execution. This code is common between the
  196. * initial entry into the guest from the host, and returning from the exit
  197. * handler back to the guest.
  198. *
  199. * Returns: Next address after end of written function.
  200. */
  201. static void *kvm_mips_build_enter_guest(void *addr)
  202. {
  203. u32 *p = addr;
  204. unsigned int i;
  205. struct uasm_label labels[2];
  206. struct uasm_reloc relocs[2];
  207. struct uasm_label __maybe_unused *l = labels;
  208. struct uasm_reloc __maybe_unused *r = relocs;
  209. memset(labels, 0, sizeof(labels));
  210. memset(relocs, 0, sizeof(relocs));
  211. /* Set Guest EPC */
  212. UASM_i_LW(&p, GPR_T0, offsetof(struct kvm_vcpu_arch, pc), GPR_K1);
  213. UASM_i_MTC0(&p, GPR_T0, C0_EPC);
  214. /* Save normal linux process pgd (VZ guarantees pgd_reg is set) */
  215. if (cpu_has_ldpte)
  216. UASM_i_MFC0(&p, GPR_K0, C0_PWBASE);
  217. else
  218. UASM_i_MFC0(&p, GPR_K0, c0_kscratch(), pgd_reg);
  219. UASM_i_SW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, host_pgd), GPR_K1);
  220. /*
  221. * Set up KVM GPA pgd.
  222. * This does roughly the same as TLBMISS_HANDLER_SETUP_PGD():
  223. * - call tlbmiss_handler_setup_pgd(mm->pgd)
  224. * - write mm->pgd into CP0_PWBase
  225. *
  226. * We keep GPR_S0 pointing at struct kvm so we can load the ASID below.
  227. */
  228. UASM_i_LW(&p, GPR_S0, (int)offsetof(struct kvm_vcpu, kvm) -
  229. (int)offsetof(struct kvm_vcpu, arch), GPR_K1);
  230. UASM_i_LW(&p, GPR_A0, offsetof(struct kvm, arch.gpa_mm.pgd), GPR_S0);
  231. UASM_i_LA(&p, GPR_T9, (unsigned long)tlbmiss_handler_setup_pgd);
  232. uasm_i_jalr(&p, GPR_RA, GPR_T9);
  233. /* delay slot */
  234. if (cpu_has_htw)
  235. UASM_i_MTC0(&p, GPR_A0, C0_PWBASE);
  236. else
  237. uasm_i_nop(&p);
  238. /* Set GM bit to setup eret to VZ guest context */
  239. uasm_i_addiu(&p, GPR_V1, GPR_ZERO, 1);
  240. uasm_i_mfc0(&p, GPR_K0, C0_GUESTCTL0);
  241. uasm_i_ins(&p, GPR_K0, GPR_V1, MIPS_GCTL0_GM_SHIFT, 1);
  242. uasm_i_mtc0(&p, GPR_K0, C0_GUESTCTL0);
  243. if (cpu_has_guestid) {
  244. /*
  245. * Set root mode GuestID, so that root TLB refill handler can
  246. * use the correct GuestID in the root TLB.
  247. */
  248. /* Get current GuestID */
  249. uasm_i_mfc0(&p, GPR_T0, C0_GUESTCTL1);
  250. /* Set GuestCtl1.RID = GuestCtl1.ID */
  251. uasm_i_ext(&p, GPR_T1, GPR_T0, MIPS_GCTL1_ID_SHIFT,
  252. MIPS_GCTL1_ID_WIDTH);
  253. uasm_i_ins(&p, GPR_T0, GPR_T1, MIPS_GCTL1_RID_SHIFT,
  254. MIPS_GCTL1_RID_WIDTH);
  255. uasm_i_mtc0(&p, GPR_T0, C0_GUESTCTL1);
  256. /* GuestID handles dealiasing so we don't need to touch ASID */
  257. goto skip_asid_restore;
  258. }
  259. /* Root ASID Dealias (RAD) */
  260. /* Save host ASID */
  261. UASM_i_MFC0(&p, GPR_K0, C0_ENTRYHI);
  262. UASM_i_SW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, host_entryhi),
  263. GPR_K1);
  264. /* Set the root ASID for the Guest */
  265. UASM_i_ADDIU(&p, GPR_T1, GPR_S0,
  266. offsetof(struct kvm, arch.gpa_mm.context.asid));
  267. /* t1: contains the base of the ASID array, need to get the cpu id */
  268. /* smp_processor_id */
  269. uasm_i_lw(&p, GPR_T2, offsetof(struct thread_info, cpu), GPR_GP);
  270. /* index the ASID array */
  271. uasm_i_sll(&p, GPR_T2, GPR_T2, ilog2(sizeof(long)));
  272. UASM_i_ADDU(&p, GPR_T3, GPR_T1, GPR_T2);
  273. UASM_i_LW(&p, GPR_K0, 0, GPR_T3);
  274. #ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
  275. /*
  276. * reuse ASID array offset
  277. * cpuinfo_mips is a multiple of sizeof(long)
  278. */
  279. uasm_i_addiu(&p, GPR_T3, GPR_ZERO, sizeof(struct cpuinfo_mips)/sizeof(long));
  280. uasm_i_mul(&p, GPR_T2, GPR_T2, GPR_T3);
  281. UASM_i_LA_mostly(&p, GPR_AT, (long)&cpu_data[0].asid_mask);
  282. UASM_i_ADDU(&p, GPR_AT, GPR_AT, GPR_T2);
  283. UASM_i_LW(&p, GPR_T2, uasm_rel_lo((long)&cpu_data[0].asid_mask), GPR_AT);
  284. uasm_i_and(&p, GPR_K0, GPR_K0, GPR_T2);
  285. #else
  286. uasm_i_andi(&p, GPR_K0, GPR_K0, MIPS_ENTRYHI_ASID);
  287. #endif
  288. /* Set up KVM VZ root ASID (!guestid) */
  289. uasm_i_mtc0(&p, GPR_K0, C0_ENTRYHI);
  290. skip_asid_restore:
  291. uasm_i_ehb(&p);
  292. /* Disable RDHWR access */
  293. uasm_i_mtc0(&p, GPR_ZERO, C0_HWRENA);
  294. /* load the guest context from VCPU and return */
  295. for (i = 1; i < 32; ++i) {
  296. /* Guest k0/k1 loaded later */
  297. if (i == GPR_K0 || i == GPR_K1)
  298. continue;
  299. UASM_i_LW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), GPR_K1);
  300. }
  301. #ifndef CONFIG_CPU_MIPSR6
  302. /* Restore hi/lo */
  303. UASM_i_LW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, hi), GPR_K1);
  304. uasm_i_mthi(&p, GPR_K0);
  305. UASM_i_LW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, lo), GPR_K1);
  306. uasm_i_mtlo(&p, GPR_K0);
  307. #endif
  308. /* Restore the guest's k0/k1 registers */
  309. UASM_i_LW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, gprs[GPR_K0]), GPR_K1);
  310. UASM_i_LW(&p, GPR_K1, offsetof(struct kvm_vcpu_arch, gprs[GPR_K1]), GPR_K1);
  311. /* Jump to guest */
  312. uasm_i_eret(&p);
  313. uasm_resolve_relocs(relocs, labels);
  314. return p;
  315. }
  316. /**
  317. * kvm_mips_build_tlb_refill_exception() - Assemble TLB refill handler.
  318. * @addr: Address to start writing code.
  319. * @handler: Address of common handler (within range of @addr).
  320. *
  321. * Assemble TLB refill exception fast path handler for guest execution.
  322. *
  323. * Returns: Next address after end of written function.
  324. */
  325. void *kvm_mips_build_tlb_refill_exception(void *addr, void *handler)
  326. {
  327. u32 *p = addr;
  328. struct uasm_label labels[2];
  329. struct uasm_reloc relocs[2];
  330. #ifndef CONFIG_CPU_LOONGSON64
  331. struct uasm_label *l = labels;
  332. struct uasm_reloc *r = relocs;
  333. #endif
  334. memset(labels, 0, sizeof(labels));
  335. memset(relocs, 0, sizeof(relocs));
  336. /* Save guest k1 into scratch register */
  337. UASM_i_MTC0(&p, GPR_K1, scratch_tmp[0], scratch_tmp[1]);
  338. /* Get the VCPU pointer from the VCPU scratch register */
  339. UASM_i_MFC0(&p, GPR_K1, scratch_vcpu[0], scratch_vcpu[1]);
  340. /* Save guest k0 into VCPU structure */
  341. UASM_i_SW(&p, GPR_K0, offsetof(struct kvm_vcpu, arch.gprs[GPR_K0]), GPR_K1);
  342. /*
  343. * Some of the common tlbex code uses current_cpu_type(). For KVM we
  344. * assume symmetry and just disable preemption to silence the warning.
  345. */
  346. preempt_disable();
  347. #ifdef CONFIG_CPU_LOONGSON64
  348. UASM_i_MFC0(&p, GPR_K1, C0_PGD);
  349. uasm_i_lddir(&p, GPR_K0, GPR_K1, 3); /* global page dir */
  350. #ifndef __PAGETABLE_PMD_FOLDED
  351. uasm_i_lddir(&p, GPR_K1, GPR_K0, 1); /* middle page dir */
  352. #endif
  353. uasm_i_ldpte(&p, GPR_K1, 0); /* even */
  354. uasm_i_ldpte(&p, GPR_K1, 1); /* odd */
  355. uasm_i_tlbwr(&p);
  356. #else
  357. /*
  358. * Now for the actual refill bit. A lot of this can be common with the
  359. * Linux TLB refill handler, however we don't need to handle so many
  360. * cases. We only need to handle user mode refills, and user mode runs
  361. * with 32-bit addressing.
  362. *
  363. * Therefore the branch to label_vmalloc generated by build_get_pmde64()
  364. * that isn't resolved should never actually get taken and is harmless
  365. * to leave in place for now.
  366. */
  367. #ifdef CONFIG_64BIT
  368. build_get_pmde64(&p, &l, &r, GPR_K0, GPR_K1); /* get pmd in GPR_K1 */
  369. #else
  370. build_get_pgde32(&p, GPR_K0, GPR_K1); /* get pgd in GPR_K1 */
  371. #endif
  372. /* we don't support huge pages yet */
  373. build_get_ptep(&p, GPR_K0, GPR_K1);
  374. build_update_entries(&p, GPR_K0, GPR_K1);
  375. build_tlb_write_entry(&p, &l, &r, tlb_random);
  376. #endif
  377. preempt_enable();
  378. /* Get the VCPU pointer from the VCPU scratch register again */
  379. UASM_i_MFC0(&p, GPR_K1, scratch_vcpu[0], scratch_vcpu[1]);
  380. /* Restore the guest's k0/k1 registers */
  381. UASM_i_LW(&p, GPR_K0, offsetof(struct kvm_vcpu, arch.gprs[GPR_K0]), GPR_K1);
  382. uasm_i_ehb(&p);
  383. UASM_i_MFC0(&p, GPR_K1, scratch_tmp[0], scratch_tmp[1]);
  384. /* Jump to guest */
  385. uasm_i_eret(&p);
  386. return p;
  387. }
  388. /**
  389. * kvm_mips_build_exception() - Assemble first level guest exception handler.
  390. * @addr: Address to start writing code.
  391. * @handler: Address of common handler (within range of @addr).
  392. *
  393. * Assemble exception vector code for guest execution. The generated vector will
  394. * branch to the common exception handler generated by kvm_mips_build_exit().
  395. *
  396. * Returns: Next address after end of written function.
  397. */
  398. void *kvm_mips_build_exception(void *addr, void *handler)
  399. {
  400. u32 *p = addr;
  401. struct uasm_label labels[2];
  402. struct uasm_reloc relocs[2];
  403. struct uasm_label *l = labels;
  404. struct uasm_reloc *r = relocs;
  405. memset(labels, 0, sizeof(labels));
  406. memset(relocs, 0, sizeof(relocs));
  407. /* Save guest k1 into scratch register */
  408. UASM_i_MTC0(&p, GPR_K1, scratch_tmp[0], scratch_tmp[1]);
  409. /* Get the VCPU pointer from the VCPU scratch register */
  410. UASM_i_MFC0(&p, GPR_K1, scratch_vcpu[0], scratch_vcpu[1]);
  411. UASM_i_ADDIU(&p, GPR_K1, GPR_K1, offsetof(struct kvm_vcpu, arch));
  412. /* Save guest k0 into VCPU structure */
  413. UASM_i_SW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, gprs[GPR_K0]), GPR_K1);
  414. /* Branch to the common handler */
  415. uasm_il_b(&p, &r, label_exit_common);
  416. uasm_i_nop(&p);
  417. uasm_l_exit_common(&l, handler);
  418. uasm_resolve_relocs(relocs, labels);
  419. return p;
  420. }
  421. /**
  422. * kvm_mips_build_exit() - Assemble common guest exit handler.
  423. * @addr: Address to start writing code.
  424. *
  425. * Assemble the generic guest exit handling code. This is called by the
  426. * exception vectors (generated by kvm_mips_build_exception()), and calls
  427. * kvm_mips_handle_exit(), then either resumes the guest or returns to the host
  428. * depending on the return value.
  429. *
  430. * Returns: Next address after end of written function.
  431. */
  432. void *kvm_mips_build_exit(void *addr)
  433. {
  434. u32 *p = addr;
  435. unsigned int i;
  436. struct uasm_label labels[3];
  437. struct uasm_reloc relocs[3];
  438. struct uasm_label *l = labels;
  439. struct uasm_reloc *r = relocs;
  440. memset(labels, 0, sizeof(labels));
  441. memset(relocs, 0, sizeof(relocs));
  442. /*
  443. * Generic Guest exception handler. We end up here when the guest
  444. * does something that causes a trap to kernel mode.
  445. *
  446. * Both k0/k1 registers will have already been saved (k0 into the vcpu
  447. * structure, and k1 into the scratch_tmp register).
  448. *
  449. * The k1 register will already contain the kvm_vcpu_arch pointer.
  450. */
  451. /* Start saving Guest context to VCPU */
  452. for (i = 0; i < 32; ++i) {
  453. /* Guest k0/k1 saved later */
  454. if (i == GPR_K0 || i == GPR_K1)
  455. continue;
  456. UASM_i_SW(&p, i, offsetof(struct kvm_vcpu_arch, gprs[i]), GPR_K1);
  457. }
  458. #ifndef CONFIG_CPU_MIPSR6
  459. /* We need to save hi/lo and restore them on the way out */
  460. uasm_i_mfhi(&p, GPR_T0);
  461. UASM_i_SW(&p, GPR_T0, offsetof(struct kvm_vcpu_arch, hi), GPR_K1);
  462. uasm_i_mflo(&p, GPR_T0);
  463. UASM_i_SW(&p, GPR_T0, offsetof(struct kvm_vcpu_arch, lo), GPR_K1);
  464. #endif
  465. /* Finally save guest k1 to VCPU */
  466. uasm_i_ehb(&p);
  467. UASM_i_MFC0(&p, GPR_T0, scratch_tmp[0], scratch_tmp[1]);
  468. UASM_i_SW(&p, GPR_T0, offsetof(struct kvm_vcpu_arch, gprs[GPR_K1]), GPR_K1);
  469. /* Now that context has been saved, we can use other registers */
  470. /* Restore vcpu */
  471. UASM_i_MFC0(&p, GPR_S0, scratch_vcpu[0], scratch_vcpu[1]);
  472. /*
  473. * Save Host level EPC, BadVaddr and Cause to VCPU, useful to process
  474. * the exception
  475. */
  476. UASM_i_MFC0(&p, GPR_K0, C0_EPC);
  477. UASM_i_SW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, pc), GPR_K1);
  478. UASM_i_MFC0(&p, GPR_K0, C0_BADVADDR);
  479. UASM_i_SW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, host_cp0_badvaddr),
  480. GPR_K1);
  481. uasm_i_mfc0(&p, GPR_K0, C0_CAUSE);
  482. uasm_i_sw(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, host_cp0_cause), GPR_K1);
  483. if (cpu_has_badinstr) {
  484. uasm_i_mfc0(&p, GPR_K0, C0_BADINSTR);
  485. uasm_i_sw(&p, GPR_K0, offsetof(struct kvm_vcpu_arch,
  486. host_cp0_badinstr), GPR_K1);
  487. }
  488. if (cpu_has_badinstrp) {
  489. uasm_i_mfc0(&p, GPR_K0, C0_BADINSTRP);
  490. uasm_i_sw(&p, GPR_K0, offsetof(struct kvm_vcpu_arch,
  491. host_cp0_badinstrp), GPR_K1);
  492. }
  493. /* Now restore the host state just enough to run the handlers */
  494. /* Switch EBASE to the one used by Linux */
  495. /* load up the host EBASE */
  496. uasm_i_mfc0(&p, GPR_V0, C0_STATUS);
  497. uasm_i_lui(&p, GPR_AT, ST0_BEV >> 16);
  498. uasm_i_or(&p, GPR_K0, GPR_V0, GPR_AT);
  499. uasm_i_mtc0(&p, GPR_K0, C0_STATUS);
  500. uasm_i_ehb(&p);
  501. UASM_i_LA_mostly(&p, GPR_K0, (long)&ebase);
  502. UASM_i_LW(&p, GPR_K0, uasm_rel_lo((long)&ebase), GPR_K0);
  503. build_set_exc_base(&p, GPR_K0);
  504. if (raw_cpu_has_fpu) {
  505. /*
  506. * If FPU is enabled, save FCR31 and clear it so that later
  507. * ctc1's don't trigger FPE for pending exceptions.
  508. */
  509. uasm_i_lui(&p, GPR_AT, ST0_CU1 >> 16);
  510. uasm_i_and(&p, GPR_V1, GPR_V0, GPR_AT);
  511. uasm_il_beqz(&p, &r, GPR_V1, label_fpu_1);
  512. uasm_i_nop(&p);
  513. uasm_i_cfc1(&p, GPR_T0, 31);
  514. uasm_i_sw(&p, GPR_T0, offsetof(struct kvm_vcpu_arch, fpu.fcr31),
  515. GPR_K1);
  516. uasm_i_ctc1(&p, GPR_ZERO, 31);
  517. uasm_l_fpu_1(&l, p);
  518. }
  519. if (cpu_has_msa) {
  520. /*
  521. * If MSA is enabled, save MSACSR and clear it so that later
  522. * instructions don't trigger MSAFPE for pending exceptions.
  523. */
  524. uasm_i_mfc0(&p, GPR_T0, C0_CONFIG5);
  525. uasm_i_ext(&p, GPR_T0, GPR_T0, 27, 1); /* MIPS_CONF5_MSAEN */
  526. uasm_il_beqz(&p, &r, GPR_T0, label_msa_1);
  527. uasm_i_nop(&p);
  528. uasm_i_cfcmsa(&p, GPR_T0, MSA_CSR);
  529. uasm_i_sw(&p, GPR_T0, offsetof(struct kvm_vcpu_arch, fpu.msacsr),
  530. GPR_K1);
  531. uasm_i_ctcmsa(&p, MSA_CSR, GPR_ZERO);
  532. uasm_l_msa_1(&l, p);
  533. }
  534. /* Restore host ASID */
  535. if (!cpu_has_guestid) {
  536. UASM_i_LW(&p, GPR_K0, offsetof(struct kvm_vcpu_arch, host_entryhi),
  537. GPR_K1);
  538. UASM_i_MTC0(&p, GPR_K0, C0_ENTRYHI);
  539. }
  540. /*
  541. * Set up normal Linux process pgd.
  542. * This does roughly the same as TLBMISS_HANDLER_SETUP_PGD():
  543. * - call tlbmiss_handler_setup_pgd(mm->pgd)
  544. * - write mm->pgd into CP0_PWBase
  545. */
  546. UASM_i_LW(&p, GPR_A0,
  547. offsetof(struct kvm_vcpu_arch, host_pgd), GPR_K1);
  548. UASM_i_LA(&p, GPR_T9, (unsigned long)tlbmiss_handler_setup_pgd);
  549. uasm_i_jalr(&p, GPR_RA, GPR_T9);
  550. /* delay slot */
  551. if (cpu_has_htw)
  552. UASM_i_MTC0(&p, GPR_A0, C0_PWBASE);
  553. else
  554. uasm_i_nop(&p);
  555. /* Clear GM bit so we don't enter guest mode when EXL is cleared */
  556. uasm_i_mfc0(&p, GPR_K0, C0_GUESTCTL0);
  557. uasm_i_ins(&p, GPR_K0, GPR_ZERO, MIPS_GCTL0_GM_SHIFT, 1);
  558. uasm_i_mtc0(&p, GPR_K0, C0_GUESTCTL0);
  559. /* Save GuestCtl0 so we can access GExcCode after CPU migration */
  560. uasm_i_sw(&p, GPR_K0,
  561. offsetof(struct kvm_vcpu_arch, host_cp0_guestctl0), GPR_K1);
  562. if (cpu_has_guestid) {
  563. /*
  564. * Clear root mode GuestID, so that root TLB operations use the
  565. * root GuestID in the root TLB.
  566. */
  567. uasm_i_mfc0(&p, GPR_T0, C0_GUESTCTL1);
  568. /* Set GuestCtl1.RID = MIPS_GCTL1_ROOT_GUESTID (i.e. 0) */
  569. uasm_i_ins(&p, GPR_T0, GPR_ZERO, MIPS_GCTL1_RID_SHIFT,
  570. MIPS_GCTL1_RID_WIDTH);
  571. uasm_i_mtc0(&p, GPR_T0, C0_GUESTCTL1);
  572. }
  573. /* Now that the new EBASE has been loaded, unset BEV and KSU_USER */
  574. uasm_i_addiu(&p, GPR_AT, GPR_ZERO, ~(ST0_EXL | KSU_USER | ST0_IE));
  575. uasm_i_and(&p, GPR_V0, GPR_V0, GPR_AT);
  576. uasm_i_lui(&p, GPR_AT, ST0_CU0 >> 16);
  577. uasm_i_or(&p, GPR_V0, GPR_V0, GPR_AT);
  578. #ifdef CONFIG_64BIT
  579. uasm_i_ori(&p, GPR_V0, GPR_V0, ST0_SX | ST0_UX);
  580. #endif
  581. uasm_i_mtc0(&p, GPR_V0, C0_STATUS);
  582. uasm_i_ehb(&p);
  583. /* Load up host GPR_GP */
  584. UASM_i_LW(&p, GPR_GP, offsetof(struct kvm_vcpu_arch, host_gp), GPR_K1);
  585. /* Need a stack before we can jump to "C" */
  586. UASM_i_LW(&p, GPR_SP, offsetof(struct kvm_vcpu_arch, host_stack), GPR_K1);
  587. /* Saved host state */
  588. UASM_i_ADDIU(&p, GPR_SP, GPR_SP, -(int)sizeof(struct pt_regs));
  589. /*
  590. * XXXKYMA do we need to load the host ASID, maybe not because the
  591. * kernel entries are marked GLOBAL, need to verify
  592. */
  593. /* Restore host scratch registers, as we'll have clobbered them */
  594. kvm_mips_build_restore_scratch(&p, GPR_K0, GPR_SP);
  595. /* Restore RDHWR access */
  596. UASM_i_LA_mostly(&p, GPR_K0, (long)&hwrena);
  597. uasm_i_lw(&p, GPR_K0, uasm_rel_lo((long)&hwrena), GPR_K0);
  598. uasm_i_mtc0(&p, GPR_K0, C0_HWRENA);
  599. /* Jump to handler */
  600. /*
  601. * XXXKYMA: not sure if this is safe, how large is the stack??
  602. * Now jump to the kvm_mips_handle_exit() to see if we can deal
  603. * with this in the kernel
  604. */
  605. uasm_i_move(&p, GPR_A0, GPR_S0);
  606. UASM_i_LA(&p, GPR_T9, (unsigned long)kvm_mips_handle_exit);
  607. uasm_i_jalr(&p, GPR_RA, GPR_T9);
  608. UASM_i_ADDIU(&p, GPR_SP, GPR_SP, -CALLFRAME_SIZ);
  609. uasm_resolve_relocs(relocs, labels);
  610. p = kvm_mips_build_ret_from_exit(p);
  611. return p;
  612. }
  613. /**
  614. * kvm_mips_build_ret_from_exit() - Assemble guest exit return handler.
  615. * @addr: Address to start writing code.
  616. *
  617. * Assemble the code to handle the return from kvm_mips_handle_exit(), either
  618. * resuming the guest or returning to the host depending on the return value.
  619. *
  620. * Returns: Next address after end of written function.
  621. */
  622. static void *kvm_mips_build_ret_from_exit(void *addr)
  623. {
  624. u32 *p = addr;
  625. struct uasm_label labels[2];
  626. struct uasm_reloc relocs[2];
  627. struct uasm_label *l = labels;
  628. struct uasm_reloc *r = relocs;
  629. memset(labels, 0, sizeof(labels));
  630. memset(relocs, 0, sizeof(relocs));
  631. /* Return from handler Make sure interrupts are disabled */
  632. uasm_i_di(&p, GPR_ZERO);
  633. uasm_i_ehb(&p);
  634. /*
  635. * XXXKYMA: k0/k1 could have been blown away if we processed
  636. * an exception while we were handling the exception from the
  637. * guest, reload k1
  638. */
  639. uasm_i_move(&p, GPR_K1, GPR_S0);
  640. UASM_i_ADDIU(&p, GPR_K1, GPR_K1, offsetof(struct kvm_vcpu, arch));
  641. /*
  642. * Check return value, should tell us if we are returning to the
  643. * host (handle I/O etc)or resuming the guest
  644. */
  645. uasm_i_andi(&p, GPR_T0, GPR_V0, RESUME_HOST);
  646. uasm_il_bnez(&p, &r, GPR_T0, label_return_to_host);
  647. uasm_i_nop(&p);
  648. p = kvm_mips_build_ret_to_guest(p);
  649. uasm_l_return_to_host(&l, p);
  650. p = kvm_mips_build_ret_to_host(p);
  651. uasm_resolve_relocs(relocs, labels);
  652. return p;
  653. }
  654. /**
  655. * kvm_mips_build_ret_to_guest() - Assemble code to return to the guest.
  656. * @addr: Address to start writing code.
  657. *
  658. * Assemble the code to handle return from the guest exit handler
  659. * (kvm_mips_handle_exit()) back to the guest.
  660. *
  661. * Returns: Next address after end of written function.
  662. */
  663. static void *kvm_mips_build_ret_to_guest(void *addr)
  664. {
  665. u32 *p = addr;
  666. /* Put the saved pointer to vcpu (s0) back into the scratch register */
  667. UASM_i_MTC0(&p, GPR_S0, scratch_vcpu[0], scratch_vcpu[1]);
  668. /* Load up the Guest EBASE to minimize the window where BEV is set */
  669. UASM_i_LW(&p, GPR_T0, offsetof(struct kvm_vcpu_arch, guest_ebase), GPR_K1);
  670. /* Switch EBASE back to the one used by KVM */
  671. uasm_i_mfc0(&p, GPR_V1, C0_STATUS);
  672. uasm_i_lui(&p, GPR_AT, ST0_BEV >> 16);
  673. uasm_i_or(&p, GPR_K0, GPR_V1, GPR_AT);
  674. uasm_i_mtc0(&p, GPR_K0, C0_STATUS);
  675. uasm_i_ehb(&p);
  676. build_set_exc_base(&p, GPR_T0);
  677. /* Setup status register for running guest in UM */
  678. uasm_i_ori(&p, GPR_V1, GPR_V1, ST0_EXL | KSU_USER | ST0_IE);
  679. UASM_i_LA(&p, GPR_AT, ~(ST0_CU0 | ST0_MX | ST0_SX | ST0_UX));
  680. uasm_i_and(&p, GPR_V1, GPR_V1, GPR_AT);
  681. uasm_i_mtc0(&p, GPR_V1, C0_STATUS);
  682. uasm_i_ehb(&p);
  683. p = kvm_mips_build_enter_guest(p);
  684. return p;
  685. }
  686. /**
  687. * kvm_mips_build_ret_to_host() - Assemble code to return to the host.
  688. * @addr: Address to start writing code.
  689. *
  690. * Assemble the code to handle return from the guest exit handler
  691. * (kvm_mips_handle_exit()) back to the host, i.e. to the caller of the vcpu_run
  692. * function generated by kvm_mips_build_vcpu_run().
  693. *
  694. * Returns: Next address after end of written function.
  695. */
  696. static void *kvm_mips_build_ret_to_host(void *addr)
  697. {
  698. u32 *p = addr;
  699. unsigned int i;
  700. /* EBASE is already pointing to Linux */
  701. UASM_i_LW(&p, GPR_K1, offsetof(struct kvm_vcpu_arch, host_stack), GPR_K1);
  702. UASM_i_ADDIU(&p, GPR_K1, GPR_K1, -(int)sizeof(struct pt_regs));
  703. /*
  704. * r2/v0 is the return code, shift it down by 2 (arithmetic)
  705. * to recover the err code
  706. */
  707. uasm_i_sra(&p, GPR_K0, GPR_V0, 2);
  708. uasm_i_move(&p, GPR_V0, GPR_K0);
  709. /* Load context saved on the host stack */
  710. for (i = 16; i < 31; ++i) {
  711. if (i == 24)
  712. i = 28;
  713. UASM_i_LW(&p, i, offsetof(struct pt_regs, regs[i]), GPR_K1);
  714. }
  715. /* Restore RDHWR access */
  716. UASM_i_LA_mostly(&p, GPR_K0, (long)&hwrena);
  717. uasm_i_lw(&p, GPR_K0, uasm_rel_lo((long)&hwrena), GPR_K0);
  718. uasm_i_mtc0(&p, GPR_K0, C0_HWRENA);
  719. /* Restore GPR_RA, which is the address we will return to */
  720. UASM_i_LW(&p, GPR_RA, offsetof(struct pt_regs, regs[GPR_RA]), GPR_K1);
  721. uasm_i_jr(&p, GPR_RA);
  722. uasm_i_nop(&p);
  723. return p;
  724. }