book3s_hv_rmhandlers.S 70 KB

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