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