< prev index next >

src/hotspot/share/runtime/continuationFreezeThaw.cpp

Print this page

  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/orderAccess.hpp"
  59 #include "runtime/prefetch.inline.hpp"
  60 #include "runtime/smallRegisterMap.inline.hpp"
  61 #include "runtime/sharedRuntime.hpp"
  62 #include "runtime/stackChunkFrameStream.inline.hpp"
  63 #include "runtime/stackFrameStream.inline.hpp"
  64 #include "runtime/stackOverflow.hpp"
  65 #include "runtime/stackWatermarkSet.inline.hpp"
  66 #include "utilities/debug.hpp"
  67 #include "utilities/exceptions.hpp"
  68 #include "utilities/macros.hpp"

  69 #if INCLUDE_ZGC
  70 #include "gc/z/zStackChunkGCData.inline.hpp"
  71 #endif
  72 
  73 #include <type_traits>
  74 
  75 /*
  76  * This file contains the implementation of continuation freezing (yield) and thawing (run).
  77  *
  78  * This code is very latency-critical and very hot. An ordinary and well-behaved server application
  79  * would likely call these operations many thousands of times per second second, on every core.
  80  *
  81  * Freeze might be called every time the application performs any I/O operation, every time it
  82  * acquires a j.u.c. lock, every time it takes a message from a queue, and thaw can be called
  83  * multiple times in each of those cases, as it is called by the return barrier, which may be
  84  * invoked on method return.
  85  *
  86  * The amortized budget for each of those two operations is ~100-150ns. That is why, for
  87  * example, every effort is made to avoid Java-VM transitions as much as possible.
  88  *

 181 static void log_frames(JavaThread* thread);
 182 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st = tty);
 183 
 184 #define assert_pfl(p, ...) \
 185 do {                                           \
 186   if (!(p)) {                                  \
 187     JavaThread* t = JavaThread::active();      \
 188     if (t->has_last_Java_frame()) {            \
 189       tty->print_cr("assert(" #p ") failed:"); \
 190       t->print_frame_layout();                 \
 191     }                                          \
 192   }                                            \
 193   vmassert(p, __VA_ARGS__);                    \
 194 } while(0)
 195 
 196 #else
 197 static void verify_continuation(oop continuation) { }
 198 #define assert_pfl(p, ...)
 199 #endif
 200 
 201 // should match Continuation.preemptStatus() in Continuation.java
 202 enum freeze_result {
 203   freeze_ok = 0,
 204   freeze_ok_bottom = 1,
 205   freeze_pinned_cs = 2,
 206   freeze_pinned_native = 3,
 207   freeze_pinned_monitor = 4,
 208   freeze_exception = 5
 209 };
 210 
 211 const char* freeze_result_names[6] = {
 212   "freeze_ok",
 213   "freeze_ok_bottom",
 214   "freeze_pinned_cs",
 215   "freeze_pinned_native",
 216   "freeze_pinned_monitor",
 217   "freeze_exception"
 218 };
 219 
 220 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint);
 221 template<typename ConfigT> static inline int freeze_internal(JavaThread* current, intptr_t* const sp);
 222 
 223 static inline int prepare_thaw_internal(JavaThread* thread, bool return_barrier);
 224 template<typename ConfigT> static inline intptr_t* thaw_internal(JavaThread* thread, const Continuation::thaw_kind kind);
 225 
 226 
 227 // Entry point to freeze. Transitions are handled manually
 228 // Called from gen_continuation_yield() in sharedRuntime_<cpu>.cpp through Continuation::freeze_entry();
 229 template<typename ConfigT>
 230 static JRT_BLOCK_ENTRY(int, freeze(JavaThread* current, intptr_t* sp))
 231   assert(sp == current->frame_anchor()->last_Java_sp(), "");
 232 
 233   if (current->raw_cont_fastpath() > current->last_continuation()->entry_sp() || current->raw_cont_fastpath() < sp) {
 234     current->set_cont_fastpath(nullptr);
 235   }
 236 
 237   return ConfigT::freeze(current, sp);
 238 JRT_END
 239 
 240 JRT_LEAF(int, Continuation::prepare_thaw(JavaThread* thread, bool return_barrier))
 241   return prepare_thaw_internal(thread, return_barrier);
 242 JRT_END
 243 
 244 template<typename ConfigT>
 245 static JRT_LEAF(intptr_t*, thaw(JavaThread* thread, int kind))
 246   // TODO: JRT_LEAF and NoHandleMark is problematic for JFR events.
 247   // vFrameStreamCommon allocates Handles in RegisterMap for continuations.


 248   // JRT_ENTRY instead?
 249   ResetNoHandleMark rnhm;

 250 
 251   // we might modify the code cache via BarrierSetNMethod::nmethod_entry_barrier
 252   MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
 253   return ConfigT::thaw(thread, (Continuation::thaw_kind)kind);
 254 JRT_END
 255 
 256 JVM_ENTRY(jint, CONT_isPinned0(JNIEnv* env, jobject cont_scope)) {
 257   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
 258   return is_pinned0(thread, JNIHandles::resolve(cont_scope), false);
 259 }
 260 JVM_END
 261 
 262 ///////////
 263 
 264 enum class oop_kind { NARROW, WIDE };
 265 template <oop_kind oops, typename BarrierSetT>
 266 class Config {
 267 public:
 268   typedef Config<oops, BarrierSetT> SelfT;
 269   using OopT = std::conditional_t<oops == oop_kind::NARROW, narrowOop, oop>;
 270 
 271   static int freeze(JavaThread* thread, intptr_t* const sp) {
 272     return freeze_internal<SelfT>(thread, sp);




 273   }
 274 
 275   static intptr_t* thaw(JavaThread* thread, Continuation::thaw_kind kind) {
 276     return thaw_internal<SelfT>(thread, kind);
 277   }
 278 };
 279 
 280 static bool stack_overflow_check(JavaThread* thread, size_t size, address sp) {
 281   const size_t page_size = os::vm_page_size();
 282   if (size > page_size) {
 283     if (sp - size < thread->stack_overflow_state()->shadow_zone_safe_limit()) {
 284       return false;
 285     }
 286   }
 287   return true;
 288 }
 289 
 290 #ifdef ASSERT
 291 static oop get_continuation(JavaThread* thread) {
 292   assert(thread != nullptr, "");
 293   assert(thread->threadObj() != nullptr, "");
 294   return java_lang_Thread::continuation(thread->threadObj());
 295 }

 296 
 297 inline void clear_anchor(JavaThread* thread) {
 298   thread->frame_anchor()->clear();
 299 }
 300 
 301 static void set_anchor(JavaThread* thread, intptr_t* sp) {
 302   address pc = ContinuationHelper::return_address_at(
 303                  sp - frame::sender_sp_ret_address_offset());


 304   assert(pc != nullptr, "");
 305 
 306   JavaFrameAnchor* anchor = thread->frame_anchor();
 307   anchor->set_last_Java_sp(sp);
 308   anchor->set_last_Java_pc(pc);
 309   ContinuationHelper::set_anchor_pd(anchor, sp);
 310 
 311   assert(thread->has_last_Java_frame(), "");
 312   assert(thread->last_frame().cb() != nullptr, "");
 313 }
 314 #endif // ASSERT
 315 
 316 static void set_anchor_to_entry(JavaThread* thread, ContinuationEntry* entry) {
 317   JavaFrameAnchor* anchor = thread->frame_anchor();
 318   anchor->set_last_Java_sp(entry->entry_sp());
 319   anchor->set_last_Java_pc(entry->entry_pc());
 320   ContinuationHelper::set_anchor_to_entry_pd(anchor, entry);
 321 
 322   assert(thread->has_last_Java_frame(), "");
 323   assert(thread->last_frame().cb() != nullptr, "");
 324 }
 325 

























 326 #if CONT_JFR
 327 class FreezeThawJfrInfo : public StackObj {
 328   short _e_size;
 329   short _e_num_interpreted_frames;
 330  public:
 331 
 332   FreezeThawJfrInfo() : _e_size(0), _e_num_interpreted_frames(0) {}
 333   inline void record_interpreted_frame() { _e_num_interpreted_frames++; }
 334   inline void record_size_copied(int size) { _e_size += size << LogBytesPerWord; }
 335   template<typename Event> void post_jfr_event(Event *e, oop continuation, JavaThread* jt);
 336 };
 337 
 338 template<typename Event> void FreezeThawJfrInfo::post_jfr_event(Event* e, oop continuation, JavaThread* jt) {
 339   if (e->should_commit()) {
 340     log_develop_trace(continuations)("JFR event: iframes: %d size: %d", _e_num_interpreted_frames, _e_size);
 341     e->set_carrierThread(JFR_JVM_THREAD_ID(jt));
 342     e->set_continuationClass(continuation->klass());
 343     e->set_interpretedFrames(_e_num_interpreted_frames);
 344     e->set_size(_e_size);
 345     e->commit();
 346   }
 347 }
 348 #endif // CONT_JFR
 349 
 350 /////////////// FREEZE ////
 351 
 352 class FreezeBase : public StackObj {
 353 protected:
 354   JavaThread* const _thread;
 355   ContinuationWrapper& _cont;
 356   bool _barriers; // only set when we allocate a chunk
 357   const bool _preempt; // used only on the slow path
 358   const intptr_t * const _frame_sp; // Top frame sp for this freeze
 359 
 360   intptr_t* _bottom_address;
 361 









 362   int _freeze_size; // total size of all frames plus metadata in words.
 363   int _total_align_size;
 364 
 365   intptr_t* _cont_stack_top;
 366   intptr_t* _cont_stack_bottom;
 367 
 368   CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;)
 369 
 370 #ifdef ASSERT
 371   intptr_t* _orig_chunk_sp;
 372   int _fast_freeze_size;
 373   bool _empty;
 374 #endif
 375 
 376   JvmtiSampledObjectAllocEventCollector* _jvmti_event_collector;
 377 
 378   NOT_PRODUCT(int _frames;)
 379   DEBUG_ONLY(intptr_t* _last_write;)
 380 
 381   inline FreezeBase(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp);
 382 
 383 public:
 384   NOINLINE freeze_result freeze_slow();
 385   void freeze_fast_existing_chunk();
 386 
 387   CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
 388   void set_jvmti_event_collector(JvmtiSampledObjectAllocEventCollector* jsoaec) { _jvmti_event_collector = jsoaec; }
 389 
 390   inline int size_if_fast_freeze_available();
 391 



 392 #ifdef ASSERT
 393   bool check_valid_fast_path();
 394 #endif
 395 
 396 protected:
 397   inline void init_rest();
 398   void throw_stack_overflow_on_humongous_chunk();
 399 
 400   // fast path
 401   inline void copy_to_chunk(intptr_t* from, intptr_t* to, int size);
 402   inline void unwind_frames();
 403   inline void patch_stack_pd(intptr_t* frame_sp, intptr_t* heap_sp);
 404 
 405   // slow path
 406   virtual stackChunkOop allocate_chunk_slow(size_t stack_size) = 0;
 407 
 408   int cont_size() { return pointer_delta_as_int(_cont_stack_bottom, _cont_stack_top); }
 409 
 410 private:
 411   // slow path
 412   frame freeze_start_frame();
 413   frame freeze_start_frame_safepoint_stub(frame f);
 414   NOINLINE freeze_result recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top);
 415   inline frame freeze_start_frame_yield_stub(frame f);
 416   template<typename FKind>
 417   inline freeze_result recurse_freeze_java_frame(const frame& f, frame& caller, int fsize, int argsize);
 418   inline void before_freeze_java_frame(const frame& f, const frame& caller, int fsize, int argsize, bool is_bottom_frame);
 419   inline void after_freeze_java_frame(const frame& hf, bool is_bottom_frame);
 420   freeze_result finalize_freeze(const frame& callee, frame& caller, int argsize);
 421   void patch(const frame& f, frame& hf, const frame& caller, bool is_bottom_frame);
 422   NOINLINE freeze_result recurse_freeze_interpreted_frame(frame& f, frame& caller, int callee_argsize, bool callee_interpreted);
 423   freeze_result recurse_freeze_compiled_frame(frame& f, frame& caller, int callee_argsize, bool callee_interpreted);
 424   NOINLINE freeze_result recurse_freeze_stub_frame(frame& f, frame& caller);
 425   NOINLINE void finish_freeze(const frame& f, const frame& top);
 426 





 427   inline bool stack_overflow();
 428 
 429   static frame sender(const frame& f) { return f.is_interpreted_frame() ? sender<ContinuationHelper::InterpretedFrame>(f)
 430                                                                         : sender<ContinuationHelper::NonInterpretedUnknownFrame>(f); }
 431   template<typename FKind> static inline frame sender(const frame& f);
 432   template<typename FKind> frame new_heap_frame(frame& f, frame& caller);
 433   inline void set_top_frame_metadata_pd(const frame& hf);
 434   inline void patch_pd(frame& callee, const frame& caller);
 435   void adjust_interpreted_frame_unextended_sp(frame& f);

 436   static inline void relativize_interpreted_frame_metadata(const frame& f, const frame& hf);
 437 
 438 protected:
 439   void freeze_fast_copy(stackChunkOop chunk, int chunk_start_sp CONT_JFR_ONLY(COMMA bool chunk_is_allocated));
 440   bool freeze_fast_new_chunk(stackChunkOop chunk);
 441 
 442 #ifdef ASSERT
 443   bool is_empty(stackChunkOop chunk) {
 444     // during freeze, the chunk is in an intermediate state (after setting the chunk's argsize but before setting its
 445     // ultimate sp) so we use this instead of stackChunkOopDesc::is_empty
 446     return chunk->sp() >= chunk->stack_size() - chunk->argsize() - frame::metadata_words_at_top;
 447   }
 448 #endif
 449 };
 450 
 451 template <typename ConfigT>
 452 class Freeze : public FreezeBase {
 453 private:
 454   stackChunkOop allocate_chunk(size_t stack_size);
 455 
 456 public:
 457   inline Freeze(JavaThread* thread, ContinuationWrapper& cont, intptr_t* frame_sp)
 458     : FreezeBase(thread, cont, frame_sp) {}
 459 
 460   freeze_result try_freeze_fast();
 461 
 462 protected:
 463   virtual stackChunkOop allocate_chunk_slow(size_t stack_size) override { return allocate_chunk(stack_size); }
 464 };
 465 
 466 FreezeBase::FreezeBase(JavaThread* thread, ContinuationWrapper& cont, intptr_t* frame_sp) :
 467     _thread(thread), _cont(cont), _barriers(false), _preempt(false), _frame_sp(frame_sp) {
 468   DEBUG_ONLY(_jvmti_event_collector = nullptr;)
 469 
 470   assert(_thread != nullptr, "");
 471   assert(_thread->last_continuation()->entry_sp() == _cont.entrySP(), "");
 472 
 473   DEBUG_ONLY(_cont.entry()->verify_cookie();)
 474 
 475   assert(!Interpreter::contains(_cont.entryPC()), "");
 476 
 477   _bottom_address = _cont.entrySP() - _cont.entry_frame_extension();
 478 #ifdef _LP64
 479   if (((intptr_t)_bottom_address & 0xf) != 0) {
 480     _bottom_address--;
 481   }
 482   assert(is_aligned(_bottom_address, frame::frame_alignment), "");
 483 #endif
 484 
 485   log_develop_trace(continuations)("bottom_address: " INTPTR_FORMAT " entrySP: " INTPTR_FORMAT " argsize: " PTR_FORMAT,
 486                 p2i(_bottom_address), p2i(_cont.entrySP()), (_cont.entrySP() - _bottom_address) << LogBytesPerWord);
 487   assert(_bottom_address != nullptr, "");
 488   assert(_bottom_address <= _cont.entrySP(), "");
 489   DEBUG_ONLY(_last_write = nullptr;)
 490 
 491   assert(_cont.chunk_invariant(), "");
 492   assert(!Interpreter::contains(_cont.entryPC()), "");
 493 #if !defined(PPC64) || defined(ZERO)
 494   static const int doYield_stub_frame_size = frame::metadata_words;
 495 #else
 496   static const int doYield_stub_frame_size = frame::native_abi_reg_args_size >> LogBytesPerWord;
 497 #endif
 498   assert(SharedRuntime::cont_doYield_stub()->frame_size() == doYield_stub_frame_size, "");

 499 
 500   // properties of the continuation on the stack; all sizes are in words
 501   _cont_stack_top    = frame_sp + doYield_stub_frame_size; // we don't freeze the doYield stub frame
 502   _cont_stack_bottom = _cont.entrySP() + (_cont.argsize() == 0 ? frame::metadata_words_at_top : 0)
 503       - ContinuationHelper::frame_align_words(_cont.argsize()); // see alignment in thaw
 504 
 505   log_develop_trace(continuations)("freeze size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT,
 506     cont_size(), _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom));
 507   assert(cont_size() > 0, "");
















 508 }
 509 
 510 void FreezeBase::init_rest() { // we want to postpone some initialization after chunk handling
 511   _freeze_size = 0;
 512   _total_align_size = 0;
 513   NOT_PRODUCT(_frames = 0;)
 514 }
 515 






















































































































 516 void FreezeBase::copy_to_chunk(intptr_t* from, intptr_t* to, int size) {
 517   stackChunkOop chunk = _cont.tail();
 518   chunk->copy_from_stack_to_chunk(from, to, size);
 519   CONT_JFR_ONLY(_jfr_info.record_size_copied(size);)
 520 
 521 #ifdef ASSERT
 522   if (_last_write != nullptr) {
 523     assert(_last_write == to + size, "Missed a spot: _last_write: " INTPTR_FORMAT " to+size: " INTPTR_FORMAT
 524         " stack_size: %d _last_write offset: " PTR_FORMAT " to+size: " PTR_FORMAT, p2i(_last_write), p2i(to+size),
 525         chunk->stack_size(), _last_write-chunk->start_address(), to+size-chunk->start_address());
 526     _last_write = to;
 527   }
 528 #endif
 529 }
 530 
 531 // Called _after_ the last possible safepoint during the freeze operation (chunk allocation)
 532 void FreezeBase::unwind_frames() {
 533   ContinuationEntry* entry = _cont.entry();
 534   entry->flush_stack_processing(_thread);
 535   set_anchor_to_entry(_thread, entry);
 536 }
 537 
 538 template <typename ConfigT>
 539 freeze_result Freeze<ConfigT>::try_freeze_fast() {
 540   assert(_thread->thread_state() == _thread_in_vm, "");
 541   assert(_thread->cont_fastpath(), "");
 542 
 543   DEBUG_ONLY(_fast_freeze_size = size_if_fast_freeze_available();)
 544   assert(_fast_freeze_size == 0, "");
 545 
 546   stackChunkOop chunk = allocate_chunk(cont_size() + frame::metadata_words);
 547   if (freeze_fast_new_chunk(chunk)) {
 548     return freeze_ok;
 549   }
 550   if (_thread->has_pending_exception()) {
 551     return freeze_exception;
 552   }
 553 
 554   // TODO R REMOVE when deopt change is fixed
 555   assert(!_thread->cont_fastpath() || _barriers, "");
 556   log_develop_trace(continuations)("-- RETRYING SLOW --");
 557   return freeze_slow();
 558 }
 559 
 560 // Returns size needed if the continuation fits, otherwise 0.
 561 int FreezeBase::size_if_fast_freeze_available() {
 562   stackChunkOop chunk = _cont.tail();
 563   if (chunk == nullptr || chunk->is_gc_mode() || chunk->requires_barriers() || chunk->has_mixed_frames()) {
 564     log_develop_trace(continuations)("chunk available %s", chunk == nullptr ? "no chunk" : "chunk requires barriers");
 565     return 0;
 566   }
 567 
 568   int total_size_needed = cont_size();
 569   const int chunk_sp = chunk->sp();
 570 
 571   // argsize can be nonzero if we have a caller, but the caller could be in a non-empty parent chunk,
 572   // so we subtract it only if we overlap with the caller, i.e. the current chunk isn't empty.
 573   // Consider leaving the chunk's argsize set when emptying it and removing the following branch,
 574   // although that would require changing stackChunkOopDesc::is_empty
 575   if (chunk_sp < chunk->stack_size()) {
 576     total_size_needed -= _cont.argsize() + frame::metadata_words_at_top;
 577   }
 578 


 579   int chunk_free_room = chunk_sp - frame::metadata_words_at_bottom;
 580   bool available = chunk_free_room >= total_size_needed;
 581   log_develop_trace(continuations)("chunk available: %s size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT,
 582     available ? "yes" : "no" , total_size_needed, _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom));
 583   return available ? total_size_needed : 0;
 584 }
 585 
 586 void FreezeBase::freeze_fast_existing_chunk() {
 587   stackChunkOop chunk = _cont.tail();
 588   DEBUG_ONLY(_orig_chunk_sp = chunk->sp_address();)
 589 
 590   DEBUG_ONLY(_fast_freeze_size = size_if_fast_freeze_available();)
 591   assert(_fast_freeze_size > 0, "");
 592 
 593   if (chunk->sp() < chunk->stack_size()) { // we are copying into a non-empty chunk
 594     DEBUG_ONLY(_empty = false;)
 595     assert(chunk->sp() < (chunk->stack_size() - chunk->argsize()), "");
 596 #ifdef ASSERT
 597     {
 598       intptr_t* retaddr_slot = (chunk->sp_address()

 641     freeze_fast_copy(chunk, chunk_start_sp CONT_JFR_ONLY(COMMA false));
 642   }
 643 }
 644 
 645 bool FreezeBase::freeze_fast_new_chunk(stackChunkOop chunk) {
 646   DEBUG_ONLY(_empty = true;)
 647 
 648   // Install new chunk
 649   _cont.set_tail(chunk);
 650 
 651   if (UNLIKELY(chunk == nullptr || !_thread->cont_fastpath() || _barriers)) { // OOME/probably humongous
 652     log_develop_trace(continuations)("Retrying slow. Barriers: %d", _barriers);
 653     return false;
 654   }
 655 
 656   chunk->set_max_thawing_size(cont_size());
 657   chunk->set_argsize(_cont.argsize());
 658 
 659   // in a fresh chunk, we freeze *with* the bottom-most frame's stack arguments.
 660   // They'll then be stored twice: in the chunk and in the parent chunk's top frame
 661   const int chunk_start_sp = cont_size() + frame::metadata_words;
 662   assert(chunk_start_sp == chunk->stack_size(), "");
 663 
 664   DEBUG_ONLY(_orig_chunk_sp = chunk->start_address() + chunk_start_sp;)
 665 
 666   freeze_fast_copy(chunk, chunk_start_sp CONT_JFR_ONLY(COMMA true));
 667 
 668   return true;
 669 }
 670 
 671 void FreezeBase::freeze_fast_copy(stackChunkOop chunk, int chunk_start_sp CONT_JFR_ONLY(COMMA bool chunk_is_allocated)) {
 672   assert(chunk != nullptr, "");
 673   assert(!chunk->has_mixed_frames(), "");
 674   assert(!chunk->is_gc_mode(), "");
 675   assert(!chunk->has_bitmap(), "");
 676   assert(!chunk->requires_barriers(), "");
 677   assert(chunk == _cont.tail(), "");
 678 
 679   // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
 680   // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
 681   // will either see no continuation on the stack, or a consistent chunk.
 682   unwind_frames();
 683 
 684   log_develop_trace(continuations)("freeze_fast start: chunk " INTPTR_FORMAT " size: %d orig sp: %d argsize: %d",
 685     p2i((oopDesc*)chunk), chunk->stack_size(), chunk_start_sp, _cont.argsize());
 686   assert(chunk_start_sp <= chunk->stack_size(), "");
 687   assert(chunk_start_sp >= cont_size(), "no room in the chunk");
 688 
 689   const int chunk_new_sp = chunk_start_sp - cont_size(); // the chunk's new sp, after freeze
 690   assert(!(_fast_freeze_size > 0) || _orig_chunk_sp - (chunk->start_address() + chunk_new_sp) == _fast_freeze_size, "");
 691 
 692   intptr_t* chunk_top = chunk->start_address() + chunk_new_sp;
 693 #ifdef ASSERT
 694   if (!_empty) {
 695     intptr_t* retaddr_slot = (_orig_chunk_sp
 696                               - frame::sender_sp_ret_address_offset());
 697     assert(ContinuationHelper::return_address_at(retaddr_slot) == chunk->pc(),
 698            "unexpected saved return address");
 699   }
 700 #endif
 701 
 702   log_develop_trace(continuations)("freeze_fast start: " INTPTR_FORMAT " sp: %d chunk_top: " INTPTR_FORMAT,
 703                               p2i(chunk->start_address()), chunk_new_sp, p2i(chunk_top));
 704   intptr_t* from = _cont_stack_top - frame::metadata_words_at_bottom;
 705   intptr_t* to   = chunk_top - frame::metadata_words_at_bottom;
 706   copy_to_chunk(from, to, cont_size() + frame::metadata_words_at_bottom);
 707   // Because we're not patched yet, the chunk is now in a bad state
 708 
 709   // patch return pc of the bottom-most frozen frame (now in the chunk)
 710   // with the actual caller's return address
 711   intptr_t* chunk_bottom_retaddr_slot = (chunk_top + cont_size()
 712                                          - _cont.argsize()
 713                                          - frame::metadata_words_at_top
 714                                          - frame::sender_sp_ret_address_offset());
 715 #ifdef ASSERT
 716   if (!_empty) {
 717     assert(ContinuationHelper::return_address_at(chunk_bottom_retaddr_slot)
 718            == StubRoutines::cont_returnBarrier(),
 719            "should be the continuation return barrier");
 720   }
 721 #endif
 722   ContinuationHelper::patch_return_address_at(chunk_bottom_retaddr_slot,
 723                                               chunk->pc());
 724 
 725   // We're always writing to a young chunk, so the GC can't see it until the next safepoint.
 726   chunk->set_sp(chunk_new_sp);
 727   // set chunk->pc to the return address of the topmost frame in the chunk
 728   chunk->set_pc(ContinuationHelper::return_address_at(
 729                   _cont_stack_top - frame::sender_sp_ret_address_offset()));
 730 





 731   _cont.write();
 732 
 733   log_develop_trace(continuations)("FREEZE CHUNK #" INTPTR_FORMAT " (young)", _cont.hash());
 734   LogTarget(Trace, continuations) lt;
 735   if (lt.develop_is_enabled()) {
 736     LogStream ls(lt);
 737     chunk->print_on(true, &ls);
 738   }
 739 
 740   // Verification
 741   assert(_cont.chunk_invariant(), "");
 742   chunk->verify();
 743 
 744 #if CONT_JFR
 745   EventContinuationFreezeFast e;
 746   if (e.should_commit()) {
 747     e.set_id(cast_from_oop<u8>(chunk));
 748     DEBUG_ONLY(e.set_allocate(chunk_is_allocated);)
 749     e.set_size(cont_size() << LogBytesPerWord);
 750     e.commit();

 769 #endif
 770 
 771   init_rest();
 772 
 773   HandleMark hm(Thread::current());
 774 
 775   frame f = freeze_start_frame();
 776 
 777   LogTarget(Debug, continuations) lt;
 778   if (lt.develop_is_enabled()) {
 779     LogStream ls(lt);
 780     f.print_on(&ls);
 781   }
 782 
 783   frame caller; // the frozen caller in the chunk
 784   freeze_result res = recurse_freeze(f, caller, 0, false, true);
 785 
 786   if (res == freeze_ok) {
 787     finish_freeze(f, caller);
 788     _cont.write();


 789   }
 790 
 791   return res;
 792 }
 793 
 794 frame FreezeBase::freeze_start_frame() {
 795   frame f = _thread->last_frame();
 796   if (LIKELY(!_preempt)) {
 797     return freeze_start_frame_yield_stub(f);
 798   } else {
 799     return freeze_start_frame_safepoint_stub(f);
 800   }
 801 }
 802 
 803 frame FreezeBase::freeze_start_frame_yield_stub(frame f) {

 804   assert(SharedRuntime::cont_doYield_stub()->contains(f.pc()), "must be");
 805   f = sender<ContinuationHelper::NonInterpretedUnknownFrame>(f);
 806   assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), f), "");
 807   return f;
 808 }
 809 
 810 frame FreezeBase::freeze_start_frame_safepoint_stub(frame f) {
 811 #if (defined(X86) || defined(AARCH64) || defined(RISCV64)) && !defined(ZERO)
 812   f.set_fp(f.real_fp()); // f.set_fp(*Frame::callee_link_address(f)); // ????
 813 #else
 814   Unimplemented();
 815 #endif
 816   if (!Interpreter::contains(f.pc())) {
 817     assert(ContinuationHelper::Frame::is_stub(f.cb()), "must be");
 818     assert(f.oop_map() != nullptr, "must be");
 819 
 820     if (Interpreter::contains(ContinuationHelper::StubFrame::return_pc(f))) {
 821       f = sender<ContinuationHelper::StubFrame>(f); // Safepoint stub in interpreter
 822     }
 823   }
 824   assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), f), "");
 825   return f;
 826 }
 827 
 828 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
 829 NOINLINE freeze_result FreezeBase::recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top) {
 830   assert(f.unextended_sp() < _bottom_address, ""); // see recurse_freeze_java_frame
 831   assert(f.is_interpreted_frame() || ((top && _preempt) == ContinuationHelper::Frame::is_stub(f.cb())), "");

 832 
 833   if (stack_overflow()) {
 834     return freeze_exception;
 835   }
 836 
 837   if (f.is_compiled_frame()) {
 838     if (UNLIKELY(f.oop_map() == nullptr)) {
 839       // special native frame
 840       return freeze_pinned_native;
 841     }
 842     return recurse_freeze_compiled_frame(f, caller, callee_argsize, callee_interpreted);
 843   } else if (f.is_interpreted_frame()) {
 844     assert((_preempt && top) || !f.interpreter_frame_method()->is_native(), "");
 845     if (_preempt && top && f.interpreter_frame_method()->is_native()) {
 846       // int native entry
 847       return freeze_pinned_native;
 848     }
 849 
 850     return recurse_freeze_interpreted_frame(f, caller, callee_argsize, callee_interpreted);
 851   } else if (_preempt && top && ContinuationHelper::Frame::is_stub(f.cb())) {
 852     return recurse_freeze_stub_frame(f, caller);






 853   } else {
 854     return freeze_pinned_native;
 855   }
 856 }
 857 
 858 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
 859 // See also StackChunkFrameStream<frame_kind>::frame_size()
 860 template<typename FKind>
 861 inline freeze_result FreezeBase::recurse_freeze_java_frame(const frame& f, frame& caller, int fsize, int argsize) {
 862   assert(FKind::is_instance(f), "");
 863 
 864   assert(fsize > 0, "");
 865   assert(argsize >= 0, "");
 866   _freeze_size += fsize;
 867   NOT_PRODUCT(_frames++;)
 868 
 869   assert(FKind::frame_bottom(f) <= _bottom_address, "");
 870 
 871   // We don't use FKind::frame_bottom(f) == _bottom_address because on x64 there's sometimes an extra word between
 872   // enterSpecial and an interpreted frame

 893 
 894 inline void FreezeBase::after_freeze_java_frame(const frame& hf, bool is_bottom_frame) {
 895   LogTarget(Trace, continuations) lt;
 896   if (lt.develop_is_enabled()) {
 897     LogStream ls(lt);
 898     DEBUG_ONLY(hf.print_value_on(&ls, nullptr);)
 899     assert(hf.is_heap_frame(), "should be");
 900     DEBUG_ONLY(print_frame_layout(hf, false, &ls);)
 901     if (is_bottom_frame) {
 902       ls.print_cr("bottom h-frame:");
 903       hf.print_on(&ls);
 904     }
 905   }
 906 }
 907 
 908 // The parameter argsize_md includes metadata that has to be part of caller/callee overlap.
 909 // See also StackChunkFrameStream<frame_kind>::frame_size()
 910 freeze_result FreezeBase::finalize_freeze(const frame& callee, frame& caller, int argsize_md) {
 911   int argsize = argsize_md - frame::metadata_words_at_top;
 912   assert(callee.is_interpreted_frame()

 913     || callee.cb()->as_nmethod()->is_osr_method()
 914     || argsize == _cont.argsize(), "argsize: %d cont.argsize: %d", argsize, _cont.argsize());
 915   log_develop_trace(continuations)("bottom: " INTPTR_FORMAT " count %d size: %d argsize: %d",
 916     p2i(_bottom_address), _frames, _freeze_size << LogBytesPerWord, argsize);
 917 
 918   LogTarget(Trace, continuations) lt;
 919 
 920 #ifdef ASSERT
 921   bool empty = _cont.is_empty();
 922   log_develop_trace(continuations)("empty: %d", empty);
 923 #endif
 924 
 925   stackChunkOop chunk = _cont.tail();
 926 
 927   assert(chunk == nullptr || (chunk->max_thawing_size() == 0) == chunk->is_empty(), "");
 928 
 929   _freeze_size += frame::metadata_words; // for top frame's metadata
 930 
 931   int overlap = 0; // the args overlap the caller -- if there is one in this chunk and is of the same kind
 932   int unextended_sp = -1;

 934     unextended_sp = chunk->sp();
 935     if (!chunk->is_empty()) {
 936       StackChunkFrameStream<ChunkFrames::Mixed> last(chunk);
 937       unextended_sp = chunk->to_offset(StackChunkFrameStream<ChunkFrames::Mixed>(chunk).unextended_sp());
 938       bool top_interpreted = Interpreter::contains(chunk->pc());
 939       if (callee.is_interpreted_frame() == top_interpreted) {
 940         overlap = argsize_md;
 941       }
 942     }
 943   }
 944 
 945   log_develop_trace(continuations)("finalize _size: %d overlap: %d unextended_sp: %d", _freeze_size, overlap, unextended_sp);
 946 
 947   _freeze_size -= overlap;
 948   assert(_freeze_size >= 0, "");
 949 
 950   assert(chunk == nullptr || chunk->is_empty()
 951           || unextended_sp == chunk->to_offset(StackChunkFrameStream<ChunkFrames::Mixed>(chunk).unextended_sp()), "");
 952   assert(chunk != nullptr || unextended_sp < _freeze_size, "");
 953 


 954   // _barriers can be set to true by an allocation in freeze_fast, in which case the chunk is available
 955   bool allocated_old_in_freeze_fast = _barriers;
 956   assert(!allocated_old_in_freeze_fast || (unextended_sp >= _freeze_size && chunk->is_empty()),
 957     "Chunk allocated in freeze_fast is of insufficient size "
 958     "unextended_sp: %d size: %d is_empty: %d", unextended_sp, _freeze_size, chunk->is_empty());
 959   assert(!allocated_old_in_freeze_fast || (!UseZGC && !UseG1GC), "Unexpected allocation");
 960 
 961   DEBUG_ONLY(bool empty_chunk = true);
 962   if (unextended_sp < _freeze_size || chunk->is_gc_mode() || (!allocated_old_in_freeze_fast && chunk->requires_barriers())) {
 963     // ALLOCATE NEW CHUNK
 964 
 965     if (lt.develop_is_enabled()) {
 966       LogStream ls(lt);
 967       if (chunk == nullptr) {
 968         ls.print_cr("no chunk");
 969       } else {
 970         ls.print_cr("chunk barriers: %d _size: %d free size: %d",
 971           chunk->requires_barriers(), _freeze_size, chunk->sp() - frame::metadata_words);
 972         chunk->print_on(&ls);
 973       }

 992     // REUSE EXISTING CHUNK
 993     log_develop_trace(continuations)("Reusing chunk mixed: %d empty: %d", chunk->has_mixed_frames(), chunk->is_empty());
 994     if (chunk->is_empty()) {
 995       int sp = chunk->stack_size() - argsize_md;
 996       chunk->set_sp(sp);
 997       chunk->set_argsize(argsize);
 998       _freeze_size += overlap;
 999       assert(chunk->max_thawing_size() == 0, "");
1000     } DEBUG_ONLY(else empty_chunk = false;)
1001   }
1002   assert(!chunk->is_gc_mode(), "");
1003   assert(!chunk->has_bitmap(), "");
1004   chunk->set_has_mixed_frames(true);
1005 
1006   assert(chunk->requires_barriers() == _barriers, "");
1007   assert(!_barriers || is_empty(chunk), "");
1008 
1009   assert(!is_empty(chunk) || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).is_done(), "");
1010   assert(!is_empty(chunk) || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame().is_empty(), "");
1011 








1012   // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
1013   // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
1014   // will either see no continuation or a consistent chunk.
1015   unwind_frames();
1016 
1017   chunk->set_max_thawing_size(chunk->max_thawing_size() + _freeze_size - frame::metadata_words);
1018 
1019   if (lt.develop_is_enabled()) {
1020     LogStream ls(lt);
1021     ls.print_cr("top chunk:");
1022     chunk->print_on(&ls);
1023   }
1024 
















1025   // The topmost existing frame in the chunk; or an empty frame if the chunk is empty
1026   caller = StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame();
1027 
1028   DEBUG_ONLY(_last_write = caller.unextended_sp() + (empty_chunk ? argsize_md : overlap);)
1029 
1030   assert(chunk->is_in_chunk(_last_write - _freeze_size),
1031     "last_write-size: " INTPTR_FORMAT " start: " INTPTR_FORMAT, p2i(_last_write-_freeze_size), p2i(chunk->start_address()));
1032 #ifdef ASSERT
1033   if (lt.develop_is_enabled()) {
1034     LogStream ls(lt);
1035     ls.print_cr("top hframe before (freeze):");
1036     assert(caller.is_heap_frame(), "should be");
1037     caller.print_on(&ls);
1038   }
1039 
1040   assert(!empty || Continuation::is_continuation_entry_frame(callee, nullptr), "");
1041 
1042   frame entry = sender(callee);
1043 
1044   assert(Continuation::is_return_barrier_entry(entry.pc()) || Continuation::is_continuation_enterSpecial(entry), "");
1045   assert(callee.is_interpreted_frame() || entry.sp() == entry.unextended_sp(), "");
1046 #endif
1047 
1048   return freeze_ok_bottom;
1049 }
1050 
1051 void FreezeBase::patch(const frame& f, frame& hf, const frame& caller, bool is_bottom_frame) {
1052   if (is_bottom_frame) {
1053     // If we're the bottom frame, we need to replace the return barrier with the real
1054     // caller's pc.
1055     address last_pc = caller.pc();
1056     assert((last_pc == nullptr) == is_empty(_cont.tail()), "");
1057     ContinuationHelper::Frame::patch_pc(caller, last_pc);
1058   } else {
1059     assert(!caller.is_empty(), "");
1060   }
1061 
1062   patch_pd(hf, caller);
1063 
1064   if (f.is_interpreted_frame()) {

1091 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
1092 // See also StackChunkFrameStream<frame_kind>::frame_size()
1093 NOINLINE freeze_result FreezeBase::recurse_freeze_interpreted_frame(frame& f, frame& caller,
1094                                                                     int callee_argsize /* incl. metadata */,
1095                                                                     bool callee_interpreted) {
1096   adjust_interpreted_frame_unextended_sp(f);
1097 
1098   // The frame's top never includes the stack arguments to the callee
1099   intptr_t* const stack_frame_top = ContinuationHelper::InterpretedFrame::frame_top(f, callee_argsize, callee_interpreted);
1100   intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
1101   const int fsize = pointer_delta_as_int(stack_frame_bottom, stack_frame_top);
1102 
1103   DEBUG_ONLY(verify_frame_top(f, stack_frame_top));
1104 
1105   Method* frame_method = ContinuationHelper::Frame::frame_method(f);
1106   // including metadata between f and its args
1107   const int argsize = ContinuationHelper::InterpretedFrame::stack_argsize(f) + frame::metadata_words_at_top;
1108 
1109   log_develop_trace(continuations)("recurse_freeze_interpreted_frame %s _size: %d fsize: %d argsize: %d",
1110     frame_method->name_and_sig_as_C_string(), _freeze_size, fsize, argsize);
1111   // we'd rather not yield inside methods annotated with @JvmtiMountTransition
1112   assert(!ContinuationHelper::Frame::frame_method(f)->jvmti_mount_transition(), "");

1113 
1114   freeze_result result = recurse_freeze_java_frame<ContinuationHelper::InterpretedFrame>(f, caller, fsize, argsize);
1115   if (UNLIKELY(result > freeze_ok_bottom)) {
1116     return result;
1117   }
1118 
1119   bool is_bottom_frame = result == freeze_ok_bottom;
1120   assert(!caller.is_empty() || is_bottom_frame, "");
1121 
1122   DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, 0, is_bottom_frame);)
1123 
1124   frame hf = new_heap_frame<ContinuationHelper::InterpretedFrame>(f, caller);
1125   _total_align_size += frame::align_wiggle; // add alignment room for internal interpreted frame alignment on AArch64/PPC64
1126 
1127   intptr_t* heap_frame_top = ContinuationHelper::InterpretedFrame::frame_top(hf, callee_argsize, callee_interpreted);
1128   intptr_t* heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
1129   assert(heap_frame_bottom == heap_frame_top + fsize, "");
1130 
1131   // Some architectures (like AArch64/PPC64/RISC-V) add padding between the locals and the fixed_frame to keep the fp 16-byte-aligned.
1132   // On those architectures we freeze the padding in order to keep the same fp-relative offsets in the fixed_frame.
1133   copy_to_chunk(stack_frame_top, heap_frame_top, fsize);
1134   assert(!is_bottom_frame || !caller.is_interpreted_frame() || (heap_frame_top + fsize) == (caller.unextended_sp() + argsize), "");
1135 
1136   relativize_interpreted_frame_metadata(f, hf);
1137 
1138   patch(f, hf, caller, is_bottom_frame);
1139 
1140   CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();)
1141   DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);)
1142   caller = hf;
1143 
1144   // Mark frame_method's GC epoch for class redefinition on_stack calculation.
1145   frame_method->record_gc_epoch();
1146 





