mips.c 42 KB

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