entry.c 26 KB

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