1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2015, Linaro Ltd. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "code/compiledIC.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "runtime/mutexLocker.hpp"
  34 #include "runtime/safepoint.hpp"
  35 
  36 // ----------------------------------------------------------------------------
  37 
  38 #define __ _masm.
  39 address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf, address mark) {
  40   // Stub is fixed up when the corresponding call is converted from
  41   // calling compiled code to calling interpreted code.
  42   // mov rmethod, 0
  43   // jmp -4 # to self
  44 
  45   if (mark == NULL) {
  46     mark = cbuf.insts_mark();  // Get mark within main instrs section.
  47   }
  48 
  49   // Note that the code buffer's insts_mark is always relative to insts.
  50   // That's why we must use the macroassembler to generate a stub.
  51   MacroAssembler _masm(&cbuf);
  52 
  53   address base = __ start_a_stub(to_interp_stub_size());
  54 
  55   int offset = __ offset();
  56   if (base == NULL) {
  57     return NULL;  // CodeBuffer::expand failed
  58   }
  59   // static stub relocation stores the instruction address of the call
  60   __ relocate(static_stub_Relocation::spec(mark));
  61   // static stub relocation also tags the Method* in the code-stream.
  62   __ mov_metadata(rmethod, (Metadata*)NULL);
  63   __ movptr(rscratch1, 0);
  64   __ b(rscratch1);
  65 
  66   assert((__ offset() - offset) <= (int)to_interp_stub_size(), "stub too big");
  67   __ end_a_stub();
  68   return base;
  69 }
  70 #undef __
  71 
  72 int CompiledStaticCall::to_interp_stub_size() {
  73   return 7 * NativeInstruction::arm_insn_sz;
  74 }
  75 
  76 int CompiledStaticCall::to_trampoline_stub_size() {
  77   // AArch32 doesn't use trampoline stubs.
  78   return 0;
  79 }
  80 
  81 // Relocation entries for call stub, compiled java to interpreter.
  82 int CompiledStaticCall::reloc_to_interp_stub() {
  83   return 4; // 3 in emit_to_interp_stub + 1 in emit_call
  84 }
  85 
  86 void CompiledDirectStaticCall::set_to_interpreted(const methodHandle& callee, address entry) {
  87   address stub = find_stub(false /* is_aot */);
  88   guarantee(stub != NULL, "stub not found");
  89 
  90   if (TraceICs) {
  91     ResourceMark rm;
  92     tty->print_cr("CompiledDirectStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
  93                   p2i(instruction_address()),
  94                   callee->name_and_sig_as_C_string());
  95   }
  96 
  97   // Creation also verifies the object.
  98   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
  99   NativeJump* jump = NativeJump::from(method_holder->next_instruction_address());
 100 #ifndef PRODUCT
 101   // read the value once
 102   volatile intptr_t data = method_holder->data();
 103   assert(data == 0 || data == (intptr_t)callee(),
 104          "a) MT-unsafe modification of inline cache");
 105   assert(data == 0 || jump->jump_destination() == entry,
 106          "b) MT-unsafe modification of inline cache");
 107 #endif
 108   // Update stub.
 109   method_holder->set_data((intptr_t)callee());
 110   jump->set_jump_destination(entry);
 111   ICache::invalidate_range(stub, to_interp_stub_size());
 112   // Update jump to call.
 113   set_destination_mt_safe(stub);
 114 }
 115 
 116 void CompiledDirectStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
 117   assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
 118   // Reset stub.
 119   address stub = static_stub->addr();
 120   assert(stub != NULL, "stub not found");
 121   // Creation also verifies the object.
 122   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 123   method_holder->set_data(0);
 124 }
 125 
 126 //-----------------------------------------------------------------------------
 127 // Non-product mode code
 128 #ifndef PRODUCT
 129 
 130 void CompiledDirectStaticCall::verify() {
 131   // Verify call.
 132   _call->verify();
 133   if (os::is_MP()) {
 134     _call->verify_alignment();
 135   }
 136 
 137   // Verify stub.
 138   address stub = find_stub(false /* is_aot */);
 139   assert(stub != NULL, "no stub found for static call");
 140   // Creation also verifies the object.
 141   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 142   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 143 
 144   // Verify state.
 145   assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
 146 }
 147 
 148 #endif // !PRODUCT