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