1 /*
  2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. 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_AARCH64_FRAME_AARCH64_INLINE_HPP
 27 #define CPU_AARCH64_FRAME_AARCH64_INLINE_HPP
 28 
 29 #include "code/codeCache.inline.hpp"
 30 #include "code/vmreg.inline.hpp"
 31 #include "interpreter/interpreter.hpp"
 32 #include "interpreter/oopMapCache.hpp"
 33 #include "runtime/sharedRuntime.hpp"
 34 #include "pauth_aarch64.hpp"
 35 
 36 // Inline functions for AArch64 frames:
 37 
 38 // Constructors:
 39 
 40 inline frame::frame() {
 41   _pc = NULL;
 42   _sp = NULL;
 43   _unextended_sp = NULL;
 44   _fp = NULL;
 45   _cb = NULL;
 46   _deopt_state = unknown;
 47 }
 48 
 49 static int spin;
 50 
 51 inline void frame::init(intptr_t* sp, intptr_t* fp, address pc) {
 52   assert(pauth_ptr_is_raw(pc), "cannot be signed");
 53   intptr_t a = intptr_t(sp);
 54   intptr_t b = intptr_t(fp);
 55   _sp = sp;
 56   _unextended_sp = sp;
 57   _fp = fp;
 58   _pc = pc;
 59   assert(pc != NULL, "no pc?");
 60   _cb = CodeCache::find_blob(pc);
 61   
 62   setup(pc);
 63 
 64   _oop_map = NULL;
 65 }
 66 
 67 inline void frame::setup(address pc) {
 68   adjust_unextended_sp();
 69 
 70   address original_pc = CompiledMethod::get_deopt_original_pc(this);
 71   if (original_pc != NULL) {
 72     _pc = original_pc;
 73     _deopt_state = is_deoptimized;
 74     assert(_cb == NULL || _cb->as_compiled_method()->insts_contains_inclusive(_pc),
 75            "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
 76   } else {
 77     if (_cb == SharedRuntime::deopt_blob()) {
 78       _deopt_state = is_deoptimized;
 79     } else {
 80       _deopt_state = not_deoptimized;
 81     }
 82   }
 83 }
 84 
 85 inline frame::frame(intptr_t* sp, intptr_t* fp, address pc) {
 86   init(sp, fp, pc);
 87 }
 88 
 89 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb) {
 90   assert(pauth_ptr_is_raw(pc), "cannot be signed");
 91   intptr_t a = intptr_t(sp);
 92   intptr_t b = intptr_t(fp);
 93   _sp = sp;
 94   _unextended_sp = unextended_sp;
 95   _fp = fp;
 96   _pc = pc;
 97   assert(pc != NULL, "no pc?");
 98   _cb = cb;
 99   _oop_map = NULL;
100   assert(_cb != NULL, "pc: " INTPTR_FORMAT, p2i(pc));
101 
102   setup(pc);
103 }
104 
105 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map) {
106   _sp = sp;
107   _unextended_sp = unextended_sp;
108   _fp = fp;
109   _pc = pc;
110   assert(pc != NULL, "no pc?");
111   _cb = cb;
112   _oop_map = oop_map;
113   assert(_cb != NULL, "pc: " INTPTR_FORMAT, p2i(pc));
114 
115   setup(pc);
116 }
117 
118 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool dummy) {
119   _sp = sp;
120   _unextended_sp = unextended_sp;
121   _fp = fp;
122   _pc = pc;
123   _cb = cb;
124   _oop_map = oop_map;
125   _deopt_state = not_deoptimized;
126 #ifdef ASSERT
127   // The following assertion has been disabled because it would sometime trap for Continuation.run, which is not *in* a continuation
128   // and therefore does not clear the _cont_fastpath flag, but this is benign even in fast mode (see Freeze::setup_jump)
129   if (cb != NULL) {
130     setup(pc);
131     assert(_pc == pc && _deopt_state == not_deoptimized, "");
132   }
133 #endif
134 }
135 
136 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc) {
137   intptr_t a = intptr_t(sp);
138   intptr_t b = intptr_t(fp);
139   _sp = sp;
140   _unextended_sp = unextended_sp;
141   _fp = fp;
142   _pc = pc;
143   _cb = CodeCache::find_blob_fast(pc);
144   _oop_map = NULL;
145   assert(_cb != NULL, "pc: " INTPTR_FORMAT " sp: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT " fp: " INTPTR_FORMAT, p2i(pc), p2i(sp), p2i(unextended_sp), p2i(fp));
146   
147   setup(pc);
148 }
149 
150 inline frame::frame(intptr_t* sp) : frame(sp, sp, *(intptr_t**)(sp - frame::sender_sp_offset), *(address*)(sp - 1)) {}
151 
152 inline frame::frame(intptr_t* sp, intptr_t* fp) {
153   intptr_t a = intptr_t(sp);
154   intptr_t b = intptr_t(fp);
155   _sp = sp;
156   _unextended_sp = sp;
157   _fp = fp;
158   _pc = (address)(sp[-1]);
159 
160   // Here's a sticky one. This constructor can be called via AsyncGetCallTrace
161   // when last_Java_sp is non-null but the pc fetched is junk. If we are truly
162   // unlucky the junk value could be to a zombied method and we'll die on the
163   // find_blob call. This is also why we can have no asserts on the validity
164   // of the pc we find here. AsyncGetCallTrace -> pd_get_top_frame_for_signal_handler
165   // -> pd_last_frame should use a specialized version of pd_last_frame which could
166   // call a specilaized frame constructor instead of this one.
167   // Then we could use the assert below. However this assert is of somewhat dubious
168   // value.
169   // assert(_pc != NULL, "no pc?");
170 
171   _cb = CodeCache::find_blob(_pc);
172   adjust_unextended_sp();
173 
174   address original_pc = CompiledMethod::get_deopt_original_pc(this);
175   if (original_pc != NULL) {
176     _pc = original_pc;
177     _deopt_state = is_deoptimized;
178   } else {
179     _deopt_state = not_deoptimized;
180   }
181 }
182 
183 // Accessors
184 
185 inline bool frame::equal(frame other) const {
186   bool ret =  sp() == other.sp()
187               && unextended_sp() == other.unextended_sp()
188               && fp() == other.fp()
189               && pc() == other.pc();
190   assert(!ret || ret && cb() == other.cb() && _deopt_state == other._deopt_state, "inconsistent construction");
191   return ret;
192 }
193 
194 // Return unique id for this frame. The id must have a value where we can distinguish
195 // identity and younger/older relationship. NULL represents an invalid (incomparable)
196 // frame.
197 inline intptr_t* frame::id(void) const { return unextended_sp(); }
198 
199 // Return true if the frame is older (less recent activation) than the frame represented by id
200 inline bool frame::is_older(intptr_t* id) const   { assert(this->id() != NULL && id != NULL, "NULL frame id");
201                                                     return this->id() > id ; }
202 
203 
204 
205 inline intptr_t* frame::link() const              { return (intptr_t*) *(intptr_t **)addr_at(link_offset); }
206 
207 
208 inline intptr_t* frame::unextended_sp() const     { return _unextended_sp; }
209 
210 inline void frame::set_unextended_sp(intptr_t* value) { _unextended_sp = value; }
211 
212 inline int frame::offset_unextended_sp() const { return (int)(intptr_t)_unextended_sp; }
213 inline void frame::set_offset_unextended_sp(int value) { _unextended_sp = (intptr_t*)(intptr_t)value; }
214 
215 
216 inline intptr_t* frame::real_fp() const {
217   if (_cb != NULL) {
218     // use the frame size if valid
219     int size = _cb->frame_size();
220     if (size > 0) {
221       return unextended_sp() + size;
222     }
223   }
224   // else rely on fp()
225   assert(! is_compiled_frame(), "unknown compiled frame size");
226   return fp();
227 }
228 
229 inline int frame::frame_size() const {
230   return is_interpreted_frame()
231     ? sender_sp() - sp()
232     : cb()->frame_size();
233 }
234 
235 inline int frame::num_oops() const {
236   assert (!is_interpreted_frame(), "interpreted");
237   assert (oop_map() != NULL, "");
238   return oop_map()->num_oops() ;
239 }
240 
241 inline int frame::compiled_frame_stack_argsize() const {
242   assert (cb()->is_compiled(), "");
243   return (cb()->as_compiled_method()->method()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord;
244 }
245 
246 inline void frame::interpreted_frame_oop_map(InterpreterOopMap* mask) const {
247   assert (mask != NULL, "");
248   Method* m = interpreter_frame_method();
249   int   bci = interpreter_frame_bci();
250   m->mask_for(bci, mask); // OopMapCache::compute_one_oop_map(m, bci, mask);
251 }
252 
253 
254 inline int frame::interpreted_frame_num_oops(InterpreterOopMap* mask) const {
255   return   mask->num_oops()
256         + 1 // for the mirror oop
257         + ((intptr_t*)interpreter_frame_monitor_begin() - (intptr_t*)interpreter_frame_monitor_end())/BasicObjectLock::size();
258 }
259 
260 // helper to update a map with callee-saved RBP
261 
262 template <typename RegisterMapT>
263 void frame::update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr) {
264   // The interpreter and compiler(s) always save EBP/RBP in a known
265   // location on entry. We must record where that location is
266   // so this if EBP/RBP was live on callout from c2 we can find
267   // the saved copy no matter what it called.
268 
269   // Since the interpreter always saves EBP/RBP if we record where it is then
270   // we don't have to always save EBP/RBP on entry and exit to c2 compiled
271   // code, on entry will be enough.
272   map->set_location(rfp->as_VMReg(), (address) link_addr);
273   // this is weird "H" ought to be at a higher address however the
274   // oopMaps seems to have the "H" regs at the same address and the
275   // vanilla register.
276   // XXXX make this go away
277   if (true) {
278     map->set_location(rfp->as_VMReg()->next(), (address) link_addr);
279   }
280 }
281 
282 template <typename RegisterMapT>
283 intptr_t** frame::saved_link_address(const RegisterMapT* map) {
284   return (intptr_t**)map->location(rfp->as_VMReg());
285 }
286 
287 // Return address:
288 
289 inline address* frame::sender_pc_addr()         const { return (address*) addr_at( return_addr_offset); }
290 inline address  frame::sender_pc_maybe_signed() const { return *sender_pc_addr(); }
291 inline address  frame::sender_pc()              const { return pauth_strip_pointer(sender_pc_maybe_signed()); }
292 
293 inline intptr_t*    frame::sender_sp()        const { return            addr_at(   sender_sp_offset); }
294 
295 inline intptr_t** frame::interpreter_frame_locals_addr() const {
296   return (intptr_t**)addr_at(interpreter_frame_locals_offset);
297 }
298 
299 template <bool relative>
300 inline intptr_t* frame::interpreter_frame_last_sp() const {
301   return (intptr_t*)at<relative>(interpreter_frame_last_sp_offset);
302 }
303 
304 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
305   return (intptr_t*)addr_at(interpreter_frame_bcp_offset);
306 }
307 
308 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
309   return (intptr_t*)addr_at(interpreter_frame_mdp_offset);
310 }
311 
312 
313 // Constant pool cache
314 
315 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
316   return (ConstantPoolCache**)addr_at(interpreter_frame_cache_offset);
317 }
318 
319 // Method
320 
321 inline Method** frame::interpreter_frame_method_addr() const {
322   return (Method**)addr_at(interpreter_frame_method_offset);
323 }
324 
325 // Mirror
326 
327 inline oop* frame::interpreter_frame_mirror_addr() const {
328   return (oop*)addr_at(interpreter_frame_mirror_offset);
329 }
330 
331 // top of expression stack
332 template <bool relative>
333 inline intptr_t* frame::interpreter_frame_tos_address() const {
334   intptr_t* last_sp = interpreter_frame_last_sp<relative>();
335   if (last_sp == NULL) {
336     return sp();
337   } else {
338     // sp() may have been extended or shrunk by an adapter.  At least
339     // check that we don't fall behind the legal region.
340     // For top deoptimized frame last_sp == interpreter_frame_monitor_end.
341     assert(last_sp <= (intptr_t*) interpreter_frame_monitor_end<relative>(), "bad tos");
342     return last_sp;
343   }
344 }
345 
346 inline oop* frame::interpreter_frame_temp_oop_addr() const {
347   return (oop *)(fp() + interpreter_frame_oop_temp_offset);
348 }
349 
350 inline int frame::interpreter_frame_monitor_size() {
351   return BasicObjectLock::size();
352 }
353 
354 
355 // expression stack
356 // (the max_stack arguments are used by the GC; see class FrameClosure)
357 
358 template <bool relative>
359 inline intptr_t* frame::interpreter_frame_expression_stack() const {
360   intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end<relative>();
361   return monitor_end-1;
362 }
363 
364 
365 // Entry frames
366 
367 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
368  return (JavaCallWrapper**)addr_at(entry_frame_call_wrapper_offset);
369 }
370 
371 
372 // Compiled frames
373 
374 inline oop frame::saved_oop_result(RegisterMap* map) const {
375   PRAGMA_DIAG_PUSH
376   PRAGMA_NONNULL_IGNORED
377   oop* result_adr = (oop *)map->location(r0->as_VMReg(), sp());
378   PRAGMA_DIAG_POP
379   guarantee(result_adr != NULL, "bad register save location");
380   oop result = *result_adr;
381 
382   // TODO: Erik: remove after integration with concurrent stack scanning
383   result = NativeAccess<>::oop_load(&result);
384 
385   return result;
386 }
387 
388 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
389   PRAGMA_DIAG_PUSH
390   PRAGMA_NONNULL_IGNORED
391   oop* result_adr = (oop *)map->location(r0->as_VMReg(), sp());
392   PRAGMA_DIAG_POP
393   guarantee(result_adr != NULL, "bad register save location");
394 
395   *result_adr = obj;
396 }
397 
398 inline bool frame::is_interpreted_frame() const {
399   return Interpreter::contains(pc());
400 }
401 
402 inline int frame::sender_sp_ret_address_offset() {
403   return frame::sender_sp_offset - frame::return_addr_offset;
404 }
405 
406 inline const ImmutableOopMap* frame::get_oop_map() const {
407   if (_cb == NULL) return NULL;
408   if (_cb->oop_maps() != NULL) {
409     NativePostCallNop* nop = nativePostCallNop_at(_pc);
410     if (nop != NULL && nop->displacement() != 0) {
411       int slot = ((nop->displacement() >> 24) & 0xff);
412       return _cb->oop_map_for_slot(slot, _pc);
413     }
414     const ImmutableOopMap* oop_map = OopMapSet::find_map(this);
415     return oop_map;
416   }
417   return NULL;
418 }
419 
420 inline frame frame::sender_raw(RegisterMap* map) const {
421   // Default is we done have to follow them. The sender_for_xxx will
422   // update it accordingly
423   map->set_include_argument_oops(false);
424 
425   if (map->in_cont()) { // already in an h-stack
426     return map->stack_chunk()->sender(*this, map);
427   }
428 
429   if (is_entry_frame())           return sender_for_entry_frame(map);
430   if (is_optimized_entry_frame()) return sender_for_optimized_entry_frame(map);
431   if (is_interpreted_frame())     return sender_for_interpreter_frame(map);
432 
433   assert(_cb == CodeCache::find_blob(pc()), "Must be the same");
434 
435   if (_cb != NULL) {
436     return _cb->is_compiled() ? sender_for_compiled_frame<false>(map) : sender_for_compiled_frame<true>(map);
437   }
438   // Must be native-compiled frame, i.e. the marshaling code for native
439   // methods that exists in the core system.
440   return frame(sender_sp(), link(), sender_pc());
441 }
442 
443 template <bool stub>
444 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
445   // we cannot rely upon the last fp having been saved to the thread
446   // in C2 code but it will have been pushed onto the stack. so we
447   // have to find it relative to the unextended sp
448 
449   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
450   intptr_t* sender_sp = unextended_sp() + _cb->frame_size();
451   assert (sender_sp == real_fp(), "");
452 
453   // the return_address is always the word on the stack
454   address sender_pc = (address) *(sender_sp-1);
455 
456   intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
457 
458   if (map->update_map()) {
459     // Tell GC to use argument oopmaps for some runtime stubs that need it.
460     // For C1, the runtime stub might not have oop maps, so set this flag
461     // outside of update_register_map.
462     if (stub) { // compiled frames do not use callee-saved registers
463       map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
464       if (oop_map() != NULL) { 
465         _oop_map->update_register_map(this, map);
466       }
467     } else {
468       assert (!_cb->caller_must_gc_arguments(map->thread()), "");
469       assert (!map->include_argument_oops(), "");
470       assert (oop_map() == NULL || !oop_map()->has_any(OopMapValue::callee_saved_value), "callee-saved value in compiled frame");
471     }
472 
473     // Since the prolog does the save and restore of EBP there is no oopmap
474     // for it so we must fill in its location as if there was an oopmap entry
475     // since if our caller was compiled code there could be live jvm state in it.
476     update_map_with_saved_link(map, saved_fp_addr);
477   }
478 
479   if (Continuation::is_return_barrier_entry(sender_pc)) {	
480     if (map->walk_cont()) { // about to walk into an h-stack 	
481       return Continuation::top_frame(*this, map);	
482     } else {
483       Continuation::fix_continuation_bottom_sender(map->thread(), *this, &sender_pc, &sender_sp);	
484     }
485   }
486 
487   intptr_t* unextended_sp = sender_sp;
488   CodeBlob* sender_cb = CodeCache::find_blob_fast(sender_pc);
489   if (sender_cb != NULL) {
490     return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc, sender_cb);
491   }
492   // tty->print_cr(">>>> NO CB sender_pc: %p", sender_pc); os::print_location(tty, (intptr_t)sender_pc); print_on(tty);
493   return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
494 }
495 
496 #endif // CPU_AARCH64_FRAME_AARCH64_INLINE_HPP