book3s_pr.c 41 KB

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