book3s_hv.c 45 KB

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