tm.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. /* We need to setup MSR for VSX register save instructions. Here we
  103. * also clear the MSR RI since when we do the treclaim, we won't have a
  104. * valid kernel pointer for a while. We clear RI here as it avoids
  105. * adding another mtmsr closer to the treclaim. This makes the region
  106. * maked as non-recoverable wider than it needs to be but it saves on
  107. * inserting another mtmsrd later.
  108. */
  109. mfmsr r14
  110. mr r15, r14
  111. ori r15, r15, MSR_FP
  112. li r16, MSR_RI
  113. andc r15, r15, r16
  114. oris r15, r15, MSR_VEC@h
  115. #ifdef CONFIG_VSX
  116. BEGIN_FTR_SECTION
  117. oris r15,r15, MSR_VSX@h
  118. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  119. #endif
  120. mtmsrd r15
  121. std r14, TM_FRAME_L0(r1)
  122. /* Stash the stack pointer away for use after reclaim */
  123. std r1, PACAR1(r13)
  124. /* ******************** FPR/VR/VSRs ************
  125. * Before reclaiming, capture the current/transactional FPR/VR
  126. * versions /if used/.
  127. *
  128. * (If VSX used, FP and VMX are implied. Or, we don't need to look
  129. * at MSR.VSX as copying FP regs if .FP, vector regs if .VMX covers it.)
  130. *
  131. * We're passed the thread's MSR as parameter 2.
  132. *
  133. * We enabled VEC/FP/VSX in the msr above, so we can execute these
  134. * instructions!
  135. */
  136. andis. r0, r4, MSR_VEC@h
  137. beq dont_backup_vec
  138. SAVE_32VRS_TRANSACT(0, r6, r3) /* r6 scratch, r3 thread */
  139. mfvscr vr0
  140. li r6, THREAD_TRANSACT_VSCR
  141. stvx vr0, r3, r6
  142. mfspr r0, SPRN_VRSAVE
  143. std r0, THREAD_TRANSACT_VRSAVE(r3)
  144. dont_backup_vec:
  145. andi. r0, r4, MSR_FP
  146. beq dont_backup_fp
  147. SAVE_32FPRS_VSRS_TRANSACT(0, R6, R3) /* r6 scratch, r3 thread */
  148. mffs fr0
  149. stfd fr0,THREAD_TRANSACT_FPSCR(r3)
  150. dont_backup_fp:
  151. /* The moment we treclaim, ALL of our GPRs will switch
  152. * to user register state. (FPRs, CCR etc. also!)
  153. * Use an sprg and a tm_scratch in the PACA to shuffle.
  154. */
  155. TRECLAIM(R5) /* Cause in r5 */
  156. /* ******************** GPRs ******************** */
  157. /* Stash the checkpointed r13 away in the scratch SPR and get the real
  158. * paca
  159. */
  160. SET_SCRATCH0(r13)
  161. GET_PACA(r13)
  162. /* Stash the checkpointed r1 away in paca tm_scratch and get the real
  163. * stack pointer back
  164. */
  165. std r1, PACATMSCRATCH(r13)
  166. ld r1, PACAR1(r13)
  167. /* Now get some more GPRS free */
  168. std r7, GPR7(r1) /* Temporary stash */
  169. std r12, GPR12(r1) /* '' '' '' */
  170. ld r12, STACK_PARAM(0)(r1) /* Param 0, thread_struct * */
  171. addi r7, r12, PT_CKPT_REGS /* Thread's ckpt_regs */
  172. /* Make r7 look like an exception frame so that we
  173. * can use the neat GPRx(n) macros. r7 is NOT a pt_regs ptr!
  174. */
  175. subi r7, r7, STACK_FRAME_OVERHEAD
  176. /* Sync the userland GPRs 2-12, 14-31 to thread->regs: */
  177. SAVE_GPR(0, r7) /* user r0 */
  178. SAVE_GPR(2, r7) /* user r2 */
  179. SAVE_4GPRS(3, r7) /* user r3-r6 */
  180. SAVE_4GPRS(8, r7) /* user r8-r11 */
  181. ld r3, PACATMSCRATCH(r13) /* user r1 */
  182. ld r4, GPR7(r1) /* user r7 */
  183. ld r5, GPR12(r1) /* user r12 */
  184. GET_SCRATCH0(6) /* user r13 */
  185. std r3, GPR1(r7)
  186. std r4, GPR7(r7)
  187. std r5, GPR12(r7)
  188. std r6, GPR13(r7)
  189. SAVE_NVGPRS(r7) /* user r14-r31 */
  190. /* ******************** NIP ******************** */
  191. mfspr r3, SPRN_TFHAR
  192. std r3, _NIP(r7) /* Returns to failhandler */
  193. /* The checkpointed NIP is ignored when rescheduling/rechkpting,
  194. * but is used in signal return to 'wind back' to the abort handler.
  195. */
  196. /* ******************** CR,LR,CCR,MSR ********** */
  197. mfctr r3
  198. mflr r4
  199. mfcr r5
  200. mfxer r6
  201. std r3, _CTR(r7)
  202. std r4, _LINK(r7)
  203. std r5, _CCR(r7)
  204. std r6, _XER(r7)
  205. /* MSR and flags: We don't change CRs, and we don't need to alter
  206. * MSR.
  207. */
  208. /* TM regs, incl TEXASR -- these live in thread_struct. Note they've
  209. * been updated by the treclaim, to explain to userland the failure
  210. * cause (aborted).
  211. */
  212. mfspr r0, SPRN_TEXASR
  213. mfspr r3, SPRN_TFHAR
  214. mfspr r4, SPRN_TFIAR
  215. std r0, THREAD_TM_TEXASR(r12)
  216. std r3, THREAD_TM_TFHAR(r12)
  217. std r4, THREAD_TM_TFIAR(r12)
  218. /* AMR and PPR are checkpointed too, but are unsupported by Linux. */
  219. /* Restore original MSR/IRQ state & clear TM mode */
  220. ld r14, TM_FRAME_L0(r1) /* Orig MSR */
  221. li r15, 0
  222. rldimi r14, r15, MSR_TS_LG, (63-MSR_TS_LG)-1
  223. mtmsrd r14
  224. REST_NVGPRS(r1)
  225. addi r1, r1, TM_FRAME_SIZE
  226. ld r4, 8(r1)
  227. ld r0, 16(r1)
  228. mtcr r4
  229. mtlr r0
  230. ld r2, 40(r1)
  231. blr
  232. /* void tm_recheckpoint(struct thread_struct *thread,
  233. * unsigned long orig_msr)
  234. * - Restore the checkpointed register state saved by tm_reclaim
  235. * when we switch_to a process.
  236. *
  237. * Call with IRQs off, stacks get all out of sync for
  238. * some periods in here!
  239. */
  240. _GLOBAL(tm_recheckpoint)
  241. mfcr r5
  242. mflr r0
  243. std r5, 8(r1)
  244. std r0, 16(r1)
  245. std r2, 40(r1)
  246. stdu r1, -TM_FRAME_SIZE(r1)
  247. /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD].
  248. * This is used for backing up the NVGPRs:
  249. */
  250. SAVE_NVGPRS(r1)
  251. std r1, PACAR1(r13)
  252. /* Load complete register state from ts_ckpt* registers */
  253. addi r7, r3, PT_CKPT_REGS /* Thread's ckpt_regs */
  254. /* Make r7 look like an exception frame so that we
  255. * can use the neat GPRx(n) macros. r7 is now NOT a pt_regs ptr!
  256. */
  257. subi r7, r7, STACK_FRAME_OVERHEAD
  258. SET_SCRATCH0(r1)
  259. mfmsr r6
  260. /* R4 = original MSR to indicate whether thread used FP/Vector etc. */
  261. /* Enable FP/vec in MSR if necessary! */
  262. lis r5, MSR_VEC@h
  263. ori r5, r5, MSR_FP
  264. and. r5, r4, r5
  265. beq restore_gprs /* if neither, skip both */
  266. #ifdef CONFIG_VSX
  267. BEGIN_FTR_SECTION
  268. oris r5, r5, MSR_VSX@h
  269. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  270. #endif
  271. or r5, r6, r5 /* Set MSR.FP+.VSX/.VEC */
  272. mtmsr r5
  273. #ifdef CONFIG_ALTIVEC
  274. /* FP and VEC registers: These are recheckpointed from thread.fpr[]
  275. * and thread.vr[] respectively. The thread.transact_fpr[] version
  276. * is more modern, and will be loaded subsequently by any FPUnavailable
  277. * trap.
  278. */
  279. andis. r0, r4, MSR_VEC@h
  280. beq dont_restore_vec
  281. li r5, THREAD_VSCR
  282. lvx vr0, r3, r5
  283. mtvscr vr0
  284. REST_32VRS(0, r5, r3) /* r5 scratch, r3 THREAD ptr */
  285. ld r5, THREAD_VRSAVE(r3)
  286. mtspr SPRN_VRSAVE, r5
  287. #endif
  288. dont_restore_vec:
  289. andi. r0, r4, MSR_FP
  290. beq dont_restore_fp
  291. lfd fr0, THREAD_FPSCR(r3)
  292. MTFSF_L(fr0)
  293. REST_32FPRS_VSRS(0, R4, R3)
  294. dont_restore_fp:
  295. mtmsr r6 /* FP/Vec off again! */
  296. restore_gprs:
  297. /* ******************** CR,LR,CCR,MSR ********** */
  298. ld r3, _CTR(r7)
  299. ld r4, _LINK(r7)
  300. ld r5, _CCR(r7)
  301. ld r6, _XER(r7)
  302. mtctr r3
  303. mtlr r4
  304. mtcr r5
  305. mtxer r6
  306. /* Clear the MSR RI since we are about to change R1. EE is already off
  307. */
  308. li r4, 0
  309. mtmsrd r4, 1
  310. REST_4GPRS(0, r7) /* GPR0-3 */
  311. REST_GPR(4, r7) /* GPR4-6 */
  312. REST_GPR(5, r7)
  313. REST_GPR(6, r7)
  314. REST_4GPRS(8, r7) /* GPR8-11 */
  315. REST_2GPRS(12, r7) /* GPR12-13 */
  316. REST_NVGPRS(r7) /* GPR14-31 */
  317. ld r7, GPR7(r7) /* GPR7 */
  318. /* Commit register state as checkpointed state: */
  319. TRECHKPT
  320. /* Our transactional state has now changed.
  321. *
  322. * Now just get out of here. Transactional (current) state will be
  323. * updated once restore is called on the return path in the _switch-ed
  324. * -to process.
  325. */
  326. GET_PACA(r13)
  327. GET_SCRATCH0(r1)
  328. /* R1 is restored, so we are recoverable again. EE is still off */
  329. li r4, MSR_RI
  330. mtmsrd r4, 1
  331. REST_NVGPRS(r1)
  332. addi r1, r1, TM_FRAME_SIZE
  333. ld r4, 8(r1)
  334. ld r0, 16(r1)
  335. mtcr r4
  336. mtlr r0
  337. ld r2, 40(r1)
  338. blr
  339. /* ****************************************************************** */