1 /*
2  * Copyright (c) 1997, 2019, 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 #ifndef SHARE_CODE_ICBUFFER_HPP
26 #define SHARE_CODE_ICBUFFER_HPP
27 
28 #include "asm/codeBuffer.hpp"
29 #include "code/stubs.hpp"
30 #include "interpreter/bytecodes.hpp"
31 #include "memory/allocation.hpp"
32 #include "runtime/safepointVerifiers.hpp"
33 #include "utilities/align.hpp"
34 #include "utilities/debug.hpp"
35 #include "utilities/macros.hpp"
36 
37 //
38 // For CompiledIC's:
39 //
40 // In cases where we do not have MT-safe state transformation,
41 // we go to a transition state, using ICStubs. At a safepoint,
42 // the inline caches are transferred from the transitional code:
43 //
44 //    instruction_address --> 01 set xxx_oop, Ginline_cache_klass
45 //                            23 jump_to Gtemp, yyyy
46 //                            4  nop
47 
48 class ICStub: public Stub {
49  private:
50   int                 _size;       // total size of the stub incl. code
51   address             _ic_site;    // points at call instruction of owning ic-buffer
52   /* stub code follows here */
53  protected:
54   friend class ICStubInterface;
55   // This will be called only by ICStubInterface
initialize(int size,CodeStrings strings)56   void    initialize(int size,
57                      CodeStrings strings)        { _size = size; _ic_site = NULL; }
58   void    finalize(); // called when a method is removed
59 
60   // General info
size() const61   int     size() const                           { return _size; }
code_size_to_size(int code_size)62   static  int code_size_to_size(int code_size)   { return align_up((int)sizeof(ICStub), CodeEntryAlignment) + code_size; }
63 
64  public:
65   // Creation
66   void set_stub(CompiledIC *ic, void* cached_value, address dest_addr);
67 
68   // Code info
code_begin() const69   address code_begin() const                     { return (address)this + align_up(sizeof(ICStub), CodeEntryAlignment); }
code_end() const70   address code_end() const                       { return (address)this + size(); }
71 
72   // Call site info
ic_site() const73   address ic_site() const                        { return _ic_site; }
74   void    clear();
is_empty() const75   bool    is_empty() const                       { return _ic_site == NULL; }
76 
77   // stub info
78   address destination() const;  // destination of jump instruction
79   void* cached_value() const;   // cached_value for stub
80 
81   // Debugging
82   void    verify()            PRODUCT_RETURN;
83   void    print()             PRODUCT_RETURN;
84 
85   // Creation
86   friend ICStub* ICStub_from_destination_address(address destination_address);
87 };
88 
89 // ICStub Creation
ICStub_from_destination_address(address destination_address)90 inline ICStub* ICStub_from_destination_address(address destination_address) {
91   ICStub* stub = (ICStub*) (destination_address - align_up(sizeof(ICStub), CodeEntryAlignment));
92   #ifdef ASSERT
93   stub->verify();
94   #endif
95   return stub;
96 }
97 
98 #ifdef ASSERT
99 // The ICRefillVerifier class is a stack allocated RAII object used to
100 // detect if a failed IC transition that required IC stub refilling has
101 // been accidentally missed. It is up to the caller to in that case
102 // refill IC stubs.
103 class ICRefillVerifier: StackObj {
104   bool _refill_requested;
105   bool _refill_remembered;
106 
107  public:
108   ICRefillVerifier();
109   ~ICRefillVerifier();
110 
request_refill()111   void request_refill() { _refill_requested = true; }
request_remembered()112   void request_remembered() { _refill_remembered = true; }
113 };
114 
115 // The ICRefillVerifierMark is used to set the thread's current
116 // ICRefillVerifier to a provided one. This is useful in particular
117 // when transitioning IC stubs in parallel and refilling from the
118 // master thread invoking the IC stub transitioning code.
119 class ICRefillVerifierMark: StackObj {
120  public:
121   ICRefillVerifierMark(ICRefillVerifier* verifier);
122   ~ICRefillVerifierMark();
123 };
124 #else
125 class ICRefillVerifier: StackObj {
126  public:
ICRefillVerifier()127   ICRefillVerifier() {}
128 };
129 class ICRefillVerifierMark: StackObj {
130  public:
ICRefillVerifierMark(ICRefillVerifier * verifier)131   ICRefillVerifierMark(ICRefillVerifier* verifier) {}
132 };
133 #endif
134 
135 class InlineCacheBuffer: public AllStatic {
136  private:
137   // friends
138   friend class ICStub;
139 
140   static int ic_stub_code_size();
141 
142   static StubQueue* _buffer;
143 
144   static CompiledICHolder* _pending_released;
145   static int _pending_count;
146 
buffer()147   static StubQueue* buffer()                         { return _buffer;         }
148 
149   static ICStub* new_ic_stub();
150 
151   // Machine-dependent implementation of ICBuffer
152   static void    assemble_ic_buffer_code(address code_begin, void* cached_value, address entry_point);
153   static address ic_buffer_entry_point  (address code_begin);
154   static void*   ic_buffer_cached_value (address code_begin);
155 
156  public:
157 
158     // Initialization; must be called before first usage
159   static void initialize();
160 
161   // Access
162   static bool contains(address instruction_address);
163 
164     // removes the ICStubs after backpatching
165   static void update_inline_caches();
166   static void refill_ic_stubs();
167 
168   // for debugging
169   static bool is_empty();
170 
171   static void release_pending_icholders();
172   static void queue_for_release(CompiledICHolder* icholder);
pending_icholder_count()173   static int pending_icholder_count() { return _pending_count; }
174 
175   // New interface
176   static bool    create_transition_stub(CompiledIC *ic, void* cached_value, address entry);
177   static address ic_destination_for(CompiledIC *ic);
178   static void*   cached_value_for(CompiledIC *ic);
179 };
180 
181 #endif // SHARE_CODE_ICBUFFER_HPP
182