1 /*
   2  * Copyright (c) 2013, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
  26 
  27 #include "gc/shared/accessBarrierSupport.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
  31 
  32 class ShenandoahBarrierSetAssembler;
  33 
  34 class ShenandoahBarrierSet: public BarrierSet {
  35 public:
  36   enum ArrayCopyStoreValMode {
  37     NONE,
  38     RESOLVE_BARRIER,
  39     EVAC_BARRIER
  40   };
  41 private:
  42 
  43   ShenandoahHeap* _heap;
  44   BufferNode::Allocator _satb_mark_queue_buffer_allocator;
  45   ShenandoahSATBMarkQueueSet _satb_mark_queue_set;
  46 
  47 public:
  48   ShenandoahBarrierSet(ShenandoahHeap* heap);
  49 
  50   static ShenandoahBarrierSetAssembler* assembler();
  51 
  52   inline static ShenandoahBarrierSet* barrier_set() {
  53     return barrier_set_cast<ShenandoahBarrierSet>(BarrierSet::barrier_set());
  54   }
  55 
  56   static ShenandoahSATBMarkQueueSet& satb_mark_queue_set() {
  57     return barrier_set()->_satb_mark_queue_set;
  58   }
  59 
  60   static bool need_load_reference_barrier(DecoratorSet decorators, BasicType type);
  61   static bool use_load_reference_barrier_native(DecoratorSet decorators, BasicType type);
  62   static bool need_keep_alive_barrier(DecoratorSet decorators, BasicType type);
  63 
  64   void print_on(outputStream* st) const;
  65 
  66   bool is_a(BarrierSet::Name bsn);
  67 
  68   bool is_aligned(HeapWord* hw);
  69 
  70   template <class T> void
  71   write_ref_array_pre_work(T* src, T* dst, size_t count, bool dest_uninitialized);
  72 
  73   inline void arraycopy_pre(oop* src, oop* dst, size_t count);
  74   inline void arraycopy_pre(narrowOop* src, narrowOop* dst, size_t count);
  75   inline void arraycopy_update(oop* src, size_t count);
  76   inline void arraycopy_update(narrowOop* src, size_t count);
  77   inline void clone_barrier(oop src);
  78   void clone_barrier_runtime(oop src);
  79 
  80   virtual void on_thread_create(Thread* thread);
  81   virtual void on_thread_destroy(Thread* thread);
  82   virtual void on_thread_attach(Thread* thread);
  83   virtual void on_thread_detach(Thread* thread);
  84 
  85   static inline oop resolve_forwarded_not_null(oop p);
  86   static inline oop resolve_forwarded(oop p);
  87   static inline oop resolve_forwarded_checked(oop p);
  88 
  89   template <DecoratorSet decorators, typename T>
  90   inline void satb_barrier(T* field);
  91   inline void satb_enqueue(oop value);
  92   inline void storeval_barrier(oop obj);
  93 
  94   template <DecoratorSet decorators>
  95   inline void keep_alive_if_weak(oop value);
  96   inline void keep_alive_if_weak(DecoratorSet decorators, oop value);
  97   inline void keep_alive_barrier(oop value);
  98 
  99   inline void enqueue(oop obj);
 100 
 101   oop load_reference_barrier(oop obj);
 102   oop load_reference_barrier_not_null(oop obj);
 103 
 104   oop load_reference_barrier_mutator(oop obj, oop* load_addr);
 105   oop load_reference_barrier_mutator(oop obj, narrowOop* load_addr);
 106 
 107   template <class T>
 108   oop load_reference_barrier_mutator_work(oop obj, T* load_addr);
 109 
 110   oop load_reference_barrier_native(oop obj, oop* load_addr);
 111   oop load_reference_barrier_native(oop obj, narrowOop* load_addr);
 112 
 113 private:
 114   template <class T>
 115   inline void arraycopy_pre_work(T* src, T* dst, size_t count);
 116   template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
 117   inline void arraycopy_work(T* src, size_t count);
 118   template <class T>
 119   inline void arraycopy_update_impl(T* src, size_t count);
 120 
 121   oop load_reference_barrier_impl(oop obj);
 122 
 123   template <class T>
 124   oop load_reference_barrier_native_impl(oop obj, T* load_addr);
 125 
 126 public:
 127   // Callbacks for runtime accesses.
 128   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
 129   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
 130     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
 131 
 132   public:
 133     // Heap oop accesses. These accessors get resolved when
 134     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 135     // an oop_* overload, and the barrier strength is AS_NORMAL.
 136     template <typename T>
 137     static oop oop_load_in_heap(T* addr);
 138     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset);
 139 
 140     template <typename T>
 141     static void oop_store_in_heap(T* addr, oop value);
 142     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value);
 143 
 144     template <typename T>
 145     static oop oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value);
 146     static oop oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value);
 147 
 148     template <typename T>
 149     static oop oop_atomic_xchg_in_heap(T* addr, oop new_value);
 150     static oop oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value);
 151 
 152     template <typename T>
 153     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 154                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 155                                       size_t length);
 156 
 157     // Clone barrier support
 158     static void clone_in_heap(oop src, oop dst, size_t size);
 159 
 160     // Needed for loads on non-heap weak references
 161     template <typename T>
 162     static oop oop_load_not_in_heap(T* addr);
 163 
 164     // Used for catching bad stores
 165     template <typename T>
 166     static void oop_store_not_in_heap(T* addr, oop value);
 167 
 168     template <typename T>
 169     static oop oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value);
 170 
 171     template <typename T>
 172     static oop oop_atomic_xchg_not_in_heap(T* addr, oop new_value);
 173 
 174   };
 175 
 176 };
 177 
 178 template<>
 179 struct BarrierSet::GetName<ShenandoahBarrierSet> {
 180   static const BarrierSet::Name value = BarrierSet::ShenandoahBarrierSet;
 181 };
 182 
 183 template<>
 184 struct BarrierSet::GetType<BarrierSet::ShenandoahBarrierSet> {
 185   typedef ::ShenandoahBarrierSet type;
 186 };
 187 
 188 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP