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