tm.S 9.3 KB

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