tm.S 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Transactional memory support routines to reclaim and recheckpoint
  3. * transactional process state.
  4. *
  5. * Copyright 2012 Matt Evans & Michael Neuling, IBM Corporation.
  6. */
  7. #include <asm/asm-offsets.h>
  8. #include <asm/ppc_asm.h>
  9. #include <asm/ppc-opcode.h>
  10. #include <asm/ptrace.h>
  11. #include <asm/reg.h>
  12. #ifdef CONFIG_VSX
  13. /* See fpu.S, this is very similar but to save/restore checkpointed FPRs/VSRs */
  14. #define __SAVE_32FPRS_VSRS_TRANSACT(n,c,base) \
  15. BEGIN_FTR_SECTION \
  16. b 2f; \
  17. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  18. SAVE_32FPRS_TRANSACT(n,base); \
  19. b 3f; \
  20. 2: SAVE_32VSRS_TRANSACT(n,c,base); \
  21. 3:
  22. /* ...and this is just plain borrowed from there. */
  23. #define __REST_32FPRS_VSRS(n,c,base) \
  24. BEGIN_FTR_SECTION \
  25. b 2f; \
  26. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  27. REST_32FPRS(n,base); \
  28. b 3f; \
  29. 2: REST_32VSRS(n,c,base); \
  30. 3:
  31. #else
  32. #define __SAVE_32FPRS_VSRS_TRANSACT(n,c,base) SAVE_32FPRS_TRANSACT(n, base)
  33. #define __REST_32FPRS_VSRS(n,c,base) REST_32FPRS(n, base)
  34. #endif
  35. #define SAVE_32FPRS_VSRS_TRANSACT(n,c,base) \
  36. __SAVE_32FPRS_VSRS_TRANSACT(n,__REG_##c,__REG_##base)
  37. #define REST_32FPRS_VSRS(n,c,base) \
  38. __REST_32FPRS_VSRS(n,__REG_##c,__REG_##base)
  39. /* Stack frame offsets for local variables. */
  40. #define TM_FRAME_L0 TM_FRAME_SIZE-16
  41. #define TM_FRAME_L1 TM_FRAME_SIZE-8
  42. #define STACK_PARAM(x) (48+((x)*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. * unsigned long orig_msr,
  75. * uint8_t cause)
  76. *
  77. * - Performs a full reclaim. This destroys outstanding
  78. * transactions and updates thread->regs.tm_ckpt_* with the
  79. * original checkpointed state. Note that thread->regs is
  80. * unchanged.
  81. * - FP regs are written back to thread->transact_fpr before
  82. * reclaiming. These are the transactional (current) versions.
  83. *
  84. * Purpose is to both abort transactions of, and preserve the state of,
  85. * a transactions at a context switch. We preserve/restore both sets of process
  86. * state to restore them when the thread's scheduled again. We continue in
  87. * userland as though nothing happened, but when the transaction is resumed
  88. * they will abort back to the checkpointed state we save out here.
  89. *
  90. * Call with IRQs off, stacks get all out of sync for some periods in here!
  91. */
  92. _GLOBAL(tm_reclaim)
  93. mfcr r6
  94. mflr r0
  95. std r6, 8(r1)
  96. std r0, 16(r1)
  97. std r2, 40(r1)
  98. stdu r1, -TM_FRAME_SIZE(r1)
  99. /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD]. */
  100. std r3, STACK_PARAM(0)(r1)
  101. SAVE_NVGPRS(r1)
  102. mfmsr r14
  103. mr r15, r14
  104. ori r15, r15, MSR_FP
  105. oris r15, r15, MSR_VEC@h
  106. #ifdef CONFIG_VSX
  107. BEGIN_FTR_SECTION
  108. oris r15,r15, MSR_VSX@h
  109. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  110. #endif
  111. mtmsrd r15
  112. std r14, TM_FRAME_L0(r1)
  113. /* Stash the stack pointer away for use after reclaim */
  114. std r1, PACAR1(r13)
  115. /* ******************** FPR/VR/VSRs ************
  116. * Before reclaiming, capture the current/transactional FPR/VR
  117. * versions /if used/.
  118. *
  119. * (If VSX used, FP and VMX are implied. Or, we don't need to look
  120. * at MSR.VSX as copying FP regs if .FP, vector regs if .VMX covers it.)
  121. *
  122. * We're passed the thread's MSR as parameter 2.
  123. *
  124. * We enabled VEC/FP/VSX in the msr above, so we can execute these
  125. * instructions!
  126. */
  127. andis. r0, r4, MSR_VEC@h
  128. beq dont_backup_vec
  129. SAVE_32VRS_TRANSACT(0, r6, r3) /* r6 scratch, r3 thread */
  130. mfvscr vr0
  131. li r6, THREAD_TRANSACT_VSCR
  132. stvx vr0, r3, r6
  133. mfspr r0, SPRN_VRSAVE
  134. std r0, THREAD_TRANSACT_VRSAVE(r3)
  135. dont_backup_vec:
  136. andi. r0, r4, MSR_FP
  137. beq dont_backup_fp
  138. SAVE_32FPRS_VSRS_TRANSACT(0, R6, R3) /* r6 scratch, r3 thread */
  139. mffs fr0
  140. stfd fr0,THREAD_TRANSACT_FPSCR(r3)
  141. dont_backup_fp:
  142. /* The moment we treclaim, ALL of our GPRs will switch
  143. * to user register state. (FPRs, CCR etc. also!)
  144. * Use an sprg and a tm_scratch in the PACA to shuffle.
  145. */
  146. TRECLAIM(R5) /* Cause in r5 */
  147. /* ******************** GPRs ******************** */
  148. /* Stash the checkpointed r13 away in the scratch SPR and get the real
  149. * paca
  150. */
  151. SET_SCRATCH0(r13)
  152. GET_PACA(r13)
  153. /* Stash the checkpointed r1 away in paca tm_scratch and get the real
  154. * stack pointer back
  155. */
  156. std r1, PACATMSCRATCH(r13)
  157. ld r1, PACAR1(r13)
  158. /* Now get some more GPRS free */
  159. std r7, GPR7(r1) /* Temporary stash */
  160. std r12, GPR12(r1) /* '' '' '' */
  161. ld r12, STACK_PARAM(0)(r1) /* Param 0, thread_struct * */
  162. addi r7, r12, PT_CKPT_REGS /* Thread's ckpt_regs */
  163. /* Make r7 look like an exception frame so that we
  164. * can use the neat GPRx(n) macros. r7 is NOT a pt_regs ptr!
  165. */
  166. subi r7, r7, STACK_FRAME_OVERHEAD
  167. /* Sync the userland GPRs 2-12, 14-31 to thread->regs: */
  168. SAVE_GPR(0, r7) /* user r0 */
  169. SAVE_GPR(2, r7) /* user r2 */
  170. SAVE_4GPRS(3, r7) /* user r3-r6 */
  171. SAVE_4GPRS(8, r7) /* user r8-r11 */
  172. ld r3, PACATMSCRATCH(r13) /* user r1 */
  173. ld r4, GPR7(r1) /* user r7 */
  174. ld r5, GPR12(r1) /* user r12 */
  175. GET_SCRATCH0(6) /* user r13 */
  176. std r3, GPR1(r7)
  177. std r4, GPR7(r7)
  178. std r5, GPR12(r7)
  179. std r6, GPR13(r7)
  180. SAVE_NVGPRS(r7) /* user r14-r31 */
  181. /* ******************** NIP ******************** */
  182. mfspr r3, SPRN_TFHAR
  183. std r3, _NIP(r7) /* Returns to failhandler */
  184. /* The checkpointed NIP is ignored when rescheduling/rechkpting,
  185. * but is used in signal return to 'wind back' to the abort handler.
  186. */
  187. /* ******************** CR,LR,CCR,MSR ********** */
  188. mfctr r3
  189. mflr r4
  190. mfcr r5
  191. mfxer r6
  192. std r3, _CTR(r7)
  193. std r4, _LINK(r7)
  194. std r5, _CCR(r7)
  195. std r6, _XER(r7)
  196. /* MSR and flags: We don't change CRs, and we don't need to alter
  197. * MSR.
  198. */
  199. /* TM regs, incl TEXASR -- these live in thread_struct. Note they've
  200. * been updated by the treclaim, to explain to userland the failure
  201. * cause (aborted).
  202. */
  203. mfspr r0, SPRN_TEXASR
  204. mfspr r3, SPRN_TFHAR
  205. mfspr r4, SPRN_TFIAR
  206. std r0, THREAD_TM_TEXASR(r12)
  207. std r3, THREAD_TM_TFHAR(r12)
  208. std r4, THREAD_TM_TFIAR(r12)
  209. /* AMR and PPR are checkpointed too, but are unsupported by Linux. */
  210. /* Restore original MSR/IRQ state & clear TM mode */
  211. ld r14, TM_FRAME_L0(r1) /* Orig MSR */
  212. li r15, 0
  213. rldimi r14, r15, MSR_TS_LG, (63-MSR_TS_LG)-1
  214. mtmsrd r14
  215. REST_NVGPRS(r1)
  216. addi r1, r1, TM_FRAME_SIZE
  217. ld r4, 8(r1)
  218. ld r0, 16(r1)
  219. mtcr r4
  220. mtlr r0
  221. ld r2, 40(r1)
  222. blr
  223. /* void tm_recheckpoint(struct thread_struct *thread,
  224. * unsigned long orig_msr)
  225. * - Restore the checkpointed register state saved by tm_reclaim
  226. * when we switch_to a process.
  227. *
  228. * Call with IRQs off, stacks get all out of sync for
  229. * some periods in here!
  230. */
  231. _GLOBAL(tm_recheckpoint)
  232. mfcr r5
  233. mflr r0
  234. std r5, 8(r1)
  235. std r0, 16(r1)
  236. std r2, 40(r1)
  237. stdu r1, -TM_FRAME_SIZE(r1)
  238. /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD].
  239. * This is used for backing up the NVGPRs:
  240. */
  241. SAVE_NVGPRS(r1)
  242. std r1, PACAR1(r13)
  243. /* Load complete register state from ts_ckpt* registers */
  244. addi r7, r3, PT_CKPT_REGS /* Thread's ckpt_regs */
  245. /* Make r7 look like an exception frame so that we
  246. * can use the neat GPRx(n) macros. r7 is now NOT a pt_regs ptr!
  247. */
  248. subi r7, r7, STACK_FRAME_OVERHEAD
  249. SET_SCRATCH0(r1)
  250. mfmsr r6
  251. /* R4 = original MSR to indicate whether thread used FP/Vector etc. */
  252. /* Enable FP/vec in MSR if necessary! */
  253. lis r5, MSR_VEC@h
  254. ori r5, r5, MSR_FP
  255. and. r5, r4, r5
  256. beq restore_gprs /* if neither, skip both */
  257. #ifdef CONFIG_VSX
  258. BEGIN_FTR_SECTION
  259. oris r5, r5, MSR_VSX@h
  260. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  261. #endif
  262. or r5, r6, r5 /* Set MSR.FP+.VSX/.VEC */
  263. mtmsr r5
  264. /* FP and VEC registers: These are recheckpointed from thread.fpr[]
  265. * and thread.vr[] respectively. The thread.transact_fpr[] version
  266. * is more modern, and will be loaded subsequently by any FPUnavailable
  267. * trap.
  268. */
  269. andis. r0, r4, MSR_VEC@h
  270. beq dont_restore_vec
  271. li r5, THREAD_VSCR
  272. lvx vr0, r3, r5
  273. mtvscr vr0
  274. REST_32VRS(0, r5, r3) /* r5 scratch, r3 THREAD ptr */
  275. ld r5, THREAD_VRSAVE(r3)
  276. mtspr SPRN_VRSAVE, r5
  277. dont_restore_vec:
  278. andi. r0, r4, MSR_FP
  279. beq dont_restore_fp
  280. lfd fr0, THREAD_FPSCR(r3)
  281. MTFSF_L(fr0)
  282. REST_32FPRS_VSRS(0, R4, R3)
  283. dont_restore_fp:
  284. mtmsr r6 /* FP/Vec off again! */
  285. restore_gprs:
  286. /* ******************** CR,LR,CCR,MSR ********** */
  287. ld r3, _CTR(r7)
  288. ld r4, _LINK(r7)
  289. ld r5, _CCR(r7)
  290. ld r6, _XER(r7)
  291. mtctr r3
  292. mtlr r4
  293. mtcr r5
  294. mtxer r6
  295. /* MSR and flags: We don't change CRs, and we don't need to alter
  296. * MSR.
  297. */
  298. REST_4GPRS(0, r7) /* GPR0-3 */
  299. REST_GPR(4, r7) /* GPR4-6 */
  300. REST_GPR(5, r7)
  301. REST_GPR(6, r7)
  302. REST_4GPRS(8, r7) /* GPR8-11 */
  303. REST_2GPRS(12, r7) /* GPR12-13 */
  304. REST_NVGPRS(r7) /* GPR14-31 */
  305. ld r7, GPR7(r7) /* GPR7 */
  306. /* Commit register state as checkpointed state: */
  307. TRECHKPT
  308. /* Our transactional state has now changed.
  309. *
  310. * Now just get out of here. Transactional (current) state will be
  311. * updated once restore is called on the return path in the _switch-ed
  312. * -to process.
  313. */
  314. GET_PACA(r13)
  315. GET_SCRATCH0(r1)
  316. REST_NVGPRS(r1)
  317. addi r1, r1, TM_FRAME_SIZE
  318. ld r4, 8(r1)
  319. ld r0, 16(r1)
  320. mtcr r4
  321. mtlr r0
  322. ld r2, 40(r1)
  323. blr
  324. /* ****************************************************************** */