1 /*
   2  * Copyright (c) 2013, 2018, 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_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
  25 #define SHARE_VM_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/shenandoahSATBMarkQueue.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   static ShenandoahSATBMarkQueueSet _satb_mark_queue_set;
  44 
  45   ShenandoahHeap* _heap;
  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 _satb_mark_queue_set;
  58   }
  59 
  60   static bool need_load_reference_barrier(DecoratorSet decorators, BasicType type);
  61   static bool need_keep_alive_barrier(DecoratorSet decorators, BasicType type);
  62 
  63   void print_on(outputStream* st) const;
  64 
  65   bool is_a(BarrierSet::Name bsn);
  66 
  67   bool is_aligned(HeapWord* hw);
  68 
  69   template <class T>
  70   inline void arraycopy_barrier(T* src, T* dst, size_t count);
  71   inline void clone_barrier(oop src);
  72   void clone_barrier_runtime(oop src);
  73 
  74   virtual void on_thread_create(Thread* thread);
  75   virtual void on_thread_destroy(Thread* thread);
  76   virtual void on_thread_attach(JavaThread* thread);
  77   virtual void on_thread_detach(JavaThread* thread);
  78 
  79   static inline oop resolve_forwarded_not_null(oop p);
  80   static inline oop resolve_forwarded_not_null_mutator(oop p);
  81   static inline oop resolve_forwarded(oop p);
  82 
  83   template <DecoratorSet decorators, typename T>
  84   inline void satb_barrier(T* field);
  85   inline void satb_enqueue(oop value);
  86   inline void storeval_barrier(oop obj);
  87 
  88   template <DecoratorSet decorators>
  89   inline void keep_alive_if_weak(oop value);
  90   inline void keep_alive_if_weak(DecoratorSet decorators, oop value);
  91 
  92   inline void enqueue(oop obj);
  93 
  94   oop load_reference_barrier(oop obj);
  95   oop load_reference_barrier_not_null(oop obj);
  96 
  97   template <class T>
  98   inline oop load_reference_barrier_mutator(oop obj, T* load_addr);
  99 
 100 private:
 101   template <class T>
 102   inline void arraycopy_marking(T* src, T* dst, size_t count);
 103   template <class T>
 104   inline void arraycopy_evacuation(T* src, size_t count);
 105   template <class T>
 106   inline void arraycopy_update(T* src, size_t count);
 107 
 108   inline void clone_marking(oop src);
 109   inline void clone_evacuation(oop src);
 110   inline void clone_update(oop src);
 111 
 112   template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
 113   inline void arraycopy_work(T* src, size_t count);
 114 
 115   oop load_reference_barrier_impl(oop obj);
 116 
 117   inline bool need_bulk_update(HeapWord* dst);
 118 public:
 119   // Callbacks for runtime accesses.
 120   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
 121   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
 122     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
 123 
 124   public:
 125     // Heap oop accesses. These accessors get resolved when
 126     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
 127     // an oop_* overload, and the barrier strength is AS_NORMAL.
 128     template <typename T>
 129     static oop oop_load_in_heap(T* addr);
 130     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset);
 131 
 132     template <typename T>
 133     static void oop_store_in_heap(T* addr, oop value);
 134     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value);
 135 
 136     template <typename T>
 137     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value);
 138     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value);
 139 
 140     template <typename T>
 141     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr);
 142     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset);
 143 
 144     template <typename T>
 145     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
 146                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
 147                                       size_t length);
 148 
 149     // Clone barrier support
 150     static void clone_in_heap(oop src, oop dst, size_t size);
 151 
 152     // Needed for loads on non-heap weak references
 153     template <typename T>
 154     static oop oop_load_not_in_heap(T* addr);
 155 
 156     // Used for catching bad stores
 157     template <typename T>
 158     static void oop_store_not_in_heap(T* addr, oop value);
 159 
 160     template <typename T>
 161     static oop oop_atomic_cmpxchg_not_in_heap(oop new_value, T* addr, oop compare_value);
 162 
 163     template <typename T>
 164     static oop oop_atomic_xchg_not_in_heap(oop new_value, T* addr);
 165 
 166   };
 167 
 168 };
 169 
 170 template<>
 171 struct BarrierSet::GetName<ShenandoahBarrierSet> {
 172   static const BarrierSet::Name value = BarrierSet::ShenandoahBarrierSet;
 173 };
 174 
 175 template<>
 176 struct BarrierSet::GetType<BarrierSet::ShenandoahBarrierSet> {
 177   typedef ::ShenandoahBarrierSet type;
 178 };
 179 
 180 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP