mips.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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/kdebug.h>
  14. #include <linux/module.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/fs.h>
  17. #include <linux/bootmem.h>
  18. #include <asm/fpu.h>
  19. #include <asm/page.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/pgtable.h>
  23. #include <linux/kvm_host.h>
  24. #include "interrupt.h"
  25. #include "commpage.h"
  26. #define CREATE_TRACE_POINTS
  27. #include "trace.h"
  28. #ifndef VECTORSPACING
  29. #define VECTORSPACING 0x100 /* for EI/VI mode */
  30. #endif
  31. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
  32. struct kvm_stats_debugfs_item debugfs_entries[] = {
  33. { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
  34. { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
  35. { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
  36. { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
  37. { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
  38. { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
  39. { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
  40. { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
  41. { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
  42. { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
  43. { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
  44. { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
  45. { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
  46. { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
  47. { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
  48. { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
  49. { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
  50. { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
  51. { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
  52. { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
  53. {NULL}
  54. };
  55. static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
  56. {
  57. int i;
  58. for_each_possible_cpu(i) {
  59. vcpu->arch.guest_kernel_asid[i] = 0;
  60. vcpu->arch.guest_user_asid[i] = 0;
  61. }
  62. return 0;
  63. }
  64. /*
  65. * XXXKYMA: We are simulatoring a processor that has the WII bit set in
  66. * Config7, so we are "runnable" if interrupts are pending
  67. */
  68. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  69. {
  70. return !!(vcpu->arch.pending_exceptions);
  71. }
  72. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  73. {
  74. return 1;
  75. }
  76. int kvm_arch_hardware_enable(void)
  77. {
  78. return 0;
  79. }
  80. int kvm_arch_hardware_setup(void)
  81. {
  82. return 0;
  83. }
  84. void kvm_arch_check_processor_compat(void *rtn)
  85. {
  86. *(int *)rtn = 0;
  87. }
  88. static void kvm_mips_init_tlbs(struct kvm *kvm)
  89. {
  90. unsigned long wired;
  91. /*
  92. * Add a wired entry to the TLB, it is used to map the commpage to
  93. * the Guest kernel
  94. */
  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_debug("%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. kfree(kvm->arch.guest_pmap);
  127. kvm_for_each_vcpu(i, vcpu, kvm) {
  128. kvm_arch_vcpu_free(vcpu);
  129. }
  130. mutex_lock(&kvm->lock);
  131. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  132. kvm->vcpus[i] = NULL;
  133. atomic_set(&kvm->online_vcpus, 0);
  134. mutex_unlock(&kvm->lock);
  135. }
  136. static void kvm_mips_uninit_tlbs(void *arg)
  137. {
  138. /* Restore wired count */
  139. write_c0_wired(0);
  140. mtc0_tlbw_hazard();
  141. /* Clear out all the TLBs */
  142. kvm_local_flush_tlb_all();
  143. }
  144. void kvm_arch_destroy_vm(struct kvm *kvm)
  145. {
  146. kvm_mips_free_vcpus(kvm);
  147. /* If this is the last instance, restore wired count */
  148. if (atomic_dec_return(&kvm_mips_instance) == 0) {
  149. kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
  150. __func__);
  151. on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
  152. }
  153. }
  154. long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
  155. unsigned long arg)
  156. {
  157. return -ENOIOCTLCMD;
  158. }
  159. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  160. unsigned long npages)
  161. {
  162. return 0;
  163. }
  164. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  165. struct kvm_memory_slot *memslot,
  166. const struct kvm_userspace_memory_region *mem,
  167. enum kvm_mr_change change)
  168. {
  169. return 0;
  170. }
  171. void kvm_arch_commit_memory_region(struct kvm *kvm,
  172. const struct kvm_userspace_memory_region *mem,
  173. const struct kvm_memory_slot *old,
  174. const struct kvm_memory_slot *new,
  175. enum kvm_mr_change change)
  176. {
  177. unsigned long npages = 0;
  178. int i;
  179. kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
  180. __func__, kvm, mem->slot, mem->guest_phys_addr,
  181. mem->memory_size, mem->userspace_addr);
  182. /* Setup Guest PMAP table */
  183. if (!kvm->arch.guest_pmap) {
  184. if (mem->slot == 0)
  185. npages = mem->memory_size >> PAGE_SHIFT;
  186. if (npages) {
  187. kvm->arch.guest_pmap_npages = npages;
  188. kvm->arch.guest_pmap =
  189. kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
  190. if (!kvm->arch.guest_pmap) {
  191. kvm_err("Failed to allocate guest PMAP");
  192. return;
  193. }
  194. kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
  195. npages, kvm->arch.guest_pmap);
  196. /* Now setup the page table */
  197. for (i = 0; i < npages; i++)
  198. kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
  199. }
  200. }
  201. }
  202. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  203. {
  204. int err, size, offset;
  205. void *gebase;
  206. int i;
  207. struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
  208. if (!vcpu) {
  209. err = -ENOMEM;
  210. goto out;
  211. }
  212. err = kvm_vcpu_init(vcpu, kvm, id);
  213. if (err)
  214. goto out_free_cpu;
  215. kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
  216. /*
  217. * Allocate space for host mode exception handlers that handle
  218. * guest mode exits
  219. */
  220. if (cpu_has_veic || cpu_has_vint)
  221. size = 0x200 + VECTORSPACING * 64;
  222. else
  223. size = 0x4000;
  224. /* Save Linux EBASE */
  225. vcpu->arch.host_ebase = (void *)read_c0_ebase();
  226. gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
  227. if (!gebase) {
  228. err = -ENOMEM;
  229. goto out_free_cpu;
  230. }
  231. kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
  232. ALIGN(size, PAGE_SIZE), gebase);
  233. /* Save new ebase */
  234. vcpu->arch.guest_ebase = gebase;
  235. /* Copy L1 Guest Exception handler to correct offset */
  236. /* TLB Refill, EXL = 0 */
  237. memcpy(gebase, mips32_exception,
  238. mips32_exceptionEnd - mips32_exception);
  239. /* General Exception Entry point */
  240. memcpy(gebase + 0x180, mips32_exception,
  241. mips32_exceptionEnd - mips32_exception);
  242. /* For vectored interrupts poke the exception code @ all offsets 0-7 */
  243. for (i = 0; i < 8; i++) {
  244. kvm_debug("L1 Vectored handler @ %p\n",
  245. gebase + 0x200 + (i * VECTORSPACING));
  246. memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
  247. mips32_exceptionEnd - mips32_exception);
  248. }
  249. /* General handler, relocate to unmapped space for sanity's sake */
  250. offset = 0x2000;
  251. kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
  252. gebase + offset,
  253. mips32_GuestExceptionEnd - mips32_GuestException);
  254. memcpy(gebase + offset, mips32_GuestException,
  255. mips32_GuestExceptionEnd - mips32_GuestException);
  256. /* Invalidate the icache for these ranges */
  257. local_flush_icache_range((unsigned long)gebase,
  258. (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
  259. /*
  260. * Allocate comm page for guest kernel, a TLB will be reserved for
  261. * mapping GVA @ 0xFFFF8000 to this page
  262. */
  263. vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
  264. if (!vcpu->arch.kseg0_commpage) {
  265. err = -ENOMEM;
  266. goto out_free_gebase;
  267. }
  268. kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
  269. kvm_mips_commpage_init(vcpu);
  270. /* Init */
  271. vcpu->arch.last_sched_cpu = -1;
  272. /* Start off the timer */
  273. kvm_mips_init_count(vcpu);
  274. return vcpu;
  275. out_free_gebase:
  276. kfree(gebase);
  277. out_free_cpu:
  278. kfree(vcpu);
  279. out:
  280. return ERR_PTR(err);
  281. }
  282. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  283. {
  284. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  285. kvm_vcpu_uninit(vcpu);
  286. kvm_mips_dump_stats(vcpu);
  287. kfree(vcpu->arch.guest_ebase);
  288. kfree(vcpu->arch.kseg0_commpage);
  289. kfree(vcpu);
  290. }
  291. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  292. {
  293. kvm_arch_vcpu_free(vcpu);
  294. }
  295. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  296. struct kvm_guest_debug *dbg)
  297. {
  298. return -ENOIOCTLCMD;
  299. }
  300. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  301. {
  302. int r = 0;
  303. sigset_t sigsaved;
  304. if (vcpu->sigset_active)
  305. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  306. if (vcpu->mmio_needed) {
  307. if (!vcpu->mmio_is_write)
  308. kvm_mips_complete_mmio_load(vcpu, run);
  309. vcpu->mmio_needed = 0;
  310. }
  311. lose_fpu(1);
  312. local_irq_disable();
  313. /* Check if we have any exceptions/interrupts pending */
  314. kvm_mips_deliver_interrupts(vcpu,
  315. kvm_read_c0_guest_cause(vcpu->arch.cop0));
  316. __kvm_guest_enter();
  317. /* Disable hardware page table walking while in guest */
  318. htw_stop();
  319. r = __kvm_mips_vcpu_run(run, vcpu);
  320. /* Re-enable HTW before enabling interrupts */
  321. htw_start();
  322. __kvm_guest_exit();
  323. local_irq_enable();
  324. if (vcpu->sigset_active)
  325. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  326. return r;
  327. }
  328. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  329. struct kvm_mips_interrupt *irq)
  330. {
  331. int intr = (int)irq->irq;
  332. struct kvm_vcpu *dvcpu = NULL;
  333. if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
  334. kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
  335. (int)intr);
  336. if (irq->cpu == -1)
  337. dvcpu = vcpu;
  338. else
  339. dvcpu = vcpu->kvm->vcpus[irq->cpu];
  340. if (intr == 2 || intr == 3 || intr == 4) {
  341. kvm_mips_callbacks->queue_io_int(dvcpu, irq);
  342. } else if (intr == -2 || intr == -3 || intr == -4) {
  343. kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
  344. } else {
  345. kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
  346. irq->cpu, irq->irq);
  347. return -EINVAL;
  348. }
  349. dvcpu->arch.wait = 0;
  350. if (waitqueue_active(&dvcpu->wq))
  351. wake_up_interruptible(&dvcpu->wq);
  352. return 0;
  353. }
  354. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  355. struct kvm_mp_state *mp_state)
  356. {
  357. return -ENOIOCTLCMD;
  358. }
  359. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  360. struct kvm_mp_state *mp_state)
  361. {
  362. return -ENOIOCTLCMD;
  363. }
  364. static u64 kvm_mips_get_one_regs[] = {
  365. KVM_REG_MIPS_R0,
  366. KVM_REG_MIPS_R1,
  367. KVM_REG_MIPS_R2,
  368. KVM_REG_MIPS_R3,
  369. KVM_REG_MIPS_R4,
  370. KVM_REG_MIPS_R5,
  371. KVM_REG_MIPS_R6,
  372. KVM_REG_MIPS_R7,
  373. KVM_REG_MIPS_R8,
  374. KVM_REG_MIPS_R9,
  375. KVM_REG_MIPS_R10,
  376. KVM_REG_MIPS_R11,
  377. KVM_REG_MIPS_R12,
  378. KVM_REG_MIPS_R13,
  379. KVM_REG_MIPS_R14,
  380. KVM_REG_MIPS_R15,
  381. KVM_REG_MIPS_R16,
  382. KVM_REG_MIPS_R17,
  383. KVM_REG_MIPS_R18,
  384. KVM_REG_MIPS_R19,
  385. KVM_REG_MIPS_R20,
  386. KVM_REG_MIPS_R21,
  387. KVM_REG_MIPS_R22,
  388. KVM_REG_MIPS_R23,
  389. KVM_REG_MIPS_R24,
  390. KVM_REG_MIPS_R25,
  391. KVM_REG_MIPS_R26,
  392. KVM_REG_MIPS_R27,
  393. KVM_REG_MIPS_R28,
  394. KVM_REG_MIPS_R29,
  395. KVM_REG_MIPS_R30,
  396. KVM_REG_MIPS_R31,
  397. KVM_REG_MIPS_HI,
  398. KVM_REG_MIPS_LO,
  399. KVM_REG_MIPS_PC,
  400. KVM_REG_MIPS_CP0_INDEX,
  401. KVM_REG_MIPS_CP0_CONTEXT,
  402. KVM_REG_MIPS_CP0_USERLOCAL,
  403. KVM_REG_MIPS_CP0_PAGEMASK,
  404. KVM_REG_MIPS_CP0_WIRED,
  405. KVM_REG_MIPS_CP0_HWRENA,
  406. KVM_REG_MIPS_CP0_BADVADDR,
  407. KVM_REG_MIPS_CP0_COUNT,
  408. KVM_REG_MIPS_CP0_ENTRYHI,
  409. KVM_REG_MIPS_CP0_COMPARE,
  410. KVM_REG_MIPS_CP0_STATUS,
  411. KVM_REG_MIPS_CP0_CAUSE,
  412. KVM_REG_MIPS_CP0_EPC,
  413. KVM_REG_MIPS_CP0_PRID,
  414. KVM_REG_MIPS_CP0_CONFIG,
  415. KVM_REG_MIPS_CP0_CONFIG1,
  416. KVM_REG_MIPS_CP0_CONFIG2,
  417. KVM_REG_MIPS_CP0_CONFIG3,
  418. KVM_REG_MIPS_CP0_CONFIG4,
  419. KVM_REG_MIPS_CP0_CONFIG5,
  420. KVM_REG_MIPS_CP0_CONFIG7,
  421. KVM_REG_MIPS_CP0_ERROREPC,
  422. KVM_REG_MIPS_COUNT_CTL,
  423. KVM_REG_MIPS_COUNT_RESUME,
  424. KVM_REG_MIPS_COUNT_HZ,
  425. };
  426. static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
  427. const struct kvm_one_reg *reg)
  428. {
  429. struct mips_coproc *cop0 = vcpu->arch.cop0;
  430. struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
  431. int ret;
  432. s64 v;
  433. s64 vs[2];
  434. unsigned int idx;
  435. switch (reg->id) {
  436. /* General purpose registers */
  437. case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
  438. v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
  439. break;
  440. case KVM_REG_MIPS_HI:
  441. v = (long)vcpu->arch.hi;
  442. break;
  443. case KVM_REG_MIPS_LO:
  444. v = (long)vcpu->arch.lo;
  445. break;
  446. case KVM_REG_MIPS_PC:
  447. v = (long)vcpu->arch.pc;
  448. break;
  449. /* Floating point registers */
  450. case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
  451. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  452. return -EINVAL;
  453. idx = reg->id - KVM_REG_MIPS_FPR_32(0);
  454. /* Odd singles in top of even double when FR=0 */
  455. if (kvm_read_c0_guest_status(cop0) & ST0_FR)
  456. v = get_fpr32(&fpu->fpr[idx], 0);
  457. else
  458. v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
  459. break;
  460. case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
  461. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  462. return -EINVAL;
  463. idx = reg->id - KVM_REG_MIPS_FPR_64(0);
  464. /* Can't access odd doubles in FR=0 mode */
  465. if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
  466. return -EINVAL;
  467. v = get_fpr64(&fpu->fpr[idx], 0);
  468. break;
  469. case KVM_REG_MIPS_FCR_IR:
  470. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  471. return -EINVAL;
  472. v = boot_cpu_data.fpu_id;
  473. break;
  474. case KVM_REG_MIPS_FCR_CSR:
  475. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  476. return -EINVAL;
  477. v = fpu->fcr31;
  478. break;
  479. /* MIPS SIMD Architecture (MSA) registers */
  480. case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
  481. if (!kvm_mips_guest_has_msa(&vcpu->arch))
  482. return -EINVAL;
  483. /* Can't access MSA registers in FR=0 mode */
  484. if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
  485. return -EINVAL;
  486. idx = reg->id - KVM_REG_MIPS_VEC_128(0);
  487. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  488. /* least significant byte first */
  489. vs[0] = get_fpr64(&fpu->fpr[idx], 0);
  490. vs[1] = get_fpr64(&fpu->fpr[idx], 1);
  491. #else
  492. /* most significant byte first */
  493. vs[0] = get_fpr64(&fpu->fpr[idx], 1);
  494. vs[1] = get_fpr64(&fpu->fpr[idx], 0);
  495. #endif
  496. break;
  497. case KVM_REG_MIPS_MSA_IR:
  498. if (!kvm_mips_guest_has_msa(&vcpu->arch))
  499. return -EINVAL;
  500. v = boot_cpu_data.msa_id;
  501. break;
  502. case KVM_REG_MIPS_MSA_CSR:
  503. if (!kvm_mips_guest_has_msa(&vcpu->arch))
  504. return -EINVAL;
  505. v = fpu->msacsr;
  506. break;
  507. /* Co-processor 0 registers */
  508. case KVM_REG_MIPS_CP0_INDEX:
  509. v = (long)kvm_read_c0_guest_index(cop0);
  510. break;
  511. case KVM_REG_MIPS_CP0_CONTEXT:
  512. v = (long)kvm_read_c0_guest_context(cop0);
  513. break;
  514. case KVM_REG_MIPS_CP0_USERLOCAL:
  515. v = (long)kvm_read_c0_guest_userlocal(cop0);
  516. break;
  517. case KVM_REG_MIPS_CP0_PAGEMASK:
  518. v = (long)kvm_read_c0_guest_pagemask(cop0);
  519. break;
  520. case KVM_REG_MIPS_CP0_WIRED:
  521. v = (long)kvm_read_c0_guest_wired(cop0);
  522. break;
  523. case KVM_REG_MIPS_CP0_HWRENA:
  524. v = (long)kvm_read_c0_guest_hwrena(cop0);
  525. break;
  526. case KVM_REG_MIPS_CP0_BADVADDR:
  527. v = (long)kvm_read_c0_guest_badvaddr(cop0);
  528. break;
  529. case KVM_REG_MIPS_CP0_ENTRYHI:
  530. v = (long)kvm_read_c0_guest_entryhi(cop0);
  531. break;
  532. case KVM_REG_MIPS_CP0_COMPARE:
  533. v = (long)kvm_read_c0_guest_compare(cop0);
  534. break;
  535. case KVM_REG_MIPS_CP0_STATUS:
  536. v = (long)kvm_read_c0_guest_status(cop0);
  537. break;
  538. case KVM_REG_MIPS_CP0_CAUSE:
  539. v = (long)kvm_read_c0_guest_cause(cop0);
  540. break;
  541. case KVM_REG_MIPS_CP0_EPC:
  542. v = (long)kvm_read_c0_guest_epc(cop0);
  543. break;
  544. case KVM_REG_MIPS_CP0_PRID:
  545. v = (long)kvm_read_c0_guest_prid(cop0);
  546. break;
  547. case KVM_REG_MIPS_CP0_CONFIG:
  548. v = (long)kvm_read_c0_guest_config(cop0);
  549. break;
  550. case KVM_REG_MIPS_CP0_CONFIG1:
  551. v = (long)kvm_read_c0_guest_config1(cop0);
  552. break;
  553. case KVM_REG_MIPS_CP0_CONFIG2:
  554. v = (long)kvm_read_c0_guest_config2(cop0);
  555. break;
  556. case KVM_REG_MIPS_CP0_CONFIG3:
  557. v = (long)kvm_read_c0_guest_config3(cop0);
  558. break;
  559. case KVM_REG_MIPS_CP0_CONFIG4:
  560. v = (long)kvm_read_c0_guest_config4(cop0);
  561. break;
  562. case KVM_REG_MIPS_CP0_CONFIG5:
  563. v = (long)kvm_read_c0_guest_config5(cop0);
  564. break;
  565. case KVM_REG_MIPS_CP0_CONFIG7:
  566. v = (long)kvm_read_c0_guest_config7(cop0);
  567. break;
  568. case KVM_REG_MIPS_CP0_ERROREPC:
  569. v = (long)kvm_read_c0_guest_errorepc(cop0);
  570. break;
  571. /* registers to be handled specially */
  572. case KVM_REG_MIPS_CP0_COUNT:
  573. case KVM_REG_MIPS_COUNT_CTL:
  574. case KVM_REG_MIPS_COUNT_RESUME:
  575. case KVM_REG_MIPS_COUNT_HZ:
  576. ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
  577. if (ret)
  578. return ret;
  579. break;
  580. default:
  581. return -EINVAL;
  582. }
  583. if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
  584. u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
  585. return put_user(v, uaddr64);
  586. } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
  587. u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
  588. u32 v32 = (u32)v;
  589. return put_user(v32, uaddr32);
  590. } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
  591. void __user *uaddr = (void __user *)(long)reg->addr;
  592. return copy_to_user(uaddr, vs, 16);
  593. } else {
  594. return -EINVAL;
  595. }
  596. }
  597. static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
  598. const struct kvm_one_reg *reg)
  599. {
  600. struct mips_coproc *cop0 = vcpu->arch.cop0;
  601. struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
  602. s64 v;
  603. s64 vs[2];
  604. unsigned int idx;
  605. if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
  606. u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
  607. if (get_user(v, uaddr64) != 0)
  608. return -EFAULT;
  609. } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
  610. u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
  611. s32 v32;
  612. if (get_user(v32, uaddr32) != 0)
  613. return -EFAULT;
  614. v = (s64)v32;
  615. } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
  616. void __user *uaddr = (void __user *)(long)reg->addr;
  617. return copy_from_user(vs, uaddr, 16);
  618. } else {
  619. return -EINVAL;
  620. }
  621. switch (reg->id) {
  622. /* General purpose registers */
  623. case KVM_REG_MIPS_R0:
  624. /* Silently ignore requests to set $0 */
  625. break;
  626. case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
  627. vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
  628. break;
  629. case KVM_REG_MIPS_HI:
  630. vcpu->arch.hi = v;
  631. break;
  632. case KVM_REG_MIPS_LO:
  633. vcpu->arch.lo = v;
  634. break;
  635. case KVM_REG_MIPS_PC:
  636. vcpu->arch.pc = v;
  637. break;
  638. /* Floating point registers */
  639. case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
  640. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  641. return -EINVAL;
  642. idx = reg->id - KVM_REG_MIPS_FPR_32(0);
  643. /* Odd singles in top of even double when FR=0 */
  644. if (kvm_read_c0_guest_status(cop0) & ST0_FR)
  645. set_fpr32(&fpu->fpr[idx], 0, v);
  646. else
  647. set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
  648. break;
  649. case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
  650. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  651. return -EINVAL;
  652. idx = reg->id - KVM_REG_MIPS_FPR_64(0);
  653. /* Can't access odd doubles in FR=0 mode */
  654. if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
  655. return -EINVAL;
  656. set_fpr64(&fpu->fpr[idx], 0, v);
  657. break;
  658. case KVM_REG_MIPS_FCR_IR:
  659. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  660. return -EINVAL;
  661. /* Read-only */
  662. break;
  663. case KVM_REG_MIPS_FCR_CSR:
  664. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  665. return -EINVAL;
  666. fpu->fcr31 = v;
  667. break;
  668. /* MIPS SIMD Architecture (MSA) registers */
  669. case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
  670. if (!kvm_mips_guest_has_msa(&vcpu->arch))
  671. return -EINVAL;
  672. idx = reg->id - KVM_REG_MIPS_VEC_128(0);
  673. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  674. /* least significant byte first */
  675. set_fpr64(&fpu->fpr[idx], 0, vs[0]);
  676. set_fpr64(&fpu->fpr[idx], 1, vs[1]);
  677. #else
  678. /* most significant byte first */
  679. set_fpr64(&fpu->fpr[idx], 1, vs[0]);
  680. set_fpr64(&fpu->fpr[idx], 0, vs[1]);
  681. #endif
  682. break;
  683. case KVM_REG_MIPS_MSA_IR:
  684. if (!kvm_mips_guest_has_msa(&vcpu->arch))
  685. return -EINVAL;
  686. /* Read-only */
  687. break;
  688. case KVM_REG_MIPS_MSA_CSR:
  689. if (!kvm_mips_guest_has_msa(&vcpu->arch))
  690. return -EINVAL;
  691. fpu->msacsr = v;
  692. break;
  693. /* Co-processor 0 registers */
  694. case KVM_REG_MIPS_CP0_INDEX:
  695. kvm_write_c0_guest_index(cop0, v);
  696. break;
  697. case KVM_REG_MIPS_CP0_CONTEXT:
  698. kvm_write_c0_guest_context(cop0, v);
  699. break;
  700. case KVM_REG_MIPS_CP0_USERLOCAL:
  701. kvm_write_c0_guest_userlocal(cop0, v);
  702. break;
  703. case KVM_REG_MIPS_CP0_PAGEMASK:
  704. kvm_write_c0_guest_pagemask(cop0, v);
  705. break;
  706. case KVM_REG_MIPS_CP0_WIRED:
  707. kvm_write_c0_guest_wired(cop0, v);
  708. break;
  709. case KVM_REG_MIPS_CP0_HWRENA:
  710. kvm_write_c0_guest_hwrena(cop0, v);
  711. break;
  712. case KVM_REG_MIPS_CP0_BADVADDR:
  713. kvm_write_c0_guest_badvaddr(cop0, v);
  714. break;
  715. case KVM_REG_MIPS_CP0_ENTRYHI:
  716. kvm_write_c0_guest_entryhi(cop0, v);
  717. break;
  718. case KVM_REG_MIPS_CP0_STATUS:
  719. kvm_write_c0_guest_status(cop0, v);
  720. break;
  721. case KVM_REG_MIPS_CP0_EPC:
  722. kvm_write_c0_guest_epc(cop0, v);
  723. break;
  724. case KVM_REG_MIPS_CP0_PRID:
  725. kvm_write_c0_guest_prid(cop0, v);
  726. break;
  727. case KVM_REG_MIPS_CP0_ERROREPC:
  728. kvm_write_c0_guest_errorepc(cop0, v);
  729. break;
  730. /* registers to be handled specially */
  731. case KVM_REG_MIPS_CP0_COUNT:
  732. case KVM_REG_MIPS_CP0_COMPARE:
  733. case KVM_REG_MIPS_CP0_CAUSE:
  734. case KVM_REG_MIPS_CP0_CONFIG:
  735. case KVM_REG_MIPS_CP0_CONFIG1:
  736. case KVM_REG_MIPS_CP0_CONFIG2:
  737. case KVM_REG_MIPS_CP0_CONFIG3:
  738. case KVM_REG_MIPS_CP0_CONFIG4:
  739. case KVM_REG_MIPS_CP0_CONFIG5:
  740. case KVM_REG_MIPS_COUNT_CTL:
  741. case KVM_REG_MIPS_COUNT_RESUME:
  742. case KVM_REG_MIPS_COUNT_HZ:
  743. return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
  744. default:
  745. return -EINVAL;
  746. }
  747. return 0;
  748. }
  749. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  750. struct kvm_enable_cap *cap)
  751. {
  752. int r = 0;
  753. if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
  754. return -EINVAL;
  755. if (cap->flags)
  756. return -EINVAL;
  757. if (cap->args[0])
  758. return -EINVAL;
  759. switch (cap->cap) {
  760. case KVM_CAP_MIPS_FPU:
  761. vcpu->arch.fpu_enabled = true;
  762. break;
  763. case KVM_CAP_MIPS_MSA:
  764. vcpu->arch.msa_enabled = true;
  765. break;
  766. default:
  767. r = -EINVAL;
  768. break;
  769. }
  770. return r;
  771. }
  772. long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
  773. unsigned long arg)
  774. {
  775. struct kvm_vcpu *vcpu = filp->private_data;
  776. void __user *argp = (void __user *)arg;
  777. long r;
  778. switch (ioctl) {
  779. case KVM_SET_ONE_REG:
  780. case KVM_GET_ONE_REG: {
  781. struct kvm_one_reg reg;
  782. if (copy_from_user(&reg, argp, sizeof(reg)))
  783. return -EFAULT;
  784. if (ioctl == KVM_SET_ONE_REG)
  785. return kvm_mips_set_reg(vcpu, &reg);
  786. else
  787. return kvm_mips_get_reg(vcpu, &reg);
  788. }
  789. case KVM_GET_REG_LIST: {
  790. struct kvm_reg_list __user *user_list = argp;
  791. u64 __user *reg_dest;
  792. struct kvm_reg_list reg_list;
  793. unsigned n;
  794. if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
  795. return -EFAULT;
  796. n = reg_list.n;
  797. reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
  798. if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
  799. return -EFAULT;
  800. if (n < reg_list.n)
  801. return -E2BIG;
  802. reg_dest = user_list->reg;
  803. if (copy_to_user(reg_dest, kvm_mips_get_one_regs,
  804. sizeof(kvm_mips_get_one_regs)))
  805. return -EFAULT;
  806. return 0;
  807. }
  808. case KVM_NMI:
  809. /* Treat the NMI as a CPU reset */
  810. r = kvm_mips_reset_vcpu(vcpu);
  811. break;
  812. case KVM_INTERRUPT:
  813. {
  814. struct kvm_mips_interrupt irq;
  815. r = -EFAULT;
  816. if (copy_from_user(&irq, argp, sizeof(irq)))
  817. goto out;
  818. kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
  819. irq.irq);
  820. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  821. break;
  822. }
  823. case KVM_ENABLE_CAP: {
  824. struct kvm_enable_cap cap;
  825. r = -EFAULT;
  826. if (copy_from_user(&cap, argp, sizeof(cap)))
  827. goto out;
  828. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  829. break;
  830. }
  831. default:
  832. r = -ENOIOCTLCMD;
  833. }
  834. out:
  835. return r;
  836. }
  837. /* Get (and clear) the dirty memory log for a memory slot. */
  838. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  839. {
  840. struct kvm_memslots *slots;
  841. struct kvm_memory_slot *memslot;
  842. unsigned long ga, ga_end;
  843. int is_dirty = 0;
  844. int r;
  845. unsigned long n;
  846. mutex_lock(&kvm->slots_lock);
  847. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  848. if (r)
  849. goto out;
  850. /* If nothing is dirty, don't bother messing with page tables. */
  851. if (is_dirty) {
  852. slots = kvm_memslots(kvm);
  853. memslot = id_to_memslot(slots, log->slot);
  854. ga = memslot->base_gfn << PAGE_SHIFT;
  855. ga_end = ga + (memslot->npages << PAGE_SHIFT);
  856. kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
  857. ga_end);
  858. n = kvm_dirty_bitmap_bytes(memslot);
  859. memset(memslot->dirty_bitmap, 0, n);
  860. }
  861. r = 0;
  862. out:
  863. mutex_unlock(&kvm->slots_lock);
  864. return r;
  865. }
  866. long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  867. {
  868. long r;
  869. switch (ioctl) {
  870. default:
  871. r = -ENOIOCTLCMD;
  872. }
  873. return r;
  874. }
  875. int kvm_arch_init(void *opaque)
  876. {
  877. if (kvm_mips_callbacks) {
  878. kvm_err("kvm: module already exists\n");
  879. return -EEXIST;
  880. }
  881. return kvm_mips_emulation_init(&kvm_mips_callbacks);
  882. }
  883. void kvm_arch_exit(void)
  884. {
  885. kvm_mips_callbacks = NULL;
  886. }
  887. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  888. struct kvm_sregs *sregs)
  889. {
  890. return -ENOIOCTLCMD;
  891. }
  892. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  893. struct kvm_sregs *sregs)
  894. {
  895. return -ENOIOCTLCMD;
  896. }
  897. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  898. {
  899. }
  900. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  901. {
  902. return -ENOIOCTLCMD;
  903. }
  904. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  905. {
  906. return -ENOIOCTLCMD;
  907. }
  908. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  909. {
  910. return VM_FAULT_SIGBUS;
  911. }
  912. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  913. {
  914. int r;
  915. switch (ext) {
  916. case KVM_CAP_ONE_REG:
  917. case KVM_CAP_ENABLE_CAP:
  918. r = 1;
  919. break;
  920. case KVM_CAP_COALESCED_MMIO:
  921. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  922. break;
  923. case KVM_CAP_MIPS_FPU:
  924. r = !!cpu_has_fpu;
  925. break;
  926. case KVM_CAP_MIPS_MSA:
  927. /*
  928. * We don't support MSA vector partitioning yet:
  929. * 1) It would require explicit support which can't be tested
  930. * yet due to lack of support in current hardware.
  931. * 2) It extends the state that would need to be saved/restored
  932. * by e.g. QEMU for migration.
  933. *
  934. * When vector partitioning hardware becomes available, support
  935. * could be added by requiring a flag when enabling
  936. * KVM_CAP_MIPS_MSA capability to indicate that userland knows
  937. * to save/restore the appropriate extra state.
  938. */
  939. r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
  940. break;
  941. default:
  942. r = 0;
  943. break;
  944. }
  945. return r;
  946. }
  947. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  948. {
  949. return kvm_mips_pending_timer(vcpu);
  950. }
  951. int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
  952. {
  953. int i;
  954. struct mips_coproc *cop0;
  955. if (!vcpu)
  956. return -1;
  957. kvm_debug("VCPU Register Dump:\n");
  958. kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
  959. kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
  960. for (i = 0; i < 32; i += 4) {
  961. kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
  962. vcpu->arch.gprs[i],
  963. vcpu->arch.gprs[i + 1],
  964. vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
  965. }
  966. kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
  967. kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
  968. cop0 = vcpu->arch.cop0;
  969. kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
  970. kvm_read_c0_guest_status(cop0),
  971. kvm_read_c0_guest_cause(cop0));
  972. kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
  973. return 0;
  974. }
  975. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  976. {
  977. int i;
  978. for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
  979. vcpu->arch.gprs[i] = regs->gpr[i];
  980. vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
  981. vcpu->arch.hi = regs->hi;
  982. vcpu->arch.lo = regs->lo;
  983. vcpu->arch.pc = regs->pc;
  984. return 0;
  985. }
  986. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  987. {
  988. int i;
  989. for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
  990. regs->gpr[i] = vcpu->arch.gprs[i];
  991. regs->hi = vcpu->arch.hi;
  992. regs->lo = vcpu->arch.lo;
  993. regs->pc = vcpu->arch.pc;
  994. return 0;
  995. }
  996. static void kvm_mips_comparecount_func(unsigned long data)
  997. {
  998. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  999. kvm_mips_callbacks->queue_timer_int(vcpu);
  1000. vcpu->arch.wait = 0;
  1001. if (waitqueue_active(&vcpu->wq))
  1002. wake_up_interruptible(&vcpu->wq);
  1003. }
  1004. /* low level hrtimer wake routine */
  1005. static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
  1006. {
  1007. struct kvm_vcpu *vcpu;
  1008. vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
  1009. kvm_mips_comparecount_func((unsigned long) vcpu);
  1010. return kvm_mips_count_timeout(vcpu);
  1011. }
  1012. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  1013. {
  1014. kvm_mips_callbacks->vcpu_init(vcpu);
  1015. hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
  1016. HRTIMER_MODE_REL);
  1017. vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
  1018. return 0;
  1019. }
  1020. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1021. struct kvm_translation *tr)
  1022. {
  1023. return 0;
  1024. }
  1025. /* Initial guest state */
  1026. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  1027. {
  1028. return kvm_mips_callbacks->vcpu_setup(vcpu);
  1029. }
  1030. static void kvm_mips_set_c0_status(void)
  1031. {
  1032. uint32_t status = read_c0_status();
  1033. if (cpu_has_dsp)
  1034. status |= (ST0_MX);
  1035. write_c0_status(status);
  1036. ehb();
  1037. }
  1038. /*
  1039. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  1040. */
  1041. int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
  1042. {
  1043. uint32_t cause = vcpu->arch.host_cp0_cause;
  1044. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1045. uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
  1046. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  1047. enum emulation_result er = EMULATE_DONE;
  1048. int ret = RESUME_GUEST;
  1049. /* re-enable HTW before enabling interrupts */
  1050. htw_start();
  1051. /* Set a default exit reason */
  1052. run->exit_reason = KVM_EXIT_UNKNOWN;
  1053. run->ready_for_interrupt_injection = 1;
  1054. /*
  1055. * Set the appropriate status bits based on host CPU features,
  1056. * before we hit the scheduler
  1057. */
  1058. kvm_mips_set_c0_status();
  1059. local_irq_enable();
  1060. kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
  1061. cause, opc, run, vcpu);
  1062. /*
  1063. * Do a privilege check, if in UM most of these exit conditions end up
  1064. * causing an exception to be delivered to the Guest Kernel
  1065. */
  1066. er = kvm_mips_check_privilege(cause, opc, run, vcpu);
  1067. if (er == EMULATE_PRIV_FAIL) {
  1068. goto skip_emul;
  1069. } else if (er == EMULATE_FAIL) {
  1070. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1071. ret = RESUME_HOST;
  1072. goto skip_emul;
  1073. }
  1074. switch (exccode) {
  1075. case T_INT:
  1076. kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
  1077. ++vcpu->stat.int_exits;
  1078. trace_kvm_exit(vcpu, INT_EXITS);
  1079. if (need_resched())
  1080. cond_resched();
  1081. ret = RESUME_GUEST;
  1082. break;
  1083. case T_COP_UNUSABLE:
  1084. kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
  1085. ++vcpu->stat.cop_unusable_exits;
  1086. trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
  1087. ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
  1088. /* XXXKYMA: Might need to return to user space */
  1089. if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
  1090. ret = RESUME_HOST;
  1091. break;
  1092. case T_TLB_MOD:
  1093. ++vcpu->stat.tlbmod_exits;
  1094. trace_kvm_exit(vcpu, TLBMOD_EXITS);
  1095. ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
  1096. break;
  1097. case T_TLB_ST_MISS:
  1098. kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
  1099. cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
  1100. badvaddr);
  1101. ++vcpu->stat.tlbmiss_st_exits;
  1102. trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
  1103. ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
  1104. break;
  1105. case T_TLB_LD_MISS:
  1106. kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
  1107. cause, opc, badvaddr);
  1108. ++vcpu->stat.tlbmiss_ld_exits;
  1109. trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
  1110. ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
  1111. break;
  1112. case T_ADDR_ERR_ST:
  1113. ++vcpu->stat.addrerr_st_exits;
  1114. trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
  1115. ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
  1116. break;
  1117. case T_ADDR_ERR_LD:
  1118. ++vcpu->stat.addrerr_ld_exits;
  1119. trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
  1120. ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
  1121. break;
  1122. case T_SYSCALL:
  1123. ++vcpu->stat.syscall_exits;
  1124. trace_kvm_exit(vcpu, SYSCALL_EXITS);
  1125. ret = kvm_mips_callbacks->handle_syscall(vcpu);
  1126. break;
  1127. case T_RES_INST:
  1128. ++vcpu->stat.resvd_inst_exits;
  1129. trace_kvm_exit(vcpu, RESVD_INST_EXITS);
  1130. ret = kvm_mips_callbacks->handle_res_inst(vcpu);
  1131. break;
  1132. case T_BREAK:
  1133. ++vcpu->stat.break_inst_exits;
  1134. trace_kvm_exit(vcpu, BREAK_INST_EXITS);
  1135. ret = kvm_mips_callbacks->handle_break(vcpu);
  1136. break;
  1137. case T_TRAP:
  1138. ++vcpu->stat.trap_inst_exits;
  1139. trace_kvm_exit(vcpu, TRAP_INST_EXITS);
  1140. ret = kvm_mips_callbacks->handle_trap(vcpu);
  1141. break;
  1142. case T_MSAFPE:
  1143. ++vcpu->stat.msa_fpe_exits;
  1144. trace_kvm_exit(vcpu, MSA_FPE_EXITS);
  1145. ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
  1146. break;
  1147. case T_FPE:
  1148. ++vcpu->stat.fpe_exits;
  1149. trace_kvm_exit(vcpu, FPE_EXITS);
  1150. ret = kvm_mips_callbacks->handle_fpe(vcpu);
  1151. break;
  1152. case T_MSADIS:
  1153. ++vcpu->stat.msa_disabled_exits;
  1154. trace_kvm_exit(vcpu, MSA_DISABLED_EXITS);
  1155. ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
  1156. break;
  1157. default:
  1158. kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
  1159. exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
  1160. kvm_read_c0_guest_status(vcpu->arch.cop0));
  1161. kvm_arch_vcpu_dump_regs(vcpu);
  1162. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1163. ret = RESUME_HOST;
  1164. break;
  1165. }
  1166. skip_emul:
  1167. local_irq_disable();
  1168. if (er == EMULATE_DONE && !(ret & RESUME_HOST))
  1169. kvm_mips_deliver_interrupts(vcpu, cause);
  1170. if (!(ret & RESUME_HOST)) {
  1171. /* Only check for signals if not already exiting to userspace */
  1172. if (signal_pending(current)) {
  1173. run->exit_reason = KVM_EXIT_INTR;
  1174. ret = (-EINTR << 2) | RESUME_HOST;
  1175. ++vcpu->stat.signal_exits;
  1176. trace_kvm_exit(vcpu, SIGNAL_EXITS);
  1177. }
  1178. }
  1179. if (ret == RESUME_GUEST) {
  1180. /*
  1181. * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
  1182. * is live), restore FCR31 / MSACSR.
  1183. *
  1184. * This should be before returning to the guest exception
  1185. * vector, as it may well cause an [MSA] FP exception if there
  1186. * are pending exception bits unmasked. (see
  1187. * kvm_mips_csr_die_notifier() for how that is handled).
  1188. */
  1189. if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
  1190. read_c0_status() & ST0_CU1)
  1191. __kvm_restore_fcsr(&vcpu->arch);
  1192. if (kvm_mips_guest_has_msa(&vcpu->arch) &&
  1193. read_c0_config5() & MIPS_CONF5_MSAEN)
  1194. __kvm_restore_msacsr(&vcpu->arch);
  1195. }
  1196. /* Disable HTW before returning to guest or host */
  1197. htw_stop();
  1198. return ret;
  1199. }
  1200. /* Enable FPU for guest and restore context */
  1201. void kvm_own_fpu(struct kvm_vcpu *vcpu)
  1202. {
  1203. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1204. unsigned int sr, cfg5;
  1205. preempt_disable();
  1206. sr = kvm_read_c0_guest_status(cop0);
  1207. /*
  1208. * If MSA state is already live, it is undefined how it interacts with
  1209. * FR=0 FPU state, and we don't want to hit reserved instruction
  1210. * exceptions trying to save the MSA state later when CU=1 && FR=1, so
  1211. * play it safe and save it first.
  1212. *
  1213. * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
  1214. * get called when guest CU1 is set, however we can't trust the guest
  1215. * not to clobber the status register directly via the commpage.
  1216. */
  1217. if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
  1218. vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA)
  1219. kvm_lose_fpu(vcpu);
  1220. /*
  1221. * Enable FPU for guest
  1222. * We set FR and FRE according to guest context
  1223. */
  1224. change_c0_status(ST0_CU1 | ST0_FR, sr);
  1225. if (cpu_has_fre) {
  1226. cfg5 = kvm_read_c0_guest_config5(cop0);
  1227. change_c0_config5(MIPS_CONF5_FRE, cfg5);
  1228. }
  1229. enable_fpu_hazard();
  1230. /* If guest FPU state not active, restore it now */
  1231. if (!(vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU)) {
  1232. __kvm_restore_fpu(&vcpu->arch);
  1233. vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_FPU;
  1234. }
  1235. preempt_enable();
  1236. }
  1237. #ifdef CONFIG_CPU_HAS_MSA
  1238. /* Enable MSA for guest and restore context */
  1239. void kvm_own_msa(struct kvm_vcpu *vcpu)
  1240. {
  1241. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1242. unsigned int sr, cfg5;
  1243. preempt_disable();
  1244. /*
  1245. * Enable FPU if enabled in guest, since we're restoring FPU context
  1246. * anyway. We set FR and FRE according to guest context.
  1247. */
  1248. if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
  1249. sr = kvm_read_c0_guest_status(cop0);
  1250. /*
  1251. * If FR=0 FPU state is already live, it is undefined how it
  1252. * interacts with MSA state, so play it safe and save it first.
  1253. */
  1254. if (!(sr & ST0_FR) &&
  1255. (vcpu->arch.fpu_inuse & (KVM_MIPS_FPU_FPU |
  1256. KVM_MIPS_FPU_MSA)) == KVM_MIPS_FPU_FPU)
  1257. kvm_lose_fpu(vcpu);
  1258. change_c0_status(ST0_CU1 | ST0_FR, sr);
  1259. if (sr & ST0_CU1 && cpu_has_fre) {
  1260. cfg5 = kvm_read_c0_guest_config5(cop0);
  1261. change_c0_config5(MIPS_CONF5_FRE, cfg5);
  1262. }
  1263. }
  1264. /* Enable MSA for guest */
  1265. set_c0_config5(MIPS_CONF5_MSAEN);
  1266. enable_fpu_hazard();
  1267. switch (vcpu->arch.fpu_inuse & (KVM_MIPS_FPU_FPU | KVM_MIPS_FPU_MSA)) {
  1268. case KVM_MIPS_FPU_FPU:
  1269. /*
  1270. * Guest FPU state already loaded, only restore upper MSA state
  1271. */
  1272. __kvm_restore_msa_upper(&vcpu->arch);
  1273. vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_MSA;
  1274. break;
  1275. case 0:
  1276. /* Neither FPU or MSA already active, restore full MSA state */
  1277. __kvm_restore_msa(&vcpu->arch);
  1278. vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_MSA;
  1279. if (kvm_mips_guest_has_fpu(&vcpu->arch))
  1280. vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_FPU;
  1281. break;
  1282. default:
  1283. break;
  1284. }
  1285. preempt_enable();
  1286. }
  1287. #endif
  1288. /* Drop FPU & MSA without saving it */
  1289. void kvm_drop_fpu(struct kvm_vcpu *vcpu)
  1290. {
  1291. preempt_disable();
  1292. if (cpu_has_msa && vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA) {
  1293. disable_msa();
  1294. vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_MSA;
  1295. }
  1296. if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU) {
  1297. clear_c0_status(ST0_CU1 | ST0_FR);
  1298. vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_FPU;
  1299. }
  1300. preempt_enable();
  1301. }
  1302. /* Save and disable FPU & MSA */
  1303. void kvm_lose_fpu(struct kvm_vcpu *vcpu)
  1304. {
  1305. /*
  1306. * FPU & MSA get disabled in root context (hardware) when it is disabled
  1307. * in guest context (software), but the register state in the hardware
  1308. * may still be in use. This is why we explicitly re-enable the hardware
  1309. * before saving.
  1310. */
  1311. preempt_disable();
  1312. if (cpu_has_msa && vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA) {
  1313. set_c0_config5(MIPS_CONF5_MSAEN);
  1314. enable_fpu_hazard();
  1315. __kvm_save_msa(&vcpu->arch);
  1316. /* Disable MSA & FPU */
  1317. disable_msa();
  1318. if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU)
  1319. clear_c0_status(ST0_CU1 | ST0_FR);
  1320. vcpu->arch.fpu_inuse &= ~(KVM_MIPS_FPU_FPU | KVM_MIPS_FPU_MSA);
  1321. } else if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU) {
  1322. set_c0_status(ST0_CU1);
  1323. enable_fpu_hazard();
  1324. __kvm_save_fpu(&vcpu->arch);
  1325. vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_FPU;
  1326. /* Disable FPU */
  1327. clear_c0_status(ST0_CU1 | ST0_FR);
  1328. }
  1329. preempt_enable();
  1330. }
  1331. /*
  1332. * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
  1333. * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
  1334. * exception if cause bits are set in the value being written.
  1335. */
  1336. static int kvm_mips_csr_die_notify(struct notifier_block *self,
  1337. unsigned long cmd, void *ptr)
  1338. {
  1339. struct die_args *args = (struct die_args *)ptr;
  1340. struct pt_regs *regs = args->regs;
  1341. unsigned long pc;
  1342. /* Only interested in FPE and MSAFPE */
  1343. if (cmd != DIE_FP && cmd != DIE_MSAFP)
  1344. return NOTIFY_DONE;
  1345. /* Return immediately if guest context isn't active */
  1346. if (!(current->flags & PF_VCPU))
  1347. return NOTIFY_DONE;
  1348. /* Should never get here from user mode */
  1349. BUG_ON(user_mode(regs));
  1350. pc = instruction_pointer(regs);
  1351. switch (cmd) {
  1352. case DIE_FP:
  1353. /* match 2nd instruction in __kvm_restore_fcsr */
  1354. if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
  1355. return NOTIFY_DONE;
  1356. break;
  1357. case DIE_MSAFP:
  1358. /* match 2nd/3rd instruction in __kvm_restore_msacsr */
  1359. if (!cpu_has_msa ||
  1360. pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
  1361. pc > (unsigned long)&__kvm_restore_msacsr + 8)
  1362. return NOTIFY_DONE;
  1363. break;
  1364. }
  1365. /* Move PC forward a little and continue executing */
  1366. instruction_pointer(regs) += 4;
  1367. return NOTIFY_STOP;
  1368. }
  1369. static struct notifier_block kvm_mips_csr_die_notifier = {
  1370. .notifier_call = kvm_mips_csr_die_notify,
  1371. };
  1372. int __init kvm_mips_init(void)
  1373. {
  1374. int ret;
  1375. ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1376. if (ret)
  1377. return ret;
  1378. register_die_notifier(&kvm_mips_csr_die_notifier);
  1379. /*
  1380. * On MIPS, kernel modules are executed from "mapped space", which
  1381. * requires TLBs. The TLB handling code is statically linked with
  1382. * the rest of the kernel (tlb.c) to avoid the possibility of
  1383. * double faulting. The issue is that the TLB code references
  1384. * routines that are part of the the KVM module, which are only
  1385. * available once the module is loaded.
  1386. */
  1387. kvm_mips_gfn_to_pfn = gfn_to_pfn;
  1388. kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
  1389. kvm_mips_is_error_pfn = is_error_pfn;
  1390. return 0;
  1391. }
  1392. void __exit kvm_mips_exit(void)
  1393. {
  1394. kvm_exit();
  1395. kvm_mips_gfn_to_pfn = NULL;
  1396. kvm_mips_release_pfn_clean = NULL;
  1397. kvm_mips_is_error_pfn = NULL;
  1398. unregister_die_notifier(&kvm_mips_csr_die_notifier);
  1399. }
  1400. module_init(kvm_mips_init);
  1401. module_exit(kvm_mips_exit);
  1402. EXPORT_TRACEPOINT_SYMBOL(kvm_exit);