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/javaClasses.inline.hpp"
34 #include "classfile/vmClasses.hpp"
35 #include "classfile/vmSymbols.hpp"
36 #include "code/codeBlob.hpp"
37 #include "code/compiledIC.hpp"
38 #include "code/pcDesc.hpp"
39 #include "code/scopeDesc.hpp"
40 #include "code/vtableStubs.hpp"
41 #include "compiler/compilationPolicy.hpp"
42 #include "compiler/disassembler.hpp"
43 #include "compiler/oopMap.hpp"
44 #include "gc/shared/barrierSet.hpp"
45 #include "gc/shared/c1/barrierSetC1.hpp"
46 #include "gc/shared/collectedHeap.hpp"
47 #include "interpreter/bytecode.hpp"
48 #include "interpreter/interpreter.hpp"
49 #include "jfr/support/jfrIntrinsics.hpp"
50 #include "logging/log.hpp"
51 #include "memory/allocation.inline.hpp"
52 #include "memory/oopFactory.hpp"
53 #include "memory/resourceArea.hpp"
54 #include "memory/universe.hpp"
55 #include "oops/access.inline.hpp"
56 #include "oops/klass.inline.hpp"
57 #include "oops/objArrayOop.inline.hpp"
58 #include "oops/objArrayKlass.hpp"
59 #include "oops/oop.inline.hpp"
60 #include "prims/jvmtiExport.hpp"
61 #include "runtime/atomic.hpp"
62 #include "runtime/fieldDescriptor.inline.hpp"
63 #include "runtime/frame.inline.hpp"
64 #include "runtime/handles.inline.hpp"
65 #include "runtime/interfaceSupport.inline.hpp"
66 #include "runtime/javaCalls.hpp"
67 #include "runtime/sharedRuntime.hpp"
68 #include "runtime/stackWatermarkSet.hpp"
69 #include "runtime/stubRoutines.hpp"
70 #include "runtime/threadCritical.hpp"
71 #include "runtime/vframe.inline.hpp"
72 #include "runtime/vframeArray.hpp"
73 #include "runtime/vm_version.hpp"
74 #include "utilities/copy.hpp"
75 #include "utilities/events.hpp"
76
77
78 // Implementation of StubAssembler
79
80 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
81 _name = name;
82 _must_gc_arguments = false;
83 _frame_size = no_frame_size;
84 _num_rt_args = 0;
85 _stub_id = stub_id;
86 }
87
88
89 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
90 _name = name;
91 _must_gc_arguments = must_gc_arguments;
92 }
93
248 switch (id) {
249 // These stubs don't need to have an oopmap
250 case C1StubId::dtrace_object_alloc_id:
251 case C1StubId::slow_subtype_check_id:
252 case C1StubId::fpu2long_stub_id:
253 case C1StubId::unwind_exception_id:
254 case C1StubId::counter_overflow_id:
255 expect_oop_map = false;
256 break;
257 default:
258 break;
259 }
260 #endif
261 C1StubIdStubAssemblerCodeGenClosure cl(id);
262 CodeBlob* blob = generate_blob(buffer_blob, id, name_for(id), expect_oop_map, &cl);
263 // install blob
264 _blobs[(int)id] = blob;
265 }
266
267 void Runtime1::initialize(BufferBlob* blob) {
268 // platform-dependent initialization
269 initialize_pd();
270 // generate stubs
271 int limit = (int)C1StubId::NUM_STUBIDS;
272 for (int id = 0; id < limit; id++) generate_blob_for(blob, (C1StubId)id);
273 // printing
274 #ifndef PRODUCT
275 if (PrintSimpleStubs) {
276 ResourceMark rm;
277 for (int id = 0; id < limit; id++) {
278 _blobs[id]->print();
279 if (_blobs[id]->oop_maps() != nullptr) {
280 _blobs[id]->oop_maps()->print();
281 }
282 }
283 }
284 #endif
285 BarrierSetC1* bs = BarrierSet::barrier_set()->barrier_set_c1();
286 bs->generate_c1_runtime_stubs(blob);
287 }
334 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit);
335 FUNCTION_CASE(entry, is_instance_of);
336 FUNCTION_CASE(entry, trace_block_entry);
337 #ifdef JFR_HAVE_INTRINSICS
338 FUNCTION_CASE(entry, JfrTime::time_function());
339 #endif
340 FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32());
341 FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32C());
342 FUNCTION_CASE(entry, StubRoutines::vectorizedMismatch());
343 FUNCTION_CASE(entry, StubRoutines::dexp());
344 FUNCTION_CASE(entry, StubRoutines::dlog());
345 FUNCTION_CASE(entry, StubRoutines::dlog10());
346 FUNCTION_CASE(entry, StubRoutines::dpow());
347 FUNCTION_CASE(entry, StubRoutines::dsin());
348 FUNCTION_CASE(entry, StubRoutines::dcos());
349 FUNCTION_CASE(entry, StubRoutines::dtan());
350 FUNCTION_CASE(entry, StubRoutines::dtanh());
351
352 #undef FUNCTION_CASE
353
354 // Soft float adds more runtime names.
355 return pd_name_for_address(entry);
356 }
357
358
359 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* current, Klass* klass))
360 #ifndef PRODUCT
361 if (PrintC1Statistics) {
362 _new_instance_slowcase_cnt++;
363 }
364 #endif
365 assert(klass->is_klass(), "not a class");
366 Handle holder(current, klass->klass_holder()); // keep the klass alive
367 InstanceKlass* h = InstanceKlass::cast(klass);
368 h->check_valid_for_instantiation(true, CHECK);
369 // make sure klass is initialized
370 h->initialize(CHECK);
371 // allocate instance and return via TLS
372 oop obj = h->allocate_instance(CHECK);
373 current->set_vm_result(obj);
374 JRT_END
375
376
377 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* current, Klass* klass, jint length))
378 #ifndef PRODUCT
379 if (PrintC1Statistics) {
380 _new_type_array_slowcase_cnt++;
381 }
382 #endif
383 // Note: no handle for klass needed since they are not used
384 // anymore after new_typeArray() and no GC can happen before.
385 // (This may have to change if this code changes!)
386 assert(klass->is_klass(), "not a class");
387 BasicType elt_type = TypeArrayKlass::cast(klass)->element_type();
388 oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
389 current->set_vm_result(obj);
390 // This is pretty rare but this runtime patch is stressful to deoptimization
391 // if we deoptimize here so force a deopt to stress the path.
392 if (DeoptimizeALot) {
393 deopt_caller(current);
394 }
395
396 JRT_END
397
398
399 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* current, Klass* array_klass, jint length))
400 #ifndef PRODUCT
401 if (PrintC1Statistics) {
402 _new_object_array_slowcase_cnt++;
403 }
404 #endif
405 // Note: no handle for klass needed since they are not used
406 // anymore after new_objArray() and no GC can happen before.
407 // (This may have to change if this code changes!)
408 assert(array_klass->is_klass(), "not a class");
409 Handle holder(current, array_klass->klass_holder()); // keep the klass alive
410 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
411 objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
412 current->set_vm_result(obj);
413 // This is pretty rare but this runtime patch is stressful to deoptimization
414 // if we deoptimize here so force a deopt to stress the path.
415 if (DeoptimizeALot) {
416 deopt_caller(current);
417 }
418 JRT_END
419
420
421 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* current, Klass* klass, int rank, jint* dims))
422 #ifndef PRODUCT
423 if (PrintC1Statistics) {
424 _new_multi_array_slowcase_cnt++;
425 }
426 #endif
427 assert(klass->is_klass(), "not a class");
428 assert(rank >= 1, "rank must be nonzero");
429 Handle holder(current, klass->klass_holder()); // keep the klass alive
430 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
431 current->set_vm_result(obj);
432 JRT_END
433
434
435 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* current, C1StubId id))
436 tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", (int)id);
437 JRT_END
438
439
440 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* current, oopDesc* obj))
441 ResourceMark rm(current);
472 case Bytecodes::_if_icmplt: case Bytecodes::_iflt:
473 case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt:
474 case Bytecodes::_if_icmple: case Bytecodes::_ifle:
475 case Bytecodes::_if_icmpge: case Bytecodes::_ifge:
476 case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq:
477 case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne:
478 case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto:
479 offset = (int16_t)Bytes::get_Java_u2(pc + 1);
480 break;
481 case Bytecodes::_goto_w:
482 offset = Bytes::get_Java_u4(pc + 1);
483 break;
484 default: ;
485 }
486 bci = branch_bci + offset;
487 }
488 osr_nm = CompilationPolicy::event(enclosing_method, method, branch_bci, bci, level, nm, current);
489 return osr_nm;
490 }
491
492 JRT_BLOCK_ENTRY(address, Runtime1::counter_overflow(JavaThread* current, int bci, Method* method))
493 nmethod* osr_nm;
494 JRT_BLOCK
495 osr_nm = counter_overflow_helper(current, bci, method);
496 if (osr_nm != nullptr) {
497 RegisterMap map(current,
498 RegisterMap::UpdateMap::skip,
499 RegisterMap::ProcessFrames::include,
500 RegisterMap::WalkContinuation::skip);
501 frame fr = current->last_frame().sender(&map);
502 Deoptimization::deoptimize_frame(current, fr.id());
503 }
504 JRT_BLOCK_END
505 return nullptr;
506 JRT_END
507
508 extern void vm_exit(int code);
509
510 // Enter this method from compiled code handler below. This is where we transition
511 // to VM mode. This is done as a helper routine so that the method called directly
512 // from compiled code does not have to transition to VM. This allows the entry
513 // method to see if the nmethod that we have just looked up a handler for has
514 // been deoptimized while we were in the vm. This simplifies the assembly code
515 // cpu directories.
516 //
517 // We are entering here from exception stub (via the entry method below)
518 // If there is a compiled exception handler in this method, we will continue there;
519 // otherwise we will unwind the stack and continue at the caller of top frame method
520 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
521 // control the area where we can allow a safepoint. After we exit the safepoint area we can
522 // check to see if the handler we are going to return is now in a nmethod that has
523 // been deoptimized. If that is the case we return the deopt blob
524 // unpack_with_exception entry instead. This makes life for the exception blob easier
525 // because making that same check and diverting is painful from assembly language.
526 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* current, oopDesc* ex, address pc, nmethod*& nm))
527 // Reset method handle flag.
528 current->set_is_method_handle_return(false);
529
530 Handle exception(current, ex);
531
532 // This function is called when we are about to throw an exception. Therefore,
533 // we have to poll the stack watermark barrier to make sure that not yet safe
534 // stack frames are made safe before returning into them.
535 if (current->last_frame().cb() == Runtime1::blob_for(C1StubId::handle_exception_from_callee_id)) {
536 // The C1StubId::handle_exception_from_callee_id handler is invoked after the
537 // frame has been unwound. It instead builds its own stub frame, to call the
538 // runtime. But the throwing frame has already been unwound here.
539 StackWatermarkSet::after_unwind(current);
540 }
541
542 nm = CodeCache::find_nmethod(pc);
543 assert(nm != nullptr, "this is not an nmethod");
544 // Adjust the pc as needed/
545 if (nm->is_deopt_pc(pc)) {
546 RegisterMap map(current,
738 _throw_class_cast_exception_count++;
739 }
740 #endif
741 ResourceMark rm(current);
742 char* message = SharedRuntime::generate_class_cast_message(current, object->klass());
743 SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_ClassCastException(), message);
744 JRT_END
745
746
747 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* current))
748 #ifndef PRODUCT
749 if (PrintC1Statistics) {
750 _throw_incompatible_class_change_error_count++;
751 }
752 #endif
753 ResourceMark rm(current);
754 SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_IncompatibleClassChangeError());
755 JRT_END
756
757
758 JRT_BLOCK_ENTRY(void, Runtime1::monitorenter(JavaThread* current, oopDesc* obj, BasicObjectLock* lock))
759 #ifndef PRODUCT
760 if (PrintC1Statistics) {
761 _monitorenter_slowcase_cnt++;
762 }
763 #endif
764 if (LockingMode == LM_MONITOR) {
765 lock->set_obj(obj);
766 }
767 assert(obj == lock->obj(), "must match");
768 SharedRuntime::monitor_enter_helper(obj, lock->lock(), current);
769 JRT_END
770
771
772 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* current, BasicObjectLock* lock))
773 assert(current == JavaThread::current(), "pre-condition");
774 #ifndef PRODUCT
775 if (PrintC1Statistics) {
776 _monitorexit_slowcase_cnt++;
777 }
778 #endif
779 assert(current->last_Java_sp(), "last_Java_sp must be set");
780 oop obj = lock->obj();
781 assert(oopDesc::is_oop(obj), "must be null or an object");
782 SharedRuntime::monitor_exit_helper(obj, lock->lock(), current);
783 JRT_END
784
785 // Cf. OptoRuntime::deoptimize_caller_frame
786 JRT_ENTRY(void, Runtime1::deoptimize(JavaThread* current, jint trap_request))
787 // Called from within the owner thread, so no need for safepoint
788 RegisterMap reg_map(current,
789 RegisterMap::UpdateMap::skip,
790 RegisterMap::ProcessFrames::include,
791 RegisterMap::WalkContinuation::skip);
792 frame stub_frame = current->last_frame();
793 assert(stub_frame.is_runtime_frame(), "Sanity check");
794 frame caller_frame = stub_frame.sender(®_map);
795 nmethod* nm = caller_frame.cb()->as_nmethod_or_null();
796 assert(nm != nullptr, "Sanity check");
797 methodHandle method(current, nm->method());
798 assert(nm == CodeCache::find_nmethod(caller_frame.pc()), "Should be the same");
799 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
800 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
801
802 if (action == Deoptimization::Action_make_not_entrant) {
803 if (nm->make_not_entrant()) {
804 if (reason == Deoptimization::Reason_tenured) {
805 MethodData* trap_mdo = Deoptimization::get_method_data(current, method, true /*create_if_missing*/);
806 if (trap_mdo != nullptr) {
809 }
810 }
811 }
812
813 // Deoptimize the caller frame.
814 Deoptimization::deoptimize_frame(current, caller_frame.id());
815 // Return to the now deoptimized frame.
816 JRT_END
817
818
819 #ifndef DEOPTIMIZE_WHEN_PATCHING
820
821 static Klass* resolve_field_return_klass(const methodHandle& caller, int bci, TRAPS) {
822 Bytecode_field field_access(caller, bci);
823 // This can be static or non-static field access
824 Bytecodes::Code code = field_access.code();
825
826 // We must load class, initialize class and resolve the field
827 fieldDescriptor result; // initialize class if needed
828 constantPoolHandle constants(THREAD, caller->constants());
829 LinkResolver::resolve_field_access(result, constants, field_access.index(), caller, Bytecodes::java_code(code), CHECK_NULL);
830 return result.field_holder();
831 }
832
833
834 //
835 // This routine patches sites where a class wasn't loaded or
836 // initialized at the time the code was generated. It handles
837 // references to classes, fields and forcing of initialization. Most
838 // of the cases are straightforward and involving simply forcing
839 // resolution of a class, rewriting the instruction stream with the
840 // needed constant and replacing the call in this function with the
841 // patched code. The case for static field is more complicated since
842 // the thread which is in the process of initializing a class can
843 // access it's static fields but other threads can't so the code
844 // either has to deoptimize when this case is detected or execute a
845 // check that the current thread is the initializing thread. The
846 // current
847 //
848 // Patches basically look like this:
849 //
912 // always end up with a correct outcome. This is easiest if there are
913 // few or no intermediate states. (Some inline caches have two
914 // related instructions that must be patched in tandem. For those,
915 // intermediate states seem to be unavoidable, but we will get the
916 // right answer from all possible observation orders.)
917 //
918 // When patching the entry instruction at the head of a method, or a
919 // linkable call instruction inside of a method, we try very hard to
920 // use a patch sequence which executes as a single memory transaction.
921 // This means, in practice, that when thread A patches an instruction,
922 // it should patch a 32-bit or 64-bit word that somehow overlaps the
923 // instruction or is contained in it. We believe that memory hardware
924 // will never break up such a word write, if it is naturally aligned
925 // for the word being written. We also know that some CPUs work very
926 // hard to create atomic updates even of naturally unaligned words,
927 // but we don't want to bet the farm on this always working.
928 //
929 // Therefore, if there is any chance of a race condition, we try to
930 // patch only naturally aligned words, as single, full-word writes.
931
932 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* current, C1StubId stub_id ))
933 #ifndef PRODUCT
934 if (PrintC1Statistics) {
935 _patch_code_slowcase_cnt++;
936 }
937 #endif
938
939 ResourceMark rm(current);
940 RegisterMap reg_map(current,
941 RegisterMap::UpdateMap::skip,
942 RegisterMap::ProcessFrames::include,
943 RegisterMap::WalkContinuation::skip);
944 frame runtime_frame = current->last_frame();
945 frame caller_frame = runtime_frame.sender(®_map);
946
947 // last java frame on stack
948 vframeStream vfst(current, true);
949 assert(!vfst.at_end(), "Java frame must exist");
950
951 methodHandle caller_method(current, vfst.method());
952 // Note that caller_method->code() may not be same as caller_code because of OSR's
957 Bytecodes::Code code = caller_method()->java_code_at(bci);
958
959 // this is used by assertions in the access_field_patching_id
960 BasicType patch_field_type = T_ILLEGAL;
961 bool deoptimize_for_volatile = false;
962 bool deoptimize_for_atomic = false;
963 int patch_field_offset = -1;
964 Klass* init_klass = nullptr; // klass needed by load_klass_patching code
965 Klass* load_klass = nullptr; // klass needed by load_klass_patching code
966 Handle mirror(current, nullptr); // oop needed by load_mirror_patching code
967 Handle appendix(current, nullptr); // oop needed by appendix_patching code
968 bool load_klass_or_mirror_patch_id =
969 (stub_id == C1StubId::load_klass_patching_id || stub_id == C1StubId::load_mirror_patching_id);
970
971 if (stub_id == C1StubId::access_field_patching_id) {
972
973 Bytecode_field field_access(caller_method, bci);
974 fieldDescriptor result; // initialize class if needed
975 Bytecodes::Code code = field_access.code();
976 constantPoolHandle constants(current, caller_method->constants());
977 LinkResolver::resolve_field_access(result, constants, field_access.index(), caller_method, Bytecodes::java_code(code), CHECK);
978 patch_field_offset = result.offset();
979
980 // If we're patching a field which is volatile then at compile it
981 // must not have been know to be volatile, so the generated code
982 // isn't correct for a volatile reference. The nmethod has to be
983 // deoptimized so that the code can be regenerated correctly.
984 // This check is only needed for access_field_patching since this
985 // is the path for patching field offsets. load_klass is only
986 // used for patching references to oops which don't need special
987 // handling in the volatile case.
988
989 deoptimize_for_volatile = result.access_flags().is_volatile();
990
991 // If we are patching a field which should be atomic, then
992 // the generated code is not correct either, force deoptimizing.
993 // We need to only cover T_LONG and T_DOUBLE fields, as we can
994 // break access atomicity only for them.
995
996 // Strictly speaking, the deoptimization on 64-bit platforms
997 // is unnecessary, and T_LONG stores on 32-bit platforms need
1299 switch (code) {
1300 case Bytecodes::_new:
1301 case Bytecodes::_anewarray:
1302 case Bytecodes::_multianewarray:
1303 case Bytecodes::_instanceof:
1304 case Bytecodes::_checkcast: {
1305 Bytecode bc(caller_method(), caller_method->bcp_from(bci));
1306 constantTag tag = caller_method->constants()->tag_at(bc.get_index_u2(code));
1307 if (tag.is_unresolved_klass_in_error()) {
1308 return false; // throws resolution error
1309 }
1310 break;
1311 }
1312
1313 default: break;
1314 }
1315 }
1316 return true;
1317 }
1318
1319 void Runtime1::patch_code(JavaThread* current, C1StubId stub_id) {
1320 #ifndef PRODUCT
1321 if (PrintC1Statistics) {
1322 _patch_code_slowcase_cnt++;
1323 }
1324 #endif
1325
1326 // Enable WXWrite: the function is called by c1 stub as a runtime function
1327 // (see another implementation above).
1328 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1329
1330 if (TracePatching) {
1331 tty->print_cr("Deoptimizing because patch is needed");
1332 }
1333
1334 RegisterMap reg_map(current,
1335 RegisterMap::UpdateMap::skip,
1336 RegisterMap::ProcessFrames::include,
1337 RegisterMap::WalkContinuation::skip);
1338
1339 frame runtime_frame = current->last_frame();
1340 frame caller_frame = runtime_frame.sender(®_map);
1341 assert(caller_frame.is_compiled_frame(), "Wrong frame type");
1342
1343 if (is_patching_needed(current, stub_id)) {
1344 // Make sure the nmethod is invalidated, i.e. made not entrant.
1345 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1346 if (nm != nullptr) {
1347 nm->make_not_entrant();
1348 }
1349 }
1350
1351 Deoptimization::deoptimize_frame(current, caller_frame.id());
1352 // Return to the now deoptimized frame.
1353 postcond(caller_is_deopted(current));
1354 }
1355
1356 #endif // DEOPTIMIZE_WHEN_PATCHING
1357
1358 // Entry point for compiled code. We want to patch a nmethod.
1359 // We don't do a normal VM transition here because we want to
1360 // know after the patching is complete and any safepoint(s) are taken
1361 // if the calling nmethod was deoptimized. We do this by calling a
1362 // helper method which does the normal VM transition and when it
1363 // completes we can check for deoptimization. This simplifies the
1364 // assembly code in the cpu directories.
1365 //
1366 int Runtime1::move_klass_patching(JavaThread* current) {
1367 //
1368 // NOTE: we are still in Java
1369 //
1370 debug_only(NoHandleMark nhm;)
1371 {
1372 // Enter VM mode
1373 ResetNoHandleMark rnhm;
1374 patch_code(current, C1StubId::load_klass_patching_id);
1425 int Runtime1::access_field_patching(JavaThread* current) {
1426 //
1427 // NOTE: we are still in Java
1428 //
1429 // Handles created in this function will be deleted by the
1430 // HandleMarkCleaner in the transition to the VM.
1431 NoHandleMark nhm;
1432 {
1433 // Enter VM mode
1434 ResetNoHandleMark rnhm;
1435 patch_code(current, C1StubId::access_field_patching_id);
1436 }
1437 // Back in JAVA, use no oops DON'T safepoint
1438
1439 // Return true if calling code is deoptimized
1440
1441 return caller_is_deopted(current);
1442 }
1443
1444
1445 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
1446 // for now we just print out the block id
1447 tty->print("%d ", block_id);
1448 JRT_END
1449
1450
1451 JRT_LEAF(int, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1452 // had to return int instead of bool, otherwise there may be a mismatch
1453 // between the C calling convention and the Java one.
1454 // e.g., on x86, GCC may clear only %al when returning a bool false, but
1455 // JVM takes the whole %eax as the return value, which may misinterpret
1456 // the return value as a boolean true.
1457
1458 assert(mirror != nullptr, "should null-check on mirror before calling");
1459 Klass* k = java_lang_Class::as_Klass(mirror);
1460 return (k != nullptr && obj != nullptr && obj->is_a(k)) ? 1 : 0;
1461 JRT_END
1462
1463 JRT_ENTRY(void, Runtime1::predicate_failed_trap(JavaThread* current))
1464 ResourceMark rm;
1465
1466 RegisterMap reg_map(current,
1467 RegisterMap::UpdateMap::skip,
1468 RegisterMap::ProcessFrames::include,
1469 RegisterMap::WalkContinuation::skip);
1470 frame runtime_frame = current->last_frame();
1471 frame caller_frame = runtime_frame.sender(®_map);
1472
1473 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1474 assert (nm != nullptr, "no more nmethod?");
1475 nm->make_not_entrant();
1476
1477 methodHandle m(current, nm->method());
1478 MethodData* mdo = m->method_data();
1479
1480 if (mdo == nullptr && !HAS_PENDING_EXCEPTION) {
1481 // Build an MDO. Ignore errors like OutOfMemory;
1482 // that simply means we won't have an MDO to update.
1483 Method::build_profiling_method_data(m, THREAD);
1503 }
1504
1505
1506 Deoptimization::deoptimize_frame(current, caller_frame.id());
1507
1508 JRT_END
1509
1510 // Check exception if AbortVMOnException flag set
1511 JRT_LEAF(void, Runtime1::check_abort_on_vm_exception(oopDesc* ex))
1512 ResourceMark rm;
1513 const char* message = nullptr;
1514 if (ex->is_a(vmClasses::Throwable_klass())) {
1515 oop msg = java_lang_Throwable::message(ex);
1516 if (msg != nullptr) {
1517 message = java_lang_String::as_utf8_string(msg);
1518 }
1519 }
1520 Exceptions::debug_check_abort(ex->klass()->external_name(), message);
1521 JRT_END
1522
1523 #ifndef PRODUCT
1524 void Runtime1::print_statistics() {
1525 tty->print_cr("C1 Runtime statistics:");
1526 tty->print_cr(" _resolve_invoke_virtual_cnt: %u", SharedRuntime::_resolve_virtual_ctr);
1527 tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %u", SharedRuntime::_resolve_opt_virtual_ctr);
1528 tty->print_cr(" _resolve_invoke_static_cnt: %u", SharedRuntime::_resolve_static_ctr);
1529 tty->print_cr(" _handle_wrong_method_cnt: %u", SharedRuntime::_wrong_method_ctr);
1530 tty->print_cr(" _ic_miss_cnt: %u", SharedRuntime::_ic_miss_ctr);
1531 tty->print_cr(" _generic_arraycopystub_cnt: %u", _generic_arraycopystub_cnt);
1532 tty->print_cr(" _byte_arraycopy_cnt: %u", _byte_arraycopy_stub_cnt);
1533 tty->print_cr(" _short_arraycopy_cnt: %u", _short_arraycopy_stub_cnt);
1534 tty->print_cr(" _int_arraycopy_cnt: %u", _int_arraycopy_stub_cnt);
1535 tty->print_cr(" _long_arraycopy_cnt: %u", _long_arraycopy_stub_cnt);
1536 tty->print_cr(" _oop_arraycopy_cnt: %u", _oop_arraycopy_stub_cnt);
1537 tty->print_cr(" _arraycopy_slowcase_cnt: %u", _arraycopy_slowcase_cnt);
1538 tty->print_cr(" _arraycopy_checkcast_cnt: %u", _arraycopy_checkcast_cnt);
1539 tty->print_cr(" _arraycopy_checkcast_attempt_cnt:%u", _arraycopy_checkcast_attempt_cnt);
1540
1541 tty->print_cr(" _new_type_array_slowcase_cnt: %u", _new_type_array_slowcase_cnt);
1542 tty->print_cr(" _new_object_array_slowcase_cnt: %u", _new_object_array_slowcase_cnt);
1543 tty->print_cr(" _new_instance_slowcase_cnt: %u", _new_instance_slowcase_cnt);
1544 tty->print_cr(" _new_multi_array_slowcase_cnt: %u", _new_multi_array_slowcase_cnt);
1545 tty->print_cr(" _monitorenter_slowcase_cnt: %u", _monitorenter_slowcase_cnt);
1546 tty->print_cr(" _monitorexit_slowcase_cnt: %u", _monitorexit_slowcase_cnt);
1547 tty->print_cr(" _patch_code_slowcase_cnt: %u", _patch_code_slowcase_cnt);
1548
1549 tty->print_cr(" _throw_range_check_exception_count: %u:", _throw_range_check_exception_count);
1550 tty->print_cr(" _throw_index_exception_count: %u:", _throw_index_exception_count);
1551 tty->print_cr(" _throw_div0_exception_count: %u:", _throw_div0_exception_count);
1552 tty->print_cr(" _throw_null_pointer_exception_count: %u:", _throw_null_pointer_exception_count);
1553 tty->print_cr(" _throw_class_cast_exception_count: %u:", _throw_class_cast_exception_count);
1554 tty->print_cr(" _throw_incompatible_class_change_error_count: %u:", _throw_incompatible_class_change_error_count);
1555 tty->print_cr(" _throw_count: %u:", _throw_count);
1556
1557 SharedRuntime::print_ic_miss_histogram();
1558 tty->cr();
1559 }
1560 #endif // PRODUCT
|
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/javaClasses.inline.hpp"
34 #include "classfile/vmClasses.hpp"
35 #include "classfile/vmSymbols.hpp"
36 #include "code/codeBlob.hpp"
37 #include "code/compiledIC.hpp"
38 #include "code/pcDesc.hpp"
39 #include "code/scopeDesc.hpp"
40 #include "code/vtableStubs.hpp"
41 #include "compiler/compilationPolicy.hpp"
42 #include "compiler/compilerDefinitions.inline.hpp"
43 #include "compiler/disassembler.hpp"
44 #include "compiler/oopMap.hpp"
45 #include "gc/shared/barrierSet.hpp"
46 #include "gc/shared/c1/barrierSetC1.hpp"
47 #include "gc/shared/collectedHeap.hpp"
48 #include "interpreter/bytecode.hpp"
49 #include "interpreter/interpreter.hpp"
50 #include "jfr/support/jfrIntrinsics.hpp"
51 #include "logging/log.hpp"
52 #include "memory/allocation.inline.hpp"
53 #include "memory/oopFactory.hpp"
54 #include "memory/resourceArea.hpp"
55 #include "memory/universe.hpp"
56 #include "oops/access.inline.hpp"
57 #include "oops/klass.inline.hpp"
58 #include "oops/objArrayOop.inline.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "prims/jvmtiExport.hpp"
62 #include "runtime/atomic.hpp"
63 #include "runtime/fieldDescriptor.inline.hpp"
64 #include "runtime/frame.inline.hpp"
65 #include "runtime/handles.inline.hpp"
66 #include "runtime/interfaceSupport.inline.hpp"
67 #include "runtime/javaCalls.hpp"
68 #include "runtime/perfData.inline.hpp"
69 #include "runtime/runtimeUpcalls.hpp"
70 #include "runtime/sharedRuntime.hpp"
71 #include "runtime/stackWatermarkSet.hpp"
72 #include "runtime/stubRoutines.hpp"
73 #include "runtime/threadCritical.hpp"
74 #include "runtime/vframe.inline.hpp"
75 #include "runtime/vframeArray.hpp"
76 #include "runtime/vm_version.hpp"
77 #include "services/management.hpp"
78 #include "utilities/copy.hpp"
79 #include "utilities/events.hpp"
80
81
82 // Implementation of StubAssembler
83
84 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
85 _name = name;
86 _must_gc_arguments = false;
87 _frame_size = no_frame_size;
88 _num_rt_args = 0;
89 _stub_id = stub_id;
90 }
91
92
93 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
94 _name = name;
95 _must_gc_arguments = must_gc_arguments;
96 }
97
252 switch (id) {
253 // These stubs don't need to have an oopmap
254 case C1StubId::dtrace_object_alloc_id:
255 case C1StubId::slow_subtype_check_id:
256 case C1StubId::fpu2long_stub_id:
257 case C1StubId::unwind_exception_id:
258 case C1StubId::counter_overflow_id:
259 expect_oop_map = false;
260 break;
261 default:
262 break;
263 }
264 #endif
265 C1StubIdStubAssemblerCodeGenClosure cl(id);
266 CodeBlob* blob = generate_blob(buffer_blob, id, name_for(id), expect_oop_map, &cl);
267 // install blob
268 _blobs[(int)id] = blob;
269 }
270
271 void Runtime1::initialize(BufferBlob* blob) {
272 init_counters();
273 // platform-dependent initialization
274 initialize_pd();
275 // generate stubs
276 int limit = (int)C1StubId::NUM_STUBIDS;
277 for (int id = 0; id < limit; id++) generate_blob_for(blob, (C1StubId)id);
278 // printing
279 #ifndef PRODUCT
280 if (PrintSimpleStubs) {
281 ResourceMark rm;
282 for (int id = 0; id < limit; id++) {
283 _blobs[id]->print();
284 if (_blobs[id]->oop_maps() != nullptr) {
285 _blobs[id]->oop_maps()->print();
286 }
287 }
288 }
289 #endif
290 BarrierSetC1* bs = BarrierSet::barrier_set()->barrier_set_c1();
291 bs->generate_c1_runtime_stubs(blob);
292 }
339 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit);
340 FUNCTION_CASE(entry, is_instance_of);
341 FUNCTION_CASE(entry, trace_block_entry);
342 #ifdef JFR_HAVE_INTRINSICS
343 FUNCTION_CASE(entry, JfrTime::time_function());
344 #endif
345 FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32());
346 FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32C());
347 FUNCTION_CASE(entry, StubRoutines::vectorizedMismatch());
348 FUNCTION_CASE(entry, StubRoutines::dexp());
349 FUNCTION_CASE(entry, StubRoutines::dlog());
350 FUNCTION_CASE(entry, StubRoutines::dlog10());
351 FUNCTION_CASE(entry, StubRoutines::dpow());
352 FUNCTION_CASE(entry, StubRoutines::dsin());
353 FUNCTION_CASE(entry, StubRoutines::dcos());
354 FUNCTION_CASE(entry, StubRoutines::dtan());
355 FUNCTION_CASE(entry, StubRoutines::dtanh());
356
357 #undef FUNCTION_CASE
358
359 // Runtime upcalls also has a map of addresses to names
360 const char* upcall_name = RuntimeUpcalls::get_name_for_upcall_address(entry);
361 if (upcall_name != nullptr) {
362 return upcall_name;
363 }
364
365 // Soft float adds more runtime names.
366 return pd_name_for_address(entry);
367 }
368
369
370 JRT_ENTRY_PROF(void, Runtime1, new_instance, Runtime1::new_instance(JavaThread* current, Klass* klass))
371 #ifndef PRODUCT
372 if (PrintC1Statistics) {
373 _new_instance_slowcase_cnt++;
374 }
375 #endif
376 assert(klass->is_klass(), "not a class");
377 Handle holder(current, klass->klass_holder()); // keep the klass alive
378 InstanceKlass* h = InstanceKlass::cast(klass);
379 h->check_valid_for_instantiation(true, CHECK);
380 // make sure klass is initialized
381 h->initialize(CHECK);
382 // allocate instance and return via TLS
383 oop obj = h->allocate_instance(CHECK);
384 current->set_vm_result(obj);
385 JRT_END
386
387
388 JRT_ENTRY_PROF(void, Runtime1, new_type_array, Runtime1::new_type_array(JavaThread* current, Klass* klass, jint length))
389 #ifndef PRODUCT
390 if (PrintC1Statistics) {
391 _new_type_array_slowcase_cnt++;
392 }
393 #endif
394 // Note: no handle for klass needed since they are not used
395 // anymore after new_typeArray() and no GC can happen before.
396 // (This may have to change if this code changes!)
397 assert(klass->is_klass(), "not a class");
398 BasicType elt_type = TypeArrayKlass::cast(klass)->element_type();
399 oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
400 current->set_vm_result(obj);
401 // This is pretty rare but this runtime patch is stressful to deoptimization
402 // if we deoptimize here so force a deopt to stress the path.
403 if (DeoptimizeALot) {
404 deopt_caller(current);
405 }
406
407 JRT_END
408
409
410 JRT_ENTRY_PROF(void, Runtime1, new_object_array, Runtime1::new_object_array(JavaThread* current, Klass* array_klass, jint length))
411 #ifndef PRODUCT
412 if (PrintC1Statistics) {
413 _new_object_array_slowcase_cnt++;
414 }
415 #endif
416 // Note: no handle for klass needed since they are not used
417 // anymore after new_objArray() and no GC can happen before.
418 // (This may have to change if this code changes!)
419 assert(array_klass->is_klass(), "not a class");
420 Handle holder(current, array_klass->klass_holder()); // keep the klass alive
421 Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
422 objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
423 current->set_vm_result(obj);
424 // This is pretty rare but this runtime patch is stressful to deoptimization
425 // if we deoptimize here so force a deopt to stress the path.
426 if (DeoptimizeALot) {
427 deopt_caller(current);
428 }
429 JRT_END
430
431
432 JRT_ENTRY_PROF(void, Runtime1, new_multi_array, Runtime1::new_multi_array(JavaThread* current, Klass* klass, int rank, jint* dims))
433 #ifndef PRODUCT
434 if (PrintC1Statistics) {
435 _new_multi_array_slowcase_cnt++;
436 }
437 #endif
438 assert(klass->is_klass(), "not a class");
439 assert(rank >= 1, "rank must be nonzero");
440 Handle holder(current, klass->klass_holder()); // keep the klass alive
441 oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
442 current->set_vm_result(obj);
443 JRT_END
444
445
446 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* current, C1StubId id))
447 tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", (int)id);
448 JRT_END
449
450
451 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* current, oopDesc* obj))
452 ResourceMark rm(current);
483 case Bytecodes::_if_icmplt: case Bytecodes::_iflt:
484 case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt:
485 case Bytecodes::_if_icmple: case Bytecodes::_ifle:
486 case Bytecodes::_if_icmpge: case Bytecodes::_ifge:
487 case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq:
488 case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne:
489 case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto:
490 offset = (int16_t)Bytes::get_Java_u2(pc + 1);
491 break;
492 case Bytecodes::_goto_w:
493 offset = Bytes::get_Java_u4(pc + 1);
494 break;
495 default: ;
496 }
497 bci = branch_bci + offset;
498 }
499 osr_nm = CompilationPolicy::event(enclosing_method, method, branch_bci, bci, level, nm, current);
500 return osr_nm;
501 }
502
503 JRT_BLOCK_ENTRY_PROF(address, Runtime1, counter_overflow, Runtime1::counter_overflow(JavaThread* current, int bci, Method* method))
504 nmethod* osr_nm;
505 JRT_BLOCK
506 osr_nm = counter_overflow_helper(current, bci, method);
507 if (osr_nm != nullptr) {
508 RegisterMap map(current,
509 RegisterMap::UpdateMap::skip,
510 RegisterMap::ProcessFrames::include,
511 RegisterMap::WalkContinuation::skip);
512 frame fr = current->last_frame().sender(&map);
513 Deoptimization::deoptimize_frame(current, fr.id());
514 }
515 JRT_BLOCK_END
516 return nullptr;
517 JRT_END
518
519 extern void vm_exit(int code);
520
521 // Enter this method from compiled code handler below. This is where we transition
522 // to VM mode. This is done as a helper routine so that the method called directly
523 // from compiled code does not have to transition to VM. This allows the entry
524 // method to see if the nmethod that we have just looked up a handler for has
525 // been deoptimized while we were in the vm. This simplifies the assembly code
526 // cpu directories.
527 //
528 // We are entering here from exception stub (via the entry method below)
529 // If there is a compiled exception handler in this method, we will continue there;
530 // otherwise we will unwind the stack and continue at the caller of top frame method
531 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
532 // control the area where we can allow a safepoint. After we exit the safepoint area we can
533 // check to see if the handler we are going to return is now in a nmethod that has
534 // been deoptimized. If that is the case we return the deopt blob
535 // unpack_with_exception entry instead. This makes life for the exception blob easier
536 // because making that same check and diverting is painful from assembly language.
537 JRT_ENTRY_NO_ASYNC_PROF(static address, Runtime1, exception_handler_for_pc_helper, exception_handler_for_pc_helper(JavaThread* current, oopDesc* ex, address pc, nmethod*& nm))
538 // Reset method handle flag.
539 current->set_is_method_handle_return(false);
540
541 Handle exception(current, ex);
542
543 // This function is called when we are about to throw an exception. Therefore,
544 // we have to poll the stack watermark barrier to make sure that not yet safe
545 // stack frames are made safe before returning into them.
546 if (current->last_frame().cb() == Runtime1::blob_for(C1StubId::handle_exception_from_callee_id)) {
547 // The C1StubId::handle_exception_from_callee_id handler is invoked after the
548 // frame has been unwound. It instead builds its own stub frame, to call the
549 // runtime. But the throwing frame has already been unwound here.
550 StackWatermarkSet::after_unwind(current);
551 }
552
553 nm = CodeCache::find_nmethod(pc);
554 assert(nm != nullptr, "this is not an nmethod");
555 // Adjust the pc as needed/
556 if (nm->is_deopt_pc(pc)) {
557 RegisterMap map(current,
749 _throw_class_cast_exception_count++;
750 }
751 #endif
752 ResourceMark rm(current);
753 char* message = SharedRuntime::generate_class_cast_message(current, object->klass());
754 SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_ClassCastException(), message);
755 JRT_END
756
757
758 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* current))
759 #ifndef PRODUCT
760 if (PrintC1Statistics) {
761 _throw_incompatible_class_change_error_count++;
762 }
763 #endif
764 ResourceMark rm(current);
765 SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_IncompatibleClassChangeError());
766 JRT_END
767
768
769 JRT_BLOCK_ENTRY_PROF(void, Runtime1, monitorenter, Runtime1::monitorenter(JavaThread* current, oopDesc* obj, BasicObjectLock* lock))
770 #ifndef PRODUCT
771 if (PrintC1Statistics) {
772 _monitorenter_slowcase_cnt++;
773 }
774 #endif
775 if (LockingMode == LM_MONITOR) {
776 lock->set_obj(obj);
777 }
778 assert(obj == lock->obj(), "must match");
779 SharedRuntime::monitor_enter_helper(obj, lock->lock(), current);
780 JRT_END
781
782
783 JRT_LEAF_PROF(void, Runtime1, monitorexit, Runtime1::monitorexit(JavaThread* current, BasicObjectLock* lock))
784 assert(current == JavaThread::current(), "pre-condition");
785 #ifndef PRODUCT
786 if (PrintC1Statistics) {
787 _monitorexit_slowcase_cnt++;
788 }
789 #endif
790 assert(current->last_Java_sp(), "last_Java_sp must be set");
791 oop obj = lock->obj();
792 assert(oopDesc::is_oop(obj), "must be null or an object");
793 SharedRuntime::monitor_exit_helper(obj, lock->lock(), current);
794 JRT_END
795
796 // Cf. OptoRuntime::deoptimize_caller_frame
797 JRT_ENTRY_PROF(void, Runtime1, deoptimize, Runtime1::deoptimize(JavaThread* current, jint trap_request))
798 // Called from within the owner thread, so no need for safepoint
799 RegisterMap reg_map(current,
800 RegisterMap::UpdateMap::skip,
801 RegisterMap::ProcessFrames::include,
802 RegisterMap::WalkContinuation::skip);
803 frame stub_frame = current->last_frame();
804 assert(stub_frame.is_runtime_frame(), "Sanity check");
805 frame caller_frame = stub_frame.sender(®_map);
806 nmethod* nm = caller_frame.cb()->as_nmethod_or_null();
807 assert(nm != nullptr, "Sanity check");
808 methodHandle method(current, nm->method());
809 assert(nm == CodeCache::find_nmethod(caller_frame.pc()), "Should be the same");
810 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
811 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
812
813 if (action == Deoptimization::Action_make_not_entrant) {
814 if (nm->make_not_entrant()) {
815 if (reason == Deoptimization::Reason_tenured) {
816 MethodData* trap_mdo = Deoptimization::get_method_data(current, method, true /*create_if_missing*/);
817 if (trap_mdo != nullptr) {
820 }
821 }
822 }
823
824 // Deoptimize the caller frame.
825 Deoptimization::deoptimize_frame(current, caller_frame.id());
826 // Return to the now deoptimized frame.
827 JRT_END
828
829
830 #ifndef DEOPTIMIZE_WHEN_PATCHING
831
832 static Klass* resolve_field_return_klass(const methodHandle& caller, int bci, TRAPS) {
833 Bytecode_field field_access(caller, bci);
834 // This can be static or non-static field access
835 Bytecodes::Code code = field_access.code();
836
837 // We must load class, initialize class and resolve the field
838 fieldDescriptor result; // initialize class if needed
839 constantPoolHandle constants(THREAD, caller->constants());
840 LinkResolver::resolve_field_access(result, constants, field_access.index(), caller,
841 Bytecodes::java_code(code), true /*initialize_class*/, CHECK_NULL);
842 return result.field_holder();
843 }
844
845
846 //
847 // This routine patches sites where a class wasn't loaded or
848 // initialized at the time the code was generated. It handles
849 // references to classes, fields and forcing of initialization. Most
850 // of the cases are straightforward and involving simply forcing
851 // resolution of a class, rewriting the instruction stream with the
852 // needed constant and replacing the call in this function with the
853 // patched code. The case for static field is more complicated since
854 // the thread which is in the process of initializing a class can
855 // access it's static fields but other threads can't so the code
856 // either has to deoptimize when this case is detected or execute a
857 // check that the current thread is the initializing thread. The
858 // current
859 //
860 // Patches basically look like this:
861 //
924 // always end up with a correct outcome. This is easiest if there are
925 // few or no intermediate states. (Some inline caches have two
926 // related instructions that must be patched in tandem. For those,
927 // intermediate states seem to be unavoidable, but we will get the
928 // right answer from all possible observation orders.)
929 //
930 // When patching the entry instruction at the head of a method, or a
931 // linkable call instruction inside of a method, we try very hard to
932 // use a patch sequence which executes as a single memory transaction.
933 // This means, in practice, that when thread A patches an instruction,
934 // it should patch a 32-bit or 64-bit word that somehow overlaps the
935 // instruction or is contained in it. We believe that memory hardware
936 // will never break up such a word write, if it is naturally aligned
937 // for the word being written. We also know that some CPUs work very
938 // hard to create atomic updates even of naturally unaligned words,
939 // but we don't want to bet the farm on this always working.
940 //
941 // Therefore, if there is any chance of a race condition, we try to
942 // patch only naturally aligned words, as single, full-word writes.
943
944 JRT_ENTRY_PROF(void, Runtime1, patch_code, Runtime1::patch_code(JavaThread* current, C1StubId stub_id))
945 #ifndef PRODUCT
946 if (PrintC1Statistics) {
947 _patch_code_slowcase_cnt++;
948 }
949 #endif
950
951 ResourceMark rm(current);
952 RegisterMap reg_map(current,
953 RegisterMap::UpdateMap::skip,
954 RegisterMap::ProcessFrames::include,
955 RegisterMap::WalkContinuation::skip);
956 frame runtime_frame = current->last_frame();
957 frame caller_frame = runtime_frame.sender(®_map);
958
959 // last java frame on stack
960 vframeStream vfst(current, true);
961 assert(!vfst.at_end(), "Java frame must exist");
962
963 methodHandle caller_method(current, vfst.method());
964 // Note that caller_method->code() may not be same as caller_code because of OSR's
969 Bytecodes::Code code = caller_method()->java_code_at(bci);
970
971 // this is used by assertions in the access_field_patching_id
972 BasicType patch_field_type = T_ILLEGAL;
973 bool deoptimize_for_volatile = false;
974 bool deoptimize_for_atomic = false;
975 int patch_field_offset = -1;
976 Klass* init_klass = nullptr; // klass needed by load_klass_patching code
977 Klass* load_klass = nullptr; // klass needed by load_klass_patching code
978 Handle mirror(current, nullptr); // oop needed by load_mirror_patching code
979 Handle appendix(current, nullptr); // oop needed by appendix_patching code
980 bool load_klass_or_mirror_patch_id =
981 (stub_id == C1StubId::load_klass_patching_id || stub_id == C1StubId::load_mirror_patching_id);
982
983 if (stub_id == C1StubId::access_field_patching_id) {
984
985 Bytecode_field field_access(caller_method, bci);
986 fieldDescriptor result; // initialize class if needed
987 Bytecodes::Code code = field_access.code();
988 constantPoolHandle constants(current, caller_method->constants());
989 LinkResolver::resolve_field_access(result, constants, field_access.index(), caller_method,
990 Bytecodes::java_code(code), true /*initialize_class*/, CHECK);
991 patch_field_offset = result.offset();
992
993 // If we're patching a field which is volatile then at compile it
994 // must not have been know to be volatile, so the generated code
995 // isn't correct for a volatile reference. The nmethod has to be
996 // deoptimized so that the code can be regenerated correctly.
997 // This check is only needed for access_field_patching since this
998 // is the path for patching field offsets. load_klass is only
999 // used for patching references to oops which don't need special
1000 // handling in the volatile case.
1001
1002 deoptimize_for_volatile = result.access_flags().is_volatile();
1003
1004 // If we are patching a field which should be atomic, then
1005 // the generated code is not correct either, force deoptimizing.
1006 // We need to only cover T_LONG and T_DOUBLE fields, as we can
1007 // break access atomicity only for them.
1008
1009 // Strictly speaking, the deoptimization on 64-bit platforms
1010 // is unnecessary, and T_LONG stores on 32-bit platforms need
1312 switch (code) {
1313 case Bytecodes::_new:
1314 case Bytecodes::_anewarray:
1315 case Bytecodes::_multianewarray:
1316 case Bytecodes::_instanceof:
1317 case Bytecodes::_checkcast: {
1318 Bytecode bc(caller_method(), caller_method->bcp_from(bci));
1319 constantTag tag = caller_method->constants()->tag_at(bc.get_index_u2(code));
1320 if (tag.is_unresolved_klass_in_error()) {
1321 return false; // throws resolution error
1322 }
1323 break;
1324 }
1325
1326 default: break;
1327 }
1328 }
1329 return true;
1330 }
1331
1332 PROF_ENTRY(void, Runtime1, patch_code, Runtime1::patch_code(JavaThread* current, C1StubId stub_id))
1333 #ifndef PRODUCT
1334 if (PrintC1Statistics) {
1335 _patch_code_slowcase_cnt++;
1336 }
1337 #endif
1338
1339 // Enable WXWrite: the function is called by c1 stub as a runtime function
1340 // (see another implementation above).
1341 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1342
1343 if (TracePatching) {
1344 tty->print_cr("Deoptimizing because patch is needed");
1345 }
1346
1347 RegisterMap reg_map(current,
1348 RegisterMap::UpdateMap::skip,
1349 RegisterMap::ProcessFrames::include,
1350 RegisterMap::WalkContinuation::skip);
1351
1352 frame runtime_frame = current->last_frame();
1353 frame caller_frame = runtime_frame.sender(®_map);
1354 assert(caller_frame.is_compiled_frame(), "Wrong frame type");
1355
1356 if (is_patching_needed(current, stub_id)) {
1357 // Make sure the nmethod is invalidated, i.e. made not entrant.
1358 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1359 if (nm != nullptr) {
1360 nm->make_not_entrant();
1361 }
1362 }
1363
1364 Deoptimization::deoptimize_frame(current, caller_frame.id());
1365 // Return to the now deoptimized frame.
1366 postcond(caller_is_deopted(current));
1367 PROF_END
1368
1369 #endif // DEOPTIMIZE_WHEN_PATCHING
1370
1371 // Entry point for compiled code. We want to patch a nmethod.
1372 // We don't do a normal VM transition here because we want to
1373 // know after the patching is complete and any safepoint(s) are taken
1374 // if the calling nmethod was deoptimized. We do this by calling a
1375 // helper method which does the normal VM transition and when it
1376 // completes we can check for deoptimization. This simplifies the
1377 // assembly code in the cpu directories.
1378 //
1379 int Runtime1::move_klass_patching(JavaThread* current) {
1380 //
1381 // NOTE: we are still in Java
1382 //
1383 debug_only(NoHandleMark nhm;)
1384 {
1385 // Enter VM mode
1386 ResetNoHandleMark rnhm;
1387 patch_code(current, C1StubId::load_klass_patching_id);
1438 int Runtime1::access_field_patching(JavaThread* current) {
1439 //
1440 // NOTE: we are still in Java
1441 //
1442 // Handles created in this function will be deleted by the
1443 // HandleMarkCleaner in the transition to the VM.
1444 NoHandleMark nhm;
1445 {
1446 // Enter VM mode
1447 ResetNoHandleMark rnhm;
1448 patch_code(current, C1StubId::access_field_patching_id);
1449 }
1450 // Back in JAVA, use no oops DON'T safepoint
1451
1452 // Return true if calling code is deoptimized
1453
1454 return caller_is_deopted(current);
1455 }
1456
1457
1458 JRT_LEAF_PROF_NO_THREAD(void, Runtime1, trace_block_entry, Runtime1::trace_block_entry(jint block_id))
1459 // for now we just print out the block id
1460 tty->print("%d ", block_id);
1461 JRT_END
1462
1463
1464 JRT_LEAF_PROF_NO_THREAD(int, Runtime1, is_instance_of, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1465 // had to return int instead of bool, otherwise there may be a mismatch
1466 // between the C calling convention and the Java one.
1467 // e.g., on x86, GCC may clear only %al when returning a bool false, but
1468 // JVM takes the whole %eax as the return value, which may misinterpret
1469 // the return value as a boolean true.
1470
1471 assert(mirror != nullptr, "should null-check on mirror before calling");
1472 Klass* k = java_lang_Class::as_Klass(mirror);
1473 return (k != nullptr && obj != nullptr && obj->is_a(k)) ? 1 : 0;
1474 JRT_END
1475
1476 JRT_ENTRY_PROF(void, Runtime1, predicate_failed_trap, Runtime1::predicate_failed_trap(JavaThread* current))
1477 ResourceMark rm;
1478
1479 RegisterMap reg_map(current,
1480 RegisterMap::UpdateMap::skip,
1481 RegisterMap::ProcessFrames::include,
1482 RegisterMap::WalkContinuation::skip);
1483 frame runtime_frame = current->last_frame();
1484 frame caller_frame = runtime_frame.sender(®_map);
1485
1486 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1487 assert (nm != nullptr, "no more nmethod?");
1488 nm->make_not_entrant();
1489
1490 methodHandle m(current, nm->method());
1491 MethodData* mdo = m->method_data();
1492
1493 if (mdo == nullptr && !HAS_PENDING_EXCEPTION) {
1494 // Build an MDO. Ignore errors like OutOfMemory;
1495 // that simply means we won't have an MDO to update.
1496 Method::build_profiling_method_data(m, THREAD);
1516 }
1517
1518
1519 Deoptimization::deoptimize_frame(current, caller_frame.id());
1520
1521 JRT_END
1522
1523 // Check exception if AbortVMOnException flag set
1524 JRT_LEAF(void, Runtime1::check_abort_on_vm_exception(oopDesc* ex))
1525 ResourceMark rm;
1526 const char* message = nullptr;
1527 if (ex->is_a(vmClasses::Throwable_klass())) {
1528 oop msg = java_lang_Throwable::message(ex);
1529 if (msg != nullptr) {
1530 message = java_lang_String::as_utf8_string(msg);
1531 }
1532 }
1533 Exceptions::debug_check_abort(ex->klass()->external_name(), message);
1534 JRT_END
1535
1536 #define DO_COUNTERS(macro) \
1537 macro(Runtime1, new_instance) \
1538 macro(Runtime1, new_type_array) \
1539 macro(Runtime1, new_object_array) \
1540 macro(Runtime1, new_multi_array) \
1541 macro(Runtime1, counter_overflow) \
1542 macro(Runtime1, exception_handler_for_pc_helper) \
1543 macro(Runtime1, monitorenter) \
1544 macro(Runtime1, monitorexit) \
1545 macro(Runtime1, deoptimize) \
1546 macro(Runtime1, is_instance_of) \
1547 macro(Runtime1, predicate_failed_trap) \
1548 macro(Runtime1, patch_code)
1549
1550 #define INIT_COUNTER(sub, name) \
1551 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
1552 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1553
1554 void Runtime1::init_counters() {
1555 assert(CompilerConfig::is_c1_enabled(), "");
1556
1557 if (UsePerfData) {
1558 EXCEPTION_MARK;
1559
1560 DO_COUNTERS(INIT_COUNTER)
1561
1562 if (HAS_PENDING_EXCEPTION) {
1563 vm_exit_during_initialization("Runtime1::init_counters() failed unexpectedly");
1564 }
1565 }
1566 }
1567 #undef INIT_COUNTER
1568
1569 #define PRINT_COUNTER(sub, name) { \
1570 if (_perf_##sub##_##name##_count != nullptr) { \
1571 jlong count = _perf_##sub##_##name##_count->get_value(); \
1572 if (count > 0) { \
1573 st->print_cr(" %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
1574 _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
1575 _perf_##sub##_##name##_timer->thread_counter_value_us(), \
1576 count); \
1577 }}}
1578
1579
1580 void Runtime1::print_counters_on(outputStream* st) {
1581 if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c1_enabled()) {
1582 DO_COUNTERS(PRINT_COUNTER)
1583 } else {
1584 st->print_cr(" Runtime1: no info (%s is disabled)",
1585 (!CompilerConfig::is_c1_enabled() ? "C1" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
1586 }
1587 }
1588
1589 #undef PRINT_COUNTER
1590 #undef DO_COUNTERS
1591
1592 #ifndef PRODUCT
1593 void Runtime1::print_statistics_on(outputStream* st) {
1594 st->print_cr("C1 Runtime statistics:");
1595 st->print_cr(" _resolve_invoke_virtual_cnt: %u", SharedRuntime::_resolve_virtual_ctr);
1596 st->print_cr(" _resolve_invoke_opt_virtual_cnt: %u", SharedRuntime::_resolve_opt_virtual_ctr);
1597 st->print_cr(" _resolve_invoke_static_cnt: %u", SharedRuntime::_resolve_static_ctr);
1598 st->print_cr(" _handle_wrong_method_cnt: %u", SharedRuntime::_wrong_method_ctr);
1599 st->print_cr(" _ic_miss_cnt: %u", SharedRuntime::_ic_miss_ctr);
1600 st->print_cr(" _generic_arraycopystub_cnt: %u", _generic_arraycopystub_cnt);
1601 st->print_cr(" _byte_arraycopy_cnt: %u", _byte_arraycopy_stub_cnt);
1602 st->print_cr(" _short_arraycopy_cnt: %u", _short_arraycopy_stub_cnt);
1603 st->print_cr(" _int_arraycopy_cnt: %u", _int_arraycopy_stub_cnt);
1604 st->print_cr(" _long_arraycopy_cnt: %u", _long_arraycopy_stub_cnt);
1605 st->print_cr(" _oop_arraycopy_cnt: %u", _oop_arraycopy_stub_cnt);
1606 st->print_cr(" _arraycopy_slowcase_cnt: %u", _arraycopy_slowcase_cnt);
1607 st->print_cr(" _arraycopy_checkcast_cnt: %u", _arraycopy_checkcast_cnt);
1608 st->print_cr(" _arraycopy_checkcast_attempt_cnt:%u", _arraycopy_checkcast_attempt_cnt);
1609
1610 st->print_cr(" _new_type_array_slowcase_cnt: %u", _new_type_array_slowcase_cnt);
1611 st->print_cr(" _new_object_array_slowcase_cnt: %u", _new_object_array_slowcase_cnt);
1612 st->print_cr(" _new_instance_slowcase_cnt: %u", _new_instance_slowcase_cnt);
1613 st->print_cr(" _new_multi_array_slowcase_cnt: %u", _new_multi_array_slowcase_cnt);
1614 st->print_cr(" _monitorenter_slowcase_cnt: %u", _monitorenter_slowcase_cnt);
1615 st->print_cr(" _monitorexit_slowcase_cnt: %u", _monitorexit_slowcase_cnt);
1616 st->print_cr(" _patch_code_slowcase_cnt: %u", _patch_code_slowcase_cnt);
1617
1618 st->print_cr(" _throw_range_check_exception_count: %u:", _throw_range_check_exception_count);
1619 st->print_cr(" _throw_index_exception_count: %u:", _throw_index_exception_count);
1620 st->print_cr(" _throw_div0_exception_count: %u:", _throw_div0_exception_count);
1621 st->print_cr(" _throw_null_pointer_exception_count: %u:", _throw_null_pointer_exception_count);
1622 st->print_cr(" _throw_class_cast_exception_count: %u:", _throw_class_cast_exception_count);
1623 st->print_cr(" _throw_incompatible_class_change_error_count: %u:", _throw_incompatible_class_change_error_count);
1624 st->print_cr(" _throw_count: %u:", _throw_count);
1625
1626 SharedRuntime::print_ic_miss_histogram_on(st);
1627 st->cr();
1628 }
1629 #endif // PRODUCT
|