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