book3s_pr.c 41 KB

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