book3s_hv_rmhandlers.S 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  12. *
  13. * Derived from book3s_rmhandlers.S and other files, which are:
  14. *
  15. * Copyright SUSE Linux Products GmbH 2009
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #include <asm/ppc_asm.h>
  20. #include <asm/kvm_asm.h>
  21. #include <asm/reg.h>
  22. #include <asm/mmu.h>
  23. #include <asm/page.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/hvcall.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/exception-64s.h>
  28. #include <asm/kvm_book3s_asm.h>
  29. #include <asm/book3s/64/mmu-hash.h>
  30. #include <asm/tm.h>
  31. #include <asm/opal.h>
  32. #include <asm/xive-regs.h>
  33. #include <asm/thread_info.h>
  34. #include <asm/asm-compat.h>
  35. #include <asm/feature-fixups.h>
  36. /* Sign-extend HDEC if not on POWER9 */
  37. #define EXTEND_HDEC(reg) \
  38. BEGIN_FTR_SECTION; \
  39. extsw reg, reg; \
  40. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  41. /* Values in HSTATE_NAPPING(r13) */
  42. #define NAPPING_CEDE 1
  43. #define NAPPING_NOVCPU 2
  44. /* Stack frame offsets for kvmppc_hv_entry */
  45. #define SFS 160
  46. #define STACK_SLOT_TRAP (SFS-4)
  47. #define STACK_SLOT_TID (SFS-16)
  48. #define STACK_SLOT_PSSCR (SFS-24)
  49. #define STACK_SLOT_PID (SFS-32)
  50. #define STACK_SLOT_IAMR (SFS-40)
  51. #define STACK_SLOT_CIABR (SFS-48)
  52. #define STACK_SLOT_DAWR (SFS-56)
  53. #define STACK_SLOT_DAWRX (SFS-64)
  54. #define STACK_SLOT_HFSCR (SFS-72)
  55. #define STACK_SLOT_AMR (SFS-80)
  56. #define STACK_SLOT_UAMOR (SFS-88)
  57. /*
  58. * Call kvmppc_hv_entry in real mode.
  59. * Must be called with interrupts hard-disabled.
  60. *
  61. * Input Registers:
  62. *
  63. * LR = return address to continue at after eventually re-enabling MMU
  64. */
  65. _GLOBAL_TOC(kvmppc_hv_entry_trampoline)
  66. mflr r0
  67. std r0, PPC_LR_STKOFF(r1)
  68. stdu r1, -112(r1)
  69. mfmsr r10
  70. std r10, HSTATE_HOST_MSR(r13)
  71. LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
  72. li r0,MSR_RI
  73. andc r0,r10,r0
  74. li r6,MSR_IR | MSR_DR
  75. andc r6,r10,r6
  76. mtmsrd r0,1 /* clear RI in MSR */
  77. mtsrr0 r5
  78. mtsrr1 r6
  79. RFI_TO_KERNEL
  80. kvmppc_call_hv_entry:
  81. BEGIN_FTR_SECTION
  82. /* On P9, do LPCR setting, if necessary */
  83. ld r3, HSTATE_SPLIT_MODE(r13)
  84. cmpdi r3, 0
  85. beq 46f
  86. lwz r4, KVM_SPLIT_DO_SET(r3)
  87. cmpwi r4, 0
  88. beq 46f
  89. bl kvmhv_p9_set_lpcr
  90. nop
  91. 46:
  92. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  93. ld r4, HSTATE_KVM_VCPU(r13)
  94. bl kvmppc_hv_entry
  95. /* Back from guest - restore host state and return to caller */
  96. BEGIN_FTR_SECTION
  97. /* Restore host DABR and DABRX */
  98. ld r5,HSTATE_DABR(r13)
  99. li r6,7
  100. mtspr SPRN_DABR,r5
  101. mtspr SPRN_DABRX,r6
  102. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  103. /* Restore SPRG3 */
  104. ld r3,PACA_SPRG_VDSO(r13)
  105. mtspr SPRN_SPRG_VDSO_WRITE,r3
  106. /* Reload the host's PMU registers */
  107. lbz r4, PACA_PMCINUSE(r13) /* is the host using the PMU? */
  108. cmpwi r4, 0
  109. beq 23f /* skip if not */
  110. BEGIN_FTR_SECTION
  111. ld r3, HSTATE_MMCR0(r13)
  112. andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
  113. cmpwi r4, MMCR0_PMAO
  114. beql kvmppc_fix_pmao
  115. END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
  116. lwz r3, HSTATE_PMC1(r13)
  117. lwz r4, HSTATE_PMC2(r13)
  118. lwz r5, HSTATE_PMC3(r13)
  119. lwz r6, HSTATE_PMC4(r13)
  120. lwz r8, HSTATE_PMC5(r13)
  121. lwz r9, HSTATE_PMC6(r13)
  122. mtspr SPRN_PMC1, r3
  123. mtspr SPRN_PMC2, r4
  124. mtspr SPRN_PMC3, r5
  125. mtspr SPRN_PMC4, r6
  126. mtspr SPRN_PMC5, r8
  127. mtspr SPRN_PMC6, r9
  128. ld r3, HSTATE_MMCR0(r13)
  129. ld r4, HSTATE_MMCR1(r13)
  130. ld r5, HSTATE_MMCRA(r13)
  131. ld r6, HSTATE_SIAR(r13)
  132. ld r7, HSTATE_SDAR(r13)
  133. mtspr SPRN_MMCR1, r4
  134. mtspr SPRN_MMCRA, r5
  135. mtspr SPRN_SIAR, r6
  136. mtspr SPRN_SDAR, r7
  137. BEGIN_FTR_SECTION
  138. ld r8, HSTATE_MMCR2(r13)
  139. ld r9, HSTATE_SIER(r13)
  140. mtspr SPRN_MMCR2, r8
  141. mtspr SPRN_SIER, r9
  142. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  143. mtspr SPRN_MMCR0, r3
  144. isync
  145. 23:
  146. /*
  147. * Reload DEC. HDEC interrupts were disabled when
  148. * we reloaded the host's LPCR value.
  149. */
  150. ld r3, HSTATE_DECEXP(r13)
  151. mftb r4
  152. subf r4, r4, r3
  153. mtspr SPRN_DEC, r4
  154. /* hwthread_req may have got set by cede or no vcpu, so clear it */
  155. li r0, 0
  156. stb r0, HSTATE_HWTHREAD_REQ(r13)
  157. /*
  158. * For external interrupts we need to call the Linux
  159. * handler to process the interrupt. We do that by jumping
  160. * to absolute address 0x500 for external interrupts.
  161. * The [h]rfid at the end of the handler will return to
  162. * the book3s_hv_interrupts.S code. For other interrupts
  163. * we do the rfid to get back to the book3s_hv_interrupts.S
  164. * code here.
  165. */
  166. ld r8, 112+PPC_LR_STKOFF(r1)
  167. addi r1, r1, 112
  168. ld r7, HSTATE_HOST_MSR(r13)
  169. /* Return the trap number on this thread as the return value */
  170. mr r3, r12
  171. /*
  172. * If we came back from the guest via a relocation-on interrupt,
  173. * we will be in virtual mode at this point, which makes it a
  174. * little easier to get back to the caller.
  175. */
  176. mfmsr r0
  177. andi. r0, r0, MSR_IR /* in real mode? */
  178. bne .Lvirt_return
  179. /* RFI into the highmem handler */
  180. mfmsr r6
  181. li r0, MSR_RI
  182. andc r6, r6, r0
  183. mtmsrd r6, 1 /* Clear RI in MSR */
  184. mtsrr0 r8
  185. mtsrr1 r7
  186. RFI_TO_KERNEL
  187. /* Virtual-mode return */
  188. .Lvirt_return:
  189. mtlr r8
  190. blr
  191. kvmppc_primary_no_guest:
  192. /* We handle this much like a ceded vcpu */
  193. /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
  194. /* HDEC may be larger than DEC for arch >= v3.00, but since the */
  195. /* HDEC value came from DEC in the first place, it will fit */
  196. mfspr r3, SPRN_HDEC
  197. mtspr SPRN_DEC, r3
  198. /*
  199. * Make sure the primary has finished the MMU switch.
  200. * We should never get here on a secondary thread, but
  201. * check it for robustness' sake.
  202. */
  203. ld r5, HSTATE_KVM_VCORE(r13)
  204. 65: lbz r0, VCORE_IN_GUEST(r5)
  205. cmpwi r0, 0
  206. beq 65b
  207. /* Set LPCR. */
  208. ld r8,VCORE_LPCR(r5)
  209. mtspr SPRN_LPCR,r8
  210. isync
  211. /* set our bit in napping_threads */
  212. ld r5, HSTATE_KVM_VCORE(r13)
  213. lbz r7, HSTATE_PTID(r13)
  214. li r0, 1
  215. sld r0, r0, r7
  216. addi r6, r5, VCORE_NAPPING_THREADS
  217. 1: lwarx r3, 0, r6
  218. or r3, r3, r0
  219. stwcx. r3, 0, r6
  220. bne 1b
  221. /* order napping_threads update vs testing entry_exit_map */
  222. isync
  223. li r12, 0
  224. lwz r7, VCORE_ENTRY_EXIT(r5)
  225. cmpwi r7, 0x100
  226. bge kvm_novcpu_exit /* another thread already exiting */
  227. li r3, NAPPING_NOVCPU
  228. stb r3, HSTATE_NAPPING(r13)
  229. li r3, 0 /* Don't wake on privileged (OS) doorbell */
  230. b kvm_do_nap
  231. /*
  232. * kvm_novcpu_wakeup
  233. * Entered from kvm_start_guest if kvm_hstate.napping is set
  234. * to NAPPING_NOVCPU
  235. * r2 = kernel TOC
  236. * r13 = paca
  237. */
  238. kvm_novcpu_wakeup:
  239. ld r1, HSTATE_HOST_R1(r13)
  240. ld r5, HSTATE_KVM_VCORE(r13)
  241. li r0, 0
  242. stb r0, HSTATE_NAPPING(r13)
  243. /* check the wake reason */
  244. bl kvmppc_check_wake_reason
  245. /*
  246. * Restore volatile registers since we could have called
  247. * a C routine in kvmppc_check_wake_reason.
  248. * r5 = VCORE
  249. */
  250. ld r5, HSTATE_KVM_VCORE(r13)
  251. /* see if any other thread is already exiting */
  252. lwz r0, VCORE_ENTRY_EXIT(r5)
  253. cmpwi r0, 0x100
  254. bge kvm_novcpu_exit
  255. /* clear our bit in napping_threads */
  256. lbz r7, HSTATE_PTID(r13)
  257. li r0, 1
  258. sld r0, r0, r7
  259. addi r6, r5, VCORE_NAPPING_THREADS
  260. 4: lwarx r7, 0, r6
  261. andc r7, r7, r0
  262. stwcx. r7, 0, r6
  263. bne 4b
  264. /* See if the wake reason means we need to exit */
  265. cmpdi r3, 0
  266. bge kvm_novcpu_exit
  267. /* See if our timeslice has expired (HDEC is negative) */
  268. mfspr r0, SPRN_HDEC
  269. EXTEND_HDEC(r0)
  270. li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
  271. cmpdi r0, 0
  272. blt kvm_novcpu_exit
  273. /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
  274. ld r4, HSTATE_KVM_VCPU(r13)
  275. cmpdi r4, 0
  276. beq kvmppc_primary_no_guest
  277. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  278. addi r3, r4, VCPU_TB_RMENTRY
  279. bl kvmhv_start_timing
  280. #endif
  281. b kvmppc_got_guest
  282. kvm_novcpu_exit:
  283. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  284. ld r4, HSTATE_KVM_VCPU(r13)
  285. cmpdi r4, 0
  286. beq 13f
  287. addi r3, r4, VCPU_TB_RMEXIT
  288. bl kvmhv_accumulate_time
  289. #endif
  290. 13: mr r3, r12
  291. stw r12, STACK_SLOT_TRAP(r1)
  292. bl kvmhv_commence_exit
  293. nop
  294. b kvmhv_switch_to_host
  295. /*
  296. * We come in here when wakened from nap mode.
  297. * Relocation is off and most register values are lost.
  298. * r13 points to the PACA.
  299. * r3 contains the SRR1 wakeup value, SRR1 is trashed.
  300. */
  301. .globl kvm_start_guest
  302. kvm_start_guest:
  303. /* Set runlatch bit the minute you wake up from nap */
  304. mfspr r0, SPRN_CTRLF
  305. ori r0, r0, 1
  306. mtspr SPRN_CTRLT, r0
  307. /*
  308. * Could avoid this and pass it through in r3. For now,
  309. * code expects it to be in SRR1.
  310. */
  311. mtspr SPRN_SRR1,r3
  312. ld r2,PACATOC(r13)
  313. li r0,0
  314. stb r0,PACA_FTRACE_ENABLED(r13)
  315. li r0,KVM_HWTHREAD_IN_KVM
  316. stb r0,HSTATE_HWTHREAD_STATE(r13)
  317. /* NV GPR values from power7_idle() will no longer be valid */
  318. li r0,1
  319. stb r0,PACA_NAPSTATELOST(r13)
  320. /* were we napping due to cede? */
  321. lbz r0,HSTATE_NAPPING(r13)
  322. cmpwi r0,NAPPING_CEDE
  323. beq kvm_end_cede
  324. cmpwi r0,NAPPING_NOVCPU
  325. beq kvm_novcpu_wakeup
  326. ld r1,PACAEMERGSP(r13)
  327. subi r1,r1,STACK_FRAME_OVERHEAD
  328. /*
  329. * We weren't napping due to cede, so this must be a secondary
  330. * thread being woken up to run a guest, or being woken up due
  331. * to a stray IPI. (Or due to some machine check or hypervisor
  332. * maintenance interrupt while the core is in KVM.)
  333. */
  334. /* Check the wake reason in SRR1 to see why we got here */
  335. bl kvmppc_check_wake_reason
  336. /*
  337. * kvmppc_check_wake_reason could invoke a C routine, but we
  338. * have no volatile registers to restore when we return.
  339. */
  340. cmpdi r3, 0
  341. bge kvm_no_guest
  342. /* get vcore pointer, NULL if we have nothing to run */
  343. ld r5,HSTATE_KVM_VCORE(r13)
  344. cmpdi r5,0
  345. /* if we have no vcore to run, go back to sleep */
  346. beq kvm_no_guest
  347. kvm_secondary_got_guest:
  348. /* Set HSTATE_DSCR(r13) to something sensible */
  349. ld r6, PACA_DSCR_DEFAULT(r13)
  350. std r6, HSTATE_DSCR(r13)
  351. /* On thread 0 of a subcore, set HDEC to max */
  352. lbz r4, HSTATE_PTID(r13)
  353. cmpwi r4, 0
  354. bne 63f
  355. LOAD_REG_ADDR(r6, decrementer_max)
  356. ld r6, 0(r6)
  357. mtspr SPRN_HDEC, r6
  358. /* and set per-LPAR registers, if doing dynamic micro-threading */
  359. ld r6, HSTATE_SPLIT_MODE(r13)
  360. cmpdi r6, 0
  361. beq 63f
  362. BEGIN_FTR_SECTION
  363. ld r0, KVM_SPLIT_RPR(r6)
  364. mtspr SPRN_RPR, r0
  365. ld r0, KVM_SPLIT_PMMAR(r6)
  366. mtspr SPRN_PMMAR, r0
  367. ld r0, KVM_SPLIT_LDBAR(r6)
  368. mtspr SPRN_LDBAR, r0
  369. isync
  370. FTR_SECTION_ELSE
  371. /* On P9 we use the split_info for coordinating LPCR changes */
  372. lwz r4, KVM_SPLIT_DO_SET(r6)
  373. cmpwi r4, 0
  374. beq 1f
  375. mr r3, r6
  376. bl kvmhv_p9_set_lpcr
  377. nop
  378. 1:
  379. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
  380. 63:
  381. /* Order load of vcpu after load of vcore */
  382. lwsync
  383. ld r4, HSTATE_KVM_VCPU(r13)
  384. bl kvmppc_hv_entry
  385. /* Back from the guest, go back to nap */
  386. /* Clear our vcpu and vcore pointers so we don't come back in early */
  387. li r0, 0
  388. std r0, HSTATE_KVM_VCPU(r13)
  389. /*
  390. * Once we clear HSTATE_KVM_VCORE(r13), the code in
  391. * kvmppc_run_core() is going to assume that all our vcpu
  392. * state is visible in memory. This lwsync makes sure
  393. * that that is true.
  394. */
  395. lwsync
  396. std r0, HSTATE_KVM_VCORE(r13)
  397. /*
  398. * All secondaries exiting guest will fall through this path.
  399. * Before proceeding, just check for HMI interrupt and
  400. * invoke opal hmi handler. By now we are sure that the
  401. * primary thread on this core/subcore has already made partition
  402. * switch/TB resync and we are good to call opal hmi handler.
  403. */
  404. cmpwi r12, BOOK3S_INTERRUPT_HMI
  405. bne kvm_no_guest
  406. li r3,0 /* NULL argument */
  407. bl hmi_exception_realmode
  408. /*
  409. * At this point we have finished executing in the guest.
  410. * We need to wait for hwthread_req to become zero, since
  411. * we may not turn on the MMU while hwthread_req is non-zero.
  412. * While waiting we also need to check if we get given a vcpu to run.
  413. */
  414. kvm_no_guest:
  415. lbz r3, HSTATE_HWTHREAD_REQ(r13)
  416. cmpwi r3, 0
  417. bne 53f
  418. HMT_MEDIUM
  419. li r0, KVM_HWTHREAD_IN_KERNEL
  420. stb r0, HSTATE_HWTHREAD_STATE(r13)
  421. /* need to recheck hwthread_req after a barrier, to avoid race */
  422. sync
  423. lbz r3, HSTATE_HWTHREAD_REQ(r13)
  424. cmpwi r3, 0
  425. bne 54f
  426. /*
  427. * We jump to pnv_wakeup_loss, which will return to the caller
  428. * of power7_nap in the powernv cpu offline loop. The value we
  429. * put in r3 becomes the return value for power7_nap. pnv_wakeup_loss
  430. * requires SRR1 in r12.
  431. */
  432. li r3, LPCR_PECE0
  433. mfspr r4, SPRN_LPCR
  434. rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
  435. mtspr SPRN_LPCR, r4
  436. li r3, 0
  437. mfspr r12,SPRN_SRR1
  438. b pnv_wakeup_loss
  439. 53: HMT_LOW
  440. ld r5, HSTATE_KVM_VCORE(r13)
  441. cmpdi r5, 0
  442. bne 60f
  443. ld r3, HSTATE_SPLIT_MODE(r13)
  444. cmpdi r3, 0
  445. beq kvm_no_guest
  446. lwz r0, KVM_SPLIT_DO_SET(r3)
  447. cmpwi r0, 0
  448. bne kvmhv_do_set
  449. lwz r0, KVM_SPLIT_DO_RESTORE(r3)
  450. cmpwi r0, 0
  451. bne kvmhv_do_restore
  452. lbz r0, KVM_SPLIT_DO_NAP(r3)
  453. cmpwi r0, 0
  454. beq kvm_no_guest
  455. HMT_MEDIUM
  456. b kvm_unsplit_nap
  457. 60: HMT_MEDIUM
  458. b kvm_secondary_got_guest
  459. 54: li r0, KVM_HWTHREAD_IN_KVM
  460. stb r0, HSTATE_HWTHREAD_STATE(r13)
  461. b kvm_no_guest
  462. kvmhv_do_set:
  463. /* Set LPCR, LPIDR etc. on P9 */
  464. HMT_MEDIUM
  465. bl kvmhv_p9_set_lpcr
  466. nop
  467. b kvm_no_guest
  468. kvmhv_do_restore:
  469. HMT_MEDIUM
  470. bl kvmhv_p9_restore_lpcr
  471. nop
  472. b kvm_no_guest
  473. /*
  474. * Here the primary thread is trying to return the core to
  475. * whole-core mode, so we need to nap.
  476. */
  477. kvm_unsplit_nap:
  478. /*
  479. * When secondaries are napping in kvm_unsplit_nap() with
  480. * hwthread_req = 1, HMI goes ignored even though subcores are
  481. * already exited the guest. Hence HMI keeps waking up secondaries
  482. * from nap in a loop and secondaries always go back to nap since
  483. * no vcore is assigned to them. This makes impossible for primary
  484. * thread to get hold of secondary threads resulting into a soft
  485. * lockup in KVM path.
  486. *
  487. * Let us check if HMI is pending and handle it before we go to nap.
  488. */
  489. cmpwi r12, BOOK3S_INTERRUPT_HMI
  490. bne 55f
  491. li r3, 0 /* NULL argument */
  492. bl hmi_exception_realmode
  493. 55:
  494. /*
  495. * Ensure that secondary doesn't nap when it has
  496. * its vcore pointer set.
  497. */
  498. sync /* matches smp_mb() before setting split_info.do_nap */
  499. ld r0, HSTATE_KVM_VCORE(r13)
  500. cmpdi r0, 0
  501. bne kvm_no_guest
  502. /* clear any pending message */
  503. BEGIN_FTR_SECTION
  504. lis r6, (PPC_DBELL_SERVER << (63-36))@h
  505. PPC_MSGCLR(6)
  506. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  507. /* Set kvm_split_mode.napped[tid] = 1 */
  508. ld r3, HSTATE_SPLIT_MODE(r13)
  509. li r0, 1
  510. lbz r4, HSTATE_TID(r13)
  511. addi r4, r4, KVM_SPLIT_NAPPED
  512. stbx r0, r3, r4
  513. /* Check the do_nap flag again after setting napped[] */
  514. sync
  515. lbz r0, KVM_SPLIT_DO_NAP(r3)
  516. cmpwi r0, 0
  517. beq 57f
  518. li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
  519. mfspr r5, SPRN_LPCR
  520. rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
  521. b kvm_nap_sequence
  522. 57: li r0, 0
  523. stbx r0, r3, r4
  524. b kvm_no_guest
  525. /******************************************************************************
  526. * *
  527. * Entry code *
  528. * *
  529. *****************************************************************************/
  530. .global kvmppc_hv_entry
  531. kvmppc_hv_entry:
  532. /* Required state:
  533. *
  534. * R4 = vcpu pointer (or NULL)
  535. * MSR = ~IR|DR
  536. * R13 = PACA
  537. * R1 = host R1
  538. * R2 = TOC
  539. * all other volatile GPRS = free
  540. * Does not preserve non-volatile GPRs or CR fields
  541. */
  542. mflr r0
  543. std r0, PPC_LR_STKOFF(r1)
  544. stdu r1, -SFS(r1)
  545. /* Save R1 in the PACA */
  546. std r1, HSTATE_HOST_R1(r13)
  547. li r6, KVM_GUEST_MODE_HOST_HV
  548. stb r6, HSTATE_IN_GUEST(r13)
  549. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  550. /* Store initial timestamp */
  551. cmpdi r4, 0
  552. beq 1f
  553. addi r3, r4, VCPU_TB_RMENTRY
  554. bl kvmhv_start_timing
  555. 1:
  556. #endif
  557. /* Use cr7 as an indication of radix mode */
  558. ld r5, HSTATE_KVM_VCORE(r13)
  559. ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
  560. lbz r0, KVM_RADIX(r9)
  561. cmpwi cr7, r0, 0
  562. /*
  563. * POWER7/POWER8 host -> guest partition switch code.
  564. * We don't have to lock against concurrent tlbies,
  565. * but we do have to coordinate across hardware threads.
  566. */
  567. /* Set bit in entry map iff exit map is zero. */
  568. li r7, 1
  569. lbz r6, HSTATE_PTID(r13)
  570. sld r7, r7, r6
  571. addi r8, r5, VCORE_ENTRY_EXIT
  572. 21: lwarx r3, 0, r8
  573. cmpwi r3, 0x100 /* any threads starting to exit? */
  574. bge secondary_too_late /* if so we're too late to the party */
  575. or r3, r3, r7
  576. stwcx. r3, 0, r8
  577. bne 21b
  578. /* Primary thread switches to guest partition. */
  579. cmpwi r6,0
  580. bne 10f
  581. /* Radix has already switched LPID and flushed core TLB */
  582. bne cr7, 22f
  583. lwz r7,KVM_LPID(r9)
  584. BEGIN_FTR_SECTION
  585. ld r6,KVM_SDR1(r9)
  586. li r0,LPID_RSVD /* switch to reserved LPID */
  587. mtspr SPRN_LPID,r0
  588. ptesync
  589. mtspr SPRN_SDR1,r6 /* switch to partition page table */
  590. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  591. mtspr SPRN_LPID,r7
  592. isync
  593. /* See if we need to flush the TLB. Hash has to be done in RM */
  594. lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
  595. BEGIN_FTR_SECTION
  596. /*
  597. * On POWER9, individual threads can come in here, but the
  598. * TLB is shared between the 4 threads in a core, hence
  599. * invalidating on one thread invalidates for all.
  600. * Thus we make all 4 threads use the same bit here.
  601. */
  602. clrrdi r6,r6,2
  603. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  604. clrldi r7,r6,64-6 /* extract bit number (6 bits) */
  605. srdi r6,r6,6 /* doubleword number */
  606. sldi r6,r6,3 /* address offset */
  607. add r6,r6,r9
  608. addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
  609. li r8,1
  610. sld r8,r8,r7
  611. ld r7,0(r6)
  612. and. r7,r7,r8
  613. beq 22f
  614. /* Flush the TLB of any entries for this LPID */
  615. lwz r0,KVM_TLB_SETS(r9)
  616. mtctr r0
  617. li r7,0x800 /* IS field = 0b10 */
  618. ptesync
  619. li r0,0 /* RS for P9 version of tlbiel */
  620. 28: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
  621. addi r7,r7,0x1000
  622. bdnz 28b
  623. ptesync
  624. 23: ldarx r7,0,r6 /* clear the bit after TLB flushed */
  625. andc r7,r7,r8
  626. stdcx. r7,0,r6
  627. bne 23b
  628. /* Add timebase offset onto timebase */
  629. 22: ld r8,VCORE_TB_OFFSET(r5)
  630. cmpdi r8,0
  631. beq 37f
  632. std r8, VCORE_TB_OFFSET_APPL(r5)
  633. mftb r6 /* current host timebase */
  634. add r8,r8,r6
  635. mtspr SPRN_TBU40,r8 /* update upper 40 bits */
  636. mftb r7 /* check if lower 24 bits overflowed */
  637. clrldi r6,r6,40
  638. clrldi r7,r7,40
  639. cmpld r7,r6
  640. bge 37f
  641. addis r8,r8,0x100 /* if so, increment upper 40 bits */
  642. mtspr SPRN_TBU40,r8
  643. /* Load guest PCR value to select appropriate compat mode */
  644. 37: ld r7, VCORE_PCR(r5)
  645. cmpdi r7, 0
  646. beq 38f
  647. mtspr SPRN_PCR, r7
  648. 38:
  649. BEGIN_FTR_SECTION
  650. /* DPDES and VTB are shared between threads */
  651. ld r8, VCORE_DPDES(r5)
  652. ld r7, VCORE_VTB(r5)
  653. mtspr SPRN_DPDES, r8
  654. mtspr SPRN_VTB, r7
  655. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  656. /* Mark the subcore state as inside guest */
  657. bl kvmppc_subcore_enter_guest
  658. nop
  659. ld r5, HSTATE_KVM_VCORE(r13)
  660. ld r4, HSTATE_KVM_VCPU(r13)
  661. li r0,1
  662. stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
  663. /* Do we have a guest vcpu to run? */
  664. 10: cmpdi r4, 0
  665. beq kvmppc_primary_no_guest
  666. kvmppc_got_guest:
  667. /* Increment yield count if they have a VPA */
  668. ld r3, VCPU_VPA(r4)
  669. cmpdi r3, 0
  670. beq 25f
  671. li r6, LPPACA_YIELDCOUNT
  672. LWZX_BE r5, r3, r6
  673. addi r5, r5, 1
  674. STWX_BE r5, r3, r6
  675. li r6, 1
  676. stb r6, VCPU_VPA_DIRTY(r4)
  677. 25:
  678. /* Save purr/spurr */
  679. mfspr r5,SPRN_PURR
  680. mfspr r6,SPRN_SPURR
  681. std r5,HSTATE_PURR(r13)
  682. std r6,HSTATE_SPURR(r13)
  683. ld r7,VCPU_PURR(r4)
  684. ld r8,VCPU_SPURR(r4)
  685. mtspr SPRN_PURR,r7
  686. mtspr SPRN_SPURR,r8
  687. /* Save host values of some registers */
  688. BEGIN_FTR_SECTION
  689. mfspr r5, SPRN_TIDR
  690. mfspr r6, SPRN_PSSCR
  691. mfspr r7, SPRN_PID
  692. std r5, STACK_SLOT_TID(r1)
  693. std r6, STACK_SLOT_PSSCR(r1)
  694. std r7, STACK_SLOT_PID(r1)
  695. mfspr r5, SPRN_HFSCR
  696. std r5, STACK_SLOT_HFSCR(r1)
  697. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  698. BEGIN_FTR_SECTION
  699. mfspr r5, SPRN_CIABR
  700. mfspr r6, SPRN_DAWR
  701. mfspr r7, SPRN_DAWRX
  702. mfspr r8, SPRN_IAMR
  703. std r5, STACK_SLOT_CIABR(r1)
  704. std r6, STACK_SLOT_DAWR(r1)
  705. std r7, STACK_SLOT_DAWRX(r1)
  706. std r8, STACK_SLOT_IAMR(r1)
  707. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  708. mfspr r5, SPRN_AMR
  709. std r5, STACK_SLOT_AMR(r1)
  710. mfspr r6, SPRN_UAMOR
  711. std r6, STACK_SLOT_UAMOR(r1)
  712. BEGIN_FTR_SECTION
  713. /* Set partition DABR */
  714. /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
  715. lwz r5,VCPU_DABRX(r4)
  716. ld r6,VCPU_DABR(r4)
  717. mtspr SPRN_DABRX,r5
  718. mtspr SPRN_DABR,r6
  719. isync
  720. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  721. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  722. /*
  723. * Branch around the call if both CPU_FTR_TM and
  724. * CPU_FTR_P9_TM_HV_ASSIST are off.
  725. */
  726. BEGIN_FTR_SECTION
  727. b 91f
  728. END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
  729. /*
  730. * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
  731. */
  732. mr r3, r4
  733. ld r4, VCPU_MSR(r3)
  734. bl kvmppc_restore_tm_hv
  735. ld r4, HSTATE_KVM_VCPU(r13)
  736. 91:
  737. #endif
  738. /* Load guest PMU registers */
  739. /* R4 is live here (vcpu pointer) */
  740. li r3, 1
  741. sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
  742. mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
  743. isync
  744. BEGIN_FTR_SECTION
  745. ld r3, VCPU_MMCR(r4)
  746. andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
  747. cmpwi r5, MMCR0_PMAO
  748. beql kvmppc_fix_pmao
  749. END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
  750. lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
  751. lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
  752. lwz r6, VCPU_PMC + 8(r4)
  753. lwz r7, VCPU_PMC + 12(r4)
  754. lwz r8, VCPU_PMC + 16(r4)
  755. lwz r9, VCPU_PMC + 20(r4)
  756. mtspr SPRN_PMC1, r3
  757. mtspr SPRN_PMC2, r5
  758. mtspr SPRN_PMC3, r6
  759. mtspr SPRN_PMC4, r7
  760. mtspr SPRN_PMC5, r8
  761. mtspr SPRN_PMC6, r9
  762. ld r3, VCPU_MMCR(r4)
  763. ld r5, VCPU_MMCR + 8(r4)
  764. ld r6, VCPU_MMCR + 16(r4)
  765. ld r7, VCPU_SIAR(r4)
  766. ld r8, VCPU_SDAR(r4)
  767. mtspr SPRN_MMCR1, r5
  768. mtspr SPRN_MMCRA, r6
  769. mtspr SPRN_SIAR, r7
  770. mtspr SPRN_SDAR, r8
  771. BEGIN_FTR_SECTION
  772. ld r5, VCPU_MMCR + 24(r4)
  773. ld r6, VCPU_SIER(r4)
  774. mtspr SPRN_MMCR2, r5
  775. mtspr SPRN_SIER, r6
  776. BEGIN_FTR_SECTION_NESTED(96)
  777. lwz r7, VCPU_PMC + 24(r4)
  778. lwz r8, VCPU_PMC + 28(r4)
  779. ld r9, VCPU_MMCR + 32(r4)
  780. mtspr SPRN_SPMC1, r7
  781. mtspr SPRN_SPMC2, r8
  782. mtspr SPRN_MMCRS, r9
  783. END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
  784. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  785. mtspr SPRN_MMCR0, r3
  786. isync
  787. /* Load up FP, VMX and VSX registers */
  788. bl kvmppc_load_fp
  789. ld r14, VCPU_GPR(R14)(r4)
  790. ld r15, VCPU_GPR(R15)(r4)
  791. ld r16, VCPU_GPR(R16)(r4)
  792. ld r17, VCPU_GPR(R17)(r4)
  793. ld r18, VCPU_GPR(R18)(r4)
  794. ld r19, VCPU_GPR(R19)(r4)
  795. ld r20, VCPU_GPR(R20)(r4)
  796. ld r21, VCPU_GPR(R21)(r4)
  797. ld r22, VCPU_GPR(R22)(r4)
  798. ld r23, VCPU_GPR(R23)(r4)
  799. ld r24, VCPU_GPR(R24)(r4)
  800. ld r25, VCPU_GPR(R25)(r4)
  801. ld r26, VCPU_GPR(R26)(r4)
  802. ld r27, VCPU_GPR(R27)(r4)
  803. ld r28, VCPU_GPR(R28)(r4)
  804. ld r29, VCPU_GPR(R29)(r4)
  805. ld r30, VCPU_GPR(R30)(r4)
  806. ld r31, VCPU_GPR(R31)(r4)
  807. /* Switch DSCR to guest value */
  808. ld r5, VCPU_DSCR(r4)
  809. mtspr SPRN_DSCR, r5
  810. BEGIN_FTR_SECTION
  811. /* Skip next section on POWER7 */
  812. b 8f
  813. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  814. /* Load up POWER8-specific registers */
  815. ld r5, VCPU_IAMR(r4)
  816. lwz r6, VCPU_PSPB(r4)
  817. ld r7, VCPU_FSCR(r4)
  818. mtspr SPRN_IAMR, r5
  819. mtspr SPRN_PSPB, r6
  820. mtspr SPRN_FSCR, r7
  821. ld r5, VCPU_DAWR(r4)
  822. ld r6, VCPU_DAWRX(r4)
  823. ld r7, VCPU_CIABR(r4)
  824. ld r8, VCPU_TAR(r4)
  825. /*
  826. * Handle broken DAWR case by not writing it. This means we
  827. * can still store the DAWR register for migration.
  828. */
  829. BEGIN_FTR_SECTION
  830. mtspr SPRN_DAWR, r5
  831. mtspr SPRN_DAWRX, r6
  832. END_FTR_SECTION_IFSET(CPU_FTR_DAWR)
  833. mtspr SPRN_CIABR, r7
  834. mtspr SPRN_TAR, r8
  835. ld r5, VCPU_IC(r4)
  836. ld r8, VCPU_EBBHR(r4)
  837. mtspr SPRN_IC, r5
  838. mtspr SPRN_EBBHR, r8
  839. ld r5, VCPU_EBBRR(r4)
  840. ld r6, VCPU_BESCR(r4)
  841. lwz r7, VCPU_GUEST_PID(r4)
  842. ld r8, VCPU_WORT(r4)
  843. mtspr SPRN_EBBRR, r5
  844. mtspr SPRN_BESCR, r6
  845. mtspr SPRN_PID, r7
  846. mtspr SPRN_WORT, r8
  847. BEGIN_FTR_SECTION
  848. /* POWER8-only registers */
  849. ld r5, VCPU_TCSCR(r4)
  850. ld r6, VCPU_ACOP(r4)
  851. ld r7, VCPU_CSIGR(r4)
  852. ld r8, VCPU_TACR(r4)
  853. mtspr SPRN_TCSCR, r5
  854. mtspr SPRN_ACOP, r6
  855. mtspr SPRN_CSIGR, r7
  856. mtspr SPRN_TACR, r8
  857. nop
  858. FTR_SECTION_ELSE
  859. /* POWER9-only registers */
  860. ld r5, VCPU_TID(r4)
  861. ld r6, VCPU_PSSCR(r4)
  862. lbz r8, HSTATE_FAKE_SUSPEND(r13)
  863. oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
  864. rldimi r6, r8, PSSCR_FAKE_SUSPEND_LG, 63 - PSSCR_FAKE_SUSPEND_LG
  865. ld r7, VCPU_HFSCR(r4)
  866. mtspr SPRN_TIDR, r5
  867. mtspr SPRN_PSSCR, r6
  868. mtspr SPRN_HFSCR, r7
  869. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
  870. 8:
  871. ld r5, VCPU_SPRG0(r4)
  872. ld r6, VCPU_SPRG1(r4)
  873. ld r7, VCPU_SPRG2(r4)
  874. ld r8, VCPU_SPRG3(r4)
  875. mtspr SPRN_SPRG0, r5
  876. mtspr SPRN_SPRG1, r6
  877. mtspr SPRN_SPRG2, r7
  878. mtspr SPRN_SPRG3, r8
  879. /* Load up DAR and DSISR */
  880. ld r5, VCPU_DAR(r4)
  881. lwz r6, VCPU_DSISR(r4)
  882. mtspr SPRN_DAR, r5
  883. mtspr SPRN_DSISR, r6
  884. /* Restore AMR and UAMOR, set AMOR to all 1s */
  885. ld r5,VCPU_AMR(r4)
  886. ld r6,VCPU_UAMOR(r4)
  887. li r7,-1
  888. mtspr SPRN_AMR,r5
  889. mtspr SPRN_UAMOR,r6
  890. mtspr SPRN_AMOR,r7
  891. /* Restore state of CTRL run bit; assume 1 on entry */
  892. lwz r5,VCPU_CTRL(r4)
  893. andi. r5,r5,1
  894. bne 4f
  895. mfspr r6,SPRN_CTRLF
  896. clrrdi r6,r6,1
  897. mtspr SPRN_CTRLT,r6
  898. 4:
  899. /* Secondary threads wait for primary to have done partition switch */
  900. ld r5, HSTATE_KVM_VCORE(r13)
  901. lbz r6, HSTATE_PTID(r13)
  902. cmpwi r6, 0
  903. beq 21f
  904. lbz r0, VCORE_IN_GUEST(r5)
  905. cmpwi r0, 0
  906. bne 21f
  907. HMT_LOW
  908. 20: lwz r3, VCORE_ENTRY_EXIT(r5)
  909. cmpwi r3, 0x100
  910. bge no_switch_exit
  911. lbz r0, VCORE_IN_GUEST(r5)
  912. cmpwi r0, 0
  913. beq 20b
  914. HMT_MEDIUM
  915. 21:
  916. /* Set LPCR. */
  917. ld r8,VCORE_LPCR(r5)
  918. mtspr SPRN_LPCR,r8
  919. isync
  920. /*
  921. * Set the decrementer to the guest decrementer.
  922. */
  923. ld r8,VCPU_DEC_EXPIRES(r4)
  924. /* r8 is a host timebase value here, convert to guest TB */
  925. ld r5,HSTATE_KVM_VCORE(r13)
  926. ld r6,VCORE_TB_OFFSET_APPL(r5)
  927. add r8,r8,r6
  928. mftb r7
  929. subf r3,r7,r8
  930. mtspr SPRN_DEC,r3
  931. /* Check if HDEC expires soon */
  932. mfspr r3, SPRN_HDEC
  933. EXTEND_HDEC(r3)
  934. cmpdi r3, 512 /* 1 microsecond */
  935. blt hdec_soon
  936. /* For hash guest, clear out and reload the SLB */
  937. ld r6, VCPU_KVM(r4)
  938. lbz r0, KVM_RADIX(r6)
  939. cmpwi r0, 0
  940. bne 9f
  941. li r6, 0
  942. slbmte r6, r6
  943. slbia
  944. ptesync
  945. /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
  946. lwz r5,VCPU_SLB_MAX(r4)
  947. cmpwi r5,0
  948. beq 9f
  949. mtctr r5
  950. addi r6,r4,VCPU_SLB
  951. 1: ld r8,VCPU_SLB_E(r6)
  952. ld r9,VCPU_SLB_V(r6)
  953. slbmte r9,r8
  954. addi r6,r6,VCPU_SLB_SIZE
  955. bdnz 1b
  956. 9:
  957. #ifdef CONFIG_KVM_XICS
  958. /* We are entering the guest on that thread, push VCPU to XIVE */
  959. ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
  960. cmpldi cr0, r10, 0
  961. beq no_xive
  962. ld r11, VCPU_XIVE_SAVED_STATE(r4)
  963. li r9, TM_QW1_OS
  964. eieio
  965. stdcix r11,r9,r10
  966. lwz r11, VCPU_XIVE_CAM_WORD(r4)
  967. li r9, TM_QW1_OS + TM_WORD2
  968. stwcix r11,r9,r10
  969. li r9, 1
  970. stb r9, VCPU_XIVE_PUSHED(r4)
  971. eieio
  972. /*
  973. * We clear the irq_pending flag. There is a small chance of a
  974. * race vs. the escalation interrupt happening on another
  975. * processor setting it again, but the only consequence is to
  976. * cause a spurrious wakeup on the next H_CEDE which is not an
  977. * issue.
  978. */
  979. li r0,0
  980. stb r0, VCPU_IRQ_PENDING(r4)
  981. /*
  982. * In single escalation mode, if the escalation interrupt is
  983. * on, we mask it.
  984. */
  985. lbz r0, VCPU_XIVE_ESC_ON(r4)
  986. cmpwi r0,0
  987. beq 1f
  988. ld r10, VCPU_XIVE_ESC_RADDR(r4)
  989. li r9, XIVE_ESB_SET_PQ_01
  990. ldcix r0, r10, r9
  991. sync
  992. /* We have a possible subtle race here: The escalation interrupt might
  993. * have fired and be on its way to the host queue while we mask it,
  994. * and if we unmask it early enough (re-cede right away), there is
  995. * a theorical possibility that it fires again, thus landing in the
  996. * target queue more than once which is a big no-no.
  997. *
  998. * Fortunately, solving this is rather easy. If the above load setting
  999. * PQ to 01 returns a previous value where P is set, then we know the
  1000. * escalation interrupt is somewhere on its way to the host. In that
  1001. * case we simply don't clear the xive_esc_on flag below. It will be
  1002. * eventually cleared by the handler for the escalation interrupt.
  1003. *
  1004. * Then, when doing a cede, we check that flag again before re-enabling
  1005. * the escalation interrupt, and if set, we abort the cede.
  1006. */
  1007. andi. r0, r0, XIVE_ESB_VAL_P
  1008. bne- 1f
  1009. /* Now P is 0, we can clear the flag */
  1010. li r0, 0
  1011. stb r0, VCPU_XIVE_ESC_ON(r4)
  1012. 1:
  1013. no_xive:
  1014. #endif /* CONFIG_KVM_XICS */
  1015. deliver_guest_interrupt:
  1016. ld r6, VCPU_CTR(r4)
  1017. ld r7, VCPU_XER(r4)
  1018. mtctr r6
  1019. mtxer r7
  1020. kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
  1021. ld r10, VCPU_PC(r4)
  1022. ld r11, VCPU_MSR(r4)
  1023. ld r6, VCPU_SRR0(r4)
  1024. ld r7, VCPU_SRR1(r4)
  1025. mtspr SPRN_SRR0, r6
  1026. mtspr SPRN_SRR1, r7
  1027. /* r11 = vcpu->arch.msr & ~MSR_HV */
  1028. rldicl r11, r11, 63 - MSR_HV_LG, 1
  1029. rotldi r11, r11, 1 + MSR_HV_LG
  1030. ori r11, r11, MSR_ME
  1031. /* Check if we can deliver an external or decrementer interrupt now */
  1032. ld r0, VCPU_PENDING_EXC(r4)
  1033. rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
  1034. cmpdi cr1, r0, 0
  1035. andi. r8, r11, MSR_EE
  1036. mfspr r8, SPRN_LPCR
  1037. /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
  1038. rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
  1039. mtspr SPRN_LPCR, r8
  1040. isync
  1041. beq 5f
  1042. li r0, BOOK3S_INTERRUPT_EXTERNAL
  1043. bne cr1, 12f
  1044. mfspr r0, SPRN_DEC
  1045. BEGIN_FTR_SECTION
  1046. /* On POWER9 check whether the guest has large decrementer enabled */
  1047. andis. r8, r8, LPCR_LD@h
  1048. bne 15f
  1049. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1050. extsw r0, r0
  1051. 15: cmpdi r0, 0
  1052. li r0, BOOK3S_INTERRUPT_DECREMENTER
  1053. bge 5f
  1054. 12: mtspr SPRN_SRR0, r10
  1055. mr r10,r0
  1056. mtspr SPRN_SRR1, r11
  1057. mr r9, r4
  1058. bl kvmppc_msr_interrupt
  1059. 5:
  1060. BEGIN_FTR_SECTION
  1061. b fast_guest_return
  1062. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  1063. /* On POWER9, check for pending doorbell requests */
  1064. lbz r0, VCPU_DBELL_REQ(r4)
  1065. cmpwi r0, 0
  1066. beq fast_guest_return
  1067. ld r5, HSTATE_KVM_VCORE(r13)
  1068. /* Set DPDES register so the CPU will take a doorbell interrupt */
  1069. li r0, 1
  1070. mtspr SPRN_DPDES, r0
  1071. std r0, VCORE_DPDES(r5)
  1072. /* Make sure other cpus see vcore->dpdes set before dbell req clear */
  1073. lwsync
  1074. /* Clear the pending doorbell request */
  1075. li r0, 0
  1076. stb r0, VCPU_DBELL_REQ(r4)
  1077. /*
  1078. * Required state:
  1079. * R4 = vcpu
  1080. * R10: value for HSRR0
  1081. * R11: value for HSRR1
  1082. * R13 = PACA
  1083. */
  1084. fast_guest_return:
  1085. li r0,0
  1086. stb r0,VCPU_CEDED(r4) /* cancel cede */
  1087. mtspr SPRN_HSRR0,r10
  1088. mtspr SPRN_HSRR1,r11
  1089. /* Activate guest mode, so faults get handled by KVM */
  1090. li r9, KVM_GUEST_MODE_GUEST_HV
  1091. stb r9, HSTATE_IN_GUEST(r13)
  1092. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1093. /* Accumulate timing */
  1094. addi r3, r4, VCPU_TB_GUEST
  1095. bl kvmhv_accumulate_time
  1096. #endif
  1097. /* Enter guest */
  1098. BEGIN_FTR_SECTION
  1099. ld r5, VCPU_CFAR(r4)
  1100. mtspr SPRN_CFAR, r5
  1101. END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
  1102. BEGIN_FTR_SECTION
  1103. ld r0, VCPU_PPR(r4)
  1104. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  1105. ld r5, VCPU_LR(r4)
  1106. ld r6, VCPU_CR(r4)
  1107. mtlr r5
  1108. mtcr r6
  1109. ld r1, VCPU_GPR(R1)(r4)
  1110. ld r2, VCPU_GPR(R2)(r4)
  1111. ld r3, VCPU_GPR(R3)(r4)
  1112. ld r5, VCPU_GPR(R5)(r4)
  1113. ld r6, VCPU_GPR(R6)(r4)
  1114. ld r7, VCPU_GPR(R7)(r4)
  1115. ld r8, VCPU_GPR(R8)(r4)
  1116. ld r9, VCPU_GPR(R9)(r4)
  1117. ld r10, VCPU_GPR(R10)(r4)
  1118. ld r11, VCPU_GPR(R11)(r4)
  1119. ld r12, VCPU_GPR(R12)(r4)
  1120. ld r13, VCPU_GPR(R13)(r4)
  1121. BEGIN_FTR_SECTION
  1122. mtspr SPRN_PPR, r0
  1123. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  1124. /* Move canary into DSISR to check for later */
  1125. BEGIN_FTR_SECTION
  1126. li r0, 0x7fff
  1127. mtspr SPRN_HDSISR, r0
  1128. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1129. ld r0, VCPU_GPR(R0)(r4)
  1130. ld r4, VCPU_GPR(R4)(r4)
  1131. HRFI_TO_GUEST
  1132. b .
  1133. secondary_too_late:
  1134. li r12, 0
  1135. stw r12, STACK_SLOT_TRAP(r1)
  1136. cmpdi r4, 0
  1137. beq 11f
  1138. stw r12, VCPU_TRAP(r4)
  1139. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1140. addi r3, r4, VCPU_TB_RMEXIT
  1141. bl kvmhv_accumulate_time
  1142. #endif
  1143. 11: b kvmhv_switch_to_host
  1144. no_switch_exit:
  1145. HMT_MEDIUM
  1146. li r12, 0
  1147. b 12f
  1148. hdec_soon:
  1149. li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
  1150. 12: stw r12, VCPU_TRAP(r4)
  1151. mr r9, r4
  1152. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1153. addi r3, r4, VCPU_TB_RMEXIT
  1154. bl kvmhv_accumulate_time
  1155. #endif
  1156. b guest_bypass
  1157. /******************************************************************************
  1158. * *
  1159. * Exit code *
  1160. * *
  1161. *****************************************************************************/
  1162. /*
  1163. * We come here from the first-level interrupt handlers.
  1164. */
  1165. .globl kvmppc_interrupt_hv
  1166. kvmppc_interrupt_hv:
  1167. /*
  1168. * Register contents:
  1169. * R12 = (guest CR << 32) | interrupt vector
  1170. * R13 = PACA
  1171. * guest R12 saved in shadow VCPU SCRATCH0
  1172. * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
  1173. * guest R13 saved in SPRN_SCRATCH0
  1174. */
  1175. std r9, HSTATE_SCRATCH2(r13)
  1176. lbz r9, HSTATE_IN_GUEST(r13)
  1177. cmpwi r9, KVM_GUEST_MODE_HOST_HV
  1178. beq kvmppc_bad_host_intr
  1179. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  1180. cmpwi r9, KVM_GUEST_MODE_GUEST
  1181. ld r9, HSTATE_SCRATCH2(r13)
  1182. beq kvmppc_interrupt_pr
  1183. #endif
  1184. /* We're now back in the host but in guest MMU context */
  1185. li r9, KVM_GUEST_MODE_HOST_HV
  1186. stb r9, HSTATE_IN_GUEST(r13)
  1187. ld r9, HSTATE_KVM_VCPU(r13)
  1188. /* Save registers */
  1189. std r0, VCPU_GPR(R0)(r9)
  1190. std r1, VCPU_GPR(R1)(r9)
  1191. std r2, VCPU_GPR(R2)(r9)
  1192. std r3, VCPU_GPR(R3)(r9)
  1193. std r4, VCPU_GPR(R4)(r9)
  1194. std r5, VCPU_GPR(R5)(r9)
  1195. std r6, VCPU_GPR(R6)(r9)
  1196. std r7, VCPU_GPR(R7)(r9)
  1197. std r8, VCPU_GPR(R8)(r9)
  1198. ld r0, HSTATE_SCRATCH2(r13)
  1199. std r0, VCPU_GPR(R9)(r9)
  1200. std r10, VCPU_GPR(R10)(r9)
  1201. std r11, VCPU_GPR(R11)(r9)
  1202. ld r3, HSTATE_SCRATCH0(r13)
  1203. std r3, VCPU_GPR(R12)(r9)
  1204. /* CR is in the high half of r12 */
  1205. srdi r4, r12, 32
  1206. std r4, VCPU_CR(r9)
  1207. BEGIN_FTR_SECTION
  1208. ld r3, HSTATE_CFAR(r13)
  1209. std r3, VCPU_CFAR(r9)
  1210. END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
  1211. BEGIN_FTR_SECTION
  1212. ld r4, HSTATE_PPR(r13)
  1213. std r4, VCPU_PPR(r9)
  1214. END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
  1215. /* Restore R1/R2 so we can handle faults */
  1216. ld r1, HSTATE_HOST_R1(r13)
  1217. ld r2, PACATOC(r13)
  1218. mfspr r10, SPRN_SRR0
  1219. mfspr r11, SPRN_SRR1
  1220. std r10, VCPU_SRR0(r9)
  1221. std r11, VCPU_SRR1(r9)
  1222. /* trap is in the low half of r12, clear CR from the high half */
  1223. clrldi r12, r12, 32
  1224. andi. r0, r12, 2 /* need to read HSRR0/1? */
  1225. beq 1f
  1226. mfspr r10, SPRN_HSRR0
  1227. mfspr r11, SPRN_HSRR1
  1228. clrrdi r12, r12, 2
  1229. 1: std r10, VCPU_PC(r9)
  1230. std r11, VCPU_MSR(r9)
  1231. GET_SCRATCH0(r3)
  1232. mflr r4
  1233. std r3, VCPU_GPR(R13)(r9)
  1234. std r4, VCPU_LR(r9)
  1235. stw r12,VCPU_TRAP(r9)
  1236. /*
  1237. * Now that we have saved away SRR0/1 and HSRR0/1,
  1238. * interrupts are recoverable in principle, so set MSR_RI.
  1239. * This becomes important for relocation-on interrupts from
  1240. * the guest, which we can get in radix mode on POWER9.
  1241. */
  1242. li r0, MSR_RI
  1243. mtmsrd r0, 1
  1244. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1245. addi r3, r9, VCPU_TB_RMINTR
  1246. mr r4, r9
  1247. bl kvmhv_accumulate_time
  1248. ld r5, VCPU_GPR(R5)(r9)
  1249. ld r6, VCPU_GPR(R6)(r9)
  1250. ld r7, VCPU_GPR(R7)(r9)
  1251. ld r8, VCPU_GPR(R8)(r9)
  1252. #endif
  1253. /* Save HEIR (HV emulation assist reg) in emul_inst
  1254. if this is an HEI (HV emulation interrupt, e40) */
  1255. li r3,KVM_INST_FETCH_FAILED
  1256. stw r3,VCPU_LAST_INST(r9)
  1257. cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
  1258. bne 11f
  1259. mfspr r3,SPRN_HEIR
  1260. 11: stw r3,VCPU_HEIR(r9)
  1261. /* these are volatile across C function calls */
  1262. #ifdef CONFIG_RELOCATABLE
  1263. ld r3, HSTATE_SCRATCH1(r13)
  1264. mtctr r3
  1265. #else
  1266. mfctr r3
  1267. #endif
  1268. mfxer r4
  1269. std r3, VCPU_CTR(r9)
  1270. std r4, VCPU_XER(r9)
  1271. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1272. /* For softpatch interrupt, go off and do TM instruction emulation */
  1273. cmpwi r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
  1274. beq kvmppc_tm_emul
  1275. #endif
  1276. /* If this is a page table miss then see if it's theirs or ours */
  1277. cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
  1278. beq kvmppc_hdsi
  1279. cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
  1280. beq kvmppc_hisi
  1281. /* See if this is a leftover HDEC interrupt */
  1282. cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
  1283. bne 2f
  1284. mfspr r3,SPRN_HDEC
  1285. EXTEND_HDEC(r3)
  1286. cmpdi r3,0
  1287. mr r4,r9
  1288. bge fast_guest_return
  1289. 2:
  1290. /* See if this is an hcall we can handle in real mode */
  1291. cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
  1292. beq hcall_try_real_mode
  1293. /* Hypervisor doorbell - exit only if host IPI flag set */
  1294. cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
  1295. bne 3f
  1296. BEGIN_FTR_SECTION
  1297. PPC_MSGSYNC
  1298. lwsync
  1299. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1300. lbz r0, HSTATE_HOST_IPI(r13)
  1301. cmpwi r0, 0
  1302. beq 4f
  1303. b guest_exit_cont
  1304. 3:
  1305. /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
  1306. cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
  1307. bne 14f
  1308. mfspr r3, SPRN_HFSCR
  1309. std r3, VCPU_HFSCR(r9)
  1310. b guest_exit_cont
  1311. 14:
  1312. /* External interrupt ? */
  1313. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  1314. bne+ guest_exit_cont
  1315. /* External interrupt, first check for host_ipi. If this is
  1316. * set, we know the host wants us out so let's do it now
  1317. */
  1318. bl kvmppc_read_intr
  1319. /*
  1320. * Restore the active volatile registers after returning from
  1321. * a C function.
  1322. */
  1323. ld r9, HSTATE_KVM_VCPU(r13)
  1324. li r12, BOOK3S_INTERRUPT_EXTERNAL
  1325. /*
  1326. * kvmppc_read_intr return codes:
  1327. *
  1328. * Exit to host (r3 > 0)
  1329. * 1 An interrupt is pending that needs to be handled by the host
  1330. * Exit guest and return to host by branching to guest_exit_cont
  1331. *
  1332. * 2 Passthrough that needs completion in the host
  1333. * Exit guest and return to host by branching to guest_exit_cont
  1334. * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
  1335. * to indicate to the host to complete handling the interrupt
  1336. *
  1337. * Before returning to guest, we check if any CPU is heading out
  1338. * to the host and if so, we head out also. If no CPUs are heading
  1339. * check return values <= 0.
  1340. *
  1341. * Return to guest (r3 <= 0)
  1342. * 0 No external interrupt is pending
  1343. * -1 A guest wakeup IPI (which has now been cleared)
  1344. * In either case, we return to guest to deliver any pending
  1345. * guest interrupts.
  1346. *
  1347. * -2 A PCI passthrough external interrupt was handled
  1348. * (interrupt was delivered directly to guest)
  1349. * Return to guest to deliver any pending guest interrupts.
  1350. */
  1351. cmpdi r3, 1
  1352. ble 1f
  1353. /* Return code = 2 */
  1354. li r12, BOOK3S_INTERRUPT_HV_RM_HARD
  1355. stw r12, VCPU_TRAP(r9)
  1356. b guest_exit_cont
  1357. 1: /* Return code <= 1 */
  1358. cmpdi r3, 0
  1359. bgt guest_exit_cont
  1360. /* Return code <= 0 */
  1361. 4: ld r5, HSTATE_KVM_VCORE(r13)
  1362. lwz r0, VCORE_ENTRY_EXIT(r5)
  1363. cmpwi r0, 0x100
  1364. mr r4, r9
  1365. blt deliver_guest_interrupt
  1366. guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
  1367. /* Save more register state */
  1368. mfdar r6
  1369. mfdsisr r7
  1370. std r6, VCPU_DAR(r9)
  1371. stw r7, VCPU_DSISR(r9)
  1372. /* don't overwrite fault_dar/fault_dsisr if HDSI */
  1373. cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
  1374. beq mc_cont
  1375. std r6, VCPU_FAULT_DAR(r9)
  1376. stw r7, VCPU_FAULT_DSISR(r9)
  1377. /* See if it is a machine check */
  1378. cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  1379. beq machine_check_realmode
  1380. mc_cont:
  1381. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1382. addi r3, r9, VCPU_TB_RMEXIT
  1383. mr r4, r9
  1384. bl kvmhv_accumulate_time
  1385. #endif
  1386. #ifdef CONFIG_KVM_XICS
  1387. /* We are exiting, pull the VP from the XIVE */
  1388. lbz r0, VCPU_XIVE_PUSHED(r9)
  1389. cmpwi cr0, r0, 0
  1390. beq 1f
  1391. li r7, TM_SPC_PULL_OS_CTX
  1392. li r6, TM_QW1_OS
  1393. mfmsr r0
  1394. andi. r0, r0, MSR_DR /* in real mode? */
  1395. beq 2f
  1396. ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
  1397. cmpldi cr0, r10, 0
  1398. beq 1f
  1399. /* First load to pull the context, we ignore the value */
  1400. eieio
  1401. lwzx r11, r7, r10
  1402. /* Second load to recover the context state (Words 0 and 1) */
  1403. ldx r11, r6, r10
  1404. b 3f
  1405. 2: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
  1406. cmpldi cr0, r10, 0
  1407. beq 1f
  1408. /* First load to pull the context, we ignore the value */
  1409. eieio
  1410. lwzcix r11, r7, r10
  1411. /* Second load to recover the context state (Words 0 and 1) */
  1412. ldcix r11, r6, r10
  1413. 3: std r11, VCPU_XIVE_SAVED_STATE(r9)
  1414. /* Fixup some of the state for the next load */
  1415. li r10, 0
  1416. li r0, 0xff
  1417. stb r10, VCPU_XIVE_PUSHED(r9)
  1418. stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
  1419. stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
  1420. eieio
  1421. 1:
  1422. #endif /* CONFIG_KVM_XICS */
  1423. /* For hash guest, read the guest SLB and save it away */
  1424. ld r5, VCPU_KVM(r9)
  1425. lbz r0, KVM_RADIX(r5)
  1426. li r5, 0
  1427. cmpwi r0, 0
  1428. bne 3f /* for radix, save 0 entries */
  1429. lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
  1430. mtctr r0
  1431. li r6,0
  1432. addi r7,r9,VCPU_SLB
  1433. 1: slbmfee r8,r6
  1434. andis. r0,r8,SLB_ESID_V@h
  1435. beq 2f
  1436. add r8,r8,r6 /* put index in */
  1437. slbmfev r3,r6
  1438. std r8,VCPU_SLB_E(r7)
  1439. std r3,VCPU_SLB_V(r7)
  1440. addi r7,r7,VCPU_SLB_SIZE
  1441. addi r5,r5,1
  1442. 2: addi r6,r6,1
  1443. bdnz 1b
  1444. /* Finally clear out the SLB */
  1445. li r0,0
  1446. slbmte r0,r0
  1447. slbia
  1448. ptesync
  1449. 3: stw r5,VCPU_SLB_MAX(r9)
  1450. /* load host SLB entries */
  1451. BEGIN_MMU_FTR_SECTION
  1452. b 0f
  1453. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
  1454. ld r8,PACA_SLBSHADOWPTR(r13)
  1455. .rept SLB_NUM_BOLTED
  1456. li r3, SLBSHADOW_SAVEAREA
  1457. LDX_BE r5, r8, r3
  1458. addi r3, r3, 8
  1459. LDX_BE r6, r8, r3
  1460. andis. r7,r5,SLB_ESID_V@h
  1461. beq 1f
  1462. slbmte r6,r5
  1463. 1: addi r8,r8,16
  1464. .endr
  1465. 0:
  1466. guest_bypass:
  1467. stw r12, STACK_SLOT_TRAP(r1)
  1468. /* Save DEC */
  1469. /* Do this before kvmhv_commence_exit so we know TB is guest TB */
  1470. ld r3, HSTATE_KVM_VCORE(r13)
  1471. mfspr r5,SPRN_DEC
  1472. mftb r6
  1473. /* On P9, if the guest has large decr enabled, don't sign extend */
  1474. BEGIN_FTR_SECTION
  1475. ld r4, VCORE_LPCR(r3)
  1476. andis. r4, r4, LPCR_LD@h
  1477. bne 16f
  1478. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1479. extsw r5,r5
  1480. 16: add r5,r5,r6
  1481. /* r5 is a guest timebase value here, convert to host TB */
  1482. ld r4,VCORE_TB_OFFSET_APPL(r3)
  1483. subf r5,r4,r5
  1484. std r5,VCPU_DEC_EXPIRES(r9)
  1485. /* Increment exit count, poke other threads to exit */
  1486. mr r3, r12
  1487. bl kvmhv_commence_exit
  1488. nop
  1489. ld r9, HSTATE_KVM_VCPU(r13)
  1490. /* Stop others sending VCPU interrupts to this physical CPU */
  1491. li r0, -1
  1492. stw r0, VCPU_CPU(r9)
  1493. stw r0, VCPU_THREAD_CPU(r9)
  1494. /* Save guest CTRL register, set runlatch to 1 */
  1495. mfspr r6,SPRN_CTRLF
  1496. stw r6,VCPU_CTRL(r9)
  1497. andi. r0,r6,1
  1498. bne 4f
  1499. ori r6,r6,1
  1500. mtspr SPRN_CTRLT,r6
  1501. 4:
  1502. /*
  1503. * Save the guest PURR/SPURR
  1504. */
  1505. mfspr r5,SPRN_PURR
  1506. mfspr r6,SPRN_SPURR
  1507. ld r7,VCPU_PURR(r9)
  1508. ld r8,VCPU_SPURR(r9)
  1509. std r5,VCPU_PURR(r9)
  1510. std r6,VCPU_SPURR(r9)
  1511. subf r5,r7,r5
  1512. subf r6,r8,r6
  1513. /*
  1514. * Restore host PURR/SPURR and add guest times
  1515. * so that the time in the guest gets accounted.
  1516. */
  1517. ld r3,HSTATE_PURR(r13)
  1518. ld r4,HSTATE_SPURR(r13)
  1519. add r3,r3,r5
  1520. add r4,r4,r6
  1521. mtspr SPRN_PURR,r3
  1522. mtspr SPRN_SPURR,r4
  1523. BEGIN_FTR_SECTION
  1524. b 8f
  1525. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
  1526. /* Save POWER8-specific registers */
  1527. mfspr r5, SPRN_IAMR
  1528. mfspr r6, SPRN_PSPB
  1529. mfspr r7, SPRN_FSCR
  1530. std r5, VCPU_IAMR(r9)
  1531. stw r6, VCPU_PSPB(r9)
  1532. std r7, VCPU_FSCR(r9)
  1533. mfspr r5, SPRN_IC
  1534. mfspr r7, SPRN_TAR
  1535. std r5, VCPU_IC(r9)
  1536. std r7, VCPU_TAR(r9)
  1537. mfspr r8, SPRN_EBBHR
  1538. std r8, VCPU_EBBHR(r9)
  1539. mfspr r5, SPRN_EBBRR
  1540. mfspr r6, SPRN_BESCR
  1541. mfspr r7, SPRN_PID
  1542. mfspr r8, SPRN_WORT
  1543. std r5, VCPU_EBBRR(r9)
  1544. std r6, VCPU_BESCR(r9)
  1545. stw r7, VCPU_GUEST_PID(r9)
  1546. std r8, VCPU_WORT(r9)
  1547. BEGIN_FTR_SECTION
  1548. mfspr r5, SPRN_TCSCR
  1549. mfspr r6, SPRN_ACOP
  1550. mfspr r7, SPRN_CSIGR
  1551. mfspr r8, SPRN_TACR
  1552. std r5, VCPU_TCSCR(r9)
  1553. std r6, VCPU_ACOP(r9)
  1554. std r7, VCPU_CSIGR(r9)
  1555. std r8, VCPU_TACR(r9)
  1556. FTR_SECTION_ELSE
  1557. mfspr r5, SPRN_TIDR
  1558. mfspr r6, SPRN_PSSCR
  1559. std r5, VCPU_TID(r9)
  1560. rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
  1561. rotldi r6, r6, 60
  1562. std r6, VCPU_PSSCR(r9)
  1563. /* Restore host HFSCR value */
  1564. ld r7, STACK_SLOT_HFSCR(r1)
  1565. mtspr SPRN_HFSCR, r7
  1566. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
  1567. /*
  1568. * Restore various registers to 0, where non-zero values
  1569. * set by the guest could disrupt the host.
  1570. */
  1571. li r0, 0
  1572. mtspr SPRN_PSPB, r0
  1573. mtspr SPRN_WORT, r0
  1574. BEGIN_FTR_SECTION
  1575. mtspr SPRN_TCSCR, r0
  1576. /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
  1577. li r0, 1
  1578. sldi r0, r0, 31
  1579. mtspr SPRN_MMCRS, r0
  1580. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  1581. /* Save and restore AMR, IAMR and UAMOR before turning on the MMU */
  1582. ld r8, STACK_SLOT_IAMR(r1)
  1583. mtspr SPRN_IAMR, r8
  1584. 8: /* Power7 jumps back in here */
  1585. mfspr r5,SPRN_AMR
  1586. mfspr r6,SPRN_UAMOR
  1587. std r5,VCPU_AMR(r9)
  1588. std r6,VCPU_UAMOR(r9)
  1589. ld r5,STACK_SLOT_AMR(r1)
  1590. ld r6,STACK_SLOT_UAMOR(r1)
  1591. mtspr SPRN_AMR, r5
  1592. mtspr SPRN_UAMOR, r6
  1593. /* Switch DSCR back to host value */
  1594. mfspr r8, SPRN_DSCR
  1595. ld r7, HSTATE_DSCR(r13)
  1596. std r8, VCPU_DSCR(r9)
  1597. mtspr SPRN_DSCR, r7
  1598. /* Save non-volatile GPRs */
  1599. std r14, VCPU_GPR(R14)(r9)
  1600. std r15, VCPU_GPR(R15)(r9)
  1601. std r16, VCPU_GPR(R16)(r9)
  1602. std r17, VCPU_GPR(R17)(r9)
  1603. std r18, VCPU_GPR(R18)(r9)
  1604. std r19, VCPU_GPR(R19)(r9)
  1605. std r20, VCPU_GPR(R20)(r9)
  1606. std r21, VCPU_GPR(R21)(r9)
  1607. std r22, VCPU_GPR(R22)(r9)
  1608. std r23, VCPU_GPR(R23)(r9)
  1609. std r24, VCPU_GPR(R24)(r9)
  1610. std r25, VCPU_GPR(R25)(r9)
  1611. std r26, VCPU_GPR(R26)(r9)
  1612. std r27, VCPU_GPR(R27)(r9)
  1613. std r28, VCPU_GPR(R28)(r9)
  1614. std r29, VCPU_GPR(R29)(r9)
  1615. std r30, VCPU_GPR(R30)(r9)
  1616. std r31, VCPU_GPR(R31)(r9)
  1617. /* Save SPRGs */
  1618. mfspr r3, SPRN_SPRG0
  1619. mfspr r4, SPRN_SPRG1
  1620. mfspr r5, SPRN_SPRG2
  1621. mfspr r6, SPRN_SPRG3
  1622. std r3, VCPU_SPRG0(r9)
  1623. std r4, VCPU_SPRG1(r9)
  1624. std r5, VCPU_SPRG2(r9)
  1625. std r6, VCPU_SPRG3(r9)
  1626. /* save FP state */
  1627. mr r3, r9
  1628. bl kvmppc_save_fp
  1629. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1630. /*
  1631. * Branch around the call if both CPU_FTR_TM and
  1632. * CPU_FTR_P9_TM_HV_ASSIST are off.
  1633. */
  1634. BEGIN_FTR_SECTION
  1635. b 91f
  1636. END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
  1637. /*
  1638. * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
  1639. */
  1640. mr r3, r9
  1641. ld r4, VCPU_MSR(r3)
  1642. bl kvmppc_save_tm_hv
  1643. ld r9, HSTATE_KVM_VCPU(r13)
  1644. 91:
  1645. #endif
  1646. /* Increment yield count if they have a VPA */
  1647. ld r8, VCPU_VPA(r9) /* do they have a VPA? */
  1648. cmpdi r8, 0
  1649. beq 25f
  1650. li r4, LPPACA_YIELDCOUNT
  1651. LWZX_BE r3, r8, r4
  1652. addi r3, r3, 1
  1653. STWX_BE r3, r8, r4
  1654. li r3, 1
  1655. stb r3, VCPU_VPA_DIRTY(r9)
  1656. 25:
  1657. /* Save PMU registers if requested */
  1658. /* r8 and cr0.eq are live here */
  1659. BEGIN_FTR_SECTION
  1660. /*
  1661. * POWER8 seems to have a hardware bug where setting
  1662. * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
  1663. * when some counters are already negative doesn't seem
  1664. * to cause a performance monitor alert (and hence interrupt).
  1665. * The effect of this is that when saving the PMU state,
  1666. * if there is no PMU alert pending when we read MMCR0
  1667. * before freezing the counters, but one becomes pending
  1668. * before we read the counters, we lose it.
  1669. * To work around this, we need a way to freeze the counters
  1670. * before reading MMCR0. Normally, freezing the counters
  1671. * is done by writing MMCR0 (to set MMCR0[FC]) which
  1672. * unavoidably writes MMCR0[PMA0] as well. On POWER8,
  1673. * we can also freeze the counters using MMCR2, by writing
  1674. * 1s to all the counter freeze condition bits (there are
  1675. * 9 bits each for 6 counters).
  1676. */
  1677. li r3, -1 /* set all freeze bits */
  1678. clrrdi r3, r3, 10
  1679. mfspr r10, SPRN_MMCR2
  1680. mtspr SPRN_MMCR2, r3
  1681. isync
  1682. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1683. li r3, 1
  1684. sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
  1685. mfspr r4, SPRN_MMCR0 /* save MMCR0 */
  1686. mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
  1687. mfspr r6, SPRN_MMCRA
  1688. /* Clear MMCRA in order to disable SDAR updates */
  1689. li r7, 0
  1690. mtspr SPRN_MMCRA, r7
  1691. isync
  1692. beq 21f /* if no VPA, save PMU stuff anyway */
  1693. lbz r7, LPPACA_PMCINUSE(r8)
  1694. cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
  1695. bne 21f
  1696. std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
  1697. b 22f
  1698. 21: mfspr r5, SPRN_MMCR1
  1699. mfspr r7, SPRN_SIAR
  1700. mfspr r8, SPRN_SDAR
  1701. std r4, VCPU_MMCR(r9)
  1702. std r5, VCPU_MMCR + 8(r9)
  1703. std r6, VCPU_MMCR + 16(r9)
  1704. BEGIN_FTR_SECTION
  1705. std r10, VCPU_MMCR + 24(r9)
  1706. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1707. std r7, VCPU_SIAR(r9)
  1708. std r8, VCPU_SDAR(r9)
  1709. mfspr r3, SPRN_PMC1
  1710. mfspr r4, SPRN_PMC2
  1711. mfspr r5, SPRN_PMC3
  1712. mfspr r6, SPRN_PMC4
  1713. mfspr r7, SPRN_PMC5
  1714. mfspr r8, SPRN_PMC6
  1715. stw r3, VCPU_PMC(r9)
  1716. stw r4, VCPU_PMC + 4(r9)
  1717. stw r5, VCPU_PMC + 8(r9)
  1718. stw r6, VCPU_PMC + 12(r9)
  1719. stw r7, VCPU_PMC + 16(r9)
  1720. stw r8, VCPU_PMC + 20(r9)
  1721. BEGIN_FTR_SECTION
  1722. mfspr r5, SPRN_SIER
  1723. std r5, VCPU_SIER(r9)
  1724. BEGIN_FTR_SECTION_NESTED(96)
  1725. mfspr r6, SPRN_SPMC1
  1726. mfspr r7, SPRN_SPMC2
  1727. mfspr r8, SPRN_MMCRS
  1728. stw r6, VCPU_PMC + 24(r9)
  1729. stw r7, VCPU_PMC + 28(r9)
  1730. std r8, VCPU_MMCR + 32(r9)
  1731. lis r4, 0x8000
  1732. mtspr SPRN_MMCRS, r4
  1733. END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
  1734. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1735. 22:
  1736. /* Restore host values of some registers */
  1737. BEGIN_FTR_SECTION
  1738. ld r5, STACK_SLOT_CIABR(r1)
  1739. ld r6, STACK_SLOT_DAWR(r1)
  1740. ld r7, STACK_SLOT_DAWRX(r1)
  1741. mtspr SPRN_CIABR, r5
  1742. /*
  1743. * If the DAWR doesn't work, it's ok to write these here as
  1744. * this value should always be zero
  1745. */
  1746. mtspr SPRN_DAWR, r6
  1747. mtspr SPRN_DAWRX, r7
  1748. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1749. BEGIN_FTR_SECTION
  1750. ld r5, STACK_SLOT_TID(r1)
  1751. ld r6, STACK_SLOT_PSSCR(r1)
  1752. ld r7, STACK_SLOT_PID(r1)
  1753. mtspr SPRN_TIDR, r5
  1754. mtspr SPRN_PSSCR, r6
  1755. mtspr SPRN_PID, r7
  1756. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1757. #ifdef CONFIG_PPC_RADIX_MMU
  1758. /*
  1759. * Are we running hash or radix ?
  1760. */
  1761. ld r5, VCPU_KVM(r9)
  1762. lbz r0, KVM_RADIX(r5)
  1763. cmpwi cr2, r0, 0
  1764. beq cr2, 2f
  1765. /*
  1766. * Radix: do eieio; tlbsync; ptesync sequence in case we
  1767. * interrupted the guest between a tlbie and a ptesync.
  1768. */
  1769. eieio
  1770. tlbsync
  1771. ptesync
  1772. /* Radix: Handle the case where the guest used an illegal PID */
  1773. LOAD_REG_ADDR(r4, mmu_base_pid)
  1774. lwz r3, VCPU_GUEST_PID(r9)
  1775. lwz r5, 0(r4)
  1776. cmpw cr0,r3,r5
  1777. blt 2f
  1778. /*
  1779. * Illegal PID, the HW might have prefetched and cached in the TLB
  1780. * some translations for the LPID 0 / guest PID combination which
  1781. * Linux doesn't know about, so we need to flush that PID out of
  1782. * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
  1783. * the right context.
  1784. */
  1785. li r0,0
  1786. mtspr SPRN_LPID,r0
  1787. isync
  1788. /* Then do a congruence class local flush */
  1789. ld r6,VCPU_KVM(r9)
  1790. lwz r0,KVM_TLB_SETS(r6)
  1791. mtctr r0
  1792. li r7,0x400 /* IS field = 0b01 */
  1793. ptesync
  1794. sldi r0,r3,32 /* RS has PID */
  1795. 1: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
  1796. addi r7,r7,0x1000
  1797. bdnz 1b
  1798. ptesync
  1799. 2:
  1800. #endif /* CONFIG_PPC_RADIX_MMU */
  1801. /*
  1802. * POWER7/POWER8 guest -> host partition switch code.
  1803. * We don't have to lock against tlbies but we do
  1804. * have to coordinate the hardware threads.
  1805. * Here STACK_SLOT_TRAP(r1) contains the trap number.
  1806. */
  1807. kvmhv_switch_to_host:
  1808. /* Secondary threads wait for primary to do partition switch */
  1809. ld r5,HSTATE_KVM_VCORE(r13)
  1810. ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
  1811. lbz r3,HSTATE_PTID(r13)
  1812. cmpwi r3,0
  1813. beq 15f
  1814. HMT_LOW
  1815. 13: lbz r3,VCORE_IN_GUEST(r5)
  1816. cmpwi r3,0
  1817. bne 13b
  1818. HMT_MEDIUM
  1819. b 16f
  1820. /* Primary thread waits for all the secondaries to exit guest */
  1821. 15: lwz r3,VCORE_ENTRY_EXIT(r5)
  1822. rlwinm r0,r3,32-8,0xff
  1823. clrldi r3,r3,56
  1824. cmpw r3,r0
  1825. bne 15b
  1826. isync
  1827. /* Did we actually switch to the guest at all? */
  1828. lbz r6, VCORE_IN_GUEST(r5)
  1829. cmpwi r6, 0
  1830. beq 19f
  1831. /* Primary thread switches back to host partition */
  1832. lwz r7,KVM_HOST_LPID(r4)
  1833. BEGIN_FTR_SECTION
  1834. ld r6,KVM_HOST_SDR1(r4)
  1835. li r8,LPID_RSVD /* switch to reserved LPID */
  1836. mtspr SPRN_LPID,r8
  1837. ptesync
  1838. mtspr SPRN_SDR1,r6 /* switch to host page table */
  1839. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  1840. mtspr SPRN_LPID,r7
  1841. isync
  1842. BEGIN_FTR_SECTION
  1843. /* DPDES and VTB are shared between threads */
  1844. mfspr r7, SPRN_DPDES
  1845. mfspr r8, SPRN_VTB
  1846. std r7, VCORE_DPDES(r5)
  1847. std r8, VCORE_VTB(r5)
  1848. /* clear DPDES so we don't get guest doorbells in the host */
  1849. li r8, 0
  1850. mtspr SPRN_DPDES, r8
  1851. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  1852. /* If HMI, call kvmppc_realmode_hmi_handler() */
  1853. lwz r12, STACK_SLOT_TRAP(r1)
  1854. cmpwi r12, BOOK3S_INTERRUPT_HMI
  1855. bne 27f
  1856. bl kvmppc_realmode_hmi_handler
  1857. nop
  1858. cmpdi r3, 0
  1859. /*
  1860. * At this point kvmppc_realmode_hmi_handler may have resync-ed
  1861. * the TB, and if it has, we must not subtract the guest timebase
  1862. * offset from the timebase. So, skip it.
  1863. *
  1864. * Also, do not call kvmppc_subcore_exit_guest() because it has
  1865. * been invoked as part of kvmppc_realmode_hmi_handler().
  1866. */
  1867. beq 30f
  1868. 27:
  1869. /* Subtract timebase offset from timebase */
  1870. ld r8, VCORE_TB_OFFSET_APPL(r5)
  1871. cmpdi r8,0
  1872. beq 17f
  1873. li r0, 0
  1874. std r0, VCORE_TB_OFFSET_APPL(r5)
  1875. mftb r6 /* current guest timebase */
  1876. subf r8,r8,r6
  1877. mtspr SPRN_TBU40,r8 /* update upper 40 bits */
  1878. mftb r7 /* check if lower 24 bits overflowed */
  1879. clrldi r6,r6,40
  1880. clrldi r7,r7,40
  1881. cmpld r7,r6
  1882. bge 17f
  1883. addis r8,r8,0x100 /* if so, increment upper 40 bits */
  1884. mtspr SPRN_TBU40,r8
  1885. 17: bl kvmppc_subcore_exit_guest
  1886. nop
  1887. 30: ld r5,HSTATE_KVM_VCORE(r13)
  1888. ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
  1889. /* Reset PCR */
  1890. ld r0, VCORE_PCR(r5)
  1891. cmpdi r0, 0
  1892. beq 18f
  1893. li r0, 0
  1894. mtspr SPRN_PCR, r0
  1895. 18:
  1896. /* Signal secondary CPUs to continue */
  1897. stb r0,VCORE_IN_GUEST(r5)
  1898. 19: lis r8,0x7fff /* MAX_INT@h */
  1899. mtspr SPRN_HDEC,r8
  1900. 16:
  1901. BEGIN_FTR_SECTION
  1902. /* On POWER9 with HPT-on-radix we need to wait for all other threads */
  1903. ld r3, HSTATE_SPLIT_MODE(r13)
  1904. cmpdi r3, 0
  1905. beq 47f
  1906. lwz r8, KVM_SPLIT_DO_RESTORE(r3)
  1907. cmpwi r8, 0
  1908. beq 47f
  1909. bl kvmhv_p9_restore_lpcr
  1910. nop
  1911. b 48f
  1912. 47:
  1913. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1914. ld r8,KVM_HOST_LPCR(r4)
  1915. mtspr SPRN_LPCR,r8
  1916. isync
  1917. 48:
  1918. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1919. /* Finish timing, if we have a vcpu */
  1920. ld r4, HSTATE_KVM_VCPU(r13)
  1921. cmpdi r4, 0
  1922. li r3, 0
  1923. beq 2f
  1924. bl kvmhv_accumulate_time
  1925. 2:
  1926. #endif
  1927. /* Unset guest mode */
  1928. li r0, KVM_GUEST_MODE_NONE
  1929. stb r0, HSTATE_IN_GUEST(r13)
  1930. lwz r12, STACK_SLOT_TRAP(r1) /* return trap # in r12 */
  1931. ld r0, SFS+PPC_LR_STKOFF(r1)
  1932. addi r1, r1, SFS
  1933. mtlr r0
  1934. blr
  1935. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1936. /*
  1937. * Softpatch interrupt for transactional memory emulation cases
  1938. * on POWER9 DD2.2. This is early in the guest exit path - we
  1939. * haven't saved registers or done a treclaim yet.
  1940. */
  1941. kvmppc_tm_emul:
  1942. /* Save instruction image in HEIR */
  1943. mfspr r3, SPRN_HEIR
  1944. stw r3, VCPU_HEIR(r9)
  1945. /*
  1946. * The cases we want to handle here are those where the guest
  1947. * is in real suspend mode and is trying to transition to
  1948. * transactional mode.
  1949. */
  1950. lbz r0, HSTATE_FAKE_SUSPEND(r13)
  1951. cmpwi r0, 0 /* keep exiting guest if in fake suspend */
  1952. bne guest_exit_cont
  1953. rldicl r3, r11, 64 - MSR_TS_S_LG, 62
  1954. cmpwi r3, 1 /* or if not in suspend state */
  1955. bne guest_exit_cont
  1956. /* Call C code to do the emulation */
  1957. mr r3, r9
  1958. bl kvmhv_p9_tm_emulation_early
  1959. nop
  1960. ld r9, HSTATE_KVM_VCPU(r13)
  1961. li r12, BOOK3S_INTERRUPT_HV_SOFTPATCH
  1962. cmpwi r3, 0
  1963. beq guest_exit_cont /* continue exiting if not handled */
  1964. ld r10, VCPU_PC(r9)
  1965. ld r11, VCPU_MSR(r9)
  1966. b fast_interrupt_c_return /* go back to guest if handled */
  1967. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1968. /*
  1969. * Check whether an HDSI is an HPTE not found fault or something else.
  1970. * If it is an HPTE not found fault that is due to the guest accessing
  1971. * a page that they have mapped but which we have paged out, then
  1972. * we continue on with the guest exit path. In all other cases,
  1973. * reflect the HDSI to the guest as a DSI.
  1974. */
  1975. kvmppc_hdsi:
  1976. ld r3, VCPU_KVM(r9)
  1977. lbz r0, KVM_RADIX(r3)
  1978. mfspr r4, SPRN_HDAR
  1979. mfspr r6, SPRN_HDSISR
  1980. BEGIN_FTR_SECTION
  1981. /* Look for DSISR canary. If we find it, retry instruction */
  1982. cmpdi r6, 0x7fff
  1983. beq 6f
  1984. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1985. cmpwi r0, 0
  1986. bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
  1987. /* HPTE not found fault or protection fault? */
  1988. andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
  1989. beq 1f /* if not, send it to the guest */
  1990. andi. r0, r11, MSR_DR /* data relocation enabled? */
  1991. beq 3f
  1992. BEGIN_FTR_SECTION
  1993. mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
  1994. b 4f
  1995. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  1996. clrrdi r0, r4, 28
  1997. PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
  1998. li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
  1999. bne 7f /* if no SLB entry found */
  2000. 4: std r4, VCPU_FAULT_DAR(r9)
  2001. stw r6, VCPU_FAULT_DSISR(r9)
  2002. /* Search the hash table. */
  2003. mr r3, r9 /* vcpu pointer */
  2004. li r7, 1 /* data fault */
  2005. bl kvmppc_hpte_hv_fault
  2006. ld r9, HSTATE_KVM_VCPU(r13)
  2007. ld r10, VCPU_PC(r9)
  2008. ld r11, VCPU_MSR(r9)
  2009. li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
  2010. cmpdi r3, 0 /* retry the instruction */
  2011. beq 6f
  2012. cmpdi r3, -1 /* handle in kernel mode */
  2013. beq guest_exit_cont
  2014. cmpdi r3, -2 /* MMIO emulation; need instr word */
  2015. beq 2f
  2016. /* Synthesize a DSI (or DSegI) for the guest */
  2017. ld r4, VCPU_FAULT_DAR(r9)
  2018. mr r6, r3
  2019. 1: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
  2020. mtspr SPRN_DSISR, r6
  2021. 7: mtspr SPRN_DAR, r4
  2022. mtspr SPRN_SRR0, r10
  2023. mtspr SPRN_SRR1, r11
  2024. mr r10, r0
  2025. bl kvmppc_msr_interrupt
  2026. fast_interrupt_c_return:
  2027. 6: ld r7, VCPU_CTR(r9)
  2028. ld r8, VCPU_XER(r9)
  2029. mtctr r7
  2030. mtxer r8
  2031. mr r4, r9
  2032. b fast_guest_return
  2033. 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
  2034. ld r5, KVM_VRMA_SLB_V(r5)
  2035. b 4b
  2036. /* If this is for emulated MMIO, load the instruction word */
  2037. 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
  2038. /* Set guest mode to 'jump over instruction' so if lwz faults
  2039. * we'll just continue at the next IP. */
  2040. li r0, KVM_GUEST_MODE_SKIP
  2041. stb r0, HSTATE_IN_GUEST(r13)
  2042. /* Do the access with MSR:DR enabled */
  2043. mfmsr r3
  2044. ori r4, r3, MSR_DR /* Enable paging for data */
  2045. mtmsrd r4
  2046. lwz r8, 0(r10)
  2047. mtmsrd r3
  2048. /* Store the result */
  2049. stw r8, VCPU_LAST_INST(r9)
  2050. /* Unset guest mode. */
  2051. li r0, KVM_GUEST_MODE_HOST_HV
  2052. stb r0, HSTATE_IN_GUEST(r13)
  2053. b guest_exit_cont
  2054. .Lradix_hdsi:
  2055. std r4, VCPU_FAULT_DAR(r9)
  2056. stw r6, VCPU_FAULT_DSISR(r9)
  2057. .Lradix_hisi:
  2058. mfspr r5, SPRN_ASDR
  2059. std r5, VCPU_FAULT_GPA(r9)
  2060. b guest_exit_cont
  2061. /*
  2062. * Similarly for an HISI, reflect it to the guest as an ISI unless
  2063. * it is an HPTE not found fault for a page that we have paged out.
  2064. */
  2065. kvmppc_hisi:
  2066. ld r3, VCPU_KVM(r9)
  2067. lbz r0, KVM_RADIX(r3)
  2068. cmpwi r0, 0
  2069. bne .Lradix_hisi /* for radix, just save ASDR */
  2070. andis. r0, r11, SRR1_ISI_NOPT@h
  2071. beq 1f
  2072. andi. r0, r11, MSR_IR /* instruction relocation enabled? */
  2073. beq 3f
  2074. BEGIN_FTR_SECTION
  2075. mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
  2076. b 4f
  2077. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  2078. clrrdi r0, r10, 28
  2079. PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
  2080. li r0, BOOK3S_INTERRUPT_INST_SEGMENT
  2081. bne 7f /* if no SLB entry found */
  2082. 4:
  2083. /* Search the hash table. */
  2084. mr r3, r9 /* vcpu pointer */
  2085. mr r4, r10
  2086. mr r6, r11
  2087. li r7, 0 /* instruction fault */
  2088. bl kvmppc_hpte_hv_fault
  2089. ld r9, HSTATE_KVM_VCPU(r13)
  2090. ld r10, VCPU_PC(r9)
  2091. ld r11, VCPU_MSR(r9)
  2092. li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
  2093. cmpdi r3, 0 /* retry the instruction */
  2094. beq fast_interrupt_c_return
  2095. cmpdi r3, -1 /* handle in kernel mode */
  2096. beq guest_exit_cont
  2097. /* Synthesize an ISI (or ISegI) for the guest */
  2098. mr r11, r3
  2099. 1: li r0, BOOK3S_INTERRUPT_INST_STORAGE
  2100. 7: mtspr SPRN_SRR0, r10
  2101. mtspr SPRN_SRR1, r11
  2102. mr r10, r0
  2103. bl kvmppc_msr_interrupt
  2104. b fast_interrupt_c_return
  2105. 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
  2106. ld r5, KVM_VRMA_SLB_V(r6)
  2107. b 4b
  2108. /*
  2109. * Try to handle an hcall in real mode.
  2110. * Returns to the guest if we handle it, or continues on up to
  2111. * the kernel if we can't (i.e. if we don't have a handler for
  2112. * it, or if the handler returns H_TOO_HARD).
  2113. *
  2114. * r5 - r8 contain hcall args,
  2115. * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
  2116. */
  2117. hcall_try_real_mode:
  2118. ld r3,VCPU_GPR(R3)(r9)
  2119. andi. r0,r11,MSR_PR
  2120. /* sc 1 from userspace - reflect to guest syscall */
  2121. bne sc_1_fast_return
  2122. clrrdi r3,r3,2
  2123. cmpldi r3,hcall_real_table_end - hcall_real_table
  2124. bge guest_exit_cont
  2125. /* See if this hcall is enabled for in-kernel handling */
  2126. ld r4, VCPU_KVM(r9)
  2127. srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
  2128. sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
  2129. add r4, r4, r0
  2130. ld r0, KVM_ENABLED_HCALLS(r4)
  2131. rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
  2132. srd r0, r0, r4
  2133. andi. r0, r0, 1
  2134. beq guest_exit_cont
  2135. /* Get pointer to handler, if any, and call it */
  2136. LOAD_REG_ADDR(r4, hcall_real_table)
  2137. lwax r3,r3,r4
  2138. cmpwi r3,0
  2139. beq guest_exit_cont
  2140. add r12,r3,r4
  2141. mtctr r12
  2142. mr r3,r9 /* get vcpu pointer */
  2143. ld r4,VCPU_GPR(R4)(r9)
  2144. bctrl
  2145. cmpdi r3,H_TOO_HARD
  2146. beq hcall_real_fallback
  2147. ld r4,HSTATE_KVM_VCPU(r13)
  2148. std r3,VCPU_GPR(R3)(r4)
  2149. ld r10,VCPU_PC(r4)
  2150. ld r11,VCPU_MSR(r4)
  2151. b fast_guest_return
  2152. sc_1_fast_return:
  2153. mtspr SPRN_SRR0,r10
  2154. mtspr SPRN_SRR1,r11
  2155. li r10, BOOK3S_INTERRUPT_SYSCALL
  2156. bl kvmppc_msr_interrupt
  2157. mr r4,r9
  2158. b fast_guest_return
  2159. /* We've attempted a real mode hcall, but it's punted it back
  2160. * to userspace. We need to restore some clobbered volatiles
  2161. * before resuming the pass-it-to-qemu path */
  2162. hcall_real_fallback:
  2163. li r12,BOOK3S_INTERRUPT_SYSCALL
  2164. ld r9, HSTATE_KVM_VCPU(r13)
  2165. b guest_exit_cont
  2166. .globl hcall_real_table
  2167. hcall_real_table:
  2168. .long 0 /* 0 - unused */
  2169. .long DOTSYM(kvmppc_h_remove) - hcall_real_table
  2170. .long DOTSYM(kvmppc_h_enter) - hcall_real_table
  2171. .long DOTSYM(kvmppc_h_read) - hcall_real_table
  2172. .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
  2173. .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
  2174. .long DOTSYM(kvmppc_h_protect) - hcall_real_table
  2175. .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
  2176. .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
  2177. .long 0 /* 0x24 - H_SET_SPRG0 */
  2178. .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
  2179. .long 0 /* 0x2c */
  2180. .long 0 /* 0x30 */
  2181. .long 0 /* 0x34 */
  2182. .long 0 /* 0x38 */
  2183. .long 0 /* 0x3c */
  2184. .long 0 /* 0x40 */
  2185. .long 0 /* 0x44 */
  2186. .long 0 /* 0x48 */
  2187. .long 0 /* 0x4c */
  2188. .long 0 /* 0x50 */
  2189. .long 0 /* 0x54 */
  2190. .long 0 /* 0x58 */
  2191. .long 0 /* 0x5c */
  2192. .long 0 /* 0x60 */
  2193. #ifdef CONFIG_KVM_XICS
  2194. .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
  2195. .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
  2196. .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
  2197. .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
  2198. .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
  2199. #else
  2200. .long 0 /* 0x64 - H_EOI */
  2201. .long 0 /* 0x68 - H_CPPR */
  2202. .long 0 /* 0x6c - H_IPI */
  2203. .long 0 /* 0x70 - H_IPOLL */
  2204. .long 0 /* 0x74 - H_XIRR */
  2205. #endif
  2206. .long 0 /* 0x78 */
  2207. .long 0 /* 0x7c */
  2208. .long 0 /* 0x80 */
  2209. .long 0 /* 0x84 */
  2210. .long 0 /* 0x88 */
  2211. .long 0 /* 0x8c */
  2212. .long 0 /* 0x90 */
  2213. .long 0 /* 0x94 */
  2214. .long 0 /* 0x98 */
  2215. .long 0 /* 0x9c */
  2216. .long 0 /* 0xa0 */
  2217. .long 0 /* 0xa4 */
  2218. .long 0 /* 0xa8 */
  2219. .long 0 /* 0xac */
  2220. .long 0 /* 0xb0 */
  2221. .long 0 /* 0xb4 */
  2222. .long 0 /* 0xb8 */
  2223. .long 0 /* 0xbc */
  2224. .long 0 /* 0xc0 */
  2225. .long 0 /* 0xc4 */
  2226. .long 0 /* 0xc8 */
  2227. .long 0 /* 0xcc */
  2228. .long 0 /* 0xd0 */
  2229. .long 0 /* 0xd4 */
  2230. .long 0 /* 0xd8 */
  2231. .long 0 /* 0xdc */
  2232. .long DOTSYM(kvmppc_h_cede) - hcall_real_table
  2233. .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
  2234. .long 0 /* 0xe8 */
  2235. .long 0 /* 0xec */
  2236. .long 0 /* 0xf0 */
  2237. .long 0 /* 0xf4 */
  2238. .long 0 /* 0xf8 */
  2239. .long 0 /* 0xfc */
  2240. .long 0 /* 0x100 */
  2241. .long 0 /* 0x104 */
  2242. .long 0 /* 0x108 */
  2243. .long 0 /* 0x10c */
  2244. .long 0 /* 0x110 */
  2245. .long 0 /* 0x114 */
  2246. .long 0 /* 0x118 */
  2247. .long 0 /* 0x11c */
  2248. .long 0 /* 0x120 */
  2249. .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
  2250. .long 0 /* 0x128 */
  2251. .long 0 /* 0x12c */
  2252. .long 0 /* 0x130 */
  2253. .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
  2254. .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
  2255. .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
  2256. .long 0 /* 0x140 */
  2257. .long 0 /* 0x144 */
  2258. .long 0 /* 0x148 */
  2259. .long 0 /* 0x14c */
  2260. .long 0 /* 0x150 */
  2261. .long 0 /* 0x154 */
  2262. .long 0 /* 0x158 */
  2263. .long 0 /* 0x15c */
  2264. .long 0 /* 0x160 */
  2265. .long 0 /* 0x164 */
  2266. .long 0 /* 0x168 */
  2267. .long 0 /* 0x16c */
  2268. .long 0 /* 0x170 */
  2269. .long 0 /* 0x174 */
  2270. .long 0 /* 0x178 */
  2271. .long 0 /* 0x17c */
  2272. .long 0 /* 0x180 */
  2273. .long 0 /* 0x184 */
  2274. .long 0 /* 0x188 */
  2275. .long 0 /* 0x18c */
  2276. .long 0 /* 0x190 */
  2277. .long 0 /* 0x194 */
  2278. .long 0 /* 0x198 */
  2279. .long 0 /* 0x19c */
  2280. .long 0 /* 0x1a0 */
  2281. .long 0 /* 0x1a4 */
  2282. .long 0 /* 0x1a8 */
  2283. .long 0 /* 0x1ac */
  2284. .long 0 /* 0x1b0 */
  2285. .long 0 /* 0x1b4 */
  2286. .long 0 /* 0x1b8 */
  2287. .long 0 /* 0x1bc */
  2288. .long 0 /* 0x1c0 */
  2289. .long 0 /* 0x1c4 */
  2290. .long 0 /* 0x1c8 */
  2291. .long 0 /* 0x1cc */
  2292. .long 0 /* 0x1d0 */
  2293. .long 0 /* 0x1d4 */
  2294. .long 0 /* 0x1d8 */
  2295. .long 0 /* 0x1dc */
  2296. .long 0 /* 0x1e0 */
  2297. .long 0 /* 0x1e4 */
  2298. .long 0 /* 0x1e8 */
  2299. .long 0 /* 0x1ec */
  2300. .long 0 /* 0x1f0 */
  2301. .long 0 /* 0x1f4 */
  2302. .long 0 /* 0x1f8 */
  2303. .long 0 /* 0x1fc */
  2304. .long 0 /* 0x200 */
  2305. .long 0 /* 0x204 */
  2306. .long 0 /* 0x208 */
  2307. .long 0 /* 0x20c */
  2308. .long 0 /* 0x210 */
  2309. .long 0 /* 0x214 */
  2310. .long 0 /* 0x218 */
  2311. .long 0 /* 0x21c */
  2312. .long 0 /* 0x220 */
  2313. .long 0 /* 0x224 */
  2314. .long 0 /* 0x228 */
  2315. .long 0 /* 0x22c */
  2316. .long 0 /* 0x230 */
  2317. .long 0 /* 0x234 */
  2318. .long 0 /* 0x238 */
  2319. .long 0 /* 0x23c */
  2320. .long 0 /* 0x240 */
  2321. .long 0 /* 0x244 */
  2322. .long 0 /* 0x248 */
  2323. .long 0 /* 0x24c */
  2324. .long 0 /* 0x250 */
  2325. .long 0 /* 0x254 */
  2326. .long 0 /* 0x258 */
  2327. .long 0 /* 0x25c */
  2328. .long 0 /* 0x260 */
  2329. .long 0 /* 0x264 */
  2330. .long 0 /* 0x268 */
  2331. .long 0 /* 0x26c */
  2332. .long 0 /* 0x270 */
  2333. .long 0 /* 0x274 */
  2334. .long 0 /* 0x278 */
  2335. .long 0 /* 0x27c */
  2336. .long 0 /* 0x280 */
  2337. .long 0 /* 0x284 */
  2338. .long 0 /* 0x288 */
  2339. .long 0 /* 0x28c */
  2340. .long 0 /* 0x290 */
  2341. .long 0 /* 0x294 */
  2342. .long 0 /* 0x298 */
  2343. .long 0 /* 0x29c */
  2344. .long 0 /* 0x2a0 */
  2345. .long 0 /* 0x2a4 */
  2346. .long 0 /* 0x2a8 */
  2347. .long 0 /* 0x2ac */
  2348. .long 0 /* 0x2b0 */
  2349. .long 0 /* 0x2b4 */
  2350. .long 0 /* 0x2b8 */
  2351. .long 0 /* 0x2bc */
  2352. .long 0 /* 0x2c0 */
  2353. .long 0 /* 0x2c4 */
  2354. .long 0 /* 0x2c8 */
  2355. .long 0 /* 0x2cc */
  2356. .long 0 /* 0x2d0 */
  2357. .long 0 /* 0x2d4 */
  2358. .long 0 /* 0x2d8 */
  2359. .long 0 /* 0x2dc */
  2360. .long 0 /* 0x2e0 */
  2361. .long 0 /* 0x2e4 */
  2362. .long 0 /* 0x2e8 */
  2363. .long 0 /* 0x2ec */
  2364. .long 0 /* 0x2f0 */
  2365. .long 0 /* 0x2f4 */
  2366. .long 0 /* 0x2f8 */
  2367. #ifdef CONFIG_KVM_XICS
  2368. .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
  2369. #else
  2370. .long 0 /* 0x2fc - H_XIRR_X*/
  2371. #endif
  2372. .long DOTSYM(kvmppc_h_random) - hcall_real_table
  2373. .globl hcall_real_table_end
  2374. hcall_real_table_end:
  2375. _GLOBAL(kvmppc_h_set_xdabr)
  2376. andi. r0, r5, DABRX_USER | DABRX_KERNEL
  2377. beq 6f
  2378. li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
  2379. andc. r0, r5, r0
  2380. beq 3f
  2381. 6: li r3, H_PARAMETER
  2382. blr
  2383. _GLOBAL(kvmppc_h_set_dabr)
  2384. li r5, DABRX_USER | DABRX_KERNEL
  2385. 3:
  2386. BEGIN_FTR_SECTION
  2387. b 2f
  2388. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  2389. std r4,VCPU_DABR(r3)
  2390. stw r5, VCPU_DABRX(r3)
  2391. mtspr SPRN_DABRX, r5
  2392. /* Work around P7 bug where DABR can get corrupted on mtspr */
  2393. 1: mtspr SPRN_DABR,r4
  2394. mfspr r5, SPRN_DABR
  2395. cmpd r4, r5
  2396. bne 1b
  2397. isync
  2398. li r3,0
  2399. blr
  2400. 2:
  2401. BEGIN_FTR_SECTION
  2402. /* POWER9 with disabled DAWR */
  2403. li r3, H_HARDWARE
  2404. blr
  2405. END_FTR_SECTION_IFCLR(CPU_FTR_DAWR)
  2406. /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
  2407. rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
  2408. rlwimi r5, r4, 2, DAWRX_WT
  2409. clrrdi r4, r4, 3
  2410. std r4, VCPU_DAWR(r3)
  2411. std r5, VCPU_DAWRX(r3)
  2412. mtspr SPRN_DAWR, r4
  2413. mtspr SPRN_DAWRX, r5
  2414. li r3, 0
  2415. blr
  2416. _GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
  2417. ori r11,r11,MSR_EE
  2418. std r11,VCPU_MSR(r3)
  2419. li r0,1
  2420. stb r0,VCPU_CEDED(r3)
  2421. sync /* order setting ceded vs. testing prodded */
  2422. lbz r5,VCPU_PRODDED(r3)
  2423. cmpwi r5,0
  2424. bne kvm_cede_prodded
  2425. li r12,0 /* set trap to 0 to say hcall is handled */
  2426. stw r12,VCPU_TRAP(r3)
  2427. li r0,H_SUCCESS
  2428. std r0,VCPU_GPR(R3)(r3)
  2429. /*
  2430. * Set our bit in the bitmask of napping threads unless all the
  2431. * other threads are already napping, in which case we send this
  2432. * up to the host.
  2433. */
  2434. ld r5,HSTATE_KVM_VCORE(r13)
  2435. lbz r6,HSTATE_PTID(r13)
  2436. lwz r8,VCORE_ENTRY_EXIT(r5)
  2437. clrldi r8,r8,56
  2438. li r0,1
  2439. sld r0,r0,r6
  2440. addi r6,r5,VCORE_NAPPING_THREADS
  2441. 31: lwarx r4,0,r6
  2442. or r4,r4,r0
  2443. cmpw r4,r8
  2444. beq kvm_cede_exit
  2445. stwcx. r4,0,r6
  2446. bne 31b
  2447. /* order napping_threads update vs testing entry_exit_map */
  2448. isync
  2449. li r0,NAPPING_CEDE
  2450. stb r0,HSTATE_NAPPING(r13)
  2451. lwz r7,VCORE_ENTRY_EXIT(r5)
  2452. cmpwi r7,0x100
  2453. bge 33f /* another thread already exiting */
  2454. /*
  2455. * Although not specifically required by the architecture, POWER7
  2456. * preserves the following registers in nap mode, even if an SMT mode
  2457. * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
  2458. * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
  2459. */
  2460. /* Save non-volatile GPRs */
  2461. std r14, VCPU_GPR(R14)(r3)
  2462. std r15, VCPU_GPR(R15)(r3)
  2463. std r16, VCPU_GPR(R16)(r3)
  2464. std r17, VCPU_GPR(R17)(r3)
  2465. std r18, VCPU_GPR(R18)(r3)
  2466. std r19, VCPU_GPR(R19)(r3)
  2467. std r20, VCPU_GPR(R20)(r3)
  2468. std r21, VCPU_GPR(R21)(r3)
  2469. std r22, VCPU_GPR(R22)(r3)
  2470. std r23, VCPU_GPR(R23)(r3)
  2471. std r24, VCPU_GPR(R24)(r3)
  2472. std r25, VCPU_GPR(R25)(r3)
  2473. std r26, VCPU_GPR(R26)(r3)
  2474. std r27, VCPU_GPR(R27)(r3)
  2475. std r28, VCPU_GPR(R28)(r3)
  2476. std r29, VCPU_GPR(R29)(r3)
  2477. std r30, VCPU_GPR(R30)(r3)
  2478. std r31, VCPU_GPR(R31)(r3)
  2479. /* save FP state */
  2480. bl kvmppc_save_fp
  2481. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2482. /*
  2483. * Branch around the call if both CPU_FTR_TM and
  2484. * CPU_FTR_P9_TM_HV_ASSIST are off.
  2485. */
  2486. BEGIN_FTR_SECTION
  2487. b 91f
  2488. END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
  2489. /*
  2490. * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
  2491. */
  2492. ld r3, HSTATE_KVM_VCPU(r13)
  2493. ld r4, VCPU_MSR(r3)
  2494. bl kvmppc_save_tm_hv
  2495. 91:
  2496. #endif
  2497. /*
  2498. * Set DEC to the smaller of DEC and HDEC, so that we wake
  2499. * no later than the end of our timeslice (HDEC interrupts
  2500. * don't wake us from nap).
  2501. */
  2502. mfspr r3, SPRN_DEC
  2503. mfspr r4, SPRN_HDEC
  2504. mftb r5
  2505. BEGIN_FTR_SECTION
  2506. /* On P9 check whether the guest has large decrementer mode enabled */
  2507. ld r6, HSTATE_KVM_VCORE(r13)
  2508. ld r6, VCORE_LPCR(r6)
  2509. andis. r6, r6, LPCR_LD@h
  2510. bne 68f
  2511. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  2512. extsw r3, r3
  2513. 68: EXTEND_HDEC(r4)
  2514. cmpd r3, r4
  2515. ble 67f
  2516. mtspr SPRN_DEC, r4
  2517. 67:
  2518. /* save expiry time of guest decrementer */
  2519. add r3, r3, r5
  2520. ld r4, HSTATE_KVM_VCPU(r13)
  2521. ld r5, HSTATE_KVM_VCORE(r13)
  2522. ld r6, VCORE_TB_OFFSET_APPL(r5)
  2523. subf r3, r6, r3 /* convert to host TB value */
  2524. std r3, VCPU_DEC_EXPIRES(r4)
  2525. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  2526. ld r4, HSTATE_KVM_VCPU(r13)
  2527. addi r3, r4, VCPU_TB_CEDE
  2528. bl kvmhv_accumulate_time
  2529. #endif
  2530. lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
  2531. /*
  2532. * Take a nap until a decrementer or external or doobell interrupt
  2533. * occurs, with PECE1 and PECE0 set in LPCR.
  2534. * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
  2535. * Also clear the runlatch bit before napping.
  2536. */
  2537. kvm_do_nap:
  2538. mfspr r0, SPRN_CTRLF
  2539. clrrdi r0, r0, 1
  2540. mtspr SPRN_CTRLT, r0
  2541. li r0,1
  2542. stb r0,HSTATE_HWTHREAD_REQ(r13)
  2543. mfspr r5,SPRN_LPCR
  2544. ori r5,r5,LPCR_PECE0 | LPCR_PECE1
  2545. BEGIN_FTR_SECTION
  2546. ori r5, r5, LPCR_PECEDH
  2547. rlwimi r5, r3, 0, LPCR_PECEDP
  2548. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  2549. kvm_nap_sequence: /* desired LPCR value in r5 */
  2550. BEGIN_FTR_SECTION
  2551. /*
  2552. * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
  2553. * enable state loss = 1 (allow SMT mode switch)
  2554. * requested level = 0 (just stop dispatching)
  2555. */
  2556. lis r3, (PSSCR_EC | PSSCR_ESL)@h
  2557. mtspr SPRN_PSSCR, r3
  2558. /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
  2559. li r4, LPCR_PECE_HVEE@higher
  2560. sldi r4, r4, 32
  2561. or r5, r5, r4
  2562. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  2563. mtspr SPRN_LPCR,r5
  2564. isync
  2565. li r0, 0
  2566. std r0, HSTATE_SCRATCH0(r13)
  2567. ptesync
  2568. ld r0, HSTATE_SCRATCH0(r13)
  2569. 1: cmpd r0, r0
  2570. bne 1b
  2571. BEGIN_FTR_SECTION
  2572. nap
  2573. FTR_SECTION_ELSE
  2574. PPC_STOP
  2575. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
  2576. b .
  2577. 33: mr r4, r3
  2578. li r3, 0
  2579. li r12, 0
  2580. b 34f
  2581. kvm_end_cede:
  2582. /* get vcpu pointer */
  2583. ld r4, HSTATE_KVM_VCPU(r13)
  2584. /* Woken by external or decrementer interrupt */
  2585. ld r1, HSTATE_HOST_R1(r13)
  2586. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  2587. addi r3, r4, VCPU_TB_RMINTR
  2588. bl kvmhv_accumulate_time
  2589. #endif
  2590. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2591. /*
  2592. * Branch around the call if both CPU_FTR_TM and
  2593. * CPU_FTR_P9_TM_HV_ASSIST are off.
  2594. */
  2595. BEGIN_FTR_SECTION
  2596. b 91f
  2597. END_FTR_SECTION(CPU_FTR_TM | CPU_FTR_P9_TM_HV_ASSIST, 0)
  2598. /*
  2599. * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
  2600. */
  2601. mr r3, r4
  2602. ld r4, VCPU_MSR(r3)
  2603. bl kvmppc_restore_tm_hv
  2604. ld r4, HSTATE_KVM_VCPU(r13)
  2605. 91:
  2606. #endif
  2607. /* load up FP state */
  2608. bl kvmppc_load_fp
  2609. /* Restore guest decrementer */
  2610. ld r3, VCPU_DEC_EXPIRES(r4)
  2611. ld r5, HSTATE_KVM_VCORE(r13)
  2612. ld r6, VCORE_TB_OFFSET_APPL(r5)
  2613. add r3, r3, r6 /* convert host TB to guest TB value */
  2614. mftb r7
  2615. subf r3, r7, r3
  2616. mtspr SPRN_DEC, r3
  2617. /* Load NV GPRS */
  2618. ld r14, VCPU_GPR(R14)(r4)
  2619. ld r15, VCPU_GPR(R15)(r4)
  2620. ld r16, VCPU_GPR(R16)(r4)
  2621. ld r17, VCPU_GPR(R17)(r4)
  2622. ld r18, VCPU_GPR(R18)(r4)
  2623. ld r19, VCPU_GPR(R19)(r4)
  2624. ld r20, VCPU_GPR(R20)(r4)
  2625. ld r21, VCPU_GPR(R21)(r4)
  2626. ld r22, VCPU_GPR(R22)(r4)
  2627. ld r23, VCPU_GPR(R23)(r4)
  2628. ld r24, VCPU_GPR(R24)(r4)
  2629. ld r25, VCPU_GPR(R25)(r4)
  2630. ld r26, VCPU_GPR(R26)(r4)
  2631. ld r27, VCPU_GPR(R27)(r4)
  2632. ld r28, VCPU_GPR(R28)(r4)
  2633. ld r29, VCPU_GPR(R29)(r4)
  2634. ld r30, VCPU_GPR(R30)(r4)
  2635. ld r31, VCPU_GPR(R31)(r4)
  2636. /* Check the wake reason in SRR1 to see why we got here */
  2637. bl kvmppc_check_wake_reason
  2638. /*
  2639. * Restore volatile registers since we could have called a
  2640. * C routine in kvmppc_check_wake_reason
  2641. * r4 = VCPU
  2642. * r3 tells us whether we need to return to host or not
  2643. * WARNING: it gets checked further down:
  2644. * should not modify r3 until this check is done.
  2645. */
  2646. ld r4, HSTATE_KVM_VCPU(r13)
  2647. /* clear our bit in vcore->napping_threads */
  2648. 34: ld r5,HSTATE_KVM_VCORE(r13)
  2649. lbz r7,HSTATE_PTID(r13)
  2650. li r0,1
  2651. sld r0,r0,r7
  2652. addi r6,r5,VCORE_NAPPING_THREADS
  2653. 32: lwarx r7,0,r6
  2654. andc r7,r7,r0
  2655. stwcx. r7,0,r6
  2656. bne 32b
  2657. li r0,0
  2658. stb r0,HSTATE_NAPPING(r13)
  2659. /* See if the wake reason saved in r3 means we need to exit */
  2660. stw r12, VCPU_TRAP(r4)
  2661. mr r9, r4
  2662. cmpdi r3, 0
  2663. bgt guest_exit_cont
  2664. /* see if any other thread is already exiting */
  2665. lwz r0,VCORE_ENTRY_EXIT(r5)
  2666. cmpwi r0,0x100
  2667. bge guest_exit_cont
  2668. b kvmppc_cede_reentry /* if not go back to guest */
  2669. /* cede when already previously prodded case */
  2670. kvm_cede_prodded:
  2671. li r0,0
  2672. stb r0,VCPU_PRODDED(r3)
  2673. sync /* order testing prodded vs. clearing ceded */
  2674. stb r0,VCPU_CEDED(r3)
  2675. li r3,H_SUCCESS
  2676. blr
  2677. /* we've ceded but we want to give control to the host */
  2678. kvm_cede_exit:
  2679. ld r9, HSTATE_KVM_VCPU(r13)
  2680. #ifdef CONFIG_KVM_XICS
  2681. /* are we using XIVE with single escalation? */
  2682. ld r10, VCPU_XIVE_ESC_VADDR(r9)
  2683. cmpdi r10, 0
  2684. beq 3f
  2685. li r6, XIVE_ESB_SET_PQ_00
  2686. /*
  2687. * If we still have a pending escalation, abort the cede,
  2688. * and we must set PQ to 10 rather than 00 so that we don't
  2689. * potentially end up with two entries for the escalation
  2690. * interrupt in the XIVE interrupt queue. In that case
  2691. * we also don't want to set xive_esc_on to 1 here in
  2692. * case we race with xive_esc_irq().
  2693. */
  2694. lbz r5, VCPU_XIVE_ESC_ON(r9)
  2695. cmpwi r5, 0
  2696. beq 4f
  2697. li r0, 0
  2698. stb r0, VCPU_CEDED(r9)
  2699. li r6, XIVE_ESB_SET_PQ_10
  2700. b 5f
  2701. 4: li r0, 1
  2702. stb r0, VCPU_XIVE_ESC_ON(r9)
  2703. /* make sure store to xive_esc_on is seen before xive_esc_irq runs */
  2704. sync
  2705. 5: /* Enable XIVE escalation */
  2706. mfmsr r0
  2707. andi. r0, r0, MSR_DR /* in real mode? */
  2708. beq 1f
  2709. ldx r0, r10, r6
  2710. b 2f
  2711. 1: ld r10, VCPU_XIVE_ESC_RADDR(r9)
  2712. ldcix r0, r10, r6
  2713. 2: sync
  2714. #endif /* CONFIG_KVM_XICS */
  2715. 3: b guest_exit_cont
  2716. /* Try to handle a machine check in real mode */
  2717. machine_check_realmode:
  2718. mr r3, r9 /* get vcpu pointer */
  2719. bl kvmppc_realmode_machine_check
  2720. nop
  2721. ld r9, HSTATE_KVM_VCPU(r13)
  2722. li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  2723. /*
  2724. * For the guest that is FWNMI capable, deliver all the MCE errors
  2725. * (handled/unhandled) by exiting the guest with KVM_EXIT_NMI exit
  2726. * reason. This new approach injects machine check errors in guest
  2727. * address space to guest with additional information in the form
  2728. * of RTAS event, thus enabling guest kernel to suitably handle
  2729. * such errors.
  2730. *
  2731. * For the guest that is not FWNMI capable (old QEMU) fallback
  2732. * to old behaviour for backward compatibility:
  2733. * Deliver unhandled/fatal (e.g. UE) MCE errors to guest either
  2734. * through machine check interrupt (set HSRR0 to 0x200).
  2735. * For handled errors (no-fatal), just go back to guest execution
  2736. * with current HSRR0.
  2737. * if we receive machine check with MSR(RI=0) then deliver it to
  2738. * guest as machine check causing guest to crash.
  2739. */
  2740. ld r11, VCPU_MSR(r9)
  2741. rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
  2742. bne mc_cont /* if so, exit to host */
  2743. /* Check if guest is capable of handling NMI exit */
  2744. ld r10, VCPU_KVM(r9)
  2745. lbz r10, KVM_FWNMI(r10)
  2746. cmpdi r10, 1 /* FWNMI capable? */
  2747. beq mc_cont /* if so, exit with KVM_EXIT_NMI. */
  2748. /* if not, fall through for backward compatibility. */
  2749. andi. r10, r11, MSR_RI /* check for unrecoverable exception */
  2750. beq 1f /* Deliver a machine check to guest */
  2751. ld r10, VCPU_PC(r9)
  2752. cmpdi r3, 0 /* Did we handle MCE ? */
  2753. bne 2f /* Continue guest execution. */
  2754. /* If not, deliver a machine check. SRR0/1 are already set */
  2755. 1: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
  2756. bl kvmppc_msr_interrupt
  2757. 2: b fast_interrupt_c_return
  2758. /*
  2759. * Check the reason we woke from nap, and take appropriate action.
  2760. * Returns (in r3):
  2761. * 0 if nothing needs to be done
  2762. * 1 if something happened that needs to be handled by the host
  2763. * -1 if there was a guest wakeup (IPI or msgsnd)
  2764. * -2 if we handled a PCI passthrough interrupt (returned by
  2765. * kvmppc_read_intr only)
  2766. *
  2767. * Also sets r12 to the interrupt vector for any interrupt that needs
  2768. * to be handled now by the host (0x500 for external interrupt), or zero.
  2769. * Modifies all volatile registers (since it may call a C function).
  2770. * This routine calls kvmppc_read_intr, a C function, if an external
  2771. * interrupt is pending.
  2772. */
  2773. kvmppc_check_wake_reason:
  2774. mfspr r6, SPRN_SRR1
  2775. BEGIN_FTR_SECTION
  2776. rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
  2777. FTR_SECTION_ELSE
  2778. rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
  2779. ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
  2780. cmpwi r6, 8 /* was it an external interrupt? */
  2781. beq 7f /* if so, see what it was */
  2782. li r3, 0
  2783. li r12, 0
  2784. cmpwi r6, 6 /* was it the decrementer? */
  2785. beq 0f
  2786. BEGIN_FTR_SECTION
  2787. cmpwi r6, 5 /* privileged doorbell? */
  2788. beq 0f
  2789. cmpwi r6, 3 /* hypervisor doorbell? */
  2790. beq 3f
  2791. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  2792. cmpwi r6, 0xa /* Hypervisor maintenance ? */
  2793. beq 4f
  2794. li r3, 1 /* anything else, return 1 */
  2795. 0: blr
  2796. /* hypervisor doorbell */
  2797. 3: li r12, BOOK3S_INTERRUPT_H_DOORBELL
  2798. /*
  2799. * Clear the doorbell as we will invoke the handler
  2800. * explicitly in the guest exit path.
  2801. */
  2802. lis r6, (PPC_DBELL_SERVER << (63-36))@h
  2803. PPC_MSGCLR(6)
  2804. /* see if it's a host IPI */
  2805. li r3, 1
  2806. BEGIN_FTR_SECTION
  2807. PPC_MSGSYNC
  2808. lwsync
  2809. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
  2810. lbz r0, HSTATE_HOST_IPI(r13)
  2811. cmpwi r0, 0
  2812. bnelr
  2813. /* if not, return -1 */
  2814. li r3, -1
  2815. blr
  2816. /* Woken up due to Hypervisor maintenance interrupt */
  2817. 4: li r12, BOOK3S_INTERRUPT_HMI
  2818. li r3, 1
  2819. blr
  2820. /* external interrupt - create a stack frame so we can call C */
  2821. 7: mflr r0
  2822. std r0, PPC_LR_STKOFF(r1)
  2823. stdu r1, -PPC_MIN_STKFRM(r1)
  2824. bl kvmppc_read_intr
  2825. nop
  2826. li r12, BOOK3S_INTERRUPT_EXTERNAL
  2827. cmpdi r3, 1
  2828. ble 1f
  2829. /*
  2830. * Return code of 2 means PCI passthrough interrupt, but
  2831. * we need to return back to host to complete handling the
  2832. * interrupt. Trap reason is expected in r12 by guest
  2833. * exit code.
  2834. */
  2835. li r12, BOOK3S_INTERRUPT_HV_RM_HARD
  2836. 1:
  2837. ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
  2838. addi r1, r1, PPC_MIN_STKFRM
  2839. mtlr r0
  2840. blr
  2841. /*
  2842. * Save away FP, VMX and VSX registers.
  2843. * r3 = vcpu pointer
  2844. * N.B. r30 and r31 are volatile across this function,
  2845. * thus it is not callable from C.
  2846. */
  2847. kvmppc_save_fp:
  2848. mflr r30
  2849. mr r31,r3
  2850. mfmsr r5
  2851. ori r8,r5,MSR_FP
  2852. #ifdef CONFIG_ALTIVEC
  2853. BEGIN_FTR_SECTION
  2854. oris r8,r8,MSR_VEC@h
  2855. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2856. #endif
  2857. #ifdef CONFIG_VSX
  2858. BEGIN_FTR_SECTION
  2859. oris r8,r8,MSR_VSX@h
  2860. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  2861. #endif
  2862. mtmsrd r8
  2863. addi r3,r3,VCPU_FPRS
  2864. bl store_fp_state
  2865. #ifdef CONFIG_ALTIVEC
  2866. BEGIN_FTR_SECTION
  2867. addi r3,r31,VCPU_VRS
  2868. bl store_vr_state
  2869. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2870. #endif
  2871. mfspr r6,SPRN_VRSAVE
  2872. stw r6,VCPU_VRSAVE(r31)
  2873. mtlr r30
  2874. blr
  2875. /*
  2876. * Load up FP, VMX and VSX registers
  2877. * r4 = vcpu pointer
  2878. * N.B. r30 and r31 are volatile across this function,
  2879. * thus it is not callable from C.
  2880. */
  2881. kvmppc_load_fp:
  2882. mflr r30
  2883. mr r31,r4
  2884. mfmsr r9
  2885. ori r8,r9,MSR_FP
  2886. #ifdef CONFIG_ALTIVEC
  2887. BEGIN_FTR_SECTION
  2888. oris r8,r8,MSR_VEC@h
  2889. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2890. #endif
  2891. #ifdef CONFIG_VSX
  2892. BEGIN_FTR_SECTION
  2893. oris r8,r8,MSR_VSX@h
  2894. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  2895. #endif
  2896. mtmsrd r8
  2897. addi r3,r4,VCPU_FPRS
  2898. bl load_fp_state
  2899. #ifdef CONFIG_ALTIVEC
  2900. BEGIN_FTR_SECTION
  2901. addi r3,r31,VCPU_VRS
  2902. bl load_vr_state
  2903. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  2904. #endif
  2905. lwz r7,VCPU_VRSAVE(r31)
  2906. mtspr SPRN_VRSAVE,r7
  2907. mtlr r30
  2908. mr r4,r31
  2909. blr
  2910. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2911. /*
  2912. * Save transactional state and TM-related registers.
  2913. * Called with r3 pointing to the vcpu struct and r4 containing
  2914. * the guest MSR value.
  2915. * This can modify all checkpointed registers, but
  2916. * restores r1 and r2 before exit.
  2917. */
  2918. kvmppc_save_tm_hv:
  2919. /* See if we need to handle fake suspend mode */
  2920. BEGIN_FTR_SECTION
  2921. b __kvmppc_save_tm
  2922. END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
  2923. lbz r0, HSTATE_FAKE_SUSPEND(r13) /* Were we fake suspended? */
  2924. cmpwi r0, 0
  2925. beq __kvmppc_save_tm
  2926. /* The following code handles the fake_suspend = 1 case */
  2927. mflr r0
  2928. std r0, PPC_LR_STKOFF(r1)
  2929. stdu r1, -PPC_MIN_STKFRM(r1)
  2930. /* Turn on TM. */
  2931. mfmsr r8
  2932. li r0, 1
  2933. rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
  2934. mtmsrd r8
  2935. rldicl. r8, r8, 64 - MSR_TS_S_LG, 62 /* Did we actually hrfid? */
  2936. beq 4f
  2937. BEGIN_FTR_SECTION
  2938. bl pnv_power9_force_smt4_catch
  2939. END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
  2940. nop
  2941. std r1, HSTATE_HOST_R1(r13)
  2942. /* Clear the MSR RI since r1, r13 may be foobar. */
  2943. li r5, 0
  2944. mtmsrd r5, 1
  2945. /* We have to treclaim here because that's the only way to do S->N */
  2946. li r3, TM_CAUSE_KVM_RESCHED
  2947. TRECLAIM(R3)
  2948. /*
  2949. * We were in fake suspend, so we are not going to save the
  2950. * register state as the guest checkpointed state (since
  2951. * we already have it), therefore we can now use any volatile GPR.
  2952. */
  2953. /* Reload PACA pointer, stack pointer and TOC. */
  2954. GET_PACA(r13)
  2955. ld r1, HSTATE_HOST_R1(r13)
  2956. ld r2, PACATOC(r13)
  2957. /* Set MSR RI now we have r1 and r13 back. */
  2958. li r5, MSR_RI
  2959. mtmsrd r5, 1
  2960. HMT_MEDIUM
  2961. ld r6, HSTATE_DSCR(r13)
  2962. mtspr SPRN_DSCR, r6
  2963. BEGIN_FTR_SECTION_NESTED(96)
  2964. bl pnv_power9_force_smt4_release
  2965. END_FTR_SECTION_NESTED(CPU_FTR_P9_TM_XER_SO_BUG, CPU_FTR_P9_TM_XER_SO_BUG, 96)
  2966. nop
  2967. 4:
  2968. mfspr r3, SPRN_PSSCR
  2969. /* PSSCR_FAKE_SUSPEND is a write-only bit, but clear it anyway */
  2970. li r0, PSSCR_FAKE_SUSPEND
  2971. andc r3, r3, r0
  2972. mtspr SPRN_PSSCR, r3
  2973. /* Don't save TEXASR, use value from last exit in real suspend state */
  2974. ld r9, HSTATE_KVM_VCPU(r13)
  2975. mfspr r5, SPRN_TFHAR
  2976. mfspr r6, SPRN_TFIAR
  2977. std r5, VCPU_TFHAR(r9)
  2978. std r6, VCPU_TFIAR(r9)
  2979. addi r1, r1, PPC_MIN_STKFRM
  2980. ld r0, PPC_LR_STKOFF(r1)
  2981. mtlr r0
  2982. blr
  2983. /*
  2984. * Restore transactional state and TM-related registers.
  2985. * Called with r3 pointing to the vcpu struct
  2986. * and r4 containing the guest MSR value.
  2987. * This potentially modifies all checkpointed registers.
  2988. * It restores r1 and r2 from the PACA.
  2989. */
  2990. kvmppc_restore_tm_hv:
  2991. /*
  2992. * If we are doing TM emulation for the guest on a POWER9 DD2,
  2993. * then we don't actually do a trechkpt -- we either set up
  2994. * fake-suspend mode, or emulate a TM rollback.
  2995. */
  2996. BEGIN_FTR_SECTION
  2997. b __kvmppc_restore_tm
  2998. END_FTR_SECTION_IFCLR(CPU_FTR_P9_TM_HV_ASSIST)
  2999. mflr r0
  3000. std r0, PPC_LR_STKOFF(r1)
  3001. li r0, 0
  3002. stb r0, HSTATE_FAKE_SUSPEND(r13)
  3003. /* Turn on TM so we can restore TM SPRs */
  3004. mfmsr r5
  3005. li r0, 1
  3006. rldimi r5, r0, MSR_TM_LG, 63-MSR_TM_LG
  3007. mtmsrd r5
  3008. /*
  3009. * The user may change these outside of a transaction, so they must
  3010. * always be context switched.
  3011. */
  3012. ld r5, VCPU_TFHAR(r3)
  3013. ld r6, VCPU_TFIAR(r3)
  3014. ld r7, VCPU_TEXASR(r3)
  3015. mtspr SPRN_TFHAR, r5
  3016. mtspr SPRN_TFIAR, r6
  3017. mtspr SPRN_TEXASR, r7
  3018. rldicl. r5, r4, 64 - MSR_TS_S_LG, 62
  3019. beqlr /* TM not active in guest */
  3020. /* Make sure the failure summary is set */
  3021. oris r7, r7, (TEXASR_FS)@h
  3022. mtspr SPRN_TEXASR, r7
  3023. cmpwi r5, 1 /* check for suspended state */
  3024. bgt 10f
  3025. stb r5, HSTATE_FAKE_SUSPEND(r13)
  3026. b 9f /* and return */
  3027. 10: stdu r1, -PPC_MIN_STKFRM(r1)
  3028. /* guest is in transactional state, so simulate rollback */
  3029. bl kvmhv_emulate_tm_rollback
  3030. nop
  3031. addi r1, r1, PPC_MIN_STKFRM
  3032. 9: ld r0, PPC_LR_STKOFF(r1)
  3033. mtlr r0
  3034. blr
  3035. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  3036. /*
  3037. * We come here if we get any exception or interrupt while we are
  3038. * executing host real mode code while in guest MMU context.
  3039. * r12 is (CR << 32) | vector
  3040. * r13 points to our PACA
  3041. * r12 is saved in HSTATE_SCRATCH0(r13)
  3042. * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
  3043. * r9 is saved in HSTATE_SCRATCH2(r13)
  3044. * r13 is saved in HSPRG1
  3045. * cfar is saved in HSTATE_CFAR(r13)
  3046. * ppr is saved in HSTATE_PPR(r13)
  3047. */
  3048. kvmppc_bad_host_intr:
  3049. /*
  3050. * Switch to the emergency stack, but start half-way down in
  3051. * case we were already on it.
  3052. */
  3053. mr r9, r1
  3054. std r1, PACAR1(r13)
  3055. ld r1, PACAEMERGSP(r13)
  3056. subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
  3057. std r9, 0(r1)
  3058. std r0, GPR0(r1)
  3059. std r9, GPR1(r1)
  3060. std r2, GPR2(r1)
  3061. SAVE_4GPRS(3, r1)
  3062. SAVE_2GPRS(7, r1)
  3063. srdi r0, r12, 32
  3064. clrldi r12, r12, 32
  3065. std r0, _CCR(r1)
  3066. std r12, _TRAP(r1)
  3067. andi. r0, r12, 2
  3068. beq 1f
  3069. mfspr r3, SPRN_HSRR0
  3070. mfspr r4, SPRN_HSRR1
  3071. mfspr r5, SPRN_HDAR
  3072. mfspr r6, SPRN_HDSISR
  3073. b 2f
  3074. 1: mfspr r3, SPRN_SRR0
  3075. mfspr r4, SPRN_SRR1
  3076. mfspr r5, SPRN_DAR
  3077. mfspr r6, SPRN_DSISR
  3078. 2: std r3, _NIP(r1)
  3079. std r4, _MSR(r1)
  3080. std r5, _DAR(r1)
  3081. std r6, _DSISR(r1)
  3082. ld r9, HSTATE_SCRATCH2(r13)
  3083. ld r12, HSTATE_SCRATCH0(r13)
  3084. GET_SCRATCH0(r0)
  3085. SAVE_4GPRS(9, r1)
  3086. std r0, GPR13(r1)
  3087. SAVE_NVGPRS(r1)
  3088. ld r5, HSTATE_CFAR(r13)
  3089. std r5, ORIG_GPR3(r1)
  3090. mflr r3
  3091. #ifdef CONFIG_RELOCATABLE
  3092. ld r4, HSTATE_SCRATCH1(r13)
  3093. #else
  3094. mfctr r4
  3095. #endif
  3096. mfxer r5
  3097. lbz r6, PACAIRQSOFTMASK(r13)
  3098. std r3, _LINK(r1)
  3099. std r4, _CTR(r1)
  3100. std r5, _XER(r1)
  3101. std r6, SOFTE(r1)
  3102. ld r2, PACATOC(r13)
  3103. LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
  3104. std r3, STACK_FRAME_OVERHEAD-16(r1)
  3105. /*
  3106. * On POWER9 do a minimal restore of the MMU and call C code,
  3107. * which will print a message and panic.
  3108. * XXX On POWER7 and POWER8, we just spin here since we don't
  3109. * know what the other threads are doing (and we don't want to
  3110. * coordinate with them) - but at least we now have register state
  3111. * in memory that we might be able to look at from another CPU.
  3112. */
  3113. BEGIN_FTR_SECTION
  3114. b .
  3115. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
  3116. ld r9, HSTATE_KVM_VCPU(r13)
  3117. ld r10, VCPU_KVM(r9)
  3118. li r0, 0
  3119. mtspr SPRN_AMR, r0
  3120. mtspr SPRN_IAMR, r0
  3121. mtspr SPRN_CIABR, r0
  3122. mtspr SPRN_DAWRX, r0
  3123. BEGIN_MMU_FTR_SECTION
  3124. b 4f
  3125. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
  3126. slbmte r0, r0
  3127. slbia
  3128. ptesync
  3129. ld r8, PACA_SLBSHADOWPTR(r13)
  3130. .rept SLB_NUM_BOLTED
  3131. li r3, SLBSHADOW_SAVEAREA
  3132. LDX_BE r5, r8, r3
  3133. addi r3, r3, 8
  3134. LDX_BE r6, r8, r3
  3135. andis. r7, r5, SLB_ESID_V@h
  3136. beq 3f
  3137. slbmte r6, r5
  3138. 3: addi r8, r8, 16
  3139. .endr
  3140. 4: lwz r7, KVM_HOST_LPID(r10)
  3141. mtspr SPRN_LPID, r7
  3142. mtspr SPRN_PID, r0
  3143. ld r8, KVM_HOST_LPCR(r10)
  3144. mtspr SPRN_LPCR, r8
  3145. isync
  3146. li r0, KVM_GUEST_MODE_NONE
  3147. stb r0, HSTATE_IN_GUEST(r13)
  3148. /*
  3149. * Turn on the MMU and jump to C code
  3150. */
  3151. bcl 20, 31, .+4
  3152. 5: mflr r3
  3153. addi r3, r3, 9f - 5b
  3154. li r4, -1
  3155. rldimi r3, r4, 62, 0 /* ensure 0xc000000000000000 bits are set */
  3156. ld r4, PACAKMSR(r13)
  3157. mtspr SPRN_SRR0, r3
  3158. mtspr SPRN_SRR1, r4
  3159. RFI_TO_KERNEL
  3160. 9: addi r3, r1, STACK_FRAME_OVERHEAD
  3161. bl kvmppc_bad_interrupt
  3162. b 9b
  3163. /*
  3164. * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
  3165. * from VCPU_INTR_MSR and is modified based on the required TM state changes.
  3166. * r11 has the guest MSR value (in/out)
  3167. * r9 has a vcpu pointer (in)
  3168. * r0 is used as a scratch register
  3169. */
  3170. kvmppc_msr_interrupt:
  3171. rldicl r0, r11, 64 - MSR_TS_S_LG, 62
  3172. cmpwi r0, 2 /* Check if we are in transactional state.. */
  3173. ld r11, VCPU_INTR_MSR(r9)
  3174. bne 1f
  3175. /* ... if transactional, change to suspended */
  3176. li r0, 1
  3177. 1: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
  3178. blr
  3179. /*
  3180. * This works around a hardware bug on POWER8E processors, where
  3181. * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
  3182. * performance monitor interrupt. Instead, when we need to have
  3183. * an interrupt pending, we have to arrange for a counter to overflow.
  3184. */
  3185. kvmppc_fix_pmao:
  3186. li r3, 0
  3187. mtspr SPRN_MMCR2, r3
  3188. lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
  3189. ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
  3190. mtspr SPRN_MMCR0, r3
  3191. lis r3, 0x7fff
  3192. ori r3, r3, 0xffff
  3193. mtspr SPRN_PMC6, r3
  3194. isync
  3195. blr
  3196. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  3197. /*
  3198. * Start timing an activity
  3199. * r3 = pointer to time accumulation struct, r4 = vcpu
  3200. */
  3201. kvmhv_start_timing:
  3202. ld r5, HSTATE_KVM_VCORE(r13)
  3203. ld r6, VCORE_TB_OFFSET_APPL(r5)
  3204. mftb r5
  3205. subf r5, r6, r5 /* subtract current timebase offset */
  3206. std r3, VCPU_CUR_ACTIVITY(r4)
  3207. std r5, VCPU_ACTIVITY_START(r4)
  3208. blr
  3209. /*
  3210. * Accumulate time to one activity and start another.
  3211. * r3 = pointer to new time accumulation struct, r4 = vcpu
  3212. */
  3213. kvmhv_accumulate_time:
  3214. ld r5, HSTATE_KVM_VCORE(r13)
  3215. ld r8, VCORE_TB_OFFSET_APPL(r5)
  3216. ld r5, VCPU_CUR_ACTIVITY(r4)
  3217. ld r6, VCPU_ACTIVITY_START(r4)
  3218. std r3, VCPU_CUR_ACTIVITY(r4)
  3219. mftb r7
  3220. subf r7, r8, r7 /* subtract current timebase offset */
  3221. std r7, VCPU_ACTIVITY_START(r4)
  3222. cmpdi r5, 0
  3223. beqlr
  3224. subf r3, r6, r7
  3225. ld r8, TAS_SEQCOUNT(r5)
  3226. cmpdi r8, 0
  3227. addi r8, r8, 1
  3228. std r8, TAS_SEQCOUNT(r5)
  3229. lwsync
  3230. ld r7, TAS_TOTAL(r5)
  3231. add r7, r7, r3
  3232. std r7, TAS_TOTAL(r5)
  3233. ld r6, TAS_MIN(r5)
  3234. ld r7, TAS_MAX(r5)
  3235. beq 3f
  3236. cmpd r3, r6
  3237. bge 1f
  3238. 3: std r3, TAS_MIN(r5)
  3239. 1: cmpd r3, r7
  3240. ble 2f
  3241. std r3, TAS_MAX(r5)
  3242. 2: lwsync
  3243. addi r8, r8, 1
  3244. std r8, TAS_SEQCOUNT(r5)
  3245. blr
  3246. #endif