book3s_hv.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626
  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 free_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. free_vcpu:
  1910. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1911. out:
  1912. return ERR_PTR(err);
  1913. }
  1914. static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
  1915. unsigned long flags)
  1916. {
  1917. int err;
  1918. int esmt = 0;
  1919. if (flags)
  1920. return -EINVAL;
  1921. if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
  1922. return -EINVAL;
  1923. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  1924. /*
  1925. * On POWER8 (or POWER7), the threading mode is "strict",
  1926. * so we pack smt_mode vcpus per vcore.
  1927. */
  1928. if (smt_mode > threads_per_subcore)
  1929. return -EINVAL;
  1930. } else {
  1931. /*
  1932. * On POWER9, the threading mode is "loose",
  1933. * so each vcpu gets its own vcore.
  1934. */
  1935. esmt = smt_mode;
  1936. smt_mode = 1;
  1937. }
  1938. mutex_lock(&kvm->lock);
  1939. err = -EBUSY;
  1940. if (!kvm->arch.online_vcores) {
  1941. kvm->arch.smt_mode = smt_mode;
  1942. kvm->arch.emul_smt_mode = esmt;
  1943. err = 0;
  1944. }
  1945. mutex_unlock(&kvm->lock);
  1946. return err;
  1947. }
  1948. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  1949. {
  1950. if (vpa->pinned_addr)
  1951. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  1952. vpa->dirty);
  1953. }
  1954. static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
  1955. {
  1956. spin_lock(&vcpu->arch.vpa_update_lock);
  1957. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  1958. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  1959. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  1960. spin_unlock(&vcpu->arch.vpa_update_lock);
  1961. kvm_vcpu_uninit(vcpu);
  1962. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1963. }
  1964. static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
  1965. {
  1966. /* Indicate we want to get back into the guest */
  1967. return 1;
  1968. }
  1969. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  1970. {
  1971. unsigned long dec_nsec, now;
  1972. now = get_tb();
  1973. if (now > vcpu->arch.dec_expires) {
  1974. /* decrementer has already gone negative */
  1975. kvmppc_core_queue_dec(vcpu);
  1976. kvmppc_core_prepare_to_enter(vcpu);
  1977. return;
  1978. }
  1979. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  1980. / tb_ticks_per_sec;
  1981. hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
  1982. vcpu->arch.timer_running = 1;
  1983. }
  1984. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  1985. {
  1986. vcpu->arch.ceded = 0;
  1987. if (vcpu->arch.timer_running) {
  1988. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1989. vcpu->arch.timer_running = 0;
  1990. }
  1991. }
  1992. extern int __kvmppc_vcore_entry(void);
  1993. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  1994. struct kvm_vcpu *vcpu)
  1995. {
  1996. u64 now;
  1997. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1998. return;
  1999. spin_lock_irq(&vcpu->arch.tbacct_lock);
  2000. now = mftb();
  2001. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  2002. vcpu->arch.stolen_logged;
  2003. vcpu->arch.busy_preempt = now;
  2004. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  2005. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  2006. --vc->n_runnable;
  2007. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
  2008. }
  2009. static int kvmppc_grab_hwthread(int cpu)
  2010. {
  2011. struct paca_struct *tpaca;
  2012. long timeout = 10000;
  2013. tpaca = paca_ptrs[cpu];
  2014. /* Ensure the thread won't go into the kernel if it wakes */
  2015. tpaca->kvm_hstate.kvm_vcpu = NULL;
  2016. tpaca->kvm_hstate.kvm_vcore = NULL;
  2017. tpaca->kvm_hstate.napping = 0;
  2018. smp_wmb();
  2019. tpaca->kvm_hstate.hwthread_req = 1;
  2020. /*
  2021. * If the thread is already executing in the kernel (e.g. handling
  2022. * a stray interrupt), wait for it to get back to nap mode.
  2023. * The smp_mb() is to ensure that our setting of hwthread_req
  2024. * is visible before we look at hwthread_state, so if this
  2025. * races with the code at system_reset_pSeries and the thread
  2026. * misses our setting of hwthread_req, we are sure to see its
  2027. * setting of hwthread_state, and vice versa.
  2028. */
  2029. smp_mb();
  2030. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  2031. if (--timeout <= 0) {
  2032. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  2033. return -EBUSY;
  2034. }
  2035. udelay(1);
  2036. }
  2037. return 0;
  2038. }
  2039. static void kvmppc_release_hwthread(int cpu)
  2040. {
  2041. struct paca_struct *tpaca;
  2042. tpaca = paca_ptrs[cpu];
  2043. tpaca->kvm_hstate.hwthread_req = 0;
  2044. tpaca->kvm_hstate.kvm_vcpu = NULL;
  2045. tpaca->kvm_hstate.kvm_vcore = NULL;
  2046. tpaca->kvm_hstate.kvm_split_mode = NULL;
  2047. }
  2048. static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
  2049. {
  2050. int i;
  2051. cpu = cpu_first_thread_sibling(cpu);
  2052. cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
  2053. /*
  2054. * Make sure setting of bit in need_tlb_flush precedes
  2055. * testing of cpu_in_guest bits. The matching barrier on
  2056. * the other side is the first smp_mb() in kvmppc_run_core().
  2057. */
  2058. smp_mb();
  2059. for (i = 0; i < threads_per_core; ++i)
  2060. if (cpumask_test_cpu(cpu + i, &kvm->arch.cpu_in_guest))
  2061. smp_call_function_single(cpu + i, do_nothing, NULL, 1);
  2062. }
  2063. static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
  2064. {
  2065. struct kvm *kvm = vcpu->kvm;
  2066. /*
  2067. * With radix, the guest can do TLB invalidations itself,
  2068. * and it could choose to use the local form (tlbiel) if
  2069. * it is invalidating a translation that has only ever been
  2070. * used on one vcpu. However, that doesn't mean it has
  2071. * only ever been used on one physical cpu, since vcpus
  2072. * can move around between pcpus. To cope with this, when
  2073. * a vcpu moves from one pcpu to another, we need to tell
  2074. * any vcpus running on the same core as this vcpu previously
  2075. * ran to flush the TLB. The TLB is shared between threads,
  2076. * so we use a single bit in .need_tlb_flush for all 4 threads.
  2077. */
  2078. if (vcpu->arch.prev_cpu != pcpu) {
  2079. if (vcpu->arch.prev_cpu >= 0 &&
  2080. cpu_first_thread_sibling(vcpu->arch.prev_cpu) !=
  2081. cpu_first_thread_sibling(pcpu))
  2082. radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu);
  2083. vcpu->arch.prev_cpu = pcpu;
  2084. }
  2085. }
  2086. static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
  2087. {
  2088. int cpu;
  2089. struct paca_struct *tpaca;
  2090. struct kvm *kvm = vc->kvm;
  2091. cpu = vc->pcpu;
  2092. if (vcpu) {
  2093. if (vcpu->arch.timer_running) {
  2094. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  2095. vcpu->arch.timer_running = 0;
  2096. }
  2097. cpu += vcpu->arch.ptid;
  2098. vcpu->cpu = vc->pcpu;
  2099. vcpu->arch.thread_cpu = cpu;
  2100. cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
  2101. }
  2102. tpaca = paca_ptrs[cpu];
  2103. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  2104. tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
  2105. tpaca->kvm_hstate.fake_suspend = 0;
  2106. /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
  2107. smp_wmb();
  2108. tpaca->kvm_hstate.kvm_vcore = vc;
  2109. if (cpu != smp_processor_id())
  2110. kvmppc_ipi_thread(cpu);
  2111. }
  2112. static void kvmppc_wait_for_nap(int n_threads)
  2113. {
  2114. int cpu = smp_processor_id();
  2115. int i, loops;
  2116. if (n_threads <= 1)
  2117. return;
  2118. for (loops = 0; loops < 1000000; ++loops) {
  2119. /*
  2120. * Check if all threads are finished.
  2121. * We set the vcore pointer when starting a thread
  2122. * and the thread clears it when finished, so we look
  2123. * for any threads that still have a non-NULL vcore ptr.
  2124. */
  2125. for (i = 1; i < n_threads; ++i)
  2126. if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
  2127. break;
  2128. if (i == n_threads) {
  2129. HMT_medium();
  2130. return;
  2131. }
  2132. HMT_low();
  2133. }
  2134. HMT_medium();
  2135. for (i = 1; i < n_threads; ++i)
  2136. if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
  2137. pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
  2138. }
  2139. /*
  2140. * Check that we are on thread 0 and that any other threads in
  2141. * this core are off-line. Then grab the threads so they can't
  2142. * enter the kernel.
  2143. */
  2144. static int on_primary_thread(void)
  2145. {
  2146. int cpu = smp_processor_id();
  2147. int thr;
  2148. /* Are we on a primary subcore? */
  2149. if (cpu_thread_in_subcore(cpu))
  2150. return 0;
  2151. thr = 0;
  2152. while (++thr < threads_per_subcore)
  2153. if (cpu_online(cpu + thr))
  2154. return 0;
  2155. /* Grab all hw threads so they can't go into the kernel */
  2156. for (thr = 1; thr < threads_per_subcore; ++thr) {
  2157. if (kvmppc_grab_hwthread(cpu + thr)) {
  2158. /* Couldn't grab one; let the others go */
  2159. do {
  2160. kvmppc_release_hwthread(cpu + thr);
  2161. } while (--thr > 0);
  2162. return 0;
  2163. }
  2164. }
  2165. return 1;
  2166. }
  2167. /*
  2168. * A list of virtual cores for each physical CPU.
  2169. * These are vcores that could run but their runner VCPU tasks are
  2170. * (or may be) preempted.
  2171. */
  2172. struct preempted_vcore_list {
  2173. struct list_head list;
  2174. spinlock_t lock;
  2175. };
  2176. static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
  2177. static void init_vcore_lists(void)
  2178. {
  2179. int cpu;
  2180. for_each_possible_cpu(cpu) {
  2181. struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
  2182. spin_lock_init(&lp->lock);
  2183. INIT_LIST_HEAD(&lp->list);
  2184. }
  2185. }
  2186. static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
  2187. {
  2188. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2189. vc->vcore_state = VCORE_PREEMPT;
  2190. vc->pcpu = smp_processor_id();
  2191. if (vc->num_threads < threads_per_vcore(vc->kvm)) {
  2192. spin_lock(&lp->lock);
  2193. list_add_tail(&vc->preempt_list, &lp->list);
  2194. spin_unlock(&lp->lock);
  2195. }
  2196. /* Start accumulating stolen time */
  2197. kvmppc_core_start_stolen(vc);
  2198. }
  2199. static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
  2200. {
  2201. struct preempted_vcore_list *lp;
  2202. kvmppc_core_end_stolen(vc);
  2203. if (!list_empty(&vc->preempt_list)) {
  2204. lp = &per_cpu(preempted_vcores, vc->pcpu);
  2205. spin_lock(&lp->lock);
  2206. list_del_init(&vc->preempt_list);
  2207. spin_unlock(&lp->lock);
  2208. }
  2209. vc->vcore_state = VCORE_INACTIVE;
  2210. }
  2211. /*
  2212. * This stores information about the virtual cores currently
  2213. * assigned to a physical core.
  2214. */
  2215. struct core_info {
  2216. int n_subcores;
  2217. int max_subcore_threads;
  2218. int total_threads;
  2219. int subcore_threads[MAX_SUBCORES];
  2220. struct kvmppc_vcore *vc[MAX_SUBCORES];
  2221. };
  2222. /*
  2223. * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
  2224. * respectively in 2-way micro-threading (split-core) mode on POWER8.
  2225. */
  2226. static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
  2227. static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
  2228. {
  2229. memset(cip, 0, sizeof(*cip));
  2230. cip->n_subcores = 1;
  2231. cip->max_subcore_threads = vc->num_threads;
  2232. cip->total_threads = vc->num_threads;
  2233. cip->subcore_threads[0] = vc->num_threads;
  2234. cip->vc[0] = vc;
  2235. }
  2236. static bool subcore_config_ok(int n_subcores, int n_threads)
  2237. {
  2238. /*
  2239. * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
  2240. * split-core mode, with one thread per subcore.
  2241. */
  2242. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2243. return n_subcores <= 4 && n_threads == 1;
  2244. /* On POWER8, can only dynamically split if unsplit to begin with */
  2245. if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
  2246. return false;
  2247. if (n_subcores > MAX_SUBCORES)
  2248. return false;
  2249. if (n_subcores > 1) {
  2250. if (!(dynamic_mt_modes & 2))
  2251. n_subcores = 4;
  2252. if (n_subcores > 2 && !(dynamic_mt_modes & 4))
  2253. return false;
  2254. }
  2255. return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
  2256. }
  2257. static void init_vcore_to_run(struct kvmppc_vcore *vc)
  2258. {
  2259. vc->entry_exit_map = 0;
  2260. vc->in_guest = 0;
  2261. vc->napping_threads = 0;
  2262. vc->conferring_threads = 0;
  2263. vc->tb_offset_applied = 0;
  2264. }
  2265. static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
  2266. {
  2267. int n_threads = vc->num_threads;
  2268. int sub;
  2269. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  2270. return false;
  2271. /* Some POWER9 chips require all threads to be in the same MMU mode */
  2272. if (no_mixing_hpt_and_radix &&
  2273. kvm_is_radix(vc->kvm) != kvm_is_radix(cip->vc[0]->kvm))
  2274. return false;
  2275. if (n_threads < cip->max_subcore_threads)
  2276. n_threads = cip->max_subcore_threads;
  2277. if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
  2278. return false;
  2279. cip->max_subcore_threads = n_threads;
  2280. sub = cip->n_subcores;
  2281. ++cip->n_subcores;
  2282. cip->total_threads += vc->num_threads;
  2283. cip->subcore_threads[sub] = vc->num_threads;
  2284. cip->vc[sub] = vc;
  2285. init_vcore_to_run(vc);
  2286. list_del_init(&vc->preempt_list);
  2287. return true;
  2288. }
  2289. /*
  2290. * Work out whether it is possible to piggyback the execution of
  2291. * vcore *pvc onto the execution of the other vcores described in *cip.
  2292. */
  2293. static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
  2294. int target_threads)
  2295. {
  2296. if (cip->total_threads + pvc->num_threads > target_threads)
  2297. return false;
  2298. return can_dynamic_split(pvc, cip);
  2299. }
  2300. static void prepare_threads(struct kvmppc_vcore *vc)
  2301. {
  2302. int i;
  2303. struct kvm_vcpu *vcpu;
  2304. for_each_runnable_thread(i, vcpu, vc) {
  2305. if (signal_pending(vcpu->arch.run_task))
  2306. vcpu->arch.ret = -EINTR;
  2307. else if (vcpu->arch.vpa.update_pending ||
  2308. vcpu->arch.slb_shadow.update_pending ||
  2309. vcpu->arch.dtl.update_pending)
  2310. vcpu->arch.ret = RESUME_GUEST;
  2311. else
  2312. continue;
  2313. kvmppc_remove_runnable(vc, vcpu);
  2314. wake_up(&vcpu->arch.cpu_run);
  2315. }
  2316. }
  2317. static void collect_piggybacks(struct core_info *cip, int target_threads)
  2318. {
  2319. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2320. struct kvmppc_vcore *pvc, *vcnext;
  2321. spin_lock(&lp->lock);
  2322. list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
  2323. if (!spin_trylock(&pvc->lock))
  2324. continue;
  2325. prepare_threads(pvc);
  2326. if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
  2327. list_del_init(&pvc->preempt_list);
  2328. if (pvc->runner == NULL) {
  2329. pvc->vcore_state = VCORE_INACTIVE;
  2330. kvmppc_core_end_stolen(pvc);
  2331. }
  2332. spin_unlock(&pvc->lock);
  2333. continue;
  2334. }
  2335. if (!can_piggyback(pvc, cip, target_threads)) {
  2336. spin_unlock(&pvc->lock);
  2337. continue;
  2338. }
  2339. kvmppc_core_end_stolen(pvc);
  2340. pvc->vcore_state = VCORE_PIGGYBACK;
  2341. if (cip->total_threads >= target_threads)
  2342. break;
  2343. }
  2344. spin_unlock(&lp->lock);
  2345. }
  2346. static bool recheck_signals_and_mmu(struct core_info *cip)
  2347. {
  2348. int sub, i;
  2349. struct kvm_vcpu *vcpu;
  2350. struct kvmppc_vcore *vc;
  2351. for (sub = 0; sub < cip->n_subcores; ++sub) {
  2352. vc = cip->vc[sub];
  2353. if (!vc->kvm->arch.mmu_ready)
  2354. return true;
  2355. for_each_runnable_thread(i, vcpu, vc)
  2356. if (signal_pending(vcpu->arch.run_task))
  2357. return true;
  2358. }
  2359. return false;
  2360. }
  2361. static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
  2362. {
  2363. int still_running = 0, i;
  2364. u64 now;
  2365. long ret;
  2366. struct kvm_vcpu *vcpu;
  2367. spin_lock(&vc->lock);
  2368. now = get_tb();
  2369. for_each_runnable_thread(i, vcpu, vc) {
  2370. /* cancel pending dec exception if dec is positive */
  2371. if (now < vcpu->arch.dec_expires &&
  2372. kvmppc_core_pending_dec(vcpu))
  2373. kvmppc_core_dequeue_dec(vcpu);
  2374. trace_kvm_guest_exit(vcpu);
  2375. ret = RESUME_GUEST;
  2376. if (vcpu->arch.trap)
  2377. ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
  2378. vcpu->arch.run_task);
  2379. vcpu->arch.ret = ret;
  2380. vcpu->arch.trap = 0;
  2381. if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
  2382. if (vcpu->arch.pending_exceptions)
  2383. kvmppc_core_prepare_to_enter(vcpu);
  2384. if (vcpu->arch.ceded)
  2385. kvmppc_set_timer(vcpu);
  2386. else
  2387. ++still_running;
  2388. } else {
  2389. kvmppc_remove_runnable(vc, vcpu);
  2390. wake_up(&vcpu->arch.cpu_run);
  2391. }
  2392. }
  2393. if (!is_master) {
  2394. if (still_running > 0) {
  2395. kvmppc_vcore_preempt(vc);
  2396. } else if (vc->runner) {
  2397. vc->vcore_state = VCORE_PREEMPT;
  2398. kvmppc_core_start_stolen(vc);
  2399. } else {
  2400. vc->vcore_state = VCORE_INACTIVE;
  2401. }
  2402. if (vc->n_runnable > 0 && vc->runner == NULL) {
  2403. /* make sure there's a candidate runner awake */
  2404. i = -1;
  2405. vcpu = next_runnable_thread(vc, &i);
  2406. wake_up(&vcpu->arch.cpu_run);
  2407. }
  2408. }
  2409. spin_unlock(&vc->lock);
  2410. }
  2411. /*
  2412. * Clear core from the list of active host cores as we are about to
  2413. * enter the guest. Only do this if it is the primary thread of the
  2414. * core (not if a subcore) that is entering the guest.
  2415. */
  2416. static inline int kvmppc_clear_host_core(unsigned int cpu)
  2417. {
  2418. int core;
  2419. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2420. return 0;
  2421. /*
  2422. * Memory barrier can be omitted here as we will do a smp_wmb()
  2423. * later in kvmppc_start_thread and we need ensure that state is
  2424. * visible to other CPUs only after we enter guest.
  2425. */
  2426. core = cpu >> threads_shift;
  2427. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
  2428. return 0;
  2429. }
  2430. /*
  2431. * Advertise this core as an active host core since we exited the guest
  2432. * Only need to do this if it is the primary thread of the core that is
  2433. * exiting.
  2434. */
  2435. static inline int kvmppc_set_host_core(unsigned int cpu)
  2436. {
  2437. int core;
  2438. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2439. return 0;
  2440. /*
  2441. * Memory barrier can be omitted here because we do a spin_unlock
  2442. * immediately after this which provides the memory barrier.
  2443. */
  2444. core = cpu >> threads_shift;
  2445. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
  2446. return 0;
  2447. }
  2448. static void set_irq_happened(int trap)
  2449. {
  2450. switch (trap) {
  2451. case BOOK3S_INTERRUPT_EXTERNAL:
  2452. local_paca->irq_happened |= PACA_IRQ_EE;
  2453. break;
  2454. case BOOK3S_INTERRUPT_H_DOORBELL:
  2455. local_paca->irq_happened |= PACA_IRQ_DBELL;
  2456. break;
  2457. case BOOK3S_INTERRUPT_HMI:
  2458. local_paca->irq_happened |= PACA_IRQ_HMI;
  2459. break;
  2460. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  2461. replay_system_reset();
  2462. break;
  2463. }
  2464. }
  2465. /*
  2466. * Run a set of guest threads on a physical core.
  2467. * Called with vc->lock held.
  2468. */
  2469. static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
  2470. {
  2471. struct kvm_vcpu *vcpu;
  2472. int i;
  2473. int srcu_idx;
  2474. struct core_info core_info;
  2475. struct kvmppc_vcore *pvc;
  2476. struct kvm_split_mode split_info, *sip;
  2477. int split, subcore_size, active;
  2478. int sub;
  2479. bool thr0_done;
  2480. unsigned long cmd_bit, stat_bit;
  2481. int pcpu, thr;
  2482. int target_threads;
  2483. int controlled_threads;
  2484. int trap;
  2485. bool is_power8;
  2486. bool hpt_on_radix;
  2487. /*
  2488. * Remove from the list any threads that have a signal pending
  2489. * or need a VPA update done
  2490. */
  2491. prepare_threads(vc);
  2492. /* if the runner is no longer runnable, let the caller pick a new one */
  2493. if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
  2494. return;
  2495. /*
  2496. * Initialize *vc.
  2497. */
  2498. init_vcore_to_run(vc);
  2499. vc->preempt_tb = TB_NIL;
  2500. /*
  2501. * Number of threads that we will be controlling: the same as
  2502. * the number of threads per subcore, except on POWER9,
  2503. * where it's 1 because the threads are (mostly) independent.
  2504. */
  2505. controlled_threads = threads_per_vcore(vc->kvm);
  2506. /*
  2507. * Make sure we are running on primary threads, and that secondary
  2508. * threads are offline. Also check if the number of threads in this
  2509. * guest are greater than the current system threads per guest.
  2510. * On POWER9, we need to be not in independent-threads mode if
  2511. * this is a HPT guest on a radix host machine where the
  2512. * CPU threads may not be in different MMU modes.
  2513. */
  2514. hpt_on_radix = no_mixing_hpt_and_radix && radix_enabled() &&
  2515. !kvm_is_radix(vc->kvm);
  2516. if (((controlled_threads > 1) &&
  2517. ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) ||
  2518. (hpt_on_radix && vc->kvm->arch.threads_indep)) {
  2519. for_each_runnable_thread(i, vcpu, vc) {
  2520. vcpu->arch.ret = -EBUSY;
  2521. kvmppc_remove_runnable(vc, vcpu);
  2522. wake_up(&vcpu->arch.cpu_run);
  2523. }
  2524. goto out;
  2525. }
  2526. /*
  2527. * See if we could run any other vcores on the physical core
  2528. * along with this one.
  2529. */
  2530. init_core_info(&core_info, vc);
  2531. pcpu = smp_processor_id();
  2532. target_threads = controlled_threads;
  2533. if (target_smt_mode && target_smt_mode < target_threads)
  2534. target_threads = target_smt_mode;
  2535. if (vc->num_threads < target_threads)
  2536. collect_piggybacks(&core_info, target_threads);
  2537. /*
  2538. * On radix, arrange for TLB flushing if necessary.
  2539. * This has to be done before disabling interrupts since
  2540. * it uses smp_call_function().
  2541. */
  2542. pcpu = smp_processor_id();
  2543. if (kvm_is_radix(vc->kvm)) {
  2544. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2545. for_each_runnable_thread(i, vcpu, core_info.vc[sub])
  2546. kvmppc_prepare_radix_vcpu(vcpu, pcpu);
  2547. }
  2548. /*
  2549. * Hard-disable interrupts, and check resched flag and signals.
  2550. * If we need to reschedule or deliver a signal, clean up
  2551. * and return without going into the guest(s).
  2552. * If the mmu_ready flag has been cleared, don't go into the
  2553. * guest because that means a HPT resize operation is in progress.
  2554. */
  2555. local_irq_disable();
  2556. hard_irq_disable();
  2557. if (lazy_irq_pending() || need_resched() ||
  2558. recheck_signals_and_mmu(&core_info)) {
  2559. local_irq_enable();
  2560. vc->vcore_state = VCORE_INACTIVE;
  2561. /* Unlock all except the primary vcore */
  2562. for (sub = 1; sub < core_info.n_subcores; ++sub) {
  2563. pvc = core_info.vc[sub];
  2564. /* Put back on to the preempted vcores list */
  2565. kvmppc_vcore_preempt(pvc);
  2566. spin_unlock(&pvc->lock);
  2567. }
  2568. for (i = 0; i < controlled_threads; ++i)
  2569. kvmppc_release_hwthread(pcpu + i);
  2570. return;
  2571. }
  2572. kvmppc_clear_host_core(pcpu);
  2573. /* Decide on micro-threading (split-core) mode */
  2574. subcore_size = threads_per_subcore;
  2575. cmd_bit = stat_bit = 0;
  2576. split = core_info.n_subcores;
  2577. sip = NULL;
  2578. is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S)
  2579. && !cpu_has_feature(CPU_FTR_ARCH_300);
  2580. if (split > 1 || hpt_on_radix) {
  2581. sip = &split_info;
  2582. memset(&split_info, 0, sizeof(split_info));
  2583. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2584. split_info.vc[sub] = core_info.vc[sub];
  2585. if (is_power8) {
  2586. if (split == 2 && (dynamic_mt_modes & 2)) {
  2587. cmd_bit = HID0_POWER8_1TO2LPAR;
  2588. stat_bit = HID0_POWER8_2LPARMODE;
  2589. } else {
  2590. split = 4;
  2591. cmd_bit = HID0_POWER8_1TO4LPAR;
  2592. stat_bit = HID0_POWER8_4LPARMODE;
  2593. }
  2594. subcore_size = MAX_SMT_THREADS / split;
  2595. split_info.rpr = mfspr(SPRN_RPR);
  2596. split_info.pmmar = mfspr(SPRN_PMMAR);
  2597. split_info.ldbar = mfspr(SPRN_LDBAR);
  2598. split_info.subcore_size = subcore_size;
  2599. } else {
  2600. split_info.subcore_size = 1;
  2601. if (hpt_on_radix) {
  2602. /* Use the split_info for LPCR/LPIDR changes */
  2603. split_info.lpcr_req = vc->lpcr;
  2604. split_info.lpidr_req = vc->kvm->arch.lpid;
  2605. split_info.host_lpcr = vc->kvm->arch.host_lpcr;
  2606. split_info.do_set = 1;
  2607. }
  2608. }
  2609. /* order writes to split_info before kvm_split_mode pointer */
  2610. smp_wmb();
  2611. }
  2612. for (thr = 0; thr < controlled_threads; ++thr) {
  2613. struct paca_struct *paca = paca_ptrs[pcpu + thr];
  2614. paca->kvm_hstate.tid = thr;
  2615. paca->kvm_hstate.napping = 0;
  2616. paca->kvm_hstate.kvm_split_mode = sip;
  2617. }
  2618. /* Initiate micro-threading (split-core) on POWER8 if required */
  2619. if (cmd_bit) {
  2620. unsigned long hid0 = mfspr(SPRN_HID0);
  2621. hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
  2622. mb();
  2623. mtspr(SPRN_HID0, hid0);
  2624. isync();
  2625. for (;;) {
  2626. hid0 = mfspr(SPRN_HID0);
  2627. if (hid0 & stat_bit)
  2628. break;
  2629. cpu_relax();
  2630. }
  2631. }
  2632. /*
  2633. * On POWER8, set RWMR register.
  2634. * Since it only affects PURR and SPURR, it doesn't affect
  2635. * the host, so we don't save/restore the host value.
  2636. */
  2637. if (is_power8) {
  2638. unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
  2639. int n_online = atomic_read(&vc->online_count);
  2640. /*
  2641. * Use the 8-thread value if we're doing split-core
  2642. * or if the vcore's online count looks bogus.
  2643. */
  2644. if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
  2645. n_online >= 1 && n_online <= MAX_SMT_THREADS)
  2646. rwmr_val = p8_rwmr_values[n_online];
  2647. mtspr(SPRN_RWMR, rwmr_val);
  2648. }
  2649. /* Start all the threads */
  2650. active = 0;
  2651. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2652. thr = is_power8 ? subcore_thread_map[sub] : sub;
  2653. thr0_done = false;
  2654. active |= 1 << thr;
  2655. pvc = core_info.vc[sub];
  2656. pvc->pcpu = pcpu + thr;
  2657. for_each_runnable_thread(i, vcpu, pvc) {
  2658. kvmppc_start_thread(vcpu, pvc);
  2659. kvmppc_create_dtl_entry(vcpu, pvc);
  2660. trace_kvm_guest_enter(vcpu);
  2661. if (!vcpu->arch.ptid)
  2662. thr0_done = true;
  2663. active |= 1 << (thr + vcpu->arch.ptid);
  2664. }
  2665. /*
  2666. * We need to start the first thread of each subcore
  2667. * even if it doesn't have a vcpu.
  2668. */
  2669. if (!thr0_done)
  2670. kvmppc_start_thread(NULL, pvc);
  2671. }
  2672. /*
  2673. * Ensure that split_info.do_nap is set after setting
  2674. * the vcore pointer in the PACA of the secondaries.
  2675. */
  2676. smp_mb();
  2677. /*
  2678. * When doing micro-threading, poke the inactive threads as well.
  2679. * This gets them to the nap instruction after kvm_do_nap,
  2680. * which reduces the time taken to unsplit later.
  2681. * For POWER9 HPT guest on radix host, we need all the secondary
  2682. * threads woken up so they can do the LPCR/LPIDR change.
  2683. */
  2684. if (cmd_bit || hpt_on_radix) {
  2685. split_info.do_nap = 1; /* ask secondaries to nap when done */
  2686. for (thr = 1; thr < threads_per_subcore; ++thr)
  2687. if (!(active & (1 << thr)))
  2688. kvmppc_ipi_thread(pcpu + thr);
  2689. }
  2690. vc->vcore_state = VCORE_RUNNING;
  2691. preempt_disable();
  2692. trace_kvmppc_run_core(vc, 0);
  2693. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2694. spin_unlock(&core_info.vc[sub]->lock);
  2695. if (kvm_is_radix(vc->kvm)) {
  2696. int tmp = pcpu;
  2697. /*
  2698. * Do we need to flush the process scoped TLB for the LPAR?
  2699. *
  2700. * On POWER9, individual threads can come in here, but the
  2701. * TLB is shared between the 4 threads in a core, hence
  2702. * invalidating on one thread invalidates for all.
  2703. * Thus we make all 4 threads use the same bit here.
  2704. *
  2705. * Hash must be flushed in realmode in order to use tlbiel.
  2706. */
  2707. mtspr(SPRN_LPID, vc->kvm->arch.lpid);
  2708. isync();
  2709. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2710. tmp &= ~0x3UL;
  2711. if (cpumask_test_cpu(tmp, &vc->kvm->arch.need_tlb_flush)) {
  2712. radix__local_flush_tlb_lpid_guest(vc->kvm->arch.lpid);
  2713. /* Clear the bit after the TLB flush */
  2714. cpumask_clear_cpu(tmp, &vc->kvm->arch.need_tlb_flush);
  2715. }
  2716. }
  2717. /*
  2718. * Interrupts will be enabled once we get into the guest,
  2719. * so tell lockdep that we're about to enable interrupts.
  2720. */
  2721. trace_hardirqs_on();
  2722. guest_enter_irqoff();
  2723. srcu_idx = srcu_read_lock(&vc->kvm->srcu);
  2724. this_cpu_disable_ftrace();
  2725. trap = __kvmppc_vcore_entry();
  2726. this_cpu_enable_ftrace();
  2727. srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
  2728. trace_hardirqs_off();
  2729. set_irq_happened(trap);
  2730. spin_lock(&vc->lock);
  2731. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  2732. vc->vcore_state = VCORE_EXITING;
  2733. /* wait for secondary threads to finish writing their state to memory */
  2734. kvmppc_wait_for_nap(controlled_threads);
  2735. /* Return to whole-core mode if we split the core earlier */
  2736. if (cmd_bit) {
  2737. unsigned long hid0 = mfspr(SPRN_HID0);
  2738. unsigned long loops = 0;
  2739. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  2740. stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  2741. mb();
  2742. mtspr(SPRN_HID0, hid0);
  2743. isync();
  2744. for (;;) {
  2745. hid0 = mfspr(SPRN_HID0);
  2746. if (!(hid0 & stat_bit))
  2747. break;
  2748. cpu_relax();
  2749. ++loops;
  2750. }
  2751. } else if (hpt_on_radix) {
  2752. /* Wait for all threads to have seen final sync */
  2753. for (thr = 1; thr < controlled_threads; ++thr) {
  2754. struct paca_struct *paca = paca_ptrs[pcpu + thr];
  2755. while (paca->kvm_hstate.kvm_split_mode) {
  2756. HMT_low();
  2757. barrier();
  2758. }
  2759. HMT_medium();
  2760. }
  2761. }
  2762. split_info.do_nap = 0;
  2763. kvmppc_set_host_core(pcpu);
  2764. local_irq_enable();
  2765. guest_exit();
  2766. /* Let secondaries go back to the offline loop */
  2767. for (i = 0; i < controlled_threads; ++i) {
  2768. kvmppc_release_hwthread(pcpu + i);
  2769. if (sip && sip->napped[i])
  2770. kvmppc_ipi_thread(pcpu + i);
  2771. cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
  2772. }
  2773. spin_unlock(&vc->lock);
  2774. /* make sure updates to secondary vcpu structs are visible now */
  2775. smp_mb();
  2776. preempt_enable();
  2777. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2778. pvc = core_info.vc[sub];
  2779. post_guest_process(pvc, pvc == vc);
  2780. }
  2781. spin_lock(&vc->lock);
  2782. out:
  2783. vc->vcore_state = VCORE_INACTIVE;
  2784. trace_kvmppc_run_core(vc, 1);
  2785. }
  2786. /*
  2787. * Wait for some other vcpu thread to execute us, and
  2788. * wake us up when we need to handle something in the host.
  2789. */
  2790. static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
  2791. struct kvm_vcpu *vcpu, int wait_state)
  2792. {
  2793. DEFINE_WAIT(wait);
  2794. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  2795. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  2796. spin_unlock(&vc->lock);
  2797. schedule();
  2798. spin_lock(&vc->lock);
  2799. }
  2800. finish_wait(&vcpu->arch.cpu_run, &wait);
  2801. }
  2802. static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
  2803. {
  2804. /* 10us base */
  2805. if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
  2806. vc->halt_poll_ns = 10000;
  2807. else
  2808. vc->halt_poll_ns *= halt_poll_ns_grow;
  2809. }
  2810. static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
  2811. {
  2812. if (halt_poll_ns_shrink == 0)
  2813. vc->halt_poll_ns = 0;
  2814. else
  2815. vc->halt_poll_ns /= halt_poll_ns_shrink;
  2816. }
  2817. #ifdef CONFIG_KVM_XICS
  2818. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  2819. {
  2820. if (!xive_enabled())
  2821. return false;
  2822. return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
  2823. vcpu->arch.xive_saved_state.cppr;
  2824. }
  2825. #else
  2826. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  2827. {
  2828. return false;
  2829. }
  2830. #endif /* CONFIG_KVM_XICS */
  2831. static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
  2832. {
  2833. if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
  2834. kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
  2835. return true;
  2836. return false;
  2837. }
  2838. /*
  2839. * Check to see if any of the runnable vcpus on the vcore have pending
  2840. * exceptions or are no longer ceded
  2841. */
  2842. static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  2843. {
  2844. struct kvm_vcpu *vcpu;
  2845. int i;
  2846. for_each_runnable_thread(i, vcpu, vc) {
  2847. if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
  2848. return 1;
  2849. }
  2850. return 0;
  2851. }
  2852. /*
  2853. * All the vcpus in this vcore are idle, so wait for a decrementer
  2854. * or external interrupt to one of the vcpus. vc->lock is held.
  2855. */
  2856. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  2857. {
  2858. ktime_t cur, start_poll, start_wait;
  2859. int do_sleep = 1;
  2860. u64 block_ns;
  2861. DECLARE_SWAITQUEUE(wait);
  2862. /* Poll for pending exceptions and ceded state */
  2863. cur = start_poll = ktime_get();
  2864. if (vc->halt_poll_ns) {
  2865. ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
  2866. ++vc->runner->stat.halt_attempted_poll;
  2867. vc->vcore_state = VCORE_POLLING;
  2868. spin_unlock(&vc->lock);
  2869. do {
  2870. if (kvmppc_vcore_check_block(vc)) {
  2871. do_sleep = 0;
  2872. break;
  2873. }
  2874. cur = ktime_get();
  2875. } while (single_task_running() && ktime_before(cur, stop));
  2876. spin_lock(&vc->lock);
  2877. vc->vcore_state = VCORE_INACTIVE;
  2878. if (!do_sleep) {
  2879. ++vc->runner->stat.halt_successful_poll;
  2880. goto out;
  2881. }
  2882. }
  2883. prepare_to_swait_exclusive(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2884. if (kvmppc_vcore_check_block(vc)) {
  2885. finish_swait(&vc->wq, &wait);
  2886. do_sleep = 0;
  2887. /* If we polled, count this as a successful poll */
  2888. if (vc->halt_poll_ns)
  2889. ++vc->runner->stat.halt_successful_poll;
  2890. goto out;
  2891. }
  2892. start_wait = ktime_get();
  2893. vc->vcore_state = VCORE_SLEEPING;
  2894. trace_kvmppc_vcore_blocked(vc, 0);
  2895. spin_unlock(&vc->lock);
  2896. schedule();
  2897. finish_swait(&vc->wq, &wait);
  2898. spin_lock(&vc->lock);
  2899. vc->vcore_state = VCORE_INACTIVE;
  2900. trace_kvmppc_vcore_blocked(vc, 1);
  2901. ++vc->runner->stat.halt_successful_wait;
  2902. cur = ktime_get();
  2903. out:
  2904. block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
  2905. /* Attribute wait time */
  2906. if (do_sleep) {
  2907. vc->runner->stat.halt_wait_ns +=
  2908. ktime_to_ns(cur) - ktime_to_ns(start_wait);
  2909. /* Attribute failed poll time */
  2910. if (vc->halt_poll_ns)
  2911. vc->runner->stat.halt_poll_fail_ns +=
  2912. ktime_to_ns(start_wait) -
  2913. ktime_to_ns(start_poll);
  2914. } else {
  2915. /* Attribute successful poll time */
  2916. if (vc->halt_poll_ns)
  2917. vc->runner->stat.halt_poll_success_ns +=
  2918. ktime_to_ns(cur) -
  2919. ktime_to_ns(start_poll);
  2920. }
  2921. /* Adjust poll time */
  2922. if (halt_poll_ns) {
  2923. if (block_ns <= vc->halt_poll_ns)
  2924. ;
  2925. /* We slept and blocked for longer than the max halt time */
  2926. else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
  2927. shrink_halt_poll_ns(vc);
  2928. /* We slept and our poll time is too small */
  2929. else if (vc->halt_poll_ns < halt_poll_ns &&
  2930. block_ns < halt_poll_ns)
  2931. grow_halt_poll_ns(vc);
  2932. if (vc->halt_poll_ns > halt_poll_ns)
  2933. vc->halt_poll_ns = halt_poll_ns;
  2934. } else
  2935. vc->halt_poll_ns = 0;
  2936. trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
  2937. }
  2938. static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
  2939. {
  2940. int r = 0;
  2941. struct kvm *kvm = vcpu->kvm;
  2942. mutex_lock(&kvm->lock);
  2943. if (!kvm->arch.mmu_ready) {
  2944. if (!kvm_is_radix(kvm))
  2945. r = kvmppc_hv_setup_htab_rma(vcpu);
  2946. if (!r) {
  2947. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2948. kvmppc_setup_partition_table(kvm);
  2949. kvm->arch.mmu_ready = 1;
  2950. }
  2951. }
  2952. mutex_unlock(&kvm->lock);
  2953. return r;
  2954. }
  2955. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  2956. {
  2957. int n_ceded, i, r;
  2958. struct kvmppc_vcore *vc;
  2959. struct kvm_vcpu *v;
  2960. trace_kvmppc_run_vcpu_enter(vcpu);
  2961. kvm_run->exit_reason = 0;
  2962. vcpu->arch.ret = RESUME_GUEST;
  2963. vcpu->arch.trap = 0;
  2964. kvmppc_update_vpas(vcpu);
  2965. /*
  2966. * Synchronize with other threads in this virtual core
  2967. */
  2968. vc = vcpu->arch.vcore;
  2969. spin_lock(&vc->lock);
  2970. vcpu->arch.ceded = 0;
  2971. vcpu->arch.run_task = current;
  2972. vcpu->arch.kvm_run = kvm_run;
  2973. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  2974. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  2975. vcpu->arch.busy_preempt = TB_NIL;
  2976. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
  2977. ++vc->n_runnable;
  2978. /*
  2979. * This happens the first time this is called for a vcpu.
  2980. * If the vcore is already running, we may be able to start
  2981. * this thread straight away and have it join in.
  2982. */
  2983. if (!signal_pending(current)) {
  2984. if ((vc->vcore_state == VCORE_PIGGYBACK ||
  2985. vc->vcore_state == VCORE_RUNNING) &&
  2986. !VCORE_IS_EXITING(vc)) {
  2987. kvmppc_create_dtl_entry(vcpu, vc);
  2988. kvmppc_start_thread(vcpu, vc);
  2989. trace_kvm_guest_enter(vcpu);
  2990. } else if (vc->vcore_state == VCORE_SLEEPING) {
  2991. swake_up_one(&vc->wq);
  2992. }
  2993. }
  2994. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  2995. !signal_pending(current)) {
  2996. /* See if the MMU is ready to go */
  2997. if (!vcpu->kvm->arch.mmu_ready) {
  2998. spin_unlock(&vc->lock);
  2999. r = kvmhv_setup_mmu(vcpu);
  3000. spin_lock(&vc->lock);
  3001. if (r) {
  3002. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3003. kvm_run->fail_entry.
  3004. hardware_entry_failure_reason = 0;
  3005. vcpu->arch.ret = r;
  3006. break;
  3007. }
  3008. }
  3009. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  3010. kvmppc_vcore_end_preempt(vc);
  3011. if (vc->vcore_state != VCORE_INACTIVE) {
  3012. kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
  3013. continue;
  3014. }
  3015. for_each_runnable_thread(i, v, vc) {
  3016. kvmppc_core_prepare_to_enter(v);
  3017. if (signal_pending(v->arch.run_task)) {
  3018. kvmppc_remove_runnable(vc, v);
  3019. v->stat.signal_exits++;
  3020. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  3021. v->arch.ret = -EINTR;
  3022. wake_up(&v->arch.cpu_run);
  3023. }
  3024. }
  3025. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  3026. break;
  3027. n_ceded = 0;
  3028. for_each_runnable_thread(i, v, vc) {
  3029. if (!kvmppc_vcpu_woken(v))
  3030. n_ceded += v->arch.ceded;
  3031. else
  3032. v->arch.ceded = 0;
  3033. }
  3034. vc->runner = vcpu;
  3035. if (n_ceded == vc->n_runnable) {
  3036. kvmppc_vcore_blocked(vc);
  3037. } else if (need_resched()) {
  3038. kvmppc_vcore_preempt(vc);
  3039. /* Let something else run */
  3040. cond_resched_lock(&vc->lock);
  3041. if (vc->vcore_state == VCORE_PREEMPT)
  3042. kvmppc_vcore_end_preempt(vc);
  3043. } else {
  3044. kvmppc_run_core(vc);
  3045. }
  3046. vc->runner = NULL;
  3047. }
  3048. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  3049. (vc->vcore_state == VCORE_RUNNING ||
  3050. vc->vcore_state == VCORE_EXITING ||
  3051. vc->vcore_state == VCORE_PIGGYBACK))
  3052. kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
  3053. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  3054. kvmppc_vcore_end_preempt(vc);
  3055. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  3056. kvmppc_remove_runnable(vc, vcpu);
  3057. vcpu->stat.signal_exits++;
  3058. kvm_run->exit_reason = KVM_EXIT_INTR;
  3059. vcpu->arch.ret = -EINTR;
  3060. }
  3061. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  3062. /* Wake up some vcpu to run the core */
  3063. i = -1;
  3064. v = next_runnable_thread(vc, &i);
  3065. wake_up(&v->arch.cpu_run);
  3066. }
  3067. trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
  3068. spin_unlock(&vc->lock);
  3069. return vcpu->arch.ret;
  3070. }
  3071. static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
  3072. {
  3073. int r;
  3074. int srcu_idx;
  3075. unsigned long ebb_regs[3] = {}; /* shut up GCC */
  3076. unsigned long user_tar = 0;
  3077. unsigned int user_vrsave;
  3078. struct kvm *kvm;
  3079. if (!vcpu->arch.sane) {
  3080. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3081. return -EINVAL;
  3082. }
  3083. /*
  3084. * Don't allow entry with a suspended transaction, because
  3085. * the guest entry/exit code will lose it.
  3086. * If the guest has TM enabled, save away their TM-related SPRs
  3087. * (they will get restored by the TM unavailable interrupt).
  3088. */
  3089. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  3090. if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
  3091. (current->thread.regs->msr & MSR_TM)) {
  3092. if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
  3093. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3094. run->fail_entry.hardware_entry_failure_reason = 0;
  3095. return -EINVAL;
  3096. }
  3097. /* Enable TM so we can read the TM SPRs */
  3098. mtmsr(mfmsr() | MSR_TM);
  3099. current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
  3100. current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
  3101. current->thread.tm_texasr = mfspr(SPRN_TEXASR);
  3102. current->thread.regs->msr &= ~MSR_TM;
  3103. }
  3104. #endif
  3105. /*
  3106. * Force online to 1 for the sake of old userspace which doesn't
  3107. * set it.
  3108. */
  3109. if (!vcpu->arch.online) {
  3110. atomic_inc(&vcpu->arch.vcore->online_count);
  3111. vcpu->arch.online = 1;
  3112. }
  3113. kvmppc_core_prepare_to_enter(vcpu);
  3114. /* No need to go into the guest when all we'll do is come back out */
  3115. if (signal_pending(current)) {
  3116. run->exit_reason = KVM_EXIT_INTR;
  3117. return -EINTR;
  3118. }
  3119. kvm = vcpu->kvm;
  3120. atomic_inc(&kvm->arch.vcpus_running);
  3121. /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
  3122. smp_mb();
  3123. flush_all_to_thread(current);
  3124. /* Save userspace EBB and other register values */
  3125. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  3126. ebb_regs[0] = mfspr(SPRN_EBBHR);
  3127. ebb_regs[1] = mfspr(SPRN_EBBRR);
  3128. ebb_regs[2] = mfspr(SPRN_BESCR);
  3129. user_tar = mfspr(SPRN_TAR);
  3130. }
  3131. user_vrsave = mfspr(SPRN_VRSAVE);
  3132. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  3133. vcpu->arch.pgdir = current->mm->pgd;
  3134. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  3135. do {
  3136. r = kvmppc_run_vcpu(run, vcpu);
  3137. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  3138. !(vcpu->arch.shregs.msr & MSR_PR)) {
  3139. trace_kvm_hcall_enter(vcpu);
  3140. r = kvmppc_pseries_do_hcall(vcpu);
  3141. trace_kvm_hcall_exit(vcpu, r);
  3142. kvmppc_core_prepare_to_enter(vcpu);
  3143. } else if (r == RESUME_PAGE_FAULT) {
  3144. srcu_idx = srcu_read_lock(&kvm->srcu);
  3145. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  3146. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  3147. srcu_read_unlock(&kvm->srcu, srcu_idx);
  3148. } else if (r == RESUME_PASSTHROUGH) {
  3149. if (WARN_ON(xive_enabled()))
  3150. r = H_SUCCESS;
  3151. else
  3152. r = kvmppc_xics_rm_complete(vcpu, 0);
  3153. }
  3154. } while (is_kvmppc_resume_guest(r));
  3155. /* Restore userspace EBB and other register values */
  3156. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  3157. mtspr(SPRN_EBBHR, ebb_regs[0]);
  3158. mtspr(SPRN_EBBRR, ebb_regs[1]);
  3159. mtspr(SPRN_BESCR, ebb_regs[2]);
  3160. mtspr(SPRN_TAR, user_tar);
  3161. mtspr(SPRN_FSCR, current->thread.fscr);
  3162. }
  3163. mtspr(SPRN_VRSAVE, user_vrsave);
  3164. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  3165. atomic_dec(&kvm->arch.vcpus_running);
  3166. return r;
  3167. }
  3168. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  3169. int shift, int sllp)
  3170. {
  3171. (*sps)->page_shift = shift;
  3172. (*sps)->slb_enc = sllp;
  3173. (*sps)->enc[0].page_shift = shift;
  3174. (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
  3175. /*
  3176. * Add 16MB MPSS support (may get filtered out by userspace)
  3177. */
  3178. if (shift != 24) {
  3179. int penc = kvmppc_pgsize_lp_encoding(shift, 24);
  3180. if (penc != -1) {
  3181. (*sps)->enc[1].page_shift = 24;
  3182. (*sps)->enc[1].pte_enc = penc;
  3183. }
  3184. }
  3185. (*sps)++;
  3186. }
  3187. static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
  3188. struct kvm_ppc_smmu_info *info)
  3189. {
  3190. struct kvm_ppc_one_seg_page_size *sps;
  3191. /*
  3192. * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
  3193. * POWER7 doesn't support keys for instruction accesses,
  3194. * POWER8 and POWER9 do.
  3195. */
  3196. info->data_keys = 32;
  3197. info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
  3198. /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
  3199. info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
  3200. info->slb_size = 32;
  3201. /* We only support these sizes for now, and no muti-size segments */
  3202. sps = &info->sps[0];
  3203. kvmppc_add_seg_page_size(&sps, 12, 0);
  3204. kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
  3205. kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
  3206. return 0;
  3207. }
  3208. /*
  3209. * Get (and clear) the dirty memory log for a memory slot.
  3210. */
  3211. static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
  3212. struct kvm_dirty_log *log)
  3213. {
  3214. struct kvm_memslots *slots;
  3215. struct kvm_memory_slot *memslot;
  3216. int i, r;
  3217. unsigned long n;
  3218. unsigned long *buf, *p;
  3219. struct kvm_vcpu *vcpu;
  3220. mutex_lock(&kvm->slots_lock);
  3221. r = -EINVAL;
  3222. if (log->slot >= KVM_USER_MEM_SLOTS)
  3223. goto out;
  3224. slots = kvm_memslots(kvm);
  3225. memslot = id_to_memslot(slots, log->slot);
  3226. r = -ENOENT;
  3227. if (!memslot->dirty_bitmap)
  3228. goto out;
  3229. /*
  3230. * Use second half of bitmap area because both HPT and radix
  3231. * accumulate bits in the first half.
  3232. */
  3233. n = kvm_dirty_bitmap_bytes(memslot);
  3234. buf = memslot->dirty_bitmap + n / sizeof(long);
  3235. memset(buf, 0, n);
  3236. if (kvm_is_radix(kvm))
  3237. r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
  3238. else
  3239. r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
  3240. if (r)
  3241. goto out;
  3242. /*
  3243. * We accumulate dirty bits in the first half of the
  3244. * memslot's dirty_bitmap area, for when pages are paged
  3245. * out or modified by the host directly. Pick up these
  3246. * bits and add them to the map.
  3247. */
  3248. p = memslot->dirty_bitmap;
  3249. for (i = 0; i < n / sizeof(long); ++i)
  3250. buf[i] |= xchg(&p[i], 0);
  3251. /* Harvest dirty bits from VPA and DTL updates */
  3252. /* Note: we never modify the SLB shadow buffer areas */
  3253. kvm_for_each_vcpu(i, vcpu, kvm) {
  3254. spin_lock(&vcpu->arch.vpa_update_lock);
  3255. kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
  3256. kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
  3257. spin_unlock(&vcpu->arch.vpa_update_lock);
  3258. }
  3259. r = -EFAULT;
  3260. if (copy_to_user(log->dirty_bitmap, buf, n))
  3261. goto out;
  3262. r = 0;
  3263. out:
  3264. mutex_unlock(&kvm->slots_lock);
  3265. return r;
  3266. }
  3267. static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
  3268. struct kvm_memory_slot *dont)
  3269. {
  3270. if (!dont || free->arch.rmap != dont->arch.rmap) {
  3271. vfree(free->arch.rmap);
  3272. free->arch.rmap = NULL;
  3273. }
  3274. }
  3275. static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
  3276. unsigned long npages)
  3277. {
  3278. slot->arch.rmap = vzalloc(array_size(npages, sizeof(*slot->arch.rmap)));
  3279. if (!slot->arch.rmap)
  3280. return -ENOMEM;
  3281. return 0;
  3282. }
  3283. static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
  3284. struct kvm_memory_slot *memslot,
  3285. const struct kvm_userspace_memory_region *mem)
  3286. {
  3287. return 0;
  3288. }
  3289. static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
  3290. const struct kvm_userspace_memory_region *mem,
  3291. const struct kvm_memory_slot *old,
  3292. const struct kvm_memory_slot *new)
  3293. {
  3294. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  3295. /*
  3296. * If we are making a new memslot, it might make
  3297. * some address that was previously cached as emulated
  3298. * MMIO be no longer emulated MMIO, so invalidate
  3299. * all the caches of emulated MMIO translations.
  3300. */
  3301. if (npages)
  3302. atomic64_inc(&kvm->arch.mmio_update);
  3303. }
  3304. /*
  3305. * Update LPCR values in kvm->arch and in vcores.
  3306. * Caller must hold kvm->lock.
  3307. */
  3308. void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
  3309. {
  3310. long int i;
  3311. u32 cores_done = 0;
  3312. if ((kvm->arch.lpcr & mask) == lpcr)
  3313. return;
  3314. kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
  3315. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  3316. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  3317. if (!vc)
  3318. continue;
  3319. spin_lock(&vc->lock);
  3320. vc->lpcr = (vc->lpcr & ~mask) | lpcr;
  3321. spin_unlock(&vc->lock);
  3322. if (++cores_done >= kvm->arch.online_vcores)
  3323. break;
  3324. }
  3325. }
  3326. static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
  3327. {
  3328. return;
  3329. }
  3330. void kvmppc_setup_partition_table(struct kvm *kvm)
  3331. {
  3332. unsigned long dw0, dw1;
  3333. if (!kvm_is_radix(kvm)) {
  3334. /* PS field - page size for VRMA */
  3335. dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
  3336. ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
  3337. /* HTABSIZE and HTABORG fields */
  3338. dw0 |= kvm->arch.sdr1;
  3339. /* Second dword as set by userspace */
  3340. dw1 = kvm->arch.process_table;
  3341. } else {
  3342. dw0 = PATB_HR | radix__get_tree_size() |
  3343. __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
  3344. dw1 = PATB_GR | kvm->arch.process_table;
  3345. }
  3346. mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1);
  3347. }
  3348. /*
  3349. * Set up HPT (hashed page table) and RMA (real-mode area).
  3350. * Must be called with kvm->lock held.
  3351. */
  3352. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  3353. {
  3354. int err = 0;
  3355. struct kvm *kvm = vcpu->kvm;
  3356. unsigned long hva;
  3357. struct kvm_memory_slot *memslot;
  3358. struct vm_area_struct *vma;
  3359. unsigned long lpcr = 0, senc;
  3360. unsigned long psize, porder;
  3361. int srcu_idx;
  3362. /* Allocate hashed page table (if not done already) and reset it */
  3363. if (!kvm->arch.hpt.virt) {
  3364. int order = KVM_DEFAULT_HPT_ORDER;
  3365. struct kvm_hpt_info info;
  3366. err = kvmppc_allocate_hpt(&info, order);
  3367. /* If we get here, it means userspace didn't specify a
  3368. * size explicitly. So, try successively smaller
  3369. * sizes if the default failed. */
  3370. while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
  3371. err = kvmppc_allocate_hpt(&info, order);
  3372. if (err < 0) {
  3373. pr_err("KVM: Couldn't alloc HPT\n");
  3374. goto out;
  3375. }
  3376. kvmppc_set_hpt(kvm, &info);
  3377. }
  3378. /* Look up the memslot for guest physical address 0 */
  3379. srcu_idx = srcu_read_lock(&kvm->srcu);
  3380. memslot = gfn_to_memslot(kvm, 0);
  3381. /* We must have some memory at 0 by now */
  3382. err = -EINVAL;
  3383. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  3384. goto out_srcu;
  3385. /* Look up the VMA for the start of this memory slot */
  3386. hva = memslot->userspace_addr;
  3387. down_read(&current->mm->mmap_sem);
  3388. vma = find_vma(current->mm, hva);
  3389. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  3390. goto up_out;
  3391. psize = vma_kernel_pagesize(vma);
  3392. up_read(&current->mm->mmap_sem);
  3393. /* We can handle 4k, 64k or 16M pages in the VRMA */
  3394. if (psize >= 0x1000000)
  3395. psize = 0x1000000;
  3396. else if (psize >= 0x10000)
  3397. psize = 0x10000;
  3398. else
  3399. psize = 0x1000;
  3400. porder = __ilog2(psize);
  3401. senc = slb_pgsize_encoding(psize);
  3402. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  3403. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  3404. /* Create HPTEs in the hash page table for the VRMA */
  3405. kvmppc_map_vrma(vcpu, memslot, porder);
  3406. /* Update VRMASD field in the LPCR */
  3407. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  3408. /* the -4 is to account for senc values starting at 0x10 */
  3409. lpcr = senc << (LPCR_VRMASD_SH - 4);
  3410. kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
  3411. }
  3412. /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
  3413. smp_wmb();
  3414. err = 0;
  3415. out_srcu:
  3416. srcu_read_unlock(&kvm->srcu, srcu_idx);
  3417. out:
  3418. return err;
  3419. up_out:
  3420. up_read(&current->mm->mmap_sem);
  3421. goto out_srcu;
  3422. }
  3423. /* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
  3424. int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
  3425. {
  3426. kvmppc_rmap_reset(kvm);
  3427. kvm->arch.process_table = 0;
  3428. /* Mutual exclusion with kvm_unmap_hva_range etc. */
  3429. spin_lock(&kvm->mmu_lock);
  3430. kvm->arch.radix = 0;
  3431. spin_unlock(&kvm->mmu_lock);
  3432. kvmppc_free_radix(kvm);
  3433. kvmppc_update_lpcr(kvm, LPCR_VPM1,
  3434. LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
  3435. return 0;
  3436. }
  3437. /* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
  3438. int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
  3439. {
  3440. int err;
  3441. err = kvmppc_init_vm_radix(kvm);
  3442. if (err)
  3443. return err;
  3444. kvmppc_rmap_reset(kvm);
  3445. /* Mutual exclusion with kvm_unmap_hva_range etc. */
  3446. spin_lock(&kvm->mmu_lock);
  3447. kvm->arch.radix = 1;
  3448. spin_unlock(&kvm->mmu_lock);
  3449. kvmppc_free_hpt(&kvm->arch.hpt);
  3450. kvmppc_update_lpcr(kvm, LPCR_UPRT | LPCR_GTSE | LPCR_HR,
  3451. LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
  3452. return 0;
  3453. }
  3454. #ifdef CONFIG_KVM_XICS
  3455. /*
  3456. * Allocate a per-core structure for managing state about which cores are
  3457. * running in the host versus the guest and for exchanging data between
  3458. * real mode KVM and CPU running in the host.
  3459. * This is only done for the first VM.
  3460. * The allocated structure stays even if all VMs have stopped.
  3461. * It is only freed when the kvm-hv module is unloaded.
  3462. * It's OK for this routine to fail, we just don't support host
  3463. * core operations like redirecting H_IPI wakeups.
  3464. */
  3465. void kvmppc_alloc_host_rm_ops(void)
  3466. {
  3467. struct kvmppc_host_rm_ops *ops;
  3468. unsigned long l_ops;
  3469. int cpu, core;
  3470. int size;
  3471. /* Not the first time here ? */
  3472. if (kvmppc_host_rm_ops_hv != NULL)
  3473. return;
  3474. ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
  3475. if (!ops)
  3476. return;
  3477. size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
  3478. ops->rm_core = kzalloc(size, GFP_KERNEL);
  3479. if (!ops->rm_core) {
  3480. kfree(ops);
  3481. return;
  3482. }
  3483. cpus_read_lock();
  3484. for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
  3485. if (!cpu_online(cpu))
  3486. continue;
  3487. core = cpu >> threads_shift;
  3488. ops->rm_core[core].rm_state.in_host = 1;
  3489. }
  3490. ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
  3491. /*
  3492. * Make the contents of the kvmppc_host_rm_ops structure visible
  3493. * to other CPUs before we assign it to the global variable.
  3494. * Do an atomic assignment (no locks used here), but if someone
  3495. * beats us to it, just free our copy and return.
  3496. */
  3497. smp_wmb();
  3498. l_ops = (unsigned long) ops;
  3499. if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
  3500. cpus_read_unlock();
  3501. kfree(ops->rm_core);
  3502. kfree(ops);
  3503. return;
  3504. }
  3505. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
  3506. "ppc/kvm_book3s:prepare",
  3507. kvmppc_set_host_core,
  3508. kvmppc_clear_host_core);
  3509. cpus_read_unlock();
  3510. }
  3511. void kvmppc_free_host_rm_ops(void)
  3512. {
  3513. if (kvmppc_host_rm_ops_hv) {
  3514. cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
  3515. kfree(kvmppc_host_rm_ops_hv->rm_core);
  3516. kfree(kvmppc_host_rm_ops_hv);
  3517. kvmppc_host_rm_ops_hv = NULL;
  3518. }
  3519. }
  3520. #endif
  3521. static int kvmppc_core_init_vm_hv(struct kvm *kvm)
  3522. {
  3523. unsigned long lpcr, lpid;
  3524. char buf[32];
  3525. int ret;
  3526. /* Allocate the guest's logical partition ID */
  3527. lpid = kvmppc_alloc_lpid();
  3528. if ((long)lpid < 0)
  3529. return -ENOMEM;
  3530. kvm->arch.lpid = lpid;
  3531. kvmppc_alloc_host_rm_ops();
  3532. /*
  3533. * Since we don't flush the TLB when tearing down a VM,
  3534. * and this lpid might have previously been used,
  3535. * make sure we flush on each core before running the new VM.
  3536. * On POWER9, the tlbie in mmu_partition_table_set_entry()
  3537. * does this flush for us.
  3538. */
  3539. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3540. cpumask_setall(&kvm->arch.need_tlb_flush);
  3541. /* Start out with the default set of hcalls enabled */
  3542. memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
  3543. sizeof(kvm->arch.enabled_hcalls));
  3544. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3545. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  3546. /* Init LPCR for virtual RMA mode */
  3547. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  3548. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  3549. lpcr &= LPCR_PECE | LPCR_LPES;
  3550. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  3551. LPCR_VPM0 | LPCR_VPM1;
  3552. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  3553. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  3554. /* On POWER8 turn on online bit to enable PURR/SPURR */
  3555. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  3556. lpcr |= LPCR_ONL;
  3557. /*
  3558. * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
  3559. * Set HVICE bit to enable hypervisor virtualization interrupts.
  3560. * Set HEIC to prevent OS interrupts to go to hypervisor (should
  3561. * be unnecessary but better safe than sorry in case we re-enable
  3562. * EE in HV mode with this LPCR still set)
  3563. */
  3564. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  3565. lpcr &= ~LPCR_VPM0;
  3566. lpcr |= LPCR_HVICE | LPCR_HEIC;
  3567. /*
  3568. * If xive is enabled, we route 0x500 interrupts directly
  3569. * to the guest.
  3570. */
  3571. if (xive_enabled())
  3572. lpcr |= LPCR_LPES;
  3573. }
  3574. /*
  3575. * If the host uses radix, the guest starts out as radix.
  3576. */
  3577. if (radix_enabled()) {
  3578. kvm->arch.radix = 1;
  3579. kvm->arch.mmu_ready = 1;
  3580. lpcr &= ~LPCR_VPM1;
  3581. lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  3582. ret = kvmppc_init_vm_radix(kvm);
  3583. if (ret) {
  3584. kvmppc_free_lpid(kvm->arch.lpid);
  3585. return ret;
  3586. }
  3587. kvmppc_setup_partition_table(kvm);
  3588. }
  3589. kvm->arch.lpcr = lpcr;
  3590. /* Initialization for future HPT resizes */
  3591. kvm->arch.resize_hpt = NULL;
  3592. /*
  3593. * Work out how many sets the TLB has, for the use of
  3594. * the TLB invalidation loop in book3s_hv_rmhandlers.S.
  3595. */
  3596. if (radix_enabled())
  3597. kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
  3598. else if (cpu_has_feature(CPU_FTR_ARCH_300))
  3599. kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
  3600. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  3601. kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
  3602. else
  3603. kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
  3604. /*
  3605. * Track that we now have a HV mode VM active. This blocks secondary
  3606. * CPU threads from coming online.
  3607. * On POWER9, we only need to do this if the "indep_threads_mode"
  3608. * module parameter has been set to N.
  3609. */
  3610. if (cpu_has_feature(CPU_FTR_ARCH_300))
  3611. kvm->arch.threads_indep = indep_threads_mode;
  3612. if (!kvm->arch.threads_indep)
  3613. kvm_hv_vm_activated();
  3614. /*
  3615. * Initialize smt_mode depending on processor.
  3616. * POWER8 and earlier have to use "strict" threading, where
  3617. * all vCPUs in a vcore have to run on the same (sub)core,
  3618. * whereas on POWER9 the threads can each run a different
  3619. * guest.
  3620. */
  3621. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3622. kvm->arch.smt_mode = threads_per_subcore;
  3623. else
  3624. kvm->arch.smt_mode = 1;
  3625. kvm->arch.emul_smt_mode = 1;
  3626. /*
  3627. * Create a debugfs directory for the VM
  3628. */
  3629. snprintf(buf, sizeof(buf), "vm%d", current->pid);
  3630. kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
  3631. kvmppc_mmu_debugfs_init(kvm);
  3632. return 0;
  3633. }
  3634. static void kvmppc_free_vcores(struct kvm *kvm)
  3635. {
  3636. long int i;
  3637. for (i = 0; i < KVM_MAX_VCORES; ++i)
  3638. kfree(kvm->arch.vcores[i]);
  3639. kvm->arch.online_vcores = 0;
  3640. }
  3641. static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
  3642. {
  3643. debugfs_remove_recursive(kvm->arch.debugfs_dir);
  3644. if (!kvm->arch.threads_indep)
  3645. kvm_hv_vm_deactivated();
  3646. kvmppc_free_vcores(kvm);
  3647. kvmppc_free_lpid(kvm->arch.lpid);
  3648. if (kvm_is_radix(kvm))
  3649. kvmppc_free_radix(kvm);
  3650. else
  3651. kvmppc_free_hpt(&kvm->arch.hpt);
  3652. kvmppc_free_pimap(kvm);
  3653. }
  3654. /* We don't need to emulate any privileged instructions or dcbz */
  3655. static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  3656. unsigned int inst, int *advance)
  3657. {
  3658. return EMULATE_FAIL;
  3659. }
  3660. static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
  3661. ulong spr_val)
  3662. {
  3663. return EMULATE_FAIL;
  3664. }
  3665. static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
  3666. ulong *spr_val)
  3667. {
  3668. return EMULATE_FAIL;
  3669. }
  3670. static int kvmppc_core_check_processor_compat_hv(void)
  3671. {
  3672. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  3673. !cpu_has_feature(CPU_FTR_ARCH_206))
  3674. return -EIO;
  3675. return 0;
  3676. }
  3677. #ifdef CONFIG_KVM_XICS
  3678. void kvmppc_free_pimap(struct kvm *kvm)
  3679. {
  3680. kfree(kvm->arch.pimap);
  3681. }
  3682. static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
  3683. {
  3684. return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
  3685. }
  3686. static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3687. {
  3688. struct irq_desc *desc;
  3689. struct kvmppc_irq_map *irq_map;
  3690. struct kvmppc_passthru_irqmap *pimap;
  3691. struct irq_chip *chip;
  3692. int i, rc = 0;
  3693. if (!kvm_irq_bypass)
  3694. return 1;
  3695. desc = irq_to_desc(host_irq);
  3696. if (!desc)
  3697. return -EIO;
  3698. mutex_lock(&kvm->lock);
  3699. pimap = kvm->arch.pimap;
  3700. if (pimap == NULL) {
  3701. /* First call, allocate structure to hold IRQ map */
  3702. pimap = kvmppc_alloc_pimap();
  3703. if (pimap == NULL) {
  3704. mutex_unlock(&kvm->lock);
  3705. return -ENOMEM;
  3706. }
  3707. kvm->arch.pimap = pimap;
  3708. }
  3709. /*
  3710. * For now, we only support interrupts for which the EOI operation
  3711. * is an OPAL call followed by a write to XIRR, since that's
  3712. * what our real-mode EOI code does, or a XIVE interrupt
  3713. */
  3714. chip = irq_data_get_irq_chip(&desc->irq_data);
  3715. if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) {
  3716. pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
  3717. host_irq, guest_gsi);
  3718. mutex_unlock(&kvm->lock);
  3719. return -ENOENT;
  3720. }
  3721. /*
  3722. * See if we already have an entry for this guest IRQ number.
  3723. * If it's mapped to a hardware IRQ number, that's an error,
  3724. * otherwise re-use this entry.
  3725. */
  3726. for (i = 0; i < pimap->n_mapped; i++) {
  3727. if (guest_gsi == pimap->mapped[i].v_hwirq) {
  3728. if (pimap->mapped[i].r_hwirq) {
  3729. mutex_unlock(&kvm->lock);
  3730. return -EINVAL;
  3731. }
  3732. break;
  3733. }
  3734. }
  3735. if (i == KVMPPC_PIRQ_MAPPED) {
  3736. mutex_unlock(&kvm->lock);
  3737. return -EAGAIN; /* table is full */
  3738. }
  3739. irq_map = &pimap->mapped[i];
  3740. irq_map->v_hwirq = guest_gsi;
  3741. irq_map->desc = desc;
  3742. /*
  3743. * Order the above two stores before the next to serialize with
  3744. * the KVM real mode handler.
  3745. */
  3746. smp_wmb();
  3747. irq_map->r_hwirq = desc->irq_data.hwirq;
  3748. if (i == pimap->n_mapped)
  3749. pimap->n_mapped++;
  3750. if (xive_enabled())
  3751. rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc);
  3752. else
  3753. kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
  3754. if (rc)
  3755. irq_map->r_hwirq = 0;
  3756. mutex_unlock(&kvm->lock);
  3757. return 0;
  3758. }
  3759. static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3760. {
  3761. struct irq_desc *desc;
  3762. struct kvmppc_passthru_irqmap *pimap;
  3763. int i, rc = 0;
  3764. if (!kvm_irq_bypass)
  3765. return 0;
  3766. desc = irq_to_desc(host_irq);
  3767. if (!desc)
  3768. return -EIO;
  3769. mutex_lock(&kvm->lock);
  3770. if (!kvm->arch.pimap)
  3771. goto unlock;
  3772. pimap = kvm->arch.pimap;
  3773. for (i = 0; i < pimap->n_mapped; i++) {
  3774. if (guest_gsi == pimap->mapped[i].v_hwirq)
  3775. break;
  3776. }
  3777. if (i == pimap->n_mapped) {
  3778. mutex_unlock(&kvm->lock);
  3779. return -ENODEV;
  3780. }
  3781. if (xive_enabled())
  3782. rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc);
  3783. else
  3784. kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
  3785. /* invalidate the entry (what do do on error from the above ?) */
  3786. pimap->mapped[i].r_hwirq = 0;
  3787. /*
  3788. * We don't free this structure even when the count goes to
  3789. * zero. The structure is freed when we destroy the VM.
  3790. */
  3791. unlock:
  3792. mutex_unlock(&kvm->lock);
  3793. return rc;
  3794. }
  3795. static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
  3796. struct irq_bypass_producer *prod)
  3797. {
  3798. int ret = 0;
  3799. struct kvm_kernel_irqfd *irqfd =
  3800. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3801. irqfd->producer = prod;
  3802. ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3803. if (ret)
  3804. pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3805. prod->irq, irqfd->gsi, ret);
  3806. return ret;
  3807. }
  3808. static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
  3809. struct irq_bypass_producer *prod)
  3810. {
  3811. int ret;
  3812. struct kvm_kernel_irqfd *irqfd =
  3813. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3814. irqfd->producer = NULL;
  3815. /*
  3816. * When producer of consumer is unregistered, we change back to
  3817. * default external interrupt handling mode - KVM real mode
  3818. * will switch back to host.
  3819. */
  3820. ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3821. if (ret)
  3822. pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3823. prod->irq, irqfd->gsi, ret);
  3824. }
  3825. #endif
  3826. static long kvm_arch_vm_ioctl_hv(struct file *filp,
  3827. unsigned int ioctl, unsigned long arg)
  3828. {
  3829. struct kvm *kvm __maybe_unused = filp->private_data;
  3830. void __user *argp = (void __user *)arg;
  3831. long r;
  3832. switch (ioctl) {
  3833. case KVM_PPC_ALLOCATE_HTAB: {
  3834. u32 htab_order;
  3835. r = -EFAULT;
  3836. if (get_user(htab_order, (u32 __user *)argp))
  3837. break;
  3838. r = kvmppc_alloc_reset_hpt(kvm, htab_order);
  3839. if (r)
  3840. break;
  3841. r = 0;
  3842. break;
  3843. }
  3844. case KVM_PPC_GET_HTAB_FD: {
  3845. struct kvm_get_htab_fd ghf;
  3846. r = -EFAULT;
  3847. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  3848. break;
  3849. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  3850. break;
  3851. }
  3852. case KVM_PPC_RESIZE_HPT_PREPARE: {
  3853. struct kvm_ppc_resize_hpt rhpt;
  3854. r = -EFAULT;
  3855. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  3856. break;
  3857. r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
  3858. break;
  3859. }
  3860. case KVM_PPC_RESIZE_HPT_COMMIT: {
  3861. struct kvm_ppc_resize_hpt rhpt;
  3862. r = -EFAULT;
  3863. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  3864. break;
  3865. r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
  3866. break;
  3867. }
  3868. default:
  3869. r = -ENOTTY;
  3870. }
  3871. return r;
  3872. }
  3873. /*
  3874. * List of hcall numbers to enable by default.
  3875. * For compatibility with old userspace, we enable by default
  3876. * all hcalls that were implemented before the hcall-enabling
  3877. * facility was added. Note this list should not include H_RTAS.
  3878. */
  3879. static unsigned int default_hcall_list[] = {
  3880. H_REMOVE,
  3881. H_ENTER,
  3882. H_READ,
  3883. H_PROTECT,
  3884. H_BULK_REMOVE,
  3885. H_GET_TCE,
  3886. H_PUT_TCE,
  3887. H_SET_DABR,
  3888. H_SET_XDABR,
  3889. H_CEDE,
  3890. H_PROD,
  3891. H_CONFER,
  3892. H_REGISTER_VPA,
  3893. #ifdef CONFIG_KVM_XICS
  3894. H_EOI,
  3895. H_CPPR,
  3896. H_IPI,
  3897. H_IPOLL,
  3898. H_XIRR,
  3899. H_XIRR_X,
  3900. #endif
  3901. 0
  3902. };
  3903. static void init_default_hcalls(void)
  3904. {
  3905. int i;
  3906. unsigned int hcall;
  3907. for (i = 0; default_hcall_list[i]; ++i) {
  3908. hcall = default_hcall_list[i];
  3909. WARN_ON(!kvmppc_hcall_impl_hv(hcall));
  3910. __set_bit(hcall / 4, default_enabled_hcalls);
  3911. }
  3912. }
  3913. static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
  3914. {
  3915. unsigned long lpcr;
  3916. int radix;
  3917. int err;
  3918. /* If not on a POWER9, reject it */
  3919. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  3920. return -ENODEV;
  3921. /* If any unknown flags set, reject it */
  3922. if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
  3923. return -EINVAL;
  3924. /* GR (guest radix) bit in process_table field must match */
  3925. radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
  3926. if (!!(cfg->process_table & PATB_GR) != radix)
  3927. return -EINVAL;
  3928. /* Process table size field must be reasonable, i.e. <= 24 */
  3929. if ((cfg->process_table & PRTS_MASK) > 24)
  3930. return -EINVAL;
  3931. /* We can change a guest to/from radix now, if the host is radix */
  3932. if (radix && !radix_enabled())
  3933. return -EINVAL;
  3934. mutex_lock(&kvm->lock);
  3935. if (radix != kvm_is_radix(kvm)) {
  3936. if (kvm->arch.mmu_ready) {
  3937. kvm->arch.mmu_ready = 0;
  3938. /* order mmu_ready vs. vcpus_running */
  3939. smp_mb();
  3940. if (atomic_read(&kvm->arch.vcpus_running)) {
  3941. kvm->arch.mmu_ready = 1;
  3942. err = -EBUSY;
  3943. goto out_unlock;
  3944. }
  3945. }
  3946. if (radix)
  3947. err = kvmppc_switch_mmu_to_radix(kvm);
  3948. else
  3949. err = kvmppc_switch_mmu_to_hpt(kvm);
  3950. if (err)
  3951. goto out_unlock;
  3952. }
  3953. kvm->arch.process_table = cfg->process_table;
  3954. kvmppc_setup_partition_table(kvm);
  3955. lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
  3956. kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
  3957. err = 0;
  3958. out_unlock:
  3959. mutex_unlock(&kvm->lock);
  3960. return err;
  3961. }
  3962. static struct kvmppc_ops kvm_ops_hv = {
  3963. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
  3964. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
  3965. .get_one_reg = kvmppc_get_one_reg_hv,
  3966. .set_one_reg = kvmppc_set_one_reg_hv,
  3967. .vcpu_load = kvmppc_core_vcpu_load_hv,
  3968. .vcpu_put = kvmppc_core_vcpu_put_hv,
  3969. .set_msr = kvmppc_set_msr_hv,
  3970. .vcpu_run = kvmppc_vcpu_run_hv,
  3971. .vcpu_create = kvmppc_core_vcpu_create_hv,
  3972. .vcpu_free = kvmppc_core_vcpu_free_hv,
  3973. .check_requests = kvmppc_core_check_requests_hv,
  3974. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
  3975. .flush_memslot = kvmppc_core_flush_memslot_hv,
  3976. .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
  3977. .commit_memory_region = kvmppc_core_commit_memory_region_hv,
  3978. .unmap_hva_range = kvm_unmap_hva_range_hv,
  3979. .age_hva = kvm_age_hva_hv,
  3980. .test_age_hva = kvm_test_age_hva_hv,
  3981. .set_spte_hva = kvm_set_spte_hva_hv,
  3982. .mmu_destroy = kvmppc_mmu_destroy_hv,
  3983. .free_memslot = kvmppc_core_free_memslot_hv,
  3984. .create_memslot = kvmppc_core_create_memslot_hv,
  3985. .init_vm = kvmppc_core_init_vm_hv,
  3986. .destroy_vm = kvmppc_core_destroy_vm_hv,
  3987. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
  3988. .emulate_op = kvmppc_core_emulate_op_hv,
  3989. .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
  3990. .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
  3991. .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
  3992. .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
  3993. .hcall_implemented = kvmppc_hcall_impl_hv,
  3994. #ifdef CONFIG_KVM_XICS
  3995. .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
  3996. .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
  3997. #endif
  3998. .configure_mmu = kvmhv_configure_mmu,
  3999. .get_rmmu_info = kvmhv_get_rmmu_info,
  4000. .set_smt_mode = kvmhv_set_smt_mode,
  4001. };
  4002. static int kvm_init_subcore_bitmap(void)
  4003. {
  4004. int i, j;
  4005. int nr_cores = cpu_nr_cores();
  4006. struct sibling_subcore_state *sibling_subcore_state;
  4007. for (i = 0; i < nr_cores; i++) {
  4008. int first_cpu = i * threads_per_core;
  4009. int node = cpu_to_node(first_cpu);
  4010. /* Ignore if it is already allocated. */
  4011. if (paca_ptrs[first_cpu]->sibling_subcore_state)
  4012. continue;
  4013. sibling_subcore_state =
  4014. kmalloc_node(sizeof(struct sibling_subcore_state),
  4015. GFP_KERNEL, node);
  4016. if (!sibling_subcore_state)
  4017. return -ENOMEM;
  4018. memset(sibling_subcore_state, 0,
  4019. sizeof(struct sibling_subcore_state));
  4020. for (j = 0; j < threads_per_core; j++) {
  4021. int cpu = first_cpu + j;
  4022. paca_ptrs[cpu]->sibling_subcore_state =
  4023. sibling_subcore_state;
  4024. }
  4025. }
  4026. return 0;
  4027. }
  4028. static int kvmppc_radix_possible(void)
  4029. {
  4030. return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
  4031. }
  4032. static int kvmppc_book3s_init_hv(void)
  4033. {
  4034. int r;
  4035. /*
  4036. * FIXME!! Do we need to check on all cpus ?
  4037. */
  4038. r = kvmppc_core_check_processor_compat_hv();
  4039. if (r < 0)
  4040. return -ENODEV;
  4041. r = kvm_init_subcore_bitmap();
  4042. if (r)
  4043. return r;
  4044. /*
  4045. * We need a way of accessing the XICS interrupt controller,
  4046. * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
  4047. * indirectly, via OPAL.
  4048. */
  4049. #ifdef CONFIG_SMP
  4050. if (!xive_enabled() && !local_paca->kvm_hstate.xics_phys) {
  4051. struct device_node *np;
  4052. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  4053. if (!np) {
  4054. pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
  4055. return -ENODEV;
  4056. }
  4057. /* presence of intc confirmed - node can be dropped again */
  4058. of_node_put(np);
  4059. }
  4060. #endif
  4061. kvm_ops_hv.owner = THIS_MODULE;
  4062. kvmppc_hv_ops = &kvm_ops_hv;
  4063. init_default_hcalls();
  4064. init_vcore_lists();
  4065. r = kvmppc_mmu_hv_init();
  4066. if (r)
  4067. return r;
  4068. if (kvmppc_radix_possible())
  4069. r = kvmppc_radix_init();
  4070. /*
  4071. * POWER9 chips before version 2.02 can't have some threads in
  4072. * HPT mode and some in radix mode on the same core.
  4073. */
  4074. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  4075. unsigned int pvr = mfspr(SPRN_PVR);
  4076. if ((pvr >> 16) == PVR_POWER9 &&
  4077. (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
  4078. ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
  4079. no_mixing_hpt_and_radix = true;
  4080. }
  4081. return r;
  4082. }
  4083. static void kvmppc_book3s_exit_hv(void)
  4084. {
  4085. kvmppc_free_host_rm_ops();
  4086. if (kvmppc_radix_possible())
  4087. kvmppc_radix_exit();
  4088. kvmppc_hv_ops = NULL;
  4089. }
  4090. module_init(kvmppc_book3s_init_hv);
  4091. module_exit(kvmppc_book3s_exit_hv);
  4092. MODULE_LICENSE("GPL");
  4093. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  4094. MODULE_ALIAS("devname:kvm");