tm.S 11 KB

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