1 /*
  2  * Copyright (c) 2013, 2021, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
 28 
 29 #include "gc/shared/barrierSet.hpp"
 30 #include "gc/shared/bufferNode.hpp"
 31 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
 32 
 33 class ShenandoahHeap;
 34 class ShenandoahBarrierSetAssembler;
 35 class ShenandoahCardTable;
 36 
 37 class ShenandoahBarrierSet: public BarrierSet {
 38 private:
 39   ShenandoahHeap* const _heap;
 40   ShenandoahCardTable* _card_table;
 41   BufferNode::Allocator _satb_mark_queue_buffer_allocator;
 42   ShenandoahSATBMarkQueueSet _satb_mark_queue_set;
 43 
 44 public:
 45   ShenandoahBarrierSet(ShenandoahHeap* heap, MemRegion heap_region);
 46 
 47   static ShenandoahBarrierSetAssembler* assembler();
 48 
 49   inline static ShenandoahBarrierSet* barrier_set() {
 50     return barrier_set_cast<ShenandoahBarrierSet>(BarrierSet::barrier_set());
 51   }
 52 
 53   inline ShenandoahCardTable* card_table() {
 54     return _card_table;
 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 need_keep_alive_barrier(DecoratorSet decorators, BasicType type);
 63   static bool need_satb_barrier(DecoratorSet decorators, BasicType type);
 64   static bool need_card_barrier(DecoratorSet decorators, BasicType type);
 65 
 66   static bool is_strong_access(DecoratorSet decorators) {
 67     return (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) == 0;
 68   }
 69 
 70   static bool is_weak_access(DecoratorSet decorators) {
 71     return (decorators & ON_WEAK_OOP_REF) != 0;
 72   }
 73 
 74   static bool is_phantom_access(DecoratorSet decorators) {
 75     return (decorators & ON_PHANTOM_OOP_REF) != 0;
 76   }
 77 
 78   static bool is_native_access(DecoratorSet decorators) {
 79     return (decorators & IN_NATIVE) != 0;
 80   }
 81 
 82   void print_on(outputStream* st) const override;
 83 
 84   template <class T>
 85   inline void arraycopy_barrier(T* src, T* dst, size_t count);
 86   inline void clone_barrier(oop src);
 87   void clone_barrier_runtime(oop src);
 88 
 89   // Support for optimizing compilers to call the barrier set on slow path allocations
 90   // that did not enter a TLAB. Used for e.g. ReduceInitialCardMarks to take any
 91   // compensating actions to restore card-marks that might otherwise be incorrectly elided.
 92   void on_slowpath_allocation_exit(JavaThread* thread, oop new_obj) override;
 93   void on_thread_create(Thread* thread) override;
 94   void on_thread_destroy(Thread* thread) override;
 95   void on_thread_attach(Thread* thread) override;
 96   void on_thread_detach(Thread* thread) override;
 97 
 98   static inline oop resolve_forwarded_not_null(oop p);
 99   static inline oop resolve_forwarded_not_null_mutator(oop p);
100   static inline oop resolve_forwarded(oop p);
101 
102   template <DecoratorSet decorators, typename T>
103   inline void satb_barrier(T* field);
104   inline void satb_enqueue(oop value);
105 
106   inline void keep_alive_if_weak(DecoratorSet decorators, oop value);
107 
108   inline void enqueue(oop obj);
109 
110   inline oop load_reference_barrier(oop obj);
111 
112   template <class T>
113   inline oop load_reference_barrier_mutator(oop obj, T* load_addr);
114 
115   template <class T>
116   inline oop load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr);
117 
118   template <typename T>
119   inline oop oop_load(DecoratorSet decorators, T* addr);
120 
121   template <typename T>
122   inline oop oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value);
123 
124   template <typename T>
125   inline oop oop_xchg(DecoratorSet decorators, T* addr, oop new_value);
126 
127   template <DecoratorSet decorators, typename T>
128   void write_ref_field_post(T* field);
129 
130   void write_ref_array(HeapWord* start, size_t count);
131 
132 private:
133   template <bool IS_GENERATIONAL, class T>
134   void arraycopy_marking(T* dst, size_t count);
135   template <class T>
136   inline void arraycopy_evacuation(T* src, size_t count);
137   template <class T>
138   inline void arraycopy_update(T* src, size_t count);
139 
140   inline void clone_evacuation(oop src);
141   inline void clone_update(oop src);
142 
143   template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
144   inline void arraycopy_work(T* src, size_t count);
145 
146   inline bool need_bulk_update(HeapWord* dst);
147 public:
148   // Callbacks for runtime accesses.
149   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
150   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
151     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
152 
153   private:
154     template <typename T>
155     static void oop_store_common(T* addr, oop value);
156 
157   public:
158     // Heap oop accesses. These accessors get resolved when
159     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
160     // an oop_* overload, and the barrier strength is AS_NORMAL.
161     template <typename T>
162     static oop oop_load_in_heap(T* addr);
163     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset);
164 
165     template <typename T>
166     static void oop_store_in_heap(T* addr, oop value);
167     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value);
168 
169     template <typename T>
170     static oop oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value);
171     static oop oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value);
172 
173     template <typename T>
174     static oop oop_atomic_xchg_in_heap(T* addr, oop new_value);
175     static oop oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value);
176 
177     template <typename T>
178     static OopCopyResult oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
179                                                arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
180                                                size_t length);
181 
182     // Clone barrier support
183     static void clone_in_heap(oop src, oop dst, size_t size);
184 
185     // Support for concurrent roots evacuation, updating and weak roots clearing
186     template <typename T>
187     static oop oop_load_not_in_heap(T* addr);
188 
189     // Support for concurrent roots marking
190     template <typename T>
191     static void oop_store_not_in_heap(T* addr, oop value);
192 
193     template <typename T>
194     static oop oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value);
195 
196     template <typename T>
197     static oop oop_atomic_xchg_not_in_heap(T* addr, oop new_value);
198   };
199 
200 };
201 
202 template<>
203 struct BarrierSet::GetName<ShenandoahBarrierSet> {
204   static const BarrierSet::Name value = BarrierSet::ShenandoahBarrierSet;
205 };
206 
207 template<>
208 struct BarrierSet::GetType<BarrierSet::ShenandoahBarrierSet> {
209   typedef ::ShenandoahBarrierSet type;
210 };
211 
212 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP