book3s_hv_rmhandlers.S 40 KB

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