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