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 #ifdef AMD64
  61 inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
  62 inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
  63 
  64 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
  65   return (jint)(*os::atomic_add_func)(add_value, dest);
  66 }
  67 
  68 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
  69   return (intptr_t)(*os::atomic_add_ptr_func)(add_value, dest);
  70 }
  71 
  72 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
  73   return (void*)(*os::atomic_add_ptr_func)(add_value, (volatile intptr_t*)dest);
  74 }
  75 
  76 inline void Atomic::inc    (volatile jint*     dest) {
  77   (void)add    (1, dest);
  78 }
  79 
  80 inline void Atomic::inc_ptr(volatile intptr_t* dest) {
  81   (void)add_ptr(1, dest);
  82 }
  83 
  84 inline void Atomic::inc_ptr(volatile void*     dest) {
  85   (void)add_ptr(1, dest);
  86 }
  87 
  88 inline void Atomic::dec    (volatile jint*     dest) {
  89   (void)add    (-1, dest);
  90 }
  91 
  92 inline void Atomic::dec_ptr(volatile intptr_t* dest) {
  93   (void)add_ptr(-1, dest);
  94 }
  95 
  96 inline void Atomic::dec_ptr(volatile void*     dest) {
  97   (void)add_ptr(-1, dest);
  98 }
  99 
 100 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 101   return (jint)(*os::atomic_xchg_func)(exchange_value, dest);
 102 }
 103 
 104 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 105   return (intptr_t)(os::atomic_xchg_ptr_func)(exchange_value, dest);
 106 }
 107 
 108 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 109   return (void *)(os::atomic_xchg_ptr_func)((intptr_t)exchange_value, (volatile intptr_t*)dest);
 110 }
 111 
 112 #define DEFINE_STUB_CMPXCHG(ByteSize, StubType, StubName)               \
 113   template<>                                                            \
 114   template<typename T>                                                  \
 115   inline T Atomic::PlatformCmpxchg<ByteSize>::operator()(T exchange_value, \
 116                                                          T volatile* dest, \
 117                                                          T compare_value, \
 118                                                          cmpxchg_memory_order order) const { \
 119     STATIC_ASSERT(ByteSize == sizeof(T));                               \
 120     return cmpxchg_using_helper<StubType>(StubName, exchange_value, dest, compare_value); \
 121   }
 122 
 123 DEFINE_STUB_CMPXCHG(1, jbyte, os::atomic_cmpxchg_byte_func)
 124 DEFINE_STUB_CMPXCHG(4, jint,  os::atomic_cmpxchg_func)
 125 DEFINE_STUB_CMPXCHG(8, jlong, os::atomic_cmpxchg_long_func)
 126 
 127 #undef DEFINE_STUB_CMPXCHG
 128 
 129 inline jlong Atomic::load(const volatile jlong* src) { return *src; }
 130 
 131 #else // !AMD64
 132 
 133 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
 134   __asm {
 135     mov edx, dest;
 136     mov eax, add_value;
 137     mov ecx, eax;
 138     lock xadd dword ptr [edx], eax;
 139     add eax, ecx;
 140   }
 141 }
 142 
 143 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 144   return (intptr_t)add((jint)add_value, (volatile jint*)dest);
 145 }
 146 
 147 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 148   return (void*)add((jint)add_value, (volatile jint*)dest);
 149 }
 150 
 151 inline void Atomic::inc    (volatile jint*     dest) {
 152   // alternative for InterlockedIncrement
 153   __asm {
 154     mov edx, dest;
 155     lock add dword ptr [edx], 1;
 156   }
 157 }
 158 
 159 inline void Atomic::inc_ptr(volatile intptr_t* dest) {
 160   inc((volatile jint*)dest);
 161 }
 162 
 163 inline void Atomic::inc_ptr(volatile void*     dest) {
 164   inc((volatile jint*)dest);
 165 }
 166 
 167 inline void Atomic::dec    (volatile jint*     dest) {
 168   // alternative for InterlockedDecrement
 169   __asm {
 170     mov edx, dest;
 171     lock sub dword ptr [edx], 1;
 172   }
 173 }
 174 
 175 inline void Atomic::dec_ptr(volatile intptr_t* dest) {
 176   dec((volatile jint*)dest);
 177 }
 178 
 179 inline void Atomic::dec_ptr(volatile void*     dest) {
 180   dec((volatile jint*)dest);
 181 }
 182 
 183 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 184   // alternative for InterlockedExchange
 185   __asm {
 186     mov eax, exchange_value;
 187     mov ecx, dest;
 188     xchg eax, dword ptr [ecx];
 189   }
 190 }
 191 
 192 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 193   return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest);
 194 }
 195 
 196 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 197   return (void*)xchg((jint)exchange_value, (volatile jint*)dest);
 198 }
 199 
 200 template<>
 201 template<typename T>
 202 inline T Atomic::PlatformCmpxchg<1>::operator()(T exchange_value,
 203                                                 T volatile* dest,
 204                                                 T compare_value,
 205                                                 cmpxchg_memory_order order) const {
 206   STATIC_ASSERT(1 == sizeof(T));
 207   // alternative for InterlockedCompareExchange
 208   __asm {
 209     mov edx, dest
 210     mov cl, exchange_value
 211     mov al, compare_value
 212     lock cmpxchg byte ptr [edx], cl
 213   }
 214 }
 215 
 216 template<>
 217 template<typename T>
 218 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
 219                                                 T volatile* dest,
 220                                                 T compare_value,
 221                                                 cmpxchg_memory_order order) const {
 222   STATIC_ASSERT(4 == sizeof(T));
 223   // alternative for InterlockedCompareExchange
 224   __asm {
 225     mov edx, dest
 226     mov ecx, exchange_value
 227     mov eax, compare_value
 228     lock cmpxchg dword ptr [edx], ecx
 229   }
 230 }
 231 
 232 template<>
 233 template<typename T>
 234 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
 235                                                 T volatile* dest,
 236                                                 T compare_value,
 237                                                 cmpxchg_memory_order order) const {
 238   STATIC_ASSERT(8 == sizeof(T));
 239   jint ex_lo  = (jint)exchange_value;
 240   jint ex_hi  = *( ((jint*)&exchange_value) + 1 );
 241   jint cmp_lo = (jint)compare_value;
 242   jint cmp_hi = *( ((jint*)&compare_value) + 1 );
 243   __asm {
 244     push ebx
 245     push edi
 246     mov eax, cmp_lo
 247     mov edx, cmp_hi
 248     mov edi, dest
 249     mov ebx, ex_lo
 250     mov ecx, ex_hi
 251     lock cmpxchg8b qword ptr [edi]
 252     pop edi
 253     pop ebx
 254   }
 255 }
 256 
 257 inline jlong Atomic::load(const volatile jlong* src) {
 258   volatile jlong dest;
 259   volatile jlong* pdest = &dest;
 260   __asm {
 261     mov eax, src
 262     fild     qword ptr [eax]
 263     mov eax, pdest
 264     fistp    qword ptr [eax]
 265   }
 266   return dest;
 267 }
 268 
 269 inline void Atomic::store(jlong store_value, volatile jlong* dest) {
 270   volatile jlong* src = &store_value;
 271   __asm {
 272     mov eax, src
 273     fild     qword ptr [eax]
 274     mov eax, dest
 275     fistp    qword ptr [eax]
 276   }
 277 }
 278 
 279 inline void Atomic::store(jlong store_value, jlong* dest) {
 280   Atomic::store(store_value, (volatile jlong*)dest);
 281 }
 282 
 283 #endif // AMD64
 284 
 285 #pragma warning(default: 4035) // Enables warnings reporting missing return statement
 286 
 287 #endif // OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_HPP