mips.c 42 KB

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