Are CSR instructions memory atomics?
No. Atomic here means the CSR read-modify-write is one instruction, not an AMO to memory.
Atomically swap CSR and integer register: read the old CSR value into rd and write rs1 into the CSR
CSRRW uses opcode 1110011 (0x73), funct3 001. The rs1 field selects the source register, the 12-bit immediate provides the second operand, and rd selects the destination.
CSRRW atomically swaps values between a CSR and an integer register: reads the old CSR value (zero-extended to XLEN bits) into rd, and writes the initial value from rs1 into the CSR. If rd=x0, the instruction shall not read the CSR and shall not cause any CSR read side effects, though the write still occurs. If rs1=x0, the value zero is written to the CSR. This is the most direct CSR atomic read/write primitive, commonly used for saving/restoring CSR state during context switches.
CSRRW is a Zicsr atomic CSR read-modify-write instruction. CSR addresses are 12 bits, and whether read/write side effects occur depends on rd and on rs1 or uimm being x0/0.
Understand this scenario with real code like «csrrw x5, mepc, x6 # x5 = old mepc; mepc = x6».
Understand this scenario with real code like «csrrw x5, mepc, x6 # x5 = old mepc; mepc = x6».
No. Atomic here means the CSR read-modify-write is one instruction, not an AMO to memory.
For CSRRW it suppresses the CSR read and read side effects; CSRRS/CSRRC still read the CSR.