trap_emul.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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/module.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/kvm_host.h>
  16. #include "interrupt.h"
  17. static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva)
  18. {
  19. gpa_t gpa;
  20. gva_t kseg = KSEGX(gva);
  21. if ((kseg == CKSEG0) || (kseg == CKSEG1))
  22. gpa = CPHYSADDR(gva);
  23. else {
  24. kvm_err("%s: cannot find GPA for GVA: %#lx\n", __func__, gva);
  25. kvm_mips_dump_host_tlbs();
  26. gpa = KVM_INVALID_ADDR;
  27. }
  28. kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa);
  29. return gpa;
  30. }
  31. static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu)
  32. {
  33. struct mips_coproc *cop0 = vcpu->arch.cop0;
  34. struct kvm_run *run = vcpu->run;
  35. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  36. u32 cause = vcpu->arch.host_cp0_cause;
  37. enum emulation_result er = EMULATE_DONE;
  38. int ret = RESUME_GUEST;
  39. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
  40. /* FPU Unusable */
  41. if (!kvm_mips_guest_has_fpu(&vcpu->arch) ||
  42. (kvm_read_c0_guest_status(cop0) & ST0_CU1) == 0) {
  43. /*
  44. * Unusable/no FPU in guest:
  45. * deliver guest COP1 Unusable Exception
  46. */
  47. er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu);
  48. } else {
  49. /* Restore FPU state */
  50. kvm_own_fpu(vcpu);
  51. er = EMULATE_DONE;
  52. }
  53. } else {
  54. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  55. }
  56. switch (er) {
  57. case EMULATE_DONE:
  58. ret = RESUME_GUEST;
  59. break;
  60. case EMULATE_FAIL:
  61. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  62. ret = RESUME_HOST;
  63. break;
  64. case EMULATE_WAIT:
  65. run->exit_reason = KVM_EXIT_INTR;
  66. ret = RESUME_HOST;
  67. break;
  68. default:
  69. BUG();
  70. }
  71. return ret;
  72. }
  73. static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu)
  74. {
  75. struct kvm_run *run = vcpu->run;
  76. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  77. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  78. u32 cause = vcpu->arch.host_cp0_cause;
  79. enum emulation_result er = EMULATE_DONE;
  80. int ret = RESUME_GUEST;
  81. if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
  82. || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
  83. kvm_debug("USER/KSEG23 ADDR TLB MOD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
  84. cause, opc, badvaddr);
  85. er = kvm_mips_handle_tlbmod(cause, opc, run, vcpu);
  86. if (er == EMULATE_DONE)
  87. ret = RESUME_GUEST;
  88. else {
  89. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  90. ret = RESUME_HOST;
  91. }
  92. } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
  93. /*
  94. * XXXKYMA: The guest kernel does not expect to get this fault
  95. * when we are not using HIGHMEM. Need to address this in a
  96. * HIGHMEM kernel
  97. */
  98. kvm_err("TLB MOD fault not handled, cause %#x, PC: %p, BadVaddr: %#lx\n",
  99. cause, opc, badvaddr);
  100. kvm_mips_dump_host_tlbs();
  101. kvm_arch_vcpu_dump_regs(vcpu);
  102. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  103. ret = RESUME_HOST;
  104. } else {
  105. kvm_err("Illegal TLB Mod fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
  106. cause, opc, badvaddr);
  107. kvm_mips_dump_host_tlbs();
  108. kvm_arch_vcpu_dump_regs(vcpu);
  109. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  110. ret = RESUME_HOST;
  111. }
  112. return ret;
  113. }
  114. static int kvm_trap_emul_handle_tlb_miss(struct kvm_vcpu *vcpu, bool store)
  115. {
  116. struct kvm_run *run = vcpu->run;
  117. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  118. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  119. u32 cause = vcpu->arch.host_cp0_cause;
  120. enum emulation_result er = EMULATE_DONE;
  121. int ret = RESUME_GUEST;
  122. if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR)
  123. && KVM_GUEST_KERNEL_MODE(vcpu)) {
  124. if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) {
  125. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  126. ret = RESUME_HOST;
  127. }
  128. } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
  129. || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
  130. kvm_debug("USER ADDR TLB %s fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
  131. store ? "ST" : "LD", cause, opc, badvaddr);
  132. /*
  133. * User Address (UA) fault, this could happen if
  134. * (1) TLB entry not present/valid in both Guest and shadow host
  135. * TLBs, in this case we pass on the fault to the guest
  136. * kernel and let it handle it.
  137. * (2) TLB entry is present in the Guest TLB but not in the
  138. * shadow, in this case we inject the TLB from the Guest TLB
  139. * into the shadow host TLB
  140. */
  141. er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu);
  142. if (er == EMULATE_DONE)
  143. ret = RESUME_GUEST;
  144. else {
  145. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  146. ret = RESUME_HOST;
  147. }
  148. } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
  149. /*
  150. * All KSEG0 faults are handled by KVM, as the guest kernel does
  151. * not expect to ever get them
  152. */
  153. if (kvm_mips_handle_kseg0_tlb_fault
  154. (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) {
  155. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  156. ret = RESUME_HOST;
  157. }
  158. } else if (KVM_GUEST_KERNEL_MODE(vcpu)
  159. && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
  160. /*
  161. * With EVA we may get a TLB exception instead of an address
  162. * error when the guest performs MMIO to KSeg1 addresses.
  163. */
  164. kvm_debug("Emulate %s MMIO space\n",
  165. store ? "Store to" : "Load from");
  166. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  167. if (er == EMULATE_FAIL) {
  168. kvm_err("Emulate %s MMIO space failed\n",
  169. store ? "Store to" : "Load from");
  170. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  171. ret = RESUME_HOST;
  172. } else {
  173. run->exit_reason = KVM_EXIT_MMIO;
  174. ret = RESUME_HOST;
  175. }
  176. } else {
  177. kvm_err("Illegal TLB %s fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
  178. store ? "ST" : "LD", cause, opc, badvaddr);
  179. kvm_mips_dump_host_tlbs();
  180. kvm_arch_vcpu_dump_regs(vcpu);
  181. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  182. ret = RESUME_HOST;
  183. }
  184. return ret;
  185. }
  186. static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
  187. {
  188. return kvm_trap_emul_handle_tlb_miss(vcpu, true);
  189. }
  190. static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
  191. {
  192. return kvm_trap_emul_handle_tlb_miss(vcpu, false);
  193. }
  194. static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu)
  195. {
  196. struct kvm_run *run = vcpu->run;
  197. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  198. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  199. u32 cause = vcpu->arch.host_cp0_cause;
  200. enum emulation_result er = EMULATE_DONE;
  201. int ret = RESUME_GUEST;
  202. if (KVM_GUEST_KERNEL_MODE(vcpu)
  203. && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
  204. kvm_debug("Emulate Store to MMIO space\n");
  205. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  206. if (er == EMULATE_FAIL) {
  207. kvm_err("Emulate Store to MMIO space failed\n");
  208. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  209. ret = RESUME_HOST;
  210. } else {
  211. run->exit_reason = KVM_EXIT_MMIO;
  212. ret = RESUME_HOST;
  213. }
  214. } else {
  215. kvm_err("Address Error (STORE): cause %#x, PC: %p, BadVaddr: %#lx\n",
  216. cause, opc, badvaddr);
  217. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  218. ret = RESUME_HOST;
  219. }
  220. return ret;
  221. }
  222. static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu)
  223. {
  224. struct kvm_run *run = vcpu->run;
  225. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  226. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  227. u32 cause = vcpu->arch.host_cp0_cause;
  228. enum emulation_result er = EMULATE_DONE;
  229. int ret = RESUME_GUEST;
  230. if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) {
  231. kvm_debug("Emulate Load from MMIO space @ %#lx\n", badvaddr);
  232. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  233. if (er == EMULATE_FAIL) {
  234. kvm_err("Emulate Load from MMIO space failed\n");
  235. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  236. ret = RESUME_HOST;
  237. } else {
  238. run->exit_reason = KVM_EXIT_MMIO;
  239. ret = RESUME_HOST;
  240. }
  241. } else {
  242. kvm_err("Address Error (LOAD): cause %#x, PC: %p, BadVaddr: %#lx\n",
  243. cause, opc, badvaddr);
  244. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  245. ret = RESUME_HOST;
  246. er = EMULATE_FAIL;
  247. }
  248. return ret;
  249. }
  250. static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu)
  251. {
  252. struct kvm_run *run = vcpu->run;
  253. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  254. u32 cause = vcpu->arch.host_cp0_cause;
  255. enum emulation_result er = EMULATE_DONE;
  256. int ret = RESUME_GUEST;
  257. er = kvm_mips_emulate_syscall(cause, opc, run, vcpu);
  258. if (er == EMULATE_DONE)
  259. ret = RESUME_GUEST;
  260. else {
  261. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  262. ret = RESUME_HOST;
  263. }
  264. return ret;
  265. }
  266. static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu)
  267. {
  268. struct kvm_run *run = vcpu->run;
  269. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  270. u32 cause = vcpu->arch.host_cp0_cause;
  271. enum emulation_result er = EMULATE_DONE;
  272. int ret = RESUME_GUEST;
  273. er = kvm_mips_handle_ri(cause, opc, run, vcpu);
  274. if (er == EMULATE_DONE)
  275. ret = RESUME_GUEST;
  276. else {
  277. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  278. ret = RESUME_HOST;
  279. }
  280. return ret;
  281. }
  282. static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu)
  283. {
  284. struct kvm_run *run = vcpu->run;
  285. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  286. u32 cause = vcpu->arch.host_cp0_cause;
  287. enum emulation_result er = EMULATE_DONE;
  288. int ret = RESUME_GUEST;
  289. er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu);
  290. if (er == EMULATE_DONE)
  291. ret = RESUME_GUEST;
  292. else {
  293. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  294. ret = RESUME_HOST;
  295. }
  296. return ret;
  297. }
  298. static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu)
  299. {
  300. struct kvm_run *run = vcpu->run;
  301. u32 __user *opc = (u32 __user *)vcpu->arch.pc;
  302. u32 cause = vcpu->arch.host_cp0_cause;
  303. enum emulation_result er = EMULATE_DONE;
  304. int ret = RESUME_GUEST;
  305. er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu);
  306. if (er == EMULATE_DONE) {
  307. ret = RESUME_GUEST;
  308. } else {
  309. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  310. ret = RESUME_HOST;
  311. }
  312. return ret;
  313. }
  314. static int kvm_trap_emul_handle_msa_fpe(struct kvm_vcpu *vcpu)
  315. {
  316. struct kvm_run *run = vcpu->run;
  317. u32 __user *opc = (u32 __user *)vcpu->arch.pc;
  318. u32 cause = vcpu->arch.host_cp0_cause;
  319. enum emulation_result er = EMULATE_DONE;
  320. int ret = RESUME_GUEST;
  321. er = kvm_mips_emulate_msafpe_exc(cause, opc, run, vcpu);
  322. if (er == EMULATE_DONE) {
  323. ret = RESUME_GUEST;
  324. } else {
  325. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  326. ret = RESUME_HOST;
  327. }
  328. return ret;
  329. }
  330. static int kvm_trap_emul_handle_fpe(struct kvm_vcpu *vcpu)
  331. {
  332. struct kvm_run *run = vcpu->run;
  333. u32 __user *opc = (u32 __user *)vcpu->arch.pc;
  334. u32 cause = vcpu->arch.host_cp0_cause;
  335. enum emulation_result er = EMULATE_DONE;
  336. int ret = RESUME_GUEST;
  337. er = kvm_mips_emulate_fpe_exc(cause, opc, run, vcpu);
  338. if (er == EMULATE_DONE) {
  339. ret = RESUME_GUEST;
  340. } else {
  341. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  342. ret = RESUME_HOST;
  343. }
  344. return ret;
  345. }
  346. /**
  347. * kvm_trap_emul_handle_msa_disabled() - Guest used MSA while disabled in root.
  348. * @vcpu: Virtual CPU context.
  349. *
  350. * Handle when the guest attempts to use MSA when it is disabled.
  351. */
  352. static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu)
  353. {
  354. struct mips_coproc *cop0 = vcpu->arch.cop0;
  355. struct kvm_run *run = vcpu->run;
  356. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  357. u32 cause = vcpu->arch.host_cp0_cause;
  358. enum emulation_result er = EMULATE_DONE;
  359. int ret = RESUME_GUEST;
  360. if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
  361. (kvm_read_c0_guest_status(cop0) & (ST0_CU1 | ST0_FR)) == ST0_CU1) {
  362. /*
  363. * No MSA in guest, or FPU enabled and not in FR=1 mode,
  364. * guest reserved instruction exception
  365. */
  366. er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  367. } else if (!(kvm_read_c0_guest_config5(cop0) & MIPS_CONF5_MSAEN)) {
  368. /* MSA disabled by guest, guest MSA disabled exception */
  369. er = kvm_mips_emulate_msadis_exc(cause, opc, run, vcpu);
  370. } else {
  371. /* Restore MSA/FPU state */
  372. kvm_own_msa(vcpu);
  373. er = EMULATE_DONE;
  374. }
  375. switch (er) {
  376. case EMULATE_DONE:
  377. ret = RESUME_GUEST;
  378. break;
  379. case EMULATE_FAIL:
  380. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  381. ret = RESUME_HOST;
  382. break;
  383. default:
  384. BUG();
  385. }
  386. return ret;
  387. }
  388. static int kvm_trap_emul_vm_init(struct kvm *kvm)
  389. {
  390. return 0;
  391. }
  392. static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu)
  393. {
  394. vcpu->arch.kscratch_enabled = 0xfc;
  395. return 0;
  396. }
  397. static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
  398. {
  399. struct mips_coproc *cop0 = vcpu->arch.cop0;
  400. u32 config, config1;
  401. int vcpu_id = vcpu->vcpu_id;
  402. /*
  403. * Arch specific stuff, set up config registers properly so that the
  404. * guest will come up as expected
  405. */
  406. #ifndef CONFIG_CPU_MIPSR6
  407. /* r2-r5, simulate a MIPS 24kc */
  408. kvm_write_c0_guest_prid(cop0, 0x00019300);
  409. #else
  410. /* r6+, simulate a generic QEMU machine */
  411. kvm_write_c0_guest_prid(cop0, 0x00010000);
  412. #endif
  413. /*
  414. * Have config1, Cacheable, noncoherent, write-back, write allocate.
  415. * Endianness, arch revision & virtually tagged icache should match
  416. * host.
  417. */
  418. config = read_c0_config() & MIPS_CONF_AR;
  419. config |= MIPS_CONF_M | CONF_CM_CACHABLE_NONCOHERENT | MIPS_CONF_MT_TLB;
  420. #ifdef CONFIG_CPU_BIG_ENDIAN
  421. config |= CONF_BE;
  422. #endif
  423. if (cpu_has_vtag_icache)
  424. config |= MIPS_CONF_VI;
  425. kvm_write_c0_guest_config(cop0, config);
  426. /* Read the cache characteristics from the host Config1 Register */
  427. config1 = (read_c0_config1() & ~0x7f);
  428. /* Set up MMU size */
  429. config1 &= ~(0x3f << 25);
  430. config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25);
  431. /* We unset some bits that we aren't emulating */
  432. config1 &= ~(MIPS_CONF1_C2 | MIPS_CONF1_MD | MIPS_CONF1_PC |
  433. MIPS_CONF1_WR | MIPS_CONF1_CA);
  434. kvm_write_c0_guest_config1(cop0, config1);
  435. /* Have config3, no tertiary/secondary caches implemented */
  436. kvm_write_c0_guest_config2(cop0, MIPS_CONF_M);
  437. /* MIPS_CONF_M | (read_c0_config2() & 0xfff) */
  438. /* Have config4, UserLocal */
  439. kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI);
  440. /* Have config5 */
  441. kvm_write_c0_guest_config4(cop0, MIPS_CONF_M);
  442. /* No config6 */
  443. kvm_write_c0_guest_config5(cop0, 0);
  444. /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */
  445. kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10));
  446. /*
  447. * Setup IntCtl defaults, compatibility mode for timer interrupts (HW5)
  448. */
  449. kvm_write_c0_guest_intctl(cop0, 0xFC000000);
  450. /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */
  451. kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 |
  452. (vcpu_id & MIPS_EBASE_CPUNUM));
  453. return 0;
  454. }
  455. static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu)
  456. {
  457. return 0;
  458. }
  459. static int kvm_trap_emul_copy_reg_indices(struct kvm_vcpu *vcpu,
  460. u64 __user *indices)
  461. {
  462. return 0;
  463. }
  464. static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu,
  465. const struct kvm_one_reg *reg,
  466. s64 *v)
  467. {
  468. switch (reg->id) {
  469. case KVM_REG_MIPS_CP0_COUNT:
  470. *v = kvm_mips_read_count(vcpu);
  471. break;
  472. case KVM_REG_MIPS_COUNT_CTL:
  473. *v = vcpu->arch.count_ctl;
  474. break;
  475. case KVM_REG_MIPS_COUNT_RESUME:
  476. *v = ktime_to_ns(vcpu->arch.count_resume);
  477. break;
  478. case KVM_REG_MIPS_COUNT_HZ:
  479. *v = vcpu->arch.count_hz;
  480. break;
  481. default:
  482. return -EINVAL;
  483. }
  484. return 0;
  485. }
  486. static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu,
  487. const struct kvm_one_reg *reg,
  488. s64 v)
  489. {
  490. struct mips_coproc *cop0 = vcpu->arch.cop0;
  491. int ret = 0;
  492. unsigned int cur, change;
  493. switch (reg->id) {
  494. case KVM_REG_MIPS_CP0_COUNT:
  495. kvm_mips_write_count(vcpu, v);
  496. break;
  497. case KVM_REG_MIPS_CP0_COMPARE:
  498. kvm_mips_write_compare(vcpu, v, false);
  499. break;
  500. case KVM_REG_MIPS_CP0_CAUSE:
  501. /*
  502. * If the timer is stopped or started (DC bit) it must look
  503. * atomic with changes to the interrupt pending bits (TI, IRQ5).
  504. * A timer interrupt should not happen in between.
  505. */
  506. if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) {
  507. if (v & CAUSEF_DC) {
  508. /* disable timer first */
  509. kvm_mips_count_disable_cause(vcpu);
  510. kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
  511. } else {
  512. /* enable timer last */
  513. kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
  514. kvm_mips_count_enable_cause(vcpu);
  515. }
  516. } else {
  517. kvm_write_c0_guest_cause(cop0, v);
  518. }
  519. break;
  520. case KVM_REG_MIPS_CP0_CONFIG:
  521. /* read-only for now */
  522. break;
  523. case KVM_REG_MIPS_CP0_CONFIG1:
  524. cur = kvm_read_c0_guest_config1(cop0);
  525. change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu);
  526. if (change) {
  527. v = cur ^ change;
  528. kvm_write_c0_guest_config1(cop0, v);
  529. }
  530. break;
  531. case KVM_REG_MIPS_CP0_CONFIG2:
  532. /* read-only for now */
  533. break;
  534. case KVM_REG_MIPS_CP0_CONFIG3:
  535. cur = kvm_read_c0_guest_config3(cop0);
  536. change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu);
  537. if (change) {
  538. v = cur ^ change;
  539. kvm_write_c0_guest_config3(cop0, v);
  540. }
  541. break;
  542. case KVM_REG_MIPS_CP0_CONFIG4:
  543. cur = kvm_read_c0_guest_config4(cop0);
  544. change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu);
  545. if (change) {
  546. v = cur ^ change;
  547. kvm_write_c0_guest_config4(cop0, v);
  548. }
  549. break;
  550. case KVM_REG_MIPS_CP0_CONFIG5:
  551. cur = kvm_read_c0_guest_config5(cop0);
  552. change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu);
  553. if (change) {
  554. v = cur ^ change;
  555. kvm_write_c0_guest_config5(cop0, v);
  556. }
  557. break;
  558. case KVM_REG_MIPS_COUNT_CTL:
  559. ret = kvm_mips_set_count_ctl(vcpu, v);
  560. break;
  561. case KVM_REG_MIPS_COUNT_RESUME:
  562. ret = kvm_mips_set_count_resume(vcpu, v);
  563. break;
  564. case KVM_REG_MIPS_COUNT_HZ:
  565. ret = kvm_mips_set_count_hz(vcpu, v);
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. return ret;
  571. }
  572. static int kvm_trap_emul_vcpu_get_regs(struct kvm_vcpu *vcpu)
  573. {
  574. kvm_lose_fpu(vcpu);
  575. return 0;
  576. }
  577. static int kvm_trap_emul_vcpu_set_regs(struct kvm_vcpu *vcpu)
  578. {
  579. return 0;
  580. }
  581. static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
  582. /* exit handlers */
  583. .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
  584. .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod,
  585. .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss,
  586. .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss,
  587. .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st,
  588. .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld,
  589. .handle_syscall = kvm_trap_emul_handle_syscall,
  590. .handle_res_inst = kvm_trap_emul_handle_res_inst,
  591. .handle_break = kvm_trap_emul_handle_break,
  592. .handle_trap = kvm_trap_emul_handle_trap,
  593. .handle_msa_fpe = kvm_trap_emul_handle_msa_fpe,
  594. .handle_fpe = kvm_trap_emul_handle_fpe,
  595. .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled,
  596. .vm_init = kvm_trap_emul_vm_init,
  597. .vcpu_init = kvm_trap_emul_vcpu_init,
  598. .vcpu_setup = kvm_trap_emul_vcpu_setup,
  599. .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
  600. .queue_timer_int = kvm_mips_queue_timer_int_cb,
  601. .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,
  602. .queue_io_int = kvm_mips_queue_io_int_cb,
  603. .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
  604. .irq_deliver = kvm_mips_irq_deliver_cb,
  605. .irq_clear = kvm_mips_irq_clear_cb,
  606. .num_regs = kvm_trap_emul_num_regs,
  607. .copy_reg_indices = kvm_trap_emul_copy_reg_indices,
  608. .get_one_reg = kvm_trap_emul_get_one_reg,
  609. .set_one_reg = kvm_trap_emul_set_one_reg,
  610. .vcpu_get_regs = kvm_trap_emul_vcpu_get_regs,
  611. .vcpu_set_regs = kvm_trap_emul_vcpu_set_regs,
  612. };
  613. int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
  614. {
  615. *install_callbacks = &kvm_trap_emul_callbacks;
  616. return 0;
  617. }