tm.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Transactional memory support routines to reclaim and recheckpoint
  4. * transactional process state.
  5. *
  6. * Copyright 2012 Matt Evans & Michael Neuling, IBM Corporation.
  7. */
  8. #include <asm/asm-offsets.h>
  9. #include <asm/ppc_asm.h>
  10. #include <asm/ppc-opcode.h>
  11. #include <asm/ptrace.h>
  12. #include <asm/reg.h>
  13. #include <asm/bug.h>
  14. #ifdef CONFIG_VSX
  15. /* See fpu.S, this is borrowed from there */
  16. #define __SAVE_32FPRS_VSRS(n,c,base) \
  17. BEGIN_FTR_SECTION \
  18. b 2f; \
  19. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  20. SAVE_32FPRS(n,base); \
  21. b 3f; \
  22. 2: SAVE_32VSRS(n,c,base); \
  23. 3:
  24. #define __REST_32FPRS_VSRS(n,c,base) \
  25. BEGIN_FTR_SECTION \
  26. b 2f; \
  27. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  28. REST_32FPRS(n,base); \
  29. b 3f; \
  30. 2: REST_32VSRS(n,c,base); \
  31. 3:
  32. #else
  33. #define __SAVE_32FPRS_VSRS(n,c,base) SAVE_32FPRS(n, base)
  34. #define __REST_32FPRS_VSRS(n,c,base) REST_32FPRS(n, base)
  35. #endif
  36. #define SAVE_32FPRS_VSRS(n,c,base) \
  37. __SAVE_32FPRS_VSRS(n,__REG_##c,__REG_##base)
  38. #define REST_32FPRS_VSRS(n,c,base) \
  39. __REST_32FPRS_VSRS(n,__REG_##c,__REG_##base)
  40. /* Stack frame offsets for local variables. */
  41. #define TM_FRAME_L0 TM_FRAME_SIZE-16
  42. #define TM_FRAME_L1 TM_FRAME_SIZE-8
  43. /* In order to access the TM SPRs, TM must be enabled. So, do so: */
  44. _GLOBAL(tm_enable)
  45. mfmsr r4
  46. li r3, MSR_TM >> 32
  47. sldi r3, r3, 32
  48. and. r0, r4, r3
  49. bne 1f
  50. or r4, r4, r3
  51. mtmsrd r4
  52. 1: blr
  53. _GLOBAL(tm_save_sprs)
  54. mfspr r0, SPRN_TFHAR
  55. std r0, THREAD_TM_TFHAR(r3)
  56. mfspr r0, SPRN_TEXASR
  57. std r0, THREAD_TM_TEXASR(r3)
  58. mfspr r0, SPRN_TFIAR
  59. std r0, THREAD_TM_TFIAR(r3)
  60. blr
  61. _GLOBAL(tm_restore_sprs)
  62. ld r0, THREAD_TM_TFHAR(r3)
  63. mtspr SPRN_TFHAR, r0
  64. ld r0, THREAD_TM_TEXASR(r3)
  65. mtspr SPRN_TEXASR, r0
  66. ld r0, THREAD_TM_TFIAR(r3)
  67. mtspr SPRN_TFIAR, r0
  68. blr
  69. /* Passed an 8-bit failure cause as first argument. */
  70. _GLOBAL(tm_abort)
  71. TABORT(R3)
  72. blr
  73. /* void tm_reclaim(struct thread_struct *thread,
  74. * uint8_t cause)
  75. *
  76. * - Performs a full reclaim. This destroys outstanding
  77. * transactions and updates thread->regs.tm_ckpt_* with the
  78. * original checkpointed state. Note that thread->regs is
  79. * unchanged.
  80. *
  81. * Purpose is to both abort transactions of, and preserve the state of,
  82. * a transactions at a context switch. We preserve/restore both sets of process
  83. * state to restore them when the thread's scheduled again. We continue in
  84. * userland as though nothing happened, but when the transaction is resumed
  85. * they will abort back to the checkpointed state we save out here.
  86. *
  87. * Call with IRQs off, stacks get all out of sync for some periods in here!
  88. */
  89. _GLOBAL(tm_reclaim)
  90. mfcr r5
  91. mflr r0
  92. stw r5, 8(r1)
  93. std r0, 16(r1)
  94. std r2, STK_GOT(r1)
  95. stdu r1, -TM_FRAME_SIZE(r1)
  96. /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD]. */
  97. std r3, STK_PARAM(R3)(r1)
  98. SAVE_NVGPRS(r1)
  99. /* We need to setup MSR for VSX register save instructions. */
  100. mfmsr r14
  101. mr r15, r14
  102. ori r15, r15, MSR_FP
  103. li r16, 0
  104. ori r16, r16, MSR_EE /* IRQs hard off */
  105. andc r15, r15, r16
  106. oris r15, r15, MSR_VEC@h
  107. #ifdef CONFIG_VSX
  108. BEGIN_FTR_SECTION
  109. oris r15,r15, MSR_VSX@h
  110. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  111. #endif
  112. mtmsrd r15
  113. std r14, TM_FRAME_L0(r1)
  114. /* Do sanity check on MSR to make sure we are suspended */
  115. li r7, (MSR_TS_S)@higher
  116. srdi r6, r14, 32
  117. and r6, r6, r7
  118. 1: tdeqi r6, 0
  119. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  120. /* Stash the stack pointer away for use after reclaim */
  121. std r1, PACAR1(r13)
  122. /* Clear MSR RI since we are about to change r1, EE is already off. */
  123. li r5, 0
  124. mtmsrd r5, 1
  125. /*
  126. * BE CAREFUL HERE:
  127. * At this point we can't take an SLB miss since we have MSR_RI
  128. * off. Load only to/from the stack/paca which are in SLB bolted regions
  129. * until we turn MSR RI back on.
  130. *
  131. * The moment we treclaim, ALL of our GPRs will switch
  132. * to user register state. (FPRs, CCR etc. also!)
  133. * Use an sprg and a tm_scratch in the PACA to shuffle.
  134. */
  135. TRECLAIM(R4) /* Cause in r4 */
  136. /* ******************** GPRs ******************** */
  137. /* Stash the checkpointed r13 away in the scratch SPR and get the real
  138. * paca
  139. */
  140. SET_SCRATCH0(r13)
  141. GET_PACA(r13)
  142. /* Stash the checkpointed r1 away in paca tm_scratch and get the real
  143. * stack pointer back
  144. */
  145. std r1, PACATMSCRATCH(r13)
  146. ld r1, PACAR1(r13)
  147. /* Store the PPR in r11 and reset to decent value */
  148. std r11, GPR11(r1) /* Temporary stash */
  149. /* Reset MSR RI so we can take SLB faults again */
  150. li r11, MSR_RI
  151. mtmsrd r11, 1
  152. mfspr r11, SPRN_PPR
  153. HMT_MEDIUM
  154. /* Now get some more GPRS free */
  155. std r7, GPR7(r1) /* Temporary stash */
  156. std r12, GPR12(r1) /* '' '' '' */
  157. ld r12, STK_PARAM(R3)(r1) /* Param 0, thread_struct * */
  158. std r11, THREAD_TM_PPR(r12) /* Store PPR and free r11 */
  159. addi r7, r12, PT_CKPT_REGS /* Thread's ckpt_regs */
  160. /* Make r7 look like an exception frame so that we
  161. * can use the neat GPRx(n) macros. r7 is NOT a pt_regs ptr!
  162. */
  163. subi r7, r7, STACK_FRAME_OVERHEAD
  164. /* Sync the userland GPRs 2-12, 14-31 to thread->regs: */
  165. SAVE_GPR(0, r7) /* user r0 */
  166. SAVE_GPR(2, r7) /* user r2 */
  167. SAVE_4GPRS(3, r7) /* user r3-r6 */
  168. SAVE_GPR(8, r7) /* user r8 */
  169. SAVE_GPR(9, r7) /* user r9 */
  170. SAVE_GPR(10, r7) /* user r10 */
  171. ld r3, PACATMSCRATCH(r13) /* user r1 */
  172. ld r4, GPR7(r1) /* user r7 */
  173. ld r5, GPR11(r1) /* user r11 */
  174. ld r6, GPR12(r1) /* user r12 */
  175. GET_SCRATCH0(8) /* user r13 */
  176. std r3, GPR1(r7)
  177. std r4, GPR7(r7)
  178. std r5, GPR11(r7)
  179. std r6, GPR12(r7)
  180. std r8, GPR13(r7)
  181. SAVE_NVGPRS(r7) /* user r14-r31 */
  182. /* ******************** NIP ******************** */
  183. mfspr r3, SPRN_TFHAR
  184. std r3, _NIP(r7) /* Returns to failhandler */
  185. /* The checkpointed NIP is ignored when rescheduling/rechkpting,
  186. * but is used in signal return to 'wind back' to the abort handler.
  187. */
  188. /* ******************** CR,LR,CCR,MSR ********** */
  189. mfctr r3
  190. mflr r4
  191. mfcr r5
  192. mfxer r6
  193. std r3, _CTR(r7)
  194. std r4, _LINK(r7)
  195. std r5, _CCR(r7)
  196. std r6, _XER(r7)
  197. /* ******************** TAR, DSCR ********** */
  198. mfspr r3, SPRN_TAR
  199. mfspr r4, SPRN_DSCR
  200. std r3, THREAD_TM_TAR(r12)
  201. std r4, THREAD_TM_DSCR(r12)
  202. /* MSR and flags: We don't change CRs, and we don't need to alter
  203. * MSR.
  204. */
  205. /* ******************** FPR/VR/VSRs ************
  206. * After reclaiming, capture the checkpointed FPRs/VRs.
  207. *
  208. * We enabled VEC/FP/VSX in the msr above, so we can execute these
  209. * instructions!
  210. */
  211. mr r3, r12
  212. /* Altivec (VEC/VMX/VR)*/
  213. addi r7, r3, THREAD_CKVRSTATE
  214. SAVE_32VRS(0, r6, r7) /* r6 scratch, r7 transact vr state */
  215. mfvscr v0
  216. li r6, VRSTATE_VSCR
  217. stvx v0, r7, r6
  218. /* VRSAVE */
  219. mfspr r0, SPRN_VRSAVE
  220. std r0, THREAD_CKVRSAVE(r3)
  221. /* Floating Point (FP) */
  222. addi r7, r3, THREAD_CKFPSTATE
  223. SAVE_32FPRS_VSRS(0, R6, R7) /* r6 scratch, r7 transact fp state */
  224. mffs fr0
  225. stfd fr0,FPSTATE_FPSCR(r7)
  226. /* TM regs, incl TEXASR -- these live in thread_struct. Note they've
  227. * been updated by the treclaim, to explain to userland the failure
  228. * cause (aborted).
  229. */
  230. mfspr r0, SPRN_TEXASR
  231. mfspr r3, SPRN_TFHAR
  232. mfspr r4, SPRN_TFIAR
  233. std r0, THREAD_TM_TEXASR(r12)
  234. std r3, THREAD_TM_TFHAR(r12)
  235. std r4, THREAD_TM_TFIAR(r12)
  236. /* AMR is checkpointed too, but is unsupported by Linux. */
  237. /* Restore original MSR/IRQ state & clear TM mode */
  238. ld r14, TM_FRAME_L0(r1) /* Orig MSR */
  239. li r15, 0
  240. rldimi r14, r15, MSR_TS_LG, (63-MSR_TS_LG)-1
  241. mtmsrd r14
  242. REST_NVGPRS(r1)
  243. addi r1, r1, TM_FRAME_SIZE
  244. lwz r4, 8(r1)
  245. ld r0, 16(r1)
  246. mtcr r4
  247. mtlr r0
  248. ld r2, STK_GOT(r1)
  249. /* Load CPU's default DSCR */
  250. ld r0, PACA_DSCR_DEFAULT(r13)
  251. mtspr SPRN_DSCR, r0
  252. blr
  253. /* void __tm_recheckpoint(struct thread_struct *thread,
  254. * unsigned long orig_msr)
  255. * - Restore the checkpointed register state saved by tm_reclaim
  256. * when we switch_to a process.
  257. *
  258. * Call with IRQs off, stacks get all out of sync for
  259. * some periods in here!
  260. */
  261. _GLOBAL(__tm_recheckpoint)
  262. mfcr r5
  263. mflr r0
  264. stw r5, 8(r1)
  265. std r0, 16(r1)
  266. std r2, STK_GOT(r1)
  267. stdu r1, -TM_FRAME_SIZE(r1)
  268. /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD].
  269. * This is used for backing up the NVGPRs:
  270. */
  271. SAVE_NVGPRS(r1)
  272. /* Load complete register state from ts_ckpt* registers */
  273. addi r7, r3, PT_CKPT_REGS /* Thread's ckpt_regs */
  274. /* Make r7 look like an exception frame so that we
  275. * can use the neat GPRx(n) macros. r7 is now NOT a pt_regs ptr!
  276. */
  277. subi r7, r7, STACK_FRAME_OVERHEAD
  278. /* We need to setup MSR for FP/VMX/VSX register save instructions. */
  279. mfmsr r6
  280. mr r5, r6
  281. ori r5, r5, MSR_FP
  282. #ifdef CONFIG_ALTIVEC
  283. oris r5, r5, MSR_VEC@h
  284. #endif
  285. #ifdef CONFIG_VSX
  286. BEGIN_FTR_SECTION
  287. oris r5,r5, MSR_VSX@h
  288. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  289. #endif
  290. mtmsrd r5
  291. #ifdef CONFIG_ALTIVEC
  292. /*
  293. * FP and VEC registers: These are recheckpointed from
  294. * thread.ckfp_state and thread.ckvr_state respectively. The
  295. * thread.fp_state[] version holds the 'live' (transactional)
  296. * and will be loaded subsequently by any FPUnavailable trap.
  297. */
  298. addi r8, r3, THREAD_CKVRSTATE
  299. li r5, VRSTATE_VSCR
  300. lvx v0, r8, r5
  301. mtvscr v0
  302. REST_32VRS(0, r5, r8) /* r5 scratch, r8 ptr */
  303. ld r5, THREAD_CKVRSAVE(r3)
  304. mtspr SPRN_VRSAVE, r5
  305. #endif
  306. addi r8, r3, THREAD_CKFPSTATE
  307. lfd fr0, FPSTATE_FPSCR(r8)
  308. MTFSF_L(fr0)
  309. REST_32FPRS_VSRS(0, R4, R8)
  310. mtmsr r6 /* FP/Vec off again! */
  311. restore_gprs:
  312. /* ******************** CR,LR,CCR,MSR ********** */
  313. ld r4, _CTR(r7)
  314. ld r5, _LINK(r7)
  315. ld r8, _XER(r7)
  316. mtctr r4
  317. mtlr r5
  318. mtxer r8
  319. /* ******************** TAR ******************** */
  320. ld r4, THREAD_TM_TAR(r3)
  321. mtspr SPRN_TAR, r4
  322. /* Load up the PPR and DSCR in GPRs only at this stage */
  323. ld r5, THREAD_TM_DSCR(r3)
  324. ld r6, THREAD_TM_PPR(r3)
  325. REST_GPR(0, r7) /* GPR0 */
  326. REST_2GPRS(2, r7) /* GPR2-3 */
  327. REST_GPR(4, r7) /* GPR4 */
  328. REST_4GPRS(8, r7) /* GPR8-11 */
  329. REST_2GPRS(12, r7) /* GPR12-13 */
  330. REST_NVGPRS(r7) /* GPR14-31 */
  331. /* Load up PPR and DSCR here so we don't run with user values for long
  332. */
  333. mtspr SPRN_DSCR, r5
  334. mtspr SPRN_PPR, r6
  335. /* Do final sanity check on TEXASR to make sure FS is set. Do this
  336. * here before we load up the userspace r1 so any bugs we hit will get
  337. * a call chain */
  338. mfspr r5, SPRN_TEXASR
  339. srdi r5, r5, 16
  340. li r6, (TEXASR_FS)@h
  341. and r6, r6, r5
  342. 1: tdeqi r6, 0
  343. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  344. /* Do final sanity check on MSR to make sure we are not transactional
  345. * or suspended
  346. */
  347. mfmsr r6
  348. li r5, (MSR_TS_MASK)@higher
  349. srdi r6, r6, 32
  350. and r6, r6, r5
  351. 1: tdnei r6, 0
  352. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  353. /* Restore CR */
  354. ld r6, _CCR(r7)
  355. mtcr r6
  356. REST_GPR(6, r7)
  357. /*
  358. * Store r1 and r5 on the stack so that we can access them
  359. * after we clear MSR RI.
  360. */
  361. REST_GPR(5, r7)
  362. std r5, -8(r1)
  363. ld r5, GPR1(r7)
  364. std r5, -16(r1)
  365. REST_GPR(7, r7)
  366. /* Clear MSR RI since we are about to change r1. EE is already off */
  367. li r5, 0
  368. mtmsrd r5, 1
  369. /*
  370. * BE CAREFUL HERE:
  371. * At this point we can't take an SLB miss since we have MSR_RI
  372. * off. Load only to/from the stack/paca which are in SLB bolted regions
  373. * until we turn MSR RI back on.
  374. */
  375. SET_SCRATCH0(r1)
  376. ld r5, -8(r1)
  377. ld r1, -16(r1)
  378. /* Commit register state as checkpointed state: */
  379. TRECHKPT
  380. HMT_MEDIUM
  381. /* Our transactional state has now changed.
  382. *
  383. * Now just get out of here. Transactional (current) state will be
  384. * updated once restore is called on the return path in the _switch-ed
  385. * -to process.
  386. */
  387. GET_PACA(r13)
  388. GET_SCRATCH0(r1)
  389. /* R1 is restored, so we are recoverable again. EE is still off */
  390. li r4, MSR_RI
  391. mtmsrd r4, 1
  392. REST_NVGPRS(r1)
  393. addi r1, r1, TM_FRAME_SIZE
  394. lwz r4, 8(r1)
  395. ld r0, 16(r1)
  396. mtcr r4
  397. mtlr r0
  398. ld r2, STK_GOT(r1)
  399. /* Load CPU's default DSCR */
  400. ld r0, PACA_DSCR_DEFAULT(r13)
  401. mtspr SPRN_DSCR, r0
  402. blr
  403. /* ****************************************************************** */