1 /*
   2  * Copyright (c) 1999, 2013, 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_INLINE_HPP
  26 #define OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_INLINE_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/os.hpp"
  30 #include "vm_version_x86.hpp"
  31 
  32 // The following alternative implementations are needed because
  33 // Windows 95 doesn't support (some of) the corresponding Windows NT
  34 // calls. Furthermore, these versions allow inlining in the caller.
  35 // (More precisely: The documentation for InterlockedExchange says
  36 // it is supported for Windows 95. However, when single-stepping
  37 // through the assembly code we cannot step into the routine and
  38 // when looking at the routine address we see only garbage code.
  39 // Better safe then sorry!). Was bug 7/31/98 (gri).
  40 //
  41 // Performance note: On uniprocessors, the 'lock' prefixes are not
  42 // necessary (and expensive). We should generate separate cases if
  43 // this becomes a performance problem.
  44 
  45 #pragma warning(disable: 4035) // Disables warnings reporting missing return statement
  46 
  47 inline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
  48 inline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
  49 inline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
  50 
  51 inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
  52 inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
  53 
  54 inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
  55 inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
  56 inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
  57 
  58 
  59 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
  60 inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
  61 
  62 // Adding a lock prefix to an instruction on MP machine
  63 // VC++ doesn't like the lock prefix to be on a single line
  64 // so we can't insert a label after the lock prefix.
  65 // By emitting a lock prefix, we can define a label after it.
  66 #define LOCK_IF_MP(mp) __asm cmp mp, 0  \
  67                        __asm je L0      \
  68                        __asm _emit 0xF0 \
  69                        __asm L0:
  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 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
  76   return (jint)(*os::atomic_add_func)(add_value, dest);
  77 }
  78 
  79 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
  80   return (intptr_t)(*os::atomic_add_ptr_func)(add_value, dest);
  81 }
  82 
  83 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
  84   return (void*)(*os::atomic_add_ptr_func)(add_value, (volatile intptr_t*)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 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 124   return (*os::atomic_cmpxchg_func)(exchange_value, dest, compare_value);
 125 }
 126 
 127 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 128   return (*os::atomic_cmpxchg_long_func)(exchange_value, dest, compare_value);
 129 }
 130 
 131 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 132   return (intptr_t)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value);
 133 }
 134 
 135 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 136   return (void*)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value);
 137 }
 138 
 139 inline jlong Atomic::load(volatile jlong* src) { return *src; }
 140 
 141 #else // !AMD64
 142 
 143 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
 144   int mp = os::is_MP();
 145   __asm {
 146     mov edx, dest;
 147     mov eax, add_value;
 148     mov ecx, eax;
 149     LOCK_IF_MP(mp)
 150     xadd dword ptr [edx], eax;
 151     add eax, ecx;
 152   }
 153 }
 154 
 155 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 156   return (intptr_t)add((jint)add_value, (volatile jint*)dest);
 157 }
 158 
 159 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 160   return (void*)add((jint)add_value, (volatile jint*)dest);
 161 }
 162 
 163 inline void Atomic::inc    (volatile jint*     dest) {
 164   // alternative for InterlockedIncrement
 165   int mp = os::is_MP();
 166   __asm {
 167     mov edx, dest;
 168     LOCK_IF_MP(mp)
 169     add dword ptr [edx], 1;
 170   }
 171 }
 172 
 173 inline void Atomic::inc_ptr(volatile intptr_t* dest) {
 174   inc((volatile jint*)dest);
 175 }
 176 
 177 inline void Atomic::inc_ptr(volatile void*     dest) {
 178   inc((volatile jint*)dest);
 179 }
 180 
 181 inline void Atomic::dec    (volatile jint*     dest) {
 182   // alternative for InterlockedDecrement
 183   int mp = os::is_MP();
 184   __asm {
 185     mov edx, dest;
 186     LOCK_IF_MP(mp)
 187     sub dword ptr [edx], 1;
 188   }
 189 }
 190 
 191 inline void Atomic::dec_ptr(volatile intptr_t* dest) {
 192   dec((volatile jint*)dest);
 193 }
 194 
 195 inline void Atomic::dec_ptr(volatile void*     dest) {
 196   dec((volatile jint*)dest);
 197 }
 198 
 199 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 200   // alternative for InterlockedExchange
 201   __asm {
 202     mov eax, exchange_value;
 203     mov ecx, dest;
 204     xchg eax, dword ptr [ecx];
 205   }
 206 }
 207 
 208 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 209   return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest);
 210 }
 211 
 212 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 213   return (void*)xchg((jint)exchange_value, (volatile jint*)dest);
 214 }
 215 
 216 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 217   // alternative for InterlockedCompareExchange
 218   int mp = os::is_MP();
 219   __asm {
 220     mov edx, dest
 221     mov ecx, exchange_value
 222     mov eax, compare_value
 223     LOCK_IF_MP(mp)
 224     cmpxchg dword ptr [edx], ecx
 225   }
 226 }
 227 
 228 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 229   int mp = os::is_MP();
 230   jint ex_lo  = (jint)exchange_value;
 231   jint ex_hi  = *( ((jint*)&exchange_value) + 1 );
 232   jint cmp_lo = (jint)compare_value;
 233   jint cmp_hi = *( ((jint*)&compare_value) + 1 );
 234   __asm {
 235     push ebx
 236     push edi
 237     mov eax, cmp_lo
 238     mov edx, cmp_hi
 239     mov edi, dest
 240     mov ebx, ex_lo
 241     mov ecx, ex_hi
 242     LOCK_IF_MP(mp)
 243     cmpxchg8b qword ptr [edi]
 244     pop edi
 245     pop ebx
 246   }
 247 }
 248 
 249 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 250   return (intptr_t)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 251 }
 252 
 253 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 254   return (void*)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 255 }
 256 
 257 inline jlong Atomic::load(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_INLINE_HPP