1 /*
   2  * Copyright (c) 1997, 2011, 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 #ifndef SHARE_VM_RUNTIME_FRAME_HPP
  26 #define SHARE_VM_RUNTIME_FRAME_HPP
  27 
  28 #include "asm/assembler.hpp"
  29 #include "oops/methodOop.hpp"
  30 #include "runtime/basicLock.hpp"
  31 #include "runtime/monitorChunk.hpp"
  32 #include "runtime/registerMap.hpp"
  33 #include "utilities/top.hpp"
  34 #ifdef COMPILER2
  35 #ifdef TARGET_ARCH_MODEL_x86_32
  36 # include "adfiles/adGlobals_x86_32.hpp"
  37 #endif
  38 #ifdef TARGET_ARCH_MODEL_x86_64
  39 # include "adfiles/adGlobals_x86_64.hpp"
  40 #endif
  41 #ifdef TARGET_ARCH_MODEL_sparc
  42 # include "adfiles/adGlobals_sparc.hpp"
  43 #endif
  44 #ifdef TARGET_ARCH_MODEL_zero
  45 # include "adfiles/adGlobals_zero.hpp"
  46 #endif
  47 #ifdef TARGET_ARCH_MODEL_arm
  48 # include "adfiles/adGlobals_arm.hpp"
  49 #endif
  50 #ifdef TARGET_ARCH_MODEL_ppc
  51 # include "adfiles/adGlobals_ppc.hpp"
  52 #endif
  53 #endif
  54 #ifdef ZERO
  55 #ifdef TARGET_ARCH_zero
  56 # include "stack_zero.hpp"
  57 #endif
  58 #endif
  59 
  60 typedef class BytecodeInterpreter* interpreterState;
  61 
  62 class CodeBlob;
  63 class vframeArray;
  64 
  65 
  66 // A frame represents a physical stack frame (an activation).  Frames
  67 // can be C or Java frames, and the Java frames can be interpreted or
  68 // compiled.  In contrast, vframes represent source-level activations,
  69 // so that one physical frame can correspond to multiple source level
  70 // frames because of inlining.
  71 
  72 class frame VALUE_OBJ_CLASS_SPEC {
  73  private:
  74   // Instance variables:
  75   intptr_t* _sp; // stack pointer (from Thread::last_Java_sp)
  76   address   _pc; // program counter (the next instruction after the call)
  77 
  78   CodeBlob* _cb; // CodeBlob that "owns" pc
  79   enum deopt_state {
  80     not_deoptimized,
  81     is_deoptimized,
  82     unknown
  83   };
  84 
  85   deopt_state _deopt_state;
  86 
  87  public:
  88   // Constructors
  89   frame();
  90 
  91   // Accessors
  92 
  93   // pc: Returns the pc at which this frame will continue normally.
  94   // It must point at the beginning of the next instruction to execute.
  95   address pc() const             { return _pc; }
  96 
  97   // This returns the pc that if you were in the debugger you'd see. Not
  98   // the idealized value in the frame object. This undoes the magic conversion
  99   // that happens for deoptimized frames. In addition it makes the value the
 100   // hardware would want to see in the native frame. The only user (at this point)
 101   // is deoptimization. It likely no one else should ever use it.
 102   address raw_pc() const;
 103 
 104   void set_pc( address   newpc );
 105 
 106   intptr_t* sp() const           { return _sp; }
 107   void set_sp( intptr_t* newsp ) { _sp = newsp; }
 108 
 109 
 110   CodeBlob* cb() const           { return _cb; }
 111 
 112   // patching operations
 113   void   patch_pc(Thread* thread, address pc);
 114 
 115   // Every frame needs to return a unique id which distinguishes it from all other frames.
 116   // For sparc and ia32 use sp. ia64 can have memory frames that are empty so multiple frames
 117   // will have identical sp values. For ia64 the bsp (fp) value will serve. No real frame
 118   // should have an id() of NULL so it is a distinguishing value for an unmatchable frame.
 119   // We also have relationals which allow comparing a frame to anoth frame's id() allow
 120   // us to distinguish younger (more recent activation) from older (less recent activations)
 121   // A NULL id is only valid when comparing for equality.
 122 
 123   intptr_t* id(void) const;
 124   bool is_younger(intptr_t* id) const;
 125   bool is_older(intptr_t* id) const;
 126 
 127   // testers
 128 
 129   // Compares for strict equality. Rarely used or needed.
 130   // It can return a different result than f1.id() == f2.id()
 131   bool equal(frame other) const;
 132 
 133   // type testers
 134   bool is_interpreted_frame()    const;
 135   bool is_java_frame()           const;
 136   bool is_entry_frame()          const;             // Java frame called from C?
 137   bool is_native_frame()         const;
 138   bool is_runtime_frame()        const;
 139   bool is_compiled_frame()       const;
 140   bool is_safepoint_blob_frame() const;
 141   bool is_deoptimized_frame()    const;
 142 
 143   // testers
 144   bool is_first_frame() const; // oldest frame? (has no sender)
 145   bool is_first_java_frame() const;              // same for Java frame
 146 
 147   bool is_interpreted_frame_valid(JavaThread* thread) const;       // performs sanity checks on interpreted frames.
 148 
 149   // tells whether this frame is marked for deoptimization
 150   bool should_be_deoptimized() const;
 151 
 152   // tells whether this frame can be deoptimized
 153   bool can_be_deoptimized() const;
 154 
 155   // returns the frame size in stack slots
 156   int frame_size(RegisterMap* map) const;
 157 
 158   // returns the sending frame
 159   frame sender(RegisterMap* map) const;
 160 
 161   // for Profiling - acting on another frame. walks sender frames
 162   // if valid.
 163   frame profile_find_Java_sender_frame(JavaThread *thread);
 164   bool safe_for_sender(JavaThread *thread);
 165 
 166   // returns the sender, but skips conversion frames
 167   frame real_sender(RegisterMap* map) const;
 168 
 169   // returns the the sending Java frame, skipping any intermediate C frames
 170   // NB: receiver must not be first frame
 171   frame java_sender() const;
 172 
 173  private:
 174   // Helper methods for better factored code in frame::sender
 175   frame sender_for_compiled_frame(RegisterMap* map) const;
 176   frame sender_for_entry_frame(RegisterMap* map) const;
 177   frame sender_for_interpreter_frame(RegisterMap* map) const;
 178   frame sender_for_native_frame(RegisterMap* map) const;
 179 
 180   // All frames:
 181 
 182   // A low-level interface for vframes:
 183 
 184  public:
 185 
 186   intptr_t* addr_at(int index) const             { return &fp()[index];    }
 187   intptr_t  at(int index) const                  { return *addr_at(index); }
 188 
 189   // accessors for locals
 190   oop obj_at(int offset) const                   { return *obj_at_addr(offset);  }
 191   void obj_at_put(int offset, oop value)         { *obj_at_addr(offset) = value; }
 192 
 193   jint int_at(int offset) const                  { return *int_at_addr(offset);  }
 194   void int_at_put(int offset, jint value)        { *int_at_addr(offset) = value; }
 195 
 196   oop*      obj_at_addr(int offset) const        { return (oop*)     addr_at(offset); }
 197 
 198   oop*      adjusted_obj_at_addr(methodOop method, int index) { return obj_at_addr(adjust_offset(method, index)); }
 199 
 200  private:
 201   jint*    int_at_addr(int offset) const         { return (jint*)    addr_at(offset); }
 202 
 203  public:
 204   // Link (i.e., the pointer to the previous frame)
 205   intptr_t* link() const;
 206   void set_link(intptr_t* addr);
 207 
 208   // Return address
 209   address  sender_pc() const;
 210 
 211   // Support for deoptimization
 212   void deoptimize(JavaThread* thread);
 213 
 214   // The frame's original SP, before any extension by an interpreted callee;
 215   // used for packing debug info into vframeArray objects and vframeArray lookup.
 216   intptr_t* unextended_sp() const;
 217 
 218   // returns the stack pointer of the calling frame
 219   intptr_t* sender_sp() const;
 220 
 221 
 222   // Interpreter frames:
 223 
 224  private:
 225   intptr_t** interpreter_frame_locals_addr() const;
 226   intptr_t*  interpreter_frame_bcx_addr() const;
 227   intptr_t*  interpreter_frame_mdx_addr() const;
 228 
 229  public:
 230   // Locals
 231 
 232   // The _at version returns a pointer because the address is used for GC.
 233   intptr_t* interpreter_frame_local_at(int index) const;
 234 
 235   void interpreter_frame_set_locals(intptr_t* locs);
 236 
 237   // byte code index/pointer (use these functions for unchecked frame access only!)
 238   intptr_t interpreter_frame_bcx() const                  { return *interpreter_frame_bcx_addr(); }
 239   void interpreter_frame_set_bcx(intptr_t bcx);
 240 
 241   // byte code index
 242   jint interpreter_frame_bci() const;
 243   void interpreter_frame_set_bci(jint bci);
 244 
 245   // byte code pointer
 246   address interpreter_frame_bcp() const;
 247   void    interpreter_frame_set_bcp(address bcp);
 248 
 249   // Unchecked access to the method data index/pointer.
 250   // Only use this if you know what you are doing.
 251   intptr_t interpreter_frame_mdx() const                  { return *interpreter_frame_mdx_addr(); }
 252   void interpreter_frame_set_mdx(intptr_t mdx);
 253 
 254   // method data pointer
 255   address interpreter_frame_mdp() const;
 256   void    interpreter_frame_set_mdp(address dp);
 257 
 258   // Find receiver out of caller's (compiled) argument list
 259   oop retrieve_receiver(RegisterMap *reg_map);
 260 
 261   // Return the monitor owner and BasicLock for compiled synchronized
 262   // native methods so that biased locking can revoke the receiver's
 263   // bias if necessary.  This is also used by JVMTI's GetLocalInstance method
 264   // (via VM_GetReceiver) to retrieve the receiver from a native wrapper frame.
 265   BasicLock* get_native_monitor();
 266   oop        get_native_receiver();
 267 
 268   // Find receiver for an invoke when arguments are just pushed on stack (i.e., callee stack-frame is
 269   // not setup)
 270   oop interpreter_callee_receiver(Symbol* signature)     { return *interpreter_callee_receiver_addr(signature); }
 271 
 272 
 273   oop* interpreter_callee_receiver_addr(Symbol* signature);
 274 
 275 
 276   // expression stack (may go up or down, direction == 1 or -1)
 277  public:
 278   intptr_t* interpreter_frame_expression_stack() const;
 279   static  jint  interpreter_frame_expression_stack_direction();
 280 
 281   // The _at version returns a pointer because the address is used for GC.
 282   intptr_t* interpreter_frame_expression_stack_at(jint offset) const;
 283 
 284   // top of expression stack
 285   intptr_t* interpreter_frame_tos_at(jint offset) const;
 286   intptr_t* interpreter_frame_tos_address() const;
 287 
 288 
 289   jint  interpreter_frame_expression_stack_size() const;
 290 
 291   intptr_t* interpreter_frame_sender_sp() const;
 292 
 293 #ifndef CC_INTERP
 294   // template based interpreter deoptimization support
 295   void  set_interpreter_frame_sender_sp(intptr_t* sender_sp);
 296   void interpreter_frame_set_monitor_end(BasicObjectLock* value);
 297 #endif // CC_INTERP
 298 
 299   // BasicObjectLocks:
 300   //
 301   // interpreter_frame_monitor_begin is higher in memory than interpreter_frame_monitor_end
 302   // Interpreter_frame_monitor_begin points to one element beyond the oldest one,
 303   // interpreter_frame_monitor_end   points to the youngest one, or if there are none,
 304   //                                 it points to one beyond where the first element will be.
 305   // interpreter_frame_monitor_size  reports the allocation size of a monitor in the interpreter stack.
 306   //                                 this value is >= BasicObjectLock::size(), and may be rounded up
 307 
 308   BasicObjectLock* interpreter_frame_monitor_begin() const;
 309   BasicObjectLock* interpreter_frame_monitor_end()   const;
 310   BasicObjectLock* next_monitor_in_interpreter_frame(BasicObjectLock* current) const;
 311   BasicObjectLock* previous_monitor_in_interpreter_frame(BasicObjectLock* current) const;
 312   static int interpreter_frame_monitor_size();
 313 
 314   void interpreter_frame_verify_monitor(BasicObjectLock* value) const;
 315 
 316   // Tells whether the current interpreter_frame frame pointer
 317   // corresponds to the old compiled/deoptimized fp
 318   // The receiver used to be a top level frame
 319   bool interpreter_frame_equals_unpacked_fp(intptr_t* fp);
 320 
 321   // Return/result value from this interpreter frame
 322   // If the method return type is T_OBJECT or T_ARRAY populates oop_result
 323   // For other (non-T_VOID) the appropriate field in the jvalue is populated
 324   // with the result value.
 325   // Should only be called when at method exit when the method is not
 326   // exiting due to an exception.
 327   BasicType interpreter_frame_result(oop* oop_result, jvalue* value_result);
 328 
 329  public:
 330   // Method & constant pool cache
 331   methodOop interpreter_frame_method() const;
 332   void interpreter_frame_set_method(methodOop method);
 333   methodOop* interpreter_frame_method_addr() const;
 334   constantPoolCacheOop* interpreter_frame_cache_addr() const;
 335 #ifdef PPC
 336   oop* interpreter_frame_mirror_addr() const;
 337 #endif
 338 
 339  public:
 340   // Entry frames
 341   JavaCallWrapper* entry_frame_call_wrapper() const;
 342   intptr_t* entry_frame_argument_at(int offset) const;
 343 
 344   // tells whether there is another chunk of Delta stack above
 345   bool entry_frame_is_first() const;
 346 
 347   // Compiled frames:
 348 
 349  public:
 350   // Given the index of a local, and the number of argument words
 351   // in this stack frame, tell which word of the stack frame to find
 352   // the local in.  Arguments are stored above the ofp/rpc pair,
 353   // while other locals are stored below it.
 354   // Since monitors (BasicLock blocks) are also assigned indexes,
 355   // but may have different storage requirements, their presence
 356   // can also affect the calculation of offsets.
 357   static int local_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors);
 358 
 359   // Given the index of a monitor, etc., tell which word of the
 360   // stack frame contains the start of the BasicLock block.
 361   // Note that the local index by convention is the __higher__
 362   // of the two indexes allocated to the block.
 363   static int monitor_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors);
 364 
 365   // Tell the smallest value that local_offset_for_compiler will attain.
 366   // This is used to help determine how much stack frame to allocate.
 367   static int min_local_offset_for_compiler(int nof_args, int max_nof_locals, int max_nof_monitors);
 368 
 369   // Tells if this register must be spilled during a call.
 370   // On Intel, all registers are smashed by calls.
 371   static bool volatile_across_calls(Register reg);
 372 
 373 
 374   // Safepoints
 375 
 376  public:
 377   oop saved_oop_result(RegisterMap* map) const;
 378   void set_saved_oop_result(RegisterMap* map, oop obj);
 379 
 380   // For debugging
 381  private:
 382   const char* print_name() const;
 383 
 384  public:
 385   void print_value() const { print_value_on(tty,NULL); }
 386   void print_value_on(outputStream* st, JavaThread *thread) const;
 387   void print_on(outputStream* st) const;
 388   void interpreter_frame_print_on(outputStream* st) const;
 389   void print_on_error(outputStream* st, char* buf, int buflen, bool verbose = false) const;
 390 
 391   // Conversion from an VMReg to physical stack location
 392   oop* oopmapreg_to_location(VMReg reg, const RegisterMap* regmap) const;
 393 
 394   // Oops-do's
 395   void oops_compiled_arguments_do(Symbol* signature, bool has_receiver, const RegisterMap* reg_map, OopClosure* f);
 396   void oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache = true);
 397 
 398  private:
 399   void oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f);
 400 
 401   // Iteration of oops
 402   void oops_do_internal(OopClosure* f, CodeBlobClosure* cf, RegisterMap* map, bool use_interpreter_oop_map_cache);
 403   void oops_entry_do(OopClosure* f, const RegisterMap* map);
 404   void oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map);
 405   int adjust_offset(methodOop method, int index); // helper for above fn
 406  public:
 407   // Memory management
 408   void oops_do(OopClosure* f, CodeBlobClosure* cf, RegisterMap* map) { oops_do_internal(f, cf, map, true); }
 409   void nmethods_do(CodeBlobClosure* cf);
 410 
 411   void gc_prologue();
 412   void gc_epilogue();
 413   void pd_gc_epilog();
 414 
 415 # ifdef ENABLE_ZAP_DEAD_LOCALS
 416  private:
 417   class CheckValueClosure: public OopClosure {
 418    public:
 419     void do_oop(oop* p);
 420     void do_oop(narrowOop* p) { ShouldNotReachHere(); }
 421   };
 422   static CheckValueClosure _check_value;
 423 
 424   class CheckOopClosure: public OopClosure {
 425    public:
 426     void do_oop(oop* p);
 427     void do_oop(narrowOop* p) { ShouldNotReachHere(); }
 428   };
 429   static CheckOopClosure _check_oop;
 430 
 431   static void check_derived_oop(oop* base, oop* derived);
 432 
 433   class ZapDeadClosure: public OopClosure {
 434    public:
 435     void do_oop(oop* p);
 436     void do_oop(narrowOop* p) { ShouldNotReachHere(); }
 437   };
 438   static ZapDeadClosure _zap_dead;
 439 
 440  public:
 441   // Zapping
 442   void zap_dead_locals            (JavaThread* thread, const RegisterMap* map);
 443   void zap_dead_interpreted_locals(JavaThread* thread, const RegisterMap* map);
 444   void zap_dead_compiled_locals   (JavaThread* thread, const RegisterMap* map);
 445   void zap_dead_entry_locals      (JavaThread* thread, const RegisterMap* map);
 446   void zap_dead_deoptimized_locals(JavaThread* thread, const RegisterMap* map);
 447 # endif
 448   // Verification
 449   void verify(const RegisterMap* map);
 450   static bool verify_return_pc(address x);
 451   static bool is_bci(intptr_t bcx);
 452   // Usage:
 453   // assert(frame::verify_return_pc(return_address), "must be a return pc");
 454 
 455   int pd_oop_map_offset_adjustment() const;
 456 
 457 #ifdef TARGET_ARCH_x86
 458 # include "frame_x86.hpp"
 459 #endif
 460 #ifdef TARGET_ARCH_sparc
 461 # include "frame_sparc.hpp"
 462 #endif
 463 #ifdef TARGET_ARCH_zero
 464 # include "frame_zero.hpp"
 465 #endif
 466 #ifdef TARGET_ARCH_arm
 467 # include "frame_arm.hpp"
 468 #endif
 469 #ifdef TARGET_ARCH_ppc
 470 # include "frame_ppc.hpp"
 471 #endif
 472 
 473 };
 474 
 475 
 476 //
 477 // StackFrameStream iterates through the frames of a thread starting from
 478 // top most frame. It automatically takes care of updating the location of
 479 // all (callee-saved) registers. Notice: If a thread is stopped at
 480 // a safepoint, all registers are saved, not only the callee-saved ones.
 481 //
 482 // Use:
 483 //
 484 //   for(StackFrameStream fst(thread); !fst.is_done(); fst.next()) {
 485 //     ...
 486 //   }
 487 //
 488 class StackFrameStream : public StackObj {
 489  private:
 490   frame       _fr;
 491   RegisterMap _reg_map;
 492   bool        _is_done;
 493  public:
 494    StackFrameStream(JavaThread *thread, bool update = true);
 495 
 496   // Iteration
 497   bool is_done()                  { return (_is_done) ? true : (_is_done = _fr.is_first_frame(), false); }
 498   void next()                     { if (!_is_done) _fr = _fr.sender(&_reg_map); }
 499 
 500   // Query
 501   frame *current()                { return &_fr; }
 502   RegisterMap* register_map()     { return &_reg_map; }
 503 };
 504 
 505 #endif // SHARE_VM_RUNTIME_FRAME_HPP