book3s_hv.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. /*
  2. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  3. * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
  4. *
  5. * Authors:
  6. * Paul Mackerras <paulus@au1.ibm.com>
  7. * Alexander Graf <agraf@suse.de>
  8. * Kevin Wolf <mail@kevin-wolf.de>
  9. *
  10. * Description: KVM functions specific to running on Book 3S
  11. * processors in hypervisor mode (specifically POWER7 and later).
  12. *
  13. * This file is derived from arch/powerpc/kvm/book3s.c,
  14. * by Alexander Graf <agraf@suse.de>.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License, version 2, as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/preempt.h>
  24. #include <linux/sched.h>
  25. #include <linux/delay.h>
  26. #include <linux/export.h>
  27. #include <linux/fs.h>
  28. #include <linux/anon_inodes.h>
  29. #include <linux/cpumask.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/page-flags.h>
  32. #include <linux/srcu.h>
  33. #include <asm/reg.h>
  34. #include <asm/cputable.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/io.h>
  39. #include <asm/kvm_ppc.h>
  40. #include <asm/kvm_book3s.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/lppaca.h>
  43. #include <asm/processor.h>
  44. #include <asm/cputhreads.h>
  45. #include <asm/page.h>
  46. #include <asm/hvcall.h>
  47. #include <asm/switch_to.h>
  48. #include <asm/smp.h>
  49. #include <linux/gfp.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/highmem.h>
  52. #include <linux/hugetlb.h>
  53. /* #define EXIT_DEBUG */
  54. /* #define EXIT_DEBUG_SIMPLE */
  55. /* #define EXIT_DEBUG_INT */
  56. /* Used to indicate that a guest page fault needs to be handled */
  57. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  58. /* Used as a "null" value for timebase values */
  59. #define TB_NIL (~(u64)0)
  60. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  61. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  62. /*
  63. * We use the vcpu_load/put functions to measure stolen time.
  64. * Stolen time is counted as time when either the vcpu is able to
  65. * run as part of a virtual core, but the task running the vcore
  66. * is preempted or sleeping, or when the vcpu needs something done
  67. * in the kernel by the task running the vcpu, but that task is
  68. * preempted or sleeping. Those two things have to be counted
  69. * separately, since one of the vcpu tasks will take on the job
  70. * of running the core, and the other vcpu tasks in the vcore will
  71. * sleep waiting for it to do that, but that sleep shouldn't count
  72. * as stolen time.
  73. *
  74. * Hence we accumulate stolen time when the vcpu can run as part of
  75. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  76. * needs its task to do other things in the kernel (for example,
  77. * service a page fault) in busy_stolen. We don't accumulate
  78. * stolen time for a vcore when it is inactive, or for a vcpu
  79. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  80. * a misnomer; it means that the vcpu task is not executing in
  81. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  82. * the kernel. We don't have any way of dividing up that time
  83. * between time that the vcpu is genuinely stopped, time that
  84. * the task is actively working on behalf of the vcpu, and time
  85. * that the task is preempted, so we don't count any of it as
  86. * stolen.
  87. *
  88. * Updates to busy_stolen are protected by arch.tbacct_lock;
  89. * updates to vc->stolen_tb are protected by the arch.tbacct_lock
  90. * of the vcpu that has taken responsibility for running the vcore
  91. * (i.e. vc->runner). The stolen times are measured in units of
  92. * timebase ticks. (Note that the != TB_NIL checks below are
  93. * purely defensive; they should never fail.)
  94. */
  95. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  96. {
  97. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  98. spin_lock(&vcpu->arch.tbacct_lock);
  99. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
  100. vc->preempt_tb != TB_NIL) {
  101. vc->stolen_tb += mftb() - vc->preempt_tb;
  102. vc->preempt_tb = TB_NIL;
  103. }
  104. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  105. vcpu->arch.busy_preempt != TB_NIL) {
  106. vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
  107. vcpu->arch.busy_preempt = TB_NIL;
  108. }
  109. spin_unlock(&vcpu->arch.tbacct_lock);
  110. }
  111. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  112. {
  113. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  114. spin_lock(&vcpu->arch.tbacct_lock);
  115. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
  116. vc->preempt_tb = mftb();
  117. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  118. vcpu->arch.busy_preempt = mftb();
  119. spin_unlock(&vcpu->arch.tbacct_lock);
  120. }
  121. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
  122. {
  123. vcpu->arch.shregs.msr = msr;
  124. kvmppc_end_cede(vcpu);
  125. }
  126. void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
  127. {
  128. vcpu->arch.pvr = pvr;
  129. }
  130. void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  131. {
  132. int r;
  133. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  134. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  135. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  136. for (r = 0; r < 16; ++r)
  137. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  138. r, kvmppc_get_gpr(vcpu, r),
  139. r+16, kvmppc_get_gpr(vcpu, r+16));
  140. pr_err("ctr = %.16lx lr = %.16lx\n",
  141. vcpu->arch.ctr, vcpu->arch.lr);
  142. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  143. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  144. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  145. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  146. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  147. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  148. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  149. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  150. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  151. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  152. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  153. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  154. for (r = 0; r < vcpu->arch.slb_max; ++r)
  155. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  156. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  157. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  158. vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
  159. vcpu->arch.last_inst);
  160. }
  161. struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  162. {
  163. int r;
  164. struct kvm_vcpu *v, *ret = NULL;
  165. mutex_lock(&kvm->lock);
  166. kvm_for_each_vcpu(r, v, kvm) {
  167. if (v->vcpu_id == id) {
  168. ret = v;
  169. break;
  170. }
  171. }
  172. mutex_unlock(&kvm->lock);
  173. return ret;
  174. }
  175. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  176. {
  177. vpa->shared_proc = 1;
  178. vpa->yield_count = 1;
  179. }
  180. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  181. unsigned long addr, unsigned long len)
  182. {
  183. /* check address is cacheline aligned */
  184. if (addr & (L1_CACHE_BYTES - 1))
  185. return -EINVAL;
  186. spin_lock(&vcpu->arch.vpa_update_lock);
  187. if (v->next_gpa != addr || v->len != len) {
  188. v->next_gpa = addr;
  189. v->len = addr ? len : 0;
  190. v->update_pending = 1;
  191. }
  192. spin_unlock(&vcpu->arch.vpa_update_lock);
  193. return 0;
  194. }
  195. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  196. struct reg_vpa {
  197. u32 dummy;
  198. union {
  199. u16 hword;
  200. u32 word;
  201. } length;
  202. };
  203. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  204. {
  205. if (vpap->update_pending)
  206. return vpap->next_gpa != 0;
  207. return vpap->pinned_addr != NULL;
  208. }
  209. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  210. unsigned long flags,
  211. unsigned long vcpuid, unsigned long vpa)
  212. {
  213. struct kvm *kvm = vcpu->kvm;
  214. unsigned long len, nb;
  215. void *va;
  216. struct kvm_vcpu *tvcpu;
  217. int err;
  218. int subfunc;
  219. struct kvmppc_vpa *vpap;
  220. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  221. if (!tvcpu)
  222. return H_PARAMETER;
  223. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  224. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  225. subfunc == H_VPA_REG_SLB) {
  226. /* Registering new area - address must be cache-line aligned */
  227. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  228. return H_PARAMETER;
  229. /* convert logical addr to kernel addr and read length */
  230. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  231. if (va == NULL)
  232. return H_PARAMETER;
  233. if (subfunc == H_VPA_REG_VPA)
  234. len = ((struct reg_vpa *)va)->length.hword;
  235. else
  236. len = ((struct reg_vpa *)va)->length.word;
  237. kvmppc_unpin_guest_page(kvm, va);
  238. /* Check length */
  239. if (len > nb || len < sizeof(struct reg_vpa))
  240. return H_PARAMETER;
  241. } else {
  242. vpa = 0;
  243. len = 0;
  244. }
  245. err = H_PARAMETER;
  246. vpap = NULL;
  247. spin_lock(&tvcpu->arch.vpa_update_lock);
  248. switch (subfunc) {
  249. case H_VPA_REG_VPA: /* register VPA */
  250. if (len < sizeof(struct lppaca))
  251. break;
  252. vpap = &tvcpu->arch.vpa;
  253. err = 0;
  254. break;
  255. case H_VPA_REG_DTL: /* register DTL */
  256. if (len < sizeof(struct dtl_entry))
  257. break;
  258. len -= len % sizeof(struct dtl_entry);
  259. /* Check that they have previously registered a VPA */
  260. err = H_RESOURCE;
  261. if (!vpa_is_registered(&tvcpu->arch.vpa))
  262. break;
  263. vpap = &tvcpu->arch.dtl;
  264. err = 0;
  265. break;
  266. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  267. /* Check that they have previously registered a VPA */
  268. err = H_RESOURCE;
  269. if (!vpa_is_registered(&tvcpu->arch.vpa))
  270. break;
  271. vpap = &tvcpu->arch.slb_shadow;
  272. err = 0;
  273. break;
  274. case H_VPA_DEREG_VPA: /* deregister VPA */
  275. /* Check they don't still have a DTL or SLB buf registered */
  276. err = H_RESOURCE;
  277. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  278. vpa_is_registered(&tvcpu->arch.slb_shadow))
  279. break;
  280. vpap = &tvcpu->arch.vpa;
  281. err = 0;
  282. break;
  283. case H_VPA_DEREG_DTL: /* deregister DTL */
  284. vpap = &tvcpu->arch.dtl;
  285. err = 0;
  286. break;
  287. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  288. vpap = &tvcpu->arch.slb_shadow;
  289. err = 0;
  290. break;
  291. }
  292. if (vpap) {
  293. vpap->next_gpa = vpa;
  294. vpap->len = len;
  295. vpap->update_pending = 1;
  296. }
  297. spin_unlock(&tvcpu->arch.vpa_update_lock);
  298. return err;
  299. }
  300. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  301. {
  302. struct kvm *kvm = vcpu->kvm;
  303. void *va;
  304. unsigned long nb;
  305. unsigned long gpa;
  306. /*
  307. * We need to pin the page pointed to by vpap->next_gpa,
  308. * but we can't call kvmppc_pin_guest_page under the lock
  309. * as it does get_user_pages() and down_read(). So we
  310. * have to drop the lock, pin the page, then get the lock
  311. * again and check that a new area didn't get registered
  312. * in the meantime.
  313. */
  314. for (;;) {
  315. gpa = vpap->next_gpa;
  316. spin_unlock(&vcpu->arch.vpa_update_lock);
  317. va = NULL;
  318. nb = 0;
  319. if (gpa)
  320. va = kvmppc_pin_guest_page(kvm, vpap->next_gpa, &nb);
  321. spin_lock(&vcpu->arch.vpa_update_lock);
  322. if (gpa == vpap->next_gpa)
  323. break;
  324. /* sigh... unpin that one and try again */
  325. if (va)
  326. kvmppc_unpin_guest_page(kvm, va);
  327. }
  328. vpap->update_pending = 0;
  329. if (va && nb < vpap->len) {
  330. /*
  331. * If it's now too short, it must be that userspace
  332. * has changed the mappings underlying guest memory,
  333. * so unregister the region.
  334. */
  335. kvmppc_unpin_guest_page(kvm, va);
  336. va = NULL;
  337. }
  338. if (vpap->pinned_addr)
  339. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr);
  340. vpap->pinned_addr = va;
  341. if (va)
  342. vpap->pinned_end = va + vpap->len;
  343. }
  344. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  345. {
  346. if (!(vcpu->arch.vpa.update_pending ||
  347. vcpu->arch.slb_shadow.update_pending ||
  348. vcpu->arch.dtl.update_pending))
  349. return;
  350. spin_lock(&vcpu->arch.vpa_update_lock);
  351. if (vcpu->arch.vpa.update_pending) {
  352. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  353. if (vcpu->arch.vpa.pinned_addr)
  354. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  355. }
  356. if (vcpu->arch.dtl.update_pending) {
  357. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  358. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  359. vcpu->arch.dtl_index = 0;
  360. }
  361. if (vcpu->arch.slb_shadow.update_pending)
  362. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  363. spin_unlock(&vcpu->arch.vpa_update_lock);
  364. }
  365. /*
  366. * Return the accumulated stolen time for the vcore up until `now'.
  367. * The caller should hold the vcore lock.
  368. */
  369. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  370. {
  371. u64 p;
  372. /*
  373. * If we are the task running the vcore, then since we hold
  374. * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
  375. * can't be updated, so we don't need the tbacct_lock.
  376. * If the vcore is inactive, it can't become active (since we
  377. * hold the vcore lock), so the vcpu load/put functions won't
  378. * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
  379. */
  380. if (vc->vcore_state != VCORE_INACTIVE &&
  381. vc->runner->arch.run_task != current) {
  382. spin_lock(&vc->runner->arch.tbacct_lock);
  383. p = vc->stolen_tb;
  384. if (vc->preempt_tb != TB_NIL)
  385. p += now - vc->preempt_tb;
  386. spin_unlock(&vc->runner->arch.tbacct_lock);
  387. } else {
  388. p = vc->stolen_tb;
  389. }
  390. return p;
  391. }
  392. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  393. struct kvmppc_vcore *vc)
  394. {
  395. struct dtl_entry *dt;
  396. struct lppaca *vpa;
  397. unsigned long stolen;
  398. unsigned long core_stolen;
  399. u64 now;
  400. dt = vcpu->arch.dtl_ptr;
  401. vpa = vcpu->arch.vpa.pinned_addr;
  402. now = mftb();
  403. core_stolen = vcore_stolen_time(vc, now);
  404. stolen = core_stolen - vcpu->arch.stolen_logged;
  405. vcpu->arch.stolen_logged = core_stolen;
  406. spin_lock(&vcpu->arch.tbacct_lock);
  407. stolen += vcpu->arch.busy_stolen;
  408. vcpu->arch.busy_stolen = 0;
  409. spin_unlock(&vcpu->arch.tbacct_lock);
  410. if (!dt || !vpa)
  411. return;
  412. memset(dt, 0, sizeof(struct dtl_entry));
  413. dt->dispatch_reason = 7;
  414. dt->processor_id = vc->pcpu + vcpu->arch.ptid;
  415. dt->timebase = now;
  416. dt->enqueue_to_dispatch_time = stolen;
  417. dt->srr0 = kvmppc_get_pc(vcpu);
  418. dt->srr1 = vcpu->arch.shregs.msr;
  419. ++dt;
  420. if (dt == vcpu->arch.dtl.pinned_end)
  421. dt = vcpu->arch.dtl.pinned_addr;
  422. vcpu->arch.dtl_ptr = dt;
  423. /* order writing *dt vs. writing vpa->dtl_idx */
  424. smp_wmb();
  425. vpa->dtl_idx = ++vcpu->arch.dtl_index;
  426. }
  427. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  428. {
  429. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  430. unsigned long target, ret = H_SUCCESS;
  431. struct kvm_vcpu *tvcpu;
  432. int idx;
  433. switch (req) {
  434. case H_ENTER:
  435. idx = srcu_read_lock(&vcpu->kvm->srcu);
  436. ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
  437. kvmppc_get_gpr(vcpu, 5),
  438. kvmppc_get_gpr(vcpu, 6),
  439. kvmppc_get_gpr(vcpu, 7));
  440. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  441. break;
  442. case H_CEDE:
  443. break;
  444. case H_PROD:
  445. target = kvmppc_get_gpr(vcpu, 4);
  446. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  447. if (!tvcpu) {
  448. ret = H_PARAMETER;
  449. break;
  450. }
  451. tvcpu->arch.prodded = 1;
  452. smp_mb();
  453. if (vcpu->arch.ceded) {
  454. if (waitqueue_active(&vcpu->wq)) {
  455. wake_up_interruptible(&vcpu->wq);
  456. vcpu->stat.halt_wakeup++;
  457. }
  458. }
  459. break;
  460. case H_CONFER:
  461. break;
  462. case H_REGISTER_VPA:
  463. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  464. kvmppc_get_gpr(vcpu, 5),
  465. kvmppc_get_gpr(vcpu, 6));
  466. break;
  467. default:
  468. return RESUME_HOST;
  469. }
  470. kvmppc_set_gpr(vcpu, 3, ret);
  471. vcpu->arch.hcall_needed = 0;
  472. return RESUME_GUEST;
  473. }
  474. static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  475. struct task_struct *tsk)
  476. {
  477. int r = RESUME_HOST;
  478. vcpu->stat.sum_exits++;
  479. run->exit_reason = KVM_EXIT_UNKNOWN;
  480. run->ready_for_interrupt_injection = 1;
  481. switch (vcpu->arch.trap) {
  482. /* We're good on these - the host merely wanted to get our attention */
  483. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  484. vcpu->stat.dec_exits++;
  485. r = RESUME_GUEST;
  486. break;
  487. case BOOK3S_INTERRUPT_EXTERNAL:
  488. vcpu->stat.ext_intr_exits++;
  489. r = RESUME_GUEST;
  490. break;
  491. case BOOK3S_INTERRUPT_PERFMON:
  492. r = RESUME_GUEST;
  493. break;
  494. case BOOK3S_INTERRUPT_PROGRAM:
  495. {
  496. ulong flags;
  497. /*
  498. * Normally program interrupts are delivered directly
  499. * to the guest by the hardware, but we can get here
  500. * as a result of a hypervisor emulation interrupt
  501. * (e40) getting turned into a 700 by BML RTAS.
  502. */
  503. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  504. kvmppc_core_queue_program(vcpu, flags);
  505. r = RESUME_GUEST;
  506. break;
  507. }
  508. case BOOK3S_INTERRUPT_SYSCALL:
  509. {
  510. /* hcall - punt to userspace */
  511. int i;
  512. if (vcpu->arch.shregs.msr & MSR_PR) {
  513. /* sc 1 from userspace - reflect to guest syscall */
  514. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
  515. r = RESUME_GUEST;
  516. break;
  517. }
  518. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  519. for (i = 0; i < 9; ++i)
  520. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  521. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  522. vcpu->arch.hcall_needed = 1;
  523. r = RESUME_HOST;
  524. break;
  525. }
  526. /*
  527. * We get these next two if the guest accesses a page which it thinks
  528. * it has mapped but which is not actually present, either because
  529. * it is for an emulated I/O device or because the corresonding
  530. * host page has been paged out. Any other HDSI/HISI interrupts
  531. * have been handled already.
  532. */
  533. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  534. r = RESUME_PAGE_FAULT;
  535. break;
  536. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  537. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  538. vcpu->arch.fault_dsisr = 0;
  539. r = RESUME_PAGE_FAULT;
  540. break;
  541. /*
  542. * This occurs if the guest executes an illegal instruction.
  543. * We just generate a program interrupt to the guest, since
  544. * we don't emulate any guest instructions at this stage.
  545. */
  546. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  547. kvmppc_core_queue_program(vcpu, 0x80000);
  548. r = RESUME_GUEST;
  549. break;
  550. default:
  551. kvmppc_dump_regs(vcpu);
  552. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  553. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  554. vcpu->arch.shregs.msr);
  555. r = RESUME_HOST;
  556. BUG();
  557. break;
  558. }
  559. return r;
  560. }
  561. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  562. struct kvm_sregs *sregs)
  563. {
  564. int i;
  565. sregs->pvr = vcpu->arch.pvr;
  566. memset(sregs, 0, sizeof(struct kvm_sregs));
  567. for (i = 0; i < vcpu->arch.slb_max; i++) {
  568. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  569. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  570. }
  571. return 0;
  572. }
  573. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  574. struct kvm_sregs *sregs)
  575. {
  576. int i, j;
  577. kvmppc_set_pvr(vcpu, sregs->pvr);
  578. j = 0;
  579. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  580. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  581. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  582. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  583. ++j;
  584. }
  585. }
  586. vcpu->arch.slb_max = j;
  587. return 0;
  588. }
  589. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  590. {
  591. int r = 0;
  592. long int i;
  593. switch (id) {
  594. case KVM_REG_PPC_HIOR:
  595. *val = get_reg_val(id, 0);
  596. break;
  597. case KVM_REG_PPC_DABR:
  598. *val = get_reg_val(id, vcpu->arch.dabr);
  599. break;
  600. case KVM_REG_PPC_DSCR:
  601. *val = get_reg_val(id, vcpu->arch.dscr);
  602. break;
  603. case KVM_REG_PPC_PURR:
  604. *val = get_reg_val(id, vcpu->arch.purr);
  605. break;
  606. case KVM_REG_PPC_SPURR:
  607. *val = get_reg_val(id, vcpu->arch.spurr);
  608. break;
  609. case KVM_REG_PPC_AMR:
  610. *val = get_reg_val(id, vcpu->arch.amr);
  611. break;
  612. case KVM_REG_PPC_UAMOR:
  613. *val = get_reg_val(id, vcpu->arch.uamor);
  614. break;
  615. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
  616. i = id - KVM_REG_PPC_MMCR0;
  617. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  618. break;
  619. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  620. i = id - KVM_REG_PPC_PMC1;
  621. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  622. break;
  623. #ifdef CONFIG_VSX
  624. case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
  625. if (cpu_has_feature(CPU_FTR_VSX)) {
  626. /* VSX => FP reg i is stored in arch.vsr[2*i] */
  627. long int i = id - KVM_REG_PPC_FPR0;
  628. *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
  629. } else {
  630. /* let generic code handle it */
  631. r = -EINVAL;
  632. }
  633. break;
  634. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
  635. if (cpu_has_feature(CPU_FTR_VSX)) {
  636. long int i = id - KVM_REG_PPC_VSR0;
  637. val->vsxval[0] = vcpu->arch.vsr[2 * i];
  638. val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
  639. } else {
  640. r = -ENXIO;
  641. }
  642. break;
  643. #endif /* CONFIG_VSX */
  644. case KVM_REG_PPC_VPA_ADDR:
  645. spin_lock(&vcpu->arch.vpa_update_lock);
  646. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  647. spin_unlock(&vcpu->arch.vpa_update_lock);
  648. break;
  649. case KVM_REG_PPC_VPA_SLB:
  650. spin_lock(&vcpu->arch.vpa_update_lock);
  651. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  652. val->vpaval.length = vcpu->arch.slb_shadow.len;
  653. spin_unlock(&vcpu->arch.vpa_update_lock);
  654. break;
  655. case KVM_REG_PPC_VPA_DTL:
  656. spin_lock(&vcpu->arch.vpa_update_lock);
  657. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  658. val->vpaval.length = vcpu->arch.dtl.len;
  659. spin_unlock(&vcpu->arch.vpa_update_lock);
  660. break;
  661. default:
  662. r = -EINVAL;
  663. break;
  664. }
  665. return r;
  666. }
  667. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  668. {
  669. int r = 0;
  670. long int i;
  671. unsigned long addr, len;
  672. switch (id) {
  673. case KVM_REG_PPC_HIOR:
  674. /* Only allow this to be set to zero */
  675. if (set_reg_val(id, *val))
  676. r = -EINVAL;
  677. break;
  678. case KVM_REG_PPC_DABR:
  679. vcpu->arch.dabr = set_reg_val(id, *val);
  680. break;
  681. case KVM_REG_PPC_DSCR:
  682. vcpu->arch.dscr = set_reg_val(id, *val);
  683. break;
  684. case KVM_REG_PPC_PURR:
  685. vcpu->arch.purr = set_reg_val(id, *val);
  686. break;
  687. case KVM_REG_PPC_SPURR:
  688. vcpu->arch.spurr = set_reg_val(id, *val);
  689. break;
  690. case KVM_REG_PPC_AMR:
  691. vcpu->arch.amr = set_reg_val(id, *val);
  692. break;
  693. case KVM_REG_PPC_UAMOR:
  694. vcpu->arch.uamor = set_reg_val(id, *val);
  695. break;
  696. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
  697. i = id - KVM_REG_PPC_MMCR0;
  698. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  699. break;
  700. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  701. i = id - KVM_REG_PPC_PMC1;
  702. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  703. break;
  704. #ifdef CONFIG_VSX
  705. case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
  706. if (cpu_has_feature(CPU_FTR_VSX)) {
  707. /* VSX => FP reg i is stored in arch.vsr[2*i] */
  708. long int i = id - KVM_REG_PPC_FPR0;
  709. vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
  710. } else {
  711. /* let generic code handle it */
  712. r = -EINVAL;
  713. }
  714. break;
  715. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
  716. if (cpu_has_feature(CPU_FTR_VSX)) {
  717. long int i = id - KVM_REG_PPC_VSR0;
  718. vcpu->arch.vsr[2 * i] = val->vsxval[0];
  719. vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
  720. } else {
  721. r = -ENXIO;
  722. }
  723. break;
  724. #endif /* CONFIG_VSX */
  725. case KVM_REG_PPC_VPA_ADDR:
  726. addr = set_reg_val(id, *val);
  727. r = -EINVAL;
  728. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  729. vcpu->arch.dtl.next_gpa))
  730. break;
  731. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  732. break;
  733. case KVM_REG_PPC_VPA_SLB:
  734. addr = val->vpaval.addr;
  735. len = val->vpaval.length;
  736. r = -EINVAL;
  737. if (addr && !vcpu->arch.vpa.next_gpa)
  738. break;
  739. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  740. break;
  741. case KVM_REG_PPC_VPA_DTL:
  742. addr = val->vpaval.addr;
  743. len = val->vpaval.length;
  744. r = -EINVAL;
  745. if (addr && (len < sizeof(struct dtl_entry) ||
  746. !vcpu->arch.vpa.next_gpa))
  747. break;
  748. len -= len % sizeof(struct dtl_entry);
  749. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  750. break;
  751. default:
  752. r = -EINVAL;
  753. break;
  754. }
  755. return r;
  756. }
  757. int kvmppc_core_check_processor_compat(void)
  758. {
  759. if (cpu_has_feature(CPU_FTR_HVMODE))
  760. return 0;
  761. return -EIO;
  762. }
  763. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  764. {
  765. struct kvm_vcpu *vcpu;
  766. int err = -EINVAL;
  767. int core;
  768. struct kvmppc_vcore *vcore;
  769. core = id / threads_per_core;
  770. if (core >= KVM_MAX_VCORES)
  771. goto out;
  772. err = -ENOMEM;
  773. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  774. if (!vcpu)
  775. goto out;
  776. err = kvm_vcpu_init(vcpu, kvm, id);
  777. if (err)
  778. goto free_vcpu;
  779. vcpu->arch.shared = &vcpu->arch.shregs;
  780. vcpu->arch.mmcr[0] = MMCR0_FC;
  781. vcpu->arch.ctrl = CTRL_RUNLATCH;
  782. /* default to host PVR, since we can't spoof it */
  783. vcpu->arch.pvr = mfspr(SPRN_PVR);
  784. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  785. spin_lock_init(&vcpu->arch.vpa_update_lock);
  786. spin_lock_init(&vcpu->arch.tbacct_lock);
  787. vcpu->arch.busy_preempt = TB_NIL;
  788. kvmppc_mmu_book3s_hv_init(vcpu);
  789. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  790. init_waitqueue_head(&vcpu->arch.cpu_run);
  791. mutex_lock(&kvm->lock);
  792. vcore = kvm->arch.vcores[core];
  793. if (!vcore) {
  794. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  795. if (vcore) {
  796. INIT_LIST_HEAD(&vcore->runnable_threads);
  797. spin_lock_init(&vcore->lock);
  798. init_waitqueue_head(&vcore->wq);
  799. vcore->preempt_tb = TB_NIL;
  800. }
  801. kvm->arch.vcores[core] = vcore;
  802. kvm->arch.online_vcores++;
  803. }
  804. mutex_unlock(&kvm->lock);
  805. if (!vcore)
  806. goto free_vcpu;
  807. spin_lock(&vcore->lock);
  808. ++vcore->num_threads;
  809. spin_unlock(&vcore->lock);
  810. vcpu->arch.vcore = vcore;
  811. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  812. kvmppc_sanity_check(vcpu);
  813. return vcpu;
  814. free_vcpu:
  815. kmem_cache_free(kvm_vcpu_cache, vcpu);
  816. out:
  817. return ERR_PTR(err);
  818. }
  819. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  820. {
  821. spin_lock(&vcpu->arch.vpa_update_lock);
  822. if (vcpu->arch.dtl.pinned_addr)
  823. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
  824. if (vcpu->arch.slb_shadow.pinned_addr)
  825. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
  826. if (vcpu->arch.vpa.pinned_addr)
  827. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
  828. spin_unlock(&vcpu->arch.vpa_update_lock);
  829. kvm_vcpu_uninit(vcpu);
  830. kmem_cache_free(kvm_vcpu_cache, vcpu);
  831. }
  832. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  833. {
  834. unsigned long dec_nsec, now;
  835. now = get_tb();
  836. if (now > vcpu->arch.dec_expires) {
  837. /* decrementer has already gone negative */
  838. kvmppc_core_queue_dec(vcpu);
  839. kvmppc_core_prepare_to_enter(vcpu);
  840. return;
  841. }
  842. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  843. / tb_ticks_per_sec;
  844. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  845. HRTIMER_MODE_REL);
  846. vcpu->arch.timer_running = 1;
  847. }
  848. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  849. {
  850. vcpu->arch.ceded = 0;
  851. if (vcpu->arch.timer_running) {
  852. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  853. vcpu->arch.timer_running = 0;
  854. }
  855. }
  856. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  857. extern void xics_wake_cpu(int cpu);
  858. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  859. struct kvm_vcpu *vcpu)
  860. {
  861. u64 now;
  862. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  863. return;
  864. spin_lock(&vcpu->arch.tbacct_lock);
  865. now = mftb();
  866. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  867. vcpu->arch.stolen_logged;
  868. vcpu->arch.busy_preempt = now;
  869. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  870. spin_unlock(&vcpu->arch.tbacct_lock);
  871. --vc->n_runnable;
  872. list_del(&vcpu->arch.run_list);
  873. }
  874. static int kvmppc_grab_hwthread(int cpu)
  875. {
  876. struct paca_struct *tpaca;
  877. long timeout = 1000;
  878. tpaca = &paca[cpu];
  879. /* Ensure the thread won't go into the kernel if it wakes */
  880. tpaca->kvm_hstate.hwthread_req = 1;
  881. tpaca->kvm_hstate.kvm_vcpu = NULL;
  882. /*
  883. * If the thread is already executing in the kernel (e.g. handling
  884. * a stray interrupt), wait for it to get back to nap mode.
  885. * The smp_mb() is to ensure that our setting of hwthread_req
  886. * is visible before we look at hwthread_state, so if this
  887. * races with the code at system_reset_pSeries and the thread
  888. * misses our setting of hwthread_req, we are sure to see its
  889. * setting of hwthread_state, and vice versa.
  890. */
  891. smp_mb();
  892. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  893. if (--timeout <= 0) {
  894. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  895. return -EBUSY;
  896. }
  897. udelay(1);
  898. }
  899. return 0;
  900. }
  901. static void kvmppc_release_hwthread(int cpu)
  902. {
  903. struct paca_struct *tpaca;
  904. tpaca = &paca[cpu];
  905. tpaca->kvm_hstate.hwthread_req = 0;
  906. tpaca->kvm_hstate.kvm_vcpu = NULL;
  907. }
  908. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  909. {
  910. int cpu;
  911. struct paca_struct *tpaca;
  912. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  913. if (vcpu->arch.timer_running) {
  914. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  915. vcpu->arch.timer_running = 0;
  916. }
  917. cpu = vc->pcpu + vcpu->arch.ptid;
  918. tpaca = &paca[cpu];
  919. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  920. tpaca->kvm_hstate.kvm_vcore = vc;
  921. tpaca->kvm_hstate.napping = 0;
  922. vcpu->cpu = vc->pcpu;
  923. smp_wmb();
  924. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  925. if (vcpu->arch.ptid) {
  926. xics_wake_cpu(cpu);
  927. ++vc->n_woken;
  928. }
  929. #endif
  930. }
  931. static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
  932. {
  933. int i;
  934. HMT_low();
  935. i = 0;
  936. while (vc->nap_count < vc->n_woken) {
  937. if (++i >= 1000000) {
  938. pr_err("kvmppc_wait_for_nap timeout %d %d\n",
  939. vc->nap_count, vc->n_woken);
  940. break;
  941. }
  942. cpu_relax();
  943. }
  944. HMT_medium();
  945. }
  946. /*
  947. * Check that we are on thread 0 and that any other threads in
  948. * this core are off-line. Then grab the threads so they can't
  949. * enter the kernel.
  950. */
  951. static int on_primary_thread(void)
  952. {
  953. int cpu = smp_processor_id();
  954. int thr = cpu_thread_in_core(cpu);
  955. if (thr)
  956. return 0;
  957. while (++thr < threads_per_core)
  958. if (cpu_online(cpu + thr))
  959. return 0;
  960. /* Grab all hw threads so they can't go into the kernel */
  961. for (thr = 1; thr < threads_per_core; ++thr) {
  962. if (kvmppc_grab_hwthread(cpu + thr)) {
  963. /* Couldn't grab one; let the others go */
  964. do {
  965. kvmppc_release_hwthread(cpu + thr);
  966. } while (--thr > 0);
  967. return 0;
  968. }
  969. }
  970. return 1;
  971. }
  972. /*
  973. * Run a set of guest threads on a physical core.
  974. * Called with vc->lock held.
  975. */
  976. static void kvmppc_run_core(struct kvmppc_vcore *vc)
  977. {
  978. struct kvm_vcpu *vcpu, *vcpu0, *vnext;
  979. long ret;
  980. u64 now;
  981. int ptid, i, need_vpa_update;
  982. int srcu_idx;
  983. struct kvm_vcpu *vcpus_to_update[threads_per_core];
  984. /* don't start if any threads have a signal pending */
  985. need_vpa_update = 0;
  986. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  987. if (signal_pending(vcpu->arch.run_task))
  988. return;
  989. if (vcpu->arch.vpa.update_pending ||
  990. vcpu->arch.slb_shadow.update_pending ||
  991. vcpu->arch.dtl.update_pending)
  992. vcpus_to_update[need_vpa_update++] = vcpu;
  993. }
  994. /*
  995. * Initialize *vc, in particular vc->vcore_state, so we can
  996. * drop the vcore lock if necessary.
  997. */
  998. vc->n_woken = 0;
  999. vc->nap_count = 0;
  1000. vc->entry_exit_count = 0;
  1001. vc->vcore_state = VCORE_STARTING;
  1002. vc->in_guest = 0;
  1003. vc->napping_threads = 0;
  1004. /*
  1005. * Updating any of the vpas requires calling kvmppc_pin_guest_page,
  1006. * which can't be called with any spinlocks held.
  1007. */
  1008. if (need_vpa_update) {
  1009. spin_unlock(&vc->lock);
  1010. for (i = 0; i < need_vpa_update; ++i)
  1011. kvmppc_update_vpas(vcpus_to_update[i]);
  1012. spin_lock(&vc->lock);
  1013. }
  1014. /*
  1015. * Assign physical thread IDs, first to non-ceded vcpus
  1016. * and then to ceded ones.
  1017. */
  1018. ptid = 0;
  1019. vcpu0 = NULL;
  1020. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1021. if (!vcpu->arch.ceded) {
  1022. if (!ptid)
  1023. vcpu0 = vcpu;
  1024. vcpu->arch.ptid = ptid++;
  1025. }
  1026. }
  1027. if (!vcpu0)
  1028. goto out; /* nothing to run; should never happen */
  1029. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1030. if (vcpu->arch.ceded)
  1031. vcpu->arch.ptid = ptid++;
  1032. /*
  1033. * Make sure we are running on thread 0, and that
  1034. * secondary threads are offline.
  1035. */
  1036. if (threads_per_core > 1 && !on_primary_thread()) {
  1037. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1038. vcpu->arch.ret = -EBUSY;
  1039. goto out;
  1040. }
  1041. vc->pcpu = smp_processor_id();
  1042. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1043. kvmppc_start_thread(vcpu);
  1044. kvmppc_create_dtl_entry(vcpu, vc);
  1045. }
  1046. vc->vcore_state = VCORE_RUNNING;
  1047. preempt_disable();
  1048. spin_unlock(&vc->lock);
  1049. kvm_guest_enter();
  1050. srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
  1051. __kvmppc_vcore_entry(NULL, vcpu0);
  1052. spin_lock(&vc->lock);
  1053. /* disable sending of IPIs on virtual external irqs */
  1054. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1055. vcpu->cpu = -1;
  1056. /* wait for secondary threads to finish writing their state to memory */
  1057. if (vc->nap_count < vc->n_woken)
  1058. kvmppc_wait_for_nap(vc);
  1059. for (i = 0; i < threads_per_core; ++i)
  1060. kvmppc_release_hwthread(vc->pcpu + i);
  1061. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  1062. vc->vcore_state = VCORE_EXITING;
  1063. spin_unlock(&vc->lock);
  1064. srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
  1065. /* make sure updates to secondary vcpu structs are visible now */
  1066. smp_mb();
  1067. kvm_guest_exit();
  1068. preempt_enable();
  1069. kvm_resched(vcpu);
  1070. spin_lock(&vc->lock);
  1071. now = get_tb();
  1072. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1073. /* cancel pending dec exception if dec is positive */
  1074. if (now < vcpu->arch.dec_expires &&
  1075. kvmppc_core_pending_dec(vcpu))
  1076. kvmppc_core_dequeue_dec(vcpu);
  1077. ret = RESUME_GUEST;
  1078. if (vcpu->arch.trap)
  1079. ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
  1080. vcpu->arch.run_task);
  1081. vcpu->arch.ret = ret;
  1082. vcpu->arch.trap = 0;
  1083. if (vcpu->arch.ceded) {
  1084. if (ret != RESUME_GUEST)
  1085. kvmppc_end_cede(vcpu);
  1086. else
  1087. kvmppc_set_timer(vcpu);
  1088. }
  1089. }
  1090. out:
  1091. vc->vcore_state = VCORE_INACTIVE;
  1092. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  1093. arch.run_list) {
  1094. if (vcpu->arch.ret != RESUME_GUEST) {
  1095. kvmppc_remove_runnable(vc, vcpu);
  1096. wake_up(&vcpu->arch.cpu_run);
  1097. }
  1098. }
  1099. }
  1100. /*
  1101. * Wait for some other vcpu thread to execute us, and
  1102. * wake us up when we need to handle something in the host.
  1103. */
  1104. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  1105. {
  1106. DEFINE_WAIT(wait);
  1107. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  1108. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  1109. schedule();
  1110. finish_wait(&vcpu->arch.cpu_run, &wait);
  1111. }
  1112. /*
  1113. * All the vcpus in this vcore are idle, so wait for a decrementer
  1114. * or external interrupt to one of the vcpus. vc->lock is held.
  1115. */
  1116. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1117. {
  1118. DEFINE_WAIT(wait);
  1119. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1120. vc->vcore_state = VCORE_SLEEPING;
  1121. spin_unlock(&vc->lock);
  1122. schedule();
  1123. finish_wait(&vc->wq, &wait);
  1124. spin_lock(&vc->lock);
  1125. vc->vcore_state = VCORE_INACTIVE;
  1126. }
  1127. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1128. {
  1129. int n_ceded;
  1130. struct kvmppc_vcore *vc;
  1131. struct kvm_vcpu *v, *vn;
  1132. kvm_run->exit_reason = 0;
  1133. vcpu->arch.ret = RESUME_GUEST;
  1134. vcpu->arch.trap = 0;
  1135. kvmppc_update_vpas(vcpu);
  1136. /*
  1137. * Synchronize with other threads in this virtual core
  1138. */
  1139. vc = vcpu->arch.vcore;
  1140. spin_lock(&vc->lock);
  1141. vcpu->arch.ceded = 0;
  1142. vcpu->arch.run_task = current;
  1143. vcpu->arch.kvm_run = kvm_run;
  1144. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  1145. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  1146. vcpu->arch.busy_preempt = TB_NIL;
  1147. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  1148. ++vc->n_runnable;
  1149. /*
  1150. * This happens the first time this is called for a vcpu.
  1151. * If the vcore is already running, we may be able to start
  1152. * this thread straight away and have it join in.
  1153. */
  1154. if (!signal_pending(current)) {
  1155. if (vc->vcore_state == VCORE_RUNNING &&
  1156. VCORE_EXIT_COUNT(vc) == 0) {
  1157. vcpu->arch.ptid = vc->n_runnable - 1;
  1158. kvmppc_create_dtl_entry(vcpu, vc);
  1159. kvmppc_start_thread(vcpu);
  1160. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1161. wake_up(&vc->wq);
  1162. }
  1163. }
  1164. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1165. !signal_pending(current)) {
  1166. if (vc->vcore_state != VCORE_INACTIVE) {
  1167. spin_unlock(&vc->lock);
  1168. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  1169. spin_lock(&vc->lock);
  1170. continue;
  1171. }
  1172. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  1173. arch.run_list) {
  1174. kvmppc_core_prepare_to_enter(v);
  1175. if (signal_pending(v->arch.run_task)) {
  1176. kvmppc_remove_runnable(vc, v);
  1177. v->stat.signal_exits++;
  1178. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  1179. v->arch.ret = -EINTR;
  1180. wake_up(&v->arch.cpu_run);
  1181. }
  1182. }
  1183. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1184. break;
  1185. vc->runner = vcpu;
  1186. n_ceded = 0;
  1187. list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
  1188. if (!v->arch.pending_exceptions)
  1189. n_ceded += v->arch.ceded;
  1190. if (n_ceded == vc->n_runnable)
  1191. kvmppc_vcore_blocked(vc);
  1192. else
  1193. kvmppc_run_core(vc);
  1194. vc->runner = NULL;
  1195. }
  1196. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1197. (vc->vcore_state == VCORE_RUNNING ||
  1198. vc->vcore_state == VCORE_EXITING)) {
  1199. spin_unlock(&vc->lock);
  1200. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  1201. spin_lock(&vc->lock);
  1202. }
  1203. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  1204. kvmppc_remove_runnable(vc, vcpu);
  1205. vcpu->stat.signal_exits++;
  1206. kvm_run->exit_reason = KVM_EXIT_INTR;
  1207. vcpu->arch.ret = -EINTR;
  1208. }
  1209. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  1210. /* Wake up some vcpu to run the core */
  1211. v = list_first_entry(&vc->runnable_threads,
  1212. struct kvm_vcpu, arch.run_list);
  1213. wake_up(&v->arch.cpu_run);
  1214. }
  1215. spin_unlock(&vc->lock);
  1216. return vcpu->arch.ret;
  1217. }
  1218. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  1219. {
  1220. int r;
  1221. int srcu_idx;
  1222. if (!vcpu->arch.sane) {
  1223. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1224. return -EINVAL;
  1225. }
  1226. kvmppc_core_prepare_to_enter(vcpu);
  1227. /* No need to go into the guest when all we'll do is come back out */
  1228. if (signal_pending(current)) {
  1229. run->exit_reason = KVM_EXIT_INTR;
  1230. return -EINTR;
  1231. }
  1232. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  1233. /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
  1234. smp_mb();
  1235. /* On the first time here, set up HTAB and VRMA or RMA */
  1236. if (!vcpu->kvm->arch.rma_setup_done) {
  1237. r = kvmppc_hv_setup_htab_rma(vcpu);
  1238. if (r)
  1239. goto out;
  1240. }
  1241. flush_fp_to_thread(current);
  1242. flush_altivec_to_thread(current);
  1243. flush_vsx_to_thread(current);
  1244. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  1245. vcpu->arch.pgdir = current->mm->pgd;
  1246. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  1247. do {
  1248. r = kvmppc_run_vcpu(run, vcpu);
  1249. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  1250. !(vcpu->arch.shregs.msr & MSR_PR)) {
  1251. r = kvmppc_pseries_do_hcall(vcpu);
  1252. kvmppc_core_prepare_to_enter(vcpu);
  1253. } else if (r == RESUME_PAGE_FAULT) {
  1254. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1255. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  1256. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  1257. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1258. }
  1259. } while (r == RESUME_GUEST);
  1260. out:
  1261. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  1262. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  1263. return r;
  1264. }
  1265. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  1266. Assumes POWER7 or PPC970. */
  1267. static inline int lpcr_rmls(unsigned long rma_size)
  1268. {
  1269. switch (rma_size) {
  1270. case 32ul << 20: /* 32 MB */
  1271. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1272. return 8; /* only supported on POWER7 */
  1273. return -1;
  1274. case 64ul << 20: /* 64 MB */
  1275. return 3;
  1276. case 128ul << 20: /* 128 MB */
  1277. return 7;
  1278. case 256ul << 20: /* 256 MB */
  1279. return 4;
  1280. case 1ul << 30: /* 1 GB */
  1281. return 2;
  1282. case 16ul << 30: /* 16 GB */
  1283. return 1;
  1284. case 256ul << 30: /* 256 GB */
  1285. return 0;
  1286. default:
  1287. return -1;
  1288. }
  1289. }
  1290. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1291. {
  1292. struct kvmppc_linear_info *ri = vma->vm_file->private_data;
  1293. struct page *page;
  1294. if (vmf->pgoff >= ri->npages)
  1295. return VM_FAULT_SIGBUS;
  1296. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  1297. get_page(page);
  1298. vmf->page = page;
  1299. return 0;
  1300. }
  1301. static const struct vm_operations_struct kvm_rma_vm_ops = {
  1302. .fault = kvm_rma_fault,
  1303. };
  1304. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  1305. {
  1306. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1307. vma->vm_ops = &kvm_rma_vm_ops;
  1308. return 0;
  1309. }
  1310. static int kvm_rma_release(struct inode *inode, struct file *filp)
  1311. {
  1312. struct kvmppc_linear_info *ri = filp->private_data;
  1313. kvm_release_rma(ri);
  1314. return 0;
  1315. }
  1316. static struct file_operations kvm_rma_fops = {
  1317. .mmap = kvm_rma_mmap,
  1318. .release = kvm_rma_release,
  1319. };
  1320. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  1321. {
  1322. struct kvmppc_linear_info *ri;
  1323. long fd;
  1324. ri = kvm_alloc_rma();
  1325. if (!ri)
  1326. return -ENOMEM;
  1327. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
  1328. if (fd < 0)
  1329. kvm_release_rma(ri);
  1330. ret->rma_size = ri->npages << PAGE_SHIFT;
  1331. return fd;
  1332. }
  1333. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  1334. int linux_psize)
  1335. {
  1336. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  1337. if (!def->shift)
  1338. return;
  1339. (*sps)->page_shift = def->shift;
  1340. (*sps)->slb_enc = def->sllp;
  1341. (*sps)->enc[0].page_shift = def->shift;
  1342. (*sps)->enc[0].pte_enc = def->penc;
  1343. (*sps)++;
  1344. }
  1345. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1346. {
  1347. struct kvm_ppc_one_seg_page_size *sps;
  1348. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  1349. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1350. info->flags |= KVM_PPC_1T_SEGMENTS;
  1351. info->slb_size = mmu_slb_size;
  1352. /* We only support these sizes for now, and no muti-size segments */
  1353. sps = &info->sps[0];
  1354. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  1355. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  1356. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  1357. return 0;
  1358. }
  1359. /*
  1360. * Get (and clear) the dirty memory log for a memory slot.
  1361. */
  1362. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1363. {
  1364. struct kvm_memory_slot *memslot;
  1365. int r;
  1366. unsigned long n;
  1367. mutex_lock(&kvm->slots_lock);
  1368. r = -EINVAL;
  1369. if (log->slot >= KVM_MEMORY_SLOTS)
  1370. goto out;
  1371. memslot = id_to_memslot(kvm->memslots, log->slot);
  1372. r = -ENOENT;
  1373. if (!memslot->dirty_bitmap)
  1374. goto out;
  1375. n = kvm_dirty_bitmap_bytes(memslot);
  1376. memset(memslot->dirty_bitmap, 0, n);
  1377. r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
  1378. if (r)
  1379. goto out;
  1380. r = -EFAULT;
  1381. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  1382. goto out;
  1383. r = 0;
  1384. out:
  1385. mutex_unlock(&kvm->slots_lock);
  1386. return r;
  1387. }
  1388. static void unpin_slot(struct kvm_memory_slot *memslot)
  1389. {
  1390. unsigned long *physp;
  1391. unsigned long j, npages, pfn;
  1392. struct page *page;
  1393. physp = memslot->arch.slot_phys;
  1394. npages = memslot->npages;
  1395. if (!physp)
  1396. return;
  1397. for (j = 0; j < npages; j++) {
  1398. if (!(physp[j] & KVMPPC_GOT_PAGE))
  1399. continue;
  1400. pfn = physp[j] >> PAGE_SHIFT;
  1401. page = pfn_to_page(pfn);
  1402. SetPageDirty(page);
  1403. put_page(page);
  1404. }
  1405. }
  1406. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1407. struct kvm_memory_slot *dont)
  1408. {
  1409. if (!dont || free->arch.rmap != dont->arch.rmap) {
  1410. vfree(free->arch.rmap);
  1411. free->arch.rmap = NULL;
  1412. }
  1413. if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
  1414. unpin_slot(free);
  1415. vfree(free->arch.slot_phys);
  1416. free->arch.slot_phys = NULL;
  1417. }
  1418. }
  1419. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1420. unsigned long npages)
  1421. {
  1422. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  1423. if (!slot->arch.rmap)
  1424. return -ENOMEM;
  1425. slot->arch.slot_phys = NULL;
  1426. return 0;
  1427. }
  1428. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1429. struct kvm_memory_slot *memslot,
  1430. struct kvm_userspace_memory_region *mem)
  1431. {
  1432. unsigned long *phys;
  1433. /* Allocate a slot_phys array if needed */
  1434. phys = memslot->arch.slot_phys;
  1435. if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
  1436. phys = vzalloc(memslot->npages * sizeof(unsigned long));
  1437. if (!phys)
  1438. return -ENOMEM;
  1439. memslot->arch.slot_phys = phys;
  1440. }
  1441. return 0;
  1442. }
  1443. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1444. struct kvm_userspace_memory_region *mem,
  1445. struct kvm_memory_slot old)
  1446. {
  1447. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  1448. struct kvm_memory_slot *memslot;
  1449. if (npages && old.npages) {
  1450. /*
  1451. * If modifying a memslot, reset all the rmap dirty bits.
  1452. * If this is a new memslot, we don't need to do anything
  1453. * since the rmap array starts out as all zeroes,
  1454. * i.e. no pages are dirty.
  1455. */
  1456. memslot = id_to_memslot(kvm->memslots, mem->slot);
  1457. kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
  1458. }
  1459. }
  1460. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  1461. {
  1462. int err = 0;
  1463. struct kvm *kvm = vcpu->kvm;
  1464. struct kvmppc_linear_info *ri = NULL;
  1465. unsigned long hva;
  1466. struct kvm_memory_slot *memslot;
  1467. struct vm_area_struct *vma;
  1468. unsigned long lpcr, senc;
  1469. unsigned long psize, porder;
  1470. unsigned long rma_size;
  1471. unsigned long rmls;
  1472. unsigned long *physp;
  1473. unsigned long i, npages;
  1474. int srcu_idx;
  1475. mutex_lock(&kvm->lock);
  1476. if (kvm->arch.rma_setup_done)
  1477. goto out; /* another vcpu beat us to it */
  1478. /* Allocate hashed page table (if not done already) and reset it */
  1479. if (!kvm->arch.hpt_virt) {
  1480. err = kvmppc_alloc_hpt(kvm, NULL);
  1481. if (err) {
  1482. pr_err("KVM: Couldn't alloc HPT\n");
  1483. goto out;
  1484. }
  1485. }
  1486. /* Look up the memslot for guest physical address 0 */
  1487. srcu_idx = srcu_read_lock(&kvm->srcu);
  1488. memslot = gfn_to_memslot(kvm, 0);
  1489. /* We must have some memory at 0 by now */
  1490. err = -EINVAL;
  1491. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1492. goto out_srcu;
  1493. /* Look up the VMA for the start of this memory slot */
  1494. hva = memslot->userspace_addr;
  1495. down_read(&current->mm->mmap_sem);
  1496. vma = find_vma(current->mm, hva);
  1497. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  1498. goto up_out;
  1499. psize = vma_kernel_pagesize(vma);
  1500. porder = __ilog2(psize);
  1501. /* Is this one of our preallocated RMAs? */
  1502. if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
  1503. hva == vma->vm_start)
  1504. ri = vma->vm_file->private_data;
  1505. up_read(&current->mm->mmap_sem);
  1506. if (!ri) {
  1507. /* On POWER7, use VRMA; on PPC970, give up */
  1508. err = -EPERM;
  1509. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1510. pr_err("KVM: CPU requires an RMO\n");
  1511. goto out_srcu;
  1512. }
  1513. /* We can handle 4k, 64k or 16M pages in the VRMA */
  1514. err = -EINVAL;
  1515. if (!(psize == 0x1000 || psize == 0x10000 ||
  1516. psize == 0x1000000))
  1517. goto out_srcu;
  1518. /* Update VRMASD field in the LPCR */
  1519. senc = slb_pgsize_encoding(psize);
  1520. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1521. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1522. lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
  1523. lpcr |= senc << (LPCR_VRMASD_SH - 4);
  1524. kvm->arch.lpcr = lpcr;
  1525. /* Create HPTEs in the hash page table for the VRMA */
  1526. kvmppc_map_vrma(vcpu, memslot, porder);
  1527. } else {
  1528. /* Set up to use an RMO region */
  1529. rma_size = ri->npages;
  1530. if (rma_size > memslot->npages)
  1531. rma_size = memslot->npages;
  1532. rma_size <<= PAGE_SHIFT;
  1533. rmls = lpcr_rmls(rma_size);
  1534. err = -EINVAL;
  1535. if (rmls < 0) {
  1536. pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
  1537. goto out_srcu;
  1538. }
  1539. atomic_inc(&ri->use_count);
  1540. kvm->arch.rma = ri;
  1541. /* Update LPCR and RMOR */
  1542. lpcr = kvm->arch.lpcr;
  1543. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1544. /* PPC970; insert RMLS value (split field) in HID4 */
  1545. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  1546. (3ul << HID4_RMLS2_SH));
  1547. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  1548. ((rmls & 3) << HID4_RMLS2_SH);
  1549. /* RMOR is also in HID4 */
  1550. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1551. << HID4_RMOR_SH;
  1552. } else {
  1553. /* POWER7 */
  1554. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1555. lpcr |= rmls << LPCR_RMLS_SH;
  1556. kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
  1557. }
  1558. kvm->arch.lpcr = lpcr;
  1559. pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
  1560. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1561. /* Initialize phys addrs of pages in RMO */
  1562. npages = ri->npages;
  1563. porder = __ilog2(npages);
  1564. physp = memslot->arch.slot_phys;
  1565. if (physp) {
  1566. if (npages > memslot->npages)
  1567. npages = memslot->npages;
  1568. spin_lock(&kvm->arch.slot_phys_lock);
  1569. for (i = 0; i < npages; ++i)
  1570. physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
  1571. porder;
  1572. spin_unlock(&kvm->arch.slot_phys_lock);
  1573. }
  1574. }
  1575. /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
  1576. smp_wmb();
  1577. kvm->arch.rma_setup_done = 1;
  1578. err = 0;
  1579. out_srcu:
  1580. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1581. out:
  1582. mutex_unlock(&kvm->lock);
  1583. return err;
  1584. up_out:
  1585. up_read(&current->mm->mmap_sem);
  1586. goto out;
  1587. }
  1588. int kvmppc_core_init_vm(struct kvm *kvm)
  1589. {
  1590. unsigned long lpcr, lpid;
  1591. /* Allocate the guest's logical partition ID */
  1592. lpid = kvmppc_alloc_lpid();
  1593. if (lpid < 0)
  1594. return -ENOMEM;
  1595. kvm->arch.lpid = lpid;
  1596. /*
  1597. * Since we don't flush the TLB when tearing down a VM,
  1598. * and this lpid might have previously been used,
  1599. * make sure we flush on each core before running the new VM.
  1600. */
  1601. cpumask_setall(&kvm->arch.need_tlb_flush);
  1602. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1603. kvm->arch.rma = NULL;
  1604. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1605. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1606. /* PPC970; HID4 is effectively the LPCR */
  1607. kvm->arch.host_lpid = 0;
  1608. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1609. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1610. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1611. ((lpid & 0xf) << HID4_LPID5_SH);
  1612. } else {
  1613. /* POWER7; init LPCR for virtual RMA mode */
  1614. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1615. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1616. lpcr &= LPCR_PECE | LPCR_LPES;
  1617. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1618. LPCR_VPM0 | LPCR_VPM1;
  1619. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  1620. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1621. }
  1622. kvm->arch.lpcr = lpcr;
  1623. kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
  1624. spin_lock_init(&kvm->arch.slot_phys_lock);
  1625. /*
  1626. * Don't allow secondary CPU threads to come online
  1627. * while any KVM VMs exist.
  1628. */
  1629. inhibit_secondary_onlining();
  1630. return 0;
  1631. }
  1632. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1633. {
  1634. uninhibit_secondary_onlining();
  1635. if (kvm->arch.rma) {
  1636. kvm_release_rma(kvm->arch.rma);
  1637. kvm->arch.rma = NULL;
  1638. }
  1639. kvmppc_free_hpt(kvm);
  1640. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1641. }
  1642. /* These are stubs for now */
  1643. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1644. {
  1645. }
  1646. /* We don't need to emulate any privileged instructions or dcbz */
  1647. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1648. unsigned int inst, int *advance)
  1649. {
  1650. return EMULATE_FAIL;
  1651. }
  1652. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  1653. {
  1654. return EMULATE_FAIL;
  1655. }
  1656. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  1657. {
  1658. return EMULATE_FAIL;
  1659. }
  1660. static int kvmppc_book3s_hv_init(void)
  1661. {
  1662. int r;
  1663. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1664. if (r)
  1665. return r;
  1666. r = kvmppc_mmu_hv_init();
  1667. return r;
  1668. }
  1669. static void kvmppc_book3s_hv_exit(void)
  1670. {
  1671. kvm_exit();
  1672. }
  1673. module_init(kvmppc_book3s_hv_init);
  1674. module_exit(kvmppc_book3s_hv_exit);