1 /*
  2  * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2023 SAP SE. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef CPU_PPC_FRAME_PPC_INLINE_HPP
 27 #define CPU_PPC_FRAME_PPC_INLINE_HPP
 28 
 29 #include "code/codeBlob.inline.hpp"
 30 #include "code/codeCache.inline.hpp"
 31 #include "code/vmreg.inline.hpp"
 32 #include "runtime/sharedRuntime.hpp"
 33 #include "utilities/align.hpp"
 34 
 35 // Inline functions for ppc64 frames:
 36 
 37 // Initialize frame members (_sp must be given)
 38 inline void frame::setup(kind knd) {
 39   if (_pc == nullptr) {
 40     _pc = (address)own_abi()->lr;
 41     assert(_pc != nullptr, "must have PC");
 42   }
 43 
 44   if (_cb == nullptr) {
 45     _cb = (knd == kind::nmethod) ? CodeCache::find_blob_fast(_pc) : CodeCache::find_blob(_pc);
 46   }
 47 
 48   if (_unextended_sp == nullptr) {
 49     _unextended_sp = _sp;
 50   }
 51 
 52   if (_fp == nullptr) {
 53     // The back link for compiled frames on the heap is not valid
 54     if (is_heap_frame()) {
 55       // fp for interpreted frames should have been derelativized and passed to the constructor
 56       assert(is_compiled_frame()
 57              || is_native_frame()   // native wrapper (nmethod) for j.l.Object::wait0
 58              || is_runtime_frame(), // e.g. Runtime1::monitorenter, SharedRuntime::complete_monitor_locking_C
 59              "sp:" PTR_FORMAT " fp:" PTR_FORMAT " name:%s", p2i(_sp), p2i(_unextended_sp + _cb->frame_size()), _cb->name());
 60       // The back link for compiled frames on the heap is invalid.
 61       _fp = _unextended_sp + _cb->frame_size();
 62     } else {
 63       _fp = (intptr_t*)own_abi()->callers_sp;
 64     }
 65   }
 66 
 67   address original_pc = get_deopt_original_pc();
 68   if (original_pc != nullptr) {
 69     _pc = original_pc;
 70     _deopt_state = is_deoptimized;
 71     assert(_cb == nullptr || _cb->as_nmethod()->insts_contains_inclusive(_pc),
 72            "original PC must be in the main code section of the compiled method (or must be immediately following it)");
 73   } else {
 74     if (_cb == SharedRuntime::deopt_blob()) {
 75       _deopt_state = is_deoptimized;
 76     } else {
 77       _deopt_state = not_deoptimized;
 78     }
 79   }
 80 
 81   // Continuation frames on the java heap are not aligned.
 82   // When thawing interpreted frames the sp can be unaligned (see new_stack_frame()).
 83   assert(_on_heap ||
 84          ((is_aligned(_sp, alignment_in_bytes) || is_interpreted_frame()) &&
 85           (is_aligned(_fp, alignment_in_bytes) || !is_fully_initialized())),
 86          "invalid alignment sp:" PTR_FORMAT " unextended_sp:" PTR_FORMAT " fp:" PTR_FORMAT, p2i(_sp), p2i(_unextended_sp), p2i(_fp));
 87 }
 88 
 89 // Constructors
 90 
 91 // Initialize all fields
 92 inline frame::frame() : _sp(nullptr), _pc(nullptr), _cb(nullptr), _oop_map(nullptr), _deopt_state(unknown),
 93                         _on_heap(false), DEBUG_ONLY(_frame_index(-1) COMMA) _unextended_sp(nullptr), _fp(nullptr) {}
 94 
 95 inline frame::frame(intptr_t* sp) : frame(sp, nullptr, kind::nmethod) {}
 96 
 97 inline frame::frame(intptr_t* sp, intptr_t* fp, address pc) : frame(sp, pc, nullptr, fp, nullptr) {}
 98 
 99 inline frame::frame(intptr_t* sp, address pc, kind knd)
100   : _sp(sp), _pc(pc), _cb(nullptr), _oop_map(nullptr),
101     _on_heap(false), DEBUG_ONLY(_frame_index(-1) COMMA) _unextended_sp(sp), _fp(nullptr) {
102   setup(knd);
103 }
104 
105 inline frame::frame(intptr_t* sp, address pc, intptr_t* unextended_sp, intptr_t* fp, CodeBlob* cb)
106   : _sp(sp), _pc(pc), _cb(cb), _oop_map(nullptr),
107     _on_heap(false), DEBUG_ONLY(_frame_index(-1) COMMA) _unextended_sp(unextended_sp), _fp(fp) {
108   setup(kind::nmethod);
109 }
110 
111 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map)
112   : _sp(sp), _pc(pc), _cb(cb), _oop_map(oop_map),
113     _on_heap(false), DEBUG_ONLY(_frame_index(-1) COMMA) _unextended_sp(unextended_sp), _fp(fp) {
114   assert(_cb != nullptr, "pc: " INTPTR_FORMAT, p2i(pc));
115   setup(kind::nmethod);
116 }
117 
118 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb,
119                     const ImmutableOopMap* oop_map, bool on_heap)
120                     : _sp(sp), _pc(pc), _cb(cb), _oop_map(oop_map), _deopt_state(not_deoptimized),
121                       _on_heap(on_heap), DEBUG_ONLY(_frame_index(-1) COMMA) _unextended_sp(unextended_sp), _fp(fp) {
122   // In thaw, non-heap frames use this constructor to pass oop_map.  I don't know why.
123   assert(_on_heap || _cb != nullptr, "these frames are always heap frames");
124   if (cb != nullptr) {
125     setup(kind::nmethod);
126   }
127 #ifdef ASSERT
128   // The following assertion has been disabled because it would sometime trap for Continuation.run,
129   // which is not *in* a continuation and therefore does not clear the _cont_fastpath flag, but this
130   // is benign even in fast mode (see Freeze::setup_jump)
131   // We might freeze deoptimized frame in slow mode
132   // assert(_pc == pc && _deopt_state == not_deoptimized, "");
133 #endif
134 }
135 
136 // Accessors
137 
138 // Return unique id for this frame. The id must have a value where we
139 // can distinguish identity and younger/older relationship. null
140 // represents an invalid (incomparable) frame.
141 inline intptr_t* frame::id(void) const {
142   // Use _fp. _sp or _unextended_sp wouldn't be correct due to resizing.
143   return _fp;
144 }
145 
146 // Return true if this frame is older (less recent activation) than
147 // the frame represented by id.
148 inline bool frame::is_older(intptr_t* id) const {
149    assert(this->id() != nullptr && id != nullptr, "null frame id");
150    // Stack grows towards smaller addresses on ppc64.
151    return this->id() > id;
152 }
153 
154 inline int frame::frame_size() const {
155   // Stack grows towards smaller addresses on PPC64: sender is at a higher address.
156   return sender_sp() - sp();
157 }
158 
159 // Return the frame's stack pointer before it has been extended by a
160 // c2i adapter.
161 // i2c adapters also modify the frame they are applied on but shared code
162 // must never use an interpreted frames unextended sp directly as the value
163 // is platform dependent.
164 inline intptr_t* frame::unextended_sp() const          { assert_absolute(); return _unextended_sp; }
165 inline void frame::set_unextended_sp(intptr_t* value)  { _unextended_sp = value; }
166 inline int  frame::offset_unextended_sp() const        { assert_offset();   return _offset_unextended_sp; }
167 inline void frame::set_offset_unextended_sp(int value) { assert_on_heap();  _offset_unextended_sp = value; }
168 
169 // All frames have this field.
170 inline address frame::sender_pc() const {
171   return (address)callers_abi()->lr;
172 }
173 inline address* frame::sender_pc_addr() const {
174   return (address*)&(callers_abi()->lr);
175 }
176 
177 // All frames have this field.
178 inline intptr_t* frame::sender_sp() const {
179   return (intptr_t*)callers_abi();
180 }
181 
182 // All frames have this field.
183 inline intptr_t* frame::link() const {
184   return (intptr_t*)callers_abi()->callers_sp;
185 }
186 
187 inline intptr_t* frame::link_or_null() const {
188   return link();
189 }
190 
191 inline intptr_t* frame::real_fp() const {
192   return fp();
193 }
194 
195 // Template Interpreter frame value accessors.
196 
197 inline frame::ijava_state* frame::get_ijava_state() const {
198   return (ijava_state*) ((uintptr_t)fp() - ijava_state_size);
199 }
200 
201 inline intptr_t* frame::interpreter_frame_locals() const {
202   intptr_t n = *addr_at(ijava_idx(locals));
203   return &fp()[n]; // return relativized locals
204 }
205 
206 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
207   return (intptr_t*) &(get_ijava_state()->bcp);
208 }
209 
210 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
211   return (intptr_t*) &(get_ijava_state()->mdx);
212 }
213 
214 inline BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
215   return (BasicObjectLock*) get_ijava_state();
216 }
217 
218 // Return register stack slot addr at which currently interpreted method is found.
219 inline Method** frame::interpreter_frame_method_addr() const {
220   return (Method**) &(get_ijava_state()->method);
221 }
222 
223 inline oop* frame::interpreter_frame_mirror_addr() const {
224   return (oop*) &(get_ijava_state()->mirror);
225 }
226 
227 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
228   return (ConstantPoolCache**) &(get_ijava_state()->cpoolCache);
229 }
230 
231 inline oop* frame::interpreter_frame_temp_oop_addr() const {
232   return (oop*) &(get_ijava_state()->oop_tmp);
233 }
234 
235 inline intptr_t* frame::interpreter_frame_esp() const {
236   return (intptr_t*) at_relative(ijava_idx(esp));
237 }
238 
239 // Convenient setters
240 inline void frame::interpreter_frame_set_monitor_end(BasicObjectLock* end) {
241   assert(is_interpreted_frame(), "interpreted frame expected");
242   // set relativized monitors
243   get_ijava_state()->monitors = (intptr_t) ((intptr_t*)end - fp());
244 }
245 
246 inline void frame::interpreter_frame_set_cpcache(ConstantPoolCache* cp)       { *interpreter_frame_cache_addr() = cp; }
247 
248 inline void frame::interpreter_frame_set_esp(intptr_t* esp) {
249   assert(is_interpreted_frame(), "interpreted frame expected");
250   // set relativized esp
251   get_ijava_state()->esp = (intptr_t) (esp - fp());
252 }
253 
254 inline void frame::interpreter_frame_set_top_frame_sp(intptr_t* top_frame_sp) {
255   assert(is_interpreted_frame(), "interpreted frame expected");
256   // set relativized top_frame_sp
257   get_ijava_state()->top_frame_sp = (intptr_t) (top_frame_sp - fp());
258 }
259 
260 inline void frame::interpreter_frame_set_sender_sp(intptr_t* sender_sp)       { get_ijava_state()->sender_sp = (intptr_t) sender_sp; }
261 
262 inline intptr_t* frame::interpreter_frame_expression_stack() const {
263   intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end();
264   return monitor_end-1;
265 }
266 
267 // top of expression stack
268 inline intptr_t* frame::interpreter_frame_tos_address() const {
269   return interpreter_frame_esp() + Interpreter::stackElementWords;
270 }
271 
272 inline int frame::interpreter_frame_monitor_size() {
273   return BasicObjectLock::size();
274 }
275 
276 // entry frames
277 
278 inline intptr_t* frame::entry_frame_argument_at(int offset) const {
279   // Since an entry frame always calls the interpreter first, the
280   // parameters are on the stack and relative to known register in the
281   // entry frame.
282   intptr_t* tos = (intptr_t*)get_entry_frame_locals()->arguments_tos_address;
283   return &tos[offset + 1]; // prepushed tos
284 }
285 
286 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
287   return (JavaCallWrapper**)&get_entry_frame_locals()->call_wrapper_address;
288 }
289 
290 inline bool frame::is_interpreted_frame() const  {
291   return Interpreter::contains(pc());
292 }
293 
294 inline frame frame::sender_raw(RegisterMap* map) const {
295   // Default is we do have to follow them. The sender_for_xxx will
296   // update it accordingly.
297   map->set_include_argument_oops(false);
298 
299   if (map->in_cont()) { // already in an h-stack
300     return map->stack_chunk()->sender(*this, map);
301   }
302 
303   if (is_entry_frame())       return sender_for_entry_frame(map);
304   if (is_upcall_stub_frame()) return sender_for_upcall_stub_frame(map);
305   if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
306 
307   assert(_cb == CodeCache::find_blob(pc()), "Must be the same");
308   if (_cb != nullptr) return sender_for_compiled_frame(map);
309 
310   // Must be native-compiled frame, i.e. the marshaling code for native
311   // methods that exists in the core system.
312   return frame(sender_sp(), sender_pc(), kind::code_blob);
313 }
314 
315 inline frame frame::sender(RegisterMap* map) const {
316   frame result = sender_raw(map);
317 
318   if (map->process_frames() && !map->in_cont()) {
319     StackWatermarkSet::on_iteration(map->thread(), result);
320   }
321 
322   return result;
323 }
324 
325 inline frame frame::sender_for_compiled_frame(RegisterMap *map) const {
326   assert(map != nullptr, "map must be set");
327 
328   intptr_t* sender_sp = this->sender_sp();
329   address   sender_pc = this->sender_pc();
330 
331   if (map->update_map()) {
332     // Tell GC to use argument oopmaps for some runtime stubs that need it.
333     // For C1, the runtime stub might not have oop maps, so set this flag
334     // outside of update_register_map.
335     if (!_cb->is_nmethod()) { // compiled frames do not use callee-saved registers
336       map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
337       if (oop_map() != nullptr) {
338         _oop_map->update_register_map(this, map);
339       }
340     } else {
341       assert(!_cb->caller_must_gc_arguments(map->thread()), "");
342       assert(!map->include_argument_oops(), "");
343       assert(oop_map() == nullptr || !oop_map()->has_any(OopMapValue::callee_saved_value), "callee-saved value in compiled frame");
344     }
345   }
346 
347   assert(sender_sp != sp(), "must have changed");
348 
349   if (Continuation::is_return_barrier_entry(sender_pc)) {
350     if (map->walk_cont()) { // about to walk into an h-stack
351       return Continuation::top_frame(*this, map);
352     } else {
353       return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
354     }
355   }
356 
357   return frame(sender_sp, sender_pc);
358 }
359 
360 inline oop frame::saved_oop_result(RegisterMap* map) const {
361   oop* result_adr = (oop *)map->location(R3->as_VMReg(), sp());
362   guarantee(result_adr != nullptr, "bad register save location");
363   return *result_adr;
364 }
365 
366 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
367   oop* result_adr = (oop *)map->location(R3->as_VMReg(), sp());
368   guarantee(result_adr != nullptr, "bad register save location");
369 
370   *result_adr = obj;
371 }
372 
373 inline int frame::compiled_frame_stack_argsize() const {
374   assert(cb()->is_nmethod(), "");
375   return (cb()->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord;
376 }
377 
378 inline void frame::interpreted_frame_oop_map(InterpreterOopMap* mask) const {
379   assert(mask != nullptr, "");
380   Method* m = interpreter_frame_method();
381   int   bci = interpreter_frame_bci();
382   m->mask_for(bci, mask); // OopMapCache::compute_one_oop_map(m, bci, mask);
383 }
384 
385 inline int frame::sender_sp_ret_address_offset() {
386   return -(int)(_abi0(lr) >> LogBytesPerWord); // offset in words
387 }
388 
389 template <typename RegisterMapT>
390 void frame::update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr) {
391   // Nothing to do.
392 }
393 
394 #endif // CPU_PPC_FRAME_PPC_INLINE_HPP