book3s_hv_rmhandlers.S 40 KB

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