transactional_memory.txt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. Transactional Memory support
  2. ============================
  3. POWER kernel support for this feature is currently limited to supporting
  4. its use by user programs. It is not currently used by the kernel itself.
  5. This file aims to sum up how it is supported by Linux and what behaviour you
  6. can expect from your user programs.
  7. Basic overview
  8. ==============
  9. Hardware Transactional Memory is supported on POWER8 processors, and is a
  10. feature that enables a different form of atomic memory access. Several new
  11. instructions are presented to delimit transactions; transactions are
  12. guaranteed to either complete atomically or roll back and undo any partial
  13. changes.
  14. A simple transaction looks like this:
  15. begin_move_money:
  16. tbegin
  17. beq abort_handler
  18. ld r4, SAVINGS_ACCT(r3)
  19. ld r5, CURRENT_ACCT(r3)
  20. subi r5, r5, 1
  21. addi r4, r4, 1
  22. std r4, SAVINGS_ACCT(r3)
  23. std r5, CURRENT_ACCT(r3)
  24. tend
  25. b continue
  26. abort_handler:
  27. ... test for odd failures ...
  28. /* Retry the transaction if it failed because it conflicted with
  29. * someone else: */
  30. b begin_move_money
  31. The 'tbegin' instruction denotes the start point, and 'tend' the end point.
  32. Between these points the processor is in 'Transactional' state; any memory
  33. references will complete in one go if there are no conflicts with other
  34. transactional or non-transactional accesses within the system. In this
  35. example, the transaction completes as though it were normal straight-line code
  36. IF no other processor has touched SAVINGS_ACCT(r3) or CURRENT_ACCT(r3); an
  37. atomic move of money from the current account to the savings account has been
  38. performed. Even though the normal ld/std instructions are used (note no
  39. lwarx/stwcx), either *both* SAVINGS_ACCT(r3) and CURRENT_ACCT(r3) will be
  40. updated, or neither will be updated.
  41. If, in the meantime, there is a conflict with the locations accessed by the
  42. transaction, the transaction will be aborted by the CPU. Register and memory
  43. state will roll back to that at the 'tbegin', and control will continue from
  44. 'tbegin+4'. The branch to abort_handler will be taken this second time; the
  45. abort handler can check the cause of the failure, and retry.
  46. Checkpointed registers include all GPRs, FPRs, VRs/VSRs, LR, CCR/CR, CTR, FPCSR
  47. and a few other status/flag regs; see the ISA for details.
  48. Causes of transaction aborts
  49. ============================
  50. - Conflicts with cache lines used by other processors
  51. - Signals
  52. - Context switches
  53. - See the ISA for full documentation of everything that will abort transactions.
  54. Syscalls
  55. ========
  56. Syscalls made from within an active transaction will not be performed and the
  57. transaction will be doomed by the kernel with the failure code TM_CAUSE_SYSCALL
  58. | TM_CAUSE_PERSISTENT.
  59. Syscalls made from within a suspended transaction are performed as normal and
  60. the transaction is not explicitly doomed by the kernel. However, what the
  61. kernel does to perform the syscall may result in the transaction being doomed
  62. by the hardware. The syscall is performed in suspended mode so any side
  63. effects will be persistent, independent of transaction success or failure. No
  64. guarantees are provided by the kernel about which syscalls will affect
  65. transaction success.
  66. Care must be taken when relying on syscalls to abort during active transactions
  67. if the calls are made via a library. Libraries may cache values (which may
  68. give the appearance of success) or perform operations that cause transaction
  69. failure before entering the kernel (which may produce different failure codes).
  70. Examples are glibc's getpid() and lazy symbol resolution.
  71. Signals
  72. =======
  73. Delivery of signals (both sync and async) during transactions provides a second
  74. thread state (ucontext/mcontext) to represent the second transactional register
  75. state. Signal delivery 'treclaim's to capture both register states, so signals
  76. abort transactions. The usual ucontext_t passed to the signal handler
  77. represents the checkpointed/original register state; the signal appears to have
  78. arisen at 'tbegin+4'.
  79. If the sighandler ucontext has uc_link set, a second ucontext has been
  80. delivered. For future compatibility the MSR.TS field should be checked to
  81. determine the transactional state -- if so, the second ucontext in uc->uc_link
  82. represents the active transactional registers at the point of the signal.
  83. For 64-bit processes, uc->uc_mcontext.regs->msr is a full 64-bit MSR and its TS
  84. field shows the transactional mode.
  85. For 32-bit processes, the mcontext's MSR register is only 32 bits; the top 32
  86. bits are stored in the MSR of the second ucontext, i.e. in
  87. uc->uc_link->uc_mcontext.regs->msr. The top word contains the transactional
  88. state TS.
  89. However, basic signal handlers don't need to be aware of transactions
  90. and simply returning from the handler will deal with things correctly:
  91. Transaction-aware signal handlers can read the transactional register state
  92. from the second ucontext. This will be necessary for crash handlers to
  93. determine, for example, the address of the instruction causing the SIGSEGV.
  94. Example signal handler:
  95. void crash_handler(int sig, siginfo_t *si, void *uc)
  96. {
  97. ucontext_t *ucp = uc;
  98. ucontext_t *transactional_ucp = ucp->uc_link;
  99. if (ucp_link) {
  100. u64 msr = ucp->uc_mcontext.regs->msr;
  101. /* May have transactional ucontext! */
  102. #ifndef __powerpc64__
  103. msr |= ((u64)transactional_ucp->uc_mcontext.regs->msr) << 32;
  104. #endif
  105. if (MSR_TM_ACTIVE(msr)) {
  106. /* Yes, we crashed during a transaction. Oops. */
  107. fprintf(stderr, "Transaction to be restarted at 0x%llx, but "
  108. "crashy instruction was at 0x%llx\n",
  109. ucp->uc_mcontext.regs->nip,
  110. transactional_ucp->uc_mcontext.regs->nip);
  111. }
  112. }
  113. fix_the_problem(ucp->dar);
  114. }
  115. When in an active transaction that takes a signal, we need to be careful with
  116. the stack. It's possible that the stack has moved back up after the tbegin.
  117. The obvious case here is when the tbegin is called inside a function that
  118. returns before a tend. In this case, the stack is part of the checkpointed
  119. transactional memory state. If we write over this non transactionally or in
  120. suspend, we are in trouble because if we get a tm abort, the program counter and
  121. stack pointer will be back at the tbegin but our in memory stack won't be valid
  122. anymore.
  123. To avoid this, when taking a signal in an active transaction, we need to use
  124. the stack pointer from the checkpointed state, rather than the speculated
  125. state. This ensures that the signal context (written tm suspended) will be
  126. written below the stack required for the rollback. The transaction is aborted
  127. because of the treclaim, so any memory written between the tbegin and the
  128. signal will be rolled back anyway.
  129. For signals taken in non-TM or suspended mode, we use the
  130. normal/non-checkpointed stack pointer.
  131. Failure cause codes used by kernel
  132. ==================================
  133. These are defined in <asm/reg.h>, and distinguish different reasons why the
  134. kernel aborted a transaction:
  135. TM_CAUSE_RESCHED Thread was rescheduled.
  136. TM_CAUSE_TLBI Software TLB invalid.
  137. TM_CAUSE_FAC_UNAV FP/VEC/VSX unavailable trap.
  138. TM_CAUSE_SYSCALL Syscall from active transaction.
  139. TM_CAUSE_SIGNAL Signal delivered.
  140. TM_CAUSE_MISC Currently unused.
  141. TM_CAUSE_ALIGNMENT Alignment fault.
  142. TM_CAUSE_EMULATE Emulation that touched memory.
  143. These can be checked by the user program's abort handler as TEXASR[0:7]. If
  144. bit 7 is set, it indicates that the error is consider persistent. For example
  145. a TM_CAUSE_ALIGNMENT will be persistent while a TM_CAUSE_RESCHED will not.
  146. GDB
  147. ===
  148. GDB and ptrace are not currently TM-aware. If one stops during a transaction,
  149. it looks like the transaction has just started (the checkpointed state is
  150. presented). The transaction cannot then be continued and will take the failure
  151. handler route. Furthermore, the transactional 2nd register state will be
  152. inaccessible. GDB can currently be used on programs using TM, but not sensibly
  153. in parts within transactions.