book3s_pr.c 35 KB

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