1 /*
   2  * Copyright 2001-2006 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 package sun.jvm.hotspot.runtime.x86;
  26 
  27 import java.util.*;
  28 import sun.jvm.hotspot.code.*;
  29 import sun.jvm.hotspot.compiler.*;
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.oops.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.types.*;
  34 import sun.jvm.hotspot.utilities.*;
  35 
  36 /** Specialization of and implementation of abstract methods of the
  37     Frame class for the x86 family of CPUs. */
  38 
  39 public class X86Frame extends Frame {
  40   private static final boolean DEBUG;
  41   static {
  42     DEBUG = System.getProperty("sun.jvm.hotspot.runtime.x86.X86Frame.DEBUG") != null;
  43   }
  44 
  45   // All frames
  46   private static final int LINK_OFFSET                =  0;
  47   private static final int RETURN_ADDR_OFFSET         =  1;
  48   private static final int SENDER_SP_OFFSET           =  2;
  49 
  50   // Interpreter frames
  51   private static final int INTERPRETER_FRAME_MIRROR_OFFSET    =  2; // for native calls only
  52   private static final int INTERPRETER_FRAME_SENDER_SP_OFFSET = -1;
  53   private static final int INTERPRETER_FRAME_LAST_SP_OFFSET   = INTERPRETER_FRAME_SENDER_SP_OFFSET - 1;
  54   private static final int INTERPRETER_FRAME_METHOD_OFFSET    = INTERPRETER_FRAME_LAST_SP_OFFSET - 1;
  55   private static       int INTERPRETER_FRAME_MDX_OFFSET;         // Non-core builds only
  56   private static       int INTERPRETER_FRAME_CACHE_OFFSET;
  57   private static       int INTERPRETER_FRAME_LOCALS_OFFSET;
  58   private static       int INTERPRETER_FRAME_BCX_OFFSET;
  59   private static       int INTERPRETER_FRAME_INITIAL_SP_OFFSET;
  60   private static       int INTERPRETER_FRAME_MONITOR_BLOCK_TOP_OFFSET;
  61   private static       int INTERPRETER_FRAME_MONITOR_BLOCK_BOTTOM_OFFSET;
  62 
  63   // Entry frames
  64   private static final int ENTRY_FRAME_CALL_WRAPPER_OFFSET   =  2;
  65 
  66   // Native frames
  67   private static final int NATIVE_FRAME_INITIAL_PARAM_OFFSET =  2;
  68 
  69   static {
  70     VM.registerVMInitializedObserver(new Observer() {
  71         public void update(Observable o, Object data) {
  72           initialize(VM.getVM().getTypeDataBase());
  73         }
  74       });
  75   }
  76 
  77   private static synchronized void initialize(TypeDataBase db) {
  78     if (VM.getVM().isCore()) {
  79       INTERPRETER_FRAME_CACHE_OFFSET = INTERPRETER_FRAME_METHOD_OFFSET - 1;
  80     } else {
  81       INTERPRETER_FRAME_MDX_OFFSET   = INTERPRETER_FRAME_METHOD_OFFSET - 1;
  82       INTERPRETER_FRAME_CACHE_OFFSET = INTERPRETER_FRAME_MDX_OFFSET - 1;
  83     }
  84     INTERPRETER_FRAME_LOCALS_OFFSET               = INTERPRETER_FRAME_CACHE_OFFSET - 1;
  85     INTERPRETER_FRAME_BCX_OFFSET                  = INTERPRETER_FRAME_LOCALS_OFFSET - 1;
  86     INTERPRETER_FRAME_INITIAL_SP_OFFSET           = INTERPRETER_FRAME_BCX_OFFSET - 1;
  87     INTERPRETER_FRAME_MONITOR_BLOCK_TOP_OFFSET    = INTERPRETER_FRAME_INITIAL_SP_OFFSET;
  88     INTERPRETER_FRAME_MONITOR_BLOCK_BOTTOM_OFFSET = INTERPRETER_FRAME_INITIAL_SP_OFFSET;
  89   }
  90 
  91   // an additional field beyond sp and pc:
  92   Address raw_fp; // frame pointer
  93   private Address raw_unextendedSP;
  94 
  95   private X86Frame() {
  96   }
  97 
  98   private void adjustForDeopt() {
  99     if ( pc != null) {
 100       // Look for a deopt pc and if it is deopted convert to original pc
 101       CodeBlob cb = VM.getVM().getCodeCache().findBlob(pc);
 102       if (cb != null && cb.isJavaMethod()) {
 103         NMethod nm = (NMethod) cb;
 104         if (pc.equals(nm.deoptBegin())) {
 105           if (Assert.ASSERTS_ENABLED) {
 106             Assert.that(this.getUnextendedSP() != null, "null SP in Java frame");
 107           }
 108           // adjust pc if frame is deoptimized.
 109           pc = this.getUnextendedSP().getAddressAt(nm.origPCOffset());
 110           deoptimized = true;
 111         }
 112       }
 113     }
 114   }
 115 
 116   public X86Frame(Address raw_sp, Address raw_fp, Address pc) {
 117     this.raw_sp = raw_sp;
 118     this.raw_unextendedSP = raw_sp;
 119     this.raw_fp = raw_fp;
 120     this.pc = pc;
 121 
 122     // Frame must be fully constructed before this call
 123     adjustForDeopt();
 124 
 125     if (DEBUG) {
 126       System.out.println("X86Frame(sp, fp, pc): " + this);
 127       dumpStack();
 128     }
 129   }
 130 
 131   public X86Frame(Address raw_sp, Address raw_fp) {
 132     this.raw_sp = raw_sp;
 133     this.raw_unextendedSP = raw_sp;
 134     this.raw_fp = raw_fp;
 135     this.pc = raw_sp.getAddressAt(-1 * VM.getVM().getAddressSize());
 136 
 137     // Frame must be fully constructed before this call
 138     adjustForDeopt();
 139 
 140     if (DEBUG) {
 141       System.out.println("X86Frame(sp, fp): " + this);
 142       dumpStack();
 143     }
 144   }
 145 
 146   // This constructor should really take the unextended SP as an arg
 147   // but then the constructor is ambiguous with constructor that takes
 148   // a PC so take an int and convert it.
 149   public X86Frame(Address raw_sp, Address raw_fp, long extension) {
 150     this.raw_sp = raw_sp;
 151     if (raw_sp == null) {
 152       this.raw_unextendedSP = null;
 153     } else {
 154       this.raw_unextendedSP = raw_sp.addOffsetTo(extension);
 155     }
 156     this.raw_fp = raw_fp;
 157     this.pc = raw_sp.getAddressAt(-1 * VM.getVM().getAddressSize());
 158 
 159     // Frame must be fully constructed before this call
 160     adjustForDeopt();
 161 
 162     if (DEBUG) {
 163       System.out.println("X86Frame(sp, fp): " + this);
 164       dumpStack();
 165     }
 166 
 167   }
 168 
 169   public Object clone() {
 170     X86Frame frame = new X86Frame();
 171     frame.raw_sp = raw_sp;
 172     frame.raw_unextendedSP = raw_unextendedSP;
 173     frame.raw_fp = raw_fp;
 174     frame.raw_fp = raw_fp;
 175     frame.pc = pc;
 176     frame.deoptimized = deoptimized;
 177     return frame;
 178   }
 179 
 180   public boolean equals(Object arg) {
 181     if (arg == null) {
 182       return false;
 183     }
 184 
 185     if (!(arg instanceof X86Frame)) {
 186       return false;
 187     }
 188 
 189     X86Frame other = (X86Frame) arg;
 190 
 191     return (AddressOps.equal(getSP(), other.getSP()) &&
 192             AddressOps.equal(getUnextendedSP(), other.getUnextendedSP()) &&
 193             AddressOps.equal(getFP(), other.getFP()) &&
 194             AddressOps.equal(getPC(), other.getPC()));
 195   }
 196 
 197   public int hashCode() {
 198     if (raw_sp == null) {
 199       return 0;
 200     }
 201 
 202     return raw_sp.hashCode();
 203   }
 204 
 205   public String toString() {
 206     return "sp: " + (getSP() == null? "null" : getSP().toString()) +
 207          ", unextendedSP: " + (getUnextendedSP() == null? "null" : getUnextendedSP().toString()) +
 208          ", fp: " + (getFP() == null? "null" : getFP().toString()) +
 209          ", pc: " + (pc == null? "null" : pc.toString());
 210   }
 211 
 212   // accessors for the instance variables
 213   public Address getFP() { return raw_fp; }
 214   public Address getSP() { return raw_sp; }
 215   public Address getID() { return raw_sp; }
 216 
 217   // FIXME: not implemented yet (should be done for Solaris/X86)
 218   public boolean isSignalHandlerFrameDbg() { return false; }
 219   public int     getSignalNumberDbg()      { return 0;     }
 220   public String  getSignalNameDbg()        { return null;  }
 221 
 222   public boolean isInterpretedFrameValid() {
 223     if (Assert.ASSERTS_ENABLED) {
 224       Assert.that(isInterpretedFrame(), "Not an interpreted frame");
 225     }
 226 
 227     // These are reasonable sanity checks
 228     if (getFP() == null || getFP().andWithMask(0x3) != null) {
 229       return false;
 230     }
 231 
 232     if (getSP() == null || getSP().andWithMask(0x3) != null) {
 233       return false;
 234     }
 235 
 236     if (getFP().addOffsetTo(INTERPRETER_FRAME_INITIAL_SP_OFFSET * VM.getVM().getAddressSize()).lessThan(getSP())) {
 237       return false;
 238     }
 239 
 240     // These are hacks to keep us out of trouble.
 241     // The problem with these is that they mask other problems
 242     if (getFP().lessThanOrEqual(getSP())) {
 243       // this attempts to deal with unsigned comparison above
 244       return false;
 245     }
 246 
 247     if (getFP().minus(getSP()) > 4096 * VM.getVM().getAddressSize()) {
 248       // stack frames shouldn't be large.
 249       return false;
 250     }
 251 
 252     return true;
 253   }
 254 
 255   // FIXME: not applicable in current system
 256   //  void    patch_pc(Thread* thread, address pc);
 257 
 258   public Frame sender(RegisterMap regMap, CodeBlob cb) {
 259     X86RegisterMap map = (X86RegisterMap) regMap;
 260 
 261     if (Assert.ASSERTS_ENABLED) {
 262       Assert.that(map != null, "map must be set");
 263     }
 264 
 265     // Default is we done have to follow them. The sender_for_xxx will
 266     // update it accordingly
 267     map.setIncludeArgumentOops(false);
 268 
 269     if (isEntryFrame())       return senderForEntryFrame(map);
 270     if (isInterpretedFrame()) return senderForInterpreterFrame(map);
 271 
 272     if (!VM.getVM().isCore()) {
 273       if(cb == null) {
 274         cb = VM.getVM().getCodeCache().findBlob(getPC());
 275       } else {
 276         if (Assert.ASSERTS_ENABLED) {
 277           Assert.that(cb.equals(VM.getVM().getCodeCache().findBlob(getPC())), "Must be the same");
 278         }
 279       }
 280 
 281       if (cb != null) {
 282          return senderForCompiledFrame(map, cb);
 283       }
 284     }
 285 
 286     // Must be native-compiled frame, i.e. the marshaling code for native
 287     // methods that exists in the core system.
 288     return new X86Frame(getSenderSP(), getLink(), getSenderPC());
 289   }
 290 
 291   private Frame senderForEntryFrame(X86RegisterMap map) {
 292     if (Assert.ASSERTS_ENABLED) {
 293       Assert.that(map != null, "map must be set");
 294     }
 295     // Java frame called from C; skip all C frames and return top C
 296     // frame of that chunk as the sender
 297     X86JavaCallWrapper jcw = (X86JavaCallWrapper) getEntryFrameCallWrapper();
 298     if (Assert.ASSERTS_ENABLED) {
 299       Assert.that(!entryFrameIsFirst(), "next Java fp must be non zero");
 300       Assert.that(jcw.getLastJavaSP().greaterThan(getSP()), "must be above this frame on stack");
 301     }
 302     X86Frame fr;
 303     if (jcw.getLastJavaPC() != null) {
 304       fr = new X86Frame(jcw.getLastJavaSP(), jcw.getLastJavaFP(), jcw.getLastJavaPC());
 305     } else {
 306       fr = new X86Frame(jcw.getLastJavaSP(), jcw.getLastJavaFP());
 307     }
 308     map.clear();
 309     if (Assert.ASSERTS_ENABLED) {
 310       Assert.that(map.getIncludeArgumentOops(), "should be set by clear");
 311     }
 312     return fr;
 313   }
 314 
 315   private Frame senderForInterpreterFrame(X86RegisterMap map) {
 316     Address unextendedSP = addressOfStackSlot(INTERPRETER_FRAME_SENDER_SP_OFFSET).getAddressAt(0);
 317     Address sp = addressOfStackSlot(SENDER_SP_OFFSET);
 318     // We do not need to update the callee-save register mapping because above
 319     // us is either another interpreter frame or a converter-frame, but never
 320     // directly a compiled frame.
 321     // 11/24/04 SFG. With the removal of adapter frames this is no longer true.
 322     // However c2 no longer uses callee save register for java calls so there
 323     // are no callee register to find.
 324 
 325     return new X86Frame(sp, getLink(), unextendedSP.minus(sp));
 326   }
 327 
 328   private Frame senderForCompiledFrame(X86RegisterMap map, CodeBlob cb) {
 329     //
 330     // NOTE: some of this code is (unfortunately) duplicated in X86CurrentFrameGuess
 331     //
 332 
 333     if (Assert.ASSERTS_ENABLED) {
 334       Assert.that(map != null, "map must be set");
 335     }
 336 
 337     // frame owned by optimizing compiler
 338     Address        sender_sp = null;
 339 
 340     if (VM.getVM().isClientCompiler()) {
 341       sender_sp        = addressOfStackSlot(SENDER_SP_OFFSET);
 342     } else {
 343       if (Assert.ASSERTS_ENABLED) {
 344         Assert.that(cb.getFrameSize() >= 0, "Compiled by Compiler1: do not use");
 345       }
 346       sender_sp = getUnextendedSP().addOffsetTo(cb.getFrameSize());
 347     }
 348 
 349     // On Intel the return_address is always the word on the stack
 350     Address sender_pc = sender_sp.getAddressAt(-1 * VM.getVM().getAddressSize());
 351 
 352     if (map.getUpdateMap() && cb.getOopMaps() != null) {
 353       OopMapSet.updateRegisterMap(this, cb, map, true);
 354     }
 355 
 356     if (VM.getVM().isClientCompiler()) {
 357       // Move this here for C1 and collecting oops in arguments (According to Rene)
 358       map.setIncludeArgumentOops(cb.callerMustGCArguments(map.getThread()));
 359     }
 360 
 361     Address saved_fp = null;
 362     if (VM.getVM().isClientCompiler()) {
 363       saved_fp = getFP().getAddressAt(0);
 364     } else if (VM.getVM().isServerCompiler() &&
 365                (VM.getVM().getInterpreter().contains(sender_pc) ||
 366                VM.getVM().getStubRoutines().returnsToCallStub(sender_pc))) {
 367       // C2 prologue saves EBP in the usual place.
 368       // however only use it if the sender had link infomration in it.
 369       saved_fp = sender_sp.getAddressAt(-2 * VM.getVM().getAddressSize());
 370     }
 371 
 372     return new X86Frame(sender_sp, saved_fp, sender_pc);
 373   }
 374 
 375   protected boolean hasSenderPD() {
 376     // FIXME
 377     // Check for null ebp? Need to do some tests.
 378     return true;
 379   }
 380 
 381   public long frameSize() {
 382     return (getSenderSP().minus(getSP()) / VM.getVM().getAddressSize());
 383   }
 384 
 385   public Address getLink() {
 386     return addressOfStackSlot(LINK_OFFSET).getAddressAt(0);
 387   }
 388 
 389   // FIXME: not implementable yet
 390   //inline void      frame::set_link(intptr_t* addr)  { *(intptr_t **)addr_at(link_offset) = addr; }
 391 
 392   public Address getUnextendedSP() { return raw_unextendedSP; }
 393 
 394   // Return address:
 395   public Address getSenderPCAddr() { return addressOfStackSlot(RETURN_ADDR_OFFSET); }
 396   public Address getSenderPC()     { return getSenderPCAddr().getAddressAt(0);      }
 397 
 398   // return address of param, zero origin index.
 399   public Address getNativeParamAddr(int idx) {
 400     return addressOfStackSlot(NATIVE_FRAME_INITIAL_PARAM_OFFSET + idx);
 401   }
 402 
 403   public Address getSenderSP()     { return addressOfStackSlot(SENDER_SP_OFFSET); }
 404 
 405   public Address compiledArgumentToLocationPD(VMReg reg, RegisterMap regMap, int argSize) {
 406     if (VM.getVM().isCore() || VM.getVM().isClientCompiler()) {
 407       throw new RuntimeException("Should not reach here");
 408     }
 409 
 410     return oopMapRegToLocation(reg, regMap);
 411   }
 412 
 413   public Address addressOfInterpreterFrameLocals() {
 414     return addressOfStackSlot(INTERPRETER_FRAME_LOCALS_OFFSET);
 415   }
 416 
 417   private Address addressOfInterpreterFrameBCX() {
 418     return addressOfStackSlot(INTERPRETER_FRAME_BCX_OFFSET);
 419   }
 420 
 421   public int getInterpreterFrameBCI() {
 422     // FIXME: this is not atomic with respect to GC and is unsuitable
 423     // for use in a non-debugging, or reflective, system. Need to
 424     // figure out how to express this.
 425     Address bcp = addressOfInterpreterFrameBCX().getAddressAt(0);
 426     OopHandle methodHandle = addressOfInterpreterFrameMethod().getOopHandleAt(0);
 427     Method method = (Method) VM.getVM().getObjectHeap().newOop(methodHandle);
 428     return bcpToBci(bcp, method);
 429   }
 430 
 431   public Address addressOfInterpreterFrameMDX() {
 432     return addressOfStackSlot(INTERPRETER_FRAME_MDX_OFFSET);
 433   }
 434 
 435   // FIXME
 436   //inline int frame::interpreter_frame_monitor_size() {
 437   //  return BasicObjectLock::size();
 438   //}
 439 
 440   // expression stack
 441   // (the max_stack arguments are used by the GC; see class FrameClosure)
 442 
 443   public Address addressOfInterpreterFrameExpressionStack() {
 444     Address monitorEnd = interpreterFrameMonitorEnd().address();
 445     return monitorEnd.addOffsetTo(-1 * VM.getVM().getAddressSize());
 446   }
 447 
 448   public int getInterpreterFrameExpressionStackDirection() { return -1; }
 449 
 450   // top of expression stack
 451   public Address addressOfInterpreterFrameTOS() {
 452     return getSP();
 453   }
 454 
 455   /** Expression stack from top down */
 456   public Address addressOfInterpreterFrameTOSAt(int slot) {
 457     return addressOfInterpreterFrameTOS().addOffsetTo(slot * VM.getVM().getAddressSize());
 458   }
 459 
 460   public Address getInterpreterFrameSenderSP() {
 461     if (Assert.ASSERTS_ENABLED) {
 462       Assert.that(isInterpretedFrame(), "interpreted frame expected");
 463     }
 464     return addressOfStackSlot(INTERPRETER_FRAME_SENDER_SP_OFFSET).getAddressAt(0);
 465   }
 466 
 467   // Monitors
 468   public BasicObjectLock interpreterFrameMonitorBegin() {
 469     return new BasicObjectLock(addressOfStackSlot(INTERPRETER_FRAME_MONITOR_BLOCK_BOTTOM_OFFSET));
 470   }
 471 
 472   public BasicObjectLock interpreterFrameMonitorEnd() {
 473     Address result = addressOfStackSlot(INTERPRETER_FRAME_MONITOR_BLOCK_TOP_OFFSET).getAddressAt(0);
 474     if (Assert.ASSERTS_ENABLED) {
 475       // make sure the pointer points inside the frame
 476       Assert.that(AddressOps.gt(getFP(), result), "result must <  than frame pointer");
 477       Assert.that(AddressOps.lte(getSP(), result), "result must >= than stack pointer");
 478     }
 479     return new BasicObjectLock(result);
 480   }
 481 
 482   public int interpreterFrameMonitorSize() {
 483     return BasicObjectLock.size();
 484   }
 485 
 486   // Method
 487   public Address addressOfInterpreterFrameMethod() {
 488     return addressOfStackSlot(INTERPRETER_FRAME_METHOD_OFFSET);
 489   }
 490 
 491   // Constant pool cache
 492   public Address addressOfInterpreterFrameCPCache() {
 493     return addressOfStackSlot(INTERPRETER_FRAME_CACHE_OFFSET);
 494   }
 495 
 496   // Entry frames
 497   public JavaCallWrapper getEntryFrameCallWrapper() {
 498     return new X86JavaCallWrapper(addressOfStackSlot(ENTRY_FRAME_CALL_WRAPPER_OFFSET).getAddressAt(0));
 499   }
 500 
 501   protected Address addressOfSavedOopResult() {
 502     // offset is 2 for compiler2 and 3 for compiler1
 503     return getSP().addOffsetTo((VM.getVM().isClientCompiler() ? 2 : 3) *
 504                                VM.getVM().getAddressSize());
 505   }
 506 
 507   protected Address addressOfSavedReceiver() {
 508     return getSP().addOffsetTo(-4 * VM.getVM().getAddressSize());
 509   }
 510 
 511   private void dumpStack() {
 512     if (getFP() != null) {
 513       for (Address addr = getSP().addOffsetTo(-5 * VM.getVM().getAddressSize());
 514            AddressOps.lte(addr, getFP().addOffsetTo(5 * VM.getVM().getAddressSize()));
 515            addr = addr.addOffsetTo(VM.getVM().getAddressSize())) {
 516         System.out.println(addr + ": " + addr.getAddressAt(0));
 517       }
 518     } else {
 519       for (Address addr = getSP().addOffsetTo(-5 * VM.getVM().getAddressSize());
 520            AddressOps.lte(addr, getSP().addOffsetTo(20 * VM.getVM().getAddressSize()));
 521            addr = addr.addOffsetTo(VM.getVM().getAddressSize())) {
 522         System.out.println(addr + ": " + addr.getAddressAt(0));
 523       }
 524     }
 525   }
 526 }