Path: blob/master/src/hotspot/share/gc/shared/barrierSetNMethod.cpp
40957 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/codeCache.hpp"26#include "code/nmethod.hpp"27#include "gc/shared/barrierSet.hpp"28#include "gc/shared/barrierSetNMethod.hpp"29#include "logging/log.hpp"30#include "runtime/thread.hpp"31#include "runtime/threadWXSetters.inline.hpp"32#include "utilities/debug.hpp"3334int BarrierSetNMethod::disarmed_value() const {35return *disarmed_value_address();36}3738bool BarrierSetNMethod::supports_entry_barrier(nmethod* nm) {39if (nm->method()->is_method_handle_intrinsic()) {40return false;41}4243if (!nm->is_native_method() && !nm->is_compiled_by_c2() && !nm->is_compiled_by_c1()) {44return false;45}4647return true;48}4950int BarrierSetNMethod::nmethod_stub_entry_barrier(address* return_address_ptr) {51// Enable WXWrite: the function is called directly from nmethod_entry_barrier52// stub.53MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current()));5455address return_address = *return_address_ptr;56CodeBlob* cb = CodeCache::find_blob(return_address);57assert(cb != NULL, "invariant");5859nmethod* nm = cb->as_nmethod();60BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();6162if (!bs_nm->is_armed(nm)) {63return 0;64}6566assert(!nm->is_osr_method(), "Should not reach here");67// Called upon first entry after being armed68bool may_enter = bs_nm->nmethod_entry_barrier(nm);6970// Diagnostic option to force deoptimization 1 in 3 times. It is otherwise71// a very rare event.72if (DeoptimizeNMethodBarriersALot) {73static volatile uint32_t counter=0;74if (Atomic::add(&counter, 1u) % 3 == 0) {75may_enter = false;76}77}7879if (!may_enter) {80log_trace(nmethod, barrier)("Deoptimizing nmethod: " PTR_FORMAT, p2i(nm));81bs_nm->deoptimize(nm, return_address_ptr);82}83return may_enter ? 0 : 1;84}8586bool BarrierSetNMethod::nmethod_osr_entry_barrier(nmethod* nm) {87// This check depends on the invariant that all nmethods that are deoptimized / made not entrant88// are NOT disarmed.89// This invariant is important because a method can be deoptimized after the method have been90// resolved / looked up by OSR by another thread. By not deoptimizing them we guarantee that91// a deoptimized method will always hit the barrier and come to the same conclusion - deoptimize92if (!is_armed(nm)) {93return true;94}9596assert(nm->is_osr_method(), "Should not reach here");97log_trace(nmethod, barrier)("Running osr nmethod entry barrier: " PTR_FORMAT, p2i(nm));98return nmethod_entry_barrier(nm);99}100101102