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