1 /*
   2  * Copyright (c) 1997, 2018, 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 #ifdef COMPILER2
  34 #include "opto/matcher.hpp"
  35 #endif
  36 
  37 // ----------------------------------------------------------------------------
  38 
  39 #define __ _masm.
  40 address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf, address mark) {
  41   // Stub is fixed up when the corresponding call is converted from calling
  42   // compiled code to calling interpreted code.
  43   // set (empty), G5
  44   // jmp -1
  45 
  46   if (mark == NULL) {
  47     mark = cbuf.insts_mark();  // Get mark within main instrs section.
  48   }
  49 
  50   MacroAssembler _masm(&cbuf);
  51 
  52   address base = __ start_a_stub(to_interp_stub_size());
  53   if (base == NULL) {
  54     return NULL;  // CodeBuffer::expand failed.
  55   }
  56 
  57   // Static stub relocation stores the instruction address of the call.
  58   __ relocate(static_stub_Relocation::spec(mark));
  59 
  60   __ set_metadata(NULL, as_Register(Matcher::inline_cache_reg_encode()));
  61 
  62   __ set_inst_mark();
  63   AddressLiteral addrlit(-1);
  64   __ JUMP(addrlit, G3, 0);
  65 
  66   __ delayed()->nop();
  67 
  68   assert(__ pc() - base <= to_interp_stub_size(), "wrong stub size");
  69 
  70   // Update current stubs pointer and restore code_end.
  71   __ end_a_stub();
  72   return base;
  73 }
  74 #undef __
  75 
  76 int CompiledStaticCall::to_trampoline_stub_size() {
  77   // SPARC doesn't use trampolines.
  78   return 0;
  79 }
  80 
  81 int CompiledStaticCall::to_interp_stub_size() {
  82   // This doesn't need to be accurate but it must be larger or equal to
  83   // the real size of the stub.
  84   return (NativeMovConstReg::instruction_size +  // sethi/setlo;
  85           NativeJump::instruction_size); // sethi; jmp; nop
  86 }
  87 
  88 // Relocation entries for call stub, compiled java to interpreter.
  89 int CompiledStaticCall::reloc_to_interp_stub() {
  90   return 10;  // 4 in emit_java_to_interp + 1 in Java_Static_Call
  91 }
  92 
  93 void CompiledDirectStaticCall::set_to_interpreted(const methodHandle& callee, address entry) {
  94   address stub = find_stub(/*is_aot*/ false);
  95   guarantee(stub != NULL, "stub not found");
  96 
  97   if (TraceICs) {
  98     ResourceMark rm;
  99     tty->print_cr("CompiledDirectStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
 100                   p2i(instruction_address()),
 101                   callee->name_and_sig_as_C_string());
 102   }
 103 
 104   // Creation also verifies the object.
 105   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 106   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 107 
 108 #ifdef ASSERT
 109   // read the value once
 110   volatile intptr_t data = method_holder->data();
 111   volatile address destination = jump->jump_destination();
 112   assert(data == 0 || data == (intptr_t)callee(),
 113          "a) MT-unsafe modification of inline cache");
 114   assert(destination == (address)-1 || destination == entry,
 115          "b) MT-unsafe modification of inline cache");
 116 #endif
 117 
 118   // Update stub.
 119   method_holder->set_data((intptr_t)callee());
 120   jump->set_jump_destination(entry);
 121 
 122   // Update jump to call.
 123   set_destination_mt_safe(stub);
 124 }
 125 
 126 void CompiledDirectStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
 127   assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
 128   // Reset stub.
 129   address stub = static_stub->addr();
 130   assert(stub != NULL, "stub not found");
 131   // Creation also verifies the object.
 132   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 133   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 134   method_holder->set_data(0);
 135   jump->set_jump_destination((address)-1);
 136 }
 137 
 138 //-----------------------------------------------------------------------------
 139 // Non-product mode code
 140 #ifndef PRODUCT
 141 
 142 void CompiledDirectStaticCall::verify() {
 143   // Verify call.
 144   _call->verify();
 145   _call->verify_alignment();
 146 
 147   // Verify stub.
 148   address stub = find_stub(/*is_aot*/ false);
 149   assert(stub != NULL, "no stub found for static call");
 150   // Creation also verifies the object.
 151   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub);
 152   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 153 
 154   // Verify state.
 155   assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
 156 }
 157 
 158 #endif // !PRODUCT