1 /*
   2  * Copyright (c) 1999, 2009, 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 // Implementation of class atomic
  26 
  27 inline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
  28 inline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
  29 inline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
  30 inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
  31 inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
  32 inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
  33 
  34 inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
  35 inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
  36 inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
  37 inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
  38 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
  39 inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
  40 
  41 inline void Atomic::inc    (volatile jint*     dest) { (void)add    (1, dest); }
  42 inline void Atomic::inc_ptr(volatile intptr_t* dest) { (void)add_ptr(1, dest); }
  43 inline void Atomic::inc_ptr(volatile void*     dest) { (void)add_ptr(1, dest); }
  44 
  45 inline void Atomic::dec    (volatile jint*     dest) { (void)add    (-1, dest); }
  46 inline void Atomic::dec_ptr(volatile intptr_t* dest) { (void)add_ptr(-1, dest); }
  47 inline void Atomic::dec_ptr(volatile void*     dest) { (void)add_ptr(-1, dest); }
  48 
  49 inline jlong Atomic::load(volatile jlong* src) { return *src; }
  50 
  51 #ifdef _GNU_SOURCE
  52 
  53 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
  54   intptr_t rv;
  55   __asm__ volatile(
  56     "1: \n\t"
  57     " ld     [%2], %%o2\n\t"
  58     " add    %1, %%o2, %%o3\n\t"
  59     " cas    [%2], %%o2, %%o3\n\t"
  60     " cmp    %%o2, %%o3\n\t"
  61     " bne    1b\n\t"
  62     "  nop\n\t"
  63     " add    %1, %%o2, %0\n\t"
  64     : "=r" (rv)
  65     : "r" (add_value), "r" (dest)
  66     : "memory", "o2", "o3");
  67   return rv;
  68 }
  69 
  70 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
  71   intptr_t rv;
  72 #ifdef _LP64
  73   __asm__ volatile(
  74     "1: \n\t"
  75     " ldx    [%2], %%o2\n\t"
  76     " add    %0, %%o2, %%o3\n\t"
  77     " casx   [%2], %%o2, %%o3\n\t"
  78     " cmp    %%o2, %%o3\n\t"
  79     " bne    %%xcc, 1b\n\t"
  80     "  nop\n\t"
  81     " add    %0, %%o2, %0\n\t"
  82     : "=r" (rv)
  83     : "r" (add_value), "r" (dest)
  84     : "memory", "o2", "o3");
  85 #else //_LP64
  86   __asm__ volatile(
  87     "1: \n\t"
  88     " ld     [%2], %%o2\n\t"
  89     " add    %1, %%o2, %%o3\n\t"
  90     " cas    [%2], %%o2, %%o3\n\t"
  91     " cmp    %%o2, %%o3\n\t"
  92     " bne    1b\n\t"
  93     "  nop\n\t"
  94     " add    %1, %%o2, %0\n\t"
  95     : "=r" (rv)
  96     : "r" (add_value), "r" (dest)
  97     : "memory", "o2", "o3");
  98 #endif // _LP64
  99   return rv;
 100 }
 101 
 102 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 103   return (void*)add_ptr((intptr_t)add_value, (volatile intptr_t*)dest);
 104 }
 105 
 106 
 107 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 108   intptr_t rv = exchange_value;
 109   __asm__ volatile(
 110     " swap   [%2],%1\n\t"
 111     : "=r" (rv)
 112     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 113     : "memory");
 114   return rv;
 115 }
 116 
 117 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 118   intptr_t rv = exchange_value;
 119 #ifdef _LP64
 120   __asm__ volatile(
 121     "1:\n\t"
 122     " mov    %1, %%o3\n\t"
 123     " ldx    [%2], %%o2\n\t"
 124     " casx   [%2], %%o2, %%o3\n\t"
 125     " cmp    %%o2, %%o3\n\t"
 126     " bne    %%xcc, 1b\n\t"
 127     "  nop\n\t"
 128     " mov    %%o2, %0\n\t"
 129     : "=r" (rv)
 130     : "r" (exchange_value), "r" (dest)
 131     : "memory", "o2", "o3");
 132 #else  //_LP64
 133   __asm__ volatile(
 134     "swap    [%2],%1\n\t"
 135     : "=r" (rv)
 136     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 137     : "memory");
 138 #endif // _LP64
 139   return rv;
 140 }
 141 
 142 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 143   return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
 144 }
 145 
 146 
 147 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 148   jint rv;
 149   __asm__ volatile(
 150     " cas    [%2], %3, %0"
 151     : "=r" (rv)
 152     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 153     : "memory");
 154   return rv;
 155 }
 156 
 157 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 158 #ifdef _LP64
 159   jlong rv;
 160   __asm__ volatile(
 161     " casx   [%2], %3, %0"
 162     : "=r" (rv)
 163     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 164     : "memory");
 165   return rv;
 166 #else  //_LP64
 167   assert(VM_Version::v9_instructions_work(), "cas only supported on v9");
 168   volatile jlong_accessor evl, cvl, rv;
 169   evl.long_value = exchange_value;
 170   cvl.long_value = compare_value;
 171 
 172   __asm__ volatile(
 173     " sllx   %2, 32, %2\n\t"
 174     " srl    %3, 0,  %3\n\t"
 175     " or     %2, %3, %2\n\t"
 176     " sllx   %5, 32, %5\n\t"
 177     " srl    %6, 0,  %6\n\t"
 178     " or     %5, %6, %5\n\t"
 179     " casx   [%4], %5, %2\n\t"
 180     " srl    %2, 0, %1\n\t"
 181     " srlx   %2, 32, %0\n\t"
 182     : "=r" (rv.words[0]), "=r" (rv.words[1])
 183     : "r"  (evl.words[0]), "r" (evl.words[1]), "r" (dest), "r" (cvl.words[0]), "r" (cvl.words[1])
 184     : "memory");
 185 
 186   return rv.long_value;
 187 #endif  //_LP64
 188 }
 189 
 190 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 191   intptr_t rv;
 192 #ifdef _LP64
 193   __asm__ volatile(
 194     " casx    [%2], %3, %0"
 195     : "=r" (rv)
 196     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 197     : "memory");
 198 #else  //_LP64
 199   __asm__ volatile(
 200     " cas     [%2], %3, %0"
 201     : "=r" (rv)
 202     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 203     : "memory");
 204 #endif // _LP64
 205   return rv;
 206 }
 207 
 208 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 209   return (void*)cmpxchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest, (intptr_t)compare_value);
 210 }
 211 
 212 #else // _GNU_SOURCE
 213 
 214 #if defined(COMPILER2) || defined(_LP64)
 215 
 216 // This is the interface to the atomic instructions in solaris_sparc.il.
 217 // It's very messy because we need to support v8 and these instructions
 218 // are illegal there.  When sparc v8 is dropped, we can drop out lots of
 219 // this code.  Also compiler2 does not support v8 so the conditional code
 220 // omits the instruction set check.
 221 
 222 extern "C" jint     _Atomic_swap32(jint     exchange_value, volatile jint*     dest);
 223 extern "C" intptr_t _Atomic_swap64(intptr_t exchange_value, volatile intptr_t* dest);
 224 
 225 extern "C" jint     _Atomic_cas32(jint     exchange_value, volatile jint*     dest, jint     compare_value);
 226 extern "C" intptr_t _Atomic_cas64(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value);
 227 extern "C" jlong    _Atomic_casl (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value);
 228 
 229 extern "C" jint     _Atomic_add32(jint     inc,       volatile jint*     dest);
 230 extern "C" intptr_t _Atomic_add64(intptr_t add_value, volatile intptr_t* dest);
 231 
 232 
 233 inline jint     Atomic::add     (jint    add_value, volatile jint*     dest) {
 234   return _Atomic_add32(add_value, dest);
 235 }
 236 
 237 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 238 #ifdef _LP64
 239   return _Atomic_add64(add_value, dest);
 240 #else  //_LP64
 241   return _Atomic_add32(add_value, dest);
 242 #endif // _LP64
 243 }
 244 
 245 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 246   return (void*)add_ptr((intptr_t)add_value, (volatile intptr_t*)dest);
 247 }
 248 
 249 
 250 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 251   return _Atomic_swap32(exchange_value, dest);
 252 }
 253 
 254 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 255 #ifdef _LP64
 256   return _Atomic_swap64(exchange_value, dest);
 257 #else  // _LP64
 258   return _Atomic_swap32(exchange_value, dest);
 259 #endif // _LP64
 260 }
 261 
 262 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 263   return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
 264 }
 265 
 266 
 267 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 268   return _Atomic_cas32(exchange_value, dest, compare_value);
 269 }
 270 
 271 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 272 #ifdef _LP64
 273   // Return 64 bit value in %o0
 274   return _Atomic_cas64((intptr_t)exchange_value, (intptr_t *)dest, (intptr_t)compare_value);
 275 #else  // _LP64
 276   assert (VM_Version::v9_instructions_work(), "only supported on v9");
 277   // Return 64 bit value in %o0,%o1 by hand
 278   return _Atomic_casl(exchange_value, dest, compare_value);
 279 #endif // _LP64
 280 }
 281 
 282 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 283 #ifdef _LP64
 284   return _Atomic_cas64(exchange_value, dest, compare_value);
 285 #else  // _LP64
 286   return _Atomic_cas32(exchange_value, dest, compare_value);
 287 #endif // _LP64
 288 }
 289 
 290 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 291   return (void*)cmpxchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest, (intptr_t)compare_value);
 292 }
 293 
 294 
 295 #else // _LP64 || COMPILER2
 296 
 297 
 298 // 32-bit compiler1 only
 299 
 300 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
 301   return (*os::atomic_add_func)(add_value, dest);
 302 }
 303 
 304 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 305   return (intptr_t)add((jint)add_value, (volatile jint*)dest);
 306 }
 307 
 308 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 309   return (void*)add((jint)add_value, (volatile jint*)dest);
 310 }
 311 
 312 
 313 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 314   return (*os::atomic_xchg_func)(exchange_value, dest);
 315 }
 316 
 317 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 318   return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest);
 319 }
 320 
 321 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 322   return (void*)xchg((jint)exchange_value, (volatile jint*)dest);
 323 }
 324 
 325 
 326 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 327   return (*os::atomic_cmpxchg_func)(exchange_value, dest, compare_value);
 328 }
 329 
 330 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 331   return (*os::atomic_cmpxchg_long_func)(exchange_value, dest, compare_value);
 332 }
 333 
 334 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 335   return (intptr_t)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 336 }
 337 
 338 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 339   return (void*)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 340 }
 341 
 342 #endif // _LP64 || COMPILER2
 343 
 344 #endif // _GNU_SOURCE