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_STACKCHUNKFRAMESTREAM_PPC_INLINE_HPP
 26 #define CPU_PPC_STACKCHUNKFRAMESTREAM_PPC_INLINE_HPP
 27 
 28 #include "interpreter/oopMapCache.hpp"
 29 #include "runtime/frame.inline.hpp"
 30 #include "runtime/registerMap.hpp"
 31 
 32 #ifdef ASSERT
 33 template <ChunkFrames frame_kind>
 34 inline bool StackChunkFrameStream<frame_kind>::is_in_frame(void* p0) const {
 35   assert(!is_done(), "");
 36   assert(is_compiled(), "");
 37   intptr_t* p = (intptr_t*)p0;
 38   int argsize = (_cb->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord;
 39   int frame_size = _cb->frame_size() + (argsize > 0 ? argsize + frame::metadata_words_at_top : 0);
 40   return (p - unextended_sp()) >= 0 && (p - unextended_sp()) < frame_size;
 41 }
 42 #endif
 43 
 44 template <ChunkFrames frame_kind>
 45 inline frame StackChunkFrameStream<frame_kind>::to_frame() const {
 46   if (is_done()) {
 47     return frame(_sp, _sp, nullptr, nullptr, nullptr, nullptr, true);
 48   } else {
 49     // Compiled frames on heap don't have back links. See FreezeBase::patch_pd() and frame::setup().
 50     return frame(sp(), unextended_sp(), Interpreter::contains(pc()) ? fp() : nullptr, pc(), cb(), _oopmap, true);
 51   }
 52 }
 53 
 54 template <ChunkFrames frame_kind>
 55 inline address StackChunkFrameStream<frame_kind>::get_pc() const {
 56   assert(!is_done(), "");
 57   return (address)((frame::common_abi*) _sp)->lr;
 58 }
 59 
 60 template <ChunkFrames frame_kind>
 61 inline intptr_t* StackChunkFrameStream<frame_kind>::fp() const {
 62   // See FreezeBase::patch_pd() and frame::setup()
 63   assert((frame_kind == ChunkFrames::Mixed && is_interpreted()), "");
 64   intptr_t* fp_addr = (intptr_t*)&((frame::common_abi*)_sp)->callers_sp;
 65   assert(*(intptr_t**)fp_addr != nullptr, "");
 66   // derelativize
 67   return fp_addr + *fp_addr;
 68 }
 69 
 70 template <ChunkFrames frame_kind>
 71 inline intptr_t* StackChunkFrameStream<frame_kind>::derelativize(int offset) const {
 72   intptr_t* fp = this->fp();
 73   assert(fp != nullptr, "");
 74   return fp + fp[offset];
 75 }
 76 
 77 template <ChunkFrames frame_kind>
 78 inline intptr_t* StackChunkFrameStream<frame_kind>::unextended_sp_for_interpreter_frame() const {
 79   assert_is_interpreted_and_frame_type_mixed();
 80   return derelativize(ijava_idx(esp)) + 1 - frame::metadata_words; // On PPC esp points to the next free slot
 81 }
 82 
 83 template <ChunkFrames frame_kind>
 84 inline void StackChunkFrameStream<frame_kind>::next_for_interpreter_frame() {
 85   assert_is_interpreted_and_frame_type_mixed();
 86   if (derelativize(ijava_idx(locals)) + 1 >= _end) {
 87     _unextended_sp = _end;
 88     _sp = _end;
 89   } else {
 90     _unextended_sp = derelativize(ijava_idx(sender_sp));
 91     _sp = this->fp();
 92   }
 93 }
 94 
 95 // Details for the comment on StackChunkFrameStream<frame_kind>::frame_size()
 96 //
 97 // Interpreted caller frames get extended even if the callee is also
 98 // interpreted. This is done to accomodate non-parameter locals.
 99 //
100 // The size of a single frame is from the unextended sp to the bottom of the
101 // locals array. The combined size of caller/callee is the single size with the
102 // overlap deducted. The overlap is the size of the call parameters plus the
103 // size of the metadata at the sp (frame::metadata_words_at_top).
104 //
105 //
106 // Case 1: no metadata between a frame                      Case 2: metadata is located between
107 //         and its locals                                           a frame and its locals as on ppc64
108 //
109 //       |  | L0 aka P0            |                    |  | L0 aka P0            |
110 //       |  | :      :             |                    |  | :      :             |
111 //       |  | :      Pn            |                    |  | :      Pn            |
112 //       |  | :                    |                    |  | :                    |
113 //       |  | Lm                   |                    |  | Lm                   |
114 //       |  ========================                    |  |----------------------|
115 //    S0 |  | Frame F0             |                    |  | Metadata@top         |
116 //       |  |                      |                 S0 |  |                      |
117 //       |  |                      |                    |  |                      |
118 //       |  |----------------------|                    |  |                      |
119 //       || | L0 aka P0            |                    |  ========================
120 // over- || | :      :             |                    |  | Frame F0             |
121 // lap   || | :      Pn            |<- unext. SP        |  |                      |
122 //        | | :                    |                    |  |                      |<- bottom_of_locals
123 //        | | Lm                   |<- SP               |  |----------------------|
124 //        | ========================                    || | L0 aka P0            |
125 //        | | Frame F1             |                    || | :      :             |
126 //     S1 | |                      |              over- || | :      Pn            |<- unext. SP
127 //        | |                      |              lap   || | :                    |   + metadata_words_at_top
128 //        | |----------------------|                    || | Lm                   |
129 //        | | L0 aka P0            |                    || |----------------------|
130 //        | | :      :             |                    || | Metadata@top         |
131 //        | | :      Pn            |<- unext. SP        || |                      |<- unextended SP
132 //          | :                    |                     | |                      |
133 //          | Lm                   |<- SP                | |                      |<- SP
134 //          ========================                     | ========================
135 //                                                       | | Frame F1             |
136 //                                                       | |                      |
137 //                                                       | |                      |
138 //                                                       | |----------------------|
139 //    overlap = size of stackargs                     S1 | | L0 aka P0            |
140 //                                                       | | :      :             |
141 //                                                       | | :      Pn            |<- unext. SP
142 //                                                       | | :                    |   + metadata_words_at_top
143 //                                                       | | Lm                   |
144 //                                                       | |----------------------|
145 //                                                       | | Metadata@top         |
146 //                                                       | |                      |<- unextended SP
147 //                                                         |                      |
148 //                                                         |                      |<- SP
149 //                                                         ========================
150 //
151 //                                           sizeof(Metadata@top) = frame::metadata_words_at_top
152 //                                           bottom_of_locals = unext. sp + sizeof(Metadata@top) + stackargs
153 //                                           overlap = bottom_of_locals - unext. sp
154 //                                                   = stackargs + sizeof(Metadata@top)
155 template <ChunkFrames frame_kind>
156 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_size() const {
157   assert_is_interpreted_and_frame_type_mixed();
158   intptr_t* top = unextended_sp(); // later subtract argsize if callee is interpreted
159   intptr_t* bottom = derelativize(ijava_idx(locals)) + 1;
160   return (int)(bottom - top);
161 }
162 
163 // Size of stack args in words (P0..Pn above). Only valid if the caller is also
164 // interpreted. The function is also called if the caller is compiled but the
165 // result is not used in that case (same on x86).
166 // See also setting of sender_sp in ContinuationHelper::InterpretedFrame::patch_sender_sp()
167 template <ChunkFrames frame_kind>
168 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_stack_argsize() const {
169   assert_is_interpreted_and_frame_type_mixed();
170   frame::ijava_state* state = (frame::ijava_state*)((uintptr_t)fp() - frame::ijava_state_size);
171   int diff = (int)(state->locals - (state->sender_sp + frame::metadata_words_at_top) + 1);
172   assert(diff == -frame::metadata_words_at_top || ((Method*)state->method)->size_of_parameters() == diff,
173          "size_of_parameters(): %d diff: %d sp: " PTR_FORMAT " fp:" PTR_FORMAT,
174          ((Method*)state->method)->size_of_parameters(), diff, p2i(sp()), p2i(fp()));
175   return diff;
176 }
177 
178 template <ChunkFrames frame_kind>
179 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_num_oops() const {
180   assert_is_interpreted_and_frame_type_mixed();
181   ResourceMark rm;
182   InterpreterOopMap mask;
183   frame f = to_frame();
184   f.interpreted_frame_oop_map(&mask);
185   return  mask.num_oops()
186           + 1 // for the mirror oop
187           + ((intptr_t*)f.interpreter_frame_monitor_begin()
188              - (intptr_t*)f.interpreter_frame_monitor_end())/BasicObjectLock::size();
189 }
190 
191 template<>
192 template<>
193 inline void StackChunkFrameStream<ChunkFrames::Mixed>::update_reg_map_pd(RegisterMap* map) {
194   // Nothing to do (no non-volatile registers in java calling convention)
195 }
196 
197 template<>
198 template<>
199 inline void StackChunkFrameStream<ChunkFrames::CompiledOnly>::update_reg_map_pd(RegisterMap* map) {
200   // Nothing to do (no non-volatile registers in java calling convention)
201 }
202 
203 template <ChunkFrames frame_kind>
204 template <typename RegisterMapT>
205 inline void StackChunkFrameStream<frame_kind>::update_reg_map_pd(RegisterMapT* map) {}
206 
207 #endif // CPU_PPC_STACKCHUNKFRAMESTREAM_PPC_INLINE_HPP