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   void print_on(outputStream* st) const;
  61 
  62   bool is_a(BarrierSet::Name bsn);
  63 
  64   bool is_aligned(HeapWord* hw);
  65 
  66   void write_ref_array(HeapWord* start, size_t count);
  67 
  68   template <class T> void
  69   write_ref_array_pre_work(T* dst, size_t count);
  70 
  71   void write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized);
  72 
  73   void write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized);
  74 
  75   // We export this to make it available in cases where the static
  76   // type of the barrier set is known.  Note that it is non-virtual.
  77   template <class T> inline void inline_write_ref_field_pre(T* field, oop new_val);
  78 
  79   // These are the more general virtual versions.
  80   void write_ref_field_pre_work(oop* field, oop new_val);
  81   void write_ref_field_pre_work(narrowOop* field, oop new_val);
  82   void write_ref_field_pre_work(void* field, oop new_val);
  83 
  84   void write_ref_field_work(void* v, oop o, bool release = false);
  85   void write_region(MemRegion mr);
  86 
  87   oop oop_load_from_native_barrier(oop obj);
  88 
  89   virtual void on_thread_create(Thread* thread);
  90   virtual void on_thread_destroy(Thread* thread);
  91   virtual void on_thread_attach(Thread* thread);
  92   virtual void on_thread_detach(Thread* thread);
  93 
  94   static inline oop resolve_forwarded_not_null(oop p);
  95   static inline oop resolve_forwarded(oop p);
  96 
  97   void storeval_barrier(oop obj);
  98   void keep_alive_barrier(oop obj);
  99 
 100   oop load_reference_barrier(oop obj);
 101   oop load_reference_barrier_mutator(oop obj);
 102   oop load_reference_barrier_not_null(oop obj);
 103 
 104   void enqueue(oop obj);
 105 
 106 private:
 107   template <class T, bool STOREVAL_WRITE_BARRIER>
 108   void write_ref_array_loop(HeapWord* start, size_t count);
 109 
 110   oop load_reference_barrier_impl(oop obj);
 111 
 112   static void keep_alive_if_weak(DecoratorSet decorators, oop value) {
 113     assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 114     const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
 115     const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
 116     if (!peek && !on_strong_oop_ref && value != NULL) {
 117       ShenandoahBarrierSet::barrier_set()->keep_alive_barrier(value);
 118     }
 119   }
 120 
 121   template <typename T>
 122   bool arraycopy_loop_1(T* src, T* dst, size_t length, Klass* bound,
 123                         bool checkcast, bool satb, bool disjoint, ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode);
 124 
 125   template <typename T, bool CHECKCAST>
 126   bool arraycopy_loop_2(T* src, T* dst, size_t length, Klass* bound,
 127                         bool satb, bool disjoint, ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode);
 128 
 129   template <typename T, bool CHECKCAST, bool SATB>
 130   bool arraycopy_loop_3(T* src, T* dst, size_t length, Klass* bound,
 131                         bool disjoint, ShenandoahBarrierSet::ArrayCopyStoreValMode storeval_mode);
 132 
 133   template <typename T, bool CHECKCAST, bool SATB, ShenandoahBarrierSet::ArrayCopyStoreValMode STOREVAL_MODE>
 134   bool arraycopy_loop(T* src, T* dst, size_t length, Klass* bound, bool disjoint);
 135 
 136   template <typename T, bool CHECKCAST, bool SATB, ShenandoahBarrierSet::ArrayCopyStoreValMode STOREVAL_MODE>
 137   bool arraycopy_element(T* cur_src, T* cur_dst, Klass* bound, Thread* const thread, ShenandoahMarkingContext* const ctx);
 138 
 139 public:
 140   // Callbacks for runtime accesses.
 141   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
 142   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
 143     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
 144 
 145     template <typename T>
 146     static oop oop_atomic_cmpxchg_in_heap_impl(oop new_value, T* addr, oop compare_value);
 147 
 148     template <typename T>
 149     static oop oop_atomic_xchg_in_heap_impl(oop new_value, T* addr);
 150 
 151   public:
 152     // Heap oop accesses. These accessors get resolved when
 153     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 154     // an oop_* overload, and the barrier strength is AS_NORMAL.
 155     template <typename T>
 156     static oop oop_load_in_heap(T* addr);
 157     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset);
 158 
 159     template <typename T>
 160     static void oop_store_in_heap(T* addr, oop value);
 161     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value);
 162 
 163     template <typename T>
 164     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value);
 165     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value);
 166 
 167     template <typename T>
 168     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr);
 169     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset);
 170 
 171     template <typename T>
 172     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 173                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 174                                       size_t length);
 175 
 176     // Clone barrier support
 177     static void clone_in_heap(oop src, oop dst, size_t size);
 178 
 179     // Needed for loads on non-heap weak references
 180     template <typename T>
 181     static oop oop_load_not_in_heap(T* addr);
 182 
 183     // Used for catching bad stores
 184     template <typename T>
 185     static void oop_store_not_in_heap(T* addr, oop value);
 186 
 187     template <typename T>
 188     static oop oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value);
 189 
 190     template <typename T>
 191     static oop oop_atomic_xchg_not_in_heap(oop new_value, T* addr);
 192 
 193   };
 194 
 195 };
 196 
 197 template<>
 198 struct BarrierSet::GetName<ShenandoahBarrierSet> {
 199   static const BarrierSet::Name value = BarrierSet::ShenandoahBarrierSet;
 200 };
 201 
 202 template<>
 203 struct BarrierSet::GetType<BarrierSet::ShenandoahBarrierSet> {
 204   typedef ::ShenandoahBarrierSet type;
 205 };
 206 
 207 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP