book3s_hv_rmhandlers.S 46 KB

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