1 /* 2 * Copyright (c) 2019, 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 #ifndef CPU_X86_CONTINUATIONFREEZETHAW_X86_INLINE_HPP 26 #define CPU_X86_CONTINUATIONFREEZETHAW_X86_INLINE_HPP 27 28 #include "code/codeBlob.inline.hpp" 29 #include "oops/stackChunkOop.inline.hpp" 30 #include "runtime/frame.hpp" 31 #include "runtime/frame.inline.hpp" 32 33 inline void patch_callee_link(const frame& f, intptr_t* fp) { 34 *ContinuationHelper::Frame::callee_link_address(f) = fp; 35 } 36 37 inline void patch_callee_link_relative(const frame& f, intptr_t* fp) { 38 intptr_t* la = (intptr_t*)ContinuationHelper::Frame::callee_link_address(f); 39 intptr_t new_value = fp - la; 40 *la = new_value; 41 } 42 43 ////// Freeze 44 45 // Fast path 46 47 inline void FreezeBase::patch_stack_pd(intptr_t* frame_sp, intptr_t* heap_sp) { 48 // copy the spilled rbp from the heap to the stack 49 *(frame_sp - frame::sender_sp_offset) = *(heap_sp - frame::sender_sp_offset); 50 } 51 52 // Slow path 53 54 template<typename FKind> 55 inline frame FreezeBase::sender(const frame& f) { 56 assert(FKind::is_instance(f), ""); 57 if (FKind::interpreted) { 58 return frame(f.sender_sp(), f.interpreter_frame_sender_sp(), f.link(), f.sender_pc()); 59 } 60 intptr_t** link_addr = link_address<FKind>(f); 61 62 intptr_t* sender_sp = (intptr_t*)(link_addr + frame::sender_sp_offset); // f.unextended_sp() + (fsize/wordSize); // 63 address sender_pc = (address) *(sender_sp-1); 64 assert(sender_sp != f.sp(), "must have changed"); 65 66 int slot = 0; 67 CodeBlob* sender_cb = CodeCache::find_blob_and_oopmap(sender_pc, slot); 68 69 // Repair the sender sp if the frame has been extended 70 if (sender_cb->is_nmethod()) { 71 sender_sp = f.repair_sender_sp(sender_sp, link_addr); 72 } 73 74 return sender_cb != nullptr 75 ? frame(sender_sp, sender_sp, *link_addr, sender_pc, sender_cb, 76 slot == -1 ? nullptr : sender_cb->oop_map_for_slot(slot, sender_pc), false) 77 : frame(sender_sp, sender_sp, *link_addr, sender_pc); 78 } 79 80 template<typename FKind> 81 frame FreezeBase::new_heap_frame(frame& f, frame& caller, int size_adjust) { 82 assert(FKind::is_instance(f), ""); 83 assert(!caller.is_interpreted_frame() 84 || caller.unextended_sp() == (intptr_t*)caller.at(frame::interpreter_frame_last_sp_offset), ""); 85 86 intptr_t *sp, *fp; // sp is really our unextended_sp 87 if (FKind::interpreted) { 88 assert((intptr_t*)f.at_relative_or_null(frame::interpreter_frame_last_sp_offset) == nullptr 89 || f.unextended_sp() == (intptr_t*)f.at_relative(frame::interpreter_frame_last_sp_offset), ""); 90 intptr_t locals_offset = *f.addr_at(frame::interpreter_frame_locals_offset); 91 // If the caller.is_empty(), i.e. we're freezing into an empty chunk, then we set 92 // the chunk's argsize in finalize_freeze and make room for it above the unextended_sp 93 bool overlap_caller = caller.is_interpreted_frame() || caller.is_empty(); 94 fp = caller.unextended_sp() - 1 - locals_offset + (overlap_caller ? ContinuationHelper::InterpretedFrame::stack_argsize(f) : 0); 95 sp = fp - (f.fp() - f.unextended_sp()); 96 assert(sp <= fp, ""); 97 assert(fp <= caller.unextended_sp(), ""); 98 caller.set_sp(fp + frame::sender_sp_offset); 99 100 assert(_cont.tail()->is_in_chunk(sp), ""); 101 102 frame hf(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */); 103 // copy relativized locals from the stack frame 104 *hf.addr_at(frame::interpreter_frame_locals_offset) = locals_offset; 105 return hf; 106 } else { 107 // For a compiled frame we need to re-read fp out of the frame because it may be an 108 // oop and we might have had a safepoint in finalize_freeze, after constructing f. 109 // For stub/native frames the value is not used while frozen, and will be constructed again 110 // when thawing the frame (see ThawBase::new_stack_frame). We use a special bad address to 111 // help with debugging, particularly when inspecting frames and identifying invalid accesses. 112 fp = FKind::compiled ? *(intptr_t**)(f.sp() - frame::sender_sp_offset) : (intptr_t*)badAddressVal; 113 114 int fsize = FKind::size(f); 115 sp = caller.unextended_sp() - fsize - size_adjust; 116 if (caller.is_interpreted_frame() && size_adjust == 0) { 117 // If the caller is interpreted, our stackargs are not supposed to overlap with it 118 // so we make more room by moving sp down by argsize 119 int argsize = FKind::stack_argsize(f); 120 sp -= argsize; 121 } 122 caller.set_sp(sp + fsize); 123 124 assert(_cont.tail()->is_in_chunk(sp), ""); 125 126 return frame(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */); 127 } 128 } 129 130 void FreezeBase::adjust_interpreted_frame_unextended_sp(frame& f) { 131 assert((f.at(frame::interpreter_frame_last_sp_offset) != 0) || (f.unextended_sp() == f.sp()), ""); 132 intptr_t* real_unextended_sp = (intptr_t*)f.at_relative_or_null(frame::interpreter_frame_last_sp_offset); 133 if (real_unextended_sp != nullptr) { 134 f.set_unextended_sp(real_unextended_sp); // can be null at a safepoint 135 } 136 } 137 138 inline void FreezeBase::prepare_freeze_interpreted_top_frame(frame& f) { 139 assert(f.interpreter_frame_last_sp() == nullptr, "should be null for top frame"); 140 f.interpreter_frame_set_last_sp(f.unextended_sp()); 141 } 142 143 inline void FreezeBase::relativize_interpreted_frame_metadata(const frame& f, const frame& hf) { 144 assert(hf.fp() == hf.unextended_sp() + (f.fp() - f.unextended_sp()), ""); 145 assert((f.at(frame::interpreter_frame_last_sp_offset) != 0) 146 || (f.unextended_sp() == f.sp()), ""); 147 assert(f.fp() > (intptr_t*)f.at_relative(frame::interpreter_frame_initial_sp_offset), ""); 148 149 // Make sure that last_sp is already relativized. 150 assert((intptr_t*)hf.at_relative(frame::interpreter_frame_last_sp_offset) == hf.unextended_sp(), ""); 151 152 // Make sure that locals is already relativized. 153 DEBUG_ONLY(Method* m = f.interpreter_frame_method();) 154 // Frames for native methods have 2 extra words (temp oop/result handler) before fixed part of frame. 155 DEBUG_ONLY(int max_locals = !m->is_native() ? m->max_locals() : m->size_of_parameters() + 2;) 156 assert((*hf.addr_at(frame::interpreter_frame_locals_offset) == frame::sender_sp_offset + max_locals - 1), ""); 157 158 // Make sure that monitor_block_top is already relativized. 159 assert(hf.at_absolute(frame::interpreter_frame_monitor_block_top_offset) <= frame::interpreter_frame_initial_sp_offset, ""); 160 161 assert((hf.fp() - hf.unextended_sp()) == (f.fp() - f.unextended_sp()), ""); 162 assert(hf.unextended_sp() == (intptr_t*)hf.at(frame::interpreter_frame_last_sp_offset), ""); 163 assert(hf.unextended_sp() <= (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), ""); 164 assert(hf.fp() > (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), ""); 165 assert(hf.fp() <= (intptr_t*)hf.at(frame::interpreter_frame_locals_offset), ""); 166 } 167 168 inline void FreezeBase::set_top_frame_metadata_pd(const frame& hf) { 169 stackChunkOop chunk = _cont.tail(); 170 assert(chunk->is_in_chunk(hf.sp() - 1), ""); 171 assert(chunk->is_in_chunk(hf.sp() - frame::sender_sp_offset), ""); 172 173 address frame_pc = hf.pc(); 174 175 *(hf.sp() - 1) = (intptr_t)hf.pc(); 176 177 intptr_t* fp_addr = hf.sp() - frame::sender_sp_offset; 178 *fp_addr = hf.is_interpreted_frame() ? (intptr_t)(hf.fp() - fp_addr) 179 : (intptr_t)hf.fp(); 180 assert(frame_pc == ContinuationHelper::Frame::real_pc(hf), ""); 181 } 182 183 inline void FreezeBase::patch_pd(frame& hf, const frame& caller, bool is_bottom_frame) { 184 if (caller.is_interpreted_frame()) { 185 assert(!caller.is_empty(), ""); 186 patch_callee_link_relative(caller, caller.fp()); 187 } else if (is_bottom_frame && caller.pc() != nullptr) { 188 assert(caller.is_compiled_frame(), ""); 189 // If we're the bottom-most frame frozen in this freeze, the caller might have stayed frozen in the chunk, 190 // and its oop-containing fp fixed. We've now just overwritten it, so we must patch it back to its value 191 // as read from the chunk. 192 patch_callee_link(caller, caller.fp()); 193 } 194 } 195 196 inline void FreezeBase::patch_pd_unused(intptr_t* sp) { 197 intptr_t* fp_addr = sp - frame::sender_sp_offset; 198 *fp_addr = badAddressVal; 199 } 200 201 //////// Thaw 202 203 // Fast path 204 205 inline void ThawBase::prefetch_chunk_pd(void* start, int size) { 206 size <<= LogBytesPerWord; 207 Prefetch::read(start, size); 208 Prefetch::read(start, size - 64); 209 } 210 211 template <typename ConfigT> 212 inline void Thaw<ConfigT>::patch_caller_links(intptr_t* sp, intptr_t* bottom) { 213 // Fast path depends on !PreserveFramePointer. See can_thaw_fast(). 214 assert(!PreserveFramePointer, "Frame pointers need to be fixed"); 215 } 216 217 // Slow path 218 219 inline frame ThawBase::new_entry_frame() { 220 intptr_t* sp = _cont.entrySP(); 221 return frame(sp, sp, _cont.entryFP(), _cont.entryPC()); // TODO PERF: This finds code blob and computes deopt state 222 } 223 224 template<typename FKind> frame ThawBase::new_stack_frame(const frame& hf, frame& caller, bool bottom, int size_adjust) { 225 assert(FKind::is_instance(hf), ""); 226 // The values in the returned frame object will be written into the callee's stack in patch. 227 228 if (FKind::interpreted) { 229 intptr_t* heap_sp = hf.unextended_sp(); 230 // If caller is interpreted it already made room for the callee arguments 231 int overlap = caller.is_interpreted_frame() ? ContinuationHelper::InterpretedFrame::stack_argsize(hf) : 0; 232 const int fsize = (int)(ContinuationHelper::InterpretedFrame::frame_bottom(hf) - hf.unextended_sp() - overlap); 233 intptr_t* frame_sp = caller.unextended_sp() - fsize; 234 intptr_t* fp = frame_sp + (hf.fp() - heap_sp); 235 DEBUG_ONLY(intptr_t* unextended_sp = fp + *hf.addr_at(frame::interpreter_frame_last_sp_offset);) 236 assert(frame_sp == unextended_sp, ""); 237 caller.set_sp(fp + frame::sender_sp_offset); 238 frame f(frame_sp, frame_sp, fp, hf.pc()); 239 // we need to set the locals so that the caller of new_stack_frame() can call 240 // ContinuationHelper::InterpretedFrame::frame_bottom 241 intptr_t locals_offset = *hf.addr_at(frame::interpreter_frame_locals_offset); 242 DEBUG_ONLY(Method* m = hf.interpreter_frame_method();) 243 // Frames for native methods have 2 extra words (temp oop/result handler) before fixed part of frame. 244 DEBUG_ONLY(const int max_locals = !m->is_native() ? m->max_locals() : m->size_of_parameters() + 2;) 245 assert((int)locals_offset == frame::sender_sp_offset + max_locals - 1, ""); 246 // copy relativized locals from the heap frame 247 *f.addr_at(frame::interpreter_frame_locals_offset) = locals_offset; 248 return f; 249 } else { 250 int fsize = FKind::size(hf); 251 intptr_t* frame_sp = caller.unextended_sp() - fsize - size_adjust; 252 if (bottom || caller.is_interpreted_frame()) { 253 if (size_adjust == 0) { 254 int argsize = FKind::stack_argsize(hf); 255 frame_sp -= argsize; 256 } 257 frame_sp = align(hf, frame_sp, caller, bottom); 258 } 259 caller.set_sp(frame_sp + fsize); 260 assert(is_aligned(frame_sp, frame::frame_alignment), ""); 261 262 assert(hf.cb() != nullptr, ""); 263 assert(hf.oop_map() != nullptr, ""); 264 intptr_t* fp; 265 if (PreserveFramePointer) { 266 // we need to recreate a "real" frame pointer, pointing into the stack 267 fp = frame_sp + fsize - frame::sender_sp_offset; 268 } else { 269 fp = FKind::stub || FKind::native 270 ? frame_sp + fsize - frame::sender_sp_offset // fp always points to the address below the pushed return pc. We need correct address. 271 : *(intptr_t**)(hf.sp() - frame::sender_sp_offset); // we need to re-read fp because it may be an oop and we might have fixed the frame. 272 } 273 return frame(frame_sp, frame_sp, fp, hf.pc(), hf.cb(), hf.oop_map(), false); // TODO PERF : this computes deopt state; is it necessary? 274 } 275 } 276 277 inline intptr_t* ThawBase::align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom) { 278 if (((intptr_t)frame_sp & 0xf) != 0) { 279 assert(caller.is_interpreted_frame() || (bottom && hf.compiled_frame_stack_argsize() % 2 != 0), ""); 280 frame_sp--; 281 } 282 assert(is_aligned(frame_sp, frame::frame_alignment), ""); 283 return frame_sp; 284 } 285 286 inline void ThawBase::patch_pd(frame& f, const frame& caller) { 287 if (caller.is_interpreted_frame() || PreserveFramePointer) { 288 patch_callee_link(caller, caller.fp()); 289 } 290 } 291 292 inline void ThawBase::patch_pd(frame& f, intptr_t* caller_sp) { 293 intptr_t* fp = caller_sp - frame::sender_sp_offset; 294 patch_callee_link(f, fp); 295 } 296 297 inline intptr_t* ThawBase::push_cleanup_continuation() { 298 frame enterSpecial = new_entry_frame(); 299 intptr_t* sp = enterSpecial.sp(); 300 301 sp[-1] = (intptr_t)ContinuationEntry::cleanup_pc(); 302 sp[-2] = (intptr_t)enterSpecial.fp(); 303 304 log_develop_trace(continuations, preempt)("push_cleanup_continuation initial sp: " INTPTR_FORMAT " final sp: " INTPTR_FORMAT, p2i(sp + 2 * frame::metadata_words), p2i(sp)); 305 return sp; 306 } 307 308 inline void ThawBase::derelativize_interpreted_frame_metadata(const frame& hf, const frame& f) { 309 // Make sure that last_sp is kept relativized. 310 assert((intptr_t*)f.at_relative(frame::interpreter_frame_last_sp_offset) == f.unextended_sp(), ""); 311 312 // Make sure that monitor_block_top is still relativized. 313 assert(f.at_absolute(frame::interpreter_frame_monitor_block_top_offset) <= frame::interpreter_frame_initial_sp_offset, ""); 314 } 315 316 #endif // CPU_X86_CONTINUATIONFREEZE_THAW_X86_INLINE_HPP