1 /*
   2  * Copyright (c) 1997, 2016, 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_STUBS_HPP
  26 #define SHARE_VM_CODE_STUBS_HPP
  27 
  28 #include "asm/codeBuffer.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 // The classes in this file provide a simple framework for the
  32 // management of little pieces of machine code - or stubs -
  33 // created on the fly and frequently discarded. In this frame-
  34 // work stubs are stored in a queue.
  35 
  36 
  37 // Stub serves as abstract base class. A concrete stub
  38 // implementation is a subclass of Stub, implementing
  39 // all (non-virtual!) functions required sketched out
  40 // in the Stub class.
  41 //
  42 // A concrete stub layout may look like this (both data
  43 // and code sections could be empty as well):
  44 //
  45 //                ________
  46 // stub       -->|        | <--+
  47 //               |  data  |    |
  48 //               |________|    |
  49 // code_begin -->|        |    |
  50 //               |        |    |
  51 //               |  code  |    | size
  52 //               |        |    |
  53 //               |________|    |
  54 // code_end   -->|        |    |
  55 //               |  data  |    |
  56 //               |________|    |
  57 //                          <--+
  58 
  59 
  60 class Stub VALUE_OBJ_CLASS_SPEC {
  61  public:
  62   // Initialization/finalization
  63   void    initialize(int size,
  64                      CodeStrings& strings)       { ShouldNotCallThis(); }                // called to initialize/specify the stub's size
  65   void    finalize()                             { ShouldNotCallThis(); }                // called before the stub is deallocated
  66 
  67   // General info/converters
  68   int     size() const                           { ShouldNotCallThis(); return 0; }      // must return the size provided by initialize
  69   static  int code_size_to_size(int code_size)   { ShouldNotCallThis(); return 0; }      // computes the size given the code size
  70 
  71   // Code info
  72   address code_begin() const                     { ShouldNotCallThis(); return NULL; }   // points to the first byte of    the code
  73   address code_end() const                       { ShouldNotCallThis(); return NULL; }   // points to the first byte after the code
  74 
  75   // Debugging
  76   void    verify()                               { ShouldNotCallThis(); }                // verifies the Stub
  77   void    print()                                { ShouldNotCallThis(); }                // prints some information about the stub
  78 };
  79 
  80 
  81 // A stub interface defines the interface between a stub queue
  82 // and the stubs it queues. In order to avoid a vtable and
  83 // (and thus the extra word) in each stub, a concrete stub
  84 // interface object is created and associated with a stub
  85 // buffer which in turn uses the stub interface to interact
  86 // with its stubs.
  87 //
  88 // StubInterface serves as an abstract base class. A concrete
  89 // stub interface implementation is a subclass of StubInterface,
  90 // forwarding its virtual function calls to non-virtual calls
  91 // of the concrete stub (see also macro below). There's exactly
  92 // one stub interface instance required per stub queue.
  93 
  94 class StubInterface: public CHeapObj<mtCode> {
  95  public:
  96   // Initialization/finalization
  97   virtual void    initialize(Stub* self, int size,
  98                              CodeStrings& strings)         = 0; // called after creation (called twice if allocated via (request, commit))
  99   virtual void    finalize(Stub* self)                     = 0; // called before deallocation
 100 
 101   // General info/converters
 102   virtual int     size(Stub* self) const                   = 0; // the total size of the stub in bytes (must be a multiple of CodeEntryAlignment)
 103   virtual int     code_size_to_size(int code_size) const   = 0; // computes the total stub size in bytes given the code size in bytes
 104 
 105   // Code info
 106   virtual address code_begin(Stub* self) const             = 0; // points to the first code byte
 107   virtual address code_end(Stub* self) const               = 0; // points to the first byte after the code
 108 
 109   // Debugging
 110   virtual void    verify(Stub* self)                       = 0; // verifies the stub
 111   virtual void    print(Stub* self)                        = 0; // prints information about the stub
 112 };
 113 
 114 
 115 // DEF_STUB_INTERFACE is used to create a concrete stub interface
 116 // class, forwarding stub interface calls to the corresponding
 117 // stub calls.
 118 
 119 #define DEF_STUB_INTERFACE(stub)                           \
 120   class stub##Interface: public StubInterface {            \
 121    private:                                                \
 122     static stub*    cast(Stub* self)                       { return (stub*)self; }                 \
 123                                                            \
 124    public:                                                 \
 125     /* Initialization/finalization */                      \
 126     virtual void    initialize(Stub* self, int size,       \
 127                                CodeStrings& strings)       { cast(self)->initialize(size, strings); } \
 128     virtual void    finalize(Stub* self)                   { cast(self)->finalize(); }             \
 129                                                            \
 130     /* General info */                                     \
 131     virtual int     size(Stub* self) const                 { return cast(self)->size(); }          \
 132     virtual int     code_size_to_size(int code_size) const { return stub::code_size_to_size(code_size); } \
 133                                                            \
 134     /* Code info */                                        \
 135     virtual address code_begin(Stub* self) const           { return cast(self)->code_begin(); }    \
 136     virtual address code_end(Stub* self) const             { return cast(self)->code_end(); }      \
 137                                                            \
 138     /* Debugging */                                        \
 139     virtual void    verify(Stub* self)                     { cast(self)->verify(); }               \
 140     virtual void    print(Stub* self)                      { cast(self)->print(); }                \
 141   };
 142 
 143 
 144 // A StubQueue maintains a queue of stubs.
 145 // Note: All sizes (spaces) are given in bytes.
 146 
 147 class StubQueue: public CHeapObj<mtCode> {
 148   friend class VMStructs;
 149  private:
 150   StubInterface* _stub_interface;                // the interface prototype
 151   address        _stub_buffer;                   // where all stubs are stored
 152   int            _buffer_size;                   // the buffer size in bytes
 153   int            _buffer_limit;                  // the (byte) index of the actual buffer limit (_buffer_limit <= _buffer_size)
 154   int            _queue_begin;                   // the (byte) index of the first queue entry (word-aligned)
 155   int            _queue_end;                     // the (byte) index of the first entry after the queue (word-aligned)
 156   int            _number_of_stubs;               // the number of buffered stubs
 157   Mutex* const   _mutex;                         // the lock used for a (request, commit) transaction
 158 
 159   void  check_index(int i) const                 { assert(0 <= i && i < _buffer_limit && i % CodeEntryAlignment == 0, "illegal index"); }
 160   bool  is_contiguous() const                    { return _queue_begin <= _queue_end; }
 161   int   index_of(Stub* s) const                  { int i = (address)s - _stub_buffer; check_index(i); return i; }
 162   Stub* stub_at(int i) const                     { check_index(i); return (Stub*)(_stub_buffer + i); }
 163   Stub* current_stub() const                     { return stub_at(_queue_end); }
 164 
 165   // Stub functionality accessed via interface
 166   void  stub_initialize(Stub* s, int size,
 167                         CodeStrings& strings)    { assert(size % CodeEntryAlignment == 0, "size not aligned"); _stub_interface->initialize(s, size, strings); }
 168   void  stub_finalize(Stub* s)                   { _stub_interface->finalize(s); }
 169   int   stub_size(Stub* s) const                 { return _stub_interface->size(s); }
 170   bool  stub_contains(Stub* s, address pc) const { return _stub_interface->code_begin(s) <= pc && pc < _stub_interface->code_end(s); }
 171   int   stub_code_size_to_size(int code_size) const { return _stub_interface->code_size_to_size(code_size); }
 172   void  stub_verify(Stub* s)                     { _stub_interface->verify(s); }
 173   void  stub_print(Stub* s)                      { _stub_interface->print(s); }
 174 
 175  public:
 176   StubQueue(StubInterface* stub_interface, int buffer_size, Mutex* lock,
 177             const char* name);
 178   ~StubQueue();
 179 
 180   // General queue info
 181   bool  is_empty() const                         { return _queue_begin == _queue_end; }
 182   int   total_space() const                      { return _buffer_size - 1; }
 183   int   available_space() const                  { int d = _queue_begin - _queue_end - 1; return d < 0 ? d + _buffer_size : d; }
 184   int   used_space() const                       { return total_space() - available_space(); }
 185   int   number_of_stubs() const                  { return _number_of_stubs; }
 186   bool  contains(address pc) const               { return _stub_buffer <= pc && pc < _stub_buffer + _buffer_limit; }
 187   Stub* stub_containing(address pc) const;
 188   address code_start() const                     { return _stub_buffer; }
 189   address code_end() const                       { return _stub_buffer + _buffer_limit; }
 190 
 191   // Stub allocation (atomic transactions)
 192   Stub* request_committed(int code_size);        // request a stub that provides exactly code_size space for code
 193   Stub* request(int requested_code_size);        // request a stub with a (maximum) code space - locks the queue
 194   void  commit (int committed_code_size,
 195                 CodeStrings& strings);           // commit the previously requested stub - unlocks the queue
 196 
 197   // Stub deallocation
 198   void  remove_first();                          // remove the first stub in the queue
 199   void  remove_first(int n);                     // remove the first n stubs in the queue
 200   void  remove_all();                            // remove all stubs in the queue
 201 
 202   void deallocate_unused_tail();                 // deallocate the unused tail of the underlying CodeBlob
 203                                                  // only used from TemplateInterpreter::initialize()
 204   // Iteration
 205   Stub* first() const                            { return number_of_stubs() > 0 ? stub_at(_queue_begin) : NULL; }
 206   Stub* next(Stub* s) const                      { int i = index_of(s) + stub_size(s);
 207                                                    // Only wrap around in the non-contiguous case (see stubss.cpp)
 208                                                    if (i == _buffer_limit && _queue_end < _buffer_limit) i = 0;
 209                                                    return (i == _queue_end) ? NULL : stub_at(i);
 210                                                  }
 211 
 212   // Debugging/printing
 213   void  verify();                                // verifies the stub queue
 214   void  print();                                 // prints information about the stub queue
 215 
 216 };
 217 
 218 #endif // SHARE_VM_CODE_STUBS_HPP