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