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