tm.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Transactional memory support routines to reclaim and recheckpoint
  4. * transactional process state.
  5. *
  6. * Copyright 2012 Matt Evans & Michael Neuling, IBM Corporation.
  7. */
  8. #include <asm/asm-offsets.h>
  9. #include <asm/ppc_asm.h>
  10. #include <asm/ppc-opcode.h>
  11. #include <asm/ptrace.h>
  12. #include <asm/reg.h>
  13. #include <asm/bug.h>
  14. #include <asm/export.h>
  15. #include <asm/feature-fixups.h>
  16. #ifdef CONFIG_VSX
  17. /* See fpu.S, this is borrowed from there */
  18. #define __SAVE_32FPRS_VSRS(n,c,base) \
  19. BEGIN_FTR_SECTION \
  20. b 2f; \
  21. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  22. SAVE_32FPRS(n,base); \
  23. b 3f; \
  24. 2: SAVE_32VSRS(n,c,base); \
  25. 3:
  26. #define __REST_32FPRS_VSRS(n,c,base) \
  27. BEGIN_FTR_SECTION \
  28. b 2f; \
  29. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  30. REST_32FPRS(n,base); \
  31. b 3f; \
  32. 2: REST_32VSRS(n,c,base); \
  33. 3:
  34. #else
  35. #define __SAVE_32FPRS_VSRS(n,c,base) SAVE_32FPRS(n, base)
  36. #define __REST_32FPRS_VSRS(n,c,base) REST_32FPRS(n, base)
  37. #endif
  38. #define SAVE_32FPRS_VSRS(n,c,base) \
  39. __SAVE_32FPRS_VSRS(n,__REG_##c,__REG_##base)
  40. #define REST_32FPRS_VSRS(n,c,base) \
  41. __REST_32FPRS_VSRS(n,__REG_##c,__REG_##base)
  42. /* Stack frame offsets for local variables. */
  43. #define TM_FRAME_L0 TM_FRAME_SIZE-16
  44. #define TM_FRAME_L1 TM_FRAME_SIZE-8
  45. /* In order to access the TM SPRs, TM must be enabled. So, do so: */
  46. _GLOBAL(tm_enable)
  47. mfmsr r4
  48. li r3, MSR_TM >> 32
  49. sldi r3, r3, 32
  50. and. r0, r4, r3
  51. bne 1f
  52. or r4, r4, r3
  53. mtmsrd r4
  54. 1: blr
  55. EXPORT_SYMBOL_GPL(tm_enable);
  56. _GLOBAL(tm_disable)
  57. mfmsr r4
  58. li r3, MSR_TM >> 32
  59. sldi r3, r3, 32
  60. andc r4, r4, r3
  61. mtmsrd r4
  62. blr
  63. EXPORT_SYMBOL_GPL(tm_disable);
  64. _GLOBAL(tm_save_sprs)
  65. mfspr r0, SPRN_TFHAR
  66. std r0, THREAD_TM_TFHAR(r3)
  67. mfspr r0, SPRN_TEXASR
  68. std r0, THREAD_TM_TEXASR(r3)
  69. mfspr r0, SPRN_TFIAR
  70. std r0, THREAD_TM_TFIAR(r3)
  71. blr
  72. _GLOBAL(tm_restore_sprs)
  73. ld r0, THREAD_TM_TFHAR(r3)
  74. mtspr SPRN_TFHAR, r0
  75. ld r0, THREAD_TM_TEXASR(r3)
  76. mtspr SPRN_TEXASR, r0
  77. ld r0, THREAD_TM_TFIAR(r3)
  78. mtspr SPRN_TFIAR, r0
  79. blr
  80. /* Passed an 8-bit failure cause as first argument. */
  81. _GLOBAL(tm_abort)
  82. TABORT(R3)
  83. blr
  84. EXPORT_SYMBOL_GPL(tm_abort);
  85. /*
  86. * void tm_reclaim(struct thread_struct *thread,
  87. * uint8_t cause)
  88. *
  89. * - Performs a full reclaim. This destroys outstanding
  90. * transactions and updates thread.ckpt_regs, thread.ckfp_state and
  91. * thread.ckvr_state with the original checkpointed state. Note that
  92. * thread->regs is unchanged.
  93. *
  94. * Purpose is to both abort transactions of, and preserve the state of,
  95. * a transactions at a context switch. We preserve/restore both sets of process
  96. * state to restore them when the thread's scheduled again. We continue in
  97. * userland as though nothing happened, but when the transaction is resumed
  98. * they will abort back to the checkpointed state we save out here.
  99. *
  100. * Call with IRQs off, stacks get all out of sync for some periods in here!
  101. */
  102. _GLOBAL(tm_reclaim)
  103. mfcr r5
  104. mflr r0
  105. stw r5, 8(r1)
  106. std r0, 16(r1)
  107. std r2, STK_GOT(r1)
  108. stdu r1, -TM_FRAME_SIZE(r1)
  109. /* We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD]. */
  110. std r3, STK_PARAM(R3)(r1)
  111. SAVE_NVGPRS(r1)
  112. /* We need to setup MSR for VSX register save instructions. */
  113. mfmsr r14
  114. mr r15, r14
  115. ori r15, r15, MSR_FP
  116. li r16, 0
  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. /* Do sanity check on MSR to make sure we are suspended */
  128. li r7, (MSR_TS_S)@higher
  129. srdi r6, r14, 32
  130. and r6, r6, r7
  131. 1: tdeqi r6, 0
  132. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  133. /* Stash the stack pointer away for use after reclaim */
  134. std r1, PACAR1(r13)
  135. /* Clear MSR RI since we are about to change r1, EE is already off. */
  136. li r5, 0
  137. mtmsrd r5, 1
  138. /*
  139. * BE CAREFUL HERE:
  140. * At this point we can't take an SLB miss since we have MSR_RI
  141. * off. Load only to/from the stack/paca which are in SLB bolted regions
  142. * until we turn MSR RI back on.
  143. *
  144. * The moment we treclaim, ALL of our GPRs will switch
  145. * to user register state. (FPRs, CCR etc. also!)
  146. * Use an sprg and a tm_scratch in the PACA to shuffle.
  147. */
  148. TRECLAIM(R4) /* Cause in r4 */
  149. /*
  150. * ******************** GPRs ********************
  151. * Stash the checkpointed r13 in the scratch SPR and get the real paca.
  152. */
  153. SET_SCRATCH0(r13)
  154. GET_PACA(r13)
  155. /*
  156. * Stash the checkpointed r1 away in paca->tm_scratch and get the real
  157. * stack pointer back into r1.
  158. */
  159. std r1, PACATMSCRATCH(r13)
  160. ld r1, PACAR1(r13)
  161. std r11, GPR11(r1) /* Temporary stash */
  162. /*
  163. * Move the saved user r1 to the kernel stack in case PACATMSCRATCH is
  164. * clobbered by an exception once we turn on MSR_RI below.
  165. */
  166. ld r11, PACATMSCRATCH(r13)
  167. std r11, GPR1(r1)
  168. /*
  169. * Store r13 away so we can free up the scratch SPR for the SLB fault
  170. * handler (needed once we start accessing the thread_struct).
  171. */
  172. GET_SCRATCH0(r11)
  173. std r11, GPR13(r1)
  174. /* Reset MSR RI so we can take SLB faults again */
  175. li r11, MSR_RI
  176. mtmsrd r11, 1
  177. /* Store the PPR in r11 and reset to decent value */
  178. mfspr r11, SPRN_PPR
  179. HMT_MEDIUM
  180. /* Now get some more GPRS free */
  181. std r7, GPR7(r1) /* Temporary stash */
  182. std r12, GPR12(r1) /* '' '' '' */
  183. ld r12, STK_PARAM(R3)(r1) /* Param 0, thread_struct * */
  184. std r11, THREAD_TM_PPR(r12) /* Store PPR and free r11 */
  185. addi r7, r12, PT_CKPT_REGS /* Thread's ckpt_regs */
  186. /*
  187. * Make r7 look like an exception frame so that we can use the neat
  188. * GPRx(n) macros. r7 is NOT a pt_regs ptr!
  189. */
  190. subi r7, r7, STACK_FRAME_OVERHEAD
  191. /* Sync the userland GPRs 2-12, 14-31 to thread->regs: */
  192. SAVE_GPR(0, r7) /* user r0 */
  193. SAVE_GPR(2, r7) /* user r2 */
  194. SAVE_4GPRS(3, r7) /* user r3-r6 */
  195. SAVE_GPR(8, r7) /* user r8 */
  196. SAVE_GPR(9, r7) /* user r9 */
  197. SAVE_GPR(10, r7) /* user r10 */
  198. ld r3, GPR1(r1) /* user r1 */
  199. ld r4, GPR7(r1) /* user r7 */
  200. ld r5, GPR11(r1) /* user r11 */
  201. ld r6, GPR12(r1) /* user r12 */
  202. ld r8, GPR13(r1) /* user r13 */
  203. std r3, GPR1(r7)
  204. std r4, GPR7(r7)
  205. std r5, GPR11(r7)
  206. std r6, GPR12(r7)
  207. std r8, GPR13(r7)
  208. SAVE_NVGPRS(r7) /* user r14-r31 */
  209. /* ******************** NIP ******************** */
  210. mfspr r3, SPRN_TFHAR
  211. std r3, _NIP(r7) /* Returns to failhandler */
  212. /*
  213. * The checkpointed NIP is ignored when rescheduling/rechkpting,
  214. * but is used in signal return to 'wind back' to the abort handler.
  215. */
  216. /* ******************** CR,LR,CCR,MSR ********** */
  217. mfctr r3
  218. mflr r4
  219. mfcr r5
  220. mfxer r6
  221. std r3, _CTR(r7)
  222. std r4, _LINK(r7)
  223. std r5, _CCR(r7)
  224. std r6, _XER(r7)
  225. /* ******************** TAR, DSCR ********** */
  226. mfspr r3, SPRN_TAR
  227. mfspr r4, SPRN_DSCR
  228. std r3, THREAD_TM_TAR(r12)
  229. std r4, THREAD_TM_DSCR(r12)
  230. /*
  231. * MSR and flags: We don't change CRs, and we don't need to alter MSR.
  232. */
  233. /*
  234. * ******************** FPR/VR/VSRs ************
  235. * After reclaiming, capture the checkpointed FPRs/VRs.
  236. *
  237. * We enabled VEC/FP/VSX in the msr above, so we can execute these
  238. * instructions!
  239. */
  240. mr r3, r12
  241. /* Altivec (VEC/VMX/VR)*/
  242. addi r7, r3, THREAD_CKVRSTATE
  243. SAVE_32VRS(0, r6, r7) /* r6 scratch, r7 ckvr_state */
  244. mfvscr v0
  245. li r6, VRSTATE_VSCR
  246. stvx v0, r7, r6
  247. /* VRSAVE */
  248. mfspr r0, SPRN_VRSAVE
  249. std r0, THREAD_CKVRSAVE(r3)
  250. /* Floating Point (FP) */
  251. addi r7, r3, THREAD_CKFPSTATE
  252. SAVE_32FPRS_VSRS(0, R6, R7) /* r6 scratch, r7 ckfp_state */
  253. mffs fr0
  254. stfd fr0,FPSTATE_FPSCR(r7)
  255. /*
  256. * TM regs, incl TEXASR -- these live in thread_struct. Note they've
  257. * been updated by the treclaim, to explain to userland the failure
  258. * cause (aborted).
  259. */
  260. mfspr r0, SPRN_TEXASR
  261. mfspr r3, SPRN_TFHAR
  262. mfspr r4, SPRN_TFIAR
  263. std r0, THREAD_TM_TEXASR(r12)
  264. std r3, THREAD_TM_TFHAR(r12)
  265. std r4, THREAD_TM_TFIAR(r12)
  266. /* AMR is checkpointed too, but is unsupported by Linux. */
  267. /* Restore original MSR/IRQ state & clear TM mode */
  268. ld r14, TM_FRAME_L0(r1) /* Orig MSR */
  269. li r15, 0
  270. rldimi r14, r15, MSR_TS_LG, (63-MSR_TS_LG)-1
  271. mtmsrd r14
  272. REST_NVGPRS(r1)
  273. addi r1, r1, TM_FRAME_SIZE
  274. lwz r4, 8(r1)
  275. ld r0, 16(r1)
  276. mtcr r4
  277. mtlr r0
  278. ld r2, STK_GOT(r1)
  279. /* Load CPU's default DSCR */
  280. ld r0, PACA_DSCR_DEFAULT(r13)
  281. mtspr SPRN_DSCR, r0
  282. blr
  283. /*
  284. * void __tm_recheckpoint(struct thread_struct *thread)
  285. * - Restore the checkpointed register state saved by tm_reclaim
  286. * when we switch_to a process.
  287. *
  288. * Call with IRQs off, stacks get all out of sync for
  289. * some periods in here!
  290. */
  291. _GLOBAL(__tm_recheckpoint)
  292. mfcr r5
  293. mflr r0
  294. stw r5, 8(r1)
  295. std r0, 16(r1)
  296. std r2, STK_GOT(r1)
  297. stdu r1, -TM_FRAME_SIZE(r1)
  298. /*
  299. * We've a struct pt_regs at [r1+STACK_FRAME_OVERHEAD].
  300. * This is used for backing up the NVGPRs:
  301. */
  302. SAVE_NVGPRS(r1)
  303. /* Load complete register state from ts_ckpt* registers */
  304. addi r7, r3, PT_CKPT_REGS /* Thread's ckpt_regs */
  305. /*
  306. * Make r7 look like an exception frame so that we can use the neat
  307. * GPRx(n) macros. r7 is now NOT a pt_regs ptr!
  308. */
  309. subi r7, r7, STACK_FRAME_OVERHEAD
  310. /* We need to setup MSR for FP/VMX/VSX register save instructions. */
  311. mfmsr r6
  312. mr r5, r6
  313. ori r5, r5, MSR_FP
  314. #ifdef CONFIG_ALTIVEC
  315. oris r5, r5, MSR_VEC@h
  316. #endif
  317. #ifdef CONFIG_VSX
  318. BEGIN_FTR_SECTION
  319. oris r5,r5, MSR_VSX@h
  320. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  321. #endif
  322. mtmsrd r5
  323. #ifdef CONFIG_ALTIVEC
  324. /*
  325. * FP and VEC registers: These are recheckpointed from
  326. * thread.ckfp_state and thread.ckvr_state respectively. The
  327. * thread.fp_state[] version holds the 'live' (transactional)
  328. * and will be loaded subsequently by any FPUnavailable trap.
  329. */
  330. addi r8, r3, THREAD_CKVRSTATE
  331. li r5, VRSTATE_VSCR
  332. lvx v0, r8, r5
  333. mtvscr v0
  334. REST_32VRS(0, r5, r8) /* r5 scratch, r8 ptr */
  335. ld r5, THREAD_CKVRSAVE(r3)
  336. mtspr SPRN_VRSAVE, r5
  337. #endif
  338. addi r8, r3, THREAD_CKFPSTATE
  339. lfd fr0, FPSTATE_FPSCR(r8)
  340. MTFSF_L(fr0)
  341. REST_32FPRS_VSRS(0, R4, R8)
  342. mtmsr r6 /* FP/Vec off again! */
  343. restore_gprs:
  344. /* ******************** CR,LR,CCR,MSR ********** */
  345. ld r4, _CTR(r7)
  346. ld r5, _LINK(r7)
  347. ld r8, _XER(r7)
  348. mtctr r4
  349. mtlr r5
  350. mtxer r8
  351. /* ******************** TAR ******************** */
  352. ld r4, THREAD_TM_TAR(r3)
  353. mtspr SPRN_TAR, r4
  354. /* Load up the PPR and DSCR in GPRs only at this stage */
  355. ld r5, THREAD_TM_DSCR(r3)
  356. ld r6, THREAD_TM_PPR(r3)
  357. REST_GPR(0, r7) /* GPR0 */
  358. REST_2GPRS(2, r7) /* GPR2-3 */
  359. REST_GPR(4, r7) /* GPR4 */
  360. REST_4GPRS(8, r7) /* GPR8-11 */
  361. REST_2GPRS(12, r7) /* GPR12-13 */
  362. REST_NVGPRS(r7) /* GPR14-31 */
  363. /* Load up PPR and DSCR here so we don't run with user values for long */
  364. mtspr SPRN_DSCR, r5
  365. mtspr SPRN_PPR, r6
  366. /*
  367. * Do final sanity check on TEXASR to make sure FS is set. Do this
  368. * here before we load up the userspace r1 so any bugs we hit will get
  369. * a call chain.
  370. */
  371. mfspr r5, SPRN_TEXASR
  372. srdi r5, r5, 16
  373. li r6, (TEXASR_FS)@h
  374. and r6, r6, r5
  375. 1: tdeqi r6, 0
  376. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  377. /*
  378. * Do final sanity check on MSR to make sure we are not transactional
  379. * or suspended.
  380. */
  381. mfmsr r6
  382. li r5, (MSR_TS_MASK)@higher
  383. srdi r6, r6, 32
  384. and r6, r6, r5
  385. 1: tdnei r6, 0
  386. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,0
  387. /* Restore CR */
  388. ld r6, _CCR(r7)
  389. mtcr r6
  390. REST_GPR(6, r7)
  391. /*
  392. * Store r1 and r5 on the stack so that we can access them after we
  393. * clear MSR RI.
  394. */
  395. REST_GPR(5, r7)
  396. std r5, -8(r1)
  397. ld r5, GPR1(r7)
  398. std r5, -16(r1)
  399. REST_GPR(7, r7)
  400. /* Clear MSR RI since we are about to change r1. EE is already off */
  401. li r5, 0
  402. mtmsrd r5, 1
  403. /*
  404. * BE CAREFUL HERE:
  405. * At this point we can't take an SLB miss since we have MSR_RI
  406. * off. Load only to/from the stack/paca which are in SLB bolted regions
  407. * until we turn MSR RI back on.
  408. */
  409. SET_SCRATCH0(r1)
  410. ld r5, -8(r1)
  411. ld r1, -16(r1)
  412. /* Commit register state as checkpointed state: */
  413. TRECHKPT
  414. HMT_MEDIUM
  415. /*
  416. * Our transactional state has now changed.
  417. *
  418. * Now just get out of here. Transactional (current) state will be
  419. * updated once restore is called on the return path in the _switch-ed
  420. * -to process.
  421. */
  422. GET_PACA(r13)
  423. GET_SCRATCH0(r1)
  424. /* R1 is restored, so we are recoverable again. EE is still off */
  425. li r4, MSR_RI
  426. mtmsrd r4, 1
  427. REST_NVGPRS(r1)
  428. addi r1, r1, TM_FRAME_SIZE
  429. lwz r4, 8(r1)
  430. ld r0, 16(r1)
  431. mtcr r4
  432. mtlr r0
  433. ld r2, STK_GOT(r1)
  434. /* Load CPU's default DSCR */
  435. ld r0, PACA_DSCR_DEFAULT(r13)
  436. mtspr SPRN_DSCR, r0
  437. blr
  438. /* ****************************************************************** */