1147   return freeze_ok;
1148 }
1149 
1150 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
1151 // See also StackChunkFrameStream<frame_kind>::frame_size()
1152 freeze_result FreezeBase::recurse_freeze_compiled_frame(frame& f, frame& caller,
1153                                                         int callee_argsize /* incl. metadata */,
1154                                                         bool callee_interpreted) {
1155   // The frame's top never includes the stack arguments to the callee
1156   intptr_t* const stack_frame_top = ContinuationHelper::CompiledFrame::frame_top(f, callee_argsize, callee_interpreted);
1157   intptr_t* const stack_frame_bottom = ContinuationHelper::CompiledFrame::frame_bottom(f);
1158   // including metadata between f and its stackargs
1159   const int argsize = ContinuationHelper::CompiledFrame::stack_argsize(f) + frame::metadata_words_at_top;
1160   const int fsize = pointer_delta_as_int(stack_frame_bottom + argsize, stack_frame_top);
1161 
1162   log_develop_trace(continuations)("recurse_freeze_compiled_frame %s _size: %d fsize: %d argsize: %d",
1163                              ContinuationHelper::Frame::frame_method(f) != nullptr ?
1164                              ContinuationHelper::Frame::frame_method(f)->name_and_sig_as_C_string() : "",
1165                              _freeze_size, fsize, argsize);
1166   // we'd rather not yield inside methods annotated with @JvmtiMountTransition
1167   assert(!ContinuationHelper::Frame::frame_method(f)->jvmti_mount_transition(), "");

1168 
1169   freeze_result result = recurse_freeze_java_frame<ContinuationHelper::CompiledFrame>(f, caller, fsize, argsize);
1170   if (UNLIKELY(result > freeze_ok_bottom)) {
1171     return result;
1172   }
1173 
1174   bool is_bottom_frame = result == freeze_ok_bottom;
1175   assert(!caller.is_empty() || is_bottom_frame, "");
1176 
1177   DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, argsize, is_bottom_frame);)
1178 
1179   frame hf = new_heap_frame<ContinuationHelper::CompiledFrame>(f, caller);
1180 
1181   intptr_t* heap_frame_top = ContinuationHelper::CompiledFrame::frame_top(hf, callee_argsize, callee_interpreted);
1182 
1183   copy_to_chunk(stack_frame_top, heap_frame_top, fsize);
1184   assert(!is_bottom_frame || !caller.is_compiled_frame() || (heap_frame_top + fsize) == (caller.unextended_sp() + argsize), "");
1185 
1186   if (caller.is_interpreted_frame()) {
1187     _total_align_size += frame::align_wiggle; // See Thaw::align
1188   }
1189 
1190   patch(f, hf, caller, is_bottom_frame);
1191 
1192   assert(is_bottom_frame || Interpreter::contains(ContinuationHelper::CompiledFrame::real_pc(caller)) == caller.is_interpreted_frame(), "");
1193 
1194   DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);)
1195   caller = hf;






1196   return freeze_ok;
1197 }
1198 
1199 NOINLINE freeze_result FreezeBase::recurse_freeze_stub_frame(frame& f, frame& caller) {



1200   intptr_t* const stack_frame_top = ContinuationHelper::StubFrame::frame_top(f, 0, 0);
1201   const int fsize = f.cb()->frame_size();
1202 
1203   log_develop_trace(continuations)("recurse_freeze_stub_frame %s _size: %d fsize: %d :: " INTPTR_FORMAT " - " INTPTR_FORMAT,
1204     f.cb()->name(), _freeze_size, fsize, p2i(stack_frame_top), p2i(stack_frame_top+fsize));
1205 
1206   // recurse_freeze_java_frame and freeze inlined here because we need to use a full RegisterMap for lock ownership
1207   NOT_PRODUCT(_frames++;)
1208   _freeze_size += fsize;
1209 
1210   RegisterMap map(_cont.thread(),
1211                   RegisterMap::UpdateMap::include,
1212                   RegisterMap::ProcessFrames::skip,
1213                   RegisterMap::WalkContinuation::skip);
1214   map.set_include_argument_oops(false);
1215   ContinuationHelper::update_register_map<ContinuationHelper::StubFrame>(f, &map);
1216   f.oop_map()->update_register_map(&f, &map); // we have callee-save registers in this case
1217   frame senderf = sender<ContinuationHelper::StubFrame>(f);
1218   assert(senderf.unextended_sp() < _bottom_address - 1, "");
1219   assert(senderf.is_compiled_frame(), "");
1220 
1221   if (UNLIKELY(senderf.oop_map() == nullptr)) {
1222     // native frame
1223     return freeze_pinned_native;
1224   }
1225 
1226   freeze_result result = recurse_freeze_compiled_frame(senderf, caller, 0, 0); // This might be deoptimized
1227   if (UNLIKELY(result > freeze_ok_bottom)) {
1228     return result;
1229   }
1230   assert(result != freeze_ok_bottom, "");
1231   assert(!caller.is_interpreted_frame(), "");
1232 
1233   DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, 0, false);)




1234   frame hf = new_heap_frame<ContinuationHelper::StubFrame>(f, caller);
1235   intptr_t* heap_frame_top = ContinuationHelper::StubFrame::frame_top(hf, 0, 0);

1236   copy_to_chunk(stack_frame_top, heap_frame_top, fsize);
1237   DEBUG_ONLY(after_freeze_java_frame(hf, false);)







