book3s_hv.c 49 KB

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