1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2015, Linaro Ltd. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #ifndef OS_CPU_LINUX_AARCH32_VM_ORDERACCESS_LINUX_AARCH32_INLINE_HPP
  28 #define OS_CPU_LINUX_AARCH32_VM_ORDERACCESS_LINUX_AARCH32_INLINE_HPP
  29 
  30 #include "runtime/atomic.inline.hpp"
  31 #include "runtime/orderAccess.hpp"
  32 #include "runtime/os.hpp"
  33 #include "vm_version_aarch32.hpp"
  34 
  35 // Implementation of class OrderAccess.
  36 
  37 inline void OrderAccess::loadload()   { acquire(); }
  38 inline void OrderAccess::storestore() { release(); }
  39 inline void OrderAccess::loadstore()  { acquire(); }
  40 inline void OrderAccess::storeload()  { fence(); }
  41 
  42 inline void OrderAccess::acquire() {
  43   READ_MEM_BARRIER;
  44 }
  45 
  46 inline void OrderAccess::release() {
  47   WRITE_MEM_BARRIER;
  48 }
  49 
  50 inline void OrderAccess::fence() {
  51   FULL_MEM_BARRIER;
  52 }
  53 
  54 // __atomic builtins should be supported since gcc 4.4, however not all 4.4 do support.
  55 // for simplicity, provide own implementation with same semantic.
  56 #define ARM_ATOMIC_ACQUIRE 2
  57 #define ARM_ATOMIC_RELEASE 3
  58 #define ARM_ATOMIC_RELAXED 0
  59 
  60 // the following implementation is only valid for values up to 4 bytes long. DO NOT USE for jlong!
  61 #define arm_atomic_load(S, D, X) { \
  62     STATIC_ASSERT(sizeof(*S) <= sizeof(jint)); \
  63     STATIC_ASSERT(X == ARM_ATOMIC_ACQUIRE || X == ARM_ATOMIC_RELAXED); \
  64     *(D) = *(S); if (X == ARM_ATOMIC_ACQUIRE) READ_MEM_BARRIER; \
  65 }
  66 #define arm_atomic_store(D, S, X) { \
  67     STATIC_ASSERT(sizeof(*S) <= sizeof(jint)); \
  68     STATIC_ASSERT(X == ARM_ATOMIC_RELEASE || X == ARM_ATOMIC_RELAXED); \
  69     if (X == ARM_ATOMIC_RELEASE) WRITE_MEM_BARRIER; *(D) = *(S); \
  70 }
  71 
  72 inline jbyte    OrderAccess::load_acquire(volatile jbyte*   p)
  73 { jbyte data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
  74 inline jshort   OrderAccess::load_acquire(volatile jshort*  p)
  75 { jshort data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
  76 inline jint     OrderAccess::load_acquire(volatile jint*    p)
  77 { jint data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
  78 inline jlong    OrderAccess::load_acquire(volatile jlong*   p)
  79 {
  80     jlong data;
  81     data = Atomic::load(p);
  82     READ_MEM_BARRIER;
  83     return data;
  84 }
  85 inline jubyte    OrderAccess::load_acquire(volatile jubyte*   p)
  86 { jubyte data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
  87 inline jushort   OrderAccess::load_acquire(volatile jushort*  p)
  88 { jushort data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
  89 inline juint     OrderAccess::load_acquire(volatile juint*    p)
  90 { juint data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
  91 inline julong   OrderAccess::load_acquire(volatile julong*  p)
  92 {
  93     julong data;
  94     data = (julong)Atomic::load((volatile jlong*)p);
  95     READ_MEM_BARRIER;
  96     return data;
  97 }
  98 inline jfloat   OrderAccess::load_acquire(volatile jfloat*  p)
  99 { jfloat data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
 100 inline jdouble  OrderAccess::load_acquire(volatile jdouble* p)
 101 {
 102     jlong data = Atomic::load((volatile jlong*)p);
 103     READ_MEM_BARRIER;
 104     // in -fno-strict-aliasing we trust. this option should be (and is) provided to g++
 105     return *(jdouble*)&data;
 106 }
 107 inline intptr_t OrderAccess::load_ptr_acquire(volatile intptr_t*   p)
 108 { intptr_t data; arm_atomic_load(p, &data, ARM_ATOMIC_ACQUIRE); return data; }
 109 inline void*    OrderAccess::load_ptr_acquire(volatile void*       p)
 110 { void* data; arm_atomic_load((void* volatile *)p, &data, ARM_ATOMIC_ACQUIRE); return data; }
 111 inline void*    OrderAccess::load_ptr_acquire(const volatile void* p)
 112 { void* data; arm_atomic_load((void* const volatile *)p, &data, ARM_ATOMIC_ACQUIRE); return data; }
 113 
 114 inline void     OrderAccess::release_store(volatile jbyte*   p, jbyte   v)
 115 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 116 inline void     OrderAccess::release_store(volatile jshort*  p, jshort  v)
 117 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 118 inline void     OrderAccess::release_store(volatile jint*    p, jint    v)
 119 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 120 inline void     OrderAccess::release_store(volatile jlong*   p, jlong   v)
 121 {
 122   WRITE_MEM_BARRIER;
 123   Atomic::store(v, p);
 124 }
 125 inline void     OrderAccess::release_store(volatile jubyte*  p, jubyte  v)
 126 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 127 inline void     OrderAccess::release_store(volatile jushort* p, jushort v)
 128 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 129 inline void     OrderAccess::release_store(volatile juint*   p, juint   v)
 130 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 131 inline void     OrderAccess::release_store(volatile julong*  p, julong  v)
 132 {
 133   WRITE_MEM_BARRIER;
 134   Atomic::store(*(jlong*)&v, (jlong*)p);
 135 }
 136 inline void     OrderAccess::release_store(volatile jfloat*  p, jfloat  v)
 137 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 138 inline void     OrderAccess::release_store(volatile jdouble* p, jdouble v)
 139 {
 140   WRITE_MEM_BARRIER;
 141   // in -fno-strict-aliasing we trust. this option should be (and is) provided to g++
 142   Atomic::store(*(jlong*)&v, (jlong*)p);
 143 }
 144 inline void     OrderAccess::release_store_ptr(volatile intptr_t* p, intptr_t v)
 145 { arm_atomic_store(p, &v, ARM_ATOMIC_RELEASE); }
 146 inline void     OrderAccess::release_store_ptr(volatile void*     p, void*    v)
 147 { arm_atomic_store((void* volatile *)p, &v, ARM_ATOMIC_RELEASE); }
 148 
 149 inline void     OrderAccess::store_fence(jbyte*   p, jbyte   v)
 150 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 151 inline void     OrderAccess::store_fence(jshort*  p, jshort  v)
 152 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 153 inline void     OrderAccess::store_fence(jint*    p, jint    v)
 154 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 155 inline void     OrderAccess::store_fence(jlong*   p, jlong   v)
 156 { Atomic::store(v, p); fence(); }
 157 inline void     OrderAccess::store_fence(jubyte*  p, jubyte  v)
 158 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 159 inline void     OrderAccess::store_fence(jushort* p, jushort v)
 160 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 161 inline void     OrderAccess::store_fence(juint*   p, juint   v)
 162 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 163 inline void     OrderAccess::store_fence(julong*  p, julong  v)
 164 { Atomic::store(*(jlong*)&v, (jlong*)p); fence(); }
 165 inline void     OrderAccess::store_fence(jfloat*  p, jfloat  v)
 166 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 167 // in -fno-strict-aliasing we trust. this option should be (and is) provided to g++
 168 inline void     OrderAccess::store_fence(jdouble* p, jdouble v)
 169 { Atomic::store(*(jlong*)&v, (jlong*)p); fence(); }
 170 inline void     OrderAccess::store_ptr_fence(intptr_t* p, intptr_t v)
 171 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 172 inline void     OrderAccess::store_ptr_fence(void**    p, void*    v)
 173 { arm_atomic_store(p, &v, ARM_ATOMIC_RELAXED); fence(); }
 174 
 175 inline void     OrderAccess::release_store_fence(volatile jbyte*   p, jbyte   v) { release_store(p, v); fence(); }
 176 inline void     OrderAccess::release_store_fence(volatile jshort*  p, jshort  v) { release_store(p, v); fence(); }
 177 inline void     OrderAccess::release_store_fence(volatile jint*    p, jint    v) { release_store(p, v); fence(); }
 178 inline void     OrderAccess::release_store_fence(volatile jlong*   p, jlong   v) { release_store(p, v); fence(); }
 179 inline void     OrderAccess::release_store_fence(volatile jubyte*  p, jubyte  v) { release_store(p, v); fence(); }
 180 inline void     OrderAccess::release_store_fence(volatile jushort* p, jushort v) { release_store(p, v); fence(); }
 181 inline void     OrderAccess::release_store_fence(volatile juint*   p, juint   v) { release_store(p, v); fence(); }
 182 inline void     OrderAccess::release_store_fence(volatile julong*  p, julong  v) { release_store(p, v); fence(); }
 183 inline void     OrderAccess::release_store_fence(volatile jfloat*  p, jfloat  v) { release_store(p, v); fence(); }
 184 inline void     OrderAccess::release_store_fence(volatile jdouble* p, jdouble v) { release_store(p, v); fence(); }
 185 
 186 inline void     OrderAccess::release_store_ptr_fence(volatile intptr_t* p, intptr_t v) { release_store_ptr(p, v); fence(); }
 187 inline void     OrderAccess::release_store_ptr_fence(volatile void*     p, void*    v) { release_store_ptr(p, v); fence(); }
 188 
 189 #undef arm_atomic_load
 190 #undef arm_atomic_store
 191 #undef ARM_ATOMIC_ACQUIRE
 192 #undef ARM_ATOMIC_RELEASE
 193 #undef ARM_ATOMIC_RELAXED
 194 
 195 #endif // OS_CPU_LINUX_AARCH32_VM_ORDERACCESS_LINUX_AARCH32_INLINE_HPP