1 /*
2 * Copyright (c) 2000, 2024, Oracle and/or its affiliates. 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_SHARED_BARRIERSET_HPP
26 #define SHARE_GC_SHARED_BARRIERSET_HPP
27
28 #include "gc/shared/barrierSetConfig.hpp"
29 #include "memory/memRegion.hpp"
30 #include "oops/access.hpp"
31 #include "oops/accessBackend.hpp"
32 #include "oops/oopsHierarchy.hpp"
33 #include "utilities/exceptions.hpp"
34 #include "utilities/fakeRttiSupport.hpp"
35 #include "utilities/macros.hpp"
36
37 class BarrierSetAssembler;
38 class BarrierSetC1;
39 class BarrierSetC2;
40 class BarrierSetNMethod;
41 class BarrierSetStackChunk;
42 class JavaThread;
43
44 // This class provides the interface between a barrier implementation and
45 // the rest of the system.
46
47 class BarrierSet: public CHeapObj<mtGC> {
48 friend class VMStructs;
49
50 static BarrierSet* _barrier_set;
51
52 public:
53 enum Name {
54 #define BARRIER_SET_DECLARE_BS_ENUM(bs_name) bs_name ,
55 FOR_EACH_BARRIER_SET_DO(BARRIER_SET_DECLARE_BS_ENUM)
56 #undef BARRIER_SET_DECLARE_BS_ENUM
57 UnknownBS
58 };
59
60 protected:
61 // Fake RTTI support. For a derived class T to participate
62 // - T must have a corresponding Name entry.
63 // - GetName<T> must be specialized to return the corresponding Name
64 // entry.
65 // - If T is a base class, the constructor must have a FakeRtti
66 // parameter and pass it up to its base class, with the tag set
67 // augmented with the corresponding Name entry.
68 // - If T is a concrete class, the constructor must create a
69 // FakeRtti object whose tag set includes the corresponding Name
70 // entry, and pass it up to its base class.
71 typedef FakeRttiSupport<BarrierSet, Name> FakeRtti;
72
73 private:
74 FakeRtti _fake_rtti;
75 BarrierSetAssembler* _barrier_set_assembler;
76 BarrierSetC1* _barrier_set_c1;
77 BarrierSetC2* _barrier_set_c2;
78 BarrierSetNMethod* _barrier_set_nmethod;
79 BarrierSetStackChunk* _barrier_set_stack_chunk;
80
81 public:
82 // Metafunction mapping a class derived from BarrierSet to the
83 // corresponding Name enum tag.
84 template<typename T> struct GetName;
85
86 // Metafunction mapping a Name enum type to the corresponding
87 // lass derived from BarrierSet.
88 template<BarrierSet::Name T> struct GetType;
89
90 // Note: This is not presently the Name corresponding to the
91 // concrete class of this object.
92 BarrierSet::Name kind() const { return _fake_rtti.concrete_tag(); }
93
94 // Test whether this object is of the type corresponding to bsn.
95 bool is_a(BarrierSet::Name bsn) const { return _fake_rtti.has_tag(bsn); }
96
97 // End of fake RTTI support.
98
99 protected:
100 BarrierSet(BarrierSetAssembler* barrier_set_assembler,
101 BarrierSetC1* barrier_set_c1,
102 BarrierSetC2* barrier_set_c2,
103 BarrierSetNMethod* barrier_set_nmethod,
104 BarrierSetStackChunk* barrier_set_stack_chunk,
105 const FakeRtti& fake_rtti);
106 ~BarrierSet() { }
107
108 template <class BarrierSetAssemblerT>
109 static BarrierSetAssembler* make_barrier_set_assembler() {
110 return NOT_ZERO(new BarrierSetAssemblerT()) ZERO_ONLY(nullptr);
111 }
112
113 template <class BarrierSetC1T>
114 static BarrierSetC1* make_barrier_set_c1() {
115 return COMPILER1_PRESENT(new BarrierSetC1T()) NOT_COMPILER1(nullptr);
116 }
117
118 template <class BarrierSetC2T>
119 static BarrierSetC2* make_barrier_set_c2() {
120 return COMPILER2_PRESENT(new BarrierSetC2T()) NOT_COMPILER2(nullptr);
121 }
122
123 static void throw_array_null_pointer_store_exception(arrayOop src, arrayOop dst, TRAPS);
124 static void throw_array_store_exception(arrayOop src, arrayOop dst, TRAPS);
125
126 public:
127 // Support for optimizing compilers to call the barrier set on slow path allocations
128 // that did not enter a TLAB. Used for e.g. ReduceInitialCardMarks.
129 // The allocation is safe to use iff it returns true. If not, the slow-path allocation
130 // is redone until it succeeds. This can e.g. prevent allocations from the slow path
131 // to be in old.
132 virtual void on_slowpath_allocation_exit(JavaThread* thread, oop new_obj) {}
133 virtual void on_thread_create(Thread* thread) {}
134 virtual void on_thread_destroy(Thread* thread) {}
135
136 // These perform BarrierSet-related initialization/cleanup before the thread
137 // is added to or removed from the corresponding set of threads. The
138 // argument thread is the current thread. These are called either holding
139 // the Threads_lock (for a JavaThread) and so not at a safepoint, or holding
140 // the NonJavaThreadsList_lock (for a NonJavaThread) locked by the
141 // caller. That locking ensures the operation is "atomic" with the list
142 // modification wrto operations that hold the NJTList_lock and either also
143 // hold the Threads_lock or are at a safepoint.
144 virtual void on_thread_attach(Thread* thread);
145 virtual void on_thread_detach(Thread* thread) {}
146
147 virtual void make_parsable(JavaThread* thread) {}
148
149 // Print a description of the memory for the barrier set
150 virtual void print_on(outputStream* st) const = 0;
151
152 static BarrierSet* barrier_set() { return _barrier_set; }
153 static void set_barrier_set(BarrierSet* barrier_set);
154
155 BarrierSetAssembler* barrier_set_assembler() {
156 assert(_barrier_set_assembler != nullptr, "should be set");
157 return _barrier_set_assembler;
158 }
159
160 BarrierSetC1* barrier_set_c1() {
161 assert(_barrier_set_c1 != nullptr, "should be set");
162 return _barrier_set_c1;
163 }
164
165 BarrierSetC2* barrier_set_c2() {
166 assert(_barrier_set_c2 != nullptr, "should be set");
167 return _barrier_set_c2;
168 }
169
170 BarrierSetNMethod* barrier_set_nmethod() {
171 return _barrier_set_nmethod;
172 }
173
174 BarrierSetStackChunk* barrier_set_stack_chunk() {
175 assert(_barrier_set_stack_chunk != nullptr, "should be set");
176 return _barrier_set_stack_chunk;
177 }
178
179 // The AccessBarrier of a BarrierSet subclass is called by the Access API
180 // (cf. oops/access.hpp) to perform decorated accesses. GC implementations
181 // may override these default access operations by declaring an
182 // AccessBarrier class in its BarrierSet. Its accessors will then be
183 // automatically resolved at runtime.
184 //
185 // In order to register a new FooBarrierSet::AccessBarrier with the Access API,
186 // the following steps should be taken:
187 // 1) Provide an enum "name" for the BarrierSet in barrierSetConfig.hpp
188 // 2) Make sure the barrier set headers are included from barrierSetConfig.inline.hpp
189 // 3) Provide specializations for BarrierSet::GetName and BarrierSet::GetType.
190 template <DecoratorSet decorators, typename BarrierSetT>
191 class AccessBarrier: protected RawAccessBarrier<decorators> {
192 private:
193 typedef RawAccessBarrier<decorators> Raw;
194
195 public:
196 // Primitive heap accesses. These accessors get resolved when
197 // IN_HEAP is set (e.g. when using the HeapAccess API), it is
198 // not an oop_* overload, and the barrier strength is AS_NORMAL.
199 template <typename T>
200 static T load_in_heap(T* addr) {
201 return Raw::template load<T>(addr);
202 }
203
204 template <typename T>
205 static T load_in_heap_at(oop base, ptrdiff_t offset) {
206 return Raw::template load_at<T>(base, offset);
207 }
208
209 template <typename T>
210 static void store_in_heap(T* addr, T value) {
211 Raw::store(addr, value);
212 }
213
214 template <typename T>
215 static void store_in_heap_at(oop base, ptrdiff_t offset, T value) {
216 Raw::store_at(base, offset, value);
217 }
218
219 template <typename T>
220 static T atomic_cmpxchg_in_heap(T* addr, T compare_value, T new_value) {
221 return Raw::atomic_cmpxchg(addr, compare_value, new_value);
222 }
223
224 template <typename T>
225 static T atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, T compare_value, T new_value) {
226 return Raw::atomic_cmpxchg_at(base, offset, compare_value, new_value);
227 }
228
229 template <typename T>
230 static T atomic_xchg_in_heap(T* addr, T new_value) {
231 return Raw::atomic_xchg(addr, new_value);
232 }
233
234 template <typename T>
235 static T atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, T new_value) {
236 return Raw::atomic_xchg_at(base, offset, new_value);
237 }
238
239 template <typename T>
240 static void arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
241 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
242 size_t length) {
243 Raw::arraycopy(src_obj, src_offset_in_bytes, src_raw,
244 dst_obj, dst_offset_in_bytes, dst_raw,
245 length);
246 }
247
248 // Heap oop accesses. These accessors get resolved when
249 // IN_HEAP is set (e.g. when using the HeapAccess API), it is
250 // an oop_* overload, and the barrier strength is AS_NORMAL.
251 template <typename T>
252 static oop oop_load_in_heap(T* addr) {
253 return Raw::template oop_load<oop>(addr);
254 }
255
256 static oop oop_load_in_heap_at(oop base, ptrdiff_t offset) {
257 return Raw::template oop_load_at<oop>(base, offset);
258 }
259
260 template <typename T>
261 static void oop_store_in_heap(T* addr, oop value) {
262 Raw::oop_store(addr, value);
263 }
264
265 static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
266 Raw::oop_store_at(base, offset, value);
267 }
268
269 template <typename T>
270 static oop oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
271 return Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
272 }
273
274 static oop oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
275 return Raw::oop_atomic_cmpxchg_at(base, offset, compare_value, new_value);
276 }
277
278 template <typename T>
279 static oop oop_atomic_xchg_in_heap(T* addr, oop new_value) {
280 return Raw::oop_atomic_xchg(addr, new_value);
281 }
282
283 static oop oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
284 return Raw::oop_atomic_xchg_at(base, offset, new_value);
285 }
286
287 template <typename T>
288 static void oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
289 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
290 size_t length);
291
292 // Off-heap oop accesses. These accessors get resolved when
293 // IN_HEAP is not set (e.g. when using the NativeAccess API), it is
294 // an oop* overload, and the barrier strength is AS_NORMAL.
295 template <typename T>
296 static oop oop_load_not_in_heap(T* addr) {
297 return Raw::template oop_load<oop>(addr);
298 }
299
300 template <typename T>
301 static void oop_store_not_in_heap(T* addr, oop value) {
302 Raw::oop_store(addr, value);
303 }
304
305 template <typename T>
306 static oop oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
307 return Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
308 }
309
310 template <typename T>
311 static oop oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
312 return Raw::oop_atomic_xchg(addr, new_value);
313 }
314
315 // Clone barrier support
316 static void clone_in_heap(oop src, oop dst, size_t size) {
317 Raw::clone(src, dst, size);
318 }
319
320 static void value_copy_in_heap(void* src, void* dst, InlineKlass* md, LayoutKind lk) {
321 Raw::value_copy(src, dst, md, lk);
322 }
323
324 };
325 };
326
327 template<typename T>
328 inline T* barrier_set_cast(BarrierSet* bs) {
329 assert(bs->is_a(BarrierSet::GetName<T>::value), "wrong type of barrier set");
330 return static_cast<T*>(bs);
331 }
332
333 #endif // SHARE_GC_SHARED_BARRIERSET_HPP