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