book3s_hv.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359
  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/signal.h>
  25. #include <linux/sched/stat.h>
  26. #include <linux/delay.h>
  27. #include <linux/export.h>
  28. #include <linux/fs.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/cpu.h>
  31. #include <linux/cpumask.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/page-flags.h>
  34. #include <linux/srcu.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/debugfs.h>
  37. #include <linux/gfp.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/highmem.h>
  40. #include <linux/hugetlb.h>
  41. #include <linux/kvm_irqfd.h>
  42. #include <linux/irqbypass.h>
  43. #include <linux/module.h>
  44. #include <linux/compiler.h>
  45. #include <linux/of.h>
  46. #include <asm/reg.h>
  47. #include <asm/ppc-opcode.h>
  48. #include <asm/asm-prototypes.h>
  49. #include <asm/disassemble.h>
  50. #include <asm/cputable.h>
  51. #include <asm/cacheflush.h>
  52. #include <asm/tlbflush.h>
  53. #include <linux/uaccess.h>
  54. #include <asm/io.h>
  55. #include <asm/kvm_ppc.h>
  56. #include <asm/kvm_book3s.h>
  57. #include <asm/mmu_context.h>
  58. #include <asm/lppaca.h>
  59. #include <asm/processor.h>
  60. #include <asm/cputhreads.h>
  61. #include <asm/page.h>
  62. #include <asm/hvcall.h>
  63. #include <asm/switch_to.h>
  64. #include <asm/smp.h>
  65. #include <asm/dbell.h>
  66. #include <asm/hmi.h>
  67. #include <asm/pnv-pci.h>
  68. #include <asm/mmu.h>
  69. #include <asm/opal.h>
  70. #include <asm/xics.h>
  71. #include <asm/xive.h>
  72. #include "book3s.h"
  73. #define CREATE_TRACE_POINTS
  74. #include "trace_hv.h"
  75. /* #define EXIT_DEBUG */
  76. /* #define EXIT_DEBUG_SIMPLE */
  77. /* #define EXIT_DEBUG_INT */
  78. /* Used to indicate that a guest page fault needs to be handled */
  79. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  80. /* Used to indicate that a guest passthrough interrupt needs to be handled */
  81. #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
  82. /* Used as a "null" value for timebase values */
  83. #define TB_NIL (~(u64)0)
  84. static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
  85. static int dynamic_mt_modes = 6;
  86. module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
  87. MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
  88. static int target_smt_mode;
  89. module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
  90. MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
  91. #ifdef CONFIG_KVM_XICS
  92. static struct kernel_param_ops module_param_ops = {
  93. .set = param_set_int,
  94. .get = param_get_int,
  95. };
  96. module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass,
  97. S_IRUGO | S_IWUSR);
  98. MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
  99. module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
  100. S_IRUGO | S_IWUSR);
  101. MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
  102. #endif
  103. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  104. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  105. static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
  106. int *ip)
  107. {
  108. int i = *ip;
  109. struct kvm_vcpu *vcpu;
  110. while (++i < MAX_SMT_THREADS) {
  111. vcpu = READ_ONCE(vc->runnable_threads[i]);
  112. if (vcpu) {
  113. *ip = i;
  114. return vcpu;
  115. }
  116. }
  117. return NULL;
  118. }
  119. /* Used to traverse the list of runnable threads for a given vcore */
  120. #define for_each_runnable_thread(i, vcpu, vc) \
  121. for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
  122. static bool kvmppc_ipi_thread(int cpu)
  123. {
  124. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  125. /* On POWER9 we can use msgsnd to IPI any cpu */
  126. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  127. msg |= get_hard_smp_processor_id(cpu);
  128. smp_mb();
  129. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  130. return true;
  131. }
  132. /* On POWER8 for IPIs to threads in the same core, use msgsnd */
  133. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  134. preempt_disable();
  135. if (cpu_first_thread_sibling(cpu) ==
  136. cpu_first_thread_sibling(smp_processor_id())) {
  137. msg |= cpu_thread_in_core(cpu);
  138. smp_mb();
  139. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  140. preempt_enable();
  141. return true;
  142. }
  143. preempt_enable();
  144. }
  145. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  146. if (cpu >= 0 && cpu < nr_cpu_ids) {
  147. if (paca[cpu].kvm_hstate.xics_phys) {
  148. xics_wake_cpu(cpu);
  149. return true;
  150. }
  151. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
  152. return true;
  153. }
  154. #endif
  155. return false;
  156. }
  157. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  158. {
  159. int cpu;
  160. struct swait_queue_head *wqp;
  161. wqp = kvm_arch_vcpu_wq(vcpu);
  162. if (swq_has_sleeper(wqp)) {
  163. swake_up(wqp);
  164. ++vcpu->stat.halt_wakeup;
  165. }
  166. cpu = READ_ONCE(vcpu->arch.thread_cpu);
  167. if (cpu >= 0 && kvmppc_ipi_thread(cpu))
  168. return;
  169. /* CPU points to the first thread of the core */
  170. cpu = vcpu->cpu;
  171. if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
  172. smp_send_reschedule(cpu);
  173. }
  174. /*
  175. * We use the vcpu_load/put functions to measure stolen time.
  176. * Stolen time is counted as time when either the vcpu is able to
  177. * run as part of a virtual core, but the task running the vcore
  178. * is preempted or sleeping, or when the vcpu needs something done
  179. * in the kernel by the task running the vcpu, but that task is
  180. * preempted or sleeping. Those two things have to be counted
  181. * separately, since one of the vcpu tasks will take on the job
  182. * of running the core, and the other vcpu tasks in the vcore will
  183. * sleep waiting for it to do that, but that sleep shouldn't count
  184. * as stolen time.
  185. *
  186. * Hence we accumulate stolen time when the vcpu can run as part of
  187. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  188. * needs its task to do other things in the kernel (for example,
  189. * service a page fault) in busy_stolen. We don't accumulate
  190. * stolen time for a vcore when it is inactive, or for a vcpu
  191. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  192. * a misnomer; it means that the vcpu task is not executing in
  193. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  194. * the kernel. We don't have any way of dividing up that time
  195. * between time that the vcpu is genuinely stopped, time that
  196. * the task is actively working on behalf of the vcpu, and time
  197. * that the task is preempted, so we don't count any of it as
  198. * stolen.
  199. *
  200. * Updates to busy_stolen are protected by arch.tbacct_lock;
  201. * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
  202. * lock. The stolen times are measured in units of timebase ticks.
  203. * (Note that the != TB_NIL checks below are purely defensive;
  204. * they should never fail.)
  205. */
  206. static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
  207. {
  208. unsigned long flags;
  209. spin_lock_irqsave(&vc->stoltb_lock, flags);
  210. vc->preempt_tb = mftb();
  211. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  212. }
  213. static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
  214. {
  215. unsigned long flags;
  216. spin_lock_irqsave(&vc->stoltb_lock, flags);
  217. if (vc->preempt_tb != TB_NIL) {
  218. vc->stolen_tb += mftb() - vc->preempt_tb;
  219. vc->preempt_tb = TB_NIL;
  220. }
  221. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  222. }
  223. static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
  224. {
  225. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  226. unsigned long flags;
  227. /*
  228. * We can test vc->runner without taking the vcore lock,
  229. * because only this task ever sets vc->runner to this
  230. * vcpu, and once it is set to this vcpu, only this task
  231. * ever sets it to NULL.
  232. */
  233. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  234. kvmppc_core_end_stolen(vc);
  235. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  236. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  237. vcpu->arch.busy_preempt != TB_NIL) {
  238. vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
  239. vcpu->arch.busy_preempt = TB_NIL;
  240. }
  241. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  242. }
  243. static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
  244. {
  245. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  246. unsigned long flags;
  247. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  248. kvmppc_core_start_stolen(vc);
  249. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  250. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  251. vcpu->arch.busy_preempt = mftb();
  252. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  253. }
  254. static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
  255. {
  256. /*
  257. * Check for illegal transactional state bit combination
  258. * and if we find it, force the TS field to a safe state.
  259. */
  260. if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
  261. msr &= ~MSR_TS_MASK;
  262. vcpu->arch.shregs.msr = msr;
  263. kvmppc_end_cede(vcpu);
  264. }
  265. static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
  266. {
  267. vcpu->arch.pvr = pvr;
  268. }
  269. /* Dummy value used in computing PCR value below */
  270. #define PCR_ARCH_300 (PCR_ARCH_207 << 1)
  271. static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
  272. {
  273. unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
  274. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  275. /* We can (emulate) our own architecture version and anything older */
  276. if (cpu_has_feature(CPU_FTR_ARCH_300))
  277. host_pcr_bit = PCR_ARCH_300;
  278. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  279. host_pcr_bit = PCR_ARCH_207;
  280. else if (cpu_has_feature(CPU_FTR_ARCH_206))
  281. host_pcr_bit = PCR_ARCH_206;
  282. else
  283. host_pcr_bit = PCR_ARCH_205;
  284. /* Determine lowest PCR bit needed to run guest in given PVR level */
  285. guest_pcr_bit = host_pcr_bit;
  286. if (arch_compat) {
  287. switch (arch_compat) {
  288. case PVR_ARCH_205:
  289. guest_pcr_bit = PCR_ARCH_205;
  290. break;
  291. case PVR_ARCH_206:
  292. case PVR_ARCH_206p:
  293. guest_pcr_bit = PCR_ARCH_206;
  294. break;
  295. case PVR_ARCH_207:
  296. guest_pcr_bit = PCR_ARCH_207;
  297. break;
  298. case PVR_ARCH_300:
  299. guest_pcr_bit = PCR_ARCH_300;
  300. break;
  301. default:
  302. return -EINVAL;
  303. }
  304. }
  305. /* Check requested PCR bits don't exceed our capabilities */
  306. if (guest_pcr_bit > host_pcr_bit)
  307. return -EINVAL;
  308. spin_lock(&vc->lock);
  309. vc->arch_compat = arch_compat;
  310. /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */
  311. vc->pcr = host_pcr_bit - guest_pcr_bit;
  312. spin_unlock(&vc->lock);
  313. return 0;
  314. }
  315. static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  316. {
  317. int r;
  318. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  319. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  320. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  321. for (r = 0; r < 16; ++r)
  322. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  323. r, kvmppc_get_gpr(vcpu, r),
  324. r+16, kvmppc_get_gpr(vcpu, r+16));
  325. pr_err("ctr = %.16lx lr = %.16lx\n",
  326. vcpu->arch.ctr, vcpu->arch.lr);
  327. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  328. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  329. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  330. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  331. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  332. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  333. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  334. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  335. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  336. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  337. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  338. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  339. for (r = 0; r < vcpu->arch.slb_max; ++r)
  340. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  341. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  342. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  343. vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
  344. vcpu->arch.last_inst);
  345. }
  346. static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  347. {
  348. struct kvm_vcpu *ret;
  349. mutex_lock(&kvm->lock);
  350. ret = kvm_get_vcpu_by_id(kvm, id);
  351. mutex_unlock(&kvm->lock);
  352. return ret;
  353. }
  354. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  355. {
  356. vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
  357. vpa->yield_count = cpu_to_be32(1);
  358. }
  359. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  360. unsigned long addr, unsigned long len)
  361. {
  362. /* check address is cacheline aligned */
  363. if (addr & (L1_CACHE_BYTES - 1))
  364. return -EINVAL;
  365. spin_lock(&vcpu->arch.vpa_update_lock);
  366. if (v->next_gpa != addr || v->len != len) {
  367. v->next_gpa = addr;
  368. v->len = addr ? len : 0;
  369. v->update_pending = 1;
  370. }
  371. spin_unlock(&vcpu->arch.vpa_update_lock);
  372. return 0;
  373. }
  374. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  375. struct reg_vpa {
  376. u32 dummy;
  377. union {
  378. __be16 hword;
  379. __be32 word;
  380. } length;
  381. };
  382. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  383. {
  384. if (vpap->update_pending)
  385. return vpap->next_gpa != 0;
  386. return vpap->pinned_addr != NULL;
  387. }
  388. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  389. unsigned long flags,
  390. unsigned long vcpuid, unsigned long vpa)
  391. {
  392. struct kvm *kvm = vcpu->kvm;
  393. unsigned long len, nb;
  394. void *va;
  395. struct kvm_vcpu *tvcpu;
  396. int err;
  397. int subfunc;
  398. struct kvmppc_vpa *vpap;
  399. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  400. if (!tvcpu)
  401. return H_PARAMETER;
  402. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  403. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  404. subfunc == H_VPA_REG_SLB) {
  405. /* Registering new area - address must be cache-line aligned */
  406. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  407. return H_PARAMETER;
  408. /* convert logical addr to kernel addr and read length */
  409. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  410. if (va == NULL)
  411. return H_PARAMETER;
  412. if (subfunc == H_VPA_REG_VPA)
  413. len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
  414. else
  415. len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
  416. kvmppc_unpin_guest_page(kvm, va, vpa, false);
  417. /* Check length */
  418. if (len > nb || len < sizeof(struct reg_vpa))
  419. return H_PARAMETER;
  420. } else {
  421. vpa = 0;
  422. len = 0;
  423. }
  424. err = H_PARAMETER;
  425. vpap = NULL;
  426. spin_lock(&tvcpu->arch.vpa_update_lock);
  427. switch (subfunc) {
  428. case H_VPA_REG_VPA: /* register VPA */
  429. /*
  430. * The size of our lppaca is 1kB because of the way we align
  431. * it for the guest to avoid crossing a 4kB boundary. We only
  432. * use 640 bytes of the structure though, so we should accept
  433. * clients that set a size of 640.
  434. */
  435. if (len < 640)
  436. break;
  437. vpap = &tvcpu->arch.vpa;
  438. err = 0;
  439. break;
  440. case H_VPA_REG_DTL: /* register DTL */
  441. if (len < sizeof(struct dtl_entry))
  442. break;
  443. len -= len % sizeof(struct dtl_entry);
  444. /* Check that they have previously registered a VPA */
  445. err = H_RESOURCE;
  446. if (!vpa_is_registered(&tvcpu->arch.vpa))
  447. break;
  448. vpap = &tvcpu->arch.dtl;
  449. err = 0;
  450. break;
  451. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  452. /* Check that they have previously registered a VPA */
  453. err = H_RESOURCE;
  454. if (!vpa_is_registered(&tvcpu->arch.vpa))
  455. break;
  456. vpap = &tvcpu->arch.slb_shadow;
  457. err = 0;
  458. break;
  459. case H_VPA_DEREG_VPA: /* deregister VPA */
  460. /* Check they don't still have a DTL or SLB buf registered */
  461. err = H_RESOURCE;
  462. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  463. vpa_is_registered(&tvcpu->arch.slb_shadow))
  464. break;
  465. vpap = &tvcpu->arch.vpa;
  466. err = 0;
  467. break;
  468. case H_VPA_DEREG_DTL: /* deregister DTL */
  469. vpap = &tvcpu->arch.dtl;
  470. err = 0;
  471. break;
  472. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  473. vpap = &tvcpu->arch.slb_shadow;
  474. err = 0;
  475. break;
  476. }
  477. if (vpap) {
  478. vpap->next_gpa = vpa;
  479. vpap->len = len;
  480. vpap->update_pending = 1;
  481. }
  482. spin_unlock(&tvcpu->arch.vpa_update_lock);
  483. return err;
  484. }
  485. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  486. {
  487. struct kvm *kvm = vcpu->kvm;
  488. void *va;
  489. unsigned long nb;
  490. unsigned long gpa;
  491. /*
  492. * We need to pin the page pointed to by vpap->next_gpa,
  493. * but we can't call kvmppc_pin_guest_page under the lock
  494. * as it does get_user_pages() and down_read(). So we
  495. * have to drop the lock, pin the page, then get the lock
  496. * again and check that a new area didn't get registered
  497. * in the meantime.
  498. */
  499. for (;;) {
  500. gpa = vpap->next_gpa;
  501. spin_unlock(&vcpu->arch.vpa_update_lock);
  502. va = NULL;
  503. nb = 0;
  504. if (gpa)
  505. va = kvmppc_pin_guest_page(kvm, gpa, &nb);
  506. spin_lock(&vcpu->arch.vpa_update_lock);
  507. if (gpa == vpap->next_gpa)
  508. break;
  509. /* sigh... unpin that one and try again */
  510. if (va)
  511. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  512. }
  513. vpap->update_pending = 0;
  514. if (va && nb < vpap->len) {
  515. /*
  516. * If it's now too short, it must be that userspace
  517. * has changed the mappings underlying guest memory,
  518. * so unregister the region.
  519. */
  520. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  521. va = NULL;
  522. }
  523. if (vpap->pinned_addr)
  524. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
  525. vpap->dirty);
  526. vpap->gpa = gpa;
  527. vpap->pinned_addr = va;
  528. vpap->dirty = false;
  529. if (va)
  530. vpap->pinned_end = va + vpap->len;
  531. }
  532. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  533. {
  534. if (!(vcpu->arch.vpa.update_pending ||
  535. vcpu->arch.slb_shadow.update_pending ||
  536. vcpu->arch.dtl.update_pending))
  537. return;
  538. spin_lock(&vcpu->arch.vpa_update_lock);
  539. if (vcpu->arch.vpa.update_pending) {
  540. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  541. if (vcpu->arch.vpa.pinned_addr)
  542. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  543. }
  544. if (vcpu->arch.dtl.update_pending) {
  545. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  546. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  547. vcpu->arch.dtl_index = 0;
  548. }
  549. if (vcpu->arch.slb_shadow.update_pending)
  550. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  551. spin_unlock(&vcpu->arch.vpa_update_lock);
  552. }
  553. /*
  554. * Return the accumulated stolen time for the vcore up until `now'.
  555. * The caller should hold the vcore lock.
  556. */
  557. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  558. {
  559. u64 p;
  560. unsigned long flags;
  561. spin_lock_irqsave(&vc->stoltb_lock, flags);
  562. p = vc->stolen_tb;
  563. if (vc->vcore_state != VCORE_INACTIVE &&
  564. vc->preempt_tb != TB_NIL)
  565. p += now - vc->preempt_tb;
  566. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  567. return p;
  568. }
  569. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  570. struct kvmppc_vcore *vc)
  571. {
  572. struct dtl_entry *dt;
  573. struct lppaca *vpa;
  574. unsigned long stolen;
  575. unsigned long core_stolen;
  576. u64 now;
  577. unsigned long flags;
  578. dt = vcpu->arch.dtl_ptr;
  579. vpa = vcpu->arch.vpa.pinned_addr;
  580. now = mftb();
  581. core_stolen = vcore_stolen_time(vc, now);
  582. stolen = core_stolen - vcpu->arch.stolen_logged;
  583. vcpu->arch.stolen_logged = core_stolen;
  584. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  585. stolen += vcpu->arch.busy_stolen;
  586. vcpu->arch.busy_stolen = 0;
  587. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  588. if (!dt || !vpa)
  589. return;
  590. memset(dt, 0, sizeof(struct dtl_entry));
  591. dt->dispatch_reason = 7;
  592. dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
  593. dt->timebase = cpu_to_be64(now + vc->tb_offset);
  594. dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
  595. dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
  596. dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
  597. ++dt;
  598. if (dt == vcpu->arch.dtl.pinned_end)
  599. dt = vcpu->arch.dtl.pinned_addr;
  600. vcpu->arch.dtl_ptr = dt;
  601. /* order writing *dt vs. writing vpa->dtl_idx */
  602. smp_wmb();
  603. vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
  604. vcpu->arch.dtl.dirty = true;
  605. }
  606. /* See if there is a doorbell interrupt pending for a vcpu */
  607. static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
  608. {
  609. int thr;
  610. struct kvmppc_vcore *vc;
  611. if (vcpu->arch.doorbell_request)
  612. return true;
  613. /*
  614. * Ensure that the read of vcore->dpdes comes after the read
  615. * of vcpu->doorbell_request. This barrier matches the
  616. * lwsync in book3s_hv_rmhandlers.S just before the
  617. * fast_guest_return label.
  618. */
  619. smp_rmb();
  620. vc = vcpu->arch.vcore;
  621. thr = vcpu->vcpu_id - vc->first_vcpuid;
  622. return !!(vc->dpdes & (1 << thr));
  623. }
  624. static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
  625. {
  626. if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
  627. return true;
  628. if ((!vcpu->arch.vcore->arch_compat) &&
  629. cpu_has_feature(CPU_FTR_ARCH_207S))
  630. return true;
  631. return false;
  632. }
  633. static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
  634. unsigned long resource, unsigned long value1,
  635. unsigned long value2)
  636. {
  637. switch (resource) {
  638. case H_SET_MODE_RESOURCE_SET_CIABR:
  639. if (!kvmppc_power8_compatible(vcpu))
  640. return H_P2;
  641. if (value2)
  642. return H_P4;
  643. if (mflags)
  644. return H_UNSUPPORTED_FLAG_START;
  645. /* Guests can't breakpoint the hypervisor */
  646. if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
  647. return H_P3;
  648. vcpu->arch.ciabr = value1;
  649. return H_SUCCESS;
  650. case H_SET_MODE_RESOURCE_SET_DAWR:
  651. if (!kvmppc_power8_compatible(vcpu))
  652. return H_P2;
  653. if (mflags)
  654. return H_UNSUPPORTED_FLAG_START;
  655. if (value2 & DABRX_HYP)
  656. return H_P4;
  657. vcpu->arch.dawr = value1;
  658. vcpu->arch.dawrx = value2;
  659. return H_SUCCESS;
  660. default:
  661. return H_TOO_HARD;
  662. }
  663. }
  664. static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
  665. {
  666. struct kvmppc_vcore *vcore = target->arch.vcore;
  667. /*
  668. * We expect to have been called by the real mode handler
  669. * (kvmppc_rm_h_confer()) which would have directly returned
  670. * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
  671. * have useful work to do and should not confer) so we don't
  672. * recheck that here.
  673. */
  674. spin_lock(&vcore->lock);
  675. if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
  676. vcore->vcore_state != VCORE_INACTIVE &&
  677. vcore->runner)
  678. target = vcore->runner;
  679. spin_unlock(&vcore->lock);
  680. return kvm_vcpu_yield_to(target);
  681. }
  682. static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
  683. {
  684. int yield_count = 0;
  685. struct lppaca *lppaca;
  686. spin_lock(&vcpu->arch.vpa_update_lock);
  687. lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
  688. if (lppaca)
  689. yield_count = be32_to_cpu(lppaca->yield_count);
  690. spin_unlock(&vcpu->arch.vpa_update_lock);
  691. return yield_count;
  692. }
  693. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  694. {
  695. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  696. unsigned long target, ret = H_SUCCESS;
  697. int yield_count;
  698. struct kvm_vcpu *tvcpu;
  699. int idx, rc;
  700. if (req <= MAX_HCALL_OPCODE &&
  701. !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
  702. return RESUME_HOST;
  703. switch (req) {
  704. case H_CEDE:
  705. break;
  706. case H_PROD:
  707. target = kvmppc_get_gpr(vcpu, 4);
  708. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  709. if (!tvcpu) {
  710. ret = H_PARAMETER;
  711. break;
  712. }
  713. tvcpu->arch.prodded = 1;
  714. smp_mb();
  715. if (tvcpu->arch.ceded)
  716. kvmppc_fast_vcpu_kick_hv(tvcpu);
  717. break;
  718. case H_CONFER:
  719. target = kvmppc_get_gpr(vcpu, 4);
  720. if (target == -1)
  721. break;
  722. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  723. if (!tvcpu) {
  724. ret = H_PARAMETER;
  725. break;
  726. }
  727. yield_count = kvmppc_get_gpr(vcpu, 5);
  728. if (kvmppc_get_yield_count(tvcpu) != yield_count)
  729. break;
  730. kvm_arch_vcpu_yield_to(tvcpu);
  731. break;
  732. case H_REGISTER_VPA:
  733. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  734. kvmppc_get_gpr(vcpu, 5),
  735. kvmppc_get_gpr(vcpu, 6));
  736. break;
  737. case H_RTAS:
  738. if (list_empty(&vcpu->kvm->arch.rtas_tokens))
  739. return RESUME_HOST;
  740. idx = srcu_read_lock(&vcpu->kvm->srcu);
  741. rc = kvmppc_rtas_hcall(vcpu);
  742. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  743. if (rc == -ENOENT)
  744. return RESUME_HOST;
  745. else if (rc == 0)
  746. break;
  747. /* Send the error out to userspace via KVM_RUN */
  748. return rc;
  749. case H_LOGICAL_CI_LOAD:
  750. ret = kvmppc_h_logical_ci_load(vcpu);
  751. if (ret == H_TOO_HARD)
  752. return RESUME_HOST;
  753. break;
  754. case H_LOGICAL_CI_STORE:
  755. ret = kvmppc_h_logical_ci_store(vcpu);
  756. if (ret == H_TOO_HARD)
  757. return RESUME_HOST;
  758. break;
  759. case H_SET_MODE:
  760. ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
  761. kvmppc_get_gpr(vcpu, 5),
  762. kvmppc_get_gpr(vcpu, 6),
  763. kvmppc_get_gpr(vcpu, 7));
  764. if (ret == H_TOO_HARD)
  765. return RESUME_HOST;
  766. break;
  767. case H_XIRR:
  768. case H_CPPR:
  769. case H_EOI:
  770. case H_IPI:
  771. case H_IPOLL:
  772. case H_XIRR_X:
  773. if (kvmppc_xics_enabled(vcpu)) {
  774. if (xive_enabled()) {
  775. ret = H_NOT_AVAILABLE;
  776. return RESUME_GUEST;
  777. }
  778. ret = kvmppc_xics_hcall(vcpu, req);
  779. break;
  780. }
  781. return RESUME_HOST;
  782. case H_PUT_TCE:
  783. ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  784. kvmppc_get_gpr(vcpu, 5),
  785. kvmppc_get_gpr(vcpu, 6));
  786. if (ret == H_TOO_HARD)
  787. return RESUME_HOST;
  788. break;
  789. case H_PUT_TCE_INDIRECT:
  790. ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
  791. kvmppc_get_gpr(vcpu, 5),
  792. kvmppc_get_gpr(vcpu, 6),
  793. kvmppc_get_gpr(vcpu, 7));
  794. if (ret == H_TOO_HARD)
  795. return RESUME_HOST;
  796. break;
  797. case H_STUFF_TCE:
  798. ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  799. kvmppc_get_gpr(vcpu, 5),
  800. kvmppc_get_gpr(vcpu, 6),
  801. kvmppc_get_gpr(vcpu, 7));
  802. if (ret == H_TOO_HARD)
  803. return RESUME_HOST;
  804. break;
  805. default:
  806. return RESUME_HOST;
  807. }
  808. kvmppc_set_gpr(vcpu, 3, ret);
  809. vcpu->arch.hcall_needed = 0;
  810. return RESUME_GUEST;
  811. }
  812. static int kvmppc_hcall_impl_hv(unsigned long cmd)
  813. {
  814. switch (cmd) {
  815. case H_CEDE:
  816. case H_PROD:
  817. case H_CONFER:
  818. case H_REGISTER_VPA:
  819. case H_SET_MODE:
  820. case H_LOGICAL_CI_LOAD:
  821. case H_LOGICAL_CI_STORE:
  822. #ifdef CONFIG_KVM_XICS
  823. case H_XIRR:
  824. case H_CPPR:
  825. case H_EOI:
  826. case H_IPI:
  827. case H_IPOLL:
  828. case H_XIRR_X:
  829. #endif
  830. return 1;
  831. }
  832. /* See if it's in the real-mode table */
  833. return kvmppc_hcall_impl_hv_realmode(cmd);
  834. }
  835. static int kvmppc_emulate_debug_inst(struct kvm_run *run,
  836. struct kvm_vcpu *vcpu)
  837. {
  838. u32 last_inst;
  839. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  840. EMULATE_DONE) {
  841. /*
  842. * Fetch failed, so return to guest and
  843. * try executing it again.
  844. */
  845. return RESUME_GUEST;
  846. }
  847. if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
  848. run->exit_reason = KVM_EXIT_DEBUG;
  849. run->debug.arch.address = kvmppc_get_pc(vcpu);
  850. return RESUME_HOST;
  851. } else {
  852. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  853. return RESUME_GUEST;
  854. }
  855. }
  856. static void do_nothing(void *x)
  857. {
  858. }
  859. static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
  860. {
  861. int thr, cpu, pcpu, nthreads;
  862. struct kvm_vcpu *v;
  863. unsigned long dpdes;
  864. nthreads = vcpu->kvm->arch.emul_smt_mode;
  865. dpdes = 0;
  866. cpu = vcpu->vcpu_id & ~(nthreads - 1);
  867. for (thr = 0; thr < nthreads; ++thr, ++cpu) {
  868. v = kvmppc_find_vcpu(vcpu->kvm, cpu);
  869. if (!v)
  870. continue;
  871. /*
  872. * If the vcpu is currently running on a physical cpu thread,
  873. * interrupt it in order to pull it out of the guest briefly,
  874. * which will update its vcore->dpdes value.
  875. */
  876. pcpu = READ_ONCE(v->cpu);
  877. if (pcpu >= 0)
  878. smp_call_function_single(pcpu, do_nothing, NULL, 1);
  879. if (kvmppc_doorbell_pending(v))
  880. dpdes |= 1 << thr;
  881. }
  882. return dpdes;
  883. }
  884. /*
  885. * On POWER9, emulate doorbell-related instructions in order to
  886. * give the guest the illusion of running on a multi-threaded core.
  887. * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
  888. * and mfspr DPDES.
  889. */
  890. static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
  891. {
  892. u32 inst, rb, thr;
  893. unsigned long arg;
  894. struct kvm *kvm = vcpu->kvm;
  895. struct kvm_vcpu *tvcpu;
  896. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  897. return EMULATE_FAIL;
  898. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE)
  899. return RESUME_GUEST;
  900. if (get_op(inst) != 31)
  901. return EMULATE_FAIL;
  902. rb = get_rb(inst);
  903. thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
  904. switch (get_xop(inst)) {
  905. case OP_31_XOP_MSGSNDP:
  906. arg = kvmppc_get_gpr(vcpu, rb);
  907. if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER)
  908. break;
  909. arg &= 0x3f;
  910. if (arg >= kvm->arch.emul_smt_mode)
  911. break;
  912. tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
  913. if (!tvcpu)
  914. break;
  915. if (!tvcpu->arch.doorbell_request) {
  916. tvcpu->arch.doorbell_request = 1;
  917. kvmppc_fast_vcpu_kick_hv(tvcpu);
  918. }
  919. break;
  920. case OP_31_XOP_MSGCLRP:
  921. arg = kvmppc_get_gpr(vcpu, rb);
  922. if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER)
  923. break;
  924. vcpu->arch.vcore->dpdes = 0;
  925. vcpu->arch.doorbell_request = 0;
  926. break;
  927. case OP_31_XOP_MFSPR:
  928. switch (get_sprn(inst)) {
  929. case SPRN_TIR:
  930. arg = thr;
  931. break;
  932. case SPRN_DPDES:
  933. arg = kvmppc_read_dpdes(vcpu);
  934. break;
  935. default:
  936. return EMULATE_FAIL;
  937. }
  938. kvmppc_set_gpr(vcpu, get_rt(inst), arg);
  939. break;
  940. default:
  941. return EMULATE_FAIL;
  942. }
  943. kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
  944. return RESUME_GUEST;
  945. }
  946. static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  947. struct task_struct *tsk)
  948. {
  949. int r = RESUME_HOST;
  950. vcpu->stat.sum_exits++;
  951. /*
  952. * This can happen if an interrupt occurs in the last stages
  953. * of guest entry or the first stages of guest exit (i.e. after
  954. * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
  955. * and before setting it to KVM_GUEST_MODE_HOST_HV).
  956. * That can happen due to a bug, or due to a machine check
  957. * occurring at just the wrong time.
  958. */
  959. if (vcpu->arch.shregs.msr & MSR_HV) {
  960. printk(KERN_EMERG "KVM trap in HV mode!\n");
  961. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  962. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  963. vcpu->arch.shregs.msr);
  964. kvmppc_dump_regs(vcpu);
  965. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  966. run->hw.hardware_exit_reason = vcpu->arch.trap;
  967. return RESUME_HOST;
  968. }
  969. run->exit_reason = KVM_EXIT_UNKNOWN;
  970. run->ready_for_interrupt_injection = 1;
  971. switch (vcpu->arch.trap) {
  972. /* We're good on these - the host merely wanted to get our attention */
  973. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  974. vcpu->stat.dec_exits++;
  975. r = RESUME_GUEST;
  976. break;
  977. case BOOK3S_INTERRUPT_EXTERNAL:
  978. case BOOK3S_INTERRUPT_H_DOORBELL:
  979. case BOOK3S_INTERRUPT_H_VIRT:
  980. vcpu->stat.ext_intr_exits++;
  981. r = RESUME_GUEST;
  982. break;
  983. /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
  984. case BOOK3S_INTERRUPT_HMI:
  985. case BOOK3S_INTERRUPT_PERFMON:
  986. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  987. r = RESUME_GUEST;
  988. break;
  989. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  990. /* Exit to guest with KVM_EXIT_NMI as exit reason */
  991. run->exit_reason = KVM_EXIT_NMI;
  992. run->hw.hardware_exit_reason = vcpu->arch.trap;
  993. /* Clear out the old NMI status from run->flags */
  994. run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
  995. /* Now set the NMI status */
  996. if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
  997. run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
  998. else
  999. run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
  1000. r = RESUME_HOST;
  1001. /* Print the MCE event to host console. */
  1002. machine_check_print_event_info(&vcpu->arch.mce_evt, false);
  1003. break;
  1004. case BOOK3S_INTERRUPT_PROGRAM:
  1005. {
  1006. ulong flags;
  1007. /*
  1008. * Normally program interrupts are delivered directly
  1009. * to the guest by the hardware, but we can get here
  1010. * as a result of a hypervisor emulation interrupt
  1011. * (e40) getting turned into a 700 by BML RTAS.
  1012. */
  1013. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  1014. kvmppc_core_queue_program(vcpu, flags);
  1015. r = RESUME_GUEST;
  1016. break;
  1017. }
  1018. case BOOK3S_INTERRUPT_SYSCALL:
  1019. {
  1020. /* hcall - punt to userspace */
  1021. int i;
  1022. /* hypercall with MSR_PR has already been handled in rmode,
  1023. * and never reaches here.
  1024. */
  1025. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  1026. for (i = 0; i < 9; ++i)
  1027. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  1028. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  1029. vcpu->arch.hcall_needed = 1;
  1030. r = RESUME_HOST;
  1031. break;
  1032. }
  1033. /*
  1034. * We get these next two if the guest accesses a page which it thinks
  1035. * it has mapped but which is not actually present, either because
  1036. * it is for an emulated I/O device or because the corresonding
  1037. * host page has been paged out. Any other HDSI/HISI interrupts
  1038. * have been handled already.
  1039. */
  1040. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  1041. r = RESUME_PAGE_FAULT;
  1042. break;
  1043. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  1044. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  1045. vcpu->arch.fault_dsisr = 0;
  1046. r = RESUME_PAGE_FAULT;
  1047. break;
  1048. /*
  1049. * This occurs if the guest executes an illegal instruction.
  1050. * If the guest debug is disabled, generate a program interrupt
  1051. * to the guest. If guest debug is enabled, we need to check
  1052. * whether the instruction is a software breakpoint instruction.
  1053. * Accordingly return to Guest or Host.
  1054. */
  1055. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  1056. if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
  1057. vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
  1058. swab32(vcpu->arch.emul_inst) :
  1059. vcpu->arch.emul_inst;
  1060. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
  1061. r = kvmppc_emulate_debug_inst(run, vcpu);
  1062. } else {
  1063. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  1064. r = RESUME_GUEST;
  1065. }
  1066. break;
  1067. /*
  1068. * This occurs if the guest (kernel or userspace), does something that
  1069. * is prohibited by HFSCR.
  1070. * On POWER9, this could be a doorbell instruction that we need
  1071. * to emulate.
  1072. * Otherwise, we just generate a program interrupt to the guest.
  1073. */
  1074. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
  1075. r = EMULATE_FAIL;
  1076. if ((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG)
  1077. r = kvmppc_emulate_doorbell_instr(vcpu);
  1078. if (r == EMULATE_FAIL) {
  1079. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  1080. r = RESUME_GUEST;
  1081. }
  1082. break;
  1083. case BOOK3S_INTERRUPT_HV_RM_HARD:
  1084. r = RESUME_PASSTHROUGH;
  1085. break;
  1086. default:
  1087. kvmppc_dump_regs(vcpu);
  1088. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  1089. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  1090. vcpu->arch.shregs.msr);
  1091. run->hw.hardware_exit_reason = vcpu->arch.trap;
  1092. r = RESUME_HOST;
  1093. break;
  1094. }
  1095. return r;
  1096. }
  1097. static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
  1098. struct kvm_sregs *sregs)
  1099. {
  1100. int i;
  1101. memset(sregs, 0, sizeof(struct kvm_sregs));
  1102. sregs->pvr = vcpu->arch.pvr;
  1103. for (i = 0; i < vcpu->arch.slb_max; i++) {
  1104. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  1105. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  1106. }
  1107. return 0;
  1108. }
  1109. static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
  1110. struct kvm_sregs *sregs)
  1111. {
  1112. int i, j;
  1113. /* Only accept the same PVR as the host's, since we can't spoof it */
  1114. if (sregs->pvr != vcpu->arch.pvr)
  1115. return -EINVAL;
  1116. j = 0;
  1117. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  1118. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  1119. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  1120. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  1121. ++j;
  1122. }
  1123. }
  1124. vcpu->arch.slb_max = j;
  1125. return 0;
  1126. }
  1127. static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
  1128. bool preserve_top32)
  1129. {
  1130. struct kvm *kvm = vcpu->kvm;
  1131. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  1132. u64 mask;
  1133. mutex_lock(&kvm->lock);
  1134. spin_lock(&vc->lock);
  1135. /*
  1136. * If ILE (interrupt little-endian) has changed, update the
  1137. * MSR_LE bit in the intr_msr for each vcpu in this vcore.
  1138. */
  1139. if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
  1140. struct kvm_vcpu *vcpu;
  1141. int i;
  1142. kvm_for_each_vcpu(i, vcpu, kvm) {
  1143. if (vcpu->arch.vcore != vc)
  1144. continue;
  1145. if (new_lpcr & LPCR_ILE)
  1146. vcpu->arch.intr_msr |= MSR_LE;
  1147. else
  1148. vcpu->arch.intr_msr &= ~MSR_LE;
  1149. }
  1150. }
  1151. /*
  1152. * Userspace can only modify DPFD (default prefetch depth),
  1153. * ILE (interrupt little-endian) and TC (translation control).
  1154. * On POWER8 and POWER9 userspace can also modify AIL (alt. interrupt loc.).
  1155. */
  1156. mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
  1157. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  1158. mask |= LPCR_AIL;
  1159. /*
  1160. * On POWER9, allow userspace to enable large decrementer for the
  1161. * guest, whether or not the host has it enabled.
  1162. */
  1163. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1164. mask |= LPCR_LD;
  1165. /* Broken 32-bit version of LPCR must not clear top bits */
  1166. if (preserve_top32)
  1167. mask &= 0xFFFFFFFF;
  1168. vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
  1169. spin_unlock(&vc->lock);
  1170. mutex_unlock(&kvm->lock);
  1171. }
  1172. static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1173. union kvmppc_one_reg *val)
  1174. {
  1175. int r = 0;
  1176. long int i;
  1177. switch (id) {
  1178. case KVM_REG_PPC_DEBUG_INST:
  1179. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  1180. break;
  1181. case KVM_REG_PPC_HIOR:
  1182. *val = get_reg_val(id, 0);
  1183. break;
  1184. case KVM_REG_PPC_DABR:
  1185. *val = get_reg_val(id, vcpu->arch.dabr);
  1186. break;
  1187. case KVM_REG_PPC_DABRX:
  1188. *val = get_reg_val(id, vcpu->arch.dabrx);
  1189. break;
  1190. case KVM_REG_PPC_DSCR:
  1191. *val = get_reg_val(id, vcpu->arch.dscr);
  1192. break;
  1193. case KVM_REG_PPC_PURR:
  1194. *val = get_reg_val(id, vcpu->arch.purr);
  1195. break;
  1196. case KVM_REG_PPC_SPURR:
  1197. *val = get_reg_val(id, vcpu->arch.spurr);
  1198. break;
  1199. case KVM_REG_PPC_AMR:
  1200. *val = get_reg_val(id, vcpu->arch.amr);
  1201. break;
  1202. case KVM_REG_PPC_UAMOR:
  1203. *val = get_reg_val(id, vcpu->arch.uamor);
  1204. break;
  1205. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1206. i = id - KVM_REG_PPC_MMCR0;
  1207. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  1208. break;
  1209. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1210. i = id - KVM_REG_PPC_PMC1;
  1211. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  1212. break;
  1213. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1214. i = id - KVM_REG_PPC_SPMC1;
  1215. *val = get_reg_val(id, vcpu->arch.spmc[i]);
  1216. break;
  1217. case KVM_REG_PPC_SIAR:
  1218. *val = get_reg_val(id, vcpu->arch.siar);
  1219. break;
  1220. case KVM_REG_PPC_SDAR:
  1221. *val = get_reg_val(id, vcpu->arch.sdar);
  1222. break;
  1223. case KVM_REG_PPC_SIER:
  1224. *val = get_reg_val(id, vcpu->arch.sier);
  1225. break;
  1226. case KVM_REG_PPC_IAMR:
  1227. *val = get_reg_val(id, vcpu->arch.iamr);
  1228. break;
  1229. case KVM_REG_PPC_PSPB:
  1230. *val = get_reg_val(id, vcpu->arch.pspb);
  1231. break;
  1232. case KVM_REG_PPC_DPDES:
  1233. *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
  1234. break;
  1235. case KVM_REG_PPC_VTB:
  1236. *val = get_reg_val(id, vcpu->arch.vcore->vtb);
  1237. break;
  1238. case KVM_REG_PPC_DAWR:
  1239. *val = get_reg_val(id, vcpu->arch.dawr);
  1240. break;
  1241. case KVM_REG_PPC_DAWRX:
  1242. *val = get_reg_val(id, vcpu->arch.dawrx);
  1243. break;
  1244. case KVM_REG_PPC_CIABR:
  1245. *val = get_reg_val(id, vcpu->arch.ciabr);
  1246. break;
  1247. case KVM_REG_PPC_CSIGR:
  1248. *val = get_reg_val(id, vcpu->arch.csigr);
  1249. break;
  1250. case KVM_REG_PPC_TACR:
  1251. *val = get_reg_val(id, vcpu->arch.tacr);
  1252. break;
  1253. case KVM_REG_PPC_TCSCR:
  1254. *val = get_reg_val(id, vcpu->arch.tcscr);
  1255. break;
  1256. case KVM_REG_PPC_PID:
  1257. *val = get_reg_val(id, vcpu->arch.pid);
  1258. break;
  1259. case KVM_REG_PPC_ACOP:
  1260. *val = get_reg_val(id, vcpu->arch.acop);
  1261. break;
  1262. case KVM_REG_PPC_WORT:
  1263. *val = get_reg_val(id, vcpu->arch.wort);
  1264. break;
  1265. case KVM_REG_PPC_TIDR:
  1266. *val = get_reg_val(id, vcpu->arch.tid);
  1267. break;
  1268. case KVM_REG_PPC_PSSCR:
  1269. *val = get_reg_val(id, vcpu->arch.psscr);
  1270. break;
  1271. case KVM_REG_PPC_VPA_ADDR:
  1272. spin_lock(&vcpu->arch.vpa_update_lock);
  1273. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  1274. spin_unlock(&vcpu->arch.vpa_update_lock);
  1275. break;
  1276. case KVM_REG_PPC_VPA_SLB:
  1277. spin_lock(&vcpu->arch.vpa_update_lock);
  1278. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  1279. val->vpaval.length = vcpu->arch.slb_shadow.len;
  1280. spin_unlock(&vcpu->arch.vpa_update_lock);
  1281. break;
  1282. case KVM_REG_PPC_VPA_DTL:
  1283. spin_lock(&vcpu->arch.vpa_update_lock);
  1284. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  1285. val->vpaval.length = vcpu->arch.dtl.len;
  1286. spin_unlock(&vcpu->arch.vpa_update_lock);
  1287. break;
  1288. case KVM_REG_PPC_TB_OFFSET:
  1289. *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
  1290. break;
  1291. case KVM_REG_PPC_LPCR:
  1292. case KVM_REG_PPC_LPCR_64:
  1293. *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
  1294. break;
  1295. case KVM_REG_PPC_PPR:
  1296. *val = get_reg_val(id, vcpu->arch.ppr);
  1297. break;
  1298. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1299. case KVM_REG_PPC_TFHAR:
  1300. *val = get_reg_val(id, vcpu->arch.tfhar);
  1301. break;
  1302. case KVM_REG_PPC_TFIAR:
  1303. *val = get_reg_val(id, vcpu->arch.tfiar);
  1304. break;
  1305. case KVM_REG_PPC_TEXASR:
  1306. *val = get_reg_val(id, vcpu->arch.texasr);
  1307. break;
  1308. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1309. i = id - KVM_REG_PPC_TM_GPR0;
  1310. *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
  1311. break;
  1312. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1313. {
  1314. int j;
  1315. i = id - KVM_REG_PPC_TM_VSR0;
  1316. if (i < 32)
  1317. for (j = 0; j < TS_FPRWIDTH; j++)
  1318. val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
  1319. else {
  1320. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1321. val->vval = vcpu->arch.vr_tm.vr[i-32];
  1322. else
  1323. r = -ENXIO;
  1324. }
  1325. break;
  1326. }
  1327. case KVM_REG_PPC_TM_CR:
  1328. *val = get_reg_val(id, vcpu->arch.cr_tm);
  1329. break;
  1330. case KVM_REG_PPC_TM_XER:
  1331. *val = get_reg_val(id, vcpu->arch.xer_tm);
  1332. break;
  1333. case KVM_REG_PPC_TM_LR:
  1334. *val = get_reg_val(id, vcpu->arch.lr_tm);
  1335. break;
  1336. case KVM_REG_PPC_TM_CTR:
  1337. *val = get_reg_val(id, vcpu->arch.ctr_tm);
  1338. break;
  1339. case KVM_REG_PPC_TM_FPSCR:
  1340. *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
  1341. break;
  1342. case KVM_REG_PPC_TM_AMR:
  1343. *val = get_reg_val(id, vcpu->arch.amr_tm);
  1344. break;
  1345. case KVM_REG_PPC_TM_PPR:
  1346. *val = get_reg_val(id, vcpu->arch.ppr_tm);
  1347. break;
  1348. case KVM_REG_PPC_TM_VRSAVE:
  1349. *val = get_reg_val(id, vcpu->arch.vrsave_tm);
  1350. break;
  1351. case KVM_REG_PPC_TM_VSCR:
  1352. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1353. *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
  1354. else
  1355. r = -ENXIO;
  1356. break;
  1357. case KVM_REG_PPC_TM_DSCR:
  1358. *val = get_reg_val(id, vcpu->arch.dscr_tm);
  1359. break;
  1360. case KVM_REG_PPC_TM_TAR:
  1361. *val = get_reg_val(id, vcpu->arch.tar_tm);
  1362. break;
  1363. #endif
  1364. case KVM_REG_PPC_ARCH_COMPAT:
  1365. *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
  1366. break;
  1367. default:
  1368. r = -EINVAL;
  1369. break;
  1370. }
  1371. return r;
  1372. }
  1373. static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1374. union kvmppc_one_reg *val)
  1375. {
  1376. int r = 0;
  1377. long int i;
  1378. unsigned long addr, len;
  1379. switch (id) {
  1380. case KVM_REG_PPC_HIOR:
  1381. /* Only allow this to be set to zero */
  1382. if (set_reg_val(id, *val))
  1383. r = -EINVAL;
  1384. break;
  1385. case KVM_REG_PPC_DABR:
  1386. vcpu->arch.dabr = set_reg_val(id, *val);
  1387. break;
  1388. case KVM_REG_PPC_DABRX:
  1389. vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
  1390. break;
  1391. case KVM_REG_PPC_DSCR:
  1392. vcpu->arch.dscr = set_reg_val(id, *val);
  1393. break;
  1394. case KVM_REG_PPC_PURR:
  1395. vcpu->arch.purr = set_reg_val(id, *val);
  1396. break;
  1397. case KVM_REG_PPC_SPURR:
  1398. vcpu->arch.spurr = set_reg_val(id, *val);
  1399. break;
  1400. case KVM_REG_PPC_AMR:
  1401. vcpu->arch.amr = set_reg_val(id, *val);
  1402. break;
  1403. case KVM_REG_PPC_UAMOR:
  1404. vcpu->arch.uamor = set_reg_val(id, *val);
  1405. break;
  1406. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1407. i = id - KVM_REG_PPC_MMCR0;
  1408. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  1409. break;
  1410. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1411. i = id - KVM_REG_PPC_PMC1;
  1412. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  1413. break;
  1414. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1415. i = id - KVM_REG_PPC_SPMC1;
  1416. vcpu->arch.spmc[i] = set_reg_val(id, *val);
  1417. break;
  1418. case KVM_REG_PPC_SIAR:
  1419. vcpu->arch.siar = set_reg_val(id, *val);
  1420. break;
  1421. case KVM_REG_PPC_SDAR:
  1422. vcpu->arch.sdar = set_reg_val(id, *val);
  1423. break;
  1424. case KVM_REG_PPC_SIER:
  1425. vcpu->arch.sier = set_reg_val(id, *val);
  1426. break;
  1427. case KVM_REG_PPC_IAMR:
  1428. vcpu->arch.iamr = set_reg_val(id, *val);
  1429. break;
  1430. case KVM_REG_PPC_PSPB:
  1431. vcpu->arch.pspb = set_reg_val(id, *val);
  1432. break;
  1433. case KVM_REG_PPC_DPDES:
  1434. vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
  1435. break;
  1436. case KVM_REG_PPC_VTB:
  1437. vcpu->arch.vcore->vtb = set_reg_val(id, *val);
  1438. break;
  1439. case KVM_REG_PPC_DAWR:
  1440. vcpu->arch.dawr = set_reg_val(id, *val);
  1441. break;
  1442. case KVM_REG_PPC_DAWRX:
  1443. vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
  1444. break;
  1445. case KVM_REG_PPC_CIABR:
  1446. vcpu->arch.ciabr = set_reg_val(id, *val);
  1447. /* Don't allow setting breakpoints in hypervisor code */
  1448. if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
  1449. vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
  1450. break;
  1451. case KVM_REG_PPC_CSIGR:
  1452. vcpu->arch.csigr = set_reg_val(id, *val);
  1453. break;
  1454. case KVM_REG_PPC_TACR:
  1455. vcpu->arch.tacr = set_reg_val(id, *val);
  1456. break;
  1457. case KVM_REG_PPC_TCSCR:
  1458. vcpu->arch.tcscr = set_reg_val(id, *val);
  1459. break;
  1460. case KVM_REG_PPC_PID:
  1461. vcpu->arch.pid = set_reg_val(id, *val);
  1462. break;
  1463. case KVM_REG_PPC_ACOP:
  1464. vcpu->arch.acop = set_reg_val(id, *val);
  1465. break;
  1466. case KVM_REG_PPC_WORT:
  1467. vcpu->arch.wort = set_reg_val(id, *val);
  1468. break;
  1469. case KVM_REG_PPC_TIDR:
  1470. vcpu->arch.tid = set_reg_val(id, *val);
  1471. break;
  1472. case KVM_REG_PPC_PSSCR:
  1473. vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
  1474. break;
  1475. case KVM_REG_PPC_VPA_ADDR:
  1476. addr = set_reg_val(id, *val);
  1477. r = -EINVAL;
  1478. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  1479. vcpu->arch.dtl.next_gpa))
  1480. break;
  1481. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  1482. break;
  1483. case KVM_REG_PPC_VPA_SLB:
  1484. addr = val->vpaval.addr;
  1485. len = val->vpaval.length;
  1486. r = -EINVAL;
  1487. if (addr && !vcpu->arch.vpa.next_gpa)
  1488. break;
  1489. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  1490. break;
  1491. case KVM_REG_PPC_VPA_DTL:
  1492. addr = val->vpaval.addr;
  1493. len = val->vpaval.length;
  1494. r = -EINVAL;
  1495. if (addr && (len < sizeof(struct dtl_entry) ||
  1496. !vcpu->arch.vpa.next_gpa))
  1497. break;
  1498. len -= len % sizeof(struct dtl_entry);
  1499. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  1500. break;
  1501. case KVM_REG_PPC_TB_OFFSET:
  1502. /*
  1503. * POWER9 DD1 has an erratum where writing TBU40 causes
  1504. * the timebase to lose ticks. So we don't let the
  1505. * timebase offset be changed on P9 DD1. (It is
  1506. * initialized to zero.)
  1507. */
  1508. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  1509. break;
  1510. /* round up to multiple of 2^24 */
  1511. vcpu->arch.vcore->tb_offset =
  1512. ALIGN(set_reg_val(id, *val), 1UL << 24);
  1513. break;
  1514. case KVM_REG_PPC_LPCR:
  1515. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
  1516. break;
  1517. case KVM_REG_PPC_LPCR_64:
  1518. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
  1519. break;
  1520. case KVM_REG_PPC_PPR:
  1521. vcpu->arch.ppr = set_reg_val(id, *val);
  1522. break;
  1523. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1524. case KVM_REG_PPC_TFHAR:
  1525. vcpu->arch.tfhar = set_reg_val(id, *val);
  1526. break;
  1527. case KVM_REG_PPC_TFIAR:
  1528. vcpu->arch.tfiar = set_reg_val(id, *val);
  1529. break;
  1530. case KVM_REG_PPC_TEXASR:
  1531. vcpu->arch.texasr = set_reg_val(id, *val);
  1532. break;
  1533. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1534. i = id - KVM_REG_PPC_TM_GPR0;
  1535. vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
  1536. break;
  1537. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1538. {
  1539. int j;
  1540. i = id - KVM_REG_PPC_TM_VSR0;
  1541. if (i < 32)
  1542. for (j = 0; j < TS_FPRWIDTH; j++)
  1543. vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
  1544. else
  1545. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1546. vcpu->arch.vr_tm.vr[i-32] = val->vval;
  1547. else
  1548. r = -ENXIO;
  1549. break;
  1550. }
  1551. case KVM_REG_PPC_TM_CR:
  1552. vcpu->arch.cr_tm = set_reg_val(id, *val);
  1553. break;
  1554. case KVM_REG_PPC_TM_XER:
  1555. vcpu->arch.xer_tm = set_reg_val(id, *val);
  1556. break;
  1557. case KVM_REG_PPC_TM_LR:
  1558. vcpu->arch.lr_tm = set_reg_val(id, *val);
  1559. break;
  1560. case KVM_REG_PPC_TM_CTR:
  1561. vcpu->arch.ctr_tm = set_reg_val(id, *val);
  1562. break;
  1563. case KVM_REG_PPC_TM_FPSCR:
  1564. vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
  1565. break;
  1566. case KVM_REG_PPC_TM_AMR:
  1567. vcpu->arch.amr_tm = set_reg_val(id, *val);
  1568. break;
  1569. case KVM_REG_PPC_TM_PPR:
  1570. vcpu->arch.ppr_tm = set_reg_val(id, *val);
  1571. break;
  1572. case KVM_REG_PPC_TM_VRSAVE:
  1573. vcpu->arch.vrsave_tm = set_reg_val(id, *val);
  1574. break;
  1575. case KVM_REG_PPC_TM_VSCR:
  1576. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1577. vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
  1578. else
  1579. r = - ENXIO;
  1580. break;
  1581. case KVM_REG_PPC_TM_DSCR:
  1582. vcpu->arch.dscr_tm = set_reg_val(id, *val);
  1583. break;
  1584. case KVM_REG_PPC_TM_TAR:
  1585. vcpu->arch.tar_tm = set_reg_val(id, *val);
  1586. break;
  1587. #endif
  1588. case KVM_REG_PPC_ARCH_COMPAT:
  1589. r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
  1590. break;
  1591. default:
  1592. r = -EINVAL;
  1593. break;
  1594. }
  1595. return r;
  1596. }
  1597. /*
  1598. * On POWER9, threads are independent and can be in different partitions.
  1599. * Therefore we consider each thread to be a subcore.
  1600. * There is a restriction that all threads have to be in the same
  1601. * MMU mode (radix or HPT), unfortunately, but since we only support
  1602. * HPT guests on a HPT host so far, that isn't an impediment yet.
  1603. */
  1604. static int threads_per_vcore(void)
  1605. {
  1606. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1607. return 1;
  1608. return threads_per_subcore;
  1609. }
  1610. static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
  1611. {
  1612. struct kvmppc_vcore *vcore;
  1613. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  1614. if (vcore == NULL)
  1615. return NULL;
  1616. spin_lock_init(&vcore->lock);
  1617. spin_lock_init(&vcore->stoltb_lock);
  1618. init_swait_queue_head(&vcore->wq);
  1619. vcore->preempt_tb = TB_NIL;
  1620. vcore->lpcr = kvm->arch.lpcr;
  1621. vcore->first_vcpuid = core * kvm->arch.smt_mode;
  1622. vcore->kvm = kvm;
  1623. INIT_LIST_HEAD(&vcore->preempt_list);
  1624. return vcore;
  1625. }
  1626. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1627. static struct debugfs_timings_element {
  1628. const char *name;
  1629. size_t offset;
  1630. } timings[] = {
  1631. {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
  1632. {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
  1633. {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
  1634. {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
  1635. {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
  1636. };
  1637. #define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
  1638. struct debugfs_timings_state {
  1639. struct kvm_vcpu *vcpu;
  1640. unsigned int buflen;
  1641. char buf[N_TIMINGS * 100];
  1642. };
  1643. static int debugfs_timings_open(struct inode *inode, struct file *file)
  1644. {
  1645. struct kvm_vcpu *vcpu = inode->i_private;
  1646. struct debugfs_timings_state *p;
  1647. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1648. if (!p)
  1649. return -ENOMEM;
  1650. kvm_get_kvm(vcpu->kvm);
  1651. p->vcpu = vcpu;
  1652. file->private_data = p;
  1653. return nonseekable_open(inode, file);
  1654. }
  1655. static int debugfs_timings_release(struct inode *inode, struct file *file)
  1656. {
  1657. struct debugfs_timings_state *p = file->private_data;
  1658. kvm_put_kvm(p->vcpu->kvm);
  1659. kfree(p);
  1660. return 0;
  1661. }
  1662. static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
  1663. size_t len, loff_t *ppos)
  1664. {
  1665. struct debugfs_timings_state *p = file->private_data;
  1666. struct kvm_vcpu *vcpu = p->vcpu;
  1667. char *s, *buf_end;
  1668. struct kvmhv_tb_accumulator tb;
  1669. u64 count;
  1670. loff_t pos;
  1671. ssize_t n;
  1672. int i, loops;
  1673. bool ok;
  1674. if (!p->buflen) {
  1675. s = p->buf;
  1676. buf_end = s + sizeof(p->buf);
  1677. for (i = 0; i < N_TIMINGS; ++i) {
  1678. struct kvmhv_tb_accumulator *acc;
  1679. acc = (struct kvmhv_tb_accumulator *)
  1680. ((unsigned long)vcpu + timings[i].offset);
  1681. ok = false;
  1682. for (loops = 0; loops < 1000; ++loops) {
  1683. count = acc->seqcount;
  1684. if (!(count & 1)) {
  1685. smp_rmb();
  1686. tb = *acc;
  1687. smp_rmb();
  1688. if (count == acc->seqcount) {
  1689. ok = true;
  1690. break;
  1691. }
  1692. }
  1693. udelay(1);
  1694. }
  1695. if (!ok)
  1696. snprintf(s, buf_end - s, "%s: stuck\n",
  1697. timings[i].name);
  1698. else
  1699. snprintf(s, buf_end - s,
  1700. "%s: %llu %llu %llu %llu\n",
  1701. timings[i].name, count / 2,
  1702. tb_to_ns(tb.tb_total),
  1703. tb_to_ns(tb.tb_min),
  1704. tb_to_ns(tb.tb_max));
  1705. s += strlen(s);
  1706. }
  1707. p->buflen = s - p->buf;
  1708. }
  1709. pos = *ppos;
  1710. if (pos >= p->buflen)
  1711. return 0;
  1712. if (len > p->buflen - pos)
  1713. len = p->buflen - pos;
  1714. n = copy_to_user(buf, p->buf + pos, len);
  1715. if (n) {
  1716. if (n == len)
  1717. return -EFAULT;
  1718. len -= n;
  1719. }
  1720. *ppos = pos + len;
  1721. return len;
  1722. }
  1723. static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
  1724. size_t len, loff_t *ppos)
  1725. {
  1726. return -EACCES;
  1727. }
  1728. static const struct file_operations debugfs_timings_ops = {
  1729. .owner = THIS_MODULE,
  1730. .open = debugfs_timings_open,
  1731. .release = debugfs_timings_release,
  1732. .read = debugfs_timings_read,
  1733. .write = debugfs_timings_write,
  1734. .llseek = generic_file_llseek,
  1735. };
  1736. /* Create a debugfs directory for the vcpu */
  1737. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1738. {
  1739. char buf[16];
  1740. struct kvm *kvm = vcpu->kvm;
  1741. snprintf(buf, sizeof(buf), "vcpu%u", id);
  1742. if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  1743. return;
  1744. vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
  1745. if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
  1746. return;
  1747. vcpu->arch.debugfs_timings =
  1748. debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
  1749. vcpu, &debugfs_timings_ops);
  1750. }
  1751. #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1752. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1753. {
  1754. }
  1755. #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1756. static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
  1757. unsigned int id)
  1758. {
  1759. struct kvm_vcpu *vcpu;
  1760. int err;
  1761. int core;
  1762. struct kvmppc_vcore *vcore;
  1763. err = -ENOMEM;
  1764. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1765. if (!vcpu)
  1766. goto out;
  1767. err = kvm_vcpu_init(vcpu, kvm, id);
  1768. if (err)
  1769. goto free_vcpu;
  1770. vcpu->arch.shared = &vcpu->arch.shregs;
  1771. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  1772. /*
  1773. * The shared struct is never shared on HV,
  1774. * so we can always use host endianness
  1775. */
  1776. #ifdef __BIG_ENDIAN__
  1777. vcpu->arch.shared_big_endian = true;
  1778. #else
  1779. vcpu->arch.shared_big_endian = false;
  1780. #endif
  1781. #endif
  1782. vcpu->arch.mmcr[0] = MMCR0_FC;
  1783. vcpu->arch.ctrl = CTRL_RUNLATCH;
  1784. /* default to host PVR, since we can't spoof it */
  1785. kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
  1786. spin_lock_init(&vcpu->arch.vpa_update_lock);
  1787. spin_lock_init(&vcpu->arch.tbacct_lock);
  1788. vcpu->arch.busy_preempt = TB_NIL;
  1789. vcpu->arch.intr_msr = MSR_SF | MSR_ME;
  1790. /*
  1791. * Set the default HFSCR for the guest from the host value.
  1792. * This value is only used on POWER9.
  1793. * On POWER9 DD1, TM doesn't work, so we make sure to
  1794. * prevent the guest from using it.
  1795. * On POWER9, we want to virtualize the doorbell facility, so we
  1796. * turn off the HFSCR bit, which causes those instructions to trap.
  1797. */
  1798. vcpu->arch.hfscr = mfspr(SPRN_HFSCR);
  1799. if (!cpu_has_feature(CPU_FTR_TM))
  1800. vcpu->arch.hfscr &= ~HFSCR_TM;
  1801. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1802. vcpu->arch.hfscr &= ~HFSCR_MSGP;
  1803. kvmppc_mmu_book3s_hv_init(vcpu);
  1804. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  1805. init_waitqueue_head(&vcpu->arch.cpu_run);
  1806. mutex_lock(&kvm->lock);
  1807. vcore = NULL;
  1808. err = -EINVAL;
  1809. core = id / kvm->arch.smt_mode;
  1810. if (core < KVM_MAX_VCORES) {
  1811. vcore = kvm->arch.vcores[core];
  1812. if (!vcore) {
  1813. err = -ENOMEM;
  1814. vcore = kvmppc_vcore_create(kvm, core);
  1815. kvm->arch.vcores[core] = vcore;
  1816. kvm->arch.online_vcores++;
  1817. }
  1818. }
  1819. mutex_unlock(&kvm->lock);
  1820. if (!vcore)
  1821. goto free_vcpu;
  1822. spin_lock(&vcore->lock);
  1823. ++vcore->num_threads;
  1824. spin_unlock(&vcore->lock);
  1825. vcpu->arch.vcore = vcore;
  1826. vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
  1827. vcpu->arch.thread_cpu = -1;
  1828. vcpu->arch.prev_cpu = -1;
  1829. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  1830. kvmppc_sanity_check(vcpu);
  1831. debugfs_vcpu_init(vcpu, id);
  1832. return vcpu;
  1833. free_vcpu:
  1834. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1835. out:
  1836. return ERR_PTR(err);
  1837. }
  1838. static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
  1839. unsigned long flags)
  1840. {
  1841. int err;
  1842. int esmt = 0;
  1843. if (flags)
  1844. return -EINVAL;
  1845. if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
  1846. return -EINVAL;
  1847. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  1848. /*
  1849. * On POWER8 (or POWER7), the threading mode is "strict",
  1850. * so we pack smt_mode vcpus per vcore.
  1851. */
  1852. if (smt_mode > threads_per_subcore)
  1853. return -EINVAL;
  1854. } else {
  1855. /*
  1856. * On POWER9, the threading mode is "loose",
  1857. * so each vcpu gets its own vcore.
  1858. */
  1859. esmt = smt_mode;
  1860. smt_mode = 1;
  1861. }
  1862. mutex_lock(&kvm->lock);
  1863. err = -EBUSY;
  1864. if (!kvm->arch.online_vcores) {
  1865. kvm->arch.smt_mode = smt_mode;
  1866. kvm->arch.emul_smt_mode = esmt;
  1867. err = 0;
  1868. }
  1869. mutex_unlock(&kvm->lock);
  1870. return err;
  1871. }
  1872. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  1873. {
  1874. if (vpa->pinned_addr)
  1875. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  1876. vpa->dirty);
  1877. }
  1878. static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
  1879. {
  1880. spin_lock(&vcpu->arch.vpa_update_lock);
  1881. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  1882. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  1883. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  1884. spin_unlock(&vcpu->arch.vpa_update_lock);
  1885. kvm_vcpu_uninit(vcpu);
  1886. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1887. }
  1888. static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
  1889. {
  1890. /* Indicate we want to get back into the guest */
  1891. return 1;
  1892. }
  1893. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  1894. {
  1895. unsigned long dec_nsec, now;
  1896. now = get_tb();
  1897. if (now > vcpu->arch.dec_expires) {
  1898. /* decrementer has already gone negative */
  1899. kvmppc_core_queue_dec(vcpu);
  1900. kvmppc_core_prepare_to_enter(vcpu);
  1901. return;
  1902. }
  1903. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  1904. / tb_ticks_per_sec;
  1905. hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
  1906. vcpu->arch.timer_running = 1;
  1907. }
  1908. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  1909. {
  1910. vcpu->arch.ceded = 0;
  1911. if (vcpu->arch.timer_running) {
  1912. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1913. vcpu->arch.timer_running = 0;
  1914. }
  1915. }
  1916. extern int __kvmppc_vcore_entry(void);
  1917. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  1918. struct kvm_vcpu *vcpu)
  1919. {
  1920. u64 now;
  1921. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1922. return;
  1923. spin_lock_irq(&vcpu->arch.tbacct_lock);
  1924. now = mftb();
  1925. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  1926. vcpu->arch.stolen_logged;
  1927. vcpu->arch.busy_preempt = now;
  1928. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  1929. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  1930. --vc->n_runnable;
  1931. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
  1932. }
  1933. static int kvmppc_grab_hwthread(int cpu)
  1934. {
  1935. struct paca_struct *tpaca;
  1936. long timeout = 10000;
  1937. tpaca = &paca[cpu];
  1938. /* Ensure the thread won't go into the kernel if it wakes */
  1939. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1940. tpaca->kvm_hstate.kvm_vcore = NULL;
  1941. tpaca->kvm_hstate.napping = 0;
  1942. smp_wmb();
  1943. tpaca->kvm_hstate.hwthread_req = 1;
  1944. /*
  1945. * If the thread is already executing in the kernel (e.g. handling
  1946. * a stray interrupt), wait for it to get back to nap mode.
  1947. * The smp_mb() is to ensure that our setting of hwthread_req
  1948. * is visible before we look at hwthread_state, so if this
  1949. * races with the code at system_reset_pSeries and the thread
  1950. * misses our setting of hwthread_req, we are sure to see its
  1951. * setting of hwthread_state, and vice versa.
  1952. */
  1953. smp_mb();
  1954. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  1955. if (--timeout <= 0) {
  1956. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  1957. return -EBUSY;
  1958. }
  1959. udelay(1);
  1960. }
  1961. return 0;
  1962. }
  1963. static void kvmppc_release_hwthread(int cpu)
  1964. {
  1965. struct paca_struct *tpaca;
  1966. tpaca = &paca[cpu];
  1967. tpaca->kvm_hstate.hwthread_req = 0;
  1968. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1969. tpaca->kvm_hstate.kvm_vcore = NULL;
  1970. tpaca->kvm_hstate.kvm_split_mode = NULL;
  1971. }
  1972. static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
  1973. {
  1974. int i;
  1975. cpu = cpu_first_thread_sibling(cpu);
  1976. cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
  1977. /*
  1978. * Make sure setting of bit in need_tlb_flush precedes
  1979. * testing of cpu_in_guest bits. The matching barrier on
  1980. * the other side is the first smp_mb() in kvmppc_run_core().
  1981. */
  1982. smp_mb();
  1983. for (i = 0; i < threads_per_core; ++i)
  1984. if (cpumask_test_cpu(cpu + i, &kvm->arch.cpu_in_guest))
  1985. smp_call_function_single(cpu + i, do_nothing, NULL, 1);
  1986. }
  1987. static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
  1988. {
  1989. struct kvm *kvm = vcpu->kvm;
  1990. /*
  1991. * With radix, the guest can do TLB invalidations itself,
  1992. * and it could choose to use the local form (tlbiel) if
  1993. * it is invalidating a translation that has only ever been
  1994. * used on one vcpu. However, that doesn't mean it has
  1995. * only ever been used on one physical cpu, since vcpus
  1996. * can move around between pcpus. To cope with this, when
  1997. * a vcpu moves from one pcpu to another, we need to tell
  1998. * any vcpus running on the same core as this vcpu previously
  1999. * ran to flush the TLB. The TLB is shared between threads,
  2000. * so we use a single bit in .need_tlb_flush for all 4 threads.
  2001. */
  2002. if (vcpu->arch.prev_cpu != pcpu) {
  2003. if (vcpu->arch.prev_cpu >= 0 &&
  2004. cpu_first_thread_sibling(vcpu->arch.prev_cpu) !=
  2005. cpu_first_thread_sibling(pcpu))
  2006. radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu);
  2007. vcpu->arch.prev_cpu = pcpu;
  2008. }
  2009. }
  2010. static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
  2011. {
  2012. int cpu;
  2013. struct paca_struct *tpaca;
  2014. struct kvm *kvm = vc->kvm;
  2015. cpu = vc->pcpu;
  2016. if (vcpu) {
  2017. if (vcpu->arch.timer_running) {
  2018. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  2019. vcpu->arch.timer_running = 0;
  2020. }
  2021. cpu += vcpu->arch.ptid;
  2022. vcpu->cpu = vc->pcpu;
  2023. vcpu->arch.thread_cpu = cpu;
  2024. cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
  2025. }
  2026. tpaca = &paca[cpu];
  2027. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  2028. tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
  2029. /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
  2030. smp_wmb();
  2031. tpaca->kvm_hstate.kvm_vcore = vc;
  2032. if (cpu != smp_processor_id())
  2033. kvmppc_ipi_thread(cpu);
  2034. }
  2035. static void kvmppc_wait_for_nap(void)
  2036. {
  2037. int cpu = smp_processor_id();
  2038. int i, loops;
  2039. int n_threads = threads_per_vcore();
  2040. if (n_threads <= 1)
  2041. return;
  2042. for (loops = 0; loops < 1000000; ++loops) {
  2043. /*
  2044. * Check if all threads are finished.
  2045. * We set the vcore pointer when starting a thread
  2046. * and the thread clears it when finished, so we look
  2047. * for any threads that still have a non-NULL vcore ptr.
  2048. */
  2049. for (i = 1; i < n_threads; ++i)
  2050. if (paca[cpu + i].kvm_hstate.kvm_vcore)
  2051. break;
  2052. if (i == n_threads) {
  2053. HMT_medium();
  2054. return;
  2055. }
  2056. HMT_low();
  2057. }
  2058. HMT_medium();
  2059. for (i = 1; i < n_threads; ++i)
  2060. if (paca[cpu + i].kvm_hstate.kvm_vcore)
  2061. pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
  2062. }
  2063. /*
  2064. * Check that we are on thread 0 and that any other threads in
  2065. * this core are off-line. Then grab the threads so they can't
  2066. * enter the kernel.
  2067. */
  2068. static int on_primary_thread(void)
  2069. {
  2070. int cpu = smp_processor_id();
  2071. int thr;
  2072. /* Are we on a primary subcore? */
  2073. if (cpu_thread_in_subcore(cpu))
  2074. return 0;
  2075. thr = 0;
  2076. while (++thr < threads_per_subcore)
  2077. if (cpu_online(cpu + thr))
  2078. return 0;
  2079. /* Grab all hw threads so they can't go into the kernel */
  2080. for (thr = 1; thr < threads_per_subcore; ++thr) {
  2081. if (kvmppc_grab_hwthread(cpu + thr)) {
  2082. /* Couldn't grab one; let the others go */
  2083. do {
  2084. kvmppc_release_hwthread(cpu + thr);
  2085. } while (--thr > 0);
  2086. return 0;
  2087. }
  2088. }
  2089. return 1;
  2090. }
  2091. /*
  2092. * A list of virtual cores for each physical CPU.
  2093. * These are vcores that could run but their runner VCPU tasks are
  2094. * (or may be) preempted.
  2095. */
  2096. struct preempted_vcore_list {
  2097. struct list_head list;
  2098. spinlock_t lock;
  2099. };
  2100. static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
  2101. static void init_vcore_lists(void)
  2102. {
  2103. int cpu;
  2104. for_each_possible_cpu(cpu) {
  2105. struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
  2106. spin_lock_init(&lp->lock);
  2107. INIT_LIST_HEAD(&lp->list);
  2108. }
  2109. }
  2110. static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
  2111. {
  2112. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2113. vc->vcore_state = VCORE_PREEMPT;
  2114. vc->pcpu = smp_processor_id();
  2115. if (vc->num_threads < threads_per_vcore()) {
  2116. spin_lock(&lp->lock);
  2117. list_add_tail(&vc->preempt_list, &lp->list);
  2118. spin_unlock(&lp->lock);
  2119. }
  2120. /* Start accumulating stolen time */
  2121. kvmppc_core_start_stolen(vc);
  2122. }
  2123. static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
  2124. {
  2125. struct preempted_vcore_list *lp;
  2126. kvmppc_core_end_stolen(vc);
  2127. if (!list_empty(&vc->preempt_list)) {
  2128. lp = &per_cpu(preempted_vcores, vc->pcpu);
  2129. spin_lock(&lp->lock);
  2130. list_del_init(&vc->preempt_list);
  2131. spin_unlock(&lp->lock);
  2132. }
  2133. vc->vcore_state = VCORE_INACTIVE;
  2134. }
  2135. /*
  2136. * This stores information about the virtual cores currently
  2137. * assigned to a physical core.
  2138. */
  2139. struct core_info {
  2140. int n_subcores;
  2141. int max_subcore_threads;
  2142. int total_threads;
  2143. int subcore_threads[MAX_SUBCORES];
  2144. struct kvmppc_vcore *vc[MAX_SUBCORES];
  2145. };
  2146. /*
  2147. * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
  2148. * respectively in 2-way micro-threading (split-core) mode.
  2149. */
  2150. static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
  2151. static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
  2152. {
  2153. memset(cip, 0, sizeof(*cip));
  2154. cip->n_subcores = 1;
  2155. cip->max_subcore_threads = vc->num_threads;
  2156. cip->total_threads = vc->num_threads;
  2157. cip->subcore_threads[0] = vc->num_threads;
  2158. cip->vc[0] = vc;
  2159. }
  2160. static bool subcore_config_ok(int n_subcores, int n_threads)
  2161. {
  2162. /* Can only dynamically split if unsplit to begin with */
  2163. if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
  2164. return false;
  2165. if (n_subcores > MAX_SUBCORES)
  2166. return false;
  2167. if (n_subcores > 1) {
  2168. if (!(dynamic_mt_modes & 2))
  2169. n_subcores = 4;
  2170. if (n_subcores > 2 && !(dynamic_mt_modes & 4))
  2171. return false;
  2172. }
  2173. return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
  2174. }
  2175. static void init_vcore_to_run(struct kvmppc_vcore *vc)
  2176. {
  2177. vc->entry_exit_map = 0;
  2178. vc->in_guest = 0;
  2179. vc->napping_threads = 0;
  2180. vc->conferring_threads = 0;
  2181. }
  2182. static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
  2183. {
  2184. int n_threads = vc->num_threads;
  2185. int sub;
  2186. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  2187. return false;
  2188. if (n_threads < cip->max_subcore_threads)
  2189. n_threads = cip->max_subcore_threads;
  2190. if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
  2191. return false;
  2192. cip->max_subcore_threads = n_threads;
  2193. sub = cip->n_subcores;
  2194. ++cip->n_subcores;
  2195. cip->total_threads += vc->num_threads;
  2196. cip->subcore_threads[sub] = vc->num_threads;
  2197. cip->vc[sub] = vc;
  2198. init_vcore_to_run(vc);
  2199. list_del_init(&vc->preempt_list);
  2200. return true;
  2201. }
  2202. /*
  2203. * Work out whether it is possible to piggyback the execution of
  2204. * vcore *pvc onto the execution of the other vcores described in *cip.
  2205. */
  2206. static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
  2207. int target_threads)
  2208. {
  2209. if (cip->total_threads + pvc->num_threads > target_threads)
  2210. return false;
  2211. return can_dynamic_split(pvc, cip);
  2212. }
  2213. static void prepare_threads(struct kvmppc_vcore *vc)
  2214. {
  2215. int i;
  2216. struct kvm_vcpu *vcpu;
  2217. for_each_runnable_thread(i, vcpu, vc) {
  2218. if (signal_pending(vcpu->arch.run_task))
  2219. vcpu->arch.ret = -EINTR;
  2220. else if (vcpu->arch.vpa.update_pending ||
  2221. vcpu->arch.slb_shadow.update_pending ||
  2222. vcpu->arch.dtl.update_pending)
  2223. vcpu->arch.ret = RESUME_GUEST;
  2224. else
  2225. continue;
  2226. kvmppc_remove_runnable(vc, vcpu);
  2227. wake_up(&vcpu->arch.cpu_run);
  2228. }
  2229. }
  2230. static void collect_piggybacks(struct core_info *cip, int target_threads)
  2231. {
  2232. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2233. struct kvmppc_vcore *pvc, *vcnext;
  2234. spin_lock(&lp->lock);
  2235. list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
  2236. if (!spin_trylock(&pvc->lock))
  2237. continue;
  2238. prepare_threads(pvc);
  2239. if (!pvc->n_runnable) {
  2240. list_del_init(&pvc->preempt_list);
  2241. if (pvc->runner == NULL) {
  2242. pvc->vcore_state = VCORE_INACTIVE;
  2243. kvmppc_core_end_stolen(pvc);
  2244. }
  2245. spin_unlock(&pvc->lock);
  2246. continue;
  2247. }
  2248. if (!can_piggyback(pvc, cip, target_threads)) {
  2249. spin_unlock(&pvc->lock);
  2250. continue;
  2251. }
  2252. kvmppc_core_end_stolen(pvc);
  2253. pvc->vcore_state = VCORE_PIGGYBACK;
  2254. if (cip->total_threads >= target_threads)
  2255. break;
  2256. }
  2257. spin_unlock(&lp->lock);
  2258. }
  2259. static bool recheck_signals(struct core_info *cip)
  2260. {
  2261. int sub, i;
  2262. struct kvm_vcpu *vcpu;
  2263. for (sub = 0; sub < cip->n_subcores; ++sub)
  2264. for_each_runnable_thread(i, vcpu, cip->vc[sub])
  2265. if (signal_pending(vcpu->arch.run_task))
  2266. return true;
  2267. return false;
  2268. }
  2269. static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
  2270. {
  2271. int still_running = 0, i;
  2272. u64 now;
  2273. long ret;
  2274. struct kvm_vcpu *vcpu;
  2275. spin_lock(&vc->lock);
  2276. now = get_tb();
  2277. for_each_runnable_thread(i, vcpu, vc) {
  2278. /* cancel pending dec exception if dec is positive */
  2279. if (now < vcpu->arch.dec_expires &&
  2280. kvmppc_core_pending_dec(vcpu))
  2281. kvmppc_core_dequeue_dec(vcpu);
  2282. trace_kvm_guest_exit(vcpu);
  2283. ret = RESUME_GUEST;
  2284. if (vcpu->arch.trap)
  2285. ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
  2286. vcpu->arch.run_task);
  2287. vcpu->arch.ret = ret;
  2288. vcpu->arch.trap = 0;
  2289. if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
  2290. if (vcpu->arch.pending_exceptions)
  2291. kvmppc_core_prepare_to_enter(vcpu);
  2292. if (vcpu->arch.ceded)
  2293. kvmppc_set_timer(vcpu);
  2294. else
  2295. ++still_running;
  2296. } else {
  2297. kvmppc_remove_runnable(vc, vcpu);
  2298. wake_up(&vcpu->arch.cpu_run);
  2299. }
  2300. }
  2301. if (!is_master) {
  2302. if (still_running > 0) {
  2303. kvmppc_vcore_preempt(vc);
  2304. } else if (vc->runner) {
  2305. vc->vcore_state = VCORE_PREEMPT;
  2306. kvmppc_core_start_stolen(vc);
  2307. } else {
  2308. vc->vcore_state = VCORE_INACTIVE;
  2309. }
  2310. if (vc->n_runnable > 0 && vc->runner == NULL) {
  2311. /* make sure there's a candidate runner awake */
  2312. i = -1;
  2313. vcpu = next_runnable_thread(vc, &i);
  2314. wake_up(&vcpu->arch.cpu_run);
  2315. }
  2316. }
  2317. spin_unlock(&vc->lock);
  2318. }
  2319. /*
  2320. * Clear core from the list of active host cores as we are about to
  2321. * enter the guest. Only do this if it is the primary thread of the
  2322. * core (not if a subcore) that is entering the guest.
  2323. */
  2324. static inline int kvmppc_clear_host_core(unsigned int cpu)
  2325. {
  2326. int core;
  2327. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2328. return 0;
  2329. /*
  2330. * Memory barrier can be omitted here as we will do a smp_wmb()
  2331. * later in kvmppc_start_thread and we need ensure that state is
  2332. * visible to other CPUs only after we enter guest.
  2333. */
  2334. core = cpu >> threads_shift;
  2335. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
  2336. return 0;
  2337. }
  2338. /*
  2339. * Advertise this core as an active host core since we exited the guest
  2340. * Only need to do this if it is the primary thread of the core that is
  2341. * exiting.
  2342. */
  2343. static inline int kvmppc_set_host_core(unsigned int cpu)
  2344. {
  2345. int core;
  2346. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2347. return 0;
  2348. /*
  2349. * Memory barrier can be omitted here because we do a spin_unlock
  2350. * immediately after this which provides the memory barrier.
  2351. */
  2352. core = cpu >> threads_shift;
  2353. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
  2354. return 0;
  2355. }
  2356. static void set_irq_happened(int trap)
  2357. {
  2358. switch (trap) {
  2359. case BOOK3S_INTERRUPT_EXTERNAL:
  2360. local_paca->irq_happened |= PACA_IRQ_EE;
  2361. break;
  2362. case BOOK3S_INTERRUPT_H_DOORBELL:
  2363. local_paca->irq_happened |= PACA_IRQ_DBELL;
  2364. break;
  2365. case BOOK3S_INTERRUPT_HMI:
  2366. local_paca->irq_happened |= PACA_IRQ_HMI;
  2367. break;
  2368. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  2369. replay_system_reset();
  2370. break;
  2371. }
  2372. }
  2373. /*
  2374. * Run a set of guest threads on a physical core.
  2375. * Called with vc->lock held.
  2376. */
  2377. static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
  2378. {
  2379. struct kvm_vcpu *vcpu;
  2380. int i;
  2381. int srcu_idx;
  2382. struct core_info core_info;
  2383. struct kvmppc_vcore *pvc;
  2384. struct kvm_split_mode split_info, *sip;
  2385. int split, subcore_size, active;
  2386. int sub;
  2387. bool thr0_done;
  2388. unsigned long cmd_bit, stat_bit;
  2389. int pcpu, thr;
  2390. int target_threads;
  2391. int controlled_threads;
  2392. int trap;
  2393. /*
  2394. * Remove from the list any threads that have a signal pending
  2395. * or need a VPA update done
  2396. */
  2397. prepare_threads(vc);
  2398. /* if the runner is no longer runnable, let the caller pick a new one */
  2399. if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
  2400. return;
  2401. /*
  2402. * Initialize *vc.
  2403. */
  2404. init_vcore_to_run(vc);
  2405. vc->preempt_tb = TB_NIL;
  2406. /*
  2407. * Number of threads that we will be controlling: the same as
  2408. * the number of threads per subcore, except on POWER9,
  2409. * where it's 1 because the threads are (mostly) independent.
  2410. */
  2411. controlled_threads = threads_per_vcore();
  2412. /*
  2413. * Make sure we are running on primary threads, and that secondary
  2414. * threads are offline. Also check if the number of threads in this
  2415. * guest are greater than the current system threads per guest.
  2416. */
  2417. if ((controlled_threads > 1) &&
  2418. ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
  2419. for_each_runnable_thread(i, vcpu, vc) {
  2420. vcpu->arch.ret = -EBUSY;
  2421. kvmppc_remove_runnable(vc, vcpu);
  2422. wake_up(&vcpu->arch.cpu_run);
  2423. }
  2424. goto out;
  2425. }
  2426. /*
  2427. * See if we could run any other vcores on the physical core
  2428. * along with this one.
  2429. */
  2430. init_core_info(&core_info, vc);
  2431. pcpu = smp_processor_id();
  2432. target_threads = controlled_threads;
  2433. if (target_smt_mode && target_smt_mode < target_threads)
  2434. target_threads = target_smt_mode;
  2435. if (vc->num_threads < target_threads)
  2436. collect_piggybacks(&core_info, target_threads);
  2437. /*
  2438. * On radix, arrange for TLB flushing if necessary.
  2439. * This has to be done before disabling interrupts since
  2440. * it uses smp_call_function().
  2441. */
  2442. pcpu = smp_processor_id();
  2443. if (kvm_is_radix(vc->kvm)) {
  2444. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2445. for_each_runnable_thread(i, vcpu, core_info.vc[sub])
  2446. kvmppc_prepare_radix_vcpu(vcpu, pcpu);
  2447. }
  2448. /*
  2449. * Hard-disable interrupts, and check resched flag and signals.
  2450. * If we need to reschedule or deliver a signal, clean up
  2451. * and return without going into the guest(s).
  2452. */
  2453. local_irq_disable();
  2454. hard_irq_disable();
  2455. if (lazy_irq_pending() || need_resched() ||
  2456. recheck_signals(&core_info)) {
  2457. local_irq_enable();
  2458. vc->vcore_state = VCORE_INACTIVE;
  2459. /* Unlock all except the primary vcore */
  2460. for (sub = 1; sub < core_info.n_subcores; ++sub) {
  2461. pvc = core_info.vc[sub];
  2462. /* Put back on to the preempted vcores list */
  2463. kvmppc_vcore_preempt(pvc);
  2464. spin_unlock(&pvc->lock);
  2465. }
  2466. for (i = 0; i < controlled_threads; ++i)
  2467. kvmppc_release_hwthread(pcpu + i);
  2468. return;
  2469. }
  2470. kvmppc_clear_host_core(pcpu);
  2471. /* Decide on micro-threading (split-core) mode */
  2472. subcore_size = threads_per_subcore;
  2473. cmd_bit = stat_bit = 0;
  2474. split = core_info.n_subcores;
  2475. sip = NULL;
  2476. if (split > 1) {
  2477. /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
  2478. if (split == 2 && (dynamic_mt_modes & 2)) {
  2479. cmd_bit = HID0_POWER8_1TO2LPAR;
  2480. stat_bit = HID0_POWER8_2LPARMODE;
  2481. } else {
  2482. split = 4;
  2483. cmd_bit = HID0_POWER8_1TO4LPAR;
  2484. stat_bit = HID0_POWER8_4LPARMODE;
  2485. }
  2486. subcore_size = MAX_SMT_THREADS / split;
  2487. sip = &split_info;
  2488. memset(&split_info, 0, sizeof(split_info));
  2489. split_info.rpr = mfspr(SPRN_RPR);
  2490. split_info.pmmar = mfspr(SPRN_PMMAR);
  2491. split_info.ldbar = mfspr(SPRN_LDBAR);
  2492. split_info.subcore_size = subcore_size;
  2493. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2494. split_info.vc[sub] = core_info.vc[sub];
  2495. /* order writes to split_info before kvm_split_mode pointer */
  2496. smp_wmb();
  2497. }
  2498. for (thr = 0; thr < controlled_threads; ++thr)
  2499. paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
  2500. /* Initiate micro-threading (split-core) if required */
  2501. if (cmd_bit) {
  2502. unsigned long hid0 = mfspr(SPRN_HID0);
  2503. hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
  2504. mb();
  2505. mtspr(SPRN_HID0, hid0);
  2506. isync();
  2507. for (;;) {
  2508. hid0 = mfspr(SPRN_HID0);
  2509. if (hid0 & stat_bit)
  2510. break;
  2511. cpu_relax();
  2512. }
  2513. }
  2514. /* Start all the threads */
  2515. active = 0;
  2516. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2517. thr = subcore_thread_map[sub];
  2518. thr0_done = false;
  2519. active |= 1 << thr;
  2520. pvc = core_info.vc[sub];
  2521. pvc->pcpu = pcpu + thr;
  2522. for_each_runnable_thread(i, vcpu, pvc) {
  2523. kvmppc_start_thread(vcpu, pvc);
  2524. kvmppc_create_dtl_entry(vcpu, pvc);
  2525. trace_kvm_guest_enter(vcpu);
  2526. if (!vcpu->arch.ptid)
  2527. thr0_done = true;
  2528. active |= 1 << (thr + vcpu->arch.ptid);
  2529. }
  2530. /*
  2531. * We need to start the first thread of each subcore
  2532. * even if it doesn't have a vcpu.
  2533. */
  2534. if (!thr0_done)
  2535. kvmppc_start_thread(NULL, pvc);
  2536. thr += pvc->num_threads;
  2537. }
  2538. /*
  2539. * Ensure that split_info.do_nap is set after setting
  2540. * the vcore pointer in the PACA of the secondaries.
  2541. */
  2542. smp_mb();
  2543. if (cmd_bit)
  2544. split_info.do_nap = 1; /* ask secondaries to nap when done */
  2545. /*
  2546. * When doing micro-threading, poke the inactive threads as well.
  2547. * This gets them to the nap instruction after kvm_do_nap,
  2548. * which reduces the time taken to unsplit later.
  2549. */
  2550. if (split > 1)
  2551. for (thr = 1; thr < threads_per_subcore; ++thr)
  2552. if (!(active & (1 << thr)))
  2553. kvmppc_ipi_thread(pcpu + thr);
  2554. vc->vcore_state = VCORE_RUNNING;
  2555. preempt_disable();
  2556. trace_kvmppc_run_core(vc, 0);
  2557. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2558. spin_unlock(&core_info.vc[sub]->lock);
  2559. /*
  2560. * Interrupts will be enabled once we get into the guest,
  2561. * so tell lockdep that we're about to enable interrupts.
  2562. */
  2563. trace_hardirqs_on();
  2564. guest_enter();
  2565. srcu_idx = srcu_read_lock(&vc->kvm->srcu);
  2566. trap = __kvmppc_vcore_entry();
  2567. srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
  2568. guest_exit();
  2569. trace_hardirqs_off();
  2570. set_irq_happened(trap);
  2571. spin_lock(&vc->lock);
  2572. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  2573. vc->vcore_state = VCORE_EXITING;
  2574. /* wait for secondary threads to finish writing their state to memory */
  2575. kvmppc_wait_for_nap();
  2576. /* Return to whole-core mode if we split the core earlier */
  2577. if (split > 1) {
  2578. unsigned long hid0 = mfspr(SPRN_HID0);
  2579. unsigned long loops = 0;
  2580. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  2581. stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  2582. mb();
  2583. mtspr(SPRN_HID0, hid0);
  2584. isync();
  2585. for (;;) {
  2586. hid0 = mfspr(SPRN_HID0);
  2587. if (!(hid0 & stat_bit))
  2588. break;
  2589. cpu_relax();
  2590. ++loops;
  2591. }
  2592. split_info.do_nap = 0;
  2593. }
  2594. kvmppc_set_host_core(pcpu);
  2595. local_irq_enable();
  2596. /* Let secondaries go back to the offline loop */
  2597. for (i = 0; i < controlled_threads; ++i) {
  2598. kvmppc_release_hwthread(pcpu + i);
  2599. if (sip && sip->napped[i])
  2600. kvmppc_ipi_thread(pcpu + i);
  2601. cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
  2602. }
  2603. spin_unlock(&vc->lock);
  2604. /* make sure updates to secondary vcpu structs are visible now */
  2605. smp_mb();
  2606. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2607. pvc = core_info.vc[sub];
  2608. post_guest_process(pvc, pvc == vc);
  2609. }
  2610. spin_lock(&vc->lock);
  2611. preempt_enable();
  2612. out:
  2613. vc->vcore_state = VCORE_INACTIVE;
  2614. trace_kvmppc_run_core(vc, 1);
  2615. }
  2616. /*
  2617. * Wait for some other vcpu thread to execute us, and
  2618. * wake us up when we need to handle something in the host.
  2619. */
  2620. static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
  2621. struct kvm_vcpu *vcpu, int wait_state)
  2622. {
  2623. DEFINE_WAIT(wait);
  2624. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  2625. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  2626. spin_unlock(&vc->lock);
  2627. schedule();
  2628. spin_lock(&vc->lock);
  2629. }
  2630. finish_wait(&vcpu->arch.cpu_run, &wait);
  2631. }
  2632. static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
  2633. {
  2634. /* 10us base */
  2635. if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
  2636. vc->halt_poll_ns = 10000;
  2637. else
  2638. vc->halt_poll_ns *= halt_poll_ns_grow;
  2639. }
  2640. static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
  2641. {
  2642. if (halt_poll_ns_shrink == 0)
  2643. vc->halt_poll_ns = 0;
  2644. else
  2645. vc->halt_poll_ns /= halt_poll_ns_shrink;
  2646. }
  2647. #ifdef CONFIG_KVM_XICS
  2648. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  2649. {
  2650. if (!xive_enabled())
  2651. return false;
  2652. return vcpu->arch.xive_saved_state.pipr <
  2653. vcpu->arch.xive_saved_state.cppr;
  2654. }
  2655. #else
  2656. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  2657. {
  2658. return false;
  2659. }
  2660. #endif /* CONFIG_KVM_XICS */
  2661. static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
  2662. {
  2663. if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
  2664. kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
  2665. return true;
  2666. return false;
  2667. }
  2668. /*
  2669. * Check to see if any of the runnable vcpus on the vcore have pending
  2670. * exceptions or are no longer ceded
  2671. */
  2672. static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  2673. {
  2674. struct kvm_vcpu *vcpu;
  2675. int i;
  2676. for_each_runnable_thread(i, vcpu, vc) {
  2677. if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
  2678. return 1;
  2679. }
  2680. return 0;
  2681. }
  2682. /*
  2683. * All the vcpus in this vcore are idle, so wait for a decrementer
  2684. * or external interrupt to one of the vcpus. vc->lock is held.
  2685. */
  2686. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  2687. {
  2688. ktime_t cur, start_poll, start_wait;
  2689. int do_sleep = 1;
  2690. u64 block_ns;
  2691. DECLARE_SWAITQUEUE(wait);
  2692. /* Poll for pending exceptions and ceded state */
  2693. cur = start_poll = ktime_get();
  2694. if (vc->halt_poll_ns) {
  2695. ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
  2696. ++vc->runner->stat.halt_attempted_poll;
  2697. vc->vcore_state = VCORE_POLLING;
  2698. spin_unlock(&vc->lock);
  2699. do {
  2700. if (kvmppc_vcore_check_block(vc)) {
  2701. do_sleep = 0;
  2702. break;
  2703. }
  2704. cur = ktime_get();
  2705. } while (single_task_running() && ktime_before(cur, stop));
  2706. spin_lock(&vc->lock);
  2707. vc->vcore_state = VCORE_INACTIVE;
  2708. if (!do_sleep) {
  2709. ++vc->runner->stat.halt_successful_poll;
  2710. goto out;
  2711. }
  2712. }
  2713. prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2714. if (kvmppc_vcore_check_block(vc)) {
  2715. finish_swait(&vc->wq, &wait);
  2716. do_sleep = 0;
  2717. /* If we polled, count this as a successful poll */
  2718. if (vc->halt_poll_ns)
  2719. ++vc->runner->stat.halt_successful_poll;
  2720. goto out;
  2721. }
  2722. start_wait = ktime_get();
  2723. vc->vcore_state = VCORE_SLEEPING;
  2724. trace_kvmppc_vcore_blocked(vc, 0);
  2725. spin_unlock(&vc->lock);
  2726. schedule();
  2727. finish_swait(&vc->wq, &wait);
  2728. spin_lock(&vc->lock);
  2729. vc->vcore_state = VCORE_INACTIVE;
  2730. trace_kvmppc_vcore_blocked(vc, 1);
  2731. ++vc->runner->stat.halt_successful_wait;
  2732. cur = ktime_get();
  2733. out:
  2734. block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
  2735. /* Attribute wait time */
  2736. if (do_sleep) {
  2737. vc->runner->stat.halt_wait_ns +=
  2738. ktime_to_ns(cur) - ktime_to_ns(start_wait);
  2739. /* Attribute failed poll time */
  2740. if (vc->halt_poll_ns)
  2741. vc->runner->stat.halt_poll_fail_ns +=
  2742. ktime_to_ns(start_wait) -
  2743. ktime_to_ns(start_poll);
  2744. } else {
  2745. /* Attribute successful poll time */
  2746. if (vc->halt_poll_ns)
  2747. vc->runner->stat.halt_poll_success_ns +=
  2748. ktime_to_ns(cur) -
  2749. ktime_to_ns(start_poll);
  2750. }
  2751. /* Adjust poll time */
  2752. if (halt_poll_ns) {
  2753. if (block_ns <= vc->halt_poll_ns)
  2754. ;
  2755. /* We slept and blocked for longer than the max halt time */
  2756. else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
  2757. shrink_halt_poll_ns(vc);
  2758. /* We slept and our poll time is too small */
  2759. else if (vc->halt_poll_ns < halt_poll_ns &&
  2760. block_ns < halt_poll_ns)
  2761. grow_halt_poll_ns(vc);
  2762. if (vc->halt_poll_ns > halt_poll_ns)
  2763. vc->halt_poll_ns = halt_poll_ns;
  2764. } else
  2765. vc->halt_poll_ns = 0;
  2766. trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
  2767. }
  2768. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  2769. {
  2770. int n_ceded, i;
  2771. struct kvmppc_vcore *vc;
  2772. struct kvm_vcpu *v;
  2773. trace_kvmppc_run_vcpu_enter(vcpu);
  2774. kvm_run->exit_reason = 0;
  2775. vcpu->arch.ret = RESUME_GUEST;
  2776. vcpu->arch.trap = 0;
  2777. kvmppc_update_vpas(vcpu);
  2778. /*
  2779. * Synchronize with other threads in this virtual core
  2780. */
  2781. vc = vcpu->arch.vcore;
  2782. spin_lock(&vc->lock);
  2783. vcpu->arch.ceded = 0;
  2784. vcpu->arch.run_task = current;
  2785. vcpu->arch.kvm_run = kvm_run;
  2786. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  2787. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  2788. vcpu->arch.busy_preempt = TB_NIL;
  2789. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
  2790. ++vc->n_runnable;
  2791. /*
  2792. * This happens the first time this is called for a vcpu.
  2793. * If the vcore is already running, we may be able to start
  2794. * this thread straight away and have it join in.
  2795. */
  2796. if (!signal_pending(current)) {
  2797. if (vc->vcore_state == VCORE_PIGGYBACK) {
  2798. if (spin_trylock(&vc->lock)) {
  2799. if (vc->vcore_state == VCORE_RUNNING &&
  2800. !VCORE_IS_EXITING(vc)) {
  2801. kvmppc_create_dtl_entry(vcpu, vc);
  2802. kvmppc_start_thread(vcpu, vc);
  2803. trace_kvm_guest_enter(vcpu);
  2804. }
  2805. spin_unlock(&vc->lock);
  2806. }
  2807. } else if (vc->vcore_state == VCORE_RUNNING &&
  2808. !VCORE_IS_EXITING(vc)) {
  2809. kvmppc_create_dtl_entry(vcpu, vc);
  2810. kvmppc_start_thread(vcpu, vc);
  2811. trace_kvm_guest_enter(vcpu);
  2812. } else if (vc->vcore_state == VCORE_SLEEPING) {
  2813. swake_up(&vc->wq);
  2814. }
  2815. }
  2816. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  2817. !signal_pending(current)) {
  2818. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  2819. kvmppc_vcore_end_preempt(vc);
  2820. if (vc->vcore_state != VCORE_INACTIVE) {
  2821. kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
  2822. continue;
  2823. }
  2824. for_each_runnable_thread(i, v, vc) {
  2825. kvmppc_core_prepare_to_enter(v);
  2826. if (signal_pending(v->arch.run_task)) {
  2827. kvmppc_remove_runnable(vc, v);
  2828. v->stat.signal_exits++;
  2829. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  2830. v->arch.ret = -EINTR;
  2831. wake_up(&v->arch.cpu_run);
  2832. }
  2833. }
  2834. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  2835. break;
  2836. n_ceded = 0;
  2837. for_each_runnable_thread(i, v, vc) {
  2838. if (!kvmppc_vcpu_woken(v))
  2839. n_ceded += v->arch.ceded;
  2840. else
  2841. v->arch.ceded = 0;
  2842. }
  2843. vc->runner = vcpu;
  2844. if (n_ceded == vc->n_runnable) {
  2845. kvmppc_vcore_blocked(vc);
  2846. } else if (need_resched()) {
  2847. kvmppc_vcore_preempt(vc);
  2848. /* Let something else run */
  2849. cond_resched_lock(&vc->lock);
  2850. if (vc->vcore_state == VCORE_PREEMPT)
  2851. kvmppc_vcore_end_preempt(vc);
  2852. } else {
  2853. kvmppc_run_core(vc);
  2854. }
  2855. vc->runner = NULL;
  2856. }
  2857. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  2858. (vc->vcore_state == VCORE_RUNNING ||
  2859. vc->vcore_state == VCORE_EXITING ||
  2860. vc->vcore_state == VCORE_PIGGYBACK))
  2861. kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
  2862. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  2863. kvmppc_vcore_end_preempt(vc);
  2864. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  2865. kvmppc_remove_runnable(vc, vcpu);
  2866. vcpu->stat.signal_exits++;
  2867. kvm_run->exit_reason = KVM_EXIT_INTR;
  2868. vcpu->arch.ret = -EINTR;
  2869. }
  2870. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  2871. /* Wake up some vcpu to run the core */
  2872. i = -1;
  2873. v = next_runnable_thread(vc, &i);
  2874. wake_up(&v->arch.cpu_run);
  2875. }
  2876. trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
  2877. spin_unlock(&vc->lock);
  2878. return vcpu->arch.ret;
  2879. }
  2880. static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
  2881. {
  2882. int r;
  2883. int srcu_idx;
  2884. unsigned long ebb_regs[3] = {}; /* shut up GCC */
  2885. unsigned long user_tar = 0;
  2886. unsigned int user_vrsave;
  2887. if (!vcpu->arch.sane) {
  2888. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2889. return -EINVAL;
  2890. }
  2891. /*
  2892. * Don't allow entry with a suspended transaction, because
  2893. * the guest entry/exit code will lose it.
  2894. * If the guest has TM enabled, save away their TM-related SPRs
  2895. * (they will get restored by the TM unavailable interrupt).
  2896. */
  2897. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2898. if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
  2899. (current->thread.regs->msr & MSR_TM)) {
  2900. if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
  2901. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2902. run->fail_entry.hardware_entry_failure_reason = 0;
  2903. return -EINVAL;
  2904. }
  2905. /* Enable TM so we can read the TM SPRs */
  2906. mtmsr(mfmsr() | MSR_TM);
  2907. current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
  2908. current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
  2909. current->thread.tm_texasr = mfspr(SPRN_TEXASR);
  2910. current->thread.regs->msr &= ~MSR_TM;
  2911. }
  2912. #endif
  2913. kvmppc_core_prepare_to_enter(vcpu);
  2914. /* No need to go into the guest when all we'll do is come back out */
  2915. if (signal_pending(current)) {
  2916. run->exit_reason = KVM_EXIT_INTR;
  2917. return -EINTR;
  2918. }
  2919. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  2920. /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
  2921. smp_mb();
  2922. /* On the first time here, set up HTAB and VRMA */
  2923. if (!kvm_is_radix(vcpu->kvm) && !vcpu->kvm->arch.hpte_setup_done) {
  2924. r = kvmppc_hv_setup_htab_rma(vcpu);
  2925. if (r)
  2926. goto out;
  2927. }
  2928. flush_all_to_thread(current);
  2929. /* Save userspace EBB and other register values */
  2930. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  2931. ebb_regs[0] = mfspr(SPRN_EBBHR);
  2932. ebb_regs[1] = mfspr(SPRN_EBBRR);
  2933. ebb_regs[2] = mfspr(SPRN_BESCR);
  2934. user_tar = mfspr(SPRN_TAR);
  2935. }
  2936. user_vrsave = mfspr(SPRN_VRSAVE);
  2937. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  2938. vcpu->arch.pgdir = current->mm->pgd;
  2939. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  2940. do {
  2941. r = kvmppc_run_vcpu(run, vcpu);
  2942. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  2943. !(vcpu->arch.shregs.msr & MSR_PR)) {
  2944. trace_kvm_hcall_enter(vcpu);
  2945. r = kvmppc_pseries_do_hcall(vcpu);
  2946. trace_kvm_hcall_exit(vcpu, r);
  2947. kvmppc_core_prepare_to_enter(vcpu);
  2948. } else if (r == RESUME_PAGE_FAULT) {
  2949. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  2950. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  2951. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  2952. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  2953. } else if (r == RESUME_PASSTHROUGH) {
  2954. if (WARN_ON(xive_enabled()))
  2955. r = H_SUCCESS;
  2956. else
  2957. r = kvmppc_xics_rm_complete(vcpu, 0);
  2958. }
  2959. } while (is_kvmppc_resume_guest(r));
  2960. /* Restore userspace EBB and other register values */
  2961. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  2962. mtspr(SPRN_EBBHR, ebb_regs[0]);
  2963. mtspr(SPRN_EBBRR, ebb_regs[1]);
  2964. mtspr(SPRN_BESCR, ebb_regs[2]);
  2965. mtspr(SPRN_TAR, user_tar);
  2966. mtspr(SPRN_FSCR, current->thread.fscr);
  2967. }
  2968. mtspr(SPRN_VRSAVE, user_vrsave);
  2969. out:
  2970. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  2971. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  2972. return r;
  2973. }
  2974. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  2975. int linux_psize)
  2976. {
  2977. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  2978. if (!def->shift)
  2979. return;
  2980. (*sps)->page_shift = def->shift;
  2981. (*sps)->slb_enc = def->sllp;
  2982. (*sps)->enc[0].page_shift = def->shift;
  2983. (*sps)->enc[0].pte_enc = def->penc[linux_psize];
  2984. /*
  2985. * Add 16MB MPSS support if host supports it
  2986. */
  2987. if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
  2988. (*sps)->enc[1].page_shift = 24;
  2989. (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
  2990. }
  2991. (*sps)++;
  2992. }
  2993. static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
  2994. struct kvm_ppc_smmu_info *info)
  2995. {
  2996. struct kvm_ppc_one_seg_page_size *sps;
  2997. /*
  2998. * Since we don't yet support HPT guests on a radix host,
  2999. * return an error if the host uses radix.
  3000. */
  3001. if (radix_enabled())
  3002. return -EINVAL;
  3003. /*
  3004. * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
  3005. * POWER7 doesn't support keys for instruction accesses,
  3006. * POWER8 and POWER9 do.
  3007. */
  3008. info->data_keys = 32;
  3009. info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
  3010. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  3011. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  3012. info->flags |= KVM_PPC_1T_SEGMENTS;
  3013. info->slb_size = mmu_slb_size;
  3014. /* We only support these sizes for now, and no muti-size segments */
  3015. sps = &info->sps[0];
  3016. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  3017. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  3018. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  3019. return 0;
  3020. }
  3021. /*
  3022. * Get (and clear) the dirty memory log for a memory slot.
  3023. */
  3024. static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
  3025. struct kvm_dirty_log *log)
  3026. {
  3027. struct kvm_memslots *slots;
  3028. struct kvm_memory_slot *memslot;
  3029. int i, r;
  3030. unsigned long n;
  3031. unsigned long *buf;
  3032. struct kvm_vcpu *vcpu;
  3033. mutex_lock(&kvm->slots_lock);
  3034. r = -EINVAL;
  3035. if (log->slot >= KVM_USER_MEM_SLOTS)
  3036. goto out;
  3037. slots = kvm_memslots(kvm);
  3038. memslot = id_to_memslot(slots, log->slot);
  3039. r = -ENOENT;
  3040. if (!memslot->dirty_bitmap)
  3041. goto out;
  3042. /*
  3043. * Use second half of bitmap area because radix accumulates
  3044. * bits in the first half.
  3045. */
  3046. n = kvm_dirty_bitmap_bytes(memslot);
  3047. buf = memslot->dirty_bitmap + n / sizeof(long);
  3048. memset(buf, 0, n);
  3049. if (kvm_is_radix(kvm))
  3050. r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
  3051. else
  3052. r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
  3053. if (r)
  3054. goto out;
  3055. /* Harvest dirty bits from VPA and DTL updates */
  3056. /* Note: we never modify the SLB shadow buffer areas */
  3057. kvm_for_each_vcpu(i, vcpu, kvm) {
  3058. spin_lock(&vcpu->arch.vpa_update_lock);
  3059. kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
  3060. kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
  3061. spin_unlock(&vcpu->arch.vpa_update_lock);
  3062. }
  3063. r = -EFAULT;
  3064. if (copy_to_user(log->dirty_bitmap, buf, n))
  3065. goto out;
  3066. r = 0;
  3067. out:
  3068. mutex_unlock(&kvm->slots_lock);
  3069. return r;
  3070. }
  3071. static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
  3072. struct kvm_memory_slot *dont)
  3073. {
  3074. if (!dont || free->arch.rmap != dont->arch.rmap) {
  3075. vfree(free->arch.rmap);
  3076. free->arch.rmap = NULL;
  3077. }
  3078. }
  3079. static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
  3080. unsigned long npages)
  3081. {
  3082. /*
  3083. * For now, if radix_enabled() then we only support radix guests,
  3084. * and in that case we don't need the rmap array.
  3085. */
  3086. if (radix_enabled()) {
  3087. slot->arch.rmap = NULL;
  3088. return 0;
  3089. }
  3090. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  3091. if (!slot->arch.rmap)
  3092. return -ENOMEM;
  3093. return 0;
  3094. }
  3095. static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
  3096. struct kvm_memory_slot *memslot,
  3097. const struct kvm_userspace_memory_region *mem)
  3098. {
  3099. return 0;
  3100. }
  3101. static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
  3102. const struct kvm_userspace_memory_region *mem,
  3103. const struct kvm_memory_slot *old,
  3104. const struct kvm_memory_slot *new)
  3105. {
  3106. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  3107. struct kvm_memslots *slots;
  3108. struct kvm_memory_slot *memslot;
  3109. /*
  3110. * If we are making a new memslot, it might make
  3111. * some address that was previously cached as emulated
  3112. * MMIO be no longer emulated MMIO, so invalidate
  3113. * all the caches of emulated MMIO translations.
  3114. */
  3115. if (npages)
  3116. atomic64_inc(&kvm->arch.mmio_update);
  3117. if (npages && old->npages && !kvm_is_radix(kvm)) {
  3118. /*
  3119. * If modifying a memslot, reset all the rmap dirty bits.
  3120. * If this is a new memslot, we don't need to do anything
  3121. * since the rmap array starts out as all zeroes,
  3122. * i.e. no pages are dirty.
  3123. */
  3124. slots = kvm_memslots(kvm);
  3125. memslot = id_to_memslot(slots, mem->slot);
  3126. kvmppc_hv_get_dirty_log_hpt(kvm, memslot, NULL);
  3127. }
  3128. }
  3129. /*
  3130. * Update LPCR values in kvm->arch and in vcores.
  3131. * Caller must hold kvm->lock.
  3132. */
  3133. void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
  3134. {
  3135. long int i;
  3136. u32 cores_done = 0;
  3137. if ((kvm->arch.lpcr & mask) == lpcr)
  3138. return;
  3139. kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
  3140. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  3141. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  3142. if (!vc)
  3143. continue;
  3144. spin_lock(&vc->lock);
  3145. vc->lpcr = (vc->lpcr & ~mask) | lpcr;
  3146. spin_unlock(&vc->lock);
  3147. if (++cores_done >= kvm->arch.online_vcores)
  3148. break;
  3149. }
  3150. }
  3151. static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
  3152. {
  3153. return;
  3154. }
  3155. static void kvmppc_setup_partition_table(struct kvm *kvm)
  3156. {
  3157. unsigned long dw0, dw1;
  3158. if (!kvm_is_radix(kvm)) {
  3159. /* PS field - page size for VRMA */
  3160. dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
  3161. ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
  3162. /* HTABSIZE and HTABORG fields */
  3163. dw0 |= kvm->arch.sdr1;
  3164. /* Second dword as set by userspace */
  3165. dw1 = kvm->arch.process_table;
  3166. } else {
  3167. dw0 = PATB_HR | radix__get_tree_size() |
  3168. __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
  3169. dw1 = PATB_GR | kvm->arch.process_table;
  3170. }
  3171. mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1);
  3172. }
  3173. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  3174. {
  3175. int err = 0;
  3176. struct kvm *kvm = vcpu->kvm;
  3177. unsigned long hva;
  3178. struct kvm_memory_slot *memslot;
  3179. struct vm_area_struct *vma;
  3180. unsigned long lpcr = 0, senc;
  3181. unsigned long psize, porder;
  3182. int srcu_idx;
  3183. mutex_lock(&kvm->lock);
  3184. if (kvm->arch.hpte_setup_done)
  3185. goto out; /* another vcpu beat us to it */
  3186. /* Allocate hashed page table (if not done already) and reset it */
  3187. if (!kvm->arch.hpt.virt) {
  3188. int order = KVM_DEFAULT_HPT_ORDER;
  3189. struct kvm_hpt_info info;
  3190. err = kvmppc_allocate_hpt(&info, order);
  3191. /* If we get here, it means userspace didn't specify a
  3192. * size explicitly. So, try successively smaller
  3193. * sizes if the default failed. */
  3194. while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
  3195. err = kvmppc_allocate_hpt(&info, order);
  3196. if (err < 0) {
  3197. pr_err("KVM: Couldn't alloc HPT\n");
  3198. goto out;
  3199. }
  3200. kvmppc_set_hpt(kvm, &info);
  3201. }
  3202. /* Look up the memslot for guest physical address 0 */
  3203. srcu_idx = srcu_read_lock(&kvm->srcu);
  3204. memslot = gfn_to_memslot(kvm, 0);
  3205. /* We must have some memory at 0 by now */
  3206. err = -EINVAL;
  3207. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  3208. goto out_srcu;
  3209. /* Look up the VMA for the start of this memory slot */
  3210. hva = memslot->userspace_addr;
  3211. down_read(&current->mm->mmap_sem);
  3212. vma = find_vma(current->mm, hva);
  3213. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  3214. goto up_out;
  3215. psize = vma_kernel_pagesize(vma);
  3216. porder = __ilog2(psize);
  3217. up_read(&current->mm->mmap_sem);
  3218. /* We can handle 4k, 64k or 16M pages in the VRMA */
  3219. err = -EINVAL;
  3220. if (!(psize == 0x1000 || psize == 0x10000 ||
  3221. psize == 0x1000000))
  3222. goto out_srcu;
  3223. senc = slb_pgsize_encoding(psize);
  3224. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  3225. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  3226. /* Create HPTEs in the hash page table for the VRMA */
  3227. kvmppc_map_vrma(vcpu, memslot, porder);
  3228. /* Update VRMASD field in the LPCR */
  3229. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  3230. /* the -4 is to account for senc values starting at 0x10 */
  3231. lpcr = senc << (LPCR_VRMASD_SH - 4);
  3232. kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
  3233. } else {
  3234. kvmppc_setup_partition_table(kvm);
  3235. }
  3236. /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
  3237. smp_wmb();
  3238. kvm->arch.hpte_setup_done = 1;
  3239. err = 0;
  3240. out_srcu:
  3241. srcu_read_unlock(&kvm->srcu, srcu_idx);
  3242. out:
  3243. mutex_unlock(&kvm->lock);
  3244. return err;
  3245. up_out:
  3246. up_read(&current->mm->mmap_sem);
  3247. goto out_srcu;
  3248. }
  3249. #ifdef CONFIG_KVM_XICS
  3250. /*
  3251. * Allocate a per-core structure for managing state about which cores are
  3252. * running in the host versus the guest and for exchanging data between
  3253. * real mode KVM and CPU running in the host.
  3254. * This is only done for the first VM.
  3255. * The allocated structure stays even if all VMs have stopped.
  3256. * It is only freed when the kvm-hv module is unloaded.
  3257. * It's OK for this routine to fail, we just don't support host
  3258. * core operations like redirecting H_IPI wakeups.
  3259. */
  3260. void kvmppc_alloc_host_rm_ops(void)
  3261. {
  3262. struct kvmppc_host_rm_ops *ops;
  3263. unsigned long l_ops;
  3264. int cpu, core;
  3265. int size;
  3266. /* Not the first time here ? */
  3267. if (kvmppc_host_rm_ops_hv != NULL)
  3268. return;
  3269. ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
  3270. if (!ops)
  3271. return;
  3272. size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
  3273. ops->rm_core = kzalloc(size, GFP_KERNEL);
  3274. if (!ops->rm_core) {
  3275. kfree(ops);
  3276. return;
  3277. }
  3278. cpus_read_lock();
  3279. for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
  3280. if (!cpu_online(cpu))
  3281. continue;
  3282. core = cpu >> threads_shift;
  3283. ops->rm_core[core].rm_state.in_host = 1;
  3284. }
  3285. ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
  3286. /*
  3287. * Make the contents of the kvmppc_host_rm_ops structure visible
  3288. * to other CPUs before we assign it to the global variable.
  3289. * Do an atomic assignment (no locks used here), but if someone
  3290. * beats us to it, just free our copy and return.
  3291. */
  3292. smp_wmb();
  3293. l_ops = (unsigned long) ops;
  3294. if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
  3295. cpus_read_unlock();
  3296. kfree(ops->rm_core);
  3297. kfree(ops);
  3298. return;
  3299. }
  3300. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
  3301. "ppc/kvm_book3s:prepare",
  3302. kvmppc_set_host_core,
  3303. kvmppc_clear_host_core);
  3304. cpus_read_unlock();
  3305. }
  3306. void kvmppc_free_host_rm_ops(void)
  3307. {
  3308. if (kvmppc_host_rm_ops_hv) {
  3309. cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
  3310. kfree(kvmppc_host_rm_ops_hv->rm_core);
  3311. kfree(kvmppc_host_rm_ops_hv);
  3312. kvmppc_host_rm_ops_hv = NULL;
  3313. }
  3314. }
  3315. #endif
  3316. static int kvmppc_core_init_vm_hv(struct kvm *kvm)
  3317. {
  3318. unsigned long lpcr, lpid;
  3319. char buf[32];
  3320. int ret;
  3321. /* Allocate the guest's logical partition ID */
  3322. lpid = kvmppc_alloc_lpid();
  3323. if ((long)lpid < 0)
  3324. return -ENOMEM;
  3325. kvm->arch.lpid = lpid;
  3326. kvmppc_alloc_host_rm_ops();
  3327. /*
  3328. * Since we don't flush the TLB when tearing down a VM,
  3329. * and this lpid might have previously been used,
  3330. * make sure we flush on each core before running the new VM.
  3331. * On POWER9, the tlbie in mmu_partition_table_set_entry()
  3332. * does this flush for us.
  3333. */
  3334. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3335. cpumask_setall(&kvm->arch.need_tlb_flush);
  3336. /* Start out with the default set of hcalls enabled */
  3337. memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
  3338. sizeof(kvm->arch.enabled_hcalls));
  3339. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3340. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  3341. /* Init LPCR for virtual RMA mode */
  3342. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  3343. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  3344. lpcr &= LPCR_PECE | LPCR_LPES;
  3345. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  3346. LPCR_VPM0 | LPCR_VPM1;
  3347. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  3348. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  3349. /* On POWER8 turn on online bit to enable PURR/SPURR */
  3350. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  3351. lpcr |= LPCR_ONL;
  3352. /*
  3353. * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
  3354. * Set HVICE bit to enable hypervisor virtualization interrupts.
  3355. * Set HEIC to prevent OS interrupts to go to hypervisor (should
  3356. * be unnecessary but better safe than sorry in case we re-enable
  3357. * EE in HV mode with this LPCR still set)
  3358. */
  3359. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  3360. lpcr &= ~LPCR_VPM0;
  3361. lpcr |= LPCR_HVICE | LPCR_HEIC;
  3362. /*
  3363. * If xive is enabled, we route 0x500 interrupts directly
  3364. * to the guest.
  3365. */
  3366. if (xive_enabled())
  3367. lpcr |= LPCR_LPES;
  3368. }
  3369. /*
  3370. * For now, if the host uses radix, the guest must be radix.
  3371. */
  3372. if (radix_enabled()) {
  3373. kvm->arch.radix = 1;
  3374. lpcr &= ~LPCR_VPM1;
  3375. lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  3376. ret = kvmppc_init_vm_radix(kvm);
  3377. if (ret) {
  3378. kvmppc_free_lpid(kvm->arch.lpid);
  3379. return ret;
  3380. }
  3381. kvmppc_setup_partition_table(kvm);
  3382. }
  3383. kvm->arch.lpcr = lpcr;
  3384. /* Initialization for future HPT resizes */
  3385. kvm->arch.resize_hpt = NULL;
  3386. /*
  3387. * Work out how many sets the TLB has, for the use of
  3388. * the TLB invalidation loop in book3s_hv_rmhandlers.S.
  3389. */
  3390. if (kvm_is_radix(kvm))
  3391. kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
  3392. else if (cpu_has_feature(CPU_FTR_ARCH_300))
  3393. kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
  3394. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  3395. kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
  3396. else
  3397. kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
  3398. /*
  3399. * Track that we now have a HV mode VM active. This blocks secondary
  3400. * CPU threads from coming online.
  3401. * On POWER9, we only need to do this for HPT guests on a radix
  3402. * host, which is not yet supported.
  3403. */
  3404. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3405. kvm_hv_vm_activated();
  3406. /*
  3407. * Initialize smt_mode depending on processor.
  3408. * POWER8 and earlier have to use "strict" threading, where
  3409. * all vCPUs in a vcore have to run on the same (sub)core,
  3410. * whereas on POWER9 the threads can each run a different
  3411. * guest.
  3412. */
  3413. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3414. kvm->arch.smt_mode = threads_per_subcore;
  3415. else
  3416. kvm->arch.smt_mode = 1;
  3417. kvm->arch.emul_smt_mode = 1;
  3418. /*
  3419. * Create a debugfs directory for the VM
  3420. */
  3421. snprintf(buf, sizeof(buf), "vm%d", current->pid);
  3422. kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
  3423. if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  3424. kvmppc_mmu_debugfs_init(kvm);
  3425. return 0;
  3426. }
  3427. static void kvmppc_free_vcores(struct kvm *kvm)
  3428. {
  3429. long int i;
  3430. for (i = 0; i < KVM_MAX_VCORES; ++i)
  3431. kfree(kvm->arch.vcores[i]);
  3432. kvm->arch.online_vcores = 0;
  3433. }
  3434. static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
  3435. {
  3436. debugfs_remove_recursive(kvm->arch.debugfs_dir);
  3437. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3438. kvm_hv_vm_deactivated();
  3439. kvmppc_free_vcores(kvm);
  3440. kvmppc_free_lpid(kvm->arch.lpid);
  3441. if (kvm_is_radix(kvm))
  3442. kvmppc_free_radix(kvm);
  3443. else
  3444. kvmppc_free_hpt(&kvm->arch.hpt);
  3445. kvmppc_free_pimap(kvm);
  3446. }
  3447. /* We don't need to emulate any privileged instructions or dcbz */
  3448. static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  3449. unsigned int inst, int *advance)
  3450. {
  3451. return EMULATE_FAIL;
  3452. }
  3453. static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
  3454. ulong spr_val)
  3455. {
  3456. return EMULATE_FAIL;
  3457. }
  3458. static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
  3459. ulong *spr_val)
  3460. {
  3461. return EMULATE_FAIL;
  3462. }
  3463. static int kvmppc_core_check_processor_compat_hv(void)
  3464. {
  3465. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  3466. !cpu_has_feature(CPU_FTR_ARCH_206))
  3467. return -EIO;
  3468. return 0;
  3469. }
  3470. #ifdef CONFIG_KVM_XICS
  3471. void kvmppc_free_pimap(struct kvm *kvm)
  3472. {
  3473. kfree(kvm->arch.pimap);
  3474. }
  3475. static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
  3476. {
  3477. return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
  3478. }
  3479. static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3480. {
  3481. struct irq_desc *desc;
  3482. struct kvmppc_irq_map *irq_map;
  3483. struct kvmppc_passthru_irqmap *pimap;
  3484. struct irq_chip *chip;
  3485. int i, rc = 0;
  3486. if (!kvm_irq_bypass)
  3487. return 1;
  3488. desc = irq_to_desc(host_irq);
  3489. if (!desc)
  3490. return -EIO;
  3491. mutex_lock(&kvm->lock);
  3492. pimap = kvm->arch.pimap;
  3493. if (pimap == NULL) {
  3494. /* First call, allocate structure to hold IRQ map */
  3495. pimap = kvmppc_alloc_pimap();
  3496. if (pimap == NULL) {
  3497. mutex_unlock(&kvm->lock);
  3498. return -ENOMEM;
  3499. }
  3500. kvm->arch.pimap = pimap;
  3501. }
  3502. /*
  3503. * For now, we only support interrupts for which the EOI operation
  3504. * is an OPAL call followed by a write to XIRR, since that's
  3505. * what our real-mode EOI code does, or a XIVE interrupt
  3506. */
  3507. chip = irq_data_get_irq_chip(&desc->irq_data);
  3508. if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) {
  3509. pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
  3510. host_irq, guest_gsi);
  3511. mutex_unlock(&kvm->lock);
  3512. return -ENOENT;
  3513. }
  3514. /*
  3515. * See if we already have an entry for this guest IRQ number.
  3516. * If it's mapped to a hardware IRQ number, that's an error,
  3517. * otherwise re-use this entry.
  3518. */
  3519. for (i = 0; i < pimap->n_mapped; i++) {
  3520. if (guest_gsi == pimap->mapped[i].v_hwirq) {
  3521. if (pimap->mapped[i].r_hwirq) {
  3522. mutex_unlock(&kvm->lock);
  3523. return -EINVAL;
  3524. }
  3525. break;
  3526. }
  3527. }
  3528. if (i == KVMPPC_PIRQ_MAPPED) {
  3529. mutex_unlock(&kvm->lock);
  3530. return -EAGAIN; /* table is full */
  3531. }
  3532. irq_map = &pimap->mapped[i];
  3533. irq_map->v_hwirq = guest_gsi;
  3534. irq_map->desc = desc;
  3535. /*
  3536. * Order the above two stores before the next to serialize with
  3537. * the KVM real mode handler.
  3538. */
  3539. smp_wmb();
  3540. irq_map->r_hwirq = desc->irq_data.hwirq;
  3541. if (i == pimap->n_mapped)
  3542. pimap->n_mapped++;
  3543. if (xive_enabled())
  3544. rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc);
  3545. else
  3546. kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
  3547. if (rc)
  3548. irq_map->r_hwirq = 0;
  3549. mutex_unlock(&kvm->lock);
  3550. return 0;
  3551. }
  3552. static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3553. {
  3554. struct irq_desc *desc;
  3555. struct kvmppc_passthru_irqmap *pimap;
  3556. int i, rc = 0;
  3557. if (!kvm_irq_bypass)
  3558. return 0;
  3559. desc = irq_to_desc(host_irq);
  3560. if (!desc)
  3561. return -EIO;
  3562. mutex_lock(&kvm->lock);
  3563. if (!kvm->arch.pimap)
  3564. goto unlock;
  3565. pimap = kvm->arch.pimap;
  3566. for (i = 0; i < pimap->n_mapped; i++) {
  3567. if (guest_gsi == pimap->mapped[i].v_hwirq)
  3568. break;
  3569. }
  3570. if (i == pimap->n_mapped) {
  3571. mutex_unlock(&kvm->lock);
  3572. return -ENODEV;
  3573. }
  3574. if (xive_enabled())
  3575. rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc);
  3576. else
  3577. kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
  3578. /* invalidate the entry (what do do on error from the above ?) */
  3579. pimap->mapped[i].r_hwirq = 0;
  3580. /*
  3581. * We don't free this structure even when the count goes to
  3582. * zero. The structure is freed when we destroy the VM.
  3583. */
  3584. unlock:
  3585. mutex_unlock(&kvm->lock);
  3586. return rc;
  3587. }
  3588. static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
  3589. struct irq_bypass_producer *prod)
  3590. {
  3591. int ret = 0;
  3592. struct kvm_kernel_irqfd *irqfd =
  3593. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3594. irqfd->producer = prod;
  3595. ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3596. if (ret)
  3597. pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3598. prod->irq, irqfd->gsi, ret);
  3599. return ret;
  3600. }
  3601. static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
  3602. struct irq_bypass_producer *prod)
  3603. {
  3604. int ret;
  3605. struct kvm_kernel_irqfd *irqfd =
  3606. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3607. irqfd->producer = NULL;
  3608. /*
  3609. * When producer of consumer is unregistered, we change back to
  3610. * default external interrupt handling mode - KVM real mode
  3611. * will switch back to host.
  3612. */
  3613. ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3614. if (ret)
  3615. pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3616. prod->irq, irqfd->gsi, ret);
  3617. }
  3618. #endif
  3619. static long kvm_arch_vm_ioctl_hv(struct file *filp,
  3620. unsigned int ioctl, unsigned long arg)
  3621. {
  3622. struct kvm *kvm __maybe_unused = filp->private_data;
  3623. void __user *argp = (void __user *)arg;
  3624. long r;
  3625. switch (ioctl) {
  3626. case KVM_PPC_ALLOCATE_HTAB: {
  3627. u32 htab_order;
  3628. r = -EFAULT;
  3629. if (get_user(htab_order, (u32 __user *)argp))
  3630. break;
  3631. r = kvmppc_alloc_reset_hpt(kvm, htab_order);
  3632. if (r)
  3633. break;
  3634. r = 0;
  3635. break;
  3636. }
  3637. case KVM_PPC_GET_HTAB_FD: {
  3638. struct kvm_get_htab_fd ghf;
  3639. r = -EFAULT;
  3640. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  3641. break;
  3642. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  3643. break;
  3644. }
  3645. case KVM_PPC_RESIZE_HPT_PREPARE: {
  3646. struct kvm_ppc_resize_hpt rhpt;
  3647. r = -EFAULT;
  3648. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  3649. break;
  3650. r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
  3651. break;
  3652. }
  3653. case KVM_PPC_RESIZE_HPT_COMMIT: {
  3654. struct kvm_ppc_resize_hpt rhpt;
  3655. r = -EFAULT;
  3656. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  3657. break;
  3658. r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
  3659. break;
  3660. }
  3661. default:
  3662. r = -ENOTTY;
  3663. }
  3664. return r;
  3665. }
  3666. /*
  3667. * List of hcall numbers to enable by default.
  3668. * For compatibility with old userspace, we enable by default
  3669. * all hcalls that were implemented before the hcall-enabling
  3670. * facility was added. Note this list should not include H_RTAS.
  3671. */
  3672. static unsigned int default_hcall_list[] = {
  3673. H_REMOVE,
  3674. H_ENTER,
  3675. H_READ,
  3676. H_PROTECT,
  3677. H_BULK_REMOVE,
  3678. H_GET_TCE,
  3679. H_PUT_TCE,
  3680. H_SET_DABR,
  3681. H_SET_XDABR,
  3682. H_CEDE,
  3683. H_PROD,
  3684. H_CONFER,
  3685. H_REGISTER_VPA,
  3686. #ifdef CONFIG_KVM_XICS
  3687. H_EOI,
  3688. H_CPPR,
  3689. H_IPI,
  3690. H_IPOLL,
  3691. H_XIRR,
  3692. H_XIRR_X,
  3693. #endif
  3694. 0
  3695. };
  3696. static void init_default_hcalls(void)
  3697. {
  3698. int i;
  3699. unsigned int hcall;
  3700. for (i = 0; default_hcall_list[i]; ++i) {
  3701. hcall = default_hcall_list[i];
  3702. WARN_ON(!kvmppc_hcall_impl_hv(hcall));
  3703. __set_bit(hcall / 4, default_enabled_hcalls);
  3704. }
  3705. }
  3706. static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
  3707. {
  3708. unsigned long lpcr;
  3709. int radix;
  3710. /* If not on a POWER9, reject it */
  3711. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3712. return -ENODEV;
  3713. /* If any unknown flags set, reject it */
  3714. if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
  3715. return -EINVAL;
  3716. /* We can't change a guest to/from radix yet */
  3717. radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
  3718. if (radix != kvm_is_radix(kvm))
  3719. return -EINVAL;
  3720. /* GR (guest radix) bit in process_table field must match */
  3721. if (!!(cfg->process_table & PATB_GR) != radix)
  3722. return -EINVAL;
  3723. /* Process table size field must be reasonable, i.e. <= 24 */
  3724. if ((cfg->process_table & PRTS_MASK) > 24)
  3725. return -EINVAL;
  3726. mutex_lock(&kvm->lock);
  3727. kvm->arch.process_table = cfg->process_table;
  3728. kvmppc_setup_partition_table(kvm);
  3729. lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
  3730. kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
  3731. mutex_unlock(&kvm->lock);
  3732. return 0;
  3733. }
  3734. static struct kvmppc_ops kvm_ops_hv = {
  3735. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
  3736. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
  3737. .get_one_reg = kvmppc_get_one_reg_hv,
  3738. .set_one_reg = kvmppc_set_one_reg_hv,
  3739. .vcpu_load = kvmppc_core_vcpu_load_hv,
  3740. .vcpu_put = kvmppc_core_vcpu_put_hv,
  3741. .set_msr = kvmppc_set_msr_hv,
  3742. .vcpu_run = kvmppc_vcpu_run_hv,
  3743. .vcpu_create = kvmppc_core_vcpu_create_hv,
  3744. .vcpu_free = kvmppc_core_vcpu_free_hv,
  3745. .check_requests = kvmppc_core_check_requests_hv,
  3746. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
  3747. .flush_memslot = kvmppc_core_flush_memslot_hv,
  3748. .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
  3749. .commit_memory_region = kvmppc_core_commit_memory_region_hv,
  3750. .unmap_hva = kvm_unmap_hva_hv,
  3751. .unmap_hva_range = kvm_unmap_hva_range_hv,
  3752. .age_hva = kvm_age_hva_hv,
  3753. .test_age_hva = kvm_test_age_hva_hv,
  3754. .set_spte_hva = kvm_set_spte_hva_hv,
  3755. .mmu_destroy = kvmppc_mmu_destroy_hv,
  3756. .free_memslot = kvmppc_core_free_memslot_hv,
  3757. .create_memslot = kvmppc_core_create_memslot_hv,
  3758. .init_vm = kvmppc_core_init_vm_hv,
  3759. .destroy_vm = kvmppc_core_destroy_vm_hv,
  3760. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
  3761. .emulate_op = kvmppc_core_emulate_op_hv,
  3762. .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
  3763. .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
  3764. .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
  3765. .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
  3766. .hcall_implemented = kvmppc_hcall_impl_hv,
  3767. #ifdef CONFIG_KVM_XICS
  3768. .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
  3769. .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
  3770. #endif
  3771. .configure_mmu = kvmhv_configure_mmu,
  3772. .get_rmmu_info = kvmhv_get_rmmu_info,
  3773. .set_smt_mode = kvmhv_set_smt_mode,
  3774. };
  3775. static int kvm_init_subcore_bitmap(void)
  3776. {
  3777. int i, j;
  3778. int nr_cores = cpu_nr_cores();
  3779. struct sibling_subcore_state *sibling_subcore_state;
  3780. for (i = 0; i < nr_cores; i++) {
  3781. int first_cpu = i * threads_per_core;
  3782. int node = cpu_to_node(first_cpu);
  3783. /* Ignore if it is already allocated. */
  3784. if (paca[first_cpu].sibling_subcore_state)
  3785. continue;
  3786. sibling_subcore_state =
  3787. kmalloc_node(sizeof(struct sibling_subcore_state),
  3788. GFP_KERNEL, node);
  3789. if (!sibling_subcore_state)
  3790. return -ENOMEM;
  3791. memset(sibling_subcore_state, 0,
  3792. sizeof(struct sibling_subcore_state));
  3793. for (j = 0; j < threads_per_core; j++) {
  3794. int cpu = first_cpu + j;
  3795. paca[cpu].sibling_subcore_state = sibling_subcore_state;
  3796. }
  3797. }
  3798. return 0;
  3799. }
  3800. static int kvmppc_radix_possible(void)
  3801. {
  3802. return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
  3803. }
  3804. static int kvmppc_book3s_init_hv(void)
  3805. {
  3806. int r;
  3807. /*
  3808. * FIXME!! Do we need to check on all cpus ?
  3809. */
  3810. r = kvmppc_core_check_processor_compat_hv();
  3811. if (r < 0)
  3812. return -ENODEV;
  3813. r = kvm_init_subcore_bitmap();
  3814. if (r)
  3815. return r;
  3816. /*
  3817. * We need a way of accessing the XICS interrupt controller,
  3818. * either directly, via paca[cpu].kvm_hstate.xics_phys, or
  3819. * indirectly, via OPAL.
  3820. */
  3821. #ifdef CONFIG_SMP
  3822. if (!xive_enabled() && !local_paca->kvm_hstate.xics_phys) {
  3823. struct device_node *np;
  3824. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  3825. if (!np) {
  3826. pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
  3827. return -ENODEV;
  3828. }
  3829. }
  3830. #endif
  3831. kvm_ops_hv.owner = THIS_MODULE;
  3832. kvmppc_hv_ops = &kvm_ops_hv;
  3833. init_default_hcalls();
  3834. init_vcore_lists();
  3835. r = kvmppc_mmu_hv_init();
  3836. if (r)
  3837. return r;
  3838. if (kvmppc_radix_possible())
  3839. r = kvmppc_radix_init();
  3840. return r;
  3841. }
  3842. static void kvmppc_book3s_exit_hv(void)
  3843. {
  3844. kvmppc_free_host_rm_ops();
  3845. if (kvmppc_radix_possible())
  3846. kvmppc_radix_exit();
  3847. kvmppc_hv_ops = NULL;
  3848. }
  3849. module_init(kvmppc_book3s_init_hv);
  3850. module_exit(kvmppc_book3s_exit_hv);
  3851. MODULE_LICENSE("GPL");
  3852. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  3853. MODULE_ALIAS("devname:kvm");