1238 
1239   caller = hf;
1240   return freeze_ok;
1241 }
1242 
1243 NOINLINE void FreezeBase::finish_freeze(const frame& f, const frame& top) {
1244   stackChunkOop chunk = _cont.tail();
1245   assert(chunk->to_offset(top.sp()) <= chunk->sp(), "");
1246 
1247   LogTarget(Trace, continuations) lt;
1248   if (lt.develop_is_enabled()) {
1249     LogStream ls(lt);
1250     assert(top.is_heap_frame(), "should be");
1251     top.print_on(&ls);
1252   }
1253 
1254   set_top_frame_metadata_pd(top);
1255 
1256   chunk->set_sp(chunk->to_offset(top.sp()));
1257   chunk->set_pc(top.pc());
1258 
1259   chunk->set_max_thawing_size(chunk->max_thawing_size() + _total_align_size);
1260 


1261   // At this point the chunk is consistent
1262 
1263   if (UNLIKELY(_barriers)) {
1264     log_develop_trace(continuations)("do barriers on old chunk");
1265     // Serial and Parallel GC can allocate objects directly into the old generation.
1266     // Then we want to relativize the derived pointers eagerly so that
1267     // old chunks are all in GC mode.
1268     assert(!UseG1GC, "G1 can not deal with allocating outside of eden");
1269     assert(!UseZGC, "ZGC can not deal with allocating chunks visible to marking");
1270     if (UseShenandoahGC) {
1271       _cont.tail()->relativize_derived_pointers_concurrently();
1272     } else {
1273       ContinuationGCSupport::transform_stack_chunk(_cont.tail());
1274     }
1275     // For objects in the old generation we must maintain the remembered set
1276     _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>();
1277   }
1278 
1279   log_develop_trace(continuations)("finish_freeze: has_mixed_frames: %d", chunk->has_mixed_frames());
1280   if (lt.develop_is_enabled()) {
1281     LogStream ls(lt);
1282     chunk->print_on(true, &ls);
1283   }
1284 
1285   if (lt.develop_is_enabled()) {
1286     LogStream ls(lt);
1287     ls.print_cr("top hframe after (freeze):");
1288     assert(_cont.last_frame().is_heap_frame(), "should be");
1289     _cont.last_frame().print_on(&ls);

1290   }
1291 
1292   assert(_cont.chunk_invariant(), "");
1293 }
1294 
1295 inline bool FreezeBase::stack_overflow() { // detect stack overflow in recursive native code
1296   JavaThread* t = !_preempt ? _thread : JavaThread::current();
1297   assert(t == JavaThread::current(), "");
1298   if (os::current_stack_pointer() < t->stack_overflow_state()->shadow_zone_safe_limit()) {
1299     if (!_preempt) {
1300       ContinuationWrapper::SafepointOp so(t, _cont); // could also call _cont.done() instead
1301       Exceptions::_throw_msg(t, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Stack overflow while freezing");
1302     }
1303     return true;
1304   }
1305   return false;
1306 }
1307 
1308 class StackChunkAllocator : public MemAllocator {
1309   const size_t                                 _stack_size;
1310   ContinuationWrapper&                         _continuation_wrapper;
1311   JvmtiSampledObjectAllocEventCollector* const _jvmti_event_collector;

1312   mutable bool                                 _took_slow_path;
1313 
1314   // Does the minimal amount of initialization needed for a TLAB allocation.
1315   // We don't need to do a full initialization, as such an allocation need not be immediately walkable.
1316   virtual oop initialize(HeapWord* mem) const override {
1317     assert(_stack_size > 0, "");
1318     assert(_stack_size <= max_jint, "");
1319     assert(_word_size > _stack_size, "");
1320 
1321     // zero out fields (but not the stack)
1322     const size_t hs = oopDesc::header_size();

1323     Copy::fill_to_aligned_words(mem + hs, vmClasses::StackChunk_klass()->size_helper() - hs);
1324 
1325     jdk_internal_vm_StackChunk::set_size(mem, (int)_stack_size);
1326     jdk_internal_vm_StackChunk::set_sp(mem, (int)_stack_size);
1327 
1328     return finish(mem);
1329   }
1330 
1331   stackChunkOop allocate_fast() const {
1332     if (!UseTLAB) {
1333       return nullptr;
1334     }
1335 
1336     HeapWord* const mem = MemAllocator::mem_allocate_inside_tlab_fast();
1337     if (mem == nullptr) {
1338       return nullptr;
1339     }
1340 
1341     oop obj = initialize(mem);
1342     return stackChunkOopDesc::cast(obj);
1343   }
1344 


1345 public:
1346   StackChunkAllocator(Klass* klass,
1347                       size_t word_size,
1348                       Thread* thread,
1349                       size_t stack_size,
1350                       ContinuationWrapper& continuation_wrapper,
1351                       JvmtiSampledObjectAllocEventCollector* jvmti_event_collector)

1352     : MemAllocator(klass, word_size, thread),
1353       _stack_size(stack_size),
1354       _continuation_wrapper(continuation_wrapper),
1355       _jvmti_event_collector(jvmti_event_collector),

1356       _took_slow_path(false) {}
1357 
1358   // Provides it's own, specialized allocation which skips instrumentation
1359   // if the memory can be allocated without going to a slow-path.
1360   stackChunkOop allocate() const {
1361     // First try to allocate without any slow-paths or instrumentation.
1362     stackChunkOop obj = allocate_fast();
1363     if (obj != nullptr) {
1364       return obj;
1365     }
1366 
1367     // Now try full-blown allocation with all expensive operations,
1368     // including potentially safepoint operations.
1369     _took_slow_path = true;
1370 
1371     // Protect unhandled Loom oops
1372     ContinuationWrapper::SafepointOp so(_thread, _continuation_wrapper);
1373 
1374     // Can safepoint
1375     _jvmti_event_collector->start();

1389 
1390   InstanceStackChunkKlass* klass = InstanceStackChunkKlass::cast(vmClasses::StackChunk_klass());
1391   size_t size_in_words = klass->instance_size(stack_size);
1392 
1393   if (CollectedHeap::stack_chunk_max_size() > 0 && size_in_words >= CollectedHeap::stack_chunk_max_size()) {
1394     if (!_preempt) {
1395       throw_stack_overflow_on_humongous_chunk();
1396     }
1397     return nullptr;
1398   }
1399 
1400   JavaThread* current = _preempt ? JavaThread::current() : _thread;
1401   assert(current == JavaThread::current(), "should be current");
1402 
1403   // Allocate the chunk.
1404   //
1405   // This might safepoint while allocating, but all safepointing due to
1406   // instrumentation have been deferred. This property is important for
1407   // some GCs, as this ensures that the allocated object is in the young
1408   // generation / newly allocated memory.
1409   StackChunkAllocator allocator(klass, size_in_words, current, stack_size, _cont, _jvmti_event_collector);
1410   stackChunkOop chunk = allocator.allocate();
1411 
1412   if (chunk == nullptr) {
1413     return nullptr; // OOME
1414   }
1415 
1416   // assert that chunk is properly initialized
1417   assert(chunk->stack_size() == (int)stack_size, "");
1418   assert(chunk->size() >= stack_size, "chunk->size(): %zu size: %zu", chunk->size(), stack_size);
1419   assert(chunk->sp() == chunk->stack_size(), "");
1420   assert((intptr_t)chunk->start_address() % 8 == 0, "");
1421   assert(chunk->max_thawing_size() == 0, "");
1422   assert(chunk->pc() == nullptr, "");
1423   assert(chunk->argsize() == 0, "");
1424   assert(chunk->flags() == 0, "");
1425   assert(chunk->is_gc_mode() == false, "");


1426 
1427   // fields are uninitialized
1428   chunk->set_parent_access<IS_DEST_UNINITIALIZED>(_cont.last_nonempty_chunk());
1429   chunk->set_cont_access<IS_DEST_UNINITIALIZED>(_cont.continuation());
1430 
1431 #if INCLUDE_ZGC
1432   if (UseZGC) {
1433     if (ZGenerational) {
1434       ZStackChunkGCData::initialize(chunk);
1435     }
1436     assert(!chunk->requires_barriers(), "ZGC always allocates in the young generation");
1437     _barriers = false;
1438   } else
1439 #endif
1440 #if INCLUDE_SHENANDOAHGC
1441   if (UseShenandoahGC) {
1442     _barriers = chunk->requires_barriers();
1443   } else
1444 #endif
1445   {

1478   return count;
1479 }
1480 
1481 static void invalidate_jvmti_stack(JavaThread* thread) {
1482   if (thread->is_interp_only_mode()) {
1483     JvmtiThreadState *state = thread->jvmti_thread_state();
1484     if (state != nullptr)
1485       state->invalidate_cur_stack_depth();
1486   }
1487 }
1488 
1489 static void jvmti_yield_cleanup(JavaThread* thread, ContinuationWrapper& cont) {
1490   if (JvmtiExport::can_post_frame_pop()) {
1491     int num_frames = num_java_frames(cont);
1492 
1493     ContinuationWrapper::SafepointOp so(Thread::current(), cont);
1494     JvmtiExport::continuation_yield_cleanup(JavaThread::current(), num_frames);
1495   }
1496   invalidate_jvmti_stack(thread);
1497 }
1498 #endif // INCLUDE_JVMTI
1499 
1500 #ifdef ASSERT
1501 static bool monitors_on_stack(JavaThread* thread) {
1502   ContinuationEntry* ce = thread->last_continuation();
1503   RegisterMap map(thread,
1504                   RegisterMap::UpdateMap::include,
1505                   RegisterMap::ProcessFrames::include,
1506                   RegisterMap::WalkContinuation::skip);
1507   map.set_include_argument_oops(false);
1508   for (frame f = thread->last_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map)) {
1509     if ((f.is_interpreted_frame() && ContinuationHelper::InterpretedFrame::is_owning_locks(f)) ||
1510         (f.is_compiled_frame() && ContinuationHelper::CompiledFrame::is_owning_locks(map.thread(), &map, f))) {
1511       return true;







1512     }
1513   }
1514   return false;


















1515 }



1516 
1517 // There are no interpreted frames if we're not called from the interpreter and we haven't ancountered an i2c
1518 // adapter or called Deoptimization::unpack_frames. As for native frames, upcalls from JNI also go through the
1519 // interpreter (see JavaCalls::call_helper), while the UpcallLinker explicitly sets cont_fastpath.
1520 bool FreezeBase::check_valid_fast_path() {
1521   ContinuationEntry* ce = _thread->last_continuation();
1522   RegisterMap map(_thread,
1523                   RegisterMap::UpdateMap::skip,
1524                   RegisterMap::ProcessFrames::skip,
1525                   RegisterMap::WalkContinuation::skip);
1526   map.set_include_argument_oops(false);
1527   for (frame f = freeze_start_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map)) {
1528     if (!f.is_compiled_frame() || f.is_deoptimized_frame()) {

1529       return false;
1530     }
1531   }
1532   return true;
1533 }
1534 #endif // ASSERT
1535 
1536 static inline int freeze_epilog(JavaThread* thread, ContinuationWrapper& cont) {
1537   verify_continuation(cont.continuation());
1538   assert(!cont.is_empty(), "");
1539   // This is done for the sake of the enterSpecial frame
1540   StackWatermarkSet::after_unwind(thread);
1541 
1542   log_develop_debug(continuations)("=== End of freeze cont ### #" INTPTR_FORMAT, cont.hash());
1543 
1544   return 0;
1545 }
1546 
1547 static int freeze_epilog(JavaThread* thread, ContinuationWrapper& cont, freeze_result res) {
1548   if (UNLIKELY(res != freeze_ok)) {
1549     verify_continuation(cont.continuation());
1550     log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1551     return res;
1552   }
1553 
1554   JVMTI_ONLY(jvmti_yield_cleanup(thread, cont)); // can safepoint
1555   return freeze_epilog(thread, cont);
1556 }
1557 
1558 template<typename ConfigT>


















1559 static inline int freeze_internal(JavaThread* current, intptr_t* const sp) {
1560   assert(!current->has_pending_exception(), "");
1561 
1562 #ifdef ASSERT
1563   log_trace(continuations)("~~~~ freeze sp: " INTPTR_FORMAT, p2i(current->last_continuation()->entry_sp()));
1564   log_frames(current);
1565 #endif
1566 
1567   CONT_JFR_ONLY(EventContinuationFreeze event;)
1568 
1569   ContinuationEntry* entry = current->last_continuation();
1570 
1571   oop oopCont = entry->cont_oop(current);
1572   assert(oopCont == current->last_continuation()->cont_oop(current), "");
1573   assert(ContinuationEntry::assert_entry_frame_laid_out(current), "");
1574 
1575   verify_continuation(oopCont);
1576   ContinuationWrapper cont(current, oopCont);
1577   log_develop_debug(continuations)("FREEZE #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
1578 
1579   assert(entry->is_virtual_thread() == (entry->scope(current) == java_lang_VirtualThread::vthread_scope()), "");
1580 
1581   assert(monitors_on_stack(current) == ((current->held_monitor_count() - current->jni_monitor_count()) > 0),
1582          "Held monitor count and locks on stack invariant: " INT64_FORMAT " JNI: " INT64_FORMAT, (int64_t)current->held_monitor_count(), (int64_t)current->jni_monitor_count());
1583 
1584   if (entry->is_pinned() || current->held_monitor_count() > 0) {

1585     log_develop_debug(continuations)("PINNED due to critical section/hold monitor");
1586     verify_continuation(cont.continuation());
1587     freeze_result res = entry->is_pinned() ? freeze_pinned_cs : freeze_pinned_monitor;
1588     log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1589     return res;
1590   }
1591 
1592   Freeze<ConfigT> freeze(current, cont, sp);















1593 
1594   assert(!current->cont_fastpath() || freeze.check_valid_fast_path(), "");
1595   bool fast = UseContinuationFastPath && current->cont_fastpath();
1596   if (fast && freeze.size_if_fast_freeze_available() > 0) {
1597     freeze.freeze_fast_existing_chunk();
1598     CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);)
1599     freeze_epilog(current, cont);
1600     return 0;










1601   }
1602 
1603   log_develop_trace(continuations)("chunk unavailable; transitioning to VM");
1604   assert(current == JavaThread::current(), "must be current thread except for preempt");
1605   JRT_BLOCK
1606     // delays a possible JvmtiSampledObjectAllocEventCollector in alloc_chunk
1607     JvmtiSampledObjectAllocEventCollector jsoaec(false);
1608     freeze.set_jvmti_event_collector(&jsoaec);
1609 
1610     freeze_result res = fast ? freeze.try_freeze_fast() : freeze.freeze_slow();
1611 
1612     CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);)
1613     freeze_epilog(current, cont, res);
1614     cont.done(); // allow safepoint in the transition back to Java
1615     return res;
1616   JRT_BLOCK_END
1617 }
1618 
1619 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint) {
1620   ContinuationEntry* entry = thread->last_continuation();
1621   if (entry == nullptr) {
1622     return freeze_ok;
1623   }
1624   if (entry->is_pinned()) {

1664       entry = entry->parent();
1665       if (entry == nullptr) {
1666         break;
1667       }
1668       if (entry->is_pinned()) {
1669         return freeze_pinned_cs;
1670       } else if (monitor_count > 0) {
1671         return freeze_pinned_monitor;
1672       }
1673     }
1674   }
1675   return freeze_ok;
1676 }
1677 
1678 /////////////// THAW ////
1679 
1680 static int thaw_size(stackChunkOop chunk) {
1681   int size = chunk->max_thawing_size();
1682   size += frame::metadata_words; // For the top pc+fp in push_return_frame or top = stack_sp - frame::metadata_words in thaw_fast
1683   size += 2*frame::align_wiggle; // in case of alignments at the top and bottom

1684   return size;
1685 }
1686 
1687 // make room on the stack for thaw
1688 // returns the size in bytes, or 0 on failure
1689 static inline int prepare_thaw_internal(JavaThread* thread, bool return_barrier) {
1690   log_develop_trace(continuations)("~~~~ prepare_thaw return_barrier: %d", return_barrier);
1691 
1692   assert(thread == JavaThread::current(), "");
1693 
1694   ContinuationEntry* ce = thread->last_continuation();
1695   assert(ce != nullptr, "");
1696   oop continuation = ce->cont_oop(thread);
1697   assert(continuation == get_continuation(thread), "");
1698   verify_continuation(continuation);
1699 
1700   stackChunkOop chunk = jdk_internal_vm_Continuation::tail(continuation);
1701   assert(chunk != nullptr, "");
1702 
1703   // The tail can be empty because it might still be available for another freeze.

1744 
1745   NOT_PRODUCT(int _frames;)
1746 
1747 protected:
1748   ThawBase(JavaThread* thread, ContinuationWrapper& cont) :
1749       _thread(thread), _cont(cont),
1750       _fastpath(nullptr) {
1751     DEBUG_ONLY(_top_unextended_sp_before_thaw = nullptr;)
1752     assert (cont.tail() != nullptr, "no last chunk");
1753     DEBUG_ONLY(_top_stack_address = _cont.entrySP() - thaw_size(cont.tail());)
1754   }
1755 
1756   void clear_chunk(stackChunkOop chunk);
1757   int remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize);
1758   void copy_from_chunk(intptr_t* from, intptr_t* to, int size);
1759 
1760   // fast path
1761   inline void prefetch_chunk_pd(void* start, int size_words);
1762   void patch_return(intptr_t* sp, bool is_last);
1763 
1764   // slow path
1765   NOINLINE intptr_t* thaw_slow(stackChunkOop chunk, bool return_barrier);

1766 
1767 private:
1768   void recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top);



1769   template<typename FKind> bool recurse_thaw_java_frame(frame& caller, int num_frames);
1770   void finalize_thaw(frame& entry, int argsize);
1771 
1772   inline bool seen_by_gc();
1773 
1774   inline void before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame);
1775   inline void after_thaw_java_frame(const frame& f, bool bottom);
1776   inline void patch(frame& f, const frame& caller, bool bottom);
1777   void clear_bitmap_bits(address start, address end);
1778 
1779   NOINLINE void recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames);
1780   void recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller);
1781   void recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames);
1782   void finish_thaw(frame& f);
1783 
1784   void push_return_frame(frame& f);
1785   inline frame new_entry_frame();
1786   template<typename FKind> frame new_stack_frame(const frame& hf, frame& caller, bool bottom);
1787   inline void patch_pd(frame& f, const frame& sender);

1788   inline intptr_t* align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom);
1789 
1790   void maybe_set_fastpath(intptr_t* sp) { if (sp > _fastpath) _fastpath = sp; }
1791 
1792   static inline void derelativize_interpreted_frame_metadata(const frame& hf, const frame& f);
1793 
1794  public:
1795   CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
1796 };
1797 
1798 template <typename ConfigT>
1799 class Thaw : public ThawBase {
1800 public:
1801   Thaw(JavaThread* thread, ContinuationWrapper& cont) : ThawBase(thread, cont) {}
1802 
1803   inline bool can_thaw_fast(stackChunkOop chunk) {
1804     return    !_barriers
1805            &&  _thread->cont_fastpath_thread_state()
1806            && !chunk->has_thaw_slowpath_condition()
1807            && !PreserveFramePointer;
1808   }
1809 
1810   inline intptr_t* thaw(Continuation::thaw_kind kind);
1811   NOINLINE intptr_t* thaw_fast(stackChunkOop chunk);

1812   inline void patch_caller_links(intptr_t* sp, intptr_t* bottom);
1813 };
1814 
1815 template <typename ConfigT>
1816 inline intptr_t* Thaw<ConfigT>::thaw(Continuation::thaw_kind kind) {
1817   verify_continuation(_cont.continuation());
1818   assert(!jdk_internal_vm_Continuation::done(_cont.continuation()), "");
1819   assert(!_cont.is_empty(), "");
1820 
1821   stackChunkOop chunk = _cont.tail();
1822   assert(chunk != nullptr, "guaranteed by prepare_thaw");
1823   assert(!chunk->is_empty(), "guaranteed by prepare_thaw");
1824 
1825   _barriers = chunk->requires_barriers();
1826   return (LIKELY(can_thaw_fast(chunk))) ? thaw_fast(chunk)
1827                                         : thaw_slow(chunk, kind != Continuation::thaw_top);
1828 }
1829 
1830 class ReconstructedStack : public StackObj {
1831   intptr_t* _base;  // _cont.entrySP(); // top of the entry frame
1832   int _thaw_size;
1833   int _argsize;
1834 public:
1835   ReconstructedStack(intptr_t* base, int thaw_size, int argsize)
1836   : _base(base), _thaw_size(thaw_size - (argsize == 0 ? frame::metadata_words_at_top : 0)), _argsize(argsize) {
1837     // The only possible source of misalignment is stack-passed arguments b/c compiled frames are 16-byte aligned.
1838     assert(argsize != 0 || (_base - _thaw_size) == ContinuationHelper::frame_align_pointer(_base - _thaw_size), "");
1839     // We're at most one alignment word away from entrySP
1840     assert(_base - 1 <= top() + total_size() + frame::metadata_words_at_bottom, "missed entry frame");
1841   }
1842 
1843   int entry_frame_extension() const { return _argsize + (_argsize > 0 ? frame::metadata_words_at_top : 0); }
1844 
1845   // top and bottom stack pointers
1846   intptr_t* sp() const { return ContinuationHelper::frame_align_pointer(_base - _thaw_size); }
1847   intptr_t* bottom_sp() const { return ContinuationHelper::frame_align_pointer(_base - entry_frame_extension()); }
1848 
1849   // several operations operate on the totality of the stack being reconstructed,
1850   // including the metadata words
1851   intptr_t* top() const { return sp() - frame::metadata_words_at_bottom;  }
1852   int total_size() const { return _thaw_size + frame::metadata_words_at_bottom; }
1853 };
1854 
1855 inline void ThawBase::clear_chunk(stackChunkOop chunk) {
1856   chunk->set_sp(chunk->stack_size());
1857   chunk->set_argsize(0);
1858   chunk->set_max_thawing_size(0);
1859 }
1860 
1861  int ThawBase::remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize) {
1862   bool empty = false;
1863   StackChunkFrameStream<ChunkFrames::CompiledOnly> f(chunk);
1864   DEBUG_ONLY(intptr_t* const chunk_sp = chunk->start_address() + chunk->sp();)
1865   assert(chunk_sp == f.sp(), "");
1866   assert(chunk_sp == f.unextended_sp(), "");
1867 
1868   const int frame_size = f.cb()->frame_size();
1869   argsize = f.stack_argsize();

1870 
1871   f.next(SmallRegisterMap::instance, true /* stop */);
1872   empty = f.is_done();
1873   assert(!empty || argsize == chunk->argsize(), "");
1874 













1875   if (empty) {
1876     clear_chunk(chunk);
1877   } else {
1878     chunk->set_sp(chunk->sp() + frame_size);
1879     chunk->set_max_thawing_size(chunk->max_thawing_size() - frame_size);
1880     // We set chunk->pc to the return pc into the next frame
1881     chunk->set_pc(f.pc());
1882 #ifdef ASSERT
1883     {
1884       intptr_t* retaddr_slot = (chunk_sp
1885                                 + frame_size
1886                                 - frame::sender_sp_ret_address_offset());
1887       assert(f.pc() == ContinuationHelper::return_address_at(retaddr_slot),
1888              "unexpected pc");
1889     }
1890 #endif
1891   }
1892   assert(empty == chunk->is_empty(), "");
1893   // returns the size required to store the frame on stack, and because it is a
1894   // compiled frame, it must include a copy of the arguments passed by the caller

1985     e.commit();
1986   }
1987 #endif
1988 
1989 #ifdef ASSERT
1990   set_anchor(_thread, rs.sp());
1991   log_frames(_thread);
1992   if (LoomDeoptAfterThaw) {
1993     do_deopt_after_thaw(_thread);
1994   }
1995   clear_anchor(_thread);
1996 #endif
1997 
1998   return rs.sp();
1999 }
2000 
2001 inline bool ThawBase::seen_by_gc() {
2002   return _barriers || _cont.tail()->is_gc_mode();
2003 }
2004 
2005 NOINLINE intptr_t* ThawBase::thaw_slow(stackChunkOop chunk, bool return_barrier) {
















































2006   LogTarget(Trace, continuations) lt;
2007   if (lt.develop_is_enabled()) {
2008     LogStream ls(lt);
2009     ls.print_cr("thaw slow return_barrier: %d " INTPTR_FORMAT, return_barrier, p2i(chunk));
2010     chunk->print_on(true, &ls);
2011   }
2012 
2013 #if CONT_JFR
2014   EventContinuationThawSlow e;
2015   if (e.should_commit()) {
2016     e.set_id(cast_from_oop<u8>(_cont.continuation()));
2017     e.commit();
2018   }
2019 #endif
2020 
2021   DEBUG_ONLY(_frames = 0;)
2022   _align_size = 0;
2023   int num_frames = (return_barrier ? 1 : 2);

2024 
2025   _stream = StackChunkFrameStream<ChunkFrames::Mixed>(chunk);
2026   _top_unextended_sp_before_thaw = _stream.unextended_sp();
2027 


2028   frame heap_frame = _stream.to_frame();
2029   if (lt.develop_is_enabled()) {
2030     LogStream ls(lt);
2031     ls.print_cr("top hframe before (thaw):");
2032     assert(heap_frame.is_heap_frame(), "should have created a relative frame");
2033     heap_frame.print_value_on(&ls, nullptr);
2034   }
2035 
2036 #if INCLUDE_ZGC || INCLUDE_SHENANDOAHGC
2037   if (UseZGC || UseShenandoahGC) {
2038     _cont.tail()->relativize_derived_pointers_concurrently();
2039   }
2040 #endif
2041 
2042   frame caller; // the thawed caller on the stack
2043   recurse_thaw(heap_frame, caller, num_frames, true);
2044   finish_thaw(caller); // caller is now the topmost thawed frame
2045   _cont.write();
2046 
2047   assert(_cont.chunk_invariant(), "");
2048 
2049   JVMTI_ONLY(if (!return_barrier) invalidate_jvmti_stack(_thread));
2050 
2051   _thread->set_cont_fastpath(_fastpath);
2052 
2053   intptr_t* sp = caller.sp();





2054   return sp;
2055 }
2056 
2057 void ThawBase::recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top) {
2058   log_develop_debug(continuations)("thaw num_frames: %d", num_frames);
2059   assert(!_cont.is_empty(), "no more frames");
2060   assert(num_frames > 0, "");
2061   assert(!heap_frame.is_empty(), "");
2062 
2063   if (top && heap_frame.is_safepoint_blob_frame()) {
2064     assert(ContinuationHelper::Frame::is_stub(heap_frame.cb()), "cb: %s", heap_frame.cb()->name());
2065     recurse_thaw_stub_frame(heap_frame, caller, num_frames);
2066   } else if (!heap_frame.is_interpreted_frame()) {
2067     recurse_thaw_compiled_frame(heap_frame, caller, num_frames, false);
2068   } else {
2069     recurse_thaw_interpreted_frame(heap_frame, caller, num_frames);
2070   }
2071 }
2072 
2073 template<typename FKind>
2074 bool ThawBase::recurse_thaw_java_frame(frame& caller, int num_frames) {
2075   assert(num_frames > 0, "");
2076 
2077   DEBUG_ONLY(_frames++;)
2078 
2079   int argsize = _stream.stack_argsize();
2080 
2081   _stream.next(SmallRegisterMap::instance);
2082   assert(_stream.to_frame().is_empty() == _stream.is_done(), "");
2083 
2084   // we never leave a compiled caller of an interpreted frame as the top frame in the chunk

2166 }
2167 
2168 void ThawBase::clear_bitmap_bits(address start, address end) {
2169   assert(is_aligned(start, wordSize), "should be aligned: " PTR_FORMAT, p2i(start));
2170   assert(is_aligned(end, VMRegImpl::stack_slot_size), "should be aligned: " PTR_FORMAT, p2i(end));
2171 
2172   // we need to clear the bits that correspond to arguments as they reside in the caller frame
2173   // or they will keep objects that are otherwise unreachable alive.
2174 
2175   // Align `end` if UseCompressedOops is not set to avoid UB when calculating the bit index, since
2176   // `end` could be at an odd number of stack slots from `start`, i.e might not be oop aligned.
2177   // If that's the case the bit range corresponding to the last stack slot should not have bits set
2178   // anyways and we assert that before returning.
2179   address effective_end = UseCompressedOops ? end : align_down(end, wordSize);
2180   log_develop_trace(continuations)("clearing bitmap for " INTPTR_FORMAT " - " INTPTR_FORMAT, p2i(start), p2i(effective_end));
2181   stackChunkOop chunk = _cont.tail();
2182   chunk->bitmap().clear_range(chunk->bit_index_for(start), chunk->bit_index_for(effective_end));
2183   assert(effective_end == end || !chunk->bitmap().at(chunk->bit_index_for(effective_end)), "bit should not be set");
2184 }
2185 

















































