book3s_pr.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. /*
  2. * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
  3. *
  4. * Authors:
  5. * Alexander Graf <agraf@suse.de>
  6. * Kevin Wolf <mail@kevin-wolf.de>
  7. * Paul Mackerras <paulus@samba.org>
  8. *
  9. * Description:
  10. * Functions relating to running KVM on Book 3S processors where
  11. * we don't have access to hypervisor mode, and we run the guest
  12. * in problem state (user mode).
  13. *
  14. * This file is derived from arch/powerpc/kvm/44x.c,
  15. * by Hollis Blanchard <hollisb@us.ibm.com>.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License, version 2, as
  19. * published by the Free Software Foundation.
  20. */
  21. #include <linux/kvm_host.h>
  22. #include <linux/export.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <asm/reg.h>
  26. #include <asm/cputable.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/io.h>
  31. #include <asm/kvm_ppc.h>
  32. #include <asm/kvm_book3s.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/switch_to.h>
  35. #include <asm/firmware.h>
  36. #include <asm/hvcall.h>
  37. #include <linux/gfp.h>
  38. #include <linux/sched.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/highmem.h>
  41. #include "trace.h"
  42. /* #define EXIT_DEBUG */
  43. /* #define DEBUG_EXT */
  44. static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
  45. ulong msr);
  46. /* Some compatibility defines */
  47. #ifdef CONFIG_PPC_BOOK3S_32
  48. #define MSR_USER32 MSR_USER
  49. #define MSR_USER64 MSR_USER
  50. #define HW_PAGE_SIZE PAGE_SIZE
  51. #endif
  52. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  53. {
  54. #ifdef CONFIG_PPC_BOOK3S_64
  55. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  56. memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb));
  57. svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max;
  58. svcpu_put(svcpu);
  59. #endif
  60. vcpu->cpu = smp_processor_id();
  61. #ifdef CONFIG_PPC_BOOK3S_32
  62. current->thread.kvm_shadow_vcpu = vcpu->arch.shadow_vcpu;
  63. #endif
  64. }
  65. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  66. {
  67. #ifdef CONFIG_PPC_BOOK3S_64
  68. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  69. memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb));
  70. to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max;
  71. svcpu_put(svcpu);
  72. #endif
  73. kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
  74. vcpu->cpu = -1;
  75. }
  76. /* Copy data needed by real-mode code from vcpu to shadow vcpu */
  77. void kvmppc_copy_to_svcpu(struct kvmppc_book3s_shadow_vcpu *svcpu,
  78. struct kvm_vcpu *vcpu)
  79. {
  80. svcpu->gpr[0] = vcpu->arch.gpr[0];
  81. svcpu->gpr[1] = vcpu->arch.gpr[1];
  82. svcpu->gpr[2] = vcpu->arch.gpr[2];
  83. svcpu->gpr[3] = vcpu->arch.gpr[3];
  84. svcpu->gpr[4] = vcpu->arch.gpr[4];
  85. svcpu->gpr[5] = vcpu->arch.gpr[5];
  86. svcpu->gpr[6] = vcpu->arch.gpr[6];
  87. svcpu->gpr[7] = vcpu->arch.gpr[7];
  88. svcpu->gpr[8] = vcpu->arch.gpr[8];
  89. svcpu->gpr[9] = vcpu->arch.gpr[9];
  90. svcpu->gpr[10] = vcpu->arch.gpr[10];
  91. svcpu->gpr[11] = vcpu->arch.gpr[11];
  92. svcpu->gpr[12] = vcpu->arch.gpr[12];
  93. svcpu->gpr[13] = vcpu->arch.gpr[13];
  94. svcpu->cr = vcpu->arch.cr;
  95. svcpu->xer = vcpu->arch.xer;
  96. svcpu->ctr = vcpu->arch.ctr;
  97. svcpu->lr = vcpu->arch.lr;
  98. svcpu->pc = vcpu->arch.pc;
  99. }
  100. /* Copy data touched by real-mode code from shadow vcpu back to vcpu */
  101. void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu,
  102. struct kvmppc_book3s_shadow_vcpu *svcpu)
  103. {
  104. vcpu->arch.gpr[0] = svcpu->gpr[0];
  105. vcpu->arch.gpr[1] = svcpu->gpr[1];
  106. vcpu->arch.gpr[2] = svcpu->gpr[2];
  107. vcpu->arch.gpr[3] = svcpu->gpr[3];
  108. vcpu->arch.gpr[4] = svcpu->gpr[4];
  109. vcpu->arch.gpr[5] = svcpu->gpr[5];
  110. vcpu->arch.gpr[6] = svcpu->gpr[6];
  111. vcpu->arch.gpr[7] = svcpu->gpr[7];
  112. vcpu->arch.gpr[8] = svcpu->gpr[8];
  113. vcpu->arch.gpr[9] = svcpu->gpr[9];
  114. vcpu->arch.gpr[10] = svcpu->gpr[10];
  115. vcpu->arch.gpr[11] = svcpu->gpr[11];
  116. vcpu->arch.gpr[12] = svcpu->gpr[12];
  117. vcpu->arch.gpr[13] = svcpu->gpr[13];
  118. vcpu->arch.cr = svcpu->cr;
  119. vcpu->arch.xer = svcpu->xer;
  120. vcpu->arch.ctr = svcpu->ctr;
  121. vcpu->arch.lr = svcpu->lr;
  122. vcpu->arch.pc = svcpu->pc;
  123. vcpu->arch.shadow_srr1 = svcpu->shadow_srr1;
  124. vcpu->arch.fault_dar = svcpu->fault_dar;
  125. vcpu->arch.fault_dsisr = svcpu->fault_dsisr;
  126. vcpu->arch.last_inst = svcpu->last_inst;
  127. }
  128. int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
  129. {
  130. int r = 1; /* Indicate we want to get back into the guest */
  131. /* We misuse TLB_FLUSH to indicate that we want to clear
  132. all shadow cache entries */
  133. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  134. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  135. return r;
  136. }
  137. /************* MMU Notifiers *************/
  138. static void do_kvm_unmap_hva(struct kvm *kvm, unsigned long start,
  139. unsigned long end)
  140. {
  141. long i;
  142. struct kvm_vcpu *vcpu;
  143. struct kvm_memslots *slots;
  144. struct kvm_memory_slot *memslot;
  145. slots = kvm_memslots(kvm);
  146. kvm_for_each_memslot(memslot, slots) {
  147. unsigned long hva_start, hva_end;
  148. gfn_t gfn, gfn_end;
  149. hva_start = max(start, memslot->userspace_addr);
  150. hva_end = min(end, memslot->userspace_addr +
  151. (memslot->npages << PAGE_SHIFT));
  152. if (hva_start >= hva_end)
  153. continue;
  154. /*
  155. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  156. * {gfn, gfn+1, ..., gfn_end-1}.
  157. */
  158. gfn = hva_to_gfn_memslot(hva_start, memslot);
  159. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  160. kvm_for_each_vcpu(i, vcpu, kvm)
  161. kvmppc_mmu_pte_pflush(vcpu, gfn << PAGE_SHIFT,
  162. gfn_end << PAGE_SHIFT);
  163. }
  164. }
  165. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  166. {
  167. trace_kvm_unmap_hva(hva);
  168. do_kvm_unmap_hva(kvm, hva, hva + PAGE_SIZE);
  169. return 0;
  170. }
  171. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  172. {
  173. do_kvm_unmap_hva(kvm, start, end);
  174. return 0;
  175. }
  176. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  177. {
  178. /* XXX could be more clever ;) */
  179. return 0;
  180. }
  181. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  182. {
  183. /* XXX could be more clever ;) */
  184. return 0;
  185. }
  186. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  187. {
  188. /* The page will get remapped properly on its next fault */
  189. do_kvm_unmap_hva(kvm, hva, hva + PAGE_SIZE);
  190. }
  191. /*****************************************/
  192. static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
  193. {
  194. ulong smsr = vcpu->arch.shared->msr;
  195. /* Guest MSR values */
  196. smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE;
  197. /* Process MSR values */
  198. smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
  199. /* External providers the guest reserved */
  200. smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
  201. /* 64-bit Process MSR values */
  202. #ifdef CONFIG_PPC_BOOK3S_64
  203. smsr |= MSR_ISF | MSR_HV;
  204. #endif
  205. vcpu->arch.shadow_msr = smsr;
  206. }
  207. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
  208. {
  209. ulong old_msr = vcpu->arch.shared->msr;
  210. #ifdef EXIT_DEBUG
  211. printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
  212. #endif
  213. msr &= to_book3s(vcpu)->msr_mask;
  214. vcpu->arch.shared->msr = msr;
  215. kvmppc_recalc_shadow_msr(vcpu);
  216. if (msr & MSR_POW) {
  217. if (!vcpu->arch.pending_exceptions) {
  218. kvm_vcpu_block(vcpu);
  219. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  220. vcpu->stat.halt_wakeup++;
  221. /* Unset POW bit after we woke up */
  222. msr &= ~MSR_POW;
  223. vcpu->arch.shared->msr = msr;
  224. }
  225. }
  226. if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
  227. (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
  228. kvmppc_mmu_flush_segments(vcpu);
  229. kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
  230. /* Preload magic page segment when in kernel mode */
  231. if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
  232. struct kvm_vcpu_arch *a = &vcpu->arch;
  233. if (msr & MSR_DR)
  234. kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
  235. else
  236. kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
  237. }
  238. }
  239. /*
  240. * When switching from 32 to 64-bit, we may have a stale 32-bit
  241. * magic page around, we need to flush it. Typically 32-bit magic
  242. * page will be instanciated when calling into RTAS. Note: We
  243. * assume that such transition only happens while in kernel mode,
  244. * ie, we never transition from user 32-bit to kernel 64-bit with
  245. * a 32-bit magic page around.
  246. */
  247. if (vcpu->arch.magic_page_pa &&
  248. !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) {
  249. /* going from RTAS to normal kernel code */
  250. kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa,
  251. ~0xFFFUL);
  252. }
  253. /* Preload FPU if it's enabled */
  254. if (vcpu->arch.shared->msr & MSR_FP)
  255. kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
  256. }
  257. void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
  258. {
  259. u32 host_pvr;
  260. vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
  261. vcpu->arch.pvr = pvr;
  262. #ifdef CONFIG_PPC_BOOK3S_64
  263. if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
  264. kvmppc_mmu_book3s_64_init(vcpu);
  265. if (!to_book3s(vcpu)->hior_explicit)
  266. to_book3s(vcpu)->hior = 0xfff00000;
  267. to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
  268. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  269. } else
  270. #endif
  271. {
  272. kvmppc_mmu_book3s_32_init(vcpu);
  273. if (!to_book3s(vcpu)->hior_explicit)
  274. to_book3s(vcpu)->hior = 0;
  275. to_book3s(vcpu)->msr_mask = 0xffffffffULL;
  276. vcpu->arch.cpu_type = KVM_CPU_3S_32;
  277. }
  278. kvmppc_sanity_check(vcpu);
  279. /* If we are in hypervisor level on 970, we can tell the CPU to
  280. * treat DCBZ as 32 bytes store */
  281. vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
  282. if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
  283. !strcmp(cur_cpu_spec->platform, "ppc970"))
  284. vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
  285. /* Cell performs badly if MSR_FEx are set. So let's hope nobody
  286. really needs them in a VM on Cell and force disable them. */
  287. if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
  288. to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
  289. /*
  290. * If they're asking for POWER6 or later, set the flag
  291. * indicating that we can do multiple large page sizes
  292. * and 1TB segments.
  293. * Also set the flag that indicates that tlbie has the large
  294. * page bit in the RB operand instead of the instruction.
  295. */
  296. switch (PVR_VER(pvr)) {
  297. case PVR_POWER6:
  298. case PVR_POWER7:
  299. case PVR_POWER7p:
  300. case PVR_POWER8:
  301. vcpu->arch.hflags |= BOOK3S_HFLAG_MULTI_PGSIZE |
  302. BOOK3S_HFLAG_NEW_TLBIE;
  303. break;
  304. }
  305. #ifdef CONFIG_PPC_BOOK3S_32
  306. /* 32 bit Book3S always has 32 byte dcbz */
  307. vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
  308. #endif
  309. /* On some CPUs we can execute paired single operations natively */
  310. asm ( "mfpvr %0" : "=r"(host_pvr));
  311. switch (host_pvr) {
  312. case 0x00080200: /* lonestar 2.0 */
  313. case 0x00088202: /* lonestar 2.2 */
  314. case 0x70000100: /* gekko 1.0 */
  315. case 0x00080100: /* gekko 2.0 */
  316. case 0x00083203: /* gekko 2.3a */
  317. case 0x00083213: /* gekko 2.3b */
  318. case 0x00083204: /* gekko 2.4 */
  319. case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
  320. case 0x00087200: /* broadway */
  321. vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
  322. /* Enable HID2.PSE - in case we need it later */
  323. mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
  324. }
  325. }
  326. /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
  327. * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
  328. * emulate 32 bytes dcbz length.
  329. *
  330. * The Book3s_64 inventors also realized this case and implemented a special bit
  331. * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
  332. *
  333. * My approach here is to patch the dcbz instruction on executing pages.
  334. */
  335. static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
  336. {
  337. struct page *hpage;
  338. u64 hpage_offset;
  339. u32 *page;
  340. int i;
  341. hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
  342. if (is_error_page(hpage))
  343. return;
  344. hpage_offset = pte->raddr & ~PAGE_MASK;
  345. hpage_offset &= ~0xFFFULL;
  346. hpage_offset /= 4;
  347. get_page(hpage);
  348. page = kmap_atomic(hpage);
  349. /* patch dcbz into reserved instruction, so we trap */
  350. for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
  351. if ((page[i] & 0xff0007ff) == INS_DCBZ)
  352. page[i] &= 0xfffffff7;
  353. kunmap_atomic(page);
  354. put_page(hpage);
  355. }
  356. static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  357. {
  358. ulong mp_pa = vcpu->arch.magic_page_pa;
  359. if (!(vcpu->arch.shared->msr & MSR_SF))
  360. mp_pa = (uint32_t)mp_pa;
  361. if (unlikely(mp_pa) &&
  362. unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
  363. return 1;
  364. }
  365. return kvm_is_visible_gfn(vcpu->kvm, gfn);
  366. }
  367. int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
  368. ulong eaddr, int vec)
  369. {
  370. bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
  371. bool iswrite = false;
  372. int r = RESUME_GUEST;
  373. int relocated;
  374. int page_found = 0;
  375. struct kvmppc_pte pte;
  376. bool is_mmio = false;
  377. bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
  378. bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
  379. u64 vsid;
  380. relocated = data ? dr : ir;
  381. if (data && (vcpu->arch.fault_dsisr & DSISR_ISSTORE))
  382. iswrite = true;
  383. /* Resolve real address if translation turned on */
  384. if (relocated) {
  385. page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data, iswrite);
  386. } else {
  387. pte.may_execute = true;
  388. pte.may_read = true;
  389. pte.may_write = true;
  390. pte.raddr = eaddr & KVM_PAM;
  391. pte.eaddr = eaddr;
  392. pte.vpage = eaddr >> 12;
  393. pte.page_size = MMU_PAGE_64K;
  394. }
  395. switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
  396. case 0:
  397. pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
  398. break;
  399. case MSR_DR:
  400. case MSR_IR:
  401. vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
  402. if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
  403. pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
  404. else
  405. pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
  406. pte.vpage |= vsid;
  407. if (vsid == -1)
  408. page_found = -EINVAL;
  409. break;
  410. }
  411. if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
  412. (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
  413. /*
  414. * If we do the dcbz hack, we have to NX on every execution,
  415. * so we can patch the executing code. This renders our guest
  416. * NX-less.
  417. */
  418. pte.may_execute = !data;
  419. }
  420. if (page_found == -ENOENT) {
  421. /* Page not found in guest PTE entries */
  422. vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
  423. vcpu->arch.shared->dsisr = vcpu->arch.fault_dsisr;
  424. vcpu->arch.shared->msr |=
  425. vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL;
  426. kvmppc_book3s_queue_irqprio(vcpu, vec);
  427. } else if (page_found == -EPERM) {
  428. /* Storage protection */
  429. vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
  430. vcpu->arch.shared->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
  431. vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
  432. vcpu->arch.shared->msr |=
  433. vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL;
  434. kvmppc_book3s_queue_irqprio(vcpu, vec);
  435. } else if (page_found == -EINVAL) {
  436. /* Page not found in guest SLB */
  437. vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
  438. kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
  439. } else if (!is_mmio &&
  440. kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
  441. if (data && !(vcpu->arch.fault_dsisr & DSISR_NOHPTE)) {
  442. /*
  443. * There is already a host HPTE there, presumably
  444. * a read-only one for a page the guest thinks
  445. * is writable, so get rid of it first.
  446. */
  447. kvmppc_mmu_unmap_page(vcpu, &pte);
  448. }
  449. /* The guest's PTE is not mapped yet. Map on the host */
  450. kvmppc_mmu_map_page(vcpu, &pte, iswrite);
  451. if (data)
  452. vcpu->stat.sp_storage++;
  453. else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
  454. (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
  455. kvmppc_patch_dcbz(vcpu, &pte);
  456. } else {
  457. /* MMIO */
  458. vcpu->stat.mmio_exits++;
  459. vcpu->arch.paddr_accessed = pte.raddr;
  460. vcpu->arch.vaddr_accessed = pte.eaddr;
  461. r = kvmppc_emulate_mmio(run, vcpu);
  462. if ( r == RESUME_HOST_NV )
  463. r = RESUME_HOST;
  464. }
  465. return r;
  466. }
  467. static inline int get_fpr_index(int i)
  468. {
  469. return i * TS_FPRWIDTH;
  470. }
  471. /* Give up external provider (FPU, Altivec, VSX) */
  472. void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
  473. {
  474. struct thread_struct *t = &current->thread;
  475. u64 *vcpu_fpr = vcpu->arch.fpr;
  476. #ifdef CONFIG_VSX
  477. u64 *vcpu_vsx = vcpu->arch.vsr;
  478. #endif
  479. u64 *thread_fpr = (u64*)t->fpr;
  480. int i;
  481. /*
  482. * VSX instructions can access FP and vector registers, so if
  483. * we are giving up VSX, make sure we give up FP and VMX as well.
  484. */
  485. if (msr & MSR_VSX)
  486. msr |= MSR_FP | MSR_VEC;
  487. msr &= vcpu->arch.guest_owned_ext;
  488. if (!msr)
  489. return;
  490. #ifdef DEBUG_EXT
  491. printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
  492. #endif
  493. if (msr & MSR_FP) {
  494. /*
  495. * Note that on CPUs with VSX, giveup_fpu stores
  496. * both the traditional FP registers and the added VSX
  497. * registers into thread.fpr[].
  498. */
  499. if (current->thread.regs->msr & MSR_FP)
  500. giveup_fpu(current);
  501. for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
  502. vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
  503. vcpu->arch.fpscr = t->fpscr.val;
  504. #ifdef CONFIG_VSX
  505. if (cpu_has_feature(CPU_FTR_VSX))
  506. for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
  507. vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
  508. #endif
  509. }
  510. #ifdef CONFIG_ALTIVEC
  511. if (msr & MSR_VEC) {
  512. if (current->thread.regs->msr & MSR_VEC)
  513. giveup_altivec(current);
  514. memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
  515. vcpu->arch.vscr = t->vscr;
  516. }
  517. #endif
  518. vcpu->arch.guest_owned_ext &= ~(msr | MSR_VSX);
  519. kvmppc_recalc_shadow_msr(vcpu);
  520. }
  521. static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
  522. {
  523. ulong srr0 = kvmppc_get_pc(vcpu);
  524. u32 last_inst = kvmppc_get_last_inst(vcpu);
  525. int ret;
  526. ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
  527. if (ret == -ENOENT) {
  528. ulong msr = vcpu->arch.shared->msr;
  529. msr = kvmppc_set_field(msr, 33, 33, 1);
  530. msr = kvmppc_set_field(msr, 34, 36, 0);
  531. vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
  532. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
  533. return EMULATE_AGAIN;
  534. }
  535. return EMULATE_DONE;
  536. }
  537. static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
  538. {
  539. /* Need to do paired single emulation? */
  540. if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
  541. return EMULATE_DONE;
  542. /* Read out the instruction */
  543. if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
  544. /* Need to emulate */
  545. return EMULATE_FAIL;
  546. return EMULATE_AGAIN;
  547. }
  548. /* Handle external providers (FPU, Altivec, VSX) */
  549. static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
  550. ulong msr)
  551. {
  552. struct thread_struct *t = &current->thread;
  553. u64 *vcpu_fpr = vcpu->arch.fpr;
  554. #ifdef CONFIG_VSX
  555. u64 *vcpu_vsx = vcpu->arch.vsr;
  556. #endif
  557. u64 *thread_fpr = (u64*)t->fpr;
  558. int i;
  559. /* When we have paired singles, we emulate in software */
  560. if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
  561. return RESUME_GUEST;
  562. if (!(vcpu->arch.shared->msr & msr)) {
  563. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  564. return RESUME_GUEST;
  565. }
  566. if (msr == MSR_VSX) {
  567. /* No VSX? Give an illegal instruction interrupt */
  568. #ifdef CONFIG_VSX
  569. if (!cpu_has_feature(CPU_FTR_VSX))
  570. #endif
  571. {
  572. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  573. return RESUME_GUEST;
  574. }
  575. /*
  576. * We have to load up all the FP and VMX registers before
  577. * we can let the guest use VSX instructions.
  578. */
  579. msr = MSR_FP | MSR_VEC | MSR_VSX;
  580. }
  581. /* See if we already own all the ext(s) needed */
  582. msr &= ~vcpu->arch.guest_owned_ext;
  583. if (!msr)
  584. return RESUME_GUEST;
  585. #ifdef DEBUG_EXT
  586. printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
  587. #endif
  588. if (msr & MSR_FP) {
  589. for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
  590. thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
  591. #ifdef CONFIG_VSX
  592. for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
  593. thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
  594. #endif
  595. t->fpscr.val = vcpu->arch.fpscr;
  596. t->fpexc_mode = 0;
  597. kvmppc_load_up_fpu();
  598. }
  599. if (msr & MSR_VEC) {
  600. #ifdef CONFIG_ALTIVEC
  601. memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
  602. t->vscr = vcpu->arch.vscr;
  603. t->vrsave = -1;
  604. kvmppc_load_up_altivec();
  605. #endif
  606. }
  607. current->thread.regs->msr |= msr;
  608. vcpu->arch.guest_owned_ext |= msr;
  609. kvmppc_recalc_shadow_msr(vcpu);
  610. return RESUME_GUEST;
  611. }
  612. /*
  613. * Kernel code using FP or VMX could have flushed guest state to
  614. * the thread_struct; if so, get it back now.
  615. */
  616. static void kvmppc_handle_lost_ext(struct kvm_vcpu *vcpu)
  617. {
  618. unsigned long lost_ext;
  619. lost_ext = vcpu->arch.guest_owned_ext & ~current->thread.regs->msr;
  620. if (!lost_ext)
  621. return;
  622. if (lost_ext & MSR_FP)
  623. kvmppc_load_up_fpu();
  624. #ifdef CONFIG_ALTIVEC
  625. if (lost_ext & MSR_VEC)
  626. kvmppc_load_up_altivec();
  627. #endif
  628. current->thread.regs->msr |= lost_ext;
  629. }
  630. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  631. unsigned int exit_nr)
  632. {
  633. int r = RESUME_HOST;
  634. int s;
  635. vcpu->stat.sum_exits++;
  636. run->exit_reason = KVM_EXIT_UNKNOWN;
  637. run->ready_for_interrupt_injection = 1;
  638. /* We get here with MSR.EE=1 */
  639. trace_kvm_exit(exit_nr, vcpu);
  640. kvm_guest_exit();
  641. switch (exit_nr) {
  642. case BOOK3S_INTERRUPT_INST_STORAGE:
  643. {
  644. ulong shadow_srr1 = vcpu->arch.shadow_srr1;
  645. vcpu->stat.pf_instruc++;
  646. #ifdef CONFIG_PPC_BOOK3S_32
  647. /* We set segments as unused segments when invalidating them. So
  648. * treat the respective fault as segment fault. */
  649. {
  650. struct kvmppc_book3s_shadow_vcpu *svcpu;
  651. u32 sr;
  652. svcpu = svcpu_get(vcpu);
  653. sr = svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT];
  654. svcpu_put(svcpu);
  655. if (sr == SR_INVALID) {
  656. kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
  657. r = RESUME_GUEST;
  658. break;
  659. }
  660. }
  661. #endif
  662. /* only care about PTEG not found errors, but leave NX alone */
  663. if (shadow_srr1 & 0x40000000) {
  664. int idx = srcu_read_lock(&vcpu->kvm->srcu);
  665. r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
  666. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  667. vcpu->stat.sp_instruc++;
  668. } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
  669. (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
  670. /*
  671. * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
  672. * so we can't use the NX bit inside the guest. Let's cross our fingers,
  673. * that no guest that needs the dcbz hack does NX.
  674. */
  675. kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
  676. r = RESUME_GUEST;
  677. } else {
  678. vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
  679. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  680. r = RESUME_GUEST;
  681. }
  682. break;
  683. }
  684. case BOOK3S_INTERRUPT_DATA_STORAGE:
  685. {
  686. ulong dar = kvmppc_get_fault_dar(vcpu);
  687. u32 fault_dsisr = vcpu->arch.fault_dsisr;
  688. vcpu->stat.pf_storage++;
  689. #ifdef CONFIG_PPC_BOOK3S_32
  690. /* We set segments as unused segments when invalidating them. So
  691. * treat the respective fault as segment fault. */
  692. {
  693. struct kvmppc_book3s_shadow_vcpu *svcpu;
  694. u32 sr;
  695. svcpu = svcpu_get(vcpu);
  696. sr = svcpu->sr[dar >> SID_SHIFT];
  697. svcpu_put(svcpu);
  698. if (sr == SR_INVALID) {
  699. kvmppc_mmu_map_segment(vcpu, dar);
  700. r = RESUME_GUEST;
  701. break;
  702. }
  703. }
  704. #endif
  705. /*
  706. * We need to handle missing shadow PTEs, and
  707. * protection faults due to us mapping a page read-only
  708. * when the guest thinks it is writable.
  709. */
  710. if (fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT)) {
  711. int idx = srcu_read_lock(&vcpu->kvm->srcu);
  712. r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
  713. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  714. } else {
  715. vcpu->arch.shared->dar = dar;
  716. vcpu->arch.shared->dsisr = fault_dsisr;
  717. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  718. r = RESUME_GUEST;
  719. }
  720. break;
  721. }
  722. case BOOK3S_INTERRUPT_DATA_SEGMENT:
  723. if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
  724. vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
  725. kvmppc_book3s_queue_irqprio(vcpu,
  726. BOOK3S_INTERRUPT_DATA_SEGMENT);
  727. }
  728. r = RESUME_GUEST;
  729. break;
  730. case BOOK3S_INTERRUPT_INST_SEGMENT:
  731. if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
  732. kvmppc_book3s_queue_irqprio(vcpu,
  733. BOOK3S_INTERRUPT_INST_SEGMENT);
  734. }
  735. r = RESUME_GUEST;
  736. break;
  737. /* We're good on these - the host merely wanted to get our attention */
  738. case BOOK3S_INTERRUPT_DECREMENTER:
  739. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  740. vcpu->stat.dec_exits++;
  741. r = RESUME_GUEST;
  742. break;
  743. case BOOK3S_INTERRUPT_EXTERNAL:
  744. case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
  745. case BOOK3S_INTERRUPT_EXTERNAL_HV:
  746. vcpu->stat.ext_intr_exits++;
  747. r = RESUME_GUEST;
  748. break;
  749. case BOOK3S_INTERRUPT_PERFMON:
  750. r = RESUME_GUEST;
  751. break;
  752. case BOOK3S_INTERRUPT_PROGRAM:
  753. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  754. {
  755. enum emulation_result er;
  756. ulong flags;
  757. program_interrupt:
  758. flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
  759. if (vcpu->arch.shared->msr & MSR_PR) {
  760. #ifdef EXIT_DEBUG
  761. printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
  762. #endif
  763. if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
  764. (INS_DCBZ & 0xfffffff7)) {
  765. kvmppc_core_queue_program(vcpu, flags);
  766. r = RESUME_GUEST;
  767. break;
  768. }
  769. }
  770. vcpu->stat.emulated_inst_exits++;
  771. er = kvmppc_emulate_instruction(run, vcpu);
  772. switch (er) {
  773. case EMULATE_DONE:
  774. r = RESUME_GUEST_NV;
  775. break;
  776. case EMULATE_AGAIN:
  777. r = RESUME_GUEST;
  778. break;
  779. case EMULATE_FAIL:
  780. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  781. __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
  782. kvmppc_core_queue_program(vcpu, flags);
  783. r = RESUME_GUEST;
  784. break;
  785. case EMULATE_DO_MMIO:
  786. run->exit_reason = KVM_EXIT_MMIO;
  787. r = RESUME_HOST_NV;
  788. break;
  789. case EMULATE_EXIT_USER:
  790. r = RESUME_HOST_NV;
  791. break;
  792. default:
  793. BUG();
  794. }
  795. break;
  796. }
  797. case BOOK3S_INTERRUPT_SYSCALL:
  798. if (vcpu->arch.papr_enabled &&
  799. (kvmppc_get_last_sc(vcpu) == 0x44000022) &&
  800. !(vcpu->arch.shared->msr & MSR_PR)) {
  801. /* SC 1 papr hypercalls */
  802. ulong cmd = kvmppc_get_gpr(vcpu, 3);
  803. int i;
  804. #ifdef CONFIG_KVM_BOOK3S_64_PR
  805. if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
  806. r = RESUME_GUEST;
  807. break;
  808. }
  809. #endif
  810. run->papr_hcall.nr = cmd;
  811. for (i = 0; i < 9; ++i) {
  812. ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
  813. run->papr_hcall.args[i] = gpr;
  814. }
  815. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  816. vcpu->arch.hcall_needed = 1;
  817. r = RESUME_HOST;
  818. } else if (vcpu->arch.osi_enabled &&
  819. (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
  820. (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
  821. /* MOL hypercalls */
  822. u64 *gprs = run->osi.gprs;
  823. int i;
  824. run->exit_reason = KVM_EXIT_OSI;
  825. for (i = 0; i < 32; i++)
  826. gprs[i] = kvmppc_get_gpr(vcpu, i);
  827. vcpu->arch.osi_needed = 1;
  828. r = RESUME_HOST_NV;
  829. } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
  830. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  831. /* KVM PV hypercalls */
  832. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  833. r = RESUME_GUEST;
  834. } else {
  835. /* Guest syscalls */
  836. vcpu->stat.syscall_exits++;
  837. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  838. r = RESUME_GUEST;
  839. }
  840. break;
  841. case BOOK3S_INTERRUPT_FP_UNAVAIL:
  842. case BOOK3S_INTERRUPT_ALTIVEC:
  843. case BOOK3S_INTERRUPT_VSX:
  844. {
  845. int ext_msr = 0;
  846. switch (exit_nr) {
  847. case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
  848. case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
  849. case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
  850. }
  851. switch (kvmppc_check_ext(vcpu, exit_nr)) {
  852. case EMULATE_DONE:
  853. /* everything ok - let's enable the ext */
  854. r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
  855. break;
  856. case EMULATE_FAIL:
  857. /* we need to emulate this instruction */
  858. goto program_interrupt;
  859. break;
  860. default:
  861. /* nothing to worry about - go again */
  862. break;
  863. }
  864. break;
  865. }
  866. case BOOK3S_INTERRUPT_ALIGNMENT:
  867. if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
  868. vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
  869. kvmppc_get_last_inst(vcpu));
  870. vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
  871. kvmppc_get_last_inst(vcpu));
  872. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  873. }
  874. r = RESUME_GUEST;
  875. break;
  876. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  877. case BOOK3S_INTERRUPT_TRACE:
  878. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  879. r = RESUME_GUEST;
  880. break;
  881. default:
  882. {
  883. ulong shadow_srr1 = vcpu->arch.shadow_srr1;
  884. /* Ugh - bork here! What did we get? */
  885. printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
  886. exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
  887. r = RESUME_HOST;
  888. BUG();
  889. break;
  890. }
  891. }
  892. if (!(r & RESUME_HOST)) {
  893. /* To avoid clobbering exit_reason, only check for signals if
  894. * we aren't already exiting to userspace for some other
  895. * reason. */
  896. /*
  897. * Interrupts could be timers for the guest which we have to
  898. * inject again, so let's postpone them until we're in the guest
  899. * and if we really did time things so badly, then we just exit
  900. * again due to a host external interrupt.
  901. */
  902. local_irq_disable();
  903. s = kvmppc_prepare_to_enter(vcpu);
  904. if (s <= 0) {
  905. local_irq_enable();
  906. r = s;
  907. } else {
  908. kvmppc_fix_ee_before_entry();
  909. }
  910. kvmppc_handle_lost_ext(vcpu);
  911. }
  912. trace_kvm_book3s_reenter(r, vcpu);
  913. return r;
  914. }
  915. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  916. struct kvm_sregs *sregs)
  917. {
  918. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  919. int i;
  920. sregs->pvr = vcpu->arch.pvr;
  921. sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
  922. if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
  923. for (i = 0; i < 64; i++) {
  924. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
  925. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  926. }
  927. } else {
  928. for (i = 0; i < 16; i++)
  929. sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
  930. for (i = 0; i < 8; i++) {
  931. sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
  932. sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
  933. }
  934. }
  935. return 0;
  936. }
  937. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  938. struct kvm_sregs *sregs)
  939. {
  940. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  941. int i;
  942. kvmppc_set_pvr(vcpu, sregs->pvr);
  943. vcpu3s->sdr1 = sregs->u.s.sdr1;
  944. if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
  945. for (i = 0; i < 64; i++) {
  946. vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
  947. sregs->u.s.ppc64.slb[i].slbe);
  948. }
  949. } else {
  950. for (i = 0; i < 16; i++) {
  951. vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
  952. }
  953. for (i = 0; i < 8; i++) {
  954. kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
  955. (u32)sregs->u.s.ppc32.ibat[i]);
  956. kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
  957. (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
  958. kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
  959. (u32)sregs->u.s.ppc32.dbat[i]);
  960. kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
  961. (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
  962. }
  963. }
  964. /* Flush the MMU after messing with the segments */
  965. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  966. return 0;
  967. }
  968. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  969. {
  970. int r = 0;
  971. switch (id) {
  972. case KVM_REG_PPC_HIOR:
  973. *val = get_reg_val(id, to_book3s(vcpu)->hior);
  974. break;
  975. #ifdef CONFIG_VSX
  976. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
  977. long int i = id - KVM_REG_PPC_VSR0;
  978. if (!cpu_has_feature(CPU_FTR_VSX)) {
  979. r = -ENXIO;
  980. break;
  981. }
  982. val->vsxval[0] = vcpu->arch.fpr[i];
  983. val->vsxval[1] = vcpu->arch.vsr[i];
  984. break;
  985. }
  986. #endif /* CONFIG_VSX */
  987. default:
  988. r = -EINVAL;
  989. break;
  990. }
  991. return r;
  992. }
  993. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  994. {
  995. int r = 0;
  996. switch (id) {
  997. case KVM_REG_PPC_HIOR:
  998. to_book3s(vcpu)->hior = set_reg_val(id, *val);
  999. to_book3s(vcpu)->hior_explicit = true;
  1000. break;
  1001. #ifdef CONFIG_VSX
  1002. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
  1003. long int i = id - KVM_REG_PPC_VSR0;
  1004. if (!cpu_has_feature(CPU_FTR_VSX)) {
  1005. r = -ENXIO;
  1006. break;
  1007. }
  1008. vcpu->arch.fpr[i] = val->vsxval[0];
  1009. vcpu->arch.vsr[i] = val->vsxval[1];
  1010. break;
  1011. }
  1012. #endif /* CONFIG_VSX */
  1013. default:
  1014. r = -EINVAL;
  1015. break;
  1016. }
  1017. return r;
  1018. }
  1019. int kvmppc_core_check_processor_compat(void)
  1020. {
  1021. return 0;
  1022. }
  1023. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  1024. {
  1025. struct kvmppc_vcpu_book3s *vcpu_book3s;
  1026. struct kvm_vcpu *vcpu;
  1027. int err = -ENOMEM;
  1028. unsigned long p;
  1029. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1030. if (!vcpu)
  1031. goto out;
  1032. vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
  1033. if (!vcpu_book3s)
  1034. goto free_vcpu;
  1035. vcpu->arch.book3s = vcpu_book3s;
  1036. #ifdef CONFIG_KVM_BOOK3S_32
  1037. vcpu->arch.shadow_vcpu =
  1038. kzalloc(sizeof(*vcpu->arch.shadow_vcpu), GFP_KERNEL);
  1039. if (!vcpu->arch.shadow_vcpu)
  1040. goto free_vcpu3s;
  1041. #endif
  1042. err = kvm_vcpu_init(vcpu, kvm, id);
  1043. if (err)
  1044. goto free_shadow_vcpu;
  1045. err = -ENOMEM;
  1046. p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
  1047. if (!p)
  1048. goto uninit_vcpu;
  1049. /* the real shared page fills the last 4k of our page */
  1050. vcpu->arch.shared = (void *)(p + PAGE_SIZE - 4096);
  1051. #ifdef CONFIG_PPC_BOOK3S_64
  1052. /*
  1053. * Default to the same as the host if we're on sufficiently
  1054. * recent machine that we have 1TB segments;
  1055. * otherwise default to PPC970FX.
  1056. */
  1057. vcpu->arch.pvr = 0x3C0301;
  1058. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1059. vcpu->arch.pvr = mfspr(SPRN_PVR);
  1060. #else
  1061. /* default to book3s_32 (750) */
  1062. vcpu->arch.pvr = 0x84202;
  1063. #endif
  1064. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  1065. vcpu->arch.slb_nr = 64;
  1066. vcpu->arch.shadow_msr = MSR_USER64;
  1067. err = kvmppc_mmu_init(vcpu);
  1068. if (err < 0)
  1069. goto uninit_vcpu;
  1070. return vcpu;
  1071. uninit_vcpu:
  1072. kvm_vcpu_uninit(vcpu);
  1073. free_shadow_vcpu:
  1074. #ifdef CONFIG_KVM_BOOK3S_32
  1075. kfree(vcpu->arch.shadow_vcpu);
  1076. free_vcpu3s:
  1077. #endif
  1078. vfree(vcpu_book3s);
  1079. free_vcpu:
  1080. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1081. out:
  1082. return ERR_PTR(err);
  1083. }
  1084. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  1085. {
  1086. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  1087. free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
  1088. kvm_vcpu_uninit(vcpu);
  1089. #ifdef CONFIG_KVM_BOOK3S_32
  1090. kfree(vcpu->arch.shadow_vcpu);
  1091. #endif
  1092. vfree(vcpu_book3s);
  1093. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1094. }
  1095. int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1096. {
  1097. int ret;
  1098. double fpr[32][TS_FPRWIDTH];
  1099. unsigned int fpscr;
  1100. int fpexc_mode;
  1101. #ifdef CONFIG_ALTIVEC
  1102. vector128 vr[32];
  1103. vector128 vscr;
  1104. unsigned long uninitialized_var(vrsave);
  1105. int used_vr;
  1106. #endif
  1107. #ifdef CONFIG_VSX
  1108. int used_vsr;
  1109. #endif
  1110. ulong ext_msr;
  1111. /* Check if we can run the vcpu at all */
  1112. if (!vcpu->arch.sane) {
  1113. kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1114. ret = -EINVAL;
  1115. goto out;
  1116. }
  1117. /*
  1118. * Interrupts could be timers for the guest which we have to inject
  1119. * again, so let's postpone them until we're in the guest and if we
  1120. * really did time things so badly, then we just exit again due to
  1121. * a host external interrupt.
  1122. */
  1123. local_irq_disable();
  1124. ret = kvmppc_prepare_to_enter(vcpu);
  1125. if (ret <= 0) {
  1126. local_irq_enable();
  1127. goto out;
  1128. }
  1129. /* Save FPU state in stack */
  1130. if (current->thread.regs->msr & MSR_FP)
  1131. giveup_fpu(current);
  1132. memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
  1133. fpscr = current->thread.fpscr.val;
  1134. fpexc_mode = current->thread.fpexc_mode;
  1135. #ifdef CONFIG_ALTIVEC
  1136. /* Save Altivec state in stack */
  1137. used_vr = current->thread.used_vr;
  1138. if (used_vr) {
  1139. if (current->thread.regs->msr & MSR_VEC)
  1140. giveup_altivec(current);
  1141. memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
  1142. vscr = current->thread.vscr;
  1143. vrsave = current->thread.vrsave;
  1144. }
  1145. #endif
  1146. #ifdef CONFIG_VSX
  1147. /* Save VSX state in stack */
  1148. used_vsr = current->thread.used_vsr;
  1149. if (used_vsr && (current->thread.regs->msr & MSR_VSX))
  1150. __giveup_vsx(current);
  1151. #endif
  1152. /* Remember the MSR with disabled extensions */
  1153. ext_msr = current->thread.regs->msr;
  1154. /* Preload FPU if it's enabled */
  1155. if (vcpu->arch.shared->msr & MSR_FP)
  1156. kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
  1157. kvmppc_fix_ee_before_entry();
  1158. ret = __kvmppc_vcpu_run(kvm_run, vcpu);
  1159. /* No need for kvm_guest_exit. It's done in handle_exit.
  1160. We also get here with interrupts enabled. */
  1161. /* Make sure we save the guest FPU/Altivec/VSX state */
  1162. kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
  1163. current->thread.regs->msr = ext_msr;
  1164. /* Restore FPU/VSX state from stack */
  1165. memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
  1166. current->thread.fpscr.val = fpscr;
  1167. current->thread.fpexc_mode = fpexc_mode;
  1168. #ifdef CONFIG_ALTIVEC
  1169. /* Restore Altivec state from stack */
  1170. if (used_vr && current->thread.used_vr) {
  1171. memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
  1172. current->thread.vscr = vscr;
  1173. current->thread.vrsave = vrsave;
  1174. }
  1175. current->thread.used_vr = used_vr;
  1176. #endif
  1177. #ifdef CONFIG_VSX
  1178. current->thread.used_vsr = used_vsr;
  1179. #endif
  1180. out:
  1181. vcpu->mode = OUTSIDE_GUEST_MODE;
  1182. return ret;
  1183. }
  1184. /*
  1185. * Get (and clear) the dirty memory log for a memory slot.
  1186. */
  1187. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1188. struct kvm_dirty_log *log)
  1189. {
  1190. struct kvm_memory_slot *memslot;
  1191. struct kvm_vcpu *vcpu;
  1192. ulong ga, ga_end;
  1193. int is_dirty = 0;
  1194. int r;
  1195. unsigned long n;
  1196. mutex_lock(&kvm->slots_lock);
  1197. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1198. if (r)
  1199. goto out;
  1200. /* If nothing is dirty, don't bother messing with page tables. */
  1201. if (is_dirty) {
  1202. memslot = id_to_memslot(kvm->memslots, log->slot);
  1203. ga = memslot->base_gfn << PAGE_SHIFT;
  1204. ga_end = ga + (memslot->npages << PAGE_SHIFT);
  1205. kvm_for_each_vcpu(n, vcpu, kvm)
  1206. kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
  1207. n = kvm_dirty_bitmap_bytes(memslot);
  1208. memset(memslot->dirty_bitmap, 0, n);
  1209. }
  1210. r = 0;
  1211. out:
  1212. mutex_unlock(&kvm->slots_lock);
  1213. return r;
  1214. }
  1215. #ifdef CONFIG_PPC64
  1216. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1217. {
  1218. long int i;
  1219. struct kvm_vcpu *vcpu;
  1220. info->flags = 0;
  1221. /* SLB is always 64 entries */
  1222. info->slb_size = 64;
  1223. /* Standard 4k base page size segment */
  1224. info->sps[0].page_shift = 12;
  1225. info->sps[0].slb_enc = 0;
  1226. info->sps[0].enc[0].page_shift = 12;
  1227. info->sps[0].enc[0].pte_enc = 0;
  1228. /*
  1229. * 64k large page size.
  1230. * We only want to put this in if the CPUs we're emulating
  1231. * support it, but unfortunately we don't have a vcpu easily
  1232. * to hand here to test. Just pick the first vcpu, and if
  1233. * that doesn't exist yet, report the minimum capability,
  1234. * i.e., no 64k pages.
  1235. * 1T segment support goes along with 64k pages.
  1236. */
  1237. i = 1;
  1238. vcpu = kvm_get_vcpu(kvm, 0);
  1239. if (vcpu && (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) {
  1240. info->flags = KVM_PPC_1T_SEGMENTS;
  1241. info->sps[i].page_shift = 16;
  1242. info->sps[i].slb_enc = SLB_VSID_L | SLB_VSID_LP_01;
  1243. info->sps[i].enc[0].page_shift = 16;
  1244. info->sps[i].enc[0].pte_enc = 1;
  1245. ++i;
  1246. }
  1247. /* Standard 16M large page size segment */
  1248. info->sps[i].page_shift = 24;
  1249. info->sps[i].slb_enc = SLB_VSID_L;
  1250. info->sps[i].enc[0].page_shift = 24;
  1251. info->sps[i].enc[0].pte_enc = 0;
  1252. return 0;
  1253. }
  1254. #endif /* CONFIG_PPC64 */
  1255. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1256. struct kvm_memory_slot *dont)
  1257. {
  1258. }
  1259. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1260. unsigned long npages)
  1261. {
  1262. return 0;
  1263. }
  1264. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1265. struct kvm_memory_slot *memslot,
  1266. struct kvm_userspace_memory_region *mem)
  1267. {
  1268. return 0;
  1269. }
  1270. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1271. struct kvm_userspace_memory_region *mem,
  1272. const struct kvm_memory_slot *old)
  1273. {
  1274. }
  1275. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1276. {
  1277. }
  1278. static unsigned int kvm_global_user_count = 0;
  1279. static DEFINE_SPINLOCK(kvm_global_user_count_lock);
  1280. int kvmppc_core_init_vm(struct kvm *kvm)
  1281. {
  1282. #ifdef CONFIG_PPC64
  1283. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1284. INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
  1285. #endif
  1286. mutex_init(&kvm->arch.hpt_mutex);
  1287. if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
  1288. spin_lock(&kvm_global_user_count_lock);
  1289. if (++kvm_global_user_count == 1)
  1290. pSeries_disable_reloc_on_exc();
  1291. spin_unlock(&kvm_global_user_count_lock);
  1292. }
  1293. return 0;
  1294. }
  1295. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1296. {
  1297. #ifdef CONFIG_PPC64
  1298. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1299. #endif
  1300. if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
  1301. spin_lock(&kvm_global_user_count_lock);
  1302. BUG_ON(kvm_global_user_count == 0);
  1303. if (--kvm_global_user_count == 0)
  1304. pSeries_enable_reloc_on_exc();
  1305. spin_unlock(&kvm_global_user_count_lock);
  1306. }
  1307. }
  1308. static int kvmppc_book3s_init(void)
  1309. {
  1310. int r;
  1311. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1312. if (r)
  1313. return r;
  1314. r = kvmppc_mmu_hpte_sysinit();
  1315. return r;
  1316. }
  1317. static void kvmppc_book3s_exit(void)
  1318. {
  1319. kvmppc_mmu_hpte_sysexit();
  1320. kvm_exit();
  1321. }
  1322. module_init(kvmppc_book3s_init);
  1323. module_exit(kvmppc_book3s_exit);