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