mips.c 40 KB

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