mips.c 40 KB

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