book3s_hv_rmhandlers.S 43 KB

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