Path: blob/master/src/hotspot/cpu/x86/gc/shared/barrierSetNMethod_x86.cpp
41155 views
/*1* Copyright (c) 2018, 2020, 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/nativeInst.hpp"27#include "gc/shared/barrierSetNMethod.hpp"28#include "logging/log.hpp"29#include "memory/resourceArea.hpp"30#include "runtime/sharedRuntime.hpp"31#include "runtime/thread.hpp"32#include "utilities/align.hpp"33#include "utilities/debug.hpp"3435class NativeNMethodCmpBarrier: public NativeInstruction {36public:37#ifdef _LP6438enum Intel_specific_constants {39instruction_code = 0x81,40instruction_size = 8,41imm_offset = 4,42instruction_rex_prefix = Assembler::REX | Assembler::REX_B,43instruction_modrm = 0x7f // [r15 + offset]44};45#else46enum Intel_specific_constants {47instruction_code = 0x81,48instruction_size = 7,49imm_offset = 2,50instruction_modrm = 0x3f // [rdi]51};52#endif5354address instruction_address() const { return addr_at(0); }55address immediate_address() const { return addr_at(imm_offset); }5657jint get_immedate() const { return int_at(imm_offset); }58void set_immediate(jint imm) { set_int_at(imm_offset, imm); }59void verify() const;60};6162#ifdef _LP6463void NativeNMethodCmpBarrier::verify() const {64if (((uintptr_t) instruction_address()) & 0x7) {65fatal("Not properly aligned");66}6768int prefix = ubyte_at(0);69if (prefix != instruction_rex_prefix) {70tty->print_cr("Addr: " INTPTR_FORMAT " Prefix: 0x%x", p2i(instruction_address()),71prefix);72fatal("not a cmp barrier");73}7475int inst = ubyte_at(1);76if (inst != instruction_code) {77tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", p2i(instruction_address()),78inst);79fatal("not a cmp barrier");80}8182int modrm = ubyte_at(2);83if (modrm != instruction_modrm) {84tty->print_cr("Addr: " INTPTR_FORMAT " mod/rm: 0x%x", p2i(instruction_address()),85modrm);86fatal("not a cmp barrier");87}88}89#else90void NativeNMethodCmpBarrier::verify() const {91if (((uintptr_t) instruction_address()) & 0x3) {92fatal("Not properly aligned");93}9495int inst = ubyte_at(0);96if (inst != instruction_code) {97tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", p2i(instruction_address()),98inst);99fatal("not a cmp barrier");100}101102int modrm = ubyte_at(1);103if (modrm != instruction_modrm) {104tty->print_cr("Addr: " INTPTR_FORMAT " mod/rm: 0x%x", p2i(instruction_address()),105modrm);106fatal("not a cmp barrier");107}108}109#endif // _LP64110111void BarrierSetNMethod::deoptimize(nmethod* nm, address* return_address_ptr) {112/*113* [ callers frame ]114* [ callers return address ] <- callers rsp115* [ callers rbp ] <- callers rbp116* [ callers frame slots ]117* [ return_address ] <- return_address_ptr118* [ cookie ] <- used to write the new rsp (callers rsp)119* [ stub rbp ]120* [ stub stuff ]121*/122123address* stub_rbp = return_address_ptr - 2;124address* callers_rsp = return_address_ptr + nm->frame_size(); /* points to callers return_address now */125address* callers_rbp = callers_rsp - 1; // 1 to move to the callers return address, 1 more to move to the rbp126address* cookie = return_address_ptr - 1;127128LogTarget(Trace, nmethod, barrier) out;129if (out.is_enabled()) {130JavaThread* jth = JavaThread::current();131ResourceMark mark;132log_trace(nmethod, barrier)("deoptimize(nmethod: %p, return_addr: %p, osr: %d, thread: %p(%s), making rsp: %p) -> %p",133nm, (address *) return_address_ptr, nm->is_osr_method(), jth,134jth->get_thread_name(), callers_rsp, nm->verified_entry_point());135}136137assert(nm->frame_size() >= 3, "invariant");138assert(*cookie == (address) -1, "invariant");139140// Preserve caller rbp.141*stub_rbp = *callers_rbp;142143// At the cookie address put the callers rsp.144*cookie = (address) callers_rsp; // should point to the return address145146// In the slot that used to be the callers rbp we put the address that our stub needs to jump to at the end.147// Overwriting the caller rbp should be okay since our stub rbp has the same value.148address* jmp_addr_ptr = callers_rbp;149*jmp_addr_ptr = SharedRuntime::get_handle_wrong_method_stub();150}151152// This is the offset of the entry barrier from where the frame is completed.153// If any code changes between the end of the verified entry where the entry154// barrier resides, and the completion of the frame, then155// NativeNMethodCmpBarrier::verify() will immediately complain when it does156// not find the expected native instruction at this offset, which needs updating.157// Note that this offset is invariant of PreserveFramePointer.158static const int entry_barrier_offset = LP64_ONLY(-19) NOT_LP64(-18);159160static NativeNMethodCmpBarrier* native_nmethod_barrier(nmethod* nm) {161address barrier_address = nm->code_begin() + nm->frame_complete_offset() + entry_barrier_offset;162NativeNMethodCmpBarrier* barrier = reinterpret_cast<NativeNMethodCmpBarrier*>(barrier_address);163debug_only(barrier->verify());164return barrier;165}166167void BarrierSetNMethod::disarm(nmethod* nm) {168if (!supports_entry_barrier(nm)) {169return;170}171172NativeNMethodCmpBarrier* cmp = native_nmethod_barrier(nm);173cmp->set_immediate(disarmed_value());174}175176bool BarrierSetNMethod::is_armed(nmethod* nm) {177if (!supports_entry_barrier(nm)) {178return false;179}180181NativeNMethodCmpBarrier* cmp = native_nmethod_barrier(nm);182return (disarmed_value() != cmp->get_immedate());183}184185186