book3s_hv.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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->__old_status |= LPPACA_OLD_SHARED_PROC;
  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 + vc->tb_offset;
  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. case H_IPOLL:
  507. case H_XIRR_X:
  508. if (kvmppc_xics_enabled(vcpu)) {
  509. ret = kvmppc_xics_hcall(vcpu, req);
  510. break;
  511. } /* fallthrough */
  512. default:
  513. return RESUME_HOST;
  514. }
  515. kvmppc_set_gpr(vcpu, 3, ret);
  516. vcpu->arch.hcall_needed = 0;
  517. return RESUME_GUEST;
  518. }
  519. static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  520. struct task_struct *tsk)
  521. {
  522. int r = RESUME_HOST;
  523. vcpu->stat.sum_exits++;
  524. run->exit_reason = KVM_EXIT_UNKNOWN;
  525. run->ready_for_interrupt_injection = 1;
  526. switch (vcpu->arch.trap) {
  527. /* We're good on these - the host merely wanted to get our attention */
  528. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  529. vcpu->stat.dec_exits++;
  530. r = RESUME_GUEST;
  531. break;
  532. case BOOK3S_INTERRUPT_EXTERNAL:
  533. vcpu->stat.ext_intr_exits++;
  534. r = RESUME_GUEST;
  535. break;
  536. case BOOK3S_INTERRUPT_PERFMON:
  537. r = RESUME_GUEST;
  538. break;
  539. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  540. /*
  541. * Deliver a machine check interrupt to the guest.
  542. * We have to do this, even if the host has handled the
  543. * machine check, because machine checks use SRR0/1 and
  544. * the interrupt might have trashed guest state in them.
  545. */
  546. kvmppc_book3s_queue_irqprio(vcpu,
  547. BOOK3S_INTERRUPT_MACHINE_CHECK);
  548. r = RESUME_GUEST;
  549. break;
  550. case BOOK3S_INTERRUPT_PROGRAM:
  551. {
  552. ulong flags;
  553. /*
  554. * Normally program interrupts are delivered directly
  555. * to the guest by the hardware, but we can get here
  556. * as a result of a hypervisor emulation interrupt
  557. * (e40) getting turned into a 700 by BML RTAS.
  558. */
  559. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  560. kvmppc_core_queue_program(vcpu, flags);
  561. r = RESUME_GUEST;
  562. break;
  563. }
  564. case BOOK3S_INTERRUPT_SYSCALL:
  565. {
  566. /* hcall - punt to userspace */
  567. int i;
  568. if (vcpu->arch.shregs.msr & MSR_PR) {
  569. /* sc 1 from userspace - reflect to guest syscall */
  570. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
  571. r = RESUME_GUEST;
  572. break;
  573. }
  574. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  575. for (i = 0; i < 9; ++i)
  576. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  577. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  578. vcpu->arch.hcall_needed = 1;
  579. r = RESUME_HOST;
  580. break;
  581. }
  582. /*
  583. * We get these next two if the guest accesses a page which it thinks
  584. * it has mapped but which is not actually present, either because
  585. * it is for an emulated I/O device or because the corresonding
  586. * host page has been paged out. Any other HDSI/HISI interrupts
  587. * have been handled already.
  588. */
  589. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  590. r = RESUME_PAGE_FAULT;
  591. break;
  592. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  593. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  594. vcpu->arch.fault_dsisr = 0;
  595. r = RESUME_PAGE_FAULT;
  596. break;
  597. /*
  598. * This occurs if the guest executes an illegal instruction.
  599. * We just generate a program interrupt to the guest, since
  600. * we don't emulate any guest instructions at this stage.
  601. */
  602. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  603. kvmppc_core_queue_program(vcpu, 0x80000);
  604. r = RESUME_GUEST;
  605. break;
  606. default:
  607. kvmppc_dump_regs(vcpu);
  608. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  609. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  610. vcpu->arch.shregs.msr);
  611. r = RESUME_HOST;
  612. BUG();
  613. break;
  614. }
  615. return r;
  616. }
  617. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  618. struct kvm_sregs *sregs)
  619. {
  620. int i;
  621. memset(sregs, 0, sizeof(struct kvm_sregs));
  622. sregs->pvr = vcpu->arch.pvr;
  623. for (i = 0; i < vcpu->arch.slb_max; i++) {
  624. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  625. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  626. }
  627. return 0;
  628. }
  629. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  630. struct kvm_sregs *sregs)
  631. {
  632. int i, j;
  633. kvmppc_set_pvr(vcpu, sregs->pvr);
  634. j = 0;
  635. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  636. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  637. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  638. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  639. ++j;
  640. }
  641. }
  642. vcpu->arch.slb_max = j;
  643. return 0;
  644. }
  645. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  646. {
  647. int r = 0;
  648. long int i;
  649. switch (id) {
  650. case KVM_REG_PPC_HIOR:
  651. *val = get_reg_val(id, 0);
  652. break;
  653. case KVM_REG_PPC_DABR:
  654. *val = get_reg_val(id, vcpu->arch.dabr);
  655. break;
  656. case KVM_REG_PPC_DSCR:
  657. *val = get_reg_val(id, vcpu->arch.dscr);
  658. break;
  659. case KVM_REG_PPC_PURR:
  660. *val = get_reg_val(id, vcpu->arch.purr);
  661. break;
  662. case KVM_REG_PPC_SPURR:
  663. *val = get_reg_val(id, vcpu->arch.spurr);
  664. break;
  665. case KVM_REG_PPC_AMR:
  666. *val = get_reg_val(id, vcpu->arch.amr);
  667. break;
  668. case KVM_REG_PPC_UAMOR:
  669. *val = get_reg_val(id, vcpu->arch.uamor);
  670. break;
  671. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
  672. i = id - KVM_REG_PPC_MMCR0;
  673. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  674. break;
  675. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  676. i = id - KVM_REG_PPC_PMC1;
  677. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  678. break;
  679. case KVM_REG_PPC_SIAR:
  680. *val = get_reg_val(id, vcpu->arch.siar);
  681. break;
  682. case KVM_REG_PPC_SDAR:
  683. *val = get_reg_val(id, vcpu->arch.sdar);
  684. break;
  685. #ifdef CONFIG_VSX
  686. case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
  687. if (cpu_has_feature(CPU_FTR_VSX)) {
  688. /* VSX => FP reg i is stored in arch.vsr[2*i] */
  689. long int i = id - KVM_REG_PPC_FPR0;
  690. *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
  691. } else {
  692. /* let generic code handle it */
  693. r = -EINVAL;
  694. }
  695. break;
  696. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
  697. if (cpu_has_feature(CPU_FTR_VSX)) {
  698. long int i = id - KVM_REG_PPC_VSR0;
  699. val->vsxval[0] = vcpu->arch.vsr[2 * i];
  700. val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
  701. } else {
  702. r = -ENXIO;
  703. }
  704. break;
  705. #endif /* CONFIG_VSX */
  706. case KVM_REG_PPC_VPA_ADDR:
  707. spin_lock(&vcpu->arch.vpa_update_lock);
  708. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  709. spin_unlock(&vcpu->arch.vpa_update_lock);
  710. break;
  711. case KVM_REG_PPC_VPA_SLB:
  712. spin_lock(&vcpu->arch.vpa_update_lock);
  713. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  714. val->vpaval.length = vcpu->arch.slb_shadow.len;
  715. spin_unlock(&vcpu->arch.vpa_update_lock);
  716. break;
  717. case KVM_REG_PPC_VPA_DTL:
  718. spin_lock(&vcpu->arch.vpa_update_lock);
  719. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  720. val->vpaval.length = vcpu->arch.dtl.len;
  721. spin_unlock(&vcpu->arch.vpa_update_lock);
  722. break;
  723. case KVM_REG_PPC_TB_OFFSET:
  724. *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
  725. break;
  726. default:
  727. r = -EINVAL;
  728. break;
  729. }
  730. return r;
  731. }
  732. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  733. {
  734. int r = 0;
  735. long int i;
  736. unsigned long addr, len;
  737. switch (id) {
  738. case KVM_REG_PPC_HIOR:
  739. /* Only allow this to be set to zero */
  740. if (set_reg_val(id, *val))
  741. r = -EINVAL;
  742. break;
  743. case KVM_REG_PPC_DABR:
  744. vcpu->arch.dabr = set_reg_val(id, *val);
  745. break;
  746. case KVM_REG_PPC_DSCR:
  747. vcpu->arch.dscr = set_reg_val(id, *val);
  748. break;
  749. case KVM_REG_PPC_PURR:
  750. vcpu->arch.purr = set_reg_val(id, *val);
  751. break;
  752. case KVM_REG_PPC_SPURR:
  753. vcpu->arch.spurr = set_reg_val(id, *val);
  754. break;
  755. case KVM_REG_PPC_AMR:
  756. vcpu->arch.amr = set_reg_val(id, *val);
  757. break;
  758. case KVM_REG_PPC_UAMOR:
  759. vcpu->arch.uamor = set_reg_val(id, *val);
  760. break;
  761. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
  762. i = id - KVM_REG_PPC_MMCR0;
  763. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  764. break;
  765. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  766. i = id - KVM_REG_PPC_PMC1;
  767. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  768. break;
  769. case KVM_REG_PPC_SIAR:
  770. vcpu->arch.siar = set_reg_val(id, *val);
  771. break;
  772. case KVM_REG_PPC_SDAR:
  773. vcpu->arch.sdar = set_reg_val(id, *val);
  774. break;
  775. #ifdef CONFIG_VSX
  776. case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
  777. if (cpu_has_feature(CPU_FTR_VSX)) {
  778. /* VSX => FP reg i is stored in arch.vsr[2*i] */
  779. long int i = id - KVM_REG_PPC_FPR0;
  780. vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
  781. } else {
  782. /* let generic code handle it */
  783. r = -EINVAL;
  784. }
  785. break;
  786. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
  787. if (cpu_has_feature(CPU_FTR_VSX)) {
  788. long int i = id - KVM_REG_PPC_VSR0;
  789. vcpu->arch.vsr[2 * i] = val->vsxval[0];
  790. vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
  791. } else {
  792. r = -ENXIO;
  793. }
  794. break;
  795. #endif /* CONFIG_VSX */
  796. case KVM_REG_PPC_VPA_ADDR:
  797. addr = set_reg_val(id, *val);
  798. r = -EINVAL;
  799. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  800. vcpu->arch.dtl.next_gpa))
  801. break;
  802. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  803. break;
  804. case KVM_REG_PPC_VPA_SLB:
  805. addr = val->vpaval.addr;
  806. len = val->vpaval.length;
  807. r = -EINVAL;
  808. if (addr && !vcpu->arch.vpa.next_gpa)
  809. break;
  810. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  811. break;
  812. case KVM_REG_PPC_VPA_DTL:
  813. addr = val->vpaval.addr;
  814. len = val->vpaval.length;
  815. r = -EINVAL;
  816. if (addr && (len < sizeof(struct dtl_entry) ||
  817. !vcpu->arch.vpa.next_gpa))
  818. break;
  819. len -= len % sizeof(struct dtl_entry);
  820. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  821. break;
  822. case KVM_REG_PPC_TB_OFFSET:
  823. /* round up to multiple of 2^24 */
  824. vcpu->arch.vcore->tb_offset =
  825. ALIGN(set_reg_val(id, *val), 1UL << 24);
  826. break;
  827. default:
  828. r = -EINVAL;
  829. break;
  830. }
  831. return r;
  832. }
  833. int kvmppc_core_check_processor_compat(void)
  834. {
  835. if (cpu_has_feature(CPU_FTR_HVMODE))
  836. return 0;
  837. return -EIO;
  838. }
  839. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  840. {
  841. struct kvm_vcpu *vcpu;
  842. int err = -EINVAL;
  843. int core;
  844. struct kvmppc_vcore *vcore;
  845. core = id / threads_per_core;
  846. if (core >= KVM_MAX_VCORES)
  847. goto out;
  848. err = -ENOMEM;
  849. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  850. if (!vcpu)
  851. goto out;
  852. err = kvm_vcpu_init(vcpu, kvm, id);
  853. if (err)
  854. goto free_vcpu;
  855. vcpu->arch.shared = &vcpu->arch.shregs;
  856. vcpu->arch.mmcr[0] = MMCR0_FC;
  857. vcpu->arch.ctrl = CTRL_RUNLATCH;
  858. /* default to host PVR, since we can't spoof it */
  859. vcpu->arch.pvr = mfspr(SPRN_PVR);
  860. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  861. spin_lock_init(&vcpu->arch.vpa_update_lock);
  862. spin_lock_init(&vcpu->arch.tbacct_lock);
  863. vcpu->arch.busy_preempt = TB_NIL;
  864. kvmppc_mmu_book3s_hv_init(vcpu);
  865. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  866. init_waitqueue_head(&vcpu->arch.cpu_run);
  867. mutex_lock(&kvm->lock);
  868. vcore = kvm->arch.vcores[core];
  869. if (!vcore) {
  870. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  871. if (vcore) {
  872. INIT_LIST_HEAD(&vcore->runnable_threads);
  873. spin_lock_init(&vcore->lock);
  874. init_waitqueue_head(&vcore->wq);
  875. vcore->preempt_tb = TB_NIL;
  876. }
  877. kvm->arch.vcores[core] = vcore;
  878. kvm->arch.online_vcores++;
  879. }
  880. mutex_unlock(&kvm->lock);
  881. if (!vcore)
  882. goto free_vcpu;
  883. spin_lock(&vcore->lock);
  884. ++vcore->num_threads;
  885. spin_unlock(&vcore->lock);
  886. vcpu->arch.vcore = vcore;
  887. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  888. kvmppc_sanity_check(vcpu);
  889. return vcpu;
  890. free_vcpu:
  891. kmem_cache_free(kvm_vcpu_cache, vcpu);
  892. out:
  893. return ERR_PTR(err);
  894. }
  895. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  896. {
  897. if (vpa->pinned_addr)
  898. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  899. vpa->dirty);
  900. }
  901. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  902. {
  903. spin_lock(&vcpu->arch.vpa_update_lock);
  904. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  905. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  906. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  907. spin_unlock(&vcpu->arch.vpa_update_lock);
  908. kvm_vcpu_uninit(vcpu);
  909. kmem_cache_free(kvm_vcpu_cache, vcpu);
  910. }
  911. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  912. {
  913. unsigned long dec_nsec, now;
  914. now = get_tb();
  915. if (now > vcpu->arch.dec_expires) {
  916. /* decrementer has already gone negative */
  917. kvmppc_core_queue_dec(vcpu);
  918. kvmppc_core_prepare_to_enter(vcpu);
  919. return;
  920. }
  921. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  922. / tb_ticks_per_sec;
  923. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  924. HRTIMER_MODE_REL);
  925. vcpu->arch.timer_running = 1;
  926. }
  927. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  928. {
  929. vcpu->arch.ceded = 0;
  930. if (vcpu->arch.timer_running) {
  931. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  932. vcpu->arch.timer_running = 0;
  933. }
  934. }
  935. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  936. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  937. struct kvm_vcpu *vcpu)
  938. {
  939. u64 now;
  940. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  941. return;
  942. spin_lock(&vcpu->arch.tbacct_lock);
  943. now = mftb();
  944. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  945. vcpu->arch.stolen_logged;
  946. vcpu->arch.busy_preempt = now;
  947. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  948. spin_unlock(&vcpu->arch.tbacct_lock);
  949. --vc->n_runnable;
  950. list_del(&vcpu->arch.run_list);
  951. }
  952. static int kvmppc_grab_hwthread(int cpu)
  953. {
  954. struct paca_struct *tpaca;
  955. long timeout = 1000;
  956. tpaca = &paca[cpu];
  957. /* Ensure the thread won't go into the kernel if it wakes */
  958. tpaca->kvm_hstate.hwthread_req = 1;
  959. tpaca->kvm_hstate.kvm_vcpu = NULL;
  960. /*
  961. * If the thread is already executing in the kernel (e.g. handling
  962. * a stray interrupt), wait for it to get back to nap mode.
  963. * The smp_mb() is to ensure that our setting of hwthread_req
  964. * is visible before we look at hwthread_state, so if this
  965. * races with the code at system_reset_pSeries and the thread
  966. * misses our setting of hwthread_req, we are sure to see its
  967. * setting of hwthread_state, and vice versa.
  968. */
  969. smp_mb();
  970. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  971. if (--timeout <= 0) {
  972. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  973. return -EBUSY;
  974. }
  975. udelay(1);
  976. }
  977. return 0;
  978. }
  979. static void kvmppc_release_hwthread(int cpu)
  980. {
  981. struct paca_struct *tpaca;
  982. tpaca = &paca[cpu];
  983. tpaca->kvm_hstate.hwthread_req = 0;
  984. tpaca->kvm_hstate.kvm_vcpu = NULL;
  985. }
  986. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  987. {
  988. int cpu;
  989. struct paca_struct *tpaca;
  990. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  991. if (vcpu->arch.timer_running) {
  992. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  993. vcpu->arch.timer_running = 0;
  994. }
  995. cpu = vc->pcpu + vcpu->arch.ptid;
  996. tpaca = &paca[cpu];
  997. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  998. tpaca->kvm_hstate.kvm_vcore = vc;
  999. tpaca->kvm_hstate.napping = 0;
  1000. vcpu->cpu = vc->pcpu;
  1001. smp_wmb();
  1002. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  1003. if (vcpu->arch.ptid) {
  1004. xics_wake_cpu(cpu);
  1005. ++vc->n_woken;
  1006. }
  1007. #endif
  1008. }
  1009. static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
  1010. {
  1011. int i;
  1012. HMT_low();
  1013. i = 0;
  1014. while (vc->nap_count < vc->n_woken) {
  1015. if (++i >= 1000000) {
  1016. pr_err("kvmppc_wait_for_nap timeout %d %d\n",
  1017. vc->nap_count, vc->n_woken);
  1018. break;
  1019. }
  1020. cpu_relax();
  1021. }
  1022. HMT_medium();
  1023. }
  1024. /*
  1025. * Check that we are on thread 0 and that any other threads in
  1026. * this core are off-line. Then grab the threads so they can't
  1027. * enter the kernel.
  1028. */
  1029. static int on_primary_thread(void)
  1030. {
  1031. int cpu = smp_processor_id();
  1032. int thr = cpu_thread_in_core(cpu);
  1033. if (thr)
  1034. return 0;
  1035. while (++thr < threads_per_core)
  1036. if (cpu_online(cpu + thr))
  1037. return 0;
  1038. /* Grab all hw threads so they can't go into the kernel */
  1039. for (thr = 1; thr < threads_per_core; ++thr) {
  1040. if (kvmppc_grab_hwthread(cpu + thr)) {
  1041. /* Couldn't grab one; let the others go */
  1042. do {
  1043. kvmppc_release_hwthread(cpu + thr);
  1044. } while (--thr > 0);
  1045. return 0;
  1046. }
  1047. }
  1048. return 1;
  1049. }
  1050. /*
  1051. * Run a set of guest threads on a physical core.
  1052. * Called with vc->lock held.
  1053. */
  1054. static void kvmppc_run_core(struct kvmppc_vcore *vc)
  1055. {
  1056. struct kvm_vcpu *vcpu, *vcpu0, *vnext;
  1057. long ret;
  1058. u64 now;
  1059. int ptid, i, need_vpa_update;
  1060. int srcu_idx;
  1061. struct kvm_vcpu *vcpus_to_update[threads_per_core];
  1062. /* don't start if any threads have a signal pending */
  1063. need_vpa_update = 0;
  1064. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1065. if (signal_pending(vcpu->arch.run_task))
  1066. return;
  1067. if (vcpu->arch.vpa.update_pending ||
  1068. vcpu->arch.slb_shadow.update_pending ||
  1069. vcpu->arch.dtl.update_pending)
  1070. vcpus_to_update[need_vpa_update++] = vcpu;
  1071. }
  1072. /*
  1073. * Initialize *vc, in particular vc->vcore_state, so we can
  1074. * drop the vcore lock if necessary.
  1075. */
  1076. vc->n_woken = 0;
  1077. vc->nap_count = 0;
  1078. vc->entry_exit_count = 0;
  1079. vc->vcore_state = VCORE_STARTING;
  1080. vc->in_guest = 0;
  1081. vc->napping_threads = 0;
  1082. /*
  1083. * Updating any of the vpas requires calling kvmppc_pin_guest_page,
  1084. * which can't be called with any spinlocks held.
  1085. */
  1086. if (need_vpa_update) {
  1087. spin_unlock(&vc->lock);
  1088. for (i = 0; i < need_vpa_update; ++i)
  1089. kvmppc_update_vpas(vcpus_to_update[i]);
  1090. spin_lock(&vc->lock);
  1091. }
  1092. /*
  1093. * Assign physical thread IDs, first to non-ceded vcpus
  1094. * and then to ceded ones.
  1095. */
  1096. ptid = 0;
  1097. vcpu0 = NULL;
  1098. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1099. if (!vcpu->arch.ceded) {
  1100. if (!ptid)
  1101. vcpu0 = vcpu;
  1102. vcpu->arch.ptid = ptid++;
  1103. }
  1104. }
  1105. if (!vcpu0)
  1106. goto out; /* nothing to run; should never happen */
  1107. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1108. if (vcpu->arch.ceded)
  1109. vcpu->arch.ptid = ptid++;
  1110. /*
  1111. * Make sure we are running on thread 0, and that
  1112. * secondary threads are offline.
  1113. */
  1114. if (threads_per_core > 1 && !on_primary_thread()) {
  1115. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1116. vcpu->arch.ret = -EBUSY;
  1117. goto out;
  1118. }
  1119. vc->pcpu = smp_processor_id();
  1120. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1121. kvmppc_start_thread(vcpu);
  1122. kvmppc_create_dtl_entry(vcpu, vc);
  1123. }
  1124. vc->vcore_state = VCORE_RUNNING;
  1125. preempt_disable();
  1126. spin_unlock(&vc->lock);
  1127. kvm_guest_enter();
  1128. srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
  1129. __kvmppc_vcore_entry(NULL, vcpu0);
  1130. spin_lock(&vc->lock);
  1131. /* disable sending of IPIs on virtual external irqs */
  1132. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1133. vcpu->cpu = -1;
  1134. /* wait for secondary threads to finish writing their state to memory */
  1135. if (vc->nap_count < vc->n_woken)
  1136. kvmppc_wait_for_nap(vc);
  1137. for (i = 0; i < threads_per_core; ++i)
  1138. kvmppc_release_hwthread(vc->pcpu + i);
  1139. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  1140. vc->vcore_state = VCORE_EXITING;
  1141. spin_unlock(&vc->lock);
  1142. srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
  1143. /* make sure updates to secondary vcpu structs are visible now */
  1144. smp_mb();
  1145. kvm_guest_exit();
  1146. preempt_enable();
  1147. kvm_resched(vcpu);
  1148. spin_lock(&vc->lock);
  1149. now = get_tb();
  1150. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1151. /* cancel pending dec exception if dec is positive */
  1152. if (now < vcpu->arch.dec_expires &&
  1153. kvmppc_core_pending_dec(vcpu))
  1154. kvmppc_core_dequeue_dec(vcpu);
  1155. ret = RESUME_GUEST;
  1156. if (vcpu->arch.trap)
  1157. ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
  1158. vcpu->arch.run_task);
  1159. vcpu->arch.ret = ret;
  1160. vcpu->arch.trap = 0;
  1161. if (vcpu->arch.ceded) {
  1162. if (ret != RESUME_GUEST)
  1163. kvmppc_end_cede(vcpu);
  1164. else
  1165. kvmppc_set_timer(vcpu);
  1166. }
  1167. }
  1168. out:
  1169. vc->vcore_state = VCORE_INACTIVE;
  1170. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  1171. arch.run_list) {
  1172. if (vcpu->arch.ret != RESUME_GUEST) {
  1173. kvmppc_remove_runnable(vc, vcpu);
  1174. wake_up(&vcpu->arch.cpu_run);
  1175. }
  1176. }
  1177. }
  1178. /*
  1179. * Wait for some other vcpu thread to execute us, and
  1180. * wake us up when we need to handle something in the host.
  1181. */
  1182. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  1183. {
  1184. DEFINE_WAIT(wait);
  1185. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  1186. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  1187. schedule();
  1188. finish_wait(&vcpu->arch.cpu_run, &wait);
  1189. }
  1190. /*
  1191. * All the vcpus in this vcore are idle, so wait for a decrementer
  1192. * or external interrupt to one of the vcpus. vc->lock is held.
  1193. */
  1194. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1195. {
  1196. DEFINE_WAIT(wait);
  1197. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1198. vc->vcore_state = VCORE_SLEEPING;
  1199. spin_unlock(&vc->lock);
  1200. schedule();
  1201. finish_wait(&vc->wq, &wait);
  1202. spin_lock(&vc->lock);
  1203. vc->vcore_state = VCORE_INACTIVE;
  1204. }
  1205. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1206. {
  1207. int n_ceded;
  1208. struct kvmppc_vcore *vc;
  1209. struct kvm_vcpu *v, *vn;
  1210. kvm_run->exit_reason = 0;
  1211. vcpu->arch.ret = RESUME_GUEST;
  1212. vcpu->arch.trap = 0;
  1213. kvmppc_update_vpas(vcpu);
  1214. /*
  1215. * Synchronize with other threads in this virtual core
  1216. */
  1217. vc = vcpu->arch.vcore;
  1218. spin_lock(&vc->lock);
  1219. vcpu->arch.ceded = 0;
  1220. vcpu->arch.run_task = current;
  1221. vcpu->arch.kvm_run = kvm_run;
  1222. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  1223. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  1224. vcpu->arch.busy_preempt = TB_NIL;
  1225. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  1226. ++vc->n_runnable;
  1227. /*
  1228. * This happens the first time this is called for a vcpu.
  1229. * If the vcore is already running, we may be able to start
  1230. * this thread straight away and have it join in.
  1231. */
  1232. if (!signal_pending(current)) {
  1233. if (vc->vcore_state == VCORE_RUNNING &&
  1234. VCORE_EXIT_COUNT(vc) == 0) {
  1235. vcpu->arch.ptid = vc->n_runnable - 1;
  1236. kvmppc_create_dtl_entry(vcpu, vc);
  1237. kvmppc_start_thread(vcpu);
  1238. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1239. wake_up(&vc->wq);
  1240. }
  1241. }
  1242. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1243. !signal_pending(current)) {
  1244. if (vc->vcore_state != VCORE_INACTIVE) {
  1245. spin_unlock(&vc->lock);
  1246. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  1247. spin_lock(&vc->lock);
  1248. continue;
  1249. }
  1250. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  1251. arch.run_list) {
  1252. kvmppc_core_prepare_to_enter(v);
  1253. if (signal_pending(v->arch.run_task)) {
  1254. kvmppc_remove_runnable(vc, v);
  1255. v->stat.signal_exits++;
  1256. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  1257. v->arch.ret = -EINTR;
  1258. wake_up(&v->arch.cpu_run);
  1259. }
  1260. }
  1261. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1262. break;
  1263. vc->runner = vcpu;
  1264. n_ceded = 0;
  1265. list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
  1266. if (!v->arch.pending_exceptions)
  1267. n_ceded += v->arch.ceded;
  1268. else
  1269. v->arch.ceded = 0;
  1270. }
  1271. if (n_ceded == vc->n_runnable)
  1272. kvmppc_vcore_blocked(vc);
  1273. else
  1274. kvmppc_run_core(vc);
  1275. vc->runner = NULL;
  1276. }
  1277. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1278. (vc->vcore_state == VCORE_RUNNING ||
  1279. vc->vcore_state == VCORE_EXITING)) {
  1280. spin_unlock(&vc->lock);
  1281. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  1282. spin_lock(&vc->lock);
  1283. }
  1284. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  1285. kvmppc_remove_runnable(vc, vcpu);
  1286. vcpu->stat.signal_exits++;
  1287. kvm_run->exit_reason = KVM_EXIT_INTR;
  1288. vcpu->arch.ret = -EINTR;
  1289. }
  1290. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  1291. /* Wake up some vcpu to run the core */
  1292. v = list_first_entry(&vc->runnable_threads,
  1293. struct kvm_vcpu, arch.run_list);
  1294. wake_up(&v->arch.cpu_run);
  1295. }
  1296. spin_unlock(&vc->lock);
  1297. return vcpu->arch.ret;
  1298. }
  1299. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  1300. {
  1301. int r;
  1302. int srcu_idx;
  1303. if (!vcpu->arch.sane) {
  1304. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1305. return -EINVAL;
  1306. }
  1307. kvmppc_core_prepare_to_enter(vcpu);
  1308. /* No need to go into the guest when all we'll do is come back out */
  1309. if (signal_pending(current)) {
  1310. run->exit_reason = KVM_EXIT_INTR;
  1311. return -EINTR;
  1312. }
  1313. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  1314. /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
  1315. smp_mb();
  1316. /* On the first time here, set up HTAB and VRMA or RMA */
  1317. if (!vcpu->kvm->arch.rma_setup_done) {
  1318. r = kvmppc_hv_setup_htab_rma(vcpu);
  1319. if (r)
  1320. goto out;
  1321. }
  1322. flush_fp_to_thread(current);
  1323. flush_altivec_to_thread(current);
  1324. flush_vsx_to_thread(current);
  1325. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  1326. vcpu->arch.pgdir = current->mm->pgd;
  1327. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  1328. do {
  1329. r = kvmppc_run_vcpu(run, vcpu);
  1330. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  1331. !(vcpu->arch.shregs.msr & MSR_PR)) {
  1332. r = kvmppc_pseries_do_hcall(vcpu);
  1333. kvmppc_core_prepare_to_enter(vcpu);
  1334. } else if (r == RESUME_PAGE_FAULT) {
  1335. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1336. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  1337. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  1338. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1339. }
  1340. } while (r == RESUME_GUEST);
  1341. out:
  1342. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  1343. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  1344. return r;
  1345. }
  1346. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  1347. Assumes POWER7 or PPC970. */
  1348. static inline int lpcr_rmls(unsigned long rma_size)
  1349. {
  1350. switch (rma_size) {
  1351. case 32ul << 20: /* 32 MB */
  1352. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1353. return 8; /* only supported on POWER7 */
  1354. return -1;
  1355. case 64ul << 20: /* 64 MB */
  1356. return 3;
  1357. case 128ul << 20: /* 128 MB */
  1358. return 7;
  1359. case 256ul << 20: /* 256 MB */
  1360. return 4;
  1361. case 1ul << 30: /* 1 GB */
  1362. return 2;
  1363. case 16ul << 30: /* 16 GB */
  1364. return 1;
  1365. case 256ul << 30: /* 256 GB */
  1366. return 0;
  1367. default:
  1368. return -1;
  1369. }
  1370. }
  1371. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1372. {
  1373. struct page *page;
  1374. struct kvm_rma_info *ri = vma->vm_file->private_data;
  1375. if (vmf->pgoff >= kvm_rma_pages)
  1376. return VM_FAULT_SIGBUS;
  1377. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  1378. get_page(page);
  1379. vmf->page = page;
  1380. return 0;
  1381. }
  1382. static const struct vm_operations_struct kvm_rma_vm_ops = {
  1383. .fault = kvm_rma_fault,
  1384. };
  1385. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  1386. {
  1387. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1388. vma->vm_ops = &kvm_rma_vm_ops;
  1389. return 0;
  1390. }
  1391. static int kvm_rma_release(struct inode *inode, struct file *filp)
  1392. {
  1393. struct kvm_rma_info *ri = filp->private_data;
  1394. kvm_release_rma(ri);
  1395. return 0;
  1396. }
  1397. static const struct file_operations kvm_rma_fops = {
  1398. .mmap = kvm_rma_mmap,
  1399. .release = kvm_rma_release,
  1400. };
  1401. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  1402. {
  1403. long fd;
  1404. struct kvm_rma_info *ri;
  1405. /*
  1406. * Only do this on PPC970 in HV mode
  1407. */
  1408. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  1409. !cpu_has_feature(CPU_FTR_ARCH_201))
  1410. return -EINVAL;
  1411. if (!kvm_rma_pages)
  1412. return -EINVAL;
  1413. ri = kvm_alloc_rma();
  1414. if (!ri)
  1415. return -ENOMEM;
  1416. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
  1417. if (fd < 0)
  1418. kvm_release_rma(ri);
  1419. ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
  1420. return fd;
  1421. }
  1422. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  1423. int linux_psize)
  1424. {
  1425. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  1426. if (!def->shift)
  1427. return;
  1428. (*sps)->page_shift = def->shift;
  1429. (*sps)->slb_enc = def->sllp;
  1430. (*sps)->enc[0].page_shift = def->shift;
  1431. /*
  1432. * Only return base page encoding. We don't want to return
  1433. * all the supporting pte_enc, because our H_ENTER doesn't
  1434. * support MPSS yet. Once they do, we can start passing all
  1435. * support pte_enc here
  1436. */
  1437. (*sps)->enc[0].pte_enc = def->penc[linux_psize];
  1438. (*sps)++;
  1439. }
  1440. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1441. {
  1442. struct kvm_ppc_one_seg_page_size *sps;
  1443. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  1444. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1445. info->flags |= KVM_PPC_1T_SEGMENTS;
  1446. info->slb_size = mmu_slb_size;
  1447. /* We only support these sizes for now, and no muti-size segments */
  1448. sps = &info->sps[0];
  1449. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  1450. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  1451. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  1452. return 0;
  1453. }
  1454. /*
  1455. * Get (and clear) the dirty memory log for a memory slot.
  1456. */
  1457. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1458. {
  1459. struct kvm_memory_slot *memslot;
  1460. int r;
  1461. unsigned long n;
  1462. mutex_lock(&kvm->slots_lock);
  1463. r = -EINVAL;
  1464. if (log->slot >= KVM_USER_MEM_SLOTS)
  1465. goto out;
  1466. memslot = id_to_memslot(kvm->memslots, log->slot);
  1467. r = -ENOENT;
  1468. if (!memslot->dirty_bitmap)
  1469. goto out;
  1470. n = kvm_dirty_bitmap_bytes(memslot);
  1471. memset(memslot->dirty_bitmap, 0, n);
  1472. r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
  1473. if (r)
  1474. goto out;
  1475. r = -EFAULT;
  1476. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  1477. goto out;
  1478. r = 0;
  1479. out:
  1480. mutex_unlock(&kvm->slots_lock);
  1481. return r;
  1482. }
  1483. static void unpin_slot(struct kvm_memory_slot *memslot)
  1484. {
  1485. unsigned long *physp;
  1486. unsigned long j, npages, pfn;
  1487. struct page *page;
  1488. physp = memslot->arch.slot_phys;
  1489. npages = memslot->npages;
  1490. if (!physp)
  1491. return;
  1492. for (j = 0; j < npages; j++) {
  1493. if (!(physp[j] & KVMPPC_GOT_PAGE))
  1494. continue;
  1495. pfn = physp[j] >> PAGE_SHIFT;
  1496. page = pfn_to_page(pfn);
  1497. SetPageDirty(page);
  1498. put_page(page);
  1499. }
  1500. }
  1501. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1502. struct kvm_memory_slot *dont)
  1503. {
  1504. if (!dont || free->arch.rmap != dont->arch.rmap) {
  1505. vfree(free->arch.rmap);
  1506. free->arch.rmap = NULL;
  1507. }
  1508. if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
  1509. unpin_slot(free);
  1510. vfree(free->arch.slot_phys);
  1511. free->arch.slot_phys = NULL;
  1512. }
  1513. }
  1514. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1515. unsigned long npages)
  1516. {
  1517. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  1518. if (!slot->arch.rmap)
  1519. return -ENOMEM;
  1520. slot->arch.slot_phys = NULL;
  1521. return 0;
  1522. }
  1523. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1524. struct kvm_memory_slot *memslot,
  1525. struct kvm_userspace_memory_region *mem)
  1526. {
  1527. unsigned long *phys;
  1528. /* Allocate a slot_phys array if needed */
  1529. phys = memslot->arch.slot_phys;
  1530. if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
  1531. phys = vzalloc(memslot->npages * sizeof(unsigned long));
  1532. if (!phys)
  1533. return -ENOMEM;
  1534. memslot->arch.slot_phys = phys;
  1535. }
  1536. return 0;
  1537. }
  1538. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1539. struct kvm_userspace_memory_region *mem,
  1540. const struct kvm_memory_slot *old)
  1541. {
  1542. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  1543. struct kvm_memory_slot *memslot;
  1544. if (npages && old->npages) {
  1545. /*
  1546. * If modifying a memslot, reset all the rmap dirty bits.
  1547. * If this is a new memslot, we don't need to do anything
  1548. * since the rmap array starts out as all zeroes,
  1549. * i.e. no pages are dirty.
  1550. */
  1551. memslot = id_to_memslot(kvm->memslots, mem->slot);
  1552. kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
  1553. }
  1554. }
  1555. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  1556. {
  1557. int err = 0;
  1558. struct kvm *kvm = vcpu->kvm;
  1559. struct kvm_rma_info *ri = NULL;
  1560. unsigned long hva;
  1561. struct kvm_memory_slot *memslot;
  1562. struct vm_area_struct *vma;
  1563. unsigned long lpcr, senc;
  1564. unsigned long psize, porder;
  1565. unsigned long rma_size;
  1566. unsigned long rmls;
  1567. unsigned long *physp;
  1568. unsigned long i, npages;
  1569. int srcu_idx;
  1570. mutex_lock(&kvm->lock);
  1571. if (kvm->arch.rma_setup_done)
  1572. goto out; /* another vcpu beat us to it */
  1573. /* Allocate hashed page table (if not done already) and reset it */
  1574. if (!kvm->arch.hpt_virt) {
  1575. err = kvmppc_alloc_hpt(kvm, NULL);
  1576. if (err) {
  1577. pr_err("KVM: Couldn't alloc HPT\n");
  1578. goto out;
  1579. }
  1580. }
  1581. /* Look up the memslot for guest physical address 0 */
  1582. srcu_idx = srcu_read_lock(&kvm->srcu);
  1583. memslot = gfn_to_memslot(kvm, 0);
  1584. /* We must have some memory at 0 by now */
  1585. err = -EINVAL;
  1586. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1587. goto out_srcu;
  1588. /* Look up the VMA for the start of this memory slot */
  1589. hva = memslot->userspace_addr;
  1590. down_read(&current->mm->mmap_sem);
  1591. vma = find_vma(current->mm, hva);
  1592. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  1593. goto up_out;
  1594. psize = vma_kernel_pagesize(vma);
  1595. porder = __ilog2(psize);
  1596. /* Is this one of our preallocated RMAs? */
  1597. if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
  1598. hva == vma->vm_start)
  1599. ri = vma->vm_file->private_data;
  1600. up_read(&current->mm->mmap_sem);
  1601. if (!ri) {
  1602. /* On POWER7, use VRMA; on PPC970, give up */
  1603. err = -EPERM;
  1604. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1605. pr_err("KVM: CPU requires an RMO\n");
  1606. goto out_srcu;
  1607. }
  1608. /* We can handle 4k, 64k or 16M pages in the VRMA */
  1609. err = -EINVAL;
  1610. if (!(psize == 0x1000 || psize == 0x10000 ||
  1611. psize == 0x1000000))
  1612. goto out_srcu;
  1613. /* Update VRMASD field in the LPCR */
  1614. senc = slb_pgsize_encoding(psize);
  1615. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1616. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1617. lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
  1618. lpcr |= senc << (LPCR_VRMASD_SH - 4);
  1619. kvm->arch.lpcr = lpcr;
  1620. /* Create HPTEs in the hash page table for the VRMA */
  1621. kvmppc_map_vrma(vcpu, memslot, porder);
  1622. } else {
  1623. /* Set up to use an RMO region */
  1624. rma_size = kvm_rma_pages;
  1625. if (rma_size > memslot->npages)
  1626. rma_size = memslot->npages;
  1627. rma_size <<= PAGE_SHIFT;
  1628. rmls = lpcr_rmls(rma_size);
  1629. err = -EINVAL;
  1630. if ((long)rmls < 0) {
  1631. pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
  1632. goto out_srcu;
  1633. }
  1634. atomic_inc(&ri->use_count);
  1635. kvm->arch.rma = ri;
  1636. /* Update LPCR and RMOR */
  1637. lpcr = kvm->arch.lpcr;
  1638. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1639. /* PPC970; insert RMLS value (split field) in HID4 */
  1640. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  1641. (3ul << HID4_RMLS2_SH));
  1642. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  1643. ((rmls & 3) << HID4_RMLS2_SH);
  1644. /* RMOR is also in HID4 */
  1645. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1646. << HID4_RMOR_SH;
  1647. } else {
  1648. /* POWER7 */
  1649. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1650. lpcr |= rmls << LPCR_RMLS_SH;
  1651. kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
  1652. }
  1653. kvm->arch.lpcr = lpcr;
  1654. pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
  1655. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1656. /* Initialize phys addrs of pages in RMO */
  1657. npages = kvm_rma_pages;
  1658. porder = __ilog2(npages);
  1659. physp = memslot->arch.slot_phys;
  1660. if (physp) {
  1661. if (npages > memslot->npages)
  1662. npages = memslot->npages;
  1663. spin_lock(&kvm->arch.slot_phys_lock);
  1664. for (i = 0; i < npages; ++i)
  1665. physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
  1666. porder;
  1667. spin_unlock(&kvm->arch.slot_phys_lock);
  1668. }
  1669. }
  1670. /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
  1671. smp_wmb();
  1672. kvm->arch.rma_setup_done = 1;
  1673. err = 0;
  1674. out_srcu:
  1675. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1676. out:
  1677. mutex_unlock(&kvm->lock);
  1678. return err;
  1679. up_out:
  1680. up_read(&current->mm->mmap_sem);
  1681. goto out_srcu;
  1682. }
  1683. int kvmppc_core_init_vm(struct kvm *kvm)
  1684. {
  1685. unsigned long lpcr, lpid;
  1686. /* Allocate the guest's logical partition ID */
  1687. lpid = kvmppc_alloc_lpid();
  1688. if ((long)lpid < 0)
  1689. return -ENOMEM;
  1690. kvm->arch.lpid = lpid;
  1691. /*
  1692. * Since we don't flush the TLB when tearing down a VM,
  1693. * and this lpid might have previously been used,
  1694. * make sure we flush on each core before running the new VM.
  1695. */
  1696. cpumask_setall(&kvm->arch.need_tlb_flush);
  1697. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1698. INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
  1699. kvm->arch.rma = NULL;
  1700. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1701. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1702. /* PPC970; HID4 is effectively the LPCR */
  1703. kvm->arch.host_lpid = 0;
  1704. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1705. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1706. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1707. ((lpid & 0xf) << HID4_LPID5_SH);
  1708. } else {
  1709. /* POWER7; init LPCR for virtual RMA mode */
  1710. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1711. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1712. lpcr &= LPCR_PECE | LPCR_LPES;
  1713. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1714. LPCR_VPM0 | LPCR_VPM1;
  1715. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  1716. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1717. }
  1718. kvm->arch.lpcr = lpcr;
  1719. kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
  1720. spin_lock_init(&kvm->arch.slot_phys_lock);
  1721. /*
  1722. * Don't allow secondary CPU threads to come online
  1723. * while any KVM VMs exist.
  1724. */
  1725. inhibit_secondary_onlining();
  1726. return 0;
  1727. }
  1728. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1729. {
  1730. uninhibit_secondary_onlining();
  1731. if (kvm->arch.rma) {
  1732. kvm_release_rma(kvm->arch.rma);
  1733. kvm->arch.rma = NULL;
  1734. }
  1735. kvmppc_rtas_tokens_free(kvm);
  1736. kvmppc_free_hpt(kvm);
  1737. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1738. }
  1739. /* These are stubs for now */
  1740. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1741. {
  1742. }
  1743. /* We don't need to emulate any privileged instructions or dcbz */
  1744. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1745. unsigned int inst, int *advance)
  1746. {
  1747. return EMULATE_FAIL;
  1748. }
  1749. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  1750. {
  1751. return EMULATE_FAIL;
  1752. }
  1753. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  1754. {
  1755. return EMULATE_FAIL;
  1756. }
  1757. static int kvmppc_book3s_hv_init(void)
  1758. {
  1759. int r;
  1760. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1761. if (r)
  1762. return r;
  1763. r = kvmppc_mmu_hv_init();
  1764. return r;
  1765. }
  1766. static void kvmppc_book3s_hv_exit(void)
  1767. {
  1768. kvm_exit();
  1769. }
  1770. module_init(kvmppc_book3s_hv_init);
  1771. module_exit(kvmppc_book3s_hv_exit);