1 /*
  2  * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "code/compiledIC.hpp"
 26 #include "code/nmethod.hpp"
 27 #include "oops/method.inline.hpp"
 28 #include "runtime/continuation.hpp"
 29 #include "runtime/continuationEntry.inline.hpp"
 30 #include "runtime/continuationHelper.inline.hpp"
 31 #include "runtime/frame.inline.hpp"
 32 #include "runtime/javaThread.hpp"
 33 #include "runtime/stackFrameStream.inline.hpp"
 34 #include "runtime/stackWatermarkSet.inline.hpp"
 35 #include "runtime/stubRoutines.hpp"
 36 
 37 int ContinuationEntry::_return_pc_offset = 0;
 38 int ContinuationEntry::_thaw_call_pc_offset = 0;
 39 int ContinuationEntry::_cleanup_offset = 0;
 40 address ContinuationEntry::_return_pc = nullptr;
 41 address ContinuationEntry::_thaw_call_pc = nullptr;
 42 address ContinuationEntry::_cleanup_pc = nullptr;
 43 nmethod* ContinuationEntry::_enter_special = nullptr;
 44 nmethod* ContinuationEntry::_do_yield = nullptr;
 45 int ContinuationEntry::_interpreted_entry_offset = 0;
 46 
 47 void ContinuationEntry::set_enter_code(nmethod* nm, int interpreted_entry_offset) {
 48   assert(_return_pc_offset != 0, "");
 49   assert(_thaw_call_pc_offset != 0, "");
 50   _return_pc = nm->code_begin() + _return_pc_offset;
 51   _thaw_call_pc = nm->code_begin() + _thaw_call_pc_offset;
 52   _cleanup_pc = nm->code_begin() + _cleanup_offset;
 53 
 54   _enter_special = nm;
 55   _interpreted_entry_offset = interpreted_entry_offset;
 56 
 57   assert(_enter_special->code_contains(compiled_entry()),    "entry not in enterSpecial");
 58   assert(_enter_special->code_contains(interpreted_entry()), "entry not in enterSpecial");
 59   assert(interpreted_entry() < compiled_entry(), "unexpected code layout");
 60 }
 61 
 62 void ContinuationEntry::set_yield_code(nmethod* nm) {
 63   _do_yield = nm;
 64 }
 65 
 66 address ContinuationEntry::compiled_entry() {
 67   return _enter_special->verified_entry_point();
 68 }
 69 
 70 address ContinuationEntry::interpreted_entry() {
 71   return _enter_special->code_begin() + _interpreted_entry_offset;
 72 }
 73 
 74 bool ContinuationEntry::is_interpreted_call(address call_address) {
 75   assert(_enter_special->code_contains(call_address), "call not in enterSpecial");
 76   assert(call_address >= interpreted_entry(), "unexpected location");
 77   return call_address < compiled_entry();
 78 }
 79 
 80 ContinuationEntry* ContinuationEntry::from_frame(const frame& f) {
 81   assert(Continuation::is_continuation_enterSpecial(f), "");
 82   return (ContinuationEntry*)f.unextended_sp();
 83 }
 84 
 85 NOINLINE static void flush_stack_processing(JavaThread* thread, intptr_t* sp) {
 86   log_develop_trace(continuations)("flush_stack_processing");
 87   for (StackFrameStream fst(thread, true, true); fst.current()->sp() <= sp; fst.next()) {
 88     ;
 89   }
 90 }
 91 
 92 inline void maybe_flush_stack_processing(JavaThread* thread, intptr_t* sp) {
 93   StackWatermark* sw;
 94   uintptr_t watermark;
 95   if ((sw = StackWatermarkSet::get(thread, StackWatermarkKind::gc)) != nullptr
 96         && (watermark = sw->watermark()) != 0
 97         && watermark <= (uintptr_t)sp) {
 98     flush_stack_processing(thread, sp);
 99   }
100 }
101 
102 void ContinuationEntry::flush_stack_processing(JavaThread* thread) const {
103   maybe_flush_stack_processing(thread, (intptr_t*)((uintptr_t)entry_sp() + ContinuationEntry::size()));
104 }
105 
106 #ifndef PRODUCT
107 void ContinuationEntry::describe(FrameValues& values, int frame_no) const {
108   address usp = (address)this;
109   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_offset())),    "parent");
110   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::cont_offset())),      "continuation");
111   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::flags_offset())),     "flags");
112   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::chunk_offset())),     "chunk");
113   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::argsize_offset())),   "argsize");
114   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::pin_count_offset())), "pin_count");
115   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_cont_fastpath_offset())),      "parent fastpath");
116   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_held_monitor_count_offset())), "parent held monitor count");
117 }
118 #endif
119 
120 #ifdef ASSERT
121 bool ContinuationEntry::assert_entry_frame_laid_out(JavaThread* thread, bool preempted) {
122   assert(thread->has_last_Java_frame(), "Wrong place to use this assertion");
123 
124   if (preempted) return true;
125 
126   ContinuationEntry* entry = thread->last_continuation();
127   assert(entry != nullptr, "");
128 
129   intptr_t* unextended_sp = entry->entry_sp();
130   intptr_t* sp;
131   if (entry->argsize() > 0) {
132     sp = entry->bottom_sender_sp();
133   } else {
134     sp = unextended_sp;
135     bool interpreted_bottom = false;
136     RegisterMap map(thread,
137                     RegisterMap::UpdateMap::skip,
138                     RegisterMap::ProcessFrames::skip,
139                     RegisterMap::WalkContinuation::skip);
140     frame f;
141     for (f = thread->last_frame();
142          !f.is_first_frame() && f.sp() <= unextended_sp && !Continuation::is_continuation_enterSpecial(f);
143          f = f.sender(&map)) {
144       interpreted_bottom = f.is_interpreted_frame();
145     }
146     assert(Continuation::is_continuation_enterSpecial(f), "");
147     sp = interpreted_bottom ? f.sp() : entry->bottom_sender_sp();
148   }
149 
150   assert(sp != nullptr, "");
151   assert(sp <= entry->entry_sp(), "");
152   address pc = ContinuationHelper::return_address_at(
153                  sp - frame::sender_sp_ret_address_offset());
154 
155   if (pc != StubRoutines::cont_returnBarrier()) {
156     CodeBlob* cb = pc != nullptr ? CodeCache::find_blob(pc) : nullptr;
157     assert(cb != nullptr, "sp: " INTPTR_FORMAT " pc: " INTPTR_FORMAT, p2i(sp), p2i(pc));
158     assert(cb->as_nmethod()->method()->is_continuation_enter_intrinsic(), "");
159   }
160 
161   return true;
162 }
163 #endif // ASSERT