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