Webcentive RISC-V Reference

A instructions

Atomic instructions

The atomic-instruction extension, named A, contains instructions that atomically read-modify-write memory to support synchronization between multiple RISC-V harts running in the same memory space. The two forms of atomic instruction provided are load-reserved/store-conditional instructions and atomic fetch-and-op memory instructions. Both types of atomic instruction support various memory consistency orderings including unordered, acquire, release, and sequentially consistent semantics. These instructions allow RISC-V to support the RCsc memory consistency model. cite:[Gharachorloo90memoryconsistency]

Note

After much debate, the language community and architecture community appear to have finally settled on release consistency as the standard memory consistency model and so the RISC-V atomic support is built around this model.

The A extension comprises instructions provided by the Zaamo and Zalrsc extensions.

Specifying Ordering of Atomic Instructions

The base RISC-V ISA has a relaxed memory model, with the FENCE instruction used to impose additional ordering constraints. The address space is divided by the execution environment into memory and I/O domains, and the FENCE instruction provides options to order accesses to one or both of these two address domains.

To provide more efficient support for release consistency cite:[Gharachorloo90memoryconsistency], each atomic instruction has two bits, aq and rl, used to specify additional memory ordering constraints as viewed by other RISC-V harts. The bits order accesses to one of the two address domains, memory or I/O, depending on which address domain the atomic instruction is accessing. No ordering constraint is implied to accesses to the other domain, and a FENCE instruction should be used to order across both domains.

If both bits are clear, no additional ordering constraints are imposed on the atomic memory operation. If only the aq bit is set, the atomic memory operation is treated as an acquire access, i.e., no following memory operations on this RISC-V hart can be observed to take place before the acquire memory operation. If only the rl bit is set, the atomic memory operation is treated as a release access, i.e., the release memory operation cannot be observed to take place before any earlier memory operations on this RISC-V hart. If both the aq and rl bits are set, the atomic memory operation is sequentially consistent and cannot be observed to happen before any earlier memory operations or after any later memory operations in the same RISC-V hart and to the same address domain.

Extension description from riscv-unified-db, reformatted.

Specification chapter: The RISC-V Instruction Set Manual, src/unpriv/a-st-ext.adoc.

Instructions (22)

MnemonicName (riscv-unified-db)XLEN
amoadd.dAtomic fetch-and-add doublewordRV64 only
amoadd.wAtomic fetch-and-add wordRV32 and RV64
amoand.dAtomic fetch-and-and doublewordRV64 only
amoand.wAtomic fetch-and-and wordRV32 and RV64
amomax.dAtomic MAX doublewordRV64 only
amomax.wAtomic MAX wordRV32 and RV64
amomaxu.dAtomic MAX unsigned doublewordRV64 only
amomaxu.wAtomic MAX unsigned wordRV32 and RV64
amomin.dAtomic MIN doublewordRV64 only
amomin.wAtomic MIN wordRV32 and RV64
amominu.dAtomic MIN unsigned doublewordRV64 only
amominu.wAtomic MIN unsigned wordRV32 and RV64
amoor.dAtomic fetch-and-or doublewordRV64 only
amoor.wAtomic fetch-and-or wordRV32 and RV64
amoswap.dAtomic SWAP doublewordRV64 only
amoswap.wAtomic SWAP wordRV32 and RV64
amoxor.dAtomic fetch-and-xor doublewordRV64 only
amoxor.wAtomic fetch-and-xor wordRV32 and RV64
lr.dLoad Reserved DoublewordRV64 only
lr.wLoad Reserved WordRV32 and RV64
sc.dStore conditional doublewordRV64 only
sc.wStore conditional wordRV32 and RV64