entry.c 25 KB

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