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 CodeCache::free_unused_tail(blob, used_space()); 96 // Update the limits to the new, trimmed CodeBlob size 97 _buffer_size = blob->content_size(); 98 _buffer_limit = blob->content_size(); 99 } 100 101 Stub* StubQueue::stub_containing(address pc) const { 102 if (contains(pc)) { 103 for (Stub* s = first(); s != NULL; s = next(s)) { 104 if (stub_contains(s, pc)) return s; 105 } 106 } 107 return NULL; 108 } 109 110 111 Stub* StubQueue::request_committed(int code_size) { 112 Stub* s = request(code_size); 113 CodeStrings strings; 114 if (s != NULL) commit(code_size, strings); 115 return s; 116 } 117 118 119 Stub* StubQueue::request(int requested_code_size) { 120 assert(requested_code_size > 0, "requested_code_size must be > 0"); 121 if (_mutex != NULL) _mutex->lock(); 122 Stub* s = current_stub(); 123 int requested_size = align_up(stub_code_size_to_size(requested_code_size), CodeEntryAlignment); 124 if (requested_size <= available_space()) { 125 if (is_contiguous()) { 126 // Queue: |...|XXXXXXX|.............| 127 // ^0 ^begin ^end ^size = limit 128 assert(_buffer_limit == _buffer_size, "buffer must be fully usable"); 129 if (_queue_end + requested_size <= _buffer_size) { 130 // code fits in at the end => nothing to do 131 CodeStrings strings; 132 stub_initialize(s, requested_size, strings); 133 return s; 134 } else { 135 // stub doesn't fit in at the queue end 136 // => reduce buffer limit & wrap around 137 assert(!is_empty(), "just checkin'"); 138 _buffer_limit = _queue_end; 139 _queue_end = 0; 140 } 141 } 142 } 143 if (requested_size <= available_space()) { 144 assert(!is_contiguous(), "just checkin'"); 145 assert(_buffer_limit <= _buffer_size, "queue invariant broken"); 146 // Queue: |XXX|.......|XXXXXXX|.......| 147 // ^0 ^end ^begin ^limit ^size 148 s = current_stub(); 149 CodeStrings strings; 150 stub_initialize(s, requested_size, strings); 151 return s; 152 } 153 // Not enough space left 154 if (_mutex != NULL) _mutex->unlock(); 155 return NULL; 156 } 157 158 159 void StubQueue::commit(int committed_code_size, CodeStrings& strings) { 160 assert(committed_code_size > 0, "committed_code_size must be > 0"); 161 int committed_size = align_up(stub_code_size_to_size(committed_code_size), CodeEntryAlignment); 162 Stub* s = current_stub(); 163 assert(committed_size <= stub_size(s), "committed size must not exceed requested size"); 164 stub_initialize(s, committed_size, strings); 165 _queue_end += committed_size; 166 _number_of_stubs++; 167 if (_mutex != NULL) _mutex->unlock(); 168 debug_only(stub_verify(s);) 169 } 170 171 172 void StubQueue::remove_first() { 173 if (number_of_stubs() == 0) return; 174 Stub* s = first(); 175 debug_only(stub_verify(s);) 176 stub_finalize(s); 177 _queue_begin += stub_size(s); 178 assert(_queue_begin <= _buffer_limit, "sanity check"); 179 if (_queue_begin == _queue_end) { 180 // buffer empty 181 // => reset queue indices 182 _queue_begin = 0; 183 _queue_end = 0; 184 _buffer_limit = _buffer_size; 185 } else if (_queue_begin == _buffer_limit) { 186 // buffer limit reached 187 // => reset buffer limit & wrap around 188 _buffer_limit = _buffer_size; 189 _queue_begin = 0; 190 } 191 _number_of_stubs--; 192 } 193 194 195 void StubQueue::remove_first(int n) { 196 int i = MIN2(n, number_of_stubs()); 197 while (i-- > 0) remove_first(); 198 } 199 200 201 void StubQueue::remove_all(){ 202 debug_only(verify();) 203 remove_first(number_of_stubs()); 204 assert(number_of_stubs() == 0, "sanity check"); 205 } 206 207 208 enum { StubQueueLimit = 10 }; // there are only a few in the world 209 static StubQueue* registered_stub_queues[StubQueueLimit]; 210 211 void StubQueue::register_queue(StubQueue* sq) { 212 for (int i = 0; i < StubQueueLimit; i++) { 213 if (registered_stub_queues[i] == NULL) { 214 registered_stub_queues[i] = sq; 215 return; 216 } 217 } 218 ShouldNotReachHere(); 219 } 220 221 222 void StubQueue::queues_do(void f(StubQueue* sq)) { 223 for (int i = 0; i < StubQueueLimit; i++) { 224 if (registered_stub_queues[i] != NULL) { 225 f(registered_stub_queues[i]); 226 } 227 } 228 } 229 230 231 void StubQueue::stubs_do(void f(Stub* s)) { 232 debug_only(verify();) 233 MutexLockerEx lock(_mutex); 234 for (Stub* s = first(); s != NULL; s = next(s)) f(s); 235 } 236 237 238 void StubQueue::verify() { 239 // verify only if initialized 240 if (_stub_buffer == NULL) return; 241 MutexLockerEx lock(_mutex); 242 // verify index boundaries 243 guarantee(0 <= _buffer_size, "buffer size must be positive"); 244 guarantee(0 <= _buffer_limit && _buffer_limit <= _buffer_size , "_buffer_limit out of bounds"); 245 guarantee(0 <= _queue_begin && _queue_begin < _buffer_limit, "_queue_begin out of bounds"); 246 guarantee(0 <= _queue_end && _queue_end <= _buffer_limit, "_queue_end out of bounds"); 247 // verify alignment 248 guarantee(_buffer_size % CodeEntryAlignment == 0, "_buffer_size not aligned"); 249 guarantee(_buffer_limit % CodeEntryAlignment == 0, "_buffer_limit not aligned"); 250 guarantee(_queue_begin % CodeEntryAlignment == 0, "_queue_begin not aligned"); 251 guarantee(_queue_end % CodeEntryAlignment == 0, "_queue_end not aligned"); 252 // verify buffer limit/size relationship 253 if (is_contiguous()) { 254 guarantee(_buffer_limit == _buffer_size, "_buffer_limit must equal _buffer_size"); 255 } 256 // verify contents 257 int n = 0; 258 for (Stub* s = first(); s != NULL; s = next(s)) { 259 stub_verify(s); 260 n++; 261 } 262 guarantee(n == number_of_stubs(), "number of stubs inconsistent"); 263 guarantee(_queue_begin != _queue_end || n == 0, "buffer indices must be the same"); 264 } 265 266 267 void StubQueue::print() { 268 MutexLockerEx lock(_mutex); 269 for (Stub* s = first(); s != NULL; s = next(s)) { 270 stub_print(s); 271 } 272 } 273