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
1295 switch (code) {
1296 case Bytecodes::_new:
1297 case Bytecodes::_anewarray:
1298 case Bytecodes::_multianewarray:
1299 case Bytecodes::_instanceof:
1300 case Bytecodes::_checkcast: {
1301 Bytecode bc(caller_method(), caller_method->bcp_from(bci));
1302 constantTag tag = caller_method->constants()->tag_at(bc.get_index_u2(code));
1303 if (tag.is_unresolved_klass_in_error()) {
1304 return false; // throws resolution error
1305 }
1306 break;
1307 }
1308
1309 default: break;
1310 }
1311 }
1312 return true;
1313 }
1314
1315 void Runtime1::patch_code(JavaThread* current, C1StubId stub_id) {
1316 #ifndef PRODUCT
1317 if (PrintC1Statistics) {
1318 _patch_code_slowcase_cnt++;
1319 }
1320 #endif
1321
1322 // Enable WXWrite: the function is called by c1 stub as a runtime function
1323 // (see another implementation above).
1324 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1325
1326 if (TracePatching) {
1327 tty->print_cr("Deoptimizing because patch is needed");
1328 }
1329
1330 RegisterMap reg_map(current,
1331 RegisterMap::UpdateMap::skip,
1332 RegisterMap::ProcessFrames::include,
1333 RegisterMap::WalkContinuation::skip);
1334
1335 frame runtime_frame = current->last_frame();
1336 frame caller_frame = runtime_frame.sender(®_map);
1337 assert(caller_frame.is_compiled_frame(), "Wrong frame type");
1338
1339 if (is_patching_needed(current, stub_id)) {
1340 // Make sure the nmethod is invalidated, i.e. made not entrant.
1341 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1342 if (nm != nullptr) {
1343 nm->make_not_entrant();
1344 }
1345 }
1346
1347 Deoptimization::deoptimize_frame(current, caller_frame.id());
1348 // Return to the now deoptimized frame.
1349 postcond(caller_is_deopted(current));
1350 }
1351
1352 #endif // DEOPTIMIZE_WHEN_PATCHING
1353
1354 // Entry point for compiled code. We want to patch a nmethod.
1355 // We don't do a normal VM transition here because we want to
1356 // know after the patching is complete and any safepoint(s) are taken
1357 // if the calling nmethod was deoptimized. We do this by calling a
1358 // helper method which does the normal VM transition and when it
1359 // completes we can check for deoptimization. This simplifies the
1360 // assembly code in the cpu directories.
1361 //
1362 int Runtime1::move_klass_patching(JavaThread* current) {
1363 //
1364 // NOTE: we are still in Java
1365 //
1366 debug_only(NoHandleMark nhm;)
1367 {
1368 // Enter VM mode
1369 ResetNoHandleMark rnhm;
1370 patch_code(current, C1StubId::load_klass_patching_id);
1421 int Runtime1::access_field_patching(JavaThread* current) {
1422 //
1423 // NOTE: we are still in Java
1424 //
1425 // Handles created in this function will be deleted by the
1426 // HandleMarkCleaner in the transition to the VM.
1427 NoHandleMark nhm;
1428 {
1429 // Enter VM mode
1430 ResetNoHandleMark rnhm;
1431 patch_code(current, C1StubId::access_field_patching_id);
1432 }
1433 // Back in JAVA, use no oops DON'T safepoint
1434
1435 // Return true if calling code is deoptimized
1436
1437 return caller_is_deopted(current);
1438 }
1439
1440
1441 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
1442 // for now we just print out the block id
1443 tty->print("%d ", block_id);
1444 JRT_END
1445
1446
1447 JRT_LEAF(int, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1448 // had to return int instead of bool, otherwise there may be a mismatch
1449 // between the C calling convention and the Java one.
1450 // e.g., on x86, GCC may clear only %al when returning a bool false, but
1451 // JVM takes the whole %eax as the return value, which may misinterpret
1452 // the return value as a boolean true.
1453
1454 assert(mirror != nullptr, "should null-check on mirror before calling");
1455 Klass* k = java_lang_Class::as_Klass(mirror);
1456 return (k != nullptr && obj != nullptr && obj->is_a(k)) ? 1 : 0;
1457 JRT_END
1458
1459 JRT_ENTRY(void, Runtime1::predicate_failed_trap(JavaThread* current))
1460 ResourceMark rm;
1461
1462 RegisterMap reg_map(current,
1463 RegisterMap::UpdateMap::skip,
1464 RegisterMap::ProcessFrames::include,
1465 RegisterMap::WalkContinuation::skip);
1466 frame runtime_frame = current->last_frame();
1467 frame caller_frame = runtime_frame.sender(®_map);
1468
1469 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1470 assert (nm != nullptr, "no more nmethod?");
1471 nm->make_not_entrant();
1472
1473 methodHandle m(current, nm->method());
1474 MethodData* mdo = m->method_data();
1475
1476 if (mdo == nullptr && !HAS_PENDING_EXCEPTION) {
1477 // Build an MDO. Ignore errors like OutOfMemory;
1478 // that simply means we won't have an MDO to update.
1479 Method::build_profiling_method_data(m, THREAD);
1499 }
1500
1501
1502 Deoptimization::deoptimize_frame(current, caller_frame.id());
1503
1504 JRT_END
1505
1506 // Check exception if AbortVMOnException flag set
1507 JRT_LEAF(void, Runtime1::check_abort_on_vm_exception(oopDesc* ex))
1508 ResourceMark rm;
1509 const char* message = nullptr;
1510 if (ex->is_a(vmClasses::Throwable_klass())) {
1511 oop msg = java_lang_Throwable::message(ex);
1512 if (msg != nullptr) {
1513 message = java_lang_String::as_utf8_string(msg);
1514 }
1515 }
1516 Exceptions::debug_check_abort(ex->klass()->external_name(), message);
1517 JRT_END
1518
1519 #ifndef PRODUCT
1520 void Runtime1::print_statistics() {
1521 tty->print_cr("C1 Runtime statistics:");
1522 tty->print_cr(" _resolve_invoke_virtual_cnt: %u", SharedRuntime::_resolve_virtual_ctr);
1523 tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %u", SharedRuntime::_resolve_opt_virtual_ctr);
1524 tty->print_cr(" _resolve_invoke_static_cnt: %u", SharedRuntime::_resolve_static_ctr);
1525 tty->print_cr(" _handle_wrong_method_cnt: %u", SharedRuntime::_wrong_method_ctr);
1526 tty->print_cr(" _ic_miss_cnt: %u", SharedRuntime::_ic_miss_ctr);
1527 tty->print_cr(" _generic_arraycopystub_cnt: %u", _generic_arraycopystub_cnt);
1528 tty->print_cr(" _byte_arraycopy_cnt: %u", _byte_arraycopy_stub_cnt);
1529 tty->print_cr(" _short_arraycopy_cnt: %u", _short_arraycopy_stub_cnt);
1530 tty->print_cr(" _int_arraycopy_cnt: %u", _int_arraycopy_stub_cnt);
1531 tty->print_cr(" _long_arraycopy_cnt: %u", _long_arraycopy_stub_cnt);
1532 tty->print_cr(" _oop_arraycopy_cnt: %u", _oop_arraycopy_stub_cnt);
1533 tty->print_cr(" _arraycopy_slowcase_cnt: %u", _arraycopy_slowcase_cnt);
1534 tty->print_cr(" _arraycopy_checkcast_cnt: %u", _arraycopy_checkcast_cnt);
1535 tty->print_cr(" _arraycopy_checkcast_attempt_cnt:%u", _arraycopy_checkcast_attempt_cnt);
1536
1537 tty->print_cr(" _new_type_array_slowcase_cnt: %u", _new_type_array_slowcase_cnt);
1538 tty->print_cr(" _new_object_array_slowcase_cnt: %u", _new_object_array_slowcase_cnt);
1539 tty->print_cr(" _new_instance_slowcase_cnt: %u", _new_instance_slowcase_cnt);
1540 tty->print_cr(" _new_multi_array_slowcase_cnt: %u", _new_multi_array_slowcase_cnt);
1541 tty->print_cr(" _monitorenter_slowcase_cnt: %u", _monitorenter_slowcase_cnt);
1542 tty->print_cr(" _monitorexit_slowcase_cnt: %u", _monitorexit_slowcase_cnt);
1543 tty->print_cr(" _patch_code_slowcase_cnt: %u", _patch_code_slowcase_cnt);
1544
1545 tty->print_cr(" _throw_range_check_exception_count: %u:", _throw_range_check_exception_count);
1546 tty->print_cr(" _throw_index_exception_count: %u:", _throw_index_exception_count);
1547 tty->print_cr(" _throw_div0_exception_count: %u:", _throw_div0_exception_count);
1548 tty->print_cr(" _throw_null_pointer_exception_count: %u:", _throw_null_pointer_exception_count);
1549 tty->print_cr(" _throw_class_cast_exception_count: %u:", _throw_class_cast_exception_count);
1550 tty->print_cr(" _throw_incompatible_class_change_error_count: %u:", _throw_incompatible_class_change_error_count);
1551 tty->print_cr(" _throw_count: %u:", _throw_count);
1552
1553 SharedRuntime::print_ic_miss_histogram();
1554 tty->cr();
1555 }
1556 #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
1308 switch (code) {
1309 case Bytecodes::_new:
1310 case Bytecodes::_anewarray:
1311 case Bytecodes::_multianewarray:
1312 case Bytecodes::_instanceof:
1313 case Bytecodes::_checkcast: {
1314 Bytecode bc(caller_method(), caller_method->bcp_from(bci));
1315 constantTag tag = caller_method->constants()->tag_at(bc.get_index_u2(code));
1316 if (tag.is_unresolved_klass_in_error()) {
1317 return false; // throws resolution error
1318 }
1319 break;
1320 }
1321
1322 default: break;
1323 }
1324 }
1325 return true;
1326 }
1327
1328 PROF_ENTRY(void, Runtime1, patch_code, Runtime1::patch_code(JavaThread* current, C1StubId stub_id))
1329 #ifndef PRODUCT
1330 if (PrintC1Statistics) {
1331 _patch_code_slowcase_cnt++;
1332 }
1333 #endif
1334
1335 // Enable WXWrite: the function is called by c1 stub as a runtime function
1336 // (see another implementation above).
1337 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1338
1339 if (TracePatching) {
1340 tty->print_cr("Deoptimizing because patch is needed");
1341 }
1342
1343 RegisterMap reg_map(current,
1344 RegisterMap::UpdateMap::skip,
1345 RegisterMap::ProcessFrames::include,
1346 RegisterMap::WalkContinuation::skip);
1347
1348 frame runtime_frame = current->last_frame();
1349 frame caller_frame = runtime_frame.sender(®_map);
1350 assert(caller_frame.is_compiled_frame(), "Wrong frame type");
1351
1352 if (is_patching_needed(current, stub_id)) {
1353 // Make sure the nmethod is invalidated, i.e. made not entrant.
1354 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1355 if (nm != nullptr) {
1356 nm->make_not_entrant();
1357 }
1358 }
1359
1360 Deoptimization::deoptimize_frame(current, caller_frame.id());
1361 // Return to the now deoptimized frame.
1362 postcond(caller_is_deopted(current));
1363 PROF_END
1364
1365 #endif // DEOPTIMIZE_WHEN_PATCHING
1366
1367 // Entry point for compiled code. We want to patch a nmethod.
1368 // We don't do a normal VM transition here because we want to
1369 // know after the patching is complete and any safepoint(s) are taken
1370 // if the calling nmethod was deoptimized. We do this by calling a
1371 // helper method which does the normal VM transition and when it
1372 // completes we can check for deoptimization. This simplifies the
1373 // assembly code in the cpu directories.
1374 //
1375 int Runtime1::move_klass_patching(JavaThread* current) {
1376 //
1377 // NOTE: we are still in Java
1378 //
1379 debug_only(NoHandleMark nhm;)
1380 {
1381 // Enter VM mode
1382 ResetNoHandleMark rnhm;
1383 patch_code(current, C1StubId::load_klass_patching_id);
1434 int Runtime1::access_field_patching(JavaThread* current) {
1435 //
1436 // NOTE: we are still in Java
1437 //
1438 // Handles created in this function will be deleted by the
1439 // HandleMarkCleaner in the transition to the VM.
1440 NoHandleMark nhm;
1441 {
1442 // Enter VM mode
1443 ResetNoHandleMark rnhm;
1444 patch_code(current, C1StubId::access_field_patching_id);
1445 }
1446 // Back in JAVA, use no oops DON'T safepoint
1447
1448 // Return true if calling code is deoptimized
1449
1450 return caller_is_deopted(current);
1451 }
1452
1453
1454 JRT_LEAF_PROF_NO_THREAD(void, Runtime1, trace_block_entry, Runtime1::trace_block_entry(jint block_id))
1455 // for now we just print out the block id
1456 tty->print("%d ", block_id);
1457 JRT_END
1458
1459
1460 JRT_LEAF_PROF_NO_THREAD(int, Runtime1, is_instance_of, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1461 // had to return int instead of bool, otherwise there may be a mismatch
1462 // between the C calling convention and the Java one.
1463 // e.g., on x86, GCC may clear only %al when returning a bool false, but
1464 // JVM takes the whole %eax as the return value, which may misinterpret
1465 // the return value as a boolean true.
1466
1467 assert(mirror != nullptr, "should null-check on mirror before calling");
1468 Klass* k = java_lang_Class::as_Klass(mirror);
1469 return (k != nullptr && obj != nullptr && obj->is_a(k)) ? 1 : 0;
1470 JRT_END
1471
1472 JRT_ENTRY_PROF(void, Runtime1, predicate_failed_trap, Runtime1::predicate_failed_trap(JavaThread* current))
1473 ResourceMark rm;
1474
1475 RegisterMap reg_map(current,
1476 RegisterMap::UpdateMap::skip,
1477 RegisterMap::ProcessFrames::include,
1478 RegisterMap::WalkContinuation::skip);
1479 frame runtime_frame = current->last_frame();
1480 frame caller_frame = runtime_frame.sender(®_map);
1481
1482 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1483 assert (nm != nullptr, "no more nmethod?");
1484 nm->make_not_entrant();
1485
1486 methodHandle m(current, nm->method());
1487 MethodData* mdo = m->method_data();
1488
1489 if (mdo == nullptr && !HAS_PENDING_EXCEPTION) {
1490 // Build an MDO. Ignore errors like OutOfMemory;
1491 // that simply means we won't have an MDO to update.
1492 Method::build_profiling_method_data(m, THREAD);
1512 }
1513
1514
1515 Deoptimization::deoptimize_frame(current, caller_frame.id());
1516
1517 JRT_END
1518
1519 // Check exception if AbortVMOnException flag set
1520 JRT_LEAF(void, Runtime1::check_abort_on_vm_exception(oopDesc* ex))
1521 ResourceMark rm;
1522 const char* message = nullptr;
1523 if (ex->is_a(vmClasses::Throwable_klass())) {
1524 oop msg = java_lang_Throwable::message(ex);
1525 if (msg != nullptr) {
1526 message = java_lang_String::as_utf8_string(msg);
1527 }
1528 }
1529 Exceptions::debug_check_abort(ex->klass()->external_name(), message);
1530 JRT_END
1531
1532 #define DO_COUNTERS(macro) \
1533 macro(Runtime1, new_instance) \
1534 macro(Runtime1, new_type_array) \
1535 macro(Runtime1, new_object_array) \
1536 macro(Runtime1, new_multi_array) \
1537 macro(Runtime1, counter_overflow) \
1538 macro(Runtime1, exception_handler_for_pc_helper) \
1539 macro(Runtime1, monitorenter) \
1540 macro(Runtime1, monitorexit) \
1541 macro(Runtime1, deoptimize) \
1542 macro(Runtime1, is_instance_of) \
1543 macro(Runtime1, predicate_failed_trap) \
1544 macro(Runtime1, patch_code)
1545
1546 #define INIT_COUNTER(sub, name) \
1547 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
1548 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1549
1550 void Runtime1::init_counters() {
1551 assert(CompilerConfig::is_c1_enabled(), "");
1552
1553 if (UsePerfData) {
1554 EXCEPTION_MARK;
1555
1556 DO_COUNTERS(INIT_COUNTER)
1557
1558 if (HAS_PENDING_EXCEPTION) {
1559 vm_exit_during_initialization("Runtime1::init_counters() failed unexpectedly");
1560 }
1561 }
1562 }
1563 #undef INIT_COUNTER
1564
1565 #define PRINT_COUNTER(sub, name) { \
1566 if (_perf_##sub##_##name##_count != nullptr) { \
1567 jlong count = _perf_##sub##_##name##_count->get_value(); \
1568 if (count > 0) { \
1569 st->print_cr(" %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
1570 _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
1571 _perf_##sub##_##name##_timer->thread_counter_value_us(), \
1572 count); \
1573 }}}
1574
1575
1576 void Runtime1::print_counters_on(outputStream* st) {
1577 if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c1_enabled()) {
1578 DO_COUNTERS(PRINT_COUNTER)
1579 } else {
1580 st->print_cr(" Runtime1: no info (%s is disabled)",
1581 (!CompilerConfig::is_c1_enabled() ? "C1" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
1582 }
1583 }
1584
1585 #undef PRINT_COUNTER
1586 #undef DO_COUNTERS
1587
1588 #ifndef PRODUCT
1589 void Runtime1::print_statistics_on(outputStream* st) {
1590 st->print_cr("C1 Runtime statistics:");
1591 st->print_cr(" _resolve_invoke_virtual_cnt: %u", SharedRuntime::_resolve_virtual_ctr);
1592 st->print_cr(" _resolve_invoke_opt_virtual_cnt: %u", SharedRuntime::_resolve_opt_virtual_ctr);
1593 st->print_cr(" _resolve_invoke_static_cnt: %u", SharedRuntime::_resolve_static_ctr);
1594 st->print_cr(" _handle_wrong_method_cnt: %u", SharedRuntime::_wrong_method_ctr);
1595 st->print_cr(" _ic_miss_cnt: %u", SharedRuntime::_ic_miss_ctr);
1596 st->print_cr(" _generic_arraycopystub_cnt: %u", _generic_arraycopystub_cnt);
1597 st->print_cr(" _byte_arraycopy_cnt: %u", _byte_arraycopy_stub_cnt);
1598 st->print_cr(" _short_arraycopy_cnt: %u", _short_arraycopy_stub_cnt);
1599 st->print_cr(" _int_arraycopy_cnt: %u", _int_arraycopy_stub_cnt);
1600 st->print_cr(" _long_arraycopy_cnt: %u", _long_arraycopy_stub_cnt);
1601 st->print_cr(" _oop_arraycopy_cnt: %u", _oop_arraycopy_stub_cnt);
1602 st->print_cr(" _arraycopy_slowcase_cnt: %u", _arraycopy_slowcase_cnt);
1603 st->print_cr(" _arraycopy_checkcast_cnt: %u", _arraycopy_checkcast_cnt);
1604 st->print_cr(" _arraycopy_checkcast_attempt_cnt:%u", _arraycopy_checkcast_attempt_cnt);
1605
1606 st->print_cr(" _new_type_array_slowcase_cnt: %u", _new_type_array_slowcase_cnt);
1607 st->print_cr(" _new_object_array_slowcase_cnt: %u", _new_object_array_slowcase_cnt);
1608 st->print_cr(" _new_instance_slowcase_cnt: %u", _new_instance_slowcase_cnt);
1609 st->print_cr(" _new_multi_array_slowcase_cnt: %u", _new_multi_array_slowcase_cnt);
1610 st->print_cr(" _monitorenter_slowcase_cnt: %u", _monitorenter_slowcase_cnt);
1611 st->print_cr(" _monitorexit_slowcase_cnt: %u", _monitorexit_slowcase_cnt);
1612 st->print_cr(" _patch_code_slowcase_cnt: %u", _patch_code_slowcase_cnt);
1613
1614 st->print_cr(" _throw_range_check_exception_count: %u:", _throw_range_check_exception_count);
1615 st->print_cr(" _throw_index_exception_count: %u:", _throw_index_exception_count);
1616 st->print_cr(" _throw_div0_exception_count: %u:", _throw_div0_exception_count);
1617 st->print_cr(" _throw_null_pointer_exception_count: %u:", _throw_null_pointer_exception_count);
1618 st->print_cr(" _throw_class_cast_exception_count: %u:", _throw_class_cast_exception_count);
1619 st->print_cr(" _throw_incompatible_class_change_error_count: %u:", _throw_incompatible_class_change_error_count);
1620 st->print_cr(" _throw_count: %u:", _throw_count);
1621
1622 SharedRuntime::print_ic_miss_histogram_on(st);
1623 st->cr();
1624 }
1625 #endif // PRODUCT
|