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