1 /*
  2  * Copyright (c) 2019, 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 #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     // We need to re-read fp out of the frame because it may be an oop and we might have
102     // had a safepoint in finalize_freeze, after constructing f.
103     fp = *(intptr_t**)(f.sp() - frame::sender_sp_offset);
104 
105     int fsize = FKind::size(f);
106     sp = caller.unextended_sp() - fsize;
107     if (caller.is_interpreted_frame()) {
108       // If the caller is interpreted, our stackargs are not supposed to overlap with it
109       // so we make more room by moving sp down by argsize
110       int argsize = FKind::stack_argsize(f);
111       sp -= argsize;
112     }
113     caller.set_sp(sp + fsize);
114 
115     assert(_cont.tail()->is_in_chunk(sp), "");
116 
117     return frame(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
118   }
119 }
120 
121 void FreezeBase::adjust_interpreted_frame_unextended_sp(frame& f) {
122   assert((f.at(frame::interpreter_frame_last_sp_offset) != 0) || (f.unextended_sp() == f.sp()), "");
123   intptr_t* real_unextended_sp = (intptr_t*)f.at_relative_or_null(frame::interpreter_frame_last_sp_offset);
124   if (real_unextended_sp != nullptr) {
125     f.set_unextended_sp(real_unextended_sp); // can be null at a safepoint
126   }
127 }
128 
129 inline void FreezeBase::relativize_interpreted_frame_metadata(const frame& f, const frame& hf) {
130   assert(hf.fp() == hf.unextended_sp() + (f.fp() - f.unextended_sp()), "");
131   assert((f.at(frame::interpreter_frame_last_sp_offset) != 0)
132     || (f.unextended_sp() == f.sp()), "");
133   assert(f.fp() > (intptr_t*)f.at_relative(frame::interpreter_frame_initial_sp_offset), "");
134 
135   // Make sure that last_sp is already relativized.
136   assert((intptr_t*)hf.at_relative(frame::interpreter_frame_last_sp_offset) == hf.unextended_sp(), "");
137 
138   // Make sure that locals is already relativized.
139   assert((*hf.addr_at(frame::interpreter_frame_locals_offset) == frame::sender_sp_offset + f.interpreter_frame_method()->max_locals() - 1), "");
140 
141   // Make sure that monitor_block_top is already relativized.
142   assert(hf.at_absolute(frame::interpreter_frame_monitor_block_top_offset) <= frame::interpreter_frame_initial_sp_offset, "");
143 
144   assert((hf.fp() - hf.unextended_sp()) == (f.fp() - f.unextended_sp()), "");
145   assert(hf.unextended_sp() == (intptr_t*)hf.at(frame::interpreter_frame_last_sp_offset), "");
146   assert(hf.unextended_sp() <= (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
147   assert(hf.fp()            >  (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
148   assert(hf.fp()            <= (intptr_t*)hf.at(frame::interpreter_frame_locals_offset), "");
149 }
150 
151 inline void FreezeBase::set_top_frame_metadata_pd(const frame& hf) {
152   stackChunkOop chunk = _cont.tail();
153   assert(chunk->is_in_chunk(hf.sp() - 1), "");
154   assert(chunk->is_in_chunk(hf.sp() - frame::sender_sp_offset), "");
155 
156   address frame_pc = hf.pc();
157 
158   *(hf.sp() - 1) = (intptr_t)hf.pc();
159 
160   intptr_t* fp_addr = hf.sp() - frame::sender_sp_offset;
161   *fp_addr = hf.is_interpreted_frame() ? (intptr_t)(hf.fp() - fp_addr)
162                                        : (intptr_t)hf.fp();
163   assert(frame_pc == ContinuationHelper::Frame::real_pc(hf), "");
164 }
165 
166 inline void FreezeBase::patch_pd(frame& hf, const frame& caller) {
167   if (caller.is_interpreted_frame()) {
168     assert(!caller.is_empty(), "");
169     patch_callee_link_relative(caller, caller.fp());
170   } else {
171     // If we're the bottom-most frame frozen in this freeze, the caller might have stayed frozen in the chunk,
172     // and its oop-containing fp fixed. We've now just overwritten it, so we must patch it back to its value
173     // as read from the chunk.
174     patch_callee_link(caller, caller.fp());
175   }
176 }
177 
178 //////// Thaw
179 
180 // Fast path
181 
182 inline void ThawBase::prefetch_chunk_pd(void* start, int size) {
183   size <<= LogBytesPerWord;
184   Prefetch::read(start, size);
185   Prefetch::read(start, size - 64);
186 }
187 
188 template <typename ConfigT>
189 inline void Thaw<ConfigT>::patch_caller_links(intptr_t* sp, intptr_t* bottom) {
190   // Fast path depends on !PreserveFramePointer. See can_thaw_fast().
191   assert(!PreserveFramePointer, "Frame pointers need to be fixed");
192 }
193 
194 // Slow path
195 
196 inline frame ThawBase::new_entry_frame() {
197   intptr_t* sp = _cont.entrySP();
198   return frame(sp, sp, _cont.entryFP(), _cont.entryPC()); // TODO PERF: This finds code blob and computes deopt state
199 }
200 
201 template<typename FKind> frame ThawBase::new_stack_frame(const frame& hf, frame& caller, bool bottom) {
202   assert(FKind::is_instance(hf), "");
203   // The values in the returned frame object will be written into the callee's stack in patch.
204 
205   if (FKind::interpreted) {
206     intptr_t* heap_sp = hf.unextended_sp();
207     // If caller is interpreted it already made room for the callee arguments
208     int overlap = caller.is_interpreted_frame() ? ContinuationHelper::InterpretedFrame::stack_argsize(hf) : 0;
209     const int fsize = (int)(ContinuationHelper::InterpretedFrame::frame_bottom(hf) - hf.unextended_sp() - overlap);
210     const int locals = hf.interpreter_frame_method()->max_locals();
211     intptr_t* frame_sp = caller.unextended_sp() - fsize;
212     intptr_t* fp = frame_sp + (hf.fp() - heap_sp);
213     DEBUG_ONLY(intptr_t* unextended_sp = fp + *hf.addr_at(frame::interpreter_frame_last_sp_offset);)
214     assert(frame_sp == unextended_sp, "");
215     caller.set_sp(fp + frame::sender_sp_offset);
216     frame f(frame_sp, frame_sp, fp, hf.pc());
217     // we need to set the locals so that the caller of new_stack_frame() can call
218     // ContinuationHelper::InterpretedFrame::frame_bottom
219     intptr_t locals_offset = *hf.addr_at(frame::interpreter_frame_locals_offset);
220     assert((int)locals_offset == frame::sender_sp_offset + locals - 1, "");
221     // copy relativized locals from the heap frame
222     *f.addr_at(frame::interpreter_frame_locals_offset) = locals_offset;
223     return f;
224   } else {
225     int fsize = FKind::size(hf);
226     intptr_t* frame_sp = caller.unextended_sp() - fsize;
227     if (bottom || caller.is_interpreted_frame()) {
228       int argsize = hf.compiled_frame_stack_argsize();
229 
230       fsize += argsize;
231       frame_sp   -= argsize;
232       caller.set_sp(caller.sp() - argsize);
233       assert(caller.sp() == frame_sp + (fsize-argsize), "");
234 
235       frame_sp = align(hf, frame_sp, caller, bottom);
236     }
237 
238     assert(hf.cb() != nullptr, "");
239     assert(hf.oop_map() != nullptr, "");
240     intptr_t* fp;
241     if (PreserveFramePointer) {
242       // we need to recreate a "real" frame pointer, pointing into the stack
243       fp = frame_sp + FKind::size(hf) - frame::sender_sp_offset;
244     } else {
245        // we need to re-read fp because it may be an oop and we might have fixed the frame.
246       fp = *(intptr_t**)(hf.sp() - frame::sender_sp_offset);
247     }
248     return frame(frame_sp, frame_sp, fp, hf.pc(), hf.cb(), hf.oop_map(), false); // TODO PERF : this computes deopt state; is it necessary?
249   }
250 }
251 
252 inline intptr_t* ThawBase::align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom) {
253 #ifdef _LP64
254   if (((intptr_t)frame_sp & 0xf) != 0) {
255     assert(caller.is_interpreted_frame() || (bottom && hf.compiled_frame_stack_argsize() % 2 != 0), "");
256     frame_sp--;
257     caller.set_sp(caller.sp() - 1);
258   }
259   assert(is_aligned(frame_sp, frame::frame_alignment), "");
260 #endif
261 
262   return frame_sp;
263 }
264 
265 inline void ThawBase::patch_pd(frame& f, const frame& caller) {
266   patch_callee_link(caller, caller.fp());
267 }
268 
269 inline void ThawBase::derelativize_interpreted_frame_metadata(const frame& hf, const frame& f) {
270   // Make sure that last_sp is kept relativized.
271   assert((intptr_t*)f.at_relative(frame::interpreter_frame_last_sp_offset) == f.unextended_sp(), "");
272 
273   // Make sure that monitor_block_top is still relativized.
274   assert(f.at_absolute(frame::interpreter_frame_monitor_block_top_offset) <= frame::interpreter_frame_initial_sp_offset, "");
275 }
276 
277 #endif // CPU_X86_CONTINUATIONFREEZE_THAW_X86_INLINE_HPP