2186 NOINLINE void ThawBase::recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames) {
2187   assert(hf.is_interpreted_frame(), "");
2188 
2189   if (UNLIKELY(seen_by_gc())) {
2190     _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance);
2191   }
2192 
2193   const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::InterpretedFrame>(caller, num_frames);
2194 
2195   DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2196 
2197   _align_size += frame::align_wiggle; // possible added alignment for internal interpreted frame alignment om AArch64
2198 
2199   frame f = new_stack_frame<ContinuationHelper::InterpretedFrame>(hf, caller, is_bottom_frame);
2200 
2201   intptr_t* const stack_frame_top = f.sp() + frame::metadata_words_at_top;
2202   intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
2203   intptr_t* const heap_frame_top = hf.unextended_sp() + frame::metadata_words_at_top;
2204   intptr_t* const heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
2205 

2226 
2227   maybe_set_fastpath(f.sp());
2228 
2229   const int locals = hf.interpreter_frame_method()->max_locals();
2230 
2231   if (!is_bottom_frame) {
2232     // can only fix caller once this frame is thawed (due to callee saved regs)
2233     _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance);
2234   } else if (_cont.tail()->has_bitmap() && locals > 0) {
2235     assert(hf.is_heap_frame(), "should be");
2236     address start = (address)(heap_frame_bottom - locals);
2237     address end = (address)heap_frame_bottom;
2238     clear_bitmap_bits(start, end);
2239   }
2240 
2241   DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2242   caller = f;
2243 }
2244 
2245 void ThawBase::recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller) {
2246   assert(!hf.is_interpreted_frame(), "");
2247   assert(_cont.is_preempted() || !stub_caller, "stub caller not at preemption");
2248 
2249   if (!stub_caller && UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2250     _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance);
2251   }
2252 
2253   const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::CompiledFrame>(caller, num_frames);
2254 
2255   DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2256 
2257   assert(caller.sp() == caller.unextended_sp(), "");
2258 
2259   if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) {
2260     _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in freeze_interpreted_frame
2261   }
2262 
2263   // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2264   // yet laid out in the stack, and so the original_pc is not stored in it.
2265   // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2266   frame f = new_stack_frame<ContinuationHelper::CompiledFrame>(hf, caller, is_bottom_frame);
2267   intptr_t* const stack_frame_top = f.sp();
2268   intptr_t* const heap_frame_top = hf.unextended_sp();
2269 
2270   const int added_argsize = (is_bottom_frame || caller.is_interpreted_frame()) ? hf.compiled_frame_stack_argsize() : 0;
2271   int fsize = ContinuationHelper::CompiledFrame::size(hf) + added_argsize;
2272   assert(fsize <= (int)(caller.unextended_sp() - f.unextended_sp()), "");
2273 
2274   intptr_t* from = heap_frame_top - frame::metadata_words_at_bottom;
2275   intptr_t* to   = stack_frame_top - frame::metadata_words_at_bottom;
2276   // copy metadata, except the metadata at the top of the (unextended) entry frame
2277   int sz = fsize + frame::metadata_words_at_bottom + (is_bottom_frame && added_argsize == 0 ? 0 : frame::metadata_words_at_top);
2278 
2279   // If we're the bottom-most thawed frame, we're writing to within one word from entrySP
2280   // (we might have one padding word for alignment)
2281   assert(!is_bottom_frame || (_cont.entrySP() - 1 <= to + sz && to + sz <= _cont.entrySP()), "");
2282   assert(!is_bottom_frame || hf.compiled_frame_stack_argsize() != 0 || (to + sz && to + sz == _cont.entrySP()), "");
2283 
2284   copy_from_chunk(from, to, sz); // copying good oops because we invoked barriers above
2285 
2286   patch(f, caller, is_bottom_frame);
2287 
2288   // f.is_deoptimized_frame() is always false and we must test hf.is_deoptimized_frame() (see comment above)
2289   assert(!f.is_deoptimized_frame(), "");
2290   if (hf.is_deoptimized_frame()) {
2291     maybe_set_fastpath(f.sp());
2292   } else if (_thread->is_interp_only_mode()
2293               || (_cont.is_preempted() && f.cb()->as_compiled_method()->is_marked_for_deoptimization())) {
2294     // The caller of the safepoint stub when the continuation is preempted is not at a call instruction, and so
2295     // cannot rely on nmethod patching for deopt.
2296     assert(_thread->is_interp_only_mode() || stub_caller, "expected a stub-caller");
2297 
2298     log_develop_trace(continuations)("Deoptimizing thawed frame");
2299     DEBUG_ONLY(ContinuationHelper::Frame::patch_pc(f, nullptr));
2300 
2301     f.deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2302     assert(f.is_deoptimized_frame(), "");
2303     assert(ContinuationHelper::Frame::is_deopt_return(f.raw_pc(), f), "");
2304     maybe_set_fastpath(f.sp());
2305   }
2306 
2307   if (!is_bottom_frame) {
2308     // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2309     _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance);
2310   } else if (_cont.tail()->has_bitmap() && added_argsize > 0) {
2311     address start = (address)(heap_frame_top + ContinuationHelper::CompiledFrame::size(hf) + frame::metadata_words_at_top);
2312     int stack_args_slots = f.cb()->as_compiled_method()->method()->num_stack_arg_slots(false /* rounded */);
2313     int argsize_in_bytes = stack_args_slots * VMRegImpl::stack_slot_size;
2314     clear_bitmap_bits(start, start + argsize_in_bytes);
2315   }
2316 
2317   DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2318   caller = f;
2319 }
2320 
2321 void ThawBase::recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames) {
2322   DEBUG_ONLY(_frames++;)

2323 
2324   {


2325     RegisterMap map(nullptr,
2326                     RegisterMap::UpdateMap::include,
2327                     RegisterMap::ProcessFrames::skip,
2328                     RegisterMap::WalkContinuation::skip);
2329     map.set_include_argument_oops(false);
2330     _stream.next(&map);
2331     assert(!_stream.is_done(), "");
2332     if (UNLIKELY(seen_by_gc())) { // we're now doing this on the stub's caller
2333       _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, &map);
2334     }
2335     assert(!_stream.is_done(), "");

2336   }
2337 
2338   recurse_thaw_compiled_frame(_stream.to_frame(), caller, num_frames, true); // this could be deoptimized
2339 
2340   DEBUG_ONLY(before_thaw_java_frame(hf, caller, false, num_frames);)








2341 
2342   assert(ContinuationHelper::Frame::is_stub(hf.cb()), "");
2343   assert(caller.sp() == caller.unextended_sp(), "");
2344   assert(!caller.is_interpreted_frame(), "");
2345 
2346   int fsize = ContinuationHelper::StubFrame::size(hf);




2347 
2348   frame f = new_stack_frame<ContinuationHelper::StubFrame>(hf, caller, false);
2349   intptr_t* stack_frame_top = f.sp();
2350   intptr_t* heap_frame_top = hf.sp();

2351 
2352   copy_from_chunk(heap_frame_top - frame::metadata_words, stack_frame_top - frame::metadata_words,
2353                   fsize + frame::metadata_words);
2354 
2355   { // can only fix caller once this frame is thawed (due to callee saved regs)



2356     RegisterMap map(nullptr,
2357                     RegisterMap::UpdateMap::include,
2358                     RegisterMap::ProcessFrames::skip,
2359                     RegisterMap::WalkContinuation::skip); // map.clear();
2360     map.set_include_argument_oops(false);
2361     f.oop_map()->update_register_map(&f, &map);
2362     ContinuationHelper::update_register_map_with_callee(caller, &map);
2363     _cont.tail()->fix_thawed_frame(caller, &map);
2364   }
2365 
2366   DEBUG_ONLY(after_thaw_java_frame(f, false);)
2367   caller = f;
2368 }
2369 
2370 void ThawBase::finish_thaw(frame& f) {
2371   stackChunkOop chunk = _cont.tail();
2372 
2373   if (chunk->is_empty()) {
2374     // Only remove chunk from list if it can't be reused for another freeze
2375     if (seen_by_gc()) {
2376       _cont.set_tail(chunk->parent());
2377     } else {
2378       chunk->set_has_mixed_frames(false);
2379     }
2380     chunk->set_max_thawing_size(0);
2381     assert(chunk->argsize() == 0, "");
2382   } else {
2383     chunk->set_max_thawing_size(chunk->max_thawing_size() - _align_size);
2384   }
2385   assert(chunk->is_empty() == (chunk->max_thawing_size() == 0), "");
2386 

2438 
2439   assert(!jdk_internal_vm_Continuation::done(oopCont), "");
2440   assert(oopCont == get_continuation(thread), "");
2441   verify_continuation(oopCont);
2442 
2443   assert(entry->is_virtual_thread() == (entry->scope(thread) == java_lang_VirtualThread::vthread_scope()), "");
2444 
2445   ContinuationWrapper cont(thread, oopCont);
2446   log_develop_debug(continuations)("THAW #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
2447 
2448 #ifdef ASSERT
2449   set_anchor_to_entry(thread, cont.entry());
2450   log_frames(thread);
2451   clear_anchor(thread);
2452 #endif
2453 
2454   Thaw<ConfigT> thw(thread, cont);
2455   intptr_t* const sp = thw.thaw(kind);
2456   assert(is_aligned(sp, frame::frame_alignment), "");
2457 
2458   // All the frames have been thawed so we know they don't hold any monitors
2459   assert(thread->held_monitor_count() == 0, "Must be");
2460 
2461 #ifdef ASSERT
2462   intptr_t* sp0 = sp;
2463   set_anchor(thread, sp0);




















2464   log_frames(thread);
2465   if (LoomVerifyAfterThaw) {
2466     assert(do_verify_after_thaw(thread, cont.tail(), tty), "");
2467   }
2468   assert(ContinuationEntry::assert_entry_frame_laid_out(thread), "");
2469   clear_anchor(thread);
2470 
2471   LogTarget(Trace, continuations) lt;
2472   if (lt.develop_is_enabled()) {
2473     LogStream ls(lt);
2474     ls.print_cr("Jumping to frame (thaw):");
2475     frame(sp).print_value_on(&ls, nullptr);
2476   }
2477 #endif
2478 
2479   CONT_JFR_ONLY(thw.jfr_info().post_jfr_event(&event, cont.continuation(), thread);)
2480 
2481   verify_continuation(cont.continuation());
2482   log_develop_debug(continuations)("=== End of thaw #" INTPTR_FORMAT, cont.hash());
2483 

2555         st->print_cr("size: %d argsize: %d",
2556                      ContinuationHelper::NonInterpretedUnknownFrame::size(fr),
2557                      ContinuationHelper::NonInterpretedUnknownFrame::stack_argsize(fr));
2558       }
2559       VMReg reg = fst.register_map()->find_register_spilled_here(cl.p(), fst.current()->sp());
2560       if (reg != nullptr) {
2561         st->print_cr("Reg %s %d", reg->name(), reg->is_stack() ? (int)reg->reg2stack() : -99);
2562       }
2563       cl.reset();
2564       DEBUG_ONLY(thread->print_frame_layout();)
2565       if (chunk != nullptr) {
2566         chunk->print_on(true, st);
2567       }
2568       return false;
2569     }
2570   }
2571   return true;
2572 }
2573 
2574 static void log_frames(JavaThread* thread) {
2575   const static int show_entry_callers = 3;
2576   LogTarget(Trace, continuations) lt;
2577   if (!lt.develop_is_enabled()) {
2578     return;
2579   }
2580   LogStream ls(lt);
2581 
2582   ls.print_cr("------- frames ---------");
2583   if (!thread->has_last_Java_frame()) {
2584     ls.print_cr("NO ANCHOR!");
2585   }
2586 
2587   RegisterMap map(thread,
2588                   RegisterMap::UpdateMap::include,
2589                   RegisterMap::ProcessFrames::include,
2590                   RegisterMap::WalkContinuation::skip);
2591   map.set_include_argument_oops(false);
2592 
2593   if (false) {
2594     for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) {
2595       f.print_on(&ls);
2596     }
2597   } else {
2598     map.set_skip_missing(true);
2599     ResetNoHandleMark rnhm;
2600     ResourceMark rm;
2601     HandleMark hm(Thread::current());
2602     FrameValues values;
2603 
2604     int i = 0;
2605     int post_entry = -1;
2606     for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) {
2607       f.describe(values, i++, &map);
2608       if (post_entry >= 0 || Continuation::is_continuation_enterSpecial(f))
2609         post_entry++;
2610       if (post_entry >= show_entry_callers)
2611         break;
2612     }
2613     values.print_on(thread, &ls);
2614   }
2615 
2616   ls.print_cr("======= end frames =========");
2617 }
2618 #endif // ASSERT
2619 
2620 #include CPU_HEADER_INLINE(continuationFreezeThaw)
2621 
2622 #ifdef ASSERT
2623 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st) {
2624   ResourceMark rm;
2625   FrameValues values;
2626   assert(f.get_cb() != nullptr, "");
2627   RegisterMap map(f.is_heap_frame() ?
2628                     nullptr :
2629                     JavaThread::current(),
2630                   RegisterMap::UpdateMap::include,
2631                   RegisterMap::ProcessFrames::skip,
2632                   RegisterMap::WalkContinuation::skip);
2633   map.set_include_argument_oops(false);
2634   map.set_skip_missing(true);
2635   if (callee_complete) {
2636     frame::update_map_with_saved_link(&map, ContinuationHelper::Frame::callee_link_address(f));
2637   }
2638   const_cast<frame&>(f).describe(values, 0, &map);
2639   values.print_on(static_cast<JavaThread*>(nullptr), st);
2640 }
2641 #endif
2642 
2643 static address thaw_entry   = nullptr;
2644 static address freeze_entry = nullptr;

2645 
2646 address Continuation::thaw_entry() {
2647   return ::thaw_entry;
2648 }
2649 
2650 address Continuation::freeze_entry() {
2651   return ::freeze_entry;
2652 }
2653 




