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