book3s_hv.c 41 KB

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