1 /*
  2  * Copyright (c) 2018, 2021, 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 "precompiled.hpp"
 26 #include "code/codeCache.hpp"
 27 #include "code/nativeInst.hpp"
 28 #include "gc/shared/barrierSetNMethod.hpp"
 29 #include "logging/log.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "runtime/sharedRuntime.hpp"
 32 #include "runtime/registerMap.hpp"
 33 #include "runtime/thread.hpp"
 34 #include "utilities/align.hpp"
 35 #include "utilities/debug.hpp"
 36 
 37 class NativeNMethodBarrier: public NativeInstruction {
 38   address instruction_address() const { return addr_at(0); }
 39 
 40   int *guard_addr() {
 41     return reinterpret_cast<int*>(instruction_address() + 10 * 4);
 42   }
 43 
 44 public:
 45   int get_value() {
 46     return Atomic::load_acquire(guard_addr());
 47   }
 48 
 49   void set_value(int value) {
 50     Atomic::release_store(guard_addr(), value);
 51   }
 52 
 53   void verify() const;
 54 };
 55 
 56 // Store the instruction bitmask, bits and name for checking the barrier.
 57 struct CheckInsn {
 58   uint32_t mask;
 59   uint32_t bits;
 60   const char *name;
 61 };
 62 
 63 static const struct CheckInsn barrierInsn[] = {
 64   { 0xff000000, 0x18000000, "ldr (literal)" },
 65   { 0xfffff0ff, 0xd50330bf, "dmb" },
 66   { 0xffc00000, 0xb9400000, "ldr"},
 67   { 0x7f20001f, 0x6b00001f, "cmp"},
 68   { 0xff00001f, 0x54000000, "b.eq"},
 69   { 0xff800000, 0xd2800000, "mov"},
 70   { 0xff800000, 0xf2800000, "movk"},
 71   { 0xff800000, 0xf2800000, "movk"},
 72   { 0xfffffc1f, 0xd63f0000, "blr"},
 73   { 0xfc000000, 0x14000000, "b"}
 74 };
 75 
 76 // The encodings must match the instructions emitted by
 77 // BarrierSetAssembler::nmethod_entry_barrier. The matching ignores the specific
 78 // register numbers and immediate values in the encoding.
 79 void NativeNMethodBarrier::verify() const {
 80   intptr_t addr = (intptr_t) instruction_address();
 81   for(unsigned int i = 0; i < sizeof(barrierInsn)/sizeof(struct CheckInsn); i++ ) {
 82     uint32_t inst = *((uint32_t*) addr);
 83     if ((inst & barrierInsn[i].mask) != barrierInsn[i].bits) {
 84       tty->print_cr("Addr: " INTPTR_FORMAT " Code: 0x%x", addr, inst);
 85       fatal("not an %s instruction.", barrierInsn[i].name);
 86     }
 87     addr +=4;
 88   }
 89 }
 90 
 91 
 92 /* We're called from an nmethod when we need to deoptimize it. We do
 93    this by throwing away the nmethod's frame and jumping to the
 94    ic_miss stub. This looks like there has been an IC miss at the
 95    entry of the nmethod, so we resolve the call, which will fall back
 96    to the interpreter if the nmethod has been unloaded. */
 97 void BarrierSetNMethod::deoptimize(nmethod* nm, address* return_address_ptr) {
 98 
 99   typedef struct {
100     intptr_t *sp; intptr_t *fp; address lr; address pc;
101   } frame_pointers_t;
102 
103   frame_pointers_t *new_frame = (frame_pointers_t *)(return_address_ptr - 5);
104 
105   JavaThread *thread = JavaThread::current();
106   RegisterMap reg_map(thread, false);
107   frame frame = thread->last_frame();
108 
109   assert(frame.is_compiled_frame() || frame.is_native_frame(), "must be");
110   assert(frame.cb() == nm, "must be");
111   frame = frame.sender(&reg_map);
112 
113   LogTarget(Trace, nmethod, barrier) out;
114   if (out.is_enabled()) {
115     ResourceMark mark;
116     log_trace(nmethod, barrier)("deoptimize(nmethod: %s(%p), return_addr: %p, osr: %d, thread: %p(%s), making rsp: %p) -> %p",
117                                 nm->method()->name_and_sig_as_C_string(),
118                                 nm, *(address *) return_address_ptr, nm->is_osr_method(), thread,
119                                 thread->name(), frame.sp(), nm->verified_entry_point());
120   }
121 
122   new_frame->sp = frame.sp();
123   new_frame->fp = frame.fp();
124   new_frame->lr = frame.pc();
125   new_frame->pc = SharedRuntime::get_handle_wrong_method_stub();
126 }
127 
128 // This is the offset of the entry barrier from where the frame is completed.
129 // If any code changes between the end of the verified entry where the entry
130 // barrier resides, and the completion of the frame, then
131 // NativeNMethodCmpBarrier::verify() will immediately complain when it does
132 // not find the expected native instruction at this offset, which needs updating.
133 // Note that this offset is invariant of PreserveFramePointer.
134 
135 static const int entry_barrier_offset = -4 * 11;
136 
137 static NativeNMethodBarrier* native_nmethod_barrier(nmethod* nm) {
138   address barrier_address = nm->code_begin() + nm->frame_complete_offset() + entry_barrier_offset;
139   NativeNMethodBarrier* barrier = reinterpret_cast<NativeNMethodBarrier*>(barrier_address);
140   debug_only(barrier->verify());
141   return barrier;
142 }
143 
144 void BarrierSetNMethod::disarm(nmethod* nm) {
145   if (!supports_entry_barrier(nm)) {
146     return;
147   }
148 
149   // Disarms the nmethod guard emitted by BarrierSetAssembler::nmethod_entry_barrier.
150   // Symmetric "LDR; DMB ISHLD" is in the nmethod barrier.
151   NativeNMethodBarrier* barrier = native_nmethod_barrier(nm);
152 
153   barrier->set_value(disarmed_value());
154 }
155 
156 bool BarrierSetNMethod::is_armed(nmethod* nm) {
157   if (!supports_entry_barrier(nm)) {
158     return false;
159   }
160 
161   NativeNMethodBarrier* barrier = native_nmethod_barrier(nm);
162   return barrier->get_value() != disarmed_value();
163 }