mips.c 41 KB

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