1 /*
  2  * Copyright (c) 1997, 2018, 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/codeCache.hpp"
 27 #include "code/compiledIC.hpp"
 28 #include "code/icBuffer.hpp"
 29 #include "code/nmethod.hpp"
 30 #include "code/scopeDesc.hpp"
 31 #include "gc/shared/collectedHeap.inline.hpp"
 32 #include "interpreter/interpreter.hpp"
 33 #include "interpreter/linkResolver.hpp"
 34 #include "memory/resourceArea.hpp"
 35 #include "memory/universe.hpp"
 36 #include "oops/method.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 #include "runtime/mutexLocker.hpp"
 40 #include "runtime/stubRoutines.hpp"
 41 #include "runtime/thread.hpp"
 42 
 43 DEF_STUB_INTERFACE(ICStub);
 44 
 45 StubQueue* InlineCacheBuffer::_buffer    = NULL;
 46 
 47 CompiledICHolder* InlineCacheBuffer::_pending_released = NULL;
 48 int InlineCacheBuffer::_pending_count = 0;
 49 
 50 #ifdef ASSERT
 51 ICRefillVerifier::ICRefillVerifier()
 52   : _refill_requested(false),
 53     _refill_remembered(false)
 54 {
 55   Thread* thread = Thread::current();
 56   assert(thread->missed_ic_stub_refill_mark() == NULL, "nesting not supported");
 57   thread->set_missed_ic_stub_refill_mark(this);
 58 }
 59 
 60 ICRefillVerifier::~ICRefillVerifier() {
 61   assert(!_refill_requested || _refill_remembered,
 62          "Forgot to refill IC stubs after failed IC transition");
 63   Thread::current()->set_missed_ic_stub_refill_mark(NULL);
 64 }
 65 
 66 ICRefillVerifierMark::ICRefillVerifierMark(ICRefillVerifier* verifier) {
 67   Thread* thread = Thread::current();
 68   assert(thread->missed_ic_stub_refill_mark() == NULL, "nesting not supported");
 69   thread->set_missed_ic_stub_refill_mark(verifier);
 70 }
 71 
 72 ICRefillVerifierMark::~ICRefillVerifierMark() {
 73   Thread::current()->set_missed_ic_stub_refill_mark(NULL);
 74 }
 75 
 76 static ICRefillVerifier* current_ic_refill_verifier() {
 77   Thread* current = Thread::current();
 78   ICRefillVerifier* verifier = reinterpret_cast<ICRefillVerifier*>(current->missed_ic_stub_refill_mark());
 79   assert(verifier != NULL, "need a verifier for safety");
 80   return verifier;
 81 }
 82 #endif
 83 
 84 void ICStub::finalize() {
 85   if (!is_empty()) {
 86     ResourceMark rm;
 87     CompiledIC *ic = CompiledIC_at(CodeCache::find_compiled(ic_site()), ic_site());
 88     assert(CodeCache::find_compiled(ic->instruction_address()) != NULL, "inline cache in non-compiled?");
 89 
 90     assert(this == ICStub_from_destination_address(ic->stub_address()), "wrong owner of ic buffer");
 91     ic->set_ic_destination_and_value(destination(), cached_value());
 92   }
 93 }
 94 
 95 
 96 address ICStub::destination() const {
 97   return InlineCacheBuffer::ic_buffer_entry_point(code_begin());
 98 }
 99 
100 void* ICStub::cached_value() const {
101   return InlineCacheBuffer::ic_buffer_cached_value(code_begin());
102 }
103 
104 
105 void ICStub::set_stub(CompiledIC *ic, void* cached_val, address dest_addr) {
106   // We cannot store a pointer to the 'ic' object, since it is resource allocated. Instead we
107   // store the location of the inline cache. Then we have enough information recreate the CompiledIC
108   // object when we need to remove the stub.
109   _ic_site = ic->instruction_address();
110 
111   // Assemble new stub
112   InlineCacheBuffer::assemble_ic_buffer_code(code_begin(), cached_val, dest_addr);
113   assert(destination() == dest_addr,   "can recover destination");
114   assert(cached_value() == cached_val, "can recover destination");
115 }
116 
117 
118 void ICStub::clear() {
119   if (CompiledIC::is_icholder_entry(destination())) {
120     InlineCacheBuffer::queue_for_release((CompiledICHolder*)cached_value());
121   }
122   _ic_site = NULL;
123 }
124 
125 
126 #ifndef PRODUCT
127 // anybody calling to this stub will trap
128 
129 void ICStub::verify() {
130 }
131 
132 void ICStub::print() {
133   tty->print_cr("ICStub: site: " INTPTR_FORMAT, p2i(_ic_site));
134 }
135 #endif
136 
137 //-----------------------------------------------------------------------------------------------
138 // Implementation of InlineCacheBuffer
139 
140 
141 void InlineCacheBuffer::initialize() {
142   if (_buffer != NULL) return; // already initialized
143   _buffer = new StubQueue(new ICStubInterface, 10*K, InlineCacheBuffer_lock, "InlineCacheBuffer");
144   assert (_buffer != NULL, "cannot allocate InlineCacheBuffer");
145 }
146 
147 
148 ICStub* InlineCacheBuffer::new_ic_stub() {
149   return (ICStub*)buffer()->request_committed(ic_stub_code_size());
150 }
151 
152 
153 void InlineCacheBuffer::refill_ic_stubs() {
154 #ifdef ASSERT
155   ICRefillVerifier* verifier = current_ic_refill_verifier();
156   verifier->request_remembered();
157 #endif
158   // we ran out of inline cache buffer space; must enter safepoint.
159   // We do this by forcing a safepoint
160   EXCEPTION_MARK;
161 
162   VM_ICBufferFull ibf;
163   VMThread::execute(&ibf);
164   // We could potential get an async. exception at this point.
165   // In that case we will rethrow it to ourselvs.
166   if (HAS_PENDING_EXCEPTION) {
167     oop exception = PENDING_EXCEPTION;
168     CLEAR_PENDING_EXCEPTION;
169     Thread::send_async_exception(JavaThread::current()->threadObj(), exception);
170   }
171 }
172 
173 
174 void InlineCacheBuffer::update_inline_caches() {
175   if (buffer()->number_of_stubs() > 0) {
176     if (TraceICBuffer) {
177       tty->print_cr("[updating inline caches with %d stubs]", buffer()->number_of_stubs());
178     }
179     buffer()->remove_all();
180   }
181   release_pending_icholders();
182 }
183 
184 
185 bool InlineCacheBuffer::contains(address instruction_address) {
186   return buffer()->contains(instruction_address);
187 }
188 
189 
190 bool InlineCacheBuffer::is_empty() {
191   return buffer()->number_of_stubs() == 0;
192 }
193 
194 
195 void InlineCacheBuffer_init() {
196   InlineCacheBuffer::initialize();
197 }
198 
199 bool InlineCacheBuffer::create_transition_stub(CompiledIC *ic, void* cached_value, address entry) {
200   assert(!SafepointSynchronize::is_at_safepoint(), "should not be called during a safepoint");
201   assert(CompiledICLocker::is_safe(ic->instruction_address()), "mt unsafe call");
202   if (TraceICBuffer) {
203     tty->print_cr("  create transition stub for " INTPTR_FORMAT " destination " INTPTR_FORMAT " cached value " INTPTR_FORMAT,
204                   p2i(ic->instruction_address()), p2i(entry), p2i(cached_value));
205   }
206 
207   // allocate and initialize new "out-of-line" inline-cache
208   ICStub* ic_stub = new_ic_stub();
209   if (ic_stub == NULL) {
210 #ifdef ASSERT
211     ICRefillVerifier* verifier = current_ic_refill_verifier();
212     verifier->request_refill();
213 #endif
214     return false;
215   }
216 
217   // If an transition stub is already associate with the inline cache, then we remove the association.
218   if (ic->is_in_transition_state()) {
219     ICStub* old_stub = ICStub_from_destination_address(ic->stub_address());
220     old_stub->clear();
221   }
222 
223   ic_stub->set_stub(ic, cached_value, entry);
224 
225   // Update inline cache in nmethod to point to new "out-of-line" allocated inline cache
226   ic->set_ic_destination(ic_stub);
227   return true;
228 }
229 
230 
231 address InlineCacheBuffer::ic_destination_for(CompiledIC *ic) {
232   ICStub* stub = ICStub_from_destination_address(ic->stub_address());
233   return stub->destination();
234 }
235 
236 
237 void* InlineCacheBuffer::cached_value_for(CompiledIC *ic) {
238   ICStub* stub = ICStub_from_destination_address(ic->stub_address());
239   return stub->cached_value();
240 }
241 
242 
243 // Free CompiledICHolder*s that are no longer in use
244 void InlineCacheBuffer::release_pending_icholders() {
245   assert(SafepointSynchronize::is_at_safepoint(), "should only be called during a safepoint");
246   CompiledICHolder* holder = _pending_released;
247   _pending_released = NULL;
248   while (holder != NULL) {
249     CompiledICHolder* next = holder->next();
250     delete holder;
251     holder = next;
252     _pending_count--;
253   }
254   assert(_pending_count == 0, "wrong count");
255 }
256 
257 // Enqueue this icholder for release during the next safepoint.  It's
258 // not safe to free them until them since they might be visible to
259 // another thread.
260 void InlineCacheBuffer::queue_for_release(CompiledICHolder* icholder) {
261   MutexLockerEx mex(InlineCacheBuffer_lock, Mutex::_no_safepoint_check_flag);
262   icholder->set_next(_pending_released);
263   _pending_released = icholder;
264   _pending_count++;
265   if (TraceICBuffer) {
266     tty->print_cr("enqueueing icholder " INTPTR_FORMAT " to be freed", p2i(icholder));
267   }
268 }