1 /*
  2  * Copyright (c) 2019, 2024, 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 inline void FreezeBase::patch_pd_unused(intptr_t* sp) {
338 }
339 
340 inline intptr_t* AnchorMark::anchor_mark_set_pd() {
341   return _top_frame.sp();
342 }
343 
344 inline void AnchorMark::anchor_mark_clear_pd() {
345 }
346 
347 //////// Thaw
348 
349 // Fast path
350 
351 inline void ThawBase::prefetch_chunk_pd(void* start, int size) {
352   size <<= LogBytesPerWord;
353   Prefetch::read(start, size);
354   Prefetch::read(start, size - 64);
355 }
356 
357 // Set back chain links of fast thawed frames such that *sp == callers_sp.
358 // See https://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#STACK
359 template <typename ConfigT>
360 inline void Thaw<ConfigT>::patch_caller_links(intptr_t* sp, intptr_t* bottom) {
361   for (intptr_t* callers_sp; sp < bottom; sp = callers_sp) {
362     address pc = (address)((frame::java_abi*) sp)->lr;
363     assert(pc != nullptr, "");
364     // see ThawBase::patch_return() which gets called just before
365     bool is_entry_frame = pc == StubRoutines::cont_returnBarrier() || pc == _cont.entryPC();
366     if (is_entry_frame) {
367       callers_sp = _cont.entryFP();
368     } else {
369       assert(!Interpreter::contains(pc), "sp:" PTR_FORMAT " pc:" PTR_FORMAT, p2i(sp), p2i(pc));
370       CodeBlob* cb = CodeCache::find_blob_fast(pc);
371       callers_sp = sp + cb->frame_size();
372     }
373     // set the back link
374     ((frame::java_abi*) sp)->callers_sp = (intptr_t) callers_sp;
375   }
376 }
377 
378 // Slow path
379 
380 inline frame ThawBase::new_entry_frame() {
381   intptr_t* sp = _cont.entrySP();
382   return frame(sp, _cont.entryPC(), sp, _cont.entryFP());
383 }
384 
385 // === New Interpreted Frame ================================================================================================================
386 //
387 // ### Non-Interpreted Caller (compiled, enterSpecial): No Overlap
388 //
389 //     Heap Frame `hf`                                   `hf` gets copied to stack _without_ overlapping the caller
390 //
391 //     |                      |                            Non-Interpreted |                      |
392 //     |                      |<- bottom                   Caller          |----------------------|
393 //     |----------------------|    ^                                       | frame::java_abi      |<- unextended SP
394 //     | L0 aka P0            |    |                                   --- ========================
395 //     | :      :             |    |                                    ^  | L0 aka P0            |
396 //     | :      Pn            |    |                                    |  | :      :             | Parameters do
397 //     | :                    |    |                                    |  | :      Pn            | not overlap with
398 //     | Lm                   |    |                                    |  | :                    | caller!
399 //     |----------------------| `fsize`                                 |  | :                    |
400 //     | frame::java_abi      |    |                                       | :                    |
401 //     ========================    |                     `fsize` + padding | Lm                   |
402 //     |                      |    |                                       |----------------------|
403 //     | ijava_state          |    |                                    |  | Opt. Align. Padding  |
404 //     |                      |    |                                    |  |----------------------|
405 //     |----------------------|    |                                    |  | frame::java_abi      |<- new SP of caller
406 //     | L0 aka P0            |    |                                    |  ========================   / FP of new frame
407 //     | :      :             |    |                                    |  |                      |   (aligned)
408 //     | :      Pn            |<- unext. SP + metadata                  |  | ijava_state          |
409 //     | :                    |    |                                    |  |                      |
410 //     | Lm                   |    |                                    |  |----------------------|
411 //     |----------------------|    v                                    |  | P0                   |
412 //     | frame::java_abi      |<- SP / unextended SP                    |  | :                    |
413 //     ========================                                         |  | Pi                   |<- unextended SP + metadata
414 //                                                                      |  |----------------------|
415 //                                           | Growth |                 v  | frame::java_abi      |<- unextended SP / SP of new frame
416 //                                           v        v                --- ========================   (not yet aligned(1))
417 //
418 //
419 // ### Interpreted Caller: Overlap with Caller
420 //
421 //     Caller                                                              New frame with resized/aligned Caller
422 //
423 //     |                      |                                            |                      |
424 //     | ijava_state          |                                            | ijava_state          |
425 //     |----------------------|                                            |----------------------|
426 //     | non param. expr.     |                                     bottom | non param. expr.     |
427 //     | - - - - - - - - - -  |                           ---           ^  | - - - - - - - - - -  |
428 //     | P0                   |                            ^            |  | L0 aka P0            |
429 //     | :                    |                            |            |  | :      :             |
430 //     | Pn                   |<- unextended SP           overlap       |  | :      Pn            |<- unextended SP
431 //     |----------------------|   + metadata_words_at_top  |            |  | :                    |   + metadata_words_at_top
432 //     | frame::java_abi      |<- unextended SP            v            |  | :                    |   (unaligned)
433 //     ========================   / SP of new frame       ---           |  | :                    |   of caller
434 //                                (not yet aligned(1))                  |  | Lm                   |
435 //                                                                `fsize`  |----------------------|
436 //       overlap = stack_argsize(hf)                              + padding| Opt. Align. Padding  |
437 //                 + frame::metadata_words_at_top                       |  |----------------------|
438 //                                                                      |  | frame::java_abi      |<- new SP of caller
439 //                                                                      |  ========================   / FP of new frame
440 //                                                                      |  |                      |   (aligned)
441 //                                  | Growth |                          |  | ijava_state          |
442 //                                  v        v                          |  |                      |
443 //                                                                      |  |----------------------|
444 //                                                                      |  | P0                   |
445 //                                                                      |  | :                    |
446 //                                                                      |  | Pi                   |<- unextended SP
447 //                                                                      |  |----------------------|    + metadata_words_at_top
448 //                                                                      v  | frame::java_abi      |<- unextended SP / SP of new frame
449 //                                                                     --- ========================   (not yet aligned(1))
450 //
451 //
452 //  (1) The SP / unextended SP of the new interpreted frame is not aligned. It
453 //      gets aligned when its callee is pushed on stack or in finish_thaw() if
454 //      it is the top frame. This allows addressing parameters: unextended SP + metadata_words_at_top
455 //
456 //  (2) If caller is interpreted then its ijava_state::top_frame_sp will be used as sender sp
457 //      of the new frame (see ContinuationHelper::InterpretedFrame::patch_sender_sp() and diagram at the end of this file)
458 //
459 //  (3) The size of alignment padding required when thawing frames is accounted for
460 //      in FreezeBase::_align_size.
461 //
462 // === New Compiled Frame ===================================================================================================================
463 //
464 //        Compiled Caller                              Interpreted Caller
465 //
466 //        - stackargs+abi overlap with caller          - gets resized for stackargs
467 //        - no alignment padding                       - SP gets aligned
468 //                                                     - no overlap with orig.
469 //                                                       caller
470 //   O C
471 //   r a  |                      |                     |                      |
472 //   i l  |                      |                     |                      |
473 //   g l  |----------------------|                     |                      |
474 //   i e  | Stack Args           |                     |                      |
475 //   n r  | (if any)             |                     |----------------------|
476 //   a    |----------------------|                     | frame::java_abi      |
477 //   l    | frame::java_abi      |<- unext. SP / SP    | (unused)             |<- unal.unext.SP
478 //  - - - ======================== - - - - - - - - - - |----------------------|- - - - - - - - - - - - - - - - - - - - - - - - - - - -
479 //    N   |                      |                     | Opt. Align. Padding  |
480 //    e   |                      |                     |----------------------|
481 //    w   |----------------------|                     | Stack Args           |
482 //        | frame::java_abi      |<- unext. SP / SP    | (if any)             |
483 //    F   ========================                     |----------------------|
484 //    r                                                | frame::java_abi      |<- caller's SP
485 //    a                                                ======================== / new frame's FP
486 //    m                                                |                      |   (aligned)
487 //    e                                                |                      |
488 //                                                     |----------------------|
489 //                                                     | frame::java_abi      |<- unext. SP / SP
490 //                                                     ========================
491 //
492 //  If the new frame is at the bottom just above the ContinuationEntry frame then the stackargs
493 //  don't overlap the caller either even though it is compiled because the size is not
494 //  limited/known. In contrast to the interpreted caller case the abi overlaps with the caller
495 //  if there are no stackargs. This is to comply with shared code (see e.g. StackChunkFrameStream::frame_size())
496 //
497 template<typename FKind> frame ThawBase::new_stack_frame(const frame& hf, frame& caller, bool bottom) {
498   assert(FKind::is_instance(hf), "");
499 
500   assert(is_aligned(caller.fp(), frame::frame_alignment), PTR_FORMAT, p2i(caller.fp()));
501   // caller.sp() can be unaligned. This is fixed below.
502   if (FKind::interpreted) {
503     // Note: we have to overlap with the caller, at least if it is interpreted, to match the
504     // max_thawing_size calculation during freeze. See also comment above.
505     intptr_t* heap_sp = hf.unextended_sp();
506     const int fsize = ContinuationHelper::InterpretedFrame::frame_bottom(hf) - hf.unextended_sp();
507     const int overlap = !caller.is_interpreted_frame() ? 0
508                         : ContinuationHelper::InterpretedFrame::stack_argsize(hf) + frame::metadata_words_at_top;
509     intptr_t* frame_sp = caller.unextended_sp() + overlap - fsize;
510     intptr_t* fp = frame_sp + (hf.fp() - heap_sp);
511     // align fp
512     int padding = fp - align_down(fp, frame::frame_alignment);
513     fp -= padding;
514     // alignment of sp is done by callee or in finish_thaw()
515     frame_sp -= padding;
516 
517     // On ppc esp points to the next free slot on the expression stack and sp + metadata points to the last parameter
518     DEBUG_ONLY(intptr_t* esp = fp + *hf.addr_at(ijava_idx(esp));)
519     assert(frame_sp + frame::metadata_words_at_top == esp+1, " frame_sp=" PTR_FORMAT " esp=" PTR_FORMAT, p2i(frame_sp), p2i(esp));
520     caller.set_sp(fp);
521     frame f(frame_sp, hf.pc(), frame_sp, fp);
522     // we need to set the locals so that the caller of new_stack_frame() can call
523     // ContinuationHelper::InterpretedFrame::frame_bottom
524     // copy relativized locals from the heap frame
525     *f.addr_at(ijava_idx(locals)) = *hf.addr_at(ijava_idx(locals));
526 
527     return f;
528   } else {
529     int fsize = FKind::size(hf);
530     int argsize = FKind::stack_argsize(hf);
531     intptr_t* frame_sp = caller.sp() - fsize;
532 
533     if ((bottom && argsize > 0) || caller.is_interpreted_frame()) {
534       frame_sp -= argsize + frame::metadata_words_at_top;
535       frame_sp = align_down(frame_sp, frame::alignment_in_bytes);
536       caller.set_sp(frame_sp + fsize);
537     }
538 
539     assert(hf.cb() != nullptr, "");
540     assert(hf.oop_map() != nullptr, "");
541     intptr_t* fp = frame_sp + fsize;
542     return frame(frame_sp, frame_sp, fp, hf.pc(), hf.cb(), hf.oop_map(), false);
543   }
544 }
545 
546 inline intptr_t* ThawBase::align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom) {
547   // Unused. Alignment is done directly in new_stack_frame() / finish_thaw().
548   return nullptr;
549 }
550 
551 inline void ThawBase::derelativize_interpreted_frame_metadata(const frame& hf, const frame& f) {
552   intptr_t* vfp = f.fp();
553 
554   // Make sure that monitors is still relativized.
555   assert(f.at_absolute(ijava_idx(monitors)) <= -(frame::ijava_state_size / wordSize), "");
556 
557   // Make sure that esp is still relativized.
558   assert(f.at_absolute(ijava_idx(esp)) <= f.at_absolute(ijava_idx(monitors)), "");
559 
560   // Keep top_frame_sp relativized.
561 }
562 
563 inline intptr_t* ThawBase::push_cleanup_continuation() {
564   frame enterSpecial = new_entry_frame();
565   frame::common_abi* enterSpecial_abi = (frame::common_abi*)enterSpecial.sp();
566 
567   enterSpecial_abi->lr = (intptr_t)ContinuationEntry::cleanup_pc();
568 
569   log_develop_trace(continuations, preempt)("push_cleanup_continuation enterSpecial sp: " INTPTR_FORMAT " cleanup pc: " INTPTR_FORMAT,
570                                             p2i(enterSpecial_abi),
571                                             p2i(ContinuationEntry::cleanup_pc()));
572 
573   return enterSpecial.sp();
574 }
575 
576 inline intptr_t* ThawBase::push_preempt_adapter() {
577   Unimplemented();
578   return nullptr;
579 }
580 
581 inline void ThawBase::patch_pd(frame& f, const frame& caller) {
582   patch_callee_link(caller, caller.fp());
583   // Prevent assertion if f gets deoptimized right away before it's fully initialized
584   f.mark_not_fully_initialized();
585 }
586 
587 inline void ThawBase::patch_pd(frame& f, intptr_t* caller_sp) {
588   assert(f.own_abi()->callers_sp == (uint64_t)caller_sp, "should have been fixed by patch_caller_links");
589 }
590 
591 //
592 // Interpreter Calling Procedure on PPC
593 //
594 // Caller                                   Resized Caller before the Call                New Callee Frame
595 //
596 //   - SP/FP are 16 byte aligned.           - The unused part of the expression stack     - The caller's original SP is passed as
597 //     Padding is added as necessary.         is removed                                    sender SP (in R21_sender_SP) also by
598 //   - SP is _not_ used as esp              - Slots for the callee's nonparameter locals    compiled callers. It is saved in the
599 //     (expression stack pointer)             are added.                                    ijava_state::sender_sp slot and
600 //   - Has reserved slots for the           - The large ABI is replaced with a minimal      restored when returning.
601 //     maximal expression stack               ABI.                                          This removes a c2i extension if there
602 //   - Has a larger ABI section on          - The original SP was saved in                  is one.
603 //     top that is required to call           ijava_state::top_frame_sp slot.             - ijava_state::sender_sp will be set
604 //     C++ code                               From there it is restored as SP _after_       as the caller's unextended sp when
605 //                                            returning from a call. This reverts the       iterating stack frames
606 //                                            resizing described above. It is also          (see frame::unextended_sp() and
607 //                                            required to undo potential i2c extensions     frame::sender_for_interpreter_frame())
608 //                                            if the calle should be compiled.
609 //                                          - Note that unextended SP < SP
610 //                                            is possible on ppc.
611 //
612 // |                      |                 |                      |                      |                      |
613 // | (frame::java_abi)    |                 | (frame::java_abi)    |                      | (frame::java_abi)    |
614 // | 4 words              |                 | 4 words              |                      | 4 words              |
615 // | Caller's SP          |<- FP of caller  | Caller's SP          |<- FP of caller       | Caller's SP          |<- FP of caller
616 // ========================   (aligned)     ========================                      ========================
617 // | frame::              |                 | frame::              |                      | frame::              |
618 // | ijava_state          |                 | ijava_state          |                      | ijava_state          |
619 // |                      |                 |                      |                      |                      |
620 // |----------------------|                 |----------------------|                      |----------------------|
621 // | P0                   |                 | L0 aka P0            |                      | L0 aka P0            |
622 // |                      |                 | :                    |                      | :                    |
623 // | Pn                   |                 | :      Pn            |                      | :      Pn            |
624 // |----------------------|                 | :                    |                      | :                    |
625 // |                      |                 | Lm                   |                      | Lm                   |
626 // | Reserved Expr. Stack |                 |----------------------|                      |----------------------|
627 // |                      |                 | Opt. Alignm. Padding |                      | Opt. Alignm. Padding |
628 // |                      |<- ConstMethod   |----------------------|                      |----------------------|
629 // |----------------------|   ::_max_stack  |                      |                      |                      |
630 // | Opt. Alignm. Padding |                 | (frame::java_abi)    |                      | (frame::java_abi)    |
631 // |----------------------|                 | 4 words              |                      | 4 words              |
632 // | Large ABI            |                 | Caller's SP          |<- new SP of caller   | Caller's SP          |<- SP of caller /
633 // | for C++ calls        |                 ========================   (aligned)          ========================   FP of callee
634 // | (frame::             |                                                               | frame::              |   (aligned)
635 // |  native_abi_reg_args)|                                                               | ijava_state          |
636 // |                      |                                                               |                      |
637 // |                      |                                                               |----------------------|
638 // |                      |                                                               |                      |
639 // | Caller's SP          |<- SP of caller                          <- unextended SP      | Reserved Expr. Stack |<- unextended SP
640 // ========================   (aligned)                                of caller          |                      |   of caller
641 //                                                                     (aligned)          |                      |
642 //                                                                                        |                      |
643 //                                                                                        |                      |
644 //                                                                                        |                      |
645 //                                                                                        |                      |<- ConstMethod
646 //                                                                                        |----------------------|   ::_max_stack
647 //                         Resize Caller                    Push new Callee Frame         | Opt. Alignm. Padding |
648 //                     -------------------->              ------------------------>       |----------------------|
649 //                     (ABI, expressions, locals)                                         | Large ABI            |
650 //                                                                                        | for C++ calls        |
651 //                                                                                        | (frame::             |
652 //                                                                                        |  native_abi_reg_args)|
653 //                                                |  Growth  |                            |                      |
654 //                                                v          v                            |                      |
655 //                                                                                        |                      |
656 //                                                                                        | Caller's SP          |<- SP of callee
657 //                                                                                        ========================   (aligned)
658 //
659 //
660 #endif // CPU_PPC_CONTINUATION_PPC_INLINE_HPP