| 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 |
#ifndef jit_AtomicOperations_h |
| 8 |
#define jit_AtomicOperations_h |
| 9 |
|
| 10 |
#include "mozilla/Types.h" |
| 11 |
|
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "jit/AtomicOperationsGenerated.h" |
| 15 |
#include "vm/SharedMem.h" |
| 16 |
|
| 17 |
namespace js { |
| 18 |
namespace jit { |
| 19 |
|
| 20 |
/* |
| 21 |
* [SMDOC] Atomic Operations |
| 22 |
* |
| 23 |
* The atomic operations layer defines types and functions for |
| 24 |
* JIT-compatible atomic operation. |
| 25 |
* |
| 26 |
* The fundamental constraints on the functions are: |
| 27 |
* |
| 28 |
* - That their realization here MUST be compatible with code the JIT |
| 29 |
* generates for its Atomics operations, so that an atomic access |
| 30 |
* from the interpreter or runtime - from any C++ code - really is |
| 31 |
* atomic relative to a concurrent, compatible atomic access from |
| 32 |
* jitted code. That is, these primitives expose JIT-compatible |
| 33 |
* atomicity functionality to C++. |
| 34 |
* |
| 35 |
* - That accesses may race without creating C++ undefined behavior: |
| 36 |
* atomic accesses (marked "SeqCst") may race with non-atomic |
| 37 |
* accesses (marked "SafeWhenRacy"); overlapping but non-matching, |
| 38 |
* and hence incompatible, atomic accesses may race; and non-atomic |
| 39 |
* accesses may race. The effects of races need not be predictable, |
| 40 |
* so garbage can be produced by a read or written by a write, but |
| 41 |
* the effects must be benign: the program must continue to run, and |
| 42 |
* only the memory in the union of addresses named in the racing |
| 43 |
* accesses may be affected. |
| 44 |
* |
| 45 |
* The compatibility constraint means that if the JIT makes dynamic |
| 46 |
* decisions about how to implement atomic operations then |
| 47 |
* corresponding dynamic decisions MUST be made in the implementations |
| 48 |
* of the functions below. |
| 49 |
* |
| 50 |
* The safe-for-races constraint means that by and large, it is hard |
| 51 |
* to implement these primitives in C++. See "Implementation notes" |
| 52 |
* below. |
| 53 |
* |
| 54 |
* The "SeqCst" suffix on operations means "sequentially consistent" |
| 55 |
* and means such a function's operation must have "sequentially |
| 56 |
* consistent" memory ordering. See mfbt/Atomics.h for an explanation |
| 57 |
* of this memory ordering. |
| 58 |
* |
| 59 |
* Note that a "SafeWhenRacy" access does not provide the atomicity of |
| 60 |
* a "relaxed atomic" access: it can read or write garbage if there's |
| 61 |
* a race. |
| 62 |
* |
| 63 |
* |
| 64 |
* Implementation notes. |
| 65 |
* |
| 66 |
* It's not a requirement that these functions be inlined; performance |
| 67 |
* is not a great concern. On some platforms these functions may call |
| 68 |
* functions that use inline assembly. See GenerateAtomicOperations.py. |
| 69 |
* |
| 70 |
* In principle these functions will not be written in C++, thus |
| 71 |
* making races defined behavior if all racy accesses from C++ go via |
| 72 |
* these functions. (Jitted code will always be safe for races and |
| 73 |
* provides the same guarantees as these functions.) |
| 74 |
* |
| 75 |
* The appropriate implementations will be platform-specific and |
| 76 |
* there are some obvious implementation strategies to choose |
| 77 |
* from, sometimes a combination is appropriate: |
| 78 |
* |
| 79 |
* - generating the code at run-time with the JIT; |
| 80 |
* - hand-written assembler (maybe inline); or |
| 81 |
* - using special compiler intrinsics or directives. |
| 82 |
* |
| 83 |
* Trusting the compiler not to generate code that blows up on a |
| 84 |
* race definitely won't work in the presence of TSan, or even of |
| 85 |
* optimizing compilers in seemingly-"innocuous" conditions. (See |
| 86 |
* https://www.usenix.org/legacy/event/hotpar11/tech/final_files/Boehm.pdf |
| 87 |
* for details.) |
| 88 |
*/ |
| 89 |
class AtomicOperations { |
| 90 |
// The following functions are defined for T = int8_t, uint8_t, |
| 91 |
// int16_t, uint16_t, int32_t, uint32_t, int64_t, and uint64_t. |
| 92 |
|
| 93 |
// Atomically read *addr. |
| 94 |
template <typename T> |
| 95 |
static inline T loadSeqCst(T* addr); |
| 96 |
|
| 97 |
// Atomically store val in *addr. |
| 98 |
template <typename T> |
| 99 |
static inline void storeSeqCst(T* addr, T val); |
| 100 |
|
| 101 |
// Atomically store val in *addr and return the old value of *addr. |
| 102 |
template <typename T> |
| 103 |
static inline T exchangeSeqCst(T* addr, T val); |
| 104 |
|
| 105 |
// Atomically check that *addr contains oldval and if so replace it |
| 106 |
// with newval, in any case returning the old contents of *addr. |
| 107 |
template <typename T> |
| 108 |
static inline T compareExchangeSeqCst(T* addr, T oldval, T newval); |
| 109 |
|
| 110 |
// Atomically add, subtract, bitwise-AND, bitwise-OR, or bitwise-XOR |
| 111 |
// val into *addr and return the old value of *addr. |
| 112 |
template <typename T> |
| 113 |
static inline T fetchAddSeqCst(T* addr, T val); |
| 114 |
|
| 115 |
template <typename T> |
| 116 |
static inline T fetchSubSeqCst(T* addr, T val); |
| 117 |
|
| 118 |
template <typename T> |
| 119 |
static inline T fetchAndSeqCst(T* addr, T val); |
| 120 |
|
| 121 |
template <typename T> |
| 122 |
static inline T fetchOrSeqCst(T* addr, T val); |
| 123 |
|
| 124 |
template <typename T> |
| 125 |
static inline T fetchXorSeqCst(T* addr, T val); |
| 126 |
|
| 127 |
// The SafeWhenRacy functions are to be used when C++ code has to access |
| 128 |
// memory without synchronization and can't guarantee that there won't be a |
| 129 |
// race on the access. But they are access-atomic for integer data so long |
| 130 |
// as any racing writes are of the same size and to the same address. |
| 131 |
|
| 132 |
// Defined for all the integral types as well as for float32 and float64, |
| 133 |
// but not access-atomic for floats, nor for int64 and uint64 on 32-bit |
| 134 |
// platforms. |
| 135 |
template <typename T> |
| 136 |
static inline T loadSafeWhenRacy(T* addr); |
| 137 |
|
| 138 |
// Defined for all the integral types as well as for float32 and float64, |
| 139 |
// but not access-atomic for floats, nor for int64 and uint64 on 32-bit |
| 140 |
// platforms. |
| 141 |
template <typename T> |
| 142 |
static inline void storeSafeWhenRacy(T* addr, T val); |
| 143 |
|
| 144 |
// Replacement for memcpy(). No access-atomicity guarantees. |
| 145 |
static inline void memcpySafeWhenRacy(void* dest, const void* src, |
| 146 |
size_t nbytes); |
| 147 |
|
| 148 |
// Replacement for memmove(). No access-atomicity guarantees. |
| 149 |
static inline void memmoveSafeWhenRacy(void* dest, const void* src, |
| 150 |
size_t nbytes); |
| 151 |
|
| 152 |
public: |
| 153 |
// Test lock-freedom for any int32 value. This implements the |
| 154 |
// Atomics::isLockFree() operation in the ECMAScript Shared Memory and |
| 155 |
// Atomics specification, as follows: |
| 156 |
// |
| 157 |
// 4-byte accesses are always lock free (in the spec). |
| 158 |
// 1-, 2-, and 8-byte accesses are always lock free (in SpiderMonkey). |
| 159 |
// |
| 160 |
// There is no lock-freedom for JS for any other values on any platform. |
| 161 |
static constexpr inline bool isLockfreeJS(int32_t n); |
| 162 |
|
| 163 |
// If the return value is true then the templated functions below are |
| 164 |
// supported for int64_t and uint64_t. If the return value is false then |
| 165 |
// those functions will MOZ_CRASH. The value of this call does not change |
| 166 |
// during execution. |
| 167 |
static inline bool hasAtomic8(); |
| 168 |
|
| 169 |
// If the return value is true then hasAtomic8() is true and the atomic |
| 170 |
// operations are indeed lock-free. The value of this call does not change |
| 171 |
// during execution. |
| 172 |
static inline bool isLockfree8(); |
| 173 |
|
| 174 |
// Execute a full memory barrier (LoadLoad+LoadStore+StoreLoad+StoreStore). |
| 175 |
static inline void fenceSeqCst(); |
| 176 |
|
| 177 |
// Pause or yield instruction. |
| 178 |
static inline void pause(); |
| 179 |
|
| 180 |
// All clients should use the APIs that take SharedMem pointers. |
| 181 |
// See above for semantics and acceptable types. |
| 182 |
|
| 183 |
template <typename T> |
| 184 |
static T loadSeqCst(SharedMem<T*> addr) { |
| 185 |
return loadSeqCst(addr.unwrap()); |
| 186 |
} |
| 187 |
|
| 188 |
template <typename T> |
| 189 |
static void storeSeqCst(SharedMem<T*> addr, T val) { |
| 190 |
return storeSeqCst(addr.unwrap(), val); |
| 191 |
} |
| 192 |
|
| 193 |
template <typename T> |
| 194 |
static T exchangeSeqCst(SharedMem<T*> addr, T val) { |
| 195 |
return exchangeSeqCst(addr.unwrap(), val); |
| 196 |
} |
| 197 |
|
| 198 |
template <typename T> |
| 199 |
static T compareExchangeSeqCst(SharedMem<T*> addr, T oldval, T newval) { |
| 200 |
return compareExchangeSeqCst(addr.unwrap(), oldval, newval); |
| 201 |
} |
| 202 |
|
| 203 |
template <typename T> |
| 204 |
static T fetchAddSeqCst(SharedMem<T*> addr, T val) { |
| 205 |
return fetchAddSeqCst(addr.unwrap(), val); |
| 206 |
} |
| 207 |
|
| 208 |
template <typename T> |
| 209 |
static T fetchSubSeqCst(SharedMem<T*> addr, T val) { |
| 210 |
return fetchSubSeqCst(addr.unwrap(), val); |
| 211 |
} |
| 212 |
|
| 213 |
template <typename T> |
| 214 |
static T fetchAndSeqCst(SharedMem<T*> addr, T val) { |
| 215 |
return fetchAndSeqCst(addr.unwrap(), val); |
| 216 |
} |
| 217 |
|
| 218 |
template <typename T> |
| 219 |
static T fetchOrSeqCst(SharedMem<T*> addr, T val) { |
| 220 |
return fetchOrSeqCst(addr.unwrap(), val); |
| 221 |
} |
| 222 |
|
| 223 |
template <typename T> |
| 224 |
static T fetchXorSeqCst(SharedMem<T*> addr, T val) { |
| 225 |
return fetchXorSeqCst(addr.unwrap(), val); |
| 226 |
} |
| 227 |
|
| 228 |
template <typename T> |
| 229 |
static T loadSafeWhenRacy(SharedMem<T*> addr) { |
| 230 |
return loadSafeWhenRacy(addr.unwrap()); |
| 231 |
} |
| 232 |
|
| 233 |
template <typename T> |
| 234 |
static void storeSafeWhenRacy(SharedMem<T*> addr, T val) { |
| 235 |
return storeSafeWhenRacy(addr.unwrap(), val); |
| 236 |
} |
| 237 |
|
| 238 |
template <typename T> |
| 239 |
static void memcpySafeWhenRacy(SharedMem<T*> dest, SharedMem<T*> src, |
| 240 |
size_t nbytes) { |
| 241 |
memcpySafeWhenRacy(dest.template cast<void*>().unwrap(), |
| 242 |
src.template cast<void*>().unwrap(), nbytes); |
| 243 |
} |
| 244 |
|
| 245 |
template <typename T> |
| 246 |
static void memcpySafeWhenRacy(SharedMem<T*> dest, T* src, size_t nbytes) { |
| 247 |
memcpySafeWhenRacy(dest.template cast<void*>().unwrap(), |
| 248 |
static_cast<void*>(src), nbytes); |
| 249 |
} |
| 250 |
|
| 251 |
template <typename T> |
| 252 |
static void memcpySafeWhenRacy(T* dest, SharedMem<T*> src, size_t nbytes) { |
| 253 |
memcpySafeWhenRacy(static_cast<void*>(dest), |
| 254 |
src.template cast<void*>().unwrap(), nbytes); |
| 255 |
} |
| 256 |
|
| 257 |
template <typename T> |
| 258 |
static void memmoveSafeWhenRacy(SharedMem<T*> dest, SharedMem<T*> src, |
| 259 |
size_t nbytes) { |
| 260 |
memmoveSafeWhenRacy(dest.template cast<void*>().unwrap(), |
| 261 |
src.template cast<void*>().unwrap(), nbytes); |
| 262 |
} |
| 263 |
|
| 264 |
static void memsetSafeWhenRacy(SharedMem<uint8_t*> dest, int value, |
| 265 |
size_t nbytes) { |
| 266 |
uint8_t buf[1024]; |
| 267 |
size_t iterations = nbytes / sizeof(buf); |
| 268 |
size_t tail = nbytes % sizeof(buf); |
| 269 |
size_t offs = 0; |
| 270 |
if (iterations > 0) { |
| 271 |
memset(buf, value, sizeof(buf)); |
| 272 |
while (iterations--) { |
| 273 |
memcpySafeWhenRacy(dest + offs, SharedMem<uint8_t*>::unshared(buf), |
| 274 |
sizeof(buf)); |
| 275 |
offs += sizeof(buf); |
| 276 |
} |
| 277 |
} else { |
| 278 |
memset(buf, value, tail); |
| 279 |
} |
| 280 |
memcpySafeWhenRacy(dest + offs, SharedMem<uint8_t*>::unshared(buf), tail); |
| 281 |
} |
| 282 |
|
| 283 |
template <typename T> |
| 284 |
static void podCopySafeWhenRacy(SharedMem<T*> dest, SharedMem<T*> src, |
| 285 |
size_t nelem) { |
| 286 |
memcpySafeWhenRacy(dest, src, nelem * sizeof(T)); |
| 287 |
} |
| 288 |
|
| 289 |
template <typename T> |
| 290 |
static void podMoveSafeWhenRacy(SharedMem<T*> dest, SharedMem<T*> src, |
| 291 |
size_t nelem) { |
| 292 |
memmoveSafeWhenRacy(dest, src, nelem * sizeof(T)); |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
constexpr inline bool AtomicOperations::isLockfreeJS(int32_t size) { |
| 297 |
// Keep this in sync with atomicIsLockFreeJS() in jit/MacroAssembler.cpp. |
| 298 |
|
| 299 |
switch (size) { |
| 300 |
case 1: |
| 301 |
return true; |
| 302 |
case 2: |
| 303 |
return true; |
| 304 |
case 4: |
| 305 |
// The spec requires Atomics.isLockFree(4) to return true. |
| 306 |
return true; |
| 307 |
case 8: |
| 308 |
return true; |
| 309 |
default: |
| 310 |
return false; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
} // namespace jit |
| 315 |
} // namespace js |
| 316 |
|
| 317 |
// As explained above, our atomic operations are not portable even in principle, |
| 318 |
// so we must include platform+compiler specific definitions here. |
| 319 |
// |
| 320 |
// x86, x64, arm, and arm64 are maintained by Mozilla. All other platform |
| 321 |
// setups are by platform maintainers' request and are not maintained by |
| 322 |
// Mozilla. |
| 323 |
// |
| 324 |
// If you are using a platform+compiler combination that causes an error below |
| 325 |
// (and if the problem isn't just that the compiler uses a different name for a |
| 326 |
// known architecture), you have basically three options: |
| 327 |
// |
| 328 |
// - find an already-supported compiler for the platform and use that instead |
| 329 |
// |
| 330 |
// - write your own support code for the platform+compiler and create a new |
| 331 |
// case below |
| 332 |
// |
| 333 |
// - include jit/shared/AtomicOperations-feeling-lucky.h in a case for the |
| 334 |
// platform below, if you have a gcc-compatible compiler and truly feel |
| 335 |
// lucky. You may have to add a little code to that file, too. |
| 336 |
// |
| 337 |
// Simulators are confusing. These atomic primitives must be compatible with |
| 338 |
// the code that the JIT emits, but of course for an ARM simulator running on |
| 339 |
// x86 the primitives here will be for x86, not for ARM, while the JIT emits ARM |
| 340 |
// code. Our ARM simulator solves that the easy way: by using these primitives |
| 341 |
// to implement its atomic operations. For other simulators there may need to |
| 342 |
// be special cases below to provide simulator-compatible primitives, for |
| 343 |
// example, for our ARM64 simulator the primitives could in principle |
| 344 |
// participate in the memory exclusivity monitors implemented by the simulator. |
| 345 |
// Such a solution is likely to be difficult. |
| 346 |
|
| 347 |
#ifdef JS_HAVE_GENERATED_ATOMIC_OPS |
| 348 |
# include "jit/shared/AtomicOperations-shared-jit.h" |
| 349 |
#elif defined(__mips__) |
| 350 |
# include "jit/mips-shared/AtomicOperations-mips-shared.h" |
| 351 |
#else |
| 352 |
# include "jit/shared/AtomicOperations-feeling-lucky.h" |
| 353 |
#endif |
| 354 |
|
| 355 |
#endif // jit_AtomicOperations_h |