Skip to content

Commit 64e0d7a

Browse files
committed
Ztso psABI atomics specification
1 parent d72db56 commit 64e0d7a

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-15
lines changed

riscv-atomic.adoc

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ endif::[]
66

77
== RISC-V atomics mappings
88

9-
This specifies mappings of C and C\++ atomic operations to RISC-V
9+
This specifies mappings of C and {Cpp} atomic operations to RISC-V
1010
machine instructions. Other languages, for example Java, provide similar
1111
facilities that should be implemented in a consistent manner, usually
12-
by applying the mapping for the corresponding C++ primitive.
12+
by applying the mapping for the corresponding {Cpp} primitive.
1313

1414
NOTE: Because different programming languages may be used within the same
1515
process, these mappings must be compatible across programming languages. For
@@ -19,12 +19,12 @@ C.
1919

2020
NOTE: Though many mappings are possible, not all of them will interoperate
2121
correctly. In particular, many mapping combinations will not
22-
correctly enforce ordering  between a C++ `memory_order_seq_cst`
22+
correctly enforce ordering  between a {Cpp} `memory_order_seq_cst`
2323
store and a subsequent `memory_order_seq_cst` load.
2424

2525
NOTE: These mappings are very similar to those that originally appeared in the
2626
appendix of the RISC-V "unprivileged" architecture specification as
27-
"Mappings from C/C++ primitives to RISC-V Primitives", which we will
27+
"Mappings from C/{Cpp} primitives to RISC-V Primitives", which we will
2828
refer to by their 2019 historical label of "Table A.6". That mapping may
2929
be used, _except_ that `atomic_store(memory_order_seq_cst)` must have an
3030
an extra trailing fence for compatibility with the "Hypothetical mappings ..."
@@ -55,10 +55,10 @@ section deals with other read-modify-write operations that require
5555
the `lr` and `sc` instructions.
5656

5757
[[tab:c11mappings]]
58-
.Mappings from C/C++ primitives to RISC-V primitives
58+
.Mappings from C/{Cpp} primitives to RISC-V primitives
5959
[cols="<22,<18,<4",options="header",]
6060
|===
61-
|C/C++ Construct |RVWMO Mapping |Notes
61+
|C/{Cpp} Construct |RVWMO Mapping |Notes
6262

6363
|Non-atomic load |`l{b\|h\|w\|d}` |
6464

@@ -97,7 +97,7 @@ the `lr` and `sc` instructions.
9797

9898
[cols="<20,<20,<4",options="header",]
9999
|===
100-
|C/C++ Construct |RVWMO AMO Mapping |Notes
100+
|C/{Cpp} Construct |RVWMO AMO Mapping |Notes
101101

102102
|`atomic_<op>(memory_order_relaxed)` |`amo<op>.{w\|d}` |4
103103

@@ -113,7 +113,7 @@ the `lr` and `sc` instructions.
113113

114114
[cols="<16,<24,<4",options="header",]
115115
|===
116-
|C/C++ Construct |RVWMO LR/SC Mapping |Notes
116+
|C/{Cpp} Construct |RVWMO LR/SC Mapping |Notes
117117

