1 /*
   2  * Copyright (c) 2017, 2019, 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 SHARE_OOPS_ACCESS_INLINE_HPP
  26 #define SHARE_OOPS_ACCESS_INLINE_HPP
  27 
  28 #include "gc/shared/barrierSet.inline.hpp"
  29 #include "gc/shared/barrierSetConfig.inline.hpp"
  30 #include "oops/access.hpp"
  31 #include "oops/accessBackend.inline.hpp"
  32 
  33 // This file outlines the last 2 steps of the template pipeline of accesses going through
  34 // the Access API.
  35 // * Step 5.a: Barrier resolution. This step is invoked the first time a runtime-dispatch
  36 //             happens for an access. The appropriate BarrierSet::AccessBarrier accessor
  37 //             is resolved, then the function pointer is updated to that accessor for
  38 //             future invocations.
  39 // * Step 5.b: Post-runtime dispatch. This step now casts previously unknown types such
  40 //             as the address type of an oop on the heap (is it oop* or narrowOop*) to
  41 //             the appropriate type. It also splits sufficiently orthogonal accesses into
  42 //             different functions, such as whether the access involves oops or primitives
  43 //             and whether the access is performed on the heap or outside. Then the
  44 //             appropriate BarrierSet::AccessBarrier is called to perform the access.
  45 
  46 namespace AccessInternal {
  47   // Step 5.b: Post-runtime dispatch.
  48   // This class is the last step before calling the BarrierSet::AccessBarrier.
  49   // Here we make sure to figure out types that were not known prior to the
  50   // runtime dispatch, such as whether an oop on the heap is oop or narrowOop.
  51   // We also split orthogonal barriers such as handling primitives vs oops
  52   // and on-heap vs off-heap into different calls to the barrier set.
  53   template <class GCBarrierType, BarrierType type, DecoratorSet decorators>
  54   struct PostRuntimeDispatch: public AllStatic { };
  55 
  56   template <class GCBarrierType, DecoratorSet decorators>
  57   struct PostRuntimeDispatch<GCBarrierType, BARRIER_STORE, decorators>: public AllStatic {
  58     template <typename T>
  59     static void access_barrier(void* addr, T value) {
  60       GCBarrierType::store_in_heap(reinterpret_cast<T*>(addr), value);
  61     }
  62 
  63     static void oop_access_barrier(void* addr, oop value) {
  64       typedef typename HeapOopType<decorators>::type OopType;
  65       if (HasDecorator<decorators, IN_HEAP>::value) {
  66         GCBarrierType::oop_store_in_heap(reinterpret_cast<OopType*>(addr), value);
  67       } else {
  68         GCBarrierType::oop_store_not_in_heap(reinterpret_cast<OopType*>(addr), value);
  69       }
  70     }
  71   };
  72 
  73   template <class GCBarrierType, DecoratorSet decorators>
  74   struct PostRuntimeDispatch<GCBarrierType, BARRIER_LOAD, decorators>: public AllStatic {
  75     template <typename T>
  76     static T access_barrier(void* addr) {
  77       return GCBarrierType::load_in_heap(reinterpret_cast<T*>(addr));
  78     }
  79 
  80     static oop oop_access_barrier(void* addr) {
  81       typedef typename HeapOopType<decorators>::type OopType;
  82       if (HasDecorator<decorators, IN_HEAP>::value) {
  83         return GCBarrierType::oop_load_in_heap(reinterpret_cast<OopType*>(addr));
  84       } else {
  85         return GCBarrierType::oop_load_not_in_heap(reinterpret_cast<OopType*>(addr));
  86       }
  87     }
  88   };
  89 
  90   template <class GCBarrierType, DecoratorSet decorators>
  91   struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_XCHG, decorators>: public AllStatic {
  92     template <typename T>
  93     static T access_barrier(T new_value, void* addr) {
  94       return GCBarrierType::atomic_xchg_in_heap(new_value, reinterpret_cast<T*>(addr));
  95     }
  96 
  97     static oop oop_access_barrier(oop new_value, void* addr) {
  98       typedef typename HeapOopType<decorators>::type OopType;
  99       if (HasDecorator<decorators, IN_HEAP>::value) {
 100         return GCBarrierType::oop_atomic_xchg_in_heap(new_value, reinterpret_cast<OopType*>(addr));
 101       } else {
 102         return GCBarrierType::oop_atomic_xchg_not_in_heap(new_value, reinterpret_cast<OopType*>(addr));
 103       }
 104     }
 105   };
 106 
 107   template <class GCBarrierType, DecoratorSet decorators>
 108   struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_CMPXCHG, decorators>: public AllStatic {
 109     template <typename T>
 110     static T access_barrier(T new_value, void* addr, T compare_value) {
 111       return GCBarrierType::atomic_cmpxchg_in_heap(new_value, reinterpret_cast<T*>(addr), compare_value);
 112     }
 113 
 114     static oop oop_access_barrier(oop new_value, void* addr, oop compare_value) {
 115       typedef typename HeapOopType<decorators>::type OopType;
 116       if (HasDecorator<decorators, IN_HEAP>::value) {
 117         return GCBarrierType::oop_atomic_cmpxchg_in_heap(new_value, reinterpret_cast<OopType*>(addr), compare_value);
 118       } else {
 119         return GCBarrierType::oop_atomic_cmpxchg_not_in_heap(new_value, reinterpret_cast<OopType*>(addr), compare_value);
 120       }
 121     }
 122   };
 123 
 124   template <class GCBarrierType, DecoratorSet decorators>
 125   struct PostRuntimeDispatch<GCBarrierType, BARRIER_ARRAYCOPY, decorators>: public AllStatic {
 126     template <typename T>
 127     static void access_barrier(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 128                                arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 129                                size_t length) {
 130       GCBarrierType::arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw,
 131                                        dst_obj, dst_offset_in_bytes, dst_raw,
 132                                        length);
 133     }
 134 
 135     template <typename T>
 136     static void oop_access_barrier(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 137                                    arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 138                                    size_t length) {
 139       typedef typename HeapOopType<decorators>::type OopType;
 140       GCBarrierType::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, reinterpret_cast<OopType*>(src_raw),
 141                                            dst_obj, dst_offset_in_bytes, reinterpret_cast<OopType*>(dst_raw),
 142                                            length);
 143     }
 144   };
 145 
 146   template <class GCBarrierType, DecoratorSet decorators>
 147   struct PostRuntimeDispatch<GCBarrierType, BARRIER_STORE_AT, decorators>: public AllStatic {
 148     template <typename T>
 149     static void access_barrier(oop base, ptrdiff_t offset, T value) {
 150       GCBarrierType::store_in_heap_at(base, offset, value);
 151     }
 152 
 153     static void oop_access_barrier(oop base, ptrdiff_t offset, oop value) {
 154       GCBarrierType::oop_store_in_heap_at(base, offset, value);
 155     }
 156   };
 157 
 158   template <class GCBarrierType, DecoratorSet decorators>
 159   struct PostRuntimeDispatch<GCBarrierType, BARRIER_LOAD_AT, decorators>: public AllStatic {
 160     template <typename T>
 161     static T access_barrier(oop base, ptrdiff_t offset) {
 162       return GCBarrierType::template load_in_heap_at<T>(base, offset);
 163     }
 164 
 165     static oop oop_access_barrier(oop base, ptrdiff_t offset) {
 166       return GCBarrierType::oop_load_in_heap_at(base, offset);
 167     }
 168   };
 169 
 170   template <class GCBarrierType, DecoratorSet decorators>
 171   struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_XCHG_AT, decorators>: public AllStatic {
 172     template <typename T>
 173     static T access_barrier(T new_value, oop base, ptrdiff_t offset) {
 174       return GCBarrierType::atomic_xchg_in_heap_at(new_value, base, offset);
 175     }
 176 
 177     static oop oop_access_barrier(oop new_value, oop base, ptrdiff_t offset) {
 178       return GCBarrierType::oop_atomic_xchg_in_heap_at(new_value, base, offset);
 179     }
 180   };
 181 
 182   template <class GCBarrierType, DecoratorSet decorators>
 183   struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_CMPXCHG_AT, decorators>: public AllStatic {
 184     template <typename T>
 185     static T access_barrier(T new_value, oop base, ptrdiff_t offset, T compare_value) {
 186       return GCBarrierType::atomic_cmpxchg_in_heap_at(new_value, base, offset, compare_value);
 187     }
 188 
 189     static oop oop_access_barrier(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
 190       return GCBarrierType::oop_atomic_cmpxchg_in_heap_at(new_value, base, offset, compare_value);
 191     }
 192   };
 193 
 194   template <class GCBarrierType, DecoratorSet decorators>
 195   struct PostRuntimeDispatch<GCBarrierType, BARRIER_CLONE, decorators>: public AllStatic {
 196     static void access_barrier(oop src, oop dst, size_t size) {
 197       GCBarrierType::clone_in_heap(src, dst, size);
 198     }
 199   };
 200 
 201   template <class GCBarrierType, DecoratorSet decorators>
 202   struct PostRuntimeDispatch<GCBarrierType, BARRIER_VALUE_COPY, decorators>: public AllStatic {
 203     static void access_barrier(void* src, void* dst, ValueKlass* md) {
 204       GCBarrierType::value_copy_in_heap(src, dst, md);
 205     }
 206   };
 207 
 208   template <class GCBarrierType, DecoratorSet decorators>
 209   struct PostRuntimeDispatch<GCBarrierType, BARRIER_RESOLVE, decorators>: public AllStatic {
 210     static oop access_barrier(oop obj) {
 211       return GCBarrierType::resolve(obj);
 212     }
 213   };
 214 
 215   // Resolving accessors with barriers from the barrier set happens in two steps.
 216   // 1. Expand paths with runtime-decorators, e.g. is UseCompressedOops on or off.
 217   // 2. Expand paths for each BarrierSet available in the system.
 218   template <DecoratorSet decorators, typename FunctionPointerT, BarrierType barrier_type>
 219   struct BarrierResolver: public AllStatic {
 220     template <DecoratorSet ds>
 221     static typename EnableIf<
 222       HasDecorator<ds, INTERNAL_VALUE_IS_OOP>::value,
 223       FunctionPointerT>::type
 224     resolve_barrier_gc() {
 225       BarrierSet* bs = BarrierSet::barrier_set();
 226       assert(bs != NULL, "GC barriers invoked before BarrierSet is set");
 227       switch (bs->kind()) {
 228 #define BARRIER_SET_RESOLVE_BARRIER_CLOSURE(bs_name)                    \
 229         case BarrierSet::bs_name: {                                     \
 230           return PostRuntimeDispatch<typename BarrierSet::GetType<BarrierSet::bs_name>::type:: \
 231             AccessBarrier<ds>, barrier_type, ds>::oop_access_barrier; \
 232         }                                                               \
 233         break;
 234         FOR_EACH_CONCRETE_BARRIER_SET_DO(BARRIER_SET_RESOLVE_BARRIER_CLOSURE)
 235 #undef BARRIER_SET_RESOLVE_BARRIER_CLOSURE
 236 
 237       default:
 238         fatal("BarrierSet AccessBarrier resolving not implemented");
 239         return NULL;
 240       };
 241     }
 242 
 243     template <DecoratorSet ds>
 244     static typename EnableIf<
 245       !HasDecorator<ds, INTERNAL_VALUE_IS_OOP>::value,
 246       FunctionPointerT>::type
 247     resolve_barrier_gc() {
 248       BarrierSet* bs = BarrierSet::barrier_set();
 249       assert(bs != NULL, "GC barriers invoked before BarrierSet is set");
 250       switch (bs->kind()) {
 251 #define BARRIER_SET_RESOLVE_BARRIER_CLOSURE(bs_name)                    \
 252         case BarrierSet::bs_name: {                                       \
 253           return PostRuntimeDispatch<typename BarrierSet::GetType<BarrierSet::bs_name>::type:: \
 254             AccessBarrier<ds>, barrier_type, ds>::access_barrier; \
 255         }                                                                 \
 256         break;
 257         FOR_EACH_CONCRETE_BARRIER_SET_DO(BARRIER_SET_RESOLVE_BARRIER_CLOSURE)
 258 #undef BARRIER_SET_RESOLVE_BARRIER_CLOSURE
 259 
 260       default:
 261         fatal("BarrierSet AccessBarrier resolving not implemented");
 262         return NULL;
 263       };
 264     }
 265 
 266     static FunctionPointerT resolve_barrier_rt() {
 267       if (UseCompressedOops) {
 268         const DecoratorSet expanded_decorators = decorators | INTERNAL_RT_USE_COMPRESSED_OOPS;
 269         return resolve_barrier_gc<expanded_decorators>();
 270       } else {
 271         return resolve_barrier_gc<decorators>();
 272       }
 273     }
 274 
 275     static FunctionPointerT resolve_barrier() {
 276       return resolve_barrier_rt();
 277     }
 278   };
 279 
 280   // Step 5.a: Barrier resolution
 281   // The RuntimeDispatch class is responsible for performing a runtime dispatch of the
 282   // accessor. This is required when the access either depends on whether compressed oops
 283   // is being used, or it depends on which GC implementation was chosen (e.g. requires GC
 284   // barriers). The way it works is that a function pointer initially pointing to an
 285   // accessor resolution function gets called for each access. Upon first invocation,
 286   // it resolves which accessor to be used in future invocations and patches the
 287   // function pointer to this new accessor.
 288 
 289   template <DecoratorSet decorators, typename T>
 290   void RuntimeDispatch<decorators, T, BARRIER_STORE>::store_init(void* addr, T value) {
 291     func_t function = BarrierResolver<decorators, func_t, BARRIER_STORE>::resolve_barrier();
 292     _store_func = function;
 293     function(addr, value);
 294   }
 295 
 296   template <DecoratorSet decorators, typename T>
 297   void RuntimeDispatch<decorators, T, BARRIER_STORE_AT>::store_at_init(oop base, ptrdiff_t offset, T value) {
 298     func_t function = BarrierResolver<decorators, func_t, BARRIER_STORE_AT>::resolve_barrier();
 299     _store_at_func = function;
 300     function(base, offset, value);
 301   }
 302 
 303   template <DecoratorSet decorators, typename T>
 304   T RuntimeDispatch<decorators, T, BARRIER_LOAD>::load_init(void* addr) {
 305     func_t function = BarrierResolver<decorators, func_t, BARRIER_LOAD>::resolve_barrier();
 306     _load_func = function;
 307     return function(addr);
 308   }
 309 
 310   template <DecoratorSet decorators, typename T>
 311   T RuntimeDispatch<decorators, T, BARRIER_LOAD_AT>::load_at_init(oop base, ptrdiff_t offset) {
 312     func_t function = BarrierResolver<decorators, func_t, BARRIER_LOAD_AT>::resolve_barrier();
 313     _load_at_func = function;
 314     return function(base, offset);
 315   }
 316 
 317   template <DecoratorSet decorators, typename T>
 318   T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_CMPXCHG>::atomic_cmpxchg_init(T new_value, void* addr, T compare_value) {
 319     func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_CMPXCHG>::resolve_barrier();
 320     _atomic_cmpxchg_func = function;
 321     return function(new_value, addr, compare_value);
 322   }
 323 
 324   template <DecoratorSet decorators, typename T>
 325   T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_CMPXCHG_AT>::atomic_cmpxchg_at_init(T new_value, oop base, ptrdiff_t offset, T compare_value) {
 326     func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_CMPXCHG_AT>::resolve_barrier();
 327     _atomic_cmpxchg_at_func = function;
 328     return function(new_value, base, offset, compare_value);
 329   }
 330 
 331   template <DecoratorSet decorators, typename T>
 332   T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_XCHG>::atomic_xchg_init(T new_value, void* addr) {
 333     func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_XCHG>::resolve_barrier();
 334     _atomic_xchg_func = function;
 335     return function(new_value, addr);
 336   }
 337 
 338   template <DecoratorSet decorators, typename T>
 339   T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_XCHG_AT>::atomic_xchg_at_init(T new_value, oop base, ptrdiff_t offset) {
 340     func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_XCHG_AT>::resolve_barrier();
 341     _atomic_xchg_at_func = function;
 342     return function(new_value, base, offset);
 343   }
 344 
 345   template <DecoratorSet decorators, typename T>
 346   void RuntimeDispatch<decorators, T, BARRIER_ARRAYCOPY>::arraycopy_init(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 347                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 348                                                                          size_t length) {
 349     func_t function = BarrierResolver<decorators, func_t, BARRIER_ARRAYCOPY>::resolve_barrier();
 350     _arraycopy_func = function;
 351     function(src_obj, src_offset_in_bytes, src_raw,
 352              dst_obj, dst_offset_in_bytes, dst_raw,
 353              length);
 354   }
 355 
 356   template <DecoratorSet decorators, typename T>
 357   void RuntimeDispatch<decorators, T, BARRIER_CLONE>::clone_init(oop src, oop dst, size_t size) {
 358     func_t function = BarrierResolver<decorators, func_t, BARRIER_CLONE>::resolve_barrier();
 359     _clone_func = function;
 360     function(src, dst, size);
 361   }
 362 
 363   template <DecoratorSet decorators, typename T>
 364   void RuntimeDispatch<decorators, T, BARRIER_VALUE_COPY>::value_copy_init(void* src, void* dst, ValueKlass* md) {
 365     func_t function = BarrierResolver<decorators, func_t, BARRIER_VALUE_COPY>::resolve_barrier();
 366     _value_copy_func = function;
 367     function(src, dst, md);
 368   }
 369 
 370   template <DecoratorSet decorators, typename T>
 371   oop RuntimeDispatch<decorators, T, BARRIER_RESOLVE>::resolve_init(oop obj) {
 372     func_t function = BarrierResolver<decorators, func_t, BARRIER_RESOLVE>::resolve_barrier();
 373     _resolve_func = function;
 374     return function(obj);
 375   }
 376 }
 377 
 378 #endif // SHARE_OOPS_ACCESS_INLINE_HPP