mips.c 40 KB

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