tm.S 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. * Derived from book3s_hv_rmhandlers.S, which is:
  12. *
  13. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  14. *
  15. */
  16. #include <asm/reg.h>
  17. #include <asm/ppc_asm.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/export.h>
  20. #include <asm/tm.h>
  21. #include <asm/cputable.h>
  22. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  23. #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
  24. /*
  25. * Save transactional state and TM-related registers.
  26. * Called with:
  27. * - r3 pointing to the vcpu struct
  28. * - r4 points to the MSR with current TS bits:
  29. * (For HV KVM, it is VCPU_MSR ; For PR KVM, it is host MSR).
  30. * This can modify all checkpointed registers, but
  31. * restores r1, r2 before exit.
  32. */
  33. _GLOBAL(__kvmppc_save_tm)
  34. mflr r0
  35. std r0, PPC_LR_STKOFF(r1)
  36. /* Turn on TM. */
  37. mfmsr r8
  38. li r0, 1
  39. rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
  40. ori r8, r8, MSR_FP
  41. oris r8, r8, (MSR_VEC | MSR_VSX)@h
  42. mtmsrd r8
  43. rldicl. r4, r4, 64 - MSR_TS_S_LG, 62
  44. beq 1f /* TM not active in guest. */
  45. std r1, HSTATE_SCRATCH2(r13)
  46. std r3, HSTATE_SCRATCH1(r13)
  47. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  48. BEGIN_FTR_SECTION
  49. /* Emulation of the treclaim instruction needs TEXASR before treclaim */
  50. mfspr r6, SPRN_TEXASR
  51. std r6, VCPU_ORIG_TEXASR(r3)
  52. END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_HV_ASSIST)
  53. #endif
  54. /* Clear the MSR RI since r1, r13 are all going to be foobar. */
  55. li r5, 0
  56. mtmsrd r5, 1
  57. li r3, TM_CAUSE_KVM_RESCHED
  58. /* All GPRs are volatile at this point. */
  59. TRECLAIM(R3)
  60. /* Temporarily store r13 and r9 so we have some regs to play with */
  61. SET_SCRATCH0(r13)
  62. GET_PACA(r13)
  63. std r9, PACATMSCRATCH(r13)
  64. ld r9, HSTATE_SCRATCH1(r13)
  65. /* Get a few more GPRs free. */
  66. std r29, VCPU_GPRS_TM(29)(r9)
  67. std r30, VCPU_GPRS_TM(30)(r9)
  68. std r31, VCPU_GPRS_TM(31)(r9)
  69. /* Save away PPR and DSCR soon so don't run with user values. */
  70. mfspr r31, SPRN_PPR
  71. HMT_MEDIUM
  72. mfspr r30, SPRN_DSCR
  73. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  74. ld r29, HSTATE_DSCR(r13)
  75. mtspr SPRN_DSCR, r29
  76. #endif
  77. /* Save all but r9, r13 & r29-r31 */
  78. reg = 0
  79. .rept 29
  80. .if (reg != 9) && (reg != 13)
  81. std reg, VCPU_GPRS_TM(reg)(r9)
  82. .endif
  83. reg = reg + 1
  84. .endr
  85. /* ... now save r13 */
  86. GET_SCRATCH0(r4)
  87. std r4, VCPU_GPRS_TM(13)(r9)
  88. /* ... and save r9 */
  89. ld r4, PACATMSCRATCH(r13)
  90. std r4, VCPU_GPRS_TM(9)(r9)
  91. /* Reload stack pointer and TOC. */
  92. ld r1, HSTATE_SCRATCH2(r13)
  93. ld r2, PACATOC(r13)
  94. /* Set MSR RI now we have r1 and r13 back. */
  95. li r5, MSR_RI
  96. mtmsrd r5, 1
  97. /* Save away checkpinted SPRs. */
  98. std r31, VCPU_PPR_TM(r9)
  99. std r30, VCPU_DSCR_TM(r9)
  100. mflr r5
  101. mfcr r6
  102. mfctr r7
  103. mfspr r8, SPRN_AMR
  104. mfspr r10, SPRN_TAR
  105. mfxer r11
  106. std r5, VCPU_LR_TM(r9)
  107. stw r6, VCPU_CR_TM(r9)
  108. std r7, VCPU_CTR_TM(r9)
  109. std r8, VCPU_AMR_TM(r9)
  110. std r10, VCPU_TAR_TM(r9)
  111. std r11, VCPU_XER_TM(r9)
  112. /* Restore r12 as trap number. */
  113. lwz r12, VCPU_TRAP(r9)
  114. /* Save FP/VSX. */
  115. addi r3, r9, VCPU_FPRS_TM
  116. bl store_fp_state
  117. addi r3, r9, VCPU_VRS_TM
  118. bl store_vr_state
  119. mfspr r6, SPRN_VRSAVE
  120. stw r6, VCPU_VRSAVE_TM(r9)
  121. 1:
  122. /*
  123. * We need to save these SPRs after the treclaim so that the software
  124. * error code is recorded correctly in the TEXASR. Also the user may
  125. * change these outside of a transaction, so they must always be
  126. * context switched.
  127. */
  128. mfspr r7, SPRN_TEXASR
  129. std r7, VCPU_TEXASR(r9)
  130. 11:
  131. mfspr r5, SPRN_TFHAR
  132. mfspr r6, SPRN_TFIAR
  133. std r5, VCPU_TFHAR(r9)
  134. std r6, VCPU_TFIAR(r9)
  135. ld r0, PPC_LR_STKOFF(r1)
  136. mtlr r0
  137. blr
  138. /*
  139. * _kvmppc_save_tm_pr() is a wrapper around __kvmppc_save_tm(), so that it can
  140. * be invoked from C function by PR KVM only.
  141. */
  142. _GLOBAL(_kvmppc_save_tm_pr)
  143. mflr r5
  144. std r5, PPC_LR_STKOFF(r1)
  145. stdu r1, -SWITCH_FRAME_SIZE(r1)
  146. SAVE_NVGPRS(r1)
  147. /* save MSR since TM/math bits might be impacted
  148. * by __kvmppc_save_tm().
  149. */
  150. mfmsr r5
  151. SAVE_GPR(5, r1)
  152. /* also save DSCR/CR/TAR so that it can be recovered later */
  153. mfspr r6, SPRN_DSCR
  154. SAVE_GPR(6, r1)
  155. mfcr r7
  156. stw r7, _CCR(r1)
  157. mfspr r8, SPRN_TAR
  158. SAVE_GPR(8, r1)
  159. bl __kvmppc_save_tm
  160. REST_GPR(8, r1)
  161. mtspr SPRN_TAR, r8
  162. ld r7, _CCR(r1)
  163. mtcr r7
  164. REST_GPR(6, r1)
  165. mtspr SPRN_DSCR, r6
  166. /* need preserve current MSR's MSR_TS bits */
  167. REST_GPR(5, r1)
  168. mfmsr r6
  169. rldicl r6, r6, 64 - MSR_TS_S_LG, 62
  170. rldimi r5, r6, MSR_TS_S_LG, 63 - MSR_TS_T_LG
  171. mtmsrd r5
  172. REST_NVGPRS(r1)
  173. addi r1, r1, SWITCH_FRAME_SIZE
  174. ld r5, PPC_LR_STKOFF(r1)
  175. mtlr r5
  176. blr
  177. EXPORT_SYMBOL_GPL(_kvmppc_save_tm_pr);
  178. /*
  179. * Restore transactional state and TM-related registers.
  180. * Called with:
  181. * - r3 pointing to the vcpu struct.
  182. * - r4 is the guest MSR with desired TS bits:
  183. * For HV KVM, it is VCPU_MSR
  184. * For PR KVM, it is provided by caller
  185. * This potentially modifies all checkpointed registers.
  186. * It restores r1, r2 from the PACA.
  187. */
  188. _GLOBAL(__kvmppc_restore_tm)
  189. mflr r0
  190. std r0, PPC_LR_STKOFF(r1)
  191. /* Turn on TM/FP/VSX/VMX so we can restore them. */
  192. mfmsr r5
  193. li r6, MSR_TM >> 32
  194. sldi r6, r6, 32
  195. or r5, r5, r6
  196. ori r5, r5, MSR_FP
  197. oris r5, r5, (MSR_VEC | MSR_VSX)@h
  198. mtmsrd r5
  199. /*
  200. * The user may change these outside of a transaction, so they must
  201. * always be context switched.
  202. */
  203. ld r5, VCPU_TFHAR(r3)
  204. ld r6, VCPU_TFIAR(r3)
  205. ld r7, VCPU_TEXASR(r3)
  206. mtspr SPRN_TFHAR, r5
  207. mtspr SPRN_TFIAR, r6
  208. mtspr SPRN_TEXASR, r7
  209. mr r5, r4
  210. rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
  211. beqlr /* TM not active in guest */
  212. std r1, HSTATE_SCRATCH2(r13)
  213. /* Make sure the failure summary is set, otherwise we'll program check
  214. * when we trechkpt. It's possible that this might have been not set
  215. * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
  216. * host.
  217. */
  218. oris r7, r7, (TEXASR_FS)@h
  219. mtspr SPRN_TEXASR, r7
  220. /*
  221. * We need to load up the checkpointed state for the guest.
  222. * We need to do this early as it will blow away any GPRs, VSRs and
  223. * some SPRs.
  224. */
  225. mr r31, r3
  226. addi r3, r31, VCPU_FPRS_TM
  227. bl load_fp_state
  228. addi r3, r31, VCPU_VRS_TM
  229. bl load_vr_state
  230. mr r3, r31
  231. lwz r7, VCPU_VRSAVE_TM(r3)
  232. mtspr SPRN_VRSAVE, r7
  233. ld r5, VCPU_LR_TM(r3)
  234. lwz r6, VCPU_CR_TM(r3)
  235. ld r7, VCPU_CTR_TM(r3)
  236. ld r8, VCPU_AMR_TM(r3)
  237. ld r9, VCPU_TAR_TM(r3)
  238. ld r10, VCPU_XER_TM(r3)
  239. mtlr r5
  240. mtcr r6
  241. mtctr r7
  242. mtspr SPRN_AMR, r8
  243. mtspr SPRN_TAR, r9
  244. mtxer r10
  245. /*
  246. * Load up PPR and DSCR values but don't put them in the actual SPRs
  247. * till the last moment to avoid running with userspace PPR and DSCR for
  248. * too long.
  249. */
  250. ld r29, VCPU_DSCR_TM(r3)
  251. ld r30, VCPU_PPR_TM(r3)
  252. std r2, PACATMSCRATCH(r13) /* Save TOC */
  253. /* Clear the MSR RI since r1, r13 are all going to be foobar. */
  254. li r5, 0
  255. mtmsrd r5, 1
  256. /* Load GPRs r0-r28 */
  257. reg = 0
  258. .rept 29
  259. ld reg, VCPU_GPRS_TM(reg)(r31)
  260. reg = reg + 1
  261. .endr
  262. mtspr SPRN_DSCR, r29
  263. mtspr SPRN_PPR, r30
  264. /* Load final GPRs */
  265. ld 29, VCPU_GPRS_TM(29)(r31)
  266. ld 30, VCPU_GPRS_TM(30)(r31)
  267. ld 31, VCPU_GPRS_TM(31)(r31)
  268. /* TM checkpointed state is now setup. All GPRs are now volatile. */
  269. TRECHKPT
  270. /* Now let's get back the state we need. */
  271. HMT_MEDIUM
  272. GET_PACA(r13)
  273. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  274. ld r29, HSTATE_DSCR(r13)
  275. mtspr SPRN_DSCR, r29
  276. #endif
  277. ld r1, HSTATE_SCRATCH2(r13)
  278. ld r2, PACATMSCRATCH(r13)
  279. /* Set the MSR RI since we have our registers back. */
  280. li r5, MSR_RI
  281. mtmsrd r5, 1
  282. ld r0, PPC_LR_STKOFF(r1)
  283. mtlr r0
  284. blr
  285. /*
  286. * _kvmppc_restore_tm_pr() is a wrapper around __kvmppc_restore_tm(), so that it
  287. * can be invoked from C function by PR KVM only.
  288. */
  289. _GLOBAL(_kvmppc_restore_tm_pr)
  290. mflr r5
  291. std r5, PPC_LR_STKOFF(r1)
  292. stdu r1, -SWITCH_FRAME_SIZE(r1)
  293. SAVE_NVGPRS(r1)
  294. /* save MSR to avoid TM/math bits change */
  295. mfmsr r5
  296. SAVE_GPR(5, r1)
  297. /* also save DSCR/CR/TAR so that it can be recovered later */
  298. mfspr r6, SPRN_DSCR
  299. SAVE_GPR(6, r1)
  300. mfcr r7
  301. stw r7, _CCR(r1)
  302. mfspr r8, SPRN_TAR
  303. SAVE_GPR(8, r1)
  304. bl __kvmppc_restore_tm
  305. REST_GPR(8, r1)
  306. mtspr SPRN_TAR, r8
  307. ld r7, _CCR(r1)
  308. mtcr r7
  309. REST_GPR(6, r1)
  310. mtspr SPRN_DSCR, r6
  311. /* need preserve current MSR's MSR_TS bits */
  312. REST_GPR(5, r1)
  313. mfmsr r6
  314. rldicl r6, r6, 64 - MSR_TS_S_LG, 62
  315. rldimi r5, r6, MSR_TS_S_LG, 63 - MSR_TS_T_LG
  316. mtmsrd r5
  317. REST_NVGPRS(r1)
  318. addi r1, r1, SWITCH_FRAME_SIZE
  319. ld r5, PPC_LR_STKOFF(r1)
  320. mtlr r5
  321. blr
  322. EXPORT_SYMBOL_GPL(_kvmppc_restore_tm_pr);
  323. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */