Debian Codegraph @

js/src/jit/shared/AtomicOperations-feeling-lucky-gcc.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /* For documentation, see jit/AtomicOperations.h, both the comment block at the
8 * beginning and the #ifdef nest near the end.
9 *
10 * This is a common file for tier-3 platforms (including simulators for our
11 * tier-1 platforms) that are not providing hardware-specific implementations of
12 * the atomic operations. Please keep it reasonably platform-independent by
13 * adding #ifdefs at the beginning as much as possible, not throughout the file.
14 *
15 *
16 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17 * !!!! NOTE !!!!
18 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19 *
20 * The implementations in this file are NOT SAFE and cannot be safe even in
21 * principle because they rely on C++ undefined behavior. However, they are
22 * frequently good enough for tier-3 platforms.
23 */
24
25 #ifndef jit_shared_AtomicOperations_feeling_lucky_gcc_h
26 #define jit_shared_AtomicOperations_feeling_lucky_gcc_h
27
28 #include "mozilla/Assertions.h"
29 #include "mozilla/Types.h"
30
31 // Explicitly exclude tier-1 platforms.
32
33 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || \
34 defined(_M_IX86) || (defined(__arm__) && __ARM_ARCH >= 7) || \
35 defined(__aarch64__))
36 # error "Do not use on a tier-1 platform where inline assembly is available"
37 #endif
38
39 #if !(defined(__clang__) || defined(__GNUC__))
40 # error "This file only for gcc/Clang"
41 #endif
42
43 // 64-bit atomics are not required by the JS spec, and you can compile
44 // SpiderMonkey without them. 64-bit atomics are required for BigInt
45 // support.
46 //
47 // 64-bit lock-free atomics are required for WebAssembly, but gating in the
48 // WebAssembly subsystem ensures that no WebAssembly-supporting platforms need
49 // code in this file.
50
51 #if defined(JS_SIMULATOR_ARM64) || defined(JS_SIMULATOR_ARM) || \
52 defined(JS_SIMULATOR_MIPS64) || defined(JS_SIMULATOR_LOONG64)
53 // On some x86 (32-bit) systems this will not work because the compiler does not
54 // open-code 64-bit atomics. If so, try linking with -latomic. If that doesn't
55 // work, you're mostly on your own.
56 # define HAS_64BIT_ATOMICS
57 # define HAS_64BIT_LOCKFREE
58 #endif
59
60 #if defined(__arm__)
61 # define HAS_64BIT_ATOMICS
62 #endif
63
64 #if defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || \
65 defined(__PPC64LE__)
66 # define HAS_64BIT_ATOMICS
67 # define HAS_64BIT_LOCKFREE
68 #endif
69
70 #if defined(__riscv) && __riscv_xlen == 64
71 # define HAS_64BIT_ATOMICS
72 # define HAS_64BIT_LOCKFREE
73 #endif
74
75 #if defined(__loongarch64)
76 # define HAS_64BIT_ATOMICS
77 # define HAS_64BIT_LOCKFREE
78 #endif
79
80 #ifdef __sparc__
81 # ifdef __LP64__
82 # define HAS_64BIT_ATOMICS
83 # define HAS_64BIT_LOCKFREE
84 # endif
85 #endif
86
87 #ifdef JS_CODEGEN_NONE
88 # ifdef JS_64BIT
89 # define HAS_64BIT_ATOMICS
90 # define HAS_64BIT_LOCKFREE
91 # endif
92 #endif
93
94 // The default implementation tactic for gcc/clang is to use the newer __atomic
95 // intrinsics added for use in C++11 <atomic>. Where that isn't available, we
96 // use GCC's older __sync functions instead.
97 //
98 // ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS is kept as a backward compatible
99 // option for older compilers: enable this to use GCC's old __sync functions
100 // instead of the newer __atomic functions. This will be required for GCC 4.6.x
101 // and earlier, and probably for Clang 3.1, should we need to use those
102 // versions. Firefox no longer supports compilers that old.
103
104 // #define ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
105
106 // Sanity check.
107
108 #if defined(HAS_64BIT_LOCKFREE) && !defined(HAS_64BIT_ATOMICS)
109 # error "This combination of features is senseless, please fix"
110 #endif
111
112 // Try to avoid platform #ifdefs below this point.
113
114 // When compiling with Clang on 32-bit linux it will be necessary to link with
115 // -latomic to get the proper 64-bit intrinsics.
116
117 inline bool js::jit::AtomicOperations::hasAtomic8() {
118 #if defined(HAS_64BIT_ATOMICS)
119 return true;
120 #else
121 return false;
122 #endif
123 }
124
125 inline bool js::jit::AtomicOperations::isLockfree8() {
126 #if defined(HAS_64BIT_LOCKFREE)
127 return true;
128 #else
129 return false;
130 #endif
131 }
132
133 inline void js::jit::AtomicOperations::fenceSeqCst() {
134 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
135 __sync_synchronize();
136 #else
137 __atomic_thread_fence(__ATOMIC_SEQ_CST);
138 #endif
139 }
140
141 inline void js::jit::AtomicOperations::pause() {
142 // No default implementation.
143 }
144
145 template <typename T>
146 inline T js::jit::AtomicOperations::loadSeqCst(T* addr) {
147 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
148 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
149 __sync_synchronize();
150 T v = *addr;
151 __sync_synchronize();
152 #else
153 T v;
154 __atomic_load(addr, &v, __ATOMIC_SEQ_CST);
155 #endif
156 return v;
157 }
158
159 #ifndef HAS_64BIT_ATOMICS
160 namespace js {
161 namespace jit {
162
163 template <>
164 inline int64_t AtomicOperations::loadSeqCst(int64_t* addr) {
165 MOZ_CRASH("No 64-bit atomics");
166 }
167
168 template <>
169 inline uint64_t AtomicOperations::loadSeqCst(uint64_t* addr) {
170 MOZ_CRASH("No 64-bit atomics");
171 }
172
173 } // namespace jit
174 } // namespace js
175 #endif
176
177 template <typename T>
178 inline void js::jit::AtomicOperations::storeSeqCst(T* addr, T val) {
179 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
180 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
181 __sync_synchronize();
182 *addr = val;
183 __sync_synchronize();
184 #else
185 __atomic_store(addr, &val, __ATOMIC_SEQ_CST);
186 #endif
187 }
188
189 #ifndef HAS_64BIT_ATOMICS
190 namespace js {
191 namespace jit {
192
193 template <>
194 inline void AtomicOperations::storeSeqCst(int64_t* addr, int64_t val) {
195 MOZ_CRASH("No 64-bit atomics");
196 }
197
198 template <>
199 inline void AtomicOperations::storeSeqCst(uint64_t* addr, uint64_t val) {
200 MOZ_CRASH("No 64-bit atomics");
201 }
202
203 } // namespace jit
204 } // namespace js
205 #endif
206
207 template <typename T>
208 inline T js::jit::AtomicOperations::exchangeSeqCst(T* addr, T val) {
209 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
210 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
211 T v;
212 __sync_synchronize();
213 do {
214 v = *addr;
215 } while (__sync_val_compare_and_swap(addr, v, val) != v);
216 return v;
217 #else
218 T v;
219 __atomic_exchange(addr, &val, &v, __ATOMIC_SEQ_CST);
220 return v;
221 #endif
222 }
223
224 #ifndef HAS_64BIT_ATOMICS
225 namespace js {
226 namespace jit {
227
228 template <>
229 inline int64_t AtomicOperations::exchangeSeqCst(int64_t* addr, int64_t val) {
230 MOZ_CRASH("No 64-bit atomics");
231 }
232
233 template <>
234 inline uint64_t AtomicOperations::exchangeSeqCst(uint64_t* addr, uint64_t val) {
235 MOZ_CRASH("No 64-bit atomics");
236 }
237
238 } // namespace jit
239 } // namespace js
240 #endif
241
242 template <typename T>
243 inline T js::jit::AtomicOperations::compareExchangeSeqCst(T* addr, T oldval,
244 T newval) {
245 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
246 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
247 return __sync_val_compare_and_swap(addr, oldval, newval);
248 #else
249 __atomic_compare_exchange(addr, &oldval, &newval, false, __ATOMIC_SEQ_CST,
250 __ATOMIC_SEQ_CST);
251 return oldval;
252 #endif
253 }
254
255 #ifndef HAS_64BIT_ATOMICS
256 namespace js {
257 namespace jit {
258
259 template <>
260 inline int64_t AtomicOperations::compareExchangeSeqCst(int64_t* addr,
261 int64_t oldval,
262 int64_t newval) {
263 MOZ_CRASH("No 64-bit atomics");
264 }
265
266 template <>
267 inline uint64_t AtomicOperations::compareExchangeSeqCst(uint64_t* addr,
268 uint64_t oldval,
269 uint64_t newval) {
270 MOZ_CRASH("No 64-bit atomics");
271 }
272
273 } // namespace jit
274 } // namespace js
275 #endif
276
277 template <typename T>
278 inline T js::jit::AtomicOperations::fetchAddSeqCst(T* addr, T val) {
279 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
280 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
281 return __sync_fetch_and_add(addr, val);
282 #else
283 return __atomic_fetch_add(addr, val, __ATOMIC_SEQ_CST);
284 #endif
285 }
286
287 #ifndef HAS_64BIT_ATOMICS
288 namespace js {
289 namespace jit {
290
291 template <>
292 inline int64_t AtomicOperations::fetchAddSeqCst(int64_t* addr, int64_t val) {
293 MOZ_CRASH("No 64-bit atomics");
294 }
295
296 template <>
297 inline uint64_t AtomicOperations::fetchAddSeqCst(uint64_t* addr, uint64_t val) {
298 MOZ_CRASH("No 64-bit atomics");
299 }
300
301 } // namespace jit
302 } // namespace js
303 #endif
304
305 template <typename T>
306 inline T js::jit::AtomicOperations::fetchSubSeqCst(T* addr, T val) {
307 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
308 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
309 return __sync_fetch_and_sub(addr, val);
310 #else
311 return __atomic_fetch_sub(addr, val, __ATOMIC_SEQ_CST);
312 #endif
313 }
314
315 #ifndef HAS_64BIT_ATOMICS
316 namespace js {
317 namespace jit {
318
319 template <>
320 inline int64_t AtomicOperations::fetchSubSeqCst(int64_t* addr, int64_t val) {
321 MOZ_CRASH("No 64-bit atomics");
322 }
323
324 template <>
325 inline uint64_t AtomicOperations::fetchSubSeqCst(uint64_t* addr, uint64_t val) {
326 MOZ_CRASH("No 64-bit atomics");
327 }
328
329 } // namespace jit
330 } // namespace js
331 #endif
332
333 template <typename T>
334 inline T js::jit::AtomicOperations::fetchAndSeqCst(T* addr, T val) {
335 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
336 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
337 return __sync_fetch_and_and(addr, val);
338 #else
339 return __atomic_fetch_and(addr, val, __ATOMIC_SEQ_CST);
340 #endif
341 }
342
343 #ifndef HAS_64BIT_ATOMICS
344 namespace js {
345 namespace jit {
346
347 template <>
348 inline int64_t AtomicOperations::fetchAndSeqCst(int64_t* addr, int64_t val) {
349 MOZ_CRASH("No 64-bit atomics");
350 }
351
352 template <>
353 inline uint64_t AtomicOperations::fetchAndSeqCst(uint64_t* addr, uint64_t val) {
354 MOZ_CRASH("No 64-bit atomics");
355 }
356
357 } // namespace jit
358 } // namespace js
359 #endif
360
361 template <typename T>
362 inline T js::jit::AtomicOperations::fetchOrSeqCst(T* addr, T val) {
363 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
364 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
365 return __sync_fetch_and_or(addr, val);
366 #else
367 return __atomic_fetch_or(addr, val, __ATOMIC_SEQ_CST);
368 #endif
369 }
370
371 #ifndef HAS_64BIT_ATOMICS
372 namespace js {
373 namespace jit {
374
375 template <>
376 inline int64_t AtomicOperations::fetchOrSeqCst(int64_t* addr, int64_t val) {
377 MOZ_CRASH("No 64-bit atomics");
378 }
379
380 template <>
381 inline uint64_t AtomicOperations::fetchOrSeqCst(uint64_t* addr, uint64_t val) {
382 MOZ_CRASH("No 64-bit atomics");
383 }
384
385 } // namespace jit
386 } // namespace js
387 #endif
388
389 template <typename T>
390 inline T js::jit::AtomicOperations::fetchXorSeqCst(T* addr, T val) {
391 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
392 #ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
393 return __sync_fetch_and_xor(addr, val);
394 #else
395 return __atomic_fetch_xor(addr, val, __ATOMIC_SEQ_CST);
396 #endif
397 }
398
399 #ifndef HAS_64BIT_ATOMICS
400 namespace js {
401 namespace jit {
402
403 template <>
404 inline int64_t AtomicOperations::fetchXorSeqCst(int64_t* addr, int64_t val) {
405 MOZ_CRASH("No 64-bit atomics");
406 }
407
408 template <>
409 inline uint64_t AtomicOperations::fetchXorSeqCst(uint64_t* addr, uint64_t val) {
410 MOZ_CRASH("No 64-bit atomics");
411 }
412
413 } // namespace jit
414 } // namespace js
415 #endif
416
417 template <typename T>
418 inline T js::jit::AtomicOperations::loadSafeWhenRacy(T* addr) {
419 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
420 // This is actually roughly right even on 32-bit platforms since in that
421 // case, double, int64, and uint64 loads need not be access-atomic.
422 //
423 // We could use __atomic_load, but it would be needlessly expensive on
424 // 32-bit platforms that could support it and just plain wrong on others.
425 return *addr;
426 }
427
428 template <typename T>
429 inline void js::jit::AtomicOperations::storeSafeWhenRacy(T* addr, T val) {
430 static_assert(sizeof(T) <= 8, "atomics supported up to 8 bytes only");
431 // This is actually roughly right even on 32-bit platforms since in that
432 // case, double, int64, and uint64 loads need not be access-atomic.
433 //
434 // We could use __atomic_store, but it would be needlessly expensive on
435 // 32-bit platforms that could support it and just plain wrong on others.
436 *addr = val;
437 }
438
439 inline void js::jit::AtomicOperations::memcpySafeWhenRacy(void* dest,
440 const void* src,
441 size_t nbytes) {
442 MOZ_ASSERT(!((char*)dest <= (char*)src && (char*)src < (char*)dest + nbytes));
443 MOZ_ASSERT(!((char*)src <= (char*)dest && (char*)dest < (char*)src + nbytes));
444 ::memcpy(dest, src, nbytes);
445 }
446
447 inline void js::jit::AtomicOperations::memmoveSafeWhenRacy(void* dest,
448 const void* src,
449 size_t nbytes) {
450 ::memmove(dest, src, nbytes);
451 }
452
453 #undef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
454 #undef HAS_64BIT_ATOMICS
455 #undef HAS_64BIT_LOCKFREE
456
457 #endif // jit_shared_AtomicOperations_feeling_lucky_gcc_h