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_PPC_CONTINUATION_PPC_INLINE_HPP
 26 #define CPU_PPC_CONTINUATION_PPC_INLINE_HPP
 27 
 28 #include "oops/stackChunkOop.inline.hpp"
 29 #include "runtime/frame.hpp"
 30 #include "runtime/frame.inline.hpp"
 31 
 32 inline void patch_callee_link(const frame& f, intptr_t* fp) {
 33   *ContinuationHelper::Frame::callee_link_address(f) = fp;
 34 }
 35 
 36 inline void patch_callee_link_relative(const frame& f, intptr_t* fp) {
 37   intptr_t* la = (intptr_t*)ContinuationHelper::Frame::callee_link_address(f);
 38   intptr_t new_value = fp - la;
 39   *la = new_value;
 40 }
 41 
 42 ////// Freeze
 43 
 44 // Fast path
 45 
 46 inline void FreezeBase::patch_stack_pd(intptr_t* frame_sp, intptr_t* heap_sp) {
 47   // Nothing to do. The backchain is reconstructed when thawing (see Thaw<ConfigT>::patch_caller_links())
 48 }
 49 
 50 // Slow path
 51 
 52 template<typename FKind>
 53 inline frame FreezeBase::sender(const frame& f) {
 54   assert(FKind::is_instance(f), "");
 55   if (FKind::interpreted) {
 56     return frame(f.sender_sp(), f.sender_pc(), f.interpreter_frame_sender_sp());
 57   }
 58 
 59   intptr_t* sender_sp = f.sender_sp();
 60   address sender_pc = f.sender_pc();
 61   assert(sender_sp != f.sp(), "must have changed");
 62 
 63   int slot = 0;
 64   CodeBlob* sender_cb = CodeCache::find_blob_and_oopmap(sender_pc, slot);
 65   return sender_cb != nullptr
 66     ? frame(sender_sp, sender_sp, nullptr, sender_pc, sender_cb, slot == -1 ? nullptr : sender_cb->oop_map_for_slot(slot, sender_pc))
 67     : frame(sender_sp, sender_pc, sender_sp);
 68 }
 69 
 70 void FreezeBase::adjust_interpreted_frame_unextended_sp(frame& f) {
 71   // nothing to do
 72 }
 73 
 74 inline void FreezeBase::prepare_freeze_interpreted_top_frame(frame& f) {
 75   // nothing to do
 76   DEBUG_ONLY( intptr_t* lspp = (intptr_t*) &(f.get_ijava_state()->top_frame_sp); )
 77   assert(*lspp == f.unextended_sp() - f.fp(), "should be " INTPTR_FORMAT " usp:" INTPTR_FORMAT " fp:" INTPTR_FORMAT, *lspp, p2i(f.unextended_sp()), p2i(f.fp()));
 78 }
 79 
 80 inline void FreezeBase::relativize_interpreted_frame_metadata(const frame& f, const frame& hf) {
 81   intptr_t* vfp = f.fp();
 82   intptr_t* hfp = hf.fp();
 83   assert(f.fp() > (intptr_t*)f.interpreter_frame_esp(), "");
 84 
 85   // There is alignment padding between vfp and f's locals array in the original
 86   // frame, because we freeze the padding (see recurse_freeze_interpreted_frame)
 87   // in order to keep the same relativized locals pointer, we don't need to change it here.
 88 
 89   // Make sure that monitors is already relativized.
 90   assert(hf.at_absolute(ijava_idx(monitors)) <= -(frame::ijava_state_size / wordSize), "");
 91 
 92   // Make sure that esp is already relativized.
 93   assert(hf.at_absolute(ijava_idx(esp)) <= hf.at_absolute(ijava_idx(monitors)), "");
 94 
 95   // top_frame_sp is already relativized
 96 
 97   // hfp == hf.sp() + (f.fp() - f.sp()) is not true on ppc because the stack frame has room for
 98   // the maximal expression stack and the expression stack in the heap frame is trimmed.
 99   assert(hf.fp() == hf.interpreter_frame_esp() + (f.fp() - f.interpreter_frame_esp()), "");
100   assert(hf.fp()                 <= (intptr_t*)hf.at(ijava_idx(locals)), "");
101 }
102 
103 inline void FreezeBase::set_top_frame_metadata_pd(const frame& hf) {
104   stackChunkOop chunk = _cont.tail();
105   assert(chunk->is_in_chunk(hf.sp()), "hf.sp()=" PTR_FORMAT, p2i(hf.sp()));
106 
107   hf.own_abi()->lr = (uint64_t)hf.pc();
108   if (hf.is_interpreted_frame()) {
109     patch_callee_link_relative(hf, hf.fp());
110   }
111 #ifdef ASSERT
112   else {
113     // See also FreezeBase::patch_pd()
114     patch_callee_link(hf, (intptr_t*)badAddress);
115   }
116 #endif
117 }
118 
119 //
120 // Heap frames differ from stack frames in the following aspects
121 //
122 // - they are just word aligned
123 // - the unextended sp of interpreted frames is set such that
124 //   unextended sp + frame::metadata_words_at_top + 1 points to the last call parameter
125 //   (the comment at the file end explains the unextended sp for interpreted frames on the stack)
126 //
127 // The difference in respect to the unextended sp is required to comply with shared code.
128 // Furthermore fast frozen and compiled frames have invalid back links (see
129 // Thaw<ConfigT>::patch_caller_links() and FreezeBase::patch_pd())
130 //
131 // === New Interpreted Frame ==========================================================================================
132 //
133 // ### Interpreted Caller: Overlap new frame with Caller
134 //
135 //     Caller on entry                                     New frame with resized Caller
136 //
137 //     | frame::java_abi        |                          |                        |
138 //     |                        |<- FP of caller           | Caller's SP            |<- FP of caller
139 //     ==========================                          ==========================
140 //     | ijava_state            |                          | ijava_state            |
141 //     |                        |                          |                        |
142 //     |------------------------|                  -----   |------------------------|
143 //     | P0                     |                    ^     | L0 aka P0              |
144 //     | :                      |                    |     | :      :               |
145 //     | Pn                     |<- unext. SP        |     | :      Pn              |<- unext. SP
146 //     |------------------------|   + metadata     overlap | :                      |   + metadata
147 //     | frame::java_abi        |                    |     | Lm                     |
148 //     | (metadata_words_at_top)|<- SP == unext. SP  v     |------------------------|<- unextended SP of caller (1)
149 //     ==========================   of caller      -----   | frame::java_abi        |
150 //                                                         | (metadata_words_at_top)|<- new SP of caller / FP of new frame
151 //      overlap = stack_argsize(f)                         ==========================       ^
152 //                + frame::metadata_words_at_top           | ijava_state            |       |
153 //                                                         |                        |       |
154 //      Where f is the frame to be relocated on the heap.  |------------------------|       |
155 //      See also StackChunkFrameStream::frame_size().      | Expressions            |   FP - esp of f
156 //                                                         | P0                     |       |
157 //                                                         | :                      |       |
158 //                            |  Growth  |                 | Pi                     |       v
159 //                            v          v                 |------------------------|      ---
160 //                                                         | frame::java_abi        |
161 //                                                         | (metadata_words_at_top)|<- unextended SP /
162 //                                                         ==========================   SP of new frame
163 // ### Compiled Caller: No Overlap
164 //
165 //     The caller is resized to accomodate the callee's locals and abi but there is _no_ overlap with
166 //     the original caller frame.
167 //
168 //     Caller on entry                                     New frame with resized Caller
169 //
170 //     | frame::java_abi        |                          |                        |
171 //     | (metadata_words_at_top)|<- FP of caller           | Caller's SP            |<- FP of caller
172 //     ==========================                          ==========================
173 //     |                        |                          |                        |
174 //     |                        |                          |                        |
175 //     |------------------------|                          |------------------------|
176 //     | frame::java_abi        |                          | frame::java_abi        |
177 //     | (metadata_words_at_top)|<- SP == unext. SP        | (metadata_words_at_top)|<- unext. SP of caller
178 //     ==========================   of caller              |------------------------|
179 //                                                         | L0 aka P0              |
180 //                                                         | :      :               |
181 //                                                         | :      Pn              |
182 //      overlap = 0                                        | Lm                     |
183 //                                                         |------------------------|
184 //      f is the frame to be relocated on the heap         | frame::java_abi        |
185 //                                                         | (metadata_words_at_top)|<- new SP of caller / FP of new frame
186 //                                                         ==========================       ^
187 //                                                         | ijava_state            |       |
188 //                             |  Growth  |                |                        |       |
189 //                             v          v                |------------------------|       |
190 //                                                         | Expressions            |   FP - esp of f
191 //                                                         | P0                     |       |
192 //                                                         | :                      |       |
193 //                                                         | Pi                     |       v
194 //                                                         |------------------------|      ---
195 //                                                         | frame::java_abi        |
196 //                                                         | (metadata_words_at_top)|<- unextended SP /
197 //                                                         ==========================   SP of new frame
198 //
199 // (1) Caller's unextended SP is preserved in callee's frame::ijava_state::sender_sp
200 //     (See ContinuationHelper::InterpretedFrame::patch_sender_sp). This is required
201 //     by StackChunkFrameStream<frame_kind>::next_for_interpreter_frame().
202 //
203 // === New Compiled Frame =============================================================================================
204 //
205 // ### Interpreted Caller: No Overlap
206 //
207 //     The caller is resized to accomodate the callee's stack arguments and abi but there is _no_ overlap with
208 //     the original caller frame.
209 //
210 //     Note: a new ABI is added to the caller even if there are no stackargs.
211 //     This is necessary to comply with shared code.
212 //
213 //     Caller on entry                                     New frame with resized Caller
214 //
215 //     | frame::java_abi        |                          | frame::java_abi        |
216 //     | (metadata_words_at_top)|<- FP of caller           | (metadata_words_at_top)|<- FP of caller
217 //     ==========================                          ==========================
218 //     | ijava_state            |                          | ijava_state            |
219 //     |                        |                          |                        |
220 //     |------------------------|                          |------------------------|
221 //     | P0                     |                          | P0                     |
222 //     | :                      |                          | :                      |
223 //     | Pn                     |<- unext. SP              | Pn                     |<- unext. SP
224 //     |------------------------|   + metadata             |------------------------|   + metadata
225 //     | frame::java_abi        |                          | frame::java_abi        |
226 //     | (metadata_words_at_top)|<- SP == unext. SP        | (metadata_words_at_top)|<- unextended SP of caller (1)
227 //     ==========================   of caller              |------------------------|
228 //                                                         | Stack Args             |
229 //      overlap = 0                                        | (if any)               |
230 //                                                         |------------------------|
231 //      f is the frame to be relocated on the heap         | frame::java_abi        |
232 //                                                         | (metadata_words_at_top)|<- new SP of caller / FP of new frame
233 //                                                         ==========================
234 //                                                         |                        |
235 //                             |  Growth  |                |                        |
236 //                             v          v                |------------------------|
237 //                                                         | frame::java_abi        |
238 //                                                         | (metadata_words_at_top)|<- SP == unext. SP of new frame
239 //                                                         ==========================
240 //
241 // ### Compiled Caller: Stackargs + ABI Overlap
242 //
243 //     Caller on entry                                     New frame with resized Caller
244 //
245 //     | frame::java_abi        |                          | frame::java_abi        |
246 //     | (metadata_words_at_top)|<- FP of caller           | (metadata_words_at_top)|<- FP of caller
247 //     ==========================                          ==========================
248 //     |                        |                          |                        |
249 //     |                        |                          |                        |
250 //     |------------------------|                   -----  |------------------------|
251 //     | Stack Args             |                     ^    | Stack Args             |
252 //     | (if any)               |                     |    | (if any)               |
253 //     |------------------------|                  overlap |------------------------|
254 //     | frame::java_abi        |                     |    | frame::java_abi        |
255 //     | (metadata_words_at_top)|<- SP == unext. SP   v    | (metadata_words_at_top)|<- SP == unext. SP of caller
256 //     ==========================   of caller       -----  ==========================   / FP of new frame
257 //                                                         |                        |
258 //      overlap = stack_argsize(f)                         |                        |
259 //                + frame::metadata_words_at_top           |------------------------|
260 //                                                         | frame::java_abi        |
261 //      Where f is the frame to be relocated on the heap.  | (metadata_words_at_top)|<- SP == unext. SP of new frame
262 //      See also StackChunkFrameStream::frame_size().      ==========================
263 //
264 template<typename FKind>
265 frame FreezeBase::new_heap_frame(frame& f, frame& caller) {
266   assert(FKind::is_instance(f), "");
267 
268   intptr_t *sp, *fp;
269   if (FKind::interpreted) {
270     intptr_t locals_offset = *f.addr_at(ijava_idx(locals));
271     // If the caller.is_empty(), i.e. we're freezing into an empty chunk, then we set
272     // the chunk's argsize in finalize_freeze and make room for it above the unextended_sp
273     // See also comment on StackChunkFrameStream<frame_kind>::interpreter_frame_size()
274     int overlap =
275         (caller.is_interpreted_frame() || caller.is_empty())
276         ? ContinuationHelper::InterpretedFrame::stack_argsize(f) + frame::metadata_words_at_top
277         : 0;
278     fp = caller.unextended_sp() - 1 - locals_offset + overlap;
279     // esp points one slot below the last argument
280     intptr_t* x86_64_like_unextended_sp = f.interpreter_frame_esp() + 1 - frame::metadata_words_at_top;
281     sp = fp - (f.fp() - x86_64_like_unextended_sp);
282 
283     assert (sp <= fp && (fp <= caller.unextended_sp() || caller.is_interpreted_frame()),
284             "sp=" PTR_FORMAT " fp=" PTR_FORMAT " caller.unextended_sp()=" PTR_FORMAT " caller.is_interpreted_frame()=%d",
285             p2i(sp), p2i(fp), p2i(caller.unextended_sp()), caller.is_interpreted_frame());
286     caller.set_sp(fp);
287 
288     assert(_cont.tail()->is_in_chunk(sp), "");
289 
290     frame hf(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
291     // frame_top() and frame_bottom() read these before relativize_interpreted_frame_metadata() is called
292     *hf.addr_at(ijava_idx(locals)) = locals_offset;
293     *hf.addr_at(ijava_idx(esp))    = f.interpreter_frame_esp() - f.fp();
294     return hf;
295   } else {
296     int fsize = FKind::size(f);
297     sp = caller.unextended_sp() - fsize;
298     if (caller.is_interpreted_frame()) {
299       // If the caller is interpreted, our stackargs are not supposed to overlap with it
300       // so we make more room by moving sp down by argsize
301       int argsize = FKind::stack_argsize(f);
302       sp -= argsize + frame::metadata_words_at_top;
303     }
304     fp = sp + fsize;
305     caller.set_sp(fp);
306 
307     assert(_cont.tail()->is_in_chunk(sp), "");
308 
309     return frame(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
310   }
311 }
312 
313 inline void FreezeBase::patch_pd(frame& hf, const frame& caller) {
314   if (caller.is_interpreted_frame()) {
315     assert(!caller.is_empty(), "");
316     patch_callee_link_relative(caller, caller.fp());
317   }
318 #ifdef ASSERT
319   else {
320     // For compiled frames the back link is actually redundant. It gets computed
321     // as unextended_sp + frame_size.
322 
323     // Note the difference on x86_64: the link is not made relative if the caller
324     // is a compiled frame because there rbp is used as a non-volatile register by
325     // c1/c2 so it could be a computed value local to the caller.
326 
327     // See also:
328     // - FreezeBase::set_top_frame_metadata_pd
329     // - StackChunkFrameStream<frame_kind>::fp()
330     // - UseContinuationFastPath: compiled frames are copied in a batch w/o patching the back link.
331     //   The backlinks are restored when thawing (see Thaw<ConfigT>::patch_caller_links())
332     patch_callee_link(hf, (intptr_t*)badAddress);
333   }
334 #endif
335 }
336 
337 //////// Thaw
338 
339 // Fast path
340 
341 inline void ThawBase::prefetch_chunk_pd(void* start, int size) {
342   size <<= LogBytesPerWord;
343   Prefetch::read(start, size);
344   Prefetch::read(start, size - 64);
345 }
346 
347 // Set back chain links of fast thawed frames such that *sp == callers_sp.
348 // See https://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#STACK
349 template <typename ConfigT>
350 inline void Thaw<ConfigT>::patch_caller_links(intptr_t* sp, intptr_t* bottom) {
351   for (intptr_t* callers_sp; sp < bottom; sp = callers_sp) {
352     address pc = (address)((frame::java_abi*) sp)->lr;
353     assert(pc != nullptr, "");
354     // see ThawBase::patch_return() which gets called just before
355     bool is_entry_frame = pc == StubRoutines::cont_returnBarrier() || pc == _cont.entryPC();
356     if (is_entry_frame) {
357       callers_sp = _cont.entryFP();
358     } else {
359       assert(!Interpreter::contains(pc), "sp:" PTR_FORMAT " pc:" PTR_FORMAT, p2i(sp), p2i(pc));
360       CodeBlob* cb = CodeCache::find_blob_fast(pc);
361       callers_sp = sp + cb->frame_size();
362     }
363     // set the back link
364     ((frame::java_abi*) sp)->callers_sp = (intptr_t) callers_sp;
365   }
366 }
367 
368 // Slow path
369 
370 inline frame ThawBase::new_entry_frame() {
371   intptr_t* sp = _cont.entrySP();
372   return frame(sp, _cont.entryPC(), sp, _cont.entryFP());
373 }
374 
375 // === New Interpreted Frame ================================================================================================================
376 //
377 // ### Non-Interpreted Caller (compiled, enterSpecial): No Overlap
378 //
379 //     Heap Frame `hf`                                   `hf` gets copied to stack _without_ overlapping the caller
380 //
381 //     |                      |                            Non-Interpreted |                      |
382 //     |                      |<- bottom                   Caller          |----------------------|
383 //     |----------------------|    ^                                       | frame::java_abi      |<- unextended SP
384 //     | L0 aka P0            |    |                                   --- ========================
385 //     | :      :             |    |                                    ^  | L0 aka P0            |
386 //     | :      Pn            |    |                                    |  | :      :             | Parameters do
387 //     | :                    |    |                                    |  | :      Pn            | not overlap with
388 //     | Lm                   |    |                                    |  | :                    | caller!
389 //     |----------------------| `fsize`                                 |  | :                    |
390 //     | frame::java_abi      |    |                                       | :                    |
391 //     ========================    |                     `fsize` + padding | Lm                   |
392 //     |                      |    |                                       |----------------------|
393 //     | ijava_state          |    |                                    |  | Opt. Align. Padding  |
394 //     |                      |    |                                    |  |----------------------|
395 //     |----------------------|    |                                    |  | frame::java_abi      |<- new SP of caller
396 //     | L0 aka P0            |    |                                    |  ========================   / FP of new frame
397 //     | :      :             |    |                                    |  |                      |   (aligned)
398 //     | :      Pn            |<- unext. SP + metadata                  |  | ijava_state          |
399 //     | :                    |    |                                    |  |                      |
400 //     | Lm                   |    |                                    |  |----------------------|
401 //     |----------------------|    v                                    |  | P0                   |
402 //     | frame::java_abi      |<- SP / unextended SP                    |  | :                    |
403 //     ========================                                         |  | Pi                   |<- unextended SP + metadata
404 //                                                                      |  |----------------------|
405 //                                           | Growth |                 v  | frame::java_abi      |<- unextended SP / SP of new frame
406 //                                           v        v                --- ========================   (not yet aligned(1))
407 //
408 //
409 // ### Interpreted Caller: Overlap with Caller
410 //
411 //     Caller                                                              New frame with resized/aligned Caller
412 //
413 //     |                      |                                            |                      |
414 //     | ijava_state          |                                            | ijava_state          |
415 //     |----------------------|                                            |----------------------|
416 //     | non param. expr.     |                                     bottom | non param. expr.     |
417 //     | - - - - - - - - - -  |                           ---           ^  | - - - - - - - - - -  |
418 //     | P0                   |                            ^            |  | L0 aka P0            |
419 //     | :                    |                            |            |  | :      :             |
420 //     | Pn                   |<- unextended SP           overlap       |  | :      Pn            |<- unextended SP
421 //     |----------------------|   + metadata_words_at_top  |            |  | :                    |   + metadata_words_at_top
422 //     | frame::java_abi      |<- unextended SP            v            |  | :                    |   (unaligned)
423 //     ========================   / SP of new frame       ---           |  | :                    |   of caller
424 //                                (not yet aligned(1))                  |  | Lm                   |
425 //                                                                `fsize`  |----------------------|
426 //       overlap = stack_argsize(hf)                              + padding| Opt. Align. Padding  |
427 //                 + frame::metadata_words_at_top                       |  |----------------------|
428 //                                                                      |  | frame::java_abi      |<- new SP of caller
429 //                                                                      |  ========================   / FP of new frame
430 //                                                                      |  |                      |   (aligned)
431 //                                  | Growth |                          |  | ijava_state          |
432 //                                  v        v                          |  |                      |
433 //                                                                      |  |----------------------|
434 //                                                                      |  | P0                   |
435 //                                                                      |  | :                    |
436 //                                                                      |  | Pi                   |<- unextended SP
437 //                                                                      |  |----------------------|    + metadata_words_at_top
438 //                                                                      v  | frame::java_abi      |<- unextended SP / SP of new frame
439 //                                                                     --- ========================   (not yet aligned(1))
440 //
441 //
442 //  (1) The SP / unextended SP of the new interpreted frame is not aligned. It
443 //      gets aligned when its callee is pushed on stack or in finish_thaw() if
444 //      it is the top frame. This allows addressing parameters: unextended SP + metadata_words_at_top
445 //
446 //  (2) If caller is interpreted then its ijava_state::top_frame_sp will be used as sender sp
447 //      of the new frame (see ContinuationHelper::InterpretedFrame::patch_sender_sp() and diagram at the end of this file)
448 //
449 //  (3) The size of alignment padding required when thawing frames is accounted for
450 //      in FreezeBase::_align_size.
451 //
452 // === New Compiled Frame ===================================================================================================================
453 //
454 //        Compiled Caller                              Interpreted Caller
455 //
456 //        - stackargs+abi overlap with caller          - gets resized for stackargs
457 //        - no alignment padding                       - SP gets aligned
458 //                                                     - no overlap with orig.
459 //                                                       caller
460 //   O C
461 //   r a  |                      |                     |                      |
462 //   i l  |                      |                     |                      |
463 //   g l  |----------------------|                     |                      |
464 //   i e  | Stack Args           |                     |                      |
465 //   n r  | (if any)             |                     |----------------------|
466 //   a    |----------------------|                     | frame::java_abi      |
467 //   l    | frame::java_abi      |<- unext. SP / SP    | (unused)             |<- unal.unext.SP
468 //  - - - ======================== - - - - - - - - - - |----------------------|- - - - - - - - - - - - - - - - - - - - - - - - - - - -
469 //    N   |                      |                     | Opt. Align. Padding  |
470 //    e   |                      |                     |----------------------|
471 //    w   |----------------------|                     | Stack Args           |
472 //        | frame::java_abi      |<- unext. SP / SP    | (if any)             |
473 //    F   ========================                     |----------------------|
474 //    r                                                | frame::java_abi      |<- caller's SP
475 //    a                                                ======================== / new frame's FP
476 //    m                                                |                      |   (aligned)
477 //    e                                                |                      |
478 //                                                     |----------------------|
479 //                                                     | frame::java_abi      |<- unext. SP / SP
480 //                                                     ========================
481 //
482 //  If the new frame is at the bottom just above the ContinuationEntry frame then the stackargs
483 //  don't overlap the caller either even though it is compiled because the size is not
484 //  limited/known. In contrast to the interpreted caller case the abi overlaps with the caller
485 //  if there are no stackargs. This is to comply with shared code (see e.g. StackChunkFrameStream::frame_size())
486 //
487 template<typename FKind> frame ThawBase::new_stack_frame(const frame& hf, frame& caller, bool bottom) {
488   assert(FKind::is_instance(hf), "");
489 
490   assert(is_aligned(caller.fp(), frame::frame_alignment), PTR_FORMAT, p2i(caller.fp()));
491   // caller.sp() can be unaligned. This is fixed below.
492   if (FKind::interpreted) {
493     // Note: we have to overlap with the caller, at least if it is interpreted, to match the
494     // max_thawing_size calculation during freeze. See also comment above.
495     intptr_t* heap_sp = hf.unextended_sp();
496     const int fsize = ContinuationHelper::InterpretedFrame::frame_bottom(hf) - hf.unextended_sp();
497     const int overlap = !caller.is_interpreted_frame() ? 0
498                         : ContinuationHelper::InterpretedFrame::stack_argsize(hf) + frame::metadata_words_at_top;
499     intptr_t* frame_sp = caller.unextended_sp() + overlap - fsize;
500     intptr_t* fp = frame_sp + (hf.fp() - heap_sp);
501     // align fp
502     int padding = fp - align_down(fp, frame::frame_alignment);
503     fp -= padding;
504     // alignment of sp is done by callee or in finish_thaw()
505     frame_sp -= padding;
506 
507     // On ppc esp points to the next free slot on the expression stack and sp + metadata points to the last parameter
508     DEBUG_ONLY(intptr_t* esp = fp + *hf.addr_at(ijava_idx(esp));)
509     assert(frame_sp + frame::metadata_words_at_top == esp+1, " frame_sp=" PTR_FORMAT " esp=" PTR_FORMAT, p2i(frame_sp), p2i(esp));
510     caller.set_sp(fp);
511     frame f(frame_sp, hf.pc(), frame_sp, fp);
512     // we need to set the locals so that the caller of new_stack_frame() can call
513     // ContinuationHelper::InterpretedFrame::frame_bottom
514     // copy relativized locals from the heap frame
515     *f.addr_at(ijava_idx(locals)) = *hf.addr_at(ijava_idx(locals));
516 
517     return f;
518   } else {
519     int fsize = FKind::size(hf);
520     int argsize = FKind::stack_argsize(hf);
521     intptr_t* frame_sp = caller.sp() - fsize;
522 
523     if ((bottom && argsize > 0) || caller.is_interpreted_frame()) {
524       frame_sp -= argsize + frame::metadata_words_at_top;
525       frame_sp = align_down(frame_sp, frame::alignment_in_bytes);
526       caller.set_sp(frame_sp + fsize);
527     }
528 
529     assert(hf.cb() != nullptr, "");
530     assert(hf.oop_map() != nullptr, "");
531     intptr_t* fp = frame_sp + fsize;
532     return frame(frame_sp, frame_sp, fp, hf.pc(), hf.cb(), hf.oop_map(), false);
533   }
534 }
535 
536 inline intptr_t* ThawBase::align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom) {
537   // Unused. Alignment is done directly in new_stack_frame() / finish_thaw().
538   return nullptr;
539 }
540 
541 inline void ThawBase::derelativize_interpreted_frame_metadata(const frame& hf, const frame& f) {
542   intptr_t* vfp = f.fp();
543 
544   // Make sure that monitors is still relativized.
545   assert(f.at_absolute(ijava_idx(monitors)) <= -(frame::ijava_state_size / wordSize), "");
546 
547   // Make sure that esp is still relativized.
548   assert(f.at_absolute(ijava_idx(esp)) <= f.at_absolute(ijava_idx(monitors)), "");
549 
550   // Keep top_frame_sp relativized.
551 }
552 
553 inline intptr_t* ThawBase::push_cleanup_continuation() {
554   frame enterSpecial = new_entry_frame();
555   frame::common_abi* enterSpecial_abi = (frame::common_abi*)enterSpecial.sp();
556 
557   enterSpecial_abi->lr = (intptr_t)ContinuationEntry::cleanup_pc();
558 
559   log_develop_trace(continuations, preempt)("push_cleanup_continuation enterSpecial sp: " INTPTR_FORMAT " cleanup pc: " INTPTR_FORMAT,
560                                             p2i(enterSpecial_abi),
561                                             p2i(ContinuationEntry::cleanup_pc()));
562 
563   return enterSpecial.sp();
564 }
565 
566 inline void ThawBase::patch_pd(frame& f, const frame& caller) {
567   patch_callee_link(caller, caller.fp());
568   // Prevent assertion if f gets deoptimized right away before it's fully initialized
569   f.mark_not_fully_initialized();
570 }
571 
572 inline void ThawBase::patch_pd(frame& f, intptr_t* caller_sp) {
573   assert(f.own_abi()->callers_sp == (uint64_t)caller_sp, "should have been fixed by patch_caller_links");
574 }
575 
576 //
577 // Interpreter Calling Procedure on PPC
578 //
579 // Caller                                   Resized Caller before the Call                New Callee Frame
580 //
581 //   - SP/FP are 16 byte aligned.           - The unused part of the expression stack     - The caller's original SP is passed as
582 //     Padding is added as necessary.         is removed                                    sender SP (in R21_sender_SP) also by
583 //   - SP is _not_ used as esp              - Slots for the callee's nonparameter locals    compiled callers. It is saved in the
584 //     (expression stack pointer)             are added.                                    ijava_state::sender_sp slot and
585 //   - Has reserved slots for the           - The large ABI is replaced with a minimal      restored when returning.
586 //     maximal expression stack               ABI.                                          This removes a c2i extension if there
587 //   - Has a larger ABI section on          - The original SP was saved in                  is one.
588 //     top that is required to call           ijava_state::top_frame_sp slot.             - ijava_state::sender_sp will be set
589 //     C++ code                               From there it is restored as SP _after_       as the caller's unextended sp when
590 //                                            returning from a call. This reverts the       iterating stack frames
591 //                                            resizing described above. It is also          (see frame::unextended_sp() and
592 //                                            required to undo potential i2c extensions     frame::sender_for_interpreter_frame())
593 //                                            if the calle should be compiled.
594 //                                          - Note that unextended SP < SP
595 //                                            is possible on ppc.
596 //
597 // |                      |                 |                      |                      |                      |
598 // | (frame::java_abi)    |                 | (frame::java_abi)    |                      | (frame::java_abi)    |
599 // | 4 words              |                 | 4 words              |                      | 4 words              |
600 // | Caller's SP          |<- FP of caller  | Caller's SP          |<- FP of caller       | Caller's SP          |<- FP of caller
601 // ========================   (aligned)     ========================                      ========================
602 // | frame::              |                 | frame::              |                      | frame::              |
603 // | ijava_state          |                 | ijava_state          |                      | ijava_state          |
604 // |                      |                 |                      |                      |                      |
605 // |----------------------|                 |----------------------|                      |----------------------|
606 // | P0                   |                 | L0 aka P0            |                      | L0 aka P0            |
607 // |                      |                 | :                    |                      | :                    |
608 // | Pn                   |                 | :      Pn            |                      | :      Pn            |
609 // |----------------------|                 | :                    |                      | :                    |
610 // |                      |                 | Lm                   |                      | Lm                   |
611 // | Reserved Expr. Stack |                 |----------------------|                      |----------------------|
612 // |                      |                 | Opt. Alignm. Padding |                      | Opt. Alignm. Padding |
613 // |                      |<- ConstMethod   |----------------------|                      |----------------------|
614 // |----------------------|   ::_max_stack  |                      |                      |                      |
615 // | Opt. Alignm. Padding |                 | (frame::java_abi)    |                      | (frame::java_abi)    |
616 // |----------------------|                 | 4 words              |                      | 4 words              |
617 // | Large ABI            |                 | Caller's SP          |<- new SP of caller   | Caller's SP          |<- SP of caller /
618 // | for C++ calls        |                 ========================   (aligned)          ========================   FP of callee
619 // | (frame::             |                                                               | frame::              |   (aligned)
620 // |  native_abi_reg_args)|                                                               | ijava_state          |
621 // |                      |                                                               |                      |
622 // |                      |                                                               |----------------------|
623 // |                      |                                                               |                      |
624 // | Caller's SP          |<- SP of caller                          <- unextended SP      | Reserved Expr. Stack |<- unextended SP
625 // ========================   (aligned)                                of caller          |                      |   of caller
626 //                                                                     (aligned)          |                      |
627 //                                                                                        |                      |
628 //                                                                                        |                      |
629 //                                                                                        |                      |
630 //                                                                                        |                      |<- ConstMethod
631 //                                                                                        |----------------------|   ::_max_stack
632 //                         Resize Caller                    Push new Callee Frame         | Opt. Alignm. Padding |
633 //                     -------------------->              ------------------------>       |----------------------|
634 //                     (ABI, expressions, locals)                                         | Large ABI            |
635 //                                                                                        | for C++ calls        |
636 //                                                                                        | (frame::             |
637 //                                                                                        |  native_abi_reg_args)|
638 //                                                |  Growth  |                            |                      |
639 //                                                v          v                            |                      |
640 //                                                                                        |                      |
641 //                                                                                        | Caller's SP          |<- SP of callee
642 //                                                                                        ========================   (aligned)
643 //
644 //
645 #endif // CPU_PPC_CONTINUATION_PPC_INLINE_HPP