mips.c 41 KB

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