mips.c 42 KB

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