src/share/vm/code/stubs.hpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_CODE_STUBS_HPP
aoqi@0 26 #define SHARE_VM_CODE_STUBS_HPP
aoqi@0 27
aoqi@0 28 #include "asm/codeBuffer.hpp"
aoqi@0 29 #include "memory/allocation.hpp"
aoqi@0 30 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 31 # include "os_linux.inline.hpp"
aoqi@0 32 #endif
aoqi@0 33 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 34 # include "os_solaris.inline.hpp"
aoqi@0 35 #endif
aoqi@0 36 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 37 # include "os_windows.inline.hpp"
aoqi@0 38 #endif
aoqi@0 39 #ifdef TARGET_OS_FAMILY_aix
aoqi@0 40 # include "os_aix.inline.hpp"
aoqi@0 41 #endif
aoqi@0 42 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 43 # include "os_bsd.inline.hpp"
aoqi@0 44 #endif
aoqi@0 45
aoqi@0 46 // The classes in this file provide a simple framework for the
aoqi@0 47 // management of little pieces of machine code - or stubs -
aoqi@0 48 // created on the fly and frequently discarded. In this frame-
aoqi@0 49 // work stubs are stored in a queue.
aoqi@0 50
aoqi@0 51
aoqi@0 52 // Stub serves as abstract base class. A concrete stub
aoqi@0 53 // implementation is a subclass of Stub, implementing
aoqi@0 54 // all (non-virtual!) functions required sketched out
aoqi@0 55 // in the Stub class.
aoqi@0 56 //
aoqi@0 57 // A concrete stub layout may look like this (both data
aoqi@0 58 // and code sections could be empty as well):
aoqi@0 59 //
aoqi@0 60 // ________
aoqi@0 61 // stub -->| | <--+
aoqi@0 62 // | data | |
aoqi@0 63 // |________| |
aoqi@0 64 // code_begin -->| | |
aoqi@0 65 // | | |
aoqi@0 66 // | code | | size
aoqi@0 67 // | | |
aoqi@0 68 // |________| |
aoqi@0 69 // code_end -->| | |
aoqi@0 70 // | data | |
aoqi@0 71 // |________| |
aoqi@0 72 // <--+
aoqi@0 73
aoqi@0 74
aoqi@0 75 class Stub VALUE_OBJ_CLASS_SPEC {
aoqi@0 76 public:
aoqi@0 77 // Initialization/finalization
aoqi@0 78 void initialize(int size,
aoqi@0 79 CodeStrings& strings) { ShouldNotCallThis(); } // called to initialize/specify the stub's size
aoqi@0 80 void finalize() { ShouldNotCallThis(); } // called before the stub is deallocated
aoqi@0 81
aoqi@0 82 // General info/converters
aoqi@0 83 int size() const { ShouldNotCallThis(); return 0; } // must return the size provided by initialize
aoqi@0 84 static int code_size_to_size(int code_size) { ShouldNotCallThis(); return 0; } // computes the size given the code size
aoqi@0 85
aoqi@0 86 // Code info
aoqi@0 87 address code_begin() const { ShouldNotCallThis(); return NULL; } // points to the first byte of the code
aoqi@0 88 address code_end() const { ShouldNotCallThis(); return NULL; } // points to the first byte after the code
aoqi@0 89
aoqi@0 90 // Debugging
aoqi@0 91 void verify() { ShouldNotCallThis(); } // verifies the Stub
aoqi@0 92 void print() { ShouldNotCallThis(); } // prints some information about the stub
aoqi@0 93 };
aoqi@0 94
aoqi@0 95
aoqi@0 96 // A stub interface defines the interface between a stub queue
aoqi@0 97 // and the stubs it queues. In order to avoid a vtable and
aoqi@0 98 // (and thus the extra word) in each stub, a concrete stub
aoqi@0 99 // interface object is created and associated with a stub
aoqi@0 100 // buffer which in turn uses the stub interface to interact
aoqi@0 101 // with its stubs.
aoqi@0 102 //
aoqi@0 103 // StubInterface serves as an abstract base class. A concrete
aoqi@0 104 // stub interface implementation is a subclass of StubInterface,
aoqi@0 105 // forwarding its virtual function calls to non-virtual calls
aoqi@0 106 // of the concrete stub (see also macro below). There's exactly
aoqi@0 107 // one stub interface instance required per stub queue.
aoqi@0 108
aoqi@0 109 class StubInterface: public CHeapObj<mtCode> {
aoqi@0 110 public:
aoqi@0 111 // Initialization/finalization
aoqi@0 112 virtual void initialize(Stub* self, int size,
aoqi@0 113 CodeStrings& strings) = 0; // called after creation (called twice if allocated via (request, commit))
aoqi@0 114 virtual void finalize(Stub* self) = 0; // called before deallocation
aoqi@0 115
aoqi@0 116 // General info/converters
aoqi@0 117 virtual int size(Stub* self) const = 0; // the total size of the stub in bytes (must be a multiple of CodeEntryAlignment)
aoqi@0 118 virtual int code_size_to_size(int code_size) const = 0; // computes the total stub size in bytes given the code size in bytes
aoqi@0 119
aoqi@0 120 // Code info
aoqi@0 121 virtual address code_begin(Stub* self) const = 0; // points to the first code byte
aoqi@0 122 virtual address code_end(Stub* self) const = 0; // points to the first byte after the code
aoqi@0 123
aoqi@0 124 // Debugging
aoqi@0 125 virtual void verify(Stub* self) = 0; // verifies the stub
aoqi@0 126 virtual void print(Stub* self) = 0; // prints information about the stub
aoqi@0 127 };
aoqi@0 128
aoqi@0 129
aoqi@0 130 // DEF_STUB_INTERFACE is used to create a concrete stub interface
aoqi@0 131 // class, forwarding stub interface calls to the corresponding
aoqi@0 132 // stub calls.
aoqi@0 133
aoqi@0 134 #define DEF_STUB_INTERFACE(stub) \
aoqi@0 135 class stub##Interface: public StubInterface { \
aoqi@0 136 private: \
aoqi@0 137 static stub* cast(Stub* self) { return (stub*)self; } \
aoqi@0 138 \
aoqi@0 139 public: \
aoqi@0 140 /* Initialization/finalization */ \
aoqi@0 141 virtual void initialize(Stub* self, int size, \
aoqi@0 142 CodeStrings& strings) { cast(self)->initialize(size, strings); } \
aoqi@0 143 virtual void finalize(Stub* self) { cast(self)->finalize(); } \
aoqi@0 144 \
aoqi@0 145 /* General info */ \
aoqi@0 146 virtual int size(Stub* self) const { return cast(self)->size(); } \
aoqi@0 147 virtual int code_size_to_size(int code_size) const { return stub::code_size_to_size(code_size); } \
aoqi@0 148 \
aoqi@0 149 /* Code info */ \
aoqi@0 150 virtual address code_begin(Stub* self) const { return cast(self)->code_begin(); } \
aoqi@0 151 virtual address code_end(Stub* self) const { return cast(self)->code_end(); } \
aoqi@0 152 \
aoqi@0 153 /* Debugging */ \
aoqi@0 154 virtual void verify(Stub* self) { cast(self)->verify(); } \
aoqi@0 155 virtual void print(Stub* self) { cast(self)->print(); } \
aoqi@0 156 };
aoqi@0 157
aoqi@0 158
aoqi@0 159 // A StubQueue maintains a queue of stubs.
aoqi@0 160 // Note: All sizes (spaces) are given in bytes.
aoqi@0 161
aoqi@0 162 class StubQueue: public CHeapObj<mtCode> {
aoqi@0 163 friend class VMStructs;
aoqi@0 164 private:
aoqi@0 165 StubInterface* _stub_interface; // the interface prototype
aoqi@0 166 address _stub_buffer; // where all stubs are stored
aoqi@0 167 int _buffer_size; // the buffer size in bytes
aoqi@0 168 int _buffer_limit; // the (byte) index of the actual buffer limit (_buffer_limit <= _buffer_size)
aoqi@0 169 int _queue_begin; // the (byte) index of the first queue entry (word-aligned)
aoqi@0 170 int _queue_end; // the (byte) index of the first entry after the queue (word-aligned)
aoqi@0 171 int _number_of_stubs; // the number of buffered stubs
aoqi@0 172 Mutex* const _mutex; // the lock used for a (request, commit) transaction
aoqi@0 173
aoqi@0 174 void check_index(int i) const { assert(0 <= i && i < _buffer_limit && i % CodeEntryAlignment == 0, "illegal index"); }
aoqi@0 175 bool is_contiguous() const { return _queue_begin <= _queue_end; }
aoqi@0 176 int index_of(Stub* s) const { int i = (address)s - _stub_buffer; check_index(i); return i; }
aoqi@0 177 Stub* stub_at(int i) const { check_index(i); return (Stub*)(_stub_buffer + i); }
aoqi@0 178 Stub* current_stub() const { return stub_at(_queue_end); }
aoqi@0 179
aoqi@0 180 // Stub functionality accessed via interface
aoqi@0 181 void stub_initialize(Stub* s, int size,
aoqi@0 182 CodeStrings& strings) { assert(size % CodeEntryAlignment == 0, "size not aligned"); _stub_interface->initialize(s, size, strings); }
aoqi@0 183 void stub_finalize(Stub* s) { _stub_interface->finalize(s); }
aoqi@0 184 int stub_size(Stub* s) const { return _stub_interface->size(s); }
aoqi@0 185 bool stub_contains(Stub* s, address pc) const { return _stub_interface->code_begin(s) <= pc && pc < _stub_interface->code_end(s); }
aoqi@0 186 int stub_code_size_to_size(int code_size) const { return _stub_interface->code_size_to_size(code_size); }
aoqi@0 187 void stub_verify(Stub* s) { _stub_interface->verify(s); }
aoqi@0 188 void stub_print(Stub* s) { _stub_interface->print(s); }
aoqi@0 189
aoqi@0 190 static void register_queue(StubQueue*);
aoqi@0 191
aoqi@0 192 public:
aoqi@0 193 StubQueue(StubInterface* stub_interface, int buffer_size, Mutex* lock,
aoqi@0 194 const char* name);
aoqi@0 195 ~StubQueue();
aoqi@0 196
aoqi@0 197 // General queue info
aoqi@0 198 bool is_empty() const { return _queue_begin == _queue_end; }
aoqi@0 199 int total_space() const { return _buffer_size - 1; }
aoqi@0 200 int available_space() const { int d = _queue_begin - _queue_end - 1; return d < 0 ? d + _buffer_size : d; }
aoqi@0 201 int used_space() const { return total_space() - available_space(); }
aoqi@0 202 int number_of_stubs() const { return _number_of_stubs; }
aoqi@0 203 bool contains(address pc) const { return _stub_buffer <= pc && pc < _stub_buffer + _buffer_limit; }
aoqi@0 204 Stub* stub_containing(address pc) const;
aoqi@0 205 address code_start() const { return _stub_buffer; }
aoqi@0 206 address code_end() const { return _stub_buffer + _buffer_limit; }
aoqi@0 207
aoqi@0 208 // Stub allocation (atomic transactions)
aoqi@0 209 Stub* request_committed(int code_size); // request a stub that provides exactly code_size space for code
aoqi@0 210 Stub* request(int requested_code_size); // request a stub with a (maximum) code space - locks the queue
aoqi@0 211 void commit (int committed_code_size,
aoqi@0 212 CodeStrings& strings); // commit the previously requested stub - unlocks the queue
aoqi@0 213
aoqi@0 214 // Stub deallocation
aoqi@0 215 void remove_first(); // remove the first stub in the queue
aoqi@0 216 void remove_first(int n); // remove the first n stubs in the queue
aoqi@0 217 void remove_all(); // remove all stubs in the queue
aoqi@0 218
aoqi@0 219 // Iteration
aoqi@0 220 static void queues_do(void f(StubQueue* s)); // call f with each StubQueue
aoqi@0 221 void stubs_do(void f(Stub* s)); // call f with all stubs
aoqi@0 222 Stub* first() const { return number_of_stubs() > 0 ? stub_at(_queue_begin) : NULL; }
aoqi@0 223 Stub* next(Stub* s) const { int i = index_of(s) + stub_size(s);
aoqi@0 224 if (i == _buffer_limit) i = 0;
aoqi@0 225 return (i == _queue_end) ? NULL : stub_at(i);
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 address stub_code_begin(Stub* s) const { return _stub_interface->code_begin(s); }
aoqi@0 229 address stub_code_end(Stub* s) const { return _stub_interface->code_end(s); }
aoqi@0 230
aoqi@0 231 // Debugging/printing
aoqi@0 232 void verify(); // verifies the stub queue
aoqi@0 233 void print(); // prints information about the stub queue
aoqi@0 234 };
aoqi@0 235
aoqi@0 236 #endif // SHARE_VM_CODE_STUBS_HPP

mercurial