book3s_hv_rmhandlers.S 59 KB

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