1 /*
  2  * Copyright (c) 2013, 2021, 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/barrierSet.hpp"
 29 #include "gc/shenandoah/shenandoahCardTable.hpp"
 30 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
 31 
 32 class ShenandoahHeap;
 33 class ShenandoahBarrierSetAssembler;
 34 
 35 class ShenandoahBarrierSet: public BarrierSet {
 36 private:
 37   ShenandoahHeap* const _heap;
 38   ShenandoahCardTable* _card_table;
 39   BufferNode::Allocator _satb_mark_queue_buffer_allocator;
 40   ShenandoahSATBMarkQueueSet _satb_mark_queue_set;
 41 
 42 public:
 43   ShenandoahBarrierSet(ShenandoahHeap* heap, MemRegion heap_region);
 44 
 45   static ShenandoahBarrierSetAssembler* assembler();
 46 
 47   inline static ShenandoahBarrierSet* barrier_set() {
 48     return barrier_set_cast<ShenandoahBarrierSet>(BarrierSet::barrier_set());
 49   }
 50 
 51   inline ShenandoahCardTable* card_table()  { return _card_table; }
 52 
 53   static ShenandoahSATBMarkQueueSet& satb_mark_queue_set() {
 54     return barrier_set()->_satb_mark_queue_set;
 55   }
 56 
 57   static bool need_load_reference_barrier(DecoratorSet decorators, BasicType type);
 58   static bool need_keep_alive_barrier(DecoratorSet decorators, BasicType type);
 59 
 60   static bool is_strong_access(DecoratorSet decorators) {
 61     return (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) == 0;
 62   }
 63 
 64   static bool is_weak_access(DecoratorSet decorators) {
 65     return (decorators & ON_WEAK_OOP_REF) != 0;
 66   }
 67 
 68   static bool is_phantom_access(DecoratorSet decorators) {
 69     return (decorators & ON_PHANTOM_OOP_REF) != 0;
 70   }
 71 
 72   static bool is_native_access(DecoratorSet decorators) {
 73     return (decorators & IN_NATIVE) != 0;
 74   }
 75 
 76   void print_on(outputStream* st) const;
 77 
 78   template <class T>
 79   inline void arraycopy_barrier(T* src, T* dst, size_t count);
 80   inline void clone_barrier(oop src);
 81   void clone_barrier_runtime(oop src);
 82 
 83   virtual void on_thread_create(Thread* thread);
 84   virtual void on_thread_destroy(Thread* thread);
 85   virtual void on_thread_attach(Thread* thread);
 86   virtual void on_thread_detach(Thread* thread);
 87 
 88   static inline oop resolve_forwarded_not_null(oop p);
 89   static inline oop resolve_forwarded_not_null_mutator(oop p);
 90   static inline oop resolve_forwarded(oop p);
 91 
 92   template <DecoratorSet decorators, typename T>
 93   inline void satb_barrier(T* field);
 94   inline void satb_enqueue(oop value);
 95   inline void iu_barrier(oop obj);
 96 
 97   inline void keep_alive_if_weak(DecoratorSet decorators, oop value);
 98 
 99   inline void enqueue(oop obj);
100 
101   inline oop load_reference_barrier(oop obj);
102 
103   template <class T>
104   inline oop load_reference_barrier_mutator(oop obj, T* load_addr);
105 
106   template <class T>
107   inline oop load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr);
108 
109   template <typename T>
110   inline oop oop_load(DecoratorSet decorators, T* addr);
111 
112   template <typename T>
113   inline oop oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value);
114 
115   template <typename T>
116   inline oop oop_xchg(DecoratorSet decorators, T* addr, oop new_value);
117 
118   template <DecoratorSet decorators, typename T>
119   void write_ref_field_post(T* field, oop newVal);
120 
121   void write_ref_array(HeapWord* start, size_t count);
122 
123 private:
124   template <class T>
125   inline void arraycopy_marking(T* src, T* dst, size_t count);
126   template <class T>
127   inline void arraycopy_evacuation(T* src, size_t count);
128   template <class T>
129   inline void arraycopy_update(T* src, size_t count);
130 
131   inline void clone_marking(oop src);
132   inline void clone_evacuation(oop src);
133   inline void clone_update(oop src);
134 
135   template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
136   inline void arraycopy_work(T* src, size_t count);
137 
138   inline bool need_bulk_update(HeapWord* dst);
139 public:
140   // Callbacks for runtime accesses.
141   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
142   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
143     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
144 
145   public:
146     // Heap oop accesses. These accessors get resolved when
147     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
148     // an oop_* overload, and the barrier strength is AS_NORMAL.
149     template <typename T>
150     static oop oop_load_in_heap(T* addr);
151     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset);
152 
153     template <typename T>
154     static void oop_store_in_heap(T* addr, oop value);
155     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value);
156 
157     template <typename T>
158     static oop oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value);
159     static oop oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value);
160 
161     template <typename T>
162     static oop oop_atomic_xchg_in_heap(T* addr, oop new_value);
163     static oop oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value);
164 
165     template <typename T>
166     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
167                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
168                                       size_t length);
169 
170     // Clone barrier support
171     static void clone_in_heap(oop src, oop dst, size_t size);
172 
173     // Support for concurrent roots evacuation, updating and weak roots clearing
174     template <typename T>
175     static oop oop_load_not_in_heap(T* addr);
176 
177     // Support for concurrent roots marking
178     template <typename T>
179     static void oop_store_not_in_heap(T* addr, oop value);
180 
181     template <typename T>
182     static oop oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value);
183 
184     template <typename T>
185     static oop oop_atomic_xchg_not_in_heap(T* addr, oop new_value);
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