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