1 /*
   2  * Copyright (c) 1999, 2017, 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 // Implementation of class atomic
  29 
  30 inline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
  31 inline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
  32 inline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
  33 inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
  34 inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
  35 inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
  36 
  37 inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
  38 inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
  39 inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
  40 inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
  41 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
  42 inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
  43 
  44 inline jlong Atomic::load(const volatile jlong* src) { return *src; }
  45 
  46 template<size_t byte_size>
  47 struct Atomic::PlatformAdd
  48   : Atomic::AddAndFetch<Atomic::PlatformAdd<byte_size> >
  49 {
  50   template<typename I, typename D>
  51   D add_and_fetch(I add_value, D volatile* dest) const;
  52 };
  53 
  54 template<>
  55 template<typename I, typename D>
  56 inline D Atomic::PlatformAdd<4>::add_and_fetch(I add_value, D volatile* dest) const {
  57   STATIC_ASSERT(4 == sizeof(I));
  58   STATIC_ASSERT(4 == sizeof(D));
  59 
  60   D 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 template<>
  77 template<typename I, typename D>
  78 inline D Atomic::PlatformAdd<8>::add_and_fetch(I add_value, D volatile* dest) const {
  79   STATIC_ASSERT(8 == sizeof(I));
  80   STATIC_ASSERT(8 == sizeof(D));
  81 
  82   D rv;
  83   __asm__ volatile(
  84     "1: \n\t"
  85     " ldx    [%2], %%o2\n\t"
  86     " add    %1, %%o2, %%o3\n\t"
  87     " casx   [%2], %%o2, %%o3\n\t"
  88     " cmp    %%o2, %%o3\n\t"
  89     " bne    %%xcc, 1b\n\t"
  90     "  nop\n\t"
  91     " add    %1, %%o2, %0\n\t"
  92     : "=r" (rv)
  93     : "r" (add_value), "r" (dest)
  94     : "memory", "o2", "o3");
  95   return rv;
  96 }
  97 
  98 template<>
  99 template<typename T>
 100 inline T Atomic::PlatformXchg<4>::operator()(T exchange_value,
 101                                              T volatile* dest) const {
 102   STATIC_ASSERT(4 == sizeof(T));
 103   T rv = exchange_value;
 104   __asm__ volatile(
 105     " swap   [%2],%1\n\t"
 106     : "=r" (rv)
 107     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 108     : "memory");
 109   return rv;
 110 }
 111 
 112 template<>
 113 template<typename T>
 114 inline T Atomic::PlatformXchg<8>::operator()(T exchange_value,
 115                                              T volatile* dest) const {
 116   STATIC_ASSERT(8 == sizeof(T));
 117   T rv = exchange_value;
 118   __asm__ volatile(
 119     "1:\n\t"
 120     " mov    %1, %%o3\n\t"
 121     " ldx    [%2], %%o2\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     " mov    %%o2, %0\n\t"
 127     : "=r" (rv)
 128     : "r" (exchange_value), "r" (dest)
 129     : "memory", "o2", "o3");
 130   return rv;
 131 }
 132 
 133 // No direct support for cmpxchg of bytes; emulate using int.
 134 template<>
 135 struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
 136 
 137 template<>
 138 template<typename T>
 139 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
 140                                                 T volatile* dest,
 141                                                 T compare_value,
 142                                                 cmpxchg_memory_order order) const {
 143   STATIC_ASSERT(4 == sizeof(T));
 144   T rv;
 145   __asm__ volatile(
 146     " cas    [%2], %3, %0"
 147     : "=r" (rv)
 148     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 149     : "memory");
 150   return rv;
 151 }
 152 
 153 template<>
 154 template<typename T>
 155 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
 156                                                 T volatile* dest,
 157                                                 T compare_value,
 158                                                 cmpxchg_memory_order order) const {
 159   STATIC_ASSERT(8 == sizeof(T));
 160   T rv;
 161   __asm__ volatile(
 162     " casx   [%2], %3, %0"
 163     : "=r" (rv)
 164     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 165     : "memory");
 166   return rv;
 167 }
 168 
 169 #endif // OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP