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