1 /*
   2  * Copyright 1997-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_frame_x86.cpp.incl"
  27 
  28 #ifdef ASSERT
  29 void RegisterMap::check_location_valid() {
  30 }
  31 #endif
  32 
  33 
  34 // Profiling/safepoint support
  35 
  36 bool frame::safe_for_sender(JavaThread *thread) {
  37   address   sp = (address)_sp;
  38   address   fp = (address)_fp;
  39   address   unextended_sp = (address)_unextended_sp;
  40   // sp must be within the stack
  41   bool sp_safe = (sp <= thread->stack_base()) &&
  42                  (sp >= thread->stack_base() - thread->stack_size());
  43 
  44   if (!sp_safe) {
  45     return false;
  46   }
  47 
  48   // unextended sp must be within the stack and above or equal sp
  49   bool unextended_sp_safe = (unextended_sp <= thread->stack_base()) &&
  50                             (unextended_sp >= sp);
  51 
  52   if (!unextended_sp_safe) {
  53     return false;
  54   }
  55 
  56   // an fp must be within the stack and above (but not equal) sp
  57   bool fp_safe = (fp <= thread->stack_base()) && (fp > sp);
  58 
  59   // We know sp/unextended_sp are safe only fp is questionable here
  60 
  61   // If the current frame is known to the code cache then we can attempt to
  62   // to construct the sender and do some validation of it. This goes a long way
  63   // toward eliminating issues when we get in frame construction code
  64 
  65   if (_cb != NULL ) {
  66 
  67     // First check if frame is complete and tester is reliable
  68     // Unfortunately we can only check frame complete for runtime stubs and nmethod
  69     // other generic buffer blobs are more problematic so we just assume they are
  70     // ok. adapter blobs never have a frame complete and are never ok.
  71 
  72     if (!_cb->is_frame_complete_at(_pc)) {
  73       if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
  74         return false;
  75       }
  76     }
  77     // Entry frame checks
  78     if (is_entry_frame()) {
  79       // an entry frame must have a valid fp.
  80 
  81       if (!fp_safe) return false;
  82 
  83       // Validate the JavaCallWrapper an entry frame must have
  84 
  85       address jcw = (address)entry_frame_call_wrapper();
  86 
  87       bool jcw_safe = (jcw <= thread->stack_base()) && ( jcw > fp);
  88 
  89       return jcw_safe;
  90 
  91     }
  92 
  93     intptr_t* sender_sp = NULL;
  94     address   sender_pc = NULL;
  95 
  96     if (is_interpreted_frame()) {
  97       // fp must be safe
  98       if (!fp_safe) {
  99         return false;
 100       }
 101 
 102       sender_pc = (address) this->fp()[return_addr_offset];
 103       sender_sp = (intptr_t*) addr_at(sender_sp_offset);
 104 
 105     } else {
 106       // must be some sort of compiled/runtime frame
 107       // fp does not have to be safe (although it could be check for c1?)
 108 
 109       sender_sp = _unextended_sp + _cb->frame_size();
 110       // On Intel the return_address is always the word on the stack
 111       sender_pc = (address) *(sender_sp-1);
 112     }
 113 
 114     // We must always be able to find a recognizable pc
 115     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
 116     if (sender_pc == NULL ||  sender_blob == NULL) {
 117       return false;
 118     }
 119 
 120 
 121     // If the potential sender is the interpreter then we can do some more checking
 122     if (Interpreter::contains(sender_pc)) {
 123 
 124       // ebp is always saved in a recognizable place in any code we generate. However
 125       // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved ebp
 126       // is really a frame pointer.
 127 
 128       intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
 129       bool saved_fp_safe = ((address)saved_fp <= thread->stack_base()) && (saved_fp > sender_sp);
 130 
 131       if (!saved_fp_safe) {
 132         return false;
 133       }
 134 
 135       // construct the potential sender
 136 
 137       frame sender(sender_sp, saved_fp, sender_pc);
 138 
 139       return sender.is_interpreted_frame_valid(thread);
 140 
 141     }
 142 
 143     // Could just be some random pointer within the codeBlob
 144 
 145     if (!sender_blob->instructions_contains(sender_pc)) return false;
 146 
 147     // We should never be able to see an adapter if the current frame is something from code cache
 148 
 149     if ( sender_blob->is_adapter_blob()) {
 150       return false;
 151     }
 152 
 153     // Could be the call_stub
 154 
 155     if (StubRoutines::returns_to_call_stub(sender_pc)) {
 156       intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
 157       bool saved_fp_safe = ((address)saved_fp <= thread->stack_base()) && (saved_fp > sender_sp);
 158 
 159       if (!saved_fp_safe) {
 160         return false;
 161       }
 162 
 163       // construct the potential sender
 164 
 165       frame sender(sender_sp, saved_fp, sender_pc);
 166 
 167       // Validate the JavaCallWrapper an entry frame must have
 168       address jcw = (address)sender.entry_frame_call_wrapper();
 169 
 170       bool jcw_safe = (jcw <= thread->stack_base()) && ( jcw > (address)sender.fp());
 171 
 172       return jcw_safe;
 173     }
 174 
 175     // If the frame size is 0 something is bad because every nmethod has a non-zero frame size
 176     // because the return address counts against the callee's frame.
 177 
 178     if (sender_blob->frame_size() == 0) {
 179       assert(!sender_blob->is_nmethod(), "should count return address at least");
 180       return false;
 181     }
 182 
 183     // We should never be able to see anything here except an nmethod. If something in the
 184     // code cache (current frame) is called by an entity within the code cache that entity
 185     // should not be anything but the call stub (already covered), the interpreter (already covered)
 186     // or an nmethod.
 187 
 188     assert(sender_blob->is_nmethod(), "Impossible call chain");
 189 
 190     // Could put some more validation for the potential non-interpreted sender
 191     // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
 192 
 193     // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
 194 
 195     // We've validated the potential sender that would be created
 196     return true;
 197   }
 198 
 199   // Must be native-compiled frame. Since sender will try and use fp to find
 200   // linkages it must be safe
 201 
 202   if (!fp_safe) {
 203     return false;
 204   }
 205 
 206   // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
 207 
 208   if ( (address) this->fp()[return_addr_offset] == NULL) return false;
 209 
 210 
 211   // could try and do some more potential verification of native frame if we could think of some...
 212 
 213   return true;
 214 
 215 }
 216 
 217 
 218 void frame::patch_pc(Thread* thread, address pc) {
 219   if (TracePcPatching) {
 220     tty->print_cr("patch_pc at address" INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "] ",
 221                   &((address *)sp())[-1], ((address *)sp())[-1], pc);
 222   }
 223   ((address *)sp())[-1] = pc;
 224   _cb = CodeCache::find_blob(pc);
 225   if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {
 226     address orig = (((nmethod*)_cb)->get_original_pc(this));
 227     assert(orig == _pc, "expected original to be stored before patching");
 228     _deopt_state = is_deoptimized;
 229     // leave _pc as is
 230   } else {
 231     _deopt_state = not_deoptimized;
 232     _pc = pc;
 233   }
 234 }
 235 
 236 bool frame::is_interpreted_frame() const  {
 237   return Interpreter::contains(pc());
 238 }
 239 
 240 int frame::frame_size(RegisterMap* map) const {
 241   frame sender = this->sender(map);
 242   return sender.sp() - sp();
 243 }
 244 
 245 intptr_t* frame::entry_frame_argument_at(int offset) const {
 246   // convert offset to index to deal with tsi
 247   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
 248   // Entry frame's arguments are always in relation to unextended_sp()
 249   return &unextended_sp()[index];
 250 }
 251 
 252 // sender_sp
 253 #ifdef CC_INTERP
 254 intptr_t* frame::interpreter_frame_sender_sp() const {
 255   assert(is_interpreted_frame(), "interpreted frame expected");
 256   // QQQ why does this specialize method exist if frame::sender_sp() does same thing?
 257   // seems odd and if we always know interpreted vs. non then sender_sp() is really
 258   // doing too much work.
 259   return get_interpreterState()->sender_sp();
 260 }
 261 
 262 // monitor elements
 263 
 264 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
 265   return get_interpreterState()->monitor_base();
 266 }
 267 
 268 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
 269   return (BasicObjectLock*) get_interpreterState()->stack_base();
 270 }
 271 
 272 #else // CC_INTERP
 273 
 274 intptr_t* frame::interpreter_frame_sender_sp() const {
 275   assert(is_interpreted_frame(), "interpreted frame expected");
 276   return (intptr_t*) at(interpreter_frame_sender_sp_offset);
 277 }
 278 
 279 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
 280   assert(is_interpreted_frame(), "interpreted frame expected");
 281   ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
 282 }
 283 
 284 
 285 // monitor elements
 286 
 287 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
 288   return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
 289 }
 290 
 291 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
 292   BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
 293   // make sure the pointer points inside the frame
 294   assert((intptr_t) fp() >  (intptr_t) result, "result must <  than frame pointer");
 295   assert((intptr_t) sp() <= (intptr_t) result, "result must >= than stack pointer");
 296   return result;
 297 }
 298 
 299 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
 300   *((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
 301 }
 302 
 303 // Used by template based interpreter deoptimization
 304 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
 305     *((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
 306 }
 307 #endif // CC_INTERP
 308 
 309 frame frame::sender_for_entry_frame(RegisterMap* map) const {
 310   assert(map != NULL, "map must be set");
 311   // Java frame called from C; skip all C frames and return top C
 312   // frame of that chunk as the sender
 313   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
 314   assert(!entry_frame_is_first(), "next Java fp must be non zero");
 315   assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
 316   map->clear();
 317   assert(map->include_argument_oops(), "should be set by clear");
 318   if (jfa->last_Java_pc() != NULL ) {
 319     frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
 320     return fr;
 321   }
 322   frame fr(jfa->last_Java_sp(), jfa->last_Java_fp());
 323   return fr;
 324 }
 325 
 326 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
 327   // sp is the raw sp from the sender after adapter or interpreter extension
 328   intptr_t* sp = (intptr_t*) addr_at(sender_sp_offset);
 329 
 330   // This is the sp before any possible extension (adapter/locals).
 331   intptr_t* unextended_sp = interpreter_frame_sender_sp();
 332 
 333   address sender_pc = this->sender_pc();
 334   CodeBlob* sender_cb = CodeCache::find_blob_unsafe(sender_pc);
 335   assert(sender_cb, "sanity");
 336   nmethod* sender_nm = sender_cb->as_nmethod_or_null();
 337   if (sender_nm != NULL && sender_nm->is_method_handle_return(sender_pc)) {
 338     unextended_sp = (intptr_t*) at(link_offset);
 339   }
 340 
 341   // The interpreter and compiler(s) always save EBP/RBP in a known
 342   // location on entry. We must record where that location is
 343   // so this if EBP/RBP was live on callout from c2 we can find
 344   // the saved copy no matter what it called.
 345 
 346   // Since the interpreter always saves EBP/RBP if we record where it is then
 347   // we don't have to always save EBP/RBP on entry and exit to c2 compiled
 348   // code, on entry will be enough.
 349 #ifdef COMPILER2
 350   if (map->update_map()) {
 351     map->set_location(rbp->as_VMReg(), (address) addr_at(link_offset));
 352 #ifdef AMD64
 353     // this is weird "H" ought to be at a higher address however the
 354     // oopMaps seems to have the "H" regs at the same address and the
 355     // vanilla register.
 356     // XXXX make this go away
 357     if (true) {
 358       map->set_location(rbp->as_VMReg()->next(), (address)addr_at(link_offset));
 359     }
 360 #endif // AMD64
 361   }
 362 #endif /* COMPILER2 */
 363   return frame(sp, unextended_sp, link(), sender_pc);
 364 }
 365 
 366 
 367 //------------------------------sender_for_compiled_frame-----------------------
 368 frame frame::sender_for_compiled_frame(RegisterMap* map) const {
 369   assert(map != NULL, "map must be set");
 370   const bool c1_compiled = _cb->is_compiled_by_c1();
 371 
 372   // frame owned by optimizing compiler
 373   intptr_t* sender_sp = NULL;
 374 
 375   assert(_cb->frame_size() >= 0, "must have non-zero frame size");
 376   sender_sp = unextended_sp() + _cb->frame_size();
 377 
 378   // On Intel the return_address is always the word on the stack
 379   address sender_pc = (address) *(sender_sp-1);
 380 
 381   // This is the saved value of ebp which may or may not really be an fp.
 382   // it is only an fp if the sender is an interpreter frame (or c1?)
 383 
 384   intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset);
 385 
 386   intptr_t* unextended_sp = sender_sp;
 387   // If we are returning to a compiled method handle call site,
 388   // the saved_fp will in fact be a saved value of the unextended SP.
 389   // The simplest way to tell whether we are returning to such a call
 390   // site is as follows:
 391   CodeBlob* sender_cb = CodeCache::find_blob_unsafe(sender_pc);
 392   assert(sender_cb, "sanity");
 393   nmethod* sender_nm = sender_cb->as_nmethod_or_null();
 394   if (sender_nm != NULL && sender_nm->is_method_handle_return(sender_pc)) {
 395     unextended_sp = saved_fp;
 396   }
 397 
 398   if (map->update_map()) {
 399     // Tell GC to use argument oopmaps for some runtime stubs that need it.
 400     // For C1, the runtime stub might not have oop maps, so set this flag
 401     // outside of update_register_map.
 402     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
 403     if (_cb->oop_maps() != NULL) {
 404       OopMapSet::update_register_map(this, map);
 405     }
 406     // Since the prolog does the save and restore of epb there is no oopmap
 407     // for it so we must fill in its location as if there was an oopmap entry
 408     // since if our caller was compiled code there could be live jvm state in it.
 409     map->set_location(rbp->as_VMReg(), (address) (sender_sp - frame::sender_sp_offset));
 410 #ifdef AMD64
 411     // this is weird "H" ought to be at a higher address however the
 412     // oopMaps seems to have the "H" regs at the same address and the
 413     // vanilla register.
 414     // XXXX make this go away
 415     if (true) {
 416       map->set_location(rbp->as_VMReg()->next(), (address) (sender_sp - frame::sender_sp_offset));
 417     }
 418 #endif // AMD64
 419   }
 420 
 421   assert(sender_sp != sp(), "must have changed");
 422   return frame(sender_sp, unextended_sp, saved_fp, sender_pc);
 423 }
 424 
 425 frame frame::sender(RegisterMap* map) const {
 426   // Default is we done have to follow them. The sender_for_xxx will
 427   // update it accordingly
 428   map->set_include_argument_oops(false);
 429 
 430   if (is_entry_frame())       return sender_for_entry_frame(map);
 431   if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
 432   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
 433 
 434   if (_cb != NULL) {
 435     return sender_for_compiled_frame(map);
 436   }
 437   // Must be native-compiled frame, i.e. the marshaling code for native
 438   // methods that exists in the core system.
 439   return frame(sender_sp(), link(), sender_pc());
 440 }
 441 
 442 
 443 bool frame::interpreter_frame_equals_unpacked_fp(intptr_t* fp) {
 444   assert(is_interpreted_frame(), "must be interpreter frame");
 445   methodOop method = interpreter_frame_method();
 446   // When unpacking an optimized frame the frame pointer is
 447   // adjusted with:
 448   int diff = (method->max_locals() - method->size_of_parameters()) *
 449              Interpreter::stackElementWords();
 450   return _fp == (fp - diff);
 451 }
 452 
 453 void frame::pd_gc_epilog() {
 454   // nothing done here now
 455 }
 456 
 457 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
 458 // QQQ
 459 #ifdef CC_INTERP
 460 #else
 461   assert(is_interpreted_frame(), "Not an interpreted frame");
 462   // These are reasonable sanity checks
 463   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
 464     return false;
 465   }
 466   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
 467     return false;
 468   }
 469   if (fp() + interpreter_frame_initial_sp_offset < sp()) {
 470     return false;
 471   }
 472   // These are hacks to keep us out of trouble.
 473   // The problem with these is that they mask other problems
 474   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
 475     return false;
 476   }
 477 
 478   // do some validation of frame elements
 479 
 480   // first the method
 481 
 482   methodOop m = *interpreter_frame_method_addr();
 483 
 484   // validate the method we'd find in this potential sender
 485   if (!Universe::heap()->is_valid_method(m)) return false;
 486 
 487   // stack frames shouldn't be much larger than max_stack elements
 488 
 489   if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize()) {
 490     return false;
 491   }
 492 
 493   // validate bci/bcx
 494 
 495   intptr_t  bcx    = interpreter_frame_bcx();
 496   if (m->validate_bci_from_bcx(bcx) < 0) {
 497     return false;
 498   }
 499 
 500   // validate constantPoolCacheOop
 501 
 502   constantPoolCacheOop cp = *interpreter_frame_cache_addr();
 503 
 504   if (cp == NULL ||
 505       !Space::is_aligned(cp) ||
 506       !Universe::heap()->is_permanent((void*)cp)) return false;
 507 
 508   // validate locals
 509 
 510   address locals =  (address) *interpreter_frame_locals_addr();
 511 
 512   if (locals > thread->stack_base() || locals < (address) fp()) return false;
 513 
 514   // We'd have to be pretty unlucky to be mislead at this point
 515 
 516 #endif // CC_INTERP
 517   return true;
 518 }
 519 
 520 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
 521 #ifdef CC_INTERP
 522   // Needed for JVMTI. The result should always be in the interpreterState object
 523   assert(false, "NYI");
 524   interpreterState istate = get_interpreterState();
 525 #endif // CC_INTERP
 526   assert(is_interpreted_frame(), "interpreted frame expected");
 527   methodOop method = interpreter_frame_method();
 528   BasicType type = method->result_type();
 529 
 530   intptr_t* tos_addr;
 531   if (method->is_native()) {
 532     // Prior to calling into the runtime to report the method_exit the possible
 533     // return value is pushed to the native stack. If the result is a jfloat/jdouble
 534     // then ST0 is saved before EAX/EDX. See the note in generate_native_result
 535     tos_addr = (intptr_t*)sp();
 536     if (type == T_FLOAT || type == T_DOUBLE) {
 537     // QQQ seems like this code is equivalent on the two platforms
 538 #ifdef AMD64
 539       // This is times two because we do a push(ltos) after pushing XMM0
 540       // and that takes two interpreter stack slots.
 541       tos_addr += 2 * Interpreter::stackElementWords();
 542 #else
 543       tos_addr += 2;
 544 #endif // AMD64
 545     }
 546   } else {
 547     tos_addr = (intptr_t*)interpreter_frame_tos_address();
 548   }
 549 
 550   switch (type) {
 551     case T_OBJECT  :
 552     case T_ARRAY   : {
 553       oop obj;
 554       if (method->is_native()) {
 555 #ifdef CC_INTERP
 556         obj = istate->_oop_temp;
 557 #else
 558         obj = (oop) at(interpreter_frame_oop_temp_offset);
 559 #endif // CC_INTERP
 560       } else {
 561         oop* obj_p = (oop*)tos_addr;
 562         obj = (obj_p == NULL) ? (oop)NULL : *obj_p;
 563       }
 564       assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
 565       *oop_result = obj;
 566       break;
 567     }
 568     case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
 569     case T_BYTE    : value_result->b = *(jbyte*)tos_addr; break;
 570     case T_CHAR    : value_result->c = *(jchar*)tos_addr; break;
 571     case T_SHORT   : value_result->s = *(jshort*)tos_addr; break;
 572     case T_INT     : value_result->i = *(jint*)tos_addr; break;
 573     case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
 574     case T_FLOAT   : {
 575 #ifdef AMD64
 576         value_result->f = *(jfloat*)tos_addr;
 577 #else
 578       if (method->is_native()) {
 579         jdouble d = *(jdouble*)tos_addr;  // Result was in ST0 so need to convert to jfloat
 580         value_result->f = (jfloat)d;
 581       } else {
 582         value_result->f = *(jfloat*)tos_addr;
 583       }
 584 #endif // AMD64
 585       break;
 586     }
 587     case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
 588     case T_VOID    : /* Nothing to do */ break;
 589     default        : ShouldNotReachHere();
 590   }
 591 
 592   return type;
 593 }
 594 
 595 
 596 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
 597   int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
 598   return &interpreter_frame_tos_address()[index];
 599 }