mips.c 40 KB

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