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_RISCV_CONTINUATIONFREEZETHAW_RISCV_INLINE_HPP
 26 #define CPU_RISCV_CONTINUATIONFREEZETHAW_RISCV_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 
 34 inline void patch_callee_link(const frame& f, intptr_t* fp) {
 35   DEBUG_ONLY(intptr_t* orig = *ContinuationHelper::Frame::callee_link_address(f));
 36   *ContinuationHelper::Frame::callee_link_address(f) = fp;
 37 }
 38 
 39 inline void patch_callee_link_relative(const frame& f, intptr_t* fp) {
 40   intptr_t* la = (intptr_t*)ContinuationHelper::Frame::callee_link_address(f);
 41   intptr_t new_value = fp - la;
 42   *la = new_value;
 43 }
 44 
 45 ////// Freeze
 46 
 47 // Fast path
 48 
 49 inline void FreezeBase::patch_stack_pd(intptr_t* frame_sp, intptr_t* heap_sp) {
 50   // copy the spilled fp from the heap to the stack
 51   *(frame_sp - 2) = *(heap_sp - 2);
 52 }
 53 
 54 // Slow path
 55 
 56 template<typename FKind>
 57 inline frame FreezeBase::sender(const frame& f) {
 58   assert(FKind::is_instance(f), "");
 59   if (FKind::interpreted) {
 60     return frame(f.sender_sp(), f.interpreter_frame_sender_sp(), f.link(), f.sender_pc());
 61   }
 62 
 63   intptr_t** link_addr = link_address<FKind>(f);
 64   intptr_t* sender_sp = (intptr_t*)(link_addr + 2); //  f.unextended_sp() + (fsize/wordSize); //
 65   address sender_pc = (address) *(sender_sp - 1);
 66   assert(sender_sp != f.sp(), "must have changed");
 67 
 68   int slot = 0;
 69   CodeBlob* sender_cb = CodeCache::find_blob_and_oopmap(sender_pc, slot);
 70   return sender_cb != nullptr
 71     ? frame(sender_sp, sender_sp, *link_addr, sender_pc, sender_cb,
 72             slot == -1 ? nullptr : sender_cb->oop_map_for_slot(slot, sender_pc),
 73             false /* on_heap ? */)
 74     : frame(sender_sp, sender_sp, *link_addr, sender_pc);
 75 }
 76 
 77 template<typename FKind> frame FreezeBase::new_heap_frame(frame& f, frame& caller) {
 78   assert(FKind::is_instance(f), "");
 79   assert(!caller.is_interpreted_frame()
 80     || caller.unextended_sp() == (intptr_t*)caller.at(frame::interpreter_frame_last_sp_offset), "");
 81 
 82   intptr_t *sp, *fp; // sp is really our unextended_sp
 83   if (FKind::interpreted) {
 84     assert((intptr_t*)f.at(frame::interpreter_frame_last_sp_offset) == nullptr
 85       || f.unextended_sp() == (intptr_t*)f.at_relative(frame::interpreter_frame_last_sp_offset), "");
 86     intptr_t locals_offset = *f.addr_at(frame::interpreter_frame_locals_offset);
 87     // If the caller.is_empty(), i.e. we're freezing into an empty chunk, then we set
 88     // the chunk's argsize in finalize_freeze and make room for it above the unextended_sp
 89     bool overlap_caller = caller.is_interpreted_frame() || caller.is_empty();
 90     fp = caller.unextended_sp() - 1 - locals_offset + (overlap_caller ? ContinuationHelper::InterpretedFrame::stack_argsize(f) : 0);
 91     sp = fp - (f.fp() - f.unextended_sp());
 92     assert(sp <= fp, "");
 93     assert(fp <= caller.unextended_sp(), "");
 94     caller.set_sp(fp + frame::sender_sp_offset);
 95 
 96     assert(_cont.tail()->is_in_chunk(sp), "");
 97 
 98     frame hf(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
 99     *hf.addr_at(frame::interpreter_frame_locals_offset) = locals_offset;
100     return hf;
101   } else {
102     // We need to re-read fp out of the frame because it may be an oop and we might have
103     // had a safepoint in finalize_freeze, after constructing f.
104     fp = *(intptr_t**)(f.sp() - 2);
105 
106     int fsize = FKind::size(f);
107     sp = caller.unextended_sp() - fsize;
108     if (caller.is_interpreted_frame()) {
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     }
114     caller.set_sp(sp + fsize);
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::relativize_interpreted_frame_metadata(const frame& f, const frame& hf) {
131   assert(hf.fp() == hf.unextended_sp() + (f.fp() - f.unextended_sp()), "");
132   assert((f.at(frame::interpreter_frame_last_sp_offset) != 0)
133     || (f.unextended_sp() == f.sp()), "");
134   assert(f.fp() > (intptr_t*)f.at_relative(frame::interpreter_frame_initial_sp_offset), "");
135 
136   // On RISCV, we may insert padding between the locals and the rest of the frame
137   // (see TemplateInterpreterGenerator::generate_normal_entry, and AbstractInterpreter::layout_activation)
138   // because we freeze the padding word (see recurse_freeze_interpreted_frame) in order to keep the same relativized
139   // locals value, we don't need to change the locals value here.
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 monitor_block_top is already relativized.
145   assert(hf.at_absolute(frame::interpreter_frame_monitor_block_top_offset) <= frame::interpreter_frame_initial_sp_offset, "");
146 
147   // extended_sp is already relativized by TemplateInterpreterGenerator::generate_normal_entry or
148   // AbstractInterpreter::layout_activation
149 
150   assert((hf.fp() - hf.unextended_sp()) == (f.fp() - f.unextended_sp()), "");
151   assert(hf.unextended_sp() == (intptr_t*)hf.at(frame::interpreter_frame_last_sp_offset), "");
152   assert(hf.unextended_sp() <= (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
153   assert(hf.unextended_sp() >  (intptr_t*)hf.at(frame::interpreter_frame_extended_sp_offset), "");
154   assert(hf.fp()            >  (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
155 #ifdef ASSERT
156   if (f.interpreter_frame_method()->max_locals() > 0) {
157     assert(hf.fp()          <= (intptr_t*)hf.at(frame::interpreter_frame_locals_offset), "");
158   }
159 #endif
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() - 2), "");
166 
167   *(hf.sp() - 1) = (intptr_t)hf.pc();
168 
169   intptr_t* fp_addr = hf.sp() - 2;
170   *fp_addr = hf.is_interpreted_frame() ? (intptr_t)(hf.fp() - fp_addr)
171                                        : (intptr_t)hf.fp();
172 }
173 
174 inline void FreezeBase::patch_pd(frame& hf, const frame& caller) {
175   if (caller.is_interpreted_frame()) {
176     assert(!caller.is_empty(), "");
177     patch_callee_link_relative(caller, caller.fp());
178   } else {
179     // If we're the bottom-most frame frozen in this freeze, the caller might have stayed frozen in the chunk,
180     // and its oop-containing fp fixed. We've now just overwritten it, so we must patch it back to its value
181     // as read from the chunk.
182     patch_callee_link(caller, caller.fp());
183   }
184 }
185 
186 //////// Thaw
187 
188 // Fast path
189 
190 inline void ThawBase::prefetch_chunk_pd(void* start, int size) {
191   size <<= LogBytesPerWord;
192   Prefetch::read(start, size);
193   Prefetch::read(start, size - 64);
194 }
195 
196 template <typename ConfigT>
197 inline void Thaw<ConfigT>::patch_caller_links(intptr_t* sp, intptr_t* bottom) {
198   // Fast path depends on !PreserveFramePointer. See can_thaw_fast().
199   assert(!PreserveFramePointer, "Frame pointers need to be fixed");
200 }
201 
202 // Slow path
203 
204 inline frame ThawBase::new_entry_frame() {
205   intptr_t* sp = _cont.entrySP();
206   return frame(sp, sp, _cont.entryFP(), _cont.entryPC()); // TODO PERF: This finds code blob and computes deopt state
207 }
208 
209 template<typename FKind> frame ThawBase::new_stack_frame(const frame& hf, frame& caller, bool bottom) {
210   assert(FKind::is_instance(hf), "");
211   // The values in the returned frame object will be written into the callee's stack in patch.
212 
213   if (FKind::interpreted) {
214     intptr_t* heap_sp = hf.unextended_sp();
215     // If caller is interpreted it already made room for the callee arguments
216     int overlap = caller.is_interpreted_frame() ? ContinuationHelper::InterpretedFrame::stack_argsize(hf) : 0;
217     const int fsize = (int)(ContinuationHelper::InterpretedFrame::frame_bottom(hf) - hf.unextended_sp() - overlap);
218     const int locals = hf.interpreter_frame_method()->max_locals();
219     intptr_t* frame_sp = caller.unextended_sp() - fsize;
220     intptr_t* fp = frame_sp + (hf.fp() - heap_sp);
221     if ((intptr_t)fp % frame::frame_alignment != 0) {
222       fp--;
223       frame_sp--;
224       log_develop_trace(continuations)("Adding internal interpreted frame alignment");
225     }
226     DEBUG_ONLY(intptr_t* unextended_sp = fp + *hf.addr_at(frame::interpreter_frame_last_sp_offset);)
227     assert(frame_sp == unextended_sp, "");
228     caller.set_sp(fp + frame::sender_sp_offset);
229     frame f(frame_sp, frame_sp, fp, hf.pc());
230     // we need to set the locals so that the caller of new_stack_frame() can call
231     // ContinuationHelper::InterpretedFrame::frame_bottom
232     // copy relativized locals from the heap frame
233     *f.addr_at(frame::interpreter_frame_locals_offset) = *hf.addr_at(frame::interpreter_frame_locals_offset);
234     assert((intptr_t)f.fp() % frame::frame_alignment == 0, "");
235     return f;
236   } else {
237     int fsize = FKind::size(hf);
238     intptr_t* frame_sp = caller.unextended_sp() - fsize;
239     if (bottom || caller.is_interpreted_frame()) {
240       int argsize = hf.compiled_frame_stack_argsize();
241 
242       fsize += argsize;
243       frame_sp -= argsize;
244       caller.set_sp(caller.sp() - argsize);
245       assert(caller.sp() == frame_sp + (fsize-argsize), "");
246 
247       frame_sp = align(hf, frame_sp, caller, bottom);
248     }
249 
250     assert(hf.cb() != nullptr, "");
251     assert(hf.oop_map() != nullptr, "");
252     intptr_t* fp;
253     if (PreserveFramePointer) {
254       // we need to recreate a "real" frame pointer, pointing into the stack
255       fp = frame_sp + FKind::size(hf) - 2;
256     } else {
257       fp = FKind::stub
258         ? frame_sp + fsize - 2 // On RISCV, this value is used for the safepoint stub
259         : *(intptr_t**)(hf.sp() - 2); // we need to re-read fp because it may be an oop and we might have fixed the frame.
260     }
261     return frame(frame_sp, frame_sp, fp, hf.pc(), hf.cb(), hf.oop_map(), false); // TODO PERF : this computes deopt state; is it necessary?
262   }
263 }
264 
265 inline intptr_t* ThawBase::align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom) {
266 #ifdef _LP64
267   if (((intptr_t)frame_sp & 0xf) != 0) {
268     assert(caller.is_interpreted_frame() || (bottom && hf.compiled_frame_stack_argsize() % 2 != 0), "");
269     frame_sp--;
270     caller.set_sp(caller.sp() - 1);
271   }
272   assert(is_aligned(frame_sp, frame::frame_alignment), "");
273 #endif
274 
275   return frame_sp;
276 }
277 
278 inline void ThawBase::patch_pd(frame& f, const frame& caller) {
279   patch_callee_link(caller, caller.fp());
280 }
281 
282 inline void ThawBase::derelativize_interpreted_frame_metadata(const frame& hf, const frame& f) {
283   // Make sure that last_sp is kept relativized.
284   assert((intptr_t*)f.at_relative(frame::interpreter_frame_last_sp_offset) == f.unextended_sp(), "");
285 
286   // Make sure that monitor_block_top is still relativized.
287   assert(f.at_absolute(frame::interpreter_frame_monitor_block_top_offset) <= frame::interpreter_frame_initial_sp_offset, "");
288 
289   // Make sure that extended_sp is kept relativized.
290   assert((intptr_t*)f.at_relative(frame::interpreter_frame_extended_sp_offset) < f.unextended_sp(), "");
291 }
292 
293 #endif // CPU_RISCV_CONTINUATIONFREEZETHAW_RISCV_INLINE_HPP