tm.S 12 KB

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