Path: blob/master/src/hotspot/cpu/aarch64/gc/shared/barrierSetNMethod_aarch64.cpp
41155 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/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/registerMap.hpp"32#include "runtime/thread.hpp"33#include "utilities/align.hpp"34#include "utilities/debug.hpp"3536class NativeNMethodBarrier: public NativeInstruction {37address instruction_address() const { return addr_at(0); }3839int *guard_addr() {40return reinterpret_cast<int*>(instruction_address() + 10 * 4);41}4243public:44int get_value() {45return Atomic::load_acquire(guard_addr());46}4748void set_value(int value) {49Atomic::release_store(guard_addr(), value);50}5152void verify() const;53};5455// Store the instruction bitmask, bits and name for checking the barrier.56struct CheckInsn {57uint32_t mask;58uint32_t bits;59const char *name;60};6162static const struct CheckInsn barrierInsn[] = {63{ 0xff000000, 0x18000000, "ldr (literal)" },64{ 0xfffff0ff, 0xd50330bf, "dmb" },65{ 0xffc00000, 0xb9400000, "ldr"},66{ 0x7f20001f, 0x6b00001f, "cmp"},67{ 0xff00001f, 0x54000000, "b.eq"},68{ 0xff800000, 0xd2800000, "mov"},69{ 0xff800000, 0xf2800000, "movk"},70{ 0xff800000, 0xf2800000, "movk"},71{ 0xfffffc1f, 0xd63f0000, "blr"},72{ 0xfc000000, 0x14000000, "b"}73};7475// The encodings must match the instructions emitted by76// BarrierSetAssembler::nmethod_entry_barrier. The matching ignores the specific77// register numbers and immediate values in the encoding.78void NativeNMethodBarrier::verify() const {79intptr_t addr = (intptr_t) instruction_address();80for(unsigned int i = 0; i < sizeof(barrierInsn)/sizeof(struct CheckInsn); i++ ) {81uint32_t inst = *((uint32_t*) addr);82if ((inst & barrierInsn[i].mask) != barrierInsn[i].bits) {83tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", addr, inst);84fatal("not an %s instruction.", barrierInsn[i].name);85}86addr +=4;87}88}899091/* We're called from an nmethod when we need to deoptimize it. We do92this by throwing away the nmethod's frame and jumping to the93ic_miss stub. This looks like there has been an IC miss at the94entry of the nmethod, so we resolve the call, which will fall back95to the interpreter if the nmethod has been unloaded. */96void BarrierSetNMethod::deoptimize(nmethod* nm, address* return_address_ptr) {9798typedef struct {99intptr_t *sp; intptr_t *fp; address lr; address pc;100} frame_pointers_t;101102frame_pointers_t *new_frame = (frame_pointers_t *)(return_address_ptr - 5);103104JavaThread *thread = JavaThread::current();105RegisterMap reg_map(thread, false);106frame frame = thread->last_frame();107108assert(frame.is_compiled_frame() || frame.is_native_frame(), "must be");109assert(frame.cb() == nm, "must be");110frame = frame.sender(®_map);111112LogTarget(Trace, nmethod, barrier) out;113if (out.is_enabled()) {114ResourceMark mark;115log_trace(nmethod, barrier)("deoptimize(nmethod: %s(%p), return_addr: %p, osr: %d, thread: %p(%s), making rsp: %p) -> %p",116nm->method()->name_and_sig_as_C_string(),117nm, *(address *) return_address_ptr, nm->is_osr_method(), thread,118thread->get_thread_name(), frame.sp(), nm->verified_entry_point());119}120121new_frame->sp = frame.sp();122new_frame->fp = frame.fp();123new_frame->lr = frame.pc();124new_frame->pc = SharedRuntime::get_handle_wrong_method_stub();125}126127// This is the offset of the entry barrier from where the frame is completed.128// If any code changes between the end of the verified entry where the entry129// barrier resides, and the completion of the frame, then130// NativeNMethodCmpBarrier::verify() will immediately complain when it does131// not find the expected native instruction at this offset, which needs updating.132// Note that this offset is invariant of PreserveFramePointer.133134static const int entry_barrier_offset = -4 * 11;135136static NativeNMethodBarrier* native_nmethod_barrier(nmethod* nm) {137address barrier_address = nm->code_begin() + nm->frame_complete_offset() + entry_barrier_offset;138NativeNMethodBarrier* barrier = reinterpret_cast<NativeNMethodBarrier*>(barrier_address);139debug_only(barrier->verify());140return barrier;141}142143void BarrierSetNMethod::disarm(nmethod* nm) {144if (!supports_entry_barrier(nm)) {145return;146}147148// Disarms the nmethod guard emitted by BarrierSetAssembler::nmethod_entry_barrier.149// Symmetric "LDR; DMB ISHLD" is in the nmethod barrier.150NativeNMethodBarrier* barrier = native_nmethod_barrier(nm);151152barrier->set_value(disarmed_value());153}154155bool BarrierSetNMethod::is_armed(nmethod* nm) {156if (!supports_entry_barrier(nm)) {157return false;158}159160NativeNMethodBarrier* barrier = native_nmethod_barrier(nm);161return barrier->get_value() != disarmed_value();162}163164165