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