1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/code/stubs.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,254 @@ 1.4 +/* 1.5 + * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "incls/_precompiled.incl" 1.29 +#include "incls/_stubs.cpp.incl" 1.30 + 1.31 + 1.32 +// Implementation of StubQueue 1.33 +// 1.34 +// Standard wrap-around queue implementation; the queue dimensions 1.35 +// are specified by the _queue_begin & _queue_end indices. The queue 1.36 +// can be in two states (transparent to the outside): 1.37 +// 1.38 +// a) contiguous state: all queue entries in one block (or empty) 1.39 +// 1.40 +// Queue: |...|XXXXXXX|...............| 1.41 +// ^0 ^begin ^end ^size = limit 1.42 +// |_______| 1.43 +// one block 1.44 +// 1.45 +// b) non-contiguous state: queue entries in two blocks 1.46 +// 1.47 +// Queue: |XXX|.......|XXXXXXX|.......| 1.48 +// ^0 ^end ^begin ^limit ^size 1.49 +// |___| |_______| 1.50 +// 1st block 2nd block 1.51 +// 1.52 +// In the non-contiguous state, the wrap-around point is 1.53 +// indicated via the _buffer_limit index since the last 1.54 +// queue entry may not fill up the queue completely in 1.55 +// which case we need to know where the 2nd block's end 1.56 +// is to do the proper wrap-around. When removing the 1.57 +// last entry of the 2nd block, _buffer_limit is reset 1.58 +// to _buffer_size. 1.59 +// 1.60 +// CAUTION: DO NOT MESS WITH THIS CODE IF YOU CANNOT PROVE 1.61 +// ITS CORRECTNESS! THIS CODE IS MORE SUBTLE THAN IT LOOKS! 1.62 + 1.63 + 1.64 +StubQueue::StubQueue(StubInterface* stub_interface, int buffer_size, 1.65 + Mutex* lock, const char* name) : _mutex(lock) { 1.66 + intptr_t size = round_to(buffer_size, 2*BytesPerWord); 1.67 + BufferBlob* blob = BufferBlob::create(name, size); 1.68 + if( blob == NULL ) vm_exit_out_of_memory1(size, "CodeCache: no room for %s", name); 1.69 + _stub_interface = stub_interface; 1.70 + _buffer_size = blob->instructions_size(); 1.71 + _buffer_limit = blob->instructions_size(); 1.72 + _stub_buffer = blob->instructions_begin(); 1.73 + _queue_begin = 0; 1.74 + _queue_end = 0; 1.75 + _number_of_stubs = 0; 1.76 + register_queue(this); 1.77 +} 1.78 + 1.79 + 1.80 +StubQueue::~StubQueue() { 1.81 + // Note: Currently StubQueues are never destroyed so nothing needs to be done here. 1.82 + // If we want to implement the destructor, we need to release the BufferBlob 1.83 + // allocated in the constructor (i.e., we need to keep it around or look it 1.84 + // up via CodeCache::find_blob(...). 1.85 + Unimplemented(); 1.86 +} 1.87 + 1.88 + 1.89 +Stub* StubQueue::stub_containing(address pc) const { 1.90 + if (contains(pc)) { 1.91 + for (Stub* s = first(); s != NULL; s = next(s)) { 1.92 + if (stub_contains(s, pc)) return s; 1.93 + } 1.94 + } 1.95 + return NULL; 1.96 +} 1.97 + 1.98 + 1.99 +Stub* StubQueue::request_committed(int code_size) { 1.100 + Stub* s = request(code_size); 1.101 + if (s != NULL) commit(code_size); 1.102 + return s; 1.103 +} 1.104 + 1.105 + 1.106 +Stub* StubQueue::request(int requested_code_size) { 1.107 + assert(requested_code_size > 0, "requested_code_size must be > 0"); 1.108 + if (_mutex != NULL) _mutex->lock(); 1.109 + Stub* s = current_stub(); 1.110 + int requested_size = round_to(stub_code_size_to_size(requested_code_size), CodeEntryAlignment); 1.111 + if (requested_size <= available_space()) { 1.112 + if (is_contiguous()) { 1.113 + // Queue: |...|XXXXXXX|.............| 1.114 + // ^0 ^begin ^end ^size = limit 1.115 + assert(_buffer_limit == _buffer_size, "buffer must be fully usable"); 1.116 + if (_queue_end + requested_size <= _buffer_size) { 1.117 + // code fits in at the end => nothing to do 1.118 + stub_initialize(s, requested_size); 1.119 + return s; 1.120 + } else { 1.121 + // stub doesn't fit in at the queue end 1.122 + // => reduce buffer limit & wrap around 1.123 + assert(!is_empty(), "just checkin'"); 1.124 + _buffer_limit = _queue_end; 1.125 + _queue_end = 0; 1.126 + } 1.127 + } 1.128 + } 1.129 + if (requested_size <= available_space()) { 1.130 + assert(!is_contiguous(), "just checkin'"); 1.131 + assert(_buffer_limit <= _buffer_size, "queue invariant broken"); 1.132 + // Queue: |XXX|.......|XXXXXXX|.......| 1.133 + // ^0 ^end ^begin ^limit ^size 1.134 + s = current_stub(); 1.135 + stub_initialize(s, requested_size); 1.136 + return s; 1.137 + } 1.138 + // Not enough space left 1.139 + if (_mutex != NULL) _mutex->unlock(); 1.140 + return NULL; 1.141 +} 1.142 + 1.143 + 1.144 +void StubQueue::commit(int committed_code_size) { 1.145 + assert(committed_code_size > 0, "committed_code_size must be > 0"); 1.146 + int committed_size = round_to(stub_code_size_to_size(committed_code_size), CodeEntryAlignment); 1.147 + Stub* s = current_stub(); 1.148 + assert(committed_size <= stub_size(s), "committed size must not exceed requested size"); 1.149 + stub_initialize(s, committed_size); 1.150 + _queue_end += committed_size; 1.151 + _number_of_stubs++; 1.152 + if (_mutex != NULL) _mutex->unlock(); 1.153 + debug_only(stub_verify(s);) 1.154 +} 1.155 + 1.156 + 1.157 +void StubQueue::remove_first() { 1.158 + if (number_of_stubs() == 0) return; 1.159 + Stub* s = first(); 1.160 + debug_only(stub_verify(s);) 1.161 + stub_finalize(s); 1.162 + _queue_begin += stub_size(s); 1.163 + assert(_queue_begin <= _buffer_limit, "sanity check"); 1.164 + if (_queue_begin == _queue_end) { 1.165 + // buffer empty 1.166 + // => reset queue indices 1.167 + _queue_begin = 0; 1.168 + _queue_end = 0; 1.169 + _buffer_limit = _buffer_size; 1.170 + } else if (_queue_begin == _buffer_limit) { 1.171 + // buffer limit reached 1.172 + // => reset buffer limit & wrap around 1.173 + _buffer_limit = _buffer_size; 1.174 + _queue_begin = 0; 1.175 + } 1.176 + _number_of_stubs--; 1.177 +} 1.178 + 1.179 + 1.180 +void StubQueue::remove_first(int n) { 1.181 + int i = MIN2(n, number_of_stubs()); 1.182 + while (i-- > 0) remove_first(); 1.183 +} 1.184 + 1.185 + 1.186 +void StubQueue::remove_all(){ 1.187 + debug_only(verify();) 1.188 + remove_first(number_of_stubs()); 1.189 + assert(number_of_stubs() == 0, "sanity check"); 1.190 +} 1.191 + 1.192 + 1.193 +enum { StubQueueLimit = 10 }; // there are only a few in the world 1.194 +static StubQueue* registered_stub_queues[StubQueueLimit]; 1.195 + 1.196 +void StubQueue::register_queue(StubQueue* sq) { 1.197 + for (int i = 0; i < StubQueueLimit; i++) { 1.198 + if (registered_stub_queues[i] == NULL) { 1.199 + registered_stub_queues[i] = sq; 1.200 + return; 1.201 + } 1.202 + } 1.203 + ShouldNotReachHere(); 1.204 +} 1.205 + 1.206 + 1.207 +void StubQueue::queues_do(void f(StubQueue* sq)) { 1.208 + for (int i = 0; i < StubQueueLimit; i++) { 1.209 + if (registered_stub_queues[i] != NULL) { 1.210 + f(registered_stub_queues[i]); 1.211 + } 1.212 + } 1.213 +} 1.214 + 1.215 + 1.216 +void StubQueue::stubs_do(void f(Stub* s)) { 1.217 + debug_only(verify();) 1.218 + MutexLockerEx lock(_mutex); 1.219 + for (Stub* s = first(); s != NULL; s = next(s)) f(s); 1.220 +} 1.221 + 1.222 + 1.223 +void StubQueue::verify() { 1.224 + // verify only if initialized 1.225 + if (_stub_buffer == NULL) return; 1.226 + MutexLockerEx lock(_mutex); 1.227 + // verify index boundaries 1.228 + guarantee(0 <= _buffer_size, "buffer size must be positive"); 1.229 + guarantee(0 <= _buffer_limit && _buffer_limit <= _buffer_size , "_buffer_limit out of bounds"); 1.230 + guarantee(0 <= _queue_begin && _queue_begin < _buffer_limit, "_queue_begin out of bounds"); 1.231 + guarantee(0 <= _queue_end && _queue_end <= _buffer_limit, "_queue_end out of bounds"); 1.232 + // verify alignment 1.233 + guarantee(_buffer_size % CodeEntryAlignment == 0, "_buffer_size not aligned"); 1.234 + guarantee(_buffer_limit % CodeEntryAlignment == 0, "_buffer_limit not aligned"); 1.235 + guarantee(_queue_begin % CodeEntryAlignment == 0, "_queue_begin not aligned"); 1.236 + guarantee(_queue_end % CodeEntryAlignment == 0, "_queue_end not aligned"); 1.237 + // verify buffer limit/size relationship 1.238 + if (is_contiguous()) { 1.239 + guarantee(_buffer_limit == _buffer_size, "_buffer_limit must equal _buffer_size"); 1.240 + } 1.241 + // verify contents 1.242 + int n = 0; 1.243 + for (Stub* s = first(); s != NULL; s = next(s)) { 1.244 + stub_verify(s); 1.245 + n++; 1.246 + } 1.247 + guarantee(n == number_of_stubs(), "number of stubs inconsistent"); 1.248 + guarantee(_queue_begin != _queue_end || n == 0, "buffer indices must be the same"); 1.249 +} 1.250 + 1.251 + 1.252 +void StubQueue::print() { 1.253 + MutexLockerEx lock(_mutex); 1.254 + for (Stub* s = first(); s != NULL; s = next(s)) { 1.255 + stub_print(s); 1.256 + } 1.257 +}