book3s_hv_rmhandlers.S 71 KB

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