book3s_hv_rmhandlers.S 40 KB

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