book3s_hv_rmhandlers.S 46 KB

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