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