1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2015 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "code/compiledIC.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/nmethod.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "runtime/mutexLocker.hpp"
  33 #include "runtime/safepoint.hpp"
  34 #ifdef COMPILER2
  35 #include "opto/matcher.hpp"
  36 #endif
  37 
  38 // ----------------------------------------------------------------------------
  39 
  40 // A PPC CompiledDirectStaticCall looks like this:
  41 //
  42 // >>>> consts
  43 //
  44 // [call target1]
  45 // [IC cache]
  46 // [call target2]
  47 //
  48 // <<<< consts
  49 // >>>> insts
  50 //
  51 // bl offset16               -+  -+             ??? // How many bits available?
  52 //                            |   |
  53 // <<<< insts                 |   |
  54 // >>>> stubs                 |   |
  55 //                            |   |- trampoline_stub_Reloc
  56 // trampoline stub:           | <-+
  57 //   r2 = toc                 |
  58 //   r2 = [r2 + offset]       |       // Load call target1 from const section
  59 //   mtctr r2                 |
  60 //   bctr                     |- static_stub_Reloc
  61 // comp_to_interp_stub:   <---+
  62 //   r1 = toc
  63 //   ICreg = [r1 + IC_offset]         // Load IC from const section
  64 //   r1    = [r1 + offset]            // Load call target2 from const section
  65 //   mtctr r1
  66 //   bctr
  67 //
  68 // <<<< stubs
  69 //
  70 // The call instruction in the code either
  71 // - branches directly to a compiled method if offset encodable in instruction
  72 // - branches to the trampoline stub if offset to compiled method not encodable
  73 // - branches to the compiled_to_interp stub if target interpreted
  74 //
  75 // Further there are three relocations from the loads to the constants in
  76 // the constant section.
  77 //
  78 // Usage of r1 and r2 in the stubs allows to distinguish them.
  79 
  80 const int IC_pos_in_java_to_interp_stub = 8;
  81 #define __ _masm.
  82 address CompiledStaticCall::emit_to_interp_stub(CodeBuffer &cbuf, address mark/* = NULL*/) {
  83 #ifdef COMPILER2
  84   if (mark == NULL) {
  85     // Get the mark within main instrs section which is set to the address of the call.
  86     mark = cbuf.insts_mark();
  87   }
  88 
  89   // Note that the code buffer's insts_mark is always relative to insts.
  90   // That's why we must use the macroassembler to generate a stub.
  91   MacroAssembler _masm(&cbuf);
  92 
  93   // Start the stub.
  94   address stub = __ start_a_stub(CompiledStaticCall::to_interp_stub_size());
  95   if (stub == NULL) {
  96     return NULL; // CodeCache is full
  97   }
  98 
  99   // For java_to_interp stubs we use R11_scratch1 as scratch register
 100   // and in call trampoline stubs we use R12_scratch2. This way we
 101   // can distinguish them (see is_NativeCallTrampolineStub_at()).
 102   Register reg_scratch = R11_scratch1;
 103 
 104   // Create a static stub relocation which relates this stub
 105   // with the call instruction at insts_call_instruction_offset in the
 106   // instructions code-section.
 107   __ relocate(static_stub_Relocation::spec(mark));
 108   const int stub_start_offset = __ offset();
 109 
 110   // Now, create the stub's code:
 111   // - load the TOC
 112   // - load the inline cache oop from the constant pool
 113   // - load the call target from the constant pool
 114   // - call
 115   __ calculate_address_from_global_toc(reg_scratch, __ method_toc());
 116   AddressLiteral ic = __ allocate_metadata_address((Metadata *)NULL);
 117   bool success = __ load_const_from_method_toc(as_Register(Matcher::inline_cache_reg_encode()),
 118                                                ic, reg_scratch, /*fixed_size*/ true);
 119   if (!success) {
 120     return NULL; // CodeCache is full
 121   }
 122 
 123   if (ReoptimizeCallSequences) {
 124     __ b64_patchable((address)-1, relocInfo::none);
 125   } else {
 126     AddressLiteral a((address)-1);
 127     success = __ load_const_from_method_toc(reg_scratch, a, reg_scratch, /*fixed_size*/ true);
 128     if (!success) {
 129       return NULL; // CodeCache is full
 130     }
 131     __ mtctr(reg_scratch);
 132     __ bctr();
 133   }
 134 
 135   // FIXME: Assert that the stub can be identified and patched.
 136 
 137   // Java_to_interp_stub_size should be good.
 138   assert((__ offset() - stub_start_offset) <= CompiledStaticCall::to_interp_stub_size(),
 139          "should be good size");
 140   assert(!is_NativeCallTrampolineStub_at(__ addr_at(stub_start_offset)),
 141          "must not confuse java_to_interp with trampoline stubs");
 142 
 143  // End the stub.
 144   __ end_a_stub();
 145   return stub;
 146 #else
 147   ShouldNotReachHere();
 148   return NULL;
 149 #endif
 150 }
 151 #undef __
 152 
 153 // Size of java_to_interp stub, this doesn't need to be accurate but it must
 154 // be larger or equal to the real size of the stub.
 155 // Used for optimization in Compile::Shorten_branches.
 156 int CompiledStaticCall::to_interp_stub_size() {
 157   return 12 * BytesPerInstWord;
 158 }
 159 
 160 // Relocation entries for call stub, compiled java to interpreter.
 161 // Used for optimization in Compile::Shorten_branches.
 162 int CompiledStaticCall::reloc_to_interp_stub() {
 163   return 5;
 164 }
 165 
 166 void CompiledDirectStaticCall::set_to_interpreted(methodHandle callee, address entry) {
 167   address stub = find_stub(/*is_aot*/ false);
 168   guarantee(stub != NULL, "stub not found");
 169 
 170   if (TraceICs) {
 171     ResourceMark rm;
 172     tty->print_cr("CompiledDirectStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
 173                   p2i(instruction_address()),
 174                   callee->name_and_sig_as_C_string());
 175   }
 176 
 177   // Creation also verifies the object.
 178   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub + IC_pos_in_java_to_interp_stub);
 179   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 180 
 181 #ifdef ASSERT
 182   // read the value once
 183   volatile intptr_t data = method_holder->data();
 184   volatile address destination = jump->jump_destination();
 185   assert(data == 0 || data == (intptr_t)callee(),
 186          "a) MT-unsafe modification of inline cache");
 187   assert(destination == (address)-1 || destination == entry,
 188          "b) MT-unsafe modification of inline cache");
 189 #endif
 190 
 191   // Update stub.
 192   method_holder->set_data((intptr_t)callee());
 193   jump->set_jump_destination(entry);
 194 
 195   // Update jump to call.
 196   set_destination_mt_safe(stub);
 197 }
 198 
 199 void CompiledDirectStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
 200   assert (CompiledIC_lock->is_locked() || SafepointSynchronize::is_at_safepoint(), "mt unsafe call");
 201   // Reset stub.
 202   address stub = static_stub->addr();
 203   assert(stub != NULL, "stub not found");
 204   // Creation also verifies the object.
 205   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub + IC_pos_in_java_to_interp_stub);
 206   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 207   method_holder->set_data(0);
 208   jump->set_jump_destination((address)-1);
 209 }
 210 
 211 //-----------------------------------------------------------------------------
 212 // Non-product mode code
 213 #ifndef PRODUCT
 214 
 215 void CompiledDirectStaticCall::verify() {
 216   // Verify call.
 217   _call->verify();
 218   if (os::is_MP()) {
 219     _call->verify_alignment();
 220   }
 221 
 222   // Verify stub.
 223   address stub = find_stub(/*is_aot*/ false);
 224   assert(stub != NULL, "no stub found for static call");
 225   // Creation also verifies the object.
 226   NativeMovConstReg* method_holder = nativeMovConstReg_at(stub + IC_pos_in_java_to_interp_stub);
 227   NativeJump*        jump          = nativeJump_at(method_holder->next_instruction_address());
 228 
 229   // Verify state.
 230   assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
 231 }
 232 
 233 #endif // !PRODUCT