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