book3s_hv.c 119 KB

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