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   template <class T> void
  67   write_ref_array_pre_work(T* src, T* dst, size_t count, bool dest_uninitialized);
  68 
  69   inline void arraycopy_pre(oop* src, oop* dst, size_t count);
  70   inline void arraycopy_pre(narrowOop* src, narrowOop* dst, size_t count);
  71   inline void arraycopy_update(oop* src, size_t count);
  72   inline void arraycopy_update(narrowOop* src, size_t count);
  73   inline void clone_barrier(oop src);
  74   void clone_barrier_runtime(oop src);
  75 
  76   // We export this to make it available in cases where the static
  77   // type of the barrier set is known.  Note that it is non-virtual.
  78   template <class T> inline void inline_write_ref_field_pre(T* field, oop new_val);
  79 
  80   // These are the more general virtual versions.
  81   void write_ref_field_pre_work(oop* field, oop new_val);
  82   void write_ref_field_pre_work(narrowOop* field, oop new_val);
  83   void write_ref_field_pre_work(void* field, oop new_val);
  84 
  85   void write_ref_field_work(void* v, oop o, bool release = false);
  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_not_null(oop obj);
 102 
 103   oop load_reference_barrier_mutator(oop obj, oop* load_addr);
 104   oop load_reference_barrier_mutator(oop obj, narrowOop* load_addr);
 105 
 106   template <class T>
 107   oop load_reference_barrier_mutator_work(oop obj, T* load_addr);
 108 
 109   void enqueue(oop obj);
 110 
 111 private:
 112   template <class T>
 113   inline void arraycopy_pre_work(T* src, T* dst, size_t count);
 114   template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
 115   inline void arraycopy_work(T* src, size_t count);
 116   template <class T>
 117   inline void arraycopy_update_impl(T* src, size_t count);
 118 
 119   oop load_reference_barrier_impl(oop obj);
 120 
 121   static void keep_alive_if_weak(DecoratorSet decorators, oop value) {
 122     assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
 123     const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
 124     const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
 125     if (!peek && !on_strong_oop_ref && value != NULL) {
 126       ShenandoahBarrierSet::barrier_set()->keep_alive_barrier(value);
 127     }
 128   }
 129 
 130 public:
 131   // Callbacks for runtime accesses.
 132   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
 133   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
 134     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
 135 
 136     template <typename T>
 137     static oop oop_atomic_cmpxchg_in_heap_impl(oop new_value, T* addr, oop compare_value);
 138 
 139     template <typename T>
 140     static oop oop_atomic_xchg_in_heap_impl(oop new_value, T* addr);
 141 
 142   public:
 143     // Heap oop accesses. These accessors get resolved when
 144     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 145     // an oop_* overload, and the barrier strength is AS_NORMAL.
 146     template <typename T>
 147     static oop oop_load_in_heap(T* addr);
 148     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset);
 149 
 150     template <typename T>
 151     static void oop_store_in_heap(T* addr, oop value);
 152     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value);
 153 
 154     template <typename T>
 155     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value);
 156     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value);
 157 
 158     template <typename T>
 159     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr);
 160     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset);
 161 
 162     template <typename T>
 163     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 164                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 165                                       size_t length);
 166 
 167     // Clone barrier support
 168     static void clone_in_heap(oop src, oop dst, size_t size);
 169 
 170     // Needed for loads on non-heap weak references
 171     template <typename T>
 172     static oop oop_load_not_in_heap(T* addr);
 173 
 174     // Used for catching bad stores
 175     template <typename T>
 176     static void oop_store_not_in_heap(T* addr, oop value);
 177 
 178     template <typename T>
 179     static oop oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value);
 180 
 181     template <typename T>
 182     static oop oop_atomic_xchg_not_in_heap(oop new_value, T* addr);
 183 
 184   };
 185 
 186 };
 187 
 188 template<>
 189 struct BarrierSet::GetName<ShenandoahBarrierSet> {
 190   static const BarrierSet::Name value = BarrierSet::ShenandoahBarrierSet;
 191 };
 192 
 193 template<>
 194 struct BarrierSet::GetType<BarrierSet::ShenandoahBarrierSet> {
 195   typedef ::ShenandoahBarrierSet type;
 196 };
 197 
 198 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP