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