book3s_hv.c 82 KB

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