book3s_hv_rmhandlers.S 45 KB

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