kvm_mips.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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: MIPS specific KVM APIs
  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/fs.h>
  16. #include <linux/bootmem.h>
  17. #include <asm/page.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/mmu_context.h>
  20. #include <linux/kvm_host.h>
  21. #include "kvm_mips_int.h"
  22. #include "kvm_mips_comm.h"
  23. #define CREATE_TRACE_POINTS
  24. #include "trace.h"
  25. #ifndef VECTORSPACING
  26. #define VECTORSPACING 0x100 /* for EI/VI mode */
  27. #endif
  28. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  29. struct kvm_stats_debugfs_item debugfs_entries[] = {
  30. { "wait", VCPU_STAT(wait_exits) },
  31. { "cache", VCPU_STAT(cache_exits) },
  32. { "signal", VCPU_STAT(signal_exits) },
  33. { "interrupt", VCPU_STAT(int_exits) },
  34. { "cop_unsuable", VCPU_STAT(cop_unusable_exits) },
  35. { "tlbmod", VCPU_STAT(tlbmod_exits) },
  36. { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits) },
  37. { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits) },
  38. { "addrerr_st", VCPU_STAT(addrerr_st_exits) },
  39. { "addrerr_ld", VCPU_STAT(addrerr_ld_exits) },
  40. { "syscall", VCPU_STAT(syscall_exits) },
  41. { "resvd_inst", VCPU_STAT(resvd_inst_exits) },
  42. { "break_inst", VCPU_STAT(break_inst_exits) },
  43. { "flush_dcache", VCPU_STAT(flush_dcache_exits) },
  44. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  45. {NULL}
  46. };
  47. static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
  48. {
  49. int i;
  50. for_each_possible_cpu(i) {
  51. vcpu->arch.guest_kernel_asid[i] = 0;
  52. vcpu->arch.guest_user_asid[i] = 0;
  53. }
  54. return 0;
  55. }
  56. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  57. {
  58. return gfn;
  59. }
  60. /* XXXKYMA: We are simulatoring a processor that has the WII bit set in Config7, so we
  61. * are "runnable" if interrupts are pending
  62. */
  63. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  64. {
  65. return !!(vcpu->arch.pending_exceptions);
  66. }
  67. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  68. {
  69. return 1;
  70. }
  71. int kvm_arch_hardware_enable(void *garbage)
  72. {
  73. return 0;
  74. }
  75. void kvm_arch_hardware_disable(void *garbage)
  76. {
  77. }
  78. int kvm_arch_hardware_setup(void)
  79. {
  80. return 0;
  81. }
  82. void kvm_arch_hardware_unsetup(void)
  83. {
  84. }
  85. void kvm_arch_check_processor_compat(void *rtn)
  86. {
  87. int *r = (int *)rtn;
  88. *r = 0;
  89. return;
  90. }
  91. static void kvm_mips_init_tlbs(struct kvm *kvm)
  92. {
  93. unsigned long wired;
  94. /* Add a wired entry to the TLB, it is used to map the commpage to the Guest kernel */
  95. wired = read_c0_wired();
  96. write_c0_wired(wired + 1);
  97. mtc0_tlbw_hazard();
  98. kvm->arch.commpage_tlb = wired;
  99. kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
  100. kvm->arch.commpage_tlb);
  101. }
  102. static void kvm_mips_init_vm_percpu(void *arg)
  103. {
  104. struct kvm *kvm = (struct kvm *)arg;
  105. kvm_mips_init_tlbs(kvm);
  106. kvm_mips_callbacks->vm_init(kvm);
  107. }
  108. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  109. {
  110. if (atomic_inc_return(&kvm_mips_instance) == 1) {
  111. kvm_info("%s: 1st KVM instance, setup host TLB parameters\n",
  112. __func__);
  113. on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
  114. }
  115. return 0;
  116. }
  117. void kvm_mips_free_vcpus(struct kvm *kvm)
  118. {
  119. unsigned int i;
  120. struct kvm_vcpu *vcpu;
  121. /* Put the pages we reserved for the guest pmap */
  122. for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
  123. if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
  124. kvm_mips_release_pfn_clean(kvm->arch.guest_pmap[i]);
  125. }
  126. if (kvm->arch.guest_pmap)
  127. kfree(kvm->arch.guest_pmap);
  128. kvm_for_each_vcpu(i, vcpu, kvm) {
  129. kvm_arch_vcpu_free(vcpu);
  130. }
  131. mutex_lock(&kvm->lock);
  132. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  133. kvm->vcpus[i] = NULL;
  134. atomic_set(&kvm->online_vcpus, 0);
  135. mutex_unlock(&kvm->lock);
  136. }
  137. void kvm_arch_sync_events(struct kvm *kvm)
  138. {
  139. }
  140. static void kvm_mips_uninit_tlbs(void *arg)
  141. {
  142. /* Restore wired count */
  143. write_c0_wired(0);
  144. mtc0_tlbw_hazard();
  145. /* Clear out all the TLBs */
  146. kvm_local_flush_tlb_all();
  147. }
  148. void kvm_arch_destroy_vm(struct kvm *kvm)
  149. {
  150. kvm_mips_free_vcpus(kvm);
  151. /* If this is the last instance, restore wired count */
  152. if (atomic_dec_return(&kvm_mips_instance) == 0) {
  153. kvm_info("%s: last KVM instance, restoring TLB parameters\n",
  154. __func__);
  155. on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
  156. }
  157. }
  158. long
  159. kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  160. {
  161. return -EINVAL;
  162. }
  163. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  164. struct kvm_memory_slot *dont)
  165. {
  166. }
  167. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  168. {
  169. return 0;
  170. }
  171. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  172. struct kvm_memory_slot *memslot,
  173. struct kvm_memory_slot old,
  174. struct kvm_userspace_memory_region *mem,
  175. bool user_alloc)
  176. {
  177. return 0;
  178. }
  179. void kvm_arch_commit_memory_region(struct kvm *kvm,
  180. struct kvm_userspace_memory_region *mem,
  181. struct kvm_memory_slot old, bool user_alloc)
  182. {
  183. unsigned long npages = 0;
  184. int i, err = 0;
  185. kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
  186. __func__, kvm, mem->slot, mem->guest_phys_addr,
  187. mem->memory_size, mem->userspace_addr);
  188. /* Setup Guest PMAP table */
  189. if (!kvm->arch.guest_pmap) {
  190. if (mem->slot == 0)
  191. npages = mem->memory_size >> PAGE_SHIFT;
  192. if (npages) {
  193. kvm->arch.guest_pmap_npages = npages;
  194. kvm->arch.guest_pmap =
  195. kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
  196. if (!kvm->arch.guest_pmap) {
  197. kvm_err("Failed to allocate guest PMAP");
  198. err = -ENOMEM;
  199. goto out;
  200. }
  201. kvm_info
  202. ("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
  203. npages, kvm->arch.guest_pmap);
  204. /* Now setup the page table */
  205. for (i = 0; i < npages; i++) {
  206. kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
  207. }
  208. }
  209. }
  210. out:
  211. return;
  212. }
  213. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  214. {
  215. }
  216. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  217. struct kvm_memory_slot *slot)
  218. {
  219. }
  220. void kvm_arch_flush_shadow(struct kvm *kvm)
  221. {
  222. }
  223. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  224. {
  225. extern char mips32_exception[], mips32_exceptionEnd[];
  226. extern char mips32_GuestException[], mips32_GuestExceptionEnd[];
  227. int err, size, offset;
  228. void *gebase;
  229. int i;
  230. struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
  231. if (!vcpu) {
  232. err = -ENOMEM;
  233. goto out;
  234. }
  235. err = kvm_vcpu_init(vcpu, kvm, id);
  236. if (err)
  237. goto out_free_cpu;
  238. kvm_info("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
  239. /* Allocate space for host mode exception handlers that handle
  240. * guest mode exits
  241. */
  242. if (cpu_has_veic || cpu_has_vint) {
  243. size = 0x200 + VECTORSPACING * 64;
  244. } else {
  245. size = 0x200;
  246. }
  247. /* Save Linux EBASE */
  248. vcpu->arch.host_ebase = (void *)read_c0_ebase();
  249. gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
  250. if (!gebase) {
  251. err = -ENOMEM;
  252. goto out_free_cpu;
  253. }
  254. kvm_info("Allocated %d bytes for KVM Exception Handlers @ %p\n",
  255. ALIGN(size, PAGE_SIZE), gebase);
  256. /* Save new ebase */
  257. vcpu->arch.guest_ebase = gebase;
  258. /* Copy L1 Guest Exception handler to correct offset */
  259. /* TLB Refill, EXL = 0 */
  260. memcpy(gebase, mips32_exception,
  261. mips32_exceptionEnd - mips32_exception);
  262. /* General Exception Entry point */
  263. memcpy(gebase + 0x180, mips32_exception,
  264. mips32_exceptionEnd - mips32_exception);
  265. /* For vectored interrupts poke the exception code @ all offsets 0-7 */
  266. for (i = 0; i < 8; i++) {
  267. kvm_debug("L1 Vectored handler @ %p\n",
  268. gebase + 0x200 + (i * VECTORSPACING));
  269. memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
  270. mips32_exceptionEnd - mips32_exception);
  271. }
  272. /* General handler, relocate to unmapped space for sanity's sake */
  273. offset = 0x2000;
  274. kvm_info("Installing KVM Exception handlers @ %p, %#x bytes\n",
  275. gebase + offset,
  276. mips32_GuestExceptionEnd - mips32_GuestException);
  277. memcpy(gebase + offset, mips32_GuestException,
  278. mips32_GuestExceptionEnd - mips32_GuestException);
  279. /* Invalidate the icache for these ranges */
  280. mips32_SyncICache((unsigned long) gebase, ALIGN(size, PAGE_SIZE));
  281. /* Allocate comm page for guest kernel, a TLB will be reserved for mapping GVA @ 0xFFFF8000 to this page */
  282. vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
  283. if (!vcpu->arch.kseg0_commpage) {
  284. err = -ENOMEM;
  285. goto out_free_gebase;
  286. }
  287. kvm_info("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
  288. kvm_mips_commpage_init(vcpu);
  289. /* Init */
  290. vcpu->arch.last_sched_cpu = -1;
  291. /* Start off the timer */
  292. kvm_mips_emulate_count(vcpu);
  293. return vcpu;
  294. out_free_gebase:
  295. kfree(gebase);
  296. out_free_cpu:
  297. kfree(vcpu);
  298. out:
  299. return ERR_PTR(err);
  300. }
  301. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  302. {
  303. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  304. kvm_vcpu_uninit(vcpu);
  305. kvm_mips_dump_stats(vcpu);
  306. if (vcpu->arch.guest_ebase)
  307. kfree(vcpu->arch.guest_ebase);
  308. if (vcpu->arch.kseg0_commpage)
  309. kfree(vcpu->arch.kseg0_commpage);
  310. }
  311. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  312. {
  313. kvm_arch_vcpu_free(vcpu);
  314. }
  315. int
  316. kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  317. struct kvm_guest_debug *dbg)
  318. {
  319. return -EINVAL;
  320. }
  321. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  322. {
  323. int r = 0;
  324. sigset_t sigsaved;
  325. if (vcpu->sigset_active)
  326. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  327. if (vcpu->mmio_needed) {
  328. if (!vcpu->mmio_is_write)
  329. kvm_mips_complete_mmio_load(vcpu, run);
  330. vcpu->mmio_needed = 0;
  331. }
  332. /* Check if we have any exceptions/interrupts pending */
  333. kvm_mips_deliver_interrupts(vcpu,
  334. kvm_read_c0_guest_cause(vcpu->arch.cop0));
  335. local_irq_disable();
  336. kvm_guest_enter();
  337. r = __kvm_mips_vcpu_run(run, vcpu);
  338. kvm_guest_exit();
  339. local_irq_enable();
  340. if (vcpu->sigset_active)
  341. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  342. return r;
  343. }
  344. int
  345. kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_mips_interrupt *irq)
  346. {
  347. int intr = (int)irq->irq;
  348. struct kvm_vcpu *dvcpu = NULL;
  349. if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
  350. kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
  351. (int)intr);
  352. if (irq->cpu == -1)
  353. dvcpu = vcpu;
  354. else
  355. dvcpu = vcpu->kvm->vcpus[irq->cpu];
  356. if (intr == 2 || intr == 3 || intr == 4) {
  357. kvm_mips_callbacks->queue_io_int(dvcpu, irq);
  358. } else if (intr == -2 || intr == -3 || intr == -4) {
  359. kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
  360. } else {
  361. kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
  362. irq->cpu, irq->irq);
  363. return -EINVAL;
  364. }
  365. dvcpu->arch.wait = 0;
  366. if (waitqueue_active(&dvcpu->wq)) {
  367. wake_up_interruptible(&dvcpu->wq);
  368. }
  369. return 0;
  370. }
  371. int
  372. kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  373. struct kvm_mp_state *mp_state)
  374. {
  375. return -EINVAL;
  376. }
  377. int
  378. kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  379. struct kvm_mp_state *mp_state)
  380. {
  381. return -EINVAL;
  382. }
  383. long
  384. kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  385. {
  386. struct kvm_vcpu *vcpu = filp->private_data;
  387. void __user *argp = (void __user *)arg;
  388. long r;
  389. int intr;
  390. switch (ioctl) {
  391. case KVM_NMI:
  392. /* Treat the NMI as a CPU reset */
  393. r = kvm_mips_reset_vcpu(vcpu);
  394. break;
  395. case KVM_INTERRUPT:
  396. {
  397. struct kvm_mips_interrupt irq;
  398. r = -EFAULT;
  399. if (copy_from_user(&irq, argp, sizeof(irq)))
  400. goto out;
  401. intr = (int)irq.irq;
  402. kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
  403. irq.irq);
  404. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  405. break;
  406. }
  407. default:
  408. r = -EINVAL;
  409. }
  410. out:
  411. return r;
  412. }
  413. /*
  414. * Get (and clear) the dirty memory log for a memory slot.
  415. */
  416. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  417. {
  418. struct kvm_memory_slot *memslot;
  419. unsigned long ga, ga_end;
  420. int is_dirty = 0;
  421. int r;
  422. unsigned long n;
  423. mutex_lock(&kvm->slots_lock);
  424. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  425. if (r)
  426. goto out;
  427. /* If nothing is dirty, don't bother messing with page tables. */
  428. if (is_dirty) {
  429. memslot = &kvm->memslots->memslots[log->slot];
  430. ga = memslot->base_gfn << PAGE_SHIFT;
  431. ga_end = ga + (memslot->npages << PAGE_SHIFT);
  432. printk("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
  433. ga_end);
  434. n = kvm_dirty_bitmap_bytes(memslot);
  435. memset(memslot->dirty_bitmap, 0, n);
  436. }
  437. r = 0;
  438. out:
  439. mutex_unlock(&kvm->slots_lock);
  440. return r;
  441. }
  442. long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  443. {
  444. long r;
  445. switch (ioctl) {
  446. default:
  447. r = -EINVAL;
  448. }
  449. return r;
  450. }
  451. int kvm_arch_init(void *opaque)
  452. {
  453. int ret;
  454. if (kvm_mips_callbacks) {
  455. kvm_err("kvm: module already exists\n");
  456. return -EEXIST;
  457. }
  458. ret = kvm_mips_emulation_init(&kvm_mips_callbacks);
  459. return ret;
  460. }
  461. void kvm_arch_exit(void)
  462. {
  463. kvm_mips_callbacks = NULL;
  464. }
  465. int
  466. kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  467. {
  468. return -ENOTSUPP;
  469. }
  470. int
  471. kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  472. {
  473. return -ENOTSUPP;
  474. }
  475. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  476. {
  477. return 0;
  478. }
  479. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  480. {
  481. return -ENOTSUPP;
  482. }
  483. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  484. {
  485. return -ENOTSUPP;
  486. }
  487. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  488. {
  489. return VM_FAULT_SIGBUS;
  490. }
  491. int kvm_dev_ioctl_check_extension(long ext)
  492. {
  493. int r;
  494. switch (ext) {
  495. case KVM_CAP_COALESCED_MMIO:
  496. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  497. break;
  498. default:
  499. r = 0;
  500. break;
  501. }
  502. return r;
  503. }
  504. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  505. {
  506. return kvm_mips_pending_timer(vcpu);
  507. }
  508. int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
  509. {
  510. int i;
  511. struct mips_coproc *cop0;
  512. if (!vcpu)
  513. return -1;
  514. printk("VCPU Register Dump:\n");
  515. printk("\tpc = 0x%08lx\n", vcpu->arch.pc);;
  516. printk("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
  517. for (i = 0; i < 32; i += 4) {
  518. printk("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
  519. vcpu->arch.gprs[i],
  520. vcpu->arch.gprs[i + 1],
  521. vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
  522. }
  523. printk("\thi: 0x%08lx\n", vcpu->arch.hi);
  524. printk("\tlo: 0x%08lx\n", vcpu->arch.lo);
  525. cop0 = vcpu->arch.cop0;
  526. printk("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
  527. kvm_read_c0_guest_status(cop0), kvm_read_c0_guest_cause(cop0));
  528. printk("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
  529. return 0;
  530. }
  531. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  532. {
  533. int i;
  534. for (i = 0; i < 32; i++)
  535. vcpu->arch.gprs[i] = regs->gprs[i];
  536. vcpu->arch.hi = regs->hi;
  537. vcpu->arch.lo = regs->lo;
  538. vcpu->arch.pc = regs->pc;
  539. return kvm_mips_callbacks->vcpu_ioctl_set_regs(vcpu, regs);
  540. }
  541. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  542. {
  543. int i;
  544. for (i = 0; i < 32; i++)
  545. regs->gprs[i] = vcpu->arch.gprs[i];
  546. regs->hi = vcpu->arch.hi;
  547. regs->lo = vcpu->arch.lo;
  548. regs->pc = vcpu->arch.pc;
  549. return kvm_mips_callbacks->vcpu_ioctl_get_regs(vcpu, regs);
  550. }
  551. void kvm_mips_comparecount_func(unsigned long data)
  552. {
  553. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  554. kvm_mips_callbacks->queue_timer_int(vcpu);
  555. vcpu->arch.wait = 0;
  556. if (waitqueue_active(&vcpu->wq)) {
  557. wake_up_interruptible(&vcpu->wq);
  558. }
  559. }
  560. /*
  561. * low level hrtimer wake routine.
  562. */
  563. enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
  564. {
  565. struct kvm_vcpu *vcpu;
  566. vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
  567. kvm_mips_comparecount_func((unsigned long) vcpu);
  568. hrtimer_forward_now(&vcpu->arch.comparecount_timer,
  569. ktime_set(0, MS_TO_NS(10)));
  570. return HRTIMER_RESTART;
  571. }
  572. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  573. {
  574. kvm_mips_callbacks->vcpu_init(vcpu);
  575. hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
  576. HRTIMER_MODE_REL);
  577. vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
  578. kvm_mips_init_shadow_tlb(vcpu);
  579. return 0;
  580. }
  581. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  582. {
  583. return;
  584. }
  585. int
  586. kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, struct kvm_translation *tr)
  587. {
  588. return 0;
  589. }
  590. /* Initial guest state */
  591. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  592. {
  593. return kvm_mips_callbacks->vcpu_setup(vcpu);
  594. }
  595. static
  596. void kvm_mips_set_c0_status(void)
  597. {
  598. uint32_t status = read_c0_status();
  599. if (cpu_has_fpu)
  600. status |= (ST0_CU1);
  601. if (cpu_has_dsp)
  602. status |= (ST0_MX);
  603. write_c0_status(status);
  604. ehb();
  605. }
  606. /*
  607. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  608. */
  609. int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
  610. {
  611. uint32_t cause = vcpu->arch.host_cp0_cause;
  612. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  613. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  614. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  615. enum emulation_result er = EMULATE_DONE;
  616. int ret = RESUME_GUEST;
  617. /* Set a default exit reason */
  618. run->exit_reason = KVM_EXIT_UNKNOWN;
  619. run->ready_for_interrupt_injection = 1;
  620. /* Set the appropriate status bits based on host CPU features, before we hit the scheduler */
  621. kvm_mips_set_c0_status();
  622. local_irq_enable();
  623. kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
  624. cause, opc, run, vcpu);
  625. /* Do a privilege check, if in UM most of these exit conditions end up
  626. * causing an exception to be delivered to the Guest Kernel
  627. */
  628. er = kvm_mips_check_privilege(cause, opc, run, vcpu);
  629. if (er == EMULATE_PRIV_FAIL) {
  630. goto skip_emul;
  631. } else if (er == EMULATE_FAIL) {
  632. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  633. ret = RESUME_HOST;
  634. goto skip_emul;
  635. }
  636. switch (exccode) {
  637. case T_INT:
  638. kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
  639. ++vcpu->stat.int_exits;
  640. trace_kvm_exit(vcpu, INT_EXITS);
  641. if (need_resched()) {
  642. cond_resched();
  643. }
  644. ret = RESUME_GUEST;
  645. break;
  646. case T_COP_UNUSABLE:
  647. kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
  648. ++vcpu->stat.cop_unusable_exits;
  649. trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
  650. ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
  651. /* XXXKYMA: Might need to return to user space */
  652. if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN) {
  653. ret = RESUME_HOST;
  654. }
  655. break;
  656. case T_TLB_MOD:
  657. ++vcpu->stat.tlbmod_exits;
  658. trace_kvm_exit(vcpu, TLBMOD_EXITS);
  659. ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
  660. break;
  661. case T_TLB_ST_MISS:
  662. kvm_debug
  663. ("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
  664. cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
  665. badvaddr);
  666. ++vcpu->stat.tlbmiss_st_exits;
  667. trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
  668. ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
  669. break;
  670. case T_TLB_LD_MISS:
  671. kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
  672. cause, opc, badvaddr);
  673. ++vcpu->stat.tlbmiss_ld_exits;
  674. trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
  675. ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
  676. break;
  677. case T_ADDR_ERR_ST:
  678. ++vcpu->stat.addrerr_st_exits;
  679. trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
  680. ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
  681. break;
  682. case T_ADDR_ERR_LD:
  683. ++vcpu->stat.addrerr_ld_exits;
  684. trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
  685. ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
  686. break;
  687. case T_SYSCALL:
  688. ++vcpu->stat.syscall_exits;
  689. trace_kvm_exit(vcpu, SYSCALL_EXITS);
  690. ret = kvm_mips_callbacks->handle_syscall(vcpu);
  691. break;
  692. case T_RES_INST:
  693. ++vcpu->stat.resvd_inst_exits;
  694. trace_kvm_exit(vcpu, RESVD_INST_EXITS);
  695. ret = kvm_mips_callbacks->handle_res_inst(vcpu);
  696. break;
  697. case T_BREAK:
  698. ++vcpu->stat.break_inst_exits;
  699. trace_kvm_exit(vcpu, BREAK_INST_EXITS);
  700. ret = kvm_mips_callbacks->handle_break(vcpu);
  701. break;
  702. default:
  703. kvm_err
  704. ("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
  705. exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
  706. kvm_read_c0_guest_status(vcpu->arch.cop0));
  707. kvm_arch_vcpu_dump_regs(vcpu);
  708. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  709. ret = RESUME_HOST;
  710. break;
  711. }
  712. skip_emul:
  713. local_irq_disable();
  714. if (er == EMULATE_DONE && !(ret & RESUME_HOST))
  715. kvm_mips_deliver_interrupts(vcpu, cause);
  716. if (!(ret & RESUME_HOST)) {
  717. /* Only check for signals if not already exiting to userspace */
  718. if (signal_pending(current)) {
  719. run->exit_reason = KVM_EXIT_INTR;
  720. ret = (-EINTR << 2) | RESUME_HOST;
  721. ++vcpu->stat.signal_exits;
  722. trace_kvm_exit(vcpu, SIGNAL_EXITS);
  723. }
  724. }
  725. return ret;
  726. }
  727. int __init kvm_mips_init(void)
  728. {
  729. int ret;
  730. ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  731. if (ret)
  732. return ret;
  733. /* On MIPS, kernel modules are executed from "mapped space", which requires TLBs.
  734. * The TLB handling code is statically linked with the rest of the kernel (kvm_tlb.c)
  735. * to avoid the possibility of double faulting. The issue is that the TLB code
  736. * references routines that are part of the the KVM module,
  737. * which are only available once the module is loaded.
  738. */
  739. kvm_mips_gfn_to_pfn = gfn_to_pfn;
  740. kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
  741. kvm_mips_is_error_pfn = is_error_pfn;
  742. pr_info("KVM/MIPS Initialized\n");
  743. return 0;
  744. }
  745. void __exit kvm_mips_exit(void)
  746. {
  747. kvm_exit();
  748. kvm_mips_gfn_to_pfn = NULL;
  749. kvm_mips_release_pfn_clean = NULL;
  750. kvm_mips_is_error_pfn = NULL;
  751. pr_info("KVM/MIPS unloaded\n");
  752. }
  753. module_init(kvm_mips_init);
  754. module_exit(kvm_mips_exit);
  755. EXPORT_TRACEPOINT_SYMBOL(kvm_exit);