1 /*
  2  * Copyright (c) 2022, 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_CONTINUATIONHELPER_PPC_INLINE_HPP
 26 #define CPU_PPC_CONTINUATIONHELPER_PPC_INLINE_HPP
 27 
 28 #include "runtime/continuationHelper.hpp"
 29 
 30 static inline void patch_return_pc_with_preempt_stub(frame& f) {
 31   if (f.is_runtime_frame()) {
 32     // Patch the pc of the now old last Java frame (we already set the anchor to enterSpecial)
 33     // so that when target goes back to Java it will actually return to the preempt cleanup stub.
 34     frame::common_abi* abi = (frame::common_abi*)f.sp();
 35     abi->lr = (uint64_t)StubRoutines::cont_preempt_stub();
 36   } else {
 37     // The target will check for preemption once it returns to the interpreter
 38     // or the native wrapper code and will manually jump to the preempt stub.
 39     JavaThread *thread = JavaThread::current();
 40     thread->set_preempt_alternate_return(StubRoutines::cont_preempt_stub());
 41   }
 42 }
 43 
 44 inline int ContinuationHelper::frame_align_words(int size) {
 45   return size & 1;
 46 }
 47 
 48 inline intptr_t* ContinuationHelper::frame_align_pointer(intptr_t* p) {
 49   return align_down(p, frame::frame_alignment);
 50 }
 51 
 52 template<typename FKind>
 53 inline void ContinuationHelper::update_register_map(const frame& f, RegisterMap* map) {
 54   // Currently all registers are considered to be volatile and saved in the caller (java) frame if needed
 55 }
 56 
 57 inline void ContinuationHelper::update_register_map_with_callee(const frame& f, RegisterMap* map) {
 58   // Currently all registers are considered to be volatile and saved in the caller (java) frame if needed
 59 }
 60 
 61 inline void ContinuationHelper::push_pd(const frame& f) {
 62   f.own_abi()->callers_sp = (uint64_t)f.fp();
 63 }
 64 
 65 
 66 inline void ContinuationHelper::set_anchor_to_entry_pd(JavaFrameAnchor* anchor, ContinuationEntry* cont) {
 67   // nothing to do
 68 }
 69 
 70 inline void ContinuationHelper::set_anchor_pd(JavaFrameAnchor* anchor, intptr_t* sp) {
 71   // nothing to do
 72 }
 73 
 74 #ifdef ASSERT
 75 inline bool ContinuationHelper::Frame::assert_frame_laid_out(frame f) {
 76   intptr_t* sp = f.sp();
 77   address pc = *(address*)(sp - frame::sender_sp_ret_address_offset());
 78   intptr_t* fp = (intptr_t*)f.own_abi()->callers_sp;
 79   assert(f.raw_pc() == pc, "f.ra_pc: " INTPTR_FORMAT " actual: " INTPTR_FORMAT, p2i(f.raw_pc()), p2i(pc));
 80   assert(f.fp() == fp, "f.fp: " INTPTR_FORMAT " actual: " INTPTR_FORMAT, p2i(f.fp()), p2i(fp));
 81   return f.raw_pc() == pc && f.fp() == fp;
 82 }
 83 #endif
 84 
 85 inline intptr_t** ContinuationHelper::Frame::callee_link_address(const frame& f) {
 86   return (intptr_t**)&f.own_abi()->callers_sp;
 87 }
 88 
 89 inline address* ContinuationHelper::InterpretedFrame::return_pc_address(const frame& f) {
 90   return (address*)&f.callers_abi()->lr;
 91 }
 92 
 93 inline void ContinuationHelper::InterpretedFrame::patch_sender_sp(frame& f, const frame& caller) {
 94   intptr_t* sp = caller.unextended_sp();
 95   if (!f.is_heap_frame() && caller.is_interpreted_frame()) {
 96     // See diagram "Interpreter Calling Procedure on PPC" at the end of continuationFreezeThaw_ppc.inline.hpp
 97     sp = (intptr_t*)caller.at_relative(ijava_idx(top_frame_sp));
 98   }
 99   assert(f.is_interpreted_frame(), "");
100   assert(f.is_heap_frame() || is_aligned(sp, frame::alignment_in_bytes), "");
101   intptr_t* la = f.addr_at(ijava_idx(sender_sp));
102   *la = f.is_heap_frame() ? (intptr_t)(sp - f.fp()) : (intptr_t)sp;
103 }
104 
105 inline address* ContinuationHelper::Frame::return_pc_address(const frame& f) {
106   return (address*)&f.callers_abi()->lr;
107 }
108 
109 inline address ContinuationHelper::Frame::real_pc(const frame& f) {
110   return (address)f.own_abi()->lr;
111 }
112 
113 inline void ContinuationHelper::Frame::patch_pc(const frame& f, address pc) {
114   f.own_abi()->lr = (uint64_t)pc;
115 }
116 
117 //                     | Minimal ABI          |
118 //                     | (frame::java_abi)    |
119 //                     | 4 words              |
120 //                     | Caller's SP          |<- FP of f's caller
121 //                     |======================|
122 //                     |                      |                                 Frame of f's caller
123 //                     |                      |
124 // frame_bottom of f ->|                      |
125 //                     |----------------------|
126 //                     | L0 aka P0            |
127 //                     | :                    |
128 //                     | :      Pn            |
129 //                     | :                    |
130 //                     | Lm                   |
131 //                     |----------------------|
132 //                     | SP alignment (opt.)  |
133 //                     |----------------------|
134 //                     | Minimal ABI          |
135 //                     | (frame::java_abi)    |
136 //                     | 4 words              |
137 //                     | Caller's SP          |<- SP of f's caller / FP of f
138 //                     |======================|
139 //                     |ijava_state (metadata)|                                 Frame of f
140 //                     |                      |
141 //                     |                      |
142 //                     |----------------------|
143 //                     | Expression stack     |
144 //                     |                      |
145 //    frame_top of f ->|                      |
146 //   if callee interp. |......................|
147 //                     | L0 aka P0            |<- ijava_state.esp + callee_argsize
148 //                     | :                    |
149 //    frame_top of f ->| :      Pn            |
150 //  + metadata_words   | :                    |<- ijava_state.esp (1 slot below Pn)
151 //    if callee comp.  | Lm                   |
152 //                     |----------------------|
153 //                     | SP alignment (opt.)  |
154 //                     |----------------------|
155 //                     | Minimal ABI          |
156 //                     | (frame::java_abi)    |
157 //                     | 4 words              |
158 //                     | Caller's SP          |<- SP of f / FP of f's callee
159 //                     |======================|
160 //                     |ijava_state (metadata)|                                 Frame of f's callee
161 //                     |                      |
162 //
163 //                           |  Growth  |
164 //                           v          v
165 //
166 // See also diagram at the end of continuation_ppc.inline.hpp
167 //
168 inline intptr_t* ContinuationHelper::InterpretedFrame::frame_top(const frame& f, InterpreterOopMap* mask) { // inclusive; this will be copied with the frame
169   int expression_stack_sz = expression_stack_size(f, mask);
170   intptr_t* res = (intptr_t*)f.interpreter_frame_monitor_end() - expression_stack_sz;
171   assert(res <= (intptr_t*)f.get_ijava_state() - expression_stack_sz,
172          "res=" PTR_FORMAT " f.get_ijava_state()=" PTR_FORMAT " expression_stack_sz=%d",
173          p2i(res), p2i(f.get_ijava_state()), expression_stack_sz);
174   assert(res >= f.unextended_sp(),
175          "res: " INTPTR_FORMAT " ijava_state: " INTPTR_FORMAT " esp: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT " expression_stack_size: %d",
176          p2i(res), p2i(f.get_ijava_state()), f.get_ijava_state()->esp, p2i(f.unextended_sp()), expression_stack_sz);
177   return res;
178 }
179 
180 inline intptr_t* ContinuationHelper::InterpretedFrame::frame_bottom(const frame& f) {
181   return (intptr_t*)f.at_relative(ijava_idx(locals)) + 1; // exclusive (will not be copied), so we add 1 word
182 }
183 
184 inline intptr_t* ContinuationHelper::InterpretedFrame::frame_top(const frame& f, int callee_argsize_incl_metadata, bool callee_interpreted) {
185   intptr_t* pseudo_unextended_sp = f.interpreter_frame_esp() + 1 - frame::metadata_words_at_top;
186   return pseudo_unextended_sp + (callee_interpreted ? callee_argsize_incl_metadata : 0);
187 }
188 
189 inline intptr_t* ContinuationHelper::InterpretedFrame::callers_sp(const frame& f) {
190   return f.fp();
191 }
192 
193 #endif // CPU_PPC_CONTINUATIONFRAMEHELPERS_PPC_INLINE_HPP