1 /*
   2  * Copyright (c) 1997, 2016, 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 "asm/macroAssembler.inline.hpp"
  27 #include "code/compiledIC.hpp"
  28 #include "code/icBuffer.hpp"
  29 #include "code/nmethod.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "runtime/mutexLocker.hpp"
  32 #include "runtime/safepoint.hpp"
  33 
  34 // ----------------------------------------------------------------------------
  35 
  36 #define __ _masm.
  37 address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf, address mark) {
  38   // Stub is fixed up when the corresponding call is converted from
  39   // calling compiled code to calling interpreted code.
  40   // movq rbx, 0
  41   // jmp -5 # to self
  42 
  43   if (mark == NULL) {
  44     mark = cbuf.insts_mark();  // Get mark within main instrs section.
  45   }
  46 
  47   // Note that the code buffer's insts_mark is always relative to insts.
  48   // That's why we must use the macroassembler to generate a stub.
  49   MacroAssembler _masm(&cbuf);
  50 
  51   address base = __ start_a_stub(to_interp_stub_size());
  52   if (base == NULL) {
  53     return NULL;  // CodeBuffer::expand failed.
  54   }
  55   // Static stub relocation stores the instruction address of the call.
  56   __ relocate(static_stub_Relocation::spec(mark), Assembler::imm_operand);
  57   // Static stub relocation also tags the Method* in the code-stream.
  58   __ mov_metadata(rbx, (Metadata*) NULL);  // Method is zapped till fixup time.
  59   // This is recognized as unresolved by relocs/nativeinst/ic code.
  60   __ jump(RuntimeAddress(__ pc()));
  61 
  62   assert(__ pc() - base <= to_interp_stub_size(), "wrong stub size");
  63 
  64   // Update current stubs pointer and restore insts_end.
  65   __ end_a_stub();
  66   return base;
  67 }
  68 #undef __
  69 
  70 int CompiledStaticCall::to_interp_stub_size() {
  71   return NOT_LP64(10)    // movl; jmp
  72          LP64_ONLY(15);  // movq (1+1+8); jmp (1+4)
  73 }
  74 
  75 // Relocation entries for call stub, compiled java to interpreter.
  76 int CompiledStaticCall::reloc_to_interp_stub() {
  77   return 4; // 3 in emit_to_interp_stub + 1 in emit_call
  78 }
  79 
  80 void CompiledStaticCall::set_to_interpreted(methodHandle callee, address entry) {
  81   address stub = find_stub();
  82   guarantee(stub != NULL, "stub not found");
  83 
  84   if (TraceICs) {
  85     ResourceMark rm;
  86     tty->print_cr("CompiledStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
  87                   p2i(instruction_address()),
  88                   callee->name_and_sig_as_C_string());
  89   }
  90 
  91   // Creation also verifies the object.
  92   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
  93   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
  94 
  95 #ifdef ASSERT
  96   // read the value once
  97   intptr_t data = method_holder->data();
  98   address destination = jump->jump_destination();
  99   assert(data == 0 || data == (intptr_t)callee(),
 100          "a) MT-unsafe modification of inline cache");
 101   assert(destination == (address)-1 || destination == entry,
 102          "b) MT-unsafe modification of inline cache");
 103 #endif
 104 
 105   // Update stub.
 106   method_holder->set_data((intptr_t)callee());
 107   jump->set_jump_destination(entry);
 108 
 109   // Update jump to call.
 110   set_destination_mt_safe(stub);
 111 }
 112 
 113 void CompiledStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
 114   assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
 115   // Reset stub.
 116   address stub = static_stub->addr();
 117   assert(stub != NULL, "stub not found");
 118   // Creation also verifies the object.
 119   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 120   method_holder->set_data(0);
 121   NativeJump* jump = nativeJump_at(method_holder->next_instruction_address());
 122   jump->set_jump_destination((address)-1);
 123 }
 124 
 125 
 126 //-----------------------------------------------------------------------------
 127 // Non-product mode code
 128 #ifndef PRODUCT
 129 
 130 void CompiledStaticCall::verify() {
 131   // Verify call.
 132   NativeCall::verify();
 133   if (os::is_MP()) {
 134     verify_alignment();
 135   }
 136 
 137   // Verify stub.
 138   address stub = find_stub();
 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 #endif // !PRODUCT