1 /*
  2  * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, 2020, 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 #include "precompiled.hpp"
 27 #include "compiler/oopMap.hpp"
 28 #include "interpreter/interpreter.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "memory/universe.hpp"
 31 #include "oops/markWord.hpp"
 32 #include "oops/method.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "prims/methodHandles.hpp"
 35 #include "runtime/frame.inline.hpp"
 36 #include "runtime/handles.inline.hpp"
 37 #include "runtime/javaCalls.hpp"
 38 #include "runtime/monitorChunk.hpp"
 39 #include "runtime/os.inline.hpp"
 40 #include "runtime/signature.hpp"
 41 #include "runtime/stackWatermarkSet.hpp"
 42 #include "runtime/stubCodeGenerator.hpp"
 43 #include "runtime/stubRoutines.hpp"
 44 #include "vmreg_aarch64.inline.hpp"
 45 #ifdef COMPILER1
 46 #include "c1/c1_Runtime1.hpp"
 47 #include "runtime/vframeArray.hpp"
 48 #endif
 49 
 50 #ifdef ASSERT
 51 void RegisterMap::check_location_valid() {
 52 }
 53 #endif
 54 
 55 
 56 // Profiling/safepoint support
 57 
 58 bool frame::safe_for_sender(JavaThread *thread) {
 59   if (is_heap_frame()) {
 60     return true;
 61   }
 62   address   sp = (address)_sp;
 63   address   fp = (address)_fp;
 64   address   unextended_sp = (address)_unextended_sp;
 65 
 66   // consider stack guards when trying to determine "safe" stack pointers
 67   // sp must be within the usable part of the stack (not in guards)
 68   if (!thread->is_in_usable_stack(sp)) {
 69     return false;
 70   }
 71 
 72   // When we are running interpreted code the machine stack pointer, SP, is
 73   // set low enough so that the Java expression stack can grow and shrink
 74   // without ever exceeding the machine stack bounds.  So, ESP >= SP.
 75 
 76   // When we call out of an interpreted method, SP is incremented so that
 77   // the space between SP and ESP is removed.  The SP saved in the callee's
 78   // frame is the SP *before* this increment.  So, when we walk a stack of
 79   // interpreter frames the sender's SP saved in a frame might be less than
 80   // the SP at the point of call.
 81 
 82   // So unextended sp must be within the stack but we need not to check
 83   // that unextended sp >= sp
 84   if (!thread->is_in_full_stack_checked(unextended_sp)) {
 85     return false;
 86   }
 87 
 88   // an fp must be within the stack and above (but not equal) sp
 89   // second evaluation on fp+ is added to handle situation where fp is -1
 90   bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
 91                  thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
 92 
 93   // We know sp/unextended_sp are safe only fp is questionable here
 94 
 95   // If the current frame is known to the code cache then we can attempt to
 96   // to construct the sender and do some validation of it. This goes a long way
 97   // toward eliminating issues when we get in frame construction code
 98 
 99   if (_cb != NULL ) {
100 
101     // First check if frame is complete and tester is reliable
102     // Unfortunately we can only check frame complete for runtime stubs and nmethod
103     // other generic buffer blobs are more problematic so we just assume they are
104     // ok. adapter blobs never have a frame complete and are never ok.
105 
106     if (!_cb->is_frame_complete_at(_pc)) {
107       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
108         return false;
109       }
110     }
111 
112     // Could just be some random pointer within the codeBlob
113     if (!_cb->code_contains(_pc)) {
114       return false;
115     }
116 
117     // Entry frame checks
118     if (is_entry_frame()) {
119       // an entry frame must have a valid fp.
120       return fp_safe && is_entry_frame_valid(thread);
121     } else if (is_upcall_stub_frame()) {
122       return fp_safe;
123     }
124 
125     intptr_t* sender_sp = NULL;
126     intptr_t* sender_unextended_sp = NULL;
127     address   sender_pc = NULL;
128     intptr_t* saved_fp =  NULL;
129 
130     if (is_interpreted_frame()) {
131       // fp must be safe
132       if (!fp_safe) {
133         return false;
134       }
135 
136       // for interpreted frames, the value below is the sender "raw" sp,
137       // which can be different from the sender unextended sp (the sp seen
138       // by the sender) because of current frame local variables
139       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
140       sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
141       saved_fp = (intptr_t*) this->fp()[link_offset];
142       sender_pc = pauth_strip_verifiable((address) this->fp()[return_addr_offset], (address)saved_fp);
143 
144     } else {
145       // must be some sort of compiled/runtime frame
146       // fp does not have to be safe (although it could be check for c1?)
147 
148       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
149       if (_cb->frame_size() <= 0) {
150         return false;
151       }
152 
153       sender_sp = _unextended_sp + _cb->frame_size();
154       // Is sender_sp safe?
155       if (!thread->is_in_full_stack_checked((address)sender_sp)) {
156         return false;
157       }
158       // Note: frame::sender_sp_offset is only valid for compiled frame
159       intptr_t **saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
160       saved_fp = *saved_fp_addr;
161       sender_pc = pauth_strip_verifiable((address) *(sender_sp-1), (address)saved_fp);
162 
163       // Repair the sender sp if this is a method with scalarized inline type args
164       sender_sp = repair_sender_sp(sender_sp, saved_fp_addr);
165       sender_unextended_sp = sender_sp;
166     }
167     if (Continuation::is_return_barrier_entry(sender_pc)) {
168       // If our sender_pc is the return barrier, then our "real" sender is the continuation entry
169       frame s = Continuation::continuation_bottom_sender(thread, *this, sender_sp);
170       sender_sp = s.sp();
171       sender_pc = s.pc();
172     }
173 
174     // If the potential sender is the interpreter then we can do some more checking
175     if (Interpreter::contains(sender_pc)) {
176 
177       // fp is always saved in a recognizable place in any code we generate. However
178       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
179       // is really a frame pointer.
180 
181       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
182         return false;
183       }
184 
185       // construct the potential sender
186 
187       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
188 
189       return sender.is_interpreted_frame_valid(thread);
190 
191     }
192 
193     // We must always be able to find a recognizable pc
194     CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
195     if (sender_pc == NULL ||  sender_blob == NULL) {
196       return false;
197     }
198 
199     // Could just be some random pointer within the codeBlob
200     if (!sender_blob->code_contains(sender_pc)) {
201       return false;
202     }
203 
204     // We should never be able to see an adapter if the current frame is something from code cache
205     if (sender_blob->is_adapter_blob()) {
206       return false;
207     }
208 
209     // Could be the call_stub
210     if (StubRoutines::returns_to_call_stub(sender_pc)) {
211       if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
212         return false;
213       }
214 
215       // construct the potential sender
216 
217       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
218 
219       // Validate the JavaCallWrapper an entry frame must have
220       address jcw = (address)sender.entry_frame_call_wrapper();
221 
222       return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
223     } else if (sender_blob->is_upcall_stub()) {
224       return false;
225     }
226 
227     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
228     if (nm != NULL) {
229       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
230           nm->method()->is_method_handle_intrinsic()) {
231         return false;
232       }
233     }
234 
235     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
236     // because the return address counts against the callee's frame.
237 
238     if (sender_blob->frame_size() <= 0) {
239       assert(!sender_blob->is_compiled(), "should count return address at least");
240       return false;
241     }
242 
243     // We should never be able to see anything here except an nmethod. If something in the
244     // code cache (current frame) is called by an entity within the code cache that entity
245     // should not be anything but the call stub (already covered), the interpreter (already covered)
246     // or an nmethod.
247 
248     if (!sender_blob->is_compiled()) {
249         return false;
250     }
251 
252     // Could put some more validation for the potential non-interpreted sender
253     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
254 
255     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
256 
257     // We've validated the potential sender that would be created
258     return true;
259   }
260 
261   // Must be native-compiled frame. Since sender will try and use fp to find
262   // linkages it must be safe
263 
264   if (!fp_safe) {
265     return false;
266   }
267 
268   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
269 
270   if ( (address) this->fp()[return_addr_offset] == NULL) return false;
271 
272 
273   // could try and do some more potential verification of native frame if we could think of some...
274 
275   return true;
276 
277 }
278 
279 void frame::patch_pc(Thread* thread, address pc) {
280   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
281   address* pc_addr = &(((address*) sp())[-1]);
282   address signing_sp = (((address*) sp())[-2]);
283   address signed_pc = pauth_sign_return_address(pc, (address)signing_sp);
284   address pc_old = pauth_strip_verifiable(*pc_addr, (address)signing_sp);
285 
286   if (TracePcPatching) {
287     tty->print("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
288                   p2i(pc_addr), p2i(pc_old), p2i(pc));
289     if (VM_Version::use_rop_protection()) {
290       tty->print(" [signed " INTPTR_FORMAT " -> " INTPTR_FORMAT "]", p2i(*pc_addr), p2i(signed_pc));
291     }
292     tty->print_cr("");
293   }
294 
295   assert(!Continuation::is_return_barrier_entry(pc_old), "return barrier");
296 
297   // Either the return address is the original one or we are going to
298   // patch in the same address that's already there.
299   assert(_pc == pc_old || pc == pc_old || pc_old == 0, "");
300   DEBUG_ONLY(address old_pc = _pc;)
301   *pc_addr = signed_pc;
302   _pc = pc; // must be set before call to get_deopt_original_pc
303   address original_pc = CompiledMethod::get_deopt_original_pc(this);
304   if (original_pc != NULL) {
305     assert(original_pc == old_pc, "expected original PC to be stored before patching");
306     _deopt_state = is_deoptimized;
307     _pc = original_pc;
308   } else {
309     _deopt_state = not_deoptimized;
310   }
311 }
312 
313 intptr_t* frame::entry_frame_argument_at(int offset) const {
314   // convert offset to index to deal with tsi
315   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
316   // Entry frame's arguments are always in relation to unextended_sp()
317   return &unextended_sp()[index];
318 }
319 
320 // sender_sp
321 intptr_t* frame::interpreter_frame_sender_sp() const {
322   assert(is_interpreted_frame(), "interpreted frame expected");
323   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
324 }
325 
326 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
327   assert(is_interpreted_frame(), "interpreted frame expected");
328   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
329 }
330 
331 
332 // monitor elements
333 
334 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
335   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
336 }
337 
338 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
339   BasicObjectLock* result = (BasicObjectLock*) at(interpreter_frame_monitor_block_top_offset);
340   // make sure the pointer points inside the frame
341   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
342   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
343   return result;
344 }
345 
346 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
347   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
348 }
349 
350 // Used by template based interpreter deoptimization
351 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
352     *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
353 }
354 
355 // Used by template based interpreter deoptimization
356 void frame::interpreter_frame_set_extended_sp(intptr_t* sp) {
357   *((intptr_t**)addr_at(interpreter_frame_extended_sp_offset)) = sp;
358 }
359 
360 frame frame::sender_for_entry_frame(RegisterMap* map) const {
361   assert(map != NULL, "map must be set");
362   // Java frame called from C; skip all C frames and return top C
363   // frame of that chunk as the sender
364   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
365   assert(!entry_frame_is_first(), "next Java fp must be non zero");
366   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
367   // Since we are walking the stack now this nested anchor is obviously walkable
368   // even if it wasn't when it was stacked.
369   jfa->make_walkable();
370   map->clear();
371   assert(map->include_argument_oops(), "should be set by clear");
372   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
373   fr.set_sp_is_trusted();
374 
375   return fr;
376 }
377 
378 UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
379   assert(frame.is_upcall_stub_frame(), "wrong frame");
380   // need unextended_sp here, since normal sp is wrong for interpreter callees
381   return reinterpret_cast<UpcallStub::FrameData*>(
382     reinterpret_cast<address>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
383 }
384 
385 bool frame::upcall_stub_frame_is_first() const {
386   assert(is_upcall_stub_frame(), "must be optimzed entry frame");
387   UpcallStub* blob = _cb->as_upcall_stub();
388   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
389   return jfa->last_Java_sp() == NULL;
390 }
391 
392 frame frame::sender_for_upcall_stub_frame(RegisterMap* map) const {
393   assert(map != NULL, "map must be set");
394   UpcallStub* blob = _cb->as_upcall_stub();
395   // Java frame called from C; skip all C frames and return top C
396   // frame of that chunk as the sender
397   JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
398   assert(!upcall_stub_frame_is_first(), "must have a frame anchor to go back to");
399   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
400   // Since we are walking the stack now this nested anchor is obviously walkable
401   // even if it wasn't when it was stacked.
402   jfa->make_walkable();
403   map->clear();
404   assert(map->include_argument_oops(), "should be set by clear");
405   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
406 
407   return fr;
408 }
409 
410 //------------------------------------------------------------------------------
411 // frame::verify_deopt_original_pc
412 //
413 // Verifies the calculated original PC of a deoptimization PC for the
414 // given unextended SP.
415 #ifdef ASSERT
416 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
417   frame fr;
418 
419   // This is ugly but it's better than to change {get,set}_original_pc
420   // to take an SP value as argument.  And it's only a debugging
421   // method anyway.
422   fr._unextended_sp = unextended_sp;
423 
424   address original_pc = nm->get_original_pc(&fr);
425   assert(nm->insts_contains_inclusive(original_pc),
426          "original PC must be in the main code section of the compiled method (or must be immediately following it)");
427 }
428 #endif
429 
430 //------------------------------------------------------------------------------
431 // frame::adjust_unextended_sp
432 #ifdef ASSERT
433 void frame::adjust_unextended_sp() {
434   // On aarch64, sites calling method handle intrinsics and lambda forms are treated
435   // as any other call site. Therefore, no special action is needed when we are
436   // returning to any of these call sites.
437 
438   if (_cb != NULL) {
439     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
440     if (sender_cm != NULL) {
441       // If the sender PC is a deoptimization point, get the original PC.
442       if (sender_cm->is_deopt_entry(_pc) ||
443           sender_cm->is_deopt_mh_entry(_pc)) {
444         verify_deopt_original_pc(sender_cm, _unextended_sp);
445       }
446     }
447   }
448 }
449 #endif
450 
451 
452 //------------------------------------------------------------------------------
453 // frame::sender_for_interpreter_frame
454 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
455   // SP is the raw SP from the sender after adapter or interpreter
456   // extension.
457   intptr_t* sender_sp = this->sender_sp();
458 
459   // This is the sp before any possible extension (adapter/locals).
460   intptr_t* unextended_sp = interpreter_frame_sender_sp();
461   intptr_t* sender_fp = link();
462 
463 #if COMPILER2_OR_JVMCI
464   if (map->update_map()) {
465     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
466   }
467 #endif // COMPILER2_OR_JVMCI
468 
469   // For ROP protection, Interpreter will have signed the sender_pc, but there is no requirement to authenticate it here.
470   address sender_pc = pauth_strip_verifiable(sender_pc_maybe_signed(), (address)link());
471 
472   if (Continuation::is_return_barrier_entry(sender_pc)) {
473     if (map->walk_cont()) { // about to walk into an h-stack
474       return Continuation::top_frame(*this, map);
475     } else {
476       return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
477     }
478   }
479 
480   return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
481 }
482 
483 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
484   assert(is_interpreted_frame(), "Not an interpreted frame");
485   // These are reasonable sanity checks
486   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
487     return false;
488   }
489   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
490     return false;
491   }
492   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
493     return false;
494   }
495   // These are hacks to keep us out of trouble.
496   // The problem with these is that they mask other problems
497   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
498     return false;
499   }
500 
501   // do some validation of frame elements
502 
503   // first the method
504 
505   Method* m = *interpreter_frame_method_addr();
506 
507   // validate the method we'd find in this potential sender
508   if (!Method::is_valid_method(m)) return false;
509 
510   // stack frames shouldn't be much larger than max_stack elements
511   // this test requires the use of unextended_sp which is the sp as seen by
512   // the current frame, and not sp which is the "raw" pc which could point
513   // further because of local variables of the callee method inserted after
514   // method arguments
515   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
516     return false;
517   }
518 
519   // validate bci/bcx
520 
521   address  bcp    = interpreter_frame_bcp();
522   if (m->validate_bci_from_bcp(bcp) < 0) {
523     return false;
524   }
525 
526   // validate constantPoolCache*
527   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
528   if (MetaspaceObj::is_valid(cp) == false) return false;
529 
530   // validate locals
531 
532   address locals =  (address) *interpreter_frame_locals_addr();
533   return thread->is_in_stack_range_incl(locals, (address)fp());
534 }
535 
536 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
537   assert(is_interpreted_frame(), "interpreted frame expected");
538   Method* method = interpreter_frame_method();
539   BasicType type = method->result_type();
540 
541   intptr_t* tos_addr;
542   if (method->is_native()) {
543     // TODO : ensure AARCH64 does the same as Intel here i.e. push v0 then r0
544     // Prior to calling into the runtime to report the method_exit the possible
545     // return value is pushed to the native stack. If the result is a jfloat/jdouble
546     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
547     tos_addr = (intptr_t*)sp();
548     if (type == T_FLOAT || type == T_DOUBLE) {
549       // This is times two because we do a push(ltos) after pushing XMM0
550       // and that takes two interpreter stack slots.
551       tos_addr += 2 * Interpreter::stackElementWords;
552     }
553   } else {
554     tos_addr = (intptr_t*)interpreter_frame_tos_address();
555   }
556 
557   switch (type) {
558     case T_PRIMITIVE_OBJECT :
559     case T_OBJECT  :
560     case T_ARRAY   : {
561       oop obj;
562       if (method->is_native()) {
563         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
564       } else {
565         oop* obj_p = (oop*)tos_addr;
566         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
567       }
568       assert(Universe::is_in_heap_or_null(obj), "sanity check");
569       *oop_result = obj;
570       break;
571     }
572     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
573     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
574     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
575     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
576     case T_INT     : value_result->i = *(jint*)tos_addr; break;
577     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
578     case T_FLOAT   : {
579         value_result->f = *(jfloat*)tos_addr;
580       break;
581     }
582     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
583     case T_VOID    : /* Nothing to do */ break;
584     default        : ShouldNotReachHere();
585   }
586 
587   return type;
588 }
589 
590 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
591   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
592   return &interpreter_frame_tos_address()[index];
593 }
594 
595 #ifndef PRODUCT
596 
597 #define DESCRIBE_FP_OFFSET(name) \
598   values.describe(frame_no, fp() + frame::name##_offset, #name)
599 
600 void frame::describe_pd(FrameValues& values, int frame_no) {
601   if (is_interpreted_frame()) {
602     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
603     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
604     DESCRIBE_FP_OFFSET(interpreter_frame_method);
605     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
606     DESCRIBE_FP_OFFSET(interpreter_frame_extended_sp);
607     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
608     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
609     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
610     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
611     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
612   }
613 
614   if (is_java_frame() || Continuation::is_continuation_enterSpecial(*this)) {
615     intptr_t* ret_pc_loc;
616     intptr_t* fp_loc;
617     if (is_interpreted_frame()) {
618       ret_pc_loc = fp() + return_addr_offset;
619       fp_loc = fp();
620     } else {
621       ret_pc_loc = real_fp() - return_addr_offset;
622       fp_loc = real_fp() - sender_sp_offset;
623     }
624     address ret_pc = *(address*)ret_pc_loc;
625     values.describe(frame_no, ret_pc_loc,
626       Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
627     values.describe(-1, fp_loc, "saved fp", 0); // "unowned" as value belongs to sender
628   }
629 }
630 #endif
631 
632 intptr_t *frame::initial_deoptimization_info() {
633   // Not used on aarch64, but we must return something.
634   return NULL;
635 }
636 
637 #undef DESCRIBE_FP_OFFSET
638 
639 #define DESCRIBE_FP_OFFSET(name)                     \
640   {                                                  \
641     uintptr_t *p = (uintptr_t *)fp;                  \
642     printf(INTPTR_FORMAT " " INTPTR_FORMAT " %s\n",  \
643            (uintptr_t)(p + frame::name##_offset),    \
644            p[frame::name##_offset], #name);          \
645   }
646 
647 static THREAD_LOCAL uintptr_t nextfp;
648 static THREAD_LOCAL uintptr_t nextpc;
649 static THREAD_LOCAL uintptr_t nextsp;
650 static THREAD_LOCAL RegisterMap *reg_map;
651 
652 static void printbc(Method *m, intptr_t bcx) {
653   const char *name;
654   char buf[16];
655   if (m->validate_bci_from_bcp((address)bcx) < 0
656       || !m->contains((address)bcx)) {
657     name = "???";
658     snprintf(buf, sizeof buf, "(bad)");
659   } else {
660     int bci = m->bci_from((address)bcx);
661     snprintf(buf, sizeof buf, "%d", bci);
662     name = Bytecodes::name(m->code_at(bci));
663   }
664   ResourceMark rm;
665   printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
666 }
667 
668 void internal_pf(uintptr_t sp, uintptr_t fp, uintptr_t pc, uintptr_t bcx) {
669   if (! fp)
670     return;
671 
672   DESCRIBE_FP_OFFSET(return_addr);
673   DESCRIBE_FP_OFFSET(link);
674   DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
675   DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
676   DESCRIBE_FP_OFFSET(interpreter_frame_method);
677   DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
678   DESCRIBE_FP_OFFSET(interpreter_frame_extended_sp);
679   DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
680   DESCRIBE_FP_OFFSET(interpreter_frame_cache);
681   DESCRIBE_FP_OFFSET(interpreter_frame_locals);
682   DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
683   DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
684   uintptr_t *p = (uintptr_t *)fp;
685 
686   // We want to see all frames, native and Java.  For compiled and
687   // interpreted frames we have special information that allows us to
688   // unwind them; for everything else we assume that the native frame
689   // pointer chain is intact.
690   frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
691   if (this_frame.is_compiled_frame() ||
692       this_frame.is_interpreted_frame()) {
693     frame sender = this_frame.sender(reg_map);
694     nextfp = (uintptr_t)sender.fp();
695     nextpc = (uintptr_t)sender.pc();
696     nextsp = (uintptr_t)sender.unextended_sp();
697   } else {
698     nextfp = p[frame::link_offset];
699     nextpc = p[frame::return_addr_offset];
700     nextsp = (uintptr_t)&p[frame::sender_sp_offset];
701   }
702 
703   if (bcx == -1ULL)
704     bcx = p[frame::interpreter_frame_bcp_offset];
705 
706   if (Interpreter::contains((address)pc)) {
707     Method* m = (Method*)p[frame::interpreter_frame_method_offset];
708     if(m && m->is_method()) {
709       printbc(m, bcx);
710     } else
711       printf("not a Method\n");
712   } else {
713     CodeBlob *cb = CodeCache::find_blob((address)pc);
714     if (cb != NULL) {
715       if (cb->is_nmethod()) {
716         ResourceMark rm;
717         nmethod* nm = (nmethod*)cb;
718         printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
719       } else if (cb->name()) {
720         printf("CodeBlob %s\n", cb->name());
721       }
722     }
723   }
724 }
725 
726 extern "C" void npf() {
727   CodeBlob *cb = CodeCache::find_blob((address)nextpc);
728   // C2 does not always chain the frame pointers when it can, instead
729   // preferring to use fixed offsets from SP, so a simple leave() does
730   // not work.  Instead, it adds the frame size to SP then pops FP and
731   // LR.  We have to do the same thing to get a good call chain.
732   if (cb && cb->frame_size())
733     nextfp = nextsp + wordSize * (cb->frame_size() - 2);
734   internal_pf (nextsp, nextfp, nextpc, -1);
735 }
736 
737 extern "C" void pf(uintptr_t sp, uintptr_t fp, uintptr_t pc,
738                    uintptr_t bcx, uintptr_t thread) {
739   if (!reg_map) {
740     reg_map = NEW_C_HEAP_OBJ(RegisterMap, mtInternal);
741     ::new (reg_map) RegisterMap((JavaThread*)thread,
742                                 RegisterMap::UpdateMap::skip,
743                                 RegisterMap::ProcessFrames::include,
744                                 RegisterMap::WalkContinuation::skip);
745   } else {
746     *reg_map = RegisterMap((JavaThread*)thread,
747                            RegisterMap::UpdateMap::skip,
748                            RegisterMap::ProcessFrames::include,
749                            RegisterMap::WalkContinuation::skip);
750   }
751 
752   {
753     CodeBlob *cb = CodeCache::find_blob((address)pc);
754     if (cb && cb->frame_size())
755       fp = sp + wordSize * (cb->frame_size() - 2);
756   }
757   internal_pf(sp, fp, pc, bcx);
758 }
759 
760 // support for printing out where we are in a Java method
761 // needs to be passed current fp and bcp register values
762 // prints method name, bc index and bytecode name
763 extern "C" void pm(uintptr_t fp, uintptr_t bcx) {
764   DESCRIBE_FP_OFFSET(interpreter_frame_method);
765   uintptr_t *p = (uintptr_t *)fp;
766   Method* m = (Method*)p[frame::interpreter_frame_method_offset];
767   printbc(m, bcx);
768 }
769 
770 #ifndef PRODUCT
771 // This is a generic constructor which is only used by pns() in debug.cpp.
772 frame::frame(void* sp, void* fp, void* pc) {
773   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
774 }
775 
776 #endif
777 
778 // Check for a method with scalarized inline type arguments that needs
779 // a stack repair and return the repaired sender stack pointer.
780 intptr_t* frame::repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const {
781   CompiledMethod* cm = _cb->as_compiled_method_or_null();
782   if (cm != NULL && cm->needs_stack_repair()) {
783     // The stack increment resides just below the saved FP on the stack and
784     // records the total frame size excluding the two words for saving FP and LR.
785     intptr_t* sp_inc_addr = (intptr_t*) (saved_fp_addr - 1);
786     assert(*sp_inc_addr % StackAlignmentInBytes == 0, "sp_inc not aligned");
787     int real_frame_size = (*sp_inc_addr / wordSize) + 2;
788     assert(real_frame_size >= _cb->frame_size() && real_frame_size <= 1000000, "invalid frame size");
789     sender_sp = unextended_sp() + real_frame_size;
790   }
791   return sender_sp;
792 }
793 
794 void JavaFrameAnchor::make_walkable() {
795   // last frame set?
796   if (last_Java_sp() == NULL) return;
797   // already walkable?
798   if (walkable()) return;
799   vmassert(last_Java_sp() != NULL, "not called from Java code?");
800   vmassert(last_Java_pc() == NULL, "already walkable");
801   _last_Java_pc = (address)_last_Java_sp[-1];
802   vmassert(walkable(), "something went wrong");
803 }