tm.S 12 KB

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