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