mips.c 40 KB

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