161 #if (defined(AARCH64) || defined(RISCV64)) && !defined(ZERO)
162 // We clear the patching epoch when disarming nmethods, so that
163 // the counter won't overflow.
164 BarrierSetAssembler::clear_patching_epoch();
165 #endif
166 }
167
168 int BarrierSetNMethod::nmethod_stub_entry_barrier(address* return_address_ptr) {
169 // Enable WXWrite: the function is called directly from nmethod_entry_barrier
170 // stub.
171 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current()));
172
173 address return_address = *return_address_ptr;
174 AARCH64_PORT_ONLY(return_address = pauth_strip_pointer(return_address));
175 CodeBlob* cb = CodeCache::find_blob(return_address);
176 assert(cb != nullptr, "invariant");
177
178 nmethod* nm = cb->as_nmethod();
179 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
180
181 // Called upon first entry after being armed
182 bool may_enter = bs_nm->nmethod_entry_barrier(nm);
183 assert(!nm->is_osr_method() || may_enter, "OSR nmethods should always be entrant after migration");
184
185 // In case a concurrent thread disarmed the nmethod, we need to ensure the new instructions
186 // are made visible, by using a cross modify fence. Note that this is synchronous cross modifying
187 // code, where the existence of new instructions is communicated via data (the guard value).
188 // This cross modify fence is only needed when the nmethod entry barrier modifies the
189 // instructions. Not all platforms currently do that, so if this check becomes expensive,
190 // it can be made conditional on the nmethod_patching_type.
191 OrderAccess::cross_modify_fence();
192
193 // Diagnostic option to force deoptimization 1 in 10 times. It is otherwise
194 // a very rare event.
195 if (DeoptimizeNMethodBarriersALot && !nm->is_osr_method()) {
196 static volatile uint32_t counter=0;
197 if (Atomic::add(&counter, 1u) % 10 == 0) {
198 may_enter = false;
199 }
200 }
201
202 if (!may_enter) {
203 log_trace(nmethod, barrier)("Deoptimizing nmethod: " PTR_FORMAT, p2i(nm));
204 bs_nm->deoptimize(nm, return_address_ptr);
205 }
206 return may_enter ? 0 : 1;
207 }
208
209 bool BarrierSetNMethod::nmethod_osr_entry_barrier(nmethod* nm) {
210 assert(nm->is_osr_method(), "Should not reach here");
211 log_trace(nmethod, barrier)("Running osr nmethod entry barrier: " PTR_FORMAT, p2i(nm));
212 bool result = nmethod_entry_barrier(nm);
213 OrderAccess::cross_modify_fence();
214 return result;
215 }
|
161 #if (defined(AARCH64) || defined(RISCV64)) && !defined(ZERO)
162 // We clear the patching epoch when disarming nmethods, so that
163 // the counter won't overflow.
164 BarrierSetAssembler::clear_patching_epoch();
165 #endif
166 }
167
168 int BarrierSetNMethod::nmethod_stub_entry_barrier(address* return_address_ptr) {
169 // Enable WXWrite: the function is called directly from nmethod_entry_barrier
170 // stub.
171 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current()));
172
173 address return_address = *return_address_ptr;
174 AARCH64_PORT_ONLY(return_address = pauth_strip_pointer(return_address));
175 CodeBlob* cb = CodeCache::find_blob(return_address);
176 assert(cb != nullptr, "invariant");
177
178 nmethod* nm = cb->as_nmethod();
179 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
180
181 log_trace(nmethod, barrier)("Running nmethod entry barrier: %d " PTR_FORMAT, nm->compile_id(), p2i(nm));
182 // Called upon first entry after being armed
183 bool may_enter = bs_nm->nmethod_entry_barrier(nm);
184 assert(!nm->is_osr_method() || may_enter, "OSR nmethods should always be entrant after migration");
185
186 // In case a concurrent thread disarmed the nmethod, we need to ensure the new instructions
187 // are made visible, by using a cross modify fence. Note that this is synchronous cross modifying
188 // code, where the existence of new instructions is communicated via data (the guard value).
189 // This cross modify fence is only needed when the nmethod entry barrier modifies the
190 // instructions. Not all platforms currently do that, so if this check becomes expensive,
191 // it can be made conditional on the nmethod_patching_type.
192 OrderAccess::cross_modify_fence();
193
194 // Diagnostic option to force deoptimization 1 in 10 times. It is otherwise
195 // a very rare event.
196 if (DeoptimizeNMethodBarriersALot && !nm->is_osr_method()) {
197 static volatile uint32_t counter=0;
198 if (Atomic::add(&counter, 1u) % 10 == 0) {
199 may_enter = false;
200 }
201 }
202
203 if (may_enter) {
204 nm->set_used();
205 } else {
206 log_trace(nmethod, barrier)("Deoptimizing nmethod: " PTR_FORMAT, p2i(nm));
207 bs_nm->deoptimize(nm, return_address_ptr);
208 }
209 return may_enter ? 0 : 1;
210 }
211
212 bool BarrierSetNMethod::nmethod_osr_entry_barrier(nmethod* nm) {
213 assert(nm->is_osr_method(), "Should not reach here");
214 log_trace(nmethod, barrier)("Running osr nmethod entry barrier: %d " PTR_FORMAT, nm->compile_id(), p2i(nm));
215 bool result = nmethod_entry_barrier(nm);
216 if (result) {
217 nm->set_used();
218 }
219 OrderAccess::cross_modify_fence();
220 return result;
221 }
|