book3s_hv.c 50 KB

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