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