1 /*
   2  * Copyright (c) 2008, 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_ARM_VM_ORDERACCESS_LINUX_ARM_INLINE_HPP
  26 #define OS_CPU_LINUX_ARM_VM_ORDERACCESS_LINUX_ARM_INLINE_HPP
  27 
  28 #include "runtime/orderAccess.hpp"
  29 #include "runtime/os.hpp"
  30 #include "vm_version_arm.hpp"
  31 
  32 // Implementation of class OrderAccess.
  33 // - we define the high level barriers below and use the general
  34 //   implementation in orderAccess.inline.hpp, with customizations
  35 //   on AARCH64 via the specialized_* template functions
  36 
  37 // Memory Ordering on ARM is weak.
  38 //
  39 // Implement all 4 memory ordering barriers by DMB, since it is a
  40 // lighter version of DSB.
  41 // dmb_sy implies full system shareability domain. RD/WR access type.
  42 // dmb_st implies full system shareability domain. WR only access type.
  43 //
  44 // NOP on < ARMv6 (MP not supported)
  45 //
  46 // Non mcr instructions can be used if we build for armv7 or higher arch
  47 //    __asm__ __volatile__ ("dmb" : : : "memory");
  48 //    __asm__ __volatile__ ("dsb" : : : "memory");
  49 //
  50 // inline void _OrderAccess_dsb() {
  51 //    volatile intptr_t dummy = 0;
  52 //    if (os::is_MP()) {
  53 //      __asm__ volatile (
  54 //        "mcr p15, 0, %0, c7, c10, 4"
  55 //        : : "r" (dummy) : "memory");
  56 //   }
  57 // }
  58 
  59 inline static void dmb_sy() {
  60    if (!os::is_MP()) {
  61      return;
  62    }
  63 #ifdef AARCH64
  64    __asm__ __volatile__ ("dmb sy" : : : "memory");
  65 #else
  66    if (VM_Version::arm_arch() >= 7) {
  67 #ifdef __thumb__
  68      __asm__ volatile (
  69      "dmb sy": : : "memory");
  70 #else
  71      __asm__ volatile (
  72      ".word 0xF57FF050 | 0xf" : : : "memory");
  73 #endif
  74    } else {
  75      intptr_t zero = 0;
  76      __asm__ volatile (
  77        "mcr p15, 0, %0, c7, c10, 5"
  78        : : "r" (zero) : "memory");
  79    }
  80 #endif
  81 }
  82 
  83 inline static void dmb_st() {
  84    if (!os::is_MP()) {
  85      return;
  86    }
  87 #ifdef AARCH64
  88    __asm__ __volatile__ ("dmb st" : : : "memory");
  89 #else
  90    if (VM_Version::arm_arch() >= 7) {
  91 #ifdef __thumb__
  92      __asm__ volatile (
  93      "dmb st": : : "memory");
  94 #else
  95      __asm__ volatile (
  96      ".word 0xF57FF050 | 0xe" : : : "memory");
  97 #endif
  98    } else {
  99      intptr_t zero = 0;
 100      __asm__ volatile (
 101        "mcr p15, 0, %0, c7, c10, 5"
 102        : : "r" (zero) : "memory");
 103    }
 104 #endif
 105 }
 106 
 107 // Load-Load/Store barrier
 108 inline static void dmb_ld() {
 109 #ifdef AARCH64
 110    if (!os::is_MP()) {
 111      return;
 112    }
 113    __asm__ __volatile__ ("dmb ld" : : : "memory");
 114 #else
 115    dmb_sy();
 116 #endif
 117 }
 118 
 119 
 120 inline void OrderAccess::loadload()   { dmb_ld(); }
 121 inline void OrderAccess::loadstore()  { dmb_ld(); }
 122 inline void OrderAccess::acquire()    { dmb_ld(); }
 123 inline void OrderAccess::storestore() { dmb_st(); }
 124 inline void OrderAccess::storeload()  { dmb_sy(); }
 125 inline void OrderAccess::release()    { dmb_sy(); }
 126 inline void OrderAccess::fence()      { dmb_sy(); }
 127 
 128 // specializations for Aarch64
 129 // TODO-AARCH64: evaluate effectiveness of ldar*/stlr* implementations compared to 32-bit ARM approach
 130 
 131 #ifdef AARCH64
 132 
 133 template<>
 134 struct OrderAccess::PlatformOrderedLoad<1, X_ACQUIRE>
 135   VALUE_OBJ_CLASS_SPEC
 136 {
 137   template <typename T>
 138   T operator()(const volatile T* p) const {
 139     volatile T result;
 140     __asm__ volatile(
 141       "ldarb %w[res], [%[ptr]]"
 142       : [res] "=&r" (result)
 143       : [ptr] "r" (p)
 144       : "memory");
 145     return result;
 146   }
 147 };
 148 
 149 template<>
 150 struct OrderAccess::PlatformOrderedLoad<2, X_ACQUIRE>
 151   VALUE_OBJ_CLASS_SPEC
 152 {
 153   template <typename T>
 154   T operator()(const volatile T* p) const {
 155     volatile T result;
 156     __asm__ volatile(
 157       "ldarh %w[res], [%[ptr]]"
 158       : [res] "=&r" (result)
 159       : [ptr] "r" (p)
 160       : "memory");
 161     return result;
 162   }
 163 };
 164 
 165 template<>
 166 struct OrderAccess::PlatformOrderedLoad<4, X_ACQUIRE>
 167   VALUE_OBJ_CLASS_SPEC
 168 {
 169   template <typename T>
 170   T operator()(const volatile T* p) const {
 171     volatile T result;
 172     __asm__ volatile(
 173       "ldar %w[res], [%[ptr]]"
 174       : [res] "=&r" (result)
 175       : [ptr] "r" (p)
 176       : "memory");
 177     return result;
 178   }
 179 };
 180 
 181 template<>
 182 struct OrderAccess::PlatformOrderedLoad<8, X_ACQUIRE>
 183   VALUE_OBJ_CLASS_SPEC
 184 {
 185   template <typename T>
 186   T operator()(const volatile T* p) const {
 187     volatile T result;
 188     __asm__ volatile(
 189       "ldar %[res], [%[ptr]]"
 190       : [res] "=&r" (result)
 191       : [ptr] "r" (p)
 192       : "memory");
 193     return result;
 194   }
 195 };
 196 
 197 template<>
 198 struct OrderAccess::PlatformOrderedStore<1, RELEASE_X_FENCE>
 199   VALUE_OBJ_CLASS_SPEC
 200 {
 201   template <typename T>
 202   void operator()(T v, volatile T* p) const {
 203     __asm__ volatile(
 204       "stlrb %w[val], [%[ptr]]"
 205       :
 206       : [ptr] "r" (p), [val] "r" (v)
 207       : "memory");
 208   }
 209 };
 210 
 211 template<>
 212 struct OrderAccess::PlatformOrderedStore<2, RELEASE_X_FENCE>
 213   VALUE_OBJ_CLASS_SPEC
 214 {
 215   template <typename T>
 216   void operator()(T v, volatile T* p) const {
 217     __asm__ volatile(
 218       "stlrh %w[val], [%[ptr]]"
 219       :
 220       : [ptr] "r" (p), [val] "r" (v)
 221       : "memory");
 222   }
 223 };
 224 
 225 template<>
 226 struct OrderAccess::PlatformOrderedStore<4, RELEASE_X_FENCE>
 227   VALUE_OBJ_CLASS_SPEC
 228 {
 229   template <typename T>
 230   void operator()(T v, volatile T* p) const {
 231     __asm__ volatile(
 232       "stlr %w[val], [%[ptr]]"
 233       :
 234       : [ptr] "r" (p), [val] "r" (v)
 235       : "memory");
 236   }
 237 };
 238 
 239 template<>
 240 struct OrderAccess::PlatformOrderedStore<8, RELEASE_X_FENCE>
 241   VALUE_OBJ_CLASS_SPEC
 242 {
 243   template <typename T>
 244   void operator()(T v, volatile T* p) const {
 245     __asm__ volatile(
 246       "stlr %[val], [%[ptr]]"
 247       :
 248       : [ptr] "r" (p), [val] "r" (v)
 249       : "memory");
 250   }
 251 };
 252 
 253 #endif // AARCH64
 254 
 255 #endif // OS_CPU_LINUX_ARM_VM_ORDERACCESS_LINUX_ARM_INLINE_HPP