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 Atomic::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 , AOTCodeEntry* aot_code_entry 343 #if INCLUDE_JVMCI 344 , char* speculations = nullptr, 345 int speculations_len = 0, 346 JVMCINMethodData* jvmci_data = nullptr 347 #endif 348 ); 349 350 // helper methods 351 void* operator new(size_t size, int nmethod_size, int comp_level) throw(); 352 353 // For method handle intrinsics: Try MethodNonProfiled, MethodProfiled and NonNMethod. 354 // Attention: Only allow NonNMethod space for special nmethods which don't need to be 355 // findable by nmethod iterators! In particular, they must not contain oops! 356 void* operator new(size_t size, int nmethod_size, bool allow_NonNMethod_space) throw(); 357 358 const char* reloc_string_for(u_char* begin, u_char* end); 359 360 bool try_transition(signed char new_state); 361 362 // Returns true if this thread changed the state of the nmethod or 363 // false if another thread performed the transition. 364 bool make_entrant() { Unimplemented(); return false; } 365 void inc_decompile_count(); 366 367 // Inform external interfaces that a compiled method has been unloaded 368 void post_compiled_method_unload(); 369 370 PcDesc* find_pc_desc(address pc, bool approximate) { 371 if (_pc_desc_container == nullptr) return nullptr; // native method 372 return _pc_desc_container->find_pc_desc(pc, approximate, code_begin(), scopes_pcs_begin(), scopes_pcs_end()); 373 } 374 375 // STW two-phase nmethod root processing helpers. 376 // 377 // When determining liveness of a given nmethod to do code cache unloading, 378 // some collectors need to do different things depending on whether the nmethods 379 // need to absolutely be kept alive during root processing; "strong"ly reachable 380 // nmethods are known to be kept alive at root processing, but the liveness of 381 // "weak"ly reachable ones is to be determined later. 382 // 383 // We want to allow strong and weak processing of nmethods by different threads 384 // at the same time without heavy synchronization. Additional constraints are 385 // to make sure that every nmethod is processed a minimal amount of time, and 386 // nmethods themselves are always iterated at most once at a particular time. 387 // 388 // Note that strong processing work must be a superset of weak processing work 389 // for this code to work. 390 // 391 // We store state and claim information in the _oops_do_mark_link member, using 392 // the two LSBs for the state and the remaining upper bits for linking together 393 // nmethods that were already visited. 394 // The last element is self-looped, i.e. points to itself to avoid some special 395 // "end-of-list" sentinel value. 396 // 397 // _oops_do_mark_link special values: 398 // 399 // _oops_do_mark_link == nullptr: the nmethod has not been visited at all yet, i.e. 400 // is Unclaimed. 401 // 402 // For other values, its lowest two bits indicate the following states of the nmethod: 403 // 404 // weak_request (WR): the nmethod has been claimed by a thread for weak processing 405 // weak_done (WD): weak processing has been completed for this nmethod. 406 // strong_request (SR): the nmethod has been found to need strong processing while 407 // being weak processed. 408 // strong_done (SD): strong processing has been completed for this nmethod . 409 // 410 // The following shows the _only_ possible progressions of the _oops_do_mark_link 411 // pointer. 412 // 413 // Given 414 // N as the nmethod 415 // X the current next value of _oops_do_mark_link 416 // 417 // Unclaimed (C)-> N|WR (C)-> X|WD: the nmethod has been processed weakly by 418 // a single thread. 419 // Unclaimed (C)-> N|WR (C)-> X|WD (O)-> X|SD: after weak processing has been 420 // completed (as above) another thread found that the nmethod needs strong 421 // processing after all. 422 // Unclaimed (C)-> N|WR (O)-> N|SR (C)-> X|SD: during weak processing another 423 // thread finds that the nmethod needs strong processing, marks it as such and 424 // terminates. The original thread completes strong processing. 425 // Unclaimed (C)-> N|SD (C)-> X|SD: the nmethod has been processed strongly from 426 // the beginning by a single thread. 427 // 428 // "|" describes the concatenation of bits in _oops_do_mark_link. 429 // 430 // The diagram also describes the threads responsible for changing the nmethod to 431 // the next state by marking the _transition_ with (C) and (O), which mean "current" 432 // and "other" thread respectively. 433 // 434 435 // States used for claiming nmethods during root processing. 436 static const uint claim_weak_request_tag = 0; 437 static const uint claim_weak_done_tag = 1; 438 static const uint claim_strong_request_tag = 2; 439 static const uint claim_strong_done_tag = 3; 440 441 static oops_do_mark_link* mark_link(nmethod* nm, uint tag) { 442 assert(tag <= claim_strong_done_tag, "invalid tag %u", tag); 443 assert(is_aligned(nm, 4), "nmethod pointer must have zero lower two LSB"); 444 return (oops_do_mark_link*)(((uintptr_t)nm & ~0x3) | tag); 445 } 446 447 static uint extract_state(oops_do_mark_link* link) { 448 return (uint)((uintptr_t)link & 0x3); 449 } 450 451 static nmethod* extract_nmethod(oops_do_mark_link* link) { 452 return (nmethod*)((uintptr_t)link & ~0x3); 453 } 454 455 void oops_do_log_change(const char* state); 456 457 static bool oops_do_has_weak_request(oops_do_mark_link* next) { 458 return extract_state(next) == claim_weak_request_tag; 459 } 460 461 static bool oops_do_has_any_strong_state(oops_do_mark_link* next) { 462 return extract_state(next) >= claim_strong_request_tag; 463 } 464 465 // Attempt Unclaimed -> N|WR transition. Returns true if successful. 466 bool oops_do_try_claim_weak_request(); 467 468 // Attempt Unclaimed -> N|SD transition. Returns the current link. 469 oops_do_mark_link* oops_do_try_claim_strong_done(); 470 // Attempt N|WR -> X|WD transition. Returns nullptr if successful, X otherwise. 471 nmethod* oops_do_try_add_to_list_as_weak_done(); 472 473 // Attempt X|WD -> N|SR transition. Returns the current link. 474 oops_do_mark_link* oops_do_try_add_strong_request(oops_do_mark_link* next); 475 // Attempt X|WD -> X|SD transition. Returns true if successful. 476 bool oops_do_try_claim_weak_done_as_strong_done(oops_do_mark_link* next); 477 478 // Do the N|SD -> X|SD transition. 479 void oops_do_add_to_list_as_strong_done(); 480 481 // Sets this nmethod as strongly claimed (as part of N|SD -> X|SD and N|SR -> X|SD 482 // transitions). 483 void oops_do_set_strong_done(nmethod* old_head); 484 485 void record_nmethod_dependency(); 486 487 nmethod* restore(address code_cache_buffer, 488 const methodHandle& method, 489 int compile_id, 490 address reloc_data, 491 GrowableArray<Handle>& oop_list, 492 GrowableArray<Metadata*>& metadata_list, 493 ImmutableOopMapSet* oop_maps, 494 address immutable_data, 495 GrowableArray<Handle>& reloc_imm_oop_list, 496 GrowableArray<Metadata*>& reloc_imm_metadata_list, 497 AOTCodeReader* aot_code_reader); 498 499 public: 500 // create nmethod using archived nmethod from AOT code cache 501 static nmethod* new_nmethod(nmethod* archived_nm, 502 const methodHandle& method, 503 AbstractCompiler* compiler, 504 int compile_id, 505 address reloc_data, 506 GrowableArray<Handle>& oop_list, 507 GrowableArray<Metadata*>& metadata_list, 508 ImmutableOopMapSet* oop_maps, 509 address immutable_data, 510 GrowableArray<Handle>& reloc_imm_oop_list, 511 GrowableArray<Metadata*>& reloc_imm_metadata_list, 512 AOTCodeReader* aot_code_reader); 513 514 // create nmethod with entry_bci 515 static nmethod* new_nmethod(const methodHandle& method, 516 int compile_id, 517 int entry_bci, 518 CodeOffsets* offsets, 519 int orig_pc_offset, 520 DebugInformationRecorder* recorder, 521 Dependencies* dependencies, 522 CodeBuffer *code_buffer, 523 int frame_size, 524 OopMapSet* oop_maps, 525 ExceptionHandlerTable* handler_table, 526 ImplicitExceptionTable* nul_chk_table, 527 AbstractCompiler* compiler, 528 CompLevel comp_level 529 , AOTCodeEntry* aot_code_entry 530 #if INCLUDE_JVMCI 531 , char* speculations = nullptr, 532 int speculations_len = 0, 533 JVMCINMethodData* jvmci_data = nullptr 534 #endif 535 ); 536 537 static nmethod* new_native_nmethod(const methodHandle& method, 538 int compile_id, 539 CodeBuffer *code_buffer, 540 int vep_offset, 541 int frame_complete, 542 int frame_size, 543 ByteSize receiver_sp_offset, 544 ByteSize basic_lock_sp_offset, 545 OopMapSet* oop_maps, 546 int exception_handler = -1); 547 548 Method* method () const { return _method; } 549 uint16_t entry_bci () const { return _entry_bci; } 550 bool is_native_method() const { return _method != nullptr && _method->is_native(); } 551 bool is_java_method () const { return _method != nullptr && !_method->is_native(); } 552 bool is_osr_method () const { return _entry_bci != InvocationEntryBci; } 553 554 // Compiler task identification. Note that all OSR methods 555 // are numbered in an independent sequence if CICountOSR is true, 556 // and native method wrappers are also numbered independently if 557 // CICountNative is true. 558 int compile_id() const { return _compile_id; } 559 const char* compile_kind() const; 560 561 inline bool is_compiled_by_c1 () const { return _compiler_type == compiler_c1; } 562 inline bool is_compiled_by_c2 () const { return _compiler_type == compiler_c2; } 563 inline bool is_compiled_by_jvmci() const { return _compiler_type == compiler_jvmci; } 564 CompilerType compiler_type () const { return _compiler_type; } 565 const char* compiler_name () const; 566 567 // boundaries for different parts 568 address consts_begin () const { return content_begin(); } 569 address consts_end () const { return code_begin() ; } 570 address insts_begin () const { return code_begin() ; } 571 address insts_end () const { return header_begin() + _stub_offset ; } 572 address stub_begin () const { return header_begin() + _stub_offset ; } 573 address stub_end () const { return code_end() ; } 574 address exception_begin () const { return header_begin() + _exception_offset ; } 575 address deopt_handler_begin () const { return header_begin() + _deopt_handler_offset ; } 576 address deopt_mh_handler_begin() const { return _deopt_mh_handler_offset != -1 ? (header_begin() + _deopt_mh_handler_offset) : nullptr; } 577 address unwind_handler_begin () const { return _unwind_handler_offset != -1 ? (insts_end() - _unwind_handler_offset) : nullptr; } 578 oop* oops_begin () const { return (oop*) data_begin(); } 579 oop* oops_end () const { return (oop*) data_end(); } 580 581 // mutable data 582 Metadata** metadata_begin () const { return (Metadata**) (mutable_data_begin() + _relocation_size); } 583 #if INCLUDE_JVMCI 584 Metadata** metadata_end () const { return (Metadata**) (mutable_data_begin() + _relocation_size + _metadata_size); } 585 address jvmci_data_begin () const { return mutable_data_begin() + _relocation_size + _metadata_size; } 586 address jvmci_data_end () const { return mutable_data_end(); } 587 #else 588 Metadata** metadata_end () const { return (Metadata**) mutable_data_end(); } 589 #endif 590 591 // immutable data 592 void set_immutable_data(address data) { _immutable_data = data; } 593 address immutable_data_begin () const { return _immutable_data; } 594 address immutable_data_end () const { return _immutable_data + _immutable_data_size ; } 595 address dependencies_begin () const { return _immutable_data; } 596 address dependencies_end () const { return _immutable_data + _nul_chk_table_offset; } 597 address nul_chk_table_begin () const { return _immutable_data + _nul_chk_table_offset; } 598 address nul_chk_table_end () const { return _immutable_data + _handler_table_offset; } 599 address handler_table_begin () const { return _immutable_data + _handler_table_offset; } 600 address handler_table_end () const { return _immutable_data + _scopes_pcs_offset ; } 601 PcDesc* scopes_pcs_begin () const { return (PcDesc*)(_immutable_data + _scopes_pcs_offset) ; } 602 PcDesc* scopes_pcs_end () const { return (PcDesc*)(_immutable_data + _scopes_data_offset) ; } 603 address scopes_data_begin () const { return _immutable_data + _scopes_data_offset ; } 604 605 #if INCLUDE_JVMCI 606 address scopes_data_end () const { return _immutable_data + _speculations_offset ; } 607 address speculations_begin () const { return _immutable_data + _speculations_offset ; } 608 address speculations_end () const { return immutable_data_end(); } 609 #else 610 address scopes_data_end () const { return immutable_data_end(); } 611 #endif 612 613 // Sizes 614 int immutable_data_size() const { return _immutable_data_size; } 615 int consts_size () const { return int( consts_end () - consts_begin ()); } 616 int insts_size () const { return int( insts_end () - insts_begin ()); } 617 int stub_size () const { return int( stub_end () - stub_begin ()); } 618 int oops_size () const { return int((address) oops_end () - (address) oops_begin ()); } 619 int metadata_size () const { return int((address) metadata_end () - (address) metadata_begin ()); } 620 int scopes_data_size () const { return int( scopes_data_end () - scopes_data_begin ()); } 621 int scopes_pcs_size () const { return int((intptr_t)scopes_pcs_end () - (intptr_t)scopes_pcs_begin ()); } 622 int dependencies_size () const { return int( dependencies_end () - dependencies_begin ()); } 623 int handler_table_size () const { return int( handler_table_end() - handler_table_begin()); } 624 int nul_chk_table_size () const { return int( nul_chk_table_end() - nul_chk_table_begin()); } 625 #if INCLUDE_JVMCI 626 int speculations_size () const { return int( speculations_end () - speculations_begin ()); } 627 int jvmci_data_size () const { return int( jvmci_data_end () - jvmci_data_begin ()); } 628 #endif 629 630 int oops_count() const { assert(oops_size() % oopSize == 0, ""); return (oops_size() / oopSize) + 1; } 631 int metadata_count() const { assert(metadata_size() % wordSize == 0, ""); return (metadata_size() / wordSize) + 1; } 632 633 int skipped_instructions_size () const { return _skipped_instructions_size; } 634 int total_size() const; 635 636 // Containment 637 bool consts_contains (address addr) const { return consts_begin () <= addr && addr < consts_end (); } 638 // Returns true if a given address is in the 'insts' section. The method 639 // insts_contains_inclusive() is end-inclusive. 640 bool insts_contains (address addr) const { return insts_begin () <= addr && addr < insts_end (); } 641 bool insts_contains_inclusive(address addr) const { return insts_begin () <= addr && addr <= insts_end (); } 642 bool stub_contains (address addr) const { return stub_begin () <= addr && addr < stub_end (); } 643 bool oops_contains (oop* addr) const { return oops_begin () <= addr && addr < oops_end (); } 644 bool metadata_contains (Metadata** addr) const { return metadata_begin () <= addr && addr < metadata_end (); } 645 bool scopes_data_contains (address addr) const { return scopes_data_begin () <= addr && addr < scopes_data_end (); } 646 bool scopes_pcs_contains (PcDesc* addr) const { return scopes_pcs_begin () <= addr && addr < scopes_pcs_end (); } 647 bool handler_table_contains (address addr) const { return handler_table_begin() <= addr && addr < handler_table_end(); } 648 bool nul_chk_table_contains (address addr) const { return nul_chk_table_begin() <= addr && addr < nul_chk_table_end(); } 649 650 // entry points 651 address entry_point() const { return code_begin() + _entry_offset; } // normal entry point 652 address verified_entry_point() const { return code_begin() + _verified_entry_offset; } // if klass is correct 653 654 enum : signed char { not_installed = -1, // in construction, only the owner doing the construction is 655 // allowed to advance state 656 in_use = 0, // executable nmethod 657 not_entrant = 1 // marked for deoptimization but activations may still exist 658 }; 659 660 // flag accessing and manipulation 661 bool is_not_installed() const { return _state == not_installed; } 662 bool is_in_use() const { return _state <= in_use; } 663 bool is_not_entrant() const { return _state == not_entrant; } 664 int get_state() const { return _state; } 665 666 void clear_unloading_state(); 667 // Heuristically deduce an nmethod isn't worth keeping around 668 bool is_cold(); 669 bool is_unloading(); 670 void do_unloading(bool unloading_occurred); 671 672 void inc_method_profiling_count(); 673 uint64_t method_profiling_count(); 674 675 bool make_in_use() { 676 return try_transition(in_use); 677 } 678 // Make the nmethod non entrant. The nmethod will continue to be 679 // alive. It is used when an uncommon trap happens. Returns true 680 // if this thread changed the state of the nmethod or false if 681 // another thread performed the transition. 682 bool make_not_entrant(const char* reason, bool make_not_entrant = true); 683 bool make_not_used() { return make_not_entrant("not used"); } 684 685 bool is_marked_for_deoptimization() const { return deoptimization_status() != not_marked; } 686 bool has_been_deoptimized() const { return deoptimization_status() == deoptimize_done; } 687 void set_deoptimized_done(); 688 689 bool update_recompile_counts() const { 690 // Update recompile counts when either the update is explicitly requested (deoptimize) 691 // or the nmethod is not marked for deoptimization at all (not_marked). 692 // The latter happens during uncommon traps when deoptimized nmethod is made not entrant. 693 DeoptimizationStatus status = deoptimization_status(); 694 return status != deoptimize_noupdate && status != deoptimize_done; 695 } 696 697 // tells whether frames described by this nmethod can be deoptimized 698 // note: native wrappers cannot be deoptimized. 699 bool can_be_deoptimized() const { return is_java_method(); } 700 701 bool has_dependencies() { return dependencies_size() != 0; } 702 void print_dependencies_on(outputStream* out) PRODUCT_RETURN; 703 void flush_dependencies(); 704 705 template<typename T> 706 T* gc_data() const { return reinterpret_cast<T*>(_gc_data); } 707 template<typename T> 708 void set_gc_data(T* gc_data) { _gc_data = reinterpret_cast<void*>(gc_data); } 709 710 bool has_unsafe_access() const { return _has_unsafe_access; } 711 void set_has_unsafe_access(bool z) { _has_unsafe_access = z; } 712 713 bool has_monitors() const { return _has_monitors; } 714 void set_has_monitors(bool z) { _has_monitors = z; } 715 716 bool has_scoped_access() const { return _has_scoped_access; } 717 void set_has_scoped_access(bool z) { _has_scoped_access = z; } 718 719 bool has_method_handle_invokes() const { return _has_method_handle_invokes; } 720 void set_has_method_handle_invokes(bool z) { _has_method_handle_invokes = z; } 721 722 bool has_wide_vectors() const { return _has_wide_vectors; } 723 void set_has_wide_vectors(bool z) { _has_wide_vectors = z; } 724 725 bool has_clinit_barriers() const { return _has_clinit_barriers; } 726 void set_has_clinit_barriers(bool z) { _has_clinit_barriers = z; } 727 728 bool preloaded() const { return _preloaded; } 729 void set_preloaded(bool z) { _preloaded = z; } 730 731 bool has_flushed_dependencies() const { return _has_flushed_dependencies; } 732 void set_has_flushed_dependencies(bool z) { 733 assert(!has_flushed_dependencies(), "should only happen once"); 734 _has_flushed_dependencies = z; 735 } 736 737 bool is_unlinked() const { return _is_unlinked; } 738 void set_is_unlinked() { 739 assert(!_is_unlinked, "already unlinked"); 740 _is_unlinked = true; 741 } 742 743 int comp_level() const { return _comp_level; } 744 745 // Support for oops in scopes and relocs: 746 // Note: index 0 is reserved for null. 747 oop oop_at(int index) const; 748 oop oop_at_phantom(int index) const; // phantom reference 749 oop* oop_addr_at(int index) const { // for GC 750 // relocation indexes are biased by 1 (because 0 is reserved) 751 assert(index > 0 && index <= oops_count(), "must be a valid non-zero index"); 752 return &oops_begin()[index - 1]; 753 } 754 755 // Support for meta data in scopes and relocs: 756 // Note: index 0 is reserved for null. 757 Metadata* metadata_at(int index) const { return index == 0 ? nullptr: *metadata_addr_at(index); } 758 Metadata** metadata_addr_at(int index) const { // for GC 759 // relocation indexes are biased by 1 (because 0 is reserved) 760 assert(index > 0 && index <= metadata_count(), "must be a valid non-zero index"); 761 return &metadata_begin()[index - 1]; 762 } 763 764 void copy_values(GrowableArray<Handle>* array); 765 void copy_values(GrowableArray<jobject>* oops); 766 void copy_values(GrowableArray<Metadata*>* metadata); 767 void copy_values(GrowableArray<address>* metadata) {} // Nothing to do 768 769 // Relocation support 770 private: 771 void fix_oop_relocations(address begin, address end, bool initialize_immediates); 772 inline void initialize_immediate_oop(oop* dest, jobject handle); 773 774 protected: 775 address oops_reloc_begin() const; 776 777 public: 778 void fix_oop_relocations(address begin, address end) { fix_oop_relocations(begin, end, false); } 779 void fix_oop_relocations() { fix_oop_relocations(nullptr, nullptr, false); } 780 781 void create_reloc_immediates_list(JavaThread* thread, GrowableArray<Handle>& oop_list, GrowableArray<Metadata*>& metadata_list); 782 783 bool is_at_poll_return(address pc); 784 bool is_at_poll_or_poll_return(address pc); 785 786 protected: 787 // Exception cache support 788 // Note: _exception_cache may be read and cleaned concurrently. 789 ExceptionCache* exception_cache() const { return _exception_cache; } 790 ExceptionCache* exception_cache_acquire() const; 791 792 public: 793 address handler_for_exception_and_pc(Handle exception, address pc); 794 void add_handler_for_exception_and_pc(Handle exception, address pc, address handler); 795 void clean_exception_cache(); 796 797 void add_exception_cache_entry(ExceptionCache* new_entry); 798 ExceptionCache* exception_cache_entry_for_exception(Handle exception); 799 800 801 // MethodHandle 802 bool is_method_handle_return(address return_pc); 803 // Deopt 804 // Return true is the PC is one would expect if the frame is being deopted. 805 inline bool is_deopt_pc(address pc); 806 inline bool is_deopt_mh_entry(address pc); 807 inline bool is_deopt_entry(address pc); 808 809 // Accessor/mutator for the original pc of a frame before a frame was deopted. 810 address get_original_pc(const frame* fr) { return *orig_pc_addr(fr); } 811 void set_original_pc(const frame* fr, address pc) { *orig_pc_addr(fr) = pc; } 812 813 const char* state() const; 814 815 bool inlinecache_check_contains(address addr) const { 816 return (addr >= code_begin() && addr < verified_entry_point()); 817 } 818 819 void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f); 820 821 // implicit exceptions support 822 address continuation_for_implicit_div0_exception(address pc) { return continuation_for_implicit_exception(pc, true); } 823 address continuation_for_implicit_null_exception(address pc) { return continuation_for_implicit_exception(pc, false); } 824 825 // Inline cache support for class unloading and nmethod unloading 826 private: 827 void cleanup_inline_caches_impl(bool unloading_occurred, bool clean_all); 828 829 address continuation_for_implicit_exception(address pc, bool for_div0_check); 830 831 public: 832 // Serial version used by whitebox test 833 void cleanup_inline_caches_whitebox(); 834 835 void clear_inline_caches(); 836 837 // Execute nmethod barrier code, as if entering through nmethod call. 838 void run_nmethod_entry_barrier(); 839 840 void verify_oop_relocations(); 841 842 bool has_evol_metadata(); 843 844 Method* attached_method(address call_pc); 845 Method* attached_method_before_pc(address pc); 846 847 // GC unloading support 848 // Cleans unloaded klasses and unloaded nmethods in inline caches 849 850 void unload_nmethod_caches(bool class_unloading_occurred); 851 852 void unlink_from_method(); 853 854 // On-stack replacement support 855 int osr_entry_bci() const { assert(is_osr_method(), "wrong kind of nmethod"); return _entry_bci; } 856 address osr_entry() const { assert(is_osr_method(), "wrong kind of nmethod"); return _osr_entry_point; } 857 nmethod* osr_link() const { return _osr_link; } 858 void set_osr_link(nmethod *n) { _osr_link = n; } 859 void invalidate_osr_method(); 860 861 int num_stack_arg_slots(bool rounded = true) const { 862 return rounded ? align_up(_num_stack_arg_slots, 2) : _num_stack_arg_slots; 863 } 864 865 // Verify calls to dead methods have been cleaned. 866 void verify_clean_inline_caches(); 867 868 // Unlink this nmethod from the system 869 void unlink(); 870 871 // Deallocate this nmethod - called by the GC 872 void purge(bool unregister_nmethod); 873 874 // See comment at definition of _last_seen_on_stack 875 void mark_as_maybe_on_stack(); 876 bool is_maybe_on_stack(); 877 878 // Evolution support. We make old (discarded) compiled methods point to new Method*s. 879 void set_method(Method* method) { _method = method; } 880 881 #if INCLUDE_JVMCI 882 // Gets the JVMCI name of this nmethod. 883 const char* jvmci_name(); 884 885 // Records the pending failed speculation in the 886 // JVMCI speculation log associated with this nmethod. 887 void update_speculation(JavaThread* thread); 888 889 // Gets the data specific to a JVMCI compiled method. 890 // This returns a non-nullptr value iff this nmethod was 891 // compiled by the JVMCI compiler. 892 JVMCINMethodData* jvmci_nmethod_data() const { 893 return jvmci_data_size() == 0 ? nullptr : (JVMCINMethodData*) jvmci_data_begin(); 894 } 895 #endif 896 897 void oops_do(OopClosure* f) { oops_do(f, false); } 898 void oops_do(OopClosure* f, bool allow_dead); 899 900 // All-in-one claiming of nmethods: returns true if the caller successfully claimed that 901 // nmethod. 902 bool oops_do_try_claim(); 903 904 // Loom support for following nmethods on the stack 905 void follow_nmethod(OopIterateClosure* cl); 906 907 // Class containing callbacks for the oops_do_process_weak/strong() methods 908 // below. 909 class OopsDoProcessor { 910 public: 911 // Process the oops of the given nmethod based on whether it has been called 912 // in a weak or strong processing context, i.e. apply either weak or strong 913 // work on it. 914 virtual void do_regular_processing(nmethod* nm) = 0; 915 // Assuming that the oops of the given nmethod has already been its weak 916 // processing applied, apply the remaining strong processing part. 917 virtual void do_remaining_strong_processing(nmethod* nm) = 0; 918 }; 919 920 // The following two methods do the work corresponding to weak/strong nmethod 921 // processing. 922 void oops_do_process_weak(OopsDoProcessor* p); 923 void oops_do_process_strong(OopsDoProcessor* p); 924 925 static void oops_do_marking_prologue(); 926 static void oops_do_marking_epilogue(); 927 928 private: 929 ScopeDesc* scope_desc_in(address begin, address end); 930 931 address* orig_pc_addr(const frame* fr); 932 933 // used by jvmti to track if the load events has been reported 934 bool load_reported() const { return _load_reported; } 935 void set_load_reported() { _load_reported = true; } 936 937 public: 938 // ScopeDesc retrieval operation 939 PcDesc* pc_desc_at(address pc) { return find_pc_desc(pc, false); } 940 // pc_desc_near returns the first PcDesc at or after the given pc. 941 PcDesc* pc_desc_near(address pc) { return find_pc_desc(pc, true); } 942 943 // ScopeDesc for an instruction 944 ScopeDesc* scope_desc_at(address pc); 945 ScopeDesc* scope_desc_near(address pc); 946 947 // copying of debugging information 948 void copy_scopes_pcs(PcDesc* pcs, int count); 949 void copy_scopes_data(address buffer, int size); 950 951 int orig_pc_offset() { return _orig_pc_offset; } 952 953 AOTCodeEntry* aot_code_entry() const { return _aot_code_entry; } 954 bool is_aot() const { return aot_code_entry() != nullptr; } 955 void set_aot_code_entry(AOTCodeEntry* entry) { _aot_code_entry = entry; } 956 957 bool used() const { return _used; } 958 void set_used() { _used = true; } 959 960 // Post successful compilation 961 void post_compiled_method(CompileTask* task); 962 963 // jvmti support: 964 void post_compiled_method_load_event(JvmtiThreadState* state = nullptr); 965 966 // verify operations 967 void verify(); 968 void verify_scopes(); 969 void verify_interrupt_point(address interrupt_point, bool is_inline_cache); 970 971 // Disassemble this nmethod with additional debug information, e.g. information about blocks. 972 void decode2(outputStream* st) const; 973 void print_constant_pool(outputStream* st); 974 975 // Avoid hiding of parent's 'decode(outputStream*)' method. 976 void decode(outputStream* st) const { decode2(st); } // just delegate here. 977 978 // printing support 979 void print_on_impl(outputStream* st) const; 980 void print_code(); 981 void print_value_on_impl(outputStream* st) const; 982 983 #if defined(SUPPORT_DATA_STRUCTS) 984 // print output in opt build for disassembler library 985 void print_relocations_on(outputStream* st) PRODUCT_RETURN; 986 void print_pcs_on(outputStream* st); 987 void print_scopes() { print_scopes_on(tty); } 988 void print_scopes_on(outputStream* st) PRODUCT_RETURN; 989 void print_handler_table(); 990 void print_nul_chk_table(); 991 void print_recorded_oop(int log_n, int index); 992 void print_recorded_oops(); 993 void print_recorded_metadata(); 994 995 void print_oops(outputStream* st); // oops from the underlying CodeBlob. 996 void print_metadata(outputStream* st); // metadata in metadata pool. 997 #else 998 void print_pcs_on(outputStream* st) { return; } 999 #endif 1000 1001 void print_calls(outputStream* st) PRODUCT_RETURN; 1002 static void print_statistics() PRODUCT_RETURN; 1003 1004 void maybe_print_nmethod(const DirectiveSet* directive); 1005 void print_nmethod(bool print_code); 1006 1007 void print_on_with_msg(outputStream* st, const char* msg) const; 1008 1009 // Logging 1010 void log_identity(xmlStream* log) const; 1011 void log_new_nmethod() const; 1012 void log_state_change(const char* reason) const; 1013 1014 // Prints block-level comments, including nmethod specific block labels: 1015 void print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels=true) const; 1016 const char* nmethod_section_label(address pos) const; 1017 1018 // returns whether this nmethod has code comments. 1019 bool has_code_comment(address begin, address end); 1020 // Prints a comment for one native instruction (reloc info, pc desc) 1021 void print_code_comment_on(outputStream* st, int column, address begin, address end); 1022 1023 // tells if this compiled method is dependent on the given changes, 1024 // and the changes have invalidated it 1025 bool check_dependency_on(DepChange& changes); 1026 1027 // Fast breakpoint support. Tells if this compiled method is 1028 // dependent on the given method. Returns true if this nmethod 1029 // corresponds to the given method as well. 1030 bool is_dependent_on_method(Method* dependee); 1031 1032 // JVMTI's GetLocalInstance() support 1033 ByteSize native_receiver_sp_offset() { 1034 assert(is_native_method(), "sanity"); 1035 return _native_receiver_sp_offset; 1036 } 1037 ByteSize native_basic_lock_sp_offset() { 1038 assert(is_native_method(), "sanity"); 1039 return _native_basic_lock_sp_offset; 1040 } 1041 1042 // support for code generation 1043 static ByteSize osr_entry_point_offset() { return byte_offset_of(nmethod, _osr_entry_point); } 1044 static ByteSize state_offset() { return byte_offset_of(nmethod, _state); } 1045 1046 void metadata_do(MetadataClosure* f); 1047 1048 address call_instruction_address(address pc) const; 1049 1050 void make_deoptimized(); 1051 void finalize_relocations(); 1052 1053 void prepare_for_archiving_impl(); 1054 1055 class Vptr : public CodeBlob::Vptr { 1056 void print_on(const CodeBlob* instance, outputStream* st) const override { 1057 ttyLocker ttyl; 1058 instance->as_nmethod()->print_on_impl(st); 1059 } 1060 void print_value_on(const CodeBlob* instance, outputStream* st) const override { 1061 instance->as_nmethod()->print_value_on_impl(st); 1062 } 1063 void prepare_for_archiving(CodeBlob* instance) const override { 1064 ((nmethod*)instance)->prepare_for_archiving_impl(); 1065 }; 1066 }; 1067 1068 static const Vptr _vpntr; 1069 }; 1070 1071 #endif // SHARE_CODE_NMETHOD_HPP