book3s_hv_rmhandlers.S 69 KB

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