1 /*
   2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/markWord.hpp"
  31 #include "oops/method.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "prims/methodHandles.hpp"
  34 #include "runtime/frame.inline.hpp"
  35 #include "runtime/handles.inline.hpp"
  36 #include "runtime/javaCalls.hpp"
  37 #include "runtime/monitorChunk.hpp"
  38 #include "runtime/os.inline.hpp"
  39 #include "runtime/signature.hpp"
  40 #include "runtime/stubCodeGenerator.hpp"
  41 #include "runtime/stubRoutines.hpp"
  42 #include "vmreg_aarch64.inline.hpp"
  43 #ifdef COMPILER1
  44 #include "c1/c1_Runtime1.hpp"
  45 #include "runtime/vframeArray.hpp"
  46 #endif
  47 
  48 #ifdef ASSERT
  49 void RegisterMap::check_location_valid() {
  50 }
  51 #endif
  52 
  53 
  54 // Profiling/safepoint support
  55 
  56 bool frame::safe_for_sender(JavaThread *thread) {
  57   address   sp = (address)_sp;
  58   address   fp = (address)_fp;
  59   address   unextended_sp = (address)_unextended_sp;
  60 
  61   // consider stack guards when trying to determine "safe" stack pointers
  62   // sp must be within the usable part of the stack (not in guards)
  63   if (!thread->is_in_usable_stack(sp)) {
  64     return false;
  65   }
  66 
  67   // When we are running interpreted code the machine stack pointer, SP, is
  68   // set low enough so that the Java expression stack can grow and shrink
  69   // without ever exceeding the machine stack bounds.  So, ESP >= SP.
  70 
  71   // When we call out of an interpreted method, SP is incremented so that
  72   // the space between SP and ESP is removed.  The SP saved in the callee's
  73   // frame is the SP *before* this increment.  So, when we walk a stack of
  74   // interpreter frames the sender's SP saved in a frame might be less than
  75   // the SP at the point of call.
  76 
  77   // So unextended sp must be within the stack but we need not to check
  78   // that unextended sp >= sp
  79 
  80   bool unextended_sp_safe = (unextended_sp < thread->stack_base());
  81 
  82   if (!unextended_sp_safe) {
  83     return false;
  84   }
  85 
  86   // an fp must be within the stack and above (but not equal) sp
  87   // second evaluation on fp+ is added to handle situation where fp is -1
  88   bool fp_safe = (fp < thread->stack_base() && (fp > sp) && (((fp + (return_addr_offset * sizeof(void*))) < thread->stack_base())));
  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 ((address)sender_sp >= thread->stack_base()) {
 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       bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
 168 
 169       if (!saved_fp_safe) {
 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       bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
 205 
 206       if (!saved_fp_safe) {
 207         return false;
 208       }
 209 
 210       // construct the potential sender
 211 
 212       frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
 213 
 214       // Validate the JavaCallWrapper an entry frame must have
 215       address jcw = (address)sender.entry_frame_call_wrapper();
 216 
 217       bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)sender.fp());
 218 
 219       return jcw_safe;
 220     }
 221 
 222     CompiledMethod* nm = sender_blob->as_compiled_method_or_null();
 223     if (nm != NULL) {
 224       if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
 225           nm->method()->is_method_handle_intrinsic()) {
 226         return false;
 227       }
 228     }
 229 
 230     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
 231     // because the return address counts against the callee's frame.
 232 
 233     if (sender_blob->frame_size() <= 0) {
 234       assert(!sender_blob->is_compiled(), "should count return address at least");
 235       return false;
 236     }
 237 
 238     // We should never be able to see anything here except an nmethod. If something in the
 239     // code cache (current frame) is called by an entity within the code cache that entity
 240     // should not be anything but the call stub (already covered), the interpreter (already covered)
 241     // or an nmethod.
 242 
 243     if (!sender_blob->is_compiled()) {
 244         return false;
 245     }
 246 
 247     // Could put some more validation for the potential non-interpreted sender
 248     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
 249 
 250     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
 251 
 252     // We've validated the potential sender that would be created
 253     return true;
 254   }
 255 
 256   // Must be native-compiled frame. Since sender will try and use fp to find
 257   // linkages it must be safe
 258 
 259   if (!fp_safe) {
 260     return false;
 261   }
 262 
 263   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
 264 
 265   if ( (address) this->fp()[return_addr_offset] == NULL) return false;
 266 
 267 
 268   // could try and do some more potential verification of native frame if we could think of some...
 269 
 270   return true;
 271 
 272 }
 273 
 274 void frame::patch_pc(Thread* thread, address pc) {
 275   address* pc_addr = &(((address*) sp())[-1]);
 276   if (TracePcPatching) {
 277     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
 278                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
 279   }
 280   // Either the return address is the original one or we are going to
 281   // patch in the same address that's already there.
 282   assert(_pc == *pc_addr || pc == *pc_addr, "must be");
 283   *pc_addr = pc;
 284   _cb = CodeCache::find_blob(pc);
 285   address original_pc = CompiledMethod::get_deopt_original_pc(this);
 286   if (original_pc != NULL) {
 287     assert(original_pc == _pc, "expected original PC to be stored before patching");
 288     _deopt_state = is_deoptimized;
 289     // leave _pc as is
 290   } else {
 291     _deopt_state = not_deoptimized;
 292     _pc = pc;
 293   }
 294 }
 295 
 296 bool frame::is_interpreted_frame() const  {
 297   return Interpreter::contains(pc());
 298 }
 299 
 300 int frame::frame_size(RegisterMap* map) const {
 301   frame sender = this->sender(map);
 302   return sender.sp() - sp();
 303 }
 304 
 305 intptr_t* frame::entry_frame_argument_at(int offset) const {
 306   // convert offset to index to deal with tsi
 307   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
 308   // Entry frame's arguments are always in relation to unextended_sp()
 309   return &unextended_sp()[index];
 310 }
 311 
 312 // sender_sp
 313 intptr_t* frame::interpreter_frame_sender_sp() const {
 314   assert(is_interpreted_frame(), "interpreted frame expected");
 315   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
 316 }
 317 
 318 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
 319   assert(is_interpreted_frame(), "interpreted frame expected");
 320   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
 321 }
 322 
 323 
 324 // monitor elements
 325 
 326 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
 327   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
 328 }
 329 
 330 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
 331   BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
 332   // make sure the pointer points inside the frame
 333   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
 334   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
 335   return result;
 336 }
 337 
 338 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
 339   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
 340 }
 341 
 342 // Used by template based interpreter deoptimization
 343 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
 344     *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
 345 }
 346 
 347 frame frame::sender_for_entry_frame(RegisterMap* map) const {
 348   assert(map != NULL, "map must be set");
 349   // Java frame called from C; skip all C frames and return top C
 350   // frame of that chunk as the sender
 351   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
 352   assert(!entry_frame_is_first(), "next Java fp must be non zero");
 353   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
 354   // Since we are walking the stack now this nested anchor is obviously walkable
 355   // even if it wasn't when it was stacked.
 356   if (!jfa->walkable()) {
 357     // Capture _last_Java_pc (if needed) and mark anchor walkable.
 358     jfa->capture_last_Java_pc();
 359   }
 360   map->clear();
 361   assert(map->include_argument_oops(), "should be set by clear");
 362   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
 363   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
 364   return fr;
 365 }
 366 
 367 //------------------------------------------------------------------------------
 368 // frame::verify_deopt_original_pc
 369 //
 370 // Verifies the calculated original PC of a deoptimization PC for the
 371 // given unextended SP.
 372 #ifdef ASSERT
 373 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
 374   frame fr;
 375 
 376   // This is ugly but it's better than to change {get,set}_original_pc
 377   // to take an SP value as argument.  And it's only a debugging
 378   // method anyway.
 379   fr._unextended_sp = unextended_sp;
 380 
 381   address original_pc = nm->get_original_pc(&fr);
 382   assert(nm->insts_contains_inclusive(original_pc),
 383          "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
 384 }
 385 #endif
 386 
 387 //------------------------------------------------------------------------------
 388 // frame::adjust_unextended_sp
 389 void frame::adjust_unextended_sp() {
 390   // On aarch64, sites calling method handle intrinsics and lambda forms are treated
 391   // as any other call site. Therefore, no special action is needed when we are
 392   // returning to any of these call sites.
 393 
 394   if (_cb != NULL) {
 395     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
 396     if (sender_cm != NULL) {
 397       // If the sender PC is a deoptimization point, get the original PC.
 398       if (sender_cm->is_deopt_entry(_pc) ||
 399           sender_cm->is_deopt_mh_entry(_pc)) {
 400         DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
 401       }
 402     }
 403   }
 404 }
 405 
 406 //------------------------------------------------------------------------------
 407 // frame::update_map_with_saved_link
 408 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
 409   // The interpreter and compiler(s) always save fp in a known
 410   // location on entry. We must record where that location is
 411   // so that if fp was live on callout from c2 we can find
 412   // the saved copy no matter what it called.
 413 
 414   // Since the interpreter always saves fp if we record where it is then
 415   // we don't have to always save fp on entry and exit to c2 compiled
 416   // code, on entry will be enough.
 417   map->set_location(rfp->as_VMReg(), (address) link_addr);
 418   // this is weird "H" ought to be at a higher address however the
 419   // oopMaps seems to have the "H" regs at the same address and the
 420   // vanilla register.
 421   // XXXX make this go away
 422   if (true) {
 423     map->set_location(rfp->as_VMReg()->next(), (address) link_addr);
 424   }
 425 }
 426 
 427 
 428 //------------------------------------------------------------------------------
 429 // frame::sender_for_interpreter_frame
 430 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
 431   // SP is the raw SP from the sender after adapter or interpreter
 432   // extension.
 433   intptr_t* sender_sp = this->sender_sp();
 434 
 435   // This is the sp before any possible extension (adapter/locals).
 436   intptr_t* unextended_sp = interpreter_frame_sender_sp();
 437 
 438 #if COMPILER2_OR_JVMCI
 439   if (map->update_map()) {
 440     update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
 441   }
 442 #endif // COMPILER2_OR_JVMCI
 443 
 444   return frame(sender_sp, unextended_sp, link(), sender_pc());
 445 }
 446 
 447 
 448 //------------------------------------------------------------------------------
 449 // frame::sender_for_compiled_frame
 450 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
 451   // we cannot rely upon the last fp having been saved to the thread
 452   // in C2 code but it will have been pushed onto the stack. so we
 453   // have to find it relative to the unextended sp
 454 
 455   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
 456   intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();
 457   intptr_t* unextended_sp = l_sender_sp;
 458 
 459   // the return_address is always the word on the stack
 460   address sender_pc = (address) *(l_sender_sp-1);
 461 
 462   intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp - frame::sender_sp_offset);
 463 
 464   // assert (sender_sp() == l_sender_sp, "should be");
 465   // assert (*saved_fp_addr == link(), "should be");
 466 
 467   if (map->update_map()) {
 468     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 469     // For C1, the runtime stub might not have oop maps, so set this flag
 470     // outside of update_register_map.
 471     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 472     if (_cb->oop_maps() != NULL) {
 473       OopMapSet::update_register_map(this, map);
 474     }
 475 
 476     // Since the prolog does the save and restore of FP there is no
 477     // oopmap for it so we must fill in its location as if there was
 478     // an oopmap entry since if our caller was compiled code there
 479     // could be live jvm state in it.
 480     update_map_with_saved_link(map, saved_fp_addr);
 481   }
 482 
 483   return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
 484 }
 485 
 486 //------------------------------------------------------------------------------
 487 // frame::sender
 488 frame frame::sender(RegisterMap* map) const {
 489   // Default is we done have to follow them. The sender_for_xxx will
 490   // update it accordingly
 491    map->set_include_argument_oops(false);
 492 
 493   if (is_entry_frame())
 494     return sender_for_entry_frame(map);
 495   if (is_interpreted_frame())
 496     return sender_for_interpreter_frame(map);
 497   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
 498 
 499   // This test looks odd: why is it not is_compiled_frame() ?  That's
 500   // because stubs also have OOP maps.
 501   if (_cb != NULL) {
 502     return sender_for_compiled_frame(map);
 503   }
 504 
 505   // Must be native-compiled frame, i.e. the marshaling code for native
 506   // methods that exists in the core system.
 507   return frame(sender_sp(), link(), sender_pc());
 508 }
 509 
 510 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 511   assert(is_interpreted_frame(), "Not an interpreted frame");
 512   // These are reasonable sanity checks
 513   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
 514     return false;
 515   }
 516   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
 517     return false;
 518   }
 519   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
 520     return false;
 521   }
 522   // These are hacks to keep us out of trouble.
 523   // The problem with these is that they mask other problems
 524   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
 525     return false;
 526   }
 527 
 528   // do some validation of frame elements
 529 
 530   // first the method
 531 
 532   Method* m = *interpreter_frame_method_addr();
 533 
 534   // validate the method we'd find in this potential sender
 535   if (!Method::is_valid_method(m)) return false;
 536 
 537   // stack frames shouldn't be much larger than max_stack elements
 538   // this test requires the use of unextended_sp which is the sp as seen by
 539   // the current frame, and not sp which is the "raw" pc which could point
 540   // further because of local variables of the callee method inserted after
 541   // method arguments
 542   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
 543     return false;
 544   }
 545 
 546   // validate bci/bcx
 547 
 548   address  bcp    = interpreter_frame_bcp();
 549   if (m->validate_bci_from_bcp(bcp) < 0) {
 550     return false;
 551   }
 552 
 553   // validate constantPoolCache*
 554   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
 555   if (MetaspaceObj::is_valid(cp) == false) return false;
 556 
 557   // validate locals
 558 
 559   address locals =  (address) *interpreter_frame_locals_addr();
 560 
 561   if (locals >= thread->stack_base() || locals < (address) fp()) return false;
 562 
 563   // We'd have to be pretty unlucky to be mislead at this point
 564   return true;
 565 }
 566 
 567 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 568   assert(is_interpreted_frame(), "interpreted frame expected");
 569   Method* method = interpreter_frame_method();
 570   BasicType type = method->result_type();
 571 
 572   intptr_t* tos_addr;
 573   if (method->is_native()) {
 574     // TODO : ensure AARCH64 does the same as Intel here i.e. push v0 then r0
 575     // Prior to calling into the runtime to report the method_exit the possible
 576     // return value is pushed to the native stack. If the result is a jfloat/jdouble
 577     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
 578     tos_addr = (intptr_t*)sp();
 579     if (type == T_FLOAT || type == T_DOUBLE) {
 580       // This is times two because we do a push(ltos) after pushing XMM0
 581       // and that takes two interpreter stack slots.
 582       tos_addr += 2 * Interpreter::stackElementWords;
 583     }
 584   } else {
 585     tos_addr = (intptr_t*)interpreter_frame_tos_address();
 586   }
 587 
 588   switch (type) {
 589     case T_OBJECT  :
 590     case T_ARRAY   : {
 591       oop obj;
 592       if (method->is_native()) {
 593         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
 594       } else {
 595         oop* obj_p = (oop*)tos_addr;
 596         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
 597       }
 598       assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 599       *oop_result = obj;
 600       break;
 601     }
 602     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
 603     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
 604     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
 605     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
 606     case T_INT     : value_result->i = *(jint*)tos_addr; break;
 607     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 608     case T_FLOAT   : {
 609         value_result->f = *(jfloat*)tos_addr;
 610       break;
 611     }
 612     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 613     case T_VOID    : /* Nothing to do */ break;
 614     default        : ShouldNotReachHere();
 615   }
 616 
 617   return type;
 618 }
 619 
 620 
 621 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 622   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
 623   return &interpreter_frame_tos_address()[index];
 624 }
 625 
 626 #ifndef PRODUCT
 627 
 628 #define DESCRIBE_FP_OFFSET(name) \
 629   values.describe(frame_no, fp() + frame::name##_offset, #name)
 630 
 631 void frame::describe_pd(FrameValues& values, int frame_no) {
 632   if (is_interpreted_frame()) {
 633     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
 634     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
 635     DESCRIBE_FP_OFFSET(interpreter_frame_method);
 636     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
 637     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
 638     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
 639     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
 640     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
 641     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
 642   }
 643 }
 644 #endif
 645 
 646 intptr_t *frame::initial_deoptimization_info() {
 647   // Not used on aarch64, but we must return something.
 648   return NULL;
 649 }
 650 
 651 intptr_t* frame::real_fp() const {
 652   if (_cb != NULL) {
 653     // use the frame size if valid
 654     int size = _cb->frame_size();
 655     if (size > 0) {
 656       return unextended_sp() + size;
 657     }
 658   }
 659   // else rely on fp()
 660   assert(! is_compiled_frame(), "unknown compiled frame size");
 661   return fp();
 662 }
 663 
 664 #undef DESCRIBE_FP_OFFSET
 665 
 666 #define DESCRIBE_FP_OFFSET(name)                                        \
 667   {                                                                     \
 668     unsigned long *p = (unsigned long *)fp;                             \
 669     printf("0x%016lx 0x%016lx %s\n", (unsigned long)(p + frame::name##_offset), \
 670            p[frame::name##_offset], #name);                             \
 671   }
 672 
 673 static __thread unsigned long nextfp;
 674 static __thread unsigned long nextpc;
 675 static __thread unsigned long nextsp;
 676 static __thread RegisterMap *reg_map;
 677 
 678 static void printbc(Method *m, intptr_t bcx) {
 679   const char *name;
 680   char buf[16];
 681   if (m->validate_bci_from_bcp((address)bcx) < 0
 682       || !m->contains((address)bcx)) {
 683     name = "???";
 684     snprintf(buf, sizeof buf, "(bad)");
 685   } else {
 686     int bci = m->bci_from((address)bcx);
 687     snprintf(buf, sizeof buf, "%d", bci);
 688     name = Bytecodes::name(m->code_at(bci));
 689   }
 690   ResourceMark rm;
 691   printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
 692 }
 693 
 694 void internal_pf(unsigned long sp, unsigned long fp, unsigned long pc, unsigned long bcx) {
 695   if (! fp)
 696     return;
 697 
 698   DESCRIBE_FP_OFFSET(return_addr);
 699   DESCRIBE_FP_OFFSET(link);
 700   DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
 701   DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
 702   DESCRIBE_FP_OFFSET(interpreter_frame_method);
 703   DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
 704   DESCRIBE_FP_OFFSET(interpreter_frame_cache);
 705   DESCRIBE_FP_OFFSET(interpreter_frame_locals);
 706   DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
 707   DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
 708   unsigned long *p = (unsigned long *)fp;
 709 
 710   // We want to see all frames, native and Java.  For compiled and
 711   // interpreted frames we have special information that allows us to
 712   // unwind them; for everything else we assume that the native frame
 713   // pointer chain is intact.
 714   frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
 715   if (this_frame.is_compiled_frame() ||
 716       this_frame.is_interpreted_frame()) {
 717     frame sender = this_frame.sender(reg_map);
 718     nextfp = (unsigned long)sender.fp();
 719     nextpc = (unsigned long)sender.pc();
 720     nextsp = (unsigned long)sender.unextended_sp();
 721   } else {
 722     nextfp = p[frame::link_offset];
 723     nextpc = p[frame::return_addr_offset];
 724     nextsp = (unsigned long)&p[frame::sender_sp_offset];
 725   }
 726 
 727   if (bcx == -1ul)
 728     bcx = p[frame::interpreter_frame_bcp_offset];
 729 
 730   if (Interpreter::contains((address)pc)) {
 731     Method* m = (Method*)p[frame::interpreter_frame_method_offset];
 732     if(m && m->is_method()) {
 733       printbc(m, bcx);
 734     } else
 735       printf("not a Method\n");
 736   } else {
 737     CodeBlob *cb = CodeCache::find_blob((address)pc);
 738     if (cb != NULL) {
 739       if (cb->is_nmethod()) {
 740         ResourceMark rm;
 741         nmethod* nm = (nmethod*)cb;
 742         printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
 743       } else if (cb->name()) {
 744         printf("CodeBlob %s\n", cb->name());
 745       }
 746     }
 747   }
 748 }
 749 
 750 extern "C" void npf() {
 751   CodeBlob *cb = CodeCache::find_blob((address)nextpc);
 752   // C2 does not always chain the frame pointers when it can, instead
 753   // preferring to use fixed offsets from SP, so a simple leave() does
 754   // not work.  Instead, it adds the frame size to SP then pops FP and
 755   // LR.  We have to do the same thing to get a good call chain.
 756   if (cb && cb->frame_size())
 757     nextfp = nextsp + wordSize * (cb->frame_size() - 2);
 758   internal_pf (nextsp, nextfp, nextpc, -1);
 759 }
 760 
 761 extern "C" void pf(unsigned long sp, unsigned long fp, unsigned long pc,
 762                    unsigned long bcx, unsigned long thread) {
 763   if (!reg_map) {
 764     reg_map = NEW_C_HEAP_OBJ(RegisterMap, mtNone);
 765     ::new (reg_map) RegisterMap((JavaThread*)thread, false);
 766   } else {
 767     *reg_map = RegisterMap((JavaThread*)thread, false);
 768   }
 769 
 770   {
 771     CodeBlob *cb = CodeCache::find_blob((address)pc);
 772     if (cb && cb->frame_size())
 773       fp = sp + wordSize * (cb->frame_size() - 2);
 774   }
 775   internal_pf(sp, fp, pc, bcx);
 776 }
 777 
 778 // support for printing out where we are in a Java method
 779 // needs to be passed current fp and bcp register values
 780 // prints method name, bc index and bytecode name
 781 extern "C" void pm(unsigned long fp, unsigned long bcx) {
 782   DESCRIBE_FP_OFFSET(interpreter_frame_method);
 783   unsigned long *p = (unsigned long *)fp;
 784   Method* m = (Method*)p[frame::interpreter_frame_method_offset];
 785   printbc(m, bcx);
 786 }
 787 
 788 #ifndef PRODUCT
 789 // This is a generic constructor which is only used by pns() in debug.cpp.
 790 frame::frame(void* sp, void* fp, void* pc) {
 791   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
 792 }
 793 
 794 void frame::pd_ps() {}
 795 #endif
 796 
 797 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
 798   // last frame set?
 799   if (last_Java_sp() == NULL) return;
 800   // already walkable?
 801   if (walkable()) return;
 802   vmassert(Thread::current() == (Thread*)thread, "not current thread");
 803   vmassert(last_Java_sp() != NULL, "not called from Java code?");
 804   vmassert(last_Java_pc() == NULL, "already walkable");
 805   capture_last_Java_pc();
 806   vmassert(walkable(), "something went wrong");
 807 }
 808 
 809 void JavaFrameAnchor::capture_last_Java_pc() {
 810   vmassert(_last_Java_sp != NULL, "no last frame set");
 811   vmassert(_last_Java_pc == NULL, "already walkable");
 812   _last_Java_pc = (address)_last_Java_sp[-1];
 813 }