book3s_hv.c 56 KB

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