/* * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #ifndef SHARE_OOPS_ACCESS_HPP #define SHARE_OOPS_ACCESS_HPP #include "memory/allocation.hpp" #include "oops/accessBackend.hpp" #include "oops/accessDecorators.hpp" #include "oops/oopsHierarchy.hpp" #include "utilities/debug.hpp" #include "utilities/globalDefinitions.hpp" // = GENERAL = // Access is an API for performing accesses with declarative semantics. Each access can have a number of "decorators". // A decorator is an attribute or property that affects the way a memory access is performed in some way. // There are different groups of decorators. Some have to do with memory ordering, others to do with, // e.g. strength of references, strength of GC barriers, or whether compression should be applied or not. // Some decorators are set at buildtime, such as whether primitives require GC barriers or not, others // at callsites such as whether an access is in the heap or not, and others are resolved at runtime // such as GC-specific barriers and encoding/decoding compressed oops. For more information about what // decorators are available, cf. oops/accessDecorators.hpp. // By pipelining handling of these decorators, the design of the Access API allows separation of concern // over the different orthogonal concerns of decorators, while providing a powerful way of // expressing these orthogonal semantic properties in a unified way. // // == OPERATIONS == // * load: Load a value from an address. // * load_at: Load a value from an internal pointer relative to a base object. // * store: Store a value at an address. // * store_at: Store a value in an internal pointer relative to a base object. // * atomic_cmpxchg: Atomically compare-and-swap a new value at an address if previous value matched the compared value. // * atomic_cmpxchg_at: Atomically compare-and-swap a new value at an internal pointer address if previous value matched the compared value. // * atomic_xchg: Atomically swap a new value at an address if previous value matched the compared value. // * atomic_xchg_at: Atomically swap a new value at an internal pointer address if previous value matched the compared value. // * arraycopy: Copy data from one heap array to another heap array. // * clone: Clone the contents of an object to a newly allocated object. // * resolve: Resolve a stable to-space invariant oop that is guaranteed not to relocate its payload until a subsequent thread transition. // * equals: Object equality, e.g. when different copies of the same objects are in use (from-space vs. to-space) // // == IMPLEMENTATION == // Each access goes through the following steps in a template pipeline. // There are essentially 5 steps for each access: // * Step 1: Set default decorators and decay types. This step gets rid of CV qualifiers // and sets default decorators to sensible values. // * Step 2: Reduce types. This step makes sure there is only a single T type and not // multiple types. The P type of the address and T type of the value must // match. // * Step 3: Pre-runtime dispatch. This step checks whether a runtime call can be // avoided, and in that case avoids it (calling raw accesses or // primitive accesses in a build that does not require primitive GC barriers) // * Step 4: Runtime-dispatch. This step performs a runtime dispatch to the corresponding // BarrierSet::AccessBarrier accessor that attaches GC-required barriers // to the access. // * Step 5.a: Barrier resolution. This step is invoked the first time a runtime-dispatch // happens for an access. The appropriate BarrierSet::AccessBarrier accessor // is resolved, then the function pointer is updated to that accessor for // future invocations. // * Step 5.b: Post-runtime dispatch. This step now casts previously unknown types such // as the address type of an oop on the heap (is it oop* or narrowOop*) to // the appropriate type. It also splits sufficiently orthogonal accesses into // different functions, such as whether the access involves oops or primitives // and whether the access is performed on the heap or outside. Then the // appropriate BarrierSet::AccessBarrier is called to perform the access. // // The implementation of step 1-4 resides in in accessBackend.hpp, to allow selected // accesses to be accessible from only access.hpp, as opposed to access.inline.hpp. // Steps 5.a and 5.b require knowledge about the GC backends, and therefore needs to // include the various GC backend .inline.hpp headers. Their implementation resides in // access.inline.hpp. The accesses that are allowed through the access.hpp file // must be instantiated in access.cpp using the INSTANTIATE_HPP_ACCESS macro. template class Access: public AllStatic { // This function asserts that if an access gets passed in a decorator outside // of the expected_decorators, then something is wrong. It additionally checks // the consistency of the decorators so that supposedly disjoint decorators are indeed // disjoint. For example, an access can not be both in heap and on root at the // same time. template static void verify_decorators(); template static void verify_primitive_decorators() { const DecoratorSet primitive_decorators = (AS_DECORATOR_MASK ^ AS_NO_KEEPALIVE ^ AS_DEST_NOT_INITIALIZED) | IN_HEAP | IN_HEAP_ARRAY; verify_decorators(); } template static void verify_oop_decorators() { const DecoratorSet oop_decorators = AS_DECORATOR_MASK | IN_DECORATOR_MASK | (ON_DECORATOR_MASK ^ ON_UNKNOWN_OOP_REF) | // no unknown oop refs outside of the heap OOP_DECORATOR_MASK; verify_decorators(); } template static void verify_heap_oop_decorators() { const DecoratorSet heap_oop_decorators = AS_DECORATOR_MASK | ON_DECORATOR_MASK | OOP_DECORATOR_MASK | (IN_DECORATOR_MASK ^ (IN_ROOT | IN_CONCURRENT_ROOT)); // no root accesses in the heap verify_decorators(); } static const DecoratorSet load_mo_decorators = MO_UNORDERED | MO_VOLATILE | MO_RELAXED | MO_ACQUIRE | MO_SEQ_CST; static const DecoratorSet store_mo_decorators = MO_UNORDERED | MO_VOLATILE | MO_RELAXED | MO_RELEASE | MO_SEQ_CST; static const DecoratorSet atomic_xchg_mo_decorators = MO_SEQ_CST; static const DecoratorSet atomic_cmpxchg_mo_decorators = MO_RELAXED | MO_SEQ_CST; public: // Primitive heap accesses static inline AccessInternal::LoadAtProxy load_at(oop base, ptrdiff_t offset) { verify_primitive_decorators(); return AccessInternal::LoadAtProxy(base, offset); } template static inline void store_at(oop base, ptrdiff_t offset, T value) { verify_primitive_decorators(); AccessInternal::store_at(base, offset, value); } template static inline T atomic_cmpxchg_at(T new_value, oop base, ptrdiff_t offset, T compare_value) { verify_primitive_decorators(); return AccessInternal::atomic_cmpxchg_at(new_value, base, offset, compare_value); } template static inline T atomic_xchg_at(T new_value, oop base, ptrdiff_t offset) { verify_primitive_decorators(); return AccessInternal::atomic_xchg_at(new_value, base, offset); } template static inline void arraycopy(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) { verify_decorators(); AccessInternal::arraycopy(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length); } // Oop heap accesses static inline AccessInternal::OopLoadAtProxy oop_load_at(oop base, ptrdiff_t offset) { verify_heap_oop_decorators(); return AccessInternal::OopLoadAtProxy(base, offset); } template static inline void oop_store_at(oop base, ptrdiff_t offset, T value) { verify_heap_oop_decorators(); typedef typename AccessInternal::OopOrNarrowOop::type OopType; OopType oop_value = value; AccessInternal::store_at(base, offset, oop_value); } template static inline T oop_atomic_cmpxchg_at(T new_value, oop base, ptrdiff_t offset, T compare_value) { verify_heap_oop_decorators(); typedef typename AccessInternal::OopOrNarrowOop::type OopType; OopType new_oop_value = new_value; OopType compare_oop_value = compare_value; return AccessInternal::atomic_cmpxchg_at(new_oop_value, base, offset, compare_oop_value); } template static inline T oop_atomic_xchg_at(T new_value, oop base, ptrdiff_t offset) { verify_heap_oop_decorators(); typedef typename AccessInternal::OopOrNarrowOop::type OopType; OopType new_oop_value = new_value; return AccessInternal::atomic_xchg_at(new_oop_value, base, offset); } template static inline bool oop_arraycopy(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) { verify_decorators(); return AccessInternal::arraycopy(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length); } // Clone an object from src to dst static inline void clone(oop src, oop dst, size_t size) { verify_decorators(); AccessInternal::clone(src, dst, size); } // Primitive accesses template static inline P load(P* addr) { verify_primitive_decorators(); return AccessInternal::load(addr); } template static inline void store(P* addr, T value) { verify_primitive_decorators(); AccessInternal::store(addr, value); } template static inline T atomic_cmpxchg(T new_value, P* addr, T compare_value) { verify_primitive_decorators(); return AccessInternal::atomic_cmpxchg(new_value, addr, compare_value); } template static inline T atomic_xchg(T new_value, P* addr) { verify_primitive_decorators(); return AccessInternal::atomic_xchg(new_value, addr); } // Oop accesses template static inline AccessInternal::OopLoadProxy oop_load(P* addr) { verify_oop_decorators(); return AccessInternal::OopLoadProxy(addr); } template static inline void oop_store(P* addr, T value) { verify_oop_decorators(); typedef typename AccessInternal::OopOrNarrowOop::type OopType; OopType oop_value = value; AccessInternal::store(addr, oop_value); } template static inline T oop_atomic_cmpxchg(T new_value, P* addr, T compare_value) { verify_oop_decorators(); typedef typename AccessInternal::OopOrNarrowOop::type OopType; OopType new_oop_value = new_value; OopType compare_oop_value = compare_value; return AccessInternal::atomic_cmpxchg(new_oop_value, addr, compare_oop_value); } template static inline T oop_atomic_xchg(T new_value, P* addr) { verify_oop_decorators(); typedef typename AccessInternal::OopOrNarrowOop::type OopType; OopType new_oop_value = new_value; return AccessInternal::atomic_xchg(new_oop_value, addr); } static oop resolve(oop obj) { verify_decorators(); return AccessInternal::resolve(obj); } static bool equals(oop o1, oop o2) { verify_decorators(); return AccessInternal::equals(o1, o2); } }; // Helper for performing raw accesses (knows only of memory ordering // atomicity decorators as well as compressed oops) template class RawAccess: public Access {}; // Helper for performing normal accesses on the heap. These accesses // may resolve an accessor on a GC barrier set template class HeapAccess: public Access {}; // Helper for performing normal accesses in roots. These accesses // may resolve an accessor on a GC barrier set template class RootAccess: public Access {}; // Helper for array access. template class ArrayAccess: public HeapAccess { public: template static inline void arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, arrayOop dst_obj, size_t dst_offset_in_bytes, size_t length) { HeapAccess::arraycopy(src_obj, src_offset_in_bytes, (const T*) NULL, dst_obj, dst_offset_in_bytes, (T*) NULL, length); } template static inline void arraycopy_to_native(arrayOop src_obj, size_t src_offset_in_bytes, T* dst, size_t length) { HeapAccess::arraycopy(src_obj, src_offset_in_bytes, (const T*) NULL, NULL, 0, dst, length); } template static inline void arraycopy_from_native(const T* src, arrayOop dst_obj, size_t dst_offset_in_bytes, size_t length) { HeapAccess::arraycopy(NULL, 0, src, dst_obj, dst_offset_in_bytes, (T*) NULL, length); } template static inline bool oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, arrayOop dst_obj, size_t dst_offset_in_bytes, size_t length) { return HeapAccess::oop_arraycopy(src_obj, src_offset_in_bytes, (const T*) NULL, dst_obj, dst_offset_in_bytes, (T*) NULL, length); } template static inline bool oop_arraycopy_raw(T* src, T* dst, size_t length) { return HeapAccess::oop_arraycopy(NULL, 0, src, NULL, 0, dst, length); } }; template template void Access::verify_decorators() { STATIC_ASSERT((~expected_decorators & decorators) == 0); // unexpected decorator used const DecoratorSet barrier_strength_decorators = decorators & AS_DECORATOR_MASK; STATIC_ASSERT(barrier_strength_decorators == 0 || ( // make sure barrier strength decorators are disjoint if set (barrier_strength_decorators ^ AS_NO_KEEPALIVE) == 0 || (barrier_strength_decorators ^ AS_DEST_NOT_INITIALIZED) == 0 || (barrier_strength_decorators ^ AS_RAW) == 0 || (barrier_strength_decorators ^ AS_NORMAL) == 0 )); const DecoratorSet ref_strength_decorators = decorators & ON_DECORATOR_MASK; STATIC_ASSERT(ref_strength_decorators == 0 || ( // make sure ref strength decorators are disjoint if set (ref_strength_decorators ^ ON_STRONG_OOP_REF) == 0 || (ref_strength_decorators ^ ON_WEAK_OOP_REF) == 0 || (ref_strength_decorators ^ ON_PHANTOM_OOP_REF) == 0 || (ref_strength_decorators ^ ON_UNKNOWN_OOP_REF) == 0 )); const DecoratorSet memory_ordering_decorators = decorators & MO_DECORATOR_MASK; STATIC_ASSERT(memory_ordering_decorators == 0 || ( // make sure memory ordering decorators are disjoint if set (memory_ordering_decorators ^ MO_UNORDERED) == 0 || (memory_ordering_decorators ^ MO_VOLATILE) == 0 || (memory_ordering_decorators ^ MO_RELAXED) == 0 || (memory_ordering_decorators ^ MO_ACQUIRE) == 0 || (memory_ordering_decorators ^ MO_RELEASE) == 0 || (memory_ordering_decorators ^ MO_SEQ_CST) == 0 )); const DecoratorSet location_decorators = decorators & IN_DECORATOR_MASK; STATIC_ASSERT(location_decorators == 0 || ( // make sure location decorators are disjoint if set (location_decorators ^ IN_ROOT) == 0 || (location_decorators ^ IN_HEAP) == 0 || (location_decorators ^ (IN_HEAP | IN_HEAP_ARRAY)) == 0 || (location_decorators ^ (IN_ROOT | IN_CONCURRENT_ROOT)) == 0 || (location_decorators ^ (IN_ROOT | IN_ARCHIVE_ROOT)) == 0 )); } #endif // SHARE_OOPS_ACCESS_HPP