book3s_hv_rmhandlers.S 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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. #ifdef __LITTLE_ENDIAN__
  31. #error Need to fix lppaca and SLB shadow accesses in little endian mode
  32. #endif
  33. /*****************************************************************************
  34. * *
  35. * Real Mode handlers that need to be in the linear mapping *
  36. * *
  37. ****************************************************************************/
  38. .globl kvmppc_skip_interrupt
  39. kvmppc_skip_interrupt:
  40. mfspr r13,SPRN_SRR0
  41. addi r13,r13,4
  42. mtspr SPRN_SRR0,r13
  43. GET_SCRATCH0(r13)
  44. rfid
  45. b .
  46. .globl kvmppc_skip_Hinterrupt
  47. kvmppc_skip_Hinterrupt:
  48. mfspr r13,SPRN_HSRR0
  49. addi r13,r13,4
  50. mtspr SPRN_HSRR0,r13
  51. GET_SCRATCH0(r13)
  52. hrfid
  53. b .
  54. /*
  55. * Call kvmppc_hv_entry in real mode.
  56. * Must be called with interrupts hard-disabled.
  57. *
  58. * Input Registers:
  59. *
  60. * LR = return address to continue at after eventually re-enabling MMU
  61. */
  62. _GLOBAL(kvmppc_hv_entry_trampoline)
  63. mfmsr r10
  64. LOAD_REG_ADDR(r5, kvmppc_hv_entry)
  65. li r0,MSR_RI
  66. andc r0,r10,r0
  67. li r6,MSR_IR | MSR_DR
  68. andc r6,r10,r6
  69. mtmsrd r0,1 /* clear RI in MSR */
  70. mtsrr0 r5
  71. mtsrr1 r6
  72. RFI
  73. /******************************************************************************
  74. * *
  75. * Entry code *
  76. * *
  77. *****************************************************************************/
  78. /*
  79. * We come in here when wakened from nap mode on a secondary hw thread.
  80. * Relocation is off and most register values are lost.
  81. * r13 points to the PACA.
  82. */
  83. .globl kvm_start_guest
  84. kvm_start_guest:
  85. ld r1,PACAEMERGSP(r13)
  86. subi r1,r1,STACK_FRAME_OVERHEAD
  87. ld r2,PACATOC(r13)
  88. li r0,KVM_HWTHREAD_IN_KVM
  89. stb r0,HSTATE_HWTHREAD_STATE(r13)
  90. /* NV GPR values from power7_idle() will no longer be valid */
  91. li r0,1
  92. stb r0,PACA_NAPSTATELOST(r13)
  93. /* were we napping due to cede? */
  94. lbz r0,HSTATE_NAPPING(r13)
  95. cmpwi r0,0
  96. bne kvm_end_cede
  97. /*
  98. * We weren't napping due to cede, so this must be a secondary
  99. * thread being woken up to run a guest, or being woken up due
  100. * to a stray IPI. (Or due to some machine check or hypervisor
  101. * maintenance interrupt while the core is in KVM.)
  102. */
  103. /* Check the wake reason in SRR1 to see why we got here */
  104. mfspr r3,SPRN_SRR1
  105. rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
  106. cmpwi r3,4 /* was it an external interrupt? */
  107. bne 27f /* if not */
  108. ld r5,HSTATE_XICS_PHYS(r13)
  109. li r7,XICS_XIRR /* if it was an external interrupt, */
  110. lwzcix r8,r5,r7 /* get and ack the interrupt */
  111. sync
  112. clrldi. r9,r8,40 /* get interrupt source ID. */
  113. beq 28f /* none there? */
  114. cmpwi r9,XICS_IPI /* was it an IPI? */
  115. bne 29f
  116. li r0,0xff
  117. li r6,XICS_MFRR
  118. stbcix r0,r5,r6 /* clear IPI */
  119. stwcix r8,r5,r7 /* EOI the interrupt */
  120. sync /* order loading of vcpu after that */
  121. /* get vcpu pointer, NULL if we have no vcpu to run */
  122. ld r4,HSTATE_KVM_VCPU(r13)
  123. cmpdi r4,0
  124. /* if we have no vcpu to run, go back to sleep */
  125. beq kvm_no_guest
  126. b kvmppc_hv_entry
  127. 27: /* XXX should handle hypervisor maintenance interrupts etc. here */
  128. b kvm_no_guest
  129. 28: /* SRR1 said external but ICP said nope?? */
  130. b kvm_no_guest
  131. 29: /* External non-IPI interrupt to offline secondary thread? help?? */
  132. stw r8,HSTATE_SAVED_XIRR(r13)
  133. b kvm_no_guest
  134. .global kvmppc_hv_entry
  135. kvmppc_hv_entry:
  136. /* Required state:
  137. *
  138. * R4 = vcpu pointer
  139. * MSR = ~IR|DR
  140. * R13 = PACA
  141. * R1 = host R1
  142. * all other volatile GPRS = free
  143. */
  144. mflr r0
  145. std r0, HSTATE_VMHANDLER(r13)
  146. /* Set partition DABR */
  147. /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
  148. li r5,3
  149. ld r6,VCPU_DABR(r4)
  150. mtspr SPRN_DABRX,r5
  151. mtspr SPRN_DABR,r6
  152. BEGIN_FTR_SECTION
  153. isync
  154. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  155. /* Load guest PMU registers */
  156. /* R4 is live here (vcpu pointer) */
  157. li r3, 1
  158. sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
  159. mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
  160. isync
  161. lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
  162. lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
  163. lwz r6, VCPU_PMC + 8(r4)
  164. lwz r7, VCPU_PMC + 12(r4)
  165. lwz r8, VCPU_PMC + 16(r4)
  166. lwz r9, VCPU_PMC + 20(r4)
  167. BEGIN_FTR_SECTION
  168. lwz r10, VCPU_PMC + 24(r4)
  169. lwz r11, VCPU_PMC + 28(r4)
  170. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  171. mtspr SPRN_PMC1, r3
  172. mtspr SPRN_PMC2, r5
  173. mtspr SPRN_PMC3, r6
  174. mtspr SPRN_PMC4, r7
  175. mtspr SPRN_PMC5, r8
  176. mtspr SPRN_PMC6, r9
  177. BEGIN_FTR_SECTION
  178. mtspr SPRN_PMC7, r10
  179. mtspr SPRN_PMC8, r11
  180. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  181. ld r3, VCPU_MMCR(r4)
  182. ld r5, VCPU_MMCR + 8(r4)
  183. ld r6, VCPU_MMCR + 16(r4)
  184. mtspr SPRN_MMCR1, r5
  185. mtspr SPRN_MMCRA, r6
  186. mtspr SPRN_MMCR0, r3
  187. isync
  188. /* Load up FP, VMX and VSX registers */
  189. bl kvmppc_load_fp
  190. ld r14, VCPU_GPR(R14)(r4)
  191. ld r15, VCPU_GPR(R15)(r4)
  192. ld r16, VCPU_GPR(R16)(r4)
  193. ld r17, VCPU_GPR(R17)(r4)
  194. ld r18, VCPU_GPR(R18)(r4)
  195. ld r19, VCPU_GPR(R19)(r4)
  196. ld r20, VCPU_GPR(R20)(r4)
  197. ld r21, VCPU_GPR(R21)(r4)
  198. ld r22, VCPU_GPR(R22)(r4)
  199. ld r23, VCPU_GPR(R23)(r4)
  200. ld r24, VCPU_GPR(R24)(r4)
  201. ld r25, VCPU_GPR(R25)(r4)
  202. ld r26, VCPU_GPR(R26)(r4)
  203. ld r27, VCPU_GPR(R27)(r4)
  204. ld r28, VCPU_GPR(R28)(r4)
  205. ld r29, VCPU_GPR(R29)(r4)
  206. ld r30, VCPU_GPR(R30)(r4)
  207. ld r31, VCPU_GPR(R31)(r4)
  208. BEGIN_FTR_SECTION
  209. /* Switch DSCR to guest value */
  210. ld r5, VCPU_DSCR(r4)
  211. mtspr SPRN_DSCR, r5
  212. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  213. /*
  214. * Set the decrementer to the guest decrementer.
  215. */
  216. ld r8,VCPU_DEC_EXPIRES(r4)
  217. mftb r7
  218. subf r3,r7,r8
  219. mtspr SPRN_DEC,r3
  220. stw r3,VCPU_DEC(r4)
  221. ld r5, VCPU_SPRG0(r4)
  222. ld r6, VCPU_SPRG1(r4)
  223. ld r7, VCPU_SPRG2(r4)
  224. ld r8, VCPU_SPRG3(r4)
  225. mtspr SPRN_SPRG0, r5
  226. mtspr SPRN_SPRG1, r6
  227. mtspr SPRN_SPRG2, r7
  228. mtspr SPRN_SPRG3, r8
  229. /* Save R1 in the PACA */
  230. std r1, HSTATE_HOST_R1(r13)
  231. /* Increment yield count if they have a VPA */
  232. ld r3, VCPU_VPA(r4)
  233. cmpdi r3, 0
  234. beq 25f
  235. lwz r5, LPPACA_YIELDCOUNT(r3)
  236. addi r5, r5, 1
  237. stw r5, LPPACA_YIELDCOUNT(r3)
  238. li r6, 1
  239. stb r6, VCPU_VPA_DIRTY(r4)
  240. 25:
  241. /* Load up DAR and DSISR */
  242. ld r5, VCPU_DAR(r4)
  243. lwz r6, VCPU_DSISR(r4)
  244. mtspr SPRN_DAR, r5
  245. mtspr SPRN_DSISR, r6
  246. BEGIN_FTR_SECTION
  247. /* Restore AMR and UAMOR, set AMOR to all 1s */
  248. ld r5,VCPU_AMR(r4)
  249. ld r6,VCPU_UAMOR(r4)
  250. li r7,-1
  251. mtspr SPRN_AMR,r5
  252. mtspr SPRN_UAMOR,r6
  253. mtspr SPRN_AMOR,r7
  254. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  255. /* Clear out SLB */
  256. li r6,0
  257. slbmte r6,r6
  258. slbia
  259. ptesync
  260. BEGIN_FTR_SECTION
  261. b 30f
  262. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  263. /*
  264. * POWER7 host -> guest partition switch code.
  265. * We don't have to lock against concurrent tlbies,
  266. * but we do have to coordinate across hardware threads.
  267. */
  268. /* Increment entry count iff exit count is zero. */
  269. ld r5,HSTATE_KVM_VCORE(r13)
  270. addi r9,r5,VCORE_ENTRY_EXIT
  271. 21: lwarx r3,0,r9
  272. cmpwi r3,0x100 /* any threads starting to exit? */
  273. bge secondary_too_late /* if so we're too late to the party */
  274. addi r3,r3,1
  275. stwcx. r3,0,r9
  276. bne 21b
  277. /* Primary thread switches to guest partition. */
  278. ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
  279. lwz r6,VCPU_PTID(r4)
  280. cmpwi r6,0
  281. bne 20f
  282. ld r6,KVM_SDR1(r9)
  283. lwz r7,KVM_LPID(r9)
  284. li r0,LPID_RSVD /* switch to reserved LPID */
  285. mtspr SPRN_LPID,r0
  286. ptesync
  287. mtspr SPRN_SDR1,r6 /* switch to partition page table */
  288. mtspr SPRN_LPID,r7
  289. isync
  290. /* See if we need to flush the TLB */
  291. lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
  292. clrldi r7,r6,64-6 /* extract bit number (6 bits) */
  293. srdi r6,r6,6 /* doubleword number */
  294. sldi r6,r6,3 /* address offset */
  295. add r6,r6,r9
  296. addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
  297. li r0,1
  298. sld r0,r0,r7
  299. ld r7,0(r6)
  300. and. r7,r7,r0
  301. beq 22f
  302. 23: ldarx r7,0,r6 /* if set, clear the bit */
  303. andc r7,r7,r0
  304. stdcx. r7,0,r6
  305. bne 23b
  306. li r6,128 /* and flush the TLB */
  307. mtctr r6
  308. li r7,0x800 /* IS field = 0b10 */
  309. ptesync
  310. 28: tlbiel r7
  311. addi r7,r7,0x1000
  312. bdnz 28b
  313. ptesync
  314. 22: li r0,1
  315. stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
  316. b 10f
  317. /* Secondary threads wait for primary to have done partition switch */
  318. 20: lbz r0,VCORE_IN_GUEST(r5)
  319. cmpwi r0,0
  320. beq 20b
  321. /* Set LPCR and RMOR. */
  322. 10: ld r8,KVM_LPCR(r9)
  323. mtspr SPRN_LPCR,r8
  324. ld r8,KVM_RMOR(r9)
  325. mtspr SPRN_RMOR,r8
  326. isync
  327. /* Check if HDEC expires soon */
  328. mfspr r3,SPRN_HDEC
  329. cmpwi r3,10
  330. li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
  331. mr r9,r4
  332. blt hdec_soon
  333. /* Save purr/spurr */
  334. mfspr r5,SPRN_PURR
  335. mfspr r6,SPRN_SPURR
  336. std r5,HSTATE_PURR(r13)
  337. std r6,HSTATE_SPURR(r13)
  338. ld r7,VCPU_PURR(r4)
  339. ld r8,VCPU_SPURR(r4)
  340. mtspr SPRN_PURR,r7
  341. mtspr SPRN_SPURR,r8
  342. b 31f
  343. /*
  344. * PPC970 host -> guest partition switch code.
  345. * We have to lock against concurrent tlbies,
  346. * using native_tlbie_lock to lock against host tlbies
  347. * and kvm->arch.tlbie_lock to lock against guest tlbies.
  348. * We also have to invalidate the TLB since its
  349. * entries aren't tagged with the LPID.
  350. */
  351. 30: ld r9,VCPU_KVM(r4) /* pointer to struct kvm */
  352. /* first take native_tlbie_lock */
  353. .section ".toc","aw"
  354. toc_tlbie_lock:
  355. .tc native_tlbie_lock[TC],native_tlbie_lock
  356. .previous
  357. ld r3,toc_tlbie_lock@toc(2)
  358. lwz r8,PACA_LOCK_TOKEN(r13)
  359. 24: lwarx r0,0,r3
  360. cmpwi r0,0
  361. bne 24b
  362. stwcx. r8,0,r3
  363. bne 24b
  364. isync
  365. ld r7,KVM_LPCR(r9) /* use kvm->arch.lpcr to store HID4 */
  366. li r0,0x18f
  367. rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
  368. or r0,r7,r0
  369. ptesync
  370. sync
  371. mtspr SPRN_HID4,r0 /* switch to reserved LPID */
  372. isync
  373. li r0,0
  374. stw r0,0(r3) /* drop native_tlbie_lock */
  375. /* invalidate the whole TLB */
  376. li r0,256
  377. mtctr r0
  378. li r6,0
  379. 25: tlbiel r6
  380. addi r6,r6,0x1000
  381. bdnz 25b
  382. ptesync
  383. /* Take the guest's tlbie_lock */
  384. addi r3,r9,KVM_TLBIE_LOCK
  385. 24: lwarx r0,0,r3
  386. cmpwi r0,0
  387. bne 24b
  388. stwcx. r8,0,r3
  389. bne 24b
  390. isync
  391. ld r6,KVM_SDR1(r9)
  392. mtspr SPRN_SDR1,r6 /* switch to partition page table */
  393. /* Set up HID4 with the guest's LPID etc. */
  394. sync
  395. mtspr SPRN_HID4,r7
  396. isync
  397. /* drop the guest's tlbie_lock */
  398. li r0,0
  399. stw r0,0(r3)
  400. /* Check if HDEC expires soon */
  401. mfspr r3,SPRN_HDEC
  402. cmpwi r3,10
  403. li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
  404. mr r9,r4
  405. blt hdec_soon
  406. /* Enable HDEC interrupts */
  407. mfspr r0,SPRN_HID0
  408. li r3,1
  409. rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
  410. sync
  411. mtspr SPRN_HID0,r0
  412. mfspr r0,SPRN_HID0
  413. mfspr r0,SPRN_HID0
  414. mfspr r0,SPRN_HID0
  415. mfspr r0,SPRN_HID0
  416. mfspr r0,SPRN_HID0
  417. mfspr r0,SPRN_HID0
  418. /* Load up guest SLB entries */
  419. 31: lwz r5,VCPU_SLB_MAX(r4)
  420. cmpwi r5,0
  421. beq 9f
  422. mtctr r5
  423. addi r6,r4,VCPU_SLB
  424. 1: ld r8,VCPU_SLB_E(r6)
  425. ld r9,VCPU_SLB_V(r6)
  426. slbmte r9,r8
  427. addi r6,r6,VCPU_SLB_SIZE
  428. bdnz 1b
  429. 9:
  430. /* Restore state of CTRL run bit; assume 1 on entry */
  431. lwz r5,VCPU_CTRL(r4)
  432. andi. r5,r5,1
  433. bne 4f
  434. mfspr r6,SPRN_CTRLF
  435. clrrdi r6,r6,1
  436. mtspr SPRN_CTRLT,r6
  437. 4:
  438. ld r6, VCPU_CTR(r4)
  439. lwz r7, VCPU_XER(r4)
  440. mtctr r6
  441. mtxer r7
  442. ld r10, VCPU_PC(r4)
  443. ld r11, VCPU_MSR(r4)
  444. kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
  445. ld r6, VCPU_SRR0(r4)
  446. ld r7, VCPU_SRR1(r4)
  447. /* r11 = vcpu->arch.msr & ~MSR_HV */
  448. rldicl r11, r11, 63 - MSR_HV_LG, 1
  449. rotldi r11, r11, 1 + MSR_HV_LG
  450. ori r11, r11, MSR_ME
  451. /* Check if we can deliver an external or decrementer interrupt now */
  452. ld r0,VCPU_PENDING_EXC(r4)
  453. lis r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
  454. and r0,r0,r8
  455. cmpdi cr1,r0,0
  456. andi. r0,r11,MSR_EE
  457. beq cr1,11f
  458. BEGIN_FTR_SECTION
  459. mfspr r8,SPRN_LPCR
  460. ori r8,r8,LPCR_MER
  461. mtspr SPRN_LPCR,r8
  462. isync
  463. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  464. beq 5f
  465. li r0,BOOK3S_INTERRUPT_EXTERNAL
  466. 12: mr r6,r10
  467. mr r10,r0
  468. mr r7,r11
  469. li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
  470. rotldi r11,r11,63
  471. b 5f
  472. 11: beq 5f
  473. mfspr r0,SPRN_DEC
  474. cmpwi r0,0
  475. li r0,BOOK3S_INTERRUPT_DECREMENTER
  476. blt 12b
  477. /* Move SRR0 and SRR1 into the respective regs */
  478. 5: mtspr SPRN_SRR0, r6
  479. mtspr SPRN_SRR1, r7
  480. fast_guest_return:
  481. li r0,0
  482. stb r0,VCPU_CEDED(r4) /* cancel cede */
  483. mtspr SPRN_HSRR0,r10
  484. mtspr SPRN_HSRR1,r11
  485. /* Activate guest mode, so faults get handled by KVM */
  486. li r9, KVM_GUEST_MODE_GUEST
  487. stb r9, HSTATE_IN_GUEST(r13)
  488. /* Enter guest */
  489. BEGIN_FTR_SECTION
  490. ld r5, VCPU_CFAR(r4)
  491. mtspr SPRN_CFAR, r5
  492. END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
  493. ld r5, VCPU_LR(r4)
  494. lwz r6, VCPU_CR(r4)
  495. mtlr r5
  496. mtcr r6
  497. ld r0, VCPU_GPR(R0)(r4)
  498. ld r1, VCPU_GPR(R1)(r4)
  499. ld r2, VCPU_GPR(R2)(r4)
  500. ld r3, VCPU_GPR(R3)(r4)
  501. ld r5, VCPU_GPR(R5)(r4)
  502. ld r6, VCPU_GPR(R6)(r4)
  503. ld r7, VCPU_GPR(R7)(r4)
  504. ld r8, VCPU_GPR(R8)(r4)
  505. ld r9, VCPU_GPR(R9)(r4)
  506. ld r10, VCPU_GPR(R10)(r4)
  507. ld r11, VCPU_GPR(R11)(r4)
  508. ld r12, VCPU_GPR(R12)(r4)
  509. ld r13, VCPU_GPR(R13)(r4)
  510. ld r4, VCPU_GPR(R4)(r4)
  511. hrfid
  512. b .
  513. /******************************************************************************
  514. * *
  515. * Exit code *
  516. * *
  517. *****************************************************************************/
  518. /*
  519. * We come here from the first-level interrupt handlers.
  520. */
  521. .globl kvmppc_interrupt
  522. kvmppc_interrupt:
  523. /*
  524. * Register contents:
  525. * R12 = interrupt vector
  526. * R13 = PACA
  527. * guest CR, R12 saved in shadow VCPU SCRATCH1/0
  528. * guest R13 saved in SPRN_SCRATCH0
  529. */
  530. /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
  531. std r9, HSTATE_HOST_R2(r13)
  532. ld r9, HSTATE_KVM_VCPU(r13)
  533. /* Save registers */
  534. std r0, VCPU_GPR(R0)(r9)
  535. std r1, VCPU_GPR(R1)(r9)
  536. std r2, VCPU_GPR(R2)(r9)
  537. std r3, VCPU_GPR(R3)(r9)
  538. std r4, VCPU_GPR(R4)(r9)
  539. std r5, VCPU_GPR(R5)(r9)
  540. std r6, VCPU_GPR(R6)(r9)
  541. std r7, VCPU_GPR(R7)(r9)
  542. std r8, VCPU_GPR(R8)(r9)
  543. ld r0, HSTATE_HOST_R2(r13)
  544. std r0, VCPU_GPR(R9)(r9)
  545. std r10, VCPU_GPR(R10)(r9)
  546. std r11, VCPU_GPR(R11)(r9)
  547. ld r3, HSTATE_SCRATCH0(r13)
  548. lwz r4, HSTATE_SCRATCH1(r13)
  549. std r3, VCPU_GPR(R12)(r9)
  550. stw r4, VCPU_CR(r9)
  551. BEGIN_FTR_SECTION
  552. ld r3, HSTATE_CFAR(r13)
  553. std r3, VCPU_CFAR(r9)
  554. END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
  555. /* Restore R1/R2 so we can handle faults */
  556. ld r1, HSTATE_HOST_R1(r13)
  557. ld r2, PACATOC(r13)
  558. mfspr r10, SPRN_SRR0
  559. mfspr r11, SPRN_SRR1
  560. std r10, VCPU_SRR0(r9)
  561. std r11, VCPU_SRR1(r9)
  562. andi. r0, r12, 2 /* need to read HSRR0/1? */
  563. beq 1f
  564. mfspr r10, SPRN_HSRR0
  565. mfspr r11, SPRN_HSRR1
  566. clrrdi r12, r12, 2
  567. 1: std r10, VCPU_PC(r9)
  568. std r11, VCPU_MSR(r9)
  569. GET_SCRATCH0(r3)
  570. mflr r4
  571. std r3, VCPU_GPR(R13)(r9)
  572. std r4, VCPU_LR(r9)
  573. /* Unset guest mode */
  574. li r0, KVM_GUEST_MODE_NONE
  575. stb r0, HSTATE_IN_GUEST(r13)
  576. stw r12,VCPU_TRAP(r9)
  577. /* Save HEIR (HV emulation assist reg) in last_inst
  578. if this is an HEI (HV emulation interrupt, e40) */
  579. li r3,KVM_INST_FETCH_FAILED
  580. BEGIN_FTR_SECTION
  581. cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
  582. bne 11f
  583. mfspr r3,SPRN_HEIR
  584. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  585. 11: stw r3,VCPU_LAST_INST(r9)
  586. /* these are volatile across C function calls */
  587. mfctr r3
  588. mfxer r4
  589. std r3, VCPU_CTR(r9)
  590. stw r4, VCPU_XER(r9)
  591. BEGIN_FTR_SECTION
  592. /* If this is a page table miss then see if it's theirs or ours */
  593. cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
  594. beq kvmppc_hdsi
  595. cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
  596. beq kvmppc_hisi
  597. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  598. /* See if this is a leftover HDEC interrupt */
  599. cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
  600. bne 2f
  601. mfspr r3,SPRN_HDEC
  602. cmpwi r3,0
  603. bge ignore_hdec
  604. 2:
  605. /* See if this is an hcall we can handle in real mode */
  606. cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
  607. beq hcall_try_real_mode
  608. /* Only handle external interrupts here on arch 206 and later */
  609. BEGIN_FTR_SECTION
  610. b ext_interrupt_to_host
  611. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
  612. /* External interrupt ? */
  613. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  614. bne+ ext_interrupt_to_host
  615. /* External interrupt, first check for host_ipi. If this is
  616. * set, we know the host wants us out so let's do it now
  617. */
  618. do_ext_interrupt:
  619. lbz r0, HSTATE_HOST_IPI(r13)
  620. cmpwi r0, 0
  621. bne ext_interrupt_to_host
  622. /* Now read the interrupt from the ICP */
  623. ld r5, HSTATE_XICS_PHYS(r13)
  624. li r7, XICS_XIRR
  625. cmpdi r5, 0
  626. beq- ext_interrupt_to_host
  627. lwzcix r3, r5, r7
  628. rlwinm. r0, r3, 0, 0xffffff
  629. sync
  630. beq 3f /* if nothing pending in the ICP */
  631. /* We found something in the ICP...
  632. *
  633. * If it's not an IPI, stash it in the PACA and return to
  634. * the host, we don't (yet) handle directing real external
  635. * interrupts directly to the guest
  636. */
  637. cmpwi r0, XICS_IPI
  638. bne ext_stash_for_host
  639. /* It's an IPI, clear the MFRR and EOI it */
  640. li r0, 0xff
  641. li r6, XICS_MFRR
  642. stbcix r0, r5, r6 /* clear the IPI */
  643. stwcix r3, r5, r7 /* EOI it */
  644. sync
  645. /* We need to re-check host IPI now in case it got set in the
  646. * meantime. If it's clear, we bounce the interrupt to the
  647. * guest
  648. */
  649. lbz r0, HSTATE_HOST_IPI(r13)
  650. cmpwi r0, 0
  651. bne- 1f
  652. /* Allright, looks like an IPI for the guest, we need to set MER */
  653. 3:
  654. /* Check if any CPU is heading out to the host, if so head out too */
  655. ld r5, HSTATE_KVM_VCORE(r13)
  656. lwz r0, VCORE_ENTRY_EXIT(r5)
  657. cmpwi r0, 0x100
  658. bge ext_interrupt_to_host
  659. /* See if there is a pending interrupt for the guest */
  660. mfspr r8, SPRN_LPCR
  661. ld r0, VCPU_PENDING_EXC(r9)
  662. /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
  663. rldicl. r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
  664. rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
  665. beq 2f
  666. /* And if the guest EE is set, we can deliver immediately, else
  667. * we return to the guest with MER set
  668. */
  669. andi. r0, r11, MSR_EE
  670. beq 2f
  671. mtspr SPRN_SRR0, r10
  672. mtspr SPRN_SRR1, r11
  673. li r10, BOOK3S_INTERRUPT_EXTERNAL
  674. li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
  675. rotldi r11, r11, 63
  676. 2: mr r4, r9
  677. mtspr SPRN_LPCR, r8
  678. b fast_guest_return
  679. /* We raced with the host, we need to resend that IPI, bummer */
  680. 1: li r0, IPI_PRIORITY
  681. stbcix r0, r5, r6 /* set the IPI */
  682. sync
  683. b ext_interrupt_to_host
  684. ext_stash_for_host:
  685. /* It's not an IPI and it's for the host, stash it in the PACA
  686. * before exit, it will be picked up by the host ICP driver
  687. */
  688. stw r3, HSTATE_SAVED_XIRR(r13)
  689. ext_interrupt_to_host:
  690. guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
  691. /* Save DEC */
  692. mfspr r5,SPRN_DEC
  693. mftb r6
  694. extsw r5,r5
  695. add r5,r5,r6
  696. std r5,VCPU_DEC_EXPIRES(r9)
  697. /* Save more register state */
  698. mfdar r6
  699. mfdsisr r7
  700. std r6, VCPU_DAR(r9)
  701. stw r7, VCPU_DSISR(r9)
  702. BEGIN_FTR_SECTION
  703. /* don't overwrite fault_dar/fault_dsisr if HDSI */
  704. cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
  705. beq 6f
  706. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  707. std r6, VCPU_FAULT_DAR(r9)
  708. stw r7, VCPU_FAULT_DSISR(r9)
  709. /* See if it is a machine check */
  710. cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  711. beq machine_check_realmode
  712. mc_cont:
  713. /* Save guest CTRL register, set runlatch to 1 */
  714. 6: mfspr r6,SPRN_CTRLF
  715. stw r6,VCPU_CTRL(r9)
  716. andi. r0,r6,1
  717. bne 4f
  718. ori r6,r6,1
  719. mtspr SPRN_CTRLT,r6
  720. 4:
  721. /* Read the guest SLB and save it away */
  722. lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
  723. mtctr r0
  724. li r6,0
  725. addi r7,r9,VCPU_SLB
  726. li r5,0
  727. 1: slbmfee r8,r6
  728. andis. r0,r8,SLB_ESID_V@h
  729. beq 2f
  730. add r8,r8,r6 /* put index in */
  731. slbmfev r3,r6
  732. std r8,VCPU_SLB_E(r7)
  733. std r3,VCPU_SLB_V(r7)
  734. addi r7,r7,VCPU_SLB_SIZE
  735. addi r5,r5,1
  736. 2: addi r6,r6,1
  737. bdnz 1b
  738. stw r5,VCPU_SLB_MAX(r9)
  739. /*
  740. * Save the guest PURR/SPURR
  741. */
  742. BEGIN_FTR_SECTION
  743. mfspr r5,SPRN_PURR
  744. mfspr r6,SPRN_SPURR
  745. ld r7,VCPU_PURR(r9)
  746. ld r8,VCPU_SPURR(r9)
  747. std r5,VCPU_PURR(r9)
  748. std r6,VCPU_SPURR(r9)
  749. subf r5,r7,r5
  750. subf r6,r8,r6
  751. /*
  752. * Restore host PURR/SPURR and add guest times
  753. * so that the time in the guest gets accounted.
  754. */
  755. ld r3,HSTATE_PURR(r13)
  756. ld r4,HSTATE_SPURR(r13)
  757. add r3,r3,r5
  758. add r4,r4,r6
  759. mtspr SPRN_PURR,r3
  760. mtspr SPRN_SPURR,r4
  761. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
  762. /* Clear out SLB */
  763. li r5,0
  764. slbmte r5,r5
  765. slbia
  766. ptesync
  767. hdec_soon: /* r9 = vcpu, r12 = trap, r13 = paca */
  768. BEGIN_FTR_SECTION
  769. b 32f
  770. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  771. /*
  772. * POWER7 guest -> host partition switch code.
  773. * We don't have to lock against tlbies but we do
  774. * have to coordinate the hardware threads.
  775. */
  776. /* Increment the threads-exiting-guest count in the 0xff00
  777. bits of vcore->entry_exit_count */
  778. lwsync
  779. ld r5,HSTATE_KVM_VCORE(r13)
  780. addi r6,r5,VCORE_ENTRY_EXIT
  781. 41: lwarx r3,0,r6
  782. addi r0,r3,0x100
  783. stwcx. r0,0,r6
  784. bne 41b
  785. lwsync
  786. /*
  787. * At this point we have an interrupt that we have to pass
  788. * up to the kernel or qemu; we can't handle it in real mode.
  789. * Thus we have to do a partition switch, so we have to
  790. * collect the other threads, if we are the first thread
  791. * to take an interrupt. To do this, we set the HDEC to 0,
  792. * which causes an HDEC interrupt in all threads within 2ns
  793. * because the HDEC register is shared between all 4 threads.
  794. * However, we don't need to bother if this is an HDEC
  795. * interrupt, since the other threads will already be on their
  796. * way here in that case.
  797. */
  798. cmpwi r3,0x100 /* Are we the first here? */
  799. bge 43f
  800. cmpwi r3,1 /* Are any other threads in the guest? */
  801. ble 43f
  802. cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
  803. beq 40f
  804. li r0,0
  805. mtspr SPRN_HDEC,r0
  806. 40:
  807. /*
  808. * Send an IPI to any napping threads, since an HDEC interrupt
  809. * doesn't wake CPUs up from nap.
  810. */
  811. lwz r3,VCORE_NAPPING_THREADS(r5)
  812. lwz r4,VCPU_PTID(r9)
  813. li r0,1
  814. sld r0,r0,r4
  815. andc. r3,r3,r0 /* no sense IPI'ing ourselves */
  816. beq 43f
  817. mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
  818. subf r6,r4,r13
  819. 42: andi. r0,r3,1
  820. beq 44f
  821. ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
  822. li r0,IPI_PRIORITY
  823. li r7,XICS_MFRR
  824. stbcix r0,r7,r8 /* trigger the IPI */
  825. 44: srdi. r3,r3,1
  826. addi r6,r6,PACA_SIZE
  827. bne 42b
  828. /* Secondary threads wait for primary to do partition switch */
  829. 43: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
  830. ld r5,HSTATE_KVM_VCORE(r13)
  831. lwz r3,VCPU_PTID(r9)
  832. cmpwi r3,0
  833. beq 15f
  834. HMT_LOW
  835. 13: lbz r3,VCORE_IN_GUEST(r5)
  836. cmpwi r3,0
  837. bne 13b
  838. HMT_MEDIUM
  839. b 16f
  840. /* Primary thread waits for all the secondaries to exit guest */
  841. 15: lwz r3,VCORE_ENTRY_EXIT(r5)
  842. srwi r0,r3,8
  843. clrldi r3,r3,56
  844. cmpw r3,r0
  845. bne 15b
  846. isync
  847. /* Primary thread switches back to host partition */
  848. ld r6,KVM_HOST_SDR1(r4)
  849. lwz r7,KVM_HOST_LPID(r4)
  850. li r8,LPID_RSVD /* switch to reserved LPID */
  851. mtspr SPRN_LPID,r8
  852. ptesync
  853. mtspr SPRN_SDR1,r6 /* switch to partition page table */
  854. mtspr SPRN_LPID,r7
  855. isync
  856. li r0,0
  857. stb r0,VCORE_IN_GUEST(r5)
  858. lis r8,0x7fff /* MAX_INT@h */
  859. mtspr SPRN_HDEC,r8
  860. 16: ld r8,KVM_HOST_LPCR(r4)
  861. mtspr SPRN_LPCR,r8
  862. isync
  863. b 33f
  864. /*
  865. * PPC970 guest -> host partition switch code.
  866. * We have to lock against concurrent tlbies, and
  867. * we have to flush the whole TLB.
  868. */
  869. 32: ld r4,VCPU_KVM(r9) /* pointer to struct kvm */
  870. /* Take the guest's tlbie_lock */
  871. lwz r8,PACA_LOCK_TOKEN(r13)
  872. addi r3,r4,KVM_TLBIE_LOCK
  873. 24: lwarx r0,0,r3
  874. cmpwi r0,0
  875. bne 24b
  876. stwcx. r8,0,r3
  877. bne 24b
  878. isync
  879. ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
  880. li r0,0x18f
  881. rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
  882. or r0,r7,r0
  883. ptesync
  884. sync
  885. mtspr SPRN_HID4,r0 /* switch to reserved LPID */
  886. isync
  887. li r0,0
  888. stw r0,0(r3) /* drop guest tlbie_lock */
  889. /* invalidate the whole TLB */
  890. li r0,256
  891. mtctr r0
  892. li r6,0
  893. 25: tlbiel r6
  894. addi r6,r6,0x1000
  895. bdnz 25b
  896. ptesync
  897. /* take native_tlbie_lock */
  898. ld r3,toc_tlbie_lock@toc(2)
  899. 24: lwarx r0,0,r3
  900. cmpwi r0,0
  901. bne 24b
  902. stwcx. r8,0,r3
  903. bne 24b
  904. isync
  905. ld r6,KVM_HOST_SDR1(r4)
  906. mtspr SPRN_SDR1,r6 /* switch to host page table */
  907. /* Set up host HID4 value */
  908. sync
  909. mtspr SPRN_HID4,r7
  910. isync
  911. li r0,0
  912. stw r0,0(r3) /* drop native_tlbie_lock */
  913. lis r8,0x7fff /* MAX_INT@h */
  914. mtspr SPRN_HDEC,r8
  915. /* Disable HDEC interrupts */
  916. mfspr r0,SPRN_HID0
  917. li r3,0
  918. rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
  919. sync
  920. mtspr SPRN_HID0,r0
  921. mfspr r0,SPRN_HID0
  922. mfspr r0,SPRN_HID0
  923. mfspr r0,SPRN_HID0
  924. mfspr r0,SPRN_HID0
  925. mfspr r0,SPRN_HID0
  926. mfspr r0,SPRN_HID0
  927. /* load host SLB entries */
  928. 33: ld r8,PACA_SLBSHADOWPTR(r13)
  929. .rept SLB_NUM_BOLTED
  930. ld r5,SLBSHADOW_SAVEAREA(r8)
  931. ld r6,SLBSHADOW_SAVEAREA+8(r8)
  932. andis. r7,r5,SLB_ESID_V@h
  933. beq 1f
  934. slbmte r6,r5
  935. 1: addi r8,r8,16
  936. .endr
  937. /* Save and reset AMR and UAMOR before turning on the MMU */
  938. BEGIN_FTR_SECTION
  939. mfspr r5,SPRN_AMR
  940. mfspr r6,SPRN_UAMOR
  941. std r5,VCPU_AMR(r9)
  942. std r6,VCPU_UAMOR(r9)
  943. li r6,0
  944. mtspr SPRN_AMR,r6
  945. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  946. /* Switch DSCR back to host value */
  947. BEGIN_FTR_SECTION
  948. mfspr r8, SPRN_DSCR
  949. ld r7, HSTATE_DSCR(r13)
  950. std r8, VCPU_DSCR(r7)
  951. mtspr SPRN_DSCR, r7
  952. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  953. /* Save non-volatile GPRs */
  954. std r14, VCPU_GPR(R14)(r9)
  955. std r15, VCPU_GPR(R15)(r9)
  956. std r16, VCPU_GPR(R16)(r9)
  957. std r17, VCPU_GPR(R17)(r9)
  958. std r18, VCPU_GPR(R18)(r9)
  959. std r19, VCPU_GPR(R19)(r9)
  960. std r20, VCPU_GPR(R20)(r9)
  961. std r21, VCPU_GPR(R21)(r9)
  962. std r22, VCPU_GPR(R22)(r9)
  963. std r23, VCPU_GPR(R23)(r9)
  964. std r24, VCPU_GPR(R24)(r9)
  965. std r25, VCPU_GPR(R25)(r9)
  966. std r26, VCPU_GPR(R26)(r9)
  967. std r27, VCPU_GPR(R27)(r9)
  968. std r28, VCPU_GPR(R28)(r9)
  969. std r29, VCPU_GPR(R29)(r9)
  970. std r30, VCPU_GPR(R30)(r9)
  971. std r31, VCPU_GPR(R31)(r9)
  972. /* Save SPRGs */
  973. mfspr r3, SPRN_SPRG0
  974. mfspr r4, SPRN_SPRG1
  975. mfspr r5, SPRN_SPRG2
  976. mfspr r6, SPRN_SPRG3
  977. std r3, VCPU_SPRG0(r9)
  978. std r4, VCPU_SPRG1(r9)
  979. std r5, VCPU_SPRG2(r9)
  980. std r6, VCPU_SPRG3(r9)
  981. /* save FP state */
  982. mr r3, r9
  983. bl .kvmppc_save_fp
  984. /* Increment yield count if they have a VPA */
  985. ld r8, VCPU_VPA(r9) /* do they have a VPA? */
  986. cmpdi r8, 0
  987. beq 25f
  988. lwz r3, LPPACA_YIELDCOUNT(r8)
  989. addi r3, r3, 1
  990. stw r3, LPPACA_YIELDCOUNT(r8)
  991. li r3, 1
  992. stb r3, VCPU_VPA_DIRTY(r9)
  993. 25:
  994. /* Save PMU registers if requested */
  995. /* r8 and cr0.eq are live here */
  996. li r3, 1
  997. sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
  998. mfspr r4, SPRN_MMCR0 /* save MMCR0 */
  999. mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
  1000. mfspr r6, SPRN_MMCRA
  1001. BEGIN_FTR_SECTION
  1002. /* On P7, clear MMCRA in order to disable SDAR updates */
  1003. li r7, 0
  1004. mtspr SPRN_MMCRA, r7
  1005. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  1006. isync
  1007. beq 21f /* if no VPA, save PMU stuff anyway */
  1008. lbz r7, LPPACA_PMCINUSE(r8)
  1009. cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
  1010. bne 21f
  1011. std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
  1012. b 22f
  1013. 21: mfspr r5, SPRN_MMCR1
  1014. std r4, VCPU_MMCR(r9)
  1015. std r5, VCPU_MMCR + 8(r9)
  1016. std r6, VCPU_MMCR + 16(r9)
  1017. mfspr r3, SPRN_PMC1
  1018. mfspr r4, SPRN_PMC2
  1019. mfspr r5, SPRN_PMC3
  1020. mfspr r6, SPRN_PMC4
  1021. mfspr r7, SPRN_PMC5
  1022. mfspr r8, SPRN_PMC6
  1023. BEGIN_FTR_SECTION
  1024. mfspr r10, SPRN_PMC7
  1025. mfspr r11, SPRN_PMC8
  1026. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  1027. stw r3, VCPU_PMC(r9)
  1028. stw r4, VCPU_PMC + 4(r9)
  1029. stw r5, VCPU_PMC + 8(r9)
  1030. stw r6, VCPU_PMC + 12(r9)
  1031. stw r7, VCPU_PMC + 16(r9)
  1032. stw r8, VCPU_PMC + 20(r9)
  1033. BEGIN_FTR_SECTION
  1034. stw r10, VCPU_PMC + 24(r9)
  1035. stw r11, VCPU_PMC + 28(r9)
  1036. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  1037. 22:
  1038. /* Secondary threads go off to take a nap on POWER7 */
  1039. BEGIN_FTR_SECTION
  1040. lwz r0,VCPU_PTID(r9)
  1041. cmpwi r0,0
  1042. bne secondary_nap
  1043. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  1044. /* Restore host DABR and DABRX */
  1045. ld r5,HSTATE_DABR(r13)
  1046. li r6,7
  1047. mtspr SPRN_DABR,r5
  1048. mtspr SPRN_DABRX,r6
  1049. /* Restore SPRG3 */
  1050. ld r3,PACA_SPRG3(r13)
  1051. mtspr SPRN_SPRG3,r3
  1052. /*
  1053. * Reload DEC. HDEC interrupts were disabled when
  1054. * we reloaded the host's LPCR value.
  1055. */
  1056. ld r3, HSTATE_DECEXP(r13)
  1057. mftb r4
  1058. subf r4, r4, r3
  1059. mtspr SPRN_DEC, r4
  1060. /* Reload the host's PMU registers */
  1061. ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
  1062. lbz r4, LPPACA_PMCINUSE(r3)
  1063. cmpwi r4, 0
  1064. beq 23f /* skip if not */
  1065. lwz r3, HSTATE_PMC(r13)
  1066. lwz r4, HSTATE_PMC + 4(r13)
  1067. lwz r5, HSTATE_PMC + 8(r13)
  1068. lwz r6, HSTATE_PMC + 12(r13)
  1069. lwz r8, HSTATE_PMC + 16(r13)
  1070. lwz r9, HSTATE_PMC + 20(r13)
  1071. BEGIN_FTR_SECTION
  1072. lwz r10, HSTATE_PMC + 24(r13)
  1073. lwz r11, HSTATE_PMC + 28(r13)
  1074. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  1075. mtspr SPRN_PMC1, r3
  1076. mtspr SPRN_PMC2, r4
  1077. mtspr SPRN_PMC3, r5
  1078. mtspr SPRN_PMC4, r6
  1079. mtspr SPRN_PMC5, r8
  1080. mtspr SPRN_PMC6, r9
  1081. BEGIN_FTR_SECTION
  1082. mtspr SPRN_PMC7, r10
  1083. mtspr SPRN_PMC8, r11
  1084. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
  1085. ld r3, HSTATE_MMCR(r13)
  1086. ld r4, HSTATE_MMCR + 8(r13)
  1087. ld r5, HSTATE_MMCR + 16(r13)
  1088. mtspr SPRN_MMCR1, r4
  1089. mtspr SPRN_MMCRA, r5
  1090. mtspr SPRN_MMCR0, r3
  1091. isync
  1092. 23:
  1093. /*
  1094. * For external and machine check interrupts, we need
  1095. * to call the Linux handler to process the interrupt.
  1096. * We do that by jumping to absolute address 0x500 for
  1097. * external interrupts, or the machine_check_fwnmi label
  1098. * for machine checks (since firmware might have patched
  1099. * the vector area at 0x200). The [h]rfid at the end of the
  1100. * handler will return to the book3s_hv_interrupts.S code.
  1101. * For other interrupts we do the rfid to get back
  1102. * to the book3s_hv_interrupts.S code here.
  1103. */
  1104. ld r8, HSTATE_VMHANDLER(r13)
  1105. ld r7, HSTATE_HOST_MSR(r13)
  1106. cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  1107. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  1108. BEGIN_FTR_SECTION
  1109. beq 11f
  1110. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
  1111. /* RFI into the highmem handler, or branch to interrupt handler */
  1112. mfmsr r6
  1113. li r0, MSR_RI
  1114. andc r6, r6, r0
  1115. mtmsrd r6, 1 /* Clear RI in MSR */
  1116. mtsrr0 r8
  1117. mtsrr1 r7
  1118. beqa 0x500 /* external interrupt (PPC970) */
  1119. beq cr1, 13f /* machine check */
  1120. RFI
  1121. /* On POWER7, we have external interrupts set to use HSRR0/1 */
  1122. 11: mtspr SPRN_HSRR0, r8
  1123. mtspr SPRN_HSRR1, r7
  1124. ba 0x500
  1125. 13: b machine_check_fwnmi
  1126. /*
  1127. * Check whether an HDSI is an HPTE not found fault or something else.
  1128. * If it is an HPTE not found fault that is due to the guest accessing
  1129. * a page that they have mapped but which we have paged out, then
  1130. * we continue on with the guest exit path. In all other cases,
  1131. * reflect the HDSI to the guest as a DSI.
  1132. */
  1133. kvmppc_hdsi:
  1134. mfspr r4, SPRN_HDAR
  1135. mfspr r6, SPRN_HDSISR
  1136. /* HPTE not found fault or protection fault? */
  1137. andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
  1138. beq 1f /* if not, send it to the guest */
  1139. andi. r0, r11, MSR_DR /* data relocation enabled? */
  1140. beq 3f
  1141. clrrdi r0, r4, 28
  1142. PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
  1143. bne 1f /* if no SLB entry found */
  1144. 4: std r4, VCPU_FAULT_DAR(r9)
  1145. stw r6, VCPU_FAULT_DSISR(r9)
  1146. /* Search the hash table. */
  1147. mr r3, r9 /* vcpu pointer */
  1148. li r7, 1 /* data fault */
  1149. bl .kvmppc_hpte_hv_fault
  1150. ld r9, HSTATE_KVM_VCPU(r13)
  1151. ld r10, VCPU_PC(r9)
  1152. ld r11, VCPU_MSR(r9)
  1153. li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
  1154. cmpdi r3, 0 /* retry the instruction */
  1155. beq 6f
  1156. cmpdi r3, -1 /* handle in kernel mode */
  1157. beq guest_exit_cont
  1158. cmpdi r3, -2 /* MMIO emulation; need instr word */
  1159. beq 2f
  1160. /* Synthesize a DSI for the guest */
  1161. ld r4, VCPU_FAULT_DAR(r9)
  1162. mr r6, r3
  1163. 1: mtspr SPRN_DAR, r4
  1164. mtspr SPRN_DSISR, r6
  1165. mtspr SPRN_SRR0, r10
  1166. mtspr SPRN_SRR1, r11
  1167. li r10, BOOK3S_INTERRUPT_DATA_STORAGE
  1168. li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
  1169. rotldi r11, r11, 63
  1170. fast_interrupt_c_return:
  1171. 6: ld r7, VCPU_CTR(r9)
  1172. lwz r8, VCPU_XER(r9)
  1173. mtctr r7
  1174. mtxer r8
  1175. mr r4, r9
  1176. b fast_guest_return
  1177. 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
  1178. ld r5, KVM_VRMA_SLB_V(r5)
  1179. b 4b
  1180. /* If this is for emulated MMIO, load the instruction word */
  1181. 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
  1182. /* Set guest mode to 'jump over instruction' so if lwz faults
  1183. * we'll just continue at the next IP. */
  1184. li r0, KVM_GUEST_MODE_SKIP
  1185. stb r0, HSTATE_IN_GUEST(r13)
  1186. /* Do the access with MSR:DR enabled */
  1187. mfmsr r3
  1188. ori r4, r3, MSR_DR /* Enable paging for data */
  1189. mtmsrd r4
  1190. lwz r8, 0(r10)
  1191. mtmsrd r3
  1192. /* Store the result */
  1193. stw r8, VCPU_LAST_INST(r9)
  1194. /* Unset guest mode. */
  1195. li r0, KVM_GUEST_MODE_NONE
  1196. stb r0, HSTATE_IN_GUEST(r13)
  1197. b guest_exit_cont
  1198. /*
  1199. * Similarly for an HISI, reflect it to the guest as an ISI unless
  1200. * it is an HPTE not found fault for a page that we have paged out.
  1201. */
  1202. kvmppc_hisi:
  1203. andis. r0, r11, SRR1_ISI_NOPT@h
  1204. beq 1f
  1205. andi. r0, r11, MSR_IR /* instruction relocation enabled? */
  1206. beq 3f
  1207. clrrdi r0, r10, 28
  1208. PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
  1209. bne 1f /* if no SLB entry found */
  1210. 4:
  1211. /* Search the hash table. */
  1212. mr r3, r9 /* vcpu pointer */
  1213. mr r4, r10
  1214. mr r6, r11
  1215. li r7, 0 /* instruction fault */
  1216. bl .kvmppc_hpte_hv_fault
  1217. ld r9, HSTATE_KVM_VCPU(r13)
  1218. ld r10, VCPU_PC(r9)
  1219. ld r11, VCPU_MSR(r9)
  1220. li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
  1221. cmpdi r3, 0 /* retry the instruction */
  1222. beq fast_interrupt_c_return
  1223. cmpdi r3, -1 /* handle in kernel mode */
  1224. beq guest_exit_cont
  1225. /* Synthesize an ISI for the guest */
  1226. mr r11, r3
  1227. 1: mtspr SPRN_SRR0, r10
  1228. mtspr SPRN_SRR1, r11
  1229. li r10, BOOK3S_INTERRUPT_INST_STORAGE
  1230. li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
  1231. rotldi r11, r11, 63
  1232. b fast_interrupt_c_return
  1233. 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
  1234. ld r5, KVM_VRMA_SLB_V(r6)
  1235. b 4b
  1236. /*
  1237. * Try to handle an hcall in real mode.
  1238. * Returns to the guest if we handle it, or continues on up to
  1239. * the kernel if we can't (i.e. if we don't have a handler for
  1240. * it, or if the handler returns H_TOO_HARD).
  1241. */
  1242. .globl hcall_try_real_mode
  1243. hcall_try_real_mode:
  1244. ld r3,VCPU_GPR(R3)(r9)
  1245. andi. r0,r11,MSR_PR
  1246. bne guest_exit_cont
  1247. clrrdi r3,r3,2
  1248. cmpldi r3,hcall_real_table_end - hcall_real_table
  1249. bge guest_exit_cont
  1250. LOAD_REG_ADDR(r4, hcall_real_table)
  1251. lwzx r3,r3,r4
  1252. cmpwi r3,0
  1253. beq guest_exit_cont
  1254. add r3,r3,r4
  1255. mtctr r3
  1256. mr r3,r9 /* get vcpu pointer */
  1257. ld r4,VCPU_GPR(R4)(r9)
  1258. bctrl
  1259. cmpdi r3,H_TOO_HARD
  1260. beq hcall_real_fallback
  1261. ld r4,HSTATE_KVM_VCPU(r13)
  1262. std r3,VCPU_GPR(R3)(r4)
  1263. ld r10,VCPU_PC(r4)
  1264. ld r11,VCPU_MSR(r4)
  1265. b fast_guest_return
  1266. /* We've attempted a real mode hcall, but it's punted it back
  1267. * to userspace. We need to restore some clobbered volatiles
  1268. * before resuming the pass-it-to-qemu path */
  1269. hcall_real_fallback:
  1270. li r12,BOOK3S_INTERRUPT_SYSCALL
  1271. ld r9, HSTATE_KVM_VCPU(r13)
  1272. b guest_exit_cont
  1273. .globl hcall_real_table
  1274. hcall_real_table:
  1275. .long 0 /* 0 - unused */
  1276. .long .kvmppc_h_remove - hcall_real_table
  1277. .long .kvmppc_h_enter - hcall_real_table
  1278. .long .kvmppc_h_read - hcall_real_table
  1279. .long 0 /* 0x10 - H_CLEAR_MOD */
  1280. .long 0 /* 0x14 - H_CLEAR_REF */
  1281. .long .kvmppc_h_protect - hcall_real_table
  1282. .long 0 /* 0x1c - H_GET_TCE */
  1283. .long .kvmppc_h_put_tce - hcall_real_table
  1284. .long 0 /* 0x24 - H_SET_SPRG0 */
  1285. .long .kvmppc_h_set_dabr - hcall_real_table
  1286. .long 0 /* 0x2c */
  1287. .long 0 /* 0x30 */
  1288. .long 0 /* 0x34 */
  1289. .long 0 /* 0x38 */
  1290. .long 0 /* 0x3c */
  1291. .long 0 /* 0x40 */
  1292. .long 0 /* 0x44 */
  1293. .long 0 /* 0x48 */
  1294. .long 0 /* 0x4c */
  1295. .long 0 /* 0x50 */
  1296. .long 0 /* 0x54 */
  1297. .long 0 /* 0x58 */
  1298. .long 0 /* 0x5c */
  1299. .long 0 /* 0x60 */
  1300. #ifdef CONFIG_KVM_XICS
  1301. .long .kvmppc_rm_h_eoi - hcall_real_table
  1302. .long .kvmppc_rm_h_cppr - hcall_real_table
  1303. .long .kvmppc_rm_h_ipi - hcall_real_table
  1304. .long 0 /* 0x70 - H_IPOLL */
  1305. .long .kvmppc_rm_h_xirr - hcall_real_table
  1306. #else
  1307. .long 0 /* 0x64 - H_EOI */
  1308. .long 0 /* 0x68 - H_CPPR */
  1309. .long 0 /* 0x6c - H_IPI */
  1310. .long 0 /* 0x70 - H_IPOLL */
  1311. .long 0 /* 0x74 - H_XIRR */
  1312. #endif
  1313. .long 0 /* 0x78 */
  1314. .long 0 /* 0x7c */
  1315. .long 0 /* 0x80 */
  1316. .long 0 /* 0x84 */
  1317. .long 0 /* 0x88 */
  1318. .long 0 /* 0x8c */
  1319. .long 0 /* 0x90 */
  1320. .long 0 /* 0x94 */
  1321. .long 0 /* 0x98 */
  1322. .long 0 /* 0x9c */
  1323. .long 0 /* 0xa0 */
  1324. .long 0 /* 0xa4 */
  1325. .long 0 /* 0xa8 */
  1326. .long 0 /* 0xac */
  1327. .long 0 /* 0xb0 */
  1328. .long 0 /* 0xb4 */
  1329. .long 0 /* 0xb8 */
  1330. .long 0 /* 0xbc */
  1331. .long 0 /* 0xc0 */
  1332. .long 0 /* 0xc4 */
  1333. .long 0 /* 0xc8 */
  1334. .long 0 /* 0xcc */
  1335. .long 0 /* 0xd0 */
  1336. .long 0 /* 0xd4 */
  1337. .long 0 /* 0xd8 */
  1338. .long 0 /* 0xdc */
  1339. .long .kvmppc_h_cede - hcall_real_table
  1340. .long 0 /* 0xe4 */
  1341. .long 0 /* 0xe8 */
  1342. .long 0 /* 0xec */
  1343. .long 0 /* 0xf0 */
  1344. .long 0 /* 0xf4 */
  1345. .long 0 /* 0xf8 */
  1346. .long 0 /* 0xfc */
  1347. .long 0 /* 0x100 */
  1348. .long 0 /* 0x104 */
  1349. .long 0 /* 0x108 */
  1350. .long 0 /* 0x10c */
  1351. .long 0 /* 0x110 */
  1352. .long 0 /* 0x114 */
  1353. .long 0 /* 0x118 */
  1354. .long 0 /* 0x11c */
  1355. .long 0 /* 0x120 */
  1356. .long .kvmppc_h_bulk_remove - hcall_real_table
  1357. hcall_real_table_end:
  1358. ignore_hdec:
  1359. mr r4,r9
  1360. b fast_guest_return
  1361. _GLOBAL(kvmppc_h_set_dabr)
  1362. std r4,VCPU_DABR(r3)
  1363. /* Work around P7 bug where DABR can get corrupted on mtspr */
  1364. 1: mtspr SPRN_DABR,r4
  1365. mfspr r5, SPRN_DABR
  1366. cmpd r4, r5
  1367. bne 1b
  1368. isync
  1369. li r3,0
  1370. blr
  1371. _GLOBAL(kvmppc_h_cede)
  1372. ori r11,r11,MSR_EE
  1373. std r11,VCPU_MSR(r3)
  1374. li r0,1
  1375. stb r0,VCPU_CEDED(r3)
  1376. sync /* order setting ceded vs. testing prodded */
  1377. lbz r5,VCPU_PRODDED(r3)
  1378. cmpwi r5,0
  1379. bne kvm_cede_prodded
  1380. li r0,0 /* set trap to 0 to say hcall is handled */
  1381. stw r0,VCPU_TRAP(r3)
  1382. li r0,H_SUCCESS
  1383. std r0,VCPU_GPR(R3)(r3)
  1384. BEGIN_FTR_SECTION
  1385. b kvm_cede_exit /* just send it up to host on 970 */
  1386. END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
  1387. /*
  1388. * Set our bit in the bitmask of napping threads unless all the
  1389. * other threads are already napping, in which case we send this
  1390. * up to the host.
  1391. */
  1392. ld r5,HSTATE_KVM_VCORE(r13)
  1393. lwz r6,VCPU_PTID(r3)
  1394. lwz r8,VCORE_ENTRY_EXIT(r5)
  1395. clrldi r8,r8,56
  1396. li r0,1
  1397. sld r0,r0,r6
  1398. addi r6,r5,VCORE_NAPPING_THREADS
  1399. 31: lwarx r4,0,r6
  1400. or r4,r4,r0
  1401. PPC_POPCNTW(R7,R4)
  1402. cmpw r7,r8
  1403. bge kvm_cede_exit
  1404. stwcx. r4,0,r6
  1405. bne 31b
  1406. li r0,1
  1407. stb r0,HSTATE_NAPPING(r13)
  1408. /* order napping_threads update vs testing entry_exit_count */
  1409. lwsync
  1410. mr r4,r3
  1411. lwz r7,VCORE_ENTRY_EXIT(r5)
  1412. cmpwi r7,0x100
  1413. bge 33f /* another thread already exiting */
  1414. /*
  1415. * Although not specifically required by the architecture, POWER7
  1416. * preserves the following registers in nap mode, even if an SMT mode
  1417. * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
  1418. * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
  1419. */
  1420. /* Save non-volatile GPRs */
  1421. std r14, VCPU_GPR(R14)(r3)
  1422. std r15, VCPU_GPR(R15)(r3)
  1423. std r16, VCPU_GPR(R16)(r3)
  1424. std r17, VCPU_GPR(R17)(r3)
  1425. std r18, VCPU_GPR(R18)(r3)
  1426. std r19, VCPU_GPR(R19)(r3)
  1427. std r20, VCPU_GPR(R20)(r3)
  1428. std r21, VCPU_GPR(R21)(r3)
  1429. std r22, VCPU_GPR(R22)(r3)
  1430. std r23, VCPU_GPR(R23)(r3)
  1431. std r24, VCPU_GPR(R24)(r3)
  1432. std r25, VCPU_GPR(R25)(r3)
  1433. std r26, VCPU_GPR(R26)(r3)
  1434. std r27, VCPU_GPR(R27)(r3)
  1435. std r28, VCPU_GPR(R28)(r3)
  1436. std r29, VCPU_GPR(R29)(r3)
  1437. std r30, VCPU_GPR(R30)(r3)
  1438. std r31, VCPU_GPR(R31)(r3)
  1439. /* save FP state */
  1440. bl .kvmppc_save_fp
  1441. /*
  1442. * Take a nap until a decrementer or external interrupt occurs,
  1443. * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
  1444. */
  1445. li r0,1
  1446. stb r0,HSTATE_HWTHREAD_REQ(r13)
  1447. mfspr r5,SPRN_LPCR
  1448. ori r5,r5,LPCR_PECE0 | LPCR_PECE1
  1449. mtspr SPRN_LPCR,r5
  1450. isync
  1451. li r0, 0
  1452. std r0, HSTATE_SCRATCH0(r13)
  1453. ptesync
  1454. ld r0, HSTATE_SCRATCH0(r13)
  1455. 1: cmpd r0, r0
  1456. bne 1b
  1457. nap
  1458. b .
  1459. kvm_end_cede:
  1460. /* get vcpu pointer */
  1461. ld r4, HSTATE_KVM_VCPU(r13)
  1462. /* Woken by external or decrementer interrupt */
  1463. ld r1, HSTATE_HOST_R1(r13)
  1464. /* load up FP state */
  1465. bl kvmppc_load_fp
  1466. /* Load NV GPRS */
  1467. ld r14, VCPU_GPR(R14)(r4)
  1468. ld r15, VCPU_GPR(R15)(r4)
  1469. ld r16, VCPU_GPR(R16)(r4)
  1470. ld r17, VCPU_GPR(R17)(r4)
  1471. ld r18, VCPU_GPR(R18)(r4)
  1472. ld r19, VCPU_GPR(R19)(r4)
  1473. ld r20, VCPU_GPR(R20)(r4)
  1474. ld r21, VCPU_GPR(R21)(r4)
  1475. ld r22, VCPU_GPR(R22)(r4)
  1476. ld r23, VCPU_GPR(R23)(r4)
  1477. ld r24, VCPU_GPR(R24)(r4)
  1478. ld r25, VCPU_GPR(R25)(r4)
  1479. ld r26, VCPU_GPR(R26)(r4)
  1480. ld r27, VCPU_GPR(R27)(r4)
  1481. ld r28, VCPU_GPR(R28)(r4)
  1482. ld r29, VCPU_GPR(R29)(r4)
  1483. ld r30, VCPU_GPR(R30)(r4)
  1484. ld r31, VCPU_GPR(R31)(r4)
  1485. /* clear our bit in vcore->napping_threads */
  1486. 33: ld r5,HSTATE_KVM_VCORE(r13)
  1487. lwz r3,VCPU_PTID(r4)
  1488. li r0,1
  1489. sld r0,r0,r3
  1490. addi r6,r5,VCORE_NAPPING_THREADS
  1491. 32: lwarx r7,0,r6
  1492. andc r7,r7,r0
  1493. stwcx. r7,0,r6
  1494. bne 32b
  1495. li r0,0
  1496. stb r0,HSTATE_NAPPING(r13)
  1497. /* Check the wake reason in SRR1 to see why we got here */
  1498. mfspr r3, SPRN_SRR1
  1499. rlwinm r3, r3, 44-31, 0x7 /* extract wake reason field */
  1500. cmpwi r3, 4 /* was it an external interrupt? */
  1501. li r12, BOOK3S_INTERRUPT_EXTERNAL
  1502. mr r9, r4
  1503. ld r10, VCPU_PC(r9)
  1504. ld r11, VCPU_MSR(r9)
  1505. beq do_ext_interrupt /* if so */
  1506. /* see if any other thread is already exiting */
  1507. lwz r0,VCORE_ENTRY_EXIT(r5)
  1508. cmpwi r0,0x100
  1509. blt kvmppc_cede_reentry /* if not go back to guest */
  1510. /* some threads are exiting, so go to the guest exit path */
  1511. b hcall_real_fallback
  1512. /* cede when already previously prodded case */
  1513. kvm_cede_prodded:
  1514. li r0,0
  1515. stb r0,VCPU_PRODDED(r3)
  1516. sync /* order testing prodded vs. clearing ceded */
  1517. stb r0,VCPU_CEDED(r3)
  1518. li r3,H_SUCCESS
  1519. blr
  1520. /* we've ceded but we want to give control to the host */
  1521. kvm_cede_exit:
  1522. b hcall_real_fallback
  1523. /* Try to handle a machine check in real mode */
  1524. machine_check_realmode:
  1525. mr r3, r9 /* get vcpu pointer */
  1526. bl .kvmppc_realmode_machine_check
  1527. nop
  1528. cmpdi r3, 0 /* continue exiting from guest? */
  1529. ld r9, HSTATE_KVM_VCPU(r13)
  1530. li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
  1531. beq mc_cont
  1532. /* If not, deliver a machine check. SRR0/1 are already set */
  1533. li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
  1534. li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
  1535. rotldi r11, r11, 63
  1536. b fast_interrupt_c_return
  1537. secondary_too_late:
  1538. ld r5,HSTATE_KVM_VCORE(r13)
  1539. HMT_LOW
  1540. 13: lbz r3,VCORE_IN_GUEST(r5)
  1541. cmpwi r3,0
  1542. bne 13b
  1543. HMT_MEDIUM
  1544. ld r11,PACA_SLBSHADOWPTR(r13)
  1545. .rept SLB_NUM_BOLTED
  1546. ld r5,SLBSHADOW_SAVEAREA(r11)
  1547. ld r6,SLBSHADOW_SAVEAREA+8(r11)
  1548. andis. r7,r5,SLB_ESID_V@h
  1549. beq 1f
  1550. slbmte r6,r5
  1551. 1: addi r11,r11,16
  1552. .endr
  1553. secondary_nap:
  1554. /* Clear our vcpu pointer so we don't come back in early */
  1555. li r0, 0
  1556. std r0, HSTATE_KVM_VCPU(r13)
  1557. lwsync
  1558. /* Clear any pending IPI - assume we're a secondary thread */
  1559. ld r5, HSTATE_XICS_PHYS(r13)
  1560. li r7, XICS_XIRR
  1561. lwzcix r3, r5, r7 /* ack any pending interrupt */
  1562. rlwinm. r0, r3, 0, 0xffffff /* any pending? */
  1563. beq 37f
  1564. sync
  1565. li r0, 0xff
  1566. li r6, XICS_MFRR
  1567. stbcix r0, r5, r6 /* clear the IPI */
  1568. stwcix r3, r5, r7 /* EOI it */
  1569. 37: sync
  1570. /* increment the nap count and then go to nap mode */
  1571. ld r4, HSTATE_KVM_VCORE(r13)
  1572. addi r4, r4, VCORE_NAP_COUNT
  1573. lwsync /* make previous updates visible */
  1574. 51: lwarx r3, 0, r4
  1575. addi r3, r3, 1
  1576. stwcx. r3, 0, r4
  1577. bne 51b
  1578. kvm_no_guest:
  1579. li r0, KVM_HWTHREAD_IN_NAP
  1580. stb r0, HSTATE_HWTHREAD_STATE(r13)
  1581. li r3, LPCR_PECE0
  1582. mfspr r4, SPRN_LPCR
  1583. rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
  1584. mtspr SPRN_LPCR, r4
  1585. isync
  1586. std r0, HSTATE_SCRATCH0(r13)
  1587. ptesync
  1588. ld r0, HSTATE_SCRATCH0(r13)
  1589. 1: cmpd r0, r0
  1590. bne 1b
  1591. nap
  1592. b .
  1593. /*
  1594. * Save away FP, VMX and VSX registers.
  1595. * r3 = vcpu pointer
  1596. */
  1597. _GLOBAL(kvmppc_save_fp)
  1598. mfmsr r5
  1599. ori r8,r5,MSR_FP
  1600. #ifdef CONFIG_ALTIVEC
  1601. BEGIN_FTR_SECTION
  1602. oris r8,r8,MSR_VEC@h
  1603. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  1604. #endif
  1605. #ifdef CONFIG_VSX
  1606. BEGIN_FTR_SECTION
  1607. oris r8,r8,MSR_VSX@h
  1608. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  1609. #endif
  1610. mtmsrd r8
  1611. isync
  1612. #ifdef CONFIG_VSX
  1613. BEGIN_FTR_SECTION
  1614. reg = 0
  1615. .rept 32
  1616. li r6,reg*16+VCPU_VSRS
  1617. STXVD2X(reg,R6,R3)
  1618. reg = reg + 1
  1619. .endr
  1620. FTR_SECTION_ELSE
  1621. #endif
  1622. reg = 0
  1623. .rept 32
  1624. stfd reg,reg*8+VCPU_FPRS(r3)
  1625. reg = reg + 1
  1626. .endr
  1627. #ifdef CONFIG_VSX
  1628. ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
  1629. #endif
  1630. mffs fr0
  1631. stfd fr0,VCPU_FPSCR(r3)
  1632. #ifdef CONFIG_ALTIVEC
  1633. BEGIN_FTR_SECTION
  1634. reg = 0
  1635. .rept 32
  1636. li r6,reg*16+VCPU_VRS
  1637. stvx reg,r6,r3
  1638. reg = reg + 1
  1639. .endr
  1640. mfvscr vr0
  1641. li r6,VCPU_VSCR
  1642. stvx vr0,r6,r3
  1643. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  1644. #endif
  1645. mfspr r6,SPRN_VRSAVE
  1646. stw r6,VCPU_VRSAVE(r3)
  1647. mtmsrd r5
  1648. isync
  1649. blr
  1650. /*
  1651. * Load up FP, VMX and VSX registers
  1652. * r4 = vcpu pointer
  1653. */
  1654. .globl kvmppc_load_fp
  1655. kvmppc_load_fp:
  1656. mfmsr r9
  1657. ori r8,r9,MSR_FP
  1658. #ifdef CONFIG_ALTIVEC
  1659. BEGIN_FTR_SECTION
  1660. oris r8,r8,MSR_VEC@h
  1661. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  1662. #endif
  1663. #ifdef CONFIG_VSX
  1664. BEGIN_FTR_SECTION
  1665. oris r8,r8,MSR_VSX@h
  1666. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  1667. #endif
  1668. mtmsrd r8
  1669. isync
  1670. lfd fr0,VCPU_FPSCR(r4)
  1671. MTFSF_L(fr0)
  1672. #ifdef CONFIG_VSX
  1673. BEGIN_FTR_SECTION
  1674. reg = 0
  1675. .rept 32
  1676. li r7,reg*16+VCPU_VSRS
  1677. LXVD2X(reg,R7,R4)
  1678. reg = reg + 1
  1679. .endr
  1680. FTR_SECTION_ELSE
  1681. #endif
  1682. reg = 0
  1683. .rept 32
  1684. lfd reg,reg*8+VCPU_FPRS(r4)
  1685. reg = reg + 1
  1686. .endr
  1687. #ifdef CONFIG_VSX
  1688. ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
  1689. #endif
  1690. #ifdef CONFIG_ALTIVEC
  1691. BEGIN_FTR_SECTION
  1692. li r7,VCPU_VSCR
  1693. lvx vr0,r7,r4
  1694. mtvscr vr0
  1695. reg = 0
  1696. .rept 32
  1697. li r7,reg*16+VCPU_VRS
  1698. lvx reg,r7,r4
  1699. reg = reg + 1
  1700. .endr
  1701. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  1702. #endif
  1703. lwz r7,VCPU_VRSAVE(r4)
  1704. mtspr SPRN_VRSAVE,r7
  1705. blr