1 /* 2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_HPP 26 #define OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_HPP 27 28 #include "runtime/os.hpp" 29 30 // The following alternative implementations are needed because 31 // Windows 95 doesn't support (some of) the corresponding Windows NT 32 // calls. Furthermore, these versions allow inlining in the caller. 33 // (More precisely: The documentation for InterlockedExchange says 34 // it is supported for Windows 95. However, when single-stepping 35 // through the assembly code we cannot step into the routine and 36 // when looking at the routine address we see only garbage code. 37 // Better safe then sorry!). Was bug 7/31/98 (gri). 38 // 39 // Performance note: On uniprocessors, the 'lock' prefixes are not 40 // necessary (and expensive). We should generate separate cases if 41 // this becomes a performance problem. 42 43 #pragma warning(disable: 4035) // Disables warnings reporting missing return statement 44 45 inline void Atomic::store (jbyte store_value, jbyte* dest) { *dest = store_value; } 46 inline void Atomic::store (jshort store_value, jshort* dest) { *dest = store_value; } 47 inline void Atomic::store (jint store_value, jint* dest) { *dest = store_value; } 48 49 inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; } 50 inline void Atomic::store_ptr(void* store_value, void* dest) { *(void**)dest = store_value; } 51 52 inline void Atomic::store (jbyte store_value, volatile jbyte* dest) { *dest = store_value; } 53 inline void Atomic::store (jshort store_value, volatile jshort* dest) { *dest = store_value; } 54 inline void Atomic::store (jint store_value, volatile jint* dest) { *dest = store_value; } 55 56 57 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; } 58 inline void Atomic::store_ptr(void* store_value, volatile void* dest) { *(void* volatile *)dest = store_value; } 59 60 template<size_t byte_size> 61 struct Atomic::PlatformAdd 62 : Atomic::AddAndFetch<Atomic::PlatformAdd<byte_size> > 63 { 64 template<typename I, typename D> 65 D add_and_fetch(I add_value, D volatile* dest) const; 66 }; 67 68 template<> 69 struct Atomic::PlatformAdd<2>: Atomic::AddShortUsingInt {}; 70 71 #ifdef AMD64 72 inline void Atomic::store (jlong store_value, jlong* dest) { *dest = store_value; } 73 inline void Atomic::store (jlong store_value, volatile jlong* dest) { *dest = store_value; } 74 75 template<> 76 template<typename I, typename D> 77 inline D Atomic::PlatformAdd<4>::add_and_fetch(I add_value, D volatile* dest) const { 78 return add_using_helper<jint>(os::atomic_add_func, add_value, dest); 79 } 80 81 template<> 82 template<typename I, typename D> 83 inline D Atomic::PlatformAdd<8>::add_and_fetch(I add_value, D volatile* dest) const { 84 return add_using_helper<intptr_t>(os::atomic_add_ptr_func, add_value, dest); 85 } 86 87 inline void Atomic::inc (volatile jint* dest) { 88 (void)add (1, dest); 89 } 90 91 inline void Atomic::inc_ptr(volatile intptr_t* dest) { 92 (void)add_ptr(1, dest); 93 } 94 95 inline void Atomic::inc_ptr(volatile void* dest) { 96 (void)add_ptr(1, dest); 97 } 98 99 inline void Atomic::dec (volatile jint* dest) { 100 (void)add (-1, dest); 101 } 102 103 inline void Atomic::dec_ptr(volatile intptr_t* dest) { 104 (void)add_ptr(-1, dest); 105 } 106 107 inline void Atomic::dec_ptr(volatile void* dest) { 108 (void)add_ptr(-1, dest); 109 } 110 111 inline jint Atomic::xchg (jint exchange_value, volatile jint* dest) { 112 return (jint)(*os::atomic_xchg_func)(exchange_value, dest); 113 } 114 115 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) { 116 return (intptr_t)(os::atomic_xchg_ptr_func)(exchange_value, dest); 117 } 118 119 inline void* Atomic::xchg_ptr(void* exchange_value, volatile void* dest) { 120 return (void *)(os::atomic_xchg_ptr_func)((intptr_t)exchange_value, (volatile intptr_t*)dest); 121 } 122 123 #define DEFINE_STUB_CMPXCHG(ByteSize, StubType, StubName) \ 124 template<> \ 125 template<typename T> \ 126 inline T Atomic::PlatformCmpxchg<ByteSize>::operator()(T exchange_value, \ 127 T volatile* dest, \ 128 T compare_value, \ 129 cmpxchg_memory_order order) const { \ 130 STATIC_ASSERT(ByteSize == sizeof(T)); \ 131 return cmpxchg_using_helper<StubType>(StubName, exchange_value, dest, compare_value); \ 132 } 133 134 DEFINE_STUB_CMPXCHG(1, jbyte, os::atomic_cmpxchg_byte_func) 135 DEFINE_STUB_CMPXCHG(4, jint, os::atomic_cmpxchg_func) 136 DEFINE_STUB_CMPXCHG(8, jlong, os::atomic_cmpxchg_long_func) 137 138 #undef DEFINE_STUB_CMPXCHG 139 140 inline jlong Atomic::load(const volatile jlong* src) { return *src; } 141 142 #else // !AMD64 143 144 template<> 145 template<typename I, typename D> 146 inline D Atomic::PlatformAdd<4>::add_and_fetch(I add_value, D volatile* dest) const { 147 STATIC_ASSERT(4 == sizeof(I)); 148 STATIC_ASSERT(4 == sizeof(D)); 149 __asm { 150 mov edx, dest; 151 mov eax, add_value; 152 mov ecx, eax; 153 lock xadd dword ptr [edx], eax; 154 add eax, ecx; 155 } 156 } 157 158 inline void Atomic::inc (volatile jint* dest) { 159 // alternative for InterlockedIncrement 160 __asm { 161 mov edx, dest; 162 lock add dword ptr [edx], 1; 163 } 164 } 165 166 inline void Atomic::inc_ptr(volatile intptr_t* dest) { 167 inc((volatile jint*)dest); 168 } 169 170 inline void Atomic::inc_ptr(volatile void* dest) { 171 inc((volatile jint*)dest); 172 } 173 174 inline void Atomic::dec (volatile jint* dest) { 175 // alternative for InterlockedDecrement 176 __asm { 177 mov edx, dest; 178 lock sub dword ptr [edx], 1; 179 } 180 } 181 182 inline void Atomic::dec_ptr(volatile intptr_t* dest) { 183 dec((volatile jint*)dest); 184 } 185 186 inline void Atomic::dec_ptr(volatile void* dest) { 187 dec((volatile jint*)dest); 188 } 189 190 inline jint Atomic::xchg (jint exchange_value, volatile jint* dest) { 191 // alternative for InterlockedExchange 192 __asm { 193 mov eax, exchange_value; 194 mov ecx, dest; 195 xchg eax, dword ptr [ecx]; 196 } 197 } 198 199 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) { 200 return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest); 201 } 202 203 inline void* Atomic::xchg_ptr(void* exchange_value, volatile void* dest) { 204 return (void*)xchg((jint)exchange_value, (volatile jint*)dest); 205 } 206 207 template<> 208 template<typename T> 209 inline T Atomic::PlatformCmpxchg<1>::operator()(T exchange_value, 210 T volatile* dest, 211 T compare_value, 212 cmpxchg_memory_order order) const { 213 STATIC_ASSERT(1 == sizeof(T)); 214 // alternative for InterlockedCompareExchange 215 __asm { 216 mov edx, dest 217 mov cl, exchange_value 218 mov al, compare_value 219 lock cmpxchg byte ptr [edx], cl 220 } 221 } 222 223 template<> 224 template<typename T> 225 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value, 226 T volatile* dest, 227 T compare_value, 228 cmpxchg_memory_order order) const { 229 STATIC_ASSERT(4 == sizeof(T)); 230 // alternative for InterlockedCompareExchange 231 __asm { 232 mov edx, dest 233 mov ecx, exchange_value 234 mov eax, compare_value 235 lock cmpxchg dword ptr [edx], ecx 236 } 237 } 238 239 template<> 240 template<typename T> 241 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value, 242 T volatile* dest, 243 T compare_value, 244 cmpxchg_memory_order order) const { 245 STATIC_ASSERT(8 == sizeof(T)); 246 jint ex_lo = (jint)exchange_value; 247 jint ex_hi = *( ((jint*)&exchange_value) + 1 ); 248 jint cmp_lo = (jint)compare_value; 249 jint cmp_hi = *( ((jint*)&compare_value) + 1 ); 250 __asm { 251 push ebx 252 push edi 253 mov eax, cmp_lo 254 mov edx, cmp_hi 255 mov edi, dest 256 mov ebx, ex_lo 257 mov ecx, ex_hi 258 lock cmpxchg8b qword ptr [edi] 259 pop edi 260 pop ebx 261 } 262 } 263 264 inline jlong Atomic::load(const volatile jlong* src) { 265 volatile jlong dest; 266 volatile jlong* pdest = &dest; 267 __asm { 268 mov eax, src 269 fild qword ptr [eax] 270 mov eax, pdest 271 fistp qword ptr [eax] 272 } 273 return dest; 274 } 275 276 inline void Atomic::store(jlong store_value, volatile jlong* dest) { 277 volatile jlong* src = &store_value; 278 __asm { 279 mov eax, src 280 fild qword ptr [eax] 281 mov eax, dest 282 fistp qword ptr [eax] 283 } 284 } 285 286 inline void Atomic::store(jlong store_value, jlong* dest) { 287 Atomic::store(store_value, (volatile jlong*)dest); 288 } 289 290 #endif // AMD64 291 292 #pragma warning(default: 4035) // Enables warnings reporting missing return statement 293 294 #endif // OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_HPP