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 int ContinuationEntry::_interpreted_entry_offset = 0;
 45 
 46 void ContinuationEntry::set_enter_code(nmethod* nm, int interpreted_entry_offset) {
 47   assert(_return_pc_offset != 0, "");
 48   _return_pc = nm->code_begin() + _return_pc_offset;
 49   _thaw_call_pc = nm->code_begin() + _thaw_call_pc_offset;
 50   _cleanup_pc = nm->code_begin() + _cleanup_offset;
 51 
 52   _enter_special = nm;
 53   _interpreted_entry_offset = interpreted_entry_offset;
 54 
 55   assert(_enter_special->code_contains(compiled_entry()),    "entry not in enterSpecial");
 56   assert(_enter_special->code_contains(interpreted_entry()), "entry not in enterSpecial");
 57   assert(interpreted_entry() < compiled_entry(), "unexpected code layout");
 58 }
 59 
 60 address ContinuationEntry::compiled_entry() {
 61   return _enter_special->verified_entry_point();
 62 }
 63 
 64 address ContinuationEntry::interpreted_entry() {
 65   return _enter_special->code_begin() + _interpreted_entry_offset;
 66 }
 67 
 68 bool ContinuationEntry::is_interpreted_call(address call_address) {
 69   assert(_enter_special->code_contains(call_address), "call not in enterSpecial");
 70   assert(call_address >= interpreted_entry(), "unexpected location");
 71   return call_address < compiled_entry();
 72 }
 73 
 74 ContinuationEntry* ContinuationEntry::from_frame(const frame& f) {
 75   assert(Continuation::is_continuation_enterSpecial(f), "");
 76   return (ContinuationEntry*)f.unextended_sp();
 77 }
 78 
 79 NOINLINE static void flush_stack_processing(JavaThread* thread, intptr_t* sp) {
 80   log_develop_trace(continuations)("flush_stack_processing");
 81   for (StackFrameStream fst(thread, true, true); fst.current()->sp() <= sp; fst.next()) {
 82     ;
 83   }
 84 }
 85 
 86 inline void maybe_flush_stack_processing(JavaThread* thread, intptr_t* sp) {
 87   StackWatermark* sw;
 88   uintptr_t watermark;
 89   if ((sw = StackWatermarkSet::get(thread, StackWatermarkKind::gc)) != nullptr
 90         && (watermark = sw->watermark()) != 0
 91         && watermark <= (uintptr_t)sp) {
 92     flush_stack_processing(thread, sp);
 93   }
 94 }
 95 
 96 void ContinuationEntry::flush_stack_processing(JavaThread* thread) const {
 97   maybe_flush_stack_processing(thread, (intptr_t*)((uintptr_t)entry_sp() + ContinuationEntry::size()));
 98 }
 99 
100 #ifndef PRODUCT
101 void ContinuationEntry::describe(FrameValues& values, int frame_no) const {
102   address usp = (address)this;
103   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_offset())),    "parent");
104   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::cont_offset())),      "continuation");
105   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::flags_offset())),     "flags");
106   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::chunk_offset())),     "chunk");
107   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::argsize_offset())),   "argsize");
108   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::pin_count_offset())), "pin_count");
109   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_cont_fastpath_offset())),      "parent fastpath");
110   values.describe(frame_no, (intptr_t*)(usp + in_bytes(ContinuationEntry::parent_held_monitor_count_offset())), "parent held monitor count");
111 }
112 #endif
113 
114 #ifdef ASSERT
115 bool ContinuationEntry::assert_entry_frame_laid_out(JavaThread* thread) {
116   assert(thread->has_last_Java_frame(), "Wrong place to use this assertion");
117 
118   ContinuationEntry* entry = thread->last_continuation();
119   assert(entry != nullptr, "");
120 
121   intptr_t* unextended_sp = entry->entry_sp();
122   intptr_t* sp;
123   if (entry->argsize() > 0) {
124     sp = entry->bottom_sender_sp();
125   } else {
126     sp = unextended_sp;
127     bool interpreted_bottom = false;
128     RegisterMap map(thread,
129                     RegisterMap::UpdateMap::skip,
130                     RegisterMap::ProcessFrames::skip,
131                     RegisterMap::WalkContinuation::skip);
132     frame f;
133     for (f = thread->last_frame();
134          !f.is_first_frame() && f.sp() <= unextended_sp && !Continuation::is_continuation_enterSpecial(f);
135          f = f.sender(&map)) {
136       interpreted_bottom = f.is_interpreted_frame();
137     }
138     assert(Continuation::is_continuation_enterSpecial(f), "");
139     sp = interpreted_bottom ? f.sp() : entry->bottom_sender_sp();
140   }
141 
142   assert(sp != nullptr, "");
143   assert(sp <= entry->entry_sp(), "");
144   address pc = ContinuationHelper::return_address_at(
145                  sp - frame::sender_sp_ret_address_offset());
146 
147   if (pc != StubRoutines::cont_returnBarrier()) {
148     CodeBlob* cb = pc != nullptr ? CodeCache::find_blob(pc) : nullptr;
149     assert(cb != nullptr, "sp: " INTPTR_FORMAT " pc: " INTPTR_FORMAT, p2i(sp), p2i(pc));
150     assert(cb->as_nmethod()->method()->is_continuation_enter_intrinsic(), "");
151   }
152 
153   return true;
154 }
155 #endif // ASSERT