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