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