book3s_hv.c 56 KB

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