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   BufferBlob(const char* name, int size, CodeBuffer* cb, int frame_complete, int frame_size, OopMapSet* oop_maps);
 239 
 240   void* operator new(size_t s, unsigned size) throw();
 241 
 242  public:
 243   // Creation
 244   static BufferBlob* create(const char* name, int buffer_size);
 245   static BufferBlob* create(const char* name, CodeBuffer* cb);
 246 
 247   static void free(BufferBlob* buf);
 248 
 249   // Typing
 250   virtual bool is_buffer_blob() const            { return true; }
 251 
 252   // GC/Verification support
 253   void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
 254   bool is_alive() const                          { return true; }
 255 
 256   void verify();
 257   void print_on(outputStream* st) const;
 258   void print_value_on(outputStream* st) const;
 259 };
 260 
 261 
 262 //----------------------------------------------------------------------------------------------------
 263 // AdapterBlob: used to hold C2I/I2C adapters
 264 
 265 class AdapterBlob: public BufferBlob {
 266 private:
 267   AdapterBlob(int size, CodeBuffer* cb, int frame_complete, int frame_size, OopMapSet* oop_maps);
 268 
 269 public:
 270   // Creation
 271   static AdapterBlob* create(CodeBuffer* cb,
 272                              int frame_complete,
 273                              int frame_size,
 274                              OopMapSet* oop_maps);
 275 
 276   // Typing
 277   virtual bool is_adapter_blob() const { return true; }
 278 
 279   bool caller_must_gc_arguments(JavaThread* thread) const { return true; }
 280 };
 281 
 282 
 283 //----------------------------------------------------------------------------------------------------
 284 // MethodHandlesAdapterBlob: used to hold MethodHandles adapters
 285 
 286 class MethodHandlesAdapterBlob: public BufferBlob {
 287 private:
 288   MethodHandlesAdapterBlob(int size)                 : BufferBlob("MethodHandles adapters", size) {}
 289 
 290 public:
 291   // Creation
 292   static MethodHandlesAdapterBlob* create(int buffer_size);
 293 
 294   // Typing
 295   virtual bool is_method_handles_adapter_blob() const { return true; }
 296 };
 297 
 298 
 299 //----------------------------------------------------------------------------------------------------
 300 // RuntimeStub: describes stubs used by compiled code to call a (static) C++ runtime routine
 301 
 302 class RuntimeStub: public CodeBlob {
 303   friend class VMStructs;
 304  private:
 305   bool        _caller_must_gc_arguments;
 306 
 307   // Creation support
 308   RuntimeStub(
 309     const char* name,
 310     CodeBuffer* cb,
 311     int         size,
 312     int         frame_complete,
 313     int         frame_size,
 314     OopMapSet*  oop_maps,
 315     bool        caller_must_gc_arguments
 316   );
 317 
 318   void* operator new(size_t s, unsigned size) throw();
 319 
 320  public:
 321   // Creation
 322   static RuntimeStub* new_runtime_stub(
 323     const char* stub_name,
 324     CodeBuffer* cb,
 325     int         frame_complete,
 326     int         frame_size,
 327     OopMapSet*  oop_maps,
 328     bool        caller_must_gc_arguments
 329   );
 330 
 331   // Typing
 332   bool is_runtime_stub() const                   { return true; }
 333 
 334   // GC support
 335   bool caller_must_gc_arguments(JavaThread* thread) const { return _caller_must_gc_arguments; }
 336 
 337   address entry_point()                          { return code_begin(); }
 338 
 339   // GC/Verification support
 340   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f)  { /* nothing to do */ }
 341   bool is_alive() const                          { return true; }
 342 
 343   void verify();
 344   void print_on(outputStream* st) const;
 345   void print_value_on(outputStream* st) const;
 346 };
 347 
 348 
 349 //----------------------------------------------------------------------------------------------------
 350 // Super-class for all blobs that exist in only one instance. Implements default behaviour.
 351 
 352 class SingletonBlob: public CodeBlob {
 353   friend class VMStructs;
 354 
 355  protected:
 356   void* operator new(size_t s, unsigned size) throw();
 357 
 358  public:
 359    SingletonBlob(
 360      const char* name,
 361      CodeBuffer* cb,
 362      int         header_size,
 363      int         size,
 364      int         frame_size,
 365      OopMapSet*  oop_maps
 366    )
 367    : CodeBlob(name, cb, header_size, size, CodeOffsets::frame_never_safe, frame_size, oop_maps)
 368   {};
 369 
 370   address entry_point()                          { return code_begin(); }
 371 
 372   bool is_alive() const                          { return true; }
 373 
 374   void verify(); // does nothing
 375   void print_on(outputStream* st) const;
 376   void print_value_on(outputStream* st) const;
 377 };
 378 
 379 
 380 //----------------------------------------------------------------------------------------------------
 381 // DeoptimizationBlob
 382 
 383 class DeoptimizationBlob: public SingletonBlob {
 384   friend class VMStructs;
 385   friend class JVMCIVMStructs;
 386  private:
 387   int _unpack_offset;
 388   int _unpack_with_exception;
 389   int _unpack_with_reexecution;
 390 
 391   int _unpack_with_exception_in_tls;
 392 
 393 #if INCLUDE_JVMCI
 394   // Offsets when JVMCI calls uncommon_trap.
 395   int _uncommon_trap_offset;
 396   int _implicit_exception_uncommon_trap_offset;
 397 #endif
 398 
 399   // Creation support
 400   DeoptimizationBlob(
 401     CodeBuffer* cb,
 402     int         size,
 403     OopMapSet*  oop_maps,
 404     int         unpack_offset,
 405     int         unpack_with_exception_offset,
 406     int         unpack_with_reexecution_offset,
 407     int         frame_size
 408   );
 409 
 410  public:
 411   // Creation
 412   static DeoptimizationBlob* create(
 413     CodeBuffer* cb,
 414     OopMapSet*  oop_maps,
 415     int         unpack_offset,
 416     int         unpack_with_exception_offset,
 417     int         unpack_with_reexecution_offset,
 418     int         frame_size
 419   );
 420 
 421   // Typing
 422   bool is_deoptimization_stub() const { return true; }
 423   bool exception_address_is_unpack_entry(address pc) const {
 424     address unpack_pc = unpack();
 425     return (pc == unpack_pc || (pc + frame::pc_return_offset) == unpack_pc);
 426   }
 427 
 428   // GC for args
 429   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* Nothing to do */ }
 430 
 431   // Printing
 432   void print_value_on(outputStream* st) const;
 433 
 434   address unpack() const                         { return code_begin() + _unpack_offset;           }
 435   address unpack_with_exception() const          { return code_begin() + _unpack_with_exception;   }
 436   address unpack_with_reexecution() const        { return code_begin() + _unpack_with_reexecution; }
 437 
 438   // Alternate entry point for C1 where the exception and issuing pc
 439   // are in JavaThread::_exception_oop and JavaThread::_exception_pc
 440   // instead of being in registers.  This is needed because C1 doesn't
 441   // model exception paths in a way that keeps these registers free so
 442   // there may be live values in those registers during deopt.
 443   void set_unpack_with_exception_in_tls_offset(int offset) {
 444     _unpack_with_exception_in_tls = offset;
 445     assert(code_contains(code_begin() + _unpack_with_exception_in_tls), "must be PC inside codeblob");
 446   }
 447   address unpack_with_exception_in_tls() const   { return code_begin() + _unpack_with_exception_in_tls; }
 448 
 449 #if INCLUDE_JVMCI
 450   // Offsets when JVMCI calls uncommon_trap.
 451   void set_uncommon_trap_offset(int offset) {
 452     _uncommon_trap_offset = offset;
 453     assert(contains(code_begin() + _uncommon_trap_offset), "must be PC inside codeblob");
 454   }
 455   address uncommon_trap() const                  { return code_begin() + _uncommon_trap_offset; }
 456 
 457   void set_implicit_exception_uncommon_trap_offset(int offset) {
 458     _implicit_exception_uncommon_trap_offset = offset;
 459     assert(contains(code_begin() + _implicit_exception_uncommon_trap_offset), "must be PC inside codeblob");
 460   }
 461   address implicit_exception_uncommon_trap() const { return code_begin() + _implicit_exception_uncommon_trap_offset; }
 462 #endif // INCLUDE_JVMCI
 463 };
 464 
 465 
 466 //----------------------------------------------------------------------------------------------------
 467 // UncommonTrapBlob (currently only used by Compiler 2)
 468 
 469 #ifdef COMPILER2
 470 
 471 class UncommonTrapBlob: public SingletonBlob {
 472   friend class VMStructs;
 473  private:
 474   // Creation support
 475   UncommonTrapBlob(
 476     CodeBuffer* cb,
 477     int         size,
 478     OopMapSet*  oop_maps,
 479     int         frame_size
 480   );
 481 
 482  public:
 483   // Creation
 484   static UncommonTrapBlob* create(
 485     CodeBuffer* cb,
 486     OopMapSet*  oop_maps,
 487     int         frame_size
 488   );
 489 
 490   // GC for args
 491   void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f)  { /* nothing to do */ }
 492 
 493   // Typing
 494   bool is_uncommon_trap_stub() const             { return true; }
 495 };
 496 
 497 
 498 //----------------------------------------------------------------------------------------------------
 499 // ExceptionBlob: used for exception unwinding in compiled code (currently only used by Compiler 2)
 500 
 501 class ExceptionBlob: public SingletonBlob {
 502   friend class VMStructs;
 503  private:
 504   // Creation support
 505   ExceptionBlob(
 506     CodeBuffer* cb,
 507     int         size,
 508     OopMapSet*  oop_maps,
 509     int         frame_size
 510   );
 511 
 512  public:
 513   // Creation
 514   static ExceptionBlob* create(
 515     CodeBuffer* cb,
 516     OopMapSet*  oop_maps,
 517     int         frame_size
 518   );
 519 
 520   // GC for args
 521   void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
 522 
 523   // Typing
 524   bool is_exception_stub() const                 { return true; }
 525 };
 526 #endif // COMPILER2
 527 
 528 
 529 //----------------------------------------------------------------------------------------------------
 530 // SafepointBlob: handles illegal_instruction exceptions during a safepoint
 531 
 532 class SafepointBlob: public SingletonBlob {
 533   friend class VMStructs;
 534  private:
 535   // Creation support
 536   SafepointBlob(
 537     CodeBuffer* cb,
 538     int         size,
 539     OopMapSet*  oop_maps,
 540     int         frame_size
 541   );
 542 
 543  public:
 544   // Creation
 545   static SafepointBlob* create(
 546     CodeBuffer* cb,
 547     OopMapSet*  oop_maps,
 548     int         frame_size
 549   );
 550 
 551   // GC for args
 552   void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
 553 
 554   // Typing
 555   bool is_safepoint_stub() const                 { return true; }
 556 };
 557 
 558 #endif // SHARE_VM_CODE_CODEBLOB_HPP