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