118118
|`atomic_<op>(memory_order_relaxed)` |`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4
119119

@@ -136,7 +136,7 @@ the `lr` and `sc` instructions.
136136
=== Meaning of notes in table
137137

138138
1) Depends on a load instruction with an RCsc aquire annotation,
139-
or a store instruction with an RCsc release annotation. These are curently
139+
or a store instruction with an RCsc release annotation. These are currently
140140
under discussion, but the specification has not yet been approved.
141141

142142
2) An RCpc load or store would also suffice, if it were to be introduced
@@ -148,11 +148,80 @@ mappings with code generated by a compiler using those older mappings.
148148

149149
4) Currently only directly possible for 32- and 64-bit operands.
150150

151-
=== Other conventions
151+
== Ztso atomics mappings
152152

153-
It is expected that the RVWMO AMO Mappings will be used for atomic read-modify-write
154-
operations that are directly supported by corresponding AMO instructions,
155-
and that LR/SC mappings will be used for the remainder, currently
153+
This specifies additional mappings of C and {Cpp} atomic operations to RISC-V
154+
machine instructions.
155+
156+
For each construct, we provide a mapping that assumes only the A and Ztso
157+
extension.
158+
159+
All mappings interoperate correctly with the RVWMO mappings, and with the
160+
original "Table A.6" mappings.
161+
162+
We present the mappings as a table in 3 sections, as above.
163+
164+
[[tab:c11mappingsztso]]
165+
.Mappings with Ztso extension from C/{Cpp} primitives to RISC-V primitives
166+
[cols="<22,<18,<4",options="header",]
167+
|===
168+
|C/{Cpp} Construct |Ztso Mapping |Notes
169+
170+
|`atomic_load(memory_order_acquire)` |`l{b\|h\|w\|d}` | 5
171+
172+
|`atomic_load(memory_order_seq_cst)` |`fence rw,rw; l{b\|h\|w\|d}` | 5
173+
174+
|`atomic_store(memory_order_release)` |`s{b\|h\|w\|d}` | 5
175+
176+
|`atomic_store(memory_order_seq_cst)` |`s{b\|h\|w\|d}; fence rw, rw` | 5
177+
178+
|`atomic_thread_fence(memory_order_acquire)` |`nop` | 5
179+
180+
|`atomic_thread_fence(memory_order_release)` |`nop` | 5
181+
182+
|`atomic_thread_fence(memory_order_acq_rel)` |`nop` | 5
183+
|===
184+
185+
[cols="<20,<20,<4",options="header",]
186+
|===
187+
|C/{Cpp} Construct |Ztso AMO Mapping |Notes
188+
189+
|`atomic_<op>(memory_order_acquire)` |`amo<op>.{w\|d}` |4, 5
190+
191+
|`atomic_<op>(memory_order_release)` |`amo<op>.{w\|d}` |4, 5
192+
193+
|`atomic_<op>(memory_order_acq_rel)` |`amo<op>.{w\|d}` |4, 5
194+
195+
|`atomic_<op>(memory_order_seq_cst)` |`amo<op>.{w\|d}` |4, 5
196+
197+
|===
198+
199+
[cols="<16,<24,<4",options="header",]
200+
|===
201+
|C/{Cpp} Construct |Ztso LR/SC Mapping |Notes
202+
203+
|`atomic_<op>(memory_order_acquire)`
204+
|`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4, 5
205+
206+
|`atomic_<op>(memory_order_release)`
207+
|`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4, 5
208+
209+
|`atomic_<op>(memory_order_acq_rel)`
210+
|`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4, 5
211+
212+
|===
213+
214+
=== Meaning of notes in table
215+
216+
4) Currently only directly possible for 32- and 64-bit operands.
217+
218+
5) Requires the Ztso extension.
219+
220+
== Other conventions
221+
222+
It is expected that the RVWMO and Ztso AMO Mappings will be used for atomic
223+
read-modify-write operations that are directly supported by corresponding AMO
224+
instructions, and that LR/SC mappings will be used for the remainder, currently
156225
including compare-exchange operations. Compare-exchange LR/SC sequences
157226
on the containing 32-bit word should be used for shorter operands. Thus,
158227
a `fetch_add` operation on a 16-bit quantity would use a 32-bit LR/SC sequence.

riscv-elf.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ raise an error.
266266
RVE::: Linker should report errors if object files of different value
267267
for RVE field.
268268

269-
TSO::: Linker should report errors if object files of different value
270-
for TSO field.
269+
TSO::: Input files can have different values for the TSO field; the linker
270+
should set this field if any of the input objects have the TSO field set.
271271

272272
NOTE: The static linker may ignore the compatibility checks if all fields in the
273273
`e_flags` are zero and all sections in the input file are non-executable

0 commit comments

Comments
 (0)