1 /* 2 * Copyright (c) 2018, 2024, 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 #include "precompiled.hpp" 26 #include "classfile/javaClasses.inline.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "code/codeCache.inline.hpp" 29 #include "code/nmethod.inline.hpp" 30 #include "code/vmreg.inline.hpp" 31 #include "compiler/oopMap.inline.hpp" 32 #include "gc/shared/continuationGCSupport.inline.hpp" 33 #include "gc/shared/gc_globals.hpp" 34 #include "gc/shared/barrierSet.hpp" 35 #include "gc/shared/memAllocator.hpp" 36 #include "gc/shared/threadLocalAllocBuffer.inline.hpp" 37 #include "interpreter/interpreter.hpp" 38 #include "jfr/jfrEvents.hpp" 39 #include "logging/log.hpp" 40 #include "logging/logStream.hpp" 41 #include "oops/access.inline.hpp" 42 #include "oops/method.inline.hpp" 43 #include "oops/oopsHierarchy.hpp" 44 #include "oops/objArrayOop.inline.hpp" 45 #include "oops/stackChunkOop.inline.hpp" 46 #include "prims/jvmtiThreadState.hpp" 47 #include "runtime/arguments.hpp" 48 #include "runtime/continuation.hpp" 49 #include "runtime/continuationEntry.inline.hpp" 50 #include "runtime/continuationHelper.inline.hpp" 51 #include "runtime/continuationJavaClasses.inline.hpp" 52 #include "runtime/continuationWrapper.inline.hpp" 53 #include "runtime/frame.inline.hpp" 54 #include "runtime/interfaceSupport.inline.hpp" 55 #include "runtime/javaThread.inline.hpp" 56 #include "runtime/jniHandles.inline.hpp" 57 #include "runtime/keepStackGCProcessed.hpp" 58 #include "runtime/objectMonitor.inline.hpp" 59 #include "runtime/orderAccess.hpp" 60 #include "runtime/prefetch.inline.hpp" 61 #include "runtime/smallRegisterMap.inline.hpp" 62 #include "runtime/sharedRuntime.hpp" 63 #include "runtime/stackChunkFrameStream.inline.hpp" 64 #include "runtime/stackFrameStream.inline.hpp" 65 #include "runtime/stackOverflow.hpp" 66 #include "runtime/stackWatermarkSet.inline.hpp" 67 #include "utilities/debug.hpp" 68 #include "utilities/exceptions.hpp" 69 #include "utilities/macros.hpp" 70 #include "utilities/vmError.hpp" 71 #if INCLUDE_ZGC 72 #include "gc/z/zStackChunkGCData.inline.hpp" 73 #endif 74 75 #include <type_traits> 76 77 /* 78 * This file contains the implementation of continuation freezing (yield) and thawing (run). 79 * 80 * This code is very latency-critical and very hot. An ordinary and well-behaved server application 81 * would likely call these operations many thousands of times per second second, on every core. 82 * 83 * Freeze might be called every time the application performs any I/O operation, every time it 84 * acquires a j.u.c. lock, every time it takes a message from a queue, and thaw can be called 85 * multiple times in each of those cases, as it is called by the return barrier, which may be 86 * invoked on method return. 87 * 88 * The amortized budget for each of those two operations is ~100-150ns. That is why, for 89 * example, every effort is made to avoid Java-VM transitions as much as possible. 90 * 91 * On the fast path, all frames are known to be compiled, and the chunk requires no barriers 92 * and so frames simply copied, and the bottom-most one is patched. 93 * On the slow path, internal pointers in interpreted frames are de/relativized to/from offsets 94 * and absolute pointers, and barriers invoked. 95 */ 96 97 /************************************************ 98 99 Thread-stack layout on freeze/thaw. 100 See corresponding stack-chunk layout in instanceStackChunkKlass.hpp 101 102 +----------------------------+ 103 | . | 104 | . | 105 | . | 106 | carrier frames | 107 | | 108 |----------------------------| 109 | | 110 | Continuation.run | 111 | | 112 |============================| 113 | enterSpecial frame | 114 | pc | 115 | rbp | 116 | ----- | 117 ^ | int argsize | = ContinuationEntry 118 | | oopDesc* cont | 119 | | oopDesc* chunk | 120 | | ContinuationEntry* parent | 121 | | ... | 122 | |============================| <------ JavaThread::_cont_entry = entry->sp() 123 | | ? alignment word ? | 124 | |----------------------------| <--\ 125 | | | | 126 | | ? caller stack args ? | | argsize (might not be 2-word aligned) words 127 Address | | | | Caller is still in the chunk. 128 | |----------------------------| | 129 | | pc (? return barrier ?) | | This pc contains the return barrier when the bottom-most frame 130 | | rbp | | isn't the last one in the continuation. 131 | | | | 132 | | frame | | 133 | | | | 134 +----------------------------| \__ Continuation frames to be frozen/thawed 135 | | / 136 | frame | | 137 | | | 138 |----------------------------| | 139 | | | 140 | frame | | 141 | | | 142 |----------------------------| <--/ 143 | | 144 | doYield/safepoint stub | When preempting forcefully, we could have a safepoint stub 145 | | instead of a doYield stub 146 |============================| <- the sp passed to freeze 147 | | 148 | Native freeze/thaw frames | 149 | . | 150 | . | 151 | . | 152 +----------------------------+ 153 154 ************************************************/ 155 156 static const bool TEST_THAW_ONE_CHUNK_FRAME = false; // force thawing frames one-at-a-time for testing 157 158 #define CONT_JFR false // emit low-level JFR events that count slow/fast path for continuation performance debugging only 159 #if CONT_JFR 160 #define CONT_JFR_ONLY(code) code 161 #else 162 #define CONT_JFR_ONLY(code) 163 #endif 164 165 // TODO: See AbstractAssembler::generate_stack_overflow_check, 166 // Compile::bang_size_in_bytes(), m->as_SafePoint()->jvms()->interpreter_frame_size() 167 // when we stack-bang, we need to update a thread field with the lowest (farthest) bang point. 168 169 // Data invariants are defined by Continuation::debug_verify_continuation and Continuation::debug_verify_stack_chunk 170 171 // Used to just annotatate cold/hot branches 172 #define LIKELY(condition) (condition) 173 #define UNLIKELY(condition) (condition) 174 175 // debugging functions 176 #ifdef ASSERT 177 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue); // address p is readable and *(intptr_t*)p != errvalue 178 179 static void verify_continuation(oop continuation) { Continuation::debug_verify_continuation(continuation); } 180 181 static void do_deopt_after_thaw(JavaThread* thread); 182 static bool do_verify_after_thaw(JavaThread* thread, stackChunkOop chunk, outputStream* st); 183 static void log_frames(JavaThread* thread); 184 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st = tty); 185 186 #define assert_pfl(p, ...) \ 187 do { \ 188 if (!(p)) { \ 189 JavaThread* t = JavaThread::active(); \ 190 if (t->has_last_Java_frame()) { \ 191 tty->print_cr("assert(" #p ") failed:"); \ 192 t->print_frame_layout(); \ 193 } \ 194 } \ 195 vmassert(p, __VA_ARGS__); \ 196 } while(0) 197 198 #else 199 static void verify_continuation(oop continuation) { } 200 #define assert_pfl(p, ...) 201 #endif 202 203 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint); 204 template<typename ConfigT, bool preempt> static inline int freeze_internal(JavaThread* thread, intptr_t* const sp); 205 206 static inline int prepare_thaw_internal(JavaThread* thread, bool return_barrier); 207 template<typename ConfigT> static inline intptr_t* thaw_internal(JavaThread* thread, const Continuation::thaw_kind kind); 208 209 210 // Entry point to freeze. Transitions are handled manually 211 // Called from gen_continuation_yield() in sharedRuntime_<cpu>.cpp through Continuation::freeze_entry(); 212 template<typename ConfigT> 213 static JRT_BLOCK_ENTRY(int, freeze(JavaThread* current, intptr_t* sp)) 214 assert(sp == current->frame_anchor()->last_Java_sp(), ""); 215 216 if (current->raw_cont_fastpath() > current->last_continuation()->entry_sp() || current->raw_cont_fastpath() < sp) { 217 current->set_cont_fastpath(nullptr); 218 } 219 220 return ConfigT::freeze(current, sp); 221 JRT_END 222 223 JRT_LEAF(int, Continuation::prepare_thaw(JavaThread* thread, bool return_barrier)) 224 return prepare_thaw_internal(thread, return_barrier); 225 JRT_END 226 227 template<typename ConfigT> 228 static JRT_LEAF(intptr_t*, thaw(JavaThread* thread, int kind)) 229 // TODO: JRT_LEAF and NoHandleMark is problematic for JFR events. 230 // vFrameStreamCommon allocates Handles in RegisterMap for continuations. 231 // Also the preemption case with JVMTI events enabled might safepoint so 232 // undo the NoSafepointVerifier here and rely on handling by ContinuationWrapper. 233 // JRT_ENTRY instead? 234 ResetNoHandleMark rnhm; 235 debug_only(PauseNoSafepointVerifier pnsv(&__nsv);) 236 237 // we might modify the code cache via BarrierSetNMethod::nmethod_entry_barrier 238 MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); 239 return ConfigT::thaw(thread, (Continuation::thaw_kind)kind); 240 JRT_END 241 242 JVM_ENTRY(jint, CONT_isPinned0(JNIEnv* env, jobject cont_scope)) { 243 JavaThread* thread = JavaThread::thread_from_jni_environment(env); 244 return is_pinned0(thread, JNIHandles::resolve(cont_scope), false); 245 } 246 JVM_END 247 248 /////////// 249 250 enum class oop_kind { NARROW, WIDE }; 251 template <oop_kind oops, typename BarrierSetT> 252 class Config { 253 public: 254 typedef Config<oops, BarrierSetT> SelfT; 255 using OopT = std::conditional_t<oops == oop_kind::NARROW, narrowOop, oop>; 256 257 static int freeze(JavaThread* thread, intptr_t* const sp) { 258 return freeze_internal<SelfT, false>(thread, sp); 259 } 260 261 static int freeze_preempt(JavaThread* thread, intptr_t* const sp) { 262 return freeze_internal<SelfT, true>(thread, sp); 263 } 264 265 static intptr_t* thaw(JavaThread* thread, Continuation::thaw_kind kind) { 266 return thaw_internal<SelfT>(thread, kind); 267 } 268 }; 269 270 static bool stack_overflow_check(JavaThread* thread, size_t size, address sp) { 271 const size_t page_size = os::vm_page_size(); 272 if (size > page_size) { 273 if (sp - size < thread->stack_overflow_state()->shadow_zone_safe_limit()) { 274 return false; 275 } 276 } 277 return true; 278 } 279 280 #ifdef ASSERT 281 static oop get_continuation(JavaThread* thread) { 282 assert(thread != nullptr, ""); 283 assert(thread->threadObj() != nullptr, ""); 284 return java_lang_Thread::continuation(thread->threadObj()); 285 } 286 #endif // ASSERT 287 288 inline void clear_anchor(JavaThread* thread) { 289 thread->frame_anchor()->clear(); 290 } 291 292 static void set_anchor(JavaThread* thread, intptr_t* sp, address pc = nullptr) { 293 if (pc == nullptr) { 294 pc = ContinuationHelper::return_address_at( 295 sp - frame::sender_sp_ret_address_offset()); 296 } 297 assert(pc != nullptr, ""); 298 299 JavaFrameAnchor* anchor = thread->frame_anchor(); 300 anchor->set_last_Java_sp(sp); 301 anchor->set_last_Java_pc(pc); 302 ContinuationHelper::set_anchor_pd(anchor, sp); 303 304 assert(thread->has_last_Java_frame(), ""); 305 assert(thread->last_frame().cb() != nullptr, ""); 306 } 307 308 static void set_anchor_to_entry(JavaThread* thread, ContinuationEntry* entry) { 309 JavaFrameAnchor* anchor = thread->frame_anchor(); 310 anchor->set_last_Java_sp(entry->entry_sp()); 311 anchor->set_last_Java_pc(entry->entry_pc()); 312 ContinuationHelper::set_anchor_to_entry_pd(anchor, entry); 313 314 assert(thread->has_last_Java_frame(), ""); 315 assert(thread->last_frame().cb() != nullptr, ""); 316 } 317 318 #ifdef ASSERT 319 static int monitors_to_fix_on_stack(JavaThread* thread) { 320 ResourceMark rm(JavaThread::current()); 321 ContinuationEntry* ce = thread->last_continuation(); 322 RegisterMap map(thread, 323 RegisterMap::UpdateMap::include, 324 RegisterMap::ProcessFrames::include, 325 RegisterMap::WalkContinuation::include); 326 map.set_include_argument_oops(false); 327 ResourceHashtable<oopDesc*, bool> rhtable; 328 int monitor_count = 0; 329 for (frame f = thread->last_frame(); Continuation::is_frame_in_continuation(thread, f); f = f.sender(&map)) { 330 if (f.is_interpreted_frame()) { 331 frame abs = !f.is_heap_frame() ? f : map.stack_chunk()->derelativize(f); 332 monitor_count += ContinuationHelper::InterpretedFrame::monitors_to_fix(thread, abs, rhtable, map.stack_chunk()()); 333 } else if (f.is_compiled_frame()) { 334 monitor_count += ContinuationHelper::CompiledFrame::monitors_to_fix(thread, &map, f, rhtable); 335 } else if (f.is_native_frame()) { 336 monitor_count += ContinuationHelper::NativeFrame::monitors_to_fix(thread, f, rhtable); 337 } 338 } 339 return monitor_count; 340 } 341 #endif 342 343 #if CONT_JFR 344 class FreezeThawJfrInfo : public StackObj { 345 short _e_size; 346 short _e_num_interpreted_frames; 347 public: 348 349 FreezeThawJfrInfo() : _e_size(0), _e_num_interpreted_frames(0) {} 350 inline void record_interpreted_frame() { _e_num_interpreted_frames++; } 351 inline void record_size_copied(int size) { _e_size += size << LogBytesPerWord; } 352 template<typename Event> void post_jfr_event(Event *e, oop continuation, JavaThread* jt); 353 }; 354 355 template<typename Event> void FreezeThawJfrInfo::post_jfr_event(Event* e, oop continuation, JavaThread* jt) { 356 if (e->should_commit()) { 357 log_develop_trace(continuations)("JFR event: iframes: %d size: %d", _e_num_interpreted_frames, _e_size); 358 e->set_carrierThread(JFR_JVM_THREAD_ID(jt)); 359 e->set_continuationClass(continuation->klass()); 360 e->set_interpretedFrames(_e_num_interpreted_frames); 361 e->set_size(_e_size); 362 e->commit(); 363 } 364 } 365 #endif // CONT_JFR 366 367 /////////////// FREEZE //// 368 369 class FreezeBase : public StackObj { 370 protected: 371 JavaThread* const _thread; 372 ContinuationWrapper& _cont; 373 bool _barriers; // only set when we allocate a chunk 374 375 intptr_t* _bottom_address; 376 377 const bool _preempt; 378 // Used on preemption only 379 frame _last_frame; 380 oop _monitorenter_obj; 381 382 // Used to support freezing with held monitors 383 int _monitors_to_fix; 384 int _monitors_in_lockstack; 385 386 int _freeze_size; // total size of all frames plus metadata in words. 387 int _total_align_size; 388 389 intptr_t* _cont_stack_top; 390 intptr_t* _cont_stack_bottom; 391 392 CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;) 393 394 #ifdef ASSERT 395 intptr_t* _orig_chunk_sp; 396 int _fast_freeze_size; 397 bool _empty; 398 #endif 399 400 JvmtiSampledObjectAllocEventCollector* _jvmti_event_collector; 401 402 NOT_PRODUCT(int _frames;) 403 DEBUG_ONLY(intptr_t* _last_write;) 404 405 inline FreezeBase(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp, bool preempt); 406 407 public: 408 NOINLINE freeze_result freeze_slow(); 409 void freeze_fast_existing_chunk(); 410 411 CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; }) 412 void set_jvmti_event_collector(JvmtiSampledObjectAllocEventCollector* jsoaec) { _jvmti_event_collector = jsoaec; } 413 414 inline int size_if_fast_freeze_available(); 415 416 inline frame& last_frame() { return _last_frame; } 417 inline void set_last_frame() { _last_frame = _thread->last_frame(); } 418 419 #ifdef ASSERT 420 bool check_valid_fast_path(); 421 #endif 422 423 protected: 424 inline void init_rest(); 425 void throw_stack_overflow_on_humongous_chunk(); 426 427 // fast path 428 inline void copy_to_chunk(intptr_t* from, intptr_t* to, int size); 429 inline void unwind_frames(); 430 inline void patch_stack_pd(intptr_t* frame_sp, intptr_t* heap_sp); 431 432 // slow path 433 virtual stackChunkOop allocate_chunk_slow(size_t stack_size) = 0; 434 435 int cont_size() { return pointer_delta_as_int(_cont_stack_bottom, _cont_stack_top); } 436 437 private: 438 // slow path 439 frame freeze_start_frame(); 440 frame freeze_start_frame_on_preempt(); 441 NOINLINE freeze_result recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top); 442 inline frame freeze_start_frame_yield_stub(); 443 template<typename FKind> 444 inline freeze_result recurse_freeze_java_frame(const frame& f, frame& caller, int fsize, int argsize); 445 inline void before_freeze_java_frame(const frame& f, const frame& caller, int fsize, int argsize, bool is_bottom_frame); 446 inline void after_freeze_java_frame(const frame& hf, bool is_bottom_frame); 447 freeze_result finalize_freeze(const frame& callee, frame& caller, int argsize); 448 void patch(const frame& f, frame& hf, const frame& caller, bool is_bottom_frame); 449 NOINLINE freeze_result recurse_freeze_interpreted_frame(frame& f, frame& caller, int callee_argsize, bool callee_interpreted); 450 freeze_result recurse_freeze_compiled_frame(frame& f, frame& caller, int callee_argsize, bool callee_interpreted); 451 NOINLINE freeze_result recurse_freeze_stub_frame(frame& f, frame& caller); 452 NOINLINE void finish_freeze(const frame& f, const frame& top); 453 454 void fix_monitors_in_interpreted_frame(frame& f); 455 template <typename RegisterMapT> 456 void fix_monitors_in_compiled_frame(frame& f, RegisterMapT* map); 457 void fix_monitors_in_fast_path(); 458 459 inline bool stack_overflow(); 460 461 static frame sender(const frame& f) { return f.is_interpreted_frame() ? sender<ContinuationHelper::InterpretedFrame>(f) 462 : sender<ContinuationHelper::NonInterpretedUnknownFrame>(f); } 463 template<typename FKind> static inline frame sender(const frame& f); 464 template<typename FKind> frame new_heap_frame(frame& f, frame& caller); 465 inline void set_top_frame_metadata_pd(const frame& hf); 466 inline void patch_pd(frame& callee, const frame& caller); 467 void adjust_interpreted_frame_unextended_sp(frame& f); 468 static inline void prepare_freeze_interpreted_top_frame(const frame& f); 469 static inline void relativize_interpreted_frame_metadata(const frame& f, const frame& hf); 470 471 protected: 472 void freeze_fast_copy(stackChunkOop chunk, int chunk_start_sp CONT_JFR_ONLY(COMMA bool chunk_is_allocated)); 473 bool freeze_fast_new_chunk(stackChunkOop chunk); 474 475 #ifdef ASSERT 476 bool is_empty(stackChunkOop chunk) { 477 // during freeze, the chunk is in an intermediate state (after setting the chunk's argsize but before setting its 478 // ultimate sp) so we use this instead of stackChunkOopDesc::is_empty 479 return chunk->sp() >= chunk->stack_size() - chunk->argsize() - frame::metadata_words_at_top; 480 } 481 #endif 482 }; 483 484 template <typename ConfigT> 485 class Freeze : public FreezeBase { 486 private: 487 stackChunkOop allocate_chunk(size_t stack_size); 488 489 public: 490 inline Freeze(JavaThread* thread, ContinuationWrapper& cont, intptr_t* frame_sp, bool preempt) 491 : FreezeBase(thread, cont, frame_sp, preempt) {} 492 493 freeze_result try_freeze_fast(); 494 495 protected: 496 virtual stackChunkOop allocate_chunk_slow(size_t stack_size) override { return allocate_chunk(stack_size); } 497 }; 498 499 FreezeBase::FreezeBase(JavaThread* thread, ContinuationWrapper& cont, intptr_t* frame_sp, bool preempt) : 500 _thread(thread), _cont(cont), _barriers(false), _preempt(preempt), _last_frame(false /* no initialization */) { 501 DEBUG_ONLY(_jvmti_event_collector = nullptr;) 502 503 assert(_thread != nullptr, ""); 504 assert(_thread->last_continuation()->entry_sp() == _cont.entrySP(), ""); 505 506 DEBUG_ONLY(_cont.entry()->verify_cookie();) 507 508 assert(!Interpreter::contains(_cont.entryPC()), ""); 509 510 _bottom_address = _cont.entrySP() - _cont.entry_frame_extension(); 511 #ifdef _LP64 512 if (((intptr_t)_bottom_address & 0xf) != 0) { 513 _bottom_address--; 514 } 515 assert(is_aligned(_bottom_address, frame::frame_alignment), ""); 516 #endif 517 518 log_develop_trace(continuations)("bottom_address: " INTPTR_FORMAT " entrySP: " INTPTR_FORMAT " argsize: " PTR_FORMAT, 519 p2i(_bottom_address), p2i(_cont.entrySP()), (_cont.entrySP() - _bottom_address) << LogBytesPerWord); 520 assert(_bottom_address != nullptr, ""); 521 assert(_bottom_address <= _cont.entrySP(), ""); 522 DEBUG_ONLY(_last_write = nullptr;) 523 524 assert(_cont.chunk_invariant(), ""); 525 assert(!Interpreter::contains(_cont.entryPC()), ""); 526 #if !defined(PPC64) || defined(ZERO) 527 static const int doYield_stub_frame_size = frame::metadata_words; 528 #else 529 static const int doYield_stub_frame_size = frame::native_abi_reg_args_size >> LogBytesPerWord; 530 #endif 531 // With preemption doYield() might not have been resolved yet 532 assert(_preempt || SharedRuntime::cont_doYield_stub()->frame_size() == doYield_stub_frame_size, ""); 533 534 // properties of the continuation on the stack; all sizes are in words 535 _cont_stack_top = frame_sp + (!preempt ? doYield_stub_frame_size : 0); // we don't freeze the doYield stub frame 536 _cont_stack_bottom = _cont.entrySP() + (_cont.argsize() == 0 ? frame::metadata_words_at_top : 0) 537 - ContinuationHelper::frame_align_words(_cont.argsize()); // see alignment in thaw 538 539 log_develop_trace(continuations)("freeze size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT, 540 cont_size(), _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom)); 541 assert(cont_size() > 0, ""); 542 543 if (LockingMode == LM_LEGACY) { 544 _monitors_to_fix = thread->held_monitor_count(); 545 assert(_monitors_to_fix >= 0, "invariant"); 546 _monitors_in_lockstack = 0; 547 assert(_thread->lock_stack().monitor_count() == 0, "should be set only for LM_LIGHTWEIGHT"); 548 } else { 549 _monitors_in_lockstack = _thread->lock_stack().monitor_count(); 550 assert(_monitors_in_lockstack >= 0 && _monitors_in_lockstack <= 8, "_monitors_in_lockstack=%d", _monitors_in_lockstack); 551 _monitors_to_fix = _monitors_in_lockstack; 552 assert(thread->held_monitor_count() == 0, "should be set only for LM_LEGACY"); 553 } 554 DEBUG_ONLY(int verified_cnt = monitors_to_fix_on_stack(_thread);) 555 assert(verified_cnt == _monitors_to_fix || thread->obj_locker_count() > 0 || 556 (LockingMode == LM_LIGHTWEIGHT && verified_cnt == _thread->lock_stack().unique_count()), 557 "wrong monitor count. Found %d in the stack but counter is %d", verified_cnt, _monitors_to_fix); 558 } 559 560 void FreezeBase::init_rest() { // we want to postpone some initialization after chunk handling 561 _freeze_size = 0; 562 _total_align_size = 0; 563 NOT_PRODUCT(_frames = 0;) 564 } 565 566 void FreezeBase::fix_monitors_in_interpreted_frame(frame& f) { 567 assert(LockingMode == LM_LEGACY, "invariant"); 568 BasicObjectLock* first_mon = f.interpreter_frame_monitor_begin(); 569 BasicObjectLock* last_mon = f.interpreter_frame_monitor_end(); 570 assert(last_mon <= first_mon, "must be"); 571 572 if (first_mon == last_mon) { 573 // No monitors in this frame 574 return; 575 } 576 577 for (BasicObjectLock* current = f.previous_monitor_in_interpreter_frame(first_mon); 578 current >= last_mon; current = f.previous_monitor_in_interpreter_frame(current)) { 579 oop obj = current->obj(); 580 if (obj == nullptr) { 581 continue; 582 } 583 if (obj == _monitorenter_obj) { 584 assert(_preempt, "should be preemption on monitorenter case"); 585 assert(obj->mark().monitor() != nullptr, "failed to acquire the lock but its not inflated"); 586 continue; 587 } 588 markWord mark = obj->mark(); 589 if (mark.has_monitor() && !mark.monitor()->is_owner_anonymous()) { 590 // Good for freezing, nothing to do. 591 assert(mark.monitor()->is_owner(_thread), "invariant"); 592 continue; 593 } 594 // Found locked monitor that needs fixing. Inflate will fix the owner for stack-locked case or inflated but anonymously owned. 595 ObjectMonitor* om = ObjectSynchronizer::inflate(_thread, obj, ObjectSynchronizer::InflateCause::inflate_cause_cont_freeze); 596 assert(om != nullptr, "invariant"); 597 assert(om->is_owner(_thread), "invariant"); 598 if (--_monitors_to_fix == 0) break; 599 } 600 assert(_monitors_to_fix >= 0, "invariant"); 601 } 602 603 template <typename RegisterMapT> 604 void FreezeBase::fix_monitors_in_compiled_frame(frame& f, RegisterMapT* map) { 605 assert(LockingMode == LM_LEGACY, "invariant"); 606 assert(!f.is_interpreted_frame(), ""); 607 assert(ContinuationHelper::CompiledFrame::is_instance(f), ""); 608 609 nmethod* nm = f.cb()->as_nmethod(); 610 611 if (!nm->has_monitors()) { 612 // No monitors in this frame 613 return; 614 } 615 616 for (ScopeDesc* scope = nm->scope_desc_at(f.pc()); scope != nullptr; scope = scope->sender()) { 617 GrowableArray<MonitorValue*>* mons = scope->monitors(); 618 if (mons == nullptr || mons->is_empty()) { 619 continue; 620 } 621 622 for (int index = (mons->length()-1); index >= 0; index--) { // see compiledVFrame::monitors() 623 MonitorValue* mon = mons->at(index); 624 if (mon->eliminated()) { 625 continue; // we ignore eliminated monitors 626 } 627 ScopeValue* ov = mon->owner(); 628 StackValue* owner_sv = StackValue::create_stack_value(&f, map, ov); // it is an oop 629 oop obj = owner_sv->get_obj()(); 630 if (obj == nullptr) { 631 continue; 632 } 633 if (obj == _monitorenter_obj) { 634 assert(_preempt, "should be preemption on monitorenter case"); 635 assert(obj->mark().monitor() != nullptr, "failed to acquire the lock but its not inflated"); 636 continue; 637 } 638 markWord mark = obj->mark(); 639 if (mark.has_monitor() && !mark.monitor()->is_owner_anonymous()) { 640 // Good for freezing, nothing to do. 641 assert(mark.monitor()->is_owner(_thread), "invariant"); 642 continue; 643 } 644 // Found locked monitor that needs fixing. Inflate will fix the owner for stack-locked case or inflated but anonymously owned. 645 ObjectMonitor* om = ObjectSynchronizer::inflate(_thread, obj, ObjectSynchronizer::InflateCause::inflate_cause_cont_freeze); 646 assert(om != nullptr, "invariant"); 647 assert(om->is_owner(_thread), "invariant"); 648 if (--_monitors_to_fix == 0) break; 649 } 650 } 651 assert(_monitors_to_fix >= 0, "invariant"); 652 } 653 654 void FreezeBase::fix_monitors_in_fast_path() { 655 if (LockingMode == LM_LIGHTWEIGHT) { 656 stackChunkOop chunk = _cont.tail(); 657 assert(chunk->sp_address() - chunk->start_address() >= _monitors_in_lockstack, "no room for lockstack"); 658 _thread->lock_stack().move_to_address((oop*)chunk->start_address()); 659 660 chunk->set_lockStackSize((uint8_t)_monitors_in_lockstack); 661 chunk->set_has_lockStack(true); 662 663 DEBUG_ONLY(_monitors_to_fix -= _monitors_in_lockstack;) 664 } else { 665 assert(LockingMode == LM_LEGACY, "invariant"); 666 _monitorenter_obj = _thread->is_on_monitorenter() ? _thread->current_pending_monitor()->object() : nullptr; 667 668 ResourceMark rm(_thread); 669 RegisterMap map(JavaThread::current(), 670 RegisterMap::UpdateMap::include, 671 RegisterMap::ProcessFrames::skip, // already processed in unwind_frames() 672 RegisterMap::WalkContinuation::skip); 673 map.set_include_argument_oops(false); 674 frame freezed_top(_cont_stack_top); 675 frame first = !_preempt ? freezed_top : freezed_top.sender(&map); 676 for (frame f = first; _monitors_to_fix > 0 && Continuation::is_frame_in_continuation(_cont.entry(), f); f = f.sender(&map)) { 677 fix_monitors_in_compiled_frame(f, &map); 678 } 679 } 680 assert(_monitors_to_fix == 0, "missing monitors in stack"); 681 assert(_thread->held_monitor_count() == 0, "should be 0 now"); 682 } 683 684 void FreezeBase::copy_to_chunk(intptr_t* from, intptr_t* to, int size) { 685 stackChunkOop chunk = _cont.tail(); 686 chunk->copy_from_stack_to_chunk(from, to, size); 687 CONT_JFR_ONLY(_jfr_info.record_size_copied(size);) 688 689 #ifdef ASSERT 690 if (_last_write != nullptr) { 691 assert(_last_write == to + size, "Missed a spot: _last_write: " INTPTR_FORMAT " to+size: " INTPTR_FORMAT 692 " stack_size: %d _last_write offset: " PTR_FORMAT " to+size: " PTR_FORMAT, p2i(_last_write), p2i(to+size), 693 chunk->stack_size(), _last_write-chunk->start_address(), to+size-chunk->start_address()); 694 _last_write = to; 695 } 696 #endif 697 } 698 699 // Called _after_ the last possible safepoint during the freeze operation (chunk allocation) 700 void FreezeBase::unwind_frames() { 701 ContinuationEntry* entry = _cont.entry(); 702 entry->flush_stack_processing(_thread); 703 set_anchor_to_entry(_thread, entry); 704 } 705 706 template <typename ConfigT> 707 freeze_result Freeze<ConfigT>::try_freeze_fast() { 708 assert(_thread->thread_state() == _thread_in_vm, ""); 709 assert(_thread->cont_fastpath(), ""); 710 711 DEBUG_ONLY(_fast_freeze_size = size_if_fast_freeze_available();) 712 assert(_fast_freeze_size == 0, ""); 713 714 stackChunkOop chunk = allocate_chunk(cont_size() + frame::metadata_words + _monitors_in_lockstack); 715 if (freeze_fast_new_chunk(chunk)) { 716 return freeze_ok; 717 } 718 if (_thread->has_pending_exception()) { 719 return freeze_exception; 720 } 721 722 // TODO R REMOVE when deopt change is fixed 723 assert(!_thread->cont_fastpath() || _barriers, ""); 724 log_develop_trace(continuations)("-- RETRYING SLOW --"); 725 return freeze_slow(); 726 } 727 728 // Returns size needed if the continuation fits, otherwise 0. 729 int FreezeBase::size_if_fast_freeze_available() { 730 stackChunkOop chunk = _cont.tail(); 731 if (chunk == nullptr || chunk->is_gc_mode() || chunk->requires_barriers() || chunk->has_mixed_frames()) { 732 log_develop_trace(continuations)("chunk available %s", chunk == nullptr ? "no chunk" : "chunk requires barriers"); 733 return 0; 734 } 735 736 int total_size_needed = cont_size(); 737 const int chunk_sp = chunk->sp(); 738 739 // argsize can be nonzero if we have a caller, but the caller could be in a non-empty parent chunk, 740 // so we subtract it only if we overlap with the caller, i.e. the current chunk isn't empty. 741 // Consider leaving the chunk's argsize set when emptying it and removing the following branch, 742 // although that would require changing stackChunkOopDesc::is_empty 743 if (chunk_sp < chunk->stack_size()) { 744 total_size_needed -= _cont.argsize() + frame::metadata_words_at_top; 745 } 746 747 total_size_needed += _monitors_in_lockstack; 748 749 int chunk_free_room = chunk_sp - frame::metadata_words_at_bottom; 750 bool available = chunk_free_room >= total_size_needed; 751 log_develop_trace(continuations)("chunk available: %s size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT, 752 available ? "yes" : "no" , total_size_needed, _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom)); 753 return available ? total_size_needed : 0; 754 } 755 756 void FreezeBase::freeze_fast_existing_chunk() { 757 stackChunkOop chunk = _cont.tail(); 758 DEBUG_ONLY(_orig_chunk_sp = chunk->sp_address();) 759 760 DEBUG_ONLY(_fast_freeze_size = size_if_fast_freeze_available();) 761 assert(_fast_freeze_size > 0, ""); 762 763 if (chunk->sp() < chunk->stack_size()) { // we are copying into a non-empty chunk 764 DEBUG_ONLY(_empty = false;) 765 assert(chunk->sp() < (chunk->stack_size() - chunk->argsize()), ""); 766 #ifdef ASSERT 767 { 768 intptr_t* retaddr_slot = (chunk->sp_address() 769 - frame::sender_sp_ret_address_offset()); 770 assert(ContinuationHelper::return_address_at(retaddr_slot) == chunk->pc(), 771 "unexpected saved return address"); 772 } 773 #endif 774 775 // the chunk's sp before the freeze, adjusted to point beyond the stack-passed arguments in the topmost frame 776 // we overlap; we'll overwrite the chunk's top frame's callee arguments 777 const int chunk_start_sp = chunk->sp() + _cont.argsize() + frame::metadata_words_at_top; 778 assert(chunk_start_sp <= chunk->stack_size(), "sp not pointing into stack"); 779 780 // increase max_size by what we're freezing minus the overlap 781 chunk->set_max_thawing_size(chunk->max_thawing_size() + cont_size() - _cont.argsize() - frame::metadata_words_at_top); 782 783 intptr_t* const bottom_sp = _cont_stack_bottom - _cont.argsize() - frame::metadata_words_at_top; 784 assert(bottom_sp == _bottom_address, ""); 785 // Because the chunk isn't empty, we know there's a caller in the chunk, therefore the bottom-most frame 786 // should have a return barrier (installed back when we thawed it). 787 #ifdef ASSERT 788 { 789 intptr_t* retaddr_slot = (bottom_sp 790 - frame::sender_sp_ret_address_offset()); 791 assert(ContinuationHelper::return_address_at(retaddr_slot) 792 == StubRoutines::cont_returnBarrier(), 793 "should be the continuation return barrier"); 794 } 795 #endif 796 // We copy the fp from the chunk back to the stack because it contains some caller data, 797 // including, possibly, an oop that might have gone stale since we thawed. 798 patch_stack_pd(bottom_sp, chunk->sp_address()); 799 // we don't patch the return pc at this time, so as not to make the stack unwalkable for async walks 800 801 freeze_fast_copy(chunk, chunk_start_sp CONT_JFR_ONLY(COMMA false)); 802 } else { // the chunk is empty 803 DEBUG_ONLY(_empty = true;) 804 const int chunk_start_sp = chunk->sp(); 805 806 assert(chunk_start_sp == chunk->stack_size(), ""); 807 808 chunk->set_max_thawing_size(cont_size()); 809 chunk->set_argsize(_cont.argsize()); 810 811 freeze_fast_copy(chunk, chunk_start_sp CONT_JFR_ONLY(COMMA false)); 812 } 813 } 814 815 bool FreezeBase::freeze_fast_new_chunk(stackChunkOop chunk) { 816 DEBUG_ONLY(_empty = true;) 817 818 // Install new chunk 819 _cont.set_tail(chunk); 820 821 if (UNLIKELY(chunk == nullptr || !_thread->cont_fastpath() || _barriers)) { // OOME/probably humongous 822 log_develop_trace(continuations)("Retrying slow. Barriers: %d", _barriers); 823 return false; 824 } 825 826 chunk->set_max_thawing_size(cont_size()); 827 chunk->set_argsize(_cont.argsize()); 828 829 // in a fresh chunk, we freeze *with* the bottom-most frame's stack arguments. 830 // They'll then be stored twice: in the chunk and in the parent chunk's top frame 831 const int chunk_start_sp = cont_size() + frame::metadata_words + _monitors_in_lockstack; 832 assert(chunk_start_sp == chunk->stack_size(), ""); 833 834 DEBUG_ONLY(_orig_chunk_sp = chunk->start_address() + chunk_start_sp;) 835 836 freeze_fast_copy(chunk, chunk_start_sp CONT_JFR_ONLY(COMMA true)); 837 838 return true; 839 } 840 841 void FreezeBase::freeze_fast_copy(stackChunkOop chunk, int chunk_start_sp CONT_JFR_ONLY(COMMA bool chunk_is_allocated)) { 842 assert(chunk != nullptr, ""); 843 assert(!chunk->has_mixed_frames(), ""); 844 assert(!chunk->is_gc_mode(), ""); 845 assert(!chunk->has_bitmap(), ""); 846 assert(!chunk->requires_barriers(), ""); 847 assert(chunk == _cont.tail(), ""); 848 849 // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before 850 // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here 851 // will either see no continuation on the stack, or a consistent chunk. 852 unwind_frames(); 853 854 log_develop_trace(continuations)("freeze_fast start: chunk " INTPTR_FORMAT " size: %d orig sp: %d argsize: %d", 855 p2i((oopDesc*)chunk), chunk->stack_size(), chunk_start_sp, _cont.argsize()); 856 assert(chunk_start_sp <= chunk->stack_size(), ""); 857 assert(chunk_start_sp >= cont_size(), "no room in the chunk"); 858 859 const int chunk_new_sp = chunk_start_sp - cont_size(); // the chunk's new sp, after freeze 860 assert(!(_fast_freeze_size > 0) || (_orig_chunk_sp - (chunk->start_address() + chunk_new_sp)) == (_fast_freeze_size - _monitors_in_lockstack), ""); 861 862 intptr_t* chunk_top = chunk->start_address() + chunk_new_sp; 863 #ifdef ASSERT 864 if (!_empty) { 865 intptr_t* retaddr_slot = (_orig_chunk_sp 866 - frame::sender_sp_ret_address_offset()); 867 assert(ContinuationHelper::return_address_at(retaddr_slot) == chunk->pc(), 868 "unexpected saved return address"); 869 } 870 #endif 871 872 log_develop_trace(continuations)("freeze_fast start: " INTPTR_FORMAT " sp: %d chunk_top: " INTPTR_FORMAT, 873 p2i(chunk->start_address()), chunk_new_sp, p2i(chunk_top)); 874 intptr_t* from = _cont_stack_top - frame::metadata_words_at_bottom; 875 intptr_t* to = chunk_top - frame::metadata_words_at_bottom; 876 copy_to_chunk(from, to, cont_size() + frame::metadata_words_at_bottom); 877 // Because we're not patched yet, the chunk is now in a bad state 878 879 // patch return pc of the bottom-most frozen frame (now in the chunk) 880 // with the actual caller's return address 881 intptr_t* chunk_bottom_retaddr_slot = (chunk_top + cont_size() 882 - _cont.argsize() 883 - frame::metadata_words_at_top 884 - frame::sender_sp_ret_address_offset()); 885 #ifdef ASSERT 886 if (!_empty) { 887 assert(ContinuationHelper::return_address_at(chunk_bottom_retaddr_slot) 888 == StubRoutines::cont_returnBarrier(), 889 "should be the continuation return barrier"); 890 } 891 #endif 892 ContinuationHelper::patch_return_address_at(chunk_bottom_retaddr_slot, 893 chunk->pc()); 894 895 // We're always writing to a young chunk, so the GC can't see it until the next safepoint. 896 chunk->set_sp(chunk_new_sp); 897 // set chunk->pc to the return address of the topmost frame in the chunk 898 chunk->set_pc(ContinuationHelper::return_address_at( 899 _cont_stack_top - frame::sender_sp_ret_address_offset())); 900 901 // Fix monitors after unwinding frames to make sure oops are valid. 902 if (_monitors_to_fix > 0) { 903 fix_monitors_in_fast_path(); 904 } 905 906 _cont.write(); 907 908 log_develop_trace(continuations)("FREEZE CHUNK #" INTPTR_FORMAT " (young)", _cont.hash()); 909 LogTarget(Trace, continuations) lt; 910 if (lt.develop_is_enabled()) { 911 LogStream ls(lt); 912 chunk->print_on(true, &ls); 913 } 914 915 // Verification 916 assert(_cont.chunk_invariant(), ""); 917 chunk->verify(); 918 919 #if CONT_JFR 920 EventContinuationFreezeFast e; 921 if (e.should_commit()) { 922 e.set_id(cast_from_oop<u8>(chunk)); 923 DEBUG_ONLY(e.set_allocate(chunk_is_allocated);) 924 e.set_size(cont_size() << LogBytesPerWord); 925 e.commit(); 926 } 927 #endif 928 } 929 930 NOINLINE freeze_result FreezeBase::freeze_slow() { 931 #ifdef ASSERT 932 ResourceMark rm; 933 #endif 934 935 log_develop_trace(continuations)("freeze_slow #" INTPTR_FORMAT, _cont.hash()); 936 assert(_thread->thread_state() == _thread_in_vm || _thread->thread_state() == _thread_blocked, ""); 937 938 #if CONT_JFR 939 EventContinuationFreezeSlow e; 940 if (e.should_commit()) { 941 e.set_id(cast_from_oop<u8>(_cont.continuation())); 942 e.commit(); 943 } 944 #endif 945 946 init_rest(); 947 948 HandleMark hm(Thread::current()); 949 950 frame f = freeze_start_frame(); 951 952 LogTarget(Debug, continuations) lt; 953 if (lt.develop_is_enabled()) { 954 LogStream ls(lt); 955 f.print_on(&ls); 956 } 957 958 frame caller; // the frozen caller in the chunk 959 freeze_result res = recurse_freeze(f, caller, 0, false, true); 960 961 if (res == freeze_ok) { 962 finish_freeze(f, caller); 963 _cont.write(); 964 assert(_monitors_to_fix == 0, "missing monitors in stack"); 965 assert(_thread->held_monitor_count() == 0, "should be 0 now"); 966 } 967 968 return res; 969 } 970 971 frame FreezeBase::freeze_start_frame() { 972 if (LIKELY(!_preempt)) { 973 return freeze_start_frame_yield_stub(); 974 } else { 975 return freeze_start_frame_on_preempt(); 976 } 977 } 978 979 frame FreezeBase::freeze_start_frame_yield_stub() { 980 frame f = _thread->last_frame(); 981 assert(SharedRuntime::cont_doYield_stub()->contains(f.pc()), "must be"); 982 f = sender<ContinuationHelper::NonInterpretedUnknownFrame>(f); 983 assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), f), ""); 984 return f; 985 } 986 987 frame FreezeBase::freeze_start_frame_on_preempt() { 988 assert(_last_frame.sp() == _thread->last_frame().sp(), "_last_frame should be already initialized"); 989 assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), _last_frame), ""); 990 return _last_frame; 991 } 992 993 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap. 994 NOINLINE freeze_result FreezeBase::recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top) { 995 assert(f.unextended_sp() < _bottom_address, ""); // see recurse_freeze_java_frame 996 assert(f.is_interpreted_frame() || ((top && _preempt) == ContinuationHelper::Frame::is_stub(f.cb())) 997 || ((top && _preempt) == f.is_native_frame()), ""); 998 999 if (stack_overflow()) { 1000 return freeze_exception; 1001 } 1002 1003 if (f.is_compiled_frame()) { 1004 if (UNLIKELY(f.oop_map() == nullptr)) { 1005 // special native frame 1006 return freeze_pinned_native; 1007 } 1008 return recurse_freeze_compiled_frame(f, caller, callee_argsize, callee_interpreted); 1009 } else if (f.is_interpreted_frame()) { 1010 assert(!f.interpreter_frame_method()->is_native() || (_preempt && top && _thread->is_on_monitorenter()), ""); 1011 if (_preempt && top && f.interpreter_frame_method()->is_native()) { 1012 // TODO: Allow preemption for this case too 1013 return freeze_pinned_native; 1014 } 1015 return recurse_freeze_interpreted_frame(f, caller, callee_argsize, callee_interpreted); 1016 } else if (_preempt && top) { 1017 assert(ContinuationHelper::Frame::is_stub(f.cb()) || (f.is_native_frame() && _thread->is_on_monitorenter()), "invariant"); 1018 if (f.is_native_frame()) { 1019 // TODO: Allow preemption for this case too 1020 return freeze_pinned_native; 1021 } else { 1022 return recurse_freeze_stub_frame(f, caller); 1023 } 1024 } else { 1025 return freeze_pinned_native; 1026 } 1027 } 1028 1029 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap. 1030 // See also StackChunkFrameStream<frame_kind>::frame_size() 1031 template<typename FKind> 1032 inline freeze_result FreezeBase::recurse_freeze_java_frame(const frame& f, frame& caller, int fsize, int argsize) { 1033 assert(FKind::is_instance(f), ""); 1034 1035 assert(fsize > 0, ""); 1036 assert(argsize >= 0, ""); 1037 _freeze_size += fsize; 1038 NOT_PRODUCT(_frames++;) 1039 1040 assert(FKind::frame_bottom(f) <= _bottom_address, ""); 1041 1042 // We don't use FKind::frame_bottom(f) == _bottom_address because on x64 there's sometimes an extra word between 1043 // enterSpecial and an interpreted frame 1044 if (FKind::frame_bottom(f) >= _bottom_address - 1) { 1045 return finalize_freeze(f, caller, argsize); // recursion end 1046 } else { 1047 frame senderf = sender<FKind>(f); 1048 assert(FKind::interpreted || senderf.sp() == senderf.unextended_sp(), ""); 1049 freeze_result result = recurse_freeze(senderf, caller, argsize, FKind::interpreted, false); // recursive call 1050 return result; 1051 } 1052 } 1053 1054 inline void FreezeBase::before_freeze_java_frame(const frame& f, const frame& caller, int fsize, int argsize, bool is_bottom_frame) { 1055 LogTarget(Trace, continuations) lt; 1056 if (lt.develop_is_enabled()) { 1057 LogStream ls(lt); 1058 ls.print_cr("======== FREEZING FRAME interpreted: %d bottom: %d", f.is_interpreted_frame(), is_bottom_frame); 1059 ls.print_cr("fsize: %d argsize: %d", fsize, argsize); 1060 f.print_value_on(&ls, nullptr); 1061 } 1062 assert(caller.is_interpreted_frame() == Interpreter::contains(caller.pc()), ""); 1063 } 1064 1065 inline void FreezeBase::after_freeze_java_frame(const frame& hf, bool is_bottom_frame) { 1066 LogTarget(Trace, continuations) lt; 1067 if (lt.develop_is_enabled()) { 1068 LogStream ls(lt); 1069 DEBUG_ONLY(hf.print_value_on(&ls, nullptr);) 1070 assert(hf.is_heap_frame(), "should be"); 1071 DEBUG_ONLY(print_frame_layout(hf, false, &ls);) 1072 if (is_bottom_frame) { 1073 ls.print_cr("bottom h-frame:"); 1074 hf.print_on(&ls); 1075 } 1076 } 1077 } 1078 1079 // The parameter argsize_md includes metadata that has to be part of caller/callee overlap. 1080 // See also StackChunkFrameStream<frame_kind>::frame_size() 1081 freeze_result FreezeBase::finalize_freeze(const frame& callee, frame& caller, int argsize_md) { 1082 int argsize = argsize_md - frame::metadata_words_at_top; 1083 assert(callee.is_interpreted_frame() 1084 || ContinuationHelper::Frame::is_stub(callee.cb()) 1085 || callee.cb()->as_nmethod()->is_osr_method() 1086 || argsize == _cont.argsize(), "argsize: %d cont.argsize: %d", argsize, _cont.argsize()); 1087 log_develop_trace(continuations)("bottom: " INTPTR_FORMAT " count %d size: %d argsize: %d", 1088 p2i(_bottom_address), _frames, _freeze_size << LogBytesPerWord, argsize); 1089 1090 LogTarget(Trace, continuations) lt; 1091 1092 #ifdef ASSERT 1093 bool empty = _cont.is_empty(); 1094 log_develop_trace(continuations)("empty: %d", empty); 1095 #endif 1096 1097 stackChunkOop chunk = _cont.tail(); 1098 1099 assert(chunk == nullptr || (chunk->max_thawing_size() == 0) == chunk->is_empty(), ""); 1100 1101 _freeze_size += frame::metadata_words; // for top frame's metadata 1102 1103 int overlap = 0; // the args overlap the caller -- if there is one in this chunk and is of the same kind 1104 int unextended_sp = -1; 1105 if (chunk != nullptr) { 1106 unextended_sp = chunk->sp(); 1107 if (!chunk->is_empty()) { 1108 StackChunkFrameStream<ChunkFrames::Mixed> last(chunk); 1109 unextended_sp = chunk->to_offset(StackChunkFrameStream<ChunkFrames::Mixed>(chunk).unextended_sp()); 1110 bool top_interpreted = Interpreter::contains(chunk->pc()); 1111 if (callee.is_interpreted_frame() == top_interpreted) { 1112 overlap = argsize_md; 1113 } 1114 } 1115 } 1116 1117 log_develop_trace(continuations)("finalize _size: %d overlap: %d unextended_sp: %d", _freeze_size, overlap, unextended_sp); 1118 1119 _freeze_size -= overlap; 1120 assert(_freeze_size >= 0, ""); 1121 1122 assert(chunk == nullptr || chunk->is_empty() 1123 || unextended_sp == chunk->to_offset(StackChunkFrameStream<ChunkFrames::Mixed>(chunk).unextended_sp()), ""); 1124 assert(chunk != nullptr || unextended_sp < _freeze_size, ""); 1125 1126 _freeze_size += _monitors_in_lockstack; 1127 1128 // _barriers can be set to true by an allocation in freeze_fast, in which case the chunk is available 1129 bool allocated_old_in_freeze_fast = _barriers; 1130 assert(!allocated_old_in_freeze_fast || (unextended_sp >= _freeze_size && chunk->is_empty()), 1131 "Chunk allocated in freeze_fast is of insufficient size " 1132 "unextended_sp: %d size: %d is_empty: %d", unextended_sp, _freeze_size, chunk->is_empty()); 1133 assert(!allocated_old_in_freeze_fast || (!UseZGC && !UseG1GC), "Unexpected allocation"); 1134 1135 DEBUG_ONLY(bool empty_chunk = true); 1136 if (unextended_sp < _freeze_size || chunk->is_gc_mode() || (!allocated_old_in_freeze_fast && chunk->requires_barriers())) { 1137 // ALLOCATE NEW CHUNK 1138 1139 if (lt.develop_is_enabled()) { 1140 LogStream ls(lt); 1141 if (chunk == nullptr) { 1142 ls.print_cr("no chunk"); 1143 } else { 1144 ls.print_cr("chunk barriers: %d _size: %d free size: %d", 1145 chunk->requires_barriers(), _freeze_size, chunk->sp() - frame::metadata_words); 1146 chunk->print_on(&ls); 1147 } 1148 } 1149 1150 _freeze_size += overlap; // we're allocating a new chunk, so no overlap 1151 // overlap = 0; 1152 1153 chunk = allocate_chunk_slow(_freeze_size); 1154 if (chunk == nullptr) { 1155 return freeze_exception; 1156 } 1157 1158 // Install new chunk 1159 _cont.set_tail(chunk); 1160 1161 int sp = chunk->stack_size() - argsize_md; 1162 chunk->set_sp(sp); 1163 chunk->set_argsize(argsize); 1164 assert(is_empty(chunk), ""); 1165 } else { 1166 // REUSE EXISTING CHUNK 1167 log_develop_trace(continuations)("Reusing chunk mixed: %d empty: %d", chunk->has_mixed_frames(), chunk->is_empty()); 1168 if (chunk->is_empty()) { 1169 int sp = chunk->stack_size() - argsize_md; 1170 chunk->set_sp(sp); 1171 chunk->set_argsize(argsize); 1172 _freeze_size += overlap; 1173 assert(chunk->max_thawing_size() == 0, ""); 1174 } DEBUG_ONLY(else empty_chunk = false;) 1175 } 1176 assert(!chunk->is_gc_mode(), ""); 1177 assert(!chunk->has_bitmap(), ""); 1178 chunk->set_has_mixed_frames(true); 1179 1180 assert(chunk->requires_barriers() == _barriers, ""); 1181 assert(!_barriers || is_empty(chunk), ""); 1182 1183 assert(!is_empty(chunk) || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).is_done(), ""); 1184 assert(!is_empty(chunk) || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame().is_empty(), ""); 1185 1186 if (_preempt) { 1187 frame f = _thread->last_frame(); 1188 if (f.is_interpreted_frame()) { 1189 // Do it now that we know freezing will be successful. 1190 prepare_freeze_interpreted_top_frame(f); 1191 } 1192 } 1193 1194 // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before 1195 // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here 1196 // will either see no continuation or a consistent chunk. 1197 unwind_frames(); 1198 1199 chunk->set_max_thawing_size(chunk->max_thawing_size() + _freeze_size - _monitors_in_lockstack - frame::metadata_words); 1200 1201 if (lt.develop_is_enabled()) { 1202 LogStream ls(lt); 1203 ls.print_cr("top chunk:"); 1204 chunk->print_on(&ls); 1205 } 1206 1207 // Fix monitors after unwinding frames to make sure oops are valid. Also do it now to avoid unnecessary 1208 // calls to fix_monitors_in_interpreted_frame/fix_monitors_in_compiled_frame() later. 1209 if (_monitors_to_fix > 0) { 1210 if (_monitors_in_lockstack > 0) { 1211 assert(chunk->sp_address() - chunk->start_address() >= _monitors_in_lockstack, "no room for lockstack"); 1212 _thread->lock_stack().move_to_address((oop*)chunk->start_address()); 1213 1214 chunk->set_lockStackSize((uint8_t)_monitors_in_lockstack); 1215 chunk->set_has_lockStack(true); 1216 1217 _monitors_to_fix -= _monitors_in_lockstack; 1218 assert(_monitors_to_fix == 0, "invariant"); 1219 } 1220 _monitorenter_obj = _thread->is_on_monitorenter() ? _thread->current_pending_monitor()->object() : nullptr; 1221 } 1222 1223 // The topmost existing frame in the chunk; or an empty frame if the chunk is empty 1224 caller = StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame(); 1225 1226 DEBUG_ONLY(_last_write = caller.unextended_sp() + (empty_chunk ? argsize_md : overlap);) 1227 1228 assert(chunk->is_in_chunk(_last_write - _freeze_size), 1229 "last_write-size: " INTPTR_FORMAT " start: " INTPTR_FORMAT, p2i(_last_write-_freeze_size), p2i(chunk->start_address())); 1230 #ifdef ASSERT 1231 if (lt.develop_is_enabled()) { 1232 LogStream ls(lt); 1233 ls.print_cr("top hframe before (freeze):"); 1234 assert(caller.is_heap_frame(), "should be"); 1235 caller.print_on(&ls); 1236 } 1237 1238 assert(!empty || Continuation::is_continuation_entry_frame(callee, nullptr), ""); 1239 1240 frame entry = sender(callee); 1241 1242 assert((!empty && Continuation::is_return_barrier_entry(entry.pc())) || (empty && Continuation::is_continuation_enterSpecial(entry)), ""); 1243 assert(callee.is_interpreted_frame() || entry.sp() == entry.unextended_sp(), ""); 1244 #endif 1245 1246 return freeze_ok_bottom; 1247 } 1248 1249 void FreezeBase::patch(const frame& f, frame& hf, const frame& caller, bool is_bottom_frame) { 1250 if (is_bottom_frame) { 1251 // If we're the bottom frame, we need to replace the return barrier with the real 1252 // caller's pc. 1253 address last_pc = caller.pc(); 1254 assert((last_pc == nullptr) == is_empty(_cont.tail()), ""); 1255 ContinuationHelper::Frame::patch_pc(caller, last_pc); 1256 } else { 1257 assert(!caller.is_empty(), ""); 1258 } 1259 1260 patch_pd(hf, caller); 1261 1262 if (f.is_interpreted_frame()) { 1263 assert(hf.is_heap_frame(), "should be"); 1264 ContinuationHelper::InterpretedFrame::patch_sender_sp(hf, caller); 1265 } 1266 1267 #ifdef ASSERT 1268 if (hf.is_compiled_frame()) { 1269 if (f.is_deoptimized_frame()) { // TODO DEOPT: long term solution: unroll on freeze and patch pc 1270 log_develop_trace(continuations)("Freezing deoptimized frame"); 1271 assert(f.cb()->as_nmethod()->is_deopt_pc(f.raw_pc()), ""); 1272 assert(f.cb()->as_nmethod()->is_deopt_pc(ContinuationHelper::Frame::real_pc(f)), ""); 1273 } 1274 } 1275 #endif 1276 } 1277 1278 #ifdef ASSERT 1279 static void verify_frame_top(const frame& f, intptr_t* top) { 1280 ResourceMark rm; 1281 InterpreterOopMap mask; 1282 f.interpreted_frame_oop_map(&mask); 1283 assert(top <= ContinuationHelper::InterpretedFrame::frame_top(f, &mask), 1284 "frame_top: " INTPTR_FORMAT " Interpreted::frame_top: " INTPTR_FORMAT, 1285 p2i(top), p2i(ContinuationHelper::InterpretedFrame::frame_top(f, &mask))); 1286 } 1287 #endif // ASSERT 1288 1289 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap. 1290 // See also StackChunkFrameStream<frame_kind>::frame_size() 1291 NOINLINE freeze_result FreezeBase::recurse_freeze_interpreted_frame(frame& f, frame& caller, 1292 int callee_argsize /* incl. metadata */, 1293 bool callee_interpreted) { 1294 adjust_interpreted_frame_unextended_sp(f); 1295 1296 // The frame's top never includes the stack arguments to the callee 1297 intptr_t* const stack_frame_top = ContinuationHelper::InterpretedFrame::frame_top(f, callee_argsize, callee_interpreted); 1298 intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f); 1299 const int fsize = pointer_delta_as_int(stack_frame_bottom, stack_frame_top); 1300 1301 DEBUG_ONLY(verify_frame_top(f, stack_frame_top)); 1302 1303 Method* frame_method = ContinuationHelper::Frame::frame_method(f); 1304 // including metadata between f and its args 1305 const int argsize = ContinuationHelper::InterpretedFrame::stack_argsize(f) + frame::metadata_words_at_top; 1306 1307 log_develop_trace(continuations)("recurse_freeze_interpreted_frame %s _size: %d fsize: %d argsize: %d", 1308 frame_method->name_and_sig_as_C_string(), _freeze_size, fsize, argsize); 1309 // we'd rather not yield inside methods annotated with @JvmtiMountTransition. In the preempt case 1310 // we already checked it is safe to do so in Continuation::is_safe_vthread_to_preempt(). 1311 assert(!ContinuationHelper::Frame::frame_method(f)->jvmti_mount_transition() || _preempt, ""); 1312 1313 freeze_result result = recurse_freeze_java_frame<ContinuationHelper::InterpretedFrame>(f, caller, fsize, argsize); 1314 if (UNLIKELY(result > freeze_ok_bottom)) { 1315 return result; 1316 } 1317 1318 bool is_bottom_frame = result == freeze_ok_bottom; 1319 assert(!caller.is_empty() || is_bottom_frame, ""); 1320 1321 DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, 0, is_bottom_frame);) 1322 1323 frame hf = new_heap_frame<ContinuationHelper::InterpretedFrame>(f, caller); 1324 _total_align_size += frame::align_wiggle; // add alignment room for internal interpreted frame alignment on AArch64/PPC64 1325 1326 intptr_t* heap_frame_top = ContinuationHelper::InterpretedFrame::frame_top(hf, callee_argsize, callee_interpreted); 1327 intptr_t* heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf); 1328 assert(heap_frame_bottom == heap_frame_top + fsize, ""); 1329 1330 // Some architectures (like AArch64/PPC64/RISC-V) add padding between the locals and the fixed_frame to keep the fp 16-byte-aligned. 1331 // On those architectures we freeze the padding in order to keep the same fp-relative offsets in the fixed_frame. 1332 copy_to_chunk(stack_frame_top, heap_frame_top, fsize); 1333 assert(!is_bottom_frame || !caller.is_interpreted_frame() || (heap_frame_top + fsize) == (caller.unextended_sp() + argsize), ""); 1334 1335 relativize_interpreted_frame_metadata(f, hf); 1336 1337 patch(f, hf, caller, is_bottom_frame); 1338 1339 CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();) 1340 DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);) 1341 caller = hf; 1342 1343 // Mark frame_method's GC epoch for class redefinition on_stack calculation. 1344 frame_method->record_gc_epoch(); 1345 1346 if (_monitors_to_fix > 0) { 1347 // Check if we have monitors in this frame 1348 fix_monitors_in_interpreted_frame(f); 1349 } 1350 1351 return freeze_ok; 1352 } 1353 1354 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap. 1355 // See also StackChunkFrameStream<frame_kind>::frame_size() 1356 freeze_result FreezeBase::recurse_freeze_compiled_frame(frame& f, frame& caller, 1357 int callee_argsize /* incl. metadata */, 1358 bool callee_interpreted) { 1359 // The frame's top never includes the stack arguments to the callee 1360 intptr_t* const stack_frame_top = ContinuationHelper::CompiledFrame::frame_top(f, callee_argsize, callee_interpreted); 1361 intptr_t* const stack_frame_bottom = ContinuationHelper::CompiledFrame::frame_bottom(f); 1362 // including metadata between f and its stackargs 1363 const int argsize = ContinuationHelper::CompiledFrame::stack_argsize(f) + frame::metadata_words_at_top; 1364 const int fsize = pointer_delta_as_int(stack_frame_bottom + argsize, stack_frame_top); 1365 1366 log_develop_trace(continuations)("recurse_freeze_compiled_frame %s _size: %d fsize: %d argsize: %d", 1367 ContinuationHelper::Frame::frame_method(f) != nullptr ? 1368 ContinuationHelper::Frame::frame_method(f)->name_and_sig_as_C_string() : "", 1369 _freeze_size, fsize, argsize); 1370 // we'd rather not yield inside methods annotated with @JvmtiMountTransition. In the preempt case 1371 // we already checked it is safe to do so in Continuation::is_safe_vthread_to_preempt(). 1372 assert(!ContinuationHelper::Frame::frame_method(f)->jvmti_mount_transition() || _preempt, ""); 1373 1374 freeze_result result = recurse_freeze_java_frame<ContinuationHelper::CompiledFrame>(f, caller, fsize, argsize); 1375 if (UNLIKELY(result > freeze_ok_bottom)) { 1376 return result; 1377 } 1378 1379 bool is_bottom_frame = result == freeze_ok_bottom; 1380 assert(!caller.is_empty() || is_bottom_frame, ""); 1381 1382 DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, argsize, is_bottom_frame);) 1383 1384 frame hf = new_heap_frame<ContinuationHelper::CompiledFrame>(f, caller); 1385 1386 intptr_t* heap_frame_top = ContinuationHelper::CompiledFrame::frame_top(hf, callee_argsize, callee_interpreted); 1387 1388 copy_to_chunk(stack_frame_top, heap_frame_top, fsize); 1389 assert(!is_bottom_frame || !caller.is_compiled_frame() || (heap_frame_top + fsize) == (caller.unextended_sp() + argsize), ""); 1390 1391 if (caller.is_interpreted_frame()) { 1392 _total_align_size += frame::align_wiggle; // See Thaw::align 1393 } 1394 1395 patch(f, hf, caller, is_bottom_frame); 1396 1397 assert(is_bottom_frame || Interpreter::contains(ContinuationHelper::CompiledFrame::real_pc(caller)) == caller.is_interpreted_frame(), ""); 1398 1399 DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);) 1400 caller = hf; 1401 1402 if (_monitors_to_fix > 0) { 1403 // Check if we have monitors in this frame 1404 fix_monitors_in_compiled_frame(f, SmallRegisterMap::instance); 1405 } 1406 1407 return freeze_ok; 1408 } 1409 1410 NOINLINE freeze_result FreezeBase::recurse_freeze_stub_frame(frame& f, frame& caller) { 1411 DEBUG_ONLY(frame fsender = sender(f);) 1412 assert(!fsender.is_native_frame() || (Continuation::is_continuation_enterSpecial(fsender) && !_cont.is_empty()), "sender should't be native except for enterSpecial case"); 1413 1414 intptr_t* const stack_frame_top = ContinuationHelper::StubFrame::frame_top(f, 0, 0); 1415 const int fsize = f.cb()->frame_size(); 1416 1417 log_develop_trace(continuations)("recurse_freeze_stub_frame %s _size: %d fsize: %d :: " INTPTR_FORMAT " - " INTPTR_FORMAT, 1418 f.cb()->name(), _freeze_size, fsize, p2i(stack_frame_top), p2i(stack_frame_top+fsize)); 1419 1420 freeze_result result = recurse_freeze_java_frame<ContinuationHelper::StubFrame>(f, caller, fsize, 0); 1421 if (UNLIKELY(result > freeze_ok_bottom)) { 1422 return result; 1423 } 1424 1425 bool is_bottom_frame = result == freeze_ok_bottom; 1426 assert(!caller.is_empty() || (is_bottom_frame && !_cont.is_empty()), ""); 1427 1428 DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, 0, is_bottom_frame);) 1429 1430 frame hf = new_heap_frame<ContinuationHelper::StubFrame>(f, caller); 1431 intptr_t* heap_frame_top = ContinuationHelper::StubFrame::frame_top(hf, 0, 0); 1432 1433 copy_to_chunk(stack_frame_top, heap_frame_top, fsize); 1434 1435 if (caller.is_interpreted_frame()) { 1436 _total_align_size += frame::align_wiggle; 1437 } 1438 1439 patch(f, hf, caller, is_bottom_frame); 1440 1441 DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);) 1442 1443 caller = hf; 1444 return freeze_ok; 1445 } 1446 1447 NOINLINE void FreezeBase::finish_freeze(const frame& f, const frame& top) { 1448 stackChunkOop chunk = _cont.tail(); 1449 1450 LogTarget(Trace, continuations) lt; 1451 if (lt.develop_is_enabled()) { 1452 LogStream ls(lt); 1453 assert(top.is_heap_frame(), "should be"); 1454 top.print_on(&ls); 1455 } 1456 1457 set_top_frame_metadata_pd(top); 1458 1459 chunk->set_sp(chunk->to_offset(top.sp())); 1460 chunk->set_pc(top.pc()); 1461 1462 chunk->set_max_thawing_size(chunk->max_thawing_size() + _total_align_size); 1463 1464 assert(chunk->sp_address() - chunk->start_address() >= _monitors_in_lockstack, "clash with lockstack"); 1465 1466 // At this point the chunk is consistent 1467 1468 if (UNLIKELY(_barriers)) { 1469 log_develop_trace(continuations)("do barriers on old chunk"); 1470 // Serial and Parallel GC can allocate objects directly into the old generation. 1471 // Then we want to relativize the derived pointers eagerly so that 1472 // old chunks are all in GC mode. 1473 assert(!UseG1GC, "G1 can not deal with allocating outside of eden"); 1474 assert(!UseZGC, "ZGC can not deal with allocating chunks visible to marking"); 1475 if (UseShenandoahGC) { 1476 _cont.tail()->relativize_derived_pointers_concurrently(); 1477 } else { 1478 ContinuationGCSupport::transform_stack_chunk(_cont.tail()); 1479 } 1480 // For objects in the old generation we must maintain the remembered set 1481 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(); 1482 } 1483 1484 log_develop_trace(continuations)("finish_freeze: has_mixed_frames: %d", chunk->has_mixed_frames()); 1485 if (lt.develop_is_enabled()) { 1486 LogStream ls(lt); 1487 chunk->print_on(true, &ls); 1488 } 1489 1490 if (lt.develop_is_enabled()) { 1491 LogStream ls(lt); 1492 ls.print_cr("top hframe after (freeze):"); 1493 assert(_cont.last_frame().is_heap_frame(), "should be"); 1494 _cont.last_frame().print_on(&ls); 1495 DEBUG_ONLY(print_frame_layout(top, false, &ls);) 1496 } 1497 1498 assert(_cont.chunk_invariant(), ""); 1499 } 1500 1501 inline bool FreezeBase::stack_overflow() { // detect stack overflow in recursive native code 1502 JavaThread* t = !_preempt ? _thread : JavaThread::current(); 1503 assert(t == JavaThread::current(), ""); 1504 if (os::current_stack_pointer() < t->stack_overflow_state()->shadow_zone_safe_limit()) { 1505 if (!_preempt) { 1506 ContinuationWrapper::SafepointOp so(t, _cont); // could also call _cont.done() instead 1507 Exceptions::_throw_msg(t, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Stack overflow while freezing"); 1508 } 1509 return true; 1510 } 1511 return false; 1512 } 1513 1514 class StackChunkAllocator : public MemAllocator { 1515 const size_t _stack_size; 1516 ContinuationWrapper& _continuation_wrapper; 1517 JvmtiSampledObjectAllocEventCollector* const _jvmti_event_collector; 1518 JavaThread* const _target; 1519 mutable bool _took_slow_path; 1520 1521 // Does the minimal amount of initialization needed for a TLAB allocation. 1522 // We don't need to do a full initialization, as such an allocation need not be immediately walkable. 1523 virtual oop initialize(HeapWord* mem) const override { 1524 assert(_stack_size > 0, ""); 1525 assert(_stack_size <= max_jint, ""); 1526 assert(_word_size > _stack_size, ""); 1527 1528 // zero out fields (but not the stack) 1529 const size_t hs = oopDesc::header_size(); 1530 oopDesc::set_klass_gap(mem, 0); 1531 Copy::fill_to_aligned_words(mem + hs, vmClasses::StackChunk_klass()->size_helper() - hs); 1532 1533 jdk_internal_vm_StackChunk::set_size(mem, (int)_stack_size); 1534 jdk_internal_vm_StackChunk::set_sp(mem, (int)_stack_size); 1535 1536 return finish(mem); 1537 } 1538 1539 stackChunkOop allocate_fast() const { 1540 if (!UseTLAB) { 1541 return nullptr; 1542 } 1543 1544 HeapWord* const mem = MemAllocator::mem_allocate_inside_tlab_fast(); 1545 if (mem == nullptr) { 1546 return nullptr; 1547 } 1548 1549 oop obj = initialize(mem); 1550 return stackChunkOopDesc::cast(obj); 1551 } 1552 1553 bool is_preempt() const { return _thread != _target; } 1554 1555 public: 1556 StackChunkAllocator(Klass* klass, 1557 size_t word_size, 1558 Thread* thread, 1559 size_t stack_size, 1560 ContinuationWrapper& continuation_wrapper, 1561 JvmtiSampledObjectAllocEventCollector* jvmti_event_collector, 1562 JavaThread* target) 1563 : MemAllocator(klass, word_size, thread), 1564 _stack_size(stack_size), 1565 _continuation_wrapper(continuation_wrapper), 1566 _jvmti_event_collector(jvmti_event_collector), 1567 _target(target), 1568 _took_slow_path(false) {} 1569 1570 // Provides it's own, specialized allocation which skips instrumentation 1571 // if the memory can be allocated without going to a slow-path. 1572 stackChunkOop allocate() const { 1573 // First try to allocate without any slow-paths or instrumentation. 1574 stackChunkOop obj = allocate_fast(); 1575 if (obj != nullptr) { 1576 return obj; 1577 } 1578 1579 // Now try full-blown allocation with all expensive operations, 1580 // including potentially safepoint operations. 1581 _took_slow_path = true; 1582 1583 // Protect unhandled Loom oops 1584 ContinuationWrapper::SafepointOp so(_thread, _continuation_wrapper); 1585 1586 // Can safepoint 1587 _jvmti_event_collector->start(); 1588 1589 // Can safepoint 1590 return stackChunkOopDesc::cast(MemAllocator::allocate()); 1591 } 1592 1593 bool took_slow_path() const { 1594 return _took_slow_path; 1595 } 1596 }; 1597 1598 template <typename ConfigT> 1599 stackChunkOop Freeze<ConfigT>::allocate_chunk(size_t stack_size) { 1600 log_develop_trace(continuations)("allocate_chunk allocating new chunk"); 1601 1602 InstanceStackChunkKlass* klass = InstanceStackChunkKlass::cast(vmClasses::StackChunk_klass()); 1603 size_t size_in_words = klass->instance_size(stack_size); 1604 1605 if (CollectedHeap::stack_chunk_max_size() > 0 && size_in_words >= CollectedHeap::stack_chunk_max_size()) { 1606 if (!_preempt) { 1607 throw_stack_overflow_on_humongous_chunk(); 1608 } 1609 return nullptr; 1610 } 1611 1612 JavaThread* current = _preempt ? JavaThread::current() : _thread; 1613 assert(current == JavaThread::current(), "should be current"); 1614 1615 // Allocate the chunk. 1616 // 1617 // This might safepoint while allocating, but all safepointing due to 1618 // instrumentation have been deferred. This property is important for 1619 // some GCs, as this ensures that the allocated object is in the young 1620 // generation / newly allocated memory. 1621 StackChunkAllocator allocator(klass, size_in_words, current, stack_size, _cont, _jvmti_event_collector, _thread); 1622 stackChunkOop chunk = allocator.allocate(); 1623 1624 if (chunk == nullptr) { 1625 return nullptr; // OOME 1626 } 1627 1628 // assert that chunk is properly initialized 1629 assert(chunk->stack_size() == (int)stack_size, ""); 1630 assert(chunk->size() >= stack_size, "chunk->size(): %zu size: %zu", chunk->size(), stack_size); 1631 assert(chunk->sp() == chunk->stack_size(), ""); 1632 assert((intptr_t)chunk->start_address() % 8 == 0, ""); 1633 assert(chunk->max_thawing_size() == 0, ""); 1634 assert(chunk->pc() == nullptr, ""); 1635 assert(chunk->argsize() == 0, ""); 1636 assert(chunk->flags() == 0, ""); 1637 assert(chunk->is_gc_mode() == false, ""); 1638 assert(chunk->lockStackSize() == 0, ""); 1639 assert(chunk->objectMonitor() == nullptr, ""); 1640 1641 // fields are uninitialized 1642 chunk->set_parent_access<IS_DEST_UNINITIALIZED>(_cont.last_nonempty_chunk()); 1643 chunk->set_cont_access<IS_DEST_UNINITIALIZED>(_cont.continuation()); 1644 1645 #if INCLUDE_ZGC 1646 if (UseZGC) { 1647 if (ZGenerational) { 1648 ZStackChunkGCData::initialize(chunk); 1649 } 1650 assert(!chunk->requires_barriers(), "ZGC always allocates in the young generation"); 1651 _barriers = false; 1652 } else 1653 #endif 1654 #if INCLUDE_SHENANDOAHGC 1655 if (UseShenandoahGC) { 1656 _barriers = chunk->requires_barriers(); 1657 } else 1658 #endif 1659 { 1660 if (!allocator.took_slow_path()) { 1661 // Guaranteed to be in young gen / newly allocated memory 1662 assert(!chunk->requires_barriers(), "Unfamiliar GC requires barriers on TLAB allocation"); 1663 _barriers = false; 1664 } else { 1665 // Some GCs could put direct allocations in old gen for slow-path 1666 // allocations; need to explicitly check if that was the case. 1667 _barriers = chunk->requires_barriers(); 1668 } 1669 } 1670 1671 if (_barriers) { 1672 log_develop_trace(continuations)("allocation requires barriers"); 1673 } 1674 1675 assert(chunk->parent() == nullptr || chunk->parent()->is_stackChunk(), ""); 1676 1677 return chunk; 1678 } 1679 1680 void FreezeBase::throw_stack_overflow_on_humongous_chunk() { 1681 ContinuationWrapper::SafepointOp so(_thread, _cont); // could also call _cont.done() instead 1682 Exceptions::_throw_msg(_thread, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Humongous stack chunk"); 1683 } 1684 1685 #if INCLUDE_JVMTI 1686 static int num_java_frames(ContinuationWrapper& cont) { 1687 ResourceMark rm; // used for scope traversal in num_java_frames(nmethod*, address) 1688 int count = 0; 1689 for (stackChunkOop chunk = cont.tail(); chunk != nullptr; chunk = chunk->parent()) { 1690 count += chunk->num_java_frames(); 1691 } 1692 return count; 1693 } 1694 1695 static void invalidate_jvmti_stack(JavaThread* thread) { 1696 if (thread->is_interp_only_mode()) { 1697 JvmtiThreadState *state = thread->jvmti_thread_state(); 1698 if (state != nullptr) 1699 state->invalidate_cur_stack_depth(); 1700 } 1701 } 1702 1703 static void jvmti_yield_cleanup(JavaThread* thread, ContinuationWrapper& cont) { 1704 if (JvmtiExport::can_post_frame_pop()) { 1705 int num_frames = num_java_frames(cont); 1706 1707 ContinuationWrapper::SafepointOp so(Thread::current(), cont); 1708 JvmtiExport::continuation_yield_cleanup(JavaThread::current(), num_frames); 1709 } 1710 invalidate_jvmti_stack(thread); 1711 } 1712 1713 static void jvmti_mount_end(JavaThread* current, ContinuationWrapper& cont, frame top, ObjectMonitor* mon, bool post_mount_event) { 1714 assert(current->vthread() != nullptr, "must be"); 1715 1716 HandleMarkCleaner hm(current); 1717 Handle vth(current, current->vthread()); 1718 1719 ContinuationWrapper::SafepointOp so(current, cont); 1720 1721 // Since we might safepoint set the anchor so that the stack can we walked. 1722 set_anchor(current, top.sp()); 1723 1724 JRT_BLOCK 1725 current->rebind_to_jvmti_thread_state_of(vth()); 1726 { 1727 MutexLocker mu(JvmtiThreadState_lock); 1728 JvmtiThreadState* state = current->jvmti_thread_state(); 1729 if (state != NULL && state->is_pending_interp_only_mode()) { 1730 JvmtiEventController::enter_interp_only_mode(); 1731 } 1732 } 1733 assert(current->is_in_VTMS_transition(), "sanity check"); 1734 assert(!current->is_in_tmp_VTMS_transition(), "sanity check"); 1735 JvmtiVTMSTransitionDisabler::finish_VTMS_transition((jthread)vth.raw_value(), /* is_mount */ true); 1736 1737 if (post_mount_event && JvmtiExport::should_post_vthread_mount()) { 1738 JvmtiExport::post_vthread_mount((jthread)vth.raw_value()); 1739 } else if (!post_mount_event) { 1740 // Preemption cancelled case. Clear the unmount event pending flag. The 1741 // flag might actually not be set if _VTMS_notify_jvmti_events was enabled 1742 // after preemption happened (late binding agents). But since this would 1743 // be a rare case we just do it unconditionally. 1744 current->set_jvmti_unmount_event_pending(false); 1745 } 1746 1747 if (mon != nullptr) { 1748 JvmtiExport::post_monitor_contended_entered(current, mon); 1749 } 1750 JRT_BLOCK_END 1751 1752 clear_anchor(current); 1753 } 1754 #endif // INCLUDE_JVMTI 1755 1756 #ifdef ASSERT 1757 1758 // There are no interpreted frames if we're not called from the interpreter and we haven't ancountered an i2c 1759 // adapter or called Deoptimization::unpack_frames. As for native frames, upcalls from JNI also go through the 1760 // interpreter (see JavaCalls::call_helper), while the UpcallLinker explicitly sets cont_fastpath. 1761 bool FreezeBase::check_valid_fast_path() { 1762 ContinuationEntry* ce = _thread->last_continuation(); 1763 RegisterMap map(_thread, 1764 RegisterMap::UpdateMap::skip, 1765 RegisterMap::ProcessFrames::skip, 1766 RegisterMap::WalkContinuation::skip); 1767 map.set_include_argument_oops(false); 1768 int i = 0; 1769 for (frame f = freeze_start_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map), i++) { 1770 if (!((f.is_compiled_frame() && !f.is_deoptimized_frame()) || (i == 0 && f.is_runtime_frame()))) { 1771 return false; 1772 } 1773 } 1774 return true; 1775 } 1776 #endif // ASSERT 1777 1778 static inline int freeze_epilog(ContinuationWrapper& cont) { 1779 verify_continuation(cont.continuation()); 1780 assert(!cont.is_empty(), ""); 1781 1782 log_develop_debug(continuations)("=== End of freeze cont ### #" INTPTR_FORMAT, cont.hash()); 1783 return 0; 1784 } 1785 1786 static int freeze_epilog(JavaThread* thread, ContinuationWrapper& cont, freeze_result res) { 1787 if (UNLIKELY(res != freeze_ok)) { 1788 verify_continuation(cont.continuation()); 1789 log_develop_trace(continuations)("=== end of freeze (fail %d)", res); 1790 return res; 1791 } 1792 1793 JVMTI_ONLY(jvmti_yield_cleanup(thread, cont)); // can safepoint 1794 return freeze_epilog(cont); 1795 } 1796 1797 static int preempt_epilog(ContinuationWrapper& cont, freeze_result res, frame& old_last_frame) { 1798 if (UNLIKELY(res != freeze_ok)) { 1799 verify_continuation(cont.continuation()); 1800 log_develop_trace(continuations)("=== end of freeze (fail %d)", res); 1801 return res; 1802 } 1803 1804 patch_return_pc_with_preempt_stub(old_last_frame); 1805 cont.set_preempted(true); 1806 cont.tail()->set_is_preempted(true); 1807 1808 if (cont.thread()->is_on_monitorenter()) { 1809 cont.tail()->set_objectMonitor(cont.thread()->current_pending_monitor()); 1810 } 1811 1812 return freeze_epilog(cont); 1813 } 1814 1815 template<typename ConfigT, bool preempt> 1816 static inline int freeze_internal(JavaThread* current, intptr_t* const sp) { 1817 assert(!current->has_pending_exception(), ""); 1818 1819 #ifdef ASSERT 1820 log_trace(continuations)("~~~~ freeze sp: " INTPTR_FORMAT "JavaThread: " INTPTR_FORMAT, p2i(current->last_continuation()->entry_sp()), p2i(current)); 1821 log_frames(current); 1822 #endif 1823 1824 CONT_JFR_ONLY(EventContinuationFreeze event;) 1825 1826 ContinuationEntry* entry = current->last_continuation(); 1827 1828 oop oopCont = entry->cont_oop(current); 1829 assert(oopCont == current->last_continuation()->cont_oop(current), ""); 1830 assert(ContinuationEntry::assert_entry_frame_laid_out(current), ""); 1831 1832 verify_continuation(oopCont); 1833 ContinuationWrapper cont(current, oopCont); 1834 log_develop_debug(continuations)("FREEZE #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont)); 1835 1836 assert(entry->is_virtual_thread() == (entry->scope(current) == java_lang_VirtualThread::vthread_scope()), ""); 1837 1838 const bool pinned_monitor = NOT_LOOM_MONITOR_SUPPORT(current->held_monitor_count() > 0) LOOM_MONITOR_SUPPORT_ONLY(current->jni_monitor_count() > 0); 1839 if (entry->is_pinned() || pinned_monitor) { 1840 log_develop_debug(continuations)("PINNED due to critical section/hold monitor"); 1841 verify_continuation(cont.continuation()); 1842 freeze_result res = entry->is_pinned() ? freeze_pinned_cs : freeze_pinned_monitor; 1843 log_develop_trace(continuations)("=== end of freeze (fail %d)", res); 1844 return res; 1845 } 1846 1847 Freeze<ConfigT> freeze(current, cont, sp, preempt); 1848 1849 if (preempt) { 1850 freeze.set_last_frame(); // remember last frame 1851 #ifdef AARCH64 1852 JvmtiSampledObjectAllocEventCollector jsoaec(false); 1853 freeze.set_jvmti_event_collector(&jsoaec); 1854 1855 // Force aarch64 to slow path always for now. It needs extra instructions to correctly set 1856 // the last pc we copy into the stackChunk, since it will not necessarily be at sp[-1]). 1857 freeze_result res = freeze.freeze_slow(); 1858 CONT_JFR_ONLY(cont.post_jfr_event(&event, oopCont, current);) 1859 return preempt_epilog(cont, res, freeze.last_frame()); 1860 #endif 1861 } 1862 1863 assert(!current->cont_fastpath() || freeze.check_valid_fast_path(), ""); 1864 bool fast = UseContinuationFastPath && current->cont_fastpath(); 1865 if (fast && freeze.size_if_fast_freeze_available() > 0) { 1866 freeze.freeze_fast_existing_chunk(); 1867 CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);) 1868 return !preempt ? freeze_epilog(cont) : preempt_epilog(cont, freeze_ok, freeze.last_frame()); 1869 } 1870 1871 if (preempt) { 1872 JvmtiSampledObjectAllocEventCollector jsoaec(false); 1873 freeze.set_jvmti_event_collector(&jsoaec); 1874 1875 freeze_result res = fast ? freeze.try_freeze_fast() : freeze.freeze_slow(); 1876 1877 CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);) 1878 preempt_epilog(cont, res, freeze.last_frame()); 1879 return res; 1880 } 1881 1882 log_develop_trace(continuations)("chunk unavailable; transitioning to VM"); 1883 assert(current == JavaThread::current(), "must be current thread"); 1884 JRT_BLOCK 1885 // delays a possible JvmtiSampledObjectAllocEventCollector in alloc_chunk 1886 JvmtiSampledObjectAllocEventCollector jsoaec(false); 1887 freeze.set_jvmti_event_collector(&jsoaec); 1888 1889 freeze_result res = fast ? freeze.try_freeze_fast() : freeze.freeze_slow(); 1890 1891 CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);) 1892 freeze_epilog(current, cont, res); 1893 cont.done(); // allow safepoint in the transition back to Java 1894 return res; 1895 JRT_BLOCK_END 1896 } 1897 1898 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint) { 1899 ContinuationEntry* entry = thread->last_continuation(); 1900 if (entry == nullptr) { 1901 return freeze_ok; 1902 } 1903 if (entry->is_pinned()) { 1904 return freeze_pinned_cs; 1905 } else if (thread->held_monitor_count() > 0) { 1906 return freeze_pinned_monitor; 1907 } 1908 1909 RegisterMap map(thread, 1910 RegisterMap::UpdateMap::include, 1911 RegisterMap::ProcessFrames::skip, 1912 RegisterMap::WalkContinuation::skip); 1913 map.set_include_argument_oops(false); 1914 frame f = thread->last_frame(); 1915 1916 if (!safepoint) { 1917 f = f.sender(&map); // this is the yield frame 1918 } else { // safepoint yield 1919 #if (defined(X86) || defined(AARCH64) || defined(RISCV64)) && !defined(ZERO) 1920 f.set_fp(f.real_fp()); // Instead of this, maybe in ContinuationWrapper::set_last_frame always use the real_fp? 1921 #else 1922 Unimplemented(); 1923 #endif 1924 if (!Interpreter::contains(f.pc())) { 1925 assert(ContinuationHelper::Frame::is_stub(f.cb()), "must be"); 1926 assert(f.oop_map() != nullptr, "must be"); 1927 f.oop_map()->update_register_map(&f, &map); // we have callee-save registers in this case 1928 } 1929 } 1930 1931 while (true) { 1932 if ((f.is_interpreted_frame() && f.interpreter_frame_method()->is_native()) || f.is_native_frame()) { 1933 return freeze_pinned_native; 1934 } 1935 1936 f = f.sender(&map); 1937 if (!Continuation::is_frame_in_continuation(entry, f)) { 1938 oop scope = jdk_internal_vm_Continuation::scope(entry->cont_oop(thread)); 1939 if (scope == cont_scope) { 1940 break; 1941 } 1942 intx monitor_count = entry->parent_held_monitor_count(); 1943 entry = entry->parent(); 1944 if (entry == nullptr) { 1945 break; 1946 } 1947 if (entry->is_pinned()) { 1948 return freeze_pinned_cs; 1949 } else if (monitor_count > 0) { 1950 return freeze_pinned_monitor; 1951 } 1952 } 1953 } 1954 return freeze_ok; 1955 } 1956 1957 /////////////// THAW //// 1958 1959 static int thaw_size(stackChunkOop chunk) { 1960 int size = chunk->max_thawing_size(); 1961 size += frame::metadata_words; // For the top pc+fp in push_return_frame or top = stack_sp - frame::metadata_words in thaw_fast 1962 size += 2*frame::align_wiggle; // in case of alignments at the top and bottom 1963 size += frame::metadata_words; // for preemption case (see push_preempt_rerun_adapter) 1964 return size; 1965 } 1966 1967 // make room on the stack for thaw 1968 // returns the size in bytes, or 0 on failure 1969 static inline int prepare_thaw_internal(JavaThread* thread, bool return_barrier) { 1970 log_develop_trace(continuations)("~~~~ prepare_thaw return_barrier: %d", return_barrier); 1971 1972 assert(thread == JavaThread::current(), ""); 1973 1974 ContinuationEntry* ce = thread->last_continuation(); 1975 assert(ce != nullptr, ""); 1976 oop continuation = ce->cont_oop(thread); 1977 assert(continuation == get_continuation(thread), ""); 1978 verify_continuation(continuation); 1979 1980 stackChunkOop chunk = jdk_internal_vm_Continuation::tail(continuation); 1981 assert(chunk != nullptr, ""); 1982 1983 // The tail can be empty because it might still be available for another freeze. 1984 // However, here we want to thaw, so we get rid of it (it will be GCed). 1985 if (UNLIKELY(chunk->is_empty())) { 1986 chunk = chunk->parent(); 1987 assert(chunk != nullptr, ""); 1988 assert(!chunk->is_empty(), ""); 1989 jdk_internal_vm_Continuation::set_tail(continuation, chunk); 1990 } 1991 1992 // Verification 1993 chunk->verify(); 1994 assert(chunk->max_thawing_size() > 0, "chunk invariant violated; expected to not be empty"); 1995 1996 // Only make space for the last chunk because we only thaw from the last chunk 1997 int size = thaw_size(chunk) << LogBytesPerWord; 1998 1999 const address bottom = (address)thread->last_continuation()->entry_sp(); 2000 // 300 is an estimate for stack size taken for this native code, in addition to StackShadowPages 2001 // for the Java frames in the check below. 2002 if (!stack_overflow_check(thread, size + 300, bottom)) { 2003 return 0; 2004 } 2005 2006 log_develop_trace(continuations)("prepare_thaw bottom: " INTPTR_FORMAT " top: " INTPTR_FORMAT " size: %d", 2007 p2i(bottom), p2i(bottom - size), size); 2008 return size; 2009 } 2010 2011 class ThawBase : public StackObj { 2012 protected: 2013 JavaThread* _thread; 2014 ContinuationWrapper& _cont; 2015 CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;) 2016 2017 intptr_t* _fastpath; 2018 bool _barriers; 2019 intptr_t* _top_unextended_sp_before_thaw; 2020 int _align_size; 2021 DEBUG_ONLY(intptr_t* _top_stack_address); 2022 2023 StackChunkFrameStream<ChunkFrames::Mixed> _stream; 2024 2025 NOT_PRODUCT(int _frames;) 2026 2027 protected: 2028 ThawBase(JavaThread* thread, ContinuationWrapper& cont) : 2029 _thread(thread), _cont(cont), 2030 _fastpath(nullptr) { 2031 DEBUG_ONLY(_top_unextended_sp_before_thaw = nullptr;) 2032 assert (cont.tail() != nullptr, "no last chunk"); 2033 DEBUG_ONLY(_top_stack_address = _cont.entrySP() - thaw_size(cont.tail());) 2034 } 2035 2036 void clear_chunk(stackChunkOop chunk); 2037 int remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize); 2038 void copy_from_chunk(intptr_t* from, intptr_t* to, int size); 2039 2040 // fast path 2041 inline void prefetch_chunk_pd(void* start, int size_words); 2042 void patch_return(intptr_t* sp, bool is_last); 2043 2044 intptr_t* handle_preempted_continuation(stackChunkOop original_chunk, intptr_t* sp, bool fast_case); 2045 inline intptr_t* push_preempt_rerun_adapter(frame top, bool is_interpreted_frame); 2046 inline intptr_t* push_preempt_monitorenter_redo(stackChunkOop chunk); 2047 2048 void recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top); 2049 void finish_thaw(frame& f); 2050 2051 private: 2052 template<typename FKind> bool recurse_thaw_java_frame(frame& caller, int num_frames); 2053 void finalize_thaw(frame& entry, int argsize); 2054 2055 inline bool seen_by_gc(); 2056 2057 inline void before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame); 2058 inline void after_thaw_java_frame(const frame& f, bool bottom); 2059 inline void patch(frame& f, const frame& caller, bool bottom); 2060 void clear_bitmap_bits(address start, address end); 2061 2062 NOINLINE void recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames); 2063 void recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller); 2064 void recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames); 2065 2066 void push_return_frame(frame& f); 2067 inline frame new_entry_frame(); 2068 template<typename FKind> frame new_stack_frame(const frame& hf, frame& caller, bool bottom); 2069 inline void patch_pd(frame& f, const frame& sender); 2070 inline void patch_pd(frame& f, intptr_t* caller_sp); 2071 inline intptr_t* align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom); 2072 2073 void maybe_set_fastpath(intptr_t* sp) { if (sp > _fastpath) _fastpath = sp; } 2074 2075 static inline void derelativize_interpreted_frame_metadata(const frame& hf, const frame& f); 2076 2077 public: 2078 CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; }) 2079 }; 2080 2081 template <typename ConfigT> 2082 class Thaw : public ThawBase { 2083 public: 2084 Thaw(JavaThread* thread, ContinuationWrapper& cont) : ThawBase(thread, cont) {} 2085 2086 inline bool can_thaw_fast(stackChunkOop chunk) { 2087 return !_barriers 2088 && _thread->cont_fastpath_thread_state() 2089 && !chunk->has_thaw_slowpath_condition() 2090 && !PreserveFramePointer; 2091 } 2092 2093 inline intptr_t* thaw(Continuation::thaw_kind kind); 2094 NOINLINE intptr_t* thaw_fast(stackChunkOop chunk); 2095 NOINLINE intptr_t* thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind); 2096 inline void patch_caller_links(intptr_t* sp, intptr_t* bottom); 2097 }; 2098 2099 template <typename ConfigT> 2100 inline intptr_t* Thaw<ConfigT>::thaw(Continuation::thaw_kind kind) { 2101 verify_continuation(_cont.continuation()); 2102 assert(!jdk_internal_vm_Continuation::done(_cont.continuation()), ""); 2103 assert(!_cont.is_empty(), ""); 2104 2105 stackChunkOop chunk = _cont.tail(); 2106 assert(chunk != nullptr, "guaranteed by prepare_thaw"); 2107 assert(!chunk->is_empty(), "guaranteed by prepare_thaw"); 2108 2109 _barriers = chunk->requires_barriers(); 2110 return (LIKELY(can_thaw_fast(chunk))) ? thaw_fast(chunk) 2111 : thaw_slow(chunk, kind); 2112 } 2113 2114 class ReconstructedStack : public StackObj { 2115 intptr_t* _base; // _cont.entrySP(); // top of the entry frame 2116 int _thaw_size; 2117 int _argsize; 2118 public: 2119 ReconstructedStack(intptr_t* base, int thaw_size, int argsize) 2120 : _base(base), _thaw_size(thaw_size - (argsize == 0 ? frame::metadata_words_at_top : 0)), _argsize(argsize) { 2121 // The only possible source of misalignment is stack-passed arguments b/c compiled frames are 16-byte aligned. 2122 assert(argsize != 0 || (_base - _thaw_size) == ContinuationHelper::frame_align_pointer(_base - _thaw_size), ""); 2123 // We're at most one alignment word away from entrySP 2124 assert(_base - 1 <= top() + total_size() + frame::metadata_words_at_bottom, "missed entry frame"); 2125 } 2126 2127 int entry_frame_extension() const { return _argsize + (_argsize > 0 ? frame::metadata_words_at_top : 0); } 2128 2129 // top and bottom stack pointers 2130 intptr_t* sp() const { return ContinuationHelper::frame_align_pointer(_base - _thaw_size); } 2131 intptr_t* bottom_sp() const { return ContinuationHelper::frame_align_pointer(_base - entry_frame_extension()); } 2132 2133 // several operations operate on the totality of the stack being reconstructed, 2134 // including the metadata words 2135 intptr_t* top() const { return sp() - frame::metadata_words_at_bottom; } 2136 int total_size() const { return _thaw_size + frame::metadata_words_at_bottom; } 2137 }; 2138 2139 inline void ThawBase::clear_chunk(stackChunkOop chunk) { 2140 chunk->set_sp(chunk->stack_size()); 2141 chunk->set_argsize(0); 2142 chunk->set_max_thawing_size(0); 2143 } 2144 2145 int ThawBase::remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize) { 2146 bool empty = false; 2147 StackChunkFrameStream<ChunkFrames::CompiledOnly> f(chunk); 2148 DEBUG_ONLY(intptr_t* const chunk_sp = chunk->start_address() + chunk->sp();) 2149 assert(chunk_sp == f.sp(), ""); 2150 assert(chunk_sp == f.unextended_sp(), ""); 2151 2152 int frame_size = f.cb()->frame_size(); 2153 argsize = f.stack_argsize(); 2154 bool is_stub = f.is_stub(); 2155 2156 f.next(SmallRegisterMap::instance, true /* stop */); 2157 empty = f.is_done(); 2158 assert(!empty || argsize == chunk->argsize(), ""); 2159 2160 assert(!is_stub || !empty, "runtime stub should have caller frame"); 2161 if (is_stub) { 2162 // If we don't thaw the top compiled frame too, after restoring the saved 2163 // registers back in Java, we would hit the return barrier to thaw one more 2164 // frame effectively overwritting the restored registers during that call. 2165 f.get_cb(); 2166 frame_size += f.cb()->frame_size(); 2167 argsize = f.stack_argsize(); 2168 f.next(SmallRegisterMap::instance, true /* stop */); 2169 empty = f.is_done(); 2170 assert(!empty || argsize == chunk->argsize(), ""); 2171 } 2172 2173 if (empty) { 2174 clear_chunk(chunk); 2175 } else { 2176 chunk->set_sp(chunk->sp() + frame_size); 2177 chunk->set_max_thawing_size(chunk->max_thawing_size() - frame_size); 2178 // We set chunk->pc to the return pc into the next frame 2179 chunk->set_pc(f.pc()); 2180 #ifdef ASSERT 2181 { 2182 intptr_t* retaddr_slot = (chunk_sp 2183 + frame_size 2184 - frame::sender_sp_ret_address_offset()); 2185 assert(f.pc() == ContinuationHelper::return_address_at(retaddr_slot), 2186 "unexpected pc"); 2187 } 2188 #endif 2189 } 2190 assert(empty == chunk->is_empty(), ""); 2191 // returns the size required to store the frame on stack, and because it is a 2192 // compiled frame, it must include a copy of the arguments passed by the caller 2193 return frame_size + argsize + frame::metadata_words_at_top; 2194 } 2195 2196 void ThawBase::copy_from_chunk(intptr_t* from, intptr_t* to, int size) { 2197 assert(to >= _top_stack_address, "overwrote past thawing space" 2198 " to: " INTPTR_FORMAT " top_address: " INTPTR_FORMAT, p2i(to), p2i(_top_stack_address)); 2199 assert(to + size <= _cont.entrySP(), "overwrote past thawing space"); 2200 _cont.tail()->copy_from_chunk_to_stack(from, to, size); 2201 CONT_JFR_ONLY(_jfr_info.record_size_copied(size);) 2202 } 2203 2204 void ThawBase::patch_return(intptr_t* sp, bool is_last) { 2205 log_develop_trace(continuations)("thaw_fast patching -- sp: " INTPTR_FORMAT, p2i(sp)); 2206 2207 address pc = !is_last ? StubRoutines::cont_returnBarrier() : _cont.entryPC(); 2208 ContinuationHelper::patch_return_address_at( 2209 sp - frame::sender_sp_ret_address_offset(), 2210 pc); 2211 } 2212 2213 template <typename ConfigT> 2214 NOINLINE intptr_t* Thaw<ConfigT>::thaw_fast(stackChunkOop chunk) { 2215 assert(chunk == _cont.tail(), ""); 2216 assert(!chunk->has_mixed_frames(), ""); 2217 assert(!chunk->requires_barriers(), ""); 2218 assert(!chunk->has_bitmap(), ""); 2219 assert(!_thread->is_interp_only_mode(), ""); 2220 2221 LogTarget(Trace, continuations) lt; 2222 if (lt.develop_is_enabled()) { 2223 LogStream ls(lt); 2224 ls.print_cr("thaw_fast"); 2225 chunk->print_on(true, &ls); 2226 } 2227 2228 // Below this heuristic, we thaw the whole chunk, above it we thaw just one frame. 2229 static const int threshold = 500; // words 2230 2231 const int full_chunk_size = chunk->stack_size() - chunk->sp(); // this initial size could be reduced if it's a partial thaw 2232 int argsize, thaw_size; 2233 2234 intptr_t* const chunk_sp = chunk->start_address() + chunk->sp(); 2235 2236 bool partial, empty; 2237 if (LIKELY(!TEST_THAW_ONE_CHUNK_FRAME && (full_chunk_size < threshold))) { 2238 prefetch_chunk_pd(chunk->start_address(), full_chunk_size); // prefetch anticipating memcpy starting at highest address 2239 2240 partial = false; 2241 argsize = chunk->argsize(); // must be called *before* clearing the chunk 2242 clear_chunk(chunk); 2243 thaw_size = full_chunk_size; 2244 empty = true; 2245 } else { // thaw a single frame 2246 partial = true; 2247 thaw_size = remove_top_compiled_frame_from_chunk(chunk, argsize); 2248 empty = chunk->is_empty(); 2249 } 2250 2251 // Are we thawing the last frame(s) in the continuation 2252 const bool is_last = empty && chunk->parent() == nullptr; 2253 assert(!is_last || argsize == 0, ""); 2254 2255 log_develop_trace(continuations)("thaw_fast partial: %d is_last: %d empty: %d size: %d argsize: %d entrySP: " PTR_FORMAT, 2256 partial, is_last, empty, thaw_size, argsize, p2i(_cont.entrySP())); 2257 2258 ReconstructedStack rs(_cont.entrySP(), thaw_size, argsize); 2259 2260 // also copy metadata words at frame bottom 2261 copy_from_chunk(chunk_sp - frame::metadata_words_at_bottom, rs.top(), rs.total_size()); 2262 2263 // update the ContinuationEntry 2264 _cont.set_argsize(argsize); 2265 log_develop_trace(continuations)("setting entry argsize: %d", _cont.argsize()); 2266 assert(rs.bottom_sp() == _cont.entry()->bottom_sender_sp(), ""); 2267 2268 // install the return barrier if not last frame, or the entry's pc if last 2269 patch_return(rs.bottom_sp(), is_last); 2270 2271 // insert the back links from callee to caller frames 2272 patch_caller_links(rs.top(), rs.top() + rs.total_size()); 2273 2274 assert(is_last == _cont.is_empty(), ""); 2275 assert(_cont.chunk_invariant(), ""); 2276 2277 #if CONT_JFR 2278 EventContinuationThawFast e; 2279 if (e.should_commit()) { 2280 e.set_id(cast_from_oop<u8>(chunk)); 2281 e.set_size(thaw_size << LogBytesPerWord); 2282 e.set_full(!partial); 2283 e.commit(); 2284 } 2285 #endif 2286 2287 #ifdef ASSERT 2288 set_anchor(_thread, rs.sp()); 2289 log_frames(_thread); 2290 if (LoomDeoptAfterThaw) { 2291 do_deopt_after_thaw(_thread); 2292 } 2293 clear_anchor(_thread); 2294 #endif 2295 2296 return rs.sp(); 2297 } 2298 2299 inline bool ThawBase::seen_by_gc() { 2300 return _barriers || _cont.tail()->is_gc_mode(); 2301 } 2302 2303 template <typename ConfigT> 2304 NOINLINE intptr_t* Thaw<ConfigT>::thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind) { 2305 bool retry_fast_path = false; 2306 2307 bool preempted_case = chunk->is_preempted(); 2308 if (preempted_case) { 2309 assert(_cont.is_preempted(), "must be"); 2310 assert(chunk->objectMonitor() != nullptr, "must be"); 2311 2312 ObjectMonitor* mon = chunk->objectMonitor(); 2313 if (!mon->is_owner(_thread)) { 2314 return push_preempt_monitorenter_redo(chunk); 2315 } 2316 chunk->set_is_preempted(false); 2317 retry_fast_path = true; 2318 } 2319 2320 #if INCLUDE_ZGC || INCLUDE_SHENANDOAHGC 2321 if (UseZGC || UseShenandoahGC) { 2322 _cont.tail()->relativize_derived_pointers_concurrently(); 2323 } 2324 #endif 2325 2326 // First thaw after freeze. If there were oops in the stacklock 2327 // during freeze, restore them now. 2328 if (chunk->lockStackSize() > 0) { 2329 int lockStackSize = chunk->lockStackSize(); 2330 assert(lockStackSize > 0, "should be"); 2331 2332 oop tmp_lockstack[8]; 2333 chunk->copy_lockstack(tmp_lockstack); 2334 _thread->lock_stack().move_from_address(tmp_lockstack, lockStackSize); 2335 2336 chunk->set_lockStackSize(0); 2337 chunk->set_has_lockStack(false); 2338 retry_fast_path = true; 2339 } 2340 2341 // Retry the fast path now that we possibly cleared the FLAG_HAS_LOCKSTACK 2342 // and FLAG_PREEMPTED flags from the stackChunk. 2343 if (retry_fast_path && can_thaw_fast(chunk)) { 2344 intptr_t* sp = thaw_fast(chunk); 2345 if (preempted_case) { 2346 assert(_cont.is_preempted(), "must be"); 2347 return handle_preempted_continuation(chunk, sp, true /* fast_case */); 2348 } 2349 return sp; 2350 } 2351 2352 LogTarget(Trace, continuations) lt; 2353 if (lt.develop_is_enabled()) { 2354 LogStream ls(lt); 2355 ls.print_cr("thaw slow return_barrier: %d " INTPTR_FORMAT, kind, p2i(chunk)); 2356 chunk->print_on(true, &ls); 2357 } 2358 2359 #if CONT_JFR 2360 EventContinuationThawSlow e; 2361 if (e.should_commit()) { 2362 e.set_id(cast_from_oop<u8>(_cont.continuation())); 2363 e.commit(); 2364 } 2365 #endif 2366 2367 DEBUG_ONLY(_frames = 0;) 2368 _align_size = 0; 2369 bool is_return_barrier = kind != Continuation::thaw_top; 2370 int num_frames = (is_return_barrier ? 1 : 2); 2371 2372 _stream = StackChunkFrameStream<ChunkFrames::Mixed>(chunk); 2373 _top_unextended_sp_before_thaw = _stream.unextended_sp(); 2374 2375 stackChunkOop original_chunk = chunk; 2376 2377 frame heap_frame = _stream.to_frame(); 2378 if (lt.develop_is_enabled()) { 2379 LogStream ls(lt); 2380 ls.print_cr("top hframe before (thaw):"); 2381 assert(heap_frame.is_heap_frame(), "should have created a relative frame"); 2382 heap_frame.print_value_on(&ls, nullptr); 2383 } 2384 2385 frame caller; // the thawed caller on the stack 2386 recurse_thaw(heap_frame, caller, num_frames, true); 2387 finish_thaw(caller); // caller is now the topmost thawed frame 2388 _cont.write(); 2389 2390 assert(_cont.chunk_invariant(), ""); 2391 2392 JVMTI_ONLY(if (!is_return_barrier) invalidate_jvmti_stack(_thread)); 2393 2394 _thread->set_cont_fastpath(_fastpath); 2395 2396 intptr_t* sp = caller.sp(); 2397 2398 if (preempted_case) { 2399 assert(_cont.is_preempted(), "must be"); 2400 return handle_preempted_continuation(original_chunk, sp, false /* fast_case */); 2401 } 2402 return sp; 2403 } 2404 2405 void ThawBase::recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top) { 2406 log_develop_debug(continuations)("thaw num_frames: %d", num_frames); 2407 assert(!_cont.is_empty(), "no more frames"); 2408 assert(num_frames > 0, ""); 2409 assert(!heap_frame.is_empty(), ""); 2410 2411 if (top && ContinuationHelper::Frame::is_stub(heap_frame.cb())) { 2412 recurse_thaw_stub_frame(heap_frame, caller, num_frames); 2413 } else if (!heap_frame.is_interpreted_frame()) { 2414 recurse_thaw_compiled_frame(heap_frame, caller, num_frames, false); 2415 } else { 2416 recurse_thaw_interpreted_frame(heap_frame, caller, num_frames); 2417 } 2418 } 2419 2420 template<typename FKind> 2421 bool ThawBase::recurse_thaw_java_frame(frame& caller, int num_frames) { 2422 assert(num_frames > 0, ""); 2423 2424 DEBUG_ONLY(_frames++;) 2425 2426 int argsize = _stream.stack_argsize(); 2427 2428 _stream.next(SmallRegisterMap::instance); 2429 assert(_stream.to_frame().is_empty() == _stream.is_done(), ""); 2430 2431 // we never leave a compiled caller of an interpreted frame as the top frame in the chunk 2432 // as it makes detecting that situation and adjusting unextended_sp tricky 2433 if (num_frames == 1 && !_stream.is_done() && FKind::interpreted && _stream.is_compiled()) { 2434 log_develop_trace(continuations)("thawing extra compiled frame to not leave a compiled interpreted-caller at top"); 2435 num_frames++; 2436 } 2437 2438 if (num_frames == 1 || _stream.is_done()) { // end recursion 2439 finalize_thaw(caller, FKind::interpreted ? 0 : argsize); 2440 return true; // bottom 2441 } else { // recurse 2442 recurse_thaw(_stream.to_frame(), caller, num_frames - 1, false); 2443 return false; 2444 } 2445 } 2446 2447 void ThawBase::finalize_thaw(frame& entry, int argsize) { 2448 stackChunkOop chunk = _cont.tail(); 2449 2450 if (!_stream.is_done()) { 2451 assert(_stream.sp() >= chunk->sp_address(), ""); 2452 chunk->set_sp(chunk->to_offset(_stream.sp())); 2453 chunk->set_pc(_stream.pc()); 2454 } else { 2455 chunk->set_argsize(0); 2456 chunk->set_sp(chunk->stack_size()); 2457 chunk->set_pc(nullptr); 2458 } 2459 assert(_stream.is_done() == chunk->is_empty(), ""); 2460 2461 int total_thawed = pointer_delta_as_int(_stream.unextended_sp(), _top_unextended_sp_before_thaw); 2462 chunk->set_max_thawing_size(chunk->max_thawing_size() - total_thawed); 2463 2464 _cont.set_argsize(argsize); 2465 entry = new_entry_frame(); 2466 2467 assert(entry.sp() == _cont.entrySP(), ""); 2468 assert(Continuation::is_continuation_enterSpecial(entry), ""); 2469 assert(_cont.is_entry_frame(entry), ""); 2470 } 2471 2472 inline void ThawBase::before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame) { 2473 LogTarget(Trace, continuations) lt; 2474 if (lt.develop_is_enabled()) { 2475 LogStream ls(lt); 2476 ls.print_cr("======== THAWING FRAME: %d", num_frame); 2477 assert(hf.is_heap_frame(), "should be"); 2478 hf.print_value_on(&ls, nullptr); 2479 } 2480 assert(bottom == _cont.is_entry_frame(caller), "bottom: %d is_entry_frame: %d", bottom, _cont.is_entry_frame(hf)); 2481 } 2482 2483 inline void ThawBase::after_thaw_java_frame(const frame& f, bool bottom) { 2484 #ifdef ASSERT 2485 LogTarget(Trace, continuations) lt; 2486 if (lt.develop_is_enabled()) { 2487 LogStream ls(lt); 2488 ls.print_cr("thawed frame:"); 2489 print_frame_layout(f, false, &ls); // f.print_on(&ls); 2490 } 2491 #endif 2492 } 2493 2494 inline void ThawBase::patch(frame& f, const frame& caller, bool bottom) { 2495 assert(!bottom || caller.fp() == _cont.entryFP(), ""); 2496 if (bottom) { 2497 ContinuationHelper::Frame::patch_pc(caller, _cont.is_empty() ? caller.pc() 2498 : StubRoutines::cont_returnBarrier()); 2499 } else { 2500 // caller might have been deoptimized during thaw but we've overwritten the return address when copying f from the heap. 2501 // If the caller is not deoptimized, pc is unchanged. 2502 ContinuationHelper::Frame::patch_pc(caller, caller.raw_pc()); 2503 } 2504 2505 patch_pd(f, caller); 2506 2507 if (f.is_interpreted_frame()) { 2508 ContinuationHelper::InterpretedFrame::patch_sender_sp(f, caller); 2509 } 2510 2511 assert(!bottom || !_cont.is_empty() || Continuation::is_continuation_entry_frame(f, nullptr), ""); 2512 assert(!bottom || (_cont.is_empty() != Continuation::is_cont_barrier_frame(f)), ""); 2513 } 2514 2515 void ThawBase::clear_bitmap_bits(address start, address end) { 2516 assert(is_aligned(start, wordSize), "should be aligned: " PTR_FORMAT, p2i(start)); 2517 assert(is_aligned(end, VMRegImpl::stack_slot_size), "should be aligned: " PTR_FORMAT, p2i(end)); 2518 2519 // we need to clear the bits that correspond to arguments as they reside in the caller frame 2520 // or they will keep objects that are otherwise unreachable alive. 2521 2522 // Align `end` if UseCompressedOops is not set to avoid UB when calculating the bit index, since 2523 // `end` could be at an odd number of stack slots from `start`, i.e might not be oop aligned. 2524 // If that's the case the bit range corresponding to the last stack slot should not have bits set 2525 // anyways and we assert that before returning. 2526 address effective_end = UseCompressedOops ? end : align_down(end, wordSize); 2527 log_develop_trace(continuations)("clearing bitmap for " INTPTR_FORMAT " - " INTPTR_FORMAT, p2i(start), p2i(effective_end)); 2528 stackChunkOop chunk = _cont.tail(); 2529 chunk->bitmap().clear_range(chunk->bit_index_for(start), chunk->bit_index_for(effective_end)); 2530 assert(effective_end == end || !chunk->bitmap().at(chunk->bit_index_for(effective_end)), "bit should not be set"); 2531 } 2532 2533 intptr_t* ThawBase::handle_preempted_continuation(stackChunkOop original_chunk, intptr_t* sp, bool fast_case) { 2534 frame top(sp); 2535 assert(top.pc() == *(address*)(sp - frame::sender_sp_ret_address_offset()), ""); 2536 bool top_is_interpreted = Interpreter::contains(top.pc()); 2537 2538 if (fast_case) { 2539 assert(ContinuationHelper::Frame::is_stub(top.cb()), "invariant"); 2540 int fsize = ContinuationHelper::StubFrame::size(top); 2541 patch_pd(top, sp + fsize); 2542 } 2543 2544 _cont.set_preempted(false); 2545 bool same_chunk = original_chunk == _cont.tail(); 2546 2547 ObjectMonitor* mon = original_chunk->objectMonitor(); 2548 if (same_chunk && mon != nullptr) { 2549 original_chunk->set_objectMonitor(nullptr); 2550 } 2551 2552 bool post_mount_event = true; 2553 if (_thread->preemption_cancelled()) { 2554 // Since we never actually unmounted don't post the mount event. 2555 post_mount_event = false; 2556 _thread->set_preemption_cancelled(false); 2557 } 2558 2559 #if INCLUDE_JVMTI 2560 bool is_vthread = Continuation::continuation_scope(_cont.continuation()) == java_lang_VirtualThread::vthread_scope(); 2561 if (is_vthread) { 2562 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) { 2563 jvmti_mount_end(_thread, _cont, top, mon, post_mount_event); 2564 } else { 2565 _thread->set_is_in_VTMS_transition(false); 2566 java_lang_Thread::set_is_in_VTMS_transition(_thread->vthread(), false); 2567 } 2568 } 2569 #endif 2570 2571 if (!top_is_interpreted) { 2572 assert(ContinuationHelper::Frame::is_stub(top.cb()), "invariant"); 2573 // The continuation might now run on a different platform thread than the previous time so 2574 // we need to adjust the current thread saved in the stub frame before restoring registers. 2575 JavaThread** thread_addr = frame::saved_thread_address(top); 2576 if (thread_addr != nullptr) *thread_addr = _thread; 2577 } 2578 sp = push_preempt_rerun_adapter(top, top_is_interpreted /* is_interpreted_frame */); 2579 return sp; 2580 } 2581 2582 NOINLINE void ThawBase::recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames) { 2583 assert(hf.is_interpreted_frame(), ""); 2584 2585 if (UNLIKELY(seen_by_gc())) { 2586 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance); 2587 } 2588 2589 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::InterpretedFrame>(caller, num_frames); 2590 2591 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);) 2592 2593 _align_size += frame::align_wiggle; // possible added alignment for internal interpreted frame alignment om AArch64 2594 2595 frame f = new_stack_frame<ContinuationHelper::InterpretedFrame>(hf, caller, is_bottom_frame); 2596 2597 intptr_t* const stack_frame_top = f.sp() + frame::metadata_words_at_top; 2598 intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f); 2599 intptr_t* const heap_frame_top = hf.unextended_sp() + frame::metadata_words_at_top; 2600 intptr_t* const heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf); 2601 2602 assert(hf.is_heap_frame(), "should be"); 2603 assert(!f.is_heap_frame(), "should not be"); 2604 2605 const int fsize = pointer_delta_as_int(heap_frame_bottom, heap_frame_top); 2606 assert((stack_frame_bottom == stack_frame_top + fsize), ""); 2607 2608 // Some architectures (like AArch64/PPC64/RISC-V) add padding between the locals and the fixed_frame to keep the fp 16-byte-aligned. 2609 // On those architectures we freeze the padding in order to keep the same fp-relative offsets in the fixed_frame. 2610 copy_from_chunk(heap_frame_top, stack_frame_top, fsize); 2611 2612 // Make sure the relativized locals is already set. 2613 assert(f.interpreter_frame_local_at(0) == stack_frame_bottom - 1, "invalid frame bottom"); 2614 2615 derelativize_interpreted_frame_metadata(hf, f); 2616 patch(f, caller, is_bottom_frame); 2617 2618 assert(f.is_interpreted_frame_valid(_cont.thread()), "invalid thawed frame"); 2619 assert(stack_frame_bottom <= ContinuationHelper::Frame::frame_top(caller), ""); 2620 2621 CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();) 2622 2623 maybe_set_fastpath(f.sp()); 2624 2625 const int locals = hf.interpreter_frame_method()->max_locals(); 2626 2627 if (!is_bottom_frame) { 2628 // can only fix caller once this frame is thawed (due to callee saved regs) 2629 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance); 2630 } else if (_cont.tail()->has_bitmap() && locals > 0) { 2631 assert(hf.is_heap_frame(), "should be"); 2632 address start = (address)(heap_frame_bottom - locals); 2633 address end = (address)heap_frame_bottom; 2634 clear_bitmap_bits(start, end); 2635 } 2636 2637 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);) 2638 caller = f; 2639 } 2640 2641 void ThawBase::recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller) { 2642 assert(hf.is_compiled_frame(), ""); 2643 assert(_cont.is_preempted() || !stub_caller, "stub caller not at preemption"); 2644 2645 if (!stub_caller && UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap 2646 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance); 2647 } 2648 2649 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::CompiledFrame>(caller, num_frames); 2650 2651 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);) 2652 2653 assert(caller.sp() == caller.unextended_sp(), ""); 2654 2655 if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) { 2656 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_compiled_frame 2657 } 2658 2659 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not 2660 // yet laid out in the stack, and so the original_pc is not stored in it. 2661 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized. 2662 frame f = new_stack_frame<ContinuationHelper::CompiledFrame>(hf, caller, is_bottom_frame); 2663 intptr_t* const stack_frame_top = f.sp(); 2664 intptr_t* const heap_frame_top = hf.unextended_sp(); 2665 2666 const int added_argsize = (is_bottom_frame || caller.is_interpreted_frame()) ? hf.compiled_frame_stack_argsize() : 0; 2667 int fsize = ContinuationHelper::CompiledFrame::size(hf) + added_argsize; 2668 assert(fsize <= (int)(caller.unextended_sp() - f.unextended_sp()), ""); 2669 2670 intptr_t* from = heap_frame_top - frame::metadata_words_at_bottom; 2671 intptr_t* to = stack_frame_top - frame::metadata_words_at_bottom; 2672 // copy metadata, except the metadata at the top of the (unextended) entry frame 2673 int sz = fsize + frame::metadata_words_at_bottom + (is_bottom_frame && added_argsize == 0 ? 0 : frame::metadata_words_at_top); 2674 2675 // If we're the bottom-most thawed frame, we're writing to within one word from entrySP 2676 // (we might have one padding word for alignment) 2677 assert(!is_bottom_frame || (_cont.entrySP() - 1 <= to + sz && to + sz <= _cont.entrySP()), ""); 2678 assert(!is_bottom_frame || hf.compiled_frame_stack_argsize() != 0 || (to + sz && to + sz == _cont.entrySP()), ""); 2679 2680 copy_from_chunk(from, to, sz); // copying good oops because we invoked barriers above 2681 2682 patch(f, caller, is_bottom_frame); 2683 2684 // f.is_deoptimized_frame() is always false and we must test hf.is_deoptimized_frame() (see comment above) 2685 assert(!f.is_deoptimized_frame(), ""); 2686 if (hf.is_deoptimized_frame()) { 2687 maybe_set_fastpath(f.sp()); 2688 } else if (_thread->is_interp_only_mode() 2689 || (_cont.is_preempted() && f.cb()->as_nmethod()->is_marked_for_deoptimization())) { 2690 // The caller of the safepoint stub when the continuation is preempted is not at a call instruction, and so 2691 // cannot rely on nmethod patching for deopt. 2692 2693 log_develop_trace(continuations)("Deoptimizing thawed frame"); 2694 DEBUG_ONLY(ContinuationHelper::Frame::patch_pc(f, nullptr)); 2695 2696 f.deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for 2697 assert(f.is_deoptimized_frame(), ""); 2698 assert(ContinuationHelper::Frame::is_deopt_return(f.raw_pc(), f), ""); 2699 maybe_set_fastpath(f.sp()); 2700 } 2701 2702 if (!is_bottom_frame) { 2703 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack 2704 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance); 2705 } else if (_cont.tail()->has_bitmap() && added_argsize > 0) { 2706 address start = (address)(heap_frame_top + ContinuationHelper::CompiledFrame::size(hf) + frame::metadata_words_at_top); 2707 int stack_args_slots = f.cb()->as_nmethod()->num_stack_arg_slots(false /* rounded */); 2708 int argsize_in_bytes = stack_args_slots * VMRegImpl::stack_slot_size; 2709 clear_bitmap_bits(start, start + argsize_in_bytes); 2710 } 2711 2712 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);) 2713 caller = f; 2714 } 2715 2716 void ThawBase::recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames) { 2717 DEBUG_ONLY(_frames++;) 2718 bool is_bottom_frame = false; 2719 2720 if (UNLIKELY(seen_by_gc())) { 2721 // Process the stub's caller here since we might need the full map (if the stub was 2722 // generated on a poll on return we shouldn't need a full map). 2723 RegisterMap map(nullptr, 2724 RegisterMap::UpdateMap::include, 2725 RegisterMap::ProcessFrames::skip, 2726 RegisterMap::WalkContinuation::skip); 2727 map.set_include_argument_oops(false); 2728 _stream.next(&map); 2729 if (!_stream.is_done()) { 2730 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, &map); 2731 } 2732 } else { 2733 _stream.next(SmallRegisterMap::instance); 2734 } 2735 2736 if (_stream.is_done()) { 2737 finalize_thaw(caller, 0); 2738 is_bottom_frame = true; 2739 } else { 2740 frame f = _stream.to_frame(); 2741 if (f.is_interpreted_frame()) { 2742 recurse_thaw_interpreted_frame(f, caller, num_frames); 2743 } else { 2744 recurse_thaw_compiled_frame(f, caller, num_frames, true); 2745 } 2746 } 2747 2748 assert(!is_bottom_frame || !_cont.is_empty(), ""); 2749 assert(caller.sp() == caller.unextended_sp(), ""); 2750 assert(!caller.is_native_frame() || (is_bottom_frame && Continuation::is_continuation_enterSpecial(caller)), "caller should't be native except for enterSpecial case"); 2751 2752 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);) 2753 2754 if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) { 2755 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_stub_frame 2756 } 2757 2758 frame f = new_stack_frame<ContinuationHelper::StubFrame>(hf, caller, false); 2759 intptr_t* stack_frame_top = f.sp(); 2760 intptr_t* heap_frame_top = hf.sp(); 2761 int fsize = ContinuationHelper::StubFrame::size(hf); 2762 2763 copy_from_chunk(heap_frame_top - frame::metadata_words, stack_frame_top - frame::metadata_words, 2764 fsize + frame::metadata_words); 2765 2766 patch(f, caller, is_bottom_frame); 2767 2768 if (!is_bottom_frame) { 2769 // can only fix caller once this frame is thawed (due to callee saved regs) 2770 RegisterMap map(nullptr, 2771 RegisterMap::UpdateMap::include, 2772 RegisterMap::ProcessFrames::skip, 2773 RegisterMap::WalkContinuation::skip); 2774 map.set_include_argument_oops(false); 2775 f.oop_map()->update_register_map(&f, &map); 2776 ContinuationHelper::update_register_map_with_callee(caller, &map); 2777 _cont.tail()->fix_thawed_frame(caller, &map); 2778 } 2779 2780 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);) 2781 caller = f; 2782 } 2783 2784 void ThawBase::finish_thaw(frame& f) { 2785 stackChunkOop chunk = _cont.tail(); 2786 2787 if (chunk->is_empty()) { 2788 // Only remove chunk from list if it can't be reused for another freeze 2789 if (seen_by_gc()) { 2790 _cont.set_tail(chunk->parent()); 2791 } else { 2792 chunk->set_has_mixed_frames(false); 2793 } 2794 chunk->set_max_thawing_size(0); 2795 assert(chunk->argsize() == 0, ""); 2796 } else { 2797 chunk->set_max_thawing_size(chunk->max_thawing_size() - _align_size); 2798 } 2799 assert(chunk->is_empty() == (chunk->max_thawing_size() == 0), ""); 2800 2801 if (!is_aligned(f.sp(), frame::frame_alignment)) { 2802 assert(f.is_interpreted_frame(), ""); 2803 f.set_sp(align_down(f.sp(), frame::frame_alignment)); 2804 } 2805 push_return_frame(f); 2806 chunk->fix_thawed_frame(f, SmallRegisterMap::instance); // can only fix caller after push_return_frame (due to callee saved regs) 2807 2808 assert(_cont.is_empty() == _cont.last_frame().is_empty(), ""); 2809 2810 log_develop_trace(continuations)("thawed %d frames", _frames); 2811 2812 LogTarget(Trace, continuations) lt; 2813 if (lt.develop_is_enabled()) { 2814 LogStream ls(lt); 2815 ls.print_cr("top hframe after (thaw):"); 2816 _cont.last_frame().print_value_on(&ls, nullptr); 2817 } 2818 } 2819 2820 void ThawBase::push_return_frame(frame& f) { // see generate_cont_thaw 2821 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == f.cb()->as_nmethod()->is_deopt_pc(f.raw_pc()), ""); 2822 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == (f.pc() != f.raw_pc()), ""); 2823 2824 LogTarget(Trace, continuations) lt; 2825 if (lt.develop_is_enabled()) { 2826 LogStream ls(lt); 2827 ls.print_cr("push_return_frame"); 2828 f.print_value_on(&ls, nullptr); 2829 } 2830 2831 assert(f.sp() - frame::metadata_words_at_bottom >= _top_stack_address, "overwrote past thawing space" 2832 " to: " INTPTR_FORMAT " top_address: " INTPTR_FORMAT, p2i(f.sp() - frame::metadata_words), p2i(_top_stack_address)); 2833 ContinuationHelper::Frame::patch_pc(f, f.raw_pc()); // in case we want to deopt the frame in a full transition, this is checked. 2834 ContinuationHelper::push_pd(f); 2835 2836 assert(ContinuationHelper::Frame::assert_frame_laid_out(f), ""); 2837 } 2838 2839 // returns new top sp 2840 // called after preparations (stack overflow check and making room) 2841 template<typename ConfigT> 2842 static inline intptr_t* thaw_internal(JavaThread* thread, const Continuation::thaw_kind kind) { 2843 assert(thread == JavaThread::current(), "Must be current thread"); 2844 2845 CONT_JFR_ONLY(EventContinuationThaw event;) 2846 2847 log_develop_trace(continuations)("~~~~ thaw kind: %d sp: " INTPTR_FORMAT, kind, p2i(thread->last_continuation()->entry_sp())); 2848 2849 ContinuationEntry* entry = thread->last_continuation(); 2850 assert(entry != nullptr, ""); 2851 oop oopCont = entry->cont_oop(thread); 2852 2853 assert(!jdk_internal_vm_Continuation::done(oopCont), ""); 2854 assert(oopCont == get_continuation(thread), ""); 2855 verify_continuation(oopCont); 2856 2857 assert(entry->is_virtual_thread() == (entry->scope(thread) == java_lang_VirtualThread::vthread_scope()), ""); 2858 2859 ContinuationWrapper cont(thread, oopCont); 2860 log_develop_debug(continuations)("THAW #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont)); 2861 2862 #ifdef ASSERT 2863 set_anchor_to_entry(thread, cont.entry()); 2864 log_frames(thread); 2865 clear_anchor(thread); 2866 #endif 2867 2868 Thaw<ConfigT> thw(thread, cont); 2869 intptr_t* const sp = thw.thaw(kind); 2870 assert(is_aligned(sp, frame::frame_alignment), ""); 2871 2872 #ifdef ASSERT 2873 intptr_t* sp0 = sp; 2874 address pc0 = *(address*)(sp - frame::sender_sp_ret_address_offset()); 2875 2876 bool sent_entry_pc = false; 2877 if (pc0 == Interpreter::cont_preempt_rerun_interpreter_adapter() || 2878 pc0 == StubRoutines::cont_preempt_rerun_compiler_adapter()) { 2879 sp0 += frame::metadata_words; // see push_preempt_rerun_adapter 2880 #ifdef AARCH64 2881 if (pc0 == StubRoutines::cont_preempt_rerun_compiler_adapter()) { 2882 address pc1 = *(address*)(sp0 - frame::sender_sp_ret_address_offset()); 2883 CodeBlob* cb = CodeCache::find_blob(pc1); 2884 assert(cb != nullptr, "should be either c1 or c2 runtime stub"); 2885 if (cb->frame_size() == 2) { 2886 sp0 += frame::metadata_words; 2887 } 2888 } 2889 #endif 2890 } else if (pc0 == StubRoutines::cont_preempt_monitorenter_redo()) { 2891 sp0 += 2 * frame::metadata_words; // see push_preempt_monitorenter_redo 2892 sent_entry_pc = true; 2893 } 2894 set_anchor(thread, sp0, sent_entry_pc ? cont.entryPC() : nullptr); 2895 log_frames(thread); 2896 if (LoomVerifyAfterThaw) { 2897 assert(do_verify_after_thaw(thread, cont.tail(), tty), ""); 2898 } 2899 assert(ContinuationEntry::assert_entry_frame_laid_out(thread), ""); 2900 clear_anchor(thread); 2901 2902 LogTarget(Trace, continuations) lt; 2903 if (lt.develop_is_enabled()) { 2904 LogStream ls(lt); 2905 ls.print_cr("Jumping to frame (thaw):"); 2906 frame(sp).print_value_on(&ls, nullptr); 2907 } 2908 #endif 2909 2910 CONT_JFR_ONLY(thw.jfr_info().post_jfr_event(&event, cont.continuation(), thread);) 2911 2912 verify_continuation(cont.continuation()); 2913 log_develop_debug(continuations)("=== End of thaw #" INTPTR_FORMAT, cont.hash()); 2914 2915 return sp; 2916 } 2917 2918 #ifdef ASSERT 2919 static void do_deopt_after_thaw(JavaThread* thread) { 2920 int i = 0; 2921 StackFrameStream fst(thread, true, false); 2922 fst.register_map()->set_include_argument_oops(false); 2923 ContinuationHelper::update_register_map_with_callee(*fst.current(), fst.register_map()); 2924 for (; !fst.is_done(); fst.next()) { 2925 if (fst.current()->cb()->is_nmethod()) { 2926 nmethod* nm = fst.current()->cb()->as_nmethod(); 2927 if (!nm->method()->is_continuation_native_intrinsic()) { 2928 nm->make_deoptimized(); 2929 } 2930 } 2931 } 2932 } 2933 2934 class ThawVerifyOopsClosure: public OopClosure { 2935 intptr_t* _p; 2936 outputStream* _st; 2937 bool is_good_oop(oop o) { 2938 return dbg_is_safe(o, -1) && dbg_is_safe(o->klass(), -1) && oopDesc::is_oop(o) && o->klass()->is_klass(); 2939 } 2940 public: 2941 ThawVerifyOopsClosure(outputStream* st) : _p(nullptr), _st(st) {} 2942 intptr_t* p() { return _p; } 2943 void reset() { _p = nullptr; } 2944 2945 virtual void do_oop(oop* p) { 2946 oop o = *p; 2947 if (o == nullptr || is_good_oop(o)) { 2948 return; 2949 } 2950 _p = (intptr_t*)p; 2951 _st->print_cr("*** non-oop " PTR_FORMAT " found at " PTR_FORMAT, p2i(*p), p2i(p)); 2952 } 2953 virtual void do_oop(narrowOop* p) { 2954 oop o = RawAccess<>::oop_load(p); 2955 if (o == nullptr || is_good_oop(o)) { 2956 return; 2957 } 2958 _p = (intptr_t*)p; 2959 _st->print_cr("*** (narrow) non-oop %x found at " PTR_FORMAT, (int)(*p), p2i(p)); 2960 } 2961 }; 2962 2963 static bool do_verify_after_thaw(JavaThread* thread, stackChunkOop chunk, outputStream* st) { 2964 assert(thread->has_last_Java_frame(), ""); 2965 2966 ResourceMark rm; 2967 ThawVerifyOopsClosure cl(st); 2968 NMethodToOopClosure cf(&cl, false); 2969 2970 StackFrameStream fst(thread, true, false); 2971 fst.register_map()->set_include_argument_oops(false); 2972 ContinuationHelper::update_register_map_with_callee(*fst.current(), fst.register_map()); 2973 for (; !fst.is_done() && !Continuation::is_continuation_enterSpecial(*fst.current()); fst.next()) { 2974 if (fst.current()->cb()->is_nmethod() && fst.current()->cb()->as_nmethod()->is_marked_for_deoptimization()) { 2975 st->print_cr(">>> do_verify_after_thaw deopt"); 2976 fst.current()->deoptimize(nullptr); 2977 fst.current()->print_on(st); 2978 } 2979 2980 fst.current()->oops_do(&cl, &cf, fst.register_map()); 2981 if (cl.p() != nullptr) { 2982 frame fr = *fst.current(); 2983 st->print_cr("Failed for frame barriers: %d",chunk->requires_barriers()); 2984 fr.print_on(st); 2985 if (!fr.is_interpreted_frame()) { 2986 st->print_cr("size: %d argsize: %d", 2987 ContinuationHelper::NonInterpretedUnknownFrame::size(fr), 2988 ContinuationHelper::NonInterpretedUnknownFrame::stack_argsize(fr)); 2989 } 2990 VMReg reg = fst.register_map()->find_register_spilled_here(cl.p(), fst.current()->sp()); 2991 if (reg != nullptr) { 2992 st->print_cr("Reg %s %d", reg->name(), reg->is_stack() ? (int)reg->reg2stack() : -99); 2993 } 2994 cl.reset(); 2995 DEBUG_ONLY(thread->print_frame_layout();) 2996 if (chunk != nullptr) { 2997 chunk->print_on(true, st); 2998 } 2999 return false; 3000 } 3001 } 3002 return true; 3003 } 3004 3005 static void log_frames(JavaThread* thread) { 3006 const static int show_entry_callers = 100; 3007 LogTarget(Trace, continuations) lt; 3008 if (!lt.develop_is_enabled()) { 3009 return; 3010 } 3011 LogStream ls(lt); 3012 3013 ls.print_cr("------- frames --------- for thread " INTPTR_FORMAT, p2i(thread)); 3014 if (!thread->has_last_Java_frame()) { 3015 ls.print_cr("NO ANCHOR!"); 3016 } 3017 3018 RegisterMap map(thread, 3019 RegisterMap::UpdateMap::include, 3020 RegisterMap::ProcessFrames::include, 3021 RegisterMap::WalkContinuation::skip); 3022 map.set_include_argument_oops(false); 3023 3024 if (false) { 3025 for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) { 3026 f.print_on(&ls); 3027 } 3028 } else { 3029 map.set_skip_missing(true); 3030 ResetNoHandleMark rnhm; 3031 ResourceMark rm; 3032 HandleMark hm(Thread::current()); 3033 FrameValues values; 3034 3035 int i = 0; 3036 int post_entry = -1; 3037 for (frame f = thread->last_frame(); !f.is_first_frame(); f = f.sender(&map), i++) { 3038 f.describe(values, i, &map, i == 0); 3039 if (post_entry >= 0 || Continuation::is_continuation_enterSpecial(f)) 3040 post_entry++; 3041 if (post_entry >= show_entry_callers) 3042 break; 3043 } 3044 values.print_on(thread, &ls); 3045 } 3046 3047 ls.print_cr("======= end frames ========="); 3048 } 3049 #endif // ASSERT 3050 3051 #include CPU_HEADER_INLINE(continuationFreezeThaw) 3052 3053 #ifdef ASSERT 3054 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st) { 3055 ResourceMark rm; 3056 FrameValues values; 3057 assert(f.get_cb() != nullptr, ""); 3058 RegisterMap map(f.is_heap_frame() ? 3059 nullptr : 3060 JavaThread::current(), 3061 RegisterMap::UpdateMap::include, 3062 RegisterMap::ProcessFrames::skip, 3063 RegisterMap::WalkContinuation::skip); 3064 map.set_include_argument_oops(false); 3065 map.set_skip_missing(true); 3066 if (callee_complete) { 3067 frame::update_map_with_saved_link(&map, ContinuationHelper::Frame::callee_link_address(f)); 3068 } 3069 const_cast<frame&>(f).describe(values, 0, &map, true); 3070 values.print_on(static_cast<JavaThread*>(nullptr), st); 3071 } 3072 #endif 3073 3074 static address thaw_entry = nullptr; 3075 static address freeze_entry = nullptr; 3076 static address freeze_preempt_entry = nullptr; 3077 3078 address Continuation::thaw_entry() { 3079 return ::thaw_entry; 3080 } 3081 3082 address Continuation::freeze_entry() { 3083 return ::freeze_entry; 3084 } 3085 3086 address Continuation::freeze_preempt_entry() { 3087 return ::freeze_preempt_entry; 3088 } 3089 3090 class ConfigResolve { 3091 public: 3092 static void resolve() { resolve_compressed(); } 3093 3094 static void resolve_compressed() { 3095 UseCompressedOops ? resolve_gc<true>() 3096 : resolve_gc<false>(); 3097 } 3098 3099 private: 3100 template <bool use_compressed> 3101 static void resolve_gc() { 3102 BarrierSet* bs = BarrierSet::barrier_set(); 3103 assert(bs != nullptr, "freeze/thaw invoked before BarrierSet is set"); 3104 switch (bs->kind()) { 3105 #define BARRIER_SET_RESOLVE_BARRIER_CLOSURE(bs_name) \ 3106 case BarrierSet::bs_name: { \ 3107 resolve<use_compressed, typename BarrierSet::GetType<BarrierSet::bs_name>::type>(); \ 3108 } \ 3109 break; 3110 FOR_EACH_CONCRETE_BARRIER_SET_DO(BARRIER_SET_RESOLVE_BARRIER_CLOSURE) 3111 #undef BARRIER_SET_RESOLVE_BARRIER_CLOSURE 3112 3113 default: 3114 fatal("BarrierSet resolving not implemented"); 3115 }; 3116 } 3117 3118 template <bool use_compressed, typename BarrierSetT> 3119 static void resolve() { 3120 typedef Config<use_compressed ? oop_kind::NARROW : oop_kind::WIDE, BarrierSetT> SelectedConfigT; 3121 3122 freeze_entry = (address)freeze<SelectedConfigT>; 3123 freeze_preempt_entry = (address)SelectedConfigT::freeze_preempt; 3124 3125 // If we wanted, we could templatize by kind and have three different thaw entries 3126 thaw_entry = (address)thaw<SelectedConfigT>; 3127 } 3128 }; 3129 3130 void Continuation::init() { 3131 ConfigResolve::resolve(); 3132 }