book3s_hv.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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 kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  490. {
  491. int r = -EINVAL;
  492. switch (reg->id) {
  493. case KVM_REG_PPC_HIOR:
  494. r = put_user(0, (u64 __user *)reg->addr);
  495. break;
  496. default:
  497. break;
  498. }
  499. return r;
  500. }
  501. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  502. {
  503. int r = -EINVAL;
  504. switch (reg->id) {
  505. case KVM_REG_PPC_HIOR:
  506. {
  507. u64 hior;
  508. /* Only allow this to be set to zero */
  509. r = get_user(hior, (u64 __user *)reg->addr);
  510. if (!r && (hior != 0))
  511. r = -EINVAL;
  512. break;
  513. }
  514. default:
  515. break;
  516. }
  517. return r;
  518. }
  519. int kvmppc_core_check_processor_compat(void)
  520. {
  521. if (cpu_has_feature(CPU_FTR_HVMODE))
  522. return 0;
  523. return -EIO;
  524. }
  525. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  526. {
  527. struct kvm_vcpu *vcpu;
  528. int err = -EINVAL;
  529. int core;
  530. struct kvmppc_vcore *vcore;
  531. core = id / threads_per_core;
  532. if (core >= KVM_MAX_VCORES)
  533. goto out;
  534. err = -ENOMEM;
  535. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  536. if (!vcpu)
  537. goto out;
  538. err = kvm_vcpu_init(vcpu, kvm, id);
  539. if (err)
  540. goto free_vcpu;
  541. vcpu->arch.shared = &vcpu->arch.shregs;
  542. vcpu->arch.last_cpu = -1;
  543. vcpu->arch.mmcr[0] = MMCR0_FC;
  544. vcpu->arch.ctrl = CTRL_RUNLATCH;
  545. /* default to host PVR, since we can't spoof it */
  546. vcpu->arch.pvr = mfspr(SPRN_PVR);
  547. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  548. spin_lock_init(&vcpu->arch.vpa_update_lock);
  549. kvmppc_mmu_book3s_hv_init(vcpu);
  550. /*
  551. * We consider the vcpu stopped until we see the first run ioctl for it.
  552. */
  553. vcpu->arch.state = KVMPPC_VCPU_STOPPED;
  554. init_waitqueue_head(&vcpu->arch.cpu_run);
  555. mutex_lock(&kvm->lock);
  556. vcore = kvm->arch.vcores[core];
  557. if (!vcore) {
  558. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  559. if (vcore) {
  560. INIT_LIST_HEAD(&vcore->runnable_threads);
  561. spin_lock_init(&vcore->lock);
  562. init_waitqueue_head(&vcore->wq);
  563. vcore->preempt_tb = mftb();
  564. }
  565. kvm->arch.vcores[core] = vcore;
  566. }
  567. mutex_unlock(&kvm->lock);
  568. if (!vcore)
  569. goto free_vcpu;
  570. spin_lock(&vcore->lock);
  571. ++vcore->num_threads;
  572. spin_unlock(&vcore->lock);
  573. vcpu->arch.vcore = vcore;
  574. vcpu->arch.stolen_logged = vcore->stolen_tb;
  575. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  576. kvmppc_sanity_check(vcpu);
  577. return vcpu;
  578. free_vcpu:
  579. kmem_cache_free(kvm_vcpu_cache, vcpu);
  580. out:
  581. return ERR_PTR(err);
  582. }
  583. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  584. {
  585. spin_lock(&vcpu->arch.vpa_update_lock);
  586. if (vcpu->arch.dtl.pinned_addr)
  587. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
  588. if (vcpu->arch.slb_shadow.pinned_addr)
  589. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
  590. if (vcpu->arch.vpa.pinned_addr)
  591. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
  592. spin_unlock(&vcpu->arch.vpa_update_lock);
  593. kvm_vcpu_uninit(vcpu);
  594. kmem_cache_free(kvm_vcpu_cache, vcpu);
  595. }
  596. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  597. {
  598. unsigned long dec_nsec, now;
  599. now = get_tb();
  600. if (now > vcpu->arch.dec_expires) {
  601. /* decrementer has already gone negative */
  602. kvmppc_core_queue_dec(vcpu);
  603. kvmppc_core_prepare_to_enter(vcpu);
  604. return;
  605. }
  606. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  607. / tb_ticks_per_sec;
  608. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  609. HRTIMER_MODE_REL);
  610. vcpu->arch.timer_running = 1;
  611. }
  612. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  613. {
  614. vcpu->arch.ceded = 0;
  615. if (vcpu->arch.timer_running) {
  616. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  617. vcpu->arch.timer_running = 0;
  618. }
  619. }
  620. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  621. extern void xics_wake_cpu(int cpu);
  622. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  623. struct kvm_vcpu *vcpu)
  624. {
  625. struct kvm_vcpu *v;
  626. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  627. return;
  628. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  629. --vc->n_runnable;
  630. ++vc->n_busy;
  631. /* decrement the physical thread id of each following vcpu */
  632. v = vcpu;
  633. list_for_each_entry_continue(v, &vc->runnable_threads, arch.run_list)
  634. --v->arch.ptid;
  635. list_del(&vcpu->arch.run_list);
  636. }
  637. static int kvmppc_grab_hwthread(int cpu)
  638. {
  639. struct paca_struct *tpaca;
  640. long timeout = 1000;
  641. tpaca = &paca[cpu];
  642. /* Ensure the thread won't go into the kernel if it wakes */
  643. tpaca->kvm_hstate.hwthread_req = 1;
  644. /*
  645. * If the thread is already executing in the kernel (e.g. handling
  646. * a stray interrupt), wait for it to get back to nap mode.
  647. * The smp_mb() is to ensure that our setting of hwthread_req
  648. * is visible before we look at hwthread_state, so if this
  649. * races with the code at system_reset_pSeries and the thread
  650. * misses our setting of hwthread_req, we are sure to see its
  651. * setting of hwthread_state, and vice versa.
  652. */
  653. smp_mb();
  654. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  655. if (--timeout <= 0) {
  656. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  657. return -EBUSY;
  658. }
  659. udelay(1);
  660. }
  661. return 0;
  662. }
  663. static void kvmppc_release_hwthread(int cpu)
  664. {
  665. struct paca_struct *tpaca;
  666. tpaca = &paca[cpu];
  667. tpaca->kvm_hstate.hwthread_req = 0;
  668. tpaca->kvm_hstate.kvm_vcpu = NULL;
  669. }
  670. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  671. {
  672. int cpu;
  673. struct paca_struct *tpaca;
  674. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  675. if (vcpu->arch.timer_running) {
  676. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  677. vcpu->arch.timer_running = 0;
  678. }
  679. cpu = vc->pcpu + vcpu->arch.ptid;
  680. tpaca = &paca[cpu];
  681. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  682. tpaca->kvm_hstate.kvm_vcore = vc;
  683. tpaca->kvm_hstate.napping = 0;
  684. vcpu->cpu = vc->pcpu;
  685. smp_wmb();
  686. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  687. if (vcpu->arch.ptid) {
  688. kvmppc_grab_hwthread(cpu);
  689. xics_wake_cpu(cpu);
  690. ++vc->n_woken;
  691. }
  692. #endif
  693. }
  694. static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
  695. {
  696. int i;
  697. HMT_low();
  698. i = 0;
  699. while (vc->nap_count < vc->n_woken) {
  700. if (++i >= 1000000) {
  701. pr_err("kvmppc_wait_for_nap timeout %d %d\n",
  702. vc->nap_count, vc->n_woken);
  703. break;
  704. }
  705. cpu_relax();
  706. }
  707. HMT_medium();
  708. }
  709. /*
  710. * Check that we are on thread 0 and that any other threads in
  711. * this core are off-line.
  712. */
  713. static int on_primary_thread(void)
  714. {
  715. int cpu = smp_processor_id();
  716. int thr = cpu_thread_in_core(cpu);
  717. if (thr)
  718. return 0;
  719. while (++thr < threads_per_core)
  720. if (cpu_online(cpu + thr))
  721. return 0;
  722. return 1;
  723. }
  724. /*
  725. * Run a set of guest threads on a physical core.
  726. * Called with vc->lock held.
  727. */
  728. static int kvmppc_run_core(struct kvmppc_vcore *vc)
  729. {
  730. struct kvm_vcpu *vcpu, *vcpu0, *vnext;
  731. long ret;
  732. u64 now;
  733. int ptid, i, need_vpa_update;
  734. int srcu_idx;
  735. /* don't start if any threads have a signal pending */
  736. need_vpa_update = 0;
  737. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  738. if (signal_pending(vcpu->arch.run_task))
  739. return 0;
  740. need_vpa_update |= vcpu->arch.vpa.update_pending |
  741. vcpu->arch.slb_shadow.update_pending |
  742. vcpu->arch.dtl.update_pending;
  743. }
  744. /*
  745. * Initialize *vc, in particular vc->vcore_state, so we can
  746. * drop the vcore lock if necessary.
  747. */
  748. vc->n_woken = 0;
  749. vc->nap_count = 0;
  750. vc->entry_exit_count = 0;
  751. vc->vcore_state = VCORE_RUNNING;
  752. vc->in_guest = 0;
  753. vc->napping_threads = 0;
  754. /*
  755. * Updating any of the vpas requires calling kvmppc_pin_guest_page,
  756. * which can't be called with any spinlocks held.
  757. */
  758. if (need_vpa_update) {
  759. spin_unlock(&vc->lock);
  760. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  761. kvmppc_update_vpas(vcpu);
  762. spin_lock(&vc->lock);
  763. }
  764. /*
  765. * Make sure we are running on thread 0, and that
  766. * secondary threads are offline.
  767. * XXX we should also block attempts to bring any
  768. * secondary threads online.
  769. */
  770. if (threads_per_core > 1 && !on_primary_thread()) {
  771. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  772. vcpu->arch.ret = -EBUSY;
  773. goto out;
  774. }
  775. /*
  776. * Assign physical thread IDs, first to non-ceded vcpus
  777. * and then to ceded ones.
  778. */
  779. ptid = 0;
  780. vcpu0 = NULL;
  781. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  782. if (!vcpu->arch.ceded) {
  783. if (!ptid)
  784. vcpu0 = vcpu;
  785. vcpu->arch.ptid = ptid++;
  786. }
  787. }
  788. if (!vcpu0)
  789. return 0; /* nothing to run */
  790. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  791. if (vcpu->arch.ceded)
  792. vcpu->arch.ptid = ptid++;
  793. vc->stolen_tb += mftb() - vc->preempt_tb;
  794. vc->pcpu = smp_processor_id();
  795. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  796. kvmppc_start_thread(vcpu);
  797. kvmppc_create_dtl_entry(vcpu, vc);
  798. }
  799. /* Grab any remaining hw threads so they can't go into the kernel */
  800. for (i = ptid; i < threads_per_core; ++i)
  801. kvmppc_grab_hwthread(vc->pcpu + i);
  802. preempt_disable();
  803. spin_unlock(&vc->lock);
  804. kvm_guest_enter();
  805. srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
  806. __kvmppc_vcore_entry(NULL, vcpu0);
  807. for (i = 0; i < threads_per_core; ++i)
  808. kvmppc_release_hwthread(vc->pcpu + i);
  809. spin_lock(&vc->lock);
  810. /* disable sending of IPIs on virtual external irqs */
  811. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  812. vcpu->cpu = -1;
  813. /* wait for secondary threads to finish writing their state to memory */
  814. if (vc->nap_count < vc->n_woken)
  815. kvmppc_wait_for_nap(vc);
  816. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  817. vc->vcore_state = VCORE_EXITING;
  818. spin_unlock(&vc->lock);
  819. srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
  820. /* make sure updates to secondary vcpu structs are visible now */
  821. smp_mb();
  822. kvm_guest_exit();
  823. preempt_enable();
  824. kvm_resched(vcpu);
  825. now = get_tb();
  826. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  827. /* cancel pending dec exception if dec is positive */
  828. if (now < vcpu->arch.dec_expires &&
  829. kvmppc_core_pending_dec(vcpu))
  830. kvmppc_core_dequeue_dec(vcpu);
  831. ret = RESUME_GUEST;
  832. if (vcpu->arch.trap)
  833. ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
  834. vcpu->arch.run_task);
  835. vcpu->arch.ret = ret;
  836. vcpu->arch.trap = 0;
  837. if (vcpu->arch.ceded) {
  838. if (ret != RESUME_GUEST)
  839. kvmppc_end_cede(vcpu);
  840. else
  841. kvmppc_set_timer(vcpu);
  842. }
  843. }
  844. spin_lock(&vc->lock);
  845. out:
  846. vc->vcore_state = VCORE_INACTIVE;
  847. vc->preempt_tb = mftb();
  848. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  849. arch.run_list) {
  850. if (vcpu->arch.ret != RESUME_GUEST) {
  851. kvmppc_remove_runnable(vc, vcpu);
  852. wake_up(&vcpu->arch.cpu_run);
  853. }
  854. }
  855. return 1;
  856. }
  857. /*
  858. * Wait for some other vcpu thread to execute us, and
  859. * wake us up when we need to handle something in the host.
  860. */
  861. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  862. {
  863. DEFINE_WAIT(wait);
  864. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  865. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  866. schedule();
  867. finish_wait(&vcpu->arch.cpu_run, &wait);
  868. }
  869. /*
  870. * All the vcpus in this vcore are idle, so wait for a decrementer
  871. * or external interrupt to one of the vcpus. vc->lock is held.
  872. */
  873. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  874. {
  875. DEFINE_WAIT(wait);
  876. struct kvm_vcpu *v;
  877. int all_idle = 1;
  878. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  879. vc->vcore_state = VCORE_SLEEPING;
  880. spin_unlock(&vc->lock);
  881. list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
  882. if (!v->arch.ceded || v->arch.pending_exceptions) {
  883. all_idle = 0;
  884. break;
  885. }
  886. }
  887. if (all_idle)
  888. schedule();
  889. finish_wait(&vc->wq, &wait);
  890. spin_lock(&vc->lock);
  891. vc->vcore_state = VCORE_INACTIVE;
  892. }
  893. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  894. {
  895. int n_ceded;
  896. int prev_state;
  897. struct kvmppc_vcore *vc;
  898. struct kvm_vcpu *v, *vn;
  899. kvm_run->exit_reason = 0;
  900. vcpu->arch.ret = RESUME_GUEST;
  901. vcpu->arch.trap = 0;
  902. /*
  903. * Synchronize with other threads in this virtual core
  904. */
  905. vc = vcpu->arch.vcore;
  906. spin_lock(&vc->lock);
  907. vcpu->arch.ceded = 0;
  908. vcpu->arch.run_task = current;
  909. vcpu->arch.kvm_run = kvm_run;
  910. prev_state = vcpu->arch.state;
  911. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  912. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  913. ++vc->n_runnable;
  914. /*
  915. * This happens the first time this is called for a vcpu.
  916. * If the vcore is already running, we may be able to start
  917. * this thread straight away and have it join in.
  918. */
  919. if (prev_state == KVMPPC_VCPU_STOPPED) {
  920. if (vc->vcore_state == VCORE_RUNNING &&
  921. VCORE_EXIT_COUNT(vc) == 0) {
  922. vcpu->arch.ptid = vc->n_runnable - 1;
  923. kvmppc_start_thread(vcpu);
  924. }
  925. } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
  926. --vc->n_busy;
  927. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  928. !signal_pending(current)) {
  929. if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
  930. spin_unlock(&vc->lock);
  931. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  932. spin_lock(&vc->lock);
  933. continue;
  934. }
  935. vc->runner = vcpu;
  936. n_ceded = 0;
  937. list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
  938. n_ceded += v->arch.ceded;
  939. if (n_ceded == vc->n_runnable)
  940. kvmppc_vcore_blocked(vc);
  941. else
  942. kvmppc_run_core(vc);
  943. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  944. arch.run_list) {
  945. kvmppc_core_prepare_to_enter(v);
  946. if (signal_pending(v->arch.run_task)) {
  947. kvmppc_remove_runnable(vc, v);
  948. v->stat.signal_exits++;
  949. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  950. v->arch.ret = -EINTR;
  951. wake_up(&v->arch.cpu_run);
  952. }
  953. }
  954. vc->runner = NULL;
  955. }
  956. if (signal_pending(current)) {
  957. if (vc->vcore_state == VCORE_RUNNING ||
  958. vc->vcore_state == VCORE_EXITING) {
  959. spin_unlock(&vc->lock);
  960. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  961. spin_lock(&vc->lock);
  962. }
  963. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  964. kvmppc_remove_runnable(vc, vcpu);
  965. vcpu->stat.signal_exits++;
  966. kvm_run->exit_reason = KVM_EXIT_INTR;
  967. vcpu->arch.ret = -EINTR;
  968. }
  969. }
  970. spin_unlock(&vc->lock);
  971. return vcpu->arch.ret;
  972. }
  973. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  974. {
  975. int r;
  976. if (!vcpu->arch.sane) {
  977. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  978. return -EINVAL;
  979. }
  980. kvmppc_core_prepare_to_enter(vcpu);
  981. /* No need to go into the guest when all we'll do is come back out */
  982. if (signal_pending(current)) {
  983. run->exit_reason = KVM_EXIT_INTR;
  984. return -EINTR;
  985. }
  986. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  987. /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
  988. smp_mb();
  989. /* On the first time here, set up HTAB and VRMA or RMA */
  990. if (!vcpu->kvm->arch.rma_setup_done) {
  991. r = kvmppc_hv_setup_htab_rma(vcpu);
  992. if (r)
  993. goto out;
  994. }
  995. flush_fp_to_thread(current);
  996. flush_altivec_to_thread(current);
  997. flush_vsx_to_thread(current);
  998. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  999. vcpu->arch.pgdir = current->mm->pgd;
  1000. do {
  1001. r = kvmppc_run_vcpu(run, vcpu);
  1002. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  1003. !(vcpu->arch.shregs.msr & MSR_PR)) {
  1004. r = kvmppc_pseries_do_hcall(vcpu);
  1005. kvmppc_core_prepare_to_enter(vcpu);
  1006. }
  1007. } while (r == RESUME_GUEST);
  1008. out:
  1009. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  1010. return r;
  1011. }
  1012. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  1013. Assumes POWER7 or PPC970. */
  1014. static inline int lpcr_rmls(unsigned long rma_size)
  1015. {
  1016. switch (rma_size) {
  1017. case 32ul << 20: /* 32 MB */
  1018. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1019. return 8; /* only supported on POWER7 */
  1020. return -1;
  1021. case 64ul << 20: /* 64 MB */
  1022. return 3;
  1023. case 128ul << 20: /* 128 MB */
  1024. return 7;
  1025. case 256ul << 20: /* 256 MB */
  1026. return 4;
  1027. case 1ul << 30: /* 1 GB */
  1028. return 2;
  1029. case 16ul << 30: /* 16 GB */
  1030. return 1;
  1031. case 256ul << 30: /* 256 GB */
  1032. return 0;
  1033. default:
  1034. return -1;
  1035. }
  1036. }
  1037. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1038. {
  1039. struct kvmppc_linear_info *ri = vma->vm_file->private_data;
  1040. struct page *page;
  1041. if (vmf->pgoff >= ri->npages)
  1042. return VM_FAULT_SIGBUS;
  1043. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  1044. get_page(page);
  1045. vmf->page = page;
  1046. return 0;
  1047. }
  1048. static const struct vm_operations_struct kvm_rma_vm_ops = {
  1049. .fault = kvm_rma_fault,
  1050. };
  1051. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  1052. {
  1053. vma->vm_flags |= VM_RESERVED;
  1054. vma->vm_ops = &kvm_rma_vm_ops;
  1055. return 0;
  1056. }
  1057. static int kvm_rma_release(struct inode *inode, struct file *filp)
  1058. {
  1059. struct kvmppc_linear_info *ri = filp->private_data;
  1060. kvm_release_rma(ri);
  1061. return 0;
  1062. }
  1063. static struct file_operations kvm_rma_fops = {
  1064. .mmap = kvm_rma_mmap,
  1065. .release = kvm_rma_release,
  1066. };
  1067. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  1068. {
  1069. struct kvmppc_linear_info *ri;
  1070. long fd;
  1071. ri = kvm_alloc_rma();
  1072. if (!ri)
  1073. return -ENOMEM;
  1074. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
  1075. if (fd < 0)
  1076. kvm_release_rma(ri);
  1077. ret->rma_size = ri->npages << PAGE_SHIFT;
  1078. return fd;
  1079. }
  1080. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  1081. int linux_psize)
  1082. {
  1083. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  1084. if (!def->shift)
  1085. return;
  1086. (*sps)->page_shift = def->shift;
  1087. (*sps)->slb_enc = def->sllp;
  1088. (*sps)->enc[0].page_shift = def->shift;
  1089. (*sps)->enc[0].pte_enc = def->penc;
  1090. (*sps)++;
  1091. }
  1092. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1093. {
  1094. struct kvm_ppc_one_seg_page_size *sps;
  1095. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  1096. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1097. info->flags |= KVM_PPC_1T_SEGMENTS;
  1098. info->slb_size = mmu_slb_size;
  1099. /* We only support these sizes for now, and no muti-size segments */
  1100. sps = &info->sps[0];
  1101. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  1102. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  1103. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  1104. return 0;
  1105. }
  1106. /*
  1107. * Get (and clear) the dirty memory log for a memory slot.
  1108. */
  1109. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1110. {
  1111. struct kvm_memory_slot *memslot;
  1112. int r;
  1113. unsigned long n;
  1114. mutex_lock(&kvm->slots_lock);
  1115. r = -EINVAL;
  1116. if (log->slot >= KVM_MEMORY_SLOTS)
  1117. goto out;
  1118. memslot = id_to_memslot(kvm->memslots, log->slot);
  1119. r = -ENOENT;
  1120. if (!memslot->dirty_bitmap)
  1121. goto out;
  1122. n = kvm_dirty_bitmap_bytes(memslot);
  1123. memset(memslot->dirty_bitmap, 0, n);
  1124. r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
  1125. if (r)
  1126. goto out;
  1127. r = -EFAULT;
  1128. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  1129. goto out;
  1130. r = 0;
  1131. out:
  1132. mutex_unlock(&kvm->slots_lock);
  1133. return r;
  1134. }
  1135. static unsigned long slb_pgsize_encoding(unsigned long psize)
  1136. {
  1137. unsigned long senc = 0;
  1138. if (psize > 0x1000) {
  1139. senc = SLB_VSID_L;
  1140. if (psize == 0x10000)
  1141. senc |= SLB_VSID_LP_01;
  1142. }
  1143. return senc;
  1144. }
  1145. static void unpin_slot(struct kvm_memory_slot *memslot)
  1146. {
  1147. unsigned long *physp;
  1148. unsigned long j, npages, pfn;
  1149. struct page *page;
  1150. physp = memslot->arch.slot_phys;
  1151. npages = memslot->npages;
  1152. if (!physp)
  1153. return;
  1154. for (j = 0; j < npages; j++) {
  1155. if (!(physp[j] & KVMPPC_GOT_PAGE))
  1156. continue;
  1157. pfn = physp[j] >> PAGE_SHIFT;
  1158. page = pfn_to_page(pfn);
  1159. SetPageDirty(page);
  1160. put_page(page);
  1161. }
  1162. }
  1163. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1164. struct kvm_memory_slot *dont)
  1165. {
  1166. if (!dont || free->arch.rmap != dont->arch.rmap) {
  1167. vfree(free->arch.rmap);
  1168. free->arch.rmap = NULL;
  1169. }
  1170. if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
  1171. unpin_slot(free);
  1172. vfree(free->arch.slot_phys);
  1173. free->arch.slot_phys = NULL;
  1174. }
  1175. }
  1176. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1177. unsigned long npages)
  1178. {
  1179. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  1180. if (!slot->arch.rmap)
  1181. return -ENOMEM;
  1182. slot->arch.slot_phys = NULL;
  1183. return 0;
  1184. }
  1185. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1186. struct kvm_memory_slot *memslot,
  1187. struct kvm_userspace_memory_region *mem)
  1188. {
  1189. unsigned long *phys;
  1190. /* Allocate a slot_phys array if needed */
  1191. phys = memslot->arch.slot_phys;
  1192. if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
  1193. phys = vzalloc(memslot->npages * sizeof(unsigned long));
  1194. if (!phys)
  1195. return -ENOMEM;
  1196. memslot->arch.slot_phys = phys;
  1197. }
  1198. return 0;
  1199. }
  1200. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1201. struct kvm_userspace_memory_region *mem,
  1202. struct kvm_memory_slot old)
  1203. {
  1204. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  1205. struct kvm_memory_slot *memslot;
  1206. if (npages && old.npages) {
  1207. /*
  1208. * If modifying a memslot, reset all the rmap dirty bits.
  1209. * If this is a new memslot, we don't need to do anything
  1210. * since the rmap array starts out as all zeroes,
  1211. * i.e. no pages are dirty.
  1212. */
  1213. memslot = id_to_memslot(kvm->memslots, mem->slot);
  1214. kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
  1215. }
  1216. }
  1217. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  1218. {
  1219. int err = 0;
  1220. struct kvm *kvm = vcpu->kvm;
  1221. struct kvmppc_linear_info *ri = NULL;
  1222. unsigned long hva;
  1223. struct kvm_memory_slot *memslot;
  1224. struct vm_area_struct *vma;
  1225. unsigned long lpcr, senc;
  1226. unsigned long psize, porder;
  1227. unsigned long rma_size;
  1228. unsigned long rmls;
  1229. unsigned long *physp;
  1230. unsigned long i, npages;
  1231. int srcu_idx;
  1232. mutex_lock(&kvm->lock);
  1233. if (kvm->arch.rma_setup_done)
  1234. goto out; /* another vcpu beat us to it */
  1235. /* Allocate hashed page table (if not done already) and reset it */
  1236. if (!kvm->arch.hpt_virt) {
  1237. err = kvmppc_alloc_hpt(kvm, NULL);
  1238. if (err) {
  1239. pr_err("KVM: Couldn't alloc HPT\n");
  1240. goto out;
  1241. }
  1242. }
  1243. /* Look up the memslot for guest physical address 0 */
  1244. srcu_idx = srcu_read_lock(&kvm->srcu);
  1245. memslot = gfn_to_memslot(kvm, 0);
  1246. /* We must have some memory at 0 by now */
  1247. err = -EINVAL;
  1248. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1249. goto out_srcu;
  1250. /* Look up the VMA for the start of this memory slot */
  1251. hva = memslot->userspace_addr;
  1252. down_read(&current->mm->mmap_sem);
  1253. vma = find_vma(current->mm, hva);
  1254. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  1255. goto up_out;
  1256. psize = vma_kernel_pagesize(vma);
  1257. porder = __ilog2(psize);
  1258. /* Is this one of our preallocated RMAs? */
  1259. if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
  1260. hva == vma->vm_start)
  1261. ri = vma->vm_file->private_data;
  1262. up_read(&current->mm->mmap_sem);
  1263. if (!ri) {
  1264. /* On POWER7, use VRMA; on PPC970, give up */
  1265. err = -EPERM;
  1266. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1267. pr_err("KVM: CPU requires an RMO\n");
  1268. goto out_srcu;
  1269. }
  1270. /* We can handle 4k, 64k or 16M pages in the VRMA */
  1271. err = -EINVAL;
  1272. if (!(psize == 0x1000 || psize == 0x10000 ||
  1273. psize == 0x1000000))
  1274. goto out_srcu;
  1275. /* Update VRMASD field in the LPCR */
  1276. senc = slb_pgsize_encoding(psize);
  1277. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1278. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1279. lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
  1280. lpcr |= senc << (LPCR_VRMASD_SH - 4);
  1281. kvm->arch.lpcr = lpcr;
  1282. /* Create HPTEs in the hash page table for the VRMA */
  1283. kvmppc_map_vrma(vcpu, memslot, porder);
  1284. } else {
  1285. /* Set up to use an RMO region */
  1286. rma_size = ri->npages;
  1287. if (rma_size > memslot->npages)
  1288. rma_size = memslot->npages;
  1289. rma_size <<= PAGE_SHIFT;
  1290. rmls = lpcr_rmls(rma_size);
  1291. err = -EINVAL;
  1292. if (rmls < 0) {
  1293. pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
  1294. goto out_srcu;
  1295. }
  1296. atomic_inc(&ri->use_count);
  1297. kvm->arch.rma = ri;
  1298. /* Update LPCR and RMOR */
  1299. lpcr = kvm->arch.lpcr;
  1300. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1301. /* PPC970; insert RMLS value (split field) in HID4 */
  1302. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  1303. (3ul << HID4_RMLS2_SH));
  1304. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  1305. ((rmls & 3) << HID4_RMLS2_SH);
  1306. /* RMOR is also in HID4 */
  1307. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1308. << HID4_RMOR_SH;
  1309. } else {
  1310. /* POWER7 */
  1311. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1312. lpcr |= rmls << LPCR_RMLS_SH;
  1313. kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
  1314. }
  1315. kvm->arch.lpcr = lpcr;
  1316. pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
  1317. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1318. /* Initialize phys addrs of pages in RMO */
  1319. npages = ri->npages;
  1320. porder = __ilog2(npages);
  1321. physp = memslot->arch.slot_phys;
  1322. if (physp) {
  1323. if (npages > memslot->npages)
  1324. npages = memslot->npages;
  1325. spin_lock(&kvm->arch.slot_phys_lock);
  1326. for (i = 0; i < npages; ++i)
  1327. physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
  1328. porder;
  1329. spin_unlock(&kvm->arch.slot_phys_lock);
  1330. }
  1331. }
  1332. /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
  1333. smp_wmb();
  1334. kvm->arch.rma_setup_done = 1;
  1335. err = 0;
  1336. out_srcu:
  1337. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1338. out:
  1339. mutex_unlock(&kvm->lock);
  1340. return err;
  1341. up_out:
  1342. up_read(&current->mm->mmap_sem);
  1343. goto out;
  1344. }
  1345. int kvmppc_core_init_vm(struct kvm *kvm)
  1346. {
  1347. unsigned long lpcr, lpid;
  1348. /* Allocate the guest's logical partition ID */
  1349. lpid = kvmppc_alloc_lpid();
  1350. if (lpid < 0)
  1351. return -ENOMEM;
  1352. kvm->arch.lpid = lpid;
  1353. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1354. kvm->arch.rma = NULL;
  1355. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1356. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1357. /* PPC970; HID4 is effectively the LPCR */
  1358. kvm->arch.host_lpid = 0;
  1359. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1360. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1361. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1362. ((lpid & 0xf) << HID4_LPID5_SH);
  1363. } else {
  1364. /* POWER7; init LPCR for virtual RMA mode */
  1365. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1366. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1367. lpcr &= LPCR_PECE | LPCR_LPES;
  1368. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1369. LPCR_VPM0 | LPCR_VPM1;
  1370. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  1371. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1372. }
  1373. kvm->arch.lpcr = lpcr;
  1374. kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
  1375. spin_lock_init(&kvm->arch.slot_phys_lock);
  1376. return 0;
  1377. }
  1378. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1379. {
  1380. if (kvm->arch.rma) {
  1381. kvm_release_rma(kvm->arch.rma);
  1382. kvm->arch.rma = NULL;
  1383. }
  1384. kvmppc_free_hpt(kvm);
  1385. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1386. }
  1387. /* These are stubs for now */
  1388. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1389. {
  1390. }
  1391. /* We don't need to emulate any privileged instructions or dcbz */
  1392. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1393. unsigned int inst, int *advance)
  1394. {
  1395. return EMULATE_FAIL;
  1396. }
  1397. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  1398. {
  1399. return EMULATE_FAIL;
  1400. }
  1401. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  1402. {
  1403. return EMULATE_FAIL;
  1404. }
  1405. static int kvmppc_book3s_hv_init(void)
  1406. {
  1407. int r;
  1408. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1409. if (r)
  1410. return r;
  1411. r = kvmppc_mmu_hv_init();
  1412. return r;
  1413. }
  1414. static void kvmppc_book3s_hv_exit(void)
  1415. {
  1416. kvm_exit();
  1417. }
  1418. module_init(kvmppc_book3s_hv_init);
  1419. module_exit(kvmppc_book3s_hv_exit);