book3s_pr.c 37 KB

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