1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2015, Linaro Ltd. All rights reserved.
   5  * Copyright (c) 2015-2018, Azul Systems, Inc. All rights reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  *
  26  */
  27 
  28 #include "precompiled.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/markOop.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.hpp"
  40 #include "runtime/signature.hpp"
  41 #include "runtime/stubCodeGenerator.hpp"
  42 #include "runtime/stubRoutines.hpp"
  43 #include "vmreg_aarch32.inline.hpp"
  44 #ifdef COMPILER1
  45 #include "c1/c1_Runtime1.hpp"
  46 #include "runtime/vframeArray.hpp"
  47 #endif
  48 
  49 #ifdef ASSERT
  50 void RegisterMap::check_location_valid() {
  51 }
  52 #endif
  53 
  54 
  55 // Profiling/safepoint support
  56 
  57 bool frame::safe_for_sender(JavaThread *thread) {
  58   address   sp = (address)_sp;
  59   address   fp = (address)_fp;
  60   address   unextended_sp = (address)_unextended_sp;
  61 
  62   // consider stack guards when trying to determine "safe" stack pointers
  63   static size_t stack_guard_size = os::uses_stack_guard_pages() ?
  64     (JavaThread::stack_red_zone_size() + JavaThread::stack_yellow_zone_size()) : 0;
  65   size_t usable_stack_size = thread->stack_size() - stack_guard_size;
  66 
  67   // sp must be within the usable part of the stack (not in guards)
  68   bool sp_safe = (sp < thread->stack_base()) &&
  69                  (sp >= thread->stack_base() - usable_stack_size);
  70 
  71 
  72   if (!sp_safe) {
  73     return false;
  74   }
  75 
  76   // unextended sp must be within the stack and above or equal sp
  77   bool unextended_sp_safe = (unextended_sp < thread->stack_base()) &&
  78                             (unextended_sp >= sp);
  79 
  80   if (!unextended_sp_safe) {
  81     return false;
  82   }
  83 
  84   // We know sp/unextended_sp are safe only fp is questionable here
  85 
  86   // If the current frame is known to the code cache then we can attempt to
  87   // to construct the sender and do some validation of it. This goes a long way
  88   // toward eliminating issues when we get in frame construction code
  89 
  90   if (_cb != NULL ) {
  91     // an fp must be within the stack and above (but not equal) sp
  92     // second evaluation on fp+ is added to handle situation where fp is -1
  93     const bool fp_safe = (fp < thread->stack_base() && (fp > sp) && (((fp + (get_return_addr_offset() * sizeof(void*))) < thread->stack_base())));
  94 
  95 
  96     // First check if frame is complete and tester is reliable
  97     // Unfortunately we can only check frame complete for runtime stubs and nmethod
  98     // other generic buffer blobs are more problematic so we just assume they are
  99     // ok. adapter blobs never have a frame complete and are never ok.
 100 
 101     if (!_cb->is_frame_complete_at(_pc)) {
 102       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
 103         return false;
 104       }
 105     }
 106 
 107     // Could just be some random pointer within the codeBlob
 108     if (!_cb->code_contains(_pc)) {
 109       return false;
 110     }
 111 
 112     // Entry frame checks
 113     if (is_entry_frame()) {
 114       // an entry frame must have a valid fp.
 115       return fp_safe && is_entry_frame_valid(thread);
 116     }
 117 
 118     intptr_t* sender_sp = NULL;
 119     intptr_t* sender_unextended_sp = NULL;
 120     address   sender_pc = NULL;
 121     intptr_t* saved_fp =  NULL;
 122 
 123     if (is_interpreted_frame()) {
 124       // fp must be safe
 125       if (!fp_safe) {
 126         return false;
 127       }
 128 
 129       sender_pc = (address) this->fp()[get_return_addr_offset()];
 130       // for interpreted frames, the value below is the sender "raw" sp,
 131       // which can be different from the sender unextended sp (the sp seen
 132       // by the sender) because of current frame local variables
 133       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
 134       sender_unextended_sp = (intptr_t*) this->fp()[get_interpreter_frame_sender_sp_offset()];
 135       saved_fp = (intptr_t*) this->fp()[get_link_offset()];
 136 
 137     } else {
 138       // must be some sort of compiled/runtime frame
 139       // fp does not have to be safe (although it could be check for c1?)
 140 
 141       // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
 142       if (_cb->frame_size() <= 0) {
 143         return false;
 144       }
 145 
 146       sender_sp = _unextended_sp + _cb->frame_size();
 147       // Is sender_sp safe?
 148       if ((address)sender_sp >= thread->stack_base()) {
 149         return false;
 150       }
 151       sender_unextended_sp = sender_sp;
 152       sender_pc = (address) *(sender_sp - 1 + frame::get_return_addr_offset());
 153       // Note: frame::sender_sp_offset is only valid for compiled frame
 154       saved_fp = (intptr_t*) *(sender_sp - 1 + frame::get_link_offset());
 155     }
 156 
 157 
 158     // If the potential sender is the interpreter then we can do some more checking
 159     if (Interpreter::contains(sender_pc)) {
 160 
 161       // fp is always saved in a recognizable place in any code we generate. However
 162       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
 163       // is really a frame pointer.
 164 
 165       bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
 166 
 167       if (!saved_fp_safe) {
 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       bool saved_fp_safe = ((address)saved_fp < thread->stack_base()) && (saved_fp > sender_sp);
 203 
 204       if (!saved_fp_safe) {
 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       bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)sender.fp());
 216 
 217       return jcw_safe;
 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   // an fp must be within the stack and above (but not equal) sp
 255   // second evaluation on fp+ is added to handle situation where fp is -1
 256   const bool fp_safe = (fp < thread->stack_base() && (fp > sp) && (((fp + (get_return_addr_offset(VMFrameAPCS) * sizeof(void*))) < thread->stack_base())));
 257 
 258   // Must be native-compiled frame. Since sender will try and use fp to find
 259   // linkages it must be safe
 260 
 261   if (!fp_safe) {
 262     return false;
 263   }
 264 
 265   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
 266 
 267   if ( (address) this->fp()[get_return_addr_offset(VMFrameAPCS)] == NULL) return false;
 268 
 269 
 270   // could try and do some more potential verification of native frame if we could think of some...
 271 
 272   return true;
 273 
 274 }
 275 
 276 void frame::patch_pc(Thread* thread, address pc) {
 277   address* pc_addr = &(((address*) sp())[-1 + frame::get_return_addr_offset()]);
 278   if (TracePcPatching) {
 279     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
 280                   p2i(pc_addr), p2i(*pc_addr), p2i(pc));
 281   }
 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   _cb = CodeCache::find_blob(pc);
 287   address original_pc = CompiledMethod::get_deopt_original_pc(this);
 288   if (original_pc != NULL) {
 289     assert(original_pc == _pc, "expected original PC to be stored before patching");
 290     _deopt_state = is_deoptimized;
 291     // leave _pc as is
 292   } else {
 293     _deopt_state = not_deoptimized;
 294     _pc = pc;
 295   }
 296 }
 297 
 298 bool frame::is_interpreted_frame() const  {
 299   return Interpreter::contains(pc());
 300 }
 301 
 302 int frame::frame_size(RegisterMap* map) const {
 303   frame sender = this->sender(map);
 304   return sender.sp() - sp();
 305 }
 306 
 307 intptr_t* frame::entry_frame_argument_at(int offset) const {
 308   // convert offset to index to deal with tsi
 309   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
 310   // Entry frame's arguments are always in relation to unextended_sp()
 311   return &unextended_sp()[index];
 312 }
 313 
 314 // sender_sp
 315 intptr_t* frame::interpreter_frame_sender_sp() const {
 316   assert(is_interpreted_frame(), "interpreted frame expected");
 317   return (intptr_t*) at(get_interpreter_frame_sender_sp_offset());
 318 }
 319 
 320 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
 321   assert(is_interpreted_frame(), "interpreted frame expected");
 322   ptr_at_put(get_interpreter_frame_sender_sp_offset(), (intptr_t) sender_sp);
 323 }
 324 
 325 
 326 // monitor elements
 327 
 328 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
 329   return (BasicObjectLock*) addr_at(get_interpreter_frame_monitor_block_bottom_offset());
 330 }
 331 
 332 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
 333   BasicObjectLock* result = (BasicObjectLock*) *addr_at(get_interpreter_frame_monitor_block_top_offset());
 334   // make sure the pointer points inside the frame
 335   assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
 336   assert((intptr_t*) result < fp(),  "monitor end should be strictly below the frame pointer");
 337   return result;
 338 }
 339 
 340 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
 341   *((BasicObjectLock**)addr_at(get_interpreter_frame_monitor_block_top_offset())) = value;
 342 }
 343 
 344 // Used by template based interpreter deoptimization
 345 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
 346     *((intptr_t**)addr_at(get_interpreter_frame_last_sp_offset())) = sp;
 347 }
 348 
 349 frame frame::sender_for_entry_frame(RegisterMap* map) const {
 350   assert(map != NULL, "map must be set");
 351   // Java frame called from C; skip all C frames and return top C
 352   // frame of that chunk as the sender
 353   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
 354   assert(!entry_frame_is_first(), "next Java fp must be non zero");
 355   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
 356   // Since we are walking the stack now this nested anchor is obviously walkable
 357   // even if it wasn't when it was stacked.
 358   if (!jfa->walkable()) {
 359     // Capture _last_Java_pc (if needed) and mark anchor walkable.
 360     jfa->capture_last_Java_pc();
 361   }
 362   map->clear();
 363   assert(map->include_argument_oops(), "should be set by clear");
 364   vmassert(jfa->last_Java_pc() != NULL, "not walkable");
 365   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
 366   return fr;
 367 }
 368 
 369 //------------------------------------------------------------------------------
 370 // frame::verify_deopt_original_pc
 371 //
 372 // Verifies the calculated original PC of a deoptimization PC for the
 373 // given unextended SP.
 374 #ifdef ASSERT
 375 void frame::verify_deopt_original_pc(CompiledMethod* nm, intptr_t* unextended_sp) {
 376   frame fr;
 377 
 378   // This is ugly but it's better than to change {get,set}_original_pc
 379   // to take an SP value as argument.  And it's only a debugging
 380   // method anyway.
 381   fr._unextended_sp = unextended_sp;
 382 
 383   address original_pc = nm->get_original_pc(&fr);
 384   assert(nm->insts_contains_inclusive(original_pc),
 385          "original PC must be in the main code section of the the compiled method (or must be immediately following it)");
 386 }
 387 #endif
 388 
 389 //------------------------------------------------------------------------------
 390 // frame::adjust_unextended_sp
 391 void frame::adjust_unextended_sp() {
 392   // On aarch32, sites calling method handle intrinsics and lambda forms are treated
 393   // as any other call site. Therefore, no special action is needed when we are
 394   // returning to any of these call sites.
 395 
 396   if (_cb != NULL) {
 397     CompiledMethod* sender_cm = _cb->as_compiled_method_or_null();
 398     if (sender_cm != NULL) {
 399       // If the sender PC is a deoptimization point, get the original PC.
 400       if (sender_cm->is_deopt_entry(_pc) ||
 401           sender_cm->is_deopt_mh_entry(_pc)) {
 402         DEBUG_ONLY(verify_deopt_original_pc(sender_cm, _unextended_sp));
 403       }
 404     }
 405   }
 406 }
 407 
 408 //------------------------------------------------------------------------------
 409 // frame::update_map_with_saved_link
 410 void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
 411   // The interpreter and compiler(s) always save fp in a known
 412   // location on entry. We must record where that location is
 413   // so that if fp was live on callout from c2 we can find
 414   // the saved copy no matter what it called.
 415 
 416   // Since the interpreter always saves fp if we record where it is then
 417   // we don't have to always save fp on entry and exit to c2 compiled
 418   // code, on entry will be enough.
 419   map->set_location(rfp->as_VMReg(), (address) link_addr);
 420 }
 421 
 422 
 423 //------------------------------------------------------------------------------
 424 // frame::sender_for_interpreter_frame
 425 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
 426   // SP is the raw SP from the sender after adapter or interpreter
 427   // extension.
 428   intptr_t* sender_sp = this->sender_sp();
 429 
 430   // This is the sp before any possible extension (adapter/locals).
 431   intptr_t* unextended_sp = interpreter_frame_sender_sp();
 432 
 433 #if COMPILER2_OR_JVMCI
 434   if (map->update_map()) {
 435     update_map_with_saved_link(map, (intptr_t**) addr_at(get_link_offset()));
 436   }
 437 #endif // COMPILER2_OR_JVMCI
 438 
 439   address sender_pc = *(address*) addr_at(get_return_addr_offset());
 440   intptr_t *link = *(intptr_t **)addr_at(get_link_offset());
 441 
 442   return frame(sender_sp, unextended_sp, link, sender_pc);
 443 }
 444 
 445 
 446 //------------------------------------------------------------------------------
 447 // frame::sender_for_compiled_frame
 448 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
 449   // we cannot rely upon the last fp having been saved to the thread
 450   // in C2 code but it will have been pushed onto the stack. so we
 451   // have to find it relative to the unextended sp
 452 
 453   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
 454   intptr_t* l_sender_sp = unextended_sp() + _cb->frame_size();
 455   intptr_t* unextended_sp = l_sender_sp;
 456 
 457   // the return_address is always the word on the stack
 458   address sender_pc = (address) *(l_sender_sp - 1 + get_return_addr_offset());
 459 
 460   intptr_t** saved_fp_addr = (intptr_t**)(l_sender_sp - 1 + get_link_offset());
 461 
 462   // assert (sender_sp() == l_sender_sp, "should be");
 463   // assert (*saved_fp_addr == link(), "should be");
 464 
 465   if (map->update_map()) {
 466     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 467     // For C1, the runtime stub might not have oop maps, so set this flag
 468     // outside of update_register_map.
 469     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 470     if (_cb->oop_maps() != NULL) {
 471       OopMapSet::update_register_map(this, map);
 472     }
 473 
 474     // Since the prolog does the save and restore of FP there is no
 475     // oopmap for it so we must fill in its location as if there was
 476     // an oopmap entry since if our caller was compiled code there
 477     // could be live jvm state in it.
 478     update_map_with_saved_link(map, saved_fp_addr);
 479   }
 480 
 481   return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
 482 }
 483 
 484 //------------------------------------------------------------------------------
 485 // frame::sender
 486 frame frame::sender(RegisterMap* map) const {
 487   // Default is we done have to follow them. The sender_for_xxx will
 488   // update it accordingly
 489   map->set_include_argument_oops(false);
 490 
 491   if (is_entry_frame()) {
 492     return sender_for_entry_frame(map);
 493   }
 494   if (is_interpreted_frame()) {
 495     return sender_for_interpreter_frame(map);
 496   }
 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   intptr_t *link = *(intptr_t**) addr_at(get_link_offset(VMFrameAPCS));
 508   return frame(sender_sp(), link, sender_pc());
 509 }
 510 
 511 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 512   assert(is_interpreted_frame(), "Not an interpreted frame");
 513   // These are reasonable sanity checks
 514   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
 515     return false;
 516   }
 517   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
 518     return false;
 519   }
 520   if (fp() + get_interpreter_frame_initial_sp_offset() < sp()) {
 521     return false;
 522   }
 523   // These are hacks to keep us out of trouble.
 524   // The problem with these is that they mask other problems
 525   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
 526     return false;
 527   }
 528 
 529   // do some validation of frame elements
 530 
 531   // first the method
 532 
 533   Method* m = *interpreter_frame_method_addr();
 534 
 535   // validate the method we'd find in this potential sender
 536   if (!m->is_valid_method()) return false;
 537 
 538   // stack frames shouldn't be much larger than max_stack elements
 539   // this test requires the use of unextended_sp which is the sp as seen by
 540   // the current frame, and not sp which is the "raw" pc which could point
 541   // further because of local variables of the callee method inserted after
 542   // method arguments
 543   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
 544     return false;
 545   }
 546 
 547   // validate bci/bcp
 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 (cp == NULL || !cp->is_metaspace_object()) 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 
 565   return true;
 566 }
 567 
 568 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 569 #ifdef CC_INTERP
 570   // Needed for JVMTI. The result should always be in the
 571   // interpreterState object
 572   interpreterState istate = get_interpreterState();
 573 #endif // CC_INTERP
 574   assert(is_interpreted_frame(), "interpreted frame expected");
 575   Method* method = interpreter_frame_method();
 576   BasicType type = method->result_type();
 577 
 578   intptr_t* tos_addr;
 579   if (method->is_native()) {
 580     tos_addr = (intptr_t*)sp();
 581     if (type == T_FLOAT || type == T_DOUBLE) {
 582       // This is times two because we do a push(ltos) after pushing D0
 583       // and that takes two interpreter stack slots.
 584 #ifdef HARD_FLOAT_CC
 585       tos_addr += 2 * Interpreter::stackElementWords;
 586 #endif
 587     }
 588   } else {
 589     tos_addr = (intptr_t*)interpreter_frame_tos_address();
 590   }
 591 
 592   switch (type) {
 593     case T_OBJECT  :
 594     case T_ARRAY   : {
 595       oop obj;
 596       if (method->is_native()) {
 597         obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
 598       } else {
 599         oop* obj_p = (oop*)tos_addr;
 600         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
 601       }
 602       assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 603       *oop_result = obj;
 604       break;
 605     }
 606     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
 607     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
 608     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
 609     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
 610     case T_INT     : value_result->i = *(jint*)tos_addr; break;
 611     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 612     case T_FLOAT   : {
 613         value_result->f = *(jfloat*)tos_addr;
 614       break;
 615     }
 616     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 617     case T_VOID    : /* Nothing to do */ break;
 618     default        : ShouldNotReachHere();
 619   }
 620 
 621   return type;
 622 }
 623 
 624 
 625 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 626   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
 627   return &interpreter_frame_tos_address()[index];
 628 }
 629 
 630 #ifndef PRODUCT
 631 
 632 #define DESCRIBE_FP_OFFSET(name) \
 633     values.describe(frame_no, fp() + frame::get_##name##_offset(), #name)
 634 
 635 void frame::describe_pd(FrameValues& values, int frame_no) {
 636   if (is_interpreted_frame()) {
 637     DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
 638     DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
 639     DESCRIBE_FP_OFFSET(interpreter_frame_method);
 640     DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
 641     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
 642     DESCRIBE_FP_OFFSET(interpreter_frame_cache);
 643     DESCRIBE_FP_OFFSET(interpreter_frame_locals);
 644     DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
 645     DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
 646   }
 647 }
 648 
 649 #endif // PRODUCT
 650 
 651 intptr_t *frame::initial_deoptimization_info() {
 652   return real_fp();
 653 }
 654 
 655 intptr_t* frame::real_fp() const {
 656   // Currently we have a fp for all frames
 657   if (_cb != NULL) {
 658     // use the frame size if valid
 659     int size = _cb->frame_size();
 660     if (size > 0) {
 661       return unextended_sp() + size;
 662     }
 663   }
 664   // else rely on fp()
 665   assert(! is_compiled_frame(), "unknown compiled frame size");
 666   return fp();
 667 }
 668 
 669 #undef DESCRIBE_FP_OFFSET
 670 
 671 #define NO_PARAM
 672 #define DESCRIBE_FP_OFFSET(name, param)                                                      \
 673   {                                                                                          \
 674     unsigned long *p = (unsigned long *)fp;                                                  \
 675     printf("0x%016lx 0x%016lx %s\n", (unsigned long)(p + frame::get_##name##_offset(param)), \
 676            p[frame::get_##name##_offset(param)], #name);                                     \
 677   }
 678 
 679 static __thread unsigned long nextfp;
 680 static __thread unsigned long nextpc;
 681 static __thread unsigned long nextsp;
 682 static __thread RegisterMap *reg_map;
 683 
 684 static void printbc(Method *m, intptr_t bcp) {
 685   const char *name;
 686   char buf[16];
 687   if (m->validate_bci_from_bcp((address)bcp) < 0 || !m->contains((address) bcp)) {
 688     name = "???";
 689     snprintf(buf, sizeof buf, "(bad)");
 690   } else {
 691     int bci = m->bci_from((address)bcp);
 692     snprintf(buf, sizeof buf, "%d", bci);
 693     name = Bytecodes::name(m->code_at(bci));
 694   }
 695   ResourceMark rm;
 696   printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
 697 }
 698 
 699 void internal_pf(unsigned long sp, unsigned long fp, unsigned long pc, unsigned long bcx) {
 700   if (! fp)
 701     return;
 702 
 703   DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp, NO_PARAM);
 704   DESCRIBE_FP_OFFSET(interpreter_frame_last_sp, NO_PARAM);
 705   DESCRIBE_FP_OFFSET(interpreter_frame_method, NO_PARAM);
 706   DESCRIBE_FP_OFFSET(interpreter_frame_mdp, NO_PARAM);
 707   DESCRIBE_FP_OFFSET(interpreter_frame_cache, NO_PARAM);
 708   DESCRIBE_FP_OFFSET(interpreter_frame_locals, NO_PARAM);
 709   DESCRIBE_FP_OFFSET(interpreter_frame_bcp, NO_PARAM);
 710   DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp, NO_PARAM);
 711 
 712   unsigned long *p = (unsigned long *)fp;
 713 
 714   // We want to see all frames, native and Java.  For compiled and
 715   // interpreted frames we have special information that allows us to
 716   // unwind them; for everything else we assume that the native frame
 717   // pointer chain is intact.
 718   frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
 719   if (this_frame.is_compiled_frame() ||
 720       this_frame.is_interpreted_frame()) {
 721     DESCRIBE_FP_OFFSET(return_addr, FrameAPCS);
 722     DESCRIBE_FP_OFFSET(link, FrameAPCS);
 723     frame sender = this_frame.sender(reg_map);
 724     nextfp = (unsigned long)sender.fp();
 725     nextpc = (unsigned long)sender.pc();
 726     nextsp = (unsigned long)sender.unextended_sp();
 727   } else {
 728     DESCRIBE_FP_OFFSET(return_addr, VMFrameAPCS);
 729     DESCRIBE_FP_OFFSET(link, VMFrameAPCS);
 730     nextfp = p[frame::get_link_offset(VMFrameAPCS)];
 731     nextpc = p[frame::get_return_addr_offset(VMFrameAPCS)];
 732     nextsp = (unsigned long)&p[frame::sender_sp_offset];
 733   }
 734 
 735   if (bcx == -1ul) {
 736     bcx = p[frame::get_interpreter_frame_bcp_offset()];
 737   }
 738 
 739   if (Interpreter::contains((address)pc)) {
 740     Method* m = (Method*)p[frame::get_interpreter_frame_method_offset()];
 741     if(m && m->is_method()) {
 742       printbc(m, bcx);
 743     } else
 744       printf("not a Method\n");
 745   } else {
 746     CodeBlob *cb = CodeCache::find_blob((address)pc);
 747     if (cb != NULL) {
 748       if (cb->is_nmethod()) {
 749         ResourceMark rm;
 750         nmethod* nm = (nmethod*)cb;
 751         printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
 752       } else if (cb->name()) {
 753         printf("CodeBlob %s\n", cb->name());
 754       }
 755     }
 756   }
 757 }
 758 
 759 extern "C" void npf() {
 760   CodeBlob *cb = CodeCache::find_blob((address)nextpc);
 761   // C2 does not always chain the frame pointers when it can, instead
 762   // preferring to use fixed offsets from SP, so a simple leave() does
 763   // not work.  Instead, it adds the frame size to SP then pops FP and
 764   // LR.  We have to do the same thing to get a good call chain.
 765   if (cb && cb->frame_size())
 766     nextfp = nextsp + wordSize * (cb->frame_size() - 2);
 767   internal_pf (nextsp, nextfp, nextpc, -1);
 768 }
 769 
 770 extern "C" void pf(unsigned long sp, unsigned long fp, unsigned long pc,
 771                    unsigned long bcx, unsigned long thread) {
 772   RegisterMap map((JavaThread*)thread, false);
 773   if (!reg_map) {
 774     reg_map = (RegisterMap*)os::malloc(sizeof map, mtNone);
 775   }
 776   memcpy(reg_map, &map, sizeof map);
 777   {
 778     CodeBlob *cb = CodeCache::find_blob((address)pc);
 779     if (cb && cb->frame_size())
 780       fp = sp + wordSize * (cb->frame_size() - 2);
 781   }
 782   internal_pf(sp, fp, pc, bcx);
 783 }
 784 
 785 // support for printing out where we are in a Java method
 786 // needs to be passed current fp and bcp register values
 787 // prints method name, bc index and bytecode name
 788 extern "C" void pm(unsigned long fp, unsigned long bcx) {
 789   DESCRIBE_FP_OFFSET(interpreter_frame_method, NO_PARAM);
 790   unsigned long *p = (unsigned long *)fp;
 791   Method* m = (Method*)p[frame::get_interpreter_frame_method_offset()];
 792   printbc(m, bcx);
 793 }
 794 
 795 #ifndef PRODUCT
 796 // This is a generic constructor which is only used by pns() in debug.cpp.
 797 frame::frame(void* sp, void* fp, void* pc) {
 798   init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
 799 }
 800 
 801 void frame::pd_ps() {}
 802 #endif
 803 
 804 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
 805   // last frame set?
 806   if (last_Java_sp() == NULL) return;
 807   // already walkable?
 808   if (walkable()) return;
 809   vmassert(Thread::current() == (Thread*)thread, "not current thread");
 810   vmassert(last_Java_sp() != NULL, "not called from Java code?");
 811   vmassert(last_Java_pc() == NULL, "already walkable");
 812   capture_last_Java_pc();
 813   vmassert(walkable(), "something went wrong");
 814 }
 815 
 816 void JavaFrameAnchor::capture_last_Java_pc() {
 817   vmassert(_last_Java_sp != NULL, "no last frame set");
 818   vmassert(_last_Java_pc == NULL, "already walkable");
 819   _last_Java_pc = (address)_last_Java_sp[-1];
 820 }