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 #ifndef SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
25 #define SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
26
27 #include "code/nmethod.hpp"
28 #include "jvmci/jvmci.hpp"
29 #include "jvmci/jvmciExceptions.hpp"
30 #include "jvmci/jvmciObject.hpp"
31
32 class JVMCIEnv;
33 class JVMCICompiler;
34 class JVMCICompileState;
35
36 // Encapsulates the JVMCI metadata for an nmethod.
37 // JVMCINMethodData objects are inlined into nmethods
38 // at nmethod::_jvmci_data_offset.
39 class JVMCINMethodData {
40 // Index for the HotSpotNmethod mirror in the nmethod's oops table.
41 // This is -1 if there is no mirror in the oops table.
42 int _nmethod_mirror_index;
43
44 // Is HotSpotNmethod.name non-null? If so, the value is
45 // embedded in the end of this object.
46 bool _has_name;
47
48 // Address of the failed speculations list to which a speculation
49 // is appended when it causes a deoptimization.
50 FailedSpeculation** _failed_speculations;
51
52 public:
53 // Computes the size of a JVMCINMethodData object
54 static int compute_size(const char* nmethod_mirror_name) {
69 // Gets the JVMCI name of the nmethod (which may be NULL).
70 const char* name() { return _has_name ? (char*)(((address) this) + sizeof(JVMCINMethodData)) : NULL; }
71
72 // Clears the HotSpotNmethod.address field in the mirror. If nm
73 // is dead, the HotSpotNmethod.entryPoint field is also cleared.
74 void invalidate_nmethod_mirror(nmethod* nm);
75
76 // Gets the mirror from nm's oops table.
77 oop get_nmethod_mirror(nmethod* nm, bool phantom_ref);
78
79 // Sets the mirror in nm's oops table.
80 void set_nmethod_mirror(nmethod* nm, oop mirror);
81
82 // Clears the mirror in nm's oops table.
83 void clear_nmethod_mirror(nmethod* nm);
84 };
85
86 // A top level class that represents an initialized JVMCI runtime.
87 // There is one instance of this class per HotSpotJVMCIRuntime object.
88 class JVMCIRuntime: public CHeapObj<mtJVMCI> {
89 public:
90 // Constants describing whether JVMCI wants to be able to adjust the compilation
91 // level selected for a method by the VM compilation policy and if so, based on
92 // what information about the method being schedule for compilation.
93 enum CompLevelAdjustment {
94 none = 0, // no adjustment
95 by_holder = 1, // adjust based on declaring class of method
96 by_full_signature = 2 // adjust based on declaring class, name and signature of method
97 };
98
99 private:
100 volatile bool _being_initialized;
101 volatile bool _initialized;
102
103 JVMCIObject _HotSpotJVMCIRuntime_instance;
104
105 bool _shutdown_called;
106
107 JVMCIObject create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS);
108
109 // Implementation methods for loading and constant pool access.
110 static Klass* get_klass_by_name_impl(Klass*& accessing_klass,
111 const constantPoolHandle& cpool,
112 Symbol* klass_name,
113 bool require_local);
114 static Klass* get_klass_by_index_impl(const constantPoolHandle& cpool,
115 int klass_index,
116 bool& is_accessible,
117 Klass* loading_klass);
118 static void get_field_by_index_impl(InstanceKlass* loading_klass, fieldDescriptor& fd,
119 int field_index);
120 static methodHandle get_method_by_index_impl(const constantPoolHandle& cpool,
121 int method_index, Bytecodes::Code bc,
122 InstanceKlass* loading_klass);
123
124 // Helper methods
125 static bool check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass);
126 static methodHandle lookup_method(InstanceKlass* accessor,
127 Klass* holder,
128 Symbol* name,
129 Symbol* sig,
130 Bytecodes::Code bc,
131 constantTag tag);
132
133 public:
134 JVMCIRuntime() {
135 _initialized = false;
136 _being_initialized = false;
137 _shutdown_called = false;
138 }
139
140 /**
141 * Compute offsets and construct any state required before executing JVMCI code.
142 */
143 void initialize(JVMCIEnv* jvmciEnv);
144
145 /**
146 * Gets the singleton HotSpotJVMCIRuntime instance, initializing it if necessary
147 */
148 JVMCIObject get_HotSpotJVMCIRuntime(JVMCI_TRAPS);
149
150 bool is_HotSpotJVMCIRuntime_initialized() {
151 return _HotSpotJVMCIRuntime_instance.is_non_null();
152 }
153
154 /**
155 * Trigger initialization of HotSpotJVMCIRuntime through JVMCI.getRuntime()
156 */
157 void initialize_JVMCI(JVMCI_TRAPS);
158
159 /**
160 * Explicitly initialize HotSpotJVMCIRuntime itself
161 */
162 void initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS);
163
164 void call_getCompiler(TRAPS);
165
166 void shutdown();
167
168 bool shutdown_called() {
169 return _shutdown_called;
170 }
171
172 void bootstrap_finished(TRAPS);
173
174 // Look up a klass by name from a particular class loader (the accessor's).
175 // If require_local, result must be defined in that class loader, or NULL.
176 // If !require_local, a result from remote class loader may be reported,
177 // if sufficient class loader constraints exist such that initiating
178 // a class loading request from the given loader is bound to return
179 // the class defined in the remote loader (or throw an error).
180 //
181 // Return an unloaded klass if !require_local and no class at all is found.
182 //
183 // The CI treats a klass as loaded if it is consistently defined in
184 // another loader, even if it hasn't yet been loaded in all loaders
185 // that could potentially see it via delegation.
186 static Klass* get_klass_by_name(Klass* accessing_klass,
187 Symbol* klass_name,
188 bool require_local);
189
190 // Constant pool access.
191 static Klass* get_klass_by_index(const constantPoolHandle& cpool,
218 int entry_bci,
219 CodeOffsets* offsets,
220 int orig_pc_offset,
221 CodeBuffer* code_buffer,
222 int frame_words,
223 OopMapSet* oop_map_set,
224 ExceptionHandlerTable* handler_table,
225 ImplicitExceptionTable* implicit_exception_table,
226 AbstractCompiler* compiler,
227 DebugInformationRecorder* debug_info,
228 Dependencies* dependencies,
229 int compile_id,
230 bool has_unsafe_access,
231 bool has_wide_vector,
232 JVMCIObject compiled_code,
233 JVMCIObject nmethod_mirror,
234 FailedSpeculation** failed_speculations,
235 char* speculations,
236 int speculations_len);
237
238 /**
239 * Exits the VM due to an unexpected exception.
240 */
241 static void exit_on_pending_exception(JVMCIEnv* JVMCIENV, const char* message);
242
243 static void describe_pending_hotspot_exception(JavaThread* THREAD, bool clear);
244
245 #define CHECK_EXIT THREAD); \
246 if (HAS_PENDING_EXCEPTION) { \
247 char buf[256]; \
248 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \
249 JVMCIRuntime::exit_on_pending_exception(NULL, buf); \
250 return; \
251 } \
252 (void)(0
253
254 #define CHECK_EXIT_(v) THREAD); \
255 if (HAS_PENDING_EXCEPTION) { \
256 char buf[256]; \
257 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \
258 JVMCIRuntime::exit_on_pending_exception(NULL, buf); \
259 return v; \
260 } \
323 static void log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline);
324 // Print the passed in object, optionally followed by a newline. If
325 // as_string is true and the object is a java.lang.String then it
326 // printed as a string, otherwise the type of the object is printed
327 // followed by its address.
328 static void log_object(JavaThread* thread, oopDesc* object, bool as_string, bool newline);
329 #if INCLUDE_G1GC
330 static void write_barrier_pre(JavaThread* thread, oopDesc* obj);
331 static void write_barrier_post(JavaThread* thread, void* card);
332 #endif
333 static jboolean validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child);
334
335 // used to throw exceptions from compiled JVMCI code
336 static int throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message);
337 // helper methods to throw exception with complex messages
338 static int throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass);
339 static int throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass);
340
341 // Test only function
342 static jint test_deoptimize_call_int(JavaThread* thread, int value);
343 };
344
345 // Tracing macros.
346
347 #define IF_TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1)) ; else
348 #define IF_TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2)) ; else
349 #define IF_TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3)) ; else
350 #define IF_TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4)) ; else
351 #define IF_TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5)) ; else
352
353 #define TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1 && (tty->print(PTR_FORMAT " JVMCITrace-1: ", p2i(JavaThread::current())), true))) ; else tty->print_cr
354 #define TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2 && (tty->print(PTR_FORMAT " JVMCITrace-2: ", p2i(JavaThread::current())), true))) ; else tty->print_cr
355 #define TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3 && (tty->print(PTR_FORMAT " JVMCITrace-3: ", p2i(JavaThread::current())), true))) ; else tty->print_cr
356 #define TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4 && (tty->print(PTR_FORMAT " JVMCITrace-4: ", p2i(JavaThread::current())), true))) ; else tty->print_cr
357 #define TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5 && (tty->print(PTR_FORMAT " JVMCITrace-5: ", p2i(JavaThread::current())), true))) ; else tty->print_cr
358
359 #endif // SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
|
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 #ifndef SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
25 #define SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
26
27 #include "code/nmethod.hpp"
28 #include "jvmci/jvmci.hpp"
29 #include "jvmci/jvmciExceptions.hpp"
30 #include "jvmci/jvmciObject.hpp"
31
32 class JVMCIEnv;
33 class JVMCICompiler;
34 class JVMCICompileState;
35 class MetadataHandles;
36
37 // Encapsulates the JVMCI metadata for an nmethod.
38 // JVMCINMethodData objects are inlined into nmethods
39 // at nmethod::_jvmci_data_offset.
40 class JVMCINMethodData {
41 // Index for the HotSpotNmethod mirror in the nmethod's oops table.
42 // This is -1 if there is no mirror in the oops table.
43 int _nmethod_mirror_index;
44
45 // Is HotSpotNmethod.name non-null? If so, the value is
46 // embedded in the end of this object.
47 bool _has_name;
48
49 // Address of the failed speculations list to which a speculation
50 // is appended when it causes a deoptimization.
51 FailedSpeculation** _failed_speculations;
52
53 public:
54 // Computes the size of a JVMCINMethodData object
55 static int compute_size(const char* nmethod_mirror_name) {
70 // Gets the JVMCI name of the nmethod (which may be NULL).
71 const char* name() { return _has_name ? (char*)(((address) this) + sizeof(JVMCINMethodData)) : NULL; }
72
73 // Clears the HotSpotNmethod.address field in the mirror. If nm
74 // is dead, the HotSpotNmethod.entryPoint field is also cleared.
75 void invalidate_nmethod_mirror(nmethod* nm);
76
77 // Gets the mirror from nm's oops table.
78 oop get_nmethod_mirror(nmethod* nm, bool phantom_ref);
79
80 // Sets the mirror in nm's oops table.
81 void set_nmethod_mirror(nmethod* nm, oop mirror);
82
83 // Clears the mirror in nm's oops table.
84 void clear_nmethod_mirror(nmethod* nm);
85 };
86
87 // A top level class that represents an initialized JVMCI runtime.
88 // There is one instance of this class per HotSpotJVMCIRuntime object.
89 class JVMCIRuntime: public CHeapObj<mtJVMCI> {
90 friend class JVMCI;
91 public:
92 // Constants describing whether JVMCI wants to be able to adjust the compilation
93 // level selected for a method by the VM compilation policy and if so, based on
94 // what information about the method being schedule for compilation.
95 enum CompLevelAdjustment {
96 none = 0, // no adjustment
97 by_holder = 1, // adjust based on declaring class of method
98 by_full_signature = 2 // adjust based on declaring class, name and signature of method
99 };
100
101 private:
102
103 enum InitState {
104 uninitialized,
105 being_initialized,
106 fully_initialized
107 };
108
109 // Initialization state of this JVMCIRuntime.
110 InitState _init_state;
111
112 JVMCIObject _HotSpotJVMCIRuntime_instance;
113
114 // Result of calling JNI_CreateJavaVM in the JVMCI shared library.
115 // Must only be modified under JVMCI_lock.
116 volatile JavaVM* _shared_library_javavm;
117
118 // The HotSpot heap based runtime will have an id of -1 and the
119 // JVMCI shared library runtime will have an id of 0.
120 int _id;
121
122 // Handles to objects in the HotSpot heap.
123 OopStorage* _object_handles;
124
125 // Handles to Metadata objects.
126 MetadataHandles* _metadata_handles;
127
128 JVMCIObject create_jvmci_primitive_type(BasicType type, JVMCI_TRAPS);
129
130 // Implementation methods for loading and constant pool access.
131 static Klass* get_klass_by_name_impl(Klass*& accessing_klass,
132 const constantPoolHandle& cpool,
133 Symbol* klass_name,
134 bool require_local);
135 static Klass* get_klass_by_index_impl(const constantPoolHandle& cpool,
136 int klass_index,
137 bool& is_accessible,
138 Klass* loading_klass);
139 static void get_field_by_index_impl(InstanceKlass* loading_klass, fieldDescriptor& fd,
140 int field_index);
141 static methodHandle get_method_by_index_impl(const constantPoolHandle& cpool,
142 int method_index, Bytecodes::Code bc,
143 InstanceKlass* loading_klass);
144
145 // Helper methods
146 static bool check_klass_accessibility(Klass* accessing_klass, Klass* resolved_klass);
147 static methodHandle lookup_method(InstanceKlass* accessor,
148 Klass* holder,
149 Symbol* name,
150 Symbol* sig,
151 Bytecodes::Code bc,
152 constantTag tag);
153
154 static OopStorage* create_object_handles(int id);
155
156 public:
157 JVMCIRuntime(int id);
158
159 int id() const { return _id; }
160
161 // Ensures that a JVMCI shared library JavaVM exists for this runtime.
162 // If the JavaVM was created by this call, then the thread-local JNI
163 // interface pointer for the JavaVM is returned otherwise NULL is returned.
164 JNIEnv* init_shared_library_javavm();
165
166 // Determines if the JVMCI shared library JavaVM exists for this runtime.
167 bool has_shared_library_javavm() { return _shared_library_javavm != NULL; }
168
169 // Copies info about the JVMCI shared library JavaVM associated with this
170 // runtime into `info` as follows:
171 // {
172 // javaVM, // the {@code JavaVM*} value
173 // javaVM->functions->reserved0,
174 // javaVM->functions->reserved1,
175 // javaVM->functions->reserved2
176 // }
177 void init_JavaVM_info(jlongArray info, JVMCI_TRAPS);
178
179 // Wrappers for calling Invocation Interface functions on the
180 // JVMCI shared library JavaVM associated with this runtime.
181 // These wrappers ensure all required thread state transitions are performed.
182 jint AttachCurrentThread(JavaThread* thread, void **penv, void *args);
183 jint AttachCurrentThreadAsDaemon(JavaThread* thread, void **penv, void *args);
184 jint DetachCurrentThread(JavaThread* thread);
185 jint GetEnv(JavaThread* thread, void **penv, jint version);
186
187 // Compute offsets and construct any state required before executing JVMCI code.
188 void initialize(JVMCIEnv* jvmciEnv);
189
190 // Allocation and management of JNI global object handles.
191 jobject make_global(const Handle& obj);
192 void destroy_global(jobject handle);
193 bool is_global_handle(jobject handle);
194
195 // Allocation and management of matadata handles.
196 jmetadata allocate_handle(const methodHandle& handle);
197 jmetadata allocate_handle(const constantPoolHandle& handle);
198 void release_handle(jmetadata handle);
199
200 // Gets the HotSpotJVMCIRuntime instance for this runtime,
201 // initializing it first if necessary.
202 JVMCIObject get_HotSpotJVMCIRuntime(JVMCI_TRAPS);
203
204 bool is_HotSpotJVMCIRuntime_initialized() {
205 return _HotSpotJVMCIRuntime_instance.is_non_null();
206 }
207
208 // Gets the current HotSpotJVMCIRuntime instance for this runtime which
209 // may be a "null" JVMCIObject value.
210 JVMCIObject probe_HotSpotJVMCIRuntime() {
211 return _HotSpotJVMCIRuntime_instance;
212 }
213
214 // Trigger initialization of HotSpotJVMCIRuntime through JVMCI.getRuntime()
215 void initialize_JVMCI(JVMCI_TRAPS);
216
217 // Explicitly initialize HotSpotJVMCIRuntime itself
218 void initialize_HotSpotJVMCIRuntime(JVMCI_TRAPS);
219
220 void call_getCompiler(TRAPS);
221
222 // Shuts down this runtime by calling HotSpotJVMCIRuntime.shutdown().
223 void shutdown();
224
225 void bootstrap_finished(TRAPS);
226
227 // Look up a klass by name from a particular class loader (the accessor's).
228 // If require_local, result must be defined in that class loader, or NULL.
229 // If !require_local, a result from remote class loader may be reported,
230 // if sufficient class loader constraints exist such that initiating
231 // a class loading request from the given loader is bound to return
232 // the class defined in the remote loader (or throw an error).
233 //
234 // Return an unloaded klass if !require_local and no class at all is found.
235 //
236 // The CI treats a klass as loaded if it is consistently defined in
237 // another loader, even if it hasn't yet been loaded in all loaders
238 // that could potentially see it via delegation.
239 static Klass* get_klass_by_name(Klass* accessing_klass,
240 Symbol* klass_name,
241 bool require_local);
242
243 // Constant pool access.
244 static Klass* get_klass_by_index(const constantPoolHandle& cpool,
271 int entry_bci,
272 CodeOffsets* offsets,
273 int orig_pc_offset,
274 CodeBuffer* code_buffer,
275 int frame_words,
276 OopMapSet* oop_map_set,
277 ExceptionHandlerTable* handler_table,
278 ImplicitExceptionTable* implicit_exception_table,
279 AbstractCompiler* compiler,
280 DebugInformationRecorder* debug_info,
281 Dependencies* dependencies,
282 int compile_id,
283 bool has_unsafe_access,
284 bool has_wide_vector,
285 JVMCIObject compiled_code,
286 JVMCIObject nmethod_mirror,
287 FailedSpeculation** failed_speculations,
288 char* speculations,
289 int speculations_len);
290
291 // Exits the VM due to an unexpected exception.
292 static void exit_on_pending_exception(JVMCIEnv* JVMCIENV, const char* message);
293
294 static void describe_pending_hotspot_exception(JavaThread* THREAD, bool clear);
295
296 #define CHECK_EXIT THREAD); \
297 if (HAS_PENDING_EXCEPTION) { \
298 char buf[256]; \
299 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \
300 JVMCIRuntime::exit_on_pending_exception(NULL, buf); \
301 return; \
302 } \
303 (void)(0
304
305 #define CHECK_EXIT_(v) THREAD); \
306 if (HAS_PENDING_EXCEPTION) { \
307 char buf[256]; \
308 jio_snprintf(buf, 256, "Uncaught exception at %s:%d", __FILE__, __LINE__); \
309 JVMCIRuntime::exit_on_pending_exception(NULL, buf); \
310 return v; \
311 } \
374 static void log_primitive(JavaThread* thread, jchar typeChar, jlong value, jboolean newline);
375 // Print the passed in object, optionally followed by a newline. If
376 // as_string is true and the object is a java.lang.String then it
377 // printed as a string, otherwise the type of the object is printed
378 // followed by its address.
379 static void log_object(JavaThread* thread, oopDesc* object, bool as_string, bool newline);
380 #if INCLUDE_G1GC
381 static void write_barrier_pre(JavaThread* thread, oopDesc* obj);
382 static void write_barrier_post(JavaThread* thread, void* card);
383 #endif
384 static jboolean validate_object(JavaThread* thread, oopDesc* parent, oopDesc* child);
385
386 // used to throw exceptions from compiled JVMCI code
387 static int throw_and_post_jvmti_exception(JavaThread* thread, const char* exception, const char* message);
388 // helper methods to throw exception with complex messages
389 static int throw_klass_external_name_exception(JavaThread* thread, const char* exception, Klass* klass);
390 static int throw_class_cast_exception(JavaThread* thread, const char* exception, Klass* caster_klass, Klass* target_klass);
391
392 // Test only function
393 static jint test_deoptimize_call_int(JavaThread* thread, int value);
394
395 // Emits the following on tty:
396 // "JVMCITrace-" <level> "[" <current thread name> "]:" <padding of width `level`>
397 // Returns true.
398 static bool trace_prefix(int level);
399 };
400
401 // Tracing macros.
402
403 #define IF_TRACE_jvmci_1 if (!(JVMCITraceLevel >= 1)) ; else
404 #define IF_TRACE_jvmci_2 if (!(JVMCITraceLevel >= 2)) ; else
405 #define IF_TRACE_jvmci_3 if (!(JVMCITraceLevel >= 3)) ; else
406 #define IF_TRACE_jvmci_4 if (!(JVMCITraceLevel >= 4)) ; else
407 #define IF_TRACE_jvmci_5 if (!(JVMCITraceLevel >= 5)) ; else
408
409 #define TRACE_jvmci_(n) if (!(JVMCITraceLevel >= n && JVMCIRuntime::trace_prefix(n))) ; else tty->print_cr
410 #define TRACE_jvmci_1 TRACE_jvmci_(1)
411 #define TRACE_jvmci_2 TRACE_jvmci_(2)
412 #define TRACE_jvmci_3 TRACE_jvmci_(3)
413 #define TRACE_jvmci_4 TRACE_jvmci_(4)
414 #define TRACE_jvmci_5 TRACE_jvmci_(5)
415
416 #endif // SHARE_VM_JVMCI_JVMCI_RUNTIME_HPP
|