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_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP
  26 #define OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP
  27 
  28 #include "orderAccess_linux_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 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
  60   intptr_t rv;
  61   __asm__ volatile(
  62     "1: \n\t"
  63     " ld     [%2], %%o2\n\t"
  64     " add    %1, %%o2, %%o3\n\t"
  65     " cas    [%2], %%o2, %%o3\n\t"
  66     " cmp    %%o2, %%o3\n\t"
  67     " bne    1b\n\t"
  68     "  nop\n\t"
  69     " add    %1, %%o2, %0\n\t"
  70     : "=r" (rv)
  71     : "r" (add_value), "r" (dest)
  72     : "memory", "o2", "o3");
  73   return rv;
  74 }
  75 
  76 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
  77   intptr_t rv;
  78 #ifdef _LP64
  79   __asm__ volatile(
  80     "1: \n\t"
  81     " ldx    [%2], %%o2\n\t"
  82     " add    %0, %%o2, %%o3\n\t"
  83     " casx   [%2], %%o2, %%o3\n\t"
  84     " cmp    %%o2, %%o3\n\t"
  85     " bne    %%xcc, 1b\n\t"
  86     "  nop\n\t"
  87     " add    %0, %%o2, %0\n\t"
  88     : "=r" (rv)
  89     : "r" (add_value), "r" (dest)
  90     : "memory", "o2", "o3");
  91 #else
  92   __asm__ volatile(
  93     "1: \n\t"
  94     " ld     [%2], %%o2\n\t"
  95     " add    %1, %%o2, %%o3\n\t"
  96     " cas    [%2], %%o2, %%o3\n\t"
  97     " cmp    %%o2, %%o3\n\t"
  98     " bne    1b\n\t"
  99     "  nop\n\t"
 100     " add    %1, %%o2, %0\n\t"
 101     : "=r" (rv)
 102     : "r" (add_value), "r" (dest)
 103     : "memory", "o2", "o3");
 104 #endif // _LP64
 105   return rv;
 106 }
 107 
 108 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 109   return (void*)add_ptr((intptr_t)add_value, (volatile intptr_t*)dest);
 110 }
 111 
 112 
 113 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 114   intptr_t rv = exchange_value;
 115   __asm__ volatile(
 116     " swap   [%2],%1\n\t"
 117     : "=r" (rv)
 118     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 119     : "memory");
 120   return rv;
 121 }
 122 
 123 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 124   intptr_t rv = exchange_value;
 125 #ifdef _LP64
 126   __asm__ volatile(
 127     "1:\n\t"
 128     " mov    %1, %%o3\n\t"
 129     " ldx    [%2], %%o2\n\t"
 130     " casx   [%2], %%o2, %%o3\n\t"
 131     " cmp    %%o2, %%o3\n\t"
 132     " bne    %%xcc, 1b\n\t"
 133     "  nop\n\t"
 134     " mov    %%o2, %0\n\t"
 135     : "=r" (rv)
 136     : "r" (exchange_value), "r" (dest)
 137     : "memory", "o2", "o3");
 138 #else
 139   __asm__ volatile(
 140     "swap    [%2],%1\n\t"
 141     : "=r" (rv)
 142     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 143     : "memory");
 144 #endif // _LP64
 145   return rv;
 146 }
 147 
 148 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 149   return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
 150 }
 151 
 152 
 153 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 154   jint rv;
 155   __asm__ volatile(
 156     " cas    [%2], %3, %0"
 157     : "=r" (rv)
 158     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 159     : "memory");
 160   return rv;
 161 }
 162 
 163 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 164 #ifdef _LP64
 165   jlong rv;
 166   __asm__ volatile(
 167     " casx   [%2], %3, %0"
 168     : "=r" (rv)
 169     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 170     : "memory");
 171   return rv;
 172 #else
 173   assert(VM_Version::v9_instructions_work(), "cas only supported on v9");
 174   volatile jlong_accessor evl, cvl, rv;
 175   evl.long_value = exchange_value;
 176   cvl.long_value = compare_value;
 177 
 178   __asm__ volatile(
 179     " sllx   %2, 32, %2\n\t"
 180     " srl    %3, 0,  %3\n\t"
 181     " or     %2, %3, %2\n\t"
 182     " sllx   %5, 32, %5\n\t"
 183     " srl    %6, 0,  %6\n\t"
 184     " or     %5, %6, %5\n\t"
 185     " casx   [%4], %5, %2\n\t"
 186     " srl    %2, 0, %1\n\t"
 187     " srlx   %2, 32, %0\n\t"
 188     : "=r" (rv.words[0]), "=r" (rv.words[1])
 189     : "r"  (evl.words[0]), "r" (evl.words[1]), "r" (dest), "r" (cvl.words[0]), "r" (cvl.words[1])
 190     : "memory");
 191 
 192   return rv.long_value;
 193 #endif
 194 }
 195 
 196 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 197   intptr_t rv;
 198 #ifdef _LP64
 199   __asm__ volatile(
 200     " casx    [%2], %3, %0"
 201     : "=r" (rv)
 202     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 203     : "memory");
 204 #else
 205   __asm__ volatile(
 206     " cas     [%2], %3, %0"
 207     : "=r" (rv)
 208     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 209     : "memory");
 210 #endif // _LP64
 211   return rv;
 212 }
 213 
 214 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 215   return (void*)cmpxchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest, (intptr_t)compare_value);
 216 }
 217 
 218 #endif // OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP