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