book3s_hv_rmhandlers.S 45 KB

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