trap_emul.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. * KVM/MIPS: Deliver/Emulate exceptions to the guest kernel
  7. *
  8. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  9. * Authors: Sanjay Lal <sanjayl@kymasys.com>
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/kvm_host.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/vmalloc.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/pgalloc.h>
  18. #include "interrupt.h"
  19. static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva)
  20. {
  21. gpa_t gpa;
  22. gva_t kseg = KSEGX(gva);
  23. gva_t gkseg = KVM_GUEST_KSEGX(gva);
  24. if ((kseg == CKSEG0) || (kseg == CKSEG1))
  25. gpa = CPHYSADDR(gva);
  26. else if (gkseg == KVM_GUEST_KSEG0)
  27. gpa = KVM_GUEST_CPHYSADDR(gva);
  28. else {
  29. kvm_err("%s: cannot find GPA for GVA: %#lx\n", __func__, gva);
  30. kvm_mips_dump_host_tlbs();
  31. gpa = KVM_INVALID_ADDR;
  32. }
  33. kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa);
  34. return gpa;
  35. }
  36. static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu)
  37. {
  38. struct mips_coproc *cop0 = vcpu->arch.cop0;
  39. struct kvm_run *run = vcpu->run;
  40. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  41. u32 cause = vcpu->arch.host_cp0_cause;
  42. enum emulation_result er = EMULATE_DONE;
  43. int ret = RESUME_GUEST;
  44. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
  45. /* FPU Unusable */
  46. if (!kvm_mips_guest_has_fpu(&vcpu->arch) ||
  47. (kvm_read_c0_guest_status(cop0) & ST0_CU1) == 0) {
  48. /*
  49. * Unusable/no FPU in guest:
  50. * deliver guest COP1 Unusable Exception
  51. */
  52. er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu);
  53. } else {
  54. /* Restore FPU state */
  55. kvm_own_fpu(vcpu);
  56. er = EMULATE_DONE;
  57. }
  58. } else {
  59. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  60. }
  61. switch (er) {
  62. case EMULATE_DONE:
  63. ret = RESUME_GUEST;
  64. break;
  65. case EMULATE_FAIL:
  66. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  67. ret = RESUME_HOST;
  68. break;
  69. case EMULATE_WAIT:
  70. run->exit_reason = KVM_EXIT_INTR;
  71. ret = RESUME_HOST;
  72. break;
  73. default:
  74. BUG();
  75. }
  76. return ret;
  77. }
  78. static int kvm_mips_bad_load(u32 cause, u32 *opc, struct kvm_run *run,
  79. struct kvm_vcpu *vcpu)
  80. {
  81. enum emulation_result er;
  82. union mips_instruction inst;
  83. int err;
  84. /* A code fetch fault doesn't count as an MMIO */
  85. if (kvm_is_ifetch_fault(&vcpu->arch)) {
  86. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  87. return RESUME_HOST;
  88. }
  89. /* Fetch the instruction. */
  90. if (cause & CAUSEF_BD)
  91. opc += 1;
  92. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  93. if (err) {
  94. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  95. return RESUME_HOST;
  96. }
  97. /* Emulate the load */
  98. er = kvm_mips_emulate_load(inst, cause, run, vcpu);
  99. if (er == EMULATE_FAIL) {
  100. kvm_err("Emulate load from MMIO space failed\n");
  101. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  102. } else {
  103. run->exit_reason = KVM_EXIT_MMIO;
  104. }
  105. return RESUME_HOST;
  106. }
  107. static int kvm_mips_bad_store(u32 cause, u32 *opc, struct kvm_run *run,
  108. struct kvm_vcpu *vcpu)
  109. {
  110. enum emulation_result er;
  111. union mips_instruction inst;
  112. int err;
  113. /* Fetch the instruction. */
  114. if (cause & CAUSEF_BD)
  115. opc += 1;
  116. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  117. if (err) {
  118. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  119. return RESUME_HOST;
  120. }
  121. /* Emulate the store */
  122. er = kvm_mips_emulate_store(inst, cause, run, vcpu);
  123. if (er == EMULATE_FAIL) {
  124. kvm_err("Emulate store to MMIO space failed\n");
  125. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  126. } else {
  127. run->exit_reason = KVM_EXIT_MMIO;
  128. }
  129. return RESUME_HOST;
  130. }
  131. static int kvm_mips_bad_access(u32 cause, u32 *opc, struct kvm_run *run,
  132. struct kvm_vcpu *vcpu, bool store)
  133. {
  134. if (store)
  135. return kvm_mips_bad_store(cause, opc, run, vcpu);
  136. else
  137. return kvm_mips_bad_load(cause, opc, run, vcpu);
  138. }
  139. static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu)
  140. {
  141. struct mips_coproc *cop0 = vcpu->arch.cop0;
  142. struct kvm_run *run = vcpu->run;
  143. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  144. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  145. u32 cause = vcpu->arch.host_cp0_cause;
  146. struct kvm_mips_tlb *tlb;
  147. unsigned long entryhi;
  148. int index;
  149. if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
  150. || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
  151. /*
  152. * First find the mapping in the guest TLB. If the failure to
  153. * write was due to the guest TLB, it should be up to the guest
  154. * to handle it.
  155. */
  156. entryhi = (badvaddr & VPN2_MASK) |
  157. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  158. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  159. /*
  160. * These should never happen.
  161. * They would indicate stale host TLB entries.
  162. */
  163. if (unlikely(index < 0)) {
  164. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  165. return RESUME_HOST;
  166. }
  167. tlb = vcpu->arch.guest_tlb + index;
  168. if (unlikely(!TLB_IS_VALID(*tlb, badvaddr))) {
  169. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  170. return RESUME_HOST;
  171. }
  172. /*
  173. * Guest entry not dirty? That would explain the TLB modified
  174. * exception. Relay that on to the guest so it can handle it.
  175. */
  176. if (!TLB_IS_DIRTY(*tlb, badvaddr)) {
  177. kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
  178. return RESUME_GUEST;
  179. }
  180. if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, badvaddr,
  181. true))
  182. /* Not writable, needs handling as MMIO */
  183. return kvm_mips_bad_store(cause, opc, run, vcpu);
  184. return RESUME_GUEST;
  185. } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
  186. if (kvm_mips_handle_kseg0_tlb_fault(badvaddr, vcpu, true) < 0)
  187. /* Not writable, needs handling as MMIO */
  188. return kvm_mips_bad_store(cause, opc, run, vcpu);
  189. return RESUME_GUEST;
  190. } else {
  191. /* host kernel addresses are all handled as MMIO */
  192. return kvm_mips_bad_store(cause, opc, run, vcpu);
  193. }
  194. }
  195. static int kvm_trap_emul_handle_tlb_miss(struct kvm_vcpu *vcpu, bool store)
  196. {
  197. struct kvm_run *run = vcpu->run;
  198. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  199. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  200. u32 cause = vcpu->arch.host_cp0_cause;
  201. enum emulation_result er = EMULATE_DONE;
  202. int ret = RESUME_GUEST;
  203. if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR)
  204. && KVM_GUEST_KERNEL_MODE(vcpu)) {
  205. if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) {
  206. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  207. ret = RESUME_HOST;
  208. }
  209. } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
  210. || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
  211. kvm_debug("USER ADDR TLB %s fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
  212. store ? "ST" : "LD", cause, opc, badvaddr);
  213. /*
  214. * User Address (UA) fault, this could happen if
  215. * (1) TLB entry not present/valid in both Guest and shadow host
  216. * TLBs, in this case we pass on the fault to the guest
  217. * kernel and let it handle it.
  218. * (2) TLB entry is present in the Guest TLB but not in the
  219. * shadow, in this case we inject the TLB from the Guest TLB
  220. * into the shadow host TLB
  221. */
  222. er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu, store);
  223. if (er == EMULATE_DONE)
  224. ret = RESUME_GUEST;
  225. else {
  226. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  227. ret = RESUME_HOST;
  228. }
  229. } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
  230. /*
  231. * All KSEG0 faults are handled by KVM, as the guest kernel does
  232. * not expect to ever get them
  233. */
  234. if (kvm_mips_handle_kseg0_tlb_fault(badvaddr, vcpu, store) < 0)
  235. ret = kvm_mips_bad_access(cause, opc, run, vcpu, store);
  236. } else if (KVM_GUEST_KERNEL_MODE(vcpu)
  237. && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
  238. /*
  239. * With EVA we may get a TLB exception instead of an address
  240. * error when the guest performs MMIO to KSeg1 addresses.
  241. */
  242. ret = kvm_mips_bad_access(cause, opc, run, vcpu, store);
  243. } else {
  244. kvm_err("Illegal TLB %s fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
  245. store ? "ST" : "LD", cause, opc, badvaddr);
  246. kvm_mips_dump_host_tlbs();
  247. kvm_arch_vcpu_dump_regs(vcpu);
  248. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  249. ret = RESUME_HOST;
  250. }
  251. return ret;
  252. }
  253. static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
  254. {
  255. return kvm_trap_emul_handle_tlb_miss(vcpu, true);
  256. }
  257. static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
  258. {
  259. return kvm_trap_emul_handle_tlb_miss(vcpu, false);
  260. }
  261. static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu)
  262. {
  263. struct kvm_run *run = vcpu->run;
  264. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  265. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  266. u32 cause = vcpu->arch.host_cp0_cause;
  267. int ret = RESUME_GUEST;
  268. if (KVM_GUEST_KERNEL_MODE(vcpu)
  269. && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
  270. ret = kvm_mips_bad_store(cause, opc, run, vcpu);
  271. } else {
  272. kvm_err("Address Error (STORE): cause %#x, PC: %p, BadVaddr: %#lx\n",
  273. cause, opc, badvaddr);
  274. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  275. ret = RESUME_HOST;
  276. }
  277. return ret;
  278. }
  279. static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu)
  280. {
  281. struct kvm_run *run = vcpu->run;
  282. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  283. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  284. u32 cause = vcpu->arch.host_cp0_cause;
  285. int ret = RESUME_GUEST;
  286. if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) {
  287. ret = kvm_mips_bad_load(cause, opc, run, vcpu);
  288. } else {
  289. kvm_err("Address Error (LOAD): cause %#x, PC: %p, BadVaddr: %#lx\n",
  290. cause, opc, badvaddr);
  291. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  292. ret = RESUME_HOST;
  293. }
  294. return ret;
  295. }
  296. static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu)
  297. {
  298. struct kvm_run *run = vcpu->run;
  299. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  300. u32 cause = vcpu->arch.host_cp0_cause;
  301. enum emulation_result er = EMULATE_DONE;
  302. int ret = RESUME_GUEST;
  303. er = kvm_mips_emulate_syscall(cause, opc, run, vcpu);
  304. if (er == EMULATE_DONE)
  305. ret = RESUME_GUEST;
  306. else {
  307. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  308. ret = RESUME_HOST;
  309. }
  310. return ret;
  311. }
  312. static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu)
  313. {
  314. struct kvm_run *run = vcpu->run;
  315. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  316. u32 cause = vcpu->arch.host_cp0_cause;
  317. enum emulation_result er = EMULATE_DONE;
  318. int ret = RESUME_GUEST;
  319. er = kvm_mips_handle_ri(cause, opc, run, vcpu);
  320. if (er == EMULATE_DONE)
  321. ret = RESUME_GUEST;
  322. else {
  323. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  324. ret = RESUME_HOST;
  325. }
  326. return ret;
  327. }
  328. static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu)
  329. {
  330. struct kvm_run *run = vcpu->run;
  331. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  332. u32 cause = vcpu->arch.host_cp0_cause;
  333. enum emulation_result er = EMULATE_DONE;
  334. int ret = RESUME_GUEST;
  335. er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu);
  336. if (er == EMULATE_DONE)
  337. ret = RESUME_GUEST;
  338. else {
  339. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  340. ret = RESUME_HOST;
  341. }
  342. return ret;
  343. }
  344. static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu)
  345. {
  346. struct kvm_run *run = vcpu->run;
  347. u32 __user *opc = (u32 __user *)vcpu->arch.pc;
  348. u32 cause = vcpu->arch.host_cp0_cause;
  349. enum emulation_result er = EMULATE_DONE;
  350. int ret = RESUME_GUEST;
  351. er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu);
  352. if (er == EMULATE_DONE) {
  353. ret = RESUME_GUEST;
  354. } else {
  355. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  356. ret = RESUME_HOST;
  357. }
  358. return ret;
  359. }
  360. static int kvm_trap_emul_handle_msa_fpe(struct kvm_vcpu *vcpu)
  361. {
  362. struct kvm_run *run = vcpu->run;
  363. u32 __user *opc = (u32 __user *)vcpu->arch.pc;
  364. u32 cause = vcpu->arch.host_cp0_cause;
  365. enum emulation_result er = EMULATE_DONE;
  366. int ret = RESUME_GUEST;
  367. er = kvm_mips_emulate_msafpe_exc(cause, opc, run, vcpu);
  368. if (er == EMULATE_DONE) {
  369. ret = RESUME_GUEST;
  370. } else {
  371. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  372. ret = RESUME_HOST;
  373. }
  374. return ret;
  375. }
  376. static int kvm_trap_emul_handle_fpe(struct kvm_vcpu *vcpu)
  377. {
  378. struct kvm_run *run = vcpu->run;
  379. u32 __user *opc = (u32 __user *)vcpu->arch.pc;
  380. u32 cause = vcpu->arch.host_cp0_cause;
  381. enum emulation_result er = EMULATE_DONE;
  382. int ret = RESUME_GUEST;
  383. er = kvm_mips_emulate_fpe_exc(cause, opc, run, vcpu);
  384. if (er == EMULATE_DONE) {
  385. ret = RESUME_GUEST;
  386. } else {
  387. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  388. ret = RESUME_HOST;
  389. }
  390. return ret;
  391. }
  392. /**
  393. * kvm_trap_emul_handle_msa_disabled() - Guest used MSA while disabled in root.
  394. * @vcpu: Virtual CPU context.
  395. *
  396. * Handle when the guest attempts to use MSA when it is disabled.
  397. */
  398. static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu)
  399. {
  400. struct mips_coproc *cop0 = vcpu->arch.cop0;
  401. struct kvm_run *run = vcpu->run;
  402. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  403. u32 cause = vcpu->arch.host_cp0_cause;
  404. enum emulation_result er = EMULATE_DONE;
  405. int ret = RESUME_GUEST;
  406. if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
  407. (kvm_read_c0_guest_status(cop0) & (ST0_CU1 | ST0_FR)) == ST0_CU1) {
  408. /*
  409. * No MSA in guest, or FPU enabled and not in FR=1 mode,
  410. * guest reserved instruction exception
  411. */
  412. er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  413. } else if (!(kvm_read_c0_guest_config5(cop0) & MIPS_CONF5_MSAEN)) {
  414. /* MSA disabled by guest, guest MSA disabled exception */
  415. er = kvm_mips_emulate_msadis_exc(cause, opc, run, vcpu);
  416. } else {
  417. /* Restore MSA/FPU state */
  418. kvm_own_msa(vcpu);
  419. er = EMULATE_DONE;
  420. }
  421. switch (er) {
  422. case EMULATE_DONE:
  423. ret = RESUME_GUEST;
  424. break;
  425. case EMULATE_FAIL:
  426. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  427. ret = RESUME_HOST;
  428. break;
  429. default:
  430. BUG();
  431. }
  432. return ret;
  433. }
  434. static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu)
  435. {
  436. struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
  437. struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
  438. /*
  439. * Allocate GVA -> HPA page tables.
  440. * MIPS doesn't use the mm_struct pointer argument.
  441. */
  442. kern_mm->pgd = pgd_alloc(kern_mm);
  443. if (!kern_mm->pgd)
  444. return -ENOMEM;
  445. user_mm->pgd = pgd_alloc(user_mm);
  446. if (!user_mm->pgd) {
  447. pgd_free(kern_mm, kern_mm->pgd);
  448. return -ENOMEM;
  449. }
  450. return 0;
  451. }
  452. static void kvm_mips_emul_free_gva_pt(pgd_t *pgd)
  453. {
  454. /* Don't free host kernel page tables copied from init_mm.pgd */
  455. const unsigned long end = 0x80000000;
  456. unsigned long pgd_va, pud_va, pmd_va;
  457. pud_t *pud;
  458. pmd_t *pmd;
  459. pte_t *pte;
  460. int i, j, k;
  461. for (i = 0; i < USER_PTRS_PER_PGD; i++) {
  462. if (pgd_none(pgd[i]))
  463. continue;
  464. pgd_va = (unsigned long)i << PGDIR_SHIFT;
  465. if (pgd_va >= end)
  466. break;
  467. pud = pud_offset(pgd + i, 0);
  468. for (j = 0; j < PTRS_PER_PUD; j++) {
  469. if (pud_none(pud[j]))
  470. continue;
  471. pud_va = pgd_va | ((unsigned long)j << PUD_SHIFT);
  472. if (pud_va >= end)
  473. break;
  474. pmd = pmd_offset(pud + j, 0);
  475. for (k = 0; k < PTRS_PER_PMD; k++) {
  476. if (pmd_none(pmd[k]))
  477. continue;
  478. pmd_va = pud_va | (k << PMD_SHIFT);
  479. if (pmd_va >= end)
  480. break;
  481. pte = pte_offset(pmd + k, 0);
  482. pte_free_kernel(NULL, pte);
  483. }
  484. pmd_free(NULL, pmd);
  485. }
  486. pud_free(NULL, pud);
  487. }
  488. pgd_free(NULL, pgd);
  489. }
  490. static void kvm_trap_emul_vcpu_uninit(struct kvm_vcpu *vcpu)
  491. {
  492. kvm_mips_emul_free_gva_pt(vcpu->arch.guest_kernel_mm.pgd);
  493. kvm_mips_emul_free_gva_pt(vcpu->arch.guest_user_mm.pgd);
  494. }
  495. static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
  496. {
  497. struct mips_coproc *cop0 = vcpu->arch.cop0;
  498. u32 config, config1;
  499. int vcpu_id = vcpu->vcpu_id;
  500. /*
  501. * Arch specific stuff, set up config registers properly so that the
  502. * guest will come up as expected
  503. */
  504. #ifndef CONFIG_CPU_MIPSR6
  505. /* r2-r5, simulate a MIPS 24kc */
  506. kvm_write_c0_guest_prid(cop0, 0x00019300);
  507. #else
  508. /* r6+, simulate a generic QEMU machine */
  509. kvm_write_c0_guest_prid(cop0, 0x00010000);
  510. #endif
  511. /*
  512. * Have config1, Cacheable, noncoherent, write-back, write allocate.
  513. * Endianness, arch revision & virtually tagged icache should match
  514. * host.
  515. */
  516. config = read_c0_config() & MIPS_CONF_AR;
  517. config |= MIPS_CONF_M | CONF_CM_CACHABLE_NONCOHERENT | MIPS_CONF_MT_TLB;
  518. #ifdef CONFIG_CPU_BIG_ENDIAN
  519. config |= CONF_BE;
  520. #endif
  521. if (cpu_has_vtag_icache)
  522. config |= MIPS_CONF_VI;
  523. kvm_write_c0_guest_config(cop0, config);
  524. /* Read the cache characteristics from the host Config1 Register */
  525. config1 = (read_c0_config1() & ~0x7f);
  526. /* Set up MMU size */
  527. config1 &= ~(0x3f << 25);
  528. config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25);
  529. /* We unset some bits that we aren't emulating */
  530. config1 &= ~(MIPS_CONF1_C2 | MIPS_CONF1_MD | MIPS_CONF1_PC |
  531. MIPS_CONF1_WR | MIPS_CONF1_CA);
  532. kvm_write_c0_guest_config1(cop0, config1);
  533. /* Have config3, no tertiary/secondary caches implemented */
  534. kvm_write_c0_guest_config2(cop0, MIPS_CONF_M);
  535. /* MIPS_CONF_M | (read_c0_config2() & 0xfff) */
  536. /* Have config4, UserLocal */
  537. kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI);
  538. /* Have config5 */
  539. kvm_write_c0_guest_config4(cop0, MIPS_CONF_M);
  540. /* No config6 */
  541. kvm_write_c0_guest_config5(cop0, 0);
  542. /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */
  543. kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10));
  544. /* Status */
  545. kvm_write_c0_guest_status(cop0, ST0_BEV | ST0_ERL);
  546. /*
  547. * Setup IntCtl defaults, compatibility mode for timer interrupts (HW5)
  548. */
  549. kvm_write_c0_guest_intctl(cop0, 0xFC000000);
  550. /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */
  551. kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 |
  552. (vcpu_id & MIPS_EBASE_CPUNUM));
  553. /* Put PC at guest reset vector */
  554. vcpu->arch.pc = KVM_GUEST_CKSEG1ADDR(0x1fc00000);
  555. return 0;
  556. }
  557. static void kvm_trap_emul_flush_shadow_all(struct kvm *kvm)
  558. {
  559. /* Flush GVA page tables and invalidate GVA ASIDs on all VCPUs */
  560. kvm_flush_remote_tlbs(kvm);
  561. }
  562. static void kvm_trap_emul_flush_shadow_memslot(struct kvm *kvm,
  563. const struct kvm_memory_slot *slot)
  564. {
  565. kvm_trap_emul_flush_shadow_all(kvm);
  566. }
  567. static u64 kvm_trap_emul_get_one_regs[] = {
  568. KVM_REG_MIPS_CP0_INDEX,
  569. KVM_REG_MIPS_CP0_ENTRYLO0,
  570. KVM_REG_MIPS_CP0_ENTRYLO1,
  571. KVM_REG_MIPS_CP0_CONTEXT,
  572. KVM_REG_MIPS_CP0_USERLOCAL,
  573. KVM_REG_MIPS_CP0_PAGEMASK,
  574. KVM_REG_MIPS_CP0_WIRED,
  575. KVM_REG_MIPS_CP0_HWRENA,
  576. KVM_REG_MIPS_CP0_BADVADDR,
  577. KVM_REG_MIPS_CP0_COUNT,
  578. KVM_REG_MIPS_CP0_ENTRYHI,
  579. KVM_REG_MIPS_CP0_COMPARE,
  580. KVM_REG_MIPS_CP0_STATUS,
  581. KVM_REG_MIPS_CP0_INTCTL,
  582. KVM_REG_MIPS_CP0_CAUSE,
  583. KVM_REG_MIPS_CP0_EPC,
  584. KVM_REG_MIPS_CP0_PRID,
  585. KVM_REG_MIPS_CP0_EBASE,
  586. KVM_REG_MIPS_CP0_CONFIG,
  587. KVM_REG_MIPS_CP0_CONFIG1,
  588. KVM_REG_MIPS_CP0_CONFIG2,
  589. KVM_REG_MIPS_CP0_CONFIG3,
  590. KVM_REG_MIPS_CP0_CONFIG4,
  591. KVM_REG_MIPS_CP0_CONFIG5,
  592. KVM_REG_MIPS_CP0_CONFIG7,
  593. KVM_REG_MIPS_CP0_ERROREPC,
  594. KVM_REG_MIPS_CP0_KSCRATCH1,
  595. KVM_REG_MIPS_CP0_KSCRATCH2,
  596. KVM_REG_MIPS_CP0_KSCRATCH3,
  597. KVM_REG_MIPS_CP0_KSCRATCH4,
  598. KVM_REG_MIPS_CP0_KSCRATCH5,
  599. KVM_REG_MIPS_CP0_KSCRATCH6,
  600. KVM_REG_MIPS_COUNT_CTL,
  601. KVM_REG_MIPS_COUNT_RESUME,
  602. KVM_REG_MIPS_COUNT_HZ,
  603. };
  604. static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu)
  605. {
  606. return ARRAY_SIZE(kvm_trap_emul_get_one_regs);
  607. }
  608. static int kvm_trap_emul_copy_reg_indices(struct kvm_vcpu *vcpu,
  609. u64 __user *indices)
  610. {
  611. if (copy_to_user(indices, kvm_trap_emul_get_one_regs,
  612. sizeof(kvm_trap_emul_get_one_regs)))
  613. return -EFAULT;
  614. indices += ARRAY_SIZE(kvm_trap_emul_get_one_regs);
  615. return 0;
  616. }
  617. static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu,
  618. const struct kvm_one_reg *reg,
  619. s64 *v)
  620. {
  621. struct mips_coproc *cop0 = vcpu->arch.cop0;
  622. switch (reg->id) {
  623. case KVM_REG_MIPS_CP0_INDEX:
  624. *v = (long)kvm_read_c0_guest_index(cop0);
  625. break;
  626. case KVM_REG_MIPS_CP0_ENTRYLO0:
  627. *v = kvm_read_c0_guest_entrylo0(cop0);
  628. break;
  629. case KVM_REG_MIPS_CP0_ENTRYLO1:
  630. *v = kvm_read_c0_guest_entrylo1(cop0);
  631. break;
  632. case KVM_REG_MIPS_CP0_CONTEXT:
  633. *v = (long)kvm_read_c0_guest_context(cop0);
  634. break;
  635. case KVM_REG_MIPS_CP0_USERLOCAL:
  636. *v = (long)kvm_read_c0_guest_userlocal(cop0);
  637. break;
  638. case KVM_REG_MIPS_CP0_PAGEMASK:
  639. *v = (long)kvm_read_c0_guest_pagemask(cop0);
  640. break;
  641. case KVM_REG_MIPS_CP0_WIRED:
  642. *v = (long)kvm_read_c0_guest_wired(cop0);
  643. break;
  644. case KVM_REG_MIPS_CP0_HWRENA:
  645. *v = (long)kvm_read_c0_guest_hwrena(cop0);
  646. break;
  647. case KVM_REG_MIPS_CP0_BADVADDR:
  648. *v = (long)kvm_read_c0_guest_badvaddr(cop0);
  649. break;
  650. case KVM_REG_MIPS_CP0_ENTRYHI:
  651. *v = (long)kvm_read_c0_guest_entryhi(cop0);
  652. break;
  653. case KVM_REG_MIPS_CP0_COMPARE:
  654. *v = (long)kvm_read_c0_guest_compare(cop0);
  655. break;
  656. case KVM_REG_MIPS_CP0_STATUS:
  657. *v = (long)kvm_read_c0_guest_status(cop0);
  658. break;
  659. case KVM_REG_MIPS_CP0_INTCTL:
  660. *v = (long)kvm_read_c0_guest_intctl(cop0);
  661. break;
  662. case KVM_REG_MIPS_CP0_CAUSE:
  663. *v = (long)kvm_read_c0_guest_cause(cop0);
  664. break;
  665. case KVM_REG_MIPS_CP0_EPC:
  666. *v = (long)kvm_read_c0_guest_epc(cop0);
  667. break;
  668. case KVM_REG_MIPS_CP0_PRID:
  669. *v = (long)kvm_read_c0_guest_prid(cop0);
  670. break;
  671. case KVM_REG_MIPS_CP0_EBASE:
  672. *v = (long)kvm_read_c0_guest_ebase(cop0);
  673. break;
  674. case KVM_REG_MIPS_CP0_CONFIG:
  675. *v = (long)kvm_read_c0_guest_config(cop0);
  676. break;
  677. case KVM_REG_MIPS_CP0_CONFIG1:
  678. *v = (long)kvm_read_c0_guest_config1(cop0);
  679. break;
  680. case KVM_REG_MIPS_CP0_CONFIG2:
  681. *v = (long)kvm_read_c0_guest_config2(cop0);
  682. break;
  683. case KVM_REG_MIPS_CP0_CONFIG3:
  684. *v = (long)kvm_read_c0_guest_config3(cop0);
  685. break;
  686. case KVM_REG_MIPS_CP0_CONFIG4:
  687. *v = (long)kvm_read_c0_guest_config4(cop0);
  688. break;
  689. case KVM_REG_MIPS_CP0_CONFIG5:
  690. *v = (long)kvm_read_c0_guest_config5(cop0);
  691. break;
  692. case KVM_REG_MIPS_CP0_CONFIG7:
  693. *v = (long)kvm_read_c0_guest_config7(cop0);
  694. break;
  695. case KVM_REG_MIPS_CP0_COUNT:
  696. *v = kvm_mips_read_count(vcpu);
  697. break;
  698. case KVM_REG_MIPS_COUNT_CTL:
  699. *v = vcpu->arch.count_ctl;
  700. break;
  701. case KVM_REG_MIPS_COUNT_RESUME:
  702. *v = ktime_to_ns(vcpu->arch.count_resume);
  703. break;
  704. case KVM_REG_MIPS_COUNT_HZ:
  705. *v = vcpu->arch.count_hz;
  706. break;
  707. case KVM_REG_MIPS_CP0_ERROREPC:
  708. *v = (long)kvm_read_c0_guest_errorepc(cop0);
  709. break;
  710. case KVM_REG_MIPS_CP0_KSCRATCH1:
  711. *v = (long)kvm_read_c0_guest_kscratch1(cop0);
  712. break;
  713. case KVM_REG_MIPS_CP0_KSCRATCH2:
  714. *v = (long)kvm_read_c0_guest_kscratch2(cop0);
  715. break;
  716. case KVM_REG_MIPS_CP0_KSCRATCH3:
  717. *v = (long)kvm_read_c0_guest_kscratch3(cop0);
  718. break;
  719. case KVM_REG_MIPS_CP0_KSCRATCH4:
  720. *v = (long)kvm_read_c0_guest_kscratch4(cop0);
  721. break;
  722. case KVM_REG_MIPS_CP0_KSCRATCH5:
  723. *v = (long)kvm_read_c0_guest_kscratch5(cop0);
  724. break;
  725. case KVM_REG_MIPS_CP0_KSCRATCH6:
  726. *v = (long)kvm_read_c0_guest_kscratch6(cop0);
  727. break;
  728. default:
  729. return -EINVAL;
  730. }
  731. return 0;
  732. }
  733. static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu,
  734. const struct kvm_one_reg *reg,
  735. s64 v)
  736. {
  737. struct mips_coproc *cop0 = vcpu->arch.cop0;
  738. int ret = 0;
  739. unsigned int cur, change;
  740. switch (reg->id) {
  741. case KVM_REG_MIPS_CP0_INDEX:
  742. kvm_write_c0_guest_index(cop0, v);
  743. break;
  744. case KVM_REG_MIPS_CP0_ENTRYLO0:
  745. kvm_write_c0_guest_entrylo0(cop0, v);
  746. break;
  747. case KVM_REG_MIPS_CP0_ENTRYLO1:
  748. kvm_write_c0_guest_entrylo1(cop0, v);
  749. break;
  750. case KVM_REG_MIPS_CP0_CONTEXT:
  751. kvm_write_c0_guest_context(cop0, v);
  752. break;
  753. case KVM_REG_MIPS_CP0_USERLOCAL:
  754. kvm_write_c0_guest_userlocal(cop0, v);
  755. break;
  756. case KVM_REG_MIPS_CP0_PAGEMASK:
  757. kvm_write_c0_guest_pagemask(cop0, v);
  758. break;
  759. case KVM_REG_MIPS_CP0_WIRED:
  760. kvm_write_c0_guest_wired(cop0, v);
  761. break;
  762. case KVM_REG_MIPS_CP0_HWRENA:
  763. kvm_write_c0_guest_hwrena(cop0, v);
  764. break;
  765. case KVM_REG_MIPS_CP0_BADVADDR:
  766. kvm_write_c0_guest_badvaddr(cop0, v);
  767. break;
  768. case KVM_REG_MIPS_CP0_ENTRYHI:
  769. kvm_write_c0_guest_entryhi(cop0, v);
  770. break;
  771. case KVM_REG_MIPS_CP0_STATUS:
  772. kvm_write_c0_guest_status(cop0, v);
  773. break;
  774. case KVM_REG_MIPS_CP0_INTCTL:
  775. /* No VInt, so no VS, read-only for now */
  776. break;
  777. case KVM_REG_MIPS_CP0_EPC:
  778. kvm_write_c0_guest_epc(cop0, v);
  779. break;
  780. case KVM_REG_MIPS_CP0_PRID:
  781. kvm_write_c0_guest_prid(cop0, v);
  782. break;
  783. case KVM_REG_MIPS_CP0_EBASE:
  784. /*
  785. * Allow core number to be written, but the exception base must
  786. * remain in guest KSeg0.
  787. */
  788. kvm_change_c0_guest_ebase(cop0, 0x1ffff000 | MIPS_EBASE_CPUNUM,
  789. v);
  790. break;
  791. case KVM_REG_MIPS_CP0_COUNT:
  792. kvm_mips_write_count(vcpu, v);
  793. break;
  794. case KVM_REG_MIPS_CP0_COMPARE:
  795. kvm_mips_write_compare(vcpu, v, false);
  796. break;
  797. case KVM_REG_MIPS_CP0_CAUSE:
  798. /*
  799. * If the timer is stopped or started (DC bit) it must look
  800. * atomic with changes to the interrupt pending bits (TI, IRQ5).
  801. * A timer interrupt should not happen in between.
  802. */
  803. if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) {
  804. if (v & CAUSEF_DC) {
  805. /* disable timer first */
  806. kvm_mips_count_disable_cause(vcpu);
  807. kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
  808. } else {
  809. /* enable timer last */
  810. kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
  811. kvm_mips_count_enable_cause(vcpu);
  812. }
  813. } else {
  814. kvm_write_c0_guest_cause(cop0, v);
  815. }
  816. break;
  817. case KVM_REG_MIPS_CP0_CONFIG:
  818. /* read-only for now */
  819. break;
  820. case KVM_REG_MIPS_CP0_CONFIG1:
  821. cur = kvm_read_c0_guest_config1(cop0);
  822. change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu);
  823. if (change) {
  824. v = cur ^ change;
  825. kvm_write_c0_guest_config1(cop0, v);
  826. }
  827. break;
  828. case KVM_REG_MIPS_CP0_CONFIG2:
  829. /* read-only for now */
  830. break;
  831. case KVM_REG_MIPS_CP0_CONFIG3:
  832. cur = kvm_read_c0_guest_config3(cop0);
  833. change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu);
  834. if (change) {
  835. v = cur ^ change;
  836. kvm_write_c0_guest_config3(cop0, v);
  837. }
  838. break;
  839. case KVM_REG_MIPS_CP0_CONFIG4:
  840. cur = kvm_read_c0_guest_config4(cop0);
  841. change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu);
  842. if (change) {
  843. v = cur ^ change;
  844. kvm_write_c0_guest_config4(cop0, v);
  845. }
  846. break;
  847. case KVM_REG_MIPS_CP0_CONFIG5:
  848. cur = kvm_read_c0_guest_config5(cop0);
  849. change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu);
  850. if (change) {
  851. v = cur ^ change;
  852. kvm_write_c0_guest_config5(cop0, v);
  853. }
  854. break;
  855. case KVM_REG_MIPS_CP0_CONFIG7:
  856. /* writes ignored */
  857. break;
  858. case KVM_REG_MIPS_COUNT_CTL:
  859. ret = kvm_mips_set_count_ctl(vcpu, v);
  860. break;
  861. case KVM_REG_MIPS_COUNT_RESUME:
  862. ret = kvm_mips_set_count_resume(vcpu, v);
  863. break;
  864. case KVM_REG_MIPS_COUNT_HZ:
  865. ret = kvm_mips_set_count_hz(vcpu, v);
  866. break;
  867. case KVM_REG_MIPS_CP0_ERROREPC:
  868. kvm_write_c0_guest_errorepc(cop0, v);
  869. break;
  870. case KVM_REG_MIPS_CP0_KSCRATCH1:
  871. kvm_write_c0_guest_kscratch1(cop0, v);
  872. break;
  873. case KVM_REG_MIPS_CP0_KSCRATCH2:
  874. kvm_write_c0_guest_kscratch2(cop0, v);
  875. break;
  876. case KVM_REG_MIPS_CP0_KSCRATCH3:
  877. kvm_write_c0_guest_kscratch3(cop0, v);
  878. break;
  879. case KVM_REG_MIPS_CP0_KSCRATCH4:
  880. kvm_write_c0_guest_kscratch4(cop0, v);
  881. break;
  882. case KVM_REG_MIPS_CP0_KSCRATCH5:
  883. kvm_write_c0_guest_kscratch5(cop0, v);
  884. break;
  885. case KVM_REG_MIPS_CP0_KSCRATCH6:
  886. kvm_write_c0_guest_kscratch6(cop0, v);
  887. break;
  888. default:
  889. return -EINVAL;
  890. }
  891. return ret;
  892. }
  893. static int kvm_trap_emul_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  894. {
  895. struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
  896. struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
  897. struct mm_struct *mm;
  898. /*
  899. * Were we in guest context? If so, restore the appropriate ASID based
  900. * on the mode of the Guest (Kernel/User).
  901. */
  902. if (current->flags & PF_VCPU) {
  903. mm = KVM_GUEST_KERNEL_MODE(vcpu) ? kern_mm : user_mm;
  904. if ((cpu_context(cpu, mm) ^ asid_cache(cpu)) &
  905. asid_version_mask(cpu))
  906. get_new_mmu_context(mm, cpu);
  907. write_c0_entryhi(cpu_asid(cpu, mm));
  908. TLBMISS_HANDLER_SETUP_PGD(mm->pgd);
  909. kvm_mips_suspend_mm(cpu);
  910. ehb();
  911. }
  912. return 0;
  913. }
  914. static int kvm_trap_emul_vcpu_put(struct kvm_vcpu *vcpu, int cpu)
  915. {
  916. kvm_lose_fpu(vcpu);
  917. if (current->flags & PF_VCPU) {
  918. /* Restore normal Linux process memory map */
  919. if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
  920. asid_version_mask(cpu)))
  921. get_new_mmu_context(current->mm, cpu);
  922. write_c0_entryhi(cpu_asid(cpu, current->mm));
  923. TLBMISS_HANDLER_SETUP_PGD(current->mm->pgd);
  924. kvm_mips_resume_mm(cpu);
  925. ehb();
  926. }
  927. return 0;
  928. }
  929. static void kvm_trap_emul_check_requests(struct kvm_vcpu *vcpu, int cpu,
  930. bool reload_asid)
  931. {
  932. struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
  933. struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
  934. struct mm_struct *mm;
  935. int i;
  936. if (likely(!vcpu->requests))
  937. return;
  938. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
  939. /*
  940. * Both kernel & user GVA mappings must be invalidated. The
  941. * caller is just about to check whether the ASID is stale
  942. * anyway so no need to reload it here.
  943. */
  944. kvm_mips_flush_gva_pt(kern_mm->pgd, KMF_GPA | KMF_KERN);
  945. kvm_mips_flush_gva_pt(user_mm->pgd, KMF_GPA | KMF_USER);
  946. for_each_possible_cpu(i) {
  947. cpu_context(i, kern_mm) = 0;
  948. cpu_context(i, user_mm) = 0;
  949. }
  950. /* Generate new ASID for current mode */
  951. if (reload_asid) {
  952. mm = KVM_GUEST_KERNEL_MODE(vcpu) ? kern_mm : user_mm;
  953. get_new_mmu_context(mm, cpu);
  954. htw_stop();
  955. write_c0_entryhi(cpu_asid(cpu, mm));
  956. TLBMISS_HANDLER_SETUP_PGD(mm->pgd);
  957. htw_start();
  958. }
  959. }
  960. }
  961. /**
  962. * kvm_trap_emul_gva_lockless_begin() - Begin lockless access to GVA space.
  963. * @vcpu: VCPU pointer.
  964. *
  965. * Call before a GVA space access outside of guest mode, to ensure that
  966. * asynchronous TLB flush requests are handled or delayed until completion of
  967. * the GVA access (as indicated by a matching kvm_trap_emul_gva_lockless_end()).
  968. *
  969. * Should be called with IRQs already enabled.
  970. */
  971. void kvm_trap_emul_gva_lockless_begin(struct kvm_vcpu *vcpu)
  972. {
  973. /* We re-enable IRQs in kvm_trap_emul_gva_lockless_end() */
  974. WARN_ON_ONCE(irqs_disabled());
  975. /*
  976. * The caller is about to access the GVA space, so we set the mode to
  977. * force TLB flush requests to send an IPI, and also disable IRQs to
  978. * delay IPI handling until kvm_trap_emul_gva_lockless_end().
  979. */
  980. local_irq_disable();
  981. /*
  982. * Make sure the read of VCPU requests is not reordered ahead of the
  983. * write to vcpu->mode, or we could miss a TLB flush request while
  984. * the requester sees the VCPU as outside of guest mode and not needing
  985. * an IPI.
  986. */
  987. smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES);
  988. /*
  989. * If a TLB flush has been requested (potentially while
  990. * OUTSIDE_GUEST_MODE and assumed immediately effective), perform it
  991. * before accessing the GVA space, and be sure to reload the ASID if
  992. * necessary as it'll be immediately used.
  993. *
  994. * TLB flush requests after this check will trigger an IPI due to the
  995. * mode change above, which will be delayed due to IRQs disabled.
  996. */
  997. kvm_trap_emul_check_requests(vcpu, smp_processor_id(), true);
  998. }
  999. /**
  1000. * kvm_trap_emul_gva_lockless_end() - End lockless access to GVA space.
  1001. * @vcpu: VCPU pointer.
  1002. *
  1003. * Called after a GVA space access outside of guest mode. Should have a matching
  1004. * call to kvm_trap_emul_gva_lockless_begin().
  1005. */
  1006. void kvm_trap_emul_gva_lockless_end(struct kvm_vcpu *vcpu)
  1007. {
  1008. /*
  1009. * Make sure the write to vcpu->mode is not reordered in front of GVA
  1010. * accesses, or a TLB flush requester may not think it necessary to send
  1011. * an IPI.
  1012. */
  1013. smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE);
  1014. /*
  1015. * Now that the access to GVA space is complete, its safe for pending
  1016. * TLB flush request IPIs to be handled (which indicates completion).
  1017. */
  1018. local_irq_enable();
  1019. }
  1020. static void kvm_trap_emul_vcpu_reenter(struct kvm_run *run,
  1021. struct kvm_vcpu *vcpu)
  1022. {
  1023. struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
  1024. struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
  1025. struct mm_struct *mm;
  1026. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1027. int i, cpu = smp_processor_id();
  1028. unsigned int gasid;
  1029. /*
  1030. * No need to reload ASID, IRQs are disabled already so there's no rush,
  1031. * and we'll check if we need to regenerate below anyway before
  1032. * re-entering the guest.
  1033. */
  1034. kvm_trap_emul_check_requests(vcpu, cpu, false);
  1035. if (KVM_GUEST_KERNEL_MODE(vcpu)) {
  1036. mm = kern_mm;
  1037. } else {
  1038. mm = user_mm;
  1039. /*
  1040. * Lazy host ASID regeneration / PT flush for guest user mode.
  1041. * If the guest ASID has changed since the last guest usermode
  1042. * execution, invalidate the stale TLB entries and flush GVA PT
  1043. * entries too.
  1044. */
  1045. gasid = kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID;
  1046. if (gasid != vcpu->arch.last_user_gasid) {
  1047. kvm_mips_flush_gva_pt(user_mm->pgd, KMF_USER);
  1048. for_each_possible_cpu(i)
  1049. cpu_context(i, user_mm) = 0;
  1050. vcpu->arch.last_user_gasid = gasid;
  1051. }
  1052. }
  1053. /*
  1054. * Check if ASID is stale. This may happen due to a TLB flush request or
  1055. * a lazy user MM invalidation.
  1056. */
  1057. if ((cpu_context(cpu, mm) ^ asid_cache(cpu)) &
  1058. asid_version_mask(cpu))
  1059. get_new_mmu_context(mm, cpu);
  1060. }
  1061. static int kvm_trap_emul_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  1062. {
  1063. int cpu = smp_processor_id();
  1064. int r;
  1065. /* Check if we have any exceptions/interrupts pending */
  1066. kvm_mips_deliver_interrupts(vcpu,
  1067. kvm_read_c0_guest_cause(vcpu->arch.cop0));
  1068. kvm_trap_emul_vcpu_reenter(run, vcpu);
  1069. /*
  1070. * We use user accessors to access guest memory, but we don't want to
  1071. * invoke Linux page faulting.
  1072. */
  1073. pagefault_disable();
  1074. /* Disable hardware page table walking while in guest */
  1075. htw_stop();
  1076. /*
  1077. * While in guest context we're in the guest's address space, not the
  1078. * host process address space, so we need to be careful not to confuse
  1079. * e.g. cache management IPIs.
  1080. */
  1081. kvm_mips_suspend_mm(cpu);
  1082. r = vcpu->arch.vcpu_run(run, vcpu);
  1083. /* We may have migrated while handling guest exits */
  1084. cpu = smp_processor_id();
  1085. /* Restore normal Linux process memory map */
  1086. if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
  1087. asid_version_mask(cpu)))
  1088. get_new_mmu_context(current->mm, cpu);
  1089. write_c0_entryhi(cpu_asid(cpu, current->mm));
  1090. TLBMISS_HANDLER_SETUP_PGD(current->mm->pgd);
  1091. kvm_mips_resume_mm(cpu);
  1092. htw_start();
  1093. pagefault_enable();
  1094. return r;
  1095. }
  1096. static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
  1097. /* exit handlers */
  1098. .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
  1099. .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod,
  1100. .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss,
  1101. .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss,
  1102. .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st,
  1103. .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld,
  1104. .handle_syscall = kvm_trap_emul_handle_syscall,
  1105. .handle_res_inst = kvm_trap_emul_handle_res_inst,
  1106. .handle_break = kvm_trap_emul_handle_break,
  1107. .handle_trap = kvm_trap_emul_handle_trap,
  1108. .handle_msa_fpe = kvm_trap_emul_handle_msa_fpe,
  1109. .handle_fpe = kvm_trap_emul_handle_fpe,
  1110. .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled,
  1111. .vcpu_init = kvm_trap_emul_vcpu_init,
  1112. .vcpu_uninit = kvm_trap_emul_vcpu_uninit,
  1113. .vcpu_setup = kvm_trap_emul_vcpu_setup,
  1114. .flush_shadow_all = kvm_trap_emul_flush_shadow_all,
  1115. .flush_shadow_memslot = kvm_trap_emul_flush_shadow_memslot,
  1116. .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
  1117. .queue_timer_int = kvm_mips_queue_timer_int_cb,
  1118. .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,
  1119. .queue_io_int = kvm_mips_queue_io_int_cb,
  1120. .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
  1121. .irq_deliver = kvm_mips_irq_deliver_cb,
  1122. .irq_clear = kvm_mips_irq_clear_cb,
  1123. .num_regs = kvm_trap_emul_num_regs,
  1124. .copy_reg_indices = kvm_trap_emul_copy_reg_indices,
  1125. .get_one_reg = kvm_trap_emul_get_one_reg,
  1126. .set_one_reg = kvm_trap_emul_set_one_reg,
  1127. .vcpu_load = kvm_trap_emul_vcpu_load,
  1128. .vcpu_put = kvm_trap_emul_vcpu_put,
  1129. .vcpu_run = kvm_trap_emul_vcpu_run,
  1130. .vcpu_reenter = kvm_trap_emul_vcpu_reenter,
  1131. };
  1132. int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
  1133. {
  1134. *install_callbacks = &kvm_trap_emul_callbacks;
  1135. return 0;
  1136. }