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