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