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