1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Red Hat Inc. 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 #include "precompiled.hpp"
  26 #include "aot/compiledIC_aot.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "memory/universe.hpp"
  30 
  31 void CompiledDirectStaticCall::set_to_far(const methodHandle& callee, address entry) {
  32   if (TraceICs) {
  33     ResourceMark rm;
  34     tty->print_cr("CompiledDirectStaticCall@" INTPTR_FORMAT ": set_to_far %s",
  35                   p2i(instruction_address()),
  36                   callee->name_and_sig_as_C_string());
  37   }
  38 
  39   set_destination_mt_safe(entry);
  40 }
  41 
  42 void CompiledPltStaticCall::set_to_interpreted(const methodHandle& callee, address entry) {
  43   address stub = find_stub();
  44   guarantee(stub != NULL, "stub not found");
  45   if (TraceICs) {
  46     ResourceMark rm;
  47     tty->print_cr("CompiledPltStaticCall@" INTPTR_FORMAT ": set_to_interpreted %s",
  48                   p2i(instruction_address()),
  49                   callee->name_and_sig_as_C_string());
  50   }
  51 
  52   // Creation also verifies the object.
  53   NativeLoadGot* method_loader = nativeLoadGot_at(stub);
  54   NativeGotJump* jump          = nativeGotJump_at(method_loader->next_instruction_address());
  55 
  56   intptr_t data = method_loader->data();
  57   address destination = jump->destination();
  58   assert(data == 0 || data == (intptr_t)callee(),
  59          "a) MT-unsafe modification of inline cache");
  60   assert(destination == (address)Universe::non_oop_word()
  61          || destination == entry,
  62          "b) MT-unsafe modification of inline cache");
  63 
  64   // Update stub.
  65   method_loader->set_data((intptr_t)callee());
  66   jump->set_jump_destination(entry);
  67 
  68   // Update jump to call.
  69   set_destination_mt_safe(stub);
  70 }
  71 
  72 #ifdef NEVER_CALLED
  73 void CompiledPltStaticCall::set_stub_to_clean(static_stub_Relocation* static_stub) {
  74   // Reset stub.
  75   address stub = static_stub->addr();
  76   assert(stub != NULL, "stub not found");
  77   assert(CompiledICLocker::is_safe(stub), "mt unsafe call");
  78   // Creation also verifies the object.
  79   NativeLoadGot* method_loader = nativeLoadGot_at(stub);
  80   NativeGotJump* jump          = nativeGotJump_at(method_loader->next_instruction_address());
  81   method_loader->set_data(0);
  82   jump->set_jump_destination((address)-1);
  83 }
  84 #endif
  85 
  86 #ifndef PRODUCT
  87 void CompiledPltStaticCall::verify() {
  88   // Verify call.
  89   _call->verify();
  90 
  91 #ifdef ASSERT
  92   CodeBlob *cb = CodeCache::find_blob_unsafe((address) _call);
  93   assert(cb && cb->is_aot(), "CompiledPltStaticCall can only be used on AOTCompiledMethod");
  94 #endif
  95 
  96   // Verify stub.
  97   address stub = find_stub();
  98   assert(stub != NULL, "no stub found for static call");
  99   // Creation also verifies the object.
 100   NativeLoadGot*     method_loader = nativeLoadGot_at(stub);
 101   NativeGotJump*     jump          = nativeGotJump_at(method_loader->next_instruction_address());
 102   // Verify state.
 103   assert(is_clean() || is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
 104 }
 105 #endif // !PRODUCT