1 /*
2 * Copyright (c) 2018, 2025, 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 #include "code/codeCache.hpp"
26 #include "code/nmethod.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/barrierSetAssembler.hpp"
29 #include "gc/shared/barrierSetNMethod.hpp"
30 #include "gc/shared/collectedHeap.hpp"
31 #include "logging/log.hpp"
32 #include "memory/iterator.hpp"
33 #include "memory/universe.hpp"
34 #include "oops/access.inline.hpp"
35 #include "oops/method.inline.hpp"
36 #include "runtime/frame.inline.hpp"
37 #include "runtime/javaThread.hpp"
38 #include "runtime/threads.hpp"
39 #include "runtime/threadWXSetters.inline.hpp"
40 #include "utilities/debug.hpp"
41 #if INCLUDE_JVMCI
42 #include "jvmci/jvmciRuntime.hpp"
43 #endif
44
45 int BarrierSetNMethod::disarmed_guard_value() const {
46 return *disarmed_guard_value_address();
47 }
48
49 bool BarrierSetNMethod::supports_entry_barrier(nmethod* nm) {
50 if (nm->method()->is_method_handle_intrinsic()) {
51 return false;
52 }
53
54 if (nm->method()->is_continuation_enter_intrinsic()) {
55 return false;
56 }
57
58 if (nm->method()->is_continuation_yield_intrinsic()) {
59 return false;
60 }
61
62 if (nm->method()->is_continuation_native_intrinsic()) {
63 guarantee(false, "Unknown Continuation native intrinsic");
64 return false;
65 }
66
67 if (nm->is_native_method() || nm->is_compiled_by_c2() || nm->is_compiled_by_c1() || nm->is_compiled_by_jvmci()) {
68 return true;
69 }
70
71 return false;
72 }
73
74 void BarrierSetNMethod::disarm(nmethod* nm) {
75 set_guard_value(nm, disarmed_guard_value());
76 }
77
78 void BarrierSetNMethod::guard_with(nmethod* nm, int value) {
79 assert((value & not_entrant) == 0, "not_entrant bit is reserved");
80 set_guard_value(nm, value);
81 }
82
83 bool BarrierSetNMethod::is_armed(nmethod* nm) {
84 return (guard_value(nm) & ~not_entrant) != disarmed_guard_value();
85 }
86
87 bool BarrierSetNMethod::nmethod_entry_barrier(nmethod* nm) {
88 class OopKeepAliveClosure : public OopClosure {
89 public:
90 virtual void do_oop(oop* p) {
91 // Loads on nmethod oops are phantom strength.
92 //
93 // Note that we could have used NativeAccess<ON_PHANTOM_OOP_REF>::oop_load(p),
94 // but that would have *required* us to convert the returned LoadOopProxy to an oop,
95 // or else keep alive load barrier will never be called. It's the LoadOopProxy-to-oop
96 // conversion that performs the load barriers. This is too subtle, so we instead
97 // perform an explicit keep alive call.
98 oop obj = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(p);
99 if (obj != nullptr) {
100 Universe::heap()->keep_alive(obj);
101 }
102 }
103
104 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
105 };
106
107 if (!is_armed(nm)) {
108 // Some other thread got here first and healed the oops
109 // and disarmed the nmethod. No need to continue.
110 return true;
111 }
112
113 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current()));
114
115 // If the nmethod is the only thing pointing to the oops, and we are using a
116 // SATB GC, then it is important that this code marks them live.
117 // Also, with concurrent GC, it is possible that frames in continuation stack
118 // chunks are not visited if they are allocated after concurrent GC started.
119 OopKeepAliveClosure cl;
120 nm->oops_do(&cl);
121
122 // CodeCache unloading support
123 nm->mark_as_maybe_on_stack();
124
125 disarm(nm);
126
127 return true;
128 }
129
130 int* BarrierSetNMethod::disarmed_guard_value_address() const {
131 return (int*) &_current_phase;
132 }
133
134 ByteSize BarrierSetNMethod::thread_disarmed_guard_value_offset() const {
135 return Thread::nmethod_disarmed_guard_value_offset();
136 }
137
138 class BarrierSetNMethodArmClosure : public ThreadClosure {
139 private:
140 int _disarmed_guard_value;
141
142 public:
143 BarrierSetNMethodArmClosure(int disarmed_guard_value) :
144 _disarmed_guard_value(disarmed_guard_value) {}
145
146 virtual void do_thread(Thread* thread) {
147 thread->set_nmethod_disarmed_guard_value(_disarmed_guard_value);
148 }
149 };
150
151 void BarrierSetNMethod::arm_all_nmethods() {
152 // Change to a new global GC phase. Doing this requires changing the thread-local
153 // disarm value for all threads, to reflect the new GC phase.
154 // We wrap around at INT_MAX. That means that we assume nmethods won't have ABA
155 // problems in their nmethod disarm values after INT_MAX - 1 GCs. Every time a GC
156 // completes, ABA problems are removed, but if a concurrent GC is started and then
157 // aborted N times, that is when there could be ABA problems. If there are anything
158 // close to INT_MAX - 1 GCs starting without being able to finish, something is
159 // seriously wrong.
160 ++_current_phase;
161 if (_current_phase == INT_MAX) {
162 _current_phase = initial;
163 }
164 BarrierSetNMethodArmClosure cl(_current_phase);
165 Threads::threads_do(&cl);
166
167 #if (defined(AARCH64) || defined(RISCV64)) && !defined(ZERO)
168 // We clear the patching epoch when disarming nmethods, so that
169 // the counter won't overflow.
170 BarrierSetAssembler::clear_patching_epoch();
171 #endif
172 }
173
174 int BarrierSetNMethod::nmethod_stub_entry_barrier(address* return_address_ptr) {
175 address return_address = *return_address_ptr;
176 AARCH64_PORT_ONLY(return_address = pauth_strip_pointer(return_address));
177 CodeBlob* cb = CodeCache::find_blob(return_address);
178 assert(cb != nullptr, "invariant");
179
180 nmethod* nm = cb->as_nmethod();
181 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
182
183 // Called upon first entry after being armed
184 bool may_enter = !bs_nm->is_not_entrant(nm) && bs_nm->nmethod_entry_barrier(nm);
185 assert(!nm->is_osr_method() || may_enter, "OSR nmethods should always be entrant after migration");
186
187 if (may_enter) {
188 // In case a concurrent thread disarmed the nmethod, we need to ensure the new instructions
189 // are made visible, by using a cross modify fence. Note that this is synchronous cross modifying
190 // code, where the existence of new instructions is communicated via data (the guard value).
191 // This cross modify fence is only needed when the nmethod entry barrier modifies the
192 // instructions. Not all platforms currently do that, so if this check becomes expensive,
193 // it can be made conditional on the nmethod_patching_type.
194 OrderAccess::cross_modify_fence();
195
196 // Diagnostic option to force deoptimization 1 in 10 times. It is otherwise
197 // a very rare event.
198 if (DeoptimizeNMethodBarriersALot && !nm->is_osr_method()) {
199 static volatile uint32_t counter=0;
200 if (AtomicAccess::add(&counter, 1u) % 10 == 0) {
201 may_enter = false;
202 }
203 }
204 }
205
206 if (!may_enter) {
207 log_trace(nmethod, barrier)("Deoptimizing nmethod: " PTR_FORMAT, p2i(nm));
208 bs_nm->deoptimize(nm, return_address_ptr);
209 }
210 return may_enter ? 0 : 1;
211 }
212
213 bool BarrierSetNMethod::nmethod_osr_entry_barrier(nmethod* nm) {
214 assert(nm->is_osr_method(), "Should not reach here");
215 log_trace(nmethod, barrier)("Running osr nmethod entry barrier: " PTR_FORMAT, p2i(nm));
216 bool result = nmethod_entry_barrier(nm);
217 OrderAccess::cross_modify_fence();
218 return result;
219 }
220
221 oop BarrierSetNMethod::oop_load_no_keepalive(const nmethod* nm, int index) {
222 return NativeAccess<AS_NO_KEEPALIVE>::oop_load(nm->oop_addr_at(index));
223 }
224
225 oop BarrierSetNMethod::oop_load_phantom(const nmethod* nm, int index) {
226 return NativeAccess<ON_PHANTOM_OOP_REF>::oop_load(nm->oop_addr_at(index));
227 }
228
229 // Make the nmethod permanently not-entrant, so that nmethod_stub_entry_barrier() will call
230 // deoptimize() to redirect the caller to SharedRuntime::get_handle_wrong_method_stub().
231 // A sticky armed bit is set and other bits are preserved. As a result, a call to
232 // nmethod_stub_entry_barrier() may appear to be spurious, because is_armed() still returns
233 // false and nmethod_entry_barrier() is not called.
234 void BarrierSetNMethod::make_not_entrant(nmethod* nm) {
235 set_guard_value(nm, not_entrant, not_entrant);
236 }
237
238 bool BarrierSetNMethod::is_not_entrant(nmethod* nm) {
239 return (guard_value(nm) & not_entrant) != 0;
240 }