16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/javaClasses.inline.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "code/codeCache.inline.hpp"
28 #include "code/nmethod.inline.hpp"
29 #include "code/vmreg.inline.hpp"
30 #include "compiler/oopMap.inline.hpp"
31 #include "gc/shared/continuationGCSupport.inline.hpp"
32 #include "gc/shared/gc_globals.hpp"
33 #include "gc/shared/barrierSet.hpp"
34 #include "gc/shared/memAllocator.hpp"
35 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "jfr/jfrEvents.hpp"
38 #include "logging/log.hpp"
39 #include "logging/logStream.hpp"
40 #include "oops/access.inline.hpp"
41 #include "oops/method.inline.hpp"
42 #include "oops/oopsHierarchy.hpp"
43 #include "oops/objArrayOop.inline.hpp"
44 #include "oops/stackChunkOop.inline.hpp"
45 #include "prims/jvmtiThreadState.hpp"
46 #include "runtime/arguments.hpp"
47 #include "runtime/continuation.hpp"
48 #include "runtime/continuationEntry.inline.hpp"
49 #include "runtime/continuationHelper.inline.hpp"
50 #include "runtime/continuationJavaClasses.inline.hpp"
51 #include "runtime/continuationWrapper.inline.hpp"
52 #include "runtime/frame.inline.hpp"
53 #include "runtime/interfaceSupport.inline.hpp"
54 #include "runtime/javaThread.inline.hpp"
55 #include "runtime/jniHandles.inline.hpp"
56 #include "runtime/keepStackGCProcessed.hpp"
57 #include "runtime/objectMonitor.inline.hpp"
58 #include "runtime/orderAccess.hpp"
59 #include "runtime/prefetch.inline.hpp"
60 #include "runtime/smallRegisterMap.inline.hpp"
61 #include "runtime/sharedRuntime.hpp"
62 #include "runtime/stackChunkFrameStream.inline.hpp"
63 #include "runtime/stackFrameStream.inline.hpp"
64 #include "runtime/stackOverflow.hpp"
65 #include "runtime/stackWatermarkSet.inline.hpp"
66 #include "utilities/debug.hpp"
67 #include "utilities/exceptions.hpp"
68 #include "utilities/macros.hpp"
69 #include "utilities/vmError.hpp"
70 #if INCLUDE_ZGC
71 #include "gc/z/zStackChunkGCData.inline.hpp"
72 #endif
73
74 #include <type_traits>
75
76 /*
77 * This file contains the implementation of continuation freezing (yield) and thawing (run).
78 *
79 * This code is very latency-critical and very hot. An ordinary and well-behaved server application
80 * would likely call these operations many thousands of times per second second, on every core.
81 *
82 * Freeze might be called every time the application performs any I/O operation, every time it
83 * acquires a j.u.c. lock, every time it takes a message from a queue, and thaw can be called
84 * multiple times in each of those cases, as it is called by the return barrier, which may be
85 * invoked on method return.
86 *
87 * The amortized budget for each of those two operations is ~100-150ns. That is why, for
88 * example, every effort is made to avoid Java-VM transitions as much as possible.
89 *
90 * On the fast path, all frames are known to be compiled, and the chunk requires no barriers
91 * and so frames simply copied, and the bottom-most one is patched.
92 * On the slow path, internal pointers in interpreted frames are de/relativized to/from offsets
162 #endif
163
164 // TODO: See AbstractAssembler::generate_stack_overflow_check,
165 // Compile::bang_size_in_bytes(), m->as_SafePoint()->jvms()->interpreter_frame_size()
166 // when we stack-bang, we need to update a thread field with the lowest (farthest) bang point.
167
168 // Data invariants are defined by Continuation::debug_verify_continuation and Continuation::debug_verify_stack_chunk
169
170 // Used to just annotatate cold/hot branches
171 #define LIKELY(condition) (condition)
172 #define UNLIKELY(condition) (condition)
173
174 // debugging functions
175 #ifdef ASSERT
176 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue); // address p is readable and *(intptr_t*)p != errvalue
177
178 static void verify_continuation(oop continuation) { Continuation::debug_verify_continuation(continuation); }
179
180 static void do_deopt_after_thaw(JavaThread* thread);
181 static bool do_verify_after_thaw(JavaThread* thread, stackChunkOop chunk, outputStream* st);
182 static void log_frames(JavaThread* thread);
183 static void log_frames_after_thaw(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp, bool preempted);
184 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st = tty);
185
186 #define assert_pfl(p, ...) \
187 do { \
188 if (!(p)) { \
189 JavaThread* t = JavaThread::active(); \
190 if (t->has_last_Java_frame()) { \
191 tty->print_cr("assert(" #p ") failed:"); \
192 t->print_frame_layout(); \
193 } \
194 } \
195 vmassert(p, __VA_ARGS__); \
196 } while(0)
197
198 #else
199 static void verify_continuation(oop continuation) { }
200 #define assert_pfl(p, ...)
201 #endif
202
203 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint);
204 template<typename ConfigT, bool preempt> static inline freeze_result freeze_internal(JavaThread* current, intptr_t* const sp);
492
493 assert(!Interpreter::contains(_cont.entryPC()), "");
494
495 _bottom_address = _cont.entrySP() - _cont.entry_frame_extension();
496 #ifdef _LP64
497 if (((intptr_t)_bottom_address & 0xf) != 0) {
498 _bottom_address--;
499 }
500 assert(is_aligned(_bottom_address, frame::frame_alignment), "");
501 #endif
502
503 log_develop_trace(continuations)("bottom_address: " INTPTR_FORMAT " entrySP: " INTPTR_FORMAT " argsize: " PTR_FORMAT,
504 p2i(_bottom_address), p2i(_cont.entrySP()), (_cont.entrySP() - _bottom_address) << LogBytesPerWord);
505 assert(_bottom_address != nullptr, "");
506 assert(_bottom_address <= _cont.entrySP(), "");
507 DEBUG_ONLY(_last_write = nullptr;)
508
509 assert(_cont.chunk_invariant(), "");
510 assert(!Interpreter::contains(_cont.entryPC()), "");
511 #if !defined(PPC64) || defined(ZERO)
512 static const int doYield_stub_frame_size = frame::metadata_words;
513 #else
514 static const int doYield_stub_frame_size = frame::native_abi_reg_args_size >> LogBytesPerWord;
515 #endif
516 // With preemption doYield() might not have been resolved yet
517 assert(_preempt || SharedRuntime::cont_doYield_stub()->frame_size() == doYield_stub_frame_size, "");
518
519 if (preempt) {
520 _last_frame = _thread->last_frame();
521 }
522
523 // properties of the continuation on the stack; all sizes are in words
524 _cont_stack_top = frame_sp + (!preempt ? doYield_stub_frame_size : 0); // we don't freeze the doYield stub frame
525 _cont_stack_bottom = _cont.entrySP() + (_cont.argsize() == 0 ? frame::metadata_words_at_top : 0)
526 - ContinuationHelper::frame_align_words(_cont.argsize()); // see alignment in thaw
527
528 log_develop_trace(continuations)("freeze size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT,
529 cont_size(), _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom));
530 assert(cont_size() > 0, "");
531
532 if (LockingMode != LM_LIGHTWEIGHT) {
533 _monitors_in_lockstack = 0;
534 } else {
535 _monitors_in_lockstack = _thread->lock_stack().monitor_count();
536 }
537 }
538
539 void FreezeBase::init_rest() { // we want to postpone some initialization after chunk handling
540 _freeze_size = 0;
541 _total_align_size = 0;
542 NOT_PRODUCT(_frames = 0;)
543 }
544
877 freeze_result res = recurse_freeze(f, caller, 0, false, true);
878
879 if (res == freeze_ok) {
880 finish_freeze(f, caller);
881 _cont.write();
882 }
883
884 return res;
885 }
886
887 frame FreezeBase::freeze_start_frame() {
888 if (LIKELY(!_preempt)) {
889 return freeze_start_frame_yield_stub();
890 } else {
891 return freeze_start_frame_on_preempt();
892 }
893 }
894
895 frame FreezeBase::freeze_start_frame_yield_stub() {
896 frame f = _thread->last_frame();
897 assert(SharedRuntime::cont_doYield_stub()->contains(f.pc()), "must be");
898 f = sender<ContinuationHelper::NonInterpretedUnknownFrame>(f);
899 assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), f), "");
900 return f;
901 }
902
903 frame FreezeBase::freeze_start_frame_on_preempt() {
904 assert(_last_frame.sp() == _thread->last_frame().sp(), "_last_frame should be already initialized");
905 assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), _last_frame), "");
906 return _last_frame;
907 }
908
909 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
910 NOINLINE freeze_result FreezeBase::recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top) {
911 assert(f.unextended_sp() < _bottom_address, ""); // see recurse_freeze_java_frame
912 assert(f.is_interpreted_frame() || ((top && _preempt) == ContinuationHelper::Frame::is_stub(f.cb()))
913 || ((top && _preempt) == f.is_native_frame()), "");
914
915 if (stack_overflow()) {
916 return freeze_exception;
917 }
1073 log_develop_trace(continuations)("Reusing chunk mixed: %d empty: %d", chunk->has_mixed_frames(), chunk->is_empty());
1074 if (chunk->is_empty()) {
1075 int sp = chunk->stack_size() - argsize_md;
1076 chunk->set_sp(sp);
1077 chunk->set_bottom(sp);
1078 _freeze_size += overlap;
1079 assert(chunk->max_thawing_size() == 0, "");
1080 } DEBUG_ONLY(else empty_chunk = false;)
1081 }
1082 assert(!chunk->is_gc_mode(), "");
1083 assert(!chunk->has_bitmap(), "");
1084 chunk->set_has_mixed_frames(true);
1085
1086 assert(chunk->requires_barriers() == _barriers, "");
1087 assert(!_barriers || chunk->is_empty(), "");
1088
1089 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).is_done(), "");
1090 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame().is_empty(), "");
1091
1092 if (_preempt) {
1093 frame f = _thread->last_frame();
1094 if (f.is_interpreted_frame()) {
1095 // Some platforms do not save the last_sp in the top interpreter frame on VM calls.
1096 // We need it so that on resume we can restore the sp to the right place, since
1097 // thawing might add an alignment word to the expression stack (see finish_thaw()).
1098 // We do it now that we know freezing will be successful.
1099 prepare_freeze_interpreted_top_frame(f);
1100 }
1101 }
1102
1103 // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
1104 // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
1105 // will either see no continuation or a consistent chunk.
1106 unwind_frames();
1107
1108 chunk->set_max_thawing_size(chunk->max_thawing_size() + _freeze_size - _monitors_in_lockstack - frame::metadata_words);
1109
1110 if (lt.develop_is_enabled()) {
1111 LogStream ls(lt);
1112 ls.print_cr("top chunk:");
1113 chunk->print_on(&ls);
1114 }
1115
1116 if (_monitors_in_lockstack > 0) {
1117 freeze_lockstack(chunk);
1118 }
1119
1591 // Some GCs could put direct allocations in old gen for slow-path
1592 // allocations; need to explicitly check if that was the case.
1593 _barriers = chunk->requires_barriers();
1594 }
1595 }
1596
1597 if (_barriers) {
1598 log_develop_trace(continuations)("allocation requires barriers");
1599 }
1600
1601 assert(chunk->parent() == nullptr || chunk->parent()->is_stackChunk(), "");
1602
1603 return chunk;
1604 }
1605
1606 void FreezeBase::throw_stack_overflow_on_humongous_chunk() {
1607 ContinuationWrapper::SafepointOp so(_thread, _cont); // could also call _cont.done() instead
1608 Exceptions::_throw_msg(_thread, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Humongous stack chunk");
1609 }
1610
1611 #if INCLUDE_JVMTI
1612 static int num_java_frames(ContinuationWrapper& cont) {
1613 ResourceMark rm; // used for scope traversal in num_java_frames(nmethod*, address)
1614 int count = 0;
1615 for (stackChunkOop chunk = cont.tail(); chunk != nullptr; chunk = chunk->parent()) {
1616 count += chunk->num_java_frames();
1617 }
1618 return count;
1619 }
1620
1621 static void invalidate_jvmti_stack(JavaThread* thread) {
1622 if (thread->is_interp_only_mode()) {
1623 JvmtiThreadState *state = thread->jvmti_thread_state();
1624 if (state != nullptr)
1625 state->invalidate_cur_stack_depth();
1626 }
1627 }
1628
1629 static void jvmti_yield_cleanup(JavaThread* thread, ContinuationWrapper& cont) {
1630 if (JvmtiExport::can_post_frame_pop()) {
1631 int num_frames = num_java_frames(cont);
1632
1633 ContinuationWrapper::SafepointOp so(Thread::current(), cont);
1634 JvmtiExport::continuation_yield_cleanup(JavaThread::current(), num_frames);
1635 }
1636 invalidate_jvmti_stack(thread);
1637 }
1638
1639 static void jvmti_mount_end(JavaThread* current, ContinuationWrapper& cont, frame top) {
1640 assert(current->vthread() != nullptr, "must be");
1641
1642 HandleMarkCleaner hm(current);
1643 Handle vth(current, current->vthread());
1644
1645 ContinuationWrapper::SafepointOp so(current, cont);
1646
1647 // Since we might safepoint set the anchor so that the stack can be walked.
1648 set_anchor(current, top.sp());
1649
1650 JRT_BLOCK
1651 JvmtiVTMSTransitionDisabler::VTMS_vthread_mount((jthread)vth.raw_value(), false);
1652
1653 if (current->pending_contended_entered_event()) {
1654 JvmtiExport::post_monitor_contended_entered(current, current->contended_entered_monitor());
1655 current->set_contended_entered_monitor(nullptr);
1656 }
1657 JRT_BLOCK_END
1658
1659 clear_anchor(current);
1660 }
1661 #endif // INCLUDE_JVMTI
1662
1663 #ifdef ASSERT
1664 // There are no interpreted frames if we're not called from the interpreter and we haven't ancountered an i2c
1665 // adapter or called Deoptimization::unpack_frames. As for native frames, upcalls from JNI also go through the
1666 // interpreter (see JavaCalls::call_helper), while the UpcallLinker explicitly sets cont_fastpath.
1667 bool FreezeBase::check_valid_fast_path() {
1668 ContinuationEntry* ce = _thread->last_continuation();
1669 RegisterMap map(_thread,
1670 RegisterMap::UpdateMap::skip,
1671 RegisterMap::ProcessFrames::skip,
1672 RegisterMap::WalkContinuation::skip);
1673 map.set_include_argument_oops(false);
1674 bool is_top_frame = true;
1675 for (frame f = freeze_start_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map), is_top_frame = false) {
1676 if (!((f.is_compiled_frame() && !f.is_deoptimized_frame()) || (is_top_frame && (f.is_runtime_frame() || f.is_native_frame())))) {
1677 return false;
1678 }
1679 }
1680 return true;
1681 }
1682 #endif // ASSERT
1683
1684 static inline freeze_result freeze_epilog(ContinuationWrapper& cont) {
1685 verify_continuation(cont.continuation());
1686 assert(!cont.is_empty(), "");
1687
1688 log_develop_debug(continuations)("=== End of freeze cont ### #" INTPTR_FORMAT, cont.hash());
1689 return freeze_ok;
1690 }
1691
1692 static freeze_result freeze_epilog(JavaThread* thread, ContinuationWrapper& cont, freeze_result res) {
1693 if (UNLIKELY(res != freeze_ok)) {
1694 JFR_ONLY(thread->set_last_freeze_fail_result(res);)
1695 verify_continuation(cont.continuation());
1696 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1697 return res;
1698 }
1699
1700 JVMTI_ONLY(jvmti_yield_cleanup(thread, cont)); // can safepoint
1701 return freeze_epilog(cont);
1702 }
1703
1704 static freeze_result preempt_epilog(ContinuationWrapper& cont, freeze_result res, frame& old_last_frame) {
1705 if (UNLIKELY(res != freeze_ok)) {
1706 verify_continuation(cont.continuation());
1707 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1708 return res;
1709 }
1710
1711 patch_return_pc_with_preempt_stub(old_last_frame);
1712 cont.tail()->set_preempted(true);
1713
1714 return freeze_epilog(cont);
1715 }
1716
1717 template<typename ConfigT, bool preempt>
1718 static inline freeze_result freeze_internal(JavaThread* current, intptr_t* const sp) {
1719 assert(!current->has_pending_exception(), "");
1720
1721 #ifdef ASSERT
1722 log_trace(continuations)("~~~~ freeze sp: " INTPTR_FORMAT "JavaThread: " INTPTR_FORMAT, p2i(current->last_continuation()->entry_sp()), p2i(current));
1723 log_frames(current);
1724 #endif
1725
1726 CONT_JFR_ONLY(EventContinuationFreeze event;)
1727
1728 ContinuationEntry* entry = current->last_continuation();
1729
1730 oop oopCont = entry->cont_oop(current);
1731 assert(oopCont == current->last_continuation()->cont_oop(current), "");
1732 assert(ContinuationEntry::assert_entry_frame_laid_out(current), "");
1733
1734 verify_continuation(oopCont);
1735 ContinuationWrapper cont(current, oopCont);
1736 log_develop_debug(continuations)("FREEZE #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
1737
1738 assert(entry->is_virtual_thread() == (entry->scope(current) == java_lang_VirtualThread::vthread_scope()), "");
1739
1740 assert(LockingMode == LM_LEGACY || (current->held_monitor_count() == 0 && current->jni_monitor_count() == 0),
1741 "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());
1742
1743 if (entry->is_pinned() || current->held_monitor_count() > 0) {
1897 // 300 is an estimate for stack size taken for this native code, in addition to StackShadowPages
1898 // for the Java frames in the check below.
1899 if (!stack_overflow_check(thread, size + 300, bottom)) {
1900 return 0;
1901 }
1902
1903 log_develop_trace(continuations)("prepare_thaw bottom: " INTPTR_FORMAT " top: " INTPTR_FORMAT " size: %d",
1904 p2i(bottom), p2i(bottom - size), size);
1905 return size;
1906 }
1907
1908 class ThawBase : public StackObj {
1909 protected:
1910 JavaThread* _thread;
1911 ContinuationWrapper& _cont;
1912 CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;)
1913
1914 intptr_t* _fastpath;
1915 bool _barriers;
1916 bool _preempted_case;
1917 intptr_t* _top_unextended_sp_before_thaw;
1918 int _align_size;
1919 DEBUG_ONLY(intptr_t* _top_stack_address);
1920
1921 StackChunkFrameStream<ChunkFrames::Mixed> _stream;
1922
1923 NOT_PRODUCT(int _frames;)
1924
1925 protected:
1926 ThawBase(JavaThread* thread, ContinuationWrapper& cont) :
1927 _thread(thread), _cont(cont),
1928 _fastpath(nullptr) {
1929 DEBUG_ONLY(_top_unextended_sp_before_thaw = nullptr;)
1930 assert (cont.tail() != nullptr, "no last chunk");
1931 DEBUG_ONLY(_top_stack_address = _cont.entrySP() - thaw_size(cont.tail());)
1932 }
1933
1934 void clear_chunk(stackChunkOop chunk);
1935 template<bool check_stub>
1936 int remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize);
1937 void copy_from_chunk(intptr_t* from, intptr_t* to, int size);
1938
1939 void thaw_lockstack(stackChunkOop chunk);
1940
1941 // fast path
1942 inline void prefetch_chunk_pd(void* start, int size_words);
1943 void patch_return(intptr_t* sp, bool is_last);
1944
1945 intptr_t* handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case);
1946 inline intptr_t* push_cleanup_continuation();
1947 void throw_interrupted_exception(JavaThread* current, frame& top);
1948
1949 void recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case);
1950 void finish_thaw(frame& f);
1951
1952 private:
1953 template<typename FKind> bool recurse_thaw_java_frame(frame& caller, int num_frames);
1954 void finalize_thaw(frame& entry, int argsize);
1955
1956 inline bool seen_by_gc();
1957
1958 inline void before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame);
1959 inline void after_thaw_java_frame(const frame& f, bool bottom);
1960 inline void patch(frame& f, const frame& caller, bool bottom);
1961 void clear_bitmap_bits(address start, address end);
1962
1963 NOINLINE void recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames);
1964 void recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller);
1965 void recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames);
1966 void recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames);
1967
1968 void push_return_frame(frame& f);
1969 inline frame new_entry_frame();
1970 template<typename FKind> frame new_stack_frame(const frame& hf, frame& caller, bool bottom);
1971 inline void patch_pd(frame& f, const frame& sender);
1972 inline void patch_pd(frame& f, intptr_t* caller_sp);
1973 inline intptr_t* align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom);
1974
1975 void maybe_set_fastpath(intptr_t* sp) { if (sp > _fastpath) _fastpath = sp; }
1976
1977 static inline void derelativize_interpreted_frame_metadata(const frame& hf, const frame& f);
1978
1979 public:
1980 CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
1981 };
1982
1983 template <typename ConfigT>
2043 chunk->set_sp(chunk->bottom());
2044 chunk->set_max_thawing_size(0);
2045 }
2046
2047 template<bool check_stub>
2048 int ThawBase::remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize) {
2049 bool empty = false;
2050 StackChunkFrameStream<ChunkFrames::CompiledOnly> f(chunk);
2051 DEBUG_ONLY(intptr_t* const chunk_sp = chunk->start_address() + chunk->sp();)
2052 assert(chunk_sp == f.sp(), "");
2053 assert(chunk_sp == f.unextended_sp(), "");
2054
2055 int frame_size = f.cb()->frame_size();
2056 argsize = f.stack_argsize();
2057
2058 assert(!f.is_stub() || check_stub, "");
2059 if (check_stub && f.is_stub()) {
2060 // If we don't thaw the top compiled frame too, after restoring the saved
2061 // registers back in Java, we would hit the return barrier to thaw one more
2062 // frame effectively overwriting the restored registers during that call.
2063 f.next(SmallRegisterMap::instance(), true /* stop */);
2064 assert(!f.is_done(), "");
2065
2066 f.get_cb();
2067 assert(f.is_compiled(), "");
2068 frame_size += f.cb()->frame_size();
2069 argsize = f.stack_argsize();
2070
2071 if (f.cb()->as_nmethod()->is_marked_for_deoptimization()) {
2072 // The caller of the runtime stub when the continuation is preempted is not at a
2073 // Java call instruction, and so cannot rely on nmethod patching for deopt.
2074 log_develop_trace(continuations)("Deoptimizing runtime stub caller");
2075 f.to_frame().deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2076 }
2077 }
2078
2079 f.next(SmallRegisterMap::instance(), true /* stop */);
2080 empty = f.is_done();
2081 assert(!empty || argsize == chunk->argsize(), "");
2082
2083 if (empty) {
2084 clear_chunk(chunk);
2085 } else {
2086 chunk->set_sp(chunk->sp() + frame_size);
2087 chunk->set_max_thawing_size(chunk->max_thawing_size() - frame_size);
2088 // We set chunk->pc to the return pc into the next frame
2089 chunk->set_pc(f.pc());
2090 #ifdef ASSERT
2091 {
2092 intptr_t* retaddr_slot = (chunk_sp
2093 + frame_size
2094 - frame::sender_sp_ret_address_offset());
2095 assert(f.pc() == ContinuationHelper::return_address_at(retaddr_slot),
2096 "unexpected pc");
2097 }
2098 #endif
2099 }
2219 return rs.sp();
2220 }
2221
2222 inline bool ThawBase::seen_by_gc() {
2223 return _barriers || _cont.tail()->is_gc_mode();
2224 }
2225
2226 static inline void relativize_chunk_concurrently(stackChunkOop chunk) {
2227 #if INCLUDE_ZGC || INCLUDE_SHENANDOAHGC
2228 if (UseZGC || UseShenandoahGC) {
2229 chunk->relativize_derived_pointers_concurrently();
2230 }
2231 #endif
2232 }
2233
2234 template <typename ConfigT>
2235 NOINLINE intptr_t* Thaw<ConfigT>::thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind) {
2236 Continuation::preempt_kind preempt_kind;
2237 bool retry_fast_path = false;
2238
2239 _preempted_case = chunk->preempted();
2240 if (_preempted_case) {
2241 ObjectWaiter* waiter = java_lang_VirtualThread::objectWaiter(_thread->vthread());
2242 if (waiter != nullptr) {
2243 // Mounted again after preemption. Resume the pending monitor operation,
2244 // which will be either a monitorenter or Object.wait() call.
2245 ObjectMonitor* mon = waiter->monitor();
2246 preempt_kind = waiter->is_wait() ? Continuation::freeze_on_wait : Continuation::freeze_on_monitorenter;
2247
2248 bool mon_acquired = mon->resume_operation(_thread, waiter, _cont);
2249 assert(!mon_acquired || mon->has_owner(_thread), "invariant");
2250 if (!mon_acquired) {
2251 // Failed to acquire monitor. Return to enterSpecial to unmount again.
2252 return push_cleanup_continuation();
2253 }
2254 chunk = _cont.tail(); // reload oop in case of safepoint in resume_operation (if posting JVMTI events).
2255 } else {
2256 // Preemption cancelled in moniterenter case. We actually acquired
2257 // the monitor after freezing all frames so nothing to do.
2258 preempt_kind = Continuation::freeze_on_monitorenter;
2259 }
2260 // Call this first to avoid racing with GC threads later when modifying the chunk flags.
2261 relativize_chunk_concurrently(chunk);
2262 chunk->set_preempted(false);
2263 retry_fast_path = true;
2264 } else {
2265 relativize_chunk_concurrently(chunk);
2266 }
2267
2268 // On first thaw after freeze restore oops to the lockstack if any.
2269 assert(chunk->lockstack_size() == 0 || kind == Continuation::thaw_top, "");
2270 if (kind == Continuation::thaw_top && chunk->lockstack_size() > 0) {
2271 thaw_lockstack(chunk);
2272 retry_fast_path = true;
2273 }
2274
2275 // Retry the fast path now that we possibly cleared the FLAG_HAS_LOCKSTACK
2276 // and FLAG_PREEMPTED flags from the stackChunk.
2277 if (retry_fast_path && can_thaw_fast(chunk)) {
2278 intptr_t* sp = thaw_fast<true>(chunk);
2279 if (_preempted_case) {
2280 return handle_preempted_continuation(sp, preempt_kind, true /* fast_case */);
2281 }
2325
2326 intptr_t* sp = caller.sp();
2327
2328 if (_preempted_case) {
2329 return handle_preempted_continuation(sp, preempt_kind, false /* fast_case */);
2330 }
2331 return sp;
2332 }
2333
2334 void ThawBase::recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case) {
2335 log_develop_debug(continuations)("thaw num_frames: %d", num_frames);
2336 assert(!_cont.is_empty(), "no more frames");
2337 assert(num_frames > 0, "");
2338 assert(!heap_frame.is_empty(), "");
2339
2340 if (top_on_preempt_case && (heap_frame.is_native_frame() || heap_frame.is_runtime_frame())) {
2341 heap_frame.is_native_frame() ? recurse_thaw_native_frame(heap_frame, caller, 2) : recurse_thaw_stub_frame(heap_frame, caller, 2);
2342 } else if (!heap_frame.is_interpreted_frame()) {
2343 recurse_thaw_compiled_frame(heap_frame, caller, num_frames, false);
2344 } else {
2345 recurse_thaw_interpreted_frame(heap_frame, caller, num_frames);
2346 }
2347 }
2348
2349 template<typename FKind>
2350 bool ThawBase::recurse_thaw_java_frame(frame& caller, int num_frames) {
2351 assert(num_frames > 0, "");
2352
2353 DEBUG_ONLY(_frames++;)
2354
2355 int argsize = _stream.stack_argsize();
2356
2357 _stream.next(SmallRegisterMap::instance());
2358 assert(_stream.to_frame().is_empty() == _stream.is_done(), "");
2359
2360 // we never leave a compiled caller of an interpreted frame as the top frame in the chunk
2361 // as it makes detecting that situation and adjusting unextended_sp tricky
2362 if (num_frames == 1 && !_stream.is_done() && FKind::interpreted && _stream.is_compiled()) {
2363 log_develop_trace(continuations)("thawing extra compiled frame to not leave a compiled interpreted-caller at top");
2364 num_frames++;
2365 }
2366
2367 if (num_frames == 1 || _stream.is_done()) { // end recursion
2368 finalize_thaw(caller, FKind::interpreted ? 0 : argsize);
2369 return true; // bottom
2370 } else { // recurse
2371 recurse_thaw(_stream.to_frame(), caller, num_frames - 1, false /* top_on_preempt_case */);
2372 return false;
2373 }
2374 }
2375
2376 void ThawBase::finalize_thaw(frame& entry, int argsize) {
2377 stackChunkOop chunk = _cont.tail();
2442
2443 void ThawBase::clear_bitmap_bits(address start, address end) {
2444 assert(is_aligned(start, wordSize), "should be aligned: " PTR_FORMAT, p2i(start));
2445 assert(is_aligned(end, VMRegImpl::stack_slot_size), "should be aligned: " PTR_FORMAT, p2i(end));
2446
2447 // we need to clear the bits that correspond to arguments as they reside in the caller frame
2448 // or they will keep objects that are otherwise unreachable alive.
2449
2450 // Align `end` if UseCompressedOops is not set to avoid UB when calculating the bit index, since
2451 // `end` could be at an odd number of stack slots from `start`, i.e might not be oop aligned.
2452 // If that's the case the bit range corresponding to the last stack slot should not have bits set
2453 // anyways and we assert that before returning.
2454 address effective_end = UseCompressedOops ? end : align_down(end, wordSize);
2455 log_develop_trace(continuations)("clearing bitmap for " INTPTR_FORMAT " - " INTPTR_FORMAT, p2i(start), p2i(effective_end));
2456 stackChunkOop chunk = _cont.tail();
2457 chunk->bitmap().clear_range(chunk->bit_index_for(start), chunk->bit_index_for(effective_end));
2458 assert(effective_end == end || !chunk->bitmap().at(chunk->bit_index_for(effective_end)), "bit should not be set");
2459 }
2460
2461 intptr_t* ThawBase::handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case) {
2462 assert(preempt_kind == Continuation::freeze_on_wait || preempt_kind == Continuation::freeze_on_monitorenter, "");
2463 frame top(sp);
2464 assert(top.pc() == *(address*)(sp - frame::sender_sp_ret_address_offset()), "");
2465
2466 #if INCLUDE_JVMTI
2467 // Finish the VTMS transition.
2468 assert(_thread->is_in_VTMS_transition(), "must be");
2469 bool is_vthread = Continuation::continuation_scope(_cont.continuation()) == java_lang_VirtualThread::vthread_scope();
2470 if (is_vthread) {
2471 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
2472 jvmti_mount_end(_thread, _cont, top);
2473 } else {
2474 _thread->set_is_in_VTMS_transition(false);
2475 java_lang_Thread::set_is_in_VTMS_transition(_thread->vthread(), false);
2476 }
2477 }
2478 #endif
2479
2480 if (fast_case) {
2481 // If we thawed in the slow path the runtime stub/native wrapper frame already
2482 // has the correct fp (see ThawBase::new_stack_frame). On the fast path though,
2483 // we copied the original fp at the time of freeze which now will have to be fixed.
2484 assert(top.is_runtime_frame() || top.is_native_frame(), "");
2485 int fsize = top.cb()->frame_size();
2486 patch_pd(top, sp + fsize);
2487 }
2488
2489 if (preempt_kind == Continuation::freeze_on_wait) {
2490 // Check now if we need to throw IE exception.
2491 if (_thread->pending_interrupted_exception()) {
2492 throw_interrupted_exception(_thread, top);
2493 _thread->set_pending_interrupted_exception(false);
2494 }
2495 } else if (top.is_runtime_frame()) {
2496 // The continuation might now run on a different platform thread than the previous time so
2497 // we need to adjust the current thread saved in the stub frame before restoring registers.
2498 JavaThread** thread_addr = frame::saved_thread_address(top);
2499 if (thread_addr != nullptr) *thread_addr = _thread;
2500 }
2501 return sp;
2502 }
2503
2504 void ThawBase::throw_interrupted_exception(JavaThread* current, frame& top) {
2505 ContinuationWrapper::SafepointOp so(current, _cont);
2506 // Since we might safepoint set the anchor so that the stack can be walked.
2507 set_anchor(current, top.sp());
2508 JRT_BLOCK
2509 THROW(vmSymbols::java_lang_InterruptedException());
2510 JRT_BLOCK_END
2511 clear_anchor(current);
2512 }
2513
2514 NOINLINE void ThawBase::recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames) {
2515 assert(hf.is_interpreted_frame(), "");
2516
2517 if (UNLIKELY(seen_by_gc())) {
2518 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance());
2519 }
2520
2521 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::InterpretedFrame>(caller, num_frames);
2522
2523 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2524
2525 _align_size += frame::align_wiggle; // possible added alignment for internal interpreted frame alignment om AArch64
2526
2527 frame f = new_stack_frame<ContinuationHelper::InterpretedFrame>(hf, caller, is_bottom_frame);
2528
2529 intptr_t* const stack_frame_top = f.sp() + frame::metadata_words_at_top;
2530 intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
2531 intptr_t* const heap_frame_top = hf.unextended_sp() + frame::metadata_words_at_top;
2532 intptr_t* const heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
2533
2534 assert(hf.is_heap_frame(), "should be");
2535 assert(!f.is_heap_frame(), "should not be");
2536
2537 const int fsize = pointer_delta_as_int(heap_frame_bottom, heap_frame_top);
2538 assert((stack_frame_bottom == stack_frame_top + fsize), "");
2543
2544 // Make sure the relativized locals is already set.
2545 assert(f.interpreter_frame_local_at(0) == stack_frame_bottom - 1, "invalid frame bottom");
2546
2547 derelativize_interpreted_frame_metadata(hf, f);
2548 patch(f, caller, is_bottom_frame);
2549
2550 assert(f.is_interpreted_frame_valid(_cont.thread()), "invalid thawed frame");
2551 assert(stack_frame_bottom <= ContinuationHelper::Frame::frame_top(caller), "");
2552
2553 CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();)
2554
2555 maybe_set_fastpath(f.sp());
2556
2557 Method* m = hf.interpreter_frame_method();
2558 assert(!m->is_native() || !is_bottom_frame, "should be top frame of thaw_top case; missing caller frame");
2559 const int locals = m->max_locals();
2560
2561 if (!is_bottom_frame) {
2562 // can only fix caller once this frame is thawed (due to callee saved regs)
2563 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance());
2564 } else if (_cont.tail()->has_bitmap() && locals > 0) {
2565 assert(hf.is_heap_frame(), "should be");
2566 address start = (address)(heap_frame_bottom - locals);
2567 address end = (address)heap_frame_bottom;
2568 clear_bitmap_bits(start, end);
2569 }
2570
2571 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2572 caller = f;
2573 }
2574
2575 void ThawBase::recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller) {
2576 assert(hf.is_compiled_frame(), "");
2577 assert(_preempted_case || !stub_caller, "stub caller not at preemption");
2578
2579 if (!stub_caller && UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2580 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance());
2581 }
2582
2583 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::CompiledFrame>(caller, num_frames);
2584
2585 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2586
2587 assert(caller.sp() == caller.unextended_sp(), "");
2588
2589 if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) {
2590 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_compiled_frame
2591 }
2592
2593 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2594 // yet laid out in the stack, and so the original_pc is not stored in it.
2595 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2596 frame f = new_stack_frame<ContinuationHelper::CompiledFrame>(hf, caller, is_bottom_frame);
2597 intptr_t* const stack_frame_top = f.sp();
2598 intptr_t* const heap_frame_top = hf.unextended_sp();
2599
2600 const int added_argsize = (is_bottom_frame || caller.is_interpreted_frame()) ? hf.compiled_frame_stack_argsize() : 0;
2619 assert(!f.is_deoptimized_frame(), "");
2620 if (hf.is_deoptimized_frame()) {
2621 maybe_set_fastpath(f.sp());
2622 } else if (_thread->is_interp_only_mode()
2623 || (stub_caller && f.cb()->as_nmethod()->is_marked_for_deoptimization())) {
2624 // The caller of the safepoint stub when the continuation is preempted is not at a call instruction, and so
2625 // cannot rely on nmethod patching for deopt.
2626 assert(_thread->is_interp_only_mode() || stub_caller, "expected a stub-caller");
2627
2628 log_develop_trace(continuations)("Deoptimizing thawed frame");
2629 DEBUG_ONLY(ContinuationHelper::Frame::patch_pc(f, nullptr));
2630
2631 f.deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2632 assert(f.is_deoptimized_frame(), "");
2633 assert(ContinuationHelper::Frame::is_deopt_return(f.raw_pc(), f), "");
2634 maybe_set_fastpath(f.sp());
2635 }
2636
2637 if (!is_bottom_frame) {
2638 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2639 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance());
2640 } else if (_cont.tail()->has_bitmap() && added_argsize > 0) {
2641 address start = (address)(heap_frame_top + ContinuationHelper::CompiledFrame::size(hf) + frame::metadata_words_at_top);
2642 int stack_args_slots = f.cb()->as_nmethod()->num_stack_arg_slots(false /* rounded */);
2643 int argsize_in_bytes = stack_args_slots * VMRegImpl::stack_slot_size;
2644 clear_bitmap_bits(start, start + argsize_in_bytes);
2645 }
2646
2647 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2648 caller = f;
2649 }
2650
2651 void ThawBase::recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames) {
2652 DEBUG_ONLY(_frames++;)
2653
2654 if (UNLIKELY(seen_by_gc())) {
2655 // Process the stub's caller here since we might need the full map.
2656 RegisterMap map(nullptr,
2657 RegisterMap::UpdateMap::include,
2658 RegisterMap::ProcessFrames::skip,
2659 RegisterMap::WalkContinuation::skip);
2660 map.set_include_argument_oops(false);
2661 _stream.next(&map);
2662 assert(!_stream.is_done(), "");
2663 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, &map);
2664 } else {
2665 _stream.next(SmallRegisterMap::instance());
2666 assert(!_stream.is_done(), "");
2667 }
2668
2669 recurse_thaw_compiled_frame(_stream.to_frame(), caller, num_frames, true);
2670
2671 assert(caller.is_compiled_frame(), "");
2672 assert(caller.sp() == caller.unextended_sp(), "");
2673
2674 DEBUG_ONLY(before_thaw_java_frame(hf, caller, false /*is_bottom_frame*/, num_frames);)
2675
2676 frame f = new_stack_frame<ContinuationHelper::StubFrame>(hf, caller, false);
2677 intptr_t* stack_frame_top = f.sp();
2678 intptr_t* heap_frame_top = hf.sp();
2679 int fsize = ContinuationHelper::StubFrame::size(hf);
2680
2681 copy_from_chunk(heap_frame_top - frame::metadata_words, stack_frame_top - frame::metadata_words,
2682 fsize + frame::metadata_words);
2683
2684 patch(f, caller, false /*is_bottom_frame*/);
2685
2686 // can only fix caller once this frame is thawed (due to callee saved regs)
2687 RegisterMap map(nullptr,
2688 RegisterMap::UpdateMap::include,
2689 RegisterMap::ProcessFrames::skip,
2690 RegisterMap::WalkContinuation::skip);
2691 map.set_include_argument_oops(false);
2692 f.oop_map()->update_register_map(&f, &map);
2693 ContinuationHelper::update_register_map_with_callee(caller, &map);
2694 _cont.tail()->fix_thawed_frame(caller, &map);
2695
2696 DEBUG_ONLY(after_thaw_java_frame(f, false /*is_bottom_frame*/);)
2697 caller = f;
2698 }
2699
2700 void ThawBase::recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames) {
2701 assert(hf.is_native_frame(), "");
2702 assert(_preempted_case && hf.cb()->as_nmethod()->method()->is_object_wait0(), "");
2703
2704 if (UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2705 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance());
2706 }
2707
2708 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::NativeFrame>(caller, num_frames);
2709 assert(!is_bottom_frame, "");
2710
2711 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2712
2713 assert(caller.sp() == caller.unextended_sp(), "");
2714
2715 if (caller.is_interpreted_frame()) {
2716 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_native_frame
2717 }
2718
2719 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2720 // yet laid out in the stack, and so the original_pc is not stored in it.
2721 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2722 frame f = new_stack_frame<ContinuationHelper::NativeFrame>(hf, caller, false /* bottom */);
2723 intptr_t* const stack_frame_top = f.sp();
2724 intptr_t* const heap_frame_top = hf.unextended_sp();
2725
2726 int fsize = ContinuationHelper::NativeFrame::size(hf);
2727 assert(fsize <= (int)(caller.unextended_sp() - f.unextended_sp()), "");
2728
2729 intptr_t* from = heap_frame_top - frame::metadata_words_at_bottom;
2730 intptr_t* to = stack_frame_top - frame::metadata_words_at_bottom;
2731 int sz = fsize + frame::metadata_words_at_bottom;
2732
2733 copy_from_chunk(from, to, sz); // copying good oops because we invoked barriers above
2734
2735 patch(f, caller, false /* bottom */);
2736
2737 // f.is_deoptimized_frame() is always false and we must test hf.is_deoptimized_frame() (see comment above)
2738 assert(!f.is_deoptimized_frame(), "");
2739 assert(!hf.is_deoptimized_frame(), "");
2740 assert(!f.cb()->as_nmethod()->is_marked_for_deoptimization(), "");
2741
2742 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2743 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance());
2744
2745 DEBUG_ONLY(after_thaw_java_frame(f, false /* bottom */);)
2746 caller = f;
2747 }
2748
2749 void ThawBase::finish_thaw(frame& f) {
2750 stackChunkOop chunk = _cont.tail();
2751
2752 if (chunk->is_empty()) {
2753 // Only remove chunk from list if it can't be reused for another freeze
2754 if (seen_by_gc()) {
2755 _cont.set_tail(chunk->parent());
2756 } else {
2757 chunk->set_has_mixed_frames(false);
2758 }
2759 chunk->set_max_thawing_size(0);
2760 } else {
2761 chunk->set_max_thawing_size(chunk->max_thawing_size() - _align_size);
2762 }
2763 assert(chunk->is_empty() == (chunk->max_thawing_size() == 0), "");
2764
2765 if (!is_aligned(f.sp(), frame::frame_alignment)) {
2766 assert(f.is_interpreted_frame(), "");
2767 f.set_sp(align_down(f.sp(), frame::frame_alignment));
2768 }
2769 push_return_frame(f);
2770 chunk->fix_thawed_frame(f, SmallRegisterMap::instance()); // can only fix caller after push_return_frame (due to callee saved regs)
2771
2772 assert(_cont.is_empty() == _cont.last_frame().is_empty(), "");
2773
2774 log_develop_trace(continuations)("thawed %d frames", _frames);
2775
2776 LogTarget(Trace, continuations) lt;
2777 if (lt.develop_is_enabled()) {
2778 LogStream ls(lt);
2779 ls.print_cr("top hframe after (thaw):");
2780 _cont.last_frame().print_value_on(&ls);
2781 }
2782 }
2783
2784 void ThawBase::push_return_frame(frame& f) { // see generate_cont_thaw
2785 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == f.cb()->as_nmethod()->is_deopt_pc(f.raw_pc()), "");
2786 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == (f.pc() != f.raw_pc()), "");
2787
2788 LogTarget(Trace, continuations) lt;
2789 if (lt.develop_is_enabled()) {
2790 LogStream ls(lt);
2812
2813 ContinuationEntry* entry = thread->last_continuation();
2814 assert(entry != nullptr, "");
2815 oop oopCont = entry->cont_oop(thread);
2816
2817 assert(!jdk_internal_vm_Continuation::done(oopCont), "");
2818 assert(oopCont == get_continuation(thread), "");
2819 verify_continuation(oopCont);
2820
2821 assert(entry->is_virtual_thread() == (entry->scope(thread) == java_lang_VirtualThread::vthread_scope()), "");
2822
2823 ContinuationWrapper cont(thread, oopCont);
2824 log_develop_debug(continuations)("THAW #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
2825
2826 #ifdef ASSERT
2827 set_anchor_to_entry(thread, cont.entry());
2828 log_frames(thread);
2829 clear_anchor(thread);
2830 #endif
2831
2832 DEBUG_ONLY(bool preempted = cont.tail()->preempted();)
2833 Thaw<ConfigT> thw(thread, cont);
2834 intptr_t* const sp = thw.thaw(kind);
2835 assert(is_aligned(sp, frame::frame_alignment), "");
2836 DEBUG_ONLY(log_frames_after_thaw(thread, cont, sp, preempted);)
2837
2838 CONT_JFR_ONLY(thw.jfr_info().post_jfr_event(&event, cont.continuation(), thread);)
2839
2840 verify_continuation(cont.continuation());
2841 log_develop_debug(continuations)("=== End of thaw #" INTPTR_FORMAT, cont.hash());
2842
2843 return sp;
2844 }
2845
2846 #ifdef ASSERT
2847 static void do_deopt_after_thaw(JavaThread* thread) {
2848 int i = 0;
2849 StackFrameStream fst(thread, true, false);
2850 fst.register_map()->set_include_argument_oops(false);
2851 ContinuationHelper::update_register_map_with_callee(*fst.current(), fst.register_map());
2852 for (; !fst.is_done(); fst.next()) {
2853 if (fst.current()->cb()->is_nmethod()) {
2854 nmethod* nm = fst.current()->cb()->as_nmethod();
2855 if (!nm->method()->is_continuation_native_intrinsic()) {
2856 nm->make_deoptimized();
2913 if (!fr.is_interpreted_frame()) {
2914 st->print_cr("size: %d argsize: %d",
2915 ContinuationHelper::NonInterpretedUnknownFrame::size(fr),
2916 ContinuationHelper::NonInterpretedUnknownFrame::stack_argsize(fr));
2917 }
2918 VMReg reg = fst.register_map()->find_register_spilled_here(cl.p(), fst.current()->sp());
2919 if (reg != nullptr) {
2920 st->print_cr("Reg %s %d", reg->name(), reg->is_stack() ? (int)reg->reg2stack() : -99);
2921 }
2922 cl.reset();
2923 DEBUG_ONLY(thread->print_frame_layout();)
2924 if (chunk != nullptr) {
2925 chunk->print_on(true, st);
2926 }
2927 return false;
2928 }
2929 }
2930 return true;
2931 }
2932
2933 static void log_frames(JavaThread* thread) {
2934 const static int show_entry_callers = 3;
2935 LogTarget(Trace, continuations) lt;
2936 if (!lt.develop_is_enabled()) {
2937 return;
2938 }
2939 LogStream ls(lt);
2940
2941 ls.print_cr("------- frames --------- for thread " INTPTR_FORMAT, p2i(thread));
2942 if (!thread->has_last_Java_frame()) {
2943 ls.print_cr("NO ANCHOR!");
2944 }
2945
2946 RegisterMap map(thread,
2947 RegisterMap::UpdateMap::include,
2948 RegisterMap::ProcessFrames::include,
2949 RegisterMap::WalkContinuation::skip);
2950 map.set_include_argument_oops(false);
2951
2952 if (false) {
2953 for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) {
2954 f.print_on(&ls);
2955 }
2956 } else {
2958 ResetNoHandleMark rnhm;
2959 ResourceMark rm;
2960 HandleMark hm(Thread::current());
2961 FrameValues values;
2962
2963 int i = 0;
2964 int post_entry = -1;
2965 for (frame f = thread->last_frame(); !f.is_first_frame(); f = f.sender(&map), i++) {
2966 f.describe(values, i, &map, i == 0);
2967 if (post_entry >= 0 || Continuation::is_continuation_enterSpecial(f))
2968 post_entry++;
2969 if (post_entry >= show_entry_callers)
2970 break;
2971 }
2972 values.print_on(thread, &ls);
2973 }
2974
2975 ls.print_cr("======= end frames =========");
2976 }
2977
2978 static void log_frames_after_thaw(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp, bool preempted) {
2979 intptr_t* sp0 = sp;
2980 address pc0 = *(address*)(sp - frame::sender_sp_ret_address_offset());
2981
2982 if (preempted && sp0 == cont.entrySP()) {
2983 // Still preempted (monitor not acquired) so no frames were thawed.
2984 assert(cont.tail()->preempted(), "");
2985 set_anchor(thread, cont.entrySP(), cont.entryPC());
2986 } else {
2987 set_anchor(thread, sp0);
2988 }
2989
2990 log_frames(thread);
2991 if (LoomVerifyAfterThaw) {
2992 assert(do_verify_after_thaw(thread, cont.tail(), tty), "");
2993 }
2994 assert(ContinuationEntry::assert_entry_frame_laid_out(thread), "");
2995 clear_anchor(thread);
2996
2997 LogTarget(Trace, continuations) lt;
2998 if (lt.develop_is_enabled()) {
2999 LogStream ls(lt);
3000 ls.print_cr("Jumping to frame (thaw):");
3001 frame(sp).print_value_on(&ls);
3002 }
3003 }
3004 #endif // ASSERT
3005
3006 #include CPU_HEADER_INLINE(continuationFreezeThaw)
3007
3008 #ifdef ASSERT
3009 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st) {
3010 ResourceMark rm;
3011 FrameValues values;
3012 assert(f.get_cb() != nullptr, "");
3013 RegisterMap map(f.is_heap_frame() ?
3014 nullptr :
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/javaClasses.inline.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "code/codeCache.inline.hpp"
28 #include "code/nmethod.inline.hpp"
29 #include "code/vmreg.inline.hpp"
30 #include "compiler/oopMap.inline.hpp"
31 #include "gc/shared/continuationGCSupport.inline.hpp"
32 #include "gc/shared/gc_globals.hpp"
33 #include "gc/shared/barrierSet.hpp"
34 #include "gc/shared/memAllocator.hpp"
35 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
36 #include "interpreter/bytecodeStream.hpp"
37 #include "interpreter/interpreter.hpp"
38 #include "interpreter/interpreterRuntime.hpp"
39 #include "jfr/jfrEvents.hpp"
40 #include "logging/log.hpp"
41 #include "logging/logStream.hpp"
42 #include "oops/access.inline.hpp"
43 #include "oops/constantPool.inline.hpp"
44 #include "oops/method.inline.hpp"
45 #include "oops/oopsHierarchy.hpp"
46 #include "oops/objArrayOop.inline.hpp"
47 #include "oops/stackChunkOop.inline.hpp"
48 #include "prims/jvmtiThreadState.hpp"
49 #include "runtime/arguments.hpp"
50 #include "runtime/continuation.hpp"
51 #include "runtime/continuationEntry.inline.hpp"
52 #include "runtime/continuationHelper.inline.hpp"
53 #include "runtime/continuationJavaClasses.inline.hpp"
54 #include "runtime/continuationWrapper.inline.hpp"
55 #include "runtime/frame.inline.hpp"
56 #include "runtime/interfaceSupport.inline.hpp"
57 #include "runtime/javaThread.inline.hpp"
58 #include "runtime/jniHandles.inline.hpp"
59 #include "runtime/keepStackGCProcessed.hpp"
60 #include "runtime/objectMonitor.inline.hpp"
61 #include "runtime/orderAccess.hpp"
62 #include "runtime/prefetch.inline.hpp"
63 #include "runtime/smallRegisterMap.inline.hpp"
64 #include "runtime/sharedRuntime.hpp"
65 #include "runtime/stackChunkFrameStream.inline.hpp"
66 #include "runtime/stackFrameStream.inline.hpp"
67 #include "runtime/stackOverflow.hpp"
68 #include "runtime/stackWatermarkSet.inline.hpp"
69 #include "runtime/vframe.inline.hpp"
70 #include "runtime/vframe_hp.hpp"
71 #include "utilities/debug.hpp"
72 #include "utilities/exceptions.hpp"
73 #include "utilities/macros.hpp"
74 #include "utilities/vmError.hpp"
75 #if INCLUDE_ZGC
76 #include "gc/z/zStackChunkGCData.inline.hpp"
77 #endif
78 #ifdef COMPILER1
79 #include "c1/c1_Runtime1.hpp"
80 #endif
81 #ifdef COMPILER2
82 #include "opto/runtime.hpp"
83 #endif
84
85 #include <type_traits>
86
87 /*
88 * This file contains the implementation of continuation freezing (yield) and thawing (run).
89 *
90 * This code is very latency-critical and very hot. An ordinary and well-behaved server application
91 * would likely call these operations many thousands of times per second second, on every core.
92 *
93 * Freeze might be called every time the application performs any I/O operation, every time it
94 * acquires a j.u.c. lock, every time it takes a message from a queue, and thaw can be called
95 * multiple times in each of those cases, as it is called by the return barrier, which may be
96 * invoked on method return.
97 *
98 * The amortized budget for each of those two operations is ~100-150ns. That is why, for
99 * example, every effort is made to avoid Java-VM transitions as much as possible.
100 *
101 * On the fast path, all frames are known to be compiled, and the chunk requires no barriers
102 * and so frames simply copied, and the bottom-most one is patched.
103 * On the slow path, internal pointers in interpreted frames are de/relativized to/from offsets
173 #endif
174
175 // TODO: See AbstractAssembler::generate_stack_overflow_check,
176 // Compile::bang_size_in_bytes(), m->as_SafePoint()->jvms()->interpreter_frame_size()
177 // when we stack-bang, we need to update a thread field with the lowest (farthest) bang point.
178
179 // Data invariants are defined by Continuation::debug_verify_continuation and Continuation::debug_verify_stack_chunk
180
181 // Used to just annotatate cold/hot branches
182 #define LIKELY(condition) (condition)
183 #define UNLIKELY(condition) (condition)
184
185 // debugging functions
186 #ifdef ASSERT
187 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue); // address p is readable and *(intptr_t*)p != errvalue
188
189 static void verify_continuation(oop continuation) { Continuation::debug_verify_continuation(continuation); }
190
191 static void do_deopt_after_thaw(JavaThread* thread);
192 static bool do_verify_after_thaw(JavaThread* thread, stackChunkOop chunk, outputStream* st);
193 static void log_frames(JavaThread* thread, bool dolog = false);
194 static void log_frames_after_thaw(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp);
195 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st = tty);
196 static void verify_frame_kind(const frame& top, Continuation::preempt_kind preempt_kind, Method** m_ptr = nullptr, const char** code_name_ptr = nullptr, int* bci_ptr = nullptr);
197
198 #define assert_pfl(p, ...) \
199 do { \
200 if (!(p)) { \
201 JavaThread* t = JavaThread::active(); \
202 if (t->has_last_Java_frame()) { \
203 tty->print_cr("assert(" #p ") failed:"); \
204 t->print_frame_layout(); \
205 } \
206 } \
207 vmassert(p, __VA_ARGS__); \
208 } while(0)
209
210 #else
211 static void verify_continuation(oop continuation) { }
212 #define assert_pfl(p, ...)
213 #endif
214
215 static freeze_result is_pinned0(JavaThread* thread, oop cont_scope, bool safepoint);
216 template<typename ConfigT, bool preempt> static inline freeze_result freeze_internal(JavaThread* current, intptr_t* const sp);
504
505 assert(!Interpreter::contains(_cont.entryPC()), "");
506
507 _bottom_address = _cont.entrySP() - _cont.entry_frame_extension();
508 #ifdef _LP64
509 if (((intptr_t)_bottom_address & 0xf) != 0) {
510 _bottom_address--;
511 }
512 assert(is_aligned(_bottom_address, frame::frame_alignment), "");
513 #endif
514
515 log_develop_trace(continuations)("bottom_address: " INTPTR_FORMAT " entrySP: " INTPTR_FORMAT " argsize: " PTR_FORMAT,
516 p2i(_bottom_address), p2i(_cont.entrySP()), (_cont.entrySP() - _bottom_address) << LogBytesPerWord);
517 assert(_bottom_address != nullptr, "");
518 assert(_bottom_address <= _cont.entrySP(), "");
519 DEBUG_ONLY(_last_write = nullptr;)
520
521 assert(_cont.chunk_invariant(), "");
522 assert(!Interpreter::contains(_cont.entryPC()), "");
523 #if !defined(PPC64) || defined(ZERO)
524 static const int do_yield_frame_size = frame::metadata_words;
525 #else
526 static const int do_yield_frame_size = frame::native_abi_reg_args_size >> LogBytesPerWord;
527 #endif
528 // With preemption doYield() might not have been resolved yet
529 assert(_preempt || ContinuationEntry::do_yield_nmethod()->frame_size() == do_yield_frame_size, "");
530
531 if (preempt) {
532 _last_frame = _thread->last_frame();
533 }
534
535 // properties of the continuation on the stack; all sizes are in words
536 _cont_stack_top = frame_sp + (!preempt ? do_yield_frame_size : 0); // we don't freeze the doYield stub frame
537 _cont_stack_bottom = _cont.entrySP() + (_cont.argsize() == 0 ? frame::metadata_words_at_top : 0)
538 - ContinuationHelper::frame_align_words(_cont.argsize()); // see alignment in thaw
539
540 log_develop_trace(continuations)("freeze size: %d argsize: %d top: " INTPTR_FORMAT " bottom: " INTPTR_FORMAT,
541 cont_size(), _cont.argsize(), p2i(_cont_stack_top), p2i(_cont_stack_bottom));
542 assert(cont_size() > 0, "");
543
544 if (LockingMode != LM_LIGHTWEIGHT) {
545 _monitors_in_lockstack = 0;
546 } else {
547 _monitors_in_lockstack = _thread->lock_stack().monitor_count();
548 }
549 }
550
551 void FreezeBase::init_rest() { // we want to postpone some initialization after chunk handling
552 _freeze_size = 0;
553 _total_align_size = 0;
554 NOT_PRODUCT(_frames = 0;)
555 }
556
889 freeze_result res = recurse_freeze(f, caller, 0, false, true);
890
891 if (res == freeze_ok) {
892 finish_freeze(f, caller);
893 _cont.write();
894 }
895
896 return res;
897 }
898
899 frame FreezeBase::freeze_start_frame() {
900 if (LIKELY(!_preempt)) {
901 return freeze_start_frame_yield_stub();
902 } else {
903 return freeze_start_frame_on_preempt();
904 }
905 }
906
907 frame FreezeBase::freeze_start_frame_yield_stub() {
908 frame f = _thread->last_frame();
909 assert(ContinuationEntry::do_yield_nmethod()->contains(f.pc()), "must be");
910 f = sender<ContinuationHelper::NonInterpretedUnknownFrame>(f);
911 assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), f), "");
912 return f;
913 }
914
915 frame FreezeBase::freeze_start_frame_on_preempt() {
916 assert(_last_frame.sp() == _thread->last_frame().sp(), "_last_frame should be already initialized");
917 assert(Continuation::is_frame_in_continuation(_thread->last_continuation(), _last_frame), "");
918 return _last_frame;
919 }
920
921 // The parameter callee_argsize includes metadata that has to be part of caller/callee overlap.
922 NOINLINE freeze_result FreezeBase::recurse_freeze(frame& f, frame& caller, int callee_argsize, bool callee_interpreted, bool top) {
923 assert(f.unextended_sp() < _bottom_address, ""); // see recurse_freeze_java_frame
924 assert(f.is_interpreted_frame() || ((top && _preempt) == ContinuationHelper::Frame::is_stub(f.cb()))
925 || ((top && _preempt) == f.is_native_frame()), "");
926
927 if (stack_overflow()) {
928 return freeze_exception;
929 }
1085 log_develop_trace(continuations)("Reusing chunk mixed: %d empty: %d", chunk->has_mixed_frames(), chunk->is_empty());
1086 if (chunk->is_empty()) {
1087 int sp = chunk->stack_size() - argsize_md;
1088 chunk->set_sp(sp);
1089 chunk->set_bottom(sp);
1090 _freeze_size += overlap;
1091 assert(chunk->max_thawing_size() == 0, "");
1092 } DEBUG_ONLY(else empty_chunk = false;)
1093 }
1094 assert(!chunk->is_gc_mode(), "");
1095 assert(!chunk->has_bitmap(), "");
1096 chunk->set_has_mixed_frames(true);
1097
1098 assert(chunk->requires_barriers() == _barriers, "");
1099 assert(!_barriers || chunk->is_empty(), "");
1100
1101 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).is_done(), "");
1102 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame().is_empty(), "");
1103
1104 if (_preempt) {
1105 frame top_frame = _thread->last_frame();
1106 if (top_frame.is_interpreted_frame()) {
1107 // Some platforms do not save the last_sp in the top interpreter frame on VM calls.
1108 // We need it so that on resume we can restore the sp to the right place, since
1109 // thawing might add an alignment word to the expression stack (see finish_thaw()).
1110 // We do it now that we know freezing will be successful.
1111 prepare_freeze_interpreted_top_frame(top_frame);
1112 }
1113
1114 // Do this now so should_process_args_at_top() is set before calling finish_freeze
1115 // in case we might need to apply GC barriers to frames in this stackChunk.
1116 if (_thread->at_preemptable_init()) {
1117 assert(top_frame.is_interpreted_frame(), "only InterpreterRuntime::_new/resolve_from_cache allowed");
1118 chunk->set_at_klass_init(true);
1119 Method* m = top_frame.interpreter_frame_method();
1120 Bytecode current_bytecode = Bytecode(m, top_frame.interpreter_frame_bcp());
1121 Bytecodes::Code code = current_bytecode.code();
1122 int exp_size = top_frame.interpreter_frame_expression_stack_size();
1123 if (code == Bytecodes::Code::_invokestatic && exp_size > 0) {
1124 chunk->set_has_args_at_top(true);
1125 }
1126 }
1127 }
1128
1129 // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
1130 // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
1131 // will either see no continuation or a consistent chunk.
1132 unwind_frames();
1133
1134 chunk->set_max_thawing_size(chunk->max_thawing_size() + _freeze_size - _monitors_in_lockstack - frame::metadata_words);
1135
1136 if (lt.develop_is_enabled()) {
1137 LogStream ls(lt);
1138 ls.print_cr("top chunk:");
1139 chunk->print_on(&ls);
1140 }
1141
1142 if (_monitors_in_lockstack > 0) {
1143 freeze_lockstack(chunk);
1144 }
1145
1617 // Some GCs could put direct allocations in old gen for slow-path
1618 // allocations; need to explicitly check if that was the case.
1619 _barriers = chunk->requires_barriers();
1620 }
1621 }
1622
1623 if (_barriers) {
1624 log_develop_trace(continuations)("allocation requires barriers");
1625 }
1626
1627 assert(chunk->parent() == nullptr || chunk->parent()->is_stackChunk(), "");
1628
1629 return chunk;
1630 }
1631
1632 void FreezeBase::throw_stack_overflow_on_humongous_chunk() {
1633 ContinuationWrapper::SafepointOp so(_thread, _cont); // could also call _cont.done() instead
1634 Exceptions::_throw_msg(_thread, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Humongous stack chunk");
1635 }
1636
1637 class AnchorMark : public StackObj {
1638 JavaThread* _current;
1639 frame& _top_frame;
1640 intptr_t* _last_sp_from_frame;
1641 bool _is_interpreted;
1642
1643 public:
1644 AnchorMark(JavaThread* current, frame& f) : _current(current), _top_frame(f), _is_interpreted(false) {
1645 intptr_t* sp = anchor_mark_set_pd();
1646 set_anchor(_current, sp);
1647 }
1648 ~AnchorMark() {
1649 clear_anchor(_current);
1650 anchor_mark_clear_pd();
1651 }
1652 inline intptr_t* anchor_mark_set_pd();
1653 inline void anchor_mark_clear_pd();
1654 };
1655
1656 #if INCLUDE_JVMTI
1657 static int num_java_frames(ContinuationWrapper& cont) {
1658 ResourceMark rm; // used for scope traversal in num_java_frames(nmethod*, address)
1659 int count = 0;
1660 for (stackChunkOop chunk = cont.tail(); chunk != nullptr; chunk = chunk->parent()) {
1661 count += chunk->num_java_frames();
1662 }
1663 return count;
1664 }
1665
1666 static void invalidate_jvmti_stack(JavaThread* thread) {
1667 if (thread->is_interp_only_mode()) {
1668 JvmtiThreadState *state = thread->jvmti_thread_state();
1669 if (state != nullptr)
1670 state->invalidate_cur_stack_depth();
1671 }
1672 }
1673
1674 static void jvmti_yield_cleanup(JavaThread* thread, ContinuationWrapper& cont) {
1675 if (JvmtiExport::can_post_frame_pop()) {
1676 int num_frames = num_java_frames(cont);
1677
1678 ContinuationWrapper::SafepointOp so(Thread::current(), cont);
1679 JvmtiExport::continuation_yield_cleanup(JavaThread::current(), num_frames);
1680 }
1681 invalidate_jvmti_stack(thread);
1682 }
1683
1684 static void jvmti_mount_end(JavaThread* current, ContinuationWrapper& cont, frame top, Continuation::preempt_kind pk) {
1685 assert(current->vthread() != nullptr, "must be");
1686
1687 HandleMarkCleaner hm(current); // Cleanup vth and so._conth Handles
1688 Handle vth(current, current->vthread());
1689 ContinuationWrapper::SafepointOp so(current, cont);
1690
1691 AnchorMark am(current, top); // Set anchor so that the stack is walkable.
1692
1693 JRT_BLOCK
1694 JvmtiVTMSTransitionDisabler::VTMS_vthread_mount((jthread)vth.raw_value(), false);
1695
1696 if (current->pending_contended_entered_event()) {
1697 // No monitor JVMTI events for ObjectLocker case.
1698 if (pk != Continuation::object_locker) {
1699 JvmtiExport::post_monitor_contended_entered(current, current->contended_entered_monitor());
1700 }
1701 current->set_contended_entered_monitor(nullptr);
1702 }
1703 JRT_BLOCK_END
1704 }
1705 #endif // INCLUDE_JVMTI
1706
1707 #ifdef ASSERT
1708 // There are no interpreted frames if we're not called from the interpreter and we haven't ancountered an i2c
1709 // adapter or called Deoptimization::unpack_frames. As for native frames, upcalls from JNI also go through the
1710 // interpreter (see JavaCalls::call_helper), while the UpcallLinker explicitly sets cont_fastpath.
1711 bool FreezeBase::check_valid_fast_path() {
1712 ContinuationEntry* ce = _thread->last_continuation();
1713 RegisterMap map(_thread,
1714 RegisterMap::UpdateMap::skip,
1715 RegisterMap::ProcessFrames::skip,
1716 RegisterMap::WalkContinuation::skip);
1717 map.set_include_argument_oops(false);
1718 bool is_top_frame = true;
1719 for (frame f = freeze_start_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map), is_top_frame = false) {
1720 if (!((f.is_compiled_frame() && !f.is_deoptimized_frame()) || (is_top_frame && (f.is_runtime_frame() || f.is_native_frame())))) {
1721 return false;
1722 }
1723 }
1724 return true;
1725 }
1726
1727 static void verify_frame_kind(const frame& top, Continuation::preempt_kind preempt_kind, Method** m_ptr, const char** code_name_ptr, int* bci_ptr) {
1728 JavaThread* current = JavaThread::current();
1729 ResourceMark rm(current);
1730
1731 Method* m;
1732 const char* code_name;
1733 int bci;
1734 if (preempt_kind == Continuation::monitorenter) {
1735 assert(top.is_interpreted_frame() || top.is_runtime_frame(), "");
1736 bool at_sync_method;
1737 if (top.is_interpreted_frame()) {
1738 m = top.interpreter_frame_method();
1739 assert(!m->is_native() || m->is_synchronized(), "invalid method %s", m->external_name());
1740 address bcp = top.interpreter_frame_bcp();
1741 assert(bcp != 0 || m->is_native(), "");
1742 at_sync_method = m->is_synchronized() && (bcp == 0 || bcp == m->code_base());
1743 // bcp is advanced on monitorenter before making the VM call, adjust for that.
1744 bool at_sync_bytecode = bcp > m->code_base() && Bytecode(m, bcp - 1).code() == Bytecodes::Code::_monitorenter;
1745 assert(at_sync_method || at_sync_bytecode, "");
1746 bci = at_sync_method ? -1 : top.interpreter_frame_bci();
1747 } else {
1748 CodeBlob* cb = top.cb();
1749 RegisterMap reg_map(current,
1750 RegisterMap::UpdateMap::skip,
1751 RegisterMap::ProcessFrames::skip,
1752 RegisterMap::WalkContinuation::skip);
1753 frame fr = top.sender(®_map);
1754 vframe* vf = vframe::new_vframe(&fr, ®_map, current);
1755 compiledVFrame* cvf = compiledVFrame::cast(vf);
1756 m = cvf->method();
1757 bci = cvf->scope()->bci();
1758 at_sync_method = bci == SynchronizationEntryBCI;
1759 assert(!at_sync_method || m->is_synchronized(), "bci is %d but method %s is not synchronized", bci, m->external_name());
1760 bool is_c1_monitorenter = false, is_c2_monitorenter = false;
1761 COMPILER1_PRESENT(is_c1_monitorenter = cb == Runtime1::blob_for(C1StubId::monitorenter_id) ||
1762 cb == Runtime1::blob_for(C1StubId::monitorenter_nofpu_id);)
1763 COMPILER2_PRESENT(is_c2_monitorenter = cb == CodeCache::find_blob(OptoRuntime::complete_monitor_locking_Java());)
1764 assert(is_c1_monitorenter || is_c2_monitorenter, "wrong runtime stub frame");
1765 }
1766 code_name = at_sync_method ? "synchronized method" : "monitorenter";
1767 } else if (preempt_kind == Continuation::object_wait) {
1768 assert(top.is_interpreted_frame() || top.is_native_frame(), "");
1769 m = top.is_interpreted_frame() ? top.interpreter_frame_method() : top.cb()->as_nmethod()->method();
1770 assert(m->is_object_wait0(), "");
1771 bci = 0;
1772 code_name = "";
1773 } else {
1774 assert(preempt_kind == Continuation::object_locker, "invalid preempt kind");
1775 assert(top.is_interpreted_frame(), "");
1776 m = top.interpreter_frame_method();
1777 Bytecode current_bytecode = Bytecode(m, top.interpreter_frame_bcp());
1778 Bytecodes::Code code = current_bytecode.code();
1779 assert(code == Bytecodes::Code::_new || code == Bytecodes::Code::_invokestatic ||
1780 (code == Bytecodes::Code::_getstatic || code == Bytecodes::Code::_putstatic), "invalid bytecode");
1781 bci = top.interpreter_frame_bci();
1782 code_name = Bytecodes::name(current_bytecode.code());
1783 }
1784 assert(bci >= 0 || m->is_synchronized(), "invalid bci:%d at method %s", bci, m->external_name());
1785
1786 if (m_ptr != nullptr) {
1787 *m_ptr = m;
1788 *code_name_ptr = code_name;
1789 *bci_ptr = bci;
1790 }
1791 }
1792
1793 static void log_preempt_after_freeze(ContinuationWrapper& cont) {
1794 JavaThread* current = cont.thread();
1795 StackChunkFrameStream<ChunkFrames::Mixed> sfs(cont.tail());
1796 frame top_frame = sfs.to_frame();
1797 bool at_init = current->at_preemptable_init();
1798 bool at_enter = current->current_pending_monitor() != nullptr;
1799 bool at_wait = current->current_waiting_monitor() != nullptr;
1800 assert((at_enter && !at_wait) || (!at_enter && at_wait), "");
1801 Continuation::preempt_kind pk = at_init ? Continuation::object_locker : at_enter ? Continuation::monitorenter : Continuation::object_wait;
1802
1803 Method* m = nullptr;
1804 const char* code_name = nullptr;
1805 int bci = InvalidFrameStateBci;
1806 verify_frame_kind(top_frame, pk, &m, &code_name, &bci);
1807 assert(m != nullptr && code_name != nullptr && bci != InvalidFrameStateBci, "should be set");
1808
1809 ResourceMark rm(current);
1810 if (bci < 0) {
1811 log_trace(continuations, preempt)("Preempted " INT64_FORMAT " while synchronizing on %smethod %s", current->monitor_owner_id(), m->is_native() ? "native " : "", m->external_name());
1812 } else if (m->is_object_wait0()) {
1813 log_trace(continuations, preempt)("Preempted " INT64_FORMAT " at native method %s", current->monitor_owner_id(), m->external_name());
1814 } else {
1815 Klass* k = current->preempt_init_klass();
1816 assert(k != nullptr || !at_init, "");
1817 log_trace(continuations, preempt)("Preempted " INT64_FORMAT " at %s(bci:%d) in method %s %s%s", current->monitor_owner_id(),
1818 code_name, bci, m->external_name(), at_init ? "trying to initialize klass " : "", at_init ? k->external_name() : "");
1819 }
1820 }
1821 #endif // ASSERT
1822
1823 static inline freeze_result freeze_epilog(ContinuationWrapper& cont) {
1824 verify_continuation(cont.continuation());
1825 assert(!cont.is_empty(), "");
1826
1827 log_develop_debug(continuations)("=== End of freeze cont ### #" INTPTR_FORMAT, cont.hash());
1828 return freeze_ok;
1829 }
1830
1831 static freeze_result freeze_epilog(JavaThread* thread, ContinuationWrapper& cont, freeze_result res) {
1832 if (UNLIKELY(res != freeze_ok)) {
1833 JFR_ONLY(thread->set_last_freeze_fail_result(res);)
1834 verify_continuation(cont.continuation());
1835 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1836 return res;
1837 }
1838
1839 JVMTI_ONLY(jvmti_yield_cleanup(thread, cont)); // can safepoint
1840 return freeze_epilog(cont);
1841 }
1842
1843 static freeze_result preempt_epilog(ContinuationWrapper& cont, freeze_result res, frame& old_last_frame) {
1844 if (UNLIKELY(res != freeze_ok)) {
1845 verify_continuation(cont.continuation());
1846 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1847 return res;
1848 }
1849
1850 // Set up things so that on return to Java we jump to preempt stub.
1851 patch_return_pc_with_preempt_stub(old_last_frame);
1852 cont.tail()->set_preempted(true);
1853 DEBUG_ONLY(log_preempt_after_freeze(cont);)
1854 return freeze_epilog(cont);
1855 }
1856
1857 template<typename ConfigT, bool preempt>
1858 static inline freeze_result freeze_internal(JavaThread* current, intptr_t* const sp) {
1859 assert(!current->has_pending_exception(), "");
1860
1861 #ifdef ASSERT
1862 log_trace(continuations)("~~~~ freeze sp: " INTPTR_FORMAT "JavaThread: " INTPTR_FORMAT, p2i(current->last_continuation()->entry_sp()), p2i(current));
1863 log_frames(current, false);
1864 #endif
1865
1866 CONT_JFR_ONLY(EventContinuationFreeze event;)
1867
1868 ContinuationEntry* entry = current->last_continuation();
1869
1870 oop oopCont = entry->cont_oop(current);
1871 assert(oopCont == current->last_continuation()->cont_oop(current), "");
1872 assert(ContinuationEntry::assert_entry_frame_laid_out(current), "");
1873
1874 verify_continuation(oopCont);
1875 ContinuationWrapper cont(current, oopCont);
1876 log_develop_debug(continuations)("FREEZE #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
1877
1878 assert(entry->is_virtual_thread() == (entry->scope(current) == java_lang_VirtualThread::vthread_scope()), "");
1879
1880 assert(LockingMode == LM_LEGACY || (current->held_monitor_count() == 0 && current->jni_monitor_count() == 0),
1881 "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());
1882
1883 if (entry->is_pinned() || current->held_monitor_count() > 0) {
2037 // 300 is an estimate for stack size taken for this native code, in addition to StackShadowPages
2038 // for the Java frames in the check below.
2039 if (!stack_overflow_check(thread, size + 300, bottom)) {
2040 return 0;
2041 }
2042
2043 log_develop_trace(continuations)("prepare_thaw bottom: " INTPTR_FORMAT " top: " INTPTR_FORMAT " size: %d",
2044 p2i(bottom), p2i(bottom - size), size);
2045 return size;
2046 }
2047
2048 class ThawBase : public StackObj {
2049 protected:
2050 JavaThread* _thread;
2051 ContinuationWrapper& _cont;
2052 CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;)
2053
2054 intptr_t* _fastpath;
2055 bool _barriers;
2056 bool _preempted_case;
2057 bool _process_args_at_top;
2058 intptr_t* _top_unextended_sp_before_thaw;
2059 int _align_size;
2060 DEBUG_ONLY(intptr_t* _top_stack_address);
2061
2062 // Only used for some preemption cases.
2063 ObjectMonitor* _monitor;
2064
2065 StackChunkFrameStream<ChunkFrames::Mixed> _stream;
2066
2067 NOT_PRODUCT(int _frames;)
2068
2069 protected:
2070 ThawBase(JavaThread* thread, ContinuationWrapper& cont) :
2071 _thread(thread), _cont(cont),
2072 _fastpath(nullptr) {
2073 DEBUG_ONLY(_top_unextended_sp_before_thaw = nullptr;)
2074 assert (cont.tail() != nullptr, "no last chunk");
2075 DEBUG_ONLY(_top_stack_address = _cont.entrySP() - thaw_size(cont.tail());)
2076 }
2077
2078 void clear_chunk(stackChunkOop chunk);
2079 template<bool check_stub>
2080 int remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize);
2081 void copy_from_chunk(intptr_t* from, intptr_t* to, int size);
2082
2083 void thaw_lockstack(stackChunkOop chunk);
2084
2085 // fast path
2086 inline void prefetch_chunk_pd(void* start, int size_words);
2087 void patch_return(intptr_t* sp, bool is_last);
2088
2089 intptr_t* handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case);
2090 inline intptr_t* push_cleanup_continuation();
2091 inline intptr_t* push_preempt_adapter();
2092 intptr_t* redo_vmcall(JavaThread* current, frame& top);
2093 void throw_interrupted_exception(JavaThread* current, frame& top);
2094
2095 void recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case);
2096 void finish_thaw(frame& f);
2097
2098 private:
2099 template<typename FKind> bool recurse_thaw_java_frame(frame& caller, int num_frames);
2100 void finalize_thaw(frame& entry, int argsize);
2101
2102 inline bool seen_by_gc();
2103
2104 inline void before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame);
2105 inline void after_thaw_java_frame(const frame& f, bool bottom);
2106 inline void patch(frame& f, const frame& caller, bool bottom);
2107 void clear_bitmap_bits(address start, address end);
2108
2109 NOINLINE void recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames, bool is_top);
2110 void recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller);
2111 void recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames);
2112 void recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames);
2113
2114 void push_return_frame(frame& f);
2115 inline frame new_entry_frame();
2116 template<typename FKind> frame new_stack_frame(const frame& hf, frame& caller, bool bottom);
2117 inline void patch_pd(frame& f, const frame& sender);
2118 inline void patch_pd(frame& f, intptr_t* caller_sp);
2119 inline intptr_t* align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom);
2120
2121 void maybe_set_fastpath(intptr_t* sp) { if (sp > _fastpath) _fastpath = sp; }
2122
2123 static inline void derelativize_interpreted_frame_metadata(const frame& hf, const frame& f);
2124
2125 public:
2126 CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
2127 };
2128
2129 template <typename ConfigT>
2189 chunk->set_sp(chunk->bottom());
2190 chunk->set_max_thawing_size(0);
2191 }
2192
2193 template<bool check_stub>
2194 int ThawBase::remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize) {
2195 bool empty = false;
2196 StackChunkFrameStream<ChunkFrames::CompiledOnly> f(chunk);
2197 DEBUG_ONLY(intptr_t* const chunk_sp = chunk->start_address() + chunk->sp();)
2198 assert(chunk_sp == f.sp(), "");
2199 assert(chunk_sp == f.unextended_sp(), "");
2200
2201 int frame_size = f.cb()->frame_size();
2202 argsize = f.stack_argsize();
2203
2204 assert(!f.is_stub() || check_stub, "");
2205 if (check_stub && f.is_stub()) {
2206 // If we don't thaw the top compiled frame too, after restoring the saved
2207 // registers back in Java, we would hit the return barrier to thaw one more
2208 // frame effectively overwriting the restored registers during that call.
2209 f.next(SmallRegisterMap::instance_no_args(), true /* stop */);
2210 assert(!f.is_done(), "");
2211
2212 f.get_cb();
2213 assert(f.is_compiled(), "");
2214 frame_size += f.cb()->frame_size();
2215 argsize = f.stack_argsize();
2216
2217 if (f.cb()->as_nmethod()->is_marked_for_deoptimization()) {
2218 // The caller of the runtime stub when the continuation is preempted is not at a
2219 // Java call instruction, and so cannot rely on nmethod patching for deopt.
2220 log_develop_trace(continuations)("Deoptimizing runtime stub caller");
2221 f.to_frame().deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2222 }
2223 }
2224
2225 f.next(SmallRegisterMap::instance_no_args(), true /* stop */);
2226 empty = f.is_done();
2227 assert(!empty || argsize == chunk->argsize(), "");
2228
2229 if (empty) {
2230 clear_chunk(chunk);
2231 } else {
2232 chunk->set_sp(chunk->sp() + frame_size);
2233 chunk->set_max_thawing_size(chunk->max_thawing_size() - frame_size);
2234 // We set chunk->pc to the return pc into the next frame
2235 chunk->set_pc(f.pc());
2236 #ifdef ASSERT
2237 {
2238 intptr_t* retaddr_slot = (chunk_sp
2239 + frame_size
2240 - frame::sender_sp_ret_address_offset());
2241 assert(f.pc() == ContinuationHelper::return_address_at(retaddr_slot),
2242 "unexpected pc");
2243 }
2244 #endif
2245 }
2365 return rs.sp();
2366 }
2367
2368 inline bool ThawBase::seen_by_gc() {
2369 return _barriers || _cont.tail()->is_gc_mode();
2370 }
2371
2372 static inline void relativize_chunk_concurrently(stackChunkOop chunk) {
2373 #if INCLUDE_ZGC || INCLUDE_SHENANDOAHGC
2374 if (UseZGC || UseShenandoahGC) {
2375 chunk->relativize_derived_pointers_concurrently();
2376 }
2377 #endif
2378 }
2379
2380 template <typename ConfigT>
2381 NOINLINE intptr_t* Thaw<ConfigT>::thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind) {
2382 Continuation::preempt_kind preempt_kind;
2383 bool retry_fast_path = false;
2384
2385 _process_args_at_top = false;
2386 _preempted_case = chunk->preempted();
2387 if (_preempted_case) {
2388 ObjectWaiter* waiter = java_lang_VirtualThread::objectWaiter(_thread->vthread());
2389 if (waiter != nullptr) {
2390 // Mounted again after preemption. Resume the pending monitor operation,
2391 // which will be either a monitorenter or Object.wait() call.
2392 ObjectMonitor* mon = waiter->monitor();
2393 preempt_kind = waiter->is_wait() ? Continuation::object_wait : Continuation::monitorenter;
2394
2395 bool mon_acquired = mon->resume_operation(_thread, waiter, _cont);
2396 assert(!mon_acquired || mon->has_owner(_thread), "invariant");
2397 if (!mon_acquired) {
2398 // Failed to acquire monitor. Return to enterSpecial to unmount again.
2399 log_trace(continuations, tracking)("Failed to acquire monitor, unmounting again");
2400 return push_cleanup_continuation();
2401 }
2402 _monitor = mon; // remember monitor since we might need it on handle_preempted_continuation()
2403 chunk = _cont.tail(); // reload oop in case of safepoint in resume_operation (if posting JVMTI events).
2404 } else {
2405 // Preemption cancelled in moniterenter case. We actually acquired
2406 // the monitor after freezing all frames so nothing to do. In case
2407 // of preemption on ObjectLocker during klass init, we released the
2408 // monitor already at ~ObjectLocker so here we just set _monitor to
2409 // nullptr so we know there is no need to release it later.
2410 preempt_kind = Continuation::monitorenter;
2411 _monitor = nullptr;
2412 }
2413
2414 // Call this first to avoid racing with GC threads later when modifying the chunk flags.
2415 relativize_chunk_concurrently(chunk);
2416
2417 if (chunk->at_klass_init()) {
2418 preempt_kind = Continuation::object_locker;
2419 chunk->set_at_klass_init(false);
2420 _process_args_at_top = chunk->has_args_at_top();
2421 if (_process_args_at_top) chunk->set_has_args_at_top(false);
2422 }
2423 chunk->set_preempted(false);
2424 retry_fast_path = true;
2425 } else {
2426 relativize_chunk_concurrently(chunk);
2427 }
2428
2429 // On first thaw after freeze restore oops to the lockstack if any.
2430 assert(chunk->lockstack_size() == 0 || kind == Continuation::thaw_top, "");
2431 if (kind == Continuation::thaw_top && chunk->lockstack_size() > 0) {
2432 thaw_lockstack(chunk);
2433 retry_fast_path = true;
2434 }
2435
2436 // Retry the fast path now that we possibly cleared the FLAG_HAS_LOCKSTACK
2437 // and FLAG_PREEMPTED flags from the stackChunk.
2438 if (retry_fast_path && can_thaw_fast(chunk)) {
2439 intptr_t* sp = thaw_fast<true>(chunk);
2440 if (_preempted_case) {
2441 return handle_preempted_continuation(sp, preempt_kind, true /* fast_case */);
2442 }
2486
2487 intptr_t* sp = caller.sp();
2488
2489 if (_preempted_case) {
2490 return handle_preempted_continuation(sp, preempt_kind, false /* fast_case */);
2491 }
2492 return sp;
2493 }
2494
2495 void ThawBase::recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case) {
2496 log_develop_debug(continuations)("thaw num_frames: %d", num_frames);
2497 assert(!_cont.is_empty(), "no more frames");
2498 assert(num_frames > 0, "");
2499 assert(!heap_frame.is_empty(), "");
2500
2501 if (top_on_preempt_case && (heap_frame.is_native_frame() || heap_frame.is_runtime_frame())) {
2502 heap_frame.is_native_frame() ? recurse_thaw_native_frame(heap_frame, caller, 2) : recurse_thaw_stub_frame(heap_frame, caller, 2);
2503 } else if (!heap_frame.is_interpreted_frame()) {
2504 recurse_thaw_compiled_frame(heap_frame, caller, num_frames, false);
2505 } else {
2506 recurse_thaw_interpreted_frame(heap_frame, caller, num_frames, top_on_preempt_case);
2507 }
2508 }
2509
2510 template<typename FKind>
2511 bool ThawBase::recurse_thaw_java_frame(frame& caller, int num_frames) {
2512 assert(num_frames > 0, "");
2513
2514 DEBUG_ONLY(_frames++;)
2515
2516 int argsize = _stream.stack_argsize();
2517
2518 _stream.next(SmallRegisterMap::instance_no_args());
2519 assert(_stream.to_frame().is_empty() == _stream.is_done(), "");
2520
2521 // we never leave a compiled caller of an interpreted frame as the top frame in the chunk
2522 // as it makes detecting that situation and adjusting unextended_sp tricky
2523 if (num_frames == 1 && !_stream.is_done() && FKind::interpreted && _stream.is_compiled()) {
2524 log_develop_trace(continuations)("thawing extra compiled frame to not leave a compiled interpreted-caller at top");
2525 num_frames++;
2526 }
2527
2528 if (num_frames == 1 || _stream.is_done()) { // end recursion
2529 finalize_thaw(caller, FKind::interpreted ? 0 : argsize);
2530 return true; // bottom
2531 } else { // recurse
2532 recurse_thaw(_stream.to_frame(), caller, num_frames - 1, false /* top_on_preempt_case */);
2533 return false;
2534 }
2535 }
2536
2537 void ThawBase::finalize_thaw(frame& entry, int argsize) {
2538 stackChunkOop chunk = _cont.tail();
2603
2604 void ThawBase::clear_bitmap_bits(address start, address end) {
2605 assert(is_aligned(start, wordSize), "should be aligned: " PTR_FORMAT, p2i(start));
2606 assert(is_aligned(end, VMRegImpl::stack_slot_size), "should be aligned: " PTR_FORMAT, p2i(end));
2607
2608 // we need to clear the bits that correspond to arguments as they reside in the caller frame
2609 // or they will keep objects that are otherwise unreachable alive.
2610
2611 // Align `end` if UseCompressedOops is not set to avoid UB when calculating the bit index, since
2612 // `end` could be at an odd number of stack slots from `start`, i.e might not be oop aligned.
2613 // If that's the case the bit range corresponding to the last stack slot should not have bits set
2614 // anyways and we assert that before returning.
2615 address effective_end = UseCompressedOops ? end : align_down(end, wordSize);
2616 log_develop_trace(continuations)("clearing bitmap for " INTPTR_FORMAT " - " INTPTR_FORMAT, p2i(start), p2i(effective_end));
2617 stackChunkOop chunk = _cont.tail();
2618 chunk->bitmap().clear_range(chunk->bit_index_for(start), chunk->bit_index_for(effective_end));
2619 assert(effective_end == end || !chunk->bitmap().at(chunk->bit_index_for(effective_end)), "bit should not be set");
2620 }
2621
2622 intptr_t* ThawBase::handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case) {
2623 frame top(sp);
2624 assert(top.pc() == *(address*)(sp - frame::sender_sp_ret_address_offset()), "");
2625 DEBUG_ONLY(verify_frame_kind(top, preempt_kind);)
2626 NOT_PRODUCT(int64_t tid = _thread->monitor_owner_id();)
2627
2628 #if INCLUDE_JVMTI
2629 // Finish the VTMS transition.
2630 assert(_thread->is_in_VTMS_transition(), "must be");
2631 bool is_vthread = Continuation::continuation_scope(_cont.continuation()) == java_lang_VirtualThread::vthread_scope();
2632 if (is_vthread) {
2633 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
2634 jvmti_mount_end(_thread, _cont, top, preempt_kind);
2635 } else {
2636 _thread->set_is_in_VTMS_transition(false);
2637 java_lang_Thread::set_is_in_VTMS_transition(_thread->vthread(), false);
2638 }
2639 }
2640 #endif
2641
2642 if (fast_case) {
2643 // If we thawed in the slow path the runtime stub/native wrapper frame already
2644 // has the correct fp (see ThawBase::new_stack_frame). On the fast path though,
2645 // we copied the original fp at the time of freeze which now will have to be fixed.
2646 assert(top.is_runtime_frame() || top.is_native_frame(), "");
2647 int fsize = top.cb()->frame_size();
2648 patch_pd(top, sp + fsize);
2649 }
2650
2651 if (preempt_kind == Continuation::object_wait) {
2652 // Check now if we need to throw IE exception.
2653 bool throw_ie = _thread->pending_interrupted_exception();
2654 if (throw_ie) {
2655 throw_interrupted_exception(_thread, top);
2656 _thread->set_pending_interrupted_exception(false);
2657 }
2658 log_develop_trace(continuations, preempt)("Resuming " INT64_FORMAT" after preemption on Object.wait%s", tid, throw_ie ? "(throwing IE)" : "");
2659 } else if (preempt_kind == Continuation::monitorenter) {
2660 if (top.is_runtime_frame()) {
2661 // The continuation might now run on a different platform thread than the previous time so
2662 // we need to adjust the current thread saved in the stub frame before restoring registers.
2663 JavaThread** thread_addr = frame::saved_thread_address(top);
2664 if (thread_addr != nullptr) *thread_addr = _thread;
2665 }
2666 log_develop_trace(continuations, preempt)("Resuming " INT64_FORMAT " after preemption on monitorenter", tid);
2667 } else {
2668 // We need to redo the original call into the VM. First though, we need
2669 // to exit the monitor we just acquired (except on preemption cancelled
2670 // case where it was already released).
2671 assert(preempt_kind == Continuation::object_locker, "");
2672 if (_monitor != nullptr) _monitor->exit(_thread);
2673 sp = redo_vmcall(_thread, top);
2674 }
2675 return sp;
2676 }
2677
2678 intptr_t* ThawBase::redo_vmcall(JavaThread* current, frame& top) {
2679 assert(!current->preempting(), "");
2680 NOT_PRODUCT(int64_t tid = current->monitor_owner_id();)
2681 intptr_t* sp = top.sp();
2682
2683 {
2684 HandleMarkCleaner hmc(current); // Cleanup so._conth Handle
2685 ContinuationWrapper::SafepointOp so(current, _cont);
2686 AnchorMark am(current, top); // Set the anchor so that the stack is walkable.
2687
2688 Method* m = top.interpreter_frame_method();
2689 Bytecode current_bytecode = Bytecode(m, top.interpreter_frame_bcp());
2690 Bytecodes::Code code = current_bytecode.code();
2691 log_develop_trace(continuations, preempt)("Redoing InterpreterRuntime::%s for " INT64_FORMAT, code == Bytecodes::Code::_new ? "_new" : "resolve_from_cache", tid);
2692
2693 // These InterpreterRuntime entry points use JRT_ENTRY which uses a HandleMarkCleaner.
2694 // Create a HandeMark to avoid destroying so._conth.
2695 HandleMark hm(current);
2696 if (code == Bytecodes::Code::_new) {
2697 InterpreterRuntime::_new(current, m->constants(), current_bytecode.get_index_u2(code));
2698 } else {
2699 InterpreterRuntime::resolve_from_cache(current, code);
2700 }
2701 }
2702
2703 if (current->preempting()) {
2704 // Preempted again so we just arrange to return to preempt stub to unmount.
2705 sp = push_preempt_adapter();
2706 current->set_preempt_alternate_return(nullptr);
2707 bool cancelled = current->preemption_cancelled();
2708 if (cancelled) {
2709 // Instead of calling thaw again from the preempt stub just unmount anyways with
2710 // state of YIELDING. This will give a chance for other vthreads to run while
2711 // minimizing repeated loops of "thaw->redo_vmcall->try_preempt->preemption_cancelled->thaw..."
2712 // in case of multiple vthreads contending for the same init_lock().
2713 current->set_preemption_cancelled(false);
2714 oop vthread = current->vthread();
2715 assert(java_lang_VirtualThread::state(vthread) == java_lang_VirtualThread::RUNNING, "wrong state for vthread");
2716 java_lang_VirtualThread::set_state(vthread, java_lang_VirtualThread::YIELDING);
2717 }
2718 log_develop_trace(continuations, preempt)("Preempted " INT64_FORMAT " again%s", tid, cancelled ? "(preemption cancelled, setting state to YIELDING)" : "");
2719 } else {
2720 log_develop_trace(continuations, preempt)("Call succesful, resuming " INT64_FORMAT, tid);
2721 }
2722 return sp;
2723 }
2724
2725 void ThawBase::throw_interrupted_exception(JavaThread* current, frame& top) {
2726 HandleMarkCleaner hm(current); // Cleanup so._conth Handle
2727 ContinuationWrapper::SafepointOp so(current, _cont);
2728 // Since we might safepoint set the anchor so that the stack can be walked.
2729 set_anchor(current, top.sp());
2730 JRT_BLOCK
2731 THROW(vmSymbols::java_lang_InterruptedException());
2732 JRT_BLOCK_END
2733 clear_anchor(current);
2734 }
2735
2736 NOINLINE void ThawBase::recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames, bool is_top) {
2737 assert(hf.is_interpreted_frame(), "");
2738
2739 if (UNLIKELY(seen_by_gc())) {
2740 if (is_top && _process_args_at_top) {
2741 log_trace(continuations, tracking)("Processing arguments in recurse_thaw_interpreted_frame");
2742 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_with_args());
2743 } else {
2744 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_no_args());
2745 }
2746 }
2747
2748 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::InterpretedFrame>(caller, num_frames);
2749
2750 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2751
2752 _align_size += frame::align_wiggle; // possible added alignment for internal interpreted frame alignment om AArch64
2753
2754 frame f = new_stack_frame<ContinuationHelper::InterpretedFrame>(hf, caller, is_bottom_frame);
2755
2756 intptr_t* const stack_frame_top = f.sp() + frame::metadata_words_at_top;
2757 intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
2758 intptr_t* const heap_frame_top = hf.unextended_sp() + frame::metadata_words_at_top;
2759 intptr_t* const heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
2760
2761 assert(hf.is_heap_frame(), "should be");
2762 assert(!f.is_heap_frame(), "should not be");
2763
2764 const int fsize = pointer_delta_as_int(heap_frame_bottom, heap_frame_top);
2765 assert((stack_frame_bottom == stack_frame_top + fsize), "");
2770
2771 // Make sure the relativized locals is already set.
2772 assert(f.interpreter_frame_local_at(0) == stack_frame_bottom - 1, "invalid frame bottom");
2773
2774 derelativize_interpreted_frame_metadata(hf, f);
2775 patch(f, caller, is_bottom_frame);
2776
2777 assert(f.is_interpreted_frame_valid(_cont.thread()), "invalid thawed frame");
2778 assert(stack_frame_bottom <= ContinuationHelper::Frame::frame_top(caller), "");
2779
2780 CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();)
2781
2782 maybe_set_fastpath(f.sp());
2783
2784 Method* m = hf.interpreter_frame_method();
2785 assert(!m->is_native() || !is_bottom_frame, "should be top frame of thaw_top case; missing caller frame");
2786 const int locals = m->max_locals();
2787
2788 if (!is_bottom_frame) {
2789 // can only fix caller once this frame is thawed (due to callee saved regs)
2790 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance_no_args());
2791 } else if (_cont.tail()->has_bitmap() && locals > 0) {
2792 assert(hf.is_heap_frame(), "should be");
2793 address start = (address)(heap_frame_bottom - locals);
2794 address end = (address)heap_frame_bottom;
2795 clear_bitmap_bits(start, end);
2796 }
2797
2798 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2799 caller = f;
2800 }
2801
2802 void ThawBase::recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller) {
2803 assert(hf.is_compiled_frame(), "");
2804 assert(_preempted_case || !stub_caller, "stub caller not at preemption");
2805
2806 if (!stub_caller && UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2807 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_no_args());
2808 }
2809
2810 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::CompiledFrame>(caller, num_frames);
2811
2812 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2813
2814 assert(caller.sp() == caller.unextended_sp(), "");
2815
2816 if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) {
2817 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_compiled_frame
2818 }
2819
2820 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2821 // yet laid out in the stack, and so the original_pc is not stored in it.
2822 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2823 frame f = new_stack_frame<ContinuationHelper::CompiledFrame>(hf, caller, is_bottom_frame);
2824 intptr_t* const stack_frame_top = f.sp();
2825 intptr_t* const heap_frame_top = hf.unextended_sp();
2826
2827 const int added_argsize = (is_bottom_frame || caller.is_interpreted_frame()) ? hf.compiled_frame_stack_argsize() : 0;
2846 assert(!f.is_deoptimized_frame(), "");
2847 if (hf.is_deoptimized_frame()) {
2848 maybe_set_fastpath(f.sp());
2849 } else if (_thread->is_interp_only_mode()
2850 || (stub_caller && f.cb()->as_nmethod()->is_marked_for_deoptimization())) {
2851 // The caller of the safepoint stub when the continuation is preempted is not at a call instruction, and so
2852 // cannot rely on nmethod patching for deopt.
2853 assert(_thread->is_interp_only_mode() || stub_caller, "expected a stub-caller");
2854
2855 log_develop_trace(continuations)("Deoptimizing thawed frame");
2856 DEBUG_ONLY(ContinuationHelper::Frame::patch_pc(f, nullptr));
2857
2858 f.deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2859 assert(f.is_deoptimized_frame(), "");
2860 assert(ContinuationHelper::Frame::is_deopt_return(f.raw_pc(), f), "");
2861 maybe_set_fastpath(f.sp());
2862 }
2863
2864 if (!is_bottom_frame) {
2865 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2866 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance_no_args());
2867 } else if (_cont.tail()->has_bitmap() && added_argsize > 0) {
2868 address start = (address)(heap_frame_top + ContinuationHelper::CompiledFrame::size(hf) + frame::metadata_words_at_top);
2869 int stack_args_slots = f.cb()->as_nmethod()->num_stack_arg_slots(false /* rounded */);
2870 int argsize_in_bytes = stack_args_slots * VMRegImpl::stack_slot_size;
2871 clear_bitmap_bits(start, start + argsize_in_bytes);
2872 }
2873
2874 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2875 caller = f;
2876 }
2877
2878 void ThawBase::recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames) {
2879 DEBUG_ONLY(_frames++;)
2880
2881 if (UNLIKELY(seen_by_gc())) {
2882 // Process the stub's caller here since we might need the full map.
2883 RegisterMap map(nullptr,
2884 RegisterMap::UpdateMap::include,
2885 RegisterMap::ProcessFrames::skip,
2886 RegisterMap::WalkContinuation::skip);
2887 map.set_include_argument_oops(false);
2888 _stream.next(&map);
2889 assert(!_stream.is_done(), "");
2890 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, &map);
2891 } else {
2892 _stream.next(SmallRegisterMap::instance_no_args());
2893 assert(!_stream.is_done(), "");
2894 }
2895
2896 recurse_thaw_compiled_frame(_stream.to_frame(), caller, num_frames, true);
2897
2898 assert(caller.is_compiled_frame(), "");
2899 assert(caller.sp() == caller.unextended_sp(), "");
2900
2901 DEBUG_ONLY(before_thaw_java_frame(hf, caller, false /*is_bottom_frame*/, num_frames);)
2902
2903 frame f = new_stack_frame<ContinuationHelper::StubFrame>(hf, caller, false);
2904 intptr_t* stack_frame_top = f.sp();
2905 intptr_t* heap_frame_top = hf.sp();
2906 int fsize = ContinuationHelper::StubFrame::size(hf);
2907
2908 copy_from_chunk(heap_frame_top - frame::metadata_words, stack_frame_top - frame::metadata_words,
2909 fsize + frame::metadata_words);
2910
2911 patch(f, caller, false /*is_bottom_frame*/);
2912
2913 // can only fix caller once this frame is thawed (due to callee saved regs)
2914 RegisterMap map(nullptr,
2915 RegisterMap::UpdateMap::include,
2916 RegisterMap::ProcessFrames::skip,
2917 RegisterMap::WalkContinuation::skip);
2918 map.set_include_argument_oops(false);
2919 f.oop_map()->update_register_map(&f, &map);
2920 ContinuationHelper::update_register_map_with_callee(caller, &map);
2921 _cont.tail()->fix_thawed_frame(caller, &map);
2922
2923 DEBUG_ONLY(after_thaw_java_frame(f, false /*is_bottom_frame*/);)
2924 caller = f;
2925 }
2926
2927 void ThawBase::recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames) {
2928 assert(hf.is_native_frame(), "");
2929 assert(_preempted_case && hf.cb()->as_nmethod()->method()->is_object_wait0(), "");
2930
2931 if (UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2932 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_no_args());
2933 }
2934
2935 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::NativeFrame>(caller, num_frames);
2936 assert(!is_bottom_frame, "");
2937
2938 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2939
2940 assert(caller.sp() == caller.unextended_sp(), "");
2941
2942 if (caller.is_interpreted_frame()) {
2943 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_native_frame
2944 }
2945
2946 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2947 // yet laid out in the stack, and so the original_pc is not stored in it.
2948 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2949 frame f = new_stack_frame<ContinuationHelper::NativeFrame>(hf, caller, false /* bottom */);
2950 intptr_t* const stack_frame_top = f.sp();
2951 intptr_t* const heap_frame_top = hf.unextended_sp();
2952
2953 int fsize = ContinuationHelper::NativeFrame::size(hf);
2954 assert(fsize <= (int)(caller.unextended_sp() - f.unextended_sp()), "");
2955
2956 intptr_t* from = heap_frame_top - frame::metadata_words_at_bottom;
2957 intptr_t* to = stack_frame_top - frame::metadata_words_at_bottom;
2958 int sz = fsize + frame::metadata_words_at_bottom;
2959
2960 copy_from_chunk(from, to, sz); // copying good oops because we invoked barriers above
2961
2962 patch(f, caller, false /* bottom */);
2963
2964 // f.is_deoptimized_frame() is always false and we must test hf.is_deoptimized_frame() (see comment above)
2965 assert(!f.is_deoptimized_frame(), "");
2966 assert(!hf.is_deoptimized_frame(), "");
2967 assert(!f.cb()->as_nmethod()->is_marked_for_deoptimization(), "");
2968
2969 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2970 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance_no_args());
2971
2972 DEBUG_ONLY(after_thaw_java_frame(f, false /* bottom */);)
2973 caller = f;
2974 }
2975
2976 void ThawBase::finish_thaw(frame& f) {
2977 stackChunkOop chunk = _cont.tail();
2978
2979 if (chunk->is_empty()) {
2980 // Only remove chunk from list if it can't be reused for another freeze
2981 if (seen_by_gc()) {
2982 _cont.set_tail(chunk->parent());
2983 } else {
2984 chunk->set_has_mixed_frames(false);
2985 }
2986 chunk->set_max_thawing_size(0);
2987 } else {
2988 chunk->set_max_thawing_size(chunk->max_thawing_size() - _align_size);
2989 }
2990 assert(chunk->is_empty() == (chunk->max_thawing_size() == 0), "");
2991
2992 if (!is_aligned(f.sp(), frame::frame_alignment)) {
2993 assert(f.is_interpreted_frame(), "");
2994 f.set_sp(align_down(f.sp(), frame::frame_alignment));
2995 }
2996 push_return_frame(f);
2997 // can only fix caller after push_return_frame (due to callee saved regs)
2998 if (_process_args_at_top) {
2999 log_trace(continuations, tracking)("Processing arguments in finish_thaw");
3000 chunk->fix_thawed_frame(f, SmallRegisterMap::instance_with_args());
3001 } else {
3002 chunk->fix_thawed_frame(f, SmallRegisterMap::instance_no_args());
3003 }
3004
3005 assert(_cont.is_empty() == _cont.last_frame().is_empty(), "");
3006
3007 log_develop_trace(continuations)("thawed %d frames", _frames);
3008
3009 LogTarget(Trace, continuations) lt;
3010 if (lt.develop_is_enabled()) {
3011 LogStream ls(lt);
3012 ls.print_cr("top hframe after (thaw):");
3013 _cont.last_frame().print_value_on(&ls);
3014 }
3015 }
3016
3017 void ThawBase::push_return_frame(frame& f) { // see generate_cont_thaw
3018 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == f.cb()->as_nmethod()->is_deopt_pc(f.raw_pc()), "");
3019 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == (f.pc() != f.raw_pc()), "");
3020
3021 LogTarget(Trace, continuations) lt;
3022 if (lt.develop_is_enabled()) {
3023 LogStream ls(lt);
3045
3046 ContinuationEntry* entry = thread->last_continuation();
3047 assert(entry != nullptr, "");
3048 oop oopCont = entry->cont_oop(thread);
3049
3050 assert(!jdk_internal_vm_Continuation::done(oopCont), "");
3051 assert(oopCont == get_continuation(thread), "");
3052 verify_continuation(oopCont);
3053
3054 assert(entry->is_virtual_thread() == (entry->scope(thread) == java_lang_VirtualThread::vthread_scope()), "");
3055
3056 ContinuationWrapper cont(thread, oopCont);
3057 log_develop_debug(continuations)("THAW #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
3058
3059 #ifdef ASSERT
3060 set_anchor_to_entry(thread, cont.entry());
3061 log_frames(thread);
3062 clear_anchor(thread);
3063 #endif
3064
3065 Thaw<ConfigT> thw(thread, cont);
3066 intptr_t* const sp = thw.thaw(kind);
3067 assert(is_aligned(sp, frame::frame_alignment), "");
3068 DEBUG_ONLY(log_frames_after_thaw(thread, cont, sp);)
3069
3070 CONT_JFR_ONLY(thw.jfr_info().post_jfr_event(&event, cont.continuation(), thread);)
3071
3072 verify_continuation(cont.continuation());
3073 log_develop_debug(continuations)("=== End of thaw #" INTPTR_FORMAT, cont.hash());
3074
3075 return sp;
3076 }
3077
3078 #ifdef ASSERT
3079 static void do_deopt_after_thaw(JavaThread* thread) {
3080 int i = 0;
3081 StackFrameStream fst(thread, true, false);
3082 fst.register_map()->set_include_argument_oops(false);
3083 ContinuationHelper::update_register_map_with_callee(*fst.current(), fst.register_map());
3084 for (; !fst.is_done(); fst.next()) {
3085 if (fst.current()->cb()->is_nmethod()) {
3086 nmethod* nm = fst.current()->cb()->as_nmethod();
3087 if (!nm->method()->is_continuation_native_intrinsic()) {
3088 nm->make_deoptimized();
3145 if (!fr.is_interpreted_frame()) {
3146 st->print_cr("size: %d argsize: %d",
3147 ContinuationHelper::NonInterpretedUnknownFrame::size(fr),
3148 ContinuationHelper::NonInterpretedUnknownFrame::stack_argsize(fr));
3149 }
3150 VMReg reg = fst.register_map()->find_register_spilled_here(cl.p(), fst.current()->sp());
3151 if (reg != nullptr) {
3152 st->print_cr("Reg %s %d", reg->name(), reg->is_stack() ? (int)reg->reg2stack() : -99);
3153 }
3154 cl.reset();
3155 DEBUG_ONLY(thread->print_frame_layout();)
3156 if (chunk != nullptr) {
3157 chunk->print_on(true, st);
3158 }
3159 return false;
3160 }
3161 }
3162 return true;
3163 }
3164
3165 static void log_frames(JavaThread* thread, bool dolog) {
3166 const static int show_entry_callers = 3;
3167 LogTarget(Trace, continuations, tracking) lt;
3168 if (!lt.develop_is_enabled() || !dolog) {
3169 return;
3170 }
3171 LogStream ls(lt);
3172
3173 ls.print_cr("------- frames --------- for thread " INTPTR_FORMAT, p2i(thread));
3174 if (!thread->has_last_Java_frame()) {
3175 ls.print_cr("NO ANCHOR!");
3176 }
3177
3178 RegisterMap map(thread,
3179 RegisterMap::UpdateMap::include,
3180 RegisterMap::ProcessFrames::include,
3181 RegisterMap::WalkContinuation::skip);
3182 map.set_include_argument_oops(false);
3183
3184 if (false) {
3185 for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) {
3186 f.print_on(&ls);
3187 }
3188 } else {
3190 ResetNoHandleMark rnhm;
3191 ResourceMark rm;
3192 HandleMark hm(Thread::current());
3193 FrameValues values;
3194
3195 int i = 0;
3196 int post_entry = -1;
3197 for (frame f = thread->last_frame(); !f.is_first_frame(); f = f.sender(&map), i++) {
3198 f.describe(values, i, &map, i == 0);
3199 if (post_entry >= 0 || Continuation::is_continuation_enterSpecial(f))
3200 post_entry++;
3201 if (post_entry >= show_entry_callers)
3202 break;
3203 }
3204 values.print_on(thread, &ls);
3205 }
3206
3207 ls.print_cr("======= end frames =========");
3208 }
3209
3210 static void log_frames_after_thaw(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp) {
3211 intptr_t* sp0 = sp;
3212 address pc0 = *(address*)(sp - frame::sender_sp_ret_address_offset());
3213
3214 bool preempted = false;
3215 stackChunkOop tail = cont.tail();
3216 if (tail != nullptr && tail->preempted()) {
3217 // Still preempted (monitor not acquired) so no frames were thawed.
3218 set_anchor(thread, cont.entrySP(), cont.entryPC());
3219 preempted = true;
3220 } else {
3221 set_anchor(thread, sp0);
3222 }
3223
3224 log_frames(thread);
3225 if (LoomVerifyAfterThaw) {
3226 assert(do_verify_after_thaw(thread, cont.tail(), tty), "");
3227 }
3228 assert(ContinuationEntry::assert_entry_frame_laid_out(thread, preempted), "");
3229 clear_anchor(thread);
3230
3231 LogTarget(Trace, continuations) lt;
3232 if (lt.develop_is_enabled()) {
3233 LogStream ls(lt);
3234 ls.print_cr("Jumping to frame (thaw):");
3235 frame(sp).print_value_on(&ls);
3236 }
3237 }
3238 #endif // ASSERT
3239
3240 #include CPU_HEADER_INLINE(continuationFreezeThaw)
3241
3242 #ifdef ASSERT
3243 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st) {
3244 ResourceMark rm;
3245 FrameValues values;
3246 assert(f.get_cb() != nullptr, "");
3247 RegisterMap map(f.is_heap_frame() ?
3248 nullptr :
|