book3s_hv.c 49 KB

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