book3s_hv.c 82 KB

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