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