book3s_hv_rmhandlers.S 41 KB

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