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 #include "precompiled.hpp" 26 #include "code/codeBlob.hpp" 27 #include "code/codeCache.hpp" 28 #include "code/stubs.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "oops/oop.inline.hpp" 31 #include "runtime/mutexLocker.hpp" 32 #include "utilities/align.hpp" 33 34 35 // Implementation of StubQueue 36 // 37 // Standard wrap-around queue implementation; the queue dimensions 38 // are specified by the _queue_begin & _queue_end indices. The queue 39 // can be in two states (transparent to the outside): 40 // 41 // a) contiguous state: all queue entries in one block (or empty) 42 // 43 // Queue: |...|XXXXXXX|...............| 44 // ^0 ^begin ^end ^size = limit 45 // |_______| 46 // one block 47 // 48 // b) non-contiguous state: queue entries in two blocks 49 // 50 // Queue: |XXX|.......|XXXXXXX|.......| 51 // ^0 ^end ^begin ^limit ^size 52 // |___| |_______| 53 // 1st block 2nd block 54 // 55 // In the non-contiguous state, the wrap-around point is 56 // indicated via the _buffer_limit index since the last 57 // queue entry may not fill up the queue completely in 58 // which case we need to know where the 2nd block's end 59 // is to do the proper wrap-around. When removing the 60 // last entry of the 2nd block, _buffer_limit is reset 61 // to _buffer_size. 62 // 63 // CAUTION: DO NOT MESS WITH THIS CODE IF YOU CANNOT PROVE 64 // ITS CORRECTNESS! THIS CODE IS MORE SUBTLE THAN IT LOOKS! 65 66 67 StubQueue::StubQueue(StubInterface* stub_interface, int buffer_size, 68 Mutex* lock, const char* name) : _mutex(lock) { 69 intptr_t size = align_up(buffer_size, 2*BytesPerWord); 70 BufferBlob* blob = BufferBlob::create(name, size); 71 if( blob == NULL) { 72 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for %s", name); 73 } 74 _stub_interface = stub_interface; 75 _buffer_size = blob->content_size(); 76 _buffer_limit = blob->content_size(); 77 _stub_buffer = blob->content_begin(); 78 _queue_begin = 0; 79 _queue_end = 0; 80 _number_of_stubs = 0; 81 register_queue(this); 82 } 83 84 85 StubQueue::~StubQueue() { 86 // Note: Currently StubQueues are never destroyed so nothing needs to be done here. 87 // If we want to implement the destructor, we need to release the BufferBlob 88 // allocated in the constructor (i.e., we need to keep it around or look it 89 // up via CodeCache::find_blob(...). 90 Unimplemented(); 91 } 92 93 void StubQueue::deallocate_unused_tail() { 94 CodeBlob* blob = CodeCache::find_blob((void*)_stub_buffer); 95 // We also have to account for the extra space (i.e. header) used by the CodeBlob 96 // which provides our memory (see BufferBlob::create() in codeBlob.cpp). 97 size_t header_size = CodeBlob::align_code_offset(blob->header_size()); 98 CodeCache::free_unused_tail(blob, header_size + used_space()); 99 // Update the limits to the new, trimmed CodeBlob size 100 _buffer_size = blob->content_size(); 101 _buffer_limit = blob->content_size(); 102 } 103 104 Stub* StubQueue::stub_containing(address pc) const { 105 if (contains(pc)) { 106 for (Stub* s = first(); s != NULL; s = next(s)) { 107 if (stub_contains(s, pc)) return s; 108 } 109 } 110 return NULL; 111 } 112 113 114 Stub* StubQueue::request_committed(int code_size) { 115 Stub* s = request(code_size); 116 CodeStrings strings; 117 if (s != NULL) commit(code_size, strings); 118 return s; 119 } 120 121 122 Stub* StubQueue::request(int requested_code_size) { 123 assert(requested_code_size > 0, "requested_code_size must be > 0"); 124 if (_mutex != NULL) _mutex->lock(); 125 Stub* s = current_stub(); 126 int requested_size = align_up(stub_code_size_to_size(requested_code_size), CodeEntryAlignment); 127 if (requested_size <= available_space()) { 128 if (is_contiguous()) { 129 // Queue: |...|XXXXXXX|.............| 130 // ^0 ^begin ^end ^size = limit 131 assert(_buffer_limit == _buffer_size, "buffer must be fully usable"); 132 if (_queue_end + requested_size <= _buffer_size) { 133 // code fits in at the end => nothing to do 134 CodeStrings strings; 135 stub_initialize(s, requested_size, strings); 136 return s; 137 } else { 138 // stub doesn't fit in at the queue end 139 // => reduce buffer limit & wrap around 140 assert(!is_empty(), "just checkin'"); 141 _buffer_limit = _queue_end; 142 _queue_end = 0; 143 } 144 } 145 } 146 if (requested_size <= available_space()) { 147 assert(!is_contiguous(), "just checkin'"); 148 assert(_buffer_limit <= _buffer_size, "queue invariant broken"); 149 // Queue: |XXX|.......|XXXXXXX|.......| 150 // ^0 ^end ^begin ^limit ^size 151 s = current_stub(); 152 CodeStrings strings; 153 stub_initialize(s, requested_size, strings); 154 return s; 155 } 156 // Not enough space left 157 if (_mutex != NULL) _mutex->unlock(); 158 return NULL; 159 } 160 161 162 void StubQueue::commit(int committed_code_size, CodeStrings& strings) { 163 assert(committed_code_size > 0, "committed_code_size must be > 0"); 164 int committed_size = align_up(stub_code_size_to_size(committed_code_size), CodeEntryAlignment); 165 Stub* s = current_stub(); 166 assert(committed_size <= stub_size(s), "committed size must not exceed requested size"); 167 stub_initialize(s, committed_size, strings); 168 _queue_end += committed_size; 169 _number_of_stubs++; 170 if (_mutex != NULL) _mutex->unlock(); 171 debug_only(stub_verify(s);) 172 } 173 174 175 void StubQueue::remove_first() { 176 if (number_of_stubs() == 0) return; 177 Stub* s = first(); 178 debug_only(stub_verify(s);) 179 stub_finalize(s); 180 _queue_begin += stub_size(s); 181 assert(_queue_begin <= _buffer_limit, "sanity check"); 182 if (_queue_begin == _queue_end) { 183 // buffer empty 184 // => reset queue indices 185 _queue_begin = 0; 186 _queue_end = 0; 187 _buffer_limit = _buffer_size; 188 } else if (_queue_begin == _buffer_limit) { 189 // buffer limit reached 190 // => reset buffer limit & wrap around 191 _buffer_limit = _buffer_size; 192 _queue_begin = 0; 193 } 194 _number_of_stubs--; 195 } 196 197 198 void StubQueue::remove_first(int n) { 199 int i = MIN2(n, number_of_stubs()); 200 while (i-- > 0) remove_first(); 201 } 202 203 204 void StubQueue::remove_all(){ 205 debug_only(verify();) 206 remove_first(number_of_stubs()); 207 assert(number_of_stubs() == 0, "sanity check"); 208 } 209 210 211 enum { StubQueueLimit = 10 }; // there are only a few in the world 212 static StubQueue* registered_stub_queues[StubQueueLimit]; 213 214 void StubQueue::register_queue(StubQueue* sq) { 215 for (int i = 0; i < StubQueueLimit; i++) { 216 if (registered_stub_queues[i] == NULL) { 217 registered_stub_queues[i] = sq; 218 return; 219 } 220 } 221 ShouldNotReachHere(); 222 } 223 224 225 void StubQueue::queues_do(void f(StubQueue* sq)) { 226 for (int i = 0; i < StubQueueLimit; i++) { 227 if (registered_stub_queues[i] != NULL) { 228 f(registered_stub_queues[i]); 229 } 230 } 231 } 232 233 234 void StubQueue::stubs_do(void f(Stub* s)) { 235 debug_only(verify();) 236 MutexLockerEx lock(_mutex); 237 for (Stub* s = first(); s != NULL; s = next(s)) f(s); 238 } 239 240 241 void StubQueue::verify() { 242 // verify only if initialized 243 if (_stub_buffer == NULL) return; 244 MutexLockerEx lock(_mutex); 245 // verify index boundaries 246 guarantee(0 <= _buffer_size, "buffer size must be positive"); 247 guarantee(0 <= _buffer_limit && _buffer_limit <= _buffer_size , "_buffer_limit out of bounds"); 248 guarantee(0 <= _queue_begin && _queue_begin < _buffer_limit, "_queue_begin out of bounds"); 249 guarantee(0 <= _queue_end && _queue_end <= _buffer_limit, "_queue_end out of bounds"); 250 // verify alignment 251 guarantee(_buffer_size % CodeEntryAlignment == 0, "_buffer_size not aligned"); 252 guarantee(_buffer_limit % CodeEntryAlignment == 0, "_buffer_limit not aligned"); 253 guarantee(_queue_begin % CodeEntryAlignment == 0, "_queue_begin not aligned"); 254 guarantee(_queue_end % CodeEntryAlignment == 0, "_queue_end not aligned"); 255 // verify buffer limit/size relationship 256 if (is_contiguous()) { 257 guarantee(_buffer_limit == _buffer_size, "_buffer_limit must equal _buffer_size"); 258 } 259 // verify contents 260 int n = 0; 261 for (Stub* s = first(); s != NULL; s = next(s)) { 262 stub_verify(s); 263 n++; 264 } 265 guarantee(n == number_of_stubs(), "number of stubs inconsistent"); 266 guarantee(_queue_begin != _queue_end || n == 0, "buffer indices must be the same"); 267 } 268 269 270 void StubQueue::print() { 271 MutexLockerEx lock(_mutex); 272 for (Stub* s = first(); s != NULL; s = next(s)) { 273 stub_print(s); 274 } 275 } 276