book3s_pr.c 46 KB

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