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