1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "code/codeCache.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/markOop.hpp"
  30 #include "oops/method.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "prims/methodHandles.hpp"
  33 #include "runtime/frame.inline.hpp"
  34 #include "runtime/handles.inline.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "runtime/monitorChunk.hpp"
  37 #include "runtime/signature.hpp"
  38 #include "runtime/stubCodeGenerator.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "vmreg_sparc.inline.hpp"
  41 #ifdef COMPILER1
  42 #include "c1/c1_Runtime1.hpp"
  43 #include "runtime/vframeArray.hpp"
  44 #endif
  45 
  46 void RegisterMap::pd_clear() {
  47   if (_thread->has_last_Java_frame()) {
  48     frame fr = _thread->last_frame();
  49     _window = fr.sp();
  50   } else {
  51     _window = NULL;
  52   }
  53   _younger_window = NULL;
  54 }
  55 
  56 
  57 // Unified register numbering scheme: each 32-bits counts as a register
  58 // number, so all the V9 registers take 2 slots.
  59 const static int R_L_nums[] = {0+040,2+040,4+040,6+040,8+040,10+040,12+040,14+040};
  60 const static int R_I_nums[] = {0+060,2+060,4+060,6+060,8+060,10+060,12+060,14+060};
  61 const static int R_O_nums[] = {0+020,2+020,4+020,6+020,8+020,10+020,12+020,14+020};
  62 const static int R_G_nums[] = {0+000,2+000,4+000,6+000,8+000,10+000,12+000,14+000};
  63 static RegisterMap::LocationValidType bad_mask = 0;
  64 static RegisterMap::LocationValidType R_LIO_mask = 0;
  65 static bool register_map_inited = false;
  66 
  67 static void register_map_init() {
  68   if (!register_map_inited) {
  69     register_map_inited = true;
  70     int i;
  71     for (i = 0; i < 8; i++) {
  72       assert(R_L_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
  73       assert(R_I_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
  74       assert(R_O_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
  75       assert(R_G_nums[i] < RegisterMap::location_valid_type_size, "in first chunk");
  76     }
  77 
  78     bad_mask |= (1LL << R_O_nums[6]); // SP
  79     bad_mask |= (1LL << R_O_nums[7]); // cPC
  80     bad_mask |= (1LL << R_I_nums[6]); // FP
  81     bad_mask |= (1LL << R_I_nums[7]); // rPC
  82     bad_mask |= (1LL << R_G_nums[2]); // TLS
  83     bad_mask |= (1LL << R_G_nums[7]); // reserved by libthread
  84 
  85     for (i = 0; i < 8; i++) {
  86       R_LIO_mask |= (1LL << R_L_nums[i]);
  87       R_LIO_mask |= (1LL << R_I_nums[i]);
  88       R_LIO_mask |= (1LL << R_O_nums[i]);
  89     }
  90   }
  91 }
  92 
  93 
  94 address RegisterMap::pd_location(VMReg regname) const {
  95   register_map_init();
  96 
  97   assert(regname->is_reg(), "sanity check");
  98   // Only the GPRs get handled this way
  99   if( !regname->is_Register())
 100     return NULL;
 101 
 102   // don't talk about bad registers
 103   if ((bad_mask & ((LocationValidType)1 << regname->value())) != 0) {
 104     return NULL;
 105   }
 106 
 107   // Convert to a GPR
 108   Register reg;
 109   int second_word = 0;
 110   // 32-bit registers for in, out and local
 111   if (!regname->is_concrete()) {
 112     // HMM ought to return NULL for any non-concrete (odd) vmreg
 113     // this all tied up in the fact we put out double oopMaps for
 114     // register locations. When that is fixed we'd will return NULL
 115     // (or assert here).
 116     reg = regname->prev()->as_Register();
 117     second_word = sizeof(jint);
 118   } else {
 119     reg = regname->as_Register();
 120   }
 121   if (reg->is_out()) {
 122     return _younger_window == NULL ? NULL :
 123       second_word + (address)&_younger_window[reg->after_save()->sp_offset_in_saved_window()];
 124   }
 125   if (reg->is_local() || reg->is_in()) {
 126     assert(_window != NULL, "Window should be available");
 127     return second_word + (address)&_window[reg->sp_offset_in_saved_window()];
 128   }
 129   // Only the window'd GPRs get handled this way; not the globals.
 130   return NULL;
 131 }
 132 
 133 
 134 #ifdef ASSERT
 135 void RegisterMap::check_location_valid() {
 136   register_map_init();
 137   assert((_location_valid[0] & bad_mask) == 0, "cannot have special locations for SP,FP,TLS,etc.");
 138 }
 139 #endif
 140 
 141 // We are shifting windows.  That means we are moving all %i to %o,
 142 // getting rid of all current %l, and keeping all %g.  This is only
 143 // complicated if any of the location pointers for these are valid.
 144 // The normal case is that everything is in its standard register window
 145 // home, and _location_valid[0] is zero.  In that case, this routine
 146 // does exactly nothing.
 147 void RegisterMap::shift_individual_registers() {
 148   if (!update_map())  return;  // this only applies to maps with locations
 149   register_map_init();
 150   check_location_valid();
 151 
 152   LocationValidType lv = _location_valid[0];
 153   LocationValidType lv0 = lv;
 154 
 155   lv &= ~R_LIO_mask;  // clear %l, %o, %i regs
 156 
 157   // if we cleared some non-%g locations, we may have to do some shifting
 158   if (lv != lv0) {
 159     // copy %i0-%i5 to %o0-%o5, if they have special locations
 160     // This can happen in within stubs which spill argument registers
 161     // around a dynamic link operation, such as resolve_opt_virtual_call.
 162     for (int i = 0; i < 8; i++) {
 163       if (lv0 & (1LL << R_I_nums[i])) {
 164         _location[R_O_nums[i]] = _location[R_I_nums[i]];
 165         lv |=  (1LL << R_O_nums[i]);
 166       }
 167     }
 168   }
 169 
 170   _location_valid[0] = lv;
 171   check_location_valid();
 172 }
 173 
 174 bool frame::safe_for_sender(JavaThread *thread) {
 175 
 176   address _SP = (address) sp();
 177   address _FP = (address) fp();
 178   address _UNEXTENDED_SP = (address) unextended_sp();
 179   // sp must be within the stack
 180   bool sp_safe = (_SP <= thread->stack_base()) &&
 181                  (_SP >= thread->stack_base() - thread->stack_size());
 182 
 183   if (!sp_safe) {
 184     return false;
 185   }
 186 
 187   // unextended sp must be within the stack
 188   bool unextended_sp_safe = (_UNEXTENDED_SP <= thread->stack_base());
 189 
 190   if (!unextended_sp_safe) return false;
 191 
 192   // an fp must be within the stack and above (but not equal) sp
 193   bool fp_safe = (_FP <= thread->stack_base()) &&
 194                  (_FP > _SP);
 195 
 196   // We know sp/unextended_sp are safe only fp is questionable here
 197 
 198   // If the current frame is known to the code cache then we can attempt to
 199   // to construct the sender and do some validation of it. This goes a long way
 200   // toward eliminating issues when we get in frame construction code
 201 
 202   if (_cb != NULL ) {
 203 
 204     // First check if frame is complete and tester is reliable
 205     // Unfortunately we can only check frame complete for runtime stubs and nmethod
 206     // other generic buffer blobs are more problematic so we just assume they are
 207     // ok. adapter blobs never have a frame complete and are never ok.
 208 
 209     if (!_cb->is_frame_complete_at(_pc)) {
 210       if (_cb->is_compiled() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
 211         return false;
 212       }
 213     }
 214 
 215     // Could just be some random pointer within the codeBlob
 216     if (!_cb->code_contains(_pc)) {
 217       return false;
 218     }
 219 
 220     // Entry frame checks
 221     if (is_entry_frame()) {
 222       // an entry frame must have a valid fp.
 223       return fp_safe && is_entry_frame_valid(thread);
 224     }
 225 
 226     intptr_t* younger_sp = sp();
 227     intptr_t* _SENDER_SP = sender_sp(); // sender is actually just _FP
 228     bool adjusted_stack = is_interpreted_frame();
 229 
 230     address   sender_pc = (address)younger_sp[I7->sp_offset_in_saved_window()] + pc_return_offset;
 231 
 232 
 233     // We must always be able to find a recognizable pc
 234     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
 235     if (sender_pc == NULL ||  sender_blob == NULL) {
 236       return false;
 237     }
 238 
 239     // Could be a zombie method
 240     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
 241       return false;
 242     }
 243 
 244     // It should be safe to construct the sender though it might not be valid
 245 
 246     frame sender(_SENDER_SP, younger_sp, adjusted_stack);
 247 
 248     // Do we have a valid fp?
 249     address sender_fp = (address) sender.fp();
 250 
 251     // an fp must be within the stack and above (but not equal) current frame's _FP
 252 
 253     bool sender_fp_safe = (sender_fp <= thread->stack_base()) &&
 254                    (sender_fp > _FP);
 255 
 256     if (!sender_fp_safe) {
 257       return false;
 258     }
 259 
 260 
 261     // If the potential sender is the interpreter then we can do some more checking
 262     if (Interpreter::contains(sender_pc)) {
 263       return sender.is_interpreted_frame_valid(thread);
 264     }
 265 
 266     // Could just be some random pointer within the codeBlob
 267     if (!sender.cb()->code_contains(sender_pc)) {
 268       return false;
 269     }
 270 
 271     // We should never be able to see an adapter if the current frame is something from code cache
 272     if (sender_blob->is_adapter_blob()) {
 273       return false;
 274     }
 275 
 276     if (sender.is_entry_frame()) {
 277       // Validate the JavaCallWrapper an entry frame must have
 278 
 279       address jcw = (address)sender.entry_frame_call_wrapper();
 280 
 281       bool jcw_safe = (jcw <= thread->stack_base()) && (jcw > sender_fp);
 282 
 283       return jcw_safe;
 284     }
 285 
 286     // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
 287     // because you must allocate window space
 288 
 289     if (sender_blob->frame_size() <= 0) {
 290       assert(!sender_blob->is_compiled(), "should count return address at least");
 291       return false;
 292     }
 293 
 294     // The sender should positively be an nmethod or call_stub. On sparc we might in fact see something else.
 295     // The cause of this is because at a save instruction the O7 we get is a leftover from an earlier
 296     // window use. So if a runtime stub creates two frames (common in fastdebug/debug) then we see the
 297     // stale pc. So if the sender blob is not something we'd expect we have little choice but to declare
 298     // the stack unwalkable. pd_get_top_frame_for_signal_handler tries to recover from this by unwinding
 299     // that initial frame and retrying.
 300 
 301     if (!sender_blob->is_compiled()) {
 302       return false;
 303     }
 304 
 305     // Could put some more validation for the potential non-interpreted sender
 306     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
 307 
 308     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
 309 
 310     // We've validated the potential sender that would be created
 311 
 312     return true;
 313 
 314   }
 315 
 316   // Must be native-compiled frame. Since sender will try and use fp to find
 317   // linkages it must be safe
 318 
 319   if (!fp_safe) return false;
 320 
 321   // could try and do some more potential verification of native frame if we could think of some...
 322 
 323   return true;
 324 }
 325 
 326 // constructors
 327 
 328 // Construct an unpatchable, deficient frame
 329 void frame::init(intptr_t* sp, address pc, CodeBlob* cb) {
 330   assert( (((intptr_t)sp & (wordSize-1)) == 0), "frame constructor passed an invalid sp");
 331   _sp = sp;
 332   _younger_sp = NULL;
 333   _pc = pc;
 334   _cb = cb;
 335   _sp_adjustment_by_callee = 0;
 336   assert(pc == NULL && cb == NULL || pc != NULL, "can't have a cb and no pc!");
 337   if (_cb == NULL && _pc != NULL ) {
 338     _cb = CodeCache::find_blob(_pc);
 339   }
 340   _deopt_state = unknown;
 341 }
 342 
 343 frame::frame(intptr_t* sp, unpatchable_t, address pc, CodeBlob* cb) {
 344   init(sp, pc, cb);
 345 }
 346 
 347 frame::frame(intptr_t* sp, intptr_t* younger_sp, bool younger_frame_is_interpreted) :
 348   _sp(sp),
 349   _younger_sp(younger_sp),
 350   _deopt_state(unknown),
 351   _sp_adjustment_by_callee(0) {
 352   if (younger_sp == NULL) {
 353     // make a deficient frame which doesn't know where its PC is
 354     _pc = NULL;
 355     _cb = NULL;
 356   } else {
 357     _pc = (address)younger_sp[I7->sp_offset_in_saved_window()] + pc_return_offset;
 358     assert( (intptr_t*)younger_sp[FP->sp_offset_in_saved_window()] == (intptr_t*)((intptr_t)sp - STACK_BIAS), "younger_sp must be valid");
 359     // Any frame we ever build should always "safe" therefore we should not have to call
 360     // find_blob_unsafe
 361     // In case of native stubs, the pc retrieved here might be
 362     // wrong.  (the _last_native_pc will have the right value)
 363     // So do not put add any asserts on the _pc here.
 364   }
 365 
 366   if (_pc != NULL)
 367     _cb = CodeCache::find_blob(_pc);
 368 
 369   // Check for MethodHandle call sites.
 370   if (_cb != NULL) {
 371     CompiledMethod* nm = _cb->as_compiled_method_or_null();
 372     if (nm != NULL) {
 373       if (nm->is_deopt_mh_entry(_pc) || nm->is_method_handle_return(_pc)) {
 374         _sp_adjustment_by_callee = (intptr_t*) ((intptr_t) sp[L7_mh_SP_save->sp_offset_in_saved_window()] + STACK_BIAS) - sp;
 375         // The SP is already adjusted by this MH call site, don't
 376         // overwrite this value with the wrong interpreter value.
 377         younger_frame_is_interpreted = false;
 378       }
 379     }
 380   }
 381 
 382   if (younger_frame_is_interpreted) {
 383     // compute adjustment to this frame's SP made by its interpreted callee
 384     _sp_adjustment_by_callee = (intptr_t*) ((intptr_t) younger_sp[I5_savedSP->sp_offset_in_saved_window()] + STACK_BIAS) - sp;
 385   }
 386 
 387   // It is important that the frame is fully constructed when we do
 388   // this lookup as get_deopt_original_pc() needs a correct value for
 389   // unextended_sp() which uses _sp_adjustment_by_callee.
 390   if (_pc != NULL) {
 391     address original_pc = CompiledMethod::get_deopt_original_pc(this);
 392     if (original_pc != NULL) {
 393       _pc = original_pc;
 394       _deopt_state = is_deoptimized;
 395     } else {
 396       _deopt_state = not_deoptimized;
 397     }
 398   }
 399 }
 400 
 401 #ifndef PRODUCT
 402 // This is a generic constructor which is only used by pns() in debug.cpp.
 403 frame::frame(void* sp, void* fp, void* pc) {
 404   init((intptr_t*)sp, (address)pc, NULL);
 405 }
 406 
 407 extern "C" void findpc(intptr_t x);
 408 
 409 void frame::pd_ps() {
 410   intptr_t* curr_sp = sp();
 411   intptr_t* prev_sp = curr_sp - 1;
 412   intptr_t *pc = NULL;
 413   intptr_t *next_pc = NULL;
 414   int count = 0;
 415   tty->print_cr("register window backtrace from " INTPTR_FORMAT ":", p2i(curr_sp));
 416   while (curr_sp != NULL && ((intptr_t)curr_sp & 7) == 0 && curr_sp > prev_sp && curr_sp < prev_sp+1000) {
 417     pc      = next_pc;
 418     next_pc = (intptr_t*) curr_sp[I7->sp_offset_in_saved_window()];
 419     tty->print("[%d] curr_sp=" INTPTR_FORMAT " pc=", count, p2i(curr_sp));
 420     findpc((intptr_t)pc);
 421     if (WizardMode && Verbose) {
 422       // print register window contents also
 423       tty->print_cr("    L0..L7: {"
 424                     INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " "
 425                     INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " ",
 426                     curr_sp[0+0], curr_sp[0+1], curr_sp[0+2], curr_sp[0+3],
 427                     curr_sp[0+4], curr_sp[0+5], curr_sp[0+6], curr_sp[0+7]);
 428       tty->print_cr("    I0..I7: {"
 429                     INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " "
 430                     INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " ",
 431                     curr_sp[8+0], curr_sp[8+1], curr_sp[8+2], curr_sp[8+3],
 432                     curr_sp[8+4], curr_sp[8+5], curr_sp[8+6], curr_sp[8+7]);
 433       // (and print stack frame contents too??)
 434 
 435       CodeBlob *b = CodeCache::find_blob((address) pc);
 436       if (b != NULL) {
 437         if (b->is_nmethod()) {
 438           Method* m = ((nmethod*)b)->method();
 439           int nlocals = m->max_locals();
 440           int nparams  = m->size_of_parameters();
 441           tty->print_cr("compiled java method (locals = %d, params = %d)", nlocals, nparams);
 442         }
 443       }
 444     }
 445     prev_sp = curr_sp;
 446     curr_sp = (intptr_t *)curr_sp[FP->sp_offset_in_saved_window()];
 447     curr_sp = (intptr_t *)((intptr_t)curr_sp + STACK_BIAS);
 448     count += 1;
 449   }
 450   if (curr_sp != NULL)
 451     tty->print("[%d] curr_sp=" INTPTR_FORMAT " [bogus sp!]", count, p2i(curr_sp));
 452 }
 453 
 454 #endif // PRODUCT
 455 
 456 bool frame::is_interpreted_frame() const  {
 457   return Interpreter::contains(pc());
 458 }
 459 
 460 // sender_sp
 461 
 462 intptr_t* frame::interpreter_frame_sender_sp() const {
 463   assert(is_interpreted_frame(), "interpreted frame expected");
 464   return fp();
 465 }
 466 
 467 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
 468   assert(is_interpreted_frame(), "interpreted frame expected");
 469   Unimplemented();
 470 }
 471 
 472 frame frame::sender_for_entry_frame(RegisterMap *map) const {
 473   assert(map != NULL, "map must be set");
 474   // Java frame called from C; skip all C frames and return top C
 475   // frame of that chunk as the sender
 476   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
 477   assert(!entry_frame_is_first(), "next Java fp must be non zero");
 478   assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");
 479   intptr_t* last_Java_sp = jfa->last_Java_sp();
 480   // Since we are walking the stack now this nested anchor is obviously walkable
 481   // even if it wasn't when it was stacked.
 482   if (!jfa->walkable()) {
 483     // Capture _last_Java_pc (if needed) and mark anchor walkable.
 484     jfa->capture_last_Java_pc(_sp);
 485   }
 486   assert(jfa->last_Java_pc() != NULL, "No captured pc!");
 487   map->clear();
 488   map->make_integer_regs_unsaved();
 489   map->shift_window(last_Java_sp, NULL);
 490   assert(map->include_argument_oops(), "should be set by clear");
 491   return frame(last_Java_sp, frame::unpatchable, jfa->last_Java_pc());
 492 }
 493 
 494 frame frame::sender_for_interpreter_frame(RegisterMap *map) const {
 495   ShouldNotCallThis();
 496   return sender(map);
 497 }
 498 
 499 frame frame::sender_for_compiled_frame(RegisterMap *map) const {
 500   ShouldNotCallThis();
 501   return sender(map);
 502 }
 503 
 504 frame frame::sender(RegisterMap* map) const {
 505   assert(map != NULL, "map must be set");
 506 
 507   assert(CodeCache::find_blob_unsafe(_pc) == _cb, "inconsistent");
 508 
 509   // Default is not to follow arguments; update it accordingly below
 510   map->set_include_argument_oops(false);
 511 
 512   if (is_entry_frame()) return sender_for_entry_frame(map);
 513 
 514   intptr_t* younger_sp = sp();
 515   intptr_t* sp         = sender_sp();
 516 
 517   // Note:  The version of this operation on any platform with callee-save
 518   //        registers must update the register map (if not null).
 519   //        In order to do this correctly, the various subtypes of
 520   //        of frame (interpreted, compiled, glue, native),
 521   //        must be distinguished.  There is no need on SPARC for
 522   //        such distinctions, because all callee-save registers are
 523   //        preserved for all frames via SPARC-specific mechanisms.
 524   //
 525   //        *** HOWEVER, *** if and when we make any floating-point
 526   //        registers callee-saved, then we will have to copy over
 527   //        the RegisterMap update logic from the Intel code.
 528 
 529   // The constructor of the sender must know whether this frame is interpreted so it can set the
 530   // sender's _sp_adjustment_by_callee field.  An osr adapter frame was originally
 531   // interpreted but its pc is in the code cache (for c1 -> osr_frame_return_id stub), so it must be
 532   // explicitly recognized.
 533 
 534 
 535   bool frame_is_interpreted = is_interpreted_frame();
 536   if (frame_is_interpreted) {
 537     map->make_integer_regs_unsaved();
 538     map->shift_window(sp, younger_sp);
 539   } else if (_cb != NULL) {
 540     // Update the locations of implicitly saved registers to be their
 541     // addresses in the register save area.
 542     // For %o registers, the addresses of %i registers in the next younger
 543     // frame are used.
 544     map->shift_window(sp, younger_sp);
 545     if (map->update_map()) {
 546       // Tell GC to use argument oopmaps for some runtime stubs that need it.
 547       // For C1, the runtime stub might not have oop maps, so set this flag
 548       // outside of update_register_map.
 549       map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 550       if (_cb->oop_maps() != NULL) {
 551         OopMapSet::update_register_map(this, map);
 552       }
 553     }
 554   }
 555   return frame(sp, younger_sp, frame_is_interpreted);
 556 }
 557 
 558 
 559 void frame::patch_pc(Thread* thread, address pc) {
 560   vmassert(_deopt_state != unknown, "frame is unpatchable");
 561   if(thread == Thread::current()) {
 562    StubRoutines::Sparc::flush_callers_register_windows_func()();
 563   }
 564   if (TracePcPatching) {
 565     // QQQ this assert is invalid (or too strong anyway) sice _pc could
 566     // be original pc and frame could have the deopt pc.
 567     // assert(_pc == *O7_addr() + pc_return_offset, "frame has wrong pc");
 568     tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
 569                   p2i(O7_addr()), p2i(_pc), p2i(pc));
 570   }
 571   _cb = CodeCache::find_blob(pc);
 572   *O7_addr() = pc - pc_return_offset;
 573   _cb = CodeCache::find_blob(_pc);
 574   address original_pc = CompiledMethod::get_deopt_original_pc(this);
 575   if (original_pc != NULL) {
 576     assert(original_pc == _pc, "expected original to be stored before patching");
 577     _deopt_state = is_deoptimized;
 578   } else {
 579     _deopt_state = not_deoptimized;
 580   }
 581 }
 582 
 583 
 584 static bool sp_is_valid(intptr_t* old_sp, intptr_t* young_sp, intptr_t* sp) {
 585   return (((intptr_t)sp & (2*wordSize-1)) == 0 &&
 586           sp <= old_sp &&
 587           sp >= young_sp);
 588 }
 589 
 590 
 591 /*
 592   Find the (biased) sp that is just younger than old_sp starting at sp.
 593   If not found return NULL. Register windows are assumed to be flushed.
 594 */
 595 intptr_t* frame::next_younger_sp_or_null(intptr_t* old_sp, intptr_t* sp) {
 596 
 597   intptr_t* previous_sp = NULL;
 598   intptr_t* orig_sp = sp;
 599 
 600   int max_frames = (old_sp - sp) / 16; // Minimum frame size is 16
 601   int max_frame2 = max_frames;
 602   while(sp != old_sp && sp_is_valid(old_sp, orig_sp, sp)) {
 603     if (max_frames-- <= 0)
 604       // too many frames have gone by; invalid parameters given to this function
 605       break;
 606     previous_sp = sp;
 607     sp = (intptr_t*)sp[FP->sp_offset_in_saved_window()];
 608     sp = (intptr_t*)((intptr_t)sp + STACK_BIAS);
 609   }
 610 
 611   return (sp == old_sp ? previous_sp : NULL);
 612 }
 613 
 614 /*
 615   Determine if "sp" is a valid stack pointer. "sp" is assumed to be younger than
 616   "valid_sp". So if "sp" is valid itself then it should be possible to walk frames
 617   from "sp" to "valid_sp". The assumption is that the registers windows for the
 618   thread stack in question are flushed.
 619 */
 620 bool frame::is_valid_stack_pointer(intptr_t* valid_sp, intptr_t* sp) {
 621   return next_younger_sp_or_null(valid_sp, sp) != NULL;
 622 }
 623 
 624 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 625   assert(is_interpreted_frame(), "Not an interpreted frame");
 626   // These are reasonable sanity checks
 627   if (fp() == 0 || (intptr_t(fp()) & (2*wordSize-1)) != 0) {
 628     return false;
 629   }
 630   if (sp() == 0 || (intptr_t(sp()) & (2*wordSize-1)) != 0) {
 631     return false;
 632   }
 633 
 634   const intptr_t interpreter_frame_initial_sp_offset = interpreter_frame_vm_local_words;
 635   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
 636     return false;
 637   }
 638   // These are hacks to keep us out of trouble.
 639   // The problem with these is that they mask other problems
 640   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
 641     return false;
 642   }
 643   // do some validation of frame elements
 644 
 645   // first the method
 646 
 647   Method* m = *interpreter_frame_method_addr();
 648 
 649   // validate the method we'd find in this potential sender
 650   if (!m->is_valid_method()) return false;
 651 
 652   // stack frames shouldn't be much larger than max_stack elements
 653 
 654   if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
 655     return false;
 656   }
 657 
 658   // validate bci/bcp
 659 
 660   address bcp = interpreter_frame_bcp();
 661   if (m->validate_bci_from_bcp(bcp) < 0) {
 662     return false;
 663   }
 664 
 665   // validate ConstantPoolCache*
 666   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
 667   if (cp == NULL || !cp->is_metaspace_object()) return false;
 668 
 669   // validate locals
 670 
 671   address locals =  (address) *interpreter_frame_locals_addr();
 672 
 673   if (locals > thread->stack_base() || locals < (address) fp()) return false;
 674 
 675   // We'd have to be pretty unlucky to be mislead at this point
 676   return true;
 677 }
 678 
 679 
 680 // Windows have been flushed on entry (but not marked). Capture the pc that
 681 // is the return address to the frame that contains "sp" as its stack pointer.
 682 // This pc resides in the called of the frame corresponding to "sp".
 683 // As a side effect we mark this JavaFrameAnchor as having flushed the windows.
 684 // This side effect lets us mark stacked JavaFrameAnchors (stacked in the
 685 // call_helper) as flushed when we have flushed the windows for the most
 686 // recent (i.e. current) JavaFrameAnchor. This saves useless flushing calls
 687 // and lets us find the pc just once rather than multiple times as it did
 688 // in the bad old _post_Java_state days.
 689 //
 690 void JavaFrameAnchor::capture_last_Java_pc(intptr_t* sp) {
 691   if (last_Java_sp() != NULL && last_Java_pc() == NULL) {
 692     // try and find the sp just younger than _last_Java_sp
 693     intptr_t* _post_Java_sp = frame::next_younger_sp_or_null(last_Java_sp(), sp);
 694     // Really this should never fail otherwise VM call must have non-standard
 695     // frame linkage (bad) or stack is not properly flushed (worse).
 696     guarantee(_post_Java_sp != NULL, "bad stack!");
 697     _last_Java_pc = (address) _post_Java_sp[ I7->sp_offset_in_saved_window()] + frame::pc_return_offset;
 698 
 699   }
 700   set_window_flushed();
 701 }
 702 
 703 void JavaFrameAnchor::make_walkable(JavaThread* thread) {
 704   if (walkable()) return;
 705   // Eventually make an assert
 706   guarantee(Thread::current() == (Thread*)thread, "only current thread can flush its registers");
 707   // We always flush in case the profiler wants it but we won't mark
 708   // the windows as flushed unless we have a last_Java_frame
 709   intptr_t* sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
 710   if (last_Java_sp() != NULL ) {
 711     capture_last_Java_pc(sp);
 712   }
 713 }
 714 
 715 intptr_t* frame::entry_frame_argument_at(int offset) const {
 716   // convert offset to index to deal with tsi
 717   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
 718 
 719   intptr_t* LSP = (intptr_t*) sp()[Lentry_args->sp_offset_in_saved_window()];
 720   return &LSP[index+1];
 721 }
 722 
 723 
 724 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 725   assert(is_interpreted_frame(), "interpreted frame expected");
 726   Method* method = interpreter_frame_method();
 727   BasicType type = method->result_type();
 728 
 729   if (method->is_native()) {
 730     // Prior to notifying the runtime of the method_exit the possible result
 731     // value is saved to l_scratch and d_scratch.
 732 
 733     intptr_t* l_scratch = fp() + interpreter_frame_l_scratch_fp_offset;
 734     intptr_t* d_scratch = fp() + interpreter_frame_d_scratch_fp_offset;
 735 
 736     address l_addr = (address)l_scratch;
 737     // On 64-bit the result for 1/8/16/32-bit result types is in the other
 738     // word half
 739     l_addr += wordSize/2;
 740 
 741     switch (type) {
 742       case T_OBJECT:
 743       case T_ARRAY: {
 744         oop obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
 745         assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 746         *oop_result = obj;
 747         break;
 748       }
 749 
 750       case T_BOOLEAN : { jint* p = (jint*)l_addr; value_result->z = (jboolean)((*p) & 0x1); break; }
 751       case T_BYTE    : { jint* p = (jint*)l_addr; value_result->b = (jbyte)((*p) & 0xff); break; }
 752       case T_CHAR    : { jint* p = (jint*)l_addr; value_result->c = (jchar)((*p) & 0xffff); break; }
 753       case T_SHORT   : { jint* p = (jint*)l_addr; value_result->s = (jshort)((*p) & 0xffff); break; }
 754       case T_INT     : value_result->i = *(jint*)l_addr; break;
 755       case T_LONG    : value_result->j = *(jlong*)l_scratch; break;
 756       case T_FLOAT   : value_result->f = *(jfloat*)d_scratch; break;
 757       case T_DOUBLE  : value_result->d = *(jdouble*)d_scratch; break;
 758       case T_VOID    : /* Nothing to do */ break;
 759       default        : ShouldNotReachHere();
 760     }
 761   } else {
 762     intptr_t* tos_addr = interpreter_frame_tos_address();
 763 
 764     switch(type) {
 765       case T_OBJECT:
 766       case T_ARRAY: {
 767         oop obj = cast_to_oop(*tos_addr);
 768         assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 769         *oop_result = obj;
 770         break;
 771       }
 772       case T_BOOLEAN : { jint* p = (jint*)tos_addr; value_result->z = (jboolean)((*p) & 0x1); break; }
 773       case T_BYTE    : { jint* p = (jint*)tos_addr; value_result->b = (jbyte)((*p) & 0xff); break; }
 774       case T_CHAR    : { jint* p = (jint*)tos_addr; value_result->c = (jchar)((*p) & 0xffff); break; }
 775       case T_SHORT   : { jint* p = (jint*)tos_addr; value_result->s = (jshort)((*p) & 0xffff); break; }
 776       case T_INT     : value_result->i = *(jint*)tos_addr; break;
 777       case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 778       case T_FLOAT   : value_result->f = *(jfloat*)tos_addr; break;
 779       case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 780       case T_VOID    : /* Nothing to do */ break;
 781       default        : ShouldNotReachHere();
 782     }
 783   };
 784 
 785   return type;
 786 }
 787 
 788 // Lesp pointer is one word lower than the top item on the stack.
 789 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 790   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize) - 1;
 791   return &interpreter_frame_tos_address()[index];
 792 }
 793 
 794 
 795 #ifndef PRODUCT
 796 
 797 #define DESCRIBE_FP_OFFSET(name) \
 798   values.describe(frame_no, fp() + frame::name##_offset, #name)
 799 
 800 void frame::describe_pd(FrameValues& values, int frame_no) {
 801   for (int w = 0; w < frame::register_save_words; w++) {
 802     values.describe(frame_no, sp() + w, err_msg("register save area word %d", w), 1);
 803   }
 804 
 805   if (is_interpreted_frame()) {
 806     DESCRIBE_FP_OFFSET(interpreter_frame_d_scratch_fp);
 807     DESCRIBE_FP_OFFSET(interpreter_frame_l_scratch_fp);
 808     DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
 809     DESCRIBE_FP_OFFSET(interpreter_frame_oop_temp);
 810 
 811     // esp, according to Lesp (e.g. not depending on bci), if seems valid
 812     intptr_t* esp = *interpreter_frame_esp_addr();
 813     if ((esp >= sp()) && (esp < fp())) {
 814       values.describe(-1, esp, "*Lesp");
 815     }
 816   }
 817 
 818   if (!is_compiled_frame()) {
 819     if (frame::callee_aggregate_return_pointer_words != 0) {
 820       values.describe(frame_no, sp() + frame::callee_aggregate_return_pointer_sp_offset, "callee_aggregate_return_pointer_word");
 821     }
 822     for (int w = 0; w < frame::callee_register_argument_save_area_words; w++) {
 823       values.describe(frame_no, sp() + frame::callee_register_argument_save_area_sp_offset + w,
 824                       err_msg("callee_register_argument_save_area_words %d", w));
 825     }
 826   }
 827 }
 828 
 829 #endif
 830 
 831 intptr_t *frame::initial_deoptimization_info() {
 832   // unused... but returns fp() to minimize changes introduced by 7087445
 833   return fp();
 834 }