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_AARCH64_STACKCHUNKFRAMESTREAM_AARCH64_INLINE_HPP
 26 #define CPU_AARCH64_STACKCHUNKFRAMESTREAM_AARCH64_INLINE_HPP
 27 
 28 #include "interpreter/oopMapCache.hpp"
 29 #include "pauth_aarch64.hpp"
 30 #include "runtime/frame.inline.hpp"
 31 #include "runtime/registerMap.hpp"
 32 
 33 #ifdef ASSERT
 34 template <ChunkFrames frame_kind>
 35 inline bool StackChunkFrameStream<frame_kind>::is_in_frame(void* p0) const {
 36   assert(!is_done(), "");
 37   intptr_t* p = (intptr_t*)p0;
 38   int frame_size = _cb->frame_size();
 39   if (is_compiled()) {
 40     nmethod* nm = _cb->as_nmethod_or_null();
 41     if (nm->needs_stack_repair() && nm->is_compiled_by_c2()) {
 42       frame f = to_frame();
 43       bool augmented = f.was_augmented_on_entry(frame_size);
 44       if (!augmented) {
 45         // Fix: C2 caller, so frame was not extended and thus the
 46         // size read from the frame does not include the arguments.
 47         // Ideally we have to count the arg size for the scalarized
 48         // convention. For now we include the size of the caller frame
 49         // which would at least be equal to that.
 50         RegisterMap map(nullptr,
 51                         RegisterMap::UpdateMap::skip,
 52                         RegisterMap::ProcessFrames::skip,
 53                         RegisterMap::WalkContinuation::skip);
 54         frame caller = to_frame().sender(&map);
 55         assert(caller.is_compiled_frame() && caller.cb()->as_nmethod()->is_compiled_by_c2(), "needs stack repair but was not extended with c1/interpreter caller");
 56         frame_size += (caller.real_fp() - caller.sp());
 57       }
 58     } else {
 59       frame_size += _cb->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size >> LogBytesPerWord;
 60     }
 61   }
 62   return p == sp() - frame::sender_sp_offset || ((p - unextended_sp()) >= 0 && (p - unextended_sp()) < frame_size);
 63 }
 64 #endif
 65 
 66 template <ChunkFrames frame_kind>
 67 inline frame StackChunkFrameStream<frame_kind>::to_frame() const {
 68   if (is_done()) {
 69     return frame(_sp, _sp, nullptr, nullptr, nullptr, nullptr, true);
 70   } else {
 71     frame f = frame(sp(), unextended_sp(), fp(), pc(), cb(), _oopmap, true);
 72     // If caller tries to get the sender of this frame and PreserveFramePointer
 73     // is set, fp() will be used which contains the old value at the time of
 74     // freeze (fp is reconstructed again during thaw). Setting sp as trusted
 75     // causes the sender code to use _unextended_sp instead (see sender_for_compiled_frame()).
 76     f.set_sp_is_trusted();
 77     return f;
 78   }
 79 }
 80 
 81 template <ChunkFrames frame_kind>
 82 inline address StackChunkFrameStream<frame_kind>::get_pc() const {
 83   assert(!is_done(), "");
 84   // Just strip it for frames on the heap.
 85   return pauth_strip_pointer(*(address*)((_callee_augmented ? _unextended_sp : _sp) - 1));
 86 }
 87 
 88 template <ChunkFrames frame_kind>
 89 inline intptr_t* StackChunkFrameStream<frame_kind>::fp() const {
 90   intptr_t* fp_addr = _sp - frame::sender_sp_offset;
 91   return (frame_kind == ChunkFrames::Mixed && is_interpreted())
 92     ? fp_addr + *fp_addr // derelativize
 93     : *(intptr_t**)fp_addr;
 94 }
 95 
 96 template <ChunkFrames frame_kind>
 97 inline intptr_t* StackChunkFrameStream<frame_kind>::derelativize(int offset) const {
 98   intptr_t* fp = this->fp();
 99   assert(fp != nullptr, "");
100   return fp + fp[offset];
101 }
102 
103 template <ChunkFrames frame_kind>
104 inline intptr_t* StackChunkFrameStream<frame_kind>::unextended_sp_for_interpreter_frame() const {
105   assert_is_interpreted_and_frame_type_mixed();
106   return derelativize(frame::interpreter_frame_last_sp_offset);
107 }
108 
109 template <ChunkFrames frame_kind>
110 inline void StackChunkFrameStream<frame_kind>::next_for_interpreter_frame() {
111   assert_is_interpreted_and_frame_type_mixed();
112   if (derelativize(frame::interpreter_frame_locals_offset) + 1 >= _end) {
113     _unextended_sp = _end;
114     _sp = _end;
115   } else {
116     intptr_t* fp = this->fp();
117     _unextended_sp = fp + fp[frame::interpreter_frame_sender_sp_offset];
118     _sp = fp + frame::sender_sp_offset;
119   }
120 }
121 
122 template <ChunkFrames frame_kind>
123 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_size() const {
124   assert_is_interpreted_and_frame_type_mixed();
125 
126   intptr_t* top = unextended_sp(); // later subtract argsize if callee is interpreted
127   intptr_t* bottom = derelativize(frame::interpreter_frame_locals_offset) + 1; // the sender's unextended sp: derelativize(frame::interpreter_frame_sender_sp_offset);
128   return (int)(bottom - top);
129 }
130 
131 template <ChunkFrames frame_kind>
132 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_stack_argsize() const {
133   assert_is_interpreted_and_frame_type_mixed();
134   int diff = (int)(derelativize(frame::interpreter_frame_locals_offset) - derelativize(frame::interpreter_frame_sender_sp_offset) + 1);
135   return diff;
136 }
137 
138 template <ChunkFrames frame_kind>
139 inline int StackChunkFrameStream<frame_kind>::interpreter_frame_num_oops() const {
140   assert_is_interpreted_and_frame_type_mixed();
141   ResourceMark rm;
142   InterpreterOopMap mask;
143   frame f = to_frame();
144   f.interpreted_frame_oop_map(&mask);
145   return  mask.num_oops()
146         + 1 // for the mirror oop
147         + (f.interpreter_frame_method()->is_native() ? 1 : 0) // temp oop slot
148         + pointer_delta_as_int((intptr_t*)f.interpreter_frame_monitor_begin(),
149               (intptr_t*)f.interpreter_frame_monitor_end())/BasicObjectLock::size();
150 }
151 
152 template<>
153 template<>
154 inline void StackChunkFrameStream<ChunkFrames::Mixed>::update_reg_map_pd(RegisterMap* map) {
155   if (map->update_map()) {
156     frame::update_map_with_saved_link(map, map->in_cont() ? (intptr_t**)(intptr_t)frame::sender_sp_offset
157                                                           : (intptr_t**)(_sp - frame::sender_sp_offset));
158   }
159 }
160 
161 template<>
162 template<>
163 inline void StackChunkFrameStream<ChunkFrames::CompiledOnly>::update_reg_map_pd(RegisterMap* map) {
164   if (map->update_map()) {
165     frame::update_map_with_saved_link(map, map->in_cont() ? (intptr_t**)(intptr_t)frame::sender_sp_offset
166                                                           : (intptr_t**)(_sp - frame::sender_sp_offset));
167   }
168 }
169 
170 template <ChunkFrames frame_kind>
171 template <typename RegisterMapT>
172 inline void StackChunkFrameStream<frame_kind>::update_reg_map_pd(RegisterMapT* map) {}
173 
174 #endif // CPU_AARCH64_STACKCHUNKFRAMESTREAM_AARCH64_INLINE_HPP