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