book3s_hv.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766
  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 <linux/miscdevice.h>
  34. #include <linux/debugfs.h>
  35. #include <asm/reg.h>
  36. #include <asm/cputable.h>
  37. #include <asm/cache.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/io.h>
  42. #include <asm/kvm_ppc.h>
  43. #include <asm/kvm_book3s.h>
  44. #include <asm/mmu_context.h>
  45. #include <asm/lppaca.h>
  46. #include <asm/processor.h>
  47. #include <asm/cputhreads.h>
  48. #include <asm/page.h>
  49. #include <asm/hvcall.h>
  50. #include <asm/switch_to.h>
  51. #include <asm/smp.h>
  52. #include <asm/dbell.h>
  53. #include <linux/gfp.h>
  54. #include <linux/vmalloc.h>
  55. #include <linux/highmem.h>
  56. #include <linux/hugetlb.h>
  57. #include <linux/module.h>
  58. #include "book3s.h"
  59. #define CREATE_TRACE_POINTS
  60. #include "trace_hv.h"
  61. /* #define EXIT_DEBUG */
  62. /* #define EXIT_DEBUG_SIMPLE */
  63. /* #define EXIT_DEBUG_INT */
  64. /* Used to indicate that a guest page fault needs to be handled */
  65. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  66. /* Used as a "null" value for timebase values */
  67. #define TB_NIL (~(u64)0)
  68. static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
  69. #if defined(CONFIG_PPC_64K_PAGES)
  70. #define MPP_BUFFER_ORDER 0
  71. #elif defined(CONFIG_PPC_4K_PAGES)
  72. #define MPP_BUFFER_ORDER 3
  73. #endif
  74. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  75. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  76. static bool kvmppc_ipi_thread(int cpu)
  77. {
  78. /* On POWER8 for IPIs to threads in the same core, use msgsnd */
  79. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  80. preempt_disable();
  81. if (cpu_first_thread_sibling(cpu) ==
  82. cpu_first_thread_sibling(smp_processor_id())) {
  83. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  84. msg |= cpu_thread_in_core(cpu);
  85. smp_mb();
  86. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  87. preempt_enable();
  88. return true;
  89. }
  90. preempt_enable();
  91. }
  92. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  93. if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
  94. xics_wake_cpu(cpu);
  95. return true;
  96. }
  97. #endif
  98. return false;
  99. }
  100. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  101. {
  102. int cpu = vcpu->cpu;
  103. wait_queue_head_t *wqp;
  104. wqp = kvm_arch_vcpu_wq(vcpu);
  105. if (waitqueue_active(wqp)) {
  106. wake_up_interruptible(wqp);
  107. ++vcpu->stat.halt_wakeup;
  108. }
  109. if (kvmppc_ipi_thread(cpu + vcpu->arch.ptid))
  110. return;
  111. /* CPU points to the first thread of the core */
  112. if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
  113. smp_send_reschedule(cpu);
  114. }
  115. /*
  116. * We use the vcpu_load/put functions to measure stolen time.
  117. * Stolen time is counted as time when either the vcpu is able to
  118. * run as part of a virtual core, but the task running the vcore
  119. * is preempted or sleeping, or when the vcpu needs something done
  120. * in the kernel by the task running the vcpu, but that task is
  121. * preempted or sleeping. Those two things have to be counted
  122. * separately, since one of the vcpu tasks will take on the job
  123. * of running the core, and the other vcpu tasks in the vcore will
  124. * sleep waiting for it to do that, but that sleep shouldn't count
  125. * as stolen time.
  126. *
  127. * Hence we accumulate stolen time when the vcpu can run as part of
  128. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  129. * needs its task to do other things in the kernel (for example,
  130. * service a page fault) in busy_stolen. We don't accumulate
  131. * stolen time for a vcore when it is inactive, or for a vcpu
  132. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  133. * a misnomer; it means that the vcpu task is not executing in
  134. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  135. * the kernel. We don't have any way of dividing up that time
  136. * between time that the vcpu is genuinely stopped, time that
  137. * the task is actively working on behalf of the vcpu, and time
  138. * that the task is preempted, so we don't count any of it as
  139. * stolen.
  140. *
  141. * Updates to busy_stolen are protected by arch.tbacct_lock;
  142. * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
  143. * lock. The stolen times are measured in units of timebase ticks.
  144. * (Note that the != TB_NIL checks below are purely defensive;
  145. * they should never fail.)
  146. */
  147. static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
  148. {
  149. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  150. unsigned long flags;
  151. /*
  152. * We can test vc->runner without taking the vcore lock,
  153. * because only this task ever sets vc->runner to this
  154. * vcpu, and once it is set to this vcpu, only this task
  155. * ever sets it to NULL.
  156. */
  157. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
  158. spin_lock_irqsave(&vc->stoltb_lock, flags);
  159. if (vc->preempt_tb != TB_NIL) {
  160. vc->stolen_tb += mftb() - vc->preempt_tb;
  161. vc->preempt_tb = TB_NIL;
  162. }
  163. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  164. }
  165. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  166. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  167. vcpu->arch.busy_preempt != TB_NIL) {
  168. vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
  169. vcpu->arch.busy_preempt = TB_NIL;
  170. }
  171. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  172. }
  173. static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
  174. {
  175. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  176. unsigned long flags;
  177. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
  178. spin_lock_irqsave(&vc->stoltb_lock, flags);
  179. vc->preempt_tb = mftb();
  180. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  181. }
  182. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  183. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  184. vcpu->arch.busy_preempt = mftb();
  185. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  186. }
  187. static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
  188. {
  189. vcpu->arch.shregs.msr = msr;
  190. kvmppc_end_cede(vcpu);
  191. }
  192. void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
  193. {
  194. vcpu->arch.pvr = pvr;
  195. }
  196. int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
  197. {
  198. unsigned long pcr = 0;
  199. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  200. if (arch_compat) {
  201. switch (arch_compat) {
  202. case PVR_ARCH_205:
  203. /*
  204. * If an arch bit is set in PCR, all the defined
  205. * higher-order arch bits also have to be set.
  206. */
  207. pcr = PCR_ARCH_206 | PCR_ARCH_205;
  208. break;
  209. case PVR_ARCH_206:
  210. case PVR_ARCH_206p:
  211. pcr = PCR_ARCH_206;
  212. break;
  213. case PVR_ARCH_207:
  214. break;
  215. default:
  216. return -EINVAL;
  217. }
  218. if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
  219. /* POWER7 can't emulate POWER8 */
  220. if (!(pcr & PCR_ARCH_206))
  221. return -EINVAL;
  222. pcr &= ~PCR_ARCH_206;
  223. }
  224. }
  225. spin_lock(&vc->lock);
  226. vc->arch_compat = arch_compat;
  227. vc->pcr = pcr;
  228. spin_unlock(&vc->lock);
  229. return 0;
  230. }
  231. void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  232. {
  233. int r;
  234. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  235. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  236. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  237. for (r = 0; r < 16; ++r)
  238. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  239. r, kvmppc_get_gpr(vcpu, r),
  240. r+16, kvmppc_get_gpr(vcpu, r+16));
  241. pr_err("ctr = %.16lx lr = %.16lx\n",
  242. vcpu->arch.ctr, vcpu->arch.lr);
  243. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  244. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  245. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  246. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  247. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  248. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  249. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  250. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  251. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  252. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  253. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  254. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  255. for (r = 0; r < vcpu->arch.slb_max; ++r)
  256. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  257. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  258. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  259. vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
  260. vcpu->arch.last_inst);
  261. }
  262. struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  263. {
  264. int r;
  265. struct kvm_vcpu *v, *ret = NULL;
  266. mutex_lock(&kvm->lock);
  267. kvm_for_each_vcpu(r, v, kvm) {
  268. if (v->vcpu_id == id) {
  269. ret = v;
  270. break;
  271. }
  272. }
  273. mutex_unlock(&kvm->lock);
  274. return ret;
  275. }
  276. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  277. {
  278. vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
  279. vpa->yield_count = cpu_to_be32(1);
  280. }
  281. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  282. unsigned long addr, unsigned long len)
  283. {
  284. /* check address is cacheline aligned */
  285. if (addr & (L1_CACHE_BYTES - 1))
  286. return -EINVAL;
  287. spin_lock(&vcpu->arch.vpa_update_lock);
  288. if (v->next_gpa != addr || v->len != len) {
  289. v->next_gpa = addr;
  290. v->len = addr ? len : 0;
  291. v->update_pending = 1;
  292. }
  293. spin_unlock(&vcpu->arch.vpa_update_lock);
  294. return 0;
  295. }
  296. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  297. struct reg_vpa {
  298. u32 dummy;
  299. union {
  300. __be16 hword;
  301. __be32 word;
  302. } length;
  303. };
  304. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  305. {
  306. if (vpap->update_pending)
  307. return vpap->next_gpa != 0;
  308. return vpap->pinned_addr != NULL;
  309. }
  310. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  311. unsigned long flags,
  312. unsigned long vcpuid, unsigned long vpa)
  313. {
  314. struct kvm *kvm = vcpu->kvm;
  315. unsigned long len, nb;
  316. void *va;
  317. struct kvm_vcpu *tvcpu;
  318. int err;
  319. int subfunc;
  320. struct kvmppc_vpa *vpap;
  321. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  322. if (!tvcpu)
  323. return H_PARAMETER;
  324. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  325. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  326. subfunc == H_VPA_REG_SLB) {
  327. /* Registering new area - address must be cache-line aligned */
  328. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  329. return H_PARAMETER;
  330. /* convert logical addr to kernel addr and read length */
  331. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  332. if (va == NULL)
  333. return H_PARAMETER;
  334. if (subfunc == H_VPA_REG_VPA)
  335. len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
  336. else
  337. len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
  338. kvmppc_unpin_guest_page(kvm, va, vpa, false);
  339. /* Check length */
  340. if (len > nb || len < sizeof(struct reg_vpa))
  341. return H_PARAMETER;
  342. } else {
  343. vpa = 0;
  344. len = 0;
  345. }
  346. err = H_PARAMETER;
  347. vpap = NULL;
  348. spin_lock(&tvcpu->arch.vpa_update_lock);
  349. switch (subfunc) {
  350. case H_VPA_REG_VPA: /* register VPA */
  351. if (len < sizeof(struct lppaca))
  352. break;
  353. vpap = &tvcpu->arch.vpa;
  354. err = 0;
  355. break;
  356. case H_VPA_REG_DTL: /* register DTL */
  357. if (len < sizeof(struct dtl_entry))
  358. break;
  359. len -= len % sizeof(struct dtl_entry);
  360. /* Check that they have previously registered a VPA */
  361. err = H_RESOURCE;
  362. if (!vpa_is_registered(&tvcpu->arch.vpa))
  363. break;
  364. vpap = &tvcpu->arch.dtl;
  365. err = 0;
  366. break;
  367. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  368. /* Check that they have previously registered a VPA */
  369. err = H_RESOURCE;
  370. if (!vpa_is_registered(&tvcpu->arch.vpa))
  371. break;
  372. vpap = &tvcpu->arch.slb_shadow;
  373. err = 0;
  374. break;
  375. case H_VPA_DEREG_VPA: /* deregister VPA */
  376. /* Check they don't still have a DTL or SLB buf registered */
  377. err = H_RESOURCE;
  378. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  379. vpa_is_registered(&tvcpu->arch.slb_shadow))
  380. break;
  381. vpap = &tvcpu->arch.vpa;
  382. err = 0;
  383. break;
  384. case H_VPA_DEREG_DTL: /* deregister DTL */
  385. vpap = &tvcpu->arch.dtl;
  386. err = 0;
  387. break;
  388. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  389. vpap = &tvcpu->arch.slb_shadow;
  390. err = 0;
  391. break;
  392. }
  393. if (vpap) {
  394. vpap->next_gpa = vpa;
  395. vpap->len = len;
  396. vpap->update_pending = 1;
  397. }
  398. spin_unlock(&tvcpu->arch.vpa_update_lock);
  399. return err;
  400. }
  401. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  402. {
  403. struct kvm *kvm = vcpu->kvm;
  404. void *va;
  405. unsigned long nb;
  406. unsigned long gpa;
  407. /*
  408. * We need to pin the page pointed to by vpap->next_gpa,
  409. * but we can't call kvmppc_pin_guest_page under the lock
  410. * as it does get_user_pages() and down_read(). So we
  411. * have to drop the lock, pin the page, then get the lock
  412. * again and check that a new area didn't get registered
  413. * in the meantime.
  414. */
  415. for (;;) {
  416. gpa = vpap->next_gpa;
  417. spin_unlock(&vcpu->arch.vpa_update_lock);
  418. va = NULL;
  419. nb = 0;
  420. if (gpa)
  421. va = kvmppc_pin_guest_page(kvm, gpa, &nb);
  422. spin_lock(&vcpu->arch.vpa_update_lock);
  423. if (gpa == vpap->next_gpa)
  424. break;
  425. /* sigh... unpin that one and try again */
  426. if (va)
  427. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  428. }
  429. vpap->update_pending = 0;
  430. if (va && nb < vpap->len) {
  431. /*
  432. * If it's now too short, it must be that userspace
  433. * has changed the mappings underlying guest memory,
  434. * so unregister the region.
  435. */
  436. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  437. va = NULL;
  438. }
  439. if (vpap->pinned_addr)
  440. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
  441. vpap->dirty);
  442. vpap->gpa = gpa;
  443. vpap->pinned_addr = va;
  444. vpap->dirty = false;
  445. if (va)
  446. vpap->pinned_end = va + vpap->len;
  447. }
  448. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  449. {
  450. if (!(vcpu->arch.vpa.update_pending ||
  451. vcpu->arch.slb_shadow.update_pending ||
  452. vcpu->arch.dtl.update_pending))
  453. return;
  454. spin_lock(&vcpu->arch.vpa_update_lock);
  455. if (vcpu->arch.vpa.update_pending) {
  456. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  457. if (vcpu->arch.vpa.pinned_addr)
  458. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  459. }
  460. if (vcpu->arch.dtl.update_pending) {
  461. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  462. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  463. vcpu->arch.dtl_index = 0;
  464. }
  465. if (vcpu->arch.slb_shadow.update_pending)
  466. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  467. spin_unlock(&vcpu->arch.vpa_update_lock);
  468. }
  469. /*
  470. * Return the accumulated stolen time for the vcore up until `now'.
  471. * The caller should hold the vcore lock.
  472. */
  473. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  474. {
  475. u64 p;
  476. unsigned long flags;
  477. spin_lock_irqsave(&vc->stoltb_lock, flags);
  478. p = vc->stolen_tb;
  479. if (vc->vcore_state != VCORE_INACTIVE &&
  480. vc->preempt_tb != TB_NIL)
  481. p += now - vc->preempt_tb;
  482. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  483. return p;
  484. }
  485. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  486. struct kvmppc_vcore *vc)
  487. {
  488. struct dtl_entry *dt;
  489. struct lppaca *vpa;
  490. unsigned long stolen;
  491. unsigned long core_stolen;
  492. u64 now;
  493. dt = vcpu->arch.dtl_ptr;
  494. vpa = vcpu->arch.vpa.pinned_addr;
  495. now = mftb();
  496. core_stolen = vcore_stolen_time(vc, now);
  497. stolen = core_stolen - vcpu->arch.stolen_logged;
  498. vcpu->arch.stolen_logged = core_stolen;
  499. spin_lock_irq(&vcpu->arch.tbacct_lock);
  500. stolen += vcpu->arch.busy_stolen;
  501. vcpu->arch.busy_stolen = 0;
  502. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  503. if (!dt || !vpa)
  504. return;
  505. memset(dt, 0, sizeof(struct dtl_entry));
  506. dt->dispatch_reason = 7;
  507. dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
  508. dt->timebase = cpu_to_be64(now + vc->tb_offset);
  509. dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
  510. dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
  511. dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
  512. ++dt;
  513. if (dt == vcpu->arch.dtl.pinned_end)
  514. dt = vcpu->arch.dtl.pinned_addr;
  515. vcpu->arch.dtl_ptr = dt;
  516. /* order writing *dt vs. writing vpa->dtl_idx */
  517. smp_wmb();
  518. vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
  519. vcpu->arch.dtl.dirty = true;
  520. }
  521. static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
  522. {
  523. if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
  524. return true;
  525. if ((!vcpu->arch.vcore->arch_compat) &&
  526. cpu_has_feature(CPU_FTR_ARCH_207S))
  527. return true;
  528. return false;
  529. }
  530. static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
  531. unsigned long resource, unsigned long value1,
  532. unsigned long value2)
  533. {
  534. switch (resource) {
  535. case H_SET_MODE_RESOURCE_SET_CIABR:
  536. if (!kvmppc_power8_compatible(vcpu))
  537. return H_P2;
  538. if (value2)
  539. return H_P4;
  540. if (mflags)
  541. return H_UNSUPPORTED_FLAG_START;
  542. /* Guests can't breakpoint the hypervisor */
  543. if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
  544. return H_P3;
  545. vcpu->arch.ciabr = value1;
  546. return H_SUCCESS;
  547. case H_SET_MODE_RESOURCE_SET_DAWR:
  548. if (!kvmppc_power8_compatible(vcpu))
  549. return H_P2;
  550. if (mflags)
  551. return H_UNSUPPORTED_FLAG_START;
  552. if (value2 & DABRX_HYP)
  553. return H_P4;
  554. vcpu->arch.dawr = value1;
  555. vcpu->arch.dawrx = value2;
  556. return H_SUCCESS;
  557. default:
  558. return H_TOO_HARD;
  559. }
  560. }
  561. static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
  562. {
  563. struct kvmppc_vcore *vcore = target->arch.vcore;
  564. /*
  565. * We expect to have been called by the real mode handler
  566. * (kvmppc_rm_h_confer()) which would have directly returned
  567. * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
  568. * have useful work to do and should not confer) so we don't
  569. * recheck that here.
  570. */
  571. spin_lock(&vcore->lock);
  572. if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
  573. vcore->vcore_state != VCORE_INACTIVE)
  574. target = vcore->runner;
  575. spin_unlock(&vcore->lock);
  576. return kvm_vcpu_yield_to(target);
  577. }
  578. static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
  579. {
  580. int yield_count = 0;
  581. struct lppaca *lppaca;
  582. spin_lock(&vcpu->arch.vpa_update_lock);
  583. lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
  584. if (lppaca)
  585. yield_count = be32_to_cpu(lppaca->yield_count);
  586. spin_unlock(&vcpu->arch.vpa_update_lock);
  587. return yield_count;
  588. }
  589. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  590. {
  591. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  592. unsigned long target, ret = H_SUCCESS;
  593. int yield_count;
  594. struct kvm_vcpu *tvcpu;
  595. int idx, rc;
  596. if (req <= MAX_HCALL_OPCODE &&
  597. !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
  598. return RESUME_HOST;
  599. switch (req) {
  600. case H_CEDE:
  601. break;
  602. case H_PROD:
  603. target = kvmppc_get_gpr(vcpu, 4);
  604. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  605. if (!tvcpu) {
  606. ret = H_PARAMETER;
  607. break;
  608. }
  609. tvcpu->arch.prodded = 1;
  610. smp_mb();
  611. if (vcpu->arch.ceded) {
  612. if (waitqueue_active(&vcpu->wq)) {
  613. wake_up_interruptible(&vcpu->wq);
  614. vcpu->stat.halt_wakeup++;
  615. }
  616. }
  617. break;
  618. case H_CONFER:
  619. target = kvmppc_get_gpr(vcpu, 4);
  620. if (target == -1)
  621. break;
  622. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  623. if (!tvcpu) {
  624. ret = H_PARAMETER;
  625. break;
  626. }
  627. yield_count = kvmppc_get_gpr(vcpu, 5);
  628. if (kvmppc_get_yield_count(tvcpu) != yield_count)
  629. break;
  630. kvm_arch_vcpu_yield_to(tvcpu);
  631. break;
  632. case H_REGISTER_VPA:
  633. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  634. kvmppc_get_gpr(vcpu, 5),
  635. kvmppc_get_gpr(vcpu, 6));
  636. break;
  637. case H_RTAS:
  638. if (list_empty(&vcpu->kvm->arch.rtas_tokens))
  639. return RESUME_HOST;
  640. idx = srcu_read_lock(&vcpu->kvm->srcu);
  641. rc = kvmppc_rtas_hcall(vcpu);
  642. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  643. if (rc == -ENOENT)
  644. return RESUME_HOST;
  645. else if (rc == 0)
  646. break;
  647. /* Send the error out to userspace via KVM_RUN */
  648. return rc;
  649. case H_LOGICAL_CI_LOAD:
  650. ret = kvmppc_h_logical_ci_load(vcpu);
  651. if (ret == H_TOO_HARD)
  652. return RESUME_HOST;
  653. break;
  654. case H_LOGICAL_CI_STORE:
  655. ret = kvmppc_h_logical_ci_store(vcpu);
  656. if (ret == H_TOO_HARD)
  657. return RESUME_HOST;
  658. break;
  659. case H_SET_MODE:
  660. ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
  661. kvmppc_get_gpr(vcpu, 5),
  662. kvmppc_get_gpr(vcpu, 6),
  663. kvmppc_get_gpr(vcpu, 7));
  664. if (ret == H_TOO_HARD)
  665. return RESUME_HOST;
  666. break;
  667. case H_XIRR:
  668. case H_CPPR:
  669. case H_EOI:
  670. case H_IPI:
  671. case H_IPOLL:
  672. case H_XIRR_X:
  673. if (kvmppc_xics_enabled(vcpu)) {
  674. ret = kvmppc_xics_hcall(vcpu, req);
  675. break;
  676. } /* fallthrough */
  677. default:
  678. return RESUME_HOST;
  679. }
  680. kvmppc_set_gpr(vcpu, 3, ret);
  681. vcpu->arch.hcall_needed = 0;
  682. return RESUME_GUEST;
  683. }
  684. static int kvmppc_hcall_impl_hv(unsigned long cmd)
  685. {
  686. switch (cmd) {
  687. case H_CEDE:
  688. case H_PROD:
  689. case H_CONFER:
  690. case H_REGISTER_VPA:
  691. case H_SET_MODE:
  692. case H_LOGICAL_CI_LOAD:
  693. case H_LOGICAL_CI_STORE:
  694. #ifdef CONFIG_KVM_XICS
  695. case H_XIRR:
  696. case H_CPPR:
  697. case H_EOI:
  698. case H_IPI:
  699. case H_IPOLL:
  700. case H_XIRR_X:
  701. #endif
  702. return 1;
  703. }
  704. /* See if it's in the real-mode table */
  705. return kvmppc_hcall_impl_hv_realmode(cmd);
  706. }
  707. static int kvmppc_emulate_debug_inst(struct kvm_run *run,
  708. struct kvm_vcpu *vcpu)
  709. {
  710. u32 last_inst;
  711. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  712. EMULATE_DONE) {
  713. /*
  714. * Fetch failed, so return to guest and
  715. * try executing it again.
  716. */
  717. return RESUME_GUEST;
  718. }
  719. if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
  720. run->exit_reason = KVM_EXIT_DEBUG;
  721. run->debug.arch.address = kvmppc_get_pc(vcpu);
  722. return RESUME_HOST;
  723. } else {
  724. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  725. return RESUME_GUEST;
  726. }
  727. }
  728. static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  729. struct task_struct *tsk)
  730. {
  731. int r = RESUME_HOST;
  732. vcpu->stat.sum_exits++;
  733. run->exit_reason = KVM_EXIT_UNKNOWN;
  734. run->ready_for_interrupt_injection = 1;
  735. switch (vcpu->arch.trap) {
  736. /* We're good on these - the host merely wanted to get our attention */
  737. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  738. vcpu->stat.dec_exits++;
  739. r = RESUME_GUEST;
  740. break;
  741. case BOOK3S_INTERRUPT_EXTERNAL:
  742. case BOOK3S_INTERRUPT_H_DOORBELL:
  743. vcpu->stat.ext_intr_exits++;
  744. r = RESUME_GUEST;
  745. break;
  746. /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
  747. case BOOK3S_INTERRUPT_HMI:
  748. case BOOK3S_INTERRUPT_PERFMON:
  749. r = RESUME_GUEST;
  750. break;
  751. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  752. /*
  753. * Deliver a machine check interrupt to the guest.
  754. * We have to do this, even if the host has handled the
  755. * machine check, because machine checks use SRR0/1 and
  756. * the interrupt might have trashed guest state in them.
  757. */
  758. kvmppc_book3s_queue_irqprio(vcpu,
  759. BOOK3S_INTERRUPT_MACHINE_CHECK);
  760. r = RESUME_GUEST;
  761. break;
  762. case BOOK3S_INTERRUPT_PROGRAM:
  763. {
  764. ulong flags;
  765. /*
  766. * Normally program interrupts are delivered directly
  767. * to the guest by the hardware, but we can get here
  768. * as a result of a hypervisor emulation interrupt
  769. * (e40) getting turned into a 700 by BML RTAS.
  770. */
  771. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  772. kvmppc_core_queue_program(vcpu, flags);
  773. r = RESUME_GUEST;
  774. break;
  775. }
  776. case BOOK3S_INTERRUPT_SYSCALL:
  777. {
  778. /* hcall - punt to userspace */
  779. int i;
  780. /* hypercall with MSR_PR has already been handled in rmode,
  781. * and never reaches here.
  782. */
  783. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  784. for (i = 0; i < 9; ++i)
  785. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  786. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  787. vcpu->arch.hcall_needed = 1;
  788. r = RESUME_HOST;
  789. break;
  790. }
  791. /*
  792. * We get these next two if the guest accesses a page which it thinks
  793. * it has mapped but which is not actually present, either because
  794. * it is for an emulated I/O device or because the corresonding
  795. * host page has been paged out. Any other HDSI/HISI interrupts
  796. * have been handled already.
  797. */
  798. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  799. r = RESUME_PAGE_FAULT;
  800. break;
  801. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  802. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  803. vcpu->arch.fault_dsisr = 0;
  804. r = RESUME_PAGE_FAULT;
  805. break;
  806. /*
  807. * This occurs if the guest executes an illegal instruction.
  808. * If the guest debug is disabled, generate a program interrupt
  809. * to the guest. If guest debug is enabled, we need to check
  810. * whether the instruction is a software breakpoint instruction.
  811. * Accordingly return to Guest or Host.
  812. */
  813. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  814. if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
  815. vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
  816. swab32(vcpu->arch.emul_inst) :
  817. vcpu->arch.emul_inst;
  818. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
  819. r = kvmppc_emulate_debug_inst(run, vcpu);
  820. } else {
  821. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  822. r = RESUME_GUEST;
  823. }
  824. break;
  825. /*
  826. * This occurs if the guest (kernel or userspace), does something that
  827. * is prohibited by HFSCR. We just generate a program interrupt to
  828. * the guest.
  829. */
  830. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
  831. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  832. r = RESUME_GUEST;
  833. break;
  834. default:
  835. kvmppc_dump_regs(vcpu);
  836. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  837. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  838. vcpu->arch.shregs.msr);
  839. run->hw.hardware_exit_reason = vcpu->arch.trap;
  840. r = RESUME_HOST;
  841. break;
  842. }
  843. return r;
  844. }
  845. static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
  846. struct kvm_sregs *sregs)
  847. {
  848. int i;
  849. memset(sregs, 0, sizeof(struct kvm_sregs));
  850. sregs->pvr = vcpu->arch.pvr;
  851. for (i = 0; i < vcpu->arch.slb_max; i++) {
  852. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  853. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  854. }
  855. return 0;
  856. }
  857. static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
  858. struct kvm_sregs *sregs)
  859. {
  860. int i, j;
  861. /* Only accept the same PVR as the host's, since we can't spoof it */
  862. if (sregs->pvr != vcpu->arch.pvr)
  863. return -EINVAL;
  864. j = 0;
  865. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  866. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  867. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  868. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  869. ++j;
  870. }
  871. }
  872. vcpu->arch.slb_max = j;
  873. return 0;
  874. }
  875. static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
  876. bool preserve_top32)
  877. {
  878. struct kvm *kvm = vcpu->kvm;
  879. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  880. u64 mask;
  881. mutex_lock(&kvm->lock);
  882. spin_lock(&vc->lock);
  883. /*
  884. * If ILE (interrupt little-endian) has changed, update the
  885. * MSR_LE bit in the intr_msr for each vcpu in this vcore.
  886. */
  887. if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
  888. struct kvm_vcpu *vcpu;
  889. int i;
  890. kvm_for_each_vcpu(i, vcpu, kvm) {
  891. if (vcpu->arch.vcore != vc)
  892. continue;
  893. if (new_lpcr & LPCR_ILE)
  894. vcpu->arch.intr_msr |= MSR_LE;
  895. else
  896. vcpu->arch.intr_msr &= ~MSR_LE;
  897. }
  898. }
  899. /*
  900. * Userspace can only modify DPFD (default prefetch depth),
  901. * ILE (interrupt little-endian) and TC (translation control).
  902. * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
  903. */
  904. mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
  905. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  906. mask |= LPCR_AIL;
  907. /* Broken 32-bit version of LPCR must not clear top bits */
  908. if (preserve_top32)
  909. mask &= 0xFFFFFFFF;
  910. vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
  911. spin_unlock(&vc->lock);
  912. mutex_unlock(&kvm->lock);
  913. }
  914. static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  915. union kvmppc_one_reg *val)
  916. {
  917. int r = 0;
  918. long int i;
  919. switch (id) {
  920. case KVM_REG_PPC_DEBUG_INST:
  921. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  922. break;
  923. case KVM_REG_PPC_HIOR:
  924. *val = get_reg_val(id, 0);
  925. break;
  926. case KVM_REG_PPC_DABR:
  927. *val = get_reg_val(id, vcpu->arch.dabr);
  928. break;
  929. case KVM_REG_PPC_DABRX:
  930. *val = get_reg_val(id, vcpu->arch.dabrx);
  931. break;
  932. case KVM_REG_PPC_DSCR:
  933. *val = get_reg_val(id, vcpu->arch.dscr);
  934. break;
  935. case KVM_REG_PPC_PURR:
  936. *val = get_reg_val(id, vcpu->arch.purr);
  937. break;
  938. case KVM_REG_PPC_SPURR:
  939. *val = get_reg_val(id, vcpu->arch.spurr);
  940. break;
  941. case KVM_REG_PPC_AMR:
  942. *val = get_reg_val(id, vcpu->arch.amr);
  943. break;
  944. case KVM_REG_PPC_UAMOR:
  945. *val = get_reg_val(id, vcpu->arch.uamor);
  946. break;
  947. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  948. i = id - KVM_REG_PPC_MMCR0;
  949. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  950. break;
  951. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  952. i = id - KVM_REG_PPC_PMC1;
  953. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  954. break;
  955. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  956. i = id - KVM_REG_PPC_SPMC1;
  957. *val = get_reg_val(id, vcpu->arch.spmc[i]);
  958. break;
  959. case KVM_REG_PPC_SIAR:
  960. *val = get_reg_val(id, vcpu->arch.siar);
  961. break;
  962. case KVM_REG_PPC_SDAR:
  963. *val = get_reg_val(id, vcpu->arch.sdar);
  964. break;
  965. case KVM_REG_PPC_SIER:
  966. *val = get_reg_val(id, vcpu->arch.sier);
  967. break;
  968. case KVM_REG_PPC_IAMR:
  969. *val = get_reg_val(id, vcpu->arch.iamr);
  970. break;
  971. case KVM_REG_PPC_PSPB:
  972. *val = get_reg_val(id, vcpu->arch.pspb);
  973. break;
  974. case KVM_REG_PPC_DPDES:
  975. *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
  976. break;
  977. case KVM_REG_PPC_DAWR:
  978. *val = get_reg_val(id, vcpu->arch.dawr);
  979. break;
  980. case KVM_REG_PPC_DAWRX:
  981. *val = get_reg_val(id, vcpu->arch.dawrx);
  982. break;
  983. case KVM_REG_PPC_CIABR:
  984. *val = get_reg_val(id, vcpu->arch.ciabr);
  985. break;
  986. case KVM_REG_PPC_CSIGR:
  987. *val = get_reg_val(id, vcpu->arch.csigr);
  988. break;
  989. case KVM_REG_PPC_TACR:
  990. *val = get_reg_val(id, vcpu->arch.tacr);
  991. break;
  992. case KVM_REG_PPC_TCSCR:
  993. *val = get_reg_val(id, vcpu->arch.tcscr);
  994. break;
  995. case KVM_REG_PPC_PID:
  996. *val = get_reg_val(id, vcpu->arch.pid);
  997. break;
  998. case KVM_REG_PPC_ACOP:
  999. *val = get_reg_val(id, vcpu->arch.acop);
  1000. break;
  1001. case KVM_REG_PPC_WORT:
  1002. *val = get_reg_val(id, vcpu->arch.wort);
  1003. break;
  1004. case KVM_REG_PPC_VPA_ADDR:
  1005. spin_lock(&vcpu->arch.vpa_update_lock);
  1006. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  1007. spin_unlock(&vcpu->arch.vpa_update_lock);
  1008. break;
  1009. case KVM_REG_PPC_VPA_SLB:
  1010. spin_lock(&vcpu->arch.vpa_update_lock);
  1011. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  1012. val->vpaval.length = vcpu->arch.slb_shadow.len;
  1013. spin_unlock(&vcpu->arch.vpa_update_lock);
  1014. break;
  1015. case KVM_REG_PPC_VPA_DTL:
  1016. spin_lock(&vcpu->arch.vpa_update_lock);
  1017. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  1018. val->vpaval.length = vcpu->arch.dtl.len;
  1019. spin_unlock(&vcpu->arch.vpa_update_lock);
  1020. break;
  1021. case KVM_REG_PPC_TB_OFFSET:
  1022. *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
  1023. break;
  1024. case KVM_REG_PPC_LPCR:
  1025. case KVM_REG_PPC_LPCR_64:
  1026. *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
  1027. break;
  1028. case KVM_REG_PPC_PPR:
  1029. *val = get_reg_val(id, vcpu->arch.ppr);
  1030. break;
  1031. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1032. case KVM_REG_PPC_TFHAR:
  1033. *val = get_reg_val(id, vcpu->arch.tfhar);
  1034. break;
  1035. case KVM_REG_PPC_TFIAR:
  1036. *val = get_reg_val(id, vcpu->arch.tfiar);
  1037. break;
  1038. case KVM_REG_PPC_TEXASR:
  1039. *val = get_reg_val(id, vcpu->arch.texasr);
  1040. break;
  1041. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1042. i = id - KVM_REG_PPC_TM_GPR0;
  1043. *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
  1044. break;
  1045. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1046. {
  1047. int j;
  1048. i = id - KVM_REG_PPC_TM_VSR0;
  1049. if (i < 32)
  1050. for (j = 0; j < TS_FPRWIDTH; j++)
  1051. val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
  1052. else {
  1053. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1054. val->vval = vcpu->arch.vr_tm.vr[i-32];
  1055. else
  1056. r = -ENXIO;
  1057. }
  1058. break;
  1059. }
  1060. case KVM_REG_PPC_TM_CR:
  1061. *val = get_reg_val(id, vcpu->arch.cr_tm);
  1062. break;
  1063. case KVM_REG_PPC_TM_LR:
  1064. *val = get_reg_val(id, vcpu->arch.lr_tm);
  1065. break;
  1066. case KVM_REG_PPC_TM_CTR:
  1067. *val = get_reg_val(id, vcpu->arch.ctr_tm);
  1068. break;
  1069. case KVM_REG_PPC_TM_FPSCR:
  1070. *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
  1071. break;
  1072. case KVM_REG_PPC_TM_AMR:
  1073. *val = get_reg_val(id, vcpu->arch.amr_tm);
  1074. break;
  1075. case KVM_REG_PPC_TM_PPR:
  1076. *val = get_reg_val(id, vcpu->arch.ppr_tm);
  1077. break;
  1078. case KVM_REG_PPC_TM_VRSAVE:
  1079. *val = get_reg_val(id, vcpu->arch.vrsave_tm);
  1080. break;
  1081. case KVM_REG_PPC_TM_VSCR:
  1082. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1083. *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
  1084. else
  1085. r = -ENXIO;
  1086. break;
  1087. case KVM_REG_PPC_TM_DSCR:
  1088. *val = get_reg_val(id, vcpu->arch.dscr_tm);
  1089. break;
  1090. case KVM_REG_PPC_TM_TAR:
  1091. *val = get_reg_val(id, vcpu->arch.tar_tm);
  1092. break;
  1093. #endif
  1094. case KVM_REG_PPC_ARCH_COMPAT:
  1095. *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
  1096. break;
  1097. default:
  1098. r = -EINVAL;
  1099. break;
  1100. }
  1101. return r;
  1102. }
  1103. static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1104. union kvmppc_one_reg *val)
  1105. {
  1106. int r = 0;
  1107. long int i;
  1108. unsigned long addr, len;
  1109. switch (id) {
  1110. case KVM_REG_PPC_HIOR:
  1111. /* Only allow this to be set to zero */
  1112. if (set_reg_val(id, *val))
  1113. r = -EINVAL;
  1114. break;
  1115. case KVM_REG_PPC_DABR:
  1116. vcpu->arch.dabr = set_reg_val(id, *val);
  1117. break;
  1118. case KVM_REG_PPC_DABRX:
  1119. vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
  1120. break;
  1121. case KVM_REG_PPC_DSCR:
  1122. vcpu->arch.dscr = set_reg_val(id, *val);
  1123. break;
  1124. case KVM_REG_PPC_PURR:
  1125. vcpu->arch.purr = set_reg_val(id, *val);
  1126. break;
  1127. case KVM_REG_PPC_SPURR:
  1128. vcpu->arch.spurr = set_reg_val(id, *val);
  1129. break;
  1130. case KVM_REG_PPC_AMR:
  1131. vcpu->arch.amr = set_reg_val(id, *val);
  1132. break;
  1133. case KVM_REG_PPC_UAMOR:
  1134. vcpu->arch.uamor = set_reg_val(id, *val);
  1135. break;
  1136. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1137. i = id - KVM_REG_PPC_MMCR0;
  1138. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  1139. break;
  1140. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1141. i = id - KVM_REG_PPC_PMC1;
  1142. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  1143. break;
  1144. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1145. i = id - KVM_REG_PPC_SPMC1;
  1146. vcpu->arch.spmc[i] = set_reg_val(id, *val);
  1147. break;
  1148. case KVM_REG_PPC_SIAR:
  1149. vcpu->arch.siar = set_reg_val(id, *val);
  1150. break;
  1151. case KVM_REG_PPC_SDAR:
  1152. vcpu->arch.sdar = set_reg_val(id, *val);
  1153. break;
  1154. case KVM_REG_PPC_SIER:
  1155. vcpu->arch.sier = set_reg_val(id, *val);
  1156. break;
  1157. case KVM_REG_PPC_IAMR:
  1158. vcpu->arch.iamr = set_reg_val(id, *val);
  1159. break;
  1160. case KVM_REG_PPC_PSPB:
  1161. vcpu->arch.pspb = set_reg_val(id, *val);
  1162. break;
  1163. case KVM_REG_PPC_DPDES:
  1164. vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
  1165. break;
  1166. case KVM_REG_PPC_DAWR:
  1167. vcpu->arch.dawr = set_reg_val(id, *val);
  1168. break;
  1169. case KVM_REG_PPC_DAWRX:
  1170. vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
  1171. break;
  1172. case KVM_REG_PPC_CIABR:
  1173. vcpu->arch.ciabr = set_reg_val(id, *val);
  1174. /* Don't allow setting breakpoints in hypervisor code */
  1175. if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
  1176. vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
  1177. break;
  1178. case KVM_REG_PPC_CSIGR:
  1179. vcpu->arch.csigr = set_reg_val(id, *val);
  1180. break;
  1181. case KVM_REG_PPC_TACR:
  1182. vcpu->arch.tacr = set_reg_val(id, *val);
  1183. break;
  1184. case KVM_REG_PPC_TCSCR:
  1185. vcpu->arch.tcscr = set_reg_val(id, *val);
  1186. break;
  1187. case KVM_REG_PPC_PID:
  1188. vcpu->arch.pid = set_reg_val(id, *val);
  1189. break;
  1190. case KVM_REG_PPC_ACOP:
  1191. vcpu->arch.acop = set_reg_val(id, *val);
  1192. break;
  1193. case KVM_REG_PPC_WORT:
  1194. vcpu->arch.wort = set_reg_val(id, *val);
  1195. break;
  1196. case KVM_REG_PPC_VPA_ADDR:
  1197. addr = set_reg_val(id, *val);
  1198. r = -EINVAL;
  1199. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  1200. vcpu->arch.dtl.next_gpa))
  1201. break;
  1202. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  1203. break;
  1204. case KVM_REG_PPC_VPA_SLB:
  1205. addr = val->vpaval.addr;
  1206. len = val->vpaval.length;
  1207. r = -EINVAL;
  1208. if (addr && !vcpu->arch.vpa.next_gpa)
  1209. break;
  1210. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  1211. break;
  1212. case KVM_REG_PPC_VPA_DTL:
  1213. addr = val->vpaval.addr;
  1214. len = val->vpaval.length;
  1215. r = -EINVAL;
  1216. if (addr && (len < sizeof(struct dtl_entry) ||
  1217. !vcpu->arch.vpa.next_gpa))
  1218. break;
  1219. len -= len % sizeof(struct dtl_entry);
  1220. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  1221. break;
  1222. case KVM_REG_PPC_TB_OFFSET:
  1223. /* round up to multiple of 2^24 */
  1224. vcpu->arch.vcore->tb_offset =
  1225. ALIGN(set_reg_val(id, *val), 1UL << 24);
  1226. break;
  1227. case KVM_REG_PPC_LPCR:
  1228. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
  1229. break;
  1230. case KVM_REG_PPC_LPCR_64:
  1231. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
  1232. break;
  1233. case KVM_REG_PPC_PPR:
  1234. vcpu->arch.ppr = set_reg_val(id, *val);
  1235. break;
  1236. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1237. case KVM_REG_PPC_TFHAR:
  1238. vcpu->arch.tfhar = set_reg_val(id, *val);
  1239. break;
  1240. case KVM_REG_PPC_TFIAR:
  1241. vcpu->arch.tfiar = set_reg_val(id, *val);
  1242. break;
  1243. case KVM_REG_PPC_TEXASR:
  1244. vcpu->arch.texasr = set_reg_val(id, *val);
  1245. break;
  1246. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1247. i = id - KVM_REG_PPC_TM_GPR0;
  1248. vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
  1249. break;
  1250. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1251. {
  1252. int j;
  1253. i = id - KVM_REG_PPC_TM_VSR0;
  1254. if (i < 32)
  1255. for (j = 0; j < TS_FPRWIDTH; j++)
  1256. vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
  1257. else
  1258. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1259. vcpu->arch.vr_tm.vr[i-32] = val->vval;
  1260. else
  1261. r = -ENXIO;
  1262. break;
  1263. }
  1264. case KVM_REG_PPC_TM_CR:
  1265. vcpu->arch.cr_tm = set_reg_val(id, *val);
  1266. break;
  1267. case KVM_REG_PPC_TM_LR:
  1268. vcpu->arch.lr_tm = set_reg_val(id, *val);
  1269. break;
  1270. case KVM_REG_PPC_TM_CTR:
  1271. vcpu->arch.ctr_tm = set_reg_val(id, *val);
  1272. break;
  1273. case KVM_REG_PPC_TM_FPSCR:
  1274. vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
  1275. break;
  1276. case KVM_REG_PPC_TM_AMR:
  1277. vcpu->arch.amr_tm = set_reg_val(id, *val);
  1278. break;
  1279. case KVM_REG_PPC_TM_PPR:
  1280. vcpu->arch.ppr_tm = set_reg_val(id, *val);
  1281. break;
  1282. case KVM_REG_PPC_TM_VRSAVE:
  1283. vcpu->arch.vrsave_tm = set_reg_val(id, *val);
  1284. break;
  1285. case KVM_REG_PPC_TM_VSCR:
  1286. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1287. vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
  1288. else
  1289. r = - ENXIO;
  1290. break;
  1291. case KVM_REG_PPC_TM_DSCR:
  1292. vcpu->arch.dscr_tm = set_reg_val(id, *val);
  1293. break;
  1294. case KVM_REG_PPC_TM_TAR:
  1295. vcpu->arch.tar_tm = set_reg_val(id, *val);
  1296. break;
  1297. #endif
  1298. case KVM_REG_PPC_ARCH_COMPAT:
  1299. r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
  1300. break;
  1301. default:
  1302. r = -EINVAL;
  1303. break;
  1304. }
  1305. return r;
  1306. }
  1307. static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
  1308. {
  1309. struct kvmppc_vcore *vcore;
  1310. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  1311. if (vcore == NULL)
  1312. return NULL;
  1313. INIT_LIST_HEAD(&vcore->runnable_threads);
  1314. spin_lock_init(&vcore->lock);
  1315. spin_lock_init(&vcore->stoltb_lock);
  1316. init_waitqueue_head(&vcore->wq);
  1317. vcore->preempt_tb = TB_NIL;
  1318. vcore->lpcr = kvm->arch.lpcr;
  1319. vcore->first_vcpuid = core * threads_per_subcore;
  1320. vcore->kvm = kvm;
  1321. vcore->mpp_buffer_is_valid = false;
  1322. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  1323. vcore->mpp_buffer = (void *)__get_free_pages(
  1324. GFP_KERNEL|__GFP_ZERO,
  1325. MPP_BUFFER_ORDER);
  1326. return vcore;
  1327. }
  1328. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1329. static struct debugfs_timings_element {
  1330. const char *name;
  1331. size_t offset;
  1332. } timings[] = {
  1333. {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
  1334. {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
  1335. {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
  1336. {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
  1337. {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
  1338. };
  1339. #define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
  1340. struct debugfs_timings_state {
  1341. struct kvm_vcpu *vcpu;
  1342. unsigned int buflen;
  1343. char buf[N_TIMINGS * 100];
  1344. };
  1345. static int debugfs_timings_open(struct inode *inode, struct file *file)
  1346. {
  1347. struct kvm_vcpu *vcpu = inode->i_private;
  1348. struct debugfs_timings_state *p;
  1349. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1350. if (!p)
  1351. return -ENOMEM;
  1352. kvm_get_kvm(vcpu->kvm);
  1353. p->vcpu = vcpu;
  1354. file->private_data = p;
  1355. return nonseekable_open(inode, file);
  1356. }
  1357. static int debugfs_timings_release(struct inode *inode, struct file *file)
  1358. {
  1359. struct debugfs_timings_state *p = file->private_data;
  1360. kvm_put_kvm(p->vcpu->kvm);
  1361. kfree(p);
  1362. return 0;
  1363. }
  1364. static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
  1365. size_t len, loff_t *ppos)
  1366. {
  1367. struct debugfs_timings_state *p = file->private_data;
  1368. struct kvm_vcpu *vcpu = p->vcpu;
  1369. char *s, *buf_end;
  1370. struct kvmhv_tb_accumulator tb;
  1371. u64 count;
  1372. loff_t pos;
  1373. ssize_t n;
  1374. int i, loops;
  1375. bool ok;
  1376. if (!p->buflen) {
  1377. s = p->buf;
  1378. buf_end = s + sizeof(p->buf);
  1379. for (i = 0; i < N_TIMINGS; ++i) {
  1380. struct kvmhv_tb_accumulator *acc;
  1381. acc = (struct kvmhv_tb_accumulator *)
  1382. ((unsigned long)vcpu + timings[i].offset);
  1383. ok = false;
  1384. for (loops = 0; loops < 1000; ++loops) {
  1385. count = acc->seqcount;
  1386. if (!(count & 1)) {
  1387. smp_rmb();
  1388. tb = *acc;
  1389. smp_rmb();
  1390. if (count == acc->seqcount) {
  1391. ok = true;
  1392. break;
  1393. }
  1394. }
  1395. udelay(1);
  1396. }
  1397. if (!ok)
  1398. snprintf(s, buf_end - s, "%s: stuck\n",
  1399. timings[i].name);
  1400. else
  1401. snprintf(s, buf_end - s,
  1402. "%s: %llu %llu %llu %llu\n",
  1403. timings[i].name, count / 2,
  1404. tb_to_ns(tb.tb_total),
  1405. tb_to_ns(tb.tb_min),
  1406. tb_to_ns(tb.tb_max));
  1407. s += strlen(s);
  1408. }
  1409. p->buflen = s - p->buf;
  1410. }
  1411. pos = *ppos;
  1412. if (pos >= p->buflen)
  1413. return 0;
  1414. if (len > p->buflen - pos)
  1415. len = p->buflen - pos;
  1416. n = copy_to_user(buf, p->buf + pos, len);
  1417. if (n) {
  1418. if (n == len)
  1419. return -EFAULT;
  1420. len -= n;
  1421. }
  1422. *ppos = pos + len;
  1423. return len;
  1424. }
  1425. static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
  1426. size_t len, loff_t *ppos)
  1427. {
  1428. return -EACCES;
  1429. }
  1430. static const struct file_operations debugfs_timings_ops = {
  1431. .owner = THIS_MODULE,
  1432. .open = debugfs_timings_open,
  1433. .release = debugfs_timings_release,
  1434. .read = debugfs_timings_read,
  1435. .write = debugfs_timings_write,
  1436. .llseek = generic_file_llseek,
  1437. };
  1438. /* Create a debugfs directory for the vcpu */
  1439. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1440. {
  1441. char buf[16];
  1442. struct kvm *kvm = vcpu->kvm;
  1443. snprintf(buf, sizeof(buf), "vcpu%u", id);
  1444. if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  1445. return;
  1446. vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
  1447. if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
  1448. return;
  1449. vcpu->arch.debugfs_timings =
  1450. debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
  1451. vcpu, &debugfs_timings_ops);
  1452. }
  1453. #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1454. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1455. {
  1456. }
  1457. #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1458. static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
  1459. unsigned int id)
  1460. {
  1461. struct kvm_vcpu *vcpu;
  1462. int err = -EINVAL;
  1463. int core;
  1464. struct kvmppc_vcore *vcore;
  1465. core = id / threads_per_subcore;
  1466. if (core >= KVM_MAX_VCORES)
  1467. goto out;
  1468. err = -ENOMEM;
  1469. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1470. if (!vcpu)
  1471. goto out;
  1472. err = kvm_vcpu_init(vcpu, kvm, id);
  1473. if (err)
  1474. goto free_vcpu;
  1475. vcpu->arch.shared = &vcpu->arch.shregs;
  1476. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  1477. /*
  1478. * The shared struct is never shared on HV,
  1479. * so we can always use host endianness
  1480. */
  1481. #ifdef __BIG_ENDIAN__
  1482. vcpu->arch.shared_big_endian = true;
  1483. #else
  1484. vcpu->arch.shared_big_endian = false;
  1485. #endif
  1486. #endif
  1487. vcpu->arch.mmcr[0] = MMCR0_FC;
  1488. vcpu->arch.ctrl = CTRL_RUNLATCH;
  1489. /* default to host PVR, since we can't spoof it */
  1490. kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
  1491. spin_lock_init(&vcpu->arch.vpa_update_lock);
  1492. spin_lock_init(&vcpu->arch.tbacct_lock);
  1493. vcpu->arch.busy_preempt = TB_NIL;
  1494. vcpu->arch.intr_msr = MSR_SF | MSR_ME;
  1495. kvmppc_mmu_book3s_hv_init(vcpu);
  1496. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  1497. init_waitqueue_head(&vcpu->arch.cpu_run);
  1498. mutex_lock(&kvm->lock);
  1499. vcore = kvm->arch.vcores[core];
  1500. if (!vcore) {
  1501. vcore = kvmppc_vcore_create(kvm, core);
  1502. kvm->arch.vcores[core] = vcore;
  1503. kvm->arch.online_vcores++;
  1504. }
  1505. mutex_unlock(&kvm->lock);
  1506. if (!vcore)
  1507. goto free_vcpu;
  1508. spin_lock(&vcore->lock);
  1509. ++vcore->num_threads;
  1510. spin_unlock(&vcore->lock);
  1511. vcpu->arch.vcore = vcore;
  1512. vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
  1513. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  1514. kvmppc_sanity_check(vcpu);
  1515. debugfs_vcpu_init(vcpu, id);
  1516. return vcpu;
  1517. free_vcpu:
  1518. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1519. out:
  1520. return ERR_PTR(err);
  1521. }
  1522. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  1523. {
  1524. if (vpa->pinned_addr)
  1525. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  1526. vpa->dirty);
  1527. }
  1528. static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
  1529. {
  1530. spin_lock(&vcpu->arch.vpa_update_lock);
  1531. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  1532. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  1533. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  1534. spin_unlock(&vcpu->arch.vpa_update_lock);
  1535. kvm_vcpu_uninit(vcpu);
  1536. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1537. }
  1538. static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
  1539. {
  1540. /* Indicate we want to get back into the guest */
  1541. return 1;
  1542. }
  1543. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  1544. {
  1545. unsigned long dec_nsec, now;
  1546. now = get_tb();
  1547. if (now > vcpu->arch.dec_expires) {
  1548. /* decrementer has already gone negative */
  1549. kvmppc_core_queue_dec(vcpu);
  1550. kvmppc_core_prepare_to_enter(vcpu);
  1551. return;
  1552. }
  1553. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  1554. / tb_ticks_per_sec;
  1555. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  1556. HRTIMER_MODE_REL);
  1557. vcpu->arch.timer_running = 1;
  1558. }
  1559. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  1560. {
  1561. vcpu->arch.ceded = 0;
  1562. if (vcpu->arch.timer_running) {
  1563. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1564. vcpu->arch.timer_running = 0;
  1565. }
  1566. }
  1567. extern void __kvmppc_vcore_entry(void);
  1568. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  1569. struct kvm_vcpu *vcpu)
  1570. {
  1571. u64 now;
  1572. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1573. return;
  1574. spin_lock_irq(&vcpu->arch.tbacct_lock);
  1575. now = mftb();
  1576. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  1577. vcpu->arch.stolen_logged;
  1578. vcpu->arch.busy_preempt = now;
  1579. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  1580. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  1581. --vc->n_runnable;
  1582. list_del(&vcpu->arch.run_list);
  1583. }
  1584. static int kvmppc_grab_hwthread(int cpu)
  1585. {
  1586. struct paca_struct *tpaca;
  1587. long timeout = 10000;
  1588. tpaca = &paca[cpu];
  1589. /* Ensure the thread won't go into the kernel if it wakes */
  1590. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1591. tpaca->kvm_hstate.napping = 0;
  1592. smp_wmb();
  1593. tpaca->kvm_hstate.hwthread_req = 1;
  1594. /*
  1595. * If the thread is already executing in the kernel (e.g. handling
  1596. * a stray interrupt), wait for it to get back to nap mode.
  1597. * The smp_mb() is to ensure that our setting of hwthread_req
  1598. * is visible before we look at hwthread_state, so if this
  1599. * races with the code at system_reset_pSeries and the thread
  1600. * misses our setting of hwthread_req, we are sure to see its
  1601. * setting of hwthread_state, and vice versa.
  1602. */
  1603. smp_mb();
  1604. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  1605. if (--timeout <= 0) {
  1606. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  1607. return -EBUSY;
  1608. }
  1609. udelay(1);
  1610. }
  1611. return 0;
  1612. }
  1613. static void kvmppc_release_hwthread(int cpu)
  1614. {
  1615. struct paca_struct *tpaca;
  1616. tpaca = &paca[cpu];
  1617. tpaca->kvm_hstate.hwthread_req = 0;
  1618. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1619. }
  1620. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  1621. {
  1622. int cpu;
  1623. struct paca_struct *tpaca;
  1624. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  1625. if (vcpu->arch.timer_running) {
  1626. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1627. vcpu->arch.timer_running = 0;
  1628. }
  1629. cpu = vc->pcpu + vcpu->arch.ptid;
  1630. tpaca = &paca[cpu];
  1631. tpaca->kvm_hstate.kvm_vcore = vc;
  1632. tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
  1633. vcpu->cpu = vc->pcpu;
  1634. /* Order stores to hstate.kvm_vcore etc. before store to kvm_vcpu */
  1635. smp_wmb();
  1636. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  1637. if (cpu != smp_processor_id())
  1638. kvmppc_ipi_thread(cpu);
  1639. }
  1640. static void kvmppc_wait_for_nap(void)
  1641. {
  1642. int cpu = smp_processor_id();
  1643. int i, loops;
  1644. for (loops = 0; loops < 1000000; ++loops) {
  1645. /*
  1646. * Check if all threads are finished.
  1647. * We set the vcpu pointer when starting a thread
  1648. * and the thread clears it when finished, so we look
  1649. * for any threads that still have a non-NULL vcpu ptr.
  1650. */
  1651. for (i = 1; i < threads_per_subcore; ++i)
  1652. if (paca[cpu + i].kvm_hstate.kvm_vcpu)
  1653. break;
  1654. if (i == threads_per_subcore) {
  1655. HMT_medium();
  1656. return;
  1657. }
  1658. HMT_low();
  1659. }
  1660. HMT_medium();
  1661. for (i = 1; i < threads_per_subcore; ++i)
  1662. if (paca[cpu + i].kvm_hstate.kvm_vcpu)
  1663. pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
  1664. }
  1665. /*
  1666. * Check that we are on thread 0 and that any other threads in
  1667. * this core are off-line. Then grab the threads so they can't
  1668. * enter the kernel.
  1669. */
  1670. static int on_primary_thread(void)
  1671. {
  1672. int cpu = smp_processor_id();
  1673. int thr;
  1674. /* Are we on a primary subcore? */
  1675. if (cpu_thread_in_subcore(cpu))
  1676. return 0;
  1677. thr = 0;
  1678. while (++thr < threads_per_subcore)
  1679. if (cpu_online(cpu + thr))
  1680. return 0;
  1681. /* Grab all hw threads so they can't go into the kernel */
  1682. for (thr = 1; thr < threads_per_subcore; ++thr) {
  1683. if (kvmppc_grab_hwthread(cpu + thr)) {
  1684. /* Couldn't grab one; let the others go */
  1685. do {
  1686. kvmppc_release_hwthread(cpu + thr);
  1687. } while (--thr > 0);
  1688. return 0;
  1689. }
  1690. }
  1691. return 1;
  1692. }
  1693. static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
  1694. {
  1695. phys_addr_t phy_addr, mpp_addr;
  1696. phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
  1697. mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
  1698. mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
  1699. logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
  1700. vc->mpp_buffer_is_valid = true;
  1701. }
  1702. static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
  1703. {
  1704. phys_addr_t phy_addr, mpp_addr;
  1705. phy_addr = virt_to_phys(vc->mpp_buffer);
  1706. mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
  1707. /* We must abort any in-progress save operations to ensure
  1708. * the table is valid so that prefetch engine knows when to
  1709. * stop prefetching. */
  1710. logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
  1711. mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
  1712. }
  1713. static void prepare_threads(struct kvmppc_vcore *vc)
  1714. {
  1715. struct kvm_vcpu *vcpu, *vnext;
  1716. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  1717. arch.run_list) {
  1718. if (signal_pending(vcpu->arch.run_task))
  1719. vcpu->arch.ret = -EINTR;
  1720. else if (vcpu->arch.vpa.update_pending ||
  1721. vcpu->arch.slb_shadow.update_pending ||
  1722. vcpu->arch.dtl.update_pending)
  1723. vcpu->arch.ret = RESUME_GUEST;
  1724. else
  1725. continue;
  1726. kvmppc_remove_runnable(vc, vcpu);
  1727. wake_up(&vcpu->arch.cpu_run);
  1728. }
  1729. }
  1730. static void post_guest_process(struct kvmppc_vcore *vc)
  1731. {
  1732. u64 now;
  1733. long ret;
  1734. struct kvm_vcpu *vcpu, *vnext;
  1735. now = get_tb();
  1736. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  1737. arch.run_list) {
  1738. /* cancel pending dec exception if dec is positive */
  1739. if (now < vcpu->arch.dec_expires &&
  1740. kvmppc_core_pending_dec(vcpu))
  1741. kvmppc_core_dequeue_dec(vcpu);
  1742. trace_kvm_guest_exit(vcpu);
  1743. ret = RESUME_GUEST;
  1744. if (vcpu->arch.trap)
  1745. ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
  1746. vcpu->arch.run_task);
  1747. vcpu->arch.ret = ret;
  1748. vcpu->arch.trap = 0;
  1749. if (vcpu->arch.ceded) {
  1750. if (!is_kvmppc_resume_guest(ret))
  1751. kvmppc_end_cede(vcpu);
  1752. else
  1753. kvmppc_set_timer(vcpu);
  1754. }
  1755. if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
  1756. kvmppc_remove_runnable(vc, vcpu);
  1757. wake_up(&vcpu->arch.cpu_run);
  1758. }
  1759. }
  1760. }
  1761. /*
  1762. * Run a set of guest threads on a physical core.
  1763. * Called with vc->lock held.
  1764. */
  1765. static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
  1766. {
  1767. struct kvm_vcpu *vcpu, *vnext;
  1768. int i;
  1769. int srcu_idx;
  1770. /*
  1771. * Remove from the list any threads that have a signal pending
  1772. * or need a VPA update done
  1773. */
  1774. prepare_threads(vc);
  1775. /* if the runner is no longer runnable, let the caller pick a new one */
  1776. if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
  1777. return;
  1778. /*
  1779. * Initialize *vc.
  1780. */
  1781. vc->entry_exit_map = 0;
  1782. vc->preempt_tb = TB_NIL;
  1783. vc->in_guest = 0;
  1784. vc->napping_threads = 0;
  1785. vc->conferring_threads = 0;
  1786. /*
  1787. * Make sure we are running on primary threads, and that secondary
  1788. * threads are offline. Also check if the number of threads in this
  1789. * guest are greater than the current system threads per guest.
  1790. */
  1791. if ((threads_per_core > 1) &&
  1792. ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
  1793. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  1794. arch.run_list) {
  1795. vcpu->arch.ret = -EBUSY;
  1796. kvmppc_remove_runnable(vc, vcpu);
  1797. wake_up(&vcpu->arch.cpu_run);
  1798. }
  1799. goto out;
  1800. }
  1801. vc->pcpu = smp_processor_id();
  1802. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1803. kvmppc_start_thread(vcpu);
  1804. kvmppc_create_dtl_entry(vcpu, vc);
  1805. trace_kvm_guest_enter(vcpu);
  1806. }
  1807. /* Set this explicitly in case thread 0 doesn't have a vcpu */
  1808. get_paca()->kvm_hstate.kvm_vcore = vc;
  1809. get_paca()->kvm_hstate.ptid = 0;
  1810. vc->vcore_state = VCORE_RUNNING;
  1811. preempt_disable();
  1812. trace_kvmppc_run_core(vc, 0);
  1813. spin_unlock(&vc->lock);
  1814. kvm_guest_enter();
  1815. srcu_idx = srcu_read_lock(&vc->kvm->srcu);
  1816. if (vc->mpp_buffer_is_valid)
  1817. kvmppc_start_restoring_l2_cache(vc);
  1818. __kvmppc_vcore_entry();
  1819. spin_lock(&vc->lock);
  1820. if (vc->mpp_buffer)
  1821. kvmppc_start_saving_l2_cache(vc);
  1822. /* disable sending of IPIs on virtual external irqs */
  1823. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1824. vcpu->cpu = -1;
  1825. /* wait for secondary threads to finish writing their state to memory */
  1826. kvmppc_wait_for_nap();
  1827. for (i = 0; i < threads_per_subcore; ++i)
  1828. kvmppc_release_hwthread(vc->pcpu + i);
  1829. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  1830. vc->vcore_state = VCORE_EXITING;
  1831. spin_unlock(&vc->lock);
  1832. srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
  1833. /* make sure updates to secondary vcpu structs are visible now */
  1834. smp_mb();
  1835. kvm_guest_exit();
  1836. preempt_enable();
  1837. spin_lock(&vc->lock);
  1838. post_guest_process(vc);
  1839. out:
  1840. vc->vcore_state = VCORE_INACTIVE;
  1841. trace_kvmppc_run_core(vc, 1);
  1842. }
  1843. /*
  1844. * Wait for some other vcpu thread to execute us, and
  1845. * wake us up when we need to handle something in the host.
  1846. */
  1847. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  1848. {
  1849. DEFINE_WAIT(wait);
  1850. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  1851. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  1852. schedule();
  1853. finish_wait(&vcpu->arch.cpu_run, &wait);
  1854. }
  1855. /*
  1856. * All the vcpus in this vcore are idle, so wait for a decrementer
  1857. * or external interrupt to one of the vcpus. vc->lock is held.
  1858. */
  1859. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1860. {
  1861. struct kvm_vcpu *vcpu;
  1862. int do_sleep = 1;
  1863. DEFINE_WAIT(wait);
  1864. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1865. /*
  1866. * Check one last time for pending exceptions and ceded state after
  1867. * we put ourselves on the wait queue
  1868. */
  1869. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1870. if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
  1871. do_sleep = 0;
  1872. break;
  1873. }
  1874. }
  1875. if (!do_sleep) {
  1876. finish_wait(&vc->wq, &wait);
  1877. return;
  1878. }
  1879. vc->vcore_state = VCORE_SLEEPING;
  1880. trace_kvmppc_vcore_blocked(vc, 0);
  1881. spin_unlock(&vc->lock);
  1882. schedule();
  1883. finish_wait(&vc->wq, &wait);
  1884. spin_lock(&vc->lock);
  1885. vc->vcore_state = VCORE_INACTIVE;
  1886. trace_kvmppc_vcore_blocked(vc, 1);
  1887. }
  1888. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1889. {
  1890. int n_ceded;
  1891. struct kvmppc_vcore *vc;
  1892. struct kvm_vcpu *v, *vn;
  1893. trace_kvmppc_run_vcpu_enter(vcpu);
  1894. kvm_run->exit_reason = 0;
  1895. vcpu->arch.ret = RESUME_GUEST;
  1896. vcpu->arch.trap = 0;
  1897. kvmppc_update_vpas(vcpu);
  1898. /*
  1899. * Synchronize with other threads in this virtual core
  1900. */
  1901. vc = vcpu->arch.vcore;
  1902. spin_lock(&vc->lock);
  1903. vcpu->arch.ceded = 0;
  1904. vcpu->arch.run_task = current;
  1905. vcpu->arch.kvm_run = kvm_run;
  1906. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  1907. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  1908. vcpu->arch.busy_preempt = TB_NIL;
  1909. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  1910. ++vc->n_runnable;
  1911. /*
  1912. * This happens the first time this is called for a vcpu.
  1913. * If the vcore is already running, we may be able to start
  1914. * this thread straight away and have it join in.
  1915. */
  1916. if (!signal_pending(current)) {
  1917. if (vc->vcore_state == VCORE_RUNNING && !VCORE_IS_EXITING(vc)) {
  1918. kvmppc_create_dtl_entry(vcpu, vc);
  1919. kvmppc_start_thread(vcpu);
  1920. trace_kvm_guest_enter(vcpu);
  1921. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1922. wake_up(&vc->wq);
  1923. }
  1924. }
  1925. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1926. !signal_pending(current)) {
  1927. if (vc->vcore_state != VCORE_INACTIVE) {
  1928. spin_unlock(&vc->lock);
  1929. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  1930. spin_lock(&vc->lock);
  1931. continue;
  1932. }
  1933. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  1934. arch.run_list) {
  1935. kvmppc_core_prepare_to_enter(v);
  1936. if (signal_pending(v->arch.run_task)) {
  1937. kvmppc_remove_runnable(vc, v);
  1938. v->stat.signal_exits++;
  1939. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  1940. v->arch.ret = -EINTR;
  1941. wake_up(&v->arch.cpu_run);
  1942. }
  1943. }
  1944. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1945. break;
  1946. n_ceded = 0;
  1947. list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
  1948. if (!v->arch.pending_exceptions)
  1949. n_ceded += v->arch.ceded;
  1950. else
  1951. v->arch.ceded = 0;
  1952. }
  1953. vc->runner = vcpu;
  1954. if (n_ceded == vc->n_runnable) {
  1955. kvmppc_vcore_blocked(vc);
  1956. } else if (should_resched()) {
  1957. vc->vcore_state = VCORE_PREEMPT;
  1958. /* Let something else run */
  1959. cond_resched_lock(&vc->lock);
  1960. vc->vcore_state = VCORE_INACTIVE;
  1961. } else {
  1962. kvmppc_run_core(vc);
  1963. }
  1964. vc->runner = NULL;
  1965. }
  1966. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1967. (vc->vcore_state == VCORE_RUNNING ||
  1968. vc->vcore_state == VCORE_EXITING)) {
  1969. spin_unlock(&vc->lock);
  1970. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  1971. spin_lock(&vc->lock);
  1972. }
  1973. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  1974. kvmppc_remove_runnable(vc, vcpu);
  1975. vcpu->stat.signal_exits++;
  1976. kvm_run->exit_reason = KVM_EXIT_INTR;
  1977. vcpu->arch.ret = -EINTR;
  1978. }
  1979. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  1980. /* Wake up some vcpu to run the core */
  1981. v = list_first_entry(&vc->runnable_threads,
  1982. struct kvm_vcpu, arch.run_list);
  1983. wake_up(&v->arch.cpu_run);
  1984. }
  1985. trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
  1986. spin_unlock(&vc->lock);
  1987. return vcpu->arch.ret;
  1988. }
  1989. static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
  1990. {
  1991. int r;
  1992. int srcu_idx;
  1993. if (!vcpu->arch.sane) {
  1994. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1995. return -EINVAL;
  1996. }
  1997. kvmppc_core_prepare_to_enter(vcpu);
  1998. /* No need to go into the guest when all we'll do is come back out */
  1999. if (signal_pending(current)) {
  2000. run->exit_reason = KVM_EXIT_INTR;
  2001. return -EINTR;
  2002. }
  2003. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  2004. /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
  2005. smp_mb();
  2006. /* On the first time here, set up HTAB and VRMA */
  2007. if (!vcpu->kvm->arch.hpte_setup_done) {
  2008. r = kvmppc_hv_setup_htab_rma(vcpu);
  2009. if (r)
  2010. goto out;
  2011. }
  2012. flush_fp_to_thread(current);
  2013. flush_altivec_to_thread(current);
  2014. flush_vsx_to_thread(current);
  2015. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  2016. vcpu->arch.pgdir = current->mm->pgd;
  2017. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  2018. do {
  2019. r = kvmppc_run_vcpu(run, vcpu);
  2020. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  2021. !(vcpu->arch.shregs.msr & MSR_PR)) {
  2022. trace_kvm_hcall_enter(vcpu);
  2023. r = kvmppc_pseries_do_hcall(vcpu);
  2024. trace_kvm_hcall_exit(vcpu, r);
  2025. kvmppc_core_prepare_to_enter(vcpu);
  2026. } else if (r == RESUME_PAGE_FAULT) {
  2027. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  2028. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  2029. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  2030. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  2031. }
  2032. } while (is_kvmppc_resume_guest(r));
  2033. out:
  2034. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  2035. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  2036. return r;
  2037. }
  2038. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  2039. int linux_psize)
  2040. {
  2041. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  2042. if (!def->shift)
  2043. return;
  2044. (*sps)->page_shift = def->shift;
  2045. (*sps)->slb_enc = def->sllp;
  2046. (*sps)->enc[0].page_shift = def->shift;
  2047. (*sps)->enc[0].pte_enc = def->penc[linux_psize];
  2048. /*
  2049. * Add 16MB MPSS support if host supports it
  2050. */
  2051. if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
  2052. (*sps)->enc[1].page_shift = 24;
  2053. (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
  2054. }
  2055. (*sps)++;
  2056. }
  2057. static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
  2058. struct kvm_ppc_smmu_info *info)
  2059. {
  2060. struct kvm_ppc_one_seg_page_size *sps;
  2061. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  2062. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  2063. info->flags |= KVM_PPC_1T_SEGMENTS;
  2064. info->slb_size = mmu_slb_size;
  2065. /* We only support these sizes for now, and no muti-size segments */
  2066. sps = &info->sps[0];
  2067. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  2068. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  2069. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  2070. return 0;
  2071. }
  2072. /*
  2073. * Get (and clear) the dirty memory log for a memory slot.
  2074. */
  2075. static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
  2076. struct kvm_dirty_log *log)
  2077. {
  2078. struct kvm_memory_slot *memslot;
  2079. int r;
  2080. unsigned long n;
  2081. mutex_lock(&kvm->slots_lock);
  2082. r = -EINVAL;
  2083. if (log->slot >= KVM_USER_MEM_SLOTS)
  2084. goto out;
  2085. memslot = id_to_memslot(kvm->memslots, log->slot);
  2086. r = -ENOENT;
  2087. if (!memslot->dirty_bitmap)
  2088. goto out;
  2089. n = kvm_dirty_bitmap_bytes(memslot);
  2090. memset(memslot->dirty_bitmap, 0, n);
  2091. r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
  2092. if (r)
  2093. goto out;
  2094. r = -EFAULT;
  2095. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  2096. goto out;
  2097. r = 0;
  2098. out:
  2099. mutex_unlock(&kvm->slots_lock);
  2100. return r;
  2101. }
  2102. static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
  2103. struct kvm_memory_slot *dont)
  2104. {
  2105. if (!dont || free->arch.rmap != dont->arch.rmap) {
  2106. vfree(free->arch.rmap);
  2107. free->arch.rmap = NULL;
  2108. }
  2109. }
  2110. static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
  2111. unsigned long npages)
  2112. {
  2113. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  2114. if (!slot->arch.rmap)
  2115. return -ENOMEM;
  2116. return 0;
  2117. }
  2118. static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
  2119. struct kvm_memory_slot *memslot,
  2120. struct kvm_userspace_memory_region *mem)
  2121. {
  2122. return 0;
  2123. }
  2124. static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
  2125. struct kvm_userspace_memory_region *mem,
  2126. const struct kvm_memory_slot *old)
  2127. {
  2128. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  2129. struct kvm_memory_slot *memslot;
  2130. if (npages && old->npages) {
  2131. /*
  2132. * If modifying a memslot, reset all the rmap dirty bits.
  2133. * If this is a new memslot, we don't need to do anything
  2134. * since the rmap array starts out as all zeroes,
  2135. * i.e. no pages are dirty.
  2136. */
  2137. memslot = id_to_memslot(kvm->memslots, mem->slot);
  2138. kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
  2139. }
  2140. }
  2141. /*
  2142. * Update LPCR values in kvm->arch and in vcores.
  2143. * Caller must hold kvm->lock.
  2144. */
  2145. void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
  2146. {
  2147. long int i;
  2148. u32 cores_done = 0;
  2149. if ((kvm->arch.lpcr & mask) == lpcr)
  2150. return;
  2151. kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
  2152. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  2153. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  2154. if (!vc)
  2155. continue;
  2156. spin_lock(&vc->lock);
  2157. vc->lpcr = (vc->lpcr & ~mask) | lpcr;
  2158. spin_unlock(&vc->lock);
  2159. if (++cores_done >= kvm->arch.online_vcores)
  2160. break;
  2161. }
  2162. }
  2163. static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
  2164. {
  2165. return;
  2166. }
  2167. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  2168. {
  2169. int err = 0;
  2170. struct kvm *kvm = vcpu->kvm;
  2171. unsigned long hva;
  2172. struct kvm_memory_slot *memslot;
  2173. struct vm_area_struct *vma;
  2174. unsigned long lpcr = 0, senc;
  2175. unsigned long psize, porder;
  2176. int srcu_idx;
  2177. mutex_lock(&kvm->lock);
  2178. if (kvm->arch.hpte_setup_done)
  2179. goto out; /* another vcpu beat us to it */
  2180. /* Allocate hashed page table (if not done already) and reset it */
  2181. if (!kvm->arch.hpt_virt) {
  2182. err = kvmppc_alloc_hpt(kvm, NULL);
  2183. if (err) {
  2184. pr_err("KVM: Couldn't alloc HPT\n");
  2185. goto out;
  2186. }
  2187. }
  2188. /* Look up the memslot for guest physical address 0 */
  2189. srcu_idx = srcu_read_lock(&kvm->srcu);
  2190. memslot = gfn_to_memslot(kvm, 0);
  2191. /* We must have some memory at 0 by now */
  2192. err = -EINVAL;
  2193. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  2194. goto out_srcu;
  2195. /* Look up the VMA for the start of this memory slot */
  2196. hva = memslot->userspace_addr;
  2197. down_read(&current->mm->mmap_sem);
  2198. vma = find_vma(current->mm, hva);
  2199. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  2200. goto up_out;
  2201. psize = vma_kernel_pagesize(vma);
  2202. porder = __ilog2(psize);
  2203. up_read(&current->mm->mmap_sem);
  2204. /* We can handle 4k, 64k or 16M pages in the VRMA */
  2205. err = -EINVAL;
  2206. if (!(psize == 0x1000 || psize == 0x10000 ||
  2207. psize == 0x1000000))
  2208. goto out_srcu;
  2209. /* Update VRMASD field in the LPCR */
  2210. senc = slb_pgsize_encoding(psize);
  2211. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  2212. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  2213. /* the -4 is to account for senc values starting at 0x10 */
  2214. lpcr = senc << (LPCR_VRMASD_SH - 4);
  2215. /* Create HPTEs in the hash page table for the VRMA */
  2216. kvmppc_map_vrma(vcpu, memslot, porder);
  2217. kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
  2218. /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
  2219. smp_wmb();
  2220. kvm->arch.hpte_setup_done = 1;
  2221. err = 0;
  2222. out_srcu:
  2223. srcu_read_unlock(&kvm->srcu, srcu_idx);
  2224. out:
  2225. mutex_unlock(&kvm->lock);
  2226. return err;
  2227. up_out:
  2228. up_read(&current->mm->mmap_sem);
  2229. goto out_srcu;
  2230. }
  2231. static int kvmppc_core_init_vm_hv(struct kvm *kvm)
  2232. {
  2233. unsigned long lpcr, lpid;
  2234. char buf[32];
  2235. /* Allocate the guest's logical partition ID */
  2236. lpid = kvmppc_alloc_lpid();
  2237. if ((long)lpid < 0)
  2238. return -ENOMEM;
  2239. kvm->arch.lpid = lpid;
  2240. /*
  2241. * Since we don't flush the TLB when tearing down a VM,
  2242. * and this lpid might have previously been used,
  2243. * make sure we flush on each core before running the new VM.
  2244. */
  2245. cpumask_setall(&kvm->arch.need_tlb_flush);
  2246. /* Start out with the default set of hcalls enabled */
  2247. memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
  2248. sizeof(kvm->arch.enabled_hcalls));
  2249. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  2250. /* Init LPCR for virtual RMA mode */
  2251. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  2252. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  2253. lpcr &= LPCR_PECE | LPCR_LPES;
  2254. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  2255. LPCR_VPM0 | LPCR_VPM1;
  2256. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  2257. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  2258. /* On POWER8 turn on online bit to enable PURR/SPURR */
  2259. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  2260. lpcr |= LPCR_ONL;
  2261. kvm->arch.lpcr = lpcr;
  2262. /*
  2263. * Track that we now have a HV mode VM active. This blocks secondary
  2264. * CPU threads from coming online.
  2265. */
  2266. kvm_hv_vm_activated();
  2267. /*
  2268. * Create a debugfs directory for the VM
  2269. */
  2270. snprintf(buf, sizeof(buf), "vm%d", current->pid);
  2271. kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
  2272. if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  2273. kvmppc_mmu_debugfs_init(kvm);
  2274. return 0;
  2275. }
  2276. static void kvmppc_free_vcores(struct kvm *kvm)
  2277. {
  2278. long int i;
  2279. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  2280. if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
  2281. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  2282. free_pages((unsigned long)vc->mpp_buffer,
  2283. MPP_BUFFER_ORDER);
  2284. }
  2285. kfree(kvm->arch.vcores[i]);
  2286. }
  2287. kvm->arch.online_vcores = 0;
  2288. }
  2289. static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
  2290. {
  2291. debugfs_remove_recursive(kvm->arch.debugfs_dir);
  2292. kvm_hv_vm_deactivated();
  2293. kvmppc_free_vcores(kvm);
  2294. kvmppc_free_hpt(kvm);
  2295. }
  2296. /* We don't need to emulate any privileged instructions or dcbz */
  2297. static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  2298. unsigned int inst, int *advance)
  2299. {
  2300. return EMULATE_FAIL;
  2301. }
  2302. static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
  2303. ulong spr_val)
  2304. {
  2305. return EMULATE_FAIL;
  2306. }
  2307. static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
  2308. ulong *spr_val)
  2309. {
  2310. return EMULATE_FAIL;
  2311. }
  2312. static int kvmppc_core_check_processor_compat_hv(void)
  2313. {
  2314. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  2315. !cpu_has_feature(CPU_FTR_ARCH_206))
  2316. return -EIO;
  2317. return 0;
  2318. }
  2319. static long kvm_arch_vm_ioctl_hv(struct file *filp,
  2320. unsigned int ioctl, unsigned long arg)
  2321. {
  2322. struct kvm *kvm __maybe_unused = filp->private_data;
  2323. void __user *argp = (void __user *)arg;
  2324. long r;
  2325. switch (ioctl) {
  2326. case KVM_PPC_ALLOCATE_HTAB: {
  2327. u32 htab_order;
  2328. r = -EFAULT;
  2329. if (get_user(htab_order, (u32 __user *)argp))
  2330. break;
  2331. r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
  2332. if (r)
  2333. break;
  2334. r = -EFAULT;
  2335. if (put_user(htab_order, (u32 __user *)argp))
  2336. break;
  2337. r = 0;
  2338. break;
  2339. }
  2340. case KVM_PPC_GET_HTAB_FD: {
  2341. struct kvm_get_htab_fd ghf;
  2342. r = -EFAULT;
  2343. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  2344. break;
  2345. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  2346. break;
  2347. }
  2348. default:
  2349. r = -ENOTTY;
  2350. }
  2351. return r;
  2352. }
  2353. /*
  2354. * List of hcall numbers to enable by default.
  2355. * For compatibility with old userspace, we enable by default
  2356. * all hcalls that were implemented before the hcall-enabling
  2357. * facility was added. Note this list should not include H_RTAS.
  2358. */
  2359. static unsigned int default_hcall_list[] = {
  2360. H_REMOVE,
  2361. H_ENTER,
  2362. H_READ,
  2363. H_PROTECT,
  2364. H_BULK_REMOVE,
  2365. H_GET_TCE,
  2366. H_PUT_TCE,
  2367. H_SET_DABR,
  2368. H_SET_XDABR,
  2369. H_CEDE,
  2370. H_PROD,
  2371. H_CONFER,
  2372. H_REGISTER_VPA,
  2373. #ifdef CONFIG_KVM_XICS
  2374. H_EOI,
  2375. H_CPPR,
  2376. H_IPI,
  2377. H_IPOLL,
  2378. H_XIRR,
  2379. H_XIRR_X,
  2380. #endif
  2381. 0
  2382. };
  2383. static void init_default_hcalls(void)
  2384. {
  2385. int i;
  2386. unsigned int hcall;
  2387. for (i = 0; default_hcall_list[i]; ++i) {
  2388. hcall = default_hcall_list[i];
  2389. WARN_ON(!kvmppc_hcall_impl_hv(hcall));
  2390. __set_bit(hcall / 4, default_enabled_hcalls);
  2391. }
  2392. }
  2393. static struct kvmppc_ops kvm_ops_hv = {
  2394. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
  2395. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
  2396. .get_one_reg = kvmppc_get_one_reg_hv,
  2397. .set_one_reg = kvmppc_set_one_reg_hv,
  2398. .vcpu_load = kvmppc_core_vcpu_load_hv,
  2399. .vcpu_put = kvmppc_core_vcpu_put_hv,
  2400. .set_msr = kvmppc_set_msr_hv,
  2401. .vcpu_run = kvmppc_vcpu_run_hv,
  2402. .vcpu_create = kvmppc_core_vcpu_create_hv,
  2403. .vcpu_free = kvmppc_core_vcpu_free_hv,
  2404. .check_requests = kvmppc_core_check_requests_hv,
  2405. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
  2406. .flush_memslot = kvmppc_core_flush_memslot_hv,
  2407. .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
  2408. .commit_memory_region = kvmppc_core_commit_memory_region_hv,
  2409. .unmap_hva = kvm_unmap_hva_hv,
  2410. .unmap_hva_range = kvm_unmap_hva_range_hv,
  2411. .age_hva = kvm_age_hva_hv,
  2412. .test_age_hva = kvm_test_age_hva_hv,
  2413. .set_spte_hva = kvm_set_spte_hva_hv,
  2414. .mmu_destroy = kvmppc_mmu_destroy_hv,
  2415. .free_memslot = kvmppc_core_free_memslot_hv,
  2416. .create_memslot = kvmppc_core_create_memslot_hv,
  2417. .init_vm = kvmppc_core_init_vm_hv,
  2418. .destroy_vm = kvmppc_core_destroy_vm_hv,
  2419. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
  2420. .emulate_op = kvmppc_core_emulate_op_hv,
  2421. .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
  2422. .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
  2423. .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
  2424. .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
  2425. .hcall_implemented = kvmppc_hcall_impl_hv,
  2426. };
  2427. static int kvmppc_book3s_init_hv(void)
  2428. {
  2429. int r;
  2430. /*
  2431. * FIXME!! Do we need to check on all cpus ?
  2432. */
  2433. r = kvmppc_core_check_processor_compat_hv();
  2434. if (r < 0)
  2435. return -ENODEV;
  2436. kvm_ops_hv.owner = THIS_MODULE;
  2437. kvmppc_hv_ops = &kvm_ops_hv;
  2438. init_default_hcalls();
  2439. r = kvmppc_mmu_hv_init();
  2440. return r;
  2441. }
  2442. static void kvmppc_book3s_exit_hv(void)
  2443. {
  2444. kvmppc_hv_ops = NULL;
  2445. }
  2446. module_init(kvmppc_book3s_init_hv);
  2447. module_exit(kvmppc_book3s_exit_hv);
  2448. MODULE_LICENSE("GPL");
  2449. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  2450. MODULE_ALIAS("devname:kvm");