book3s_pr.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. giveup_fpu(current);
  406. for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
  407. vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
  408. vcpu->arch.fpscr = t->fpscr.val;
  409. #ifdef CONFIG_VSX
  410. if (cpu_has_feature(CPU_FTR_VSX))
  411. for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
  412. vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
  413. #endif
  414. }
  415. #ifdef CONFIG_ALTIVEC
  416. if (msr & MSR_VEC) {
  417. giveup_altivec(current);
  418. memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
  419. vcpu->arch.vscr = t->vscr;
  420. }
  421. #endif
  422. vcpu->arch.guest_owned_ext &= ~(msr | MSR_VSX);
  423. kvmppc_recalc_shadow_msr(vcpu);
  424. }
  425. static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
  426. {
  427. ulong srr0 = kvmppc_get_pc(vcpu);
  428. u32 last_inst = kvmppc_get_last_inst(vcpu);
  429. int ret;
  430. ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
  431. if (ret == -ENOENT) {
  432. ulong msr = vcpu->arch.shared->msr;
  433. msr = kvmppc_set_field(msr, 33, 33, 1);
  434. msr = kvmppc_set_field(msr, 34, 36, 0);
  435. vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
  436. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
  437. return EMULATE_AGAIN;
  438. }
  439. return EMULATE_DONE;
  440. }
  441. static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
  442. {
  443. /* Need to do paired single emulation? */
  444. if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
  445. return EMULATE_DONE;
  446. /* Read out the instruction */
  447. if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
  448. /* Need to emulate */
  449. return EMULATE_FAIL;
  450. return EMULATE_AGAIN;
  451. }
  452. /* Handle external providers (FPU, Altivec, VSX) */
  453. static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
  454. ulong msr)
  455. {
  456. struct thread_struct *t = &current->thread;
  457. u64 *vcpu_fpr = vcpu->arch.fpr;
  458. #ifdef CONFIG_VSX
  459. u64 *vcpu_vsx = vcpu->arch.vsr;
  460. #endif
  461. u64 *thread_fpr = (u64*)t->fpr;
  462. int i;
  463. /* When we have paired singles, we emulate in software */
  464. if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
  465. return RESUME_GUEST;
  466. if (!(vcpu->arch.shared->msr & msr)) {
  467. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  468. return RESUME_GUEST;
  469. }
  470. if (msr == MSR_VSX) {
  471. /* No VSX? Give an illegal instruction interrupt */
  472. #ifdef CONFIG_VSX
  473. if (!cpu_has_feature(CPU_FTR_VSX))
  474. #endif
  475. {
  476. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  477. return RESUME_GUEST;
  478. }
  479. /*
  480. * We have to load up all the FP and VMX registers before
  481. * we can let the guest use VSX instructions.
  482. */
  483. msr = MSR_FP | MSR_VEC | MSR_VSX;
  484. }
  485. /* See if we already own all the ext(s) needed */
  486. msr &= ~vcpu->arch.guest_owned_ext;
  487. if (!msr)
  488. return RESUME_GUEST;
  489. #ifdef DEBUG_EXT
  490. printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
  491. #endif
  492. current->thread.regs->msr |= msr;
  493. if (msr & MSR_FP) {
  494. for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
  495. thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
  496. #ifdef CONFIG_VSX
  497. for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr) / 2; i++)
  498. thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
  499. #endif
  500. t->fpscr.val = vcpu->arch.fpscr;
  501. t->fpexc_mode = 0;
  502. kvmppc_load_up_fpu();
  503. }
  504. if (msr & MSR_VEC) {
  505. #ifdef CONFIG_ALTIVEC
  506. memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
  507. t->vscr = vcpu->arch.vscr;
  508. t->vrsave = -1;
  509. kvmppc_load_up_altivec();
  510. #endif
  511. }
  512. vcpu->arch.guest_owned_ext |= msr;
  513. kvmppc_recalc_shadow_msr(vcpu);
  514. return RESUME_GUEST;
  515. }
  516. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  517. unsigned int exit_nr)
  518. {
  519. int r = RESUME_HOST;
  520. int s;
  521. vcpu->stat.sum_exits++;
  522. run->exit_reason = KVM_EXIT_UNKNOWN;
  523. run->ready_for_interrupt_injection = 1;
  524. /* We get here with MSR.EE=1 */
  525. trace_kvm_exit(exit_nr, vcpu);
  526. kvm_guest_exit();
  527. switch (exit_nr) {
  528. case BOOK3S_INTERRUPT_INST_STORAGE:
  529. {
  530. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  531. ulong shadow_srr1 = svcpu->shadow_srr1;
  532. vcpu->stat.pf_instruc++;
  533. #ifdef CONFIG_PPC_BOOK3S_32
  534. /* We set segments as unused segments when invalidating them. So
  535. * treat the respective fault as segment fault. */
  536. if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) {
  537. kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
  538. r = RESUME_GUEST;
  539. svcpu_put(svcpu);
  540. break;
  541. }
  542. #endif
  543. svcpu_put(svcpu);
  544. /* only care about PTEG not found errors, but leave NX alone */
  545. if (shadow_srr1 & 0x40000000) {
  546. r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
  547. vcpu->stat.sp_instruc++;
  548. } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
  549. (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
  550. /*
  551. * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
  552. * so we can't use the NX bit inside the guest. Let's cross our fingers,
  553. * that no guest that needs the dcbz hack does NX.
  554. */
  555. kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
  556. r = RESUME_GUEST;
  557. } else {
  558. vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000;
  559. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  560. r = RESUME_GUEST;
  561. }
  562. break;
  563. }
  564. case BOOK3S_INTERRUPT_DATA_STORAGE:
  565. {
  566. ulong dar = kvmppc_get_fault_dar(vcpu);
  567. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  568. u32 fault_dsisr = svcpu->fault_dsisr;
  569. vcpu->stat.pf_storage++;
  570. #ifdef CONFIG_PPC_BOOK3S_32
  571. /* We set segments as unused segments when invalidating them. So
  572. * treat the respective fault as segment fault. */
  573. if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) {
  574. kvmppc_mmu_map_segment(vcpu, dar);
  575. r = RESUME_GUEST;
  576. svcpu_put(svcpu);
  577. break;
  578. }
  579. #endif
  580. svcpu_put(svcpu);
  581. /* The only case we need to handle is missing shadow PTEs */
  582. if (fault_dsisr & DSISR_NOHPTE) {
  583. r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
  584. } else {
  585. vcpu->arch.shared->dar = dar;
  586. vcpu->arch.shared->dsisr = fault_dsisr;
  587. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  588. r = RESUME_GUEST;
  589. }
  590. break;
  591. }
  592. case BOOK3S_INTERRUPT_DATA_SEGMENT:
  593. if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
  594. vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
  595. kvmppc_book3s_queue_irqprio(vcpu,
  596. BOOK3S_INTERRUPT_DATA_SEGMENT);
  597. }
  598. r = RESUME_GUEST;
  599. break;
  600. case BOOK3S_INTERRUPT_INST_SEGMENT:
  601. if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
  602. kvmppc_book3s_queue_irqprio(vcpu,
  603. BOOK3S_INTERRUPT_INST_SEGMENT);
  604. }
  605. r = RESUME_GUEST;
  606. break;
  607. /* We're good on these - the host merely wanted to get our attention */
  608. case BOOK3S_INTERRUPT_DECREMENTER:
  609. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  610. vcpu->stat.dec_exits++;
  611. r = RESUME_GUEST;
  612. break;
  613. case BOOK3S_INTERRUPT_EXTERNAL:
  614. case BOOK3S_INTERRUPT_EXTERNAL_LEVEL:
  615. case BOOK3S_INTERRUPT_EXTERNAL_HV:
  616. vcpu->stat.ext_intr_exits++;
  617. r = RESUME_GUEST;
  618. break;
  619. case BOOK3S_INTERRUPT_PERFMON:
  620. r = RESUME_GUEST;
  621. break;
  622. case BOOK3S_INTERRUPT_PROGRAM:
  623. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  624. {
  625. enum emulation_result er;
  626. struct kvmppc_book3s_shadow_vcpu *svcpu;
  627. ulong flags;
  628. program_interrupt:
  629. svcpu = svcpu_get(vcpu);
  630. flags = svcpu->shadow_srr1 & 0x1f0000ull;
  631. svcpu_put(svcpu);
  632. if (vcpu->arch.shared->msr & MSR_PR) {
  633. #ifdef EXIT_DEBUG
  634. printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
  635. #endif
  636. if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
  637. (INS_DCBZ & 0xfffffff7)) {
  638. kvmppc_core_queue_program(vcpu, flags);
  639. r = RESUME_GUEST;
  640. break;
  641. }
  642. }
  643. vcpu->stat.emulated_inst_exits++;
  644. er = kvmppc_emulate_instruction(run, vcpu);
  645. switch (er) {
  646. case EMULATE_DONE:
  647. r = RESUME_GUEST_NV;
  648. break;
  649. case EMULATE_AGAIN:
  650. r = RESUME_GUEST;
  651. break;
  652. case EMULATE_FAIL:
  653. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  654. __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
  655. kvmppc_core_queue_program(vcpu, flags);
  656. r = RESUME_GUEST;
  657. break;
  658. case EMULATE_DO_MMIO:
  659. run->exit_reason = KVM_EXIT_MMIO;
  660. r = RESUME_HOST_NV;
  661. break;
  662. case EMULATE_DO_PAPR:
  663. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  664. vcpu->arch.hcall_needed = 1;
  665. r = RESUME_HOST_NV;
  666. break;
  667. default:
  668. BUG();
  669. }
  670. break;
  671. }
  672. case BOOK3S_INTERRUPT_SYSCALL:
  673. if (vcpu->arch.papr_enabled &&
  674. (kvmppc_get_last_inst(vcpu) == 0x44000022) &&
  675. !(vcpu->arch.shared->msr & MSR_PR)) {
  676. /* SC 1 papr hypercalls */
  677. ulong cmd = kvmppc_get_gpr(vcpu, 3);
  678. int i;
  679. #ifdef CONFIG_KVM_BOOK3S_64_PR
  680. if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) {
  681. r = RESUME_GUEST;
  682. break;
  683. }
  684. #endif
  685. run->papr_hcall.nr = cmd;
  686. for (i = 0; i < 9; ++i) {
  687. ulong gpr = kvmppc_get_gpr(vcpu, 4 + i);
  688. run->papr_hcall.args[i] = gpr;
  689. }
  690. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  691. vcpu->arch.hcall_needed = 1;
  692. r = RESUME_HOST;
  693. } else if (vcpu->arch.osi_enabled &&
  694. (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
  695. (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
  696. /* MOL hypercalls */
  697. u64 *gprs = run->osi.gprs;
  698. int i;
  699. run->exit_reason = KVM_EXIT_OSI;
  700. for (i = 0; i < 32; i++)
  701. gprs[i] = kvmppc_get_gpr(vcpu, i);
  702. vcpu->arch.osi_needed = 1;
  703. r = RESUME_HOST_NV;
  704. } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
  705. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  706. /* KVM PV hypercalls */
  707. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  708. r = RESUME_GUEST;
  709. } else {
  710. /* Guest syscalls */
  711. vcpu->stat.syscall_exits++;
  712. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  713. r = RESUME_GUEST;
  714. }
  715. break;
  716. case BOOK3S_INTERRUPT_FP_UNAVAIL:
  717. case BOOK3S_INTERRUPT_ALTIVEC:
  718. case BOOK3S_INTERRUPT_VSX:
  719. {
  720. int ext_msr = 0;
  721. switch (exit_nr) {
  722. case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
  723. case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
  724. case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
  725. }
  726. switch (kvmppc_check_ext(vcpu, exit_nr)) {
  727. case EMULATE_DONE:
  728. /* everything ok - let's enable the ext */
  729. r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
  730. break;
  731. case EMULATE_FAIL:
  732. /* we need to emulate this instruction */
  733. goto program_interrupt;
  734. break;
  735. default:
  736. /* nothing to worry about - go again */
  737. break;
  738. }
  739. break;
  740. }
  741. case BOOK3S_INTERRUPT_ALIGNMENT:
  742. if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
  743. vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
  744. kvmppc_get_last_inst(vcpu));
  745. vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
  746. kvmppc_get_last_inst(vcpu));
  747. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  748. }
  749. r = RESUME_GUEST;
  750. break;
  751. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  752. case BOOK3S_INTERRUPT_TRACE:
  753. kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
  754. r = RESUME_GUEST;
  755. break;
  756. default:
  757. {
  758. struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
  759. ulong shadow_srr1 = svcpu->shadow_srr1;
  760. svcpu_put(svcpu);
  761. /* Ugh - bork here! What did we get? */
  762. printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
  763. exit_nr, kvmppc_get_pc(vcpu), shadow_srr1);
  764. r = RESUME_HOST;
  765. BUG();
  766. break;
  767. }
  768. }
  769. if (!(r & RESUME_HOST)) {
  770. /* To avoid clobbering exit_reason, only check for signals if
  771. * we aren't already exiting to userspace for some other
  772. * reason. */
  773. /*
  774. * Interrupts could be timers for the guest which we have to
  775. * inject again, so let's postpone them until we're in the guest
  776. * and if we really did time things so badly, then we just exit
  777. * again due to a host external interrupt.
  778. */
  779. local_irq_disable();
  780. s = kvmppc_prepare_to_enter(vcpu);
  781. if (s <= 0) {
  782. local_irq_enable();
  783. r = s;
  784. } else {
  785. kvmppc_lazy_ee_enable();
  786. }
  787. }
  788. trace_kvm_book3s_reenter(r, vcpu);
  789. return r;
  790. }
  791. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  792. struct kvm_sregs *sregs)
  793. {
  794. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  795. int i;
  796. sregs->pvr = vcpu->arch.pvr;
  797. sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
  798. if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
  799. for (i = 0; i < 64; i++) {
  800. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
  801. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  802. }
  803. } else {
  804. for (i = 0; i < 16; i++)
  805. sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
  806. for (i = 0; i < 8; i++) {
  807. sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
  808. sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
  809. }
  810. }
  811. return 0;
  812. }
  813. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  814. struct kvm_sregs *sregs)
  815. {
  816. struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
  817. int i;
  818. kvmppc_set_pvr(vcpu, sregs->pvr);
  819. vcpu3s->sdr1 = sregs->u.s.sdr1;
  820. if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
  821. for (i = 0; i < 64; i++) {
  822. vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
  823. sregs->u.s.ppc64.slb[i].slbe);
  824. }
  825. } else {
  826. for (i = 0; i < 16; i++) {
  827. vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
  828. }
  829. for (i = 0; i < 8; i++) {
  830. kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
  831. (u32)sregs->u.s.ppc32.ibat[i]);
  832. kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
  833. (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
  834. kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
  835. (u32)sregs->u.s.ppc32.dbat[i]);
  836. kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
  837. (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
  838. }
  839. }
  840. /* Flush the MMU after messing with the segments */
  841. kvmppc_mmu_pte_flush(vcpu, 0, 0);
  842. return 0;
  843. }
  844. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  845. {
  846. int r = 0;
  847. switch (id) {
  848. case KVM_REG_PPC_HIOR:
  849. *val = get_reg_val(id, to_book3s(vcpu)->hior);
  850. break;
  851. #ifdef CONFIG_VSX
  852. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
  853. long int i = id - KVM_REG_PPC_VSR0;
  854. if (!cpu_has_feature(CPU_FTR_VSX)) {
  855. r = -ENXIO;
  856. break;
  857. }
  858. val->vsxval[0] = vcpu->arch.fpr[i];
  859. val->vsxval[1] = vcpu->arch.vsr[i];
  860. break;
  861. }
  862. #endif /* CONFIG_VSX */
  863. default:
  864. r = -EINVAL;
  865. break;
  866. }
  867. return r;
  868. }
  869. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  870. {
  871. int r = 0;
  872. switch (id) {
  873. case KVM_REG_PPC_HIOR:
  874. to_book3s(vcpu)->hior = set_reg_val(id, *val);
  875. to_book3s(vcpu)->hior_explicit = true;
  876. break;
  877. #ifdef CONFIG_VSX
  878. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: {
  879. long int i = id - KVM_REG_PPC_VSR0;
  880. if (!cpu_has_feature(CPU_FTR_VSX)) {
  881. r = -ENXIO;
  882. break;
  883. }
  884. vcpu->arch.fpr[i] = val->vsxval[0];
  885. vcpu->arch.vsr[i] = val->vsxval[1];
  886. break;
  887. }
  888. #endif /* CONFIG_VSX */
  889. default:
  890. r = -EINVAL;
  891. break;
  892. }
  893. return r;
  894. }
  895. int kvmppc_core_check_processor_compat(void)
  896. {
  897. return 0;
  898. }
  899. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  900. {
  901. struct kvmppc_vcpu_book3s *vcpu_book3s;
  902. struct kvm_vcpu *vcpu;
  903. int err = -ENOMEM;
  904. unsigned long p;
  905. vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
  906. if (!vcpu_book3s)
  907. goto out;
  908. vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
  909. kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
  910. if (!vcpu_book3s->shadow_vcpu)
  911. goto free_vcpu;
  912. vcpu = &vcpu_book3s->vcpu;
  913. err = kvm_vcpu_init(vcpu, kvm, id);
  914. if (err)
  915. goto free_shadow_vcpu;
  916. p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
  917. /* the real shared page fills the last 4k of our page */
  918. vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
  919. if (!p)
  920. goto uninit_vcpu;
  921. #ifdef CONFIG_PPC_BOOK3S_64
  922. /* default to book3s_64 (970fx) */
  923. vcpu->arch.pvr = 0x3C0301;
  924. #else
  925. /* default to book3s_32 (750) */
  926. vcpu->arch.pvr = 0x84202;
  927. #endif
  928. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  929. vcpu->arch.slb_nr = 64;
  930. vcpu->arch.shadow_msr = MSR_USER64;
  931. err = kvmppc_mmu_init(vcpu);
  932. if (err < 0)
  933. goto uninit_vcpu;
  934. return vcpu;
  935. uninit_vcpu:
  936. kvm_vcpu_uninit(vcpu);
  937. free_shadow_vcpu:
  938. kfree(vcpu_book3s->shadow_vcpu);
  939. free_vcpu:
  940. vfree(vcpu_book3s);
  941. out:
  942. return ERR_PTR(err);
  943. }
  944. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  945. {
  946. struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
  947. free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
  948. kvm_vcpu_uninit(vcpu);
  949. kfree(vcpu_book3s->shadow_vcpu);
  950. vfree(vcpu_book3s);
  951. }
  952. int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  953. {
  954. int ret;
  955. double fpr[32][TS_FPRWIDTH];
  956. unsigned int fpscr;
  957. int fpexc_mode;
  958. #ifdef CONFIG_ALTIVEC
  959. vector128 vr[32];
  960. vector128 vscr;
  961. unsigned long uninitialized_var(vrsave);
  962. int used_vr;
  963. #endif
  964. #ifdef CONFIG_VSX
  965. int used_vsr;
  966. #endif
  967. ulong ext_msr;
  968. /* Check if we can run the vcpu at all */
  969. if (!vcpu->arch.sane) {
  970. kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  971. ret = -EINVAL;
  972. goto out;
  973. }
  974. /*
  975. * Interrupts could be timers for the guest which we have to inject
  976. * again, so let's postpone them until we're in the guest and if we
  977. * really did time things so badly, then we just exit again due to
  978. * a host external interrupt.
  979. */
  980. local_irq_disable();
  981. ret = kvmppc_prepare_to_enter(vcpu);
  982. if (ret <= 0) {
  983. local_irq_enable();
  984. goto out;
  985. }
  986. /* Save FPU state in stack */
  987. if (current->thread.regs->msr & MSR_FP)
  988. giveup_fpu(current);
  989. memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
  990. fpscr = current->thread.fpscr.val;
  991. fpexc_mode = current->thread.fpexc_mode;
  992. #ifdef CONFIG_ALTIVEC
  993. /* Save Altivec state in stack */
  994. used_vr = current->thread.used_vr;
  995. if (used_vr) {
  996. if (current->thread.regs->msr & MSR_VEC)
  997. giveup_altivec(current);
  998. memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
  999. vscr = current->thread.vscr;
  1000. vrsave = current->thread.vrsave;
  1001. }
  1002. #endif
  1003. #ifdef CONFIG_VSX
  1004. /* Save VSX state in stack */
  1005. used_vsr = current->thread.used_vsr;
  1006. if (used_vsr && (current->thread.regs->msr & MSR_VSX))
  1007. __giveup_vsx(current);
  1008. #endif
  1009. /* Remember the MSR with disabled extensions */
  1010. ext_msr = current->thread.regs->msr;
  1011. /* Preload FPU if it's enabled */
  1012. if (vcpu->arch.shared->msr & MSR_FP)
  1013. kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
  1014. kvmppc_lazy_ee_enable();
  1015. ret = __kvmppc_vcpu_run(kvm_run, vcpu);
  1016. /* No need for kvm_guest_exit. It's done in handle_exit.
  1017. We also get here with interrupts enabled. */
  1018. /* Make sure we save the guest FPU/Altivec/VSX state */
  1019. kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX);
  1020. current->thread.regs->msr = ext_msr;
  1021. /* Restore FPU/VSX state from stack */
  1022. memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
  1023. current->thread.fpscr.val = fpscr;
  1024. current->thread.fpexc_mode = fpexc_mode;
  1025. #ifdef CONFIG_ALTIVEC
  1026. /* Restore Altivec state from stack */
  1027. if (used_vr && current->thread.used_vr) {
  1028. memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
  1029. current->thread.vscr = vscr;
  1030. current->thread.vrsave = vrsave;
  1031. }
  1032. current->thread.used_vr = used_vr;
  1033. #endif
  1034. #ifdef CONFIG_VSX
  1035. current->thread.used_vsr = used_vsr;
  1036. #endif
  1037. out:
  1038. vcpu->mode = OUTSIDE_GUEST_MODE;
  1039. return ret;
  1040. }
  1041. /*
  1042. * Get (and clear) the dirty memory log for a memory slot.
  1043. */
  1044. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1045. struct kvm_dirty_log *log)
  1046. {
  1047. struct kvm_memory_slot *memslot;
  1048. struct kvm_vcpu *vcpu;
  1049. ulong ga, ga_end;
  1050. int is_dirty = 0;
  1051. int r;
  1052. unsigned long n;
  1053. mutex_lock(&kvm->slots_lock);
  1054. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1055. if (r)
  1056. goto out;
  1057. /* If nothing is dirty, don't bother messing with page tables. */
  1058. if (is_dirty) {
  1059. memslot = id_to_memslot(kvm->memslots, log->slot);
  1060. ga = memslot->base_gfn << PAGE_SHIFT;
  1061. ga_end = ga + (memslot->npages << PAGE_SHIFT);
  1062. kvm_for_each_vcpu(n, vcpu, kvm)
  1063. kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
  1064. n = kvm_dirty_bitmap_bytes(memslot);
  1065. memset(memslot->dirty_bitmap, 0, n);
  1066. }
  1067. r = 0;
  1068. out:
  1069. mutex_unlock(&kvm->slots_lock);
  1070. return r;
  1071. }
  1072. #ifdef CONFIG_PPC64
  1073. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1074. {
  1075. /* No flags */
  1076. info->flags = 0;
  1077. /* SLB is always 64 entries */
  1078. info->slb_size = 64;
  1079. /* Standard 4k base page size segment */
  1080. info->sps[0].page_shift = 12;
  1081. info->sps[0].slb_enc = 0;
  1082. info->sps[0].enc[0].page_shift = 12;
  1083. info->sps[0].enc[0].pte_enc = 0;
  1084. /* Standard 16M large page size segment */
  1085. info->sps[1].page_shift = 24;
  1086. info->sps[1].slb_enc = SLB_VSID_L;
  1087. info->sps[1].enc[0].page_shift = 24;
  1088. info->sps[1].enc[0].pte_enc = 0;
  1089. return 0;
  1090. }
  1091. #endif /* CONFIG_PPC64 */
  1092. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1093. struct kvm_memory_slot *dont)
  1094. {
  1095. }
  1096. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1097. unsigned long npages)
  1098. {
  1099. return 0;
  1100. }
  1101. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1102. struct kvm_memory_slot *memslot,
  1103. struct kvm_userspace_memory_region *mem)
  1104. {
  1105. return 0;
  1106. }
  1107. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1108. struct kvm_userspace_memory_region *mem,
  1109. struct kvm_memory_slot old)
  1110. {
  1111. }
  1112. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1113. {
  1114. }
  1115. static unsigned int kvm_global_user_count = 0;
  1116. static DEFINE_SPINLOCK(kvm_global_user_count_lock);
  1117. int kvmppc_core_init_vm(struct kvm *kvm)
  1118. {
  1119. #ifdef CONFIG_PPC64
  1120. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1121. #endif
  1122. if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
  1123. spin_lock(&kvm_global_user_count_lock);
  1124. if (++kvm_global_user_count == 1)
  1125. pSeries_disable_reloc_on_exc();
  1126. spin_unlock(&kvm_global_user_count_lock);
  1127. }
  1128. return 0;
  1129. }
  1130. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1131. {
  1132. #ifdef CONFIG_PPC64
  1133. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1134. #endif
  1135. if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
  1136. spin_lock(&kvm_global_user_count_lock);
  1137. BUG_ON(kvm_global_user_count == 0);
  1138. if (--kvm_global_user_count == 0)
  1139. pSeries_enable_reloc_on_exc();
  1140. spin_unlock(&kvm_global_user_count_lock);
  1141. }
  1142. }
  1143. static int kvmppc_book3s_init(void)
  1144. {
  1145. int r;
  1146. r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
  1147. THIS_MODULE);
  1148. if (r)
  1149. return r;
  1150. r = kvmppc_mmu_hpte_sysinit();
  1151. return r;
  1152. }
  1153. static void kvmppc_book3s_exit(void)
  1154. {
  1155. kvmppc_mmu_hpte_sysexit();
  1156. kvm_exit();
  1157. }
  1158. module_init(kvmppc_book3s_init);
  1159. module_exit(kvmppc_book3s_exit);