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