book3s_pr.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_pr,
  1329. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_pr,
  1330. .get_one_reg = kvmppc_get_one_reg_pr,
  1331. .set_one_reg = kvmppc_set_one_reg_pr,
  1332. .vcpu_load = kvmppc_core_vcpu_load_pr,
  1333. .vcpu_put = kvmppc_core_vcpu_put_pr,
  1334. .set_msr = kvmppc_set_msr_pr,
  1335. .vcpu_run = kvmppc_vcpu_run_pr,
  1336. .vcpu_create = kvmppc_core_vcpu_create_pr,
  1337. .vcpu_free = kvmppc_core_vcpu_free_pr,
  1338. .check_requests = kvmppc_core_check_requests_pr,
  1339. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_pr,
  1340. .flush_memslot = kvmppc_core_flush_memslot_pr,
  1341. .prepare_memory_region = kvmppc_core_prepare_memory_region_pr,
  1342. .commit_memory_region = kvmppc_core_commit_memory_region_pr,
  1343. .unmap_hva = kvm_unmap_hva_pr,
  1344. .unmap_hva_range = kvm_unmap_hva_range_pr,
  1345. .age_hva = kvm_age_hva_pr,
  1346. .test_age_hva = kvm_test_age_hva_pr,
  1347. .set_spte_hva = kvm_set_spte_hva_pr,
  1348. .mmu_destroy = kvmppc_mmu_destroy_pr,
  1349. .free_memslot = kvmppc_core_free_memslot_pr,
  1350. .create_memslot = kvmppc_core_create_memslot_pr,
  1351. .init_vm = kvmppc_core_init_vm_pr,
  1352. .destroy_vm = kvmppc_core_destroy_vm_pr,
  1353. .check_processor_compat = kvmppc_core_check_processor_compat_pr,
  1354. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_pr,
  1355. .emulate_op = kvmppc_core_emulate_op_pr,
  1356. .emulate_mtspr = kvmppc_core_emulate_mtspr_pr,
  1357. .emulate_mfspr = kvmppc_core_emulate_mfspr_pr,
  1358. .fast_vcpu_kick = kvm_vcpu_kick,
  1359. .arch_vm_ioctl = kvm_arch_vm_ioctl_pr,
  1360. };
  1361. static int kvmppc_book3s_init_pr(void)
  1362. {
  1363. int r;
  1364. r = kvm_init(&kvmppc_pr_ops, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1365. if (r)
  1366. return r;
  1367. r = kvmppc_mmu_hpte_sysinit();
  1368. return r;
  1369. }
  1370. static void kvmppc_book3s_exit_pr(void)
  1371. {
  1372. kvmppc_mmu_hpte_sysexit();
  1373. kvm_exit();
  1374. }
  1375. module_init(kvmppc_book3s_init_pr);
  1376. module_exit(kvmppc_book3s_exit_pr);