1 /*
   2  * Copyright (c) 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 "code/codeCache.hpp"
  27 #include "code/nmethod.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shared/barrierSetNMethod.hpp"
  30 #include "logging/log.hpp"
  31 #include "runtime/thread.hpp"
  32 #include "utilities/debug.hpp"
  33 
  34 int BarrierSetNMethod::disarmed_value() const {
  35   char* disarmed_addr = reinterpret_cast<char*>(Thread::current());
  36   disarmed_addr += in_bytes(thread_disarmed_offset());
  37   return *reinterpret_cast<int*>(disarmed_addr);
  38 }
  39 
  40 bool BarrierSetNMethod::supports_entry_barrier(nmethod* nm) {
  41   if (nm->method()->is_method_handle_intrinsic()) {
  42     return false;
  43   }
  44 
  45   if (!nm->is_native_method() && !nm->is_compiled_by_c2() && !nm->is_compiled_by_c1()) {
  46     return false;
  47   }
  48 
  49   return true;
  50 }
  51 
  52 int BarrierSetNMethod::nmethod_stub_entry_barrier(address* return_address_ptr) {
  53   address return_address = *return_address_ptr;
  54   CodeBlob* cb = CodeCache::find_blob(return_address);
  55   assert(cb != NULL, "invariant");
  56 
  57   nmethod* nm = cb->as_nmethod();
  58   BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
  59 
  60   if (!bs_nm->is_armed(nm)) {
  61     return 0;
  62   }
  63 
  64   assert(!nm->is_osr_method(), "Should not reach here");
  65   // Called upon first entry after being armed
  66   bool may_enter = bs_nm->nmethod_entry_barrier(nm);
  67   if (!may_enter) {
  68     log_trace(nmethod, barrier)("Deoptimizing nmethod: " PTR_FORMAT, p2i(nm));
  69     bs_nm->deoptimize(nm, return_address_ptr);
  70   }
  71   return may_enter ? 0 : 1;
  72 }
  73 
  74 bool BarrierSetNMethod::nmethod_osr_entry_barrier(nmethod* nm) {
  75   // This check depends on the invariant that all nmethods that are deoptimized / made not entrant
  76   // are NOT disarmed.
  77   // This invariant is important because a method can be deoptimized after the method have been
  78   // resolved / looked up by OSR by another thread. By not deoptimizing them we guarantee that
  79   // a deoptimized method will always hit the barrier and come to the same conclusion - deoptimize
  80   if (!is_armed(nm)) {
  81     return true;
  82   }
  83 
  84   assert(nm->is_osr_method(), "Should not reach here");
  85   log_trace(nmethod, barrier)("Running osr nmethod entry barrier: " PTR_FORMAT, p2i(nm));
  86   return nmethod_entry_barrier(nm);
  87 }