1 /*
   2  * Copyright (c) 1999, 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/codeBuffer.hpp"
  27 #include "c1/c1_CodeStubs.hpp"
  28 #include "c1/c1_Defs.hpp"
  29 #include "c1/c1_FrameMap.hpp"
  30 #include "c1/c1_LIRAssembler.hpp"
  31 #include "c1/c1_MacroAssembler.hpp"
  32 #include "c1/c1_Runtime1.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/vmSymbols.hpp"
  35 #include "code/codeBlob.hpp"
  36 #include "code/compiledIC.hpp"
  37 #include "code/pcDesc.hpp"
  38 #include "code/scopeDesc.hpp"
  39 #include "code/vtableStubs.hpp"
  40 #include "compiler/disassembler.hpp"
  41 #include "gc/shared/barrierSet.hpp"
  42 #include "gc/shared/collectedHeap.hpp"
  43 #include "interpreter/bytecode.hpp"
  44 #include "interpreter/interpreter.hpp"
  45 #include "logging/log.hpp"
  46 #include "memory/allocation.inline.hpp"
  47 #include "memory/oopFactory.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "oops/access.inline.hpp"
  50 #include "oops/objArrayOop.inline.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/oop.inline.hpp"
  53 #include "runtime/atomic.hpp"
  54 #include "runtime/biasedLocking.hpp"
  55 #include "runtime/compilationPolicy.hpp"
  56 #include "runtime/interfaceSupport.inline.hpp"
  57 #include "runtime/frame.inline.hpp"
  58 #include "runtime/javaCalls.hpp"
  59 #include "runtime/sharedRuntime.hpp"
  60 #include "runtime/threadCritical.hpp"
  61 #include "runtime/vframe.inline.hpp"
  62 #include "runtime/vframeArray.hpp"
  63 #include "runtime/vm_version.hpp"
  64 #include "utilities/copy.hpp"
  65 #include "utilities/events.hpp"
  66 
  67 
  68 // Implementation of StubAssembler
  69 
  70 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
  71   _name = name;
  72   _must_gc_arguments = false;
  73   _frame_size = no_frame_size;
  74   _num_rt_args = 0;
  75   _stub_id = stub_id;
  76 }
  77 
  78 
  79 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
  80   _name = name;
  81   _must_gc_arguments = must_gc_arguments;
  82 }
  83 
  84 
  85 void StubAssembler::set_frame_size(int size) {
  86   if (_frame_size == no_frame_size) {
  87     _frame_size = size;
  88   }
  89   assert(_frame_size == size, "can't change the frame size");
  90 }
  91 
  92 
  93 void StubAssembler::set_num_rt_args(int args) {
  94   if (_num_rt_args == 0) {
  95     _num_rt_args = args;
  96   }
  97   assert(_num_rt_args == args, "can't change the number of args");
  98 }
  99 
 100 // Implementation of Runtime1
 101 
 102 CodeBlob* Runtime1::_blobs[Runtime1::number_of_ids];
 103 const char *Runtime1::_blob_names[] = {
 104   RUNTIME1_STUBS(STUB_NAME, LAST_STUB_NAME)
 105 };
 106 
 107 #ifndef PRODUCT
 108 // statistics
 109 int Runtime1::_generic_arraycopy_cnt = 0;
 110 int Runtime1::_generic_arraycopystub_cnt = 0;
 111 int Runtime1::_arraycopy_slowcase_cnt = 0;
 112 int Runtime1::_arraycopy_checkcast_cnt = 0;
 113 int Runtime1::_arraycopy_checkcast_attempt_cnt = 0;
 114 int Runtime1::_new_type_array_slowcase_cnt = 0;
 115 int Runtime1::_new_object_array_slowcase_cnt = 0;
 116 int Runtime1::_new_instance_slowcase_cnt = 0;
 117 int Runtime1::_new_multi_array_slowcase_cnt = 0;
 118 int Runtime1::_monitorenter_slowcase_cnt = 0;
 119 int Runtime1::_monitorexit_slowcase_cnt = 0;
 120 int Runtime1::_patch_code_slowcase_cnt = 0;
 121 int Runtime1::_throw_range_check_exception_count = 0;
 122 int Runtime1::_throw_index_exception_count = 0;
 123 int Runtime1::_throw_div0_exception_count = 0;
 124 int Runtime1::_throw_null_pointer_exception_count = 0;
 125 int Runtime1::_throw_class_cast_exception_count = 0;
 126 int Runtime1::_throw_incompatible_class_change_error_count = 0;
 127 int Runtime1::_throw_array_store_exception_count = 0;
 128 int Runtime1::_throw_count = 0;
 129 
 130 static int _byte_arraycopy_stub_cnt = 0;
 131 static int _short_arraycopy_stub_cnt = 0;
 132 static int _int_arraycopy_stub_cnt = 0;
 133 static int _long_arraycopy_stub_cnt = 0;
 134 static int _oop_arraycopy_stub_cnt = 0;
 135 
 136 address Runtime1::arraycopy_count_address(BasicType type) {
 137   switch (type) {
 138   case T_BOOLEAN:
 139   case T_BYTE:   return (address)&_byte_arraycopy_stub_cnt;
 140   case T_CHAR:
 141   case T_SHORT:  return (address)&_short_arraycopy_stub_cnt;
 142   case T_FLOAT:
 143   case T_INT:    return (address)&_int_arraycopy_stub_cnt;
 144   case T_DOUBLE:
 145   case T_LONG:   return (address)&_long_arraycopy_stub_cnt;
 146   case T_ARRAY:
 147   case T_OBJECT: return (address)&_oop_arraycopy_stub_cnt;
 148   default:
 149     ShouldNotReachHere();
 150     return NULL;
 151   }
 152 }
 153 
 154 
 155 #endif
 156 
 157 // Simple helper to see if the caller of a runtime stub which
 158 // entered the VM has been deoptimized
 159 
 160 static bool caller_is_deopted() {
 161   JavaThread* thread = JavaThread::current();
 162   RegisterMap reg_map(thread, false);
 163   frame runtime_frame = thread->last_frame();
 164   frame caller_frame = runtime_frame.sender(&reg_map);
 165   assert(caller_frame.is_compiled_frame(), "must be compiled");
 166   return caller_frame.is_deoptimized_frame();
 167 }
 168 
 169 // Stress deoptimization
 170 static void deopt_caller() {
 171   if ( !caller_is_deopted()) {
 172     JavaThread* thread = JavaThread::current();
 173     RegisterMap reg_map(thread, false);
 174     frame runtime_frame = thread->last_frame();
 175     frame caller_frame = runtime_frame.sender(&reg_map);
 176     Deoptimization::deoptimize_frame(thread, caller_frame.id());
 177     assert(caller_is_deopted(), "Must be deoptimized");
 178   }
 179 }
 180 
 181 
 182 void Runtime1::generate_blob_for(BufferBlob* buffer_blob, StubID id) {
 183   assert(0 <= id && id < number_of_ids, "illegal stub id");
 184   ResourceMark rm;
 185   // create code buffer for code storage
 186   CodeBuffer code(buffer_blob);
 187 
 188   OopMapSet* oop_maps;
 189   int frame_size;
 190   bool must_gc_arguments;
 191 
 192   Compilation::setup_code_buffer(&code, 0);
 193 
 194   // create assembler for code generation
 195   StubAssembler* sasm = new StubAssembler(&code, name_for(id), id);
 196   // generate code for runtime stub
 197   oop_maps = generate_code_for(id, sasm);
 198   assert(oop_maps == NULL || sasm->frame_size() != no_frame_size,
 199          "if stub has an oop map it must have a valid frame size");
 200 
 201 #ifdef ASSERT
 202   // Make sure that stubs that need oopmaps have them
 203   switch (id) {
 204     // These stubs don't need to have an oopmap
 205   case dtrace_object_alloc_id:
 206   case g1_pre_barrier_slow_id:
 207   case g1_post_barrier_slow_id:
 208   case slow_subtype_check_id:
 209   case fpu2long_stub_id:
 210   case unwind_exception_id:
 211   case counter_overflow_id:
 212 #if defined(SPARC) || defined(PPC32)
 213   case handle_exception_nofpu_id:  // Unused on sparc
 214 #endif
 215     break;
 216 
 217     // All other stubs should have oopmaps
 218   default:
 219     assert(oop_maps != NULL, "must have an oopmap");
 220   }
 221 #endif
 222 
 223   // align so printing shows nop's instead of random code at the end (SimpleStubs are aligned)
 224   sasm->align(BytesPerWord);
 225   // make sure all code is in code buffer
 226   sasm->flush();
 227 
 228   frame_size = sasm->frame_size();
 229   must_gc_arguments = sasm->must_gc_arguments();
 230   // create blob - distinguish a few special cases
 231   CodeBlob* blob = RuntimeStub::new_runtime_stub(name_for(id),
 232                                                  &code,
 233                                                  CodeOffsets::frame_never_safe,
 234                                                  frame_size,
 235                                                  oop_maps,
 236                                                  must_gc_arguments);
 237   // install blob
 238   assert(blob != NULL, "blob must exist");
 239   _blobs[id] = blob;
 240 }
 241 
 242 
 243 void Runtime1::initialize(BufferBlob* blob) {
 244   // platform-dependent initialization
 245   initialize_pd();
 246   // generate stubs
 247   for (int id = 0; id < number_of_ids; id++) generate_blob_for(blob, (StubID)id);
 248   // printing
 249 #ifndef PRODUCT
 250   if (PrintSimpleStubs) {
 251     ResourceMark rm;
 252     for (int id = 0; id < number_of_ids; id++) {
 253       _blobs[id]->print();
 254       if (_blobs[id]->oop_maps() != NULL) {
 255         _blobs[id]->oop_maps()->print();
 256       }
 257     }
 258   }
 259 #endif
 260 }
 261 
 262 
 263 CodeBlob* Runtime1::blob_for(StubID id) {
 264   assert(0 <= id && id < number_of_ids, "illegal stub id");
 265   return _blobs[id];
 266 }
 267 
 268 
 269 const char* Runtime1::name_for(StubID id) {
 270   assert(0 <= id && id < number_of_ids, "illegal stub id");
 271   return _blob_names[id];
 272 }
 273 
 274 const char* Runtime1::name_for_address(address entry) {
 275   for (int id = 0; id < number_of_ids; id++) {
 276     if (entry == entry_for((StubID)id)) return name_for((StubID)id);
 277   }
 278 
 279 #define FUNCTION_CASE(a, f) \
 280   if ((intptr_t)a == CAST_FROM_FN_PTR(intptr_t, f))  return #f
 281 
 282   FUNCTION_CASE(entry, os::javaTimeMillis);
 283   FUNCTION_CASE(entry, os::javaTimeNanos);
 284   FUNCTION_CASE(entry, SharedRuntime::OSR_migration_end);
 285   FUNCTION_CASE(entry, SharedRuntime::d2f);
 286   FUNCTION_CASE(entry, SharedRuntime::d2i);
 287   FUNCTION_CASE(entry, SharedRuntime::d2l);
 288   FUNCTION_CASE(entry, SharedRuntime::dcos);
 289   FUNCTION_CASE(entry, SharedRuntime::dexp);
 290   FUNCTION_CASE(entry, SharedRuntime::dlog);
 291   FUNCTION_CASE(entry, SharedRuntime::dlog10);
 292   FUNCTION_CASE(entry, SharedRuntime::dpow);
 293   FUNCTION_CASE(entry, SharedRuntime::drem);
 294   FUNCTION_CASE(entry, SharedRuntime::dsin);
 295   FUNCTION_CASE(entry, SharedRuntime::dtan);
 296   FUNCTION_CASE(entry, SharedRuntime::f2i);
 297   FUNCTION_CASE(entry, SharedRuntime::f2l);
 298   FUNCTION_CASE(entry, SharedRuntime::frem);
 299   FUNCTION_CASE(entry, SharedRuntime::l2d);
 300   FUNCTION_CASE(entry, SharedRuntime::l2f);
 301   FUNCTION_CASE(entry, SharedRuntime::ldiv);
 302   FUNCTION_CASE(entry, SharedRuntime::lmul);
 303   FUNCTION_CASE(entry, SharedRuntime::lrem);
 304   FUNCTION_CASE(entry, SharedRuntime::lrem);
 305   FUNCTION_CASE(entry, SharedRuntime::dtrace_method_entry);
 306   FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit);
 307   FUNCTION_CASE(entry, is_instance_of);
 308   FUNCTION_CASE(entry, trace_block_entry);
 309 #ifdef TRACE_HAVE_INTRINSICS
 310   FUNCTION_CASE(entry, TRACE_TIME_METHOD);
 311 #endif
 312   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32());
 313   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32C());
 314   FUNCTION_CASE(entry, StubRoutines::vectorizedMismatch());
 315   FUNCTION_CASE(entry, StubRoutines::dexp());
 316   FUNCTION_CASE(entry, StubRoutines::dlog());
 317   FUNCTION_CASE(entry, StubRoutines::dlog10());
 318   FUNCTION_CASE(entry, StubRoutines::dpow());
 319   FUNCTION_CASE(entry, StubRoutines::dsin());
 320   FUNCTION_CASE(entry, StubRoutines::dcos());
 321   FUNCTION_CASE(entry, StubRoutines::dtan());
 322 
 323 #undef FUNCTION_CASE
 324 
 325   // Soft float adds more runtime names.
 326   return pd_name_for_address(entry);
 327 }
 328 
 329 
 330 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* thread, Klass* klass))
 331   NOT_PRODUCT(_new_instance_slowcase_cnt++;)
 332 
 333   assert(klass->is_klass(), "not a class");
 334   Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
 335   InstanceKlass* h = InstanceKlass::cast(klass);
 336   h->check_valid_for_instantiation(true, CHECK);
 337   // make sure klass is initialized
 338   h->initialize(CHECK);
 339   // allocate instance and return via TLS
 340   oop obj = h->allocate_instance(CHECK);
 341   thread->set_vm_result(obj);
 342 JRT_END
 343 
 344 
 345 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* thread, Klass* klass, jint length))
 346   NOT_PRODUCT(_new_type_array_slowcase_cnt++;)
 347   // Note: no handle for klass needed since they are not used
 348   //       anymore after new_typeArray() and no GC can happen before.
 349   //       (This may have to change if this code changes!)
 350   assert(klass->is_klass(), "not a class");
 351   BasicType elt_type = TypeArrayKlass::cast(klass)->element_type();
 352   oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
 353   thread->set_vm_result(obj);
 354   // This is pretty rare but this runtime patch is stressful to deoptimization
 355   // if we deoptimize here so force a deopt to stress the path.
 356   if (DeoptimizeALot) {
 357     deopt_caller();
 358   }
 359 
 360 JRT_END
 361 
 362 
 363 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* thread, Klass* array_klass, jint length))
 364   NOT_PRODUCT(_new_object_array_slowcase_cnt++;)
 365 
 366   // Note: no handle for klass needed since they are not used
 367   //       anymore after new_objArray() and no GC can happen before.
 368   //       (This may have to change if this code changes!)
 369   assert(array_klass->is_klass(), "not a class");
 370   Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive
 371   Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
 372   objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
 373   thread->set_vm_result(obj);
 374   // This is pretty rare but this runtime patch is stressful to deoptimization
 375   // if we deoptimize here so force a deopt to stress the path.
 376   if (DeoptimizeALot) {
 377     deopt_caller();
 378   }
 379 JRT_END
 380 
 381 
 382 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims))
 383   NOT_PRODUCT(_new_multi_array_slowcase_cnt++;)
 384 
 385   assert(klass->is_klass(), "not a class");
 386   assert(rank >= 1, "rank must be nonzero");
 387   Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
 388   oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
 389   thread->set_vm_result(obj);
 390 JRT_END
 391 
 392 
 393 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* thread, StubID id))
 394   tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", id);
 395 JRT_END
 396 
 397 
 398 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* thread, oopDesc* obj))
 399   ResourceMark rm(thread);
 400   const char* klass_name = obj->klass()->external_name();
 401   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayStoreException(), klass_name);
 402 JRT_END
 403 
 404 
 405 // counter_overflow() is called from within C1-compiled methods. The enclosing method is the method
 406 // associated with the top activation record. The inlinee (that is possibly included in the enclosing
 407 // method) method oop is passed as an argument. In order to do that it is embedded in the code as
 408 // a constant.
 409 static nmethod* counter_overflow_helper(JavaThread* THREAD, int branch_bci, Method* m) {
 410   nmethod* osr_nm = NULL;
 411   methodHandle method(THREAD, m);
 412 
 413   RegisterMap map(THREAD, false);
 414   frame fr =  THREAD->last_frame().sender(&map);
 415   nmethod* nm = (nmethod*) fr.cb();
 416   assert(nm!= NULL && nm->is_nmethod(), "Sanity check");
 417   methodHandle enclosing_method(THREAD, nm->method());
 418 
 419   CompLevel level = (CompLevel)nm->comp_level();
 420   int bci = InvocationEntryBci;
 421   if (branch_bci != InvocationEntryBci) {
 422     // Compute destination bci
 423     address pc = method()->code_base() + branch_bci;
 424     Bytecodes::Code branch = Bytecodes::code_at(method(), pc);
 425     int offset = 0;
 426     switch (branch) {
 427       case Bytecodes::_if_icmplt: case Bytecodes::_iflt:
 428       case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt:
 429       case Bytecodes::_if_icmple: case Bytecodes::_ifle:
 430       case Bytecodes::_if_icmpge: case Bytecodes::_ifge:
 431       case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq:
 432       case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne:
 433       case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto:
 434         offset = (int16_t)Bytes::get_Java_u2(pc + 1);
 435         break;
 436       case Bytecodes::_goto_w:
 437         offset = Bytes::get_Java_u4(pc + 1);
 438         break;
 439       default: ;
 440     }
 441     bci = branch_bci + offset;
 442   }
 443   assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending");
 444   osr_nm = CompilationPolicy::policy()->event(enclosing_method, method, branch_bci, bci, level, nm, THREAD);
 445   assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions");
 446   return osr_nm;
 447 }
 448 
 449 JRT_BLOCK_ENTRY(address, Runtime1::counter_overflow(JavaThread* thread, int bci, Method* method))
 450   nmethod* osr_nm;
 451   JRT_BLOCK
 452     osr_nm = counter_overflow_helper(thread, bci, method);
 453     if (osr_nm != NULL) {
 454       RegisterMap map(thread, false);
 455       frame fr =  thread->last_frame().sender(&map);
 456       Deoptimization::deoptimize_frame(thread, fr.id());
 457     }
 458   JRT_BLOCK_END
 459   return NULL;
 460 JRT_END
 461 
 462 extern void vm_exit(int code);
 463 
 464 // Enter this method from compiled code handler below. This is where we transition
 465 // to VM mode. This is done as a helper routine so that the method called directly
 466 // from compiled code does not have to transition to VM. This allows the entry
 467 // method to see if the nmethod that we have just looked up a handler for has
 468 // been deoptimized while we were in the vm. This simplifies the assembly code
 469 // cpu directories.
 470 //
 471 // We are entering here from exception stub (via the entry method below)
 472 // If there is a compiled exception handler in this method, we will continue there;
 473 // otherwise we will unwind the stack and continue at the caller of top frame method
 474 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
 475 // control the area where we can allow a safepoint. After we exit the safepoint area we can
 476 // check to see if the handler we are going to return is now in a nmethod that has
 477 // been deoptimized. If that is the case we return the deopt blob
 478 // unpack_with_exception entry instead. This makes life for the exception blob easier
 479 // because making that same check and diverting is painful from assembly language.
 480 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, nmethod*& nm))
 481   // Reset method handle flag.
 482   thread->set_is_method_handle_return(false);
 483 
 484   Handle exception(thread, ex);
 485   nm = CodeCache::find_nmethod(pc);
 486   assert(nm != NULL, "this is not an nmethod");
 487   // Adjust the pc as needed/
 488   if (nm->is_deopt_pc(pc)) {
 489     RegisterMap map(thread, false);
 490     frame exception_frame = thread->last_frame().sender(&map);
 491     // if the frame isn't deopted then pc must not correspond to the caller of last_frame
 492     assert(exception_frame.is_deoptimized_frame(), "must be deopted");
 493     pc = exception_frame.pc();
 494   }
 495 #ifdef ASSERT
 496   assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
 497   // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
 498   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
 499     if (ExitVMOnVerifyError) vm_exit(-1);
 500     ShouldNotReachHere();
 501   }
 502 #endif
 503 
 504   // Check the stack guard pages and reenable them if necessary and there is
 505   // enough space on the stack to do so.  Use fast exceptions only if the guard
 506   // pages are enabled.
 507   bool guard_pages_enabled = thread->stack_guards_enabled();
 508   if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack();
 509 
 510   if (JvmtiExport::can_post_on_exceptions()) {
 511     // To ensure correct notification of exception catches and throws
 512     // we have to deoptimize here.  If we attempted to notify the
 513     // catches and throws during this exception lookup it's possible
 514     // we could deoptimize on the way out of the VM and end back in
 515     // the interpreter at the throw site.  This would result in double
 516     // notifications since the interpreter would also notify about
 517     // these same catches and throws as it unwound the frame.
 518 
 519     RegisterMap reg_map(thread);
 520     frame stub_frame = thread->last_frame();
 521     frame caller_frame = stub_frame.sender(&reg_map);
 522 
 523     // We don't really want to deoptimize the nmethod itself since we
 524     // can actually continue in the exception handler ourselves but I
 525     // don't see an easy way to have the desired effect.
 526     Deoptimization::deoptimize_frame(thread, caller_frame.id());
 527     assert(caller_is_deopted(), "Must be deoptimized");
 528 
 529     return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 530   }
 531 
 532   // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions
 533   if (guard_pages_enabled) {
 534     address fast_continuation = nm->handler_for_exception_and_pc(exception, pc);
 535     if (fast_continuation != NULL) {
 536       // Set flag if return address is a method handle call site.
 537       thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
 538       return fast_continuation;
 539     }
 540   }
 541 
 542   // If the stack guard pages are enabled, check whether there is a handler in
 543   // the current method.  Otherwise (guard pages disabled), force an unwind and
 544   // skip the exception cache update (i.e., just leave continuation==NULL).
 545   address continuation = NULL;
 546   if (guard_pages_enabled) {
 547 
 548     // New exception handling mechanism can support inlined methods
 549     // with exception handlers since the mappings are from PC to PC
 550 
 551     // debugging support
 552     // tracing
 553     if (log_is_enabled(Info, exceptions)) {
 554       ResourceMark rm;
 555       stringStream tempst;
 556       tempst.print("compiled method <%s>\n"
 557                    " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT,
 558                    nm->method()->print_value_string(), p2i(pc), p2i(thread));
 559       Exceptions::log_exception(exception, tempst);
 560     }
 561     // for AbortVMOnException flag
 562     Exceptions::debug_check_abort(exception);
 563 
 564     // Clear out the exception oop and pc since looking up an
 565     // exception handler can cause class loading, which might throw an
 566     // exception and those fields are expected to be clear during
 567     // normal bytecode execution.
 568     thread->clear_exception_oop_and_pc();
 569 
 570     bool recursive_exception = false;
 571     continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false, recursive_exception);
 572     // If an exception was thrown during exception dispatch, the exception oop may have changed
 573     thread->set_exception_oop(exception());
 574     thread->set_exception_pc(pc);
 575 
 576     // the exception cache is used only by non-implicit exceptions
 577     // Update the exception cache only when there didn't happen
 578     // another exception during the computation of the compiled
 579     // exception handler. Checking for exception oop equality is not
 580     // sufficient because some exceptions are pre-allocated and reused.
 581     if (continuation != NULL && !recursive_exception) {
 582       nm->add_handler_for_exception_and_pc(exception, pc, continuation);
 583     }
 584   }
 585 
 586   thread->set_vm_result(exception());
 587   // Set flag if return address is a method handle call site.
 588   thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
 589 
 590   if (log_is_enabled(Info, exceptions)) {
 591     ResourceMark rm;
 592     log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT
 593                          " for exception thrown at PC " PTR_FORMAT,
 594                          p2i(thread), p2i(continuation), p2i(pc));
 595   }
 596 
 597   return continuation;
 598 JRT_END
 599 
 600 // Enter this method from compiled code only if there is a Java exception handler
 601 // in the method handling the exception.
 602 // We are entering here from exception stub. We don't do a normal VM transition here.
 603 // We do it in a helper. This is so we can check to see if the nmethod we have just
 604 // searched for an exception handler has been deoptimized in the meantime.
 605 address Runtime1::exception_handler_for_pc(JavaThread* thread) {
 606   oop exception = thread->exception_oop();
 607   address pc = thread->exception_pc();
 608   // Still in Java mode
 609   DEBUG_ONLY(ResetNoHandleMark rnhm);
 610   nmethod* nm = NULL;
 611   address continuation = NULL;
 612   {
 613     // Enter VM mode by calling the helper
 614     ResetNoHandleMark rnhm;
 615     continuation = exception_handler_for_pc_helper(thread, exception, pc, nm);
 616   }
 617   // Back in JAVA, use no oops DON'T safepoint
 618 
 619   // Now check to see if the nmethod we were called from is now deoptimized.
 620   // If so we must return to the deopt blob and deoptimize the nmethod
 621   if (nm != NULL && caller_is_deopted()) {
 622     continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 623   }
 624 
 625   assert(continuation != NULL, "no handler found");
 626   return continuation;
 627 }
 628 
 629 
 630 JRT_ENTRY(void, Runtime1::throw_range_check_exception(JavaThread* thread, int index))
 631   NOT_PRODUCT(_throw_range_check_exception_count++;)
 632   char message[jintAsStringSize];
 633   sprintf(message, "%d", index);
 634   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), message);
 635 JRT_END
 636 
 637 
 638 JRT_ENTRY(void, Runtime1::throw_index_exception(JavaThread* thread, int index))
 639   NOT_PRODUCT(_throw_index_exception_count++;)
 640   char message[16];
 641   sprintf(message, "%d", index);
 642   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IndexOutOfBoundsException(), message);
 643 JRT_END
 644 
 645 
 646 JRT_ENTRY(void, Runtime1::throw_div0_exception(JavaThread* thread))
 647   NOT_PRODUCT(_throw_div0_exception_count++;)
 648   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArithmeticException(), "/ by zero");
 649 JRT_END
 650 
 651 
 652 JRT_ENTRY(void, Runtime1::throw_null_pointer_exception(JavaThread* thread))
 653   NOT_PRODUCT(_throw_null_pointer_exception_count++;)
 654   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
 655 JRT_END
 656 
 657 
 658 JRT_ENTRY(void, Runtime1::throw_class_cast_exception(JavaThread* thread, oopDesc* object))
 659   NOT_PRODUCT(_throw_class_cast_exception_count++;)
 660   ResourceMark rm(thread);
 661   char* message = SharedRuntime::generate_class_cast_message(
 662     thread, object->klass());
 663   SharedRuntime::throw_and_post_jvmti_exception(
 664     thread, vmSymbols::java_lang_ClassCastException(), message);
 665 JRT_END
 666 
 667 
 668 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* thread))
 669   NOT_PRODUCT(_throw_incompatible_class_change_error_count++;)
 670   ResourceMark rm(thread);
 671   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IncompatibleClassChangeError());
 672 JRT_END
 673 
 674 
 675 JRT_ENTRY_NO_ASYNC(void, Runtime1::monitorenter(JavaThread* thread, oopDesc* obj, BasicObjectLock* lock))
 676   NOT_PRODUCT(_monitorenter_slowcase_cnt++;)
 677   if (PrintBiasedLockingStatistics) {
 678     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
 679   }
 680   Handle h_obj(thread, obj);
 681   if (UseBiasedLocking) {
 682     // Retry fast entry if bias is revoked to avoid unnecessary inflation
 683     ObjectSynchronizer::fast_enter(h_obj, lock->lock(), true, CHECK);
 684   } else {
 685     if (UseFastLocking) {
 686       // When using fast locking, the compiled code has already tried the fast case
 687       assert(obj == lock->obj(), "must match");
 688       ObjectSynchronizer::slow_enter(h_obj, lock->lock(), THREAD);
 689     } else {
 690       lock->set_obj(obj);
 691       ObjectSynchronizer::fast_enter(h_obj, lock->lock(), false, THREAD);
 692     }
 693   }
 694 JRT_END
 695 
 696 
 697 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* thread, BasicObjectLock* lock))
 698   NOT_PRODUCT(_monitorexit_slowcase_cnt++;)
 699   assert(thread == JavaThread::current(), "threads must correspond");
 700   assert(thread->last_Java_sp(), "last_Java_sp must be set");
 701   // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown
 702   EXCEPTION_MARK;
 703 
 704   oop obj = lock->obj();
 705   assert(oopDesc::is_oop(obj), "must be NULL or an object");
 706   if (UseFastLocking) {
 707     // When using fast locking, the compiled code has already tried the fast case
 708     ObjectSynchronizer::slow_exit(obj, lock->lock(), THREAD);
 709   } else {
 710     ObjectSynchronizer::fast_exit(obj, lock->lock(), THREAD);
 711   }
 712 JRT_END
 713 
 714 // Cf. OptoRuntime::deoptimize_caller_frame
 715 JRT_ENTRY(void, Runtime1::deoptimize(JavaThread* thread, jint trap_request))
 716   // Called from within the owner thread, so no need for safepoint
 717   RegisterMap reg_map(thread, false);
 718   frame stub_frame = thread->last_frame();
 719   assert(stub_frame.is_runtime_frame(), "Sanity check");
 720   frame caller_frame = stub_frame.sender(&reg_map);
 721   nmethod* nm = caller_frame.cb()->as_nmethod_or_null();
 722   assert(nm != NULL, "Sanity check");
 723   methodHandle method(thread, nm->method());
 724   assert(nm == CodeCache::find_nmethod(caller_frame.pc()), "Should be the same");
 725   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
 726   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
 727 
 728   if (action == Deoptimization::Action_make_not_entrant) {
 729     if (nm->make_not_entrant()) {
 730       if (reason == Deoptimization::Reason_tenured) {
 731         MethodData* trap_mdo = Deoptimization::get_method_data(thread, method, true /*create_if_missing*/);
 732         if (trap_mdo != NULL) {
 733           trap_mdo->inc_tenure_traps();
 734         }
 735       }
 736     }
 737   }
 738 
 739   // Deoptimize the caller frame.
 740   Deoptimization::deoptimize_frame(thread, caller_frame.id());
 741   // Return to the now deoptimized frame.
 742 JRT_END
 743 
 744 
 745 #ifndef DEOPTIMIZE_WHEN_PATCHING
 746 
 747 static Klass* resolve_field_return_klass(const methodHandle& caller, int bci, TRAPS) {
 748   Bytecode_field field_access(caller, bci);
 749   // This can be static or non-static field access
 750   Bytecodes::Code code       = field_access.code();
 751 
 752   // We must load class, initialize class and resolve the field
 753   fieldDescriptor result; // initialize class if needed
 754   constantPoolHandle constants(THREAD, caller->constants());
 755   LinkResolver::resolve_field_access(result, constants, field_access.index(), caller, Bytecodes::java_code(code), CHECK_NULL);
 756   return result.field_holder();
 757 }
 758 
 759 
 760 //
 761 // This routine patches sites where a class wasn't loaded or
 762 // initialized at the time the code was generated.  It handles
 763 // references to classes, fields and forcing of initialization.  Most
 764 // of the cases are straightforward and involving simply forcing
 765 // resolution of a class, rewriting the instruction stream with the
 766 // needed constant and replacing the call in this function with the
 767 // patched code.  The case for static field is more complicated since
 768 // the thread which is in the process of initializing a class can
 769 // access it's static fields but other threads can't so the code
 770 // either has to deoptimize when this case is detected or execute a
 771 // check that the current thread is the initializing thread.  The
 772 // current
 773 //
 774 // Patches basically look like this:
 775 //
 776 //
 777 // patch_site: jmp patch stub     ;; will be patched
 778 // continue:   ...
 779 //             ...
 780 //             ...
 781 //             ...
 782 //
 783 // They have a stub which looks like this:
 784 //
 785 //             ;; patch body
 786 //             movl <const>, reg           (for class constants)
 787 //        <or> movl [reg1 + <const>], reg  (for field offsets)
 788 //        <or> movl reg, [reg1 + <const>]  (for field offsets)
 789 //             <being_init offset> <bytes to copy> <bytes to skip>
 790 // patch_stub: call Runtime1::patch_code (through a runtime stub)
 791 //             jmp patch_site
 792 //
 793 //
 794 // A normal patch is done by rewriting the patch body, usually a move,
 795 // and then copying it into place over top of the jmp instruction
 796 // being careful to flush caches and doing it in an MP-safe way.  The
 797 // constants following the patch body are used to find various pieces
 798 // of the patch relative to the call site for Runtime1::patch_code.
 799 // The case for getstatic and putstatic is more complicated because
 800 // getstatic and putstatic have special semantics when executing while
 801 // the class is being initialized.  getstatic/putstatic on a class
 802 // which is being_initialized may be executed by the initializing
 803 // thread but other threads have to block when they execute it.  This
 804 // is accomplished in compiled code by executing a test of the current
 805 // thread against the initializing thread of the class.  It's emitted
 806 // as boilerplate in their stub which allows the patched code to be
 807 // executed before it's copied back into the main body of the nmethod.
 808 //
 809 // being_init: get_thread(<tmp reg>
 810 //             cmpl [reg1 + <init_thread_offset>], <tmp reg>
 811 //             jne patch_stub
 812 //             movl [reg1 + <const>], reg  (for field offsets)  <or>
 813 //             movl reg, [reg1 + <const>]  (for field offsets)
 814 //             jmp continue
 815 //             <being_init offset> <bytes to copy> <bytes to skip>
 816 // patch_stub: jmp Runtim1::patch_code (through a runtime stub)
 817 //             jmp patch_site
 818 //
 819 // If the class is being initialized the patch body is rewritten and
 820 // the patch site is rewritten to jump to being_init, instead of
 821 // patch_stub.  Whenever this code is executed it checks the current
 822 // thread against the intializing thread so other threads will enter
 823 // the runtime and end up blocked waiting the class to finish
 824 // initializing inside the calls to resolve_field below.  The
 825 // initializing class will continue on it's way.  Once the class is
 826 // fully_initialized, the intializing_thread of the class becomes
 827 // NULL, so the next thread to execute this code will fail the test,
 828 // call into patch_code and complete the patching process by copying
 829 // the patch body back into the main part of the nmethod and resume
 830 // executing.
 831 //
 832 //
 833 
 834 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id ))
 835   NOT_PRODUCT(_patch_code_slowcase_cnt++;)
 836 
 837   ResourceMark rm(thread);
 838   RegisterMap reg_map(thread, false);
 839   frame runtime_frame = thread->last_frame();
 840   frame caller_frame = runtime_frame.sender(&reg_map);
 841 
 842   // last java frame on stack
 843   vframeStream vfst(thread, true);
 844   assert(!vfst.at_end(), "Java frame must exist");
 845 
 846   methodHandle caller_method(THREAD, vfst.method());
 847   // Note that caller_method->code() may not be same as caller_code because of OSR's
 848   // Note also that in the presence of inlining it is not guaranteed
 849   // that caller_method() == caller_code->method()
 850 
 851   int bci = vfst.bci();
 852   Bytecodes::Code code = caller_method()->java_code_at(bci);
 853 
 854   // this is used by assertions in the access_field_patching_id
 855   BasicType patch_field_type = T_ILLEGAL;
 856   bool deoptimize_for_volatile = false;
 857   bool deoptimize_for_atomic = false;
 858   int patch_field_offset = -1;
 859   Klass* init_klass = NULL; // klass needed by load_klass_patching code
 860   Klass* load_klass = NULL; // klass needed by load_klass_patching code
 861   Handle mirror(THREAD, NULL);                    // oop needed by load_mirror_patching code
 862   Handle appendix(THREAD, NULL);                  // oop needed by appendix_patching code
 863   bool load_klass_or_mirror_patch_id =
 864     (stub_id == Runtime1::load_klass_patching_id || stub_id == Runtime1::load_mirror_patching_id);
 865 
 866   if (stub_id == Runtime1::access_field_patching_id) {
 867 
 868     Bytecode_field field_access(caller_method, bci);
 869     fieldDescriptor result; // initialize class if needed
 870     Bytecodes::Code code = field_access.code();
 871     constantPoolHandle constants(THREAD, caller_method->constants());
 872     LinkResolver::resolve_field_access(result, constants, field_access.index(), caller_method, Bytecodes::java_code(code), CHECK);
 873     patch_field_offset = result.offset();
 874 
 875     // If we're patching a field which is volatile then at compile it
 876     // must not have been know to be volatile, so the generated code
 877     // isn't correct for a volatile reference.  The nmethod has to be
 878     // deoptimized so that the code can be regenerated correctly.
 879     // This check is only needed for access_field_patching since this
 880     // is the path for patching field offsets.  load_klass is only
 881     // used for patching references to oops which don't need special
 882     // handling in the volatile case.
 883 
 884     deoptimize_for_volatile = result.access_flags().is_volatile();
 885 
 886     // If we are patching a field which should be atomic, then
 887     // the generated code is not correct either, force deoptimizing.
 888     // We need to only cover T_LONG and T_DOUBLE fields, as we can
 889     // break access atomicity only for them.
 890 
 891     // Strictly speaking, the deoptimizaation on 64-bit platforms
 892     // is unnecessary, and T_LONG stores on 32-bit platforms need
 893     // to be handled by special patching code when AlwaysAtomicAccesses
 894     // becomes product feature. At this point, we are still going
 895     // for the deoptimization for consistency against volatile
 896     // accesses.
 897 
 898     patch_field_type = result.field_type();
 899     deoptimize_for_atomic = (AlwaysAtomicAccesses && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG));
 900 
 901   } else if (load_klass_or_mirror_patch_id) {
 902     Klass* k = NULL;
 903     switch (code) {
 904       case Bytecodes::_putstatic:
 905       case Bytecodes::_getstatic:
 906         { Klass* klass = resolve_field_return_klass(caller_method, bci, CHECK);
 907           init_klass = klass;
 908           mirror = Handle(THREAD, klass->java_mirror());
 909         }
 910         break;
 911       case Bytecodes::_new:
 912         { Bytecode_new bnew(caller_method(), caller_method->bcp_from(bci));
 913           k = caller_method->constants()->klass_at(bnew.index(), CHECK);
 914         }
 915         break;
 916       case Bytecodes::_multianewarray:
 917         { Bytecode_multianewarray mna(caller_method(), caller_method->bcp_from(bci));
 918           k = caller_method->constants()->klass_at(mna.index(), CHECK);
 919         }
 920         break;
 921       case Bytecodes::_instanceof:
 922         { Bytecode_instanceof io(caller_method(), caller_method->bcp_from(bci));
 923           k = caller_method->constants()->klass_at(io.index(), CHECK);
 924         }
 925         break;
 926       case Bytecodes::_checkcast:
 927         { Bytecode_checkcast cc(caller_method(), caller_method->bcp_from(bci));
 928           k = caller_method->constants()->klass_at(cc.index(), CHECK);
 929         }
 930         break;
 931       case Bytecodes::_anewarray:
 932         { Bytecode_anewarray anew(caller_method(), caller_method->bcp_from(bci));
 933           Klass* ek = caller_method->constants()->klass_at(anew.index(), CHECK);
 934           k = ek->array_klass(CHECK);
 935         }
 936         break;
 937       case Bytecodes::_ldc:
 938       case Bytecodes::_ldc_w:
 939         {
 940           Bytecode_loadconstant cc(caller_method, bci);
 941           oop m = cc.resolve_constant(CHECK);
 942           mirror = Handle(THREAD, m);
 943         }
 944         break;
 945       default: fatal("unexpected bytecode for load_klass_or_mirror_patch_id");
 946     }
 947     load_klass = k;
 948   } else if (stub_id == load_appendix_patching_id) {
 949     Bytecode_invoke bytecode(caller_method, bci);
 950     Bytecodes::Code bc = bytecode.invoke_code();
 951 
 952     CallInfo info;
 953     constantPoolHandle pool(thread, caller_method->constants());
 954     int index = bytecode.index();
 955     LinkResolver::resolve_invoke(info, Handle(), pool, index, bc, CHECK);
 956     switch (bc) {
 957       case Bytecodes::_invokehandle: {
 958         int cache_index = ConstantPool::decode_cpcache_index(index, true);
 959         assert(cache_index >= 0 && cache_index < pool->cache()->length(), "unexpected cache index");
 960         ConstantPoolCacheEntry* cpce = pool->cache()->entry_at(cache_index);
 961         cpce->set_method_handle(pool, info);
 962         appendix = Handle(THREAD, cpce->appendix_if_resolved(pool)); // just in case somebody already resolved the entry
 963         break;
 964       }
 965       case Bytecodes::_invokedynamic: {
 966         ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
 967         cpce->set_dynamic_call(pool, info);
 968         appendix = Handle(THREAD, cpce->appendix_if_resolved(pool)); // just in case somebody already resolved the entry
 969         break;
 970       }
 971       default: fatal("unexpected bytecode for load_appendix_patching_id");
 972     }
 973   } else {
 974     ShouldNotReachHere();
 975   }
 976 
 977   if (deoptimize_for_volatile || deoptimize_for_atomic) {
 978     // At compile time we assumed the field wasn't volatile/atomic but after
 979     // loading it turns out it was volatile/atomic so we have to throw the
 980     // compiled code out and let it be regenerated.
 981     if (TracePatching) {
 982       if (deoptimize_for_volatile) {
 983         tty->print_cr("Deoptimizing for patching volatile field reference");
 984       }
 985       if (deoptimize_for_atomic) {
 986         tty->print_cr("Deoptimizing for patching atomic field reference");
 987       }
 988     }
 989 
 990     // It's possible the nmethod was invalidated in the last
 991     // safepoint, but if it's still alive then make it not_entrant.
 992     nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
 993     if (nm != NULL) {
 994       nm->make_not_entrant();
 995     }
 996 
 997     Deoptimization::deoptimize_frame(thread, caller_frame.id());
 998 
 999     // Return to the now deoptimized frame.
1000   }
1001 
1002   // Now copy code back
1003 
1004   {
1005     MutexLockerEx ml_patch (Patching_lock, Mutex::_no_safepoint_check_flag);
1006     //
1007     // Deoptimization may have happened while we waited for the lock.
1008     // In that case we don't bother to do any patching we just return
1009     // and let the deopt happen
1010     if (!caller_is_deopted()) {
1011       NativeGeneralJump* jump = nativeGeneralJump_at(caller_frame.pc());
1012       address instr_pc = jump->jump_destination();
1013       NativeInstruction* ni = nativeInstruction_at(instr_pc);
1014       if (ni->is_jump() ) {
1015         // the jump has not been patched yet
1016         // The jump destination is slow case and therefore not part of the stubs
1017         // (stubs are only for StaticCalls)
1018 
1019         // format of buffer
1020         //    ....
1021         //    instr byte 0     <-- copy_buff
1022         //    instr byte 1
1023         //    ..
1024         //    instr byte n-1
1025         //      n
1026         //    ....             <-- call destination
1027 
1028         address stub_location = caller_frame.pc() + PatchingStub::patch_info_offset();
1029         unsigned char* byte_count = (unsigned char*) (stub_location - 1);
1030         unsigned char* byte_skip = (unsigned char*) (stub_location - 2);
1031         unsigned char* being_initialized_entry_offset = (unsigned char*) (stub_location - 3);
1032         address copy_buff = stub_location - *byte_skip - *byte_count;
1033         address being_initialized_entry = stub_location - *being_initialized_entry_offset;
1034         if (TracePatching) {
1035           ttyLocker ttyl;
1036           tty->print_cr(" Patching %s at bci %d at address " INTPTR_FORMAT "  (%s)", Bytecodes::name(code), bci,
1037                         p2i(instr_pc), (stub_id == Runtime1::access_field_patching_id) ? "field" : "klass");
1038           nmethod* caller_code = CodeCache::find_nmethod(caller_frame.pc());
1039           assert(caller_code != NULL, "nmethod not found");
1040 
1041           // NOTE we use pc() not original_pc() because we already know they are
1042           // identical otherwise we'd have never entered this block of code
1043 
1044           const ImmutableOopMap* map = caller_code->oop_map_for_return_address(caller_frame.pc());
1045           assert(map != NULL, "null check");
1046           map->print();
1047           tty->cr();
1048 
1049           Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
1050         }
1051         // depending on the code below, do_patch says whether to copy the patch body back into the nmethod
1052         bool do_patch = true;
1053         if (stub_id == Runtime1::access_field_patching_id) {
1054           // The offset may not be correct if the class was not loaded at code generation time.
1055           // Set it now.
1056           NativeMovRegMem* n_move = nativeMovRegMem_at(copy_buff);
1057           assert(n_move->offset() == 0 || (n_move->offset() == 4 && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG)), "illegal offset for type");
1058           assert(patch_field_offset >= 0, "illegal offset");
1059           n_move->add_offset_in_bytes(patch_field_offset);
1060         } else if (load_klass_or_mirror_patch_id) {
1061           // If a getstatic or putstatic is referencing a klass which
1062           // isn't fully initialized, the patch body isn't copied into
1063           // place until initialization is complete.  In this case the
1064           // patch site is setup so that any threads besides the
1065           // initializing thread are forced to come into the VM and
1066           // block.
1067           do_patch = (code != Bytecodes::_getstatic && code != Bytecodes::_putstatic) ||
1068                      InstanceKlass::cast(init_klass)->is_initialized();
1069           NativeGeneralJump* jump = nativeGeneralJump_at(instr_pc);
1070           if (jump->jump_destination() == being_initialized_entry) {
1071             assert(do_patch == true, "initialization must be complete at this point");
1072           } else {
1073             // patch the instruction <move reg, klass>
1074             NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff);
1075 
1076             assert(n_copy->data() == 0 ||
1077                    n_copy->data() == (intptr_t)Universe::non_oop_word(),
1078                    "illegal init value");
1079             if (stub_id == Runtime1::load_klass_patching_id) {
1080               assert(load_klass != NULL, "klass not set");
1081               n_copy->set_data((intx) (load_klass));
1082             } else {
1083               assert(mirror() != NULL, "klass not set");
1084               // Don't need a G1 pre-barrier here since we assert above that data isn't an oop.
1085               n_copy->set_data(cast_from_oop<intx>(mirror()));
1086             }
1087 
1088             if (TracePatching) {
1089               Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
1090             }
1091           }
1092         } else if (stub_id == Runtime1::load_appendix_patching_id) {
1093           NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff);
1094           assert(n_copy->data() == 0 ||
1095                  n_copy->data() == (intptr_t)Universe::non_oop_word(),
1096                  "illegal init value");
1097           n_copy->set_data(cast_from_oop<intx>(appendix()));
1098 
1099           if (TracePatching) {
1100             Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
1101           }
1102         } else {
1103           ShouldNotReachHere();
1104         }
1105 
1106 #if defined(SPARC) || defined(PPC32)
1107         if (load_klass_or_mirror_patch_id ||
1108             stub_id == Runtime1::load_appendix_patching_id) {
1109           // Update the location in the nmethod with the proper
1110           // metadata.  When the code was generated, a NULL was stuffed
1111           // in the metadata table and that table needs to be update to
1112           // have the right value.  On intel the value is kept
1113           // directly in the instruction instead of in the metadata
1114           // table, so set_data above effectively updated the value.
1115           nmethod* nm = CodeCache::find_nmethod(instr_pc);
1116           assert(nm != NULL, "invalid nmethod_pc");
1117           RelocIterator mds(nm, copy_buff, copy_buff + 1);
1118           bool found = false;
1119           while (mds.next() && !found) {
1120             if (mds.type() == relocInfo::oop_type) {
1121               assert(stub_id == Runtime1::load_mirror_patching_id ||
1122                      stub_id == Runtime1::load_appendix_patching_id, "wrong stub id");
1123               oop_Relocation* r = mds.oop_reloc();
1124               oop* oop_adr = r->oop_addr();
1125               *oop_adr = stub_id == Runtime1::load_mirror_patching_id ? mirror() : appendix();
1126               r->fix_oop_relocation();
1127               found = true;
1128             } else if (mds.type() == relocInfo::metadata_type) {
1129               assert(stub_id == Runtime1::load_klass_patching_id, "wrong stub id");
1130               metadata_Relocation* r = mds.metadata_reloc();
1131               Metadata** metadata_adr = r->metadata_addr();
1132               *metadata_adr = load_klass;
1133               r->fix_metadata_relocation();
1134               found = true;
1135             }
1136           }
1137           assert(found, "the metadata must exist!");
1138         }
1139 #endif
1140         if (do_patch) {
1141           // replace instructions
1142           // first replace the tail, then the call
1143 #ifdef ARM
1144           if((load_klass_or_mirror_patch_id ||
1145               stub_id == Runtime1::load_appendix_patching_id) &&
1146               nativeMovConstReg_at(copy_buff)->is_pc_relative()) {
1147             nmethod* nm = CodeCache::find_nmethod(instr_pc);
1148             address addr = NULL;
1149             assert(nm != NULL, "invalid nmethod_pc");
1150             RelocIterator mds(nm, copy_buff, copy_buff + 1);
1151             while (mds.next()) {
1152               if (mds.type() == relocInfo::oop_type) {
1153                 assert(stub_id == Runtime1::load_mirror_patching_id ||
1154                        stub_id == Runtime1::load_appendix_patching_id, "wrong stub id");
1155                 oop_Relocation* r = mds.oop_reloc();
1156                 addr = (address)r->oop_addr();
1157                 break;
1158               } else if (mds.type() == relocInfo::metadata_type) {
1159                 assert(stub_id == Runtime1::load_klass_patching_id, "wrong stub id");
1160                 metadata_Relocation* r = mds.metadata_reloc();
1161                 addr = (address)r->metadata_addr();
1162                 break;
1163               }
1164             }
1165             assert(addr != NULL, "metadata relocation must exist");
1166             copy_buff -= *byte_count;
1167             NativeMovConstReg* n_copy2 = nativeMovConstReg_at(copy_buff);
1168             n_copy2->set_pc_relative_offset(addr, instr_pc);
1169           }
1170 #endif
1171 
1172           for (int i = NativeGeneralJump::instruction_size; i < *byte_count; i++) {
1173             address ptr = copy_buff + i;
1174             int a_byte = (*ptr) & 0xFF;
1175             address dst = instr_pc + i;
1176             *(unsigned char*)dst = (unsigned char) a_byte;
1177           }
1178           ICache::invalidate_range(instr_pc, *byte_count);
1179           NativeGeneralJump::replace_mt_safe(instr_pc, copy_buff);
1180 
1181           if (load_klass_or_mirror_patch_id ||
1182               stub_id == Runtime1::load_appendix_patching_id) {
1183             relocInfo::relocType rtype =
1184               (stub_id == Runtime1::load_klass_patching_id) ?
1185                                    relocInfo::metadata_type :
1186                                    relocInfo::oop_type;
1187             // update relocInfo to metadata
1188             nmethod* nm = CodeCache::find_nmethod(instr_pc);
1189             assert(nm != NULL, "invalid nmethod_pc");
1190 
1191             // The old patch site is now a move instruction so update
1192             // the reloc info so that it will get updated during
1193             // future GCs.
1194             RelocIterator iter(nm, (address)instr_pc, (address)(instr_pc + 1));
1195             relocInfo::change_reloc_info_for_address(&iter, (address) instr_pc,
1196                                                      relocInfo::none, rtype);
1197 #ifdef SPARC
1198             // Sparc takes two relocations for an metadata so update the second one.
1199             address instr_pc2 = instr_pc + NativeMovConstReg::add_offset;
1200             RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
1201             relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2,
1202                                                      relocInfo::none, rtype);
1203 #endif
1204 #ifdef PPC32
1205           { address instr_pc2 = instr_pc + NativeMovConstReg::lo_offset;
1206             RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
1207             relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2,
1208                                                      relocInfo::none, rtype);
1209           }
1210 #endif
1211           }
1212 
1213         } else {
1214           ICache::invalidate_range(copy_buff, *byte_count);
1215           NativeGeneralJump::insert_unconditional(instr_pc, being_initialized_entry);
1216         }
1217       }
1218     }
1219   }
1220 
1221   // If we are patching in a non-perm oop, make sure the nmethod
1222   // is on the right list.
1223   if (ScavengeRootsInCode) {
1224     MutexLockerEx ml_code (CodeCache_lock, Mutex::_no_safepoint_check_flag);
1225     nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1226     guarantee(nm != NULL, "only nmethods can contain non-perm oops");
1227 
1228     // Since we've patched some oops in the nmethod,
1229     // (re)register it with the heap.
1230     Universe::heap()->register_nmethod(nm);
1231   }
1232 JRT_END
1233 
1234 #else // DEOPTIMIZE_WHEN_PATCHING
1235 
1236 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id ))
1237   RegisterMap reg_map(thread, false);
1238 
1239   NOT_PRODUCT(_patch_code_slowcase_cnt++;)
1240   if (TracePatching) {
1241     tty->print_cr("Deoptimizing because patch is needed");
1242   }
1243 
1244   frame runtime_frame = thread->last_frame();
1245   frame caller_frame = runtime_frame.sender(&reg_map);
1246 
1247   // It's possible the nmethod was invalidated in the last
1248   // safepoint, but if it's still alive then make it not_entrant.
1249   nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1250   if (nm != NULL) {
1251     nm->make_not_entrant();
1252   }
1253 
1254   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1255 
1256   // Return to the now deoptimized frame.
1257 JRT_END
1258 
1259 #endif // DEOPTIMIZE_WHEN_PATCHING
1260 
1261 //
1262 // Entry point for compiled code. We want to patch a nmethod.
1263 // We don't do a normal VM transition here because we want to
1264 // know after the patching is complete and any safepoint(s) are taken
1265 // if the calling nmethod was deoptimized. We do this by calling a
1266 // helper method which does the normal VM transition and when it
1267 // completes we can check for deoptimization. This simplifies the
1268 // assembly code in the cpu directories.
1269 //
1270 int Runtime1::move_klass_patching(JavaThread* thread) {
1271 //
1272 // NOTE: we are still in Java
1273 //
1274   Thread* THREAD = thread;
1275   debug_only(NoHandleMark nhm;)
1276   {
1277     // Enter VM mode
1278 
1279     ResetNoHandleMark rnhm;
1280     patch_code(thread, load_klass_patching_id);
1281   }
1282   // Back in JAVA, use no oops DON'T safepoint
1283 
1284   // Return true if calling code is deoptimized
1285 
1286   return caller_is_deopted();
1287 }
1288 
1289 int Runtime1::move_mirror_patching(JavaThread* thread) {
1290 //
1291 // NOTE: we are still in Java
1292 //
1293   Thread* THREAD = thread;
1294   debug_only(NoHandleMark nhm;)
1295   {
1296     // Enter VM mode
1297 
1298     ResetNoHandleMark rnhm;
1299     patch_code(thread, load_mirror_patching_id);
1300   }
1301   // Back in JAVA, use no oops DON'T safepoint
1302 
1303   // Return true if calling code is deoptimized
1304 
1305   return caller_is_deopted();
1306 }
1307 
1308 int Runtime1::move_appendix_patching(JavaThread* thread) {
1309 //
1310 // NOTE: we are still in Java
1311 //
1312   Thread* THREAD = thread;
1313   debug_only(NoHandleMark nhm;)
1314   {
1315     // Enter VM mode
1316 
1317     ResetNoHandleMark rnhm;
1318     patch_code(thread, load_appendix_patching_id);
1319   }
1320   // Back in JAVA, use no oops DON'T safepoint
1321 
1322   // Return true if calling code is deoptimized
1323 
1324   return caller_is_deopted();
1325 }
1326 //
1327 // Entry point for compiled code. We want to patch a nmethod.
1328 // We don't do a normal VM transition here because we want to
1329 // know after the patching is complete and any safepoint(s) are taken
1330 // if the calling nmethod was deoptimized. We do this by calling a
1331 // helper method which does the normal VM transition and when it
1332 // completes we can check for deoptimization. This simplifies the
1333 // assembly code in the cpu directories.
1334 //
1335 
1336 int Runtime1::access_field_patching(JavaThread* thread) {
1337 //
1338 // NOTE: we are still in Java
1339 //
1340   Thread* THREAD = thread;
1341   debug_only(NoHandleMark nhm;)
1342   {
1343     // Enter VM mode
1344 
1345     ResetNoHandleMark rnhm;
1346     patch_code(thread, access_field_patching_id);
1347   }
1348   // Back in JAVA, use no oops DON'T safepoint
1349 
1350   // Return true if calling code is deoptimized
1351 
1352   return caller_is_deopted();
1353 JRT_END
1354 
1355 
1356 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
1357   // for now we just print out the block id
1358   tty->print("%d ", block_id);
1359 JRT_END
1360 
1361 
1362 JRT_LEAF(int, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1363   // had to return int instead of bool, otherwise there may be a mismatch
1364   // between the C calling convention and the Java one.
1365   // e.g., on x86, GCC may clear only %al when returning a bool false, but
1366   // JVM takes the whole %eax as the return value, which may misinterpret
1367   // the return value as a boolean true.
1368 
1369   assert(mirror != NULL, "should null-check on mirror before calling");
1370   Klass* k = java_lang_Class::as_Klass(mirror);
1371   return (k != NULL && obj != NULL && obj->is_a(k)) ? 1 : 0;
1372 JRT_END
1373 
1374 JRT_ENTRY(void, Runtime1::predicate_failed_trap(JavaThread* thread))
1375   ResourceMark rm;
1376 
1377   assert(!TieredCompilation, "incompatible with tiered compilation");
1378 
1379   RegisterMap reg_map(thread, false);
1380   frame runtime_frame = thread->last_frame();
1381   frame caller_frame = runtime_frame.sender(&reg_map);
1382 
1383   nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1384   assert (nm != NULL, "no more nmethod?");
1385   nm->make_not_entrant();
1386 
1387   methodHandle m(nm->method());
1388   MethodData* mdo = m->method_data();
1389 
1390   if (mdo == NULL && !HAS_PENDING_EXCEPTION) {
1391     // Build an MDO.  Ignore errors like OutOfMemory;
1392     // that simply means we won't have an MDO to update.
1393     Method::build_interpreter_method_data(m, THREAD);
1394     if (HAS_PENDING_EXCEPTION) {
1395       assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
1396       CLEAR_PENDING_EXCEPTION;
1397     }
1398     mdo = m->method_data();
1399   }
1400 
1401   if (mdo != NULL) {
1402     mdo->inc_trap_count(Deoptimization::Reason_none);
1403   }
1404 
1405   if (TracePredicateFailedTraps) {
1406     stringStream ss1, ss2;
1407     vframeStream vfst(thread);
1408     methodHandle inlinee = methodHandle(vfst.method());
1409     inlinee->print_short_name(&ss1);
1410     m->print_short_name(&ss2);
1411     tty->print_cr("Predicate failed trap in method %s at bci %d inlined in %s at pc " INTPTR_FORMAT, ss1.as_string(), vfst.bci(), ss2.as_string(), p2i(caller_frame.pc()));
1412   }
1413 
1414 
1415   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1416 
1417 JRT_END
1418 
1419 #ifndef PRODUCT
1420 void Runtime1::print_statistics() {
1421   tty->print_cr("C1 Runtime statistics:");
1422   tty->print_cr(" _resolve_invoke_virtual_cnt:     %d", SharedRuntime::_resolve_virtual_ctr);
1423   tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %d", SharedRuntime::_resolve_opt_virtual_ctr);
1424   tty->print_cr(" _resolve_invoke_static_cnt:      %d", SharedRuntime::_resolve_static_ctr);
1425   tty->print_cr(" _handle_wrong_method_cnt:        %d", SharedRuntime::_wrong_method_ctr);
1426   tty->print_cr(" _ic_miss_cnt:                    %d", SharedRuntime::_ic_miss_ctr);
1427   tty->print_cr(" _generic_arraycopy_cnt:          %d", _generic_arraycopy_cnt);
1428   tty->print_cr(" _generic_arraycopystub_cnt:      %d", _generic_arraycopystub_cnt);
1429   tty->print_cr(" _byte_arraycopy_cnt:             %d", _byte_arraycopy_stub_cnt);
1430   tty->print_cr(" _short_arraycopy_cnt:            %d", _short_arraycopy_stub_cnt);
1431   tty->print_cr(" _int_arraycopy_cnt:              %d", _int_arraycopy_stub_cnt);
1432   tty->print_cr(" _long_arraycopy_cnt:             %d", _long_arraycopy_stub_cnt);
1433   tty->print_cr(" _oop_arraycopy_cnt:              %d", _oop_arraycopy_stub_cnt);
1434   tty->print_cr(" _arraycopy_slowcase_cnt:         %d", _arraycopy_slowcase_cnt);
1435   tty->print_cr(" _arraycopy_checkcast_cnt:        %d", _arraycopy_checkcast_cnt);
1436   tty->print_cr(" _arraycopy_checkcast_attempt_cnt:%d", _arraycopy_checkcast_attempt_cnt);
1437 
1438   tty->print_cr(" _new_type_array_slowcase_cnt:    %d", _new_type_array_slowcase_cnt);
1439   tty->print_cr(" _new_object_array_slowcase_cnt:  %d", _new_object_array_slowcase_cnt);
1440   tty->print_cr(" _new_instance_slowcase_cnt:      %d", _new_instance_slowcase_cnt);
1441   tty->print_cr(" _new_multi_array_slowcase_cnt:   %d", _new_multi_array_slowcase_cnt);
1442   tty->print_cr(" _monitorenter_slowcase_cnt:      %d", _monitorenter_slowcase_cnt);
1443   tty->print_cr(" _monitorexit_slowcase_cnt:       %d", _monitorexit_slowcase_cnt);
1444   tty->print_cr(" _patch_code_slowcase_cnt:        %d", _patch_code_slowcase_cnt);
1445 
1446   tty->print_cr(" _throw_range_check_exception_count:            %d:", _throw_range_check_exception_count);
1447   tty->print_cr(" _throw_index_exception_count:                  %d:", _throw_index_exception_count);
1448   tty->print_cr(" _throw_div0_exception_count:                   %d:", _throw_div0_exception_count);
1449   tty->print_cr(" _throw_null_pointer_exception_count:           %d:", _throw_null_pointer_exception_count);
1450   tty->print_cr(" _throw_class_cast_exception_count:             %d:", _throw_class_cast_exception_count);
1451   tty->print_cr(" _throw_incompatible_class_change_error_count:  %d:", _throw_incompatible_class_change_error_count);
1452   tty->print_cr(" _throw_array_store_exception_count:            %d:", _throw_array_store_exception_count);
1453   tty->print_cr(" _throw_count:                                  %d:", _throw_count);
1454 
1455   SharedRuntime::print_ic_miss_histogram();
1456   tty->cr();
1457 }
1458 #endif // PRODUCT