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