book3s_hv.c 50 KB

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