book3s_pr.c 45 KB

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