book3s_pr.c 41 KB

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