book3s_hv_rmhandlers.S 35 KB

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