trap_emul.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 {
  159. kvm_err("Illegal TLB %s fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
  160. store ? "ST" : "LD", cause, opc, badvaddr);
  161. kvm_mips_dump_host_tlbs();
  162. kvm_arch_vcpu_dump_regs(vcpu);
  163. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  164. ret = RESUME_HOST;
  165. }
  166. return ret;
  167. }
  168. static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
  169. {
  170. return kvm_trap_emul_handle_tlb_miss(vcpu, true);
  171. }
  172. static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
  173. {
  174. return kvm_trap_emul_handle_tlb_miss(vcpu, false);
  175. }
  176. static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu)
  177. {
  178. struct kvm_run *run = vcpu->run;
  179. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  180. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  181. u32 cause = vcpu->arch.host_cp0_cause;
  182. enum emulation_result er = EMULATE_DONE;
  183. int ret = RESUME_GUEST;
  184. if (KVM_GUEST_KERNEL_MODE(vcpu)
  185. && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
  186. kvm_debug("Emulate Store to MMIO space\n");
  187. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  188. if (er == EMULATE_FAIL) {
  189. kvm_err("Emulate Store to MMIO space failed\n");
  190. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  191. ret = RESUME_HOST;
  192. } else {
  193. run->exit_reason = KVM_EXIT_MMIO;
  194. ret = RESUME_HOST;
  195. }
  196. } else {
  197. kvm_err("Address Error (STORE): cause %#x, PC: %p, BadVaddr: %#lx\n",
  198. cause, opc, badvaddr);
  199. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  200. ret = RESUME_HOST;
  201. }
  202. return ret;
  203. }
  204. static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu)
  205. {
  206. struct kvm_run *run = vcpu->run;
  207. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  208. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  209. u32 cause = vcpu->arch.host_cp0_cause;
  210. enum emulation_result er = EMULATE_DONE;
  211. int ret = RESUME_GUEST;
  212. if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) {
  213. kvm_debug("Emulate Load from MMIO space @ %#lx\n", badvaddr);
  214. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  215. if (er == EMULATE_FAIL) {
  216. kvm_err("Emulate Load from MMIO space failed\n");
  217. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  218. ret = RESUME_HOST;
  219. } else {
  220. run->exit_reason = KVM_EXIT_MMIO;
  221. ret = RESUME_HOST;
  222. }
  223. } else {
  224. kvm_err("Address Error (LOAD): cause %#x, PC: %p, BadVaddr: %#lx\n",
  225. cause, opc, badvaddr);
  226. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  227. ret = RESUME_HOST;
  228. er = EMULATE_FAIL;
  229. }
  230. return ret;
  231. }
  232. static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu)
  233. {
  234. struct kvm_run *run = vcpu->run;
  235. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  236. u32 cause = vcpu->arch.host_cp0_cause;
  237. enum emulation_result er = EMULATE_DONE;
  238. int ret = RESUME_GUEST;
  239. er = kvm_mips_emulate_syscall(cause, opc, run, vcpu);
  240. if (er == EMULATE_DONE)
  241. ret = RESUME_GUEST;
  242. else {
  243. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  244. ret = RESUME_HOST;
  245. }
  246. return ret;
  247. }
  248. static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu)
  249. {
  250. struct kvm_run *run = vcpu->run;
  251. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  252. u32 cause = vcpu->arch.host_cp0_cause;
  253. enum emulation_result er = EMULATE_DONE;
  254. int ret = RESUME_GUEST;
  255. er = kvm_mips_handle_ri(cause, opc, run, vcpu);
  256. if (er == EMULATE_DONE)
  257. ret = RESUME_GUEST;
  258. else {
  259. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  260. ret = RESUME_HOST;
  261. }
  262. return ret;
  263. }
  264. static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu)
  265. {
  266. struct kvm_run *run = vcpu->run;
  267. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  268. u32 cause = vcpu->arch.host_cp0_cause;
  269. enum emulation_result er = EMULATE_DONE;
  270. int ret = RESUME_GUEST;
  271. er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu);
  272. if (er == EMULATE_DONE)
  273. ret = RESUME_GUEST;
  274. else {
  275. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  276. ret = RESUME_HOST;
  277. }
  278. return ret;
  279. }
  280. static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu)
  281. {
  282. struct kvm_run *run = vcpu->run;
  283. u32 __user *opc = (u32 __user *)vcpu->arch.pc;
  284. u32 cause = vcpu->arch.host_cp0_cause;
  285. enum emulation_result er = EMULATE_DONE;
  286. int ret = RESUME_GUEST;
  287. er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu);
  288. if (er == EMULATE_DONE) {
  289. ret = RESUME_GUEST;
  290. } else {
  291. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  292. ret = RESUME_HOST;
  293. }
  294. return ret;
  295. }
  296. static int kvm_trap_emul_handle_msa_fpe(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_msafpe_exc(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_fpe(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_emulate_fpe_exc(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. /**
  329. * kvm_trap_emul_handle_msa_disabled() - Guest used MSA while disabled in root.
  330. * @vcpu: Virtual CPU context.
  331. *
  332. * Handle when the guest attempts to use MSA when it is disabled.
  333. */
  334. static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu)
  335. {
  336. struct mips_coproc *cop0 = vcpu->arch.cop0;
  337. struct kvm_run *run = vcpu->run;
  338. u32 __user *opc = (u32 __user *) vcpu->arch.pc;
  339. u32 cause = vcpu->arch.host_cp0_cause;
  340. enum emulation_result er = EMULATE_DONE;
  341. int ret = RESUME_GUEST;
  342. if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
  343. (kvm_read_c0_guest_status(cop0) & (ST0_CU1 | ST0_FR)) == ST0_CU1) {
  344. /*
  345. * No MSA in guest, or FPU enabled and not in FR=1 mode,
  346. * guest reserved instruction exception
  347. */
  348. er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  349. } else if (!(kvm_read_c0_guest_config5(cop0) & MIPS_CONF5_MSAEN)) {
  350. /* MSA disabled by guest, guest MSA disabled exception */
  351. er = kvm_mips_emulate_msadis_exc(cause, opc, run, vcpu);
  352. } else {
  353. /* Restore MSA/FPU state */
  354. kvm_own_msa(vcpu);
  355. er = EMULATE_DONE;
  356. }
  357. switch (er) {
  358. case EMULATE_DONE:
  359. ret = RESUME_GUEST;
  360. break;
  361. case EMULATE_FAIL:
  362. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  363. ret = RESUME_HOST;
  364. break;
  365. default:
  366. BUG();
  367. }
  368. return ret;
  369. }
  370. static int kvm_trap_emul_vm_init(struct kvm *kvm)
  371. {
  372. return 0;
  373. }
  374. static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu)
  375. {
  376. vcpu->arch.kscratch_enabled = 0xfc;
  377. return 0;
  378. }
  379. static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
  380. {
  381. struct mips_coproc *cop0 = vcpu->arch.cop0;
  382. u32 config, config1;
  383. int vcpu_id = vcpu->vcpu_id;
  384. /*
  385. * Arch specific stuff, set up config registers properly so that the
  386. * guest will come up as expected
  387. */
  388. #ifndef CONFIG_CPU_MIPSR6
  389. /* r2-r5, simulate a MIPS 24kc */
  390. kvm_write_c0_guest_prid(cop0, 0x00019300);
  391. #else
  392. /* r6+, simulate a generic QEMU machine */
  393. kvm_write_c0_guest_prid(cop0, 0x00010000);
  394. #endif
  395. /*
  396. * Have config1, Cacheable, noncoherent, write-back, write allocate.
  397. * Endianness, arch revision & virtually tagged icache should match
  398. * host.
  399. */
  400. config = read_c0_config() & MIPS_CONF_AR;
  401. config |= MIPS_CONF_M | CONF_CM_CACHABLE_NONCOHERENT | MIPS_CONF_MT_TLB;
  402. #ifdef CONFIG_CPU_BIG_ENDIAN
  403. config |= CONF_BE;
  404. #endif
  405. if (cpu_has_vtag_icache)
  406. config |= MIPS_CONF_VI;
  407. kvm_write_c0_guest_config(cop0, config);
  408. /* Read the cache characteristics from the host Config1 Register */
  409. config1 = (read_c0_config1() & ~0x7f);
  410. /* Set up MMU size */
  411. config1 &= ~(0x3f << 25);
  412. config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25);
  413. /* We unset some bits that we aren't emulating */
  414. config1 &= ~(MIPS_CONF1_C2 | MIPS_CONF1_MD | MIPS_CONF1_PC |
  415. MIPS_CONF1_WR | MIPS_CONF1_CA);
  416. kvm_write_c0_guest_config1(cop0, config1);
  417. /* Have config3, no tertiary/secondary caches implemented */
  418. kvm_write_c0_guest_config2(cop0, MIPS_CONF_M);
  419. /* MIPS_CONF_M | (read_c0_config2() & 0xfff) */
  420. /* Have config4, UserLocal */
  421. kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI);
  422. /* Have config5 */
  423. kvm_write_c0_guest_config4(cop0, MIPS_CONF_M);
  424. /* No config6 */
  425. kvm_write_c0_guest_config5(cop0, 0);
  426. /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */
  427. kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10));
  428. /*
  429. * Setup IntCtl defaults, compatibility mode for timer interrupts (HW5)
  430. */
  431. kvm_write_c0_guest_intctl(cop0, 0xFC000000);
  432. /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */
  433. kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 |
  434. (vcpu_id & MIPS_EBASE_CPUNUM));
  435. return 0;
  436. }
  437. static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu)
  438. {
  439. return 0;
  440. }
  441. static int kvm_trap_emul_copy_reg_indices(struct kvm_vcpu *vcpu,
  442. u64 __user *indices)
  443. {
  444. return 0;
  445. }
  446. static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu,
  447. const struct kvm_one_reg *reg,
  448. s64 *v)
  449. {
  450. switch (reg->id) {
  451. case KVM_REG_MIPS_CP0_COUNT:
  452. *v = kvm_mips_read_count(vcpu);
  453. break;
  454. case KVM_REG_MIPS_COUNT_CTL:
  455. *v = vcpu->arch.count_ctl;
  456. break;
  457. case KVM_REG_MIPS_COUNT_RESUME:
  458. *v = ktime_to_ns(vcpu->arch.count_resume);
  459. break;
  460. case KVM_REG_MIPS_COUNT_HZ:
  461. *v = vcpu->arch.count_hz;
  462. break;
  463. default:
  464. return -EINVAL;
  465. }
  466. return 0;
  467. }
  468. static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu,
  469. const struct kvm_one_reg *reg,
  470. s64 v)
  471. {
  472. struct mips_coproc *cop0 = vcpu->arch.cop0;
  473. int ret = 0;
  474. unsigned int cur, change;
  475. switch (reg->id) {
  476. case KVM_REG_MIPS_CP0_COUNT:
  477. kvm_mips_write_count(vcpu, v);
  478. break;
  479. case KVM_REG_MIPS_CP0_COMPARE:
  480. kvm_mips_write_compare(vcpu, v, false);
  481. break;
  482. case KVM_REG_MIPS_CP0_CAUSE:
  483. /*
  484. * If the timer is stopped or started (DC bit) it must look
  485. * atomic with changes to the interrupt pending bits (TI, IRQ5).
  486. * A timer interrupt should not happen in between.
  487. */
  488. if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) {
  489. if (v & CAUSEF_DC) {
  490. /* disable timer first */
  491. kvm_mips_count_disable_cause(vcpu);
  492. kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
  493. } else {
  494. /* enable timer last */
  495. kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
  496. kvm_mips_count_enable_cause(vcpu);
  497. }
  498. } else {
  499. kvm_write_c0_guest_cause(cop0, v);
  500. }
  501. break;
  502. case KVM_REG_MIPS_CP0_CONFIG:
  503. /* read-only for now */
  504. break;
  505. case KVM_REG_MIPS_CP0_CONFIG1:
  506. cur = kvm_read_c0_guest_config1(cop0);
  507. change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu);
  508. if (change) {
  509. v = cur ^ change;
  510. kvm_write_c0_guest_config1(cop0, v);
  511. }
  512. break;
  513. case KVM_REG_MIPS_CP0_CONFIG2:
  514. /* read-only for now */
  515. break;
  516. case KVM_REG_MIPS_CP0_CONFIG3:
  517. cur = kvm_read_c0_guest_config3(cop0);
  518. change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu);
  519. if (change) {
  520. v = cur ^ change;
  521. kvm_write_c0_guest_config3(cop0, v);
  522. }
  523. break;
  524. case KVM_REG_MIPS_CP0_CONFIG4:
  525. cur = kvm_read_c0_guest_config4(cop0);
  526. change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu);
  527. if (change) {
  528. v = cur ^ change;
  529. kvm_write_c0_guest_config4(cop0, v);
  530. }
  531. break;
  532. case KVM_REG_MIPS_CP0_CONFIG5:
  533. cur = kvm_read_c0_guest_config5(cop0);
  534. change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu);
  535. if (change) {
  536. v = cur ^ change;
  537. kvm_write_c0_guest_config5(cop0, v);
  538. }
  539. break;
  540. case KVM_REG_MIPS_COUNT_CTL:
  541. ret = kvm_mips_set_count_ctl(vcpu, v);
  542. break;
  543. case KVM_REG_MIPS_COUNT_RESUME:
  544. ret = kvm_mips_set_count_resume(vcpu, v);
  545. break;
  546. case KVM_REG_MIPS_COUNT_HZ:
  547. ret = kvm_mips_set_count_hz(vcpu, v);
  548. break;
  549. default:
  550. return -EINVAL;
  551. }
  552. return ret;
  553. }
  554. static int kvm_trap_emul_vcpu_get_regs(struct kvm_vcpu *vcpu)
  555. {
  556. kvm_lose_fpu(vcpu);
  557. return 0;
  558. }
  559. static int kvm_trap_emul_vcpu_set_regs(struct kvm_vcpu *vcpu)
  560. {
  561. return 0;
  562. }
  563. static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
  564. /* exit handlers */
  565. .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
  566. .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod,
  567. .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss,
  568. .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss,
  569. .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st,
  570. .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld,
  571. .handle_syscall = kvm_trap_emul_handle_syscall,
  572. .handle_res_inst = kvm_trap_emul_handle_res_inst,
  573. .handle_break = kvm_trap_emul_handle_break,
  574. .handle_trap = kvm_trap_emul_handle_trap,
  575. .handle_msa_fpe = kvm_trap_emul_handle_msa_fpe,
  576. .handle_fpe = kvm_trap_emul_handle_fpe,
  577. .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled,
  578. .vm_init = kvm_trap_emul_vm_init,
  579. .vcpu_init = kvm_trap_emul_vcpu_init,
  580. .vcpu_setup = kvm_trap_emul_vcpu_setup,
  581. .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
  582. .queue_timer_int = kvm_mips_queue_timer_int_cb,
  583. .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,
  584. .queue_io_int = kvm_mips_queue_io_int_cb,
  585. .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
  586. .irq_deliver = kvm_mips_irq_deliver_cb,
  587. .irq_clear = kvm_mips_irq_clear_cb,
  588. .num_regs = kvm_trap_emul_num_regs,
  589. .copy_reg_indices = kvm_trap_emul_copy_reg_indices,
  590. .get_one_reg = kvm_trap_emul_get_one_reg,
  591. .set_one_reg = kvm_trap_emul_set_one_reg,
  592. .vcpu_get_regs = kvm_trap_emul_vcpu_get_regs,
  593. .vcpu_set_regs = kvm_trap_emul_vcpu_set_regs,
  594. };
  595. int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
  596. {
  597. *install_callbacks = &kvm_trap_emul_callbacks;
  598. return 0;
  599. }