2654 class ConfigResolve {
2655 public:
2656   static void resolve() { resolve_compressed(); }
2657 
2658   static void resolve_compressed() {
2659     UseCompressedOops ? resolve_gc<true>()
2660                       : resolve_gc<false>();
2661   }
2662 
2663 private:
2664   template <bool use_compressed>
2665   static void resolve_gc() {
2666     BarrierSet* bs = BarrierSet::barrier_set();
2667     assert(bs != nullptr, "freeze/thaw invoked before BarrierSet is set");
2668     switch (bs->kind()) {
2669 #define BARRIER_SET_RESOLVE_BARRIER_CLOSURE(bs_name)                    \
2670       case BarrierSet::bs_name: {                                       \
2671         resolve<use_compressed, typename BarrierSet::GetType<BarrierSet::bs_name>::type>(); \
2672       }                                                                 \
2673         break;
2674       FOR_EACH_CONCRETE_BARRIER_SET_DO(BARRIER_SET_RESOLVE_BARRIER_CLOSURE)
2675 #undef BARRIER_SET_RESOLVE_BARRIER_CLOSURE
2676 
2677     default:
2678       fatal("BarrierSet resolving not implemented");
2679     };
2680   }
2681 
2682   template <bool use_compressed, typename BarrierSetT>
2683   static void resolve() {
2684     typedef Config<use_compressed ? oop_kind::NARROW : oop_kind::WIDE, BarrierSetT> SelectedConfigT;
2685 
2686     freeze_entry = (address)freeze<SelectedConfigT>;

2687 
2688     // If we wanted, we could templatize by kind and have three different thaw entries
2689     thaw_entry   = (address)thaw<SelectedConfigT>;
2690   }
2691 };
2692 
2693 void Continuation::init() {
2694   ConfigResolve::resolve();
2695 }

  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  *

 183 static void log_frames(JavaThread* thread);
 184 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st = tty);
 185 
 186 #define assert_pfl(p, ...) \
 187 do {                                           \
 188   if (!(p)) {                                  \
 189     JavaThread* t = JavaThread::active();      \
 190     if (t->has_last_Java_frame()) {            \
 191       tty->print_cr("assert(" #p ") failed:"); \
 192       t->print_frame_layout();                 \
 193     }                                          \
 194   }                                            \
 195   vmassert(p, __VA_ARGS__);                    \
 196 } while(0)
 197 
 198 #else
 199 static void verify_continuation(oop continuation) { }
 200 #define assert_pfl(p, ...)
 201 #endif
 202 



















 203 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint);
 204 template<typename ConfigT, bool preempt> static inline int freeze_internal(JavaThread* thread, intptr_t* const sp);
 205 
 206 static inline int prepare_thaw_internal(JavaThread* thread, bool return_barrier);
 207 template<typename ConfigT> static inline intptr_t* thaw_internal(JavaThread* thread, const Continuation::thaw_kind kind);
 208 
 209 
 210 // Entry point to freeze. Transitions are handled manually
 211 // Called from gen_continuation_yield() in sharedRuntime_<cpu>.cpp through Continuation::freeze_entry();
 212 template<typename ConfigT>
 213 static JRT_BLOCK_ENTRY(int, freeze(JavaThread* current, intptr_t* sp))
 214   assert(sp == current->frame_anchor()->last_Java_sp(), "");
 215 
 216   if (current->raw_cont_fastpath() > current->last_continuation()->entry_sp() || current->raw_cont_fastpath() < sp) {
 217     current->set_cont_fastpath(nullptr);
 218   }
 219 
 220   return ConfigT::freeze(current, sp);
 221 JRT_END
 222 
 223 JRT_LEAF(int, Continuation::prepare_thaw(JavaThread* thread, bool return_barrier))
 224   return prepare_thaw_internal(thread, return_barrier);
 225 JRT_END
 226 
 227 template<typename ConfigT>
 228 static JRT_LEAF(intptr_t*, thaw(JavaThread* thread, int kind))
 229   // TODO: JRT_LEAF and NoHandleMark is problematic for JFR events.
 230   // vFrameStreamCommon allocates Handles in RegisterMap for continuations.
 231   // Also the preemption case with JVMTI events enabled might safepoint so
 232   // undo the NoSafepointVerifier here and rely on handling by ContinuationWrapper.
 233   // JRT_ENTRY instead?
 234   ResetNoHandleMark rnhm;
 235   debug_only(PauseNoSafepointVerifier pnsv(&__nsv);)
 236 
 237   // we might modify the code cache via BarrierSetNMethod::nmethod_entry_barrier
 238   MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
 239   return ConfigT::thaw(thread, (Continuation::thaw_kind)kind);
 240 JRT_END
 241 
 242 JVM_ENTRY(jint, CONT_isPinned0(JNIEnv* env, jobject cont_scope)) {
 243   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
 244   return is_pinned0(thread, JNIHandles::resolve(cont_scope), false);
 245 }
 246 JVM_END
 247 
 248 ///////////
 249 
 250 enum class oop_kind { NARROW, WIDE };
 251 template <oop_kind oops, typename BarrierSetT>
 252 class Config {
 253 public:
 254   typedef Config<oops, BarrierSetT> SelfT;
 255   using OopT = std::conditional_t<oops == oop_kind::NARROW, narrowOop, oop>;
 256 
 257   static int freeze(JavaThread* thread, intptr_t* const sp) {
 258     return freeze_internal<SelfT, false>(thread, sp);
 259   }
 260 
 261   static int freeze_preempt(JavaThread* thread, intptr_t* const sp) {
 262     return freeze_internal<SelfT, true>(thread, sp);
 263   }
 264 
 265   static intptr_t* thaw(JavaThread* thread, Continuation::thaw_kind kind) {
 266     return thaw_internal<SelfT>(thread, kind);
 267   }
 268 };
 269 
 270 static bool stack_overflow_check(JavaThread* thread, size_t size, address sp) {
 271   const size_t page_size = os::vm_page_size();
 272   if (size > page_size) {
 273     if (sp - size < thread->stack_overflow_state()->shadow_zone_safe_limit()) {
 274       return false;
 275     }
 276   }
 277   return true;
 278 }
 279 
 280 #ifdef ASSERT
 281 static oop get_continuation(JavaThread* thread) {
 282   assert(thread != nullptr, "");
 283   assert(thread->threadObj() != nullptr, "");
 284   return java_lang_Thread::continuation(thread->threadObj());
 285 }
 286 #endif // ASSERT
 287 
 288 inline void clear_anchor(JavaThread* thread) {
 289   thread->frame_anchor()->clear();
 290 }
 291 
 292 static void set_anchor(JavaThread* thread, intptr_t* sp, address pc = nullptr) {
 293   if (pc == nullptr) {
 294     pc = ContinuationHelper::return_address_at(
 295            sp - frame::sender_sp_ret_address_offset());
 296   }
 297   assert(pc != nullptr, "");
 298 
 299   JavaFrameAnchor* anchor = thread->frame_anchor();
 300   anchor->set_last_Java_sp(sp);
 301   anchor->set_last_Java_pc(pc);
 302   ContinuationHelper::set_anchor_pd(anchor, sp);
 303 
 304   assert(thread->has_last_Java_frame(), "");
 305   assert(thread->last_frame().cb() != nullptr, "");
 306 }

 307 
 308 static void set_anchor_to_entry(JavaThread* thread, ContinuationEntry* entry) {
 309   JavaFrameAnchor* anchor = thread->frame_anchor();
 310   anchor->set_last_Java_sp(entry->entry_sp());
 311   anchor->set_last_Java_pc(entry->entry_pc());
 312   ContinuationHelper::set_anchor_to_entry_pd(anchor, entry);
 313 
 314   assert(thread->has_last_Java_frame(), "");
 315   assert(thread->last_frame().cb() != nullptr, "");
 316 }
 317 
 318 #ifdef ASSERT
 319 static int monitors_to_fix_on_stack(JavaThread* thread) {
 320   ResourceMark rm(JavaThread::current());
 321   ContinuationEntry* ce = thread->last_continuation();
 322   RegisterMap map(thread,
 323                   RegisterMap::UpdateMap::include,
 324                   RegisterMap::ProcessFrames::include,
 325                   RegisterMap::WalkContinuation::include);
 326   map.set_include_argument_oops(false);
 327   ResourceHashtable<oopDesc*, bool> rhtable;
 328   int monitor_count = 0;
 329   for (frame f = thread->last_frame(); Continuation::is_frame_in_continuation(thread, f); f = f.sender(&map)) {
 330     if (f.is_interpreted_frame()) {
 331       frame abs = !f.is_heap_frame() ? f : map.stack_chunk()->derelativize(f);
 332       monitor_count += ContinuationHelper::InterpretedFrame::monitors_to_fix(thread, abs, rhtable, map.stack_chunk()());
 333     } else if (f.is_compiled_frame()) {
 334       monitor_count += ContinuationHelper::CompiledFrame::monitors_to_fix(thread, &map, f, rhtable);
 335     } else if (f.is_native_frame()) {
 336       monitor_count += ContinuationHelper::NativeFrame::monitors_to_fix(thread, f, rhtable);
 337     }
 338   }
 339   return monitor_count;
 340 }
 341 #endif
 342 
 343 #if CONT_JFR
 344 class FreezeThawJfrInfo : public StackObj {
 345   short _e_size;
 346   short _e_num_interpreted_frames;
 347  public:
 348 
 349   FreezeThawJfrInfo() : _e_size(0), _e_num_interpreted_frames(0) {}
 350   inline void record_interpreted_frame() { _e_num_interpreted_frames++; }
 351   inline void record_size_copied(int size) { _e_size += size << LogBytesPerWord; }
 352   template<typename Event> void post_jfr_event(Event *e, oop continuation, JavaThread* jt);
 353 };
 354 
 355 template<typename Event> void FreezeThawJfrInfo::post_jfr_event(Event* e, oop continuation, JavaThread* jt) {
 356   if (e->should_commit()) {
 357     log_develop_trace(continuations)("JFR event: iframes: %d size: %d", _e_num_interpreted_frames, _e_size);
 358     e->set_carrierThread(JFR_JVM_THREAD_ID(jt));
 359     e->set_continuationClass(continuation->klass());
 360     e->set_interpretedFrames(_e_num_interpreted_frames);
 361     e->set_size(_e_size);
 362     e->commit();
 363   }
 364 }
 365 #endif // CONT_JFR
 366 
 367 /////////////// FREEZE ////
 368 
 369 class FreezeBase : public StackObj {
 370 protected:
 371   JavaThread* const _thread;
 372   ContinuationWrapper& _cont;
 373   bool _barriers; // only set when we allocate a chunk


 374 
 375   intptr_t* _bottom_address;
 376 
 377   const bool _preempt;
 378   // Used on preemption only
 379   frame _last_frame;
 380   oop _monitorenter_obj;
 381 
 382   // Used to support freezing with held monitors
 383   int _monitors_to_fix;
 384   int _monitors_in_lockstack;
 385 
 386   int _freeze_size; // total size of all frames plus metadata in words.
 387   int _total_align_size;
 388 
 389   intptr_t* _cont_stack_top;
 390   intptr_t* _cont_stack_bottom;
 391 
 392   CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;)
 393 
 394 #ifdef ASSERT
 395   intptr_t* _orig_chunk_sp;
 396   int _fast_freeze_size;
 397   bool _empty;
 398 #endif
 399 
 400   JvmtiSampledObjectAllocEventCollector* _jvmti_event_collector;
 401 
 402   NOT_PRODUCT(int _frames;)
 403   DEBUG_ONLY(intptr_t* _last_write;)
 404 
 405   inline FreezeBase(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp, bool preempt);
 406 
 407 public:
 408   NOINLINE freeze_result freeze_slow();
 409   void freeze_fast_existing_chunk();
 410 
 411   CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
 412   void set_jvmti_event_collector(JvmtiSampledObjectAllocEventCollector* jsoaec) { _jvmti_event_collector = jsoaec; }
 413 
 414   inline int size_if_fast_freeze_available();
 415 
 416   inline frame& last_frame() { return _last_frame; }
 417   inline void set_last_frame() { _last_frame = _thread->last_frame(); }
 418 
 419 #ifdef ASSERT
 420   bool check_valid_fast_path();
 421 #endif
 422 
 423 protected:
 424   inline void init_rest();
 425   void throw_stack_overflow_on_humongous_chunk();
 426 
 427   // fast path
 428   inline void copy_to_chunk(intptr_t* from, intptr_t* to, int size);
 429   inline void unwind_frames();
 430   inline void patch_stack_pd(intptr_t* frame_sp, intptr_t* heap_sp);
 431 
 432   // slow path
 433   virtual stackChunkOop allocate_chunk_slow(size_t stack_size) = 0;
 434 
 435   int cont_size() { return pointer_delta_as_int(_cont_stack_bottom, _cont_stack_top); }
 436 
 437 private:
 438   // slow path
 439   frame freeze_start_frame();
 440   frame freeze_start_frame_on_preempt();
 441   NOINLINE freeze_result recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top);
 442   inline frame freeze_start_frame_yield_stub();
 443   template<typename FKind>
 444   inline freeze_result recurse_freeze_java_frame(const frame& f, frame& caller, int fsize, int argsize);
 445   inline void before_freeze_java_frame(const frame& f, const frame& caller, int fsize, int argsize, bool is_bottom_frame);
 446   inline void after_freeze_java_frame(const frame& hf, bool is_bottom_frame);
 447   freeze_result finalize_freeze(const frame& callee, frame& caller, int argsize);
 448   void patch(const frame& f, frame& hf, const frame& caller, bool is_bottom_frame);
 449   NOINLINE freeze_result recurse_freeze_interpreted_frame(frame& f, frame& caller, int callee_argsize, bool callee_interpreted);
 450   freeze_result recurse_freeze_compiled_frame(frame& f, frame& caller, int callee_argsize, bool callee_interpreted);
 451   NOINLINE freeze_result recurse_freeze_stub_frame(frame& f, frame& caller);
 452   NOINLINE void finish_freeze(const frame& f, const frame& top);
 453 
 454   void fix_monitors_in_interpreted_frame(frame& f);
 455   template <typename RegisterMapT>
 456   void fix_monitors_in_compiled_frame(frame& f, RegisterMapT* map);
 457   void fix_monitors_in_fast_path();
 458 
 459   inline bool stack_overflow();
 460 
 461   static frame sender(const frame& f) { return f.is_interpreted_frame() ? sender<ContinuationHelper::InterpretedFrame>(f)
 462                                                                         : sender<ContinuationHelper::NonInterpretedUnknownFrame>(f); }
 463   template<typename FKind> static inline frame sender(const frame& f);
 464   template<typename FKind> frame new_heap_frame(frame& f, frame& caller);
 465   inline void set_top_frame_metadata_pd(const frame& hf);
 466   inline void patch_pd(frame& callee, const frame& caller);
 467   void adjust_interpreted_frame_unextended_sp(frame& f);
 468   static inline void prepare_freeze_interpreted_top_frame(const frame& f);
 469   static inline void relativize_interpreted_frame_metadata(const frame& f, const frame& hf);
 470 
 471 protected:
 472   void freeze_fast_copy(stackChunkOop chunk, int chunk_start_sp CONT_JFR_ONLY(COMMA bool chunk_is_allocated));
 473   bool freeze_fast_new_chunk(stackChunkOop chunk);
 474 
 475 #ifdef ASSERT
 476   bool is_empty(stackChunkOop chunk) {
 477     // during freeze, the chunk is in an intermediate state (after setting the chunk's argsize but before setting its
 478     // ultimate sp) so we use this instead of stackChunkOopDesc::is_empty
 479     return chunk->sp() >= chunk->stack_size() - chunk->argsize() - frame::metadata_words_at_top;
 480   }
 481 #endif
 482 };
 483 
 484 template <typename ConfigT>
 485 class Freeze : public FreezeBase {
 486 private:
 487   stackChunkOop allocate_chunk(size_t stack_size);
 488 
 489 public:
 490   inline Freeze(JavaThread* thread, ContinuationWrapper& cont, intptr_t* frame_sp, bool preempt)
 491     : FreezeBase(thread, cont, frame_sp, preempt) {}
 492 
 493   freeze_result try_freeze_fast();
 494 
 495 protected:
 496   virtual stackChunkOop allocate_chunk_slow(size_t stack_size) override { return allocate_chunk(stack_size); }
 497 };
 498 
 499 FreezeBase::FreezeBase(JavaThread* thread, ContinuationWrapper& cont, intptr_t* frame_sp, bool preempt) :
 500     _thread(thread), _cont(cont), _barriers(false), _preempt(preempt), _last_frame(false /* no initialization */) {
 501   DEBUG_ONLY(_jvmti_event_collector = nullptr;)
 502 
 503   assert(_thread != nullptr, "");
 504   assert(_thread->last_continuation()->entry_sp() == _cont.entrySP(), "");
 505 
 506   DEBUG_ONLY(_cont.entry()->verify_cookie();)
 507 
 508   assert(!Interpreter::contains(_cont.entryPC()), "");
 509 
 510   _bottom_address = _cont.entrySP() - _cont.entry_frame_extension();
 511 #ifdef _LP64
 512   if (((intptr_t)_bottom_address & 0xf) != 0) {
 513     _bottom_address--;
 514   }
 515   assert(is_aligned(_bottom_address, frame::frame_alignment), "");
 516 #endif
 517 
 518   log_develop_trace(continuations)("bottom_address: " INTPTR_FORMAT " entrySP: " INTPTR_FORMAT " argsize: " PTR_FORMAT,
 519                 p2i(_bottom_address), p2i(_cont.entrySP()), (_cont.entrySP() - _bottom_address) << LogBytesPerWord);
 520   assert(_bottom_address != nullptr, "");
 521   assert(_bottom_address <= _cont.entrySP(), "");
 522   DEBUG_ONLY(_last_write = nullptr;)
 523 
 524   assert(_cont.chunk_invariant(), "");
 525   assert(!Interpreter::contains(_cont.entryPC()), "");
 526 #if !defined(PPC64) || defined(ZERO)
 527   static const int doYield_stub_frame_size = frame::metadata_words;
 528 #else
 529   static const int doYield_stub_frame_size = frame::native_abi_reg_args_size >> LogBytesPerWord;
 530 #endif
 531   // With preemption doYield() might not have been resolved yet
 532   assert(_preempt || SharedRuntime::cont_doYield_stub()->frame_size() == doYield_stub_frame_size, "");
 533 
 534   // properties of the continuation on the stack; all sizes are in words
 535   _cont_stack_top    = frame_sp + (!preempt ? doYield_stub_frame_size : 0); // we don't freeze the doYield stub frame
 536   _cont_stack_bottom = _cont.entrySP() + (_cont.argsize() == 0 ? frame::metadata_words_at_top : 0)
 537       - ContinuationHelper::frame_align_words(_cont.argsize()); // see alignment in thaw
 538 
 539   log_develop_trace(continuations)("freeze size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT,
 540     cont_size(), _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom));
 541   assert(cont_size() > 0, "");
 542 
 543   if (LockingMode == LM_LEGACY) {
 544     _monitors_to_fix = thread->held_monitor_count();
 545     assert(_monitors_to_fix >= 0, "invariant");
 546     _monitors_in_lockstack = 0;
 547     assert(_thread->lock_stack().monitor_count() == 0, "should be set only for LM_LIGHTWEIGHT");
 548   } else {
 549     _monitors_in_lockstack = _thread->lock_stack().monitor_count();
 550     assert(_monitors_in_lockstack >= 0 && _monitors_in_lockstack <= 8, "_monitors_in_lockstack=%d", _monitors_in_lockstack);
 551     _monitors_to_fix = _monitors_in_lockstack;
 552     assert(thread->held_monitor_count() == 0, "should be set only for LM_LEGACY");
 553   }
 554   DEBUG_ONLY(int verified_cnt = monitors_to_fix_on_stack(_thread);)
 555   assert(verified_cnt == _monitors_to_fix || thread->obj_locker_count() > 0 ||
 556          (LockingMode == LM_LIGHTWEIGHT && verified_cnt == _thread->lock_stack().unique_count()),
 557          "wrong monitor count. Found %d in the stack but counter is %d", verified_cnt, _monitors_to_fix);
 558 }
 559 
 560 void FreezeBase::init_rest() { // we want to postpone some initialization after chunk handling
 561   _freeze_size = 0;
 562   _total_align_size = 0;
 563   NOT_PRODUCT(_frames = 0;)
 564 }
 565 
 566 void FreezeBase::fix_monitors_in_interpreted_frame(frame& f) {
 567   assert(LockingMode == LM_LEGACY, "invariant");
 568   BasicObjectLock* first_mon = f.interpreter_frame_monitor_begin();
 569   BasicObjectLock* last_mon = f.interpreter_frame_monitor_end();
 570   assert(last_mon <= first_mon, "must be");
 571 
 572   if (first_mon == last_mon) {
 573     // No monitors in this frame
 574     return;
 575   }
 576 
 577   for (BasicObjectLock* current = f.previous_monitor_in_interpreter_frame(first_mon);
 578        current >= last_mon; current = f.previous_monitor_in_interpreter_frame(current)) {
 579     oop obj = current->obj();
 580     if (obj == nullptr) {
 581       continue;
 582     }
 583     if (obj == _monitorenter_obj) {
 584       assert(_preempt, "should be preemption on monitorenter case");
 585       assert(obj->mark().monitor() != nullptr, "failed to acquire the lock but its not inflated");
 586       continue;
 587     }
 588     markWord mark = obj->mark();
 589     if (mark.has_monitor() && !mark.monitor()->is_owner_anonymous()) {
 590       // Good for freezing, nothing to do.
 591       assert(mark.monitor()->is_owner(_thread), "invariant");
 592       continue;
 593     }
 594     // Found locked monitor that needs fixing. Inflate will fix the owner for stack-locked case or inflated but anonymously owned.
 595     ObjectMonitor* om = ObjectSynchronizer::inflate(_thread, obj, ObjectSynchronizer::InflateCause::inflate_cause_cont_freeze);
 596     assert(om != nullptr, "invariant");
 597     assert(om->is_owner(_thread), "invariant");
 598     if (--_monitors_to_fix == 0) break;
 599   }
 600   assert(_monitors_to_fix >= 0, "invariant");
 601 }
 602 
 603 template <typename RegisterMapT>
 604 void FreezeBase::fix_monitors_in_compiled_frame(frame& f, RegisterMapT* map) {
 605   assert(LockingMode == LM_LEGACY, "invariant");
 606   assert(!f.is_interpreted_frame(), "");
 607   assert(ContinuationHelper::CompiledFrame::is_instance(f), "");
 608 
 609   CompiledMethod* cm = f.cb()->as_compiled_method();
 610 
 611   if (!cm->has_monitors()) {
 612     // No monitors in this frame
 613     return;
 614   }
 615 
 616   for (ScopeDesc* scope = cm->scope_desc_at(f.pc()); scope != nullptr; scope = scope->sender()) {
 617     GrowableArray<MonitorValue*>* mons = scope->monitors();
 618     if (mons == nullptr || mons->is_empty()) {
 619       continue;
 620     }
 621 
 622     for (int index = (mons->length()-1); index >= 0; index--) { // see compiledVFrame::monitors()
 623       MonitorValue* mon = mons->at(index);
 624       if (mon->eliminated()) {
 625         continue; // we ignore eliminated monitors
 626       }
 627       ScopeValue* ov = mon->owner();
 628       StackValue* owner_sv = StackValue::create_stack_value(&f, map, ov); // it is an oop
 629       oop obj = owner_sv->get_obj()();
 630       if (obj == nullptr) {
 631         continue;
 632       }
 633       if (obj == _monitorenter_obj) {
 634         assert(_preempt, "should be preemption on monitorenter case");
 635         assert(obj->mark().monitor() != nullptr, "failed to acquire the lock but its not inflated");
 636         continue;
 637       }
 638       markWord mark = obj->mark();
 639       if (mark.has_monitor() && !mark.monitor()->is_owner_anonymous()) {
 640         // Good for freezing, nothing to do.
 641         assert(mark.monitor()->is_owner(_thread), "invariant");
 642         continue;
 643       }
 644       // Found locked monitor that needs fixing. Inflate will fix the owner for stack-locked case or inflated but anonymously owned.
 645       ObjectMonitor* om = ObjectSynchronizer::inflate(_thread, obj, ObjectSynchronizer::InflateCause::inflate_cause_cont_freeze);
 646       assert(om != nullptr, "invariant");
 647       assert(om->is_owner(_thread), "invariant");
 648       if (--_monitors_to_fix == 0) break;
 649     }
 650   }
 651   assert(_monitors_to_fix >= 0, "invariant");
 652 }
 653 
 654 void FreezeBase::fix_monitors_in_fast_path() {
 655   if (LockingMode == LM_LIGHTWEIGHT) {
 656     stackChunkOop chunk = _cont.tail();
 657     assert(chunk->sp_address() - chunk->start_address() >= _monitors_in_lockstack, "no room for lockstack");
 658     _thread->lock_stack().move_to_address((oop*)chunk->start_address());
 659 
 660     chunk->set_lockStackSize((uint8_t)_monitors_in_lockstack);
 661     chunk->set_has_lockStack(true);
 662 
 663     DEBUG_ONLY(_monitors_to_fix -= _monitors_in_lockstack;)
 664   } else {
 665     assert(LockingMode == LM_LEGACY, "invariant");
 666     _monitorenter_obj = _thread->is_on_monitorenter() ? ((ObjectMonitor*)(_thread->_Stalled))->object() : nullptr;
 667 
 668     ResourceMark rm(_thread);
 669     RegisterMap map(JavaThread::current(),
 670                   RegisterMap::UpdateMap::include,
 671                   RegisterMap::ProcessFrames::skip, // already processed in unwind_frames()
 672                   RegisterMap::WalkContinuation::skip);
 673     map.set_include_argument_oops(false);
 674     frame freezed_top(_cont_stack_top);
 675     frame first = !_preempt ? freezed_top : freezed_top.sender(&map);
 676     for (frame f = first; _monitors_to_fix > 0 && Continuation::is_frame_in_continuation(_cont.entry(), f); f = f.sender(&map)) {
 677       fix_monitors_in_compiled_frame(f, &map);
 678     }
 679   }
 680   assert(_monitors_to_fix == 0, "missing monitors in stack");
 681   assert(_thread->held_monitor_count() == 0, "should be 0 now");
 682 }
 683 
 684 void FreezeBase::copy_to_chunk(intptr_t* from, intptr_t* to, int size) {
 685   stackChunkOop chunk = _cont.tail();
 686   chunk->copy_from_stack_to_chunk(from, to, size);
 687   CONT_JFR_ONLY(_jfr_info.record_size_copied(size);)
 688 
 689 #ifdef ASSERT
 690   if (_last_write != nullptr) {
 691     assert(_last_write == to + size, "Missed a spot: _last_write: " INTPTR_FORMAT " to+size: " INTPTR_FORMAT
 692         " stack_size: %d _last_write offset: " PTR_FORMAT " to+size: " PTR_FORMAT, p2i(_last_write), p2i(to+size),
 693         chunk->stack_size(), _last_write-chunk->start_address(), to+size-chunk->start_address());
 694     _last_write = to;
 695   }
 696 #endif
 697 }
 698 
 699 // Called _after_ the last possible safepoint during the freeze operation (chunk allocation)
 700 void FreezeBase::unwind_frames() {
 701   ContinuationEntry* entry = _cont.entry();
 702   entry->flush_stack_processing(_thread);
 703   set_anchor_to_entry(_thread, entry);
 704 }
 705 
 706 template <typename ConfigT>
 707 freeze_result Freeze<ConfigT>::try_freeze_fast() {
 708   assert(_thread->thread_state() == _thread_in_vm, "");
 709   assert(_thread->cont_fastpath(), "");
 710 
 711   DEBUG_ONLY(_fast_freeze_size = size_if_fast_freeze_available();)
 712   assert(_fast_freeze_size == 0, "");
 713 
 714   stackChunkOop chunk = allocate_chunk(cont_size() + frame::metadata_words + _monitors_in_lockstack);
 715   if (freeze_fast_new_chunk(chunk)) {
 716     return freeze_ok;
 717   }
 718   if (_thread->has_pending_exception()) {
 719     return freeze_exception;
 720   }
 721 
 722   // TODO R REMOVE when deopt change is fixed
 723   assert(!_thread->cont_fastpath() || _barriers, "");
 724   log_develop_trace(continuations)("-- RETRYING SLOW --");
 725   return freeze_slow();
 726 }
 727 
 728 // Returns size needed if the continuation fits, otherwise 0.
 729 int FreezeBase::size_if_fast_freeze_available() {
 730   stackChunkOop chunk = _cont.tail();
 731   if (chunk == nullptr || chunk->is_gc_mode() || chunk->requires_barriers() || chunk->has_mixed_frames()) {
 732     log_develop_trace(continuations)("chunk available %s", chunk == nullptr ? "no chunk" : "chunk requires barriers");
 733     return 0;
 734   }
 735 
 736   int total_size_needed = cont_size();
 737   const int chunk_sp = chunk->sp();
 738 
 739   // argsize can be nonzero if we have a caller, but the caller could be in a non-empty parent chunk,
 740   // so we subtract it only if we overlap with the caller, i.e. the current chunk isn't empty.
 741   // Consider leaving the chunk's argsize set when emptying it and removing the following branch,
 742   // although that would require changing stackChunkOopDesc::is_empty
 743   if (chunk_sp < chunk->stack_size()) {
 744     total_size_needed -= _cont.argsize() + frame::metadata_words_at_top;
 745   }
 746 
 747   total_size_needed += _monitors_in_lockstack;
 748 
 749   int chunk_free_room = chunk_sp - frame::metadata_words_at_bottom;
 750   bool available = chunk_free_room >= total_size_needed;
 751   log_develop_trace(continuations)("chunk available: %s size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT,
 752     available ? "yes" : "no" , total_size_needed, _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom));
 753   return available ? total_size_needed : 0;
 754 }
 755 
 756 void FreezeBase::freeze_fast_existing_chunk() {
 757   stackChunkOop chunk = _cont.tail();
 758   DEBUG_ONLY(_orig_chunk_sp = chunk->sp_address();)
 759 
 760   DEBUG_ONLY(_fast_freeze_size = size_if_fast_freeze_available();)
 761   assert(_fast_freeze_size > 0, "");
 762 
 763   if (chunk->sp() < chunk->stack_size()) { // we are copying into a non-empty chunk
 764     DEBUG_ONLY(_empty = false;)
 765     assert(chunk->sp() < (chunk->stack_size() - chunk->argsize()), "");
 766 #ifdef ASSERT
 767     {
 768       intptr_t* retaddr_slot = (chunk->sp_address()

 811     freeze_fast_copy(chunk, chunk_start_sp CONT_JFR_ONLY(COMMA false));
 812   }
 813 }
 814 
 815 bool FreezeBase::freeze_fast_new_chunk(stackChunkOop chunk) {
 816   DEBUG_ONLY(_empty = true;)
 817 
 818   // Install new chunk
 819   _cont.set_tail(chunk);
 820 
 821   if (UNLIKELY(chunk == nullptr || !_thread->cont_fastpath() || _barriers)) { // OOME/probably humongous
 822     log_develop_trace(continuations)("Retrying slow. Barriers: %d", _barriers);
 823     return false;
 824   }
 825 
 826   chunk->set_max_thawing_size(cont_size());
 827   chunk->set_argsize(_cont.argsize());
 828 
 829   // in a fresh chunk, we freeze *with* the bottom-most frame's stack arguments.
 830   // They'll then be stored twice: in the chunk and in the parent chunk's top frame
 831   const int chunk_start_sp = cont_size() + frame::metadata_words + _monitors_in_lockstack;
 832   assert(chunk_start_sp == chunk->stack_size(), "");
 833 
 834   DEBUG_ONLY(_orig_chunk_sp = chunk->start_address() + chunk_start_sp;)
 835 
 836   freeze_fast_copy(chunk, chunk_start_sp CONT_JFR_ONLY(COMMA true));
 837 
 838   return true;
 839 }
 840 
 841 void FreezeBase::freeze_fast_copy(stackChunkOop chunk, int chunk_start_sp CONT_JFR_ONLY(COMMA bool chunk_is_allocated)) {
 842   assert(chunk != nullptr, "");
 843   assert(!chunk->has_mixed_frames(), "");
 844   assert(!chunk->is_gc_mode(), "");
 845   assert(!chunk->has_bitmap(), "");
 846   assert(!chunk->requires_barriers(), "");
 847   assert(chunk == _cont.tail(), "");
 848 
 849   // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
 850   // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
 851   // will either see no continuation on the stack, or a consistent chunk.
 852   unwind_frames();
 853 
 854   log_develop_trace(continuations)("freeze_fast start: chunk " INTPTR_FORMAT " size: %d orig sp: %d argsize: %d",
 855     p2i((oopDesc*)chunk), chunk->stack_size(), chunk_start_sp, _cont.argsize());
 856   assert(chunk_start_sp <= chunk->stack_size(), "");
 857   assert(chunk_start_sp >= cont_size(), "no room in the chunk");
 858 
 859   const int chunk_new_sp = chunk_start_sp - cont_size(); // the chunk's new sp, after freeze
 860   assert(!(_fast_freeze_size > 0) || (_orig_chunk_sp - (chunk->start_address() + chunk_new_sp)) == (_fast_freeze_size - _monitors_in_lockstack), "");
 861 
 862   intptr_t* chunk_top = chunk->start_address() + chunk_new_sp;
 863 #ifdef ASSERT
 864   if (!_empty) {
 865     intptr_t* retaddr_slot = (_orig_chunk_sp
 866                               - frame::sender_sp_ret_address_offset());
 867     assert(ContinuationHelper::return_address_at(retaddr_slot) == chunk->pc(),
 868            "unexpected saved return address");
 869   }
 870 #endif
 871 
 872   log_develop_trace(continuations)("freeze_fast start: " INTPTR_FORMAT " sp: %d chunk_top: " INTPTR_FORMAT,
 873                               p2i(chunk->start_address()), chunk_new_sp, p2i(chunk_top));
 874   intptr_t* from = _cont_stack_top - frame::metadata_words_at_bottom;
 875   intptr_t* to   = chunk_top - frame::metadata_words_at_bottom;
 876   copy_to_chunk(from, to, cont_size() + frame::metadata_words_at_bottom);
 877   // Because we're not patched yet, the chunk is now in a bad state
 878 
 879   // patch return pc of the bottom-most frozen frame (now in the chunk)
 880   // with the actual caller's return address
 881   intptr_t* chunk_bottom_retaddr_slot = (chunk_top + cont_size()
 882                                          - _cont.argsize()
 883                                          - frame::metadata_words_at_top
 884                                          - frame::sender_sp_ret_address_offset());
 885 #ifdef ASSERT
 886   if (!_empty) {
 887     assert(ContinuationHelper::return_address_at(chunk_bottom_retaddr_slot)
 888            == StubRoutines::cont_returnBarrier(),
 889            "should be the continuation return barrier");
 890   }
 891 #endif
 892   ContinuationHelper::patch_return_address_at(chunk_bottom_retaddr_slot,
 893                                               chunk->pc());
 894 
 895   // We're always writing to a young chunk, so the GC can't see it until the next safepoint.
 896   chunk->set_sp(chunk_new_sp);
 897   // set chunk->pc to the return address of the topmost frame in the chunk
 898   chunk->set_pc(ContinuationHelper::return_address_at(
 899                   _cont_stack_top - frame::sender_sp_ret_address_offset()));
 900 
 901   // Fix monitors after unwinding frames to make sure oops are valid.
 902   if (_monitors_to_fix > 0) {
 903     fix_monitors_in_fast_path();
 904   }
 905 
 906   _cont.write();
 907 
 908   log_develop_trace(continuations)("FREEZE CHUNK #" INTPTR_FORMAT " (young)", _cont.hash());
 909   LogTarget(Trace, continuations) lt;
 910   if (lt.develop_is_enabled()) {
 911     LogStream ls(lt);
 912     chunk->print_on(true, &ls);
 913   }
 914 
 915   // Verification
 916   assert(_cont.chunk_invariant(), "");
 917   chunk->verify();
 918 
 919 #if CONT_JFR
 920   EventContinuationFreezeFast e;
 921   if (e.should_commit()) {
 922     e.set_id(cast_from_oop<u8>(chunk));
 923     DEBUG_ONLY(e.set_allocate(chunk_is_allocated);)
 924     e.set_size(cont_size() << LogBytesPerWord);
 925     e.commit();

 944 #endif
 945 
 946   init_rest();
 947 
 948   HandleMark hm(Thread::current());
 949 
 950   frame f = freeze_start_frame();
 951 
 952   LogTarget(Debug, continuations) lt;
 953   if (lt.develop_is_enabled()) {
 954     LogStream ls(lt);
 955     f.print_on(&ls);
 956   }
 957 
 958   frame caller; // the frozen caller in the chunk
 959   freeze_result res = recurse_freeze(f, caller, 0, false, true);
 960 
 961   if (res == freeze_ok) {
 962     finish_freeze(f, caller);
 963     _cont.write();
 964     assert(_monitors_to_fix == 0, "missing monitors in stack");
 965     assert(_thread->held_monitor_count() == 0, "should be 0 now");
 966   }
 967 
 968   return res;
 969 }
 970 
 971 frame FreezeBase::freeze_start_frame() {

 972   if (LIKELY(!_preempt)) {
 973     return freeze_start_frame_yield_stub();
 974   } else {
 975     return freeze_start_frame_on_preempt();
 976   }
 977 }
 978 
 979 frame FreezeBase::freeze_start_frame_yield_stub() {
 980   frame f = _thread->last_frame();
 981   assert(SharedRuntime::cont_doYield_stub()->contains(f.pc()), "must be");
 982   f = sender<ContinuationHelper::NonInterpretedUnknownFrame>(f);
 983   assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), f), "");
 984   return f;
 985 }
 986 
 987 frame FreezeBase::freeze_start_frame_on_preempt() {
 988   assert(_last_frame.sp() == _thread->last_frame().sp(), "_last_frame should be already initialized");
 989   assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), _last_frame), "");
 990   return _last_frame;












 991 }
 992 
 993 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
 994 NOINLINE freeze_result FreezeBase::recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top) {
 995   assert(f.unextended_sp() < _bottom_address, ""); // see recurse_freeze_java_frame
 996   assert(f.is_interpreted_frame() || ((top && _preempt) == ContinuationHelper::Frame::is_stub(f.cb()))
 997          || ((top && _preempt) == f.is_native_frame()), "");
 998 
 999   if (stack_overflow()) {
1000     return freeze_exception;
1001   }
1002 
1003   if (f.is_compiled_frame()) {
1004     if (UNLIKELY(f.oop_map() == nullptr)) {
1005       // special native frame
1006       return freeze_pinned_native;
1007     }
1008     return recurse_freeze_compiled_frame(f, caller, callee_argsize, callee_interpreted);
1009   } else if (f.is_interpreted_frame()) {
1010     assert(!f.interpreter_frame_method()->is_native() || (_preempt && top && _thread->is_on_monitorenter()), "");
1011     if (_preempt && top && f.interpreter_frame_method()->is_native()) {
1012       // TODO: Allow preemption for this case too
1013       return freeze_pinned_native;
1014     }

1015     return recurse_freeze_interpreted_frame(f, caller, callee_argsize, callee_interpreted);
1016   } else if (_preempt && top) {
1017     assert(ContinuationHelper::Frame::is_stub(f.cb()) || (f.is_native_frame() && _thread->is_on_monitorenter()), "invariant");
1018     if (f.is_native_frame()) {
1019       // TODO: Allow preemption for this case too
1020       return freeze_pinned_native;
1021     } else {
1022       return recurse_freeze_stub_frame(f, caller);
1023     }
1024   } else {
1025     return freeze_pinned_native;
1026   }
1027 }
1028 
1029 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
1030 // See also StackChunkFrameStream<frame_kind>::frame_size()
1031 template<typename FKind>
1032 inline freeze_result FreezeBase::recurse_freeze_java_frame(const frame& f, frame& caller, int fsize, int argsize) {
1033   assert(FKind::is_instance(f), "");
1034 
1035   assert(fsize > 0, "");
1036   assert(argsize >= 0, "");
1037   _freeze_size += fsize;
1038   NOT_PRODUCT(_frames++;)
1039 
1040   assert(FKind::frame_bottom(f) <= _bottom_address, "");
1041 
1042   // We don't use FKind::frame_bottom(f) == _bottom_address because on x64 there's sometimes an extra word between
1043   // enterSpecial and an interpreted frame

1064 
1065 inline void FreezeBase::after_freeze_java_frame(const frame& hf, bool is_bottom_frame) {
1066   LogTarget(Trace, continuations) lt;
1067   if (lt.develop_is_enabled()) {
1068     LogStream ls(lt);
1069     DEBUG_ONLY(hf.print_value_on(&ls, nullptr);)
1070     assert(hf.is_heap_frame(), "should be");
1071     DEBUG_ONLY(print_frame_layout(hf, false, &ls);)
1072     if (is_bottom_frame) {
1073       ls.print_cr("bottom h-frame:");
1074       hf.print_on(&ls);
1075     }
1076   }
1077 }
1078 
1079 // The parameter argsize_md includes metadata that has to be part of caller/callee overlap.
1080 // See also StackChunkFrameStream<frame_kind>::frame_size()
1081 freeze_result FreezeBase::finalize_freeze(const frame& callee, frame& caller, int argsize_md) {
1082   int argsize = argsize_md - frame::metadata_words_at_top;
1083   assert(callee.is_interpreted_frame()
1084     || ContinuationHelper::Frame::is_stub(callee.cb())
1085     || callee.cb()->as_nmethod()->is_osr_method()
1086     || argsize == _cont.argsize(), "argsize: %d cont.argsize: %d", argsize, _cont.argsize());
1087   log_develop_trace(continuations)("bottom: " INTPTR_FORMAT " count %d size: %d argsize: %d",
1088     p2i(_bottom_address), _frames, _freeze_size << LogBytesPerWord, argsize);
1089 
1090   LogTarget(Trace, continuations) lt;
1091 
1092 #ifdef ASSERT
1093   bool empty = _cont.is_empty();
1094   log_develop_trace(continuations)("empty: %d", empty);
1095 #endif
1096 
1097   stackChunkOop chunk = _cont.tail();
1098 
1099   assert(chunk == nullptr || (chunk->max_thawing_size() == 0) == chunk->is_empty(), "");
1100 
1101   _freeze_size += frame::metadata_words; // for top frame's metadata
1102 
1103   int overlap = 0; // the args overlap the caller -- if there is one in this chunk and is of the same kind
1104   int unextended_sp = -1;

1106     unextended_sp = chunk->sp();
1107     if (!chunk->is_empty()) {
1108       StackChunkFrameStream<ChunkFrames::Mixed> last(chunk);
1109       unextended_sp = chunk->to_offset(StackChunkFrameStream<ChunkFrames::Mixed>(chunk).unextended_sp());
1110       bool top_interpreted = Interpreter::contains(chunk->pc());
1111       if (callee.is_interpreted_frame() == top_interpreted) {
1112         overlap = argsize_md;
1113       }
1114     }
1115   }
1116 
1117   log_develop_trace(continuations)("finalize _size: %d overlap: %d unextended_sp: %d", _freeze_size, overlap, unextended_sp);
1118 
1119   _freeze_size -= overlap;
1120   assert(_freeze_size >= 0, "");
1121 
1122   assert(chunk == nullptr || chunk->is_empty()
1123           || unextended_sp == chunk->to_offset(StackChunkFrameStream<ChunkFrames::Mixed>(chunk).unextended_sp()), "");
1124   assert(chunk != nullptr || unextended_sp < _freeze_size, "");
1125 
1126   _freeze_size += _monitors_in_lockstack;
1127 
1128   // _barriers can be set to true by an allocation in freeze_fast, in which case the chunk is available
1129   bool allocated_old_in_freeze_fast = _barriers;
1130   assert(!allocated_old_in_freeze_fast || (unextended_sp >= _freeze_size && chunk->is_empty()),
1131     "Chunk allocated in freeze_fast is of insufficient size "
1132     "unextended_sp: %d size: %d is_empty: %d", unextended_sp, _freeze_size, chunk->is_empty());
1133   assert(!allocated_old_in_freeze_fast || (!UseZGC && !UseG1GC), "Unexpected allocation");
1134 
1135   DEBUG_ONLY(bool empty_chunk = true);
1136   if (unextended_sp < _freeze_size || chunk->is_gc_mode() || (!allocated_old_in_freeze_fast && chunk->requires_barriers())) {
1137     // ALLOCATE NEW CHUNK
1138 
1139     if (lt.develop_is_enabled()) {
1140       LogStream ls(lt);
1141       if (chunk == nullptr) {
1142         ls.print_cr("no chunk");
1143       } else {
1144         ls.print_cr("chunk barriers: %d _size: %d free size: %d",
1145           chunk->requires_barriers(), _freeze_size, chunk->sp() - frame::metadata_words);
1146         chunk->print_on(&ls);
1147       }

1166     // REUSE EXISTING CHUNK
1167     log_develop_trace(continuations)("Reusing chunk mixed: %d empty: %d", chunk->has_mixed_frames(), chunk->is_empty());
1168     if (chunk->is_empty()) {
1169       int sp = chunk->stack_size() - argsize_md;
1170       chunk->set_sp(sp);
1171       chunk->set_argsize(argsize);
1172       _freeze_size += overlap;
1173       assert(chunk->max_thawing_size() == 0, "");
1174     } DEBUG_ONLY(else empty_chunk = false;)
1175   }
1176   assert(!chunk->is_gc_mode(), "");
1177   assert(!chunk->has_bitmap(), "");
1178   chunk->set_has_mixed_frames(true);
1179 
1180   assert(chunk->requires_barriers() == _barriers, "");
1181   assert(!_barriers || is_empty(chunk), "");
1182 
1183   assert(!is_empty(chunk) || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).is_done(), "");
1184   assert(!is_empty(chunk) || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame().is_empty(), "");
1185 
1186   if (_preempt) {
1187     frame f = _thread->last_frame();
1188     if (f.is_interpreted_frame()) {
1189       // Do it now that we know freezing will be successful.
1190       prepare_freeze_interpreted_top_frame(f);
1191     }
1192   }
1193 
1194   // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
1195   // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
1196   // will either see no continuation or a consistent chunk.
1197   unwind_frames();
1198 
1199   chunk->set_max_thawing_size(chunk->max_thawing_size() + _freeze_size - _monitors_in_lockstack - frame::metadata_words);
1200 
1201   if (lt.develop_is_enabled()) {
1202     LogStream ls(lt);
1203     ls.print_cr("top chunk:");
1204     chunk->print_on(&ls);
1205   }
1206 
1207   // Fix monitors after unwinding frames to make sure oops are valid. Also do it now to avoid unnecessary
1208   // calls to fix_monitors_in_interpreted_frame/fix_monitors_in_compiled_frame() later.
1209   if (_monitors_to_fix > 0) {
1210     if (_monitors_in_lockstack > 0) {
1211       assert(chunk->sp_address() - chunk->start_address() >= _monitors_in_lockstack, "no room for lockstack");
1212       _thread->lock_stack().move_to_address((oop*)chunk->start_address());
1213 
1214       chunk->set_lockStackSize((uint8_t)_monitors_in_lockstack);
1215       chunk->set_has_lockStack(true);
1216 
1217       _monitors_to_fix -= _monitors_in_lockstack;
1218       assert(_monitors_to_fix == 0, "invariant");
1219     }
1220     _monitorenter_obj = _thread->is_on_monitorenter() ? ((ObjectMonitor*)(_thread->_Stalled))->object() : nullptr;
1221   }
1222 
1223   // The topmost existing frame in the chunk; or an empty frame if the chunk is empty
1224   caller = StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame();
1225 
1226   DEBUG_ONLY(_last_write = caller.unextended_sp() + (empty_chunk ? argsize_md : overlap);)
1227 
1228   assert(chunk->is_in_chunk(_last_write - _freeze_size),
1229     "last_write-size: " INTPTR_FORMAT " start: " INTPTR_FORMAT, p2i(_last_write-_freeze_size), p2i(chunk->start_address()));
1230 #ifdef ASSERT
1231   if (lt.develop_is_enabled()) {
1232     LogStream ls(lt);
1233     ls.print_cr("top hframe before (freeze):");
1234     assert(caller.is_heap_frame(), "should be");
1235     caller.print_on(&ls);
1236   }
1237 
1238   assert(!empty || Continuation::is_continuation_entry_frame(callee, nullptr), "");
1239 
1240   frame entry = sender(callee);
1241 
1242   assert((!empty && Continuation::is_return_barrier_entry(entry.pc())) || (empty && Continuation::is_continuation_enterSpecial(entry)), "");
1243   assert(callee.is_interpreted_frame() || entry.sp() == entry.unextended_sp(), "");
1244 #endif
1245 
1246   return freeze_ok_bottom;
1247 }
1248 
1249 void FreezeBase::patch(const frame& f, frame& hf, const frame& caller, bool is_bottom_frame) {
1250   if (is_bottom_frame) {
1251     // If we're the bottom frame, we need to replace the return barrier with the real
1252     // caller's pc.
1253     address last_pc = caller.pc();
1254     assert((last_pc == nullptr) == is_empty(_cont.tail()), "");
1255     ContinuationHelper::Frame::patch_pc(caller, last_pc);
1256   } else {
1257     assert(!caller.is_empty(), "");
1258   }
1259 
1260   patch_pd(hf, caller);
1261 
1262   if (f.is_interpreted_frame()) {

1289 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
1290 // See also StackChunkFrameStream<frame_kind>::frame_size()
1291 NOINLINE freeze_result FreezeBase::recurse_freeze_interpreted_frame(frame& f, frame& caller,
1292                                                                     int callee_argsize /* incl. metadata */,
1293                                                                     bool callee_interpreted) {
1294   adjust_interpreted_frame_unextended_sp(f);
1295 
1296   // The frame's top never includes the stack arguments to the callee
1297   intptr_t* const stack_frame_top = ContinuationHelper::InterpretedFrame::frame_top(f, callee_argsize, callee_interpreted);
1298   intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
1299   const int fsize = pointer_delta_as_int(stack_frame_bottom, stack_frame_top);
1300 
1301   DEBUG_ONLY(verify_frame_top(f, stack_frame_top));
1302 
1303   Method* frame_method = ContinuationHelper::Frame::frame_method(f);
1304   // including metadata between f and its args
1305   const int argsize = ContinuationHelper::InterpretedFrame::stack_argsize(f) + frame::metadata_words_at_top;
1306 
1307   log_develop_trace(continuations)("recurse_freeze_interpreted_frame %s _size: %d fsize: %d argsize: %d",
1308     frame_method->name_and_sig_as_C_string(), _freeze_size, fsize, argsize);
1309   // we'd rather not yield inside methods annotated with @JvmtiMountTransition. In the preempt case
1310   // we already checked it is safe to do so in Continuation::is_safe_vthread_to_preempt().
1311   assert(!ContinuationHelper::Frame::frame_method(f)->jvmti_mount_transition() || _preempt, "");
1312 
1313   freeze_result result = recurse_freeze_java_frame<ContinuationHelper::InterpretedFrame>(f, caller, fsize, argsize);
1314   if (UNLIKELY(result > freeze_ok_bottom)) {
1315     return result;
1316   }
1317 
1318   bool is_bottom_frame = result == freeze_ok_bottom;
1319   assert(!caller.is_empty() || is_bottom_frame, "");
1320 
1321   DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, 0, is_bottom_frame);)
1322 
1323   frame hf = new_heap_frame<ContinuationHelper::InterpretedFrame>(f, caller);
1324   _total_align_size += frame::align_wiggle; // add alignment room for internal interpreted frame alignment on AArch64/PPC64
1325 
1326   intptr_t* heap_frame_top = ContinuationHelper::InterpretedFrame::frame_top(hf, callee_argsize, callee_interpreted);
1327   intptr_t* heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
1328   assert(heap_frame_bottom == heap_frame_top + fsize, "");
1329 
1330   // Some architectures (like AArch64/PPC64/RISC-V) add padding between the locals and the fixed_frame to keep the fp 16-byte-aligned.
1331   // On those architectures we freeze the padding in order to keep the same fp-relative offsets in the fixed_frame.
1332   copy_to_chunk(stack_frame_top, heap_frame_top, fsize);
1333   assert(!is_bottom_frame || !caller.is_interpreted_frame() || (heap_frame_top + fsize) == (caller.unextended_sp() + argsize), "");
1334 
1335   relativize_interpreted_frame_metadata(f, hf);
1336 
1337   patch(f, hf, caller, is_bottom_frame);
1338 
1339   CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();)
1340   DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);)
1341   caller = hf;
1342 
1343   // Mark frame_method's GC epoch for class redefinition on_stack calculation.
1344   frame_method->record_gc_epoch();
1345 
1346   if (_monitors_to_fix > 0) {
1347     // Check if we have monitors in this frame
1348     fix_monitors_in_interpreted_frame(f);
1349   }
1350 
1351   return freeze_ok;
1352 }
1353 
1354 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
1355 // See also StackChunkFrameStream<frame_kind>::frame_size()
1356 freeze_result FreezeBase::recurse_freeze_compiled_frame(frame& f, frame& caller,
1357                                                         int callee_argsize /* incl. metadata */,
1358                                                         bool callee_interpreted) {
1359   // The frame's top never includes the stack arguments to the callee
1360   intptr_t* const stack_frame_top = ContinuationHelper::CompiledFrame::frame_top(f, callee_argsize, callee_interpreted);
1361   intptr_t* const stack_frame_bottom = ContinuationHelper::CompiledFrame::frame_bottom(f);
1362   // including metadata between f and its stackargs
1363   const int argsize = ContinuationHelper::CompiledFrame::stack_argsize(f) + frame::metadata_words_at_top;
1364   const int fsize = pointer_delta_as_int(stack_frame_bottom + argsize, stack_frame_top);
1365 
1366   log_develop_trace(continuations)("recurse_freeze_compiled_frame %s _size: %d fsize: %d argsize: %d",
1367                              ContinuationHelper::Frame::frame_method(f) != nullptr ?
1368                              ContinuationHelper::Frame::frame_method(f)->name_and_sig_as_C_string() : "",
1369                              _freeze_size, fsize, argsize);
1370   // we'd rather not yield inside methods annotated with @JvmtiMountTransition. In the preempt case
1371   // we already checked it is safe to do so in Continuation::is_safe_vthread_to_preempt().
1372   assert(!ContinuationHelper::Frame::frame_method(f)->jvmti_mount_transition() || _preempt, "");
1373 
1374   freeze_result result = recurse_freeze_java_frame<ContinuationHelper::CompiledFrame>(f, caller, fsize, argsize);
1375   if (UNLIKELY(result > freeze_ok_bottom)) {
1376     return result;
1377   }
1378 
1379   bool is_bottom_frame = result == freeze_ok_bottom;
1380   assert(!caller.is_empty() || is_bottom_frame, "");
1381 
1382   DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, argsize, is_bottom_frame);)
1383 
1384   frame hf = new_heap_frame<ContinuationHelper::CompiledFrame>(f, caller);
1385 
1386   intptr_t* heap_frame_top = ContinuationHelper::CompiledFrame::frame_top(hf, callee_argsize, callee_interpreted);
1387 
1388   copy_to_chunk(stack_frame_top, heap_frame_top, fsize);
1389   assert(!is_bottom_frame || !caller.is_compiled_frame() || (heap_frame_top + fsize) == (caller.unextended_sp() + argsize), "");
1390 
1391   if (caller.is_interpreted_frame()) {
1392     _total_align_size += frame::align_wiggle; // See Thaw::align
1393   }
1394 
1395   patch(f, hf, caller, is_bottom_frame);
1396 
1397   assert(is_bottom_frame || Interpreter::contains(ContinuationHelper::CompiledFrame::real_pc(caller)) == caller.is_interpreted_frame(), "");
1398 
1399   DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);)
1400   caller = hf;
1401 
1402   if (_monitors_to_fix > 0) {
1403     // Check if we have monitors in this frame
1404     fix_monitors_in_compiled_frame(f, SmallRegisterMap::instance);
1405   }
1406 
1407   return freeze_ok;
1408 }
1409 
1410 NOINLINE freeze_result FreezeBase::recurse_freeze_stub_frame(frame& f, frame& caller) {
1411   DEBUG_ONLY(frame fsender = sender(f);)
1412   assert(!fsender.is_native_frame() || (Continuation::is_continuation_enterSpecial(fsender) && !_cont.is_empty()), "sender should't be native except for enterSpecial case");
1413 
1414   intptr_t* const stack_frame_top = ContinuationHelper::StubFrame::frame_top(f, 0, 0);
1415   const int fsize = f.cb()->frame_size();
1416 
1417   log_develop_trace(continuations)("recurse_freeze_stub_frame %s _size: %d fsize: %d :: " INTPTR_FORMAT " - " INTPTR_FORMAT,
1418     f.cb()->name(), _freeze_size, fsize, p2i(stack_frame_top), p2i(stack_frame_top+fsize));
1419 
1420   freeze_result result = recurse_freeze_java_frame<ContinuationHelper::StubFrame>(f, caller, fsize, 0);




















1421   if (UNLIKELY(result > freeze_ok_bottom)) {
1422     return result;
1423   }


1424 
1425   bool is_bottom_frame = result == freeze_ok_bottom;
1426   assert(!caller.is_empty() || (is_bottom_frame && !_cont.is_empty()), "");
1427 
1428   DEBUG_ONLY(before_freeze_java_frame(f, caller, fsize, 0, is_bottom_frame);)
1429 
1430   frame hf = new_heap_frame<ContinuationHelper::StubFrame>(f, caller);
1431   intptr_t* heap_frame_top = ContinuationHelper::StubFrame::frame_top(hf, 0, 0);
1432 
1433   copy_to_chunk(stack_frame_top, heap_frame_top, fsize);
1434 
1435   if (caller.is_interpreted_frame()) {
1436     _total_align_size += frame::align_wiggle;
1437   }
1438 
1439   patch(f, hf, caller, is_bottom_frame);
1440 
1441   DEBUG_ONLY(after_freeze_java_frame(hf, is_bottom_frame);)
1442 
1443   caller = hf;
1444   return freeze_ok;
1445 }
1446 
1447 NOINLINE void FreezeBase::finish_freeze(const frame& f, const frame& top) {
1448   stackChunkOop chunk = _cont.tail();
1449   assert(chunk->to_offset(top.sp()) <= chunk->sp(), "");
1450 
1451   LogTarget(Trace, continuations) lt;
1452   if (lt.develop_is_enabled()) {
1453     LogStream ls(lt);
1454     assert(top.is_heap_frame(), "should be");
1455     top.print_on(&ls);
1456   }
1457 
1458   set_top_frame_metadata_pd(top);
1459 
1460   chunk->set_sp(chunk->to_offset(top.sp()));
1461   chunk->set_pc(top.pc());
1462 
1463   chunk->set_max_thawing_size(chunk->max_thawing_size() + _total_align_size);
1464 
1465   assert(chunk->sp_address() - chunk->start_address() >= _monitors_in_lockstack, "clash with lockstack");
1466 
1467   // At this point the chunk is consistent
1468 
1469   if (UNLIKELY(_barriers)) {
1470     log_develop_trace(continuations)("do barriers on old chunk");
1471     // Serial and Parallel GC can allocate objects directly into the old generation.
1472     // Then we want to relativize the derived pointers eagerly so that
1473     // old chunks are all in GC mode.
1474     assert(!UseG1GC, "G1 can not deal with allocating outside of eden");
1475     assert(!UseZGC, "ZGC can not deal with allocating chunks visible to marking");
1476     if (UseShenandoahGC) {
1477       _cont.tail()->relativize_derived_pointers_concurrently();
1478     } else {
1479       ContinuationGCSupport::transform_stack_chunk(_cont.tail());
1480     }
1481     // For objects in the old generation we must maintain the remembered set
1482     _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>();
1483   }
1484 
1485   log_develop_trace(continuations)("finish_freeze: has_mixed_frames: %d", chunk->has_mixed_frames());
1486   if (lt.develop_is_enabled()) {
1487     LogStream ls(lt);
1488     chunk->print_on(true, &ls);
1489   }
1490 
1491   if (lt.develop_is_enabled()) {
1492     LogStream ls(lt);
1493     ls.print_cr("top hframe after (freeze):");
1494     assert(_cont.last_frame().is_heap_frame(), "should be");
1495     _cont.last_frame().print_on(&ls);
1496     DEBUG_ONLY(print_frame_layout(top, false, &ls);)
1497   }
1498 
1499   assert(_cont.chunk_invariant(), "");
1500 }
1501 
1502 inline bool FreezeBase::stack_overflow() { // detect stack overflow in recursive native code
1503   JavaThread* t = !_preempt ? _thread : JavaThread::current();
1504   assert(t == JavaThread::current(), "");
1505   if (os::current_stack_pointer() < t->stack_overflow_state()->shadow_zone_safe_limit()) {
1506     if (!_preempt) {
1507       ContinuationWrapper::SafepointOp so(t, _cont); // could also call _cont.done() instead
1508       Exceptions::_throw_msg(t, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Stack overflow while freezing");
1509     }
1510     return true;
1511   }
1512   return false;
1513 }
1514 
1515 class StackChunkAllocator : public MemAllocator {
1516   const size_t                                 _stack_size;
1517   ContinuationWrapper&                         _continuation_wrapper;
1518   JvmtiSampledObjectAllocEventCollector* const _jvmti_event_collector;
1519   JavaThread* const                            _target;
1520   mutable bool                                 _took_slow_path;
1521 
1522   // Does the minimal amount of initialization needed for a TLAB allocation.
1523   // We don't need to do a full initialization, as such an allocation need not be immediately walkable.
1524   virtual oop initialize(HeapWord* mem) const override {
1525     assert(_stack_size > 0, "");
1526     assert(_stack_size <= max_jint, "");
1527     assert(_word_size > _stack_size, "");
1528 
1529     // zero out fields (but not the stack)
1530     const size_t hs = oopDesc::header_size();
1531     oopDesc::set_klass_gap(mem, 0);
1532     Copy::fill_to_aligned_words(mem + hs, vmClasses::StackChunk_klass()->size_helper() - hs);
1533 
1534     jdk_internal_vm_StackChunk::set_size(mem, (int)_stack_size);
1535     jdk_internal_vm_StackChunk::set_sp(mem, (int)_stack_size);
1536 
1537     return finish(mem);
1538   }
1539 
1540   stackChunkOop allocate_fast() const {
1541     if (!UseTLAB) {
1542       return nullptr;
1543     }
1544 
1545     HeapWord* const mem = MemAllocator::mem_allocate_inside_tlab_fast();
1546     if (mem == nullptr) {
1547       return nullptr;
1548     }
1549 
1550     oop obj = initialize(mem);
1551     return stackChunkOopDesc::cast(obj);
1552   }
1553 
1554   bool is_preempt() const { return _thread != _target; }
1555 
1556 public:
1557   StackChunkAllocator(Klass* klass,
1558                       size_t word_size,
1559                       Thread* thread,
1560                       size_t stack_size,
1561                       ContinuationWrapper& continuation_wrapper,
1562                       JvmtiSampledObjectAllocEventCollector* jvmti_event_collector,
1563                       JavaThread* target)
1564     : MemAllocator(klass, word_size, thread),
1565       _stack_size(stack_size),
1566       _continuation_wrapper(continuation_wrapper),
1567       _jvmti_event_collector(jvmti_event_collector),
1568       _target(target),
1569       _took_slow_path(false) {}
1570 
1571   // Provides it's own, specialized allocation which skips instrumentation
1572   // if the memory can be allocated without going to a slow-path.
1573   stackChunkOop allocate() const {
1574     // First try to allocate without any slow-paths or instrumentation.
1575     stackChunkOop obj = allocate_fast();
1576     if (obj != nullptr) {
1577       return obj;
1578     }
1579 
1580     // Now try full-blown allocation with all expensive operations,
1581     // including potentially safepoint operations.
1582     _took_slow_path = true;
1583 
1584     // Protect unhandled Loom oops
1585     ContinuationWrapper::SafepointOp so(_thread, _continuation_wrapper);
1586 
1587     // Can safepoint
1588     _jvmti_event_collector->start();

1602 
1603   InstanceStackChunkKlass* klass = InstanceStackChunkKlass::cast(vmClasses::StackChunk_klass());
1604   size_t size_in_words = klass->instance_size(stack_size);
1605 
1606   if (CollectedHeap::stack_chunk_max_size() > 0 && size_in_words >= CollectedHeap::stack_chunk_max_size()) {
1607     if (!_preempt) {
1608       throw_stack_overflow_on_humongous_chunk();
1609     }
1610     return nullptr;
1611   }
1612 
1613   JavaThread* current = _preempt ? JavaThread::current() : _thread;
1614   assert(current == JavaThread::current(), "should be current");
1615 
1616   // Allocate the chunk.
1617   //
1618   // This might safepoint while allocating, but all safepointing due to
1619   // instrumentation have been deferred. This property is important for
1620   // some GCs, as this ensures that the allocated object is in the young
1621   // generation / newly allocated memory.
1622   StackChunkAllocator allocator(klass, size_in_words, current, stack_size, _cont, _jvmti_event_collector, _thread);
1623   stackChunkOop chunk = allocator.allocate();
1624 
1625   if (chunk == nullptr) {
1626     return nullptr; // OOME
1627   }
1628 
1629   // assert that chunk is properly initialized
1630   assert(chunk->stack_size() == (int)stack_size, "");
1631   assert(chunk->size() >= stack_size, "chunk->size(): %zu size: %zu", chunk->size(), stack_size);
1632   assert(chunk->sp() == chunk->stack_size(), "");
1633   assert((intptr_t)chunk->start_address() % 8 == 0, "");
1634   assert(chunk->max_thawing_size() == 0, "");
1635   assert(chunk->pc() == nullptr, "");
1636   assert(chunk->argsize() == 0, "");
1637   assert(chunk->flags() == 0, "");
1638   assert(chunk->is_gc_mode() == false, "");
1639   assert(chunk->lockStackSize() == 0, "");
1640   assert(chunk->objectMonitor() == nullptr, "");
1641 
1642   // fields are uninitialized
1643   chunk->set_parent_access<IS_DEST_UNINITIALIZED>(_cont.last_nonempty_chunk());
1644   chunk->set_cont_access<IS_DEST_UNINITIALIZED>(_cont.continuation());
1645 
1646 #if INCLUDE_ZGC
1647   if (UseZGC) {
1648     if (ZGenerational) {
1649       ZStackChunkGCData::initialize(chunk);
1650     }
1651     assert(!chunk->requires_barriers(), "ZGC always allocates in the young generation");
1652     _barriers = false;
1653   } else
1654 #endif
1655 #if INCLUDE_SHENANDOAHGC
1656   if (UseShenandoahGC) {
1657     _barriers = chunk->requires_barriers();
1658   } else
1659 #endif
1660   {

1693   return count;
1694 }
1695 
1696 static void invalidate_jvmti_stack(JavaThread* thread) {
1697   if (thread->is_interp_only_mode()) {
1698     JvmtiThreadState *state = thread->jvmti_thread_state();
1699     if (state != nullptr)
1700       state->invalidate_cur_stack_depth();
1701   }
1702 }
1703 
1704 static void jvmti_yield_cleanup(JavaThread* thread, ContinuationWrapper& cont) {
1705   if (JvmtiExport::can_post_frame_pop()) {
1706     int num_frames = num_java_frames(cont);
1707 
1708     ContinuationWrapper::SafepointOp so(Thread::current(), cont);
1709     JvmtiExport::continuation_yield_cleanup(JavaThread::current(), num_frames);
1710   }
1711   invalidate_jvmti_stack(thread);
1712 }

1713 
1714 static void jvmti_mount_end(JavaThread* current, ContinuationWrapper& cont, frame top, ObjectMonitor* mon, bool post_mount_event) {
1715   assert(current->vthread() != nullptr, "must be");
1716 
1717   HandleMarkCleaner hm(current);
1718   Handle vth(current, current->vthread());
1719 
1720   ContinuationWrapper::SafepointOp so(current, cont);
1721 
1722   // Since we might safepoint set the anchor so that the stack can we walked.
1723   set_anchor(current, top.sp());
1724 
1725   JRT_BLOCK
1726     current->rebind_to_jvmti_thread_state_of(vth());
1727     {
1728       MutexLocker mu(JvmtiThreadState_lock);
1729       JvmtiThreadState* state = current->jvmti_thread_state();
1730       if (state != NULL && state->is_pending_interp_only_mode()) {
1731         JvmtiEventController::enter_interp_only_mode();
1732       }
1733     }
1734     assert(current->is_in_VTMS_transition(), "sanity check");
1735     assert(!current->is_in_tmp_VTMS_transition(), "sanity check");
1736     JvmtiVTMSTransitionDisabler::finish_VTMS_transition((jthread)vth.raw_value(), /* is_mount */ true);
1737 
1738     if (post_mount_event && JvmtiExport::should_post_vthread_mount()) {
1739       JvmtiExport::post_vthread_mount((jthread)vth.raw_value());
1740     } else if (!post_mount_event) {
1741       // Preemption cancelled case. Clear the unmount event pending flag. The
1742       // flag might actually not be set if _VTMS_notify_jvmti_events was enabled
1743       // after preemption happened (late binding agents). But since this would
1744       // be a rare case we just do it unconditionally.
1745       current->set_jvmti_unmount_event_pending(false);
1746     }
1747 
1748     if (mon != nullptr) {
1749       JvmtiExport::post_monitor_contended_entered(current, mon);
1750     }
1751   JRT_BLOCK_END
1752 
1753   clear_anchor(current);
1754 }
1755 #endif // INCLUDE_JVMTI
1756 
1757 #ifdef ASSERT
1758 
1759 // There are no interpreted frames if we're not called from the interpreter and we haven't ancountered an i2c
1760 // adapter or called Deoptimization::unpack_frames. As for native frames, upcalls from JNI also go through the
1761 // interpreter (see JavaCalls::call_helper), while the UpcallLinker explicitly sets cont_fastpath.
1762 bool FreezeBase::check_valid_fast_path() {
1763   ContinuationEntry* ce = _thread->last_continuation();
1764   RegisterMap map(_thread,
1765                   RegisterMap::UpdateMap::skip,
1766                   RegisterMap::ProcessFrames::skip,
1767                   RegisterMap::WalkContinuation::skip);
1768   map.set_include_argument_oops(false);
1769   int i = 0;
1770   for (frame f = freeze_start_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map), i++) {
1771     if (!((f.is_compiled_frame() && !f.is_deoptimized_frame()) || (i == 0 && f.is_runtime_frame()))) {
1772       return false;
1773     }
1774   }
1775   return true;
1776 }
1777 #endif // ASSERT
1778 
1779 static inline int freeze_epilog(ContinuationWrapper& cont) {
1780   verify_continuation(cont.continuation());
1781   assert(!cont.is_empty(), "");


1782 
1783   log_develop_debug(continuations)("=== End of freeze cont ### #" INTPTR_FORMAT, cont.hash());

1784   return 0;
1785 }
1786 
1787 static int freeze_epilog(JavaThread* thread, ContinuationWrapper& cont, freeze_result res) {
1788   if (UNLIKELY(res != freeze_ok)) {
1789     verify_continuation(cont.continuation());
1790     log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1791     return res;
1792   }
1793 
1794   JVMTI_ONLY(jvmti_yield_cleanup(thread, cont)); // can safepoint
1795   return freeze_epilog(cont);
1796 }
1797 
1798 static int preempt_epilog(ContinuationWrapper& cont, freeze_result res, frame& old_last_frame) {
1799   if (UNLIKELY(res != freeze_ok)) {
1800     verify_continuation(cont.continuation());
1801     log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1802     return res;
1803   }
1804 
1805   patch_return_pc_with_preempt_stub(old_last_frame);
1806   cont.set_preempted(true);
1807   cont.tail()->set_is_preempted(true);
1808 
1809   if (cont.thread()->is_on_monitorenter()) {
1810     cont.tail()->set_objectMonitor((ObjectMonitor*)cont.thread()->_Stalled);
1811   }
1812 
1813   return freeze_epilog(cont);
1814 }
1815 
1816 template<typename ConfigT, bool preempt>
1817 static inline int freeze_internal(JavaThread* current, intptr_t* const sp) {
1818   assert(!current->has_pending_exception(), "");
1819 
1820 #ifdef ASSERT
1821   log_trace(continuations)("~~~~ freeze sp: " INTPTR_FORMAT "JavaThread: " INTPTR_FORMAT, p2i(current->last_continuation()->entry_sp()), p2i(current));
1822   log_frames(current);
1823 #endif
1824 
1825   CONT_JFR_ONLY(EventContinuationFreeze event;)
1826 
1827   ContinuationEntry* entry = current->last_continuation();
1828 
1829   oop oopCont = entry->cont_oop(current);
1830   assert(oopCont == current->last_continuation()->cont_oop(current), "");
1831   assert(ContinuationEntry::assert_entry_frame_laid_out(current), "");
1832 
1833   verify_continuation(oopCont);
1834   ContinuationWrapper cont(current, oopCont);
1835   log_develop_debug(continuations)("FREEZE #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
1836 
1837   assert(entry->is_virtual_thread() == (entry->scope(current) == java_lang_VirtualThread::vthread_scope()), "");
1838 
1839   // assert(monitors_on_stack(current) == ((current->held_monitor_count() - current->jni_monitor_count()) > 0),
1840   //       "Held monitor count and locks on stack invariant: " INT64_FORMAT " JNI: " INT64_FORMAT, (int64_t)current->held_monitor_count(), (int64_t)current->jni_monitor_count());
1841 
1842   const bool pinned_monitor = NOT_LOOM_MONITOR_SUPPORT(current->held_monitor_count() > 0) LOOM_MONITOR_SUPPORT_ONLY(current->jni_monitor_count() > 0);
1843   if (entry->is_pinned() || pinned_monitor) {
1844     log_develop_debug(continuations)("PINNED due to critical section/hold monitor");
1845     verify_continuation(cont.continuation());
1846     freeze_result res = entry->is_pinned() ? freeze_pinned_cs : freeze_pinned_monitor;
1847     log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1848     return res;
1849   }
1850 
1851   Freeze<ConfigT> freeze(current, cont, sp, preempt);
1852 
1853   if (preempt) {
1854     freeze.set_last_frame();  // remember last frame
1855     frame last_frame = freeze.last_frame();
1856 #ifdef AARCH64
1857     JvmtiSampledObjectAllocEventCollector jsoaec(false);
1858     freeze.set_jvmti_event_collector(&jsoaec);
1859 
1860     // Force aarch64 to slow path always for now. It needs extra instructions to correctly set
1861     // the last pc we copy into the stackChunk, since it will not necessarily be at sp[-1]).
1862     freeze_result res = freeze.freeze_slow();
1863     CONT_JFR_ONLY(cont.post_jfr_event(&event, oopCont, current);)
1864     return preempt_epilog(cont, res, freeze.last_frame());
1865 #endif
1866   }
1867 
1868   assert(!current->cont_fastpath() || freeze.check_valid_fast_path(), "");
1869   bool fast = UseContinuationFastPath && current->cont_fastpath();
1870   if (fast && freeze.size_if_fast_freeze_available() > 0) {
1871     freeze.freeze_fast_existing_chunk();
1872     CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);)
1873     return !preempt ? freeze_epilog(cont) : preempt_epilog(cont, freeze_ok, freeze.last_frame());
1874   }
1875 
1876   if (preempt) {
1877     JvmtiSampledObjectAllocEventCollector jsoaec(false);
1878     freeze.set_jvmti_event_collector(&jsoaec);
1879 
1880     freeze_result res = fast ? freeze.try_freeze_fast() : freeze.freeze_slow();
1881 
1882     CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);)
1883     preempt_epilog(cont, res, freeze.last_frame());
1884     return res;
1885   }
1886 
1887   log_develop_trace(continuations)("chunk unavailable; transitioning to VM");
1888   assert(current == JavaThread::current(), "must be current thread");
1889   JRT_BLOCK
1890     // delays a possible JvmtiSampledObjectAllocEventCollector in alloc_chunk
1891     JvmtiSampledObjectAllocEventCollector jsoaec(false);
1892     freeze.set_jvmti_event_collector(&jsoaec);
1893 
1894     freeze_result res = fast ? freeze.try_freeze_fast() : freeze.freeze_slow();
1895 
1896     CONT_JFR_ONLY(freeze.jfr_info().post_jfr_event(&event, oopCont, current);)
1897     freeze_epilog(current, cont, res);
1898     cont.done(); // allow safepoint in the transition back to Java
1899     return res;
1900   JRT_BLOCK_END
1901 }
1902 
1903 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint) {
1904   ContinuationEntry* entry = thread->last_continuation();
1905   if (entry == nullptr) {
1906     return freeze_ok;
1907   }
1908   if (entry->is_pinned()) {

1948       entry = entry->parent();
1949       if (entry == nullptr) {
1950         break;
1951       }
1952       if (entry->is_pinned()) {
1953         return freeze_pinned_cs;
1954       } else if (monitor_count > 0) {
1955         return freeze_pinned_monitor;
1956       }
1957     }
1958   }
1959   return freeze_ok;
1960 }
1961 
1962 /////////////// THAW ////
1963 
1964 static int thaw_size(stackChunkOop chunk) {
1965   int size = chunk->max_thawing_size();
1966   size += frame::metadata_words; // For the top pc+fp in push_return_frame or top = stack_sp - frame::metadata_words in thaw_fast
1967   size += 2*frame::align_wiggle; // in case of alignments at the top and bottom
1968   size += frame::metadata_words; // for preemption case (see push_preempt_rerun_adapter)
1969   return size;
1970 }
1971 
1972 // make room on the stack for thaw
1973 // returns the size in bytes, or 0 on failure
1974 static inline int prepare_thaw_internal(JavaThread* thread, bool return_barrier) {
1975   log_develop_trace(continuations)("~~~~ prepare_thaw return_barrier: %d", return_barrier);
1976 
1977   assert(thread == JavaThread::current(), "");
1978 
1979   ContinuationEntry* ce = thread->last_continuation();
1980   assert(ce != nullptr, "");
1981   oop continuation = ce->cont_oop(thread);
1982   assert(continuation == get_continuation(thread), "");
1983   verify_continuation(continuation);
1984 
1985   stackChunkOop chunk = jdk_internal_vm_Continuation::tail(continuation);
1986   assert(chunk != nullptr, "");
1987 
1988   // The tail can be empty because it might still be available for another freeze.

2029 
2030   NOT_PRODUCT(int _frames;)
2031 
2032 protected:
2033   ThawBase(JavaThread* thread, ContinuationWrapper& cont) :
2034       _thread(thread), _cont(cont),
2035       _fastpath(nullptr) {
2036     DEBUG_ONLY(_top_unextended_sp_before_thaw = nullptr;)
2037     assert (cont.tail() != nullptr, "no last chunk");
2038     DEBUG_ONLY(_top_stack_address = _cont.entrySP() - thaw_size(cont.tail());)
2039   }
2040 
2041   void clear_chunk(stackChunkOop chunk);
2042   int remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize);
2043   void copy_from_chunk(intptr_t* from, intptr_t* to, int size);
2044 
2045   // fast path
2046   inline void prefetch_chunk_pd(void* start, int size_words);
2047   void patch_return(intptr_t* sp, bool is_last);
2048 
2049   intptr_t* handle_preempted_continuation(stackChunkOop original_chunk, intptr_t* sp, bool fast_case);
2050   inline intptr_t* push_preempt_rerun_adapter(frame top, bool is_interpreted_frame);
2051   inline intptr_t* push_preempt_monitorenter_redo(stackChunkOop chunk);
2052 

