book3s_hv.c 53 KB

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