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_HPP
 26 #define SHARE_OOPS_ACCESS_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "oops/accessBackend.hpp"
 30 #include "oops/accessDecorators.hpp"
 31 #include "oops/oopsHierarchy.hpp"
 32 #include "utilities/debug.hpp"
 33 #include "utilities/globalDefinitions.hpp"
 34 
 35 
 36 // = GENERAL =
 37 // Access is an API for performing accesses with declarative semantics. Each access can have a number of "decorators".
 38 // A decorator is an attribute or property that affects the way a memory access is performed in some way.
 39 // There are different groups of decorators. Some have to do with memory ordering, others to do with,
 40 // e.g. strength of references, strength of GC barriers, or whether compression should be applied or not.
 41 // Some decorators are set at buildtime, such as whether primitives require GC barriers or not, others
 42 // at callsites such as whether an access is in the heap or not, and others are resolved at runtime
 43 // such as GC-specific barriers and encoding/decoding compressed oops. For more information about what
 44 // decorators are available, cf. oops/accessDecorators.hpp.
 45 // By pipelining handling of these decorators, the design of the Access API allows separation of concern
 46 // over the different orthogonal concerns of decorators, while providing a powerful way of
 47 // expressing these orthogonal semantic properties in a unified way.
 48 //
 49 // == OPERATIONS ==
 50 // * load: Load a value from an address.
 51 // * load_at: Load a value from an internal pointer relative to a base object.
 52 // * store: Store a value at an address.
 53 // * store_at: Store a value in an internal pointer relative to a base object.
 54 // * atomic_cmpxchg: Atomically compare-and-swap a new value at an address if previous value matched the compared value.
 55 // * atomic_cmpxchg_at: Atomically compare-and-swap a new value at an internal pointer address if previous value matched the compared value.
 56 // * atomic_xchg: Atomically swap a new value at an address if previous value matched the compared value.
 57 // * atomic_xchg_at: Atomically swap a new value at an internal pointer address if previous value matched the compared value.
 58 // * arraycopy: Copy data from one heap array to another heap array.
 59 // * clone: Clone the contents of an object to a newly allocated object.
 60 // * resolve: Resolve a stable to-space invariant oop that is guaranteed not to relocate its payload until a subsequent thread transition.
 61 // * equals: Object equality, e.g. when different copies of the same objects are in use (from-space vs. to-space)
 62 //
 63 // == IMPLEMENTATION ==
 64 // Each access goes through the following steps in a template pipeline.
 65 // There are essentially 5 steps for each access:
 66 // * Step 1:   Set default decorators and decay types. This step gets rid of CV qualifiers
 67 //             and sets default decorators to sensible values.
 68 // * Step 2:   Reduce types. This step makes sure there is only a single T type and not
 69 //             multiple types. The P type of the address and T type of the value must
 70 //             match.
 71 // * Step 3:   Pre-runtime dispatch. This step checks whether a runtime call can be
 72 //             avoided, and in that case avoids it (calling raw accesses or
 73 //             primitive accesses in a build that does not require primitive GC barriers)
 74 // * Step 4:   Runtime-dispatch. This step performs a runtime dispatch to the corresponding
 75 //             BarrierSet::AccessBarrier accessor that attaches GC-required barriers
 76 //             to the access.
 77 // * Step 5.a: Barrier resolution. This step is invoked the first time a runtime-dispatch
 78 //             happens for an access. The appropriate BarrierSet::AccessBarrier accessor
 79 //             is resolved, then the function pointer is updated to that accessor for
 80 //             future invocations.
 81 // * Step 5.b: Post-runtime dispatch. This step now casts previously unknown types such
 82 //             as the address type of an oop on the heap (is it oop* or narrowOop*) to
 83 //             the appropriate type. It also splits sufficiently orthogonal accesses into
 84 //             different functions, such as whether the access involves oops or primitives
 85 //             and whether the access is performed on the heap or outside. Then the
 86 //             appropriate BarrierSet::AccessBarrier is called to perform the access.
 87 //
 88 // The implementation of step 1-4 resides in in accessBackend.hpp, to allow selected
 89 // accesses to be accessible from only access.hpp, as opposed to access.inline.hpp.
 90 // Steps 5.a and 5.b require knowledge about the GC backends, and therefore needs to
 91 // include the various GC backend .inline.hpp headers. Their implementation resides in
 92 // access.inline.hpp. The accesses that are allowed through the access.hpp file
 93 // must be instantiated in access.cpp using the INSTANTIATE_HPP_ACCESS macro.
 94 
 95 namespace AccessInternal {
 96   template <DecoratorSet decorators, typename T>
 97   void store_at(oop base, ptrdiff_t offset, T value);
 98 
 99   template <DecoratorSet decorators, typename T>
100   T load_at(oop base, ptrdiff_t offset);
101 
102   template <DecoratorSet decorators, typename T>
103   T atomic_cmpxchg_at(T new_value, oop base, ptrdiff_t offset, T compare_value);
104 
105   template <DecoratorSet decorators, typename T>
106   T atomic_xchg_at(T new_value, oop base, ptrdiff_t offset);
107 
108   template <DecoratorSet decorators, typename P, typename T>
109   void store(P* addr, T value);
110 
111   template <DecoratorSet decorators, typename P, typename T>
112   T load(P* addr);
113 
114   template <DecoratorSet decorators, typename P, typename T>
115   T atomic_cmpxchg(T new_value, P* addr, T compare_value);
116 
117   template <DecoratorSet decorators, typename P, typename T>
118   T atomic_xchg(T new_value, P* addr);
119 
120   template <DecoratorSet decorators, typename T>
121   bool arraycopy(arrayOop src_obj, arrayOop dst_obj, T *src, T *dst, size_t length);
122 
123   template <DecoratorSet decorators>
124   void clone(oop src, oop dst, size_t size);
125 
126   template <DecoratorSet decorators>
127   oop resolve(oop src);
128 
129   template <DecoratorSet decorators>
130   bool equals(oop o1, oop o2);
131 
132   // Infer the type that should be returned from a load.
133   template <typename P, DecoratorSet decorators>
134   class OopLoadProxy: public StackObj {
135   private:
136     P *const _addr;
137   public:
138     OopLoadProxy(P* addr) : _addr(addr) {}
139 
140     inline operator oop() {
141       return load<decorators | INTERNAL_VALUE_IS_OOP, P, oop>(_addr);
142     }
143 
144     inline operator narrowOop() {
145       return load<decorators | INTERNAL_VALUE_IS_OOP, P, narrowOop>(_addr);
146     }
147 
148     template <typename T>
149     inline bool operator ==(const T& other) const {
150       return load<decorators | INTERNAL_VALUE_IS_OOP, P, T>(_addr) == other;
151     }
152 
153     template <typename T>
154     inline bool operator !=(const T& other) const {
155       return load<decorators | INTERNAL_VALUE_IS_OOP, P, T>(_addr) != other;
156     }
157   };
158 
159   // Infer the type that should be returned from a load_at.
160   template <DecoratorSet decorators>
161   class LoadAtProxy: public StackObj {
162   private:
163     const oop _base;
164     const ptrdiff_t _offset;
165   public:
166     LoadAtProxy(oop base, ptrdiff_t offset) : _base(base), _offset(offset) {}
167 
168     template <typename T>
169     inline operator T() const {
170       return load_at<decorators, T>(_base, _offset);
171     }
172 
173     template <typename T>
174     inline bool operator ==(const T& other) const { return load_at<decorators, T>(_base, _offset) == other; }
175 
176     template <typename T>
177     inline bool operator !=(const T& other) const { return load_at<decorators, T>(_base, _offset) != other; }
178   };
179 
180   template <DecoratorSet decorators>
181   class OopLoadAtProxy: public StackObj {
182   private:
183     const oop _base;
184     const ptrdiff_t _offset;
185   public:
186     OopLoadAtProxy(oop base, ptrdiff_t offset) : _base(base), _offset(offset) {}
187 
188     inline operator oop() const {
189       return load_at<decorators | INTERNAL_VALUE_IS_OOP, oop>(_base, _offset);
190     }
191 
192     inline operator narrowOop() const {
193       return load_at<decorators | INTERNAL_VALUE_IS_OOP, narrowOop>(_base, _offset);
194     }
195 
196     template <typename T>
197     inline bool operator ==(const T& other) const {
198       return load_at<decorators | INTERNAL_VALUE_IS_OOP, T>(_base, _offset) == other;
199     }
200 
201     template <typename T>
202     inline bool operator !=(const T& other) const {
203       return load_at<decorators | INTERNAL_VALUE_IS_OOP, T>(_base, _offset) != other;
204     }
205   };
206 }
207 
208 template <DecoratorSet decorators = INTERNAL_EMPTY>
209 class Access: public AllStatic {
210   // This function asserts that if an access gets passed in a decorator outside
211   // of the expected_decorators, then something is wrong. It additionally checks
212   // the consistency of the decorators so that supposedly disjoint decorators are indeed
213   // disjoint. For example, an access can not be both in heap and on root at the
214   // same time.
215   template <DecoratorSet expected_decorators>
216   static void verify_decorators();
217 
218   template <DecoratorSet expected_mo_decorators>
219   static void verify_primitive_decorators() {
220     const DecoratorSet primitive_decorators = (AS_DECORATOR_MASK ^ AS_NO_KEEPALIVE ^ AS_DEST_NOT_INITIALIZED) |
221                                               IN_HEAP | IN_HEAP_ARRAY;
222     verify_decorators<expected_mo_decorators | primitive_decorators>();
223   }
224 
225   template <DecoratorSet expected_mo_decorators>
226   static void verify_oop_decorators() {
227     const DecoratorSet oop_decorators = AS_DECORATOR_MASK | IN_DECORATOR_MASK |
228                                         (ON_DECORATOR_MASK ^ ON_UNKNOWN_OOP_REF) | // no unknown oop refs outside of the heap
229                                         OOP_DECORATOR_MASK;
230     verify_decorators<expected_mo_decorators | oop_decorators>();
231   }
232 
233   template <DecoratorSet expected_mo_decorators>
234   static void verify_heap_oop_decorators() {
235     const DecoratorSet heap_oop_decorators = AS_DECORATOR_MASK | ON_DECORATOR_MASK |
236                                              OOP_DECORATOR_MASK | (IN_DECORATOR_MASK ^
237                                                                    (IN_ROOT | IN_CONCURRENT_ROOT)); // no root accesses in the heap
238     verify_decorators<expected_mo_decorators | heap_oop_decorators>();
239   }
240 
241   static const DecoratorSet load_mo_decorators = MO_UNORDERED | MO_VOLATILE | MO_RELAXED | MO_ACQUIRE | MO_SEQ_CST;
242   static const DecoratorSet store_mo_decorators = MO_UNORDERED | MO_VOLATILE | MO_RELAXED | MO_RELEASE | MO_SEQ_CST;
243   static const DecoratorSet atomic_xchg_mo_decorators = MO_SEQ_CST;
244   static const DecoratorSet atomic_cmpxchg_mo_decorators = MO_RELAXED | MO_SEQ_CST;
245 
246 public:
247   // Primitive heap accesses
248   static inline AccessInternal::LoadAtProxy<decorators> load_at(oop base, ptrdiff_t offset) {
249     verify_primitive_decorators<load_mo_decorators>();
250     return AccessInternal::LoadAtProxy<decorators>(base, offset);
251   }
252 
253   template <typename T>
254   static inline void store_at(oop base, ptrdiff_t offset, T value) {
255     verify_primitive_decorators<store_mo_decorators>();
256     AccessInternal::store_at<decorators>(base, offset, value);
257   }
258 
259   template <typename T>
260   static inline T atomic_cmpxchg_at(T new_value, oop base, ptrdiff_t offset, T compare_value) {
261     verify_primitive_decorators<atomic_cmpxchg_mo_decorators>();
262     return AccessInternal::atomic_cmpxchg_at<decorators>(new_value, base, offset, compare_value);
263   }
264 
265   template <typename T>
266   static inline T atomic_xchg_at(T new_value, oop base, ptrdiff_t offset) {
267     verify_primitive_decorators<atomic_xchg_mo_decorators>();
268     return AccessInternal::atomic_xchg_at<decorators>(new_value, base, offset);
269   }
270 
271   template <typename T>
272   static inline void arraycopy(arrayOop src_obj, arrayOop dst_obj, T *src, T *dst, size_t length) {
273     verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP |
274                       AS_DECORATOR_MASK>();
275     AccessInternal::arraycopy<decorators>(src_obj, dst_obj, src, dst, length);
276   }
277 
278   // Oop heap accesses
279   static inline AccessInternal::OopLoadAtProxy<decorators> oop_load_at(oop base, ptrdiff_t offset) {
280     verify_heap_oop_decorators<load_mo_decorators>();
281     return AccessInternal::OopLoadAtProxy<decorators>(base, offset);
282   }
283 
284   template <typename T>
285   static inline void oop_store_at(oop base, ptrdiff_t offset, T value) {
286     verify_heap_oop_decorators<store_mo_decorators>();
287     typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
288     OopType oop_value = value;
289     AccessInternal::store_at<decorators | INTERNAL_VALUE_IS_OOP>(base, offset, oop_value);
290   }
291 
292   template <typename T>
293   static inline T oop_atomic_cmpxchg_at(T new_value, oop base, ptrdiff_t offset, T compare_value) {
294     verify_heap_oop_decorators<atomic_cmpxchg_mo_decorators>();
295     typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
296     OopType new_oop_value = new_value;
297     OopType compare_oop_value = compare_value;
298     return AccessInternal::atomic_cmpxchg_at<decorators | INTERNAL_VALUE_IS_OOP>(new_oop_value, base, offset, compare_oop_value);
299   }
300 
301   template <typename T>
302   static inline T oop_atomic_xchg_at(T new_value, oop base, ptrdiff_t offset) {
303     verify_heap_oop_decorators<atomic_xchg_mo_decorators>();
304     typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
305     OopType new_oop_value = new_value;
306     return AccessInternal::atomic_xchg_at<decorators | INTERNAL_VALUE_IS_OOP>(new_oop_value, base, offset);
307   }
308 
309   template <typename T>
310   static inline bool oop_arraycopy(arrayOop src_obj, arrayOop dst_obj, T *src, T *dst, size_t length) {
311     verify_decorators<ARRAYCOPY_DECORATOR_MASK | IN_HEAP | AS_DECORATOR_MASK>();
312     return AccessInternal::arraycopy<decorators | INTERNAL_VALUE_IS_OOP>(src_obj, dst_obj, src, dst, length);
313   }
314 
315   // Clone an object from src to dst
316   static inline void clone(oop src, oop dst, size_t size) {
317     verify_decorators<IN_HEAP>();
318     AccessInternal::clone<decorators>(src, dst, size);
319   }
320 
321   // Primitive accesses
322   template <typename P>
323   static inline P load(P* addr) {
324     verify_primitive_decorators<load_mo_decorators>();
325     return AccessInternal::load<decorators, P, P>(addr);
326   }
327 
328   template <typename P, typename T>
329   static inline void store(P* addr, T value) {
330     verify_primitive_decorators<store_mo_decorators>();
331     AccessInternal::store<decorators>(addr, value);
332   }
333 
334   template <typename P, typename T>
335   static inline T atomic_cmpxchg(T new_value, P* addr, T compare_value) {
336     verify_primitive_decorators<atomic_cmpxchg_mo_decorators>();
337     return AccessInternal::atomic_cmpxchg<decorators>(new_value, addr, compare_value);
338   }
339 
340   template <typename P, typename T>
341   static inline T atomic_xchg(T new_value, P* addr) {
342     verify_primitive_decorators<atomic_xchg_mo_decorators>();
343     return AccessInternal::atomic_xchg<decorators>(new_value, addr);
344   }
345 
346   // Oop accesses
347   template <typename P>
348   static inline AccessInternal::OopLoadProxy<P, decorators> oop_load(P* addr) {
349     verify_oop_decorators<load_mo_decorators>();
350     return AccessInternal::OopLoadProxy<P, decorators>(addr);
351   }
352 
353   template <typename P, typename T>
354   static inline void oop_store(P* addr, T value) {
355     verify_oop_decorators<store_mo_decorators>();
356     typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
357     OopType oop_value = value;
358     AccessInternal::store<decorators | INTERNAL_VALUE_IS_OOP>(addr, oop_value);
359   }
360 
361   template <typename P, typename T>
362   static inline T oop_atomic_cmpxchg(T new_value, P* addr, T compare_value) {
363     verify_oop_decorators<atomic_cmpxchg_mo_decorators>();
364     typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
365     OopType new_oop_value = new_value;
366     OopType compare_oop_value = compare_value;
367     return AccessInternal::atomic_cmpxchg<decorators | INTERNAL_VALUE_IS_OOP>(new_oop_value, addr, compare_oop_value);
368   }
369 
370   template <typename P, typename T>
371   static inline T oop_atomic_xchg(T new_value, P* addr) {
372     verify_oop_decorators<atomic_xchg_mo_decorators>();
373     typedef typename AccessInternal::OopOrNarrowOop<T>::type OopType;
374     OopType new_oop_value = new_value;
375     return AccessInternal::atomic_xchg<decorators | INTERNAL_VALUE_IS_OOP>(new_oop_value, addr);
376   }
377 
378   static oop resolve(oop obj) {
379     verify_decorators<INTERNAL_EMPTY>();
380     return AccessInternal::resolve<decorators>(obj);
381   }
382 
383   static bool equals(oop o1, oop o2) {
384     verify_decorators<INTERNAL_EMPTY>();
385     return AccessInternal::equals<decorators>(o1, o2);
386   }
387 };
388 
389 // Helper for performing raw accesses (knows only of memory ordering
390 // atomicity decorators as well as compressed oops)
391 template <DecoratorSet decorators = INTERNAL_EMPTY>
392 class RawAccess: public Access<AS_RAW | decorators> {};
393 
394 // Helper for performing normal accesses on the heap. These accesses
395 // may resolve an accessor on a GC barrier set
396 template <DecoratorSet decorators = INTERNAL_EMPTY>
397 class HeapAccess: public Access<IN_HEAP | decorators> {};
398 
399 // Helper for performing normal accesses in roots. These accesses
400 // may resolve an accessor on a GC barrier set
401 template <DecoratorSet decorators = INTERNAL_EMPTY>
402 class RootAccess: public Access<IN_ROOT | decorators> {};
403 
404 template <DecoratorSet decorators>
405 template <DecoratorSet expected_decorators>
406 void Access<decorators>::verify_decorators() {
407   STATIC_ASSERT((~expected_decorators & decorators) == 0); // unexpected decorator used
408   const DecoratorSet barrier_strength_decorators = decorators & AS_DECORATOR_MASK;
409   STATIC_ASSERT(barrier_strength_decorators == 0 || ( // make sure barrier strength decorators are disjoint if set
410     (barrier_strength_decorators ^ AS_NO_KEEPALIVE) == 0 ||
411     (barrier_strength_decorators ^ AS_DEST_NOT_INITIALIZED) == 0 ||
412     (barrier_strength_decorators ^ AS_RAW) == 0 ||
413     (barrier_strength_decorators ^ AS_NORMAL) == 0
414   ));
415   const DecoratorSet ref_strength_decorators = decorators & ON_DECORATOR_MASK;
416   STATIC_ASSERT(ref_strength_decorators == 0 || ( // make sure ref strength decorators are disjoint if set
417     (ref_strength_decorators ^ ON_STRONG_OOP_REF) == 0 ||
418     (ref_strength_decorators ^ ON_WEAK_OOP_REF) == 0 ||
419     (ref_strength_decorators ^ ON_PHANTOM_OOP_REF) == 0 ||
420     (ref_strength_decorators ^ ON_UNKNOWN_OOP_REF) == 0
421   ));
422   const DecoratorSet memory_ordering_decorators = decorators & MO_DECORATOR_MASK;
423   STATIC_ASSERT(memory_ordering_decorators == 0 || ( // make sure memory ordering decorators are disjoint if set
424     (memory_ordering_decorators ^ MO_UNORDERED) == 0 ||
425     (memory_ordering_decorators ^ MO_VOLATILE) == 0 ||
426     (memory_ordering_decorators ^ MO_RELAXED) == 0 ||
427     (memory_ordering_decorators ^ MO_ACQUIRE) == 0 ||
428     (memory_ordering_decorators ^ MO_RELEASE) == 0 ||
429     (memory_ordering_decorators ^ MO_SEQ_CST) == 0
430   ));
431   const DecoratorSet location_decorators = decorators & IN_DECORATOR_MASK;
432   STATIC_ASSERT(location_decorators == 0 || ( // make sure location decorators are disjoint if set
433     (location_decorators ^ IN_ROOT) == 0 ||
434     (location_decorators ^ IN_HEAP) == 0 ||
435     (location_decorators ^ (IN_HEAP | IN_HEAP_ARRAY)) == 0 ||
436     (location_decorators ^ (IN_ROOT | IN_CONCURRENT_ROOT)) == 0 ||
437     (location_decorators ^ (IN_ROOT | IN_ARCHIVE_ROOT)) == 0
438   ));
439 }
440 
441 #endif // SHARE_OOPS_ACCESS_HPP