1 /*
   2  * Copyright 1998-2007 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 // CodeBlob - superclass for all entries in the CodeCache.
  26 //
  27 // Suptypes are:
  28 //   nmethod            : Compiled Java methods (include method that calls to native code)
  29 //   RuntimeStub        : Call to VM runtime methods
  30 //   DeoptimizationBlob : Used for deoptimizatation
  31 //   ExceptionBlob      : Used for stack unrolling
  32 //   SafepointBlob      : Used to handle illegal instruction exceptions
  33 //
  34 //
  35 // Layout:
  36 //   - header
  37 //   - relocation
  38 //   - instruction space
  39 //   - data space
  40 class DeoptimizationBlob;
  41 
  42 class CodeBlob VALUE_OBJ_CLASS_SPEC {
  43 
  44   friend class VMStructs;
  45 
  46  private:
  47   const char* _name;
  48   int        _size;                              // total size of CodeBlob in bytes
  49   int        _header_size;                       // size of header (depends on subclass)
  50   int        _relocation_size;                   // size of relocation
  51   int        _instructions_offset;               // offset to where instructions region begins
  52   int        _frame_complete_offset;             // instruction offsets in [0.._frame_complete_offset) have
  53                                                  // not finished setting up their frame. Beware of pc's in
  54                                                  // that range. There is a similar range(s) on returns
  55                                                  // which we don't detect.
  56   int        _data_offset;                       // offset to where data region begins
  57   int        _oops_offset;                       // offset to where embedded oop table begins (inside data)
  58   int        _oops_length;                       // number of embedded oops
  59   int        _frame_size;                        // size of stack frame
  60   OopMapSet* _oop_maps;                          // OopMap for this CodeBlob
  61   CodeComments _comments;
  62 
  63   friend class OopRecorder;
  64 
  65   void fix_oop_relocations(address begin, address end, bool initialize_immediates);
  66   inline void initialize_immediate_oop(oop* dest, jobject handle);
  67 
  68  public:
  69   // Returns the space needed for CodeBlob
  70   static unsigned int allocation_size(CodeBuffer* cb, int header_size);
  71 
  72   // Creation
  73   // a) simple CodeBlob
  74   // frame_complete is the offset from the beginning of the instructions
  75   // to where the frame setup (from stackwalk viewpoint) is complete.
  76   CodeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size);
  77 
  78   // b) full CodeBlob
  79   CodeBlob(
  80     const char* name,
  81     CodeBuffer* cb,
  82     int         header_size,
  83     int         size,
  84     int         frame_complete,
  85     int         frame_size,
  86     OopMapSet*  oop_maps
  87   );
  88 
  89   // Deletion
  90   void flush();
  91 
  92   // Typing
  93   virtual bool is_buffer_blob() const            { return false; }
  94   virtual bool is_nmethod() const                { return false; }
  95   virtual bool is_runtime_stub() const           { return false; }
  96   virtual bool is_deoptimization_stub() const    { return false; }
  97   virtual bool is_uncommon_trap_stub() const     { return false; }
  98   virtual bool is_exception_stub() const         { return false; }
  99   virtual bool is_safepoint_stub() const         { return false; }
 100   virtual bool is_adapter_blob() const           { return false; }
 101 
 102   virtual bool is_compiled_by_c2() const         { return false; }
 103   virtual bool is_compiled_by_c1() const         { return false; }
 104 
 105   // Boundaries
 106   address    header_begin() const                { return (address)    this; }
 107   address    header_end() const                  { return ((address)   this) + _header_size; };
 108   relocInfo* relocation_begin() const            { return (relocInfo*) header_end(); };
 109   relocInfo* relocation_end() const              { return (relocInfo*)(header_end()   + _relocation_size); }
 110   address    instructions_begin() const          { return (address)    header_begin() + _instructions_offset;  }
 111   address    instructions_end() const            { return (address)    header_begin() + _data_offset; }
 112   address    data_begin() const                  { return (address)    header_begin() + _data_offset; }
 113   address    data_end() const                    { return (address)    header_begin() + _size; }
 114   oop*       oops_begin() const                  { return (oop*)      (header_begin() + _oops_offset); }
 115   oop*       oops_end() const                    { return                oops_begin() + _oops_length; }
 116 
 117   // Offsets
 118   int relocation_offset() const                  { return _header_size; }
 119   int instructions_offset() const                { return _instructions_offset; }
 120   int data_offset() const                        { return _data_offset; }
 121   int oops_offset() const                        { return _oops_offset; }
 122 
 123   // Sizes
 124   int size() const                               { return _size; }
 125   int header_size() const                        { return _header_size; }
 126   int relocation_size() const                    { return (address) relocation_end() - (address) relocation_begin(); }
 127   int instructions_size() const                  { return instructions_end() - instructions_begin();  }
 128   int data_size() const                          { return data_end() - data_begin(); }
 129   int oops_size() const                          { return (address) oops_end() - (address) oops_begin(); }
 130 
 131   // Containment
 132   bool blob_contains(address addr) const         { return header_begin()       <= addr && addr < data_end(); }
 133   bool relocation_contains(relocInfo* addr) const{ return relocation_begin()   <= addr && addr < relocation_end(); }
 134   bool instructions_contains(address addr) const { return instructions_begin() <= addr && addr < instructions_end(); }
 135   bool data_contains(address addr) const         { return data_begin()         <= addr && addr < data_end(); }
 136   bool oops_contains(oop* addr) const            { return oops_begin()         <= addr && addr < oops_end(); }
 137   bool contains(address addr) const              { return instructions_contains(addr); }
 138   bool is_frame_complete_at(address addr) const  { return instructions_contains(addr) &&
 139                                                           addr >= instructions_begin() + _frame_complete_offset; }
 140 
 141   // Relocation support
 142   void fix_oop_relocations(address begin, address end) {
 143     fix_oop_relocations(begin, end, false);
 144   }
 145   void fix_oop_relocations() {
 146     fix_oop_relocations(NULL, NULL, false);
 147   }
 148   relocInfo::relocType reloc_type_for_address(address pc);
 149   bool is_at_poll_return(address pc);
 150   bool is_at_poll_or_poll_return(address pc);
 151 
 152   // Support for oops in scopes and relocs:
 153   // Note: index 0 is reserved for null.
 154   oop  oop_at(int index) const                   { return index == 0? (oop)NULL: *oop_addr_at(index); }
 155   oop* oop_addr_at(int index) const{             // for GC
 156     // relocation indexes are biased by 1 (because 0 is reserved)
 157     assert(index > 0 && index <= _oops_length, "must be a valid non-zero index");
 158     return &oops_begin()[index-1];
 159   }
 160 
 161   void copy_oops(GrowableArray<jobject>* oops);
 162 
 163   // CodeCache support: really only used by the nmethods, but in order to get
 164   // asserts and certain bookkeeping to work in the CodeCache they are defined
 165   // virtual here.
 166   virtual bool is_zombie() const                 { return false; }
 167   virtual bool is_locked_by_vm() const           { return false; }
 168 
 169   virtual bool is_unloaded() const               { return false; }
 170   virtual bool is_not_entrant() const            { return false; }
 171 
 172   // GC support
 173   virtual bool is_alive() const                  = 0;
 174   virtual void do_unloading(BoolObjectClosure* is_alive,
 175                             OopClosure* keep_alive,
 176                             bool unloading_occurred);
 177   virtual void oops_do(OopClosure* f) = 0;
 178   // (All CodeBlob subtypes other than NMethod currently have
 179   // an empty oops_do() method.
 180 
 181   // OopMap for frame
 182   OopMapSet* oop_maps() const                    { return _oop_maps; }
 183   void set_oop_maps(OopMapSet* p);
 184   OopMap* oop_map_for_return_address(address return_address);
 185   virtual void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { ShouldNotReachHere(); }
 186 
 187   // Frame support
 188   int  frame_size() const                        { return _frame_size; }
 189   void set_frame_size(int size)                  { _frame_size = size; }
 190 
 191   // Returns true, if the next frame is responsible for GC'ing oops passed as arguments
 192   virtual bool caller_must_gc_arguments(JavaThread* thread) const { return false; }
 193 
 194   // Naming
 195   const char* name() const                       { return _name; }
 196   void set_name(const char* name)                { _name = name; }
 197 
 198   // Debugging
 199   virtual void verify();
 200   virtual void print() const                     PRODUCT_RETURN;
 201   virtual void print_value_on(outputStream* st) const PRODUCT_RETURN;
 202 
 203   // Print the comment associated with offset on stream, if there is one
 204   void print_block_comment(outputStream* stream, intptr_t offset) {
 205     _comments.print_block_comment(stream, offset);
 206   }
 207 
 208   // Transfer ownership of comments to this CodeBlob
 209   void set_comments(CodeComments& comments) {
 210     _comments.assign(comments);
 211   }
 212 };
 213 
 214 
 215 //----------------------------------------------------------------------------------------------------
 216 // BufferBlob: used to hold non-relocatable machine code such as the interpreter, stubroutines, etc.
 217 
 218 class BufferBlob: public CodeBlob {
 219   friend class VMStructs;
 220  private:
 221   // Creation support
 222   BufferBlob(const char* name, int size);
 223   BufferBlob(const char* name, int size, CodeBuffer* cb);
 224 
 225   void* operator new(size_t s, unsigned size);
 226 
 227  public:
 228   // Creation
 229   static BufferBlob* create(const char* name, int buffer_size);
 230   static BufferBlob* create(const char* name, CodeBuffer* cb);
 231 
 232   static void free(BufferBlob* buf);
 233 
 234   // Typing
 235   bool is_buffer_blob() const                    { return true; }
 236   bool is_adapter_blob() const;
 237 
 238   // GC/Verification support
 239   void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
 240   bool is_alive() const                          { return true; }
 241   void do_unloading(BoolObjectClosure* is_alive,
 242                     OopClosure* keep_alive,
 243                     bool unloading_occurred)     { /* do nothing */ }
 244 
 245   void oops_do(OopClosure* f)                    { /* do nothing*/ }
 246 
 247   void verify();
 248   void print() const                             PRODUCT_RETURN;
 249   void print_value_on(outputStream* st) const    PRODUCT_RETURN;
 250 };
 251 
 252 
 253 //----------------------------------------------------------------------------------------------------
 254 // RuntimeStub: describes stubs used by compiled code to call a (static) C++ runtime routine
 255 
 256 class RuntimeStub: public CodeBlob {
 257   friend class VMStructs;
 258  private:
 259   bool        _caller_must_gc_arguments;
 260 
 261   // Creation support
 262   RuntimeStub(
 263     const char* name,
 264     CodeBuffer* cb,
 265     int         size,
 266     int         frame_complete,
 267     int         frame_size,
 268     OopMapSet*  oop_maps,
 269     bool        caller_must_gc_arguments
 270   );
 271 
 272   void* operator new(size_t s, unsigned size);
 273 
 274  public:
 275   // Creation
 276   static RuntimeStub* new_runtime_stub(
 277     const char* stub_name,
 278     CodeBuffer* cb,
 279     int         frame_complete,
 280     int         frame_size,
 281     OopMapSet*  oop_maps,
 282     bool        caller_must_gc_arguments
 283   );
 284 
 285   // Typing
 286   bool is_runtime_stub() const                   { return true; }
 287 
 288   // GC support
 289   bool caller_must_gc_arguments(JavaThread* thread) const { return _caller_must_gc_arguments; }
 290 
 291   address entry_point()                          { return instructions_begin(); }
 292 
 293   // GC/Verification support
 294   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f)  { /* nothing to do */ }
 295   bool is_alive() const                          { return true; }
 296   void do_unloading(BoolObjectClosure* is_alive,
 297                     OopClosure* keep_alive,
 298                     bool unloading_occurred)     { /* do nothing */ }
 299   void oops_do(OopClosure* f) { /* do-nothing*/ }
 300 
 301   void verify();
 302   void print() const                             PRODUCT_RETURN;
 303   void print_value_on(outputStream* st) const    PRODUCT_RETURN;
 304 };
 305 
 306 
 307 //----------------------------------------------------------------------------------------------------
 308 // Super-class for all blobs that exist in only one instance. Implements default behaviour.
 309 
 310 class SingletonBlob: public CodeBlob {
 311   friend class VMStructs;
 312   public:
 313    SingletonBlob(
 314      const char* name,
 315      CodeBuffer* cb,
 316      int         header_size,
 317      int         size,
 318      int         frame_size,
 319      OopMapSet*  oop_maps
 320    )
 321    : CodeBlob(name, cb, header_size, size, CodeOffsets::frame_never_safe, frame_size, oop_maps)
 322    {};
 323 
 324    bool is_alive() const                         { return true; }
 325    void do_unloading(BoolObjectClosure* is_alive,
 326                      OopClosure* keep_alive,
 327                      bool unloading_occurred)    { /* do-nothing*/ }
 328 
 329    void verify(); // does nothing
 330    void print() const                            PRODUCT_RETURN;
 331    void print_value_on(outputStream* st) const   PRODUCT_RETURN;
 332 };
 333 
 334 
 335 //----------------------------------------------------------------------------------------------------
 336 // DeoptimizationBlob
 337 
 338 class DeoptimizationBlob: public SingletonBlob {
 339   friend class VMStructs;
 340  private:
 341   int _unpack_offset;
 342   int _unpack_with_exception;
 343   int _unpack_with_reexecution;
 344 
 345   int _unpack_with_exception_in_tls;
 346 
 347   // Creation support
 348   DeoptimizationBlob(
 349     CodeBuffer* cb,
 350     int         size,
 351     OopMapSet*  oop_maps,
 352     int         unpack_offset,
 353     int         unpack_with_exception_offset,
 354     int         unpack_with_reexecution_offset,
 355     int         frame_size
 356   );
 357 
 358   void* operator new(size_t s, unsigned size);
 359 
 360  public:
 361   // Creation
 362   static DeoptimizationBlob* create(
 363     CodeBuffer* cb,
 364     OopMapSet*  oop_maps,
 365     int         unpack_offset,
 366     int         unpack_with_exception_offset,
 367     int         unpack_with_reexecution_offset,
 368     int         frame_size
 369   );
 370 
 371   // Typing
 372   bool is_deoptimization_stub() const { return true; }
 373   const DeoptimizationBlob *as_deoptimization_stub() const { return this; }
 374   bool exception_address_is_unpack_entry(address pc) const {
 375     address unpack_pc = unpack();
 376     return (pc == unpack_pc || (pc + frame::pc_return_offset) == unpack_pc);
 377   }
 378 
 379 
 380 
 381 
 382   // GC for args
 383   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* Nothing to do */ }
 384 
 385   // Iteration
 386   void oops_do(OopClosure* f) {}
 387 
 388   // Printing
 389   void print_value_on(outputStream* st) const PRODUCT_RETURN;
 390 
 391   address unpack() const                         { return instructions_begin() + _unpack_offset;           }
 392   address unpack_with_exception() const          { return instructions_begin() + _unpack_with_exception;   }
 393   address unpack_with_reexecution() const        { return instructions_begin() + _unpack_with_reexecution; }
 394 
 395   // Alternate entry point for C1 where the exception and issuing pc
 396   // are in JavaThread::_exception_oop and JavaThread::_exception_pc
 397   // instead of being in registers.  This is needed because C1 doesn't
 398   // model exception paths in a way that keeps these registers free so
 399   // there may be live values in those registers during deopt.
 400   void set_unpack_with_exception_in_tls_offset(int offset) {
 401     _unpack_with_exception_in_tls = offset;
 402     assert(contains(instructions_begin() + _unpack_with_exception_in_tls), "must be PC inside codeblob");
 403   }
 404   address unpack_with_exception_in_tls() const   { return instructions_begin() + _unpack_with_exception_in_tls;   }
 405 };
 406 
 407 
 408 //----------------------------------------------------------------------------------------------------
 409 // UncommonTrapBlob (currently only used by Compiler 2)
 410 
 411 #ifdef COMPILER2
 412 
 413 class UncommonTrapBlob: public SingletonBlob {
 414   friend class VMStructs;
 415  private:
 416   // Creation support
 417   UncommonTrapBlob(
 418     CodeBuffer* cb,
 419     int         size,
 420     OopMapSet*  oop_maps,
 421     int         frame_size
 422   );
 423 
 424   void* operator new(size_t s, unsigned size);
 425 
 426  public:
 427   // Creation
 428   static UncommonTrapBlob* create(
 429     CodeBuffer* cb,
 430     OopMapSet*  oop_maps,
 431     int         frame_size
 432   );
 433 
 434   // GC for args
 435   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f)  { /* nothing to do */ }
 436 
 437   // Typing
 438   bool is_uncommon_trap_stub() const             { return true; }
 439 
 440   // Iteration
 441   void oops_do(OopClosure* f) {}
 442 };
 443 
 444 
 445 //----------------------------------------------------------------------------------------------------
 446 // ExceptionBlob: used for exception unwinding in compiled code (currently only used by Compiler 2)
 447 
 448 class ExceptionBlob: public SingletonBlob {
 449   friend class VMStructs;
 450  private:
 451   // Creation support
 452   ExceptionBlob(
 453     CodeBuffer* cb,
 454     int         size,
 455     OopMapSet*  oop_maps,
 456     int         frame_size
 457   );
 458 
 459   void* operator new(size_t s, unsigned size);
 460 
 461  public:
 462   // Creation
 463   static ExceptionBlob* create(
 464     CodeBuffer* cb,
 465     OopMapSet*  oop_maps,
 466     int         frame_size
 467   );
 468 
 469   // GC for args
 470   void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
 471 
 472   // Typing
 473   bool is_exception_stub() const                 { return true; }
 474 
 475   // Iteration
 476   void oops_do(OopClosure* f) {}
 477 };
 478 #endif // COMPILER2
 479 
 480 
 481 //----------------------------------------------------------------------------------------------------
 482 // SafepointBlob: handles illegal_instruction exceptions during a safepoint
 483 
 484 class SafepointBlob: public SingletonBlob {
 485   friend class VMStructs;
 486  private:
 487   // Creation support
 488   SafepointBlob(
 489     CodeBuffer* cb,
 490     int         size,
 491     OopMapSet*  oop_maps,
 492     int         frame_size
 493   );
 494 
 495   void* operator new(size_t s, unsigned size);
 496 
 497  public:
 498   // Creation
 499   static SafepointBlob* create(
 500     CodeBuffer* cb,
 501     OopMapSet*  oop_maps,
 502     int         frame_size
 503   );
 504 
 505   // GC for args
 506   void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
 507 
 508   // Typing
 509   bool is_safepoint_stub() const                 { return true; }
 510 
 511   // Iteration
 512   void oops_do(OopClosure* f) {}
 513 };