kvm_trap_emul.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "kvm_mips_opcode.h"
  17. #include "kvm_mips_int.h"
  18. static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva)
  19. {
  20. gpa_t gpa;
  21. uint32_t kseg = KSEGX(gva);
  22. if ((kseg == CKSEG0) || (kseg == CKSEG1))
  23. gpa = CPHYSADDR(gva);
  24. else {
  25. printk("%s: cannot find GPA for GVA: %#lx\n", __func__, gva);
  26. kvm_mips_dump_host_tlbs();
  27. gpa = KVM_INVALID_ADDR;
  28. }
  29. #ifdef DEBUG
  30. kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa);
  31. #endif
  32. return gpa;
  33. }
  34. static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu)
  35. {
  36. struct kvm_run *run = vcpu->run;
  37. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  38. unsigned long cause = vcpu->arch.host_cp0_cause;
  39. enum emulation_result er = EMULATE_DONE;
  40. int ret = RESUME_GUEST;
  41. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
  42. er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu);
  43. } else
  44. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  45. switch (er) {
  46. case EMULATE_DONE:
  47. ret = RESUME_GUEST;
  48. break;
  49. case EMULATE_FAIL:
  50. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  51. ret = RESUME_HOST;
  52. break;
  53. case EMULATE_WAIT:
  54. run->exit_reason = KVM_EXIT_INTR;
  55. ret = RESUME_HOST;
  56. break;
  57. default:
  58. BUG();
  59. }
  60. return ret;
  61. }
  62. static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu)
  63. {
  64. struct kvm_run *run = vcpu->run;
  65. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  66. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  67. unsigned long cause = vcpu->arch.host_cp0_cause;
  68. enum emulation_result er = EMULATE_DONE;
  69. int ret = RESUME_GUEST;
  70. if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
  71. || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
  72. #ifdef DEBUG
  73. kvm_debug
  74. ("USER/KSEG23 ADDR TLB MOD fault: cause %#lx, PC: %p, BadVaddr: %#lx\n",
  75. cause, opc, badvaddr);
  76. #endif
  77. er = kvm_mips_handle_tlbmod(cause, opc, run, vcpu);
  78. if (er == EMULATE_DONE)
  79. ret = RESUME_GUEST;
  80. else {
  81. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  82. ret = RESUME_HOST;
  83. }
  84. } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
  85. /* XXXKYMA: The guest kernel does not expect to get this fault when we are not
  86. * using HIGHMEM. Need to address this in a HIGHMEM kernel
  87. */
  88. printk
  89. ("TLB MOD fault not handled, cause %#lx, PC: %p, BadVaddr: %#lx\n",
  90. cause, opc, badvaddr);
  91. kvm_mips_dump_host_tlbs();
  92. kvm_arch_vcpu_dump_regs(vcpu);
  93. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  94. ret = RESUME_HOST;
  95. } else {
  96. printk
  97. ("Illegal TLB Mod fault address , cause %#lx, PC: %p, BadVaddr: %#lx\n",
  98. cause, opc, badvaddr);
  99. kvm_mips_dump_host_tlbs();
  100. kvm_arch_vcpu_dump_regs(vcpu);
  101. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  102. ret = RESUME_HOST;
  103. }
  104. return ret;
  105. }
  106. static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
  107. {
  108. struct kvm_run *run = vcpu->run;
  109. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  110. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  111. unsigned long cause = vcpu->arch.host_cp0_cause;
  112. enum emulation_result er = EMULATE_DONE;
  113. int ret = RESUME_GUEST;
  114. if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR)
  115. && KVM_GUEST_KERNEL_MODE(vcpu)) {
  116. if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) {
  117. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  118. ret = RESUME_HOST;
  119. }
  120. } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
  121. || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
  122. #ifdef DEBUG
  123. kvm_debug
  124. ("USER ADDR TLB LD fault: cause %#lx, PC: %p, BadVaddr: %#lx\n",
  125. cause, opc, badvaddr);
  126. #endif
  127. er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu);
  128. if (er == EMULATE_DONE)
  129. ret = RESUME_GUEST;
  130. else {
  131. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  132. ret = RESUME_HOST;
  133. }
  134. } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
  135. /* All KSEG0 faults are handled by KVM, as the guest kernel does not
  136. * expect to ever get them
  137. */
  138. if (kvm_mips_handle_kseg0_tlb_fault
  139. (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) {
  140. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  141. ret = RESUME_HOST;
  142. }
  143. } else {
  144. kvm_err
  145. ("Illegal TLB LD fault address , cause %#lx, PC: %p, BadVaddr: %#lx\n",
  146. cause, opc, badvaddr);
  147. kvm_mips_dump_host_tlbs();
  148. kvm_arch_vcpu_dump_regs(vcpu);
  149. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  150. ret = RESUME_HOST;
  151. }
  152. return ret;
  153. }
  154. static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
  155. {
  156. struct kvm_run *run = vcpu->run;
  157. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  158. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  159. unsigned long cause = vcpu->arch.host_cp0_cause;
  160. enum emulation_result er = EMULATE_DONE;
  161. int ret = RESUME_GUEST;
  162. if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR)
  163. && KVM_GUEST_KERNEL_MODE(vcpu)) {
  164. if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) {
  165. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  166. ret = RESUME_HOST;
  167. }
  168. } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
  169. || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
  170. #ifdef DEBUG
  171. kvm_debug("USER ADDR TLB ST fault: PC: %#lx, BadVaddr: %#lx\n",
  172. vcpu->arch.pc, badvaddr);
  173. #endif
  174. /* User Address (UA) fault, this could happen if
  175. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  176. * case we pass on the fault to the guest kernel and let it handle it.
  177. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  178. * case we inject the TLB from the Guest TLB into the shadow host TLB
  179. */
  180. er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu);
  181. if (er == EMULATE_DONE)
  182. ret = RESUME_GUEST;
  183. else {
  184. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  185. ret = RESUME_HOST;
  186. }
  187. } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
  188. if (kvm_mips_handle_kseg0_tlb_fault
  189. (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) {
  190. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  191. ret = RESUME_HOST;
  192. }
  193. } else {
  194. printk
  195. ("Illegal TLB ST fault address , cause %#lx, PC: %p, BadVaddr: %#lx\n",
  196. cause, opc, badvaddr);
  197. kvm_mips_dump_host_tlbs();
  198. kvm_arch_vcpu_dump_regs(vcpu);
  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_st(struct kvm_vcpu *vcpu)
  205. {
  206. struct kvm_run *run = vcpu->run;
  207. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  208. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  209. unsigned long cause = vcpu->arch.host_cp0_cause;
  210. enum emulation_result er = EMULATE_DONE;
  211. int ret = RESUME_GUEST;
  212. if (KVM_GUEST_KERNEL_MODE(vcpu)
  213. && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
  214. #ifdef DEBUG
  215. kvm_debug("Emulate Store to MMIO space\n");
  216. #endif
  217. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  218. if (er == EMULATE_FAIL) {
  219. printk("Emulate Store to MMIO space failed\n");
  220. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  221. ret = RESUME_HOST;
  222. } else {
  223. run->exit_reason = KVM_EXIT_MMIO;
  224. ret = RESUME_HOST;
  225. }
  226. } else {
  227. printk
  228. ("Address Error (STORE): cause %#lx, PC: %p, BadVaddr: %#lx\n",
  229. cause, opc, badvaddr);
  230. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  231. ret = RESUME_HOST;
  232. }
  233. return ret;
  234. }
  235. static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu)
  236. {
  237. struct kvm_run *run = vcpu->run;
  238. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  239. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  240. unsigned long cause = vcpu->arch.host_cp0_cause;
  241. enum emulation_result er = EMULATE_DONE;
  242. int ret = RESUME_GUEST;
  243. if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) {
  244. #ifdef DEBUG
  245. kvm_debug("Emulate Load from MMIO space @ %#lx\n", badvaddr);
  246. #endif
  247. er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
  248. if (er == EMULATE_FAIL) {
  249. printk("Emulate Load from MMIO space failed\n");
  250. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  251. ret = RESUME_HOST;
  252. } else {
  253. run->exit_reason = KVM_EXIT_MMIO;
  254. ret = RESUME_HOST;
  255. }
  256. } else {
  257. printk
  258. ("Address Error (LOAD): cause %#lx, PC: %p, BadVaddr: %#lx\n",
  259. cause, opc, badvaddr);
  260. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  261. ret = RESUME_HOST;
  262. er = EMULATE_FAIL;
  263. }
  264. return ret;
  265. }
  266. static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu)
  267. {
  268. struct kvm_run *run = vcpu->run;
  269. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  270. unsigned long cause = vcpu->arch.host_cp0_cause;
  271. enum emulation_result er = EMULATE_DONE;
  272. int ret = RESUME_GUEST;
  273. er = kvm_mips_emulate_syscall(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_res_inst(struct kvm_vcpu *vcpu)
  283. {
  284. struct kvm_run *run = vcpu->run;
  285. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  286. unsigned long cause = vcpu->arch.host_cp0_cause;
  287. enum emulation_result er = EMULATE_DONE;
  288. int ret = RESUME_GUEST;
  289. er = kvm_mips_handle_ri(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_break(struct kvm_vcpu *vcpu)
  299. {
  300. struct kvm_run *run = vcpu->run;
  301. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  302. unsigned long cause = vcpu->arch.host_cp0_cause;
  303. enum emulation_result er = EMULATE_DONE;
  304. int ret = RESUME_GUEST;
  305. er = kvm_mips_emulate_bp_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_vm_init(struct kvm *kvm)
  315. {
  316. return 0;
  317. }
  318. static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu)
  319. {
  320. return 0;
  321. }
  322. static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
  323. {
  324. struct mips_coproc *cop0 = vcpu->arch.cop0;
  325. uint32_t config1;
  326. int vcpu_id = vcpu->vcpu_id;
  327. /* Arch specific stuff, set up config registers properly so that the
  328. * guest will come up as expected, for now we simulate a
  329. * MIPS 24kc
  330. */
  331. kvm_write_c0_guest_prid(cop0, 0x00019300);
  332. kvm_write_c0_guest_config(cop0,
  333. MIPS_CONFIG0 | (0x1 << CP0C0_AR) |
  334. (MMU_TYPE_R4000 << CP0C0_MT));
  335. /* Read the cache characteristics from the host Config1 Register */
  336. config1 = (read_c0_config1() & ~0x7f);
  337. /* Set up MMU size */
  338. config1 &= ~(0x3f << 25);
  339. config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25);
  340. /* We unset some bits that we aren't emulating */
  341. config1 &=
  342. ~((1 << CP0C1_C2) | (1 << CP0C1_MD) | (1 << CP0C1_PC) |
  343. (1 << CP0C1_WR) | (1 << CP0C1_CA));
  344. kvm_write_c0_guest_config1(cop0, config1);
  345. kvm_write_c0_guest_config2(cop0, MIPS_CONFIG2);
  346. /* MIPS_CONFIG2 | (read_c0_config2() & 0xfff) */
  347. kvm_write_c0_guest_config3(cop0,
  348. MIPS_CONFIG3 | (0 << CP0C3_VInt) | (1 <<
  349. CP0C3_ULRI));
  350. /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */
  351. kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10));
  352. /* Setup IntCtl defaults, compatibilty mode for timer interrupts (HW5) */
  353. kvm_write_c0_guest_intctl(cop0, 0xFC000000);
  354. /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */
  355. kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 | (vcpu_id & 0xFF));
  356. return 0;
  357. }
  358. static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
  359. /* exit handlers */
  360. .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
  361. .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod,
  362. .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss,
  363. .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss,
  364. .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st,
  365. .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld,
  366. .handle_syscall = kvm_trap_emul_handle_syscall,
  367. .handle_res_inst = kvm_trap_emul_handle_res_inst,
  368. .handle_break = kvm_trap_emul_handle_break,
  369. .vm_init = kvm_trap_emul_vm_init,
  370. .vcpu_init = kvm_trap_emul_vcpu_init,
  371. .vcpu_setup = kvm_trap_emul_vcpu_setup,
  372. .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
  373. .queue_timer_int = kvm_mips_queue_timer_int_cb,
  374. .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,
  375. .queue_io_int = kvm_mips_queue_io_int_cb,
  376. .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
  377. .irq_deliver = kvm_mips_irq_deliver_cb,
  378. .irq_clear = kvm_mips_irq_clear_cb,
  379. };
  380. int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
  381. {
  382. *install_callbacks = &kvm_trap_emul_callbacks;
  383. return 0;
  384. }