tm.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. std r4, STK_PARAM(R4)(r1)
  101. SAVE_NVGPRS(r1)
  102. /* We need to setup MSR for VSX register save instructions. */
  103. mfmsr r14
  104. mr r15, r14
  105. ori r15, r15, MSR_FP
  106. li r16, 0
  107. ori r16, r16, MSR_EE /* IRQs hard off */
  108. andc r15, r15, r16
  109. oris r15, r15, MSR_VEC@h
  110. #ifdef CONFIG_VSX
  111. BEGIN_FTR_SECTION
  112. oris r15,r15, MSR_VSX@h
  113. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  114. #endif
  115. mtmsrd r15
  116. std r14, TM_FRAME_L0(r1)
  117. /* Do sanity check on MSR to make sure we are suspended */
  118. li r7, (MSR_TS_S)@higher
  119. srdi r6, r14, 32
  120. and r6, r6, r7
  121. 1: tdeqi r6, 0
  122. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  123. /* Stash the stack pointer away for use after reclaim */
  124. std r1, PACAR1(r13)
  125. /* Clear MSR RI since we are about to change r1, EE is already off. */
  126. li r4, 0
  127. mtmsrd r4, 1
  128. /*
  129. * BE CAREFUL HERE:
  130. * At this point we can't take an SLB miss since we have MSR_RI
  131. * off. Load only to/from the stack/paca which are in SLB bolted regions
  132. * until we turn MSR RI back on.
  133. *
  134. * The moment we treclaim, ALL of our GPRs will switch
  135. * to user register state. (FPRs, CCR etc. also!)
  136. * Use an sprg and a tm_scratch in the PACA to shuffle.
  137. */
  138. TRECLAIM(R5) /* Cause in r5 */
  139. /* ******************** GPRs ******************** */
  140. /* Stash the checkpointed r13 away in the scratch SPR and get the real
  141. * paca
  142. */
  143. SET_SCRATCH0(r13)
  144. GET_PACA(r13)
  145. /* Stash the checkpointed r1 away in paca tm_scratch and get the real
  146. * stack pointer back
  147. */
  148. std r1, PACATMSCRATCH(r13)
  149. ld r1, PACAR1(r13)
  150. /* Store the PPR in r11 and reset to decent value */
  151. std r11, GPR11(r1) /* Temporary stash */
  152. /* Reset MSR RI so we can take SLB faults again */
  153. li r11, MSR_RI
  154. mtmsrd r11, 1
  155. mfspr r11, SPRN_PPR
  156. HMT_MEDIUM
  157. /* Now get some more GPRS free */
  158. std r7, GPR7(r1) /* Temporary stash */
  159. std r12, GPR12(r1) /* '' '' '' */
  160. ld r12, STK_PARAM(R3)(r1) /* Param 0, thread_struct * */
  161. std r11, THREAD_TM_PPR(r12) /* Store PPR and free r11 */
  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_GPR(8, r7) /* user r8 */
  172. SAVE_GPR(9, r7) /* user r9 */
  173. SAVE_GPR(10, r7) /* user r10 */
  174. ld r3, PACATMSCRATCH(r13) /* user r1 */
  175. ld r4, GPR7(r1) /* user r7 */
  176. ld r5, GPR11(r1) /* user r11 */
  177. ld r6, GPR12(r1) /* user r12 */
  178. GET_SCRATCH0(8) /* user r13 */
  179. std r3, GPR1(r7)
  180. std r4, GPR7(r7)
  181. std r5, GPR11(r7)
  182. std r6, GPR12(r7)
  183. std r8, GPR13(r7)
  184. SAVE_NVGPRS(r7) /* user r14-r31 */
  185. /* ******************** NIP ******************** */
  186. mfspr r3, SPRN_TFHAR
  187. std r3, _NIP(r7) /* Returns to failhandler */
  188. /* The checkpointed NIP is ignored when rescheduling/rechkpting,
  189. * but is used in signal return to 'wind back' to the abort handler.
  190. */
  191. /* ******************** CR,LR,CCR,MSR ********** */
  192. mfctr r3
  193. mflr r4
  194. mfcr r5
  195. mfxer r6
  196. std r3, _CTR(r7)
  197. std r4, _LINK(r7)
  198. std r5, _CCR(r7)
  199. std r6, _XER(r7)
  200. /* ******************** TAR, DSCR ********** */
  201. mfspr r3, SPRN_TAR
  202. mfspr r4, SPRN_DSCR
  203. std r3, THREAD_TM_TAR(r12)
  204. std r4, THREAD_TM_DSCR(r12)
  205. /* MSR and flags: We don't change CRs, and we don't need to alter
  206. * MSR.
  207. */
  208. /* ******************** FPR/VR/VSRs ************
  209. * After reclaiming, capture the checkpointed FPRs/VRs /if used/.
  210. *
  211. * (If VSX used, FP and VMX are implied. Or, we don't need to look
  212. * at MSR.VSX as copying FP regs if .FP, vector regs if .VMX covers it.)
  213. *
  214. * We're passed the thread's MSR as the second parameter
  215. *
  216. * We enabled VEC/FP/VSX in the msr above, so we can execute these
  217. * instructions!
  218. */
  219. ld r4, STK_PARAM(R4)(r1) /* Second parameter, MSR * */
  220. mr r3, r12
  221. andis. r0, r4, MSR_VEC@h
  222. beq dont_backup_vec
  223. addi r7, r3, THREAD_CKVRSTATE
  224. SAVE_32VRS(0, r6, r7) /* r6 scratch, r7 transact vr state */
  225. mfvscr v0
  226. li r6, VRSTATE_VSCR
  227. stvx v0, r7, r6
  228. dont_backup_vec:
  229. mfspr r0, SPRN_VRSAVE
  230. std r0, THREAD_CKVRSAVE(r3)
  231. andi. r0, r4, MSR_FP
  232. beq dont_backup_fp
  233. addi r7, r3, THREAD_CKFPSTATE
  234. SAVE_32FPRS_VSRS(0, R6, R7) /* r6 scratch, r7 transact fp state */
  235. mffs fr0
  236. stfd fr0,FPSTATE_FPSCR(r7)
  237. dont_backup_fp:
  238. /* TM regs, incl TEXASR -- these live in thread_struct. Note they've
  239. * been updated by the treclaim, to explain to userland the failure
  240. * cause (aborted).
  241. */
  242. mfspr r0, SPRN_TEXASR
  243. mfspr r3, SPRN_TFHAR
  244. mfspr r4, SPRN_TFIAR
  245. std r0, THREAD_TM_TEXASR(r12)
  246. std r3, THREAD_TM_TFHAR(r12)
  247. std r4, THREAD_TM_TFIAR(r12)
  248. /* AMR is checkpointed too, but is unsupported by Linux. */
  249. /* Restore original MSR/IRQ state & clear TM mode */
  250. ld r14, TM_FRAME_L0(r1) /* Orig MSR */
  251. li r15, 0
  252. rldimi r14, r15, MSR_TS_LG, (63-MSR_TS_LG)-1
  253. mtmsrd r14
  254. REST_NVGPRS(r1)
  255. addi r1, r1, TM_FRAME_SIZE
  256. lwz r4, 8(r1)
  257. ld r0, 16(r1)
  258. mtcr r4
  259. mtlr r0
  260. ld r2, STK_GOT(r1)
  261. /* Load CPU's default DSCR */
  262. ld r0, PACA_DSCR_DEFAULT(r13)
  263. mtspr SPRN_DSCR, r0
  264. blr
  265. /* void tm_recheckpoint(struct thread_struct *thread,
  266. * unsigned long orig_msr)
  267. * - Restore the checkpointed register state saved by tm_reclaim
  268. * when we switch_to a process.
  269. *
  270. * Call with IRQs off, stacks get all out of sync for
  271. * some periods in here!
  272. */
  273. _GLOBAL(__tm_recheckpoint)
  274. mfcr r5
  275. mflr r0
  276. stw r5, 8(r1)
  277. std r0, 16(r1)
  278. std r2, STK_GOT(r1)
  279. stdu r1, -TM_FRAME_SIZE(r1)
  280. /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD].
  281. * This is used for backing up the NVGPRs:
  282. */
  283. SAVE_NVGPRS(r1)
  284. /* Load complete register state from ts_ckpt* registers */
  285. addi r7, r3, PT_CKPT_REGS /* Thread's ckpt_regs */
  286. /* Make r7 look like an exception frame so that we
  287. * can use the neat GPRx(n) macros. r7 is now NOT a pt_regs ptr!
  288. */
  289. subi r7, r7, STACK_FRAME_OVERHEAD
  290. mfmsr r6
  291. /* R4 = original MSR to indicate whether thread used FP/Vector etc. */
  292. /* Enable FP/vec in MSR if necessary! */
  293. lis r5, MSR_VEC@h
  294. ori r5, r5, MSR_FP
  295. and. r5, r4, r5
  296. beq restore_gprs /* if neither, skip both */
  297. #ifdef CONFIG_VSX
  298. BEGIN_FTR_SECTION
  299. oris r5, r5, MSR_VSX@h
  300. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  301. #endif
  302. or r5, r6, r5 /* Set MSR.FP+.VSX/.VEC */
  303. mtmsr r5
  304. #ifdef CONFIG_ALTIVEC
  305. /*
  306. * FP and VEC registers: These are recheckpointed from
  307. * thread.ckfp_state and thread.ckvr_state respectively. The
  308. * thread.fp_state[] version holds the 'live' (transactional)
  309. * and will be loaded subsequently by any FPUnavailable trap.
  310. */
  311. andis. r0, r4, MSR_VEC@h
  312. beq dont_restore_vec
  313. addi r8, r3, THREAD_CKVRSTATE
  314. li r5, VRSTATE_VSCR
  315. lvx v0, r8, r5
  316. mtvscr v0
  317. REST_32VRS(0, r5, r8) /* r5 scratch, r8 ptr */
  318. dont_restore_vec:
  319. ld r5, THREAD_CKVRSAVE(r3)
  320. mtspr SPRN_VRSAVE, r5
  321. #endif
  322. andi. r0, r4, MSR_FP
  323. beq dont_restore_fp
  324. addi r8, r3, THREAD_CKFPSTATE
  325. lfd fr0, FPSTATE_FPSCR(r8)
  326. MTFSF_L(fr0)
  327. REST_32FPRS_VSRS(0, R4, R8)
  328. dont_restore_fp:
  329. mtmsr r6 /* FP/Vec off again! */
  330. restore_gprs:
  331. /* ******************** CR,LR,CCR,MSR ********** */
  332. ld r4, _CTR(r7)
  333. ld r5, _LINK(r7)
  334. ld r8, _XER(r7)
  335. mtctr r4
  336. mtlr r5
  337. mtxer r8
  338. /* ******************** TAR ******************** */
  339. ld r4, THREAD_TM_TAR(r3)
  340. mtspr SPRN_TAR, r4
  341. /* Load up the PPR and DSCR in GPRs only at this stage */
  342. ld r5, THREAD_TM_DSCR(r3)
  343. ld r6, THREAD_TM_PPR(r3)
  344. REST_GPR(0, r7) /* GPR0 */
  345. REST_2GPRS(2, r7) /* GPR2-3 */
  346. REST_GPR(4, r7) /* GPR4 */
  347. REST_4GPRS(8, r7) /* GPR8-11 */
  348. REST_2GPRS(12, r7) /* GPR12-13 */
  349. REST_NVGPRS(r7) /* GPR14-31 */
  350. /* Load up PPR and DSCR here so we don't run with user values for long
  351. */
  352. mtspr SPRN_DSCR, r5
  353. mtspr SPRN_PPR, r6
  354. /* Do final sanity check on TEXASR to make sure FS is set. Do this
  355. * here before we load up the userspace r1 so any bugs we hit will get
  356. * a call chain */
  357. mfspr r5, SPRN_TEXASR
  358. srdi r5, r5, 16
  359. li r6, (TEXASR_FS)@h
  360. and r6, r6, r5
  361. 1: tdeqi r6, 0
  362. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  363. /* Do final sanity check on MSR to make sure we are not transactional
  364. * or suspended
  365. */
  366. mfmsr r6
  367. li r5, (MSR_TS_MASK)@higher
  368. srdi r6, r6, 32
  369. and r6, r6, r5
  370. 1: tdnei r6, 0
  371. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  372. /* Restore CR */
  373. ld r6, _CCR(r7)
  374. mtcr r6
  375. REST_GPR(6, r7)
  376. /*
  377. * Store r1 and r5 on the stack so that we can access them
  378. * after we clear MSR RI.
  379. */
  380. REST_GPR(5, r7)
  381. std r5, -8(r1)
  382. ld r5, GPR1(r7)
  383. std r5, -16(r1)
  384. REST_GPR(7, r7)
  385. /* Clear MSR RI since we are about to change r1. EE is already off */
  386. li r5, 0
  387. mtmsrd r5, 1
  388. /*
  389. * BE CAREFUL HERE:
  390. * At this point we can't take an SLB miss since we have MSR_RI
  391. * off. Load only to/from the stack/paca which are in SLB bolted regions
  392. * until we turn MSR RI back on.
  393. */
  394. SET_SCRATCH0(r1)
  395. ld r5, -8(r1)
  396. ld r1, -16(r1)
  397. /* Commit register state as checkpointed state: */
  398. TRECHKPT
  399. HMT_MEDIUM
  400. /* Our transactional state has now changed.
  401. *
  402. * Now just get out of here. Transactional (current) state will be
  403. * updated once restore is called on the return path in the _switch-ed
  404. * -to process.
  405. */
  406. GET_PACA(r13)
  407. GET_SCRATCH0(r1)
  408. /* R1 is restored, so we are recoverable again. EE is still off */
  409. li r4, MSR_RI
  410. mtmsrd r4, 1
  411. REST_NVGPRS(r1)
  412. addi r1, r1, TM_FRAME_SIZE
  413. lwz r4, 8(r1)
  414. ld r0, 16(r1)
  415. mtcr r4
  416. mtlr r0
  417. ld r2, STK_GOT(r1)
  418. /* Load CPU's default DSCR */
  419. ld r0, PACA_DSCR_DEFAULT(r13)
  420. mtspr SPRN_DSCR, r0
  421. blr
  422. /* ****************************************************************** */