book3s_hv.c 118 KB

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