book3s_hv.c 49 KB

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