1 /* 2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_CODE_NMETHOD_HPP 26 #define SHARE_CODE_NMETHOD_HPP 27 28 #include "code/codeBlob.hpp" 29 #include "code/pcDesc.hpp" 30 #include "oops/metadata.hpp" 31 #include "oops/method.hpp" 32 33 class AbstractCompiler; 34 class CompiledDirectCall; 35 class CompiledIC; 36 class CompiledICData; 37 class CompileTask; 38 class DepChange; 39 class Dependencies; 40 class DirectiveSet; 41 class DebugInformationRecorder; 42 class ExceptionHandlerTable; 43 class ImplicitExceptionTable; 44 class JvmtiThreadState; 45 class MetadataClosure; 46 class NativeCallWrapper; 47 class OopIterateClosure; 48 class AOTCodeReader; 49 class AOTCodeEntry; 50 class ScopeDesc; 51 class xmlStream; 52 53 // This class is used internally by nmethods, to cache 54 // exception/pc/handler information. 55 56 class ExceptionCache : public CHeapObj<mtCode> { 57 friend class VMStructs; 58 private: 59 enum { cache_size = 16 }; 60 Klass* _exception_type; 61 address _pc[cache_size]; 62 address _handler[cache_size]; 63 volatile int _count; 64 ExceptionCache* volatile _next; 65 ExceptionCache* _purge_list_next; 66 67 inline address pc_at(int index); 68 void set_pc_at(int index, address a) { assert(index >= 0 && index < cache_size,""); _pc[index] = a; } 69 70 inline address handler_at(int index); 71 void set_handler_at(int index, address a) { assert(index >= 0 && index < cache_size,""); _handler[index] = a; } 72 73 inline int count(); 74 // increment_count is only called under lock, but there may be concurrent readers. 75 void increment_count(); 76 77 public: 78 79 ExceptionCache(Handle exception, address pc, address handler); 80 81 Klass* exception_type() { return _exception_type; } 82 ExceptionCache* next(); 83 void set_next(ExceptionCache *ec); 84 ExceptionCache* purge_list_next() { return _purge_list_next; } 85 void set_purge_list_next(ExceptionCache *ec) { _purge_list_next = ec; } 86 87 address match(Handle exception, address pc); 88 bool match_exception_with_space(Handle exception) ; 89 address test_address(address addr); 90 bool add_address_and_handler(address addr, address handler) ; 91 }; 92 93 // cache pc descs found in earlier inquiries 94 class PcDescCache { 95 friend class VMStructs; 96 private: 97 enum { cache_size = 4 }; 98 // The array elements MUST be volatile! Several threads may modify 99 // and read from the cache concurrently. find_pc_desc_internal has 100 // returned wrong results. C++ compiler (namely xlC12) may duplicate 101 // C++ field accesses if the elements are not volatile. 102 typedef PcDesc* PcDescPtr; 103 volatile PcDescPtr _pc_descs[cache_size]; // last cache_size pc_descs found 104 public: 105 PcDescCache() { DEBUG_ONLY(_pc_descs[0] = nullptr); } 106 void init_to(PcDesc* initial_pc_desc); 107 PcDesc* find_pc_desc(int pc_offset, bool approximate); 108 void add_pc_desc(PcDesc* pc_desc); 109 PcDesc* last_pc_desc() { return _pc_descs[0]; } 110 }; 111 112 class PcDescContainer : public CHeapObj<mtCode> { 113 private: 114 PcDescCache _pc_desc_cache; 115 public: 116 PcDescContainer(PcDesc* initial_pc_desc) { _pc_desc_cache.init_to(initial_pc_desc); } 117 118 PcDesc* find_pc_desc_internal(address pc, bool approximate, address code_begin, 119 PcDesc* lower, PcDesc* upper); 120 121 PcDesc* find_pc_desc(address pc, bool approximate, address code_begin, PcDesc* lower, PcDesc* upper) 122 #ifdef PRODUCT 123 { 124 PcDesc* desc = _pc_desc_cache.last_pc_desc(); 125 assert(desc != nullptr, "PcDesc cache should be initialized already"); 126 if (desc->pc_offset() == (pc - code_begin)) { 127 // Cached value matched 128 return desc; 129 } 130 return find_pc_desc_internal(pc, approximate, code_begin, lower, upper); 131 } 132 #endif 133 ; 134 }; 135 136 // nmethods (native methods) are the compiled code versions of Java methods. 137 // 138 // An nmethod contains: 139 // - Header (the nmethod structure) 140 // - Constant part (doubles, longs and floats used in nmethod) 141 // - Code part: 142 // - Code body 143 // - Exception handler 144 // - Stub code 145 // - OOP table 146 // 147 // As a CodeBlob, an nmethod references [mutable data] allocated on the C heap: 148 // - CodeBlob relocation data 149 // - Metainfo 150 // - JVMCI data 151 // 152 // An nmethod references [immutable data] allocated on C heap: 153 // - Dependency assertions data 154 // - Implicit null table array 155 // - Handler entry point array 156 // - Debugging information: 157 // - Scopes data array 158 // - Scopes pcs array 159 // - JVMCI speculations array 160 161 #if INCLUDE_JVMCI 162 class FailedSpeculation; 163 class JVMCINMethodData; 164 #endif 165 166 class nmethod : public CodeBlob { 167 friend class VMStructs; 168 friend class JVMCIVMStructs; 169 friend class CodeCache; // scavengable oops 170 friend class JVMCINMethodData; 171 friend class DeoptimizationScope; 172 173 private: 174 175 // Used to track in which deoptimize handshake this method will be deoptimized. 176 uint64_t _deoptimization_generation; 177 178 uint64_t _gc_epoch; 179 180 // Profiling counter used to figure out the hottest nmethods to record into CDS 181 volatile uint64_t _method_profiling_count; 182 183 Method* _method; 184 185 // To reduce header size union fields which usages do not overlap. 186 union { 187 // To support simple linked-list chaining of nmethods: 188 nmethod* _osr_link; // from InstanceKlass::osr_nmethods_head 189 struct { 190 // These are used for compiled synchronized native methods to 191 // locate the owner and stack slot for the BasicLock. They are 192 // needed because there is no debug information for compiled native 193 // wrappers and the oop maps are insufficient to allow 194 // frame::retrieve_receiver() to work. Currently they are expected 195 // to be byte offsets from the Java stack pointer for maximum code 196 // sharing between platforms. JVMTI's GetLocalInstance() uses these 197 // offsets to find the receiver for non-static native wrapper frames. 198 ByteSize _native_receiver_sp_offset; 199 ByteSize _native_basic_lock_sp_offset; 200 }; 201 }; 202 203 // nmethod's read-only data 204 address _immutable_data; 205 206 PcDescContainer* _pc_desc_container; 207 ExceptionCache* volatile _exception_cache; 208 209 void* _gc_data; 210 211 struct oops_do_mark_link; // Opaque data type. 212 static nmethod* volatile _oops_do_mark_nmethods; 213 oops_do_mark_link* volatile _oops_do_mark_link; 214 215 CompiledICData* _compiled_ic_data; 216 217 // offsets for entry points 218 address _osr_entry_point; // entry point for on stack replacement 219 uint16_t _entry_offset; // entry point with class check 220 uint16_t _verified_entry_offset; // entry point without class check 221 int _entry_bci; // != InvocationEntryBci if this nmethod is an on-stack replacement method 222 int _immutable_data_size; 223 224 // _consts_offset == _content_offset because SECT_CONSTS is first in code buffer 225 226 int _skipped_instructions_size; 227 228 int _stub_offset; 229 230 // Offsets for different stubs section parts 231 int _exception_offset; 232 // All deoptee's will resume execution at this location described by 233 // this offset. 234 int _deopt_handler_offset; 235 // All deoptee's at a MethodHandle call site will resume execution 236 // at this location described by this offset. 237 int _deopt_mh_handler_offset; 238 // Offset (from insts_end) of the unwind handler if it exists 239 int16_t _unwind_handler_offset; 240 // Number of arguments passed on the stack 241 uint16_t _num_stack_arg_slots; 242 243 uint16_t _oops_size; 244 #if INCLUDE_JVMCI 245 // _metadata_size is not specific to JVMCI. In the non-JVMCI case, it can be derived as: 246 // _metadata_size = mutable_data_size - relocation_size 247 uint16_t _metadata_size; 248 #endif 249 250 // Offset in immutable data section 251 // _dependencies_offset == 0 252 uint16_t _nul_chk_table_offset; 253 uint16_t _handler_table_offset; // This table could be big in C1 code 254 int _scopes_pcs_offset; 255 int _scopes_data_offset; 256 #if INCLUDE_JVMCI 257 int _speculations_offset; 258 #endif 259 260 // location in frame (offset for sp) that deopt can store the original 261 // pc during a deopt. 262 int _orig_pc_offset; 263 264 int _compile_id; // which compilation made this nmethod 265 CompLevel _comp_level; // compilation level (s1) 266 CompilerType _compiler_type; // which compiler made this nmethod (u1) 267 268 AOTCodeEntry* _aot_code_entry; 269 270 bool _used; // has this nmethod ever been invoked? 271 272 // Local state used to keep track of whether unloading is happening or not 273 volatile uint8_t _is_unloading_state; 274 275 // Protected by NMethodState_lock 276 volatile signed char _state; // {not_installed, in_use, not_entrant} 277 278 // set during construction 279 uint8_t _has_unsafe_access:1, // May fault due to unsafe access. 280 _has_method_handle_invokes:1,// Has this method MethodHandle invokes? 281 _has_wide_vectors:1, // Preserve wide vectors at safepoints 282 _has_monitors:1, // Fastpath monitor detection for continuations 283 _has_scoped_access:1, // used by for shared scope closure (scopedMemoryAccess.cpp) 284 _has_flushed_dependencies:1, // Used for maintenance of dependencies (under CodeCache_lock) 285 _is_unlinked:1, // mark during class unloading 286 _load_reported:1, // used by jvmti to track if an event has been posted for this nmethod 287 _preloaded:1, 288 _has_clinit_barriers:1; 289 290 enum DeoptimizationStatus : u1 { 291 not_marked, 292 deoptimize, 293 deoptimize_noupdate, 294 deoptimize_done 295 }; 296 297 volatile DeoptimizationStatus _deoptimization_status; // Used for stack deoptimization 298 299 DeoptimizationStatus deoptimization_status() const { 300 return AtomicAccess::load(&_deoptimization_status); 301 } 302 303 // Initialize fields to their default values 304 void init_defaults(CodeBuffer *code_buffer, CodeOffsets* offsets); 305 306 // Post initialization 307 void post_init(); 308 309 // For native wrappers 310 nmethod(Method* method, 311 CompilerType type, 312 int nmethod_size, 313 int compile_id, 314 CodeOffsets* offsets, 315 CodeBuffer *code_buffer, 316 int frame_size, 317 ByteSize basic_lock_owner_sp_offset, /* synchronized natives only */ 318 ByteSize basic_lock_sp_offset, /* synchronized natives only */ 319 OopMapSet* oop_maps, 320 int mutable_data_size); 321 322 // For normal JIT compiled code 323 nmethod(Method* method, 324 CompilerType type, 325 int nmethod_size, 326 int immutable_data_size, 327 int mutable_data_size, 328 int compile_id, 329 int entry_bci, 330 address immutable_data, 331 CodeOffsets* offsets, 332 int orig_pc_offset, 333 DebugInformationRecorder *recorder, 334 Dependencies* dependencies, 335 CodeBuffer *code_buffer, 336 int frame_size, 337 OopMapSet* oop_maps, 338 ExceptionHandlerTable* handler_table, 339 ImplicitExceptionTable* nul_chk_table, 340 AbstractCompiler* compiler, 341 CompLevel comp_level 342 #if INCLUDE_JVMCI 343 , char* speculations = nullptr, 344 int speculations_len = 0, 345 JVMCINMethodData* jvmci_data = nullptr 346 #endif 347 ); 348 349 // helper methods 350 void* operator new(size_t size, int nmethod_size, int comp_level) throw(); 351 352 // For method handle intrinsics: Try MethodNonProfiled, MethodProfiled and NonNMethod. 353 // Attention: Only allow NonNMethod space for special nmethods which don't need to be 354 // findable by nmethod iterators! In particular, they must not contain oops! 355 void* operator new(size_t size, int nmethod_size, bool allow_NonNMethod_space) throw(); 356 357 const char* reloc_string_for(u_char* begin, u_char* end); 358 359 bool try_transition(signed char new_state); 360 361 // Returns true if this thread changed the state of the nmethod or 362 // false if another thread performed the transition. 363 bool make_entrant() { Unimplemented(); return false; } 364 void inc_decompile_count(); 365 366 // Inform external interfaces that a compiled method has been unloaded 367 void post_compiled_method_unload(); 368 369 PcDesc* find_pc_desc(address pc, bool approximate) { 370 if (_pc_desc_container == nullptr) return nullptr; // native method 371 return _pc_desc_container->find_pc_desc(pc, approximate, code_begin(), scopes_pcs_begin(), scopes_pcs_end()); 372 } 373 374 // STW two-phase nmethod root processing helpers. 375 // 376 // When determining liveness of a given nmethod to do code cache unloading, 377 // some collectors need to do different things depending on whether the nmethods 378 // need to absolutely be kept alive during root processing; "strong"ly reachable 379 // nmethods are known to be kept alive at root processing, but the liveness of 380 // "weak"ly reachable ones is to be determined later. 381 // 382 // We want to allow strong and weak processing of nmethods by different threads 383 // at the same time without heavy synchronization. Additional constraints are 384 // to make sure that every nmethod is processed a minimal amount of time, and 385 // nmethods themselves are always iterated at most once at a particular time. 386 // 387 // Note that strong processing work must be a superset of weak processing work 388 // for this code to work. 389 // 390 // We store state and claim information in the _oops_do_mark_link member, using 391 // the two LSBs for the state and the remaining upper bits for linking together 392 // nmethods that were already visited. 393 // The last element is self-looped, i.e. points to itself to avoid some special 394 // "end-of-list" sentinel value. 395 // 396 // _oops_do_mark_link special values: 397 // 398 // _oops_do_mark_link == nullptr: the nmethod has not been visited at all yet, i.e. 399 // is Unclaimed. 400 // 401 // For other values, its lowest two bits indicate the following states of the nmethod: 402 // 403 // weak_request (WR): the nmethod has been claimed by a thread for weak processing 404 // weak_done (WD): weak processing has been completed for this nmethod. 405 // strong_request (SR): the nmethod has been found to need strong processing while 406 // being weak processed. 407 // strong_done (SD): strong processing has been completed for this nmethod . 408 // 409 // The following shows the _only_ possible progressions of the _oops_do_mark_link 410 // pointer. 411 // 412 // Given 413 // N as the nmethod 414 // X the current next value of _oops_do_mark_link 415 // 416 // Unclaimed (C)-> N|WR (C)-> X|WD: the nmethod has been processed weakly by 417 // a single thread. 418 // Unclaimed (C)-> N|WR (C)-> X|WD (O)-> X|SD: after weak processing has been 419 // completed (as above) another thread found that the nmethod needs strong 420 // processing after all. 421 // Unclaimed (C)-> N|WR (O)-> N|SR (C)-> X|SD: during weak processing another 422 // thread finds that the nmethod needs strong processing, marks it as such and 423 // terminates. The original thread completes strong processing. 424 // Unclaimed (C)-> N|SD (C)-> X|SD: the nmethod has been processed strongly from 425 // the beginning by a single thread. 426 // 427 // "|" describes the concatenation of bits in _oops_do_mark_link. 428 // 429 // The diagram also describes the threads responsible for changing the nmethod to 430 // the next state by marking the _transition_ with (C) and (O), which mean "current" 431 // and "other" thread respectively. 432 // 433 434 // States used for claiming nmethods during root processing. 435 static const uint claim_weak_request_tag = 0; 436 static const uint claim_weak_done_tag = 1; 437 static const uint claim_strong_request_tag = 2; 438 static const uint claim_strong_done_tag = 3; 439 440 static oops_do_mark_link* mark_link(nmethod* nm, uint tag) { 441 assert(tag <= claim_strong_done_tag, "invalid tag %u", tag); 442 assert(is_aligned(nm, 4), "nmethod pointer must have zero lower two LSB"); 443 return (oops_do_mark_link*)(((uintptr_t)nm & ~0x3) | tag); 444 } 445 446 static uint extract_state(oops_do_mark_link* link) { 447 return (uint)((uintptr_t)link & 0x3); 448 } 449 450 static nmethod* extract_nmethod(oops_do_mark_link* link) { 451 return (nmethod*)((uintptr_t)link & ~0x3); 452 } 453 454 void oops_do_log_change(const char* state); 455 456 static bool oops_do_has_weak_request(oops_do_mark_link* next) { 457 return extract_state(next) == claim_weak_request_tag; 458 } 459 460 static bool oops_do_has_any_strong_state(oops_do_mark_link* next) { 461 return extract_state(next) >= claim_strong_request_tag; 462 } 463 464 // Attempt Unclaimed -> N|WR transition. Returns true if successful. 465 bool oops_do_try_claim_weak_request(); 466 467 // Attempt Unclaimed -> N|SD transition. Returns the current link. 468 oops_do_mark_link* oops_do_try_claim_strong_done(); 469 // Attempt N|WR -> X|WD transition. Returns nullptr if successful, X otherwise. 470 nmethod* oops_do_try_add_to_list_as_weak_done(); 471 472 // Attempt X|WD -> N|SR transition. Returns the current link. 473 oops_do_mark_link* oops_do_try_add_strong_request(oops_do_mark_link* next); 474 // Attempt X|WD -> X|SD transition. Returns true if successful. 475 bool oops_do_try_claim_weak_done_as_strong_done(oops_do_mark_link* next); 476 477 // Do the N|SD -> X|SD transition. 478 void oops_do_add_to_list_as_strong_done(); 479 480 // Sets this nmethod as strongly claimed (as part of N|SD -> X|SD and N|SR -> X|SD 481 // transitions). 482 void oops_do_set_strong_done(nmethod* old_head); 483 484 void record_nmethod_dependency(); 485 486 nmethod* restore(address code_cache_buffer, 487 const methodHandle& method, 488 int compile_id, 489 address reloc_data, 490 GrowableArray<Handle>& oop_list, 491 GrowableArray<Metadata*>& metadata_list, 492 ImmutableOopMapSet* oop_maps, 493 address immutable_data, 494 GrowableArray<Handle>& reloc_imm_oop_list, 495 GrowableArray<Metadata*>& reloc_imm_metadata_list, 496 AOTCodeReader* aot_code_reader); 497 498 public: 499 // create nmethod using archived nmethod from AOT code cache 500 static nmethod* new_nmethod(nmethod* archived_nm, 501 const methodHandle& method, 502 AbstractCompiler* compiler, 503 int compile_id, 504 address reloc_data, 505 GrowableArray<Handle>& oop_list, 506 GrowableArray<Metadata*>& metadata_list, 507 ImmutableOopMapSet* oop_maps, 508 address immutable_data, 509 GrowableArray<Handle>& reloc_imm_oop_list, 510 GrowableArray<Metadata*>& reloc_imm_metadata_list, 511 AOTCodeReader* aot_code_reader); 512 513 // If you change anything in this enum please patch 514 // vmStructs_jvmci.cpp accordingly. 515 enum class InvalidationReason : s1 { 516 NOT_INVALIDATED = -1, 517 C1_CODEPATCH, 518 C1_DEOPTIMIZE, 519 C1_DEOPTIMIZE_FOR_PATCHING, 520 C1_PREDICATE_FAILED_TRAP, 521 CI_REPLAY, 522 UNLOADING, 523 UNLOADING_COLD, 524 JVMCI_INVALIDATE, 525 JVMCI_MATERIALIZE_VIRTUAL_OBJECT, 526 JVMCI_REPLACED_WITH_NEW_CODE, 527 JVMCI_REPROFILE, 528 MARKED_FOR_DEOPTIMIZATION, 529 MISSING_EXCEPTION_HANDLER, 530 NOT_USED, 531 OSR_INVALIDATION_BACK_BRANCH, 532 OSR_INVALIDATION_FOR_COMPILING_WITH_C1, 533 OSR_INVALIDATION_OF_LOWER_LEVEL, 534 SET_NATIVE_FUNCTION, 535 UNCOMMON_TRAP, 536 WHITEBOX_DEOPTIMIZATION, 537 ZOMBIE, 538 INVALIDATION_REASONS_COUNT 539 }; 540 541 542 static const char* invalidation_reason_to_string(InvalidationReason invalidation_reason) { 543 switch (invalidation_reason) { 544 case InvalidationReason::C1_CODEPATCH: 545 return "C1 code patch"; 546 case InvalidationReason::C1_DEOPTIMIZE: 547 return "C1 deoptimized"; 548 case InvalidationReason::C1_DEOPTIMIZE_FOR_PATCHING: 549 return "C1 deoptimize for patching"; 550 case InvalidationReason::C1_PREDICATE_FAILED_TRAP: 551 return "C1 predicate failed trap"; 552 case InvalidationReason::CI_REPLAY: 553 return "CI replay"; 554 case InvalidationReason::JVMCI_INVALIDATE: 555 return "JVMCI invalidate"; 556 case InvalidationReason::JVMCI_MATERIALIZE_VIRTUAL_OBJECT: 557 return "JVMCI materialize virtual object"; 558 case InvalidationReason::JVMCI_REPLACED_WITH_NEW_CODE: 559 return "JVMCI replaced with new code"; 560 case InvalidationReason::JVMCI_REPROFILE: 561 return "JVMCI reprofile"; 562 case InvalidationReason::MARKED_FOR_DEOPTIMIZATION: 563 return "marked for deoptimization"; 564 case InvalidationReason::MISSING_EXCEPTION_HANDLER: 565 return "missing exception handler"; 566 case InvalidationReason::NOT_USED: 567 return "not used"; 568 case InvalidationReason::OSR_INVALIDATION_BACK_BRANCH: 569 return "OSR invalidation back branch"; 570 case InvalidationReason::OSR_INVALIDATION_FOR_COMPILING_WITH_C1: 571 return "OSR invalidation for compiling with C1"; 572 case InvalidationReason::OSR_INVALIDATION_OF_LOWER_LEVEL: 573 return "OSR invalidation of lower level"; 574 case InvalidationReason::SET_NATIVE_FUNCTION: 575 return "set native function"; 576 case InvalidationReason::UNCOMMON_TRAP: 577 return "uncommon trap"; 578 case InvalidationReason::WHITEBOX_DEOPTIMIZATION: 579 return "whitebox deoptimization"; 580 case InvalidationReason::ZOMBIE: 581 return "zombie"; 582 default: { 583 assert(false, "Unhandled reason"); 584 return "Unknown"; 585 } 586 } 587 } 588 589 // create nmethod with entry_bci 590 static nmethod* new_nmethod(const methodHandle& method, 591 int compile_id, 592 int entry_bci, 593 CodeOffsets* offsets, 594 int orig_pc_offset, 595 DebugInformationRecorder* recorder, 596 Dependencies* dependencies, 597 CodeBuffer *code_buffer, 598 int frame_size, 599 OopMapSet* oop_maps, 600 ExceptionHandlerTable* handler_table, 601 ImplicitExceptionTable* nul_chk_table, 602 AbstractCompiler* compiler, 603 CompLevel comp_level 604 #if INCLUDE_JVMCI 605 , char* speculations = nullptr, 606 int speculations_len = 0, 607 JVMCINMethodData* jvmci_data = nullptr 608 #endif 609 ); 610 611 static nmethod* new_native_nmethod(const methodHandle& method, 612 int compile_id, 613 CodeBuffer *code_buffer, 614 int vep_offset, 615 int frame_complete, 616 int frame_size, 617 ByteSize receiver_sp_offset, 618 ByteSize basic_lock_sp_offset, 619 OopMapSet* oop_maps, 620 int exception_handler = -1); 621 622 Method* method () const { return _method; } 623 uint16_t entry_bci () const { return _entry_bci; } 624 bool is_native_method() const { return _method != nullptr && _method->is_native(); } 625 bool is_java_method () const { return _method != nullptr && !_method->is_native(); } 626 bool is_osr_method () const { return _entry_bci != InvocationEntryBci; } 627 628 int orig_pc_offset() { return _orig_pc_offset; } 629 630 // Compiler task identification. Note that all OSR methods 631 // are numbered in an independent sequence if CICountOSR is true, 632 // and native method wrappers are also numbered independently if 633 // CICountNative is true. 634 int compile_id() const { return _compile_id; } 635 int comp_level() const { return _comp_level; } 636 const char* compile_kind() const; 637 638 inline bool is_compiled_by_c1 () const { return _compiler_type == compiler_c1; } 639 inline bool is_compiled_by_c2 () const { return _compiler_type == compiler_c2; } 640 inline bool is_compiled_by_jvmci() const { return _compiler_type == compiler_jvmci; } 641 CompilerType compiler_type () const { return _compiler_type; } 642 const char* compiler_name () const; 643 644 // boundaries for different parts 645 address consts_begin () const { return content_begin(); } 646 address consts_end () const { return code_begin() ; } 647 address insts_begin () const { return code_begin() ; } 648 address insts_end () const { return header_begin() + _stub_offset ; } 649 address stub_begin () const { return header_begin() + _stub_offset ; } 650 address stub_end () const { return code_end() ; } 651 address exception_begin () const { return header_begin() + _exception_offset ; } 652 address deopt_handler_begin () const { return header_begin() + _deopt_handler_offset ; } 653 address deopt_mh_handler_begin() const { return _deopt_mh_handler_offset != -1 ? (header_begin() + _deopt_mh_handler_offset) : nullptr; } 654 address unwind_handler_begin () const { return _unwind_handler_offset != -1 ? (insts_end() - _unwind_handler_offset) : nullptr; } 655 oop* oops_begin () const { return (oop*) data_begin(); } 656 oop* oops_end () const { return (oop*) data_end(); } 657 658 // mutable data 659 Metadata** metadata_begin () const { return (Metadata**) (mutable_data_begin() + _relocation_size); } 660 #if INCLUDE_JVMCI 661 Metadata** metadata_end () const { return (Metadata**) (mutable_data_begin() + _relocation_size + _metadata_size); } 662 address jvmci_data_begin () const { return mutable_data_begin() + _relocation_size + _metadata_size; } 663 address jvmci_data_end () const { return mutable_data_end(); } 664 #else 665 Metadata** metadata_end () const { return (Metadata**) mutable_data_end(); } 666 #endif 667 668 // immutable data 669 void set_immutable_data(address data) { _immutable_data = data; } 670 address immutable_data_begin () const { return _immutable_data; } 671 address immutable_data_end () const { return _immutable_data + _immutable_data_size ; } 672 address dependencies_begin () const { return _immutable_data; } 673 address dependencies_end () const { return _immutable_data + _nul_chk_table_offset; } 674 address nul_chk_table_begin () const { return _immutable_data + _nul_chk_table_offset; } 675 address nul_chk_table_end () const { return _immutable_data + _handler_table_offset; } 676 address handler_table_begin () const { return _immutable_data + _handler_table_offset; } 677 address handler_table_end () const { return _immutable_data + _scopes_pcs_offset ; } 678 PcDesc* scopes_pcs_begin () const { return (PcDesc*)(_immutable_data + _scopes_pcs_offset) ; } 679 PcDesc* scopes_pcs_end () const { return (PcDesc*)(_immutable_data + _scopes_data_offset) ; } 680 address scopes_data_begin () const { return _immutable_data + _scopes_data_offset ; } 681 682 #if INCLUDE_JVMCI 683 address scopes_data_end () const { return _immutable_data + _speculations_offset ; } 684 address speculations_begin () const { return _immutable_data + _speculations_offset ; } 685 address speculations_end () const { return immutable_data_end(); } 686 #else 687 address scopes_data_end () const { return immutable_data_end(); } 688 #endif 689 690 // Sizes 691 int immutable_data_size() const { return _immutable_data_size; } 692 int consts_size () const { return int( consts_end () - consts_begin ()); } 693 int insts_size () const { return int( insts_end () - insts_begin ()); } 694 int stub_size () const { return int( stub_end () - stub_begin ()); } 695 int oops_size () const { return int((address) oops_end () - (address) oops_begin ()); } 696 int metadata_size () const { return int((address) metadata_end () - (address) metadata_begin ()); } 697 int scopes_data_size () const { return int( scopes_data_end () - scopes_data_begin ()); } 698 int scopes_pcs_size () const { return int((intptr_t)scopes_pcs_end () - (intptr_t)scopes_pcs_begin ()); } 699 int dependencies_size () const { return int( dependencies_end () - dependencies_begin ()); } 700 int handler_table_size () const { return int( handler_table_end() - handler_table_begin()); } 701 int nul_chk_table_size () const { return int( nul_chk_table_end() - nul_chk_table_begin()); } 702 #if INCLUDE_JVMCI 703 int speculations_size () const { return int( speculations_end () - speculations_begin ()); } 704 int jvmci_data_size () const { return int( jvmci_data_end () - jvmci_data_begin ()); } 705 #endif 706 707 int oops_count() const { assert(oops_size() % oopSize == 0, ""); return (oops_size() / oopSize) + 1; } 708 int metadata_count() const { assert(metadata_size() % wordSize == 0, ""); return (metadata_size() / wordSize) + 1; } 709 710 int skipped_instructions_size () const { return _skipped_instructions_size; } 711 int total_size() const; 712 713 // Containment 714 bool consts_contains (address addr) const { return consts_begin () <= addr && addr < consts_end (); } 715 // Returns true if a given address is in the 'insts' section. The method 716 // insts_contains_inclusive() is end-inclusive. 717 bool insts_contains (address addr) const { return insts_begin () <= addr && addr < insts_end (); } 718 bool insts_contains_inclusive(address addr) const { return insts_begin () <= addr && addr <= insts_end (); } 719 bool stub_contains (address addr) const { return stub_begin () <= addr && addr < stub_end (); } 720 bool oops_contains (oop* addr) const { return oops_begin () <= addr && addr < oops_end (); } 721 bool metadata_contains (Metadata** addr) const { return metadata_begin () <= addr && addr < metadata_end (); } 722 bool scopes_data_contains (address addr) const { return scopes_data_begin () <= addr && addr < scopes_data_end (); } 723 bool scopes_pcs_contains (PcDesc* addr) const { return scopes_pcs_begin () <= addr && addr < scopes_pcs_end (); } 724 bool handler_table_contains (address addr) const { return handler_table_begin() <= addr && addr < handler_table_end(); } 725 bool nul_chk_table_contains (address addr) const { return nul_chk_table_begin() <= addr && addr < nul_chk_table_end(); } 726 727 // entry points 728 address entry_point() const { return code_begin() + _entry_offset; } // normal entry point 729 address verified_entry_point() const { return code_begin() + _verified_entry_offset; } // if klass is correct 730 731 enum : signed char { not_installed = -1, // in construction, only the owner doing the construction is 732 // allowed to advance state 733 in_use = 0, // executable nmethod 734 not_entrant = 1 // marked for deoptimization but activations may still exist 735 }; 736 737 // flag accessing and manipulation 738 bool is_not_installed() const { return _state == not_installed; } 739 bool is_in_use() const { return _state <= in_use; } 740 bool is_not_entrant() const { return _state == not_entrant; } 741 int get_state() const { return _state; } 742 743 void clear_unloading_state(); 744 // Heuristically deduce an nmethod isn't worth keeping around 745 bool is_cold(); 746 bool is_unloading(); 747 void do_unloading(bool unloading_occurred); 748 749 void inc_method_profiling_count(); 750 uint64_t method_profiling_count(); 751 752 bool make_in_use() { 753 return try_transition(in_use); 754 } 755 // Make the nmethod non entrant. The nmethod will continue to be 756 // alive. It is used when an uncommon trap happens. Returns true 757 // if this thread changed the state of the nmethod or false if 758 // another thread performed the transition. 759 bool make_not_entrant(InvalidationReason invalidation_reason, bool keep_aot_entry = false); 760 bool make_not_used() { return make_not_entrant(InvalidationReason::NOT_USED, true /* keep AOT entry */); } 761 762 bool is_marked_for_deoptimization() const { return deoptimization_status() != not_marked; } 763 bool has_been_deoptimized() const { return deoptimization_status() == deoptimize_done; } 764 void set_deoptimized_done(); 765 766 bool update_recompile_counts() const { 767 // Update recompile counts when either the update is explicitly requested (deoptimize) 768 // or the nmethod is not marked for deoptimization at all (not_marked). 769 // The latter happens during uncommon traps when deoptimized nmethod is made not entrant. 770 DeoptimizationStatus status = deoptimization_status(); 771 return status != deoptimize_noupdate && status != deoptimize_done; 772 } 773 774 // tells whether frames described by this nmethod can be deoptimized 775 // note: native wrappers cannot be deoptimized. 776 bool can_be_deoptimized() const { return is_java_method(); } 777 778 bool has_dependencies() { return dependencies_size() != 0; } 779 void print_dependencies_on(outputStream* out) PRODUCT_RETURN; 780 void flush_dependencies(); 781 782 template<typename T> 783 T* gc_data() const { return reinterpret_cast<T*>(_gc_data); } 784 template<typename T> 785 void set_gc_data(T* gc_data) { _gc_data = reinterpret_cast<void*>(gc_data); } 786 787 bool has_unsafe_access() const { return _has_unsafe_access; } 788 void set_has_unsafe_access(bool z) { _has_unsafe_access = z; } 789 790 bool has_monitors() const { return _has_monitors; } 791 void set_has_monitors(bool z) { _has_monitors = z; } 792 793 bool has_scoped_access() const { return _has_scoped_access; } 794 void set_has_scoped_access(bool z) { _has_scoped_access = z; } 795 796 bool has_method_handle_invokes() const { return _has_method_handle_invokes; } 797 void set_has_method_handle_invokes(bool z) { _has_method_handle_invokes = z; } 798 799 bool has_wide_vectors() const { return _has_wide_vectors; } 800 void set_has_wide_vectors(bool z) { _has_wide_vectors = z; } 801 802 bool has_clinit_barriers() const { return _has_clinit_barriers; } 803 void set_has_clinit_barriers(bool z) { _has_clinit_barriers = z; } 804 805 bool preloaded() const { return _preloaded; } 806 void set_preloaded(bool z) { _preloaded = z; } 807 808 bool has_flushed_dependencies() const { return _has_flushed_dependencies; } 809 void set_has_flushed_dependencies(bool z) { 810 assert(!has_flushed_dependencies(), "should only happen once"); 811 _has_flushed_dependencies = z; 812 } 813 814 bool is_unlinked() const { return _is_unlinked; } 815 void set_is_unlinked() { 816 assert(!_is_unlinked, "already unlinked"); 817 _is_unlinked = true; 818 } 819 820 bool used() const { return _used; } 821 void set_used() { _used = true; } 822 823 bool is_aot() const { return _aot_code_entry != nullptr; } 824 void set_aot_code_entry(AOTCodeEntry* entry) { _aot_code_entry = entry; } 825 AOTCodeEntry* aot_code_entry() const { return _aot_code_entry; } 826 827 // Support for oops in scopes and relocs: 828 // Note: index 0 is reserved for null. 829 oop oop_at(int index) const; 830 oop oop_at_phantom(int index) const; // phantom reference 831 oop* oop_addr_at(int index) const { // for GC 832 // relocation indexes are biased by 1 (because 0 is reserved) 833 assert(index > 0 && index <= oops_count(), "must be a valid non-zero index"); 834 return &oops_begin()[index - 1]; 835 } 836 837 // Support for meta data in scopes and relocs: 838 // Note: index 0 is reserved for null. 839 Metadata* metadata_at(int index) const { return index == 0 ? nullptr: *metadata_addr_at(index); } 840 Metadata** metadata_addr_at(int index) const { // for GC 841 // relocation indexes are biased by 1 (because 0 is reserved) 842 assert(index > 0 && index <= metadata_count(), "must be a valid non-zero index"); 843 return &metadata_begin()[index - 1]; 844 } 845 846 void copy_values(GrowableArray<Handle>* array); 847 void copy_values(GrowableArray<jobject>* oops); 848 void copy_values(GrowableArray<Metadata*>* metadata); 849 void copy_values(GrowableArray<address>* metadata) {} // Nothing to do 850 851 // Relocation support 852 private: 853 void fix_oop_relocations(address begin, address end, bool initialize_immediates); 854 inline void initialize_immediate_oop(oop* dest, jobject handle); 855 856 protected: 857 address oops_reloc_begin() const; 858 859 public: 860 void fix_oop_relocations(address begin, address end) { fix_oop_relocations(begin, end, false); } 861 void fix_oop_relocations() { fix_oop_relocations(nullptr, nullptr, false); } 862 863 void create_reloc_immediates_list(JavaThread* thread, GrowableArray<Handle>& oop_list, GrowableArray<Metadata*>& metadata_list); 864 865 bool is_at_poll_return(address pc); 866 bool is_at_poll_or_poll_return(address pc); 867 868 protected: 869 // Exception cache support 870 // Note: _exception_cache may be read and cleaned concurrently. 871 ExceptionCache* exception_cache() const { return _exception_cache; } 872 ExceptionCache* exception_cache_acquire() const; 873 874 public: 875 address handler_for_exception_and_pc(Handle exception, address pc); 876 void add_handler_for_exception_and_pc(Handle exception, address pc, address handler); 877 void clean_exception_cache(); 878 879 void add_exception_cache_entry(ExceptionCache* new_entry); 880 ExceptionCache* exception_cache_entry_for_exception(Handle exception); 881 882 883 // MethodHandle 884 bool is_method_handle_return(address return_pc); 885 // Deopt 886 // Return true is the PC is one would expect if the frame is being deopted. 887 inline bool is_deopt_pc(address pc); 888 inline bool is_deopt_mh_entry(address pc); 889 inline bool is_deopt_entry(address pc); 890 891 // Accessor/mutator for the original pc of a frame before a frame was deopted. 892 address get_original_pc(const frame* fr) { return *orig_pc_addr(fr); } 893 void set_original_pc(const frame* fr, address pc) { *orig_pc_addr(fr) = pc; } 894 895 const char* state() const; 896 897 bool inlinecache_check_contains(address addr) const { 898 return (addr >= code_begin() && addr < verified_entry_point()); 899 } 900 901 void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f); 902 903 // implicit exceptions support 904 address continuation_for_implicit_div0_exception(address pc) { return continuation_for_implicit_exception(pc, true); } 905 address continuation_for_implicit_null_exception(address pc) { return continuation_for_implicit_exception(pc, false); } 906 907 // Inline cache support for class unloading and nmethod unloading 908 private: 909 void cleanup_inline_caches_impl(bool unloading_occurred, bool clean_all); 910 911 address continuation_for_implicit_exception(address pc, bool for_div0_check); 912 913 public: 914 // Serial version used by whitebox test 915 void cleanup_inline_caches_whitebox(); 916 917 void clear_inline_caches(); 918 919 // Execute nmethod barrier code, as if entering through nmethod call. 920 void run_nmethod_entry_barrier(); 921 922 void verify_oop_relocations(); 923 924 bool has_evol_metadata(); 925 926 Method* attached_method(address call_pc); 927 Method* attached_method_before_pc(address pc); 928 929 // GC unloading support 930 // Cleans unloaded klasses and unloaded nmethods in inline caches 931 932 void unload_nmethod_caches(bool class_unloading_occurred); 933 934 void unlink_from_method(); 935 936 // On-stack replacement support 937 int osr_entry_bci() const { assert(is_osr_method(), "wrong kind of nmethod"); return _entry_bci; } 938 address osr_entry() const { assert(is_osr_method(), "wrong kind of nmethod"); return _osr_entry_point; } 939 nmethod* osr_link() const { return _osr_link; } 940 void set_osr_link(nmethod *n) { _osr_link = n; } 941 void invalidate_osr_method(); 942 943 int num_stack_arg_slots(bool rounded = true) const { 944 return rounded ? align_up(_num_stack_arg_slots, 2) : _num_stack_arg_slots; 945 } 946 947 // Verify calls to dead methods have been cleaned. 948 void verify_clean_inline_caches(); 949 950 // Unlink this nmethod from the system 951 void unlink(); 952 953 // Deallocate this nmethod - called by the GC 954 void purge(bool unregister_nmethod); 955 956 // See comment at definition of _last_seen_on_stack 957 void mark_as_maybe_on_stack(); 958 bool is_maybe_on_stack(); 959 960 // Evolution support. We make old (discarded) compiled methods point to new Method*s. 961 void set_method(Method* method) { _method = method; } 962 963 #if INCLUDE_JVMCI 964 // Gets the JVMCI name of this nmethod. 965 const char* jvmci_name(); 966 967 // Records the pending failed speculation in the 968 // JVMCI speculation log associated with this nmethod. 969 void update_speculation(JavaThread* thread); 970 971 // Gets the data specific to a JVMCI compiled method. 972 // This returns a non-nullptr value iff this nmethod was 973 // compiled by the JVMCI compiler. 974 JVMCINMethodData* jvmci_nmethod_data() const { 975 return jvmci_data_size() == 0 ? nullptr : (JVMCINMethodData*) jvmci_data_begin(); 976 } 977 978 // Returns true if the runtime should NOT collect deoptimization profile for a JVMCI 979 // compiled method 980 bool jvmci_skip_profile_deopt() const; 981 #endif 982 983 void oops_do(OopClosure* f); 984 985 // All-in-one claiming of nmethods: returns true if the caller successfully claimed that 986 // nmethod. 987 bool oops_do_try_claim(); 988 989 // Loom support for following nmethods on the stack 990 void follow_nmethod(OopIterateClosure* cl); 991 992 // Class containing callbacks for the oops_do_process_weak/strong() methods 993 // below. 994 class OopsDoProcessor { 995 public: 996 // Process the oops of the given nmethod based on whether it has been called 997 // in a weak or strong processing context, i.e. apply either weak or strong 998 // work on it. 999 virtual void do_regular_processing(nmethod* nm) = 0; 1000 // Assuming that the oops of the given nmethod has already been its weak 1001 // processing applied, apply the remaining strong processing part. 1002 virtual void do_remaining_strong_processing(nmethod* nm) = 0; 1003 }; 1004 1005 // The following two methods do the work corresponding to weak/strong nmethod 1006 // processing. 1007 void oops_do_process_weak(OopsDoProcessor* p); 1008 void oops_do_process_strong(OopsDoProcessor* p); 1009 1010 static void oops_do_marking_prologue(); 1011 static void oops_do_marking_epilogue(); 1012 1013 private: 1014 ScopeDesc* scope_desc_in(address begin, address end); 1015 1016 address* orig_pc_addr(const frame* fr); 1017 1018 // used by jvmti to track if the load events has been reported 1019 bool load_reported() const { return _load_reported; } 1020 void set_load_reported() { _load_reported = true; } 1021 1022 public: 1023 // ScopeDesc retrieval operation 1024 PcDesc* pc_desc_at(address pc) { return find_pc_desc(pc, false); } 1025 // pc_desc_near returns the first PcDesc at or after the given pc. 1026 PcDesc* pc_desc_near(address pc) { return find_pc_desc(pc, true); } 1027 1028 // ScopeDesc for an instruction 1029 ScopeDesc* scope_desc_at(address pc); 1030 ScopeDesc* scope_desc_near(address pc); 1031 1032 // copying of debugging information 1033 void copy_scopes_pcs(PcDesc* pcs, int count); 1034 void copy_scopes_data(address buffer, int size); 1035 1036 // Post successful compilation 1037 void post_compiled_method(CompileTask* task); 1038 1039 // jvmti support: 1040 void post_compiled_method_load_event(JvmtiThreadState* state = nullptr); 1041 1042 // verify operations 1043 void verify(); 1044 void verify_scopes(); 1045 void verify_interrupt_point(address interrupt_point, bool is_inline_cache); 1046 1047 // Disassemble this nmethod with additional debug information, e.g. information about blocks. 1048 void decode2(outputStream* st) const; 1049 void print_constant_pool(outputStream* st); 1050 1051 // Avoid hiding of parent's 'decode(outputStream*)' method. 1052 void decode(outputStream* st) const { decode2(st); } // just delegate here. 1053 1054 // printing support 1055 void print_on_impl(outputStream* st) const; 1056 void print_code(); 1057 void print_value_on_impl(outputStream* st) const; 1058 1059 #if defined(SUPPORT_DATA_STRUCTS) 1060 // print output in opt build for disassembler library 1061 void print_relocations_on(outputStream* st) PRODUCT_RETURN; 1062 void print_pcs_on(outputStream* st); 1063 void print_scopes() { print_scopes_on(tty); } 1064 void print_scopes_on(outputStream* st) PRODUCT_RETURN; 1065 void print_handler_table(); 1066 void print_nul_chk_table(); 1067 void print_recorded_oop(int log_n, int index); 1068 void print_recorded_oops(); 1069 void print_recorded_metadata(); 1070 1071 void print_oops(outputStream* st); // oops from the underlying CodeBlob. 1072 void print_metadata(outputStream* st); // metadata in metadata pool. 1073 #else 1074 void print_pcs_on(outputStream* st) { return; } 1075 #endif 1076 1077 void print_calls(outputStream* st) PRODUCT_RETURN; 1078 static void print_statistics() PRODUCT_RETURN; 1079 1080 void maybe_print_nmethod(const DirectiveSet* directive); 1081 void print_nmethod(bool print_code); 1082 1083 void print_on_with_msg(outputStream* st, const char* msg) const; 1084 1085 // Logging 1086 void log_identity(xmlStream* log) const; 1087 void log_new_nmethod() const; 1088 void log_state_change(InvalidationReason invalidation_reason) const; 1089 1090 // Prints block-level comments, including nmethod specific block labels: 1091 void print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels=true) const; 1092 const char* nmethod_section_label(address pos) const; 1093 1094 // returns whether this nmethod has code comments. 1095 bool has_code_comment(address begin, address end); 1096 // Prints a comment for one native instruction (reloc info, pc desc) 1097 void print_code_comment_on(outputStream* st, int column, address begin, address end); 1098 1099 // tells if this compiled method is dependent on the given changes, 1100 // and the changes have invalidated it 1101 bool check_dependency_on(DepChange& changes); 1102 1103 // Fast breakpoint support. Tells if this compiled method is 1104 // dependent on the given method. Returns true if this nmethod 1105 // corresponds to the given method as well. 1106 bool is_dependent_on_method(Method* dependee); 1107 1108 // JVMTI's GetLocalInstance() support 1109 ByteSize native_receiver_sp_offset() { 1110 assert(is_native_method(), "sanity"); 1111 return _native_receiver_sp_offset; 1112 } 1113 ByteSize native_basic_lock_sp_offset() { 1114 assert(is_native_method(), "sanity"); 1115 return _native_basic_lock_sp_offset; 1116 } 1117 1118 // support for code generation 1119 static ByteSize osr_entry_point_offset() { return byte_offset_of(nmethod, _osr_entry_point); } 1120 static ByteSize state_offset() { return byte_offset_of(nmethod, _state); } 1121 1122 void metadata_do(MetadataClosure* f); 1123 1124 address call_instruction_address(address pc) const; 1125 1126 void make_deoptimized(); 1127 void finalize_relocations(); 1128 1129 void prepare_for_archiving_impl(); 1130 1131 class Vptr : public CodeBlob::Vptr { 1132 void print_on(const CodeBlob* instance, outputStream* st) const override { 1133 ttyLocker ttyl; 1134 instance->as_nmethod()->print_on_impl(st); 1135 } 1136 void print_value_on(const CodeBlob* instance, outputStream* st) const override { 1137 instance->as_nmethod()->print_value_on_impl(st); 1138 } 1139 void prepare_for_archiving(CodeBlob* instance) const override { 1140 ((nmethod*)instance)->prepare_for_archiving_impl(); 1141 }; 1142 }; 1143 1144 static const Vptr _vpntr; 1145 }; 1146 1147 #endif // SHARE_CODE_NMETHOD_HPP