book3s_hv_rmhandlers.S 40 KB

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