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