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