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