tm.S 8.9 KB

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