book3s_pr.c 34 KB

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