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