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