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);
1073 log_develop_trace(continuations)("Reusing chunk mixed: %d empty: %d", chunk->has_mixed_frames(), chunk->is_empty());
1074 if (chunk->is_empty()) {
1075 int sp = chunk->stack_size() - argsize_md;
1076 chunk->set_sp(sp);
1077 chunk->set_bottom(sp);
1078 _freeze_size += overlap;
1079 assert(chunk->max_thawing_size() == 0, "");
1080 } DEBUG_ONLY(else empty_chunk = false;)
1081 }
1082 assert(!chunk->is_gc_mode(), "");
1083 assert(!chunk->has_bitmap(), "");
1084 chunk->set_has_mixed_frames(true);
1085
1086 assert(chunk->requires_barriers() == _barriers, "");
1087 assert(!_barriers || chunk->is_empty(), "");
1088
1089 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).is_done(), "");
1090 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame().is_empty(), "");
1091
1092 if (_preempt) {
1093 frame f = _thread->last_frame();
1094 if (f.is_interpreted_frame()) {
1095 // Some platforms do not save the last_sp in the top interpreter frame on VM calls.
1096 // We need it so that on resume we can restore the sp to the right place, since
1097 // thawing might add an alignment word to the expression stack (see finish_thaw()).
1098 // We do it now that we know freezing will be successful.
1099 prepare_freeze_interpreted_top_frame(f);
1100 }
1101 }
1102
1103 // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
1104 // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
1105 // will either see no continuation or a consistent chunk.
1106 unwind_frames();
1107
1108 chunk->set_max_thawing_size(chunk->max_thawing_size() + _freeze_size - _monitors_in_lockstack - frame::metadata_words);
1109
1110 if (lt.develop_is_enabled()) {
1111 LogStream ls(lt);
1112 ls.print_cr("top chunk:");
1113 chunk->print_on(&ls);
1114 }
1115
1116 if (_monitors_in_lockstack > 0) {
1117 freeze_lockstack(chunk);
1118 }
1119
1591 // Some GCs could put direct allocations in old gen for slow-path
1592 // allocations; need to explicitly check if that was the case.
1593 _barriers = chunk->requires_barriers();
1594 }
1595 }
1596
1597 if (_barriers) {
1598 log_develop_trace(continuations)("allocation requires barriers");
1599 }
1600
1601 assert(chunk->parent() == nullptr || chunk->parent()->is_stackChunk(), "");
1602
1603 return chunk;
1604 }
1605
1606 void FreezeBase::throw_stack_overflow_on_humongous_chunk() {
1607 ContinuationWrapper::SafepointOp so(_thread, _cont); // could also call _cont.done() instead
1608 Exceptions::_throw_msg(_thread, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Humongous stack chunk");
1609 }
1610
1611 #if INCLUDE_JVMTI
1612 static int num_java_frames(ContinuationWrapper& cont) {
1613 ResourceMark rm; // used for scope traversal in num_java_frames(nmethod*, address)
1614 int count = 0;
1615 for (stackChunkOop chunk = cont.tail(); chunk != nullptr; chunk = chunk->parent()) {
1616 count += chunk->num_java_frames();
1617 }
1618 return count;
1619 }
1620
1621 static void invalidate_jvmti_stack(JavaThread* thread) {
1622 JvmtiThreadState *state = thread->jvmti_thread_state();
1623 if (state != nullptr) {
1624 state->invalidate_cur_stack_depth();
1625 }
1626 }
1627
1628 static void jvmti_yield_cleanup(JavaThread* thread, ContinuationWrapper& cont) {
1629 if (JvmtiExport::has_frame_pops(thread)) {
1630 int num_frames = num_java_frames(cont);
1631
1632 ContinuationWrapper::SafepointOp so(Thread::current(), cont);
1633 JvmtiExport::continuation_yield_cleanup(JavaThread::current(), num_frames);
1634 }
1635 invalidate_jvmti_stack(thread);
1636 }
1637
1638 static void jvmti_mount_end(JavaThread* current, ContinuationWrapper& cont, frame top) {
1639 assert(current->vthread() != nullptr, "must be");
1640
1641 HandleMarkCleaner hm(current);
1642 Handle vth(current, current->vthread());
1643
1644 ContinuationWrapper::SafepointOp so(current, cont);
1645
1646 // Since we might safepoint set the anchor so that the stack can be walked.
1647 set_anchor(current, top.sp());
1648
1649 JRT_BLOCK
1650 JvmtiVTMSTransitionDisabler::VTMS_vthread_mount((jthread)vth.raw_value(), false);
1651
1652 if (current->pending_contended_entered_event()) {
1653 JvmtiExport::post_monitor_contended_entered(current, current->contended_entered_monitor());
1654 current->set_contended_entered_monitor(nullptr);
1655 }
1656 JRT_BLOCK_END
1657
1658 clear_anchor(current);
1659 }
1660 #endif // INCLUDE_JVMTI
1661
1662 #ifdef ASSERT
1663 // There are no interpreted frames if we're not called from the interpreter and we haven't ancountered an i2c
1664 // adapter or called Deoptimization::unpack_frames. As for native frames, upcalls from JNI also go through the
1665 // interpreter (see JavaCalls::call_helper), while the UpcallLinker explicitly sets cont_fastpath.
1666 bool FreezeBase::check_valid_fast_path() {
1667 ContinuationEntry* ce = _thread->last_continuation();
1668 RegisterMap map(_thread,
1669 RegisterMap::UpdateMap::skip,
1670 RegisterMap::ProcessFrames::skip,
1671 RegisterMap::WalkContinuation::skip);
1672 map.set_include_argument_oops(false);
1673 bool is_top_frame = true;
1674 for (frame f = freeze_start_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map), is_top_frame = false) {
1675 if (!((f.is_compiled_frame() && !f.is_deoptimized_frame()) || (is_top_frame && (f.is_runtime_frame() || f.is_native_frame())))) {
1676 return false;
1677 }
1678 }
1679 return true;
1680 }
1681 #endif // ASSERT
1682
1683 static inline freeze_result freeze_epilog(ContinuationWrapper& cont) {
1684 verify_continuation(cont.continuation());
1685 assert(!cont.is_empty(), "");
1686
1687 log_develop_debug(continuations)("=== End of freeze cont ### #" INTPTR_FORMAT, cont.hash());
1688 return freeze_ok;
1689 }
1690
1691 static freeze_result freeze_epilog(JavaThread* thread, ContinuationWrapper& cont, freeze_result res) {
1692 if (UNLIKELY(res != freeze_ok)) {
1693 JFR_ONLY(thread->set_last_freeze_fail_result(res);)
1694 verify_continuation(cont.continuation());
1695 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1696 return res;
1697 }
1698
1699 JVMTI_ONLY(jvmti_yield_cleanup(thread, cont)); // can safepoint
1700 return freeze_epilog(cont);
1701 }
1702
1703 static freeze_result preempt_epilog(ContinuationWrapper& cont, freeze_result res, frame& old_last_frame) {
1704 if (UNLIKELY(res != freeze_ok)) {
1705 verify_continuation(cont.continuation());
1706 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1707 return res;
1708 }
1709
1710 patch_return_pc_with_preempt_stub(old_last_frame);
1711 cont.tail()->set_preempted(true);
1712
1713 return freeze_epilog(cont);
1714 }
1715
1716 template<typename ConfigT, bool preempt>
1717 static inline freeze_result freeze_internal(JavaThread* current, intptr_t* const sp) {
1718 assert(!current->has_pending_exception(), "");
1719
1720 #ifdef ASSERT
1721 log_trace(continuations)("~~~~ freeze sp: " INTPTR_FORMAT "JavaThread: " INTPTR_FORMAT, p2i(current->last_continuation()->entry_sp()), p2i(current));
1722 log_frames(current);
1723 #endif
1724
1725 CONT_JFR_ONLY(EventContinuationFreeze event;)
1726
1727 ContinuationEntry* entry = current->last_continuation();
1728
1729 oop oopCont = entry->cont_oop(current);
1730 assert(oopCont == current->last_continuation()->cont_oop(current), "");
1731 assert(ContinuationEntry::assert_entry_frame_laid_out(current), "");
1732
1888 // 300 is an estimate for stack size taken for this native code, in addition to StackShadowPages
1889 // for the Java frames in the check below.
1890 if (!stack_overflow_check(thread, size + 300, bottom)) {
1891 return 0;
1892 }
1893
1894 log_develop_trace(continuations)("prepare_thaw bottom: " INTPTR_FORMAT " top: " INTPTR_FORMAT " size: %d",
1895 p2i(bottom), p2i(bottom - size), size);
1896 return size;
1897 }
1898
1899 class ThawBase : public StackObj {
1900 protected:
1901 JavaThread* _thread;
1902 ContinuationWrapper& _cont;
1903 CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;)
1904
1905 intptr_t* _fastpath;
1906 bool _barriers;
1907 bool _preempted_case;
1908 intptr_t* _top_unextended_sp_before_thaw;
1909 int _align_size;
1910 DEBUG_ONLY(intptr_t* _top_stack_address);
1911
1912 StackChunkFrameStream<ChunkFrames::Mixed> _stream;
1913
1914 NOT_PRODUCT(int _frames;)
1915
1916 protected:
1917 ThawBase(JavaThread* thread, ContinuationWrapper& cont) :
1918 _thread(thread), _cont(cont),
1919 _fastpath(nullptr) {
1920 DEBUG_ONLY(_top_unextended_sp_before_thaw = nullptr;)
1921 assert (cont.tail() != nullptr, "no last chunk");
1922 DEBUG_ONLY(_top_stack_address = _cont.entrySP() - thaw_size(cont.tail());)
1923 }
1924
1925 void clear_chunk(stackChunkOop chunk);
1926 template<bool check_stub>
1927 int remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize);
1928 void copy_from_chunk(intptr_t* from, intptr_t* to, int size);
1929
1930 void thaw_lockstack(stackChunkOop chunk);
1931
1932 // fast path
1933 inline void prefetch_chunk_pd(void* start, int size_words);
1934 void patch_return(intptr_t* sp, bool is_last);
1935
1936 intptr_t* handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case);
1937 inline intptr_t* push_cleanup_continuation();
1938 void throw_interrupted_exception(JavaThread* current, frame& top);
1939
1940 void recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case);
1941 void finish_thaw(frame& f);
1942
1943 private:
1944 template<typename FKind> bool recurse_thaw_java_frame(frame& caller, int num_frames);
1945 void finalize_thaw(frame& entry, int argsize);
1946
1947 inline bool seen_by_gc();
1948
1949 inline void before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame);
1950 inline void after_thaw_java_frame(const frame& f, bool bottom);
1951 inline void patch(frame& f, const frame& caller, bool bottom);
1952 void clear_bitmap_bits(address start, address end);
1953
1954 NOINLINE void recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames);
1955 void recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller);
1956 void recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames);
1957 void recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames);
1958
1959 void push_return_frame(frame& f);
1960 inline frame new_entry_frame();
1961 template<typename FKind> frame new_stack_frame(const frame& hf, frame& caller, bool bottom);
1962 inline void patch_pd(frame& f, const frame& sender);
1963 inline void patch_pd(frame& f, intptr_t* caller_sp);
1964 inline intptr_t* align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom);
1965
1966 void maybe_set_fastpath(intptr_t* sp) { if (sp > _fastpath) _fastpath = sp; }
1967
1968 static inline void derelativize_interpreted_frame_metadata(const frame& hf, const frame& f);
1969
1970 public:
1971 CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
1972 };
1973
1974 template <typename ConfigT>
2034 chunk->set_sp(chunk->bottom());
2035 chunk->set_max_thawing_size(0);
2036 }
2037
2038 template<bool check_stub>
2039 int ThawBase::remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize) {
2040 bool empty = false;
2041 StackChunkFrameStream<ChunkFrames::CompiledOnly> f(chunk);
2042 DEBUG_ONLY(intptr_t* const chunk_sp = chunk->start_address() + chunk->sp();)
2043 assert(chunk_sp == f.sp(), "");
2044 assert(chunk_sp == f.unextended_sp(), "");
2045
2046 int frame_size = f.cb()->frame_size();
2047 argsize = f.stack_argsize();
2048
2049 assert(!f.is_stub() || check_stub, "");
2050 if (check_stub && f.is_stub()) {
2051 // If we don't thaw the top compiled frame too, after restoring the saved
2052 // registers back in Java, we would hit the return barrier to thaw one more
2053 // frame effectively overwriting the restored registers during that call.
2054 f.next(SmallRegisterMap::instance(), true /* stop */);
2055 assert(!f.is_done(), "");
2056
2057 f.get_cb();
2058 assert(f.is_compiled(), "");
2059 frame_size += f.cb()->frame_size();
2060 argsize = f.stack_argsize();
2061
2062 if (f.cb()->as_nmethod()->is_marked_for_deoptimization()) {
2063 // The caller of the runtime stub when the continuation is preempted is not at a
2064 // Java call instruction, and so cannot rely on nmethod patching for deopt.
2065 log_develop_trace(continuations)("Deoptimizing runtime stub caller");
2066 f.to_frame().deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2067 }
2068 }
2069
2070 f.next(SmallRegisterMap::instance(), true /* stop */);
2071 empty = f.is_done();
2072 assert(!empty || argsize == chunk->argsize(), "");
2073
2074 if (empty) {
2075 clear_chunk(chunk);
2076 } else {
2077 chunk->set_sp(chunk->sp() + frame_size);
2078 chunk->set_max_thawing_size(chunk->max_thawing_size() - frame_size);
2079 // We set chunk->pc to the return pc into the next frame
2080 chunk->set_pc(f.pc());
2081 #ifdef ASSERT
2082 {
2083 intptr_t* retaddr_slot = (chunk_sp
2084 + frame_size
2085 - frame::sender_sp_ret_address_offset());
2086 assert(f.pc() == ContinuationHelper::return_address_at(retaddr_slot),
2087 "unexpected pc");
2088 }
2089 #endif
2090 }
2210 return rs.sp();
2211 }
2212
2213 inline bool ThawBase::seen_by_gc() {
2214 return _barriers || _cont.tail()->is_gc_mode();
2215 }
2216
2217 static inline void relativize_chunk_concurrently(stackChunkOop chunk) {
2218 #if INCLUDE_ZGC || INCLUDE_SHENANDOAHGC
2219 if (UseZGC || UseShenandoahGC) {
2220 chunk->relativize_derived_pointers_concurrently();
2221 }
2222 #endif
2223 }
2224
2225 template <typename ConfigT>
2226 NOINLINE intptr_t* Thaw<ConfigT>::thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind) {
2227 Continuation::preempt_kind preempt_kind;
2228 bool retry_fast_path = false;
2229
2230 _preempted_case = chunk->preempted();
2231 if (_preempted_case) {
2232 ObjectWaiter* waiter = java_lang_VirtualThread::objectWaiter(_thread->vthread());
2233 if (waiter != nullptr) {
2234 // Mounted again after preemption. Resume the pending monitor operation,
2235 // which will be either a monitorenter or Object.wait() call.
2236 ObjectMonitor* mon = waiter->monitor();
2237 preempt_kind = waiter->is_wait() ? Continuation::freeze_on_wait : Continuation::freeze_on_monitorenter;
2238
2239 bool mon_acquired = mon->resume_operation(_thread, waiter, _cont);
2240 assert(!mon_acquired || mon->has_owner(_thread), "invariant");
2241 if (!mon_acquired) {
2242 // Failed to acquire monitor. Return to enterSpecial to unmount again.
2243 return push_cleanup_continuation();
2244 }
2245 chunk = _cont.tail(); // reload oop in case of safepoint in resume_operation (if posting JVMTI events).
2246 } else {
2247 // Preemption cancelled in moniterenter case. We actually acquired
2248 // the monitor after freezing all frames so nothing to do.
2249 preempt_kind = Continuation::freeze_on_monitorenter;
2250 }
2251 // Call this first to avoid racing with GC threads later when modifying the chunk flags.
2252 relativize_chunk_concurrently(chunk);
2253 chunk->set_preempted(false);
2254 retry_fast_path = true;
2255 } else {
2256 relativize_chunk_concurrently(chunk);
2257 }
2258
2259 // On first thaw after freeze restore oops to the lockstack if any.
2260 assert(chunk->lockstack_size() == 0 || kind == Continuation::thaw_top, "");
2261 if (kind == Continuation::thaw_top && chunk->lockstack_size() > 0) {
2262 thaw_lockstack(chunk);
2263 retry_fast_path = true;
2264 }
2265
2266 // Retry the fast path now that we possibly cleared the FLAG_HAS_LOCKSTACK
2267 // and FLAG_PREEMPTED flags from the stackChunk.
2268 if (retry_fast_path && can_thaw_fast(chunk)) {
2269 intptr_t* sp = thaw_fast<true>(chunk);
2270 if (_preempted_case) {
2271 return handle_preempted_continuation(sp, preempt_kind, true /* fast_case */);
2272 }
2316
2317 intptr_t* sp = caller.sp();
2318
2319 if (_preempted_case) {
2320 return handle_preempted_continuation(sp, preempt_kind, false /* fast_case */);
2321 }
2322 return sp;
2323 }
2324
2325 void ThawBase::recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case) {
2326 log_develop_debug(continuations)("thaw num_frames: %d", num_frames);
2327 assert(!_cont.is_empty(), "no more frames");
2328 assert(num_frames > 0, "");
2329 assert(!heap_frame.is_empty(), "");
2330
2331 if (top_on_preempt_case && (heap_frame.is_native_frame() || heap_frame.is_runtime_frame())) {
2332 heap_frame.is_native_frame() ? recurse_thaw_native_frame(heap_frame, caller, 2) : recurse_thaw_stub_frame(heap_frame, caller, 2);
2333 } else if (!heap_frame.is_interpreted_frame()) {
2334 recurse_thaw_compiled_frame(heap_frame, caller, num_frames, false);
2335 } else {
2336 recurse_thaw_interpreted_frame(heap_frame, caller, num_frames);
2337 }
2338 }
2339
2340 template<typename FKind>
2341 bool ThawBase::recurse_thaw_java_frame(frame& caller, int num_frames) {
2342 assert(num_frames > 0, "");
2343
2344 DEBUG_ONLY(_frames++;)
2345
2346 int argsize = _stream.stack_argsize();
2347
2348 _stream.next(SmallRegisterMap::instance());
2349 assert(_stream.to_frame().is_empty() == _stream.is_done(), "");
2350
2351 // we never leave a compiled caller of an interpreted frame as the top frame in the chunk
2352 // as it makes detecting that situation and adjusting unextended_sp tricky
2353 if (num_frames == 1 && !_stream.is_done() && FKind::interpreted && _stream.is_compiled()) {
2354 log_develop_trace(continuations)("thawing extra compiled frame to not leave a compiled interpreted-caller at top");
2355 num_frames++;
2356 }
2357
2358 if (num_frames == 1 || _stream.is_done()) { // end recursion
2359 finalize_thaw(caller, FKind::interpreted ? 0 : argsize);
2360 return true; // bottom
2361 } else { // recurse
2362 recurse_thaw(_stream.to_frame(), caller, num_frames - 1, false /* top_on_preempt_case */);
2363 return false;
2364 }
2365 }
2366
2367 void ThawBase::finalize_thaw(frame& entry, int argsize) {
2368 stackChunkOop chunk = _cont.tail();
2433
2434 void ThawBase::clear_bitmap_bits(address start, address end) {
2435 assert(is_aligned(start, wordSize), "should be aligned: " PTR_FORMAT, p2i(start));
2436 assert(is_aligned(end, VMRegImpl::stack_slot_size), "should be aligned: " PTR_FORMAT, p2i(end));
2437
2438 // we need to clear the bits that correspond to arguments as they reside in the caller frame
2439 // or they will keep objects that are otherwise unreachable alive.
2440
2441 // Align `end` if UseCompressedOops is not set to avoid UB when calculating the bit index, since
2442 // `end` could be at an odd number of stack slots from `start`, i.e might not be oop aligned.
2443 // If that's the case the bit range corresponding to the last stack slot should not have bits set
2444 // anyways and we assert that before returning.
2445 address effective_end = UseCompressedOops ? end : align_down(end, wordSize);
2446 log_develop_trace(continuations)("clearing bitmap for " INTPTR_FORMAT " - " INTPTR_FORMAT, p2i(start), p2i(effective_end));
2447 stackChunkOop chunk = _cont.tail();
2448 chunk->bitmap().clear_range(chunk->bit_index_for(start), chunk->bit_index_for(effective_end));
2449 assert(effective_end == end || !chunk->bitmap().at(chunk->bit_index_for(effective_end)), "bit should not be set");
2450 }
2451
2452 intptr_t* ThawBase::handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case) {
2453 assert(preempt_kind == Continuation::freeze_on_wait || preempt_kind == Continuation::freeze_on_monitorenter, "");
2454 frame top(sp);
2455 assert(top.pc() == *(address*)(sp - frame::sender_sp_ret_address_offset()), "");
2456
2457 #if INCLUDE_JVMTI
2458 // Finish the VTMS transition.
2459 assert(_thread->is_in_VTMS_transition(), "must be");
2460 bool is_vthread = Continuation::continuation_scope(_cont.continuation()) == java_lang_VirtualThread::vthread_scope();
2461 if (is_vthread) {
2462 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
2463 jvmti_mount_end(_thread, _cont, top);
2464 } else {
2465 _thread->set_is_in_VTMS_transition(false);
2466 java_lang_Thread::set_is_in_VTMS_transition(_thread->vthread(), false);
2467 }
2468 }
2469 #endif
2470
2471 if (fast_case) {
2472 // If we thawed in the slow path the runtime stub/native wrapper frame already
2473 // has the correct fp (see ThawBase::new_stack_frame). On the fast path though,
2474 // we copied the fp patched during freeze, which will now have to be fixed.
2475 assert(top.is_runtime_frame() || top.is_native_frame(), "");
2476 int fsize = top.cb()->frame_size();
2477 patch_pd(top, sp + fsize);
2478 }
2479
2480 if (preempt_kind == Continuation::freeze_on_wait) {
2481 // Check now if we need to throw IE exception.
2482 if (_thread->pending_interrupted_exception()) {
2483 throw_interrupted_exception(_thread, top);
2484 _thread->set_pending_interrupted_exception(false);
2485 }
2486 } else if (top.is_runtime_frame()) {
2487 // The continuation might now run on a different platform thread than the previous time so
2488 // we need to adjust the current thread saved in the stub frame before restoring registers.
2489 JavaThread** thread_addr = frame::saved_thread_address(top);
2490 if (thread_addr != nullptr) *thread_addr = _thread;
2491 }
2492 return sp;
2493 }
2494
2495 void ThawBase::throw_interrupted_exception(JavaThread* current, frame& top) {
2496 ContinuationWrapper::SafepointOp so(current, _cont);
2497 // Since we might safepoint set the anchor so that the stack can be walked.
2498 set_anchor(current, top.sp());
2499 JRT_BLOCK
2500 THROW(vmSymbols::java_lang_InterruptedException());
2501 JRT_BLOCK_END
2502 clear_anchor(current);
2503 }
2504
2505 NOINLINE void ThawBase::recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames) {
2506 assert(hf.is_interpreted_frame(), "");
2507
2508 if (UNLIKELY(seen_by_gc())) {
2509 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance());
2510 }
2511
2512 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::InterpretedFrame>(caller, num_frames);
2513
2514 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2515
2516 _align_size += frame::align_wiggle; // possible added alignment for internal interpreted frame alignment om AArch64
2517
2518 frame f = new_stack_frame<ContinuationHelper::InterpretedFrame>(hf, caller, is_bottom_frame);
2519
2520 intptr_t* const stack_frame_top = f.sp() + frame::metadata_words_at_top;
2521 intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
2522 intptr_t* const heap_frame_top = hf.unextended_sp() + frame::metadata_words_at_top;
2523 intptr_t* const heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
2524
2525 assert(hf.is_heap_frame(), "should be");
2526 assert(!f.is_heap_frame(), "should not be");
2527
2528 const int fsize = pointer_delta_as_int(heap_frame_bottom, heap_frame_top);
2529 assert((stack_frame_bottom == stack_frame_top + fsize), "");
2534
2535 // Make sure the relativized locals is already set.
2536 assert(f.interpreter_frame_local_at(0) == stack_frame_bottom - 1, "invalid frame bottom");
2537
2538 derelativize_interpreted_frame_metadata(hf, f);
2539 patch(f, caller, is_bottom_frame);
2540
2541 assert(f.is_interpreted_frame_valid(_cont.thread()), "invalid thawed frame");
2542 assert(stack_frame_bottom <= ContinuationHelper::Frame::frame_top(caller), "");
2543
2544 CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();)
2545
2546 maybe_set_fastpath(f.sp());
2547
2548 Method* m = hf.interpreter_frame_method();
2549 assert(!m->is_native() || !is_bottom_frame, "should be top frame of thaw_top case; missing caller frame");
2550 const int locals = m->max_locals();
2551
2552 if (!is_bottom_frame) {
2553 // can only fix caller once this frame is thawed (due to callee saved regs)
2554 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance());
2555 } else if (_cont.tail()->has_bitmap() && locals > 0) {
2556 assert(hf.is_heap_frame(), "should be");
2557 address start = (address)(heap_frame_bottom - locals);
2558 address end = (address)heap_frame_bottom;
2559 clear_bitmap_bits(start, end);
2560 }
2561
2562 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2563 caller = f;
2564 }
2565
2566 void ThawBase::recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller) {
2567 assert(hf.is_compiled_frame(), "");
2568 assert(_preempted_case || !stub_caller, "stub caller not at preemption");
2569
2570 if (!stub_caller && UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2571 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance());
2572 }
2573
2574 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::CompiledFrame>(caller, num_frames);
2575
2576 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2577
2578 assert(caller.sp() == caller.unextended_sp(), "");
2579
2580 if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) {
2581 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_compiled_frame
2582 }
2583
2584 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2585 // yet laid out in the stack, and so the original_pc is not stored in it.
2586 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2587 frame f = new_stack_frame<ContinuationHelper::CompiledFrame>(hf, caller, is_bottom_frame);
2588 intptr_t* const stack_frame_top = f.sp();
2589 intptr_t* const heap_frame_top = hf.unextended_sp();
2590
2591 const int added_argsize = (is_bottom_frame || caller.is_interpreted_frame()) ? hf.compiled_frame_stack_argsize() : 0;
2610 assert(!f.is_deoptimized_frame(), "");
2611 if (hf.is_deoptimized_frame()) {
2612 maybe_set_fastpath(f.sp());
2613 } else if (_thread->is_interp_only_mode()
2614 || (stub_caller && f.cb()->as_nmethod()->is_marked_for_deoptimization())) {
2615 // The caller of the safepoint stub when the continuation is preempted is not at a call instruction, and so
2616 // cannot rely on nmethod patching for deopt.
2617 assert(_thread->is_interp_only_mode() || stub_caller, "expected a stub-caller");
2618
2619 log_develop_trace(continuations)("Deoptimizing thawed frame");
2620 DEBUG_ONLY(ContinuationHelper::Frame::patch_pc(f, nullptr));
2621
2622 f.deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2623 assert(f.is_deoptimized_frame(), "");
2624 assert(ContinuationHelper::Frame::is_deopt_return(f.raw_pc(), f), "");
2625 maybe_set_fastpath(f.sp());
2626 }
2627
2628 if (!is_bottom_frame) {
2629 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2630 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance());
2631 } else if (_cont.tail()->has_bitmap() && added_argsize > 0) {
2632 address start = (address)(heap_frame_top + ContinuationHelper::CompiledFrame::size(hf) + frame::metadata_words_at_top);
2633 int stack_args_slots = f.cb()->as_nmethod()->num_stack_arg_slots(false /* rounded */);
2634 int argsize_in_bytes = stack_args_slots * VMRegImpl::stack_slot_size;
2635 clear_bitmap_bits(start, start + argsize_in_bytes);
2636 }
2637
2638 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2639 caller = f;
2640 }
2641
2642 void ThawBase::recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames) {
2643 DEBUG_ONLY(_frames++;)
2644
2645 if (UNLIKELY(seen_by_gc())) {
2646 // Process the stub's caller here since we might need the full map.
2647 RegisterMap map(nullptr,
2648 RegisterMap::UpdateMap::include,
2649 RegisterMap::ProcessFrames::skip,
2650 RegisterMap::WalkContinuation::skip);
2651 map.set_include_argument_oops(false);
2652 _stream.next(&map);
2653 assert(!_stream.is_done(), "");
2654 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, &map);
2655 } else {
2656 _stream.next(SmallRegisterMap::instance());
2657 assert(!_stream.is_done(), "");
2658 }
2659
2660 recurse_thaw_compiled_frame(_stream.to_frame(), caller, num_frames, true);
2661
2662 assert(caller.is_compiled_frame(), "");
2663 assert(caller.sp() == caller.unextended_sp(), "");
2664
2665 DEBUG_ONLY(before_thaw_java_frame(hf, caller, false /*is_bottom_frame*/, num_frames);)
2666
2667 frame f = new_stack_frame<ContinuationHelper::StubFrame>(hf, caller, false);
2668 intptr_t* stack_frame_top = f.sp();
2669 intptr_t* heap_frame_top = hf.sp();
2670 int fsize = ContinuationHelper::StubFrame::size(hf);
2671
2672 copy_from_chunk(heap_frame_top - frame::metadata_words, stack_frame_top - frame::metadata_words,
2673 fsize + frame::metadata_words);
2674
2675 patch(f, caller, false /*is_bottom_frame*/);
2676
2677 // can only fix caller once this frame is thawed (due to callee saved regs)
2678 RegisterMap map(nullptr,
2679 RegisterMap::UpdateMap::include,
2680 RegisterMap::ProcessFrames::skip,
2681 RegisterMap::WalkContinuation::skip);
2682 map.set_include_argument_oops(false);
2683 f.oop_map()->update_register_map(&f, &map);
2684 ContinuationHelper::update_register_map_with_callee(caller, &map);
2685 _cont.tail()->fix_thawed_frame(caller, &map);
2686
2687 DEBUG_ONLY(after_thaw_java_frame(f, false /*is_bottom_frame*/);)
2688 caller = f;
2689 }
2690
2691 void ThawBase::recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames) {
2692 assert(hf.is_native_frame(), "");
2693 assert(_preempted_case && hf.cb()->as_nmethod()->method()->is_object_wait0(), "");
2694
2695 if (UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2696 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance());
2697 }
2698
2699 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::NativeFrame>(caller, num_frames);
2700 assert(!is_bottom_frame, "");
2701
2702 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2703
2704 assert(caller.sp() == caller.unextended_sp(), "");
2705
2706 if (caller.is_interpreted_frame()) {
2707 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_native_frame
2708 }
2709
2710 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2711 // yet laid out in the stack, and so the original_pc is not stored in it.
2712 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2713 frame f = new_stack_frame<ContinuationHelper::NativeFrame>(hf, caller, false /* bottom */);
2714 intptr_t* const stack_frame_top = f.sp();
2715 intptr_t* const heap_frame_top = hf.unextended_sp();
2716
2717 int fsize = ContinuationHelper::NativeFrame::size(hf);
2718 assert(fsize <= (int)(caller.unextended_sp() - f.unextended_sp()), "");
2719
2720 intptr_t* from = heap_frame_top - frame::metadata_words_at_bottom;
2721 intptr_t* to = stack_frame_top - frame::metadata_words_at_bottom;
2722 int sz = fsize + frame::metadata_words_at_bottom;
2723
2724 copy_from_chunk(from, to, sz); // copying good oops because we invoked barriers above
2725
2726 patch(f, caller, false /* bottom */);
2727
2728 // f.is_deoptimized_frame() is always false and we must test hf.is_deoptimized_frame() (see comment above)
2729 assert(!f.is_deoptimized_frame(), "");
2730 assert(!hf.is_deoptimized_frame(), "");
2731 assert(!f.cb()->as_nmethod()->is_marked_for_deoptimization(), "");
2732
2733 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2734 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance());
2735
2736 DEBUG_ONLY(after_thaw_java_frame(f, false /* bottom */);)
2737 caller = f;
2738 }
2739
2740 void ThawBase::finish_thaw(frame& f) {
2741 stackChunkOop chunk = _cont.tail();
2742
2743 if (chunk->is_empty()) {
2744 // Only remove chunk from list if it can't be reused for another freeze
2745 if (seen_by_gc()) {
2746 _cont.set_tail(chunk->parent());
2747 } else {
2748 chunk->set_has_mixed_frames(false);
2749 }
2750 chunk->set_max_thawing_size(0);
2751 } else {
2752 chunk->set_max_thawing_size(chunk->max_thawing_size() - _align_size);
2753 }
2754 assert(chunk->is_empty() == (chunk->max_thawing_size() == 0), "");
2755
2756 if (!is_aligned(f.sp(), frame::frame_alignment)) {
2757 assert(f.is_interpreted_frame(), "");
2758 f.set_sp(align_down(f.sp(), frame::frame_alignment));
2759 }
2760 push_return_frame(f);
2761 chunk->fix_thawed_frame(f, SmallRegisterMap::instance()); // can only fix caller after push_return_frame (due to callee saved regs)
2762
2763 assert(_cont.is_empty() == _cont.last_frame().is_empty(), "");
2764
2765 log_develop_trace(continuations)("thawed %d frames", _frames);
2766
2767 LogTarget(Trace, continuations) lt;
2768 if (lt.develop_is_enabled()) {
2769 LogStream ls(lt);
2770 ls.print_cr("top hframe after (thaw):");
2771 _cont.last_frame().print_value_on(&ls);
2772 }
2773 }
2774
2775 void ThawBase::push_return_frame(frame& f) { // see generate_cont_thaw
2776 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == f.cb()->as_nmethod()->is_deopt_pc(f.raw_pc()), "");
2777 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == (f.pc() != f.raw_pc()), "");
2778
2779 LogTarget(Trace, continuations) lt;
2780 if (lt.develop_is_enabled()) {
2781 LogStream ls(lt);
2803
2804 ContinuationEntry* entry = thread->last_continuation();
2805 assert(entry != nullptr, "");
2806 oop oopCont = entry->cont_oop(thread);
2807
2808 assert(!jdk_internal_vm_Continuation::done(oopCont), "");
2809 assert(oopCont == get_continuation(thread), "");
2810 verify_continuation(oopCont);
2811
2812 assert(entry->is_virtual_thread() == (entry->scope(thread) == java_lang_VirtualThread::vthread_scope()), "");
2813
2814 ContinuationWrapper cont(thread, oopCont);
2815 log_develop_debug(continuations)("THAW #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
2816
2817 #ifdef ASSERT
2818 set_anchor_to_entry(thread, cont.entry());
2819 log_frames(thread);
2820 clear_anchor(thread);
2821 #endif
2822
2823 DEBUG_ONLY(bool preempted = cont.tail()->preempted();)
2824 Thaw<ConfigT> thw(thread, cont);
2825 intptr_t* const sp = thw.thaw(kind);
2826 assert(is_aligned(sp, frame::frame_alignment), "");
2827 DEBUG_ONLY(log_frames_after_thaw(thread, cont, sp, preempted);)
2828
2829 CONT_JFR_ONLY(thw.jfr_info().post_jfr_event(&event, cont.continuation(), thread);)
2830
2831 verify_continuation(cont.continuation());
2832 log_develop_debug(continuations)("=== End of thaw #" INTPTR_FORMAT, cont.hash());
2833
2834 return sp;
2835 }
2836
2837 #ifdef ASSERT
2838 static void do_deopt_after_thaw(JavaThread* thread) {
2839 int i = 0;
2840 StackFrameStream fst(thread, true, false);
2841 fst.register_map()->set_include_argument_oops(false);
2842 ContinuationHelper::update_register_map_with_callee(*fst.current(), fst.register_map());
2843 for (; !fst.is_done(); fst.next()) {
2844 if (fst.current()->cb()->is_nmethod()) {
2845 nmethod* nm = fst.current()->cb()->as_nmethod();
2846 if (!nm->method()->is_continuation_native_intrinsic()) {
2847 nm->make_deoptimized();
2904 if (!fr.is_interpreted_frame()) {
2905 st->print_cr("size: %d argsize: %d",
2906 ContinuationHelper::NonInterpretedUnknownFrame::size(fr),
2907 ContinuationHelper::NonInterpretedUnknownFrame::stack_argsize(fr));
2908 }
2909 VMReg reg = fst.register_map()->find_register_spilled_here(cl.p(), fst.current()->sp());
2910 if (reg != nullptr) {
2911 st->print_cr("Reg %s %d", reg->name(), reg->is_stack() ? (int)reg->reg2stack() : -99);
2912 }
2913 cl.reset();
2914 DEBUG_ONLY(thread->print_frame_layout();)
2915 if (chunk != nullptr) {
2916 chunk->print_on(true, st);
2917 }
2918 return false;
2919 }
2920 }
2921 return true;
2922 }
2923
2924 static void log_frames(JavaThread* thread) {
2925 const static int show_entry_callers = 3;
2926 LogTarget(Trace, continuations) lt;
2927 if (!lt.develop_is_enabled()) {
2928 return;
2929 }
2930 LogStream ls(lt);
2931
2932 ls.print_cr("------- frames --------- for thread " INTPTR_FORMAT, p2i(thread));
2933 if (!thread->has_last_Java_frame()) {
2934 ls.print_cr("NO ANCHOR!");
2935 }
2936
2937 RegisterMap map(thread,
2938 RegisterMap::UpdateMap::include,
2939 RegisterMap::ProcessFrames::include,
2940 RegisterMap::WalkContinuation::skip);
2941 map.set_include_argument_oops(false);
2942
2943 if (false) {
2944 for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) {
2945 f.print_on(&ls);
2946 }
2947 } else {
2949 ResetNoHandleMark rnhm;
2950 ResourceMark rm;
2951 HandleMark hm(Thread::current());
2952 FrameValues values;
2953
2954 int i = 0;
2955 int post_entry = -1;
2956 for (frame f = thread->last_frame(); !f.is_first_frame(); f = f.sender(&map), i++) {
2957 f.describe(values, i, &map, i == 0);
2958 if (post_entry >= 0 || Continuation::is_continuation_enterSpecial(f))
2959 post_entry++;
2960 if (post_entry >= show_entry_callers)
2961 break;
2962 }
2963 values.print_on(thread, &ls);
2964 }
2965
2966 ls.print_cr("======= end frames =========");
2967 }
2968
2969 static void log_frames_after_thaw(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp, bool preempted) {
2970 intptr_t* sp0 = sp;
2971 address pc0 = *(address*)(sp - frame::sender_sp_ret_address_offset());
2972
2973 if (preempted && sp0 == cont.entrySP()) {
2974 // Still preempted (monitor not acquired) so no frames were thawed.
2975 assert(cont.tail()->preempted(), "");
2976 set_anchor(thread, cont.entrySP(), cont.entryPC());
2977 } else {
2978 set_anchor(thread, sp0);
2979 }
2980
2981 log_frames(thread);
2982 if (LoomVerifyAfterThaw) {
2983 assert(do_verify_after_thaw(thread, cont.tail(), tty), "");
2984 }
2985 assert(ContinuationEntry::assert_entry_frame_laid_out(thread), "");
2986 clear_anchor(thread);
2987
2988 LogTarget(Trace, continuations) lt;
2989 if (lt.develop_is_enabled()) {
2990 LogStream ls(lt);
2991 ls.print_cr("Jumping to frame (thaw):");
2992 frame(sp).print_value_on(&ls);
2993 }
2994 }
2995 #endif // ASSERT
2996
2997 #include CPU_HEADER_INLINE(continuationFreezeThaw)
2998
2999 #ifdef ASSERT
3000 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st) {
3001 ResourceMark rm;
3002 FrameValues values;
3003 assert(f.get_cb() != nullptr, "");
3004 RegisterMap map(f.is_heap_frame() ?
3005 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 = true);
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);
1085 log_develop_trace(continuations)("Reusing chunk mixed: %d empty: %d", chunk->has_mixed_frames(), chunk->is_empty());
1086 if (chunk->is_empty()) {
1087 int sp = chunk->stack_size() - argsize_md;
1088 chunk->set_sp(sp);
1089 chunk->set_bottom(sp);
1090 _freeze_size += overlap;
1091 assert(chunk->max_thawing_size() == 0, "");
1092 } DEBUG_ONLY(else empty_chunk = false;)
1093 }
1094 assert(!chunk->is_gc_mode(), "");
1095 assert(!chunk->has_bitmap(), "");
1096 chunk->set_has_mixed_frames(true);
1097
1098 assert(chunk->requires_barriers() == _barriers, "");
1099 assert(!_barriers || chunk->is_empty(), "");
1100
1101 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).is_done(), "");
1102 assert(!chunk->is_empty() || StackChunkFrameStream<ChunkFrames::Mixed>(chunk).to_frame().is_empty(), "");
1103
1104 if (_preempt) {
1105 frame top_frame = _thread->last_frame();
1106 if (top_frame.is_interpreted_frame()) {
1107 // Some platforms do not save the last_sp in the top interpreter frame on VM calls.
1108 // We need it so that on resume we can restore the sp to the right place, since
1109 // thawing might add an alignment word to the expression stack (see finish_thaw()).
1110 // We do it now that we know freezing will be successful.
1111 prepare_freeze_interpreted_top_frame(top_frame);
1112 }
1113
1114 // Do this now so should_process_args_at_top() is set before calling finish_freeze
1115 // in case we might need to apply GC barriers to frames in this stackChunk.
1116 if (_thread->at_preemptable_init()) {
1117 assert(top_frame.is_interpreted_frame(), "only InterpreterRuntime::_new/resolve_from_cache allowed");
1118 chunk->set_at_klass_init(true);
1119 methodHandle m(_thread, top_frame.interpreter_frame_method());
1120 Bytecode_invoke call = Bytecode_invoke_check(m, top_frame.interpreter_frame_bci());
1121 assert(!call.is_valid() || call.is_invokestatic(), "only invokestatic allowed");
1122 if (call.is_invokestatic() && call.size_of_parameters() > 0) {
1123 assert(top_frame.interpreter_frame_expression_stack_size() > 0, "should have parameters in exp stack");
1124 chunk->set_has_args_at_top(true);
1125 }
1126 }
1127 }
1128
1129 // We unwind frames after the last safepoint so that the GC will have found the oops in the frames, but before
1130 // writing into the chunk. This is so that an asynchronous stack walk (not at a safepoint) that suspends us here
1131 // will either see no continuation or a consistent chunk.
1132 unwind_frames();
1133
1134 chunk->set_max_thawing_size(chunk->max_thawing_size() + _freeze_size - _monitors_in_lockstack - frame::metadata_words);
1135
1136 if (lt.develop_is_enabled()) {
1137 LogStream ls(lt);
1138 ls.print_cr("top chunk:");
1139 chunk->print_on(&ls);
1140 }
1141
1142 if (_monitors_in_lockstack > 0) {
1143 freeze_lockstack(chunk);
1144 }
1145
1617 // Some GCs could put direct allocations in old gen for slow-path
1618 // allocations; need to explicitly check if that was the case.
1619 _barriers = chunk->requires_barriers();
1620 }
1621 }
1622
1623 if (_barriers) {
1624 log_develop_trace(continuations)("allocation requires barriers");
1625 }
1626
1627 assert(chunk->parent() == nullptr || chunk->parent()->is_stackChunk(), "");
1628
1629 return chunk;
1630 }
1631
1632 void FreezeBase::throw_stack_overflow_on_humongous_chunk() {
1633 ContinuationWrapper::SafepointOp so(_thread, _cont); // could also call _cont.done() instead
1634 Exceptions::_throw_msg(_thread, __FILE__, __LINE__, vmSymbols::java_lang_StackOverflowError(), "Humongous stack chunk");
1635 }
1636
1637 class AnchorMark : public StackObj {
1638 JavaThread* _current;
1639 frame& _top_frame;
1640 intptr_t* _last_sp_from_frame;
1641 bool _is_interpreted;
1642
1643 public:
1644 AnchorMark(JavaThread* current, frame& f) : _current(current), _top_frame(f), _is_interpreted(false) {
1645 intptr_t* sp = anchor_mark_set_pd();
1646 set_anchor(_current, sp);
1647 }
1648 ~AnchorMark() {
1649 clear_anchor(_current);
1650 anchor_mark_clear_pd();
1651 }
1652 inline intptr_t* anchor_mark_set_pd();
1653 inline void anchor_mark_clear_pd();
1654 };
1655
1656 #if INCLUDE_JVMTI
1657 static int num_java_frames(ContinuationWrapper& cont) {
1658 ResourceMark rm; // used for scope traversal in num_java_frames(nmethod*, address)
1659 int count = 0;
1660 for (stackChunkOop chunk = cont.tail(); chunk != nullptr; chunk = chunk->parent()) {
1661 count += chunk->num_java_frames();
1662 }
1663 return count;
1664 }
1665
1666 static void invalidate_jvmti_stack(JavaThread* thread) {
1667 JvmtiThreadState *state = thread->jvmti_thread_state();
1668 if (state != nullptr) {
1669 state->invalidate_cur_stack_depth();
1670 }
1671 }
1672
1673 static void jvmti_yield_cleanup(JavaThread* thread, ContinuationWrapper& cont) {
1674 if (JvmtiExport::has_frame_pops(thread)) {
1675 int num_frames = num_java_frames(cont);
1676
1677 ContinuationWrapper::SafepointOp so(Thread::current(), cont);
1678 JvmtiExport::continuation_yield_cleanup(JavaThread::current(), num_frames);
1679 }
1680 invalidate_jvmti_stack(thread);
1681 }
1682
1683 static void jvmti_mount_end(JavaThread* current, ContinuationWrapper& cont, frame top, Continuation::preempt_kind pk) {
1684 assert(current->vthread() != nullptr, "must be");
1685
1686 HandleMarkCleaner hm(current); // Cleanup vth and so._conth Handles
1687 Handle vth(current, current->vthread());
1688 ContinuationWrapper::SafepointOp so(current, cont);
1689
1690 AnchorMark am(current, top); // Set anchor so that the stack is walkable.
1691
1692 JRT_BLOCK
1693 JvmtiVTMSTransitionDisabler::VTMS_vthread_mount((jthread)vth.raw_value(), false);
1694
1695 if (current->pending_contended_entered_event()) {
1696 // No monitor JVMTI events for ObjectLocker case.
1697 if (pk != Continuation::object_locker) {
1698 JvmtiExport::post_monitor_contended_entered(current, current->contended_entered_monitor());
1699 }
1700 current->set_contended_entered_monitor(nullptr);
1701 }
1702 JRT_BLOCK_END
1703 }
1704 #endif // INCLUDE_JVMTI
1705
1706 #ifdef ASSERT
1707 // There are no interpreted frames if we're not called from the interpreter and we haven't ancountered an i2c
1708 // adapter or called Deoptimization::unpack_frames. As for native frames, upcalls from JNI also go through the
1709 // interpreter (see JavaCalls::call_helper), while the UpcallLinker explicitly sets cont_fastpath.
1710 bool FreezeBase::check_valid_fast_path() {
1711 ContinuationEntry* ce = _thread->last_continuation();
1712 RegisterMap map(_thread,
1713 RegisterMap::UpdateMap::skip,
1714 RegisterMap::ProcessFrames::skip,
1715 RegisterMap::WalkContinuation::skip);
1716 map.set_include_argument_oops(false);
1717 bool is_top_frame = true;
1718 for (frame f = freeze_start_frame(); Continuation::is_frame_in_continuation(ce, f); f = f.sender(&map), is_top_frame = false) {
1719 if (!((f.is_compiled_frame() && !f.is_deoptimized_frame()) || (is_top_frame && (f.is_runtime_frame() || f.is_native_frame())))) {
1720 return false;
1721 }
1722 }
1723 return true;
1724 }
1725
1726 static void verify_frame_kind(const frame& top, Continuation::preempt_kind preempt_kind, Method** m_ptr, const char** code_name_ptr, int* bci_ptr) {
1727 Method* m;
1728 const char* code_name;
1729 int bci;
1730 if (preempt_kind == Continuation::monitorenter) {
1731 assert(top.is_interpreted_frame() || top.is_runtime_frame(), "");
1732 bool at_sync_method;
1733 if (top.is_interpreted_frame()) {
1734 m = top.interpreter_frame_method();
1735 assert(!m->is_native() || m->is_synchronized(), "invalid method %s", m->external_name());
1736 address bcp = top.interpreter_frame_bcp();
1737 assert(bcp != 0 || m->is_native(), "");
1738 at_sync_method = m->is_synchronized() && (bcp == 0 || bcp == m->code_base());
1739 // bcp is advanced on monitorenter before making the VM call, adjust for that.
1740 bool at_sync_bytecode = bcp > m->code_base() && Bytecode(m, bcp - 1).code() == Bytecodes::Code::_monitorenter;
1741 assert(at_sync_method || at_sync_bytecode, "");
1742 bci = at_sync_method ? -1 : top.interpreter_frame_bci();
1743 } else {
1744 JavaThread* current = JavaThread::current();
1745 ResourceMark rm(current);
1746 CodeBlob* cb = top.cb();
1747 RegisterMap reg_map(current,
1748 RegisterMap::UpdateMap::skip,
1749 RegisterMap::ProcessFrames::skip,
1750 RegisterMap::WalkContinuation::skip);
1751 frame fr = top.sender(®_map);
1752 vframe* vf = vframe::new_vframe(&fr, ®_map, current);
1753 compiledVFrame* cvf = compiledVFrame::cast(vf);
1754 m = cvf->method();
1755 bci = cvf->scope()->bci();
1756 at_sync_method = bci == SynchronizationEntryBCI;
1757 assert(!at_sync_method || m->is_synchronized(), "bci is %d but method %s is not synchronized", bci, m->external_name());
1758 bool is_c1_monitorenter = false, is_c2_monitorenter = false;
1759 COMPILER1_PRESENT(is_c1_monitorenter = cb == Runtime1::blob_for(StubId::c1_monitorenter_id) ||
1760 cb == Runtime1::blob_for(StubId::c1_monitorenter_nofpu_id);)
1761 COMPILER2_PRESENT(is_c2_monitorenter = cb == CodeCache::find_blob(OptoRuntime::complete_monitor_locking_Java());)
1762 assert(is_c1_monitorenter || is_c2_monitorenter, "wrong runtime stub frame");
1763 }
1764 code_name = at_sync_method ? "synchronized method" : "monitorenter";
1765 } else if (preempt_kind == Continuation::object_wait) {
1766 assert(top.is_interpreted_frame() || top.is_native_frame(), "");
1767 m = top.is_interpreted_frame() ? top.interpreter_frame_method() : top.cb()->as_nmethod()->method();
1768 assert(m->is_object_wait0(), "");
1769 bci = 0;
1770 code_name = "";
1771 } else {
1772 assert(preempt_kind == Continuation::object_locker, "invalid preempt kind");
1773 assert(top.is_interpreted_frame(), "");
1774 m = top.interpreter_frame_method();
1775 Bytecode current_bytecode = Bytecode(m, top.interpreter_frame_bcp());
1776 Bytecodes::Code code = current_bytecode.code();
1777 assert(code == Bytecodes::Code::_new || code == Bytecodes::Code::_invokestatic ||
1778 (code == Bytecodes::Code::_getstatic || code == Bytecodes::Code::_putstatic), "invalid bytecode");
1779 bci = top.interpreter_frame_bci();
1780 code_name = Bytecodes::name(current_bytecode.code());
1781 }
1782 assert(bci >= 0 || m->is_synchronized(), "invalid bci:%d at method %s", bci, m->external_name());
1783
1784 if (m_ptr != nullptr) {
1785 *m_ptr = m;
1786 *code_name_ptr = code_name;
1787 *bci_ptr = bci;
1788 }
1789 }
1790
1791 static void log_preempt_after_freeze(ContinuationWrapper& cont) {
1792 JavaThread* current = cont.thread();
1793 int64_t tid = current->monitor_owner_id();
1794
1795 StackChunkFrameStream<ChunkFrames::Mixed> sfs(cont.tail());
1796 frame top_frame = sfs.to_frame();
1797 bool at_init = current->at_preemptable_init();
1798 bool at_enter = current->current_pending_monitor() != nullptr;
1799 bool at_wait = current->current_waiting_monitor() != nullptr;
1800 assert((at_enter && !at_wait) || (!at_enter && at_wait), "");
1801 Continuation::preempt_kind pk = at_init ? Continuation::object_locker : at_enter ? Continuation::monitorenter : Continuation::object_wait;
1802
1803 Method* m = nullptr;
1804 const char* code_name = nullptr;
1805 int bci = InvalidFrameStateBci;
1806 verify_frame_kind(top_frame, pk, &m, &code_name, &bci);
1807 assert(m != nullptr && code_name != nullptr && bci != InvalidFrameStateBci, "should be set");
1808
1809 ResourceMark rm(current);
1810 if (bci < 0) {
1811 log_trace(continuations, preempt)("Preempted " INT64_FORMAT " while synchronizing on %smethod %s", tid, m->is_native() ? "native " : "", m->external_name());
1812 } else if (m->is_object_wait0()) {
1813 log_trace(continuations, preempt)("Preempted " INT64_FORMAT " at native method %s", tid, m->external_name());
1814 } else {
1815 Klass* k = current->preempt_init_klass();
1816 assert(k != nullptr || !at_init, "");
1817 log_trace(continuations, preempt)("Preempted " INT64_FORMAT " at %s(bci:%d) in method %s %s%s", tid, code_name, bci,
1818 m->external_name(), at_init ? "trying to initialize klass " : "", at_init ? k->external_name() : "");
1819 }
1820 }
1821 #endif // ASSERT
1822
1823 static inline freeze_result freeze_epilog(ContinuationWrapper& cont) {
1824 verify_continuation(cont.continuation());
1825 assert(!cont.is_empty(), "");
1826
1827 log_develop_debug(continuations)("=== End of freeze cont ### #" INTPTR_FORMAT, cont.hash());
1828 return freeze_ok;
1829 }
1830
1831 static freeze_result freeze_epilog(JavaThread* thread, ContinuationWrapper& cont, freeze_result res) {
1832 if (UNLIKELY(res != freeze_ok)) {
1833 JFR_ONLY(thread->set_last_freeze_fail_result(res);)
1834 verify_continuation(cont.continuation());
1835 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1836 return res;
1837 }
1838
1839 JVMTI_ONLY(jvmti_yield_cleanup(thread, cont)); // can safepoint
1840 return freeze_epilog(cont);
1841 }
1842
1843 static freeze_result preempt_epilog(ContinuationWrapper& cont, freeze_result res, frame& old_last_frame) {
1844 if (UNLIKELY(res != freeze_ok)) {
1845 verify_continuation(cont.continuation());
1846 log_develop_trace(continuations)("=== end of freeze (fail %d)", res);
1847 return res;
1848 }
1849
1850 // Set up things so that on return to Java we jump to preempt stub.
1851 patch_return_pc_with_preempt_stub(old_last_frame);
1852 cont.tail()->set_preempted(true);
1853 DEBUG_ONLY(log_preempt_after_freeze(cont);)
1854 return freeze_epilog(cont);
1855 }
1856
1857 template<typename ConfigT, bool preempt>
1858 static inline freeze_result freeze_internal(JavaThread* current, intptr_t* const sp) {
1859 assert(!current->has_pending_exception(), "");
1860
1861 #ifdef ASSERT
1862 log_trace(continuations)("~~~~ freeze sp: " INTPTR_FORMAT "JavaThread: " INTPTR_FORMAT, p2i(current->last_continuation()->entry_sp()), p2i(current));
1863 log_frames(current);
1864 #endif
1865
1866 CONT_JFR_ONLY(EventContinuationFreeze event;)
1867
1868 ContinuationEntry* entry = current->last_continuation();
1869
1870 oop oopCont = entry->cont_oop(current);
1871 assert(oopCont == current->last_continuation()->cont_oop(current), "");
1872 assert(ContinuationEntry::assert_entry_frame_laid_out(current), "");
1873
2029 // 300 is an estimate for stack size taken for this native code, in addition to StackShadowPages
2030 // for the Java frames in the check below.
2031 if (!stack_overflow_check(thread, size + 300, bottom)) {
2032 return 0;
2033 }
2034
2035 log_develop_trace(continuations)("prepare_thaw bottom: " INTPTR_FORMAT " top: " INTPTR_FORMAT " size: %d",
2036 p2i(bottom), p2i(bottom - size), size);
2037 return size;
2038 }
2039
2040 class ThawBase : public StackObj {
2041 protected:
2042 JavaThread* _thread;
2043 ContinuationWrapper& _cont;
2044 CONT_JFR_ONLY(FreezeThawJfrInfo _jfr_info;)
2045
2046 intptr_t* _fastpath;
2047 bool _barriers;
2048 bool _preempted_case;
2049 bool _process_args_at_top;
2050 intptr_t* _top_unextended_sp_before_thaw;
2051 int _align_size;
2052 DEBUG_ONLY(intptr_t* _top_stack_address);
2053
2054 // Only used for preemption on ObjectLocker
2055 ObjectMonitor* _monitor;
2056
2057 StackChunkFrameStream<ChunkFrames::Mixed> _stream;
2058
2059 NOT_PRODUCT(int _frames;)
2060
2061 protected:
2062 ThawBase(JavaThread* thread, ContinuationWrapper& cont) :
2063 _thread(thread), _cont(cont),
2064 _fastpath(nullptr) {
2065 DEBUG_ONLY(_top_unextended_sp_before_thaw = nullptr;)
2066 assert (cont.tail() != nullptr, "no last chunk");
2067 DEBUG_ONLY(_top_stack_address = _cont.entrySP() - thaw_size(cont.tail());)
2068 }
2069
2070 void clear_chunk(stackChunkOop chunk);
2071 template<bool check_stub>
2072 int remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize);
2073 void copy_from_chunk(intptr_t* from, intptr_t* to, int size);
2074
2075 void thaw_lockstack(stackChunkOop chunk);
2076
2077 // fast path
2078 inline void prefetch_chunk_pd(void* start, int size_words);
2079 void patch_return(intptr_t* sp, bool is_last);
2080
2081 intptr_t* handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case);
2082 inline intptr_t* push_cleanup_continuation();
2083 inline intptr_t* push_preempt_adapter();
2084 intptr_t* redo_vmcall(JavaThread* current, frame& top);
2085 void throw_interrupted_exception(JavaThread* current, frame& top);
2086
2087 void recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case);
2088 void finish_thaw(frame& f);
2089
2090 private:
2091 template<typename FKind> bool recurse_thaw_java_frame(frame& caller, int num_frames);
2092 void finalize_thaw(frame& entry, int argsize);
2093
2094 inline bool seen_by_gc();
2095
2096 inline void before_thaw_java_frame(const frame& hf, const frame& caller, bool bottom, int num_frame);
2097 inline void after_thaw_java_frame(const frame& f, bool bottom);
2098 inline void patch(frame& f, const frame& caller, bool bottom);
2099 void clear_bitmap_bits(address start, address end);
2100
2101 NOINLINE void recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames, bool is_top);
2102 void recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller);
2103 void recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames);
2104 void recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames);
2105
2106 void push_return_frame(frame& f);
2107 inline frame new_entry_frame();
2108 template<typename FKind> frame new_stack_frame(const frame& hf, frame& caller, bool bottom);
2109 inline void patch_pd(frame& f, const frame& sender);
2110 inline void patch_pd(frame& f, intptr_t* caller_sp);
2111 inline intptr_t* align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom);
2112
2113 void maybe_set_fastpath(intptr_t* sp) { if (sp > _fastpath) _fastpath = sp; }
2114
2115 static inline void derelativize_interpreted_frame_metadata(const frame& hf, const frame& f);
2116
2117 public:
2118 CONT_JFR_ONLY(FreezeThawJfrInfo& jfr_info() { return _jfr_info; })
2119 };
2120
2121 template <typename ConfigT>
2181 chunk->set_sp(chunk->bottom());
2182 chunk->set_max_thawing_size(0);
2183 }
2184
2185 template<bool check_stub>
2186 int ThawBase::remove_top_compiled_frame_from_chunk(stackChunkOop chunk, int &argsize) {
2187 bool empty = false;
2188 StackChunkFrameStream<ChunkFrames::CompiledOnly> f(chunk);
2189 DEBUG_ONLY(intptr_t* const chunk_sp = chunk->start_address() + chunk->sp();)
2190 assert(chunk_sp == f.sp(), "");
2191 assert(chunk_sp == f.unextended_sp(), "");
2192
2193 int frame_size = f.cb()->frame_size();
2194 argsize = f.stack_argsize();
2195
2196 assert(!f.is_stub() || check_stub, "");
2197 if (check_stub && f.is_stub()) {
2198 // If we don't thaw the top compiled frame too, after restoring the saved
2199 // registers back in Java, we would hit the return barrier to thaw one more
2200 // frame effectively overwriting the restored registers during that call.
2201 f.next(SmallRegisterMap::instance_no_args(), true /* stop */);
2202 assert(!f.is_done(), "");
2203
2204 f.get_cb();
2205 assert(f.is_compiled(), "");
2206 frame_size += f.cb()->frame_size();
2207 argsize = f.stack_argsize();
2208
2209 if (f.cb()->as_nmethod()->is_marked_for_deoptimization()) {
2210 // The caller of the runtime stub when the continuation is preempted is not at a
2211 // Java call instruction, and so cannot rely on nmethod patching for deopt.
2212 log_develop_trace(continuations)("Deoptimizing runtime stub caller");
2213 f.to_frame().deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2214 }
2215 }
2216
2217 f.next(SmallRegisterMap::instance_no_args(), true /* stop */);
2218 empty = f.is_done();
2219 assert(!empty || argsize == chunk->argsize(), "");
2220
2221 if (empty) {
2222 clear_chunk(chunk);
2223 } else {
2224 chunk->set_sp(chunk->sp() + frame_size);
2225 chunk->set_max_thawing_size(chunk->max_thawing_size() - frame_size);
2226 // We set chunk->pc to the return pc into the next frame
2227 chunk->set_pc(f.pc());
2228 #ifdef ASSERT
2229 {
2230 intptr_t* retaddr_slot = (chunk_sp
2231 + frame_size
2232 - frame::sender_sp_ret_address_offset());
2233 assert(f.pc() == ContinuationHelper::return_address_at(retaddr_slot),
2234 "unexpected pc");
2235 }
2236 #endif
2237 }
2357 return rs.sp();
2358 }
2359
2360 inline bool ThawBase::seen_by_gc() {
2361 return _barriers || _cont.tail()->is_gc_mode();
2362 }
2363
2364 static inline void relativize_chunk_concurrently(stackChunkOop chunk) {
2365 #if INCLUDE_ZGC || INCLUDE_SHENANDOAHGC
2366 if (UseZGC || UseShenandoahGC) {
2367 chunk->relativize_derived_pointers_concurrently();
2368 }
2369 #endif
2370 }
2371
2372 template <typename ConfigT>
2373 NOINLINE intptr_t* Thaw<ConfigT>::thaw_slow(stackChunkOop chunk, Continuation::thaw_kind kind) {
2374 Continuation::preempt_kind preempt_kind;
2375 bool retry_fast_path = false;
2376
2377 _process_args_at_top = false;
2378 _preempted_case = chunk->preempted();
2379 if (_preempted_case) {
2380 ObjectWaiter* waiter = java_lang_VirtualThread::objectWaiter(_thread->vthread());
2381 if (waiter != nullptr) {
2382 // Mounted again after preemption. Resume the pending monitor operation,
2383 // which will be either a monitorenter or Object.wait() call.
2384 ObjectMonitor* mon = waiter->monitor();
2385 preempt_kind = waiter->is_wait() ? Continuation::object_wait : Continuation::monitorenter;
2386
2387 bool mon_acquired = mon->resume_operation(_thread, waiter, _cont);
2388 assert(!mon_acquired || mon->has_owner(_thread), "invariant");
2389 if (!mon_acquired) {
2390 // Failed to acquire monitor. Return to enterSpecial to unmount again.
2391 log_develop_trace(continuations, preempt)("Failed to acquire monitor, unmounting again");
2392 return push_cleanup_continuation();
2393 }
2394 _monitor = mon; // remember monitor since we might need it on handle_preempted_continuation()
2395 chunk = _cont.tail(); // reload oop in case of safepoint in resume_operation (if posting JVMTI events).
2396 JVMTI_ONLY(assert(_thread->contended_entered_monitor() == nullptr || _thread->contended_entered_monitor() == _monitor, ""));
2397 } else {
2398 // Preemption cancelled on moniterenter or ObjectLocker case. We
2399 // actually acquired the monitor after freezing all frames so no
2400 // need to call resume_operation. If this is the ObjectLocker case
2401 // we released the monitor already at ~ObjectLocker, so here we set
2402 // _monitor to nullptr to indicate there is no need to release it later.
2403 preempt_kind = Continuation::monitorenter;
2404 _monitor = nullptr;
2405 }
2406
2407 // Call this first to avoid racing with GC threads later when modifying the chunk flags.
2408 relativize_chunk_concurrently(chunk);
2409
2410 if (chunk->at_klass_init()) {
2411 preempt_kind = Continuation::object_locker;
2412 chunk->set_at_klass_init(false);
2413 _process_args_at_top = chunk->has_args_at_top();
2414 if (_process_args_at_top) {
2415 // Only needed for the top frame which will be thawed.
2416 chunk->set_has_args_at_top(false);
2417 }
2418 }
2419 chunk->set_preempted(false);
2420 retry_fast_path = true;
2421 } else {
2422 relativize_chunk_concurrently(chunk);
2423 }
2424
2425 // On first thaw after freeze restore oops to the lockstack if any.
2426 assert(chunk->lockstack_size() == 0 || kind == Continuation::thaw_top, "");
2427 if (kind == Continuation::thaw_top && chunk->lockstack_size() > 0) {
2428 thaw_lockstack(chunk);
2429 retry_fast_path = true;
2430 }
2431
2432 // Retry the fast path now that we possibly cleared the FLAG_HAS_LOCKSTACK
2433 // and FLAG_PREEMPTED flags from the stackChunk.
2434 if (retry_fast_path && can_thaw_fast(chunk)) {
2435 intptr_t* sp = thaw_fast<true>(chunk);
2436 if (_preempted_case) {
2437 return handle_preempted_continuation(sp, preempt_kind, true /* fast_case */);
2438 }
2482
2483 intptr_t* sp = caller.sp();
2484
2485 if (_preempted_case) {
2486 return handle_preempted_continuation(sp, preempt_kind, false /* fast_case */);
2487 }
2488 return sp;
2489 }
2490
2491 void ThawBase::recurse_thaw(const frame& heap_frame, frame& caller, int num_frames, bool top_on_preempt_case) {
2492 log_develop_debug(continuations)("thaw num_frames: %d", num_frames);
2493 assert(!_cont.is_empty(), "no more frames");
2494 assert(num_frames > 0, "");
2495 assert(!heap_frame.is_empty(), "");
2496
2497 if (top_on_preempt_case && (heap_frame.is_native_frame() || heap_frame.is_runtime_frame())) {
2498 heap_frame.is_native_frame() ? recurse_thaw_native_frame(heap_frame, caller, 2) : recurse_thaw_stub_frame(heap_frame, caller, 2);
2499 } else if (!heap_frame.is_interpreted_frame()) {
2500 recurse_thaw_compiled_frame(heap_frame, caller, num_frames, false);
2501 } else {
2502 recurse_thaw_interpreted_frame(heap_frame, caller, num_frames, top_on_preempt_case);
2503 }
2504 }
2505
2506 template<typename FKind>
2507 bool ThawBase::recurse_thaw_java_frame(frame& caller, int num_frames) {
2508 assert(num_frames > 0, "");
2509
2510 DEBUG_ONLY(_frames++;)
2511
2512 int argsize = _stream.stack_argsize();
2513
2514 _stream.next(SmallRegisterMap::instance_no_args());
2515 assert(_stream.to_frame().is_empty() == _stream.is_done(), "");
2516
2517 // we never leave a compiled caller of an interpreted frame as the top frame in the chunk
2518 // as it makes detecting that situation and adjusting unextended_sp tricky
2519 if (num_frames == 1 && !_stream.is_done() && FKind::interpreted && _stream.is_compiled()) {
2520 log_develop_trace(continuations)("thawing extra compiled frame to not leave a compiled interpreted-caller at top");
2521 num_frames++;
2522 }
2523
2524 if (num_frames == 1 || _stream.is_done()) { // end recursion
2525 finalize_thaw(caller, FKind::interpreted ? 0 : argsize);
2526 return true; // bottom
2527 } else { // recurse
2528 recurse_thaw(_stream.to_frame(), caller, num_frames - 1, false /* top_on_preempt_case */);
2529 return false;
2530 }
2531 }
2532
2533 void ThawBase::finalize_thaw(frame& entry, int argsize) {
2534 stackChunkOop chunk = _cont.tail();
2599
2600 void ThawBase::clear_bitmap_bits(address start, address end) {
2601 assert(is_aligned(start, wordSize), "should be aligned: " PTR_FORMAT, p2i(start));
2602 assert(is_aligned(end, VMRegImpl::stack_slot_size), "should be aligned: " PTR_FORMAT, p2i(end));
2603
2604 // we need to clear the bits that correspond to arguments as they reside in the caller frame
2605 // or they will keep objects that are otherwise unreachable alive.
2606
2607 // Align `end` if UseCompressedOops is not set to avoid UB when calculating the bit index, since
2608 // `end` could be at an odd number of stack slots from `start`, i.e might not be oop aligned.
2609 // If that's the case the bit range corresponding to the last stack slot should not have bits set
2610 // anyways and we assert that before returning.
2611 address effective_end = UseCompressedOops ? end : align_down(end, wordSize);
2612 log_develop_trace(continuations)("clearing bitmap for " INTPTR_FORMAT " - " INTPTR_FORMAT, p2i(start), p2i(effective_end));
2613 stackChunkOop chunk = _cont.tail();
2614 chunk->bitmap().clear_range(chunk->bit_index_for(start), chunk->bit_index_for(effective_end));
2615 assert(effective_end == end || !chunk->bitmap().at(chunk->bit_index_for(effective_end)), "bit should not be set");
2616 }
2617
2618 intptr_t* ThawBase::handle_preempted_continuation(intptr_t* sp, Continuation::preempt_kind preempt_kind, bool fast_case) {
2619 frame top(sp);
2620 assert(top.pc() == *(address*)(sp - frame::sender_sp_ret_address_offset()), "");
2621 DEBUG_ONLY(verify_frame_kind(top, preempt_kind);)
2622 NOT_PRODUCT(int64_t tid = _thread->monitor_owner_id();)
2623
2624 #if INCLUDE_JVMTI
2625 // Finish the VTMS transition.
2626 assert(_thread->is_in_VTMS_transition(), "must be");
2627 bool is_vthread = Continuation::continuation_scope(_cont.continuation()) == java_lang_VirtualThread::vthread_scope();
2628 if (is_vthread) {
2629 if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
2630 jvmti_mount_end(_thread, _cont, top, preempt_kind);
2631 } else {
2632 _thread->set_is_in_VTMS_transition(false);
2633 java_lang_Thread::set_is_in_VTMS_transition(_thread->vthread(), false);
2634 }
2635 }
2636 #endif
2637
2638 if (fast_case) {
2639 // If we thawed in the slow path the runtime stub/native wrapper frame already
2640 // has the correct fp (see ThawBase::new_stack_frame). On the fast path though,
2641 // we copied the fp patched during freeze, which will now have to be fixed.
2642 assert(top.is_runtime_frame() || top.is_native_frame(), "");
2643 int fsize = top.cb()->frame_size();
2644 patch_pd(top, sp + fsize);
2645 }
2646
2647 if (preempt_kind == Continuation::object_wait) {
2648 // Check now if we need to throw IE exception.
2649 bool throw_ie = _thread->pending_interrupted_exception();
2650 if (throw_ie) {
2651 throw_interrupted_exception(_thread, top);
2652 _thread->set_pending_interrupted_exception(false);
2653 }
2654 log_develop_trace(continuations, preempt)("Resuming " INT64_FORMAT" after preemption on Object.wait%s", tid, throw_ie ? "(throwing IE)" : "");
2655 } else if (preempt_kind == Continuation::monitorenter) {
2656 if (top.is_runtime_frame()) {
2657 // The continuation might now run on a different platform thread than the previous time so
2658 // we need to adjust the current thread saved in the stub frame before restoring registers.
2659 JavaThread** thread_addr = frame::saved_thread_address(top);
2660 if (thread_addr != nullptr) *thread_addr = _thread;
2661 }
2662 log_develop_trace(continuations, preempt)("Resuming " INT64_FORMAT " after preemption on monitorenter", tid);
2663 } else {
2664 // We need to redo the original call into the VM. First though, we need
2665 // to exit the monitor we just acquired (except on preemption cancelled
2666 // case where it was already released).
2667 assert(preempt_kind == Continuation::object_locker, "");
2668 if (_monitor != nullptr) _monitor->exit(_thread);
2669 sp = redo_vmcall(_thread, top);
2670 }
2671 return sp;
2672 }
2673
2674 intptr_t* ThawBase::redo_vmcall(JavaThread* current, frame& top) {
2675 assert(!current->preempting(), "");
2676 NOT_PRODUCT(int64_t tid = current->monitor_owner_id();)
2677 intptr_t* sp = top.sp();
2678
2679 {
2680 HandleMarkCleaner hmc(current); // Cleanup so._conth Handle
2681 ContinuationWrapper::SafepointOp so(current, _cont);
2682 AnchorMark am(current, top); // Set the anchor so that the stack is walkable.
2683
2684 Method* m = top.interpreter_frame_method();
2685 Bytecode current_bytecode = Bytecode(m, top.interpreter_frame_bcp());
2686 Bytecodes::Code code = current_bytecode.code();
2687 log_develop_trace(continuations, preempt)("Redoing InterpreterRuntime::%s for " INT64_FORMAT, code == Bytecodes::Code::_new ? "_new" : "resolve_from_cache", tid);
2688
2689 // These InterpreterRuntime entry points use JRT_ENTRY which uses a HandleMarkCleaner.
2690 // Create a HandeMark to avoid destroying so._conth.
2691 HandleMark hm(current);
2692 DEBUG_ONLY(JavaThread::AtRedoVMCall apvmc(current);)
2693 if (code == Bytecodes::Code::_new) {
2694 InterpreterRuntime::_new(current, m->constants(), current_bytecode.get_index_u2(code));
2695 } else {
2696 InterpreterRuntime::resolve_from_cache(current, code);
2697 }
2698 }
2699
2700 if (current->preempting()) {
2701 // Preempted again so we just arrange to return to preempt stub to unmount.
2702 sp = push_preempt_adapter();
2703 current->set_preempt_alternate_return(nullptr);
2704 bool cancelled = current->preemption_cancelled();
2705 if (cancelled) {
2706 // Since preemption was cancelled, the thread will call thaw again from the preempt
2707 // stub. These retries could happen several times due to contention on the init_lock,
2708 // so just let the vthread umount to give a chance for other vthreads to run.
2709 current->set_preemption_cancelled(false);
2710 oop vthread = current->vthread();
2711 assert(java_lang_VirtualThread::state(vthread) == java_lang_VirtualThread::RUNNING, "wrong state for vthread");
2712 java_lang_VirtualThread::set_state(vthread, java_lang_VirtualThread::YIELDING);
2713 #if INCLUDE_JVMTI
2714 if (current->contended_entered_monitor() != nullptr) {
2715 current->set_contended_entered_monitor(nullptr);
2716 }
2717 #endif
2718 }
2719 log_develop_trace(continuations, preempt)("Preempted " INT64_FORMAT " again%s", tid, cancelled ? "(preemption cancelled, setting state to YIELDING)" : "");
2720 } else {
2721 log_develop_trace(continuations, preempt)("Call succesful, resuming " INT64_FORMAT, tid);
2722 }
2723 return sp;
2724 }
2725
2726 void ThawBase::throw_interrupted_exception(JavaThread* current, frame& top) {
2727 HandleMarkCleaner hm(current); // Cleanup so._conth Handle
2728 ContinuationWrapper::SafepointOp so(current, _cont);
2729 // Since we might safepoint set the anchor so that the stack can be walked.
2730 set_anchor(current, top.sp());
2731 JRT_BLOCK
2732 THROW(vmSymbols::java_lang_InterruptedException());
2733 JRT_BLOCK_END
2734 clear_anchor(current);
2735 }
2736
2737 NOINLINE void ThawBase::recurse_thaw_interpreted_frame(const frame& hf, frame& caller, int num_frames, bool is_top) {
2738 assert(hf.is_interpreted_frame(), "");
2739
2740 if (UNLIKELY(seen_by_gc())) {
2741 if (is_top && _process_args_at_top) {
2742 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_with_args());
2743 } else {
2744 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_no_args());
2745 }
2746 }
2747
2748 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::InterpretedFrame>(caller, num_frames);
2749
2750 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2751
2752 _align_size += frame::align_wiggle; // possible added alignment for internal interpreted frame alignment om AArch64
2753
2754 frame f = new_stack_frame<ContinuationHelper::InterpretedFrame>(hf, caller, is_bottom_frame);
2755
2756 intptr_t* const stack_frame_top = f.sp() + frame::metadata_words_at_top;
2757 intptr_t* const stack_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(f);
2758 intptr_t* const heap_frame_top = hf.unextended_sp() + frame::metadata_words_at_top;
2759 intptr_t* const heap_frame_bottom = ContinuationHelper::InterpretedFrame::frame_bottom(hf);
2760
2761 assert(hf.is_heap_frame(), "should be");
2762 assert(!f.is_heap_frame(), "should not be");
2763
2764 const int fsize = pointer_delta_as_int(heap_frame_bottom, heap_frame_top);
2765 assert((stack_frame_bottom == stack_frame_top + fsize), "");
2770
2771 // Make sure the relativized locals is already set.
2772 assert(f.interpreter_frame_local_at(0) == stack_frame_bottom - 1, "invalid frame bottom");
2773
2774 derelativize_interpreted_frame_metadata(hf, f);
2775 patch(f, caller, is_bottom_frame);
2776
2777 assert(f.is_interpreted_frame_valid(_cont.thread()), "invalid thawed frame");
2778 assert(stack_frame_bottom <= ContinuationHelper::Frame::frame_top(caller), "");
2779
2780 CONT_JFR_ONLY(_jfr_info.record_interpreted_frame();)
2781
2782 maybe_set_fastpath(f.sp());
2783
2784 Method* m = hf.interpreter_frame_method();
2785 assert(!m->is_native() || !is_bottom_frame, "should be top frame of thaw_top case; missing caller frame");
2786 const int locals = m->max_locals();
2787
2788 if (!is_bottom_frame) {
2789 // can only fix caller once this frame is thawed (due to callee saved regs)
2790 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance_no_args());
2791 } else if (_cont.tail()->has_bitmap() && locals > 0) {
2792 assert(hf.is_heap_frame(), "should be");
2793 address start = (address)(heap_frame_bottom - locals);
2794 address end = (address)heap_frame_bottom;
2795 clear_bitmap_bits(start, end);
2796 }
2797
2798 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2799 caller = f;
2800 }
2801
2802 void ThawBase::recurse_thaw_compiled_frame(const frame& hf, frame& caller, int num_frames, bool stub_caller) {
2803 assert(hf.is_compiled_frame(), "");
2804 assert(_preempted_case || !stub_caller, "stub caller not at preemption");
2805
2806 if (!stub_caller && UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2807 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_no_args());
2808 }
2809
2810 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::CompiledFrame>(caller, num_frames);
2811
2812 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2813
2814 assert(caller.sp() == caller.unextended_sp(), "");
2815
2816 if ((!is_bottom_frame && caller.is_interpreted_frame()) || (is_bottom_frame && Interpreter::contains(_cont.tail()->pc()))) {
2817 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_compiled_frame
2818 }
2819
2820 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2821 // yet laid out in the stack, and so the original_pc is not stored in it.
2822 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2823 frame f = new_stack_frame<ContinuationHelper::CompiledFrame>(hf, caller, is_bottom_frame);
2824 intptr_t* const stack_frame_top = f.sp();
2825 intptr_t* const heap_frame_top = hf.unextended_sp();
2826
2827 const int added_argsize = (is_bottom_frame || caller.is_interpreted_frame()) ? hf.compiled_frame_stack_argsize() : 0;
2846 assert(!f.is_deoptimized_frame(), "");
2847 if (hf.is_deoptimized_frame()) {
2848 maybe_set_fastpath(f.sp());
2849 } else if (_thread->is_interp_only_mode()
2850 || (stub_caller && f.cb()->as_nmethod()->is_marked_for_deoptimization())) {
2851 // The caller of the safepoint stub when the continuation is preempted is not at a call instruction, and so
2852 // cannot rely on nmethod patching for deopt.
2853 assert(_thread->is_interp_only_mode() || stub_caller, "expected a stub-caller");
2854
2855 log_develop_trace(continuations)("Deoptimizing thawed frame");
2856 DEBUG_ONLY(ContinuationHelper::Frame::patch_pc(f, nullptr));
2857
2858 f.deoptimize(nullptr); // the null thread simply avoids the assertion in deoptimize which we're not set up for
2859 assert(f.is_deoptimized_frame(), "");
2860 assert(ContinuationHelper::Frame::is_deopt_return(f.raw_pc(), f), "");
2861 maybe_set_fastpath(f.sp());
2862 }
2863
2864 if (!is_bottom_frame) {
2865 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2866 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance_no_args());
2867 } else if (_cont.tail()->has_bitmap() && added_argsize > 0) {
2868 address start = (address)(heap_frame_top + ContinuationHelper::CompiledFrame::size(hf) + frame::metadata_words_at_top);
2869 int stack_args_slots = f.cb()->as_nmethod()->num_stack_arg_slots(false /* rounded */);
2870 int argsize_in_bytes = stack_args_slots * VMRegImpl::stack_slot_size;
2871 clear_bitmap_bits(start, start + argsize_in_bytes);
2872 }
2873
2874 DEBUG_ONLY(after_thaw_java_frame(f, is_bottom_frame);)
2875 caller = f;
2876 }
2877
2878 void ThawBase::recurse_thaw_stub_frame(const frame& hf, frame& caller, int num_frames) {
2879 DEBUG_ONLY(_frames++;)
2880
2881 if (UNLIKELY(seen_by_gc())) {
2882 // Process the stub's caller here since we might need the full map.
2883 RegisterMap map(nullptr,
2884 RegisterMap::UpdateMap::include,
2885 RegisterMap::ProcessFrames::skip,
2886 RegisterMap::WalkContinuation::skip);
2887 map.set_include_argument_oops(false);
2888 _stream.next(&map);
2889 assert(!_stream.is_done(), "");
2890 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, &map);
2891 } else {
2892 _stream.next(SmallRegisterMap::instance_no_args());
2893 assert(!_stream.is_done(), "");
2894 }
2895
2896 recurse_thaw_compiled_frame(_stream.to_frame(), caller, num_frames, true);
2897
2898 assert(caller.is_compiled_frame(), "");
2899 assert(caller.sp() == caller.unextended_sp(), "");
2900
2901 DEBUG_ONLY(before_thaw_java_frame(hf, caller, false /*is_bottom_frame*/, num_frames);)
2902
2903 frame f = new_stack_frame<ContinuationHelper::StubFrame>(hf, caller, false);
2904 intptr_t* stack_frame_top = f.sp();
2905 intptr_t* heap_frame_top = hf.sp();
2906 int fsize = ContinuationHelper::StubFrame::size(hf);
2907
2908 copy_from_chunk(heap_frame_top - frame::metadata_words, stack_frame_top - frame::metadata_words,
2909 fsize + frame::metadata_words);
2910
2911 patch(f, caller, false /*is_bottom_frame*/);
2912
2913 // can only fix caller once this frame is thawed (due to callee saved regs)
2914 RegisterMap map(nullptr,
2915 RegisterMap::UpdateMap::include,
2916 RegisterMap::ProcessFrames::skip,
2917 RegisterMap::WalkContinuation::skip);
2918 map.set_include_argument_oops(false);
2919 f.oop_map()->update_register_map(&f, &map);
2920 ContinuationHelper::update_register_map_with_callee(caller, &map);
2921 _cont.tail()->fix_thawed_frame(caller, &map);
2922
2923 DEBUG_ONLY(after_thaw_java_frame(f, false /*is_bottom_frame*/);)
2924 caller = f;
2925 }
2926
2927 void ThawBase::recurse_thaw_native_frame(const frame& hf, frame& caller, int num_frames) {
2928 assert(hf.is_native_frame(), "");
2929 assert(_preempted_case && hf.cb()->as_nmethod()->method()->is_object_wait0(), "");
2930
2931 if (UNLIKELY(seen_by_gc())) { // recurse_thaw_stub_frame already invoked our barriers with a full regmap
2932 _cont.tail()->do_barriers<stackChunkOopDesc::BarrierType::Store>(_stream, SmallRegisterMap::instance_no_args());
2933 }
2934
2935 const bool is_bottom_frame = recurse_thaw_java_frame<ContinuationHelper::NativeFrame>(caller, num_frames);
2936 assert(!is_bottom_frame, "");
2937
2938 DEBUG_ONLY(before_thaw_java_frame(hf, caller, is_bottom_frame, num_frames);)
2939
2940 assert(caller.sp() == caller.unextended_sp(), "");
2941
2942 if (caller.is_interpreted_frame()) {
2943 _align_size += frame::align_wiggle; // we add one whether or not we've aligned because we add it in recurse_freeze_native_frame
2944 }
2945
2946 // new_stack_frame must construct the resulting frame using hf.pc() rather than hf.raw_pc() because the frame is not
2947 // yet laid out in the stack, and so the original_pc is not stored in it.
2948 // As a result, f.is_deoptimized_frame() is always false and we must test hf to know if the frame is deoptimized.
2949 frame f = new_stack_frame<ContinuationHelper::NativeFrame>(hf, caller, false /* bottom */);
2950 intptr_t* const stack_frame_top = f.sp();
2951 intptr_t* const heap_frame_top = hf.unextended_sp();
2952
2953 int fsize = ContinuationHelper::NativeFrame::size(hf);
2954 assert(fsize <= (int)(caller.unextended_sp() - f.unextended_sp()), "");
2955
2956 intptr_t* from = heap_frame_top - frame::metadata_words_at_bottom;
2957 intptr_t* to = stack_frame_top - frame::metadata_words_at_bottom;
2958 int sz = fsize + frame::metadata_words_at_bottom;
2959
2960 copy_from_chunk(from, to, sz); // copying good oops because we invoked barriers above
2961
2962 patch(f, caller, false /* bottom */);
2963
2964 // f.is_deoptimized_frame() is always false and we must test hf.is_deoptimized_frame() (see comment above)
2965 assert(!f.is_deoptimized_frame(), "");
2966 assert(!hf.is_deoptimized_frame(), "");
2967 assert(!f.cb()->as_nmethod()->is_marked_for_deoptimization(), "");
2968
2969 // can only fix caller once this frame is thawed (due to callee saved regs); this happens on the stack
2970 _cont.tail()->fix_thawed_frame(caller, SmallRegisterMap::instance_no_args());
2971
2972 DEBUG_ONLY(after_thaw_java_frame(f, false /* bottom */);)
2973 caller = f;
2974 }
2975
2976 void ThawBase::finish_thaw(frame& f) {
2977 stackChunkOop chunk = _cont.tail();
2978
2979 if (chunk->is_empty()) {
2980 // Only remove chunk from list if it can't be reused for another freeze
2981 if (seen_by_gc()) {
2982 _cont.set_tail(chunk->parent());
2983 } else {
2984 chunk->set_has_mixed_frames(false);
2985 }
2986 chunk->set_max_thawing_size(0);
2987 } else {
2988 chunk->set_max_thawing_size(chunk->max_thawing_size() - _align_size);
2989 }
2990 assert(chunk->is_empty() == (chunk->max_thawing_size() == 0), "");
2991
2992 if (!is_aligned(f.sp(), frame::frame_alignment)) {
2993 assert(f.is_interpreted_frame(), "");
2994 f.set_sp(align_down(f.sp(), frame::frame_alignment));
2995 }
2996 push_return_frame(f);
2997 // can only fix caller after push_return_frame (due to callee saved regs)
2998 if (_process_args_at_top) {
2999 chunk->fix_thawed_frame(f, SmallRegisterMap::instance_with_args());
3000 } else {
3001 chunk->fix_thawed_frame(f, SmallRegisterMap::instance_no_args());
3002 }
3003
3004 assert(_cont.is_empty() == _cont.last_frame().is_empty(), "");
3005
3006 log_develop_trace(continuations)("thawed %d frames", _frames);
3007
3008 LogTarget(Trace, continuations) lt;
3009 if (lt.develop_is_enabled()) {
3010 LogStream ls(lt);
3011 ls.print_cr("top hframe after (thaw):");
3012 _cont.last_frame().print_value_on(&ls);
3013 }
3014 }
3015
3016 void ThawBase::push_return_frame(frame& f) { // see generate_cont_thaw
3017 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == f.cb()->as_nmethod()->is_deopt_pc(f.raw_pc()), "");
3018 assert(!f.is_compiled_frame() || f.is_deoptimized_frame() == (f.pc() != f.raw_pc()), "");
3019
3020 LogTarget(Trace, continuations) lt;
3021 if (lt.develop_is_enabled()) {
3022 LogStream ls(lt);
3044
3045 ContinuationEntry* entry = thread->last_continuation();
3046 assert(entry != nullptr, "");
3047 oop oopCont = entry->cont_oop(thread);
3048
3049 assert(!jdk_internal_vm_Continuation::done(oopCont), "");
3050 assert(oopCont == get_continuation(thread), "");
3051 verify_continuation(oopCont);
3052
3053 assert(entry->is_virtual_thread() == (entry->scope(thread) == java_lang_VirtualThread::vthread_scope()), "");
3054
3055 ContinuationWrapper cont(thread, oopCont);
3056 log_develop_debug(continuations)("THAW #" INTPTR_FORMAT " " INTPTR_FORMAT, cont.hash(), p2i((oopDesc*)oopCont));
3057
3058 #ifdef ASSERT
3059 set_anchor_to_entry(thread, cont.entry());
3060 log_frames(thread);
3061 clear_anchor(thread);
3062 #endif
3063
3064 Thaw<ConfigT> thw(thread, cont);
3065 intptr_t* const sp = thw.thaw(kind);
3066 assert(is_aligned(sp, frame::frame_alignment), "");
3067 DEBUG_ONLY(log_frames_after_thaw(thread, cont, sp);)
3068
3069 CONT_JFR_ONLY(thw.jfr_info().post_jfr_event(&event, cont.continuation(), thread);)
3070
3071 verify_continuation(cont.continuation());
3072 log_develop_debug(continuations)("=== End of thaw #" INTPTR_FORMAT, cont.hash());
3073
3074 return sp;
3075 }
3076
3077 #ifdef ASSERT
3078 static void do_deopt_after_thaw(JavaThread* thread) {
3079 int i = 0;
3080 StackFrameStream fst(thread, true, false);
3081 fst.register_map()->set_include_argument_oops(false);
3082 ContinuationHelper::update_register_map_with_callee(*fst.current(), fst.register_map());
3083 for (; !fst.is_done(); fst.next()) {
3084 if (fst.current()->cb()->is_nmethod()) {
3085 nmethod* nm = fst.current()->cb()->as_nmethod();
3086 if (!nm->method()->is_continuation_native_intrinsic()) {
3087 nm->make_deoptimized();
3144 if (!fr.is_interpreted_frame()) {
3145 st->print_cr("size: %d argsize: %d",
3146 ContinuationHelper::NonInterpretedUnknownFrame::size(fr),
3147 ContinuationHelper::NonInterpretedUnknownFrame::stack_argsize(fr));
3148 }
3149 VMReg reg = fst.register_map()->find_register_spilled_here(cl.p(), fst.current()->sp());
3150 if (reg != nullptr) {
3151 st->print_cr("Reg %s %d", reg->name(), reg->is_stack() ? (int)reg->reg2stack() : -99);
3152 }
3153 cl.reset();
3154 DEBUG_ONLY(thread->print_frame_layout();)
3155 if (chunk != nullptr) {
3156 chunk->print_on(true, st);
3157 }
3158 return false;
3159 }
3160 }
3161 return true;
3162 }
3163
3164 static void log_frames(JavaThread* thread, bool dolog) {
3165 const static int show_entry_callers = 3;
3166 LogTarget(Trace, continuations) lt;
3167 if (!lt.develop_is_enabled() || !dolog) {
3168 return;
3169 }
3170 LogStream ls(lt);
3171
3172 ls.print_cr("------- frames --------- for thread " INTPTR_FORMAT, p2i(thread));
3173 if (!thread->has_last_Java_frame()) {
3174 ls.print_cr("NO ANCHOR!");
3175 }
3176
3177 RegisterMap map(thread,
3178 RegisterMap::UpdateMap::include,
3179 RegisterMap::ProcessFrames::include,
3180 RegisterMap::WalkContinuation::skip);
3181 map.set_include_argument_oops(false);
3182
3183 if (false) {
3184 for (frame f = thread->last_frame(); !f.is_entry_frame(); f = f.sender(&map)) {
3185 f.print_on(&ls);
3186 }
3187 } else {
3189 ResetNoHandleMark rnhm;
3190 ResourceMark rm;
3191 HandleMark hm(Thread::current());
3192 FrameValues values;
3193
3194 int i = 0;
3195 int post_entry = -1;
3196 for (frame f = thread->last_frame(); !f.is_first_frame(); f = f.sender(&map), i++) {
3197 f.describe(values, i, &map, i == 0);
3198 if (post_entry >= 0 || Continuation::is_continuation_enterSpecial(f))
3199 post_entry++;
3200 if (post_entry >= show_entry_callers)
3201 break;
3202 }
3203 values.print_on(thread, &ls);
3204 }
3205
3206 ls.print_cr("======= end frames =========");
3207 }
3208
3209 static void log_frames_after_thaw(JavaThread* thread, ContinuationWrapper& cont, intptr_t* sp) {
3210 intptr_t* sp0 = sp;
3211 address pc0 = *(address*)(sp - frame::sender_sp_ret_address_offset());
3212
3213 bool preempted = false;
3214 stackChunkOop tail = cont.tail();
3215 if (tail != nullptr && tail->preempted()) {
3216 // Still preempted (monitor not acquired) so no frames were thawed.
3217 set_anchor(thread, cont.entrySP(), cont.entryPC());
3218 preempted = true;
3219 } else {
3220 set_anchor(thread, sp0);
3221 }
3222
3223 log_frames(thread);
3224 if (LoomVerifyAfterThaw) {
3225 assert(do_verify_after_thaw(thread, cont.tail(), tty), "");
3226 }
3227 assert(ContinuationEntry::assert_entry_frame_laid_out(thread, preempted), "");
3228 clear_anchor(thread);
3229
3230 LogTarget(Trace, continuations) lt;
3231 if (lt.develop_is_enabled()) {
3232 LogStream ls(lt);
3233 ls.print_cr("Jumping to frame (thaw):");
3234 frame(sp).print_value_on(&ls);
3235 }
3236 }
3237 #endif // ASSERT
3238
3239 #include CPU_HEADER_INLINE(continuationFreezeThaw)
3240
3241 #ifdef ASSERT
3242 static void print_frame_layout(const frame& f, bool callee_complete, outputStream* st) {
3243 ResourceMark rm;
3244 FrameValues values;
3245 assert(f.get_cb() != nullptr, "");
3246 RegisterMap map(f.is_heap_frame() ?
3247 nullptr :
|