2053   void recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top);
2054   void finish_thaw(frame& f);
2055 
2056 private:
2057   template<typename FKind> bool recurse_thaw_java_frame(frame& caller, int num_frames);
2058   void finalize_thaw(frame& entry, int argsize);
2059 
2060   inline bool seen_by_gc();
2061 
2062   inline void before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame);
2063   inline void after_thaw_java_frame(const frame& f, bool bottom);
2064   inline void patch(frame& f, const frame& caller, bool bottom);
2065   void clear_bitmap_bits(address start, address end);
2066 
2067   NOINLINE void recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames);
2068   void recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller);
2069   void recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames);

2070 
2071   void push_return_frame(frame& f);
2072   inline frame new_entry_frame();
2073   template<typename FKind> frame new_stack_frame(const frame& hf, frame& caller, bool bottom);
2074   inline void patch_pd(frame& f, const frame& sender);
2075   inline void patch_pd(frame& f, intptr_t* caller_sp);
2076   inline intptr_t* align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom);
2077 
2078   void maybe_set_fastpath(intptr_t* sp) { if (sp > _fastpath) _fastpath = sp; }
2079 
2080   static inline void derelativize_interpreted_frame_metadata(const frame& hf, const frame& f);
2081 
2082  public:
2083   CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
2084 };
2085 
2086 template <typename ConfigT>
2087 class Thaw : public ThawBase {
2088 public:
2089   Thaw(JavaThread* thread, ContinuationWrapper& cont) : ThawBase(thread, cont) {}
2090 
2091   inline bool can_thaw_fast(stackChunkOop chunk) {
2092     return    !_barriers
2093            &&  _thread->cont_fastpath_thread_state()
2094            && !chunk->has_thaw_slowpath_condition()
2095            && !PreserveFramePointer;
2096   }
2097 
2098   inline intptr_t* thaw(Continuation::thaw_kind kind);
2099   NOINLINE intptr_t* thaw_fast(stackChunkOop chunk);
2100   NOINLINE intptr_t* thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind);
2101   inline void patch_caller_links(intptr_t* sp, intptr_t* bottom);
2102 };
2103 
2104 template <typename ConfigT>
2105 inline intptr_t* Thaw<ConfigT>::thaw(Continuation::thaw_kind kind) {
2106   verify_continuation(_cont.continuation());
2107   assert(!jdk_internal_vm_Continuation::done(_cont.continuation()), "");
2108   assert(!_cont.is_empty(), "");
2109 
2110   stackChunkOop chunk = _cont.tail();
2111   assert(chunk != nullptr, "guaranteed by prepare_thaw");
2112   assert(!chunk->is_empty(), "guaranteed by prepare_thaw");
2113 
2114   _barriers = chunk->requires_barriers();
2115   return (LIKELY(can_thaw_fast(chunk))) ? thaw_fast(chunk)
2116                                         : thaw_slow(chunk, kind);
2117 }
2118 
2119 class ReconstructedStack : public StackObj {
2120   intptr_t* _base;  // _cont.entrySP(); // top of the entry frame
2121   int _thaw_size;
2122   int _argsize;
2123 public:
2124   ReconstructedStack(intptr_t* base, int thaw_size, int argsize)
2125   : _base(base), _thaw_size(thaw_size - (argsize == 0 ? frame::metadata_words_at_top : 0)), _argsize(argsize) {
2126     // The only possible source of misalignment is stack-passed arguments b/c compiled frames are 16-byte aligned.
2127     assert(argsize != 0 || (_base - _thaw_size) == ContinuationHelper::frame_align_pointer(_base - _thaw_size), "");
2128     // We're at most one alignment word away from entrySP
2129     assert(_base - 1 <= top() + total_size() + frame::metadata_words_at_bottom, "missed entry frame");
2130   }
2131 
2132   int entry_frame_extension() const { return _argsize + (_argsize > 0 ? frame::metadata_words_at_top : 0); }
2133 
2134   // top and bottom stack pointers
2135   intptr_t* sp() const { return ContinuationHelper::frame_align_pointer(_base - _thaw_size); }
2136   intptr_t* bottom_sp() const { return ContinuationHelper::frame_align_pointer(_base - entry_frame_extension()); }
2137 
2138   // several operations operate on the totality of the stack being reconstructed,
2139   // including the metadata words
2140   intptr_t* top() const { return sp() - frame::metadata_words_at_bottom;  }
2141   int total_size() const { return _thaw_size + frame::metadata_words_at_bottom; }
2142 };
2143 
2144 inline void ThawBase::clear_chunk(stackChunkOop chunk) {
2145   chunk->set_sp(chunk->stack_size());
2146   chunk->set_argsize(0);
2147   chunk->set_max_thawing_size(0);
2148 }
2149 
2150  int ThawBase::remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize) {
2151   bool empty = false;
2152   StackChunkFrameStream<ChunkFrames::CompiledOnly> f(chunk);
2153   DEBUG_ONLY(intptr_t* const chunk_sp = chunk->start_address() + chunk->sp();)
2154   assert(chunk_sp == f.sp(), "");
2155   assert(chunk_sp == f.unextended_sp(), "");
2156 
2157   int frame_size = f.cb()->frame_size();
2158   argsize = f.stack_argsize();
2159   bool is_stub = f.is_stub();
2160 
2161   f.next(SmallRegisterMap::instance, true /* stop */);
2162   empty = f.is_done();
2163   assert(!empty || argsize == chunk->argsize(), "");
2164 
2165   assert(!is_stub || !empty, "runtime stub should have caller frame");
2166   if (is_stub) {
2167     // If we don't thaw the top compiled frame too, after restoring the saved
2168     // registers back in Java, we would hit the return barrier to thaw one more
2169     // frame effectively overwritting the restored registers during that call.
2170     f.get_cb();
2171     frame_size += f.cb()->frame_size();
2172     argsize = f.stack_argsize();
2173     f.next(SmallRegisterMap::instance, true /* stop */);
2174     empty = f.is_done();
2175     assert(!empty || argsize == chunk->argsize(), "");
2176   }
2177 
2178   if (empty) {
2179     clear_chunk(chunk);
2180   } else {
2181     chunk->set_sp(chunk->sp() + frame_size);
2182     chunk->set_max_thawing_size(chunk->max_thawing_size() - frame_size);
2183     // We set chunk->pc to the return pc into the next frame
2184     chunk->set_pc(f.pc());
2185 #ifdef ASSERT
2186     {
2187       intptr_t* retaddr_slot = (chunk_sp
2188                                 + frame_size
2189                                 - frame::sender_sp_ret_address_offset());
2190       assert(f.pc() == ContinuationHelper::return_address_at(retaddr_slot),
2191              "unexpected pc");
2192     }
2193 #endif
2194   }
2195   assert(empty == chunk->is_empty(), "");
2196   // returns the size required to store the frame on stack, and because it is a
2197   // compiled frame, it must include a copy of the arguments passed by the caller

2288     e.commit();
2289   }
2290 #endif
2291 
2292 #ifdef ASSERT
2293   set_anchor(_thread, rs.sp());
2294   log_frames(_thread);
2295   if (LoomDeoptAfterThaw) {
2296     do_deopt_after_thaw(_thread);
2297   }
2298   clear_anchor(_thread);
2299 #endif
2300 
2301   return rs.sp();
2302 }
2303 
2304 inline bool ThawBase::seen_by_gc() {
2305   return _barriers || _cont.tail()->is_gc_mode();
2306 }
2307 
2308 template <typename ConfigT>
2309 NOINLINE intptr_t* Thaw<ConfigT>::thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind) {
2310   bool retry_fast_path = false;
2311 
2312   bool preempted_case = chunk->is_preempted();
2313   if (preempted_case) {
2314     assert(_cont.is_preempted(), "must be");
2315     assert(chunk->objectMonitor() != nullptr, "must be");
2316 
2317     ObjectMonitor* mon = chunk->objectMonitor();
2318     if (!mon->is_owner(_thread)) {
2319       return push_preempt_monitorenter_redo(chunk);
2320     }
2321     chunk->set_is_preempted(false);
2322     retry_fast_path = true;
2323   }
2324 
2325 #if INCLUDE_ZGC || INCLUDE_SHENANDOAHGC
2326   if (UseZGC || UseShenandoahGC) {
2327     _cont.tail()->relativize_derived_pointers_concurrently();
2328   }
2329 #endif
2330 
2331   // First thaw after freeze. If there were oops in the stacklock
2332   // during freeze, restore them now.
2333   if (chunk->lockStackSize() > 0) {
2334     int lockStackSize = chunk->lockStackSize();
2335     assert(lockStackSize > 0, "should be");
2336 
2337     oop tmp_lockstack[8];
2338     chunk->copy_lockstack(tmp_lockstack);
2339     _thread->lock_stack().move_from_address(tmp_lockstack, lockStackSize);
2340 
2341     chunk->set_lockStackSize(0);
2342     chunk->set_has_lockStack(false);
2343     retry_fast_path = true;
2344   }
2345 
2346   // Retry the fast path now that we possibly cleared the FLAG_HAS_LOCKSTACK
2347   // and FLAG_PREEMPTED flags from the stackChunk.
2348   if (retry_fast_path && can_thaw_fast(chunk)) {
2349     intptr_t* sp = thaw_fast(chunk);
2350     if (preempted_case) {
2351       assert(_cont.is_preempted(), "must be");
2352       return handle_preempted_continuation(chunk, sp, true /* fast_case */);
2353     }
2354     return sp;
2355   }
2356 
2357   LogTarget(Trace, continuations) lt;
2358   if (lt.develop_is_enabled()) {
2359     LogStream ls(lt);
2360     ls.print_cr("thaw slow return_barrier: %d " INTPTR_FORMAT, kind, p2i(chunk));
2361     chunk->print_on(true, &ls);
2362   }
2363 
2364 #if CONT_JFR
2365   EventContinuationThawSlow e;
2366   if (e.should_commit()) {
2367     e.set_id(cast_from_oop<u8>(_cont.continuation()));
2368     e.commit();
2369   }
2370 #endif
2371 
2372   DEBUG_ONLY(_frames = 0;)
2373   _align_size = 0;
2374   bool is_return_barrier = kind != Continuation::thaw_top;
2375   int num_frames = (is_return_barrier ? 1 : 2);
2376 
2377   _stream = StackChunkFrameStream<ChunkFrames::Mixed>(chunk);
2378   _top_unextended_sp_before_thaw = _stream.unextended_sp();
2379 
2380   stackChunkOop original_chunk = chunk;
2381 
2382   frame heap_frame = _stream.to_frame();
2383   if (lt.develop_is_enabled()) {
2384     LogStream ls(lt);
2385     ls.print_cr("top hframe before (thaw):");
2386     assert(heap_frame.is_heap_frame(), "should have created a relative frame");
2387     heap_frame.print_value_on(&ls, nullptr);
2388   }
2389 






2390   frame caller; // the thawed caller on the stack
2391   recurse_thaw(heap_frame, caller, num_frames, true);
2392   finish_thaw(caller); // caller is now the topmost thawed frame
2393   _cont.write();
2394 
2395   assert(_cont.chunk_invariant(), "");
2396 
2397   JVMTI_ONLY(if (!is_return_barrier) invalidate_jvmti_stack(_thread));
2398 
2399   _thread->set_cont_fastpath(_fastpath);
2400 
2401   intptr_t* sp = caller.sp();
2402 
2403   if (preempted_case) {
2404     assert(_cont.is_preempted(), "must be");
2405     return handle_preempted_continuation(original_chunk, sp, false /* fast_case */);
2406   }
2407   return sp;
2408 }
2409 
2410 void ThawBase::recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top) {
2411   log_develop_debug(continuations)("thaw num_frames: %d", num_frames);
2412   assert(!_cont.is_empty(), "no more frames");
2413   assert(num_frames > 0, "");
2414   assert(!heap_frame.is_empty(), "");
2415 
2416   if (top && ContinuationHelper::Frame::is_stub(heap_frame.cb())) {

2417     recurse_thaw_stub_frame(heap_frame, caller, num_frames);
2418   } else if (!heap_frame.is_interpreted_frame()) {
2419     recurse_thaw_compiled_frame(heap_frame, caller, num_frames, false);
2420   } else {
2421     recurse_thaw_interpreted_frame(heap_frame, caller, num_frames);
2422   }
2423 }
2424 
2425 template<typename FKind>
2426 bool ThawBase::recurse_thaw_java_frame(frame& caller, int num_frames) {
2427   assert(num_frames > 0, "");
2428 
2429   DEBUG_ONLY(_frames++;)
2430 
2431   int argsize = _stream.stack_argsize();
2432 
2433   _stream.next(SmallRegisterMap::instance);
2434   assert(_stream.to_frame().is_empty() == _stream.is_done(), "");
2435 
2436   // we never leave a compiled caller of an interpreted frame as the top frame in the chunk

2518 }
2519 
2520 void ThawBase::clear_bitmap_bits(address start, address end) {
2521   assert(is_aligned(start, wordSize), "should be aligned: " PTR_FORMAT, p2i(start));
2522   assert(is_aligned(end, VMRegImpl::stack_slot_size), "should be aligned: " PTR_FORMAT, p2i(end));
2523 
2524   // we need to clear the bits that correspond to arguments as they reside in the caller frame
2525   // or they will keep objects that are otherwise unreachable alive.
2526 
2527   // Align `end` if UseCompressedOops is not set to avoid UB when calculating the bit index, since
2528   // `end` could be at an odd number of stack slots from `start`, i.e might not be oop aligned.
2529   // If that's the case the bit range corresponding to the last stack slot should not have bits set
2530   // anyways and we assert that before returning.
2531   address effective_end = UseCompressedOops ? end : align_down(end, wordSize);
2532   log_develop_trace(continuations)("clearing bitmap for " INTPTR_FORMAT " - " INTPTR_FORMAT, p2i(start), p2i(effective_end));
2533   stackChunkOop chunk = _cont.tail();
2534   chunk->bitmap().clear_range(chunk->bit_index_for(start), chunk->bit_index_for(effective_end));
2535   assert(effective_end == end || !chunk->bitmap().at(chunk->bit_index_for(effective_end)), "bit should not be set");
2536 }
2537 
2538 intptr_t* ThawBase::handle_preempted_continuation(stackChunkOop original_chunk, intptr_t* sp, bool fast_case) {
2539   frame top(sp);
2540   assert(top.pc() == *(address*)(sp - frame::sender_sp_ret_address_offset()), "");
2541   bool top_is_interpreted = Interpreter::contains(top.pc());
2542 
2543   if (fast_case) {
2544     assert(ContinuationHelper::Frame::is_stub(top.cb()), "invariant");
2545     int fsize = ContinuationHelper::StubFrame::size(top);
2546     patch_pd(top, sp + fsize);
2547   }
2548 
2549   _cont.set_preempted(false);
2550   bool same_chunk = original_chunk == _cont.tail();
2551 
2552   ObjectMonitor* mon = original_chunk->objectMonitor();
2553   if (same_chunk && mon != nullptr) {
2554     original_chunk->set_objectMonitor(nullptr);
2555   }
2556 
2557   bool post_mount_event = true;
2558   if (_thread->preemption_cancelled()) {
2559     // Since we never actually unmounted don't post the mount event.
2560     post_mount_event = false;
2561     _thread->set_preemption_cancelled(false);
2562   }
2563 
2564 #if INCLUDE_JVMTI
2565   bool is_vthread = Continuation::continuation_scope(_cont.continuation()) == java_lang_VirtualThread::vthread_scope();
2566   if (is_vthread) {
2567     if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
2568       jvmti_mount_end(_thread, _cont, top, mon, post_mount_event);
2569     } else {
2570       _thread->set_is_in_VTMS_transition(false);
2571       java_lang_Thread::set_is_in_VTMS_transition(_thread->vthread(), false);
2572     }
2573   }
2574 #endif
2575 
2576   if (!top_is_interpreted) {
2577     assert(ContinuationHelper::Frame::is_stub(top.cb()), "invariant");
2578     // The continuation might now run on a different platform thread than the previous time so
2579     // we need to adjust the current thread saved in the stub frame before restoring registers.
2580     JavaThread** thread_addr = frame::saved_thread_address(top);
2581     if (thread_addr != nullptr) *thread_addr = _thread;
2582   }
2583   sp = push_preempt_rerun_adapter(top, top_is_interpreted /* is_interpreted_frame */);
2584   return sp;
2585 }
2586 
2587 NOINLINE void ThawBase::recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames) {
2588   assert(hf.is_interpreted_frame(), "");
2589 
2590   if (UNLIKELY(seen_by_gc())) {
2591     _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance);
2592   }
2593 
2594   const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::InterpretedFrame>(caller, num_frames);
2595 
2596   DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2597 
2598   _align_size += frame::align_wiggle; // possible added alignment for internal interpreted frame alignment om AArch64
2599 
2600   frame f = new_stack_frame<ContinuationHelper::InterpretedFrame>(hf, caller, is_bottom_frame);
2601 
2602   intptr_t* const stack_frame_top = f.sp() + frame::metadata_words_at_top;
2603   intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
2604   intptr_t* const heap_frame_top = hf.unextended_sp() + frame::metadata_words_at_top;
2605   intptr_t* const heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
2606 

2627 
2628   maybe_set_fastpath(f.sp());
2629 
2630   const int locals = hf.interpreter_frame_method()->max_locals();
2631 
2632   if (!is_bottom_frame) {
2633     // can only fix caller once this frame is thawed (due to callee saved regs)
2634     _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance);
2635   } else if (_cont.tail()->has_bitmap() && locals > 0) {
2636     assert(hf.is_heap_frame(), "should be");
2637     address start = (address)(heap_frame_bottom - locals);
2638     address end = (address)heap_frame_bottom;
2639     clear_bitmap_bits(start, end);
2640   }
2641 
2642   DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2643   caller = f;
2644 }
2645 
2646 void ThawBase::recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller) {
2647   assert(hf.is_compiled_frame(), "");
2648   assert(_cont.is_preempted() || !stub_caller, "stub caller not at preemption");
2649 
2650   if (!stub_caller && UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2651     _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance);
2652   }
2653 
2654   const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::CompiledFrame>(caller, num_frames);
2655 
2656   DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2657 
2658   assert(caller.sp() == caller.unextended_sp(), "");
2659 
2660   if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) {
2661     _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_compiled_frame
2662   }
2663 
2664   // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2665   // yet laid out in the stack, and so the original_pc is not stored in it.
2666   // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2667   frame f = new_stack_frame<ContinuationHelper::CompiledFrame>(hf, caller, is_bottom_frame);
2668   intptr_t* const stack_frame_top = f.sp();
2669   intptr_t* const heap_frame_top = hf.unextended_sp();
2670 
2671   const int added_argsize = (is_bottom_frame || caller.is_interpreted_frame()) ? hf.compiled_frame_stack_argsize() : 0;
2672   int fsize = ContinuationHelper::CompiledFrame::size(hf) + added_argsize;
2673   assert(fsize <= (int)(caller.unextended_sp() - f.unextended_sp()), "");
2674 
2675   intptr_t* from = heap_frame_top - frame::metadata_words_at_bottom;
2676   intptr_t* to   = stack_frame_top - frame::metadata_words_at_bottom;
2677   // copy metadata, except the metadata at the top of the (unextended) entry frame
2678   int sz = fsize + frame::metadata_words_at_bottom + (is_bottom_frame && added_argsize == 0 ? 0 : frame::metadata_words_at_top);
2679 
2680   // If we're the bottom-most thawed frame, we're writing to within one word from entrySP
2681   // (we might have one padding word for alignment)
2682   assert(!is_bottom_frame || (_cont.entrySP() - 1 <= to + sz && to + sz <= _cont.entrySP()), "");
2683   assert(!is_bottom_frame || hf.compiled_frame_stack_argsize() != 0 || (to + sz && to + sz == _cont.entrySP()), "");
2684 
2685   copy_from_chunk(from, to, sz); // copying good oops because we invoked barriers above
2686 
2687   patch(f, caller, is_bottom_frame);
2688 
2689   // f.is_deoptimized_frame() is always false and we must test hf.is_deoptimized_frame() (see comment above)
2690   assert(!f.is_deoptimized_frame(), "");
2691   if (hf.is_deoptimized_frame()) {
2692     maybe_set_fastpath(f.sp());
2693   } else if (_thread->is_interp_only_mode()
2694               || (_cont.is_preempted() && f.cb()->as_compiled_method()->is_marked_for_deoptimization())) {
2695     // The caller of the safepoint stub when the continuation is preempted is not at a call instruction, and so
2696     // cannot rely on nmethod patching for deopt.

2697 
2698     log_develop_trace(continuations)("Deoptimizing thawed frame");
2699     DEBUG_ONLY(ContinuationHelper::Frame::patch_pc(f, nullptr));
2700 
2701     f.deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2702     assert(f.is_deoptimized_frame(), "");
2703     assert(ContinuationHelper::Frame::is_deopt_return(f.raw_pc(), f), "");
2704     maybe_set_fastpath(f.sp());
2705   }
2706 
2707   if (!is_bottom_frame) {
2708     // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2709     _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance);
2710   } else if (_cont.tail()->has_bitmap() && added_argsize > 0) {
2711     address start = (address)(heap_frame_top + ContinuationHelper::CompiledFrame::size(hf) + frame::metadata_words_at_top);
2712     int stack_args_slots = f.cb()->as_compiled_method()->method()->num_stack_arg_slots(false /* rounded */);
2713     int argsize_in_bytes = stack_args_slots * VMRegImpl::stack_slot_size;
2714     clear_bitmap_bits(start, start + argsize_in_bytes);
2715   }
2716 
2717   DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2718   caller = f;
2719 }
2720 
2721 void ThawBase::recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames) {
2722   DEBUG_ONLY(_frames++;)
2723   bool is_bottom_frame = false;
2724 
2725   if (UNLIKELY(seen_by_gc())) {
2726     // Process the stub's caller here since we might need the full map (if the stub was
2727     // generated on a poll on return we shouldn't need a full map).
2728     RegisterMap map(nullptr,
2729                     RegisterMap::UpdateMap::include,
2730                     RegisterMap::ProcessFrames::skip,
2731                     RegisterMap::WalkContinuation::skip);
2732     map.set_include_argument_oops(false);
2733     _stream.next(&map);
2734     if (!_stream.is_done()) {

2735       _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, &map);
2736     }
2737   } else {
2738     _stream.next(SmallRegisterMap::instance);
2739   }
2740 
2741   if (_stream.is_done()) {
2742     finalize_thaw(caller, 0);
2743     is_bottom_frame = true;
2744   } else {
2745     frame f = _stream.to_frame();
2746     if (f.is_interpreted_frame()) {
2747       recurse_thaw_interpreted_frame(f, caller, num_frames);
2748     } else {
2749       recurse_thaw_compiled_frame(f, caller, num_frames, true);
2750     }
2751   }
2752 
2753   assert(!is_bottom_frame || !_cont.is_empty(), "");
2754   assert(caller.sp() == caller.unextended_sp(), "");
2755   assert(!caller.is_native_frame() || (is_bottom_frame && Continuation::is_continuation_enterSpecial(caller)), "caller should't be native except for enterSpecial case");
2756 
2757   DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2758 
2759   if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) {
2760     _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_stub_frame
2761   }
2762 
2763   frame f = new_stack_frame<ContinuationHelper::StubFrame>(hf, caller, false);
2764   intptr_t* stack_frame_top = f.sp();
2765   intptr_t* heap_frame_top = hf.sp();
2766   int fsize = ContinuationHelper::StubFrame::size(hf);
2767 
2768   copy_from_chunk(heap_frame_top - frame::metadata_words, stack_frame_top - frame::metadata_words,
2769                   fsize + frame::metadata_words);
2770 
2771   patch(f, caller, is_bottom_frame);
2772 
2773   if (!is_bottom_frame) {
2774     // can only fix caller once this frame is thawed (due to callee saved regs)
2775     RegisterMap map(nullptr,
2776                     RegisterMap::UpdateMap::include,
2777                     RegisterMap::ProcessFrames::skip,
2778                     RegisterMap::WalkContinuation::skip);
2779     map.set_include_argument_oops(false);
2780     f.oop_map()->update_register_map(&f, &map);
2781     ContinuationHelper::update_register_map_with_callee(caller, &map);
2782     _cont.tail()->fix_thawed_frame(caller, &map);
2783   }
2784 
2785   DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2786   caller = f;
2787 }
2788 
2789 void ThawBase::finish_thaw(frame& f) {
2790   stackChunkOop chunk = _cont.tail();
2791 
2792   if (chunk->is_empty()) {
2793     // Only remove chunk from list if it can't be reused for another freeze
2794     if (seen_by_gc()) {
2795       _cont.set_tail(chunk->parent());
2796     } else {
2797       chunk->set_has_mixed_frames(false);
2798     }
2799     chunk->set_max_thawing_size(0);
2800     assert(chunk->argsize() == 0, "");
2801   } else {
2802     chunk->set_max_thawing_size(chunk->max_thawing_size() - _align_size);
2803   }
2804   assert(chunk->is_empty() == (chunk->max_thawing_size() == 0), "");
2805 

2857 
2858   assert(!jdk_internal_vm_Continuation::done(oopCont), "");
2859   assert(oopCont == get_continuation(thread), "");
2860   verify_continuation(oopCont);
2861 
2862   assert(entry->is_virtual_thread() == (entry->scope(thread) == java_lang_VirtualThread::vthread_scope()), "");
2863 
2864   ContinuationWrapper cont(thread, oopCont);
2865   log_develop_debug(continuations)("THAW #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
2866 
2867 #ifdef ASSERT
2868   set_anchor_to_entry(thread, cont.entry());
2869   log_frames(thread);
2870   clear_anchor(thread);
2871 #endif
2872 
2873   Thaw<ConfigT> thw(thread, cont);
2874   intptr_t* const sp = thw.thaw(kind);
2875   assert(is_aligned(sp, frame::frame_alignment), "");
2876 



2877 #ifdef ASSERT
2878   intptr_t* sp0 = sp;
2879   address pc0 = *(address*)(sp - frame::sender_sp_ret_address_offset());
2880 
2881   bool sent_entry_pc = false;
2882   if (pc0 == Interpreter::cont_preempt_rerun_interpreter_adapter() ||
2883       pc0 == StubRoutines::cont_preempt_rerun_compiler_adapter()) {
2884     sp0 += frame::metadata_words;    // see push_preempt_rerun_adapter
2885 #ifdef AARCH64
2886     if (pc0 == StubRoutines::cont_preempt_rerun_compiler_adapter()) {
2887       address pc1 = *(address*)(sp0 - frame::sender_sp_ret_address_offset());
2888       CodeBlob* cb = CodeCache::find_blob(pc1);
2889       assert(cb != nullptr, "should be either c1 or c2 runtime stub");
2890       if (cb->frame_size() == 2) {
2891         sp0 += frame::metadata_words;
2892       }
2893     }
2894 #endif
2895   } else if (pc0 == StubRoutines::cont_preempt_monitorenter_redo()) {
2896     sp0 += 2 * frame::metadata_words; // see push_preempt_monitorenter_redo
2897     sent_entry_pc = true;
2898   }
2899   set_anchor(thread, sp0, sent_entry_pc ? cont.entryPC() : nullptr);
2900   log_frames(thread);
2901   if (LoomVerifyAfterThaw) {
2902     assert(do_verify_after_thaw(thread, cont.tail(), tty), "");
2903   }
2904   assert(ContinuationEntry::assert_entry_frame_laid_out(thread), "");
2905   clear_anchor(thread);
2906 
2907   LogTarget(Trace, continuations) lt;
2908   if (lt.develop_is_enabled()) {
2909     LogStream ls(lt);
2910     ls.print_cr("Jumping to frame (thaw):");
2911     frame(sp).print_value_on(&ls, nullptr);
2912   }
2913 #endif
2914 
2915   CONT_JFR_ONLY(thw.jfr_info().post_jfr_event(&event, cont.continuation(), thread);)
2916 
2917   verify_continuation(cont.continuation());
2918   log_develop_debug(continuations)("=== End of thaw #" INTPTR_FORMAT, cont.hash());
2919 

2991         st->print_cr("size: %d argsize: %d",
2992                      ContinuationHelper::NonInterpretedUnknownFrame::size(fr),
2993                      ContinuationHelper::NonInterpretedUnknownFrame::stack_argsize(fr));
2994       }
2995       VMReg reg = fst.register_map()->find_register_spilled_here(cl.p(), fst.current()->sp());
2996       if (reg != nullptr) {
2997         st->print_cr("Reg %s %d", reg->name(), reg->is_stack() ? (int)reg->reg2stack() : -99);
2998       }
2999       cl.reset();
3000       DEBUG_ONLY(thread->print_frame_layout();)
3001       if (chunk != nullptr) {
3002         chunk->print_on(true, st);
3003       }
3004       return false;
3005     }
3006   }
3007   return true;
3008 }
3009 
3010 static void log_frames(JavaThread* thread) {
3011   const static int show_entry_callers = 100;
3012   LogTarget(Trace, continuations) lt;
3013   if (!lt.develop_is_enabled()) {
3014     return;
3015   }
3016   LogStream ls(lt);
3017 
3018   ls.print_cr("------- frames --------- for thread " INTPTR_FORMAT, p2i(thread));
3019   if (!thread->has_last_Java_frame()) {
3020     ls.print_cr("NO ANCHOR!");
3021   }
3022 
3023   RegisterMap map(thread,
3024                   RegisterMap::UpdateMap::include,
3025                   RegisterMap::ProcessFrames::include,
3026                   RegisterMap::WalkContinuation::skip);
3027   map.set_include_argument_oops(false);
3028 
3029   if (false) {
3030     for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) {
3031       f.print_on(&ls);
3032     }
3033   } else {
3034     map.set_skip_missing(true);
3035     ResetNoHandleMark rnhm;
3036     ResourceMark rm;
3037     HandleMark hm(Thread::current());
3038     FrameValues values;
3039 
3040     int i = 0;
3041     int post_entry = -1;
3042     for (frame f = thread->last_frame(); !f.is_first_frame(); f = f.sender(&map), i++) {
3043       f.describe(values, i, &map, i == 0);
3044       if (post_entry >= 0 || Continuation::is_continuation_enterSpecial(f))
3045         post_entry++;
3046       if (post_entry >= show_entry_callers)
3047         break;
3048     }
3049     values.print_on(thread, &ls);
3050   }
3051 
3052   ls.print_cr("======= end frames =========");
3053 }
3054 #endif // ASSERT
3055 
3056 #include CPU_HEADER_INLINE(continuationFreezeThaw)
3057 
3058 #ifdef ASSERT
3059 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st) {
3060   ResourceMark rm;
3061   FrameValues values;
3062   assert(f.get_cb() != nullptr, "");
3063   RegisterMap map(f.is_heap_frame() ?
3064                     nullptr :
3065                     JavaThread::current(),
3066                   RegisterMap::UpdateMap::include,
3067                   RegisterMap::ProcessFrames::skip,
3068                   RegisterMap::WalkContinuation::skip);
3069   map.set_include_argument_oops(false);
3070   map.set_skip_missing(true);
3071   if (callee_complete) {
3072     frame::update_map_with_saved_link(&map, ContinuationHelper::Frame::callee_link_address(f));
3073   }
3074   const_cast<frame&>(f).describe(values, 0, &map, true);
3075   values.print_on(static_cast<JavaThread*>(nullptr), st);
3076 }
3077 #endif
3078 
3079 static address thaw_entry   = nullptr;
3080 static address freeze_entry = nullptr;
3081 static address freeze_preempt_entry = nullptr;
3082 
3083 address Continuation::thaw_entry() {
3084   return ::thaw_entry;
3085 }
3086 
3087 address Continuation::freeze_entry() {
3088   return ::freeze_entry;
3089 }
3090 
3091 address Continuation::freeze_preempt_entry() {
3092   return ::freeze_preempt_entry;
3093 }
3094 
3095 class ConfigResolve {
3096 public:
3097   static void resolve() { resolve_compressed(); }
3098 
3099   static void resolve_compressed() {
3100     UseCompressedOops ? resolve_gc<true>()
3101                       : resolve_gc<false>();
3102   }
3103 
3104 private:
3105   template <bool use_compressed>
3106   static void resolve_gc() {
3107     BarrierSet* bs = BarrierSet::barrier_set();
3108     assert(bs != nullptr, "freeze/thaw invoked before BarrierSet is set");
3109     switch (bs->kind()) {
3110 #define BARRIER_SET_RESOLVE_BARRIER_CLOSURE(bs_name)                    \
3111       case BarrierSet::bs_name: {                                       \
3112         resolve<use_compressed, typename BarrierSet::GetType<BarrierSet::bs_name>::type>(); \
3113       }                                                                 \
3114         break;
3115       FOR_EACH_CONCRETE_BARRIER_SET_DO(BARRIER_SET_RESOLVE_BARRIER_CLOSURE)
3116 #undef BARRIER_SET_RESOLVE_BARRIER_CLOSURE
3117 
3118     default:
3119       fatal("BarrierSet resolving not implemented");
3120     };
3121   }
3122 
3123   template <bool use_compressed, typename BarrierSetT>
3124   static void resolve() {
3125     typedef Config<use_compressed ? oop_kind::NARROW : oop_kind::WIDE, BarrierSetT> SelectedConfigT;
3126 
3127     freeze_entry = (address)freeze<SelectedConfigT>;
3128     freeze_preempt_entry = (address)SelectedConfigT::freeze_preempt;
3129 
3130     // If we wanted, we could templatize by kind and have three different thaw entries
3131     thaw_entry   = (address)thaw<SelectedConfigT>;
3132   }
3133 };
3134 
3135 void Continuation::init() {
3136   ConfigResolve::resolve();
3137 }
< prev index next >