book3s_hv_rmhandlers.S 59 KB

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