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