162306a36Sopenharmony_ci============================ 262306a36Sopenharmony_ciTransactional Memory support 362306a36Sopenharmony_ci============================ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciPOWER kernel support for this feature is currently limited to supporting 662306a36Sopenharmony_ciits use by user programs. It is not currently used by the kernel itself. 762306a36Sopenharmony_ci 862306a36Sopenharmony_ciThis file aims to sum up how it is supported by Linux and what behaviour you 962306a36Sopenharmony_cican expect from your user programs. 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciBasic overview 1362306a36Sopenharmony_ci============== 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ciHardware Transactional Memory is supported on POWER8 processors, and is a 1662306a36Sopenharmony_cifeature that enables a different form of atomic memory access. Several new 1762306a36Sopenharmony_ciinstructions are presented to delimit transactions; transactions are 1862306a36Sopenharmony_ciguaranteed to either complete atomically or roll back and undo any partial 1962306a36Sopenharmony_cichanges. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciA simple transaction looks like this:: 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci begin_move_money: 2462306a36Sopenharmony_ci tbegin 2562306a36Sopenharmony_ci beq abort_handler 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci ld r4, SAVINGS_ACCT(r3) 2862306a36Sopenharmony_ci ld r5, CURRENT_ACCT(r3) 2962306a36Sopenharmony_ci subi r5, r5, 1 3062306a36Sopenharmony_ci addi r4, r4, 1 3162306a36Sopenharmony_ci std r4, SAVINGS_ACCT(r3) 3262306a36Sopenharmony_ci std r5, CURRENT_ACCT(r3) 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci tend 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci b continue 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci abort_handler: 3962306a36Sopenharmony_ci ... test for odd failures ... 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci /* Retry the transaction if it failed because it conflicted with 4262306a36Sopenharmony_ci * someone else: */ 4362306a36Sopenharmony_ci b begin_move_money 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ciThe 'tbegin' instruction denotes the start point, and 'tend' the end point. 4762306a36Sopenharmony_ciBetween these points the processor is in 'Transactional' state; any memory 4862306a36Sopenharmony_cireferences will complete in one go if there are no conflicts with other 4962306a36Sopenharmony_citransactional or non-transactional accesses within the system. In this 5062306a36Sopenharmony_ciexample, the transaction completes as though it were normal straight-line code 5162306a36Sopenharmony_ciIF no other processor has touched SAVINGS_ACCT(r3) or CURRENT_ACCT(r3); an 5262306a36Sopenharmony_ciatomic move of money from the current account to the savings account has been 5362306a36Sopenharmony_ciperformed. Even though the normal ld/std instructions are used (note no 5462306a36Sopenharmony_cilwarx/stwcx), either *both* SAVINGS_ACCT(r3) and CURRENT_ACCT(r3) will be 5562306a36Sopenharmony_ciupdated, or neither will be updated. 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ciIf, in the meantime, there is a conflict with the locations accessed by the 5862306a36Sopenharmony_citransaction, the transaction will be aborted by the CPU. Register and memory 5962306a36Sopenharmony_cistate will roll back to that at the 'tbegin', and control will continue from 6062306a36Sopenharmony_ci'tbegin+4'. The branch to abort_handler will be taken this second time; the 6162306a36Sopenharmony_ciabort handler can check the cause of the failure, and retry. 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ciCheckpointed registers include all GPRs, FPRs, VRs/VSRs, LR, CCR/CR, CTR, FPCSR 6462306a36Sopenharmony_ciand a few other status/flag regs; see the ISA for details. 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciCauses of transaction aborts 6762306a36Sopenharmony_ci============================ 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci- Conflicts with cache lines used by other processors 7062306a36Sopenharmony_ci- Signals 7162306a36Sopenharmony_ci- Context switches 7262306a36Sopenharmony_ci- See the ISA for full documentation of everything that will abort transactions. 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ciSyscalls 7662306a36Sopenharmony_ci======== 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ciSyscalls made from within an active transaction will not be performed and the 7962306a36Sopenharmony_citransaction will be doomed by the kernel with the failure code TM_CAUSE_SYSCALL 8062306a36Sopenharmony_ci| TM_CAUSE_PERSISTENT. 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ciSyscalls made from within a suspended transaction are performed as normal and 8362306a36Sopenharmony_cithe transaction is not explicitly doomed by the kernel. However, what the 8462306a36Sopenharmony_cikernel does to perform the syscall may result in the transaction being doomed 8562306a36Sopenharmony_ciby the hardware. The syscall is performed in suspended mode so any side 8662306a36Sopenharmony_cieffects will be persistent, independent of transaction success or failure. No 8762306a36Sopenharmony_ciguarantees are provided by the kernel about which syscalls will affect 8862306a36Sopenharmony_citransaction success. 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciCare must be taken when relying on syscalls to abort during active transactions 9162306a36Sopenharmony_ciif the calls are made via a library. Libraries may cache values (which may 9262306a36Sopenharmony_cigive the appearance of success) or perform operations that cause transaction 9362306a36Sopenharmony_cifailure before entering the kernel (which may produce different failure codes). 9462306a36Sopenharmony_ciExamples are glibc's getpid() and lazy symbol resolution. 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ciSignals 9862306a36Sopenharmony_ci======= 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciDelivery of signals (both sync and async) during transactions provides a second 10162306a36Sopenharmony_cithread state (ucontext/mcontext) to represent the second transactional register 10262306a36Sopenharmony_cistate. Signal delivery 'treclaim's to capture both register states, so signals 10362306a36Sopenharmony_ciabort transactions. The usual ucontext_t passed to the signal handler 10462306a36Sopenharmony_cirepresents the checkpointed/original register state; the signal appears to have 10562306a36Sopenharmony_ciarisen at 'tbegin+4'. 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ciIf the sighandler ucontext has uc_link set, a second ucontext has been 10862306a36Sopenharmony_cidelivered. For future compatibility the MSR.TS field should be checked to 10962306a36Sopenharmony_cidetermine the transactional state -- if so, the second ucontext in uc->uc_link 11062306a36Sopenharmony_cirepresents the active transactional registers at the point of the signal. 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ciFor 64-bit processes, uc->uc_mcontext.regs->msr is a full 64-bit MSR and its TS 11362306a36Sopenharmony_cifield shows the transactional mode. 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ciFor 32-bit processes, the mcontext's MSR register is only 32 bits; the top 32 11662306a36Sopenharmony_cibits are stored in the MSR of the second ucontext, i.e. in 11762306a36Sopenharmony_ciuc->uc_link->uc_mcontext.regs->msr. The top word contains the transactional 11862306a36Sopenharmony_cistate TS. 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ciHowever, basic signal handlers don't need to be aware of transactions 12162306a36Sopenharmony_ciand simply returning from the handler will deal with things correctly: 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ciTransaction-aware signal handlers can read the transactional register state 12462306a36Sopenharmony_cifrom the second ucontext. This will be necessary for crash handlers to 12562306a36Sopenharmony_cidetermine, for example, the address of the instruction causing the SIGSEGV. 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ciExample signal handler:: 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci void crash_handler(int sig, siginfo_t *si, void *uc) 13062306a36Sopenharmony_ci { 13162306a36Sopenharmony_ci ucontext_t *ucp = uc; 13262306a36Sopenharmony_ci ucontext_t *transactional_ucp = ucp->uc_link; 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci if (ucp_link) { 13562306a36Sopenharmony_ci u64 msr = ucp->uc_mcontext.regs->msr; 13662306a36Sopenharmony_ci /* May have transactional ucontext! */ 13762306a36Sopenharmony_ci #ifndef __powerpc64__ 13862306a36Sopenharmony_ci msr |= ((u64)transactional_ucp->uc_mcontext.regs->msr) << 32; 13962306a36Sopenharmony_ci #endif 14062306a36Sopenharmony_ci if (MSR_TM_ACTIVE(msr)) { 14162306a36Sopenharmony_ci /* Yes, we crashed during a transaction. Oops. */ 14262306a36Sopenharmony_ci fprintf(stderr, "Transaction to be restarted at 0x%llx, but " 14362306a36Sopenharmony_ci "crashy instruction was at 0x%llx\n", 14462306a36Sopenharmony_ci ucp->uc_mcontext.regs->nip, 14562306a36Sopenharmony_ci transactional_ucp->uc_mcontext.regs->nip); 14662306a36Sopenharmony_ci } 14762306a36Sopenharmony_ci } 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci fix_the_problem(ucp->dar); 15062306a36Sopenharmony_ci } 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ciWhen in an active transaction that takes a signal, we need to be careful with 15362306a36Sopenharmony_cithe stack. It's possible that the stack has moved back up after the tbegin. 15462306a36Sopenharmony_ciThe obvious case here is when the tbegin is called inside a function that 15562306a36Sopenharmony_cireturns before a tend. In this case, the stack is part of the checkpointed 15662306a36Sopenharmony_citransactional memory state. If we write over this non transactionally or in 15762306a36Sopenharmony_cisuspend, we are in trouble because if we get a tm abort, the program counter and 15862306a36Sopenharmony_cistack pointer will be back at the tbegin but our in memory stack won't be valid 15962306a36Sopenharmony_cianymore. 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ciTo avoid this, when taking a signal in an active transaction, we need to use 16262306a36Sopenharmony_cithe stack pointer from the checkpointed state, rather than the speculated 16362306a36Sopenharmony_cistate. This ensures that the signal context (written tm suspended) will be 16462306a36Sopenharmony_ciwritten below the stack required for the rollback. The transaction is aborted 16562306a36Sopenharmony_cibecause of the treclaim, so any memory written between the tbegin and the 16662306a36Sopenharmony_cisignal will be rolled back anyway. 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ciFor signals taken in non-TM or suspended mode, we use the 16962306a36Sopenharmony_cinormal/non-checkpointed stack pointer. 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ciAny transaction initiated inside a sighandler and suspended on return 17262306a36Sopenharmony_cifrom the sighandler to the kernel will get reclaimed and discarded. 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ciFailure cause codes used by kernel 17562306a36Sopenharmony_ci================================== 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ciThese are defined in <asm/reg.h>, and distinguish different reasons why the 17862306a36Sopenharmony_cikernel aborted a transaction: 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci ====================== ================================ 18162306a36Sopenharmony_ci TM_CAUSE_RESCHED Thread was rescheduled. 18262306a36Sopenharmony_ci TM_CAUSE_TLBI Software TLB invalid. 18362306a36Sopenharmony_ci TM_CAUSE_FAC_UNAV FP/VEC/VSX unavailable trap. 18462306a36Sopenharmony_ci TM_CAUSE_SYSCALL Syscall from active transaction. 18562306a36Sopenharmony_ci TM_CAUSE_SIGNAL Signal delivered. 18662306a36Sopenharmony_ci TM_CAUSE_MISC Currently unused. 18762306a36Sopenharmony_ci TM_CAUSE_ALIGNMENT Alignment fault. 18862306a36Sopenharmony_ci TM_CAUSE_EMULATE Emulation that touched memory. 18962306a36Sopenharmony_ci ====================== ================================ 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ciThese can be checked by the user program's abort handler as TEXASR[0:7]. If 19262306a36Sopenharmony_cibit 7 is set, it indicates that the error is considered persistent. For example 19362306a36Sopenharmony_cia TM_CAUSE_ALIGNMENT will be persistent while a TM_CAUSE_RESCHED will not. 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ciGDB 19662306a36Sopenharmony_ci=== 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ciGDB and ptrace are not currently TM-aware. If one stops during a transaction, 19962306a36Sopenharmony_ciit looks like the transaction has just started (the checkpointed state is 20062306a36Sopenharmony_cipresented). The transaction cannot then be continued and will take the failure 20162306a36Sopenharmony_cihandler route. Furthermore, the transactional 2nd register state will be 20262306a36Sopenharmony_ciinaccessible. GDB can currently be used on programs using TM, but not sensibly 20362306a36Sopenharmony_ciin parts within transactions. 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ciPOWER9 20662306a36Sopenharmony_ci====== 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ciTM on POWER9 has issues with storing the complete register state. This 20962306a36Sopenharmony_ciis described in this commit:: 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci commit 4bb3c7a0208fc13ca70598efd109901a7cd45ae7 21262306a36Sopenharmony_ci Author: Paul Mackerras <paulus@ozlabs.org> 21362306a36Sopenharmony_ci Date: Wed Mar 21 21:32:01 2018 +1100 21462306a36Sopenharmony_ci KVM: PPC: Book3S HV: Work around transactional memory bugs in POWER9 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ciTo account for this different POWER9 chips have TM enabled in 21762306a36Sopenharmony_cidifferent ways. 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ciOn POWER9N DD2.01 and below, TM is disabled. ie 22062306a36Sopenharmony_ciHWCAP2[PPC_FEATURE2_HTM] is not set. 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ciOn POWER9N DD2.1 TM is configured by firmware to always abort a 22362306a36Sopenharmony_citransaction when tm suspend occurs. So tsuspend will cause a 22462306a36Sopenharmony_citransaction to be aborted and rolled back. Kernel exceptions will also 22562306a36Sopenharmony_cicause the transaction to be aborted and rolled back and the exception 22662306a36Sopenharmony_ciwill not occur. If userspace constructs a sigcontext that enables TM 22762306a36Sopenharmony_cisuspend, the sigcontext will be rejected by the kernel. This mode is 22862306a36Sopenharmony_ciadvertised to users with HWCAP2[PPC_FEATURE2_HTM_NO_SUSPEND] set. 22962306a36Sopenharmony_ciHWCAP2[PPC_FEATURE2_HTM] is not set in this mode. 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ciOn POWER9N DD2.2 and above, KVM and POWERVM emulate TM for guests (as 23262306a36Sopenharmony_cidescribed in commit 4bb3c7a0208f), hence TM is enabled for guests 23362306a36Sopenharmony_ciie. HWCAP2[PPC_FEATURE2_HTM] is set for guest userspace. Guests that 23462306a36Sopenharmony_cimakes heavy use of TM suspend (tsuspend or kernel suspend) will result 23562306a36Sopenharmony_ciin traps into the hypervisor and hence will suffer a performance 23662306a36Sopenharmony_cidegradation. Host userspace has TM disabled 23762306a36Sopenharmony_ciie. HWCAP2[PPC_FEATURE2_HTM] is not set. (although we make enable it 23862306a36Sopenharmony_ciat some point in the future if we bring the emulation into host 23962306a36Sopenharmony_ciuserspace context switching). 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ciPOWER9C DD1.2 and above are only available with POWERVM and hence 24262306a36Sopenharmony_ciLinux only runs as a guest. On these systems TM is emulated like on 24362306a36Sopenharmony_ciPOWER9N DD2.2. 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ciGuest migration from POWER8 to POWER9 will work with POWER9N DD2.2 and 24662306a36Sopenharmony_ciPOWER9C DD1.2. Since earlier POWER9 processors don't support TM 24762306a36Sopenharmony_ciemulation, migration from POWER8 to POWER9 is not supported there. 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciKernel implementation 25062306a36Sopenharmony_ci===================== 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_cih/rfid mtmsrd quirk 25362306a36Sopenharmony_ci------------------- 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ciAs defined in the ISA, rfid has a quirk which is useful in early 25662306a36Sopenharmony_ciexception handling. When in a userspace transaction and we enter the 25762306a36Sopenharmony_cikernel via some exception, MSR will end up as TM=0 and TS=01 (ie. TM 25862306a36Sopenharmony_cioff but TM suspended). Regularly the kernel will want change bits in 25962306a36Sopenharmony_cithe MSR and will perform an rfid to do this. In this case rfid can 26062306a36Sopenharmony_cihave SRR0 TM = 0 and TS = 00 (ie. TM off and non transaction) and the 26162306a36Sopenharmony_ciresulting MSR will retain TM = 0 and TS=01 from before (ie. stay in 26262306a36Sopenharmony_cisuspend). This is a quirk in the architecture as this would normally 26362306a36Sopenharmony_cibe a transition from TS=01 to TS=00 (ie. suspend -> non transactional) 26462306a36Sopenharmony_ciwhich is an illegal transition. 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ciThis quirk is described the architecture in the definition of rfid 26762306a36Sopenharmony_ciwith these lines: 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci if (MSR 29:31 ¬ = 0b010 | SRR1 29:31 ¬ = 0b000) then 27062306a36Sopenharmony_ci MSR 29:31 <- SRR1 29:31 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_cihrfid and mtmsrd have the same quirk. 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ciThe Linux kernel uses this quirk in its early exception handling. 275