1/*
2 * Copyright (c) 2011, 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// no precompiled headers
25#include "ci/ciUtilities.hpp"
26#include "gc/shared/barrierSet.hpp"
27#include "gc/shared/cardTable.hpp"
28#include "gc/shared/collectedHeap.hpp"
29#include "jvmci/jvmciEnv.hpp"
30#include "jvmci/jvmciCompilerToVM.hpp"
31#include "jvmci/vmStructs_jvmci.hpp"
32#include "memory/universe.hpp"
33#include "oops/compressedOops.hpp"
34#include "runtime/sharedRuntime.hpp"
35#include "utilities/resourceHash.hpp"
36
37
38int CompilerToVM::Data::Klass_vtable_start_offset;
39int CompilerToVM::Data::Klass_vtable_length_offset;
40
41int CompilerToVM::Data::Method_extra_stack_entries;
42
43address CompilerToVM::Data::SharedRuntime_ic_miss_stub;
44address CompilerToVM::Data::SharedRuntime_handle_wrong_method_stub;
45address CompilerToVM::Data::SharedRuntime_deopt_blob_unpack;
46address CompilerToVM::Data::SharedRuntime_deopt_blob_uncommon_trap;
47
48size_t CompilerToVM::Data::ThreadLocalAllocBuffer_alignment_reserve;
49
50CollectedHeap* CompilerToVM::Data::Universe_collectedHeap;
51int CompilerToVM::Data::Universe_base_vtable_size;
52address CompilerToVM::Data::Universe_narrow_oop_base;
53int CompilerToVM::Data::Universe_narrow_oop_shift;
54address CompilerToVM::Data::Universe_narrow_klass_base;
55int CompilerToVM::Data::Universe_narrow_klass_shift;
56void* CompilerToVM::Data::Universe_non_oop_bits;
57uintptr_t CompilerToVM::Data::Universe_verify_oop_mask;
58uintptr_t CompilerToVM::Data::Universe_verify_oop_bits;
59
60bool CompilerToVM::Data::_supports_inline_contig_alloc;
61HeapWord** CompilerToVM::Data::_heap_end_addr;
62HeapWord* volatile* CompilerToVM::Data::_heap_top_addr;
63int CompilerToVM::Data::_max_oop_map_stack_offset;
64int CompilerToVM::Data::_fields_annotations_base_offset;
65
66CardTable::CardValue* CompilerToVM::Data::cardtable_start_address;
67int CompilerToVM::Data::cardtable_shift;
68
69int CompilerToVM::Data::vm_page_size;
70
71int CompilerToVM::Data::sizeof_vtableEntry = sizeof(vtableEntry);
72int CompilerToVM::Data::sizeof_ExceptionTableElement = sizeof(ExceptionTableElement);
73int CompilerToVM::Data::sizeof_LocalVariableTableElement = sizeof(LocalVariableTableElement);
74int CompilerToVM::Data::sizeof_ConstantPool = sizeof(ConstantPool);
75int CompilerToVM::Data::sizeof_narrowKlass = sizeof(narrowKlass);
76int CompilerToVM::Data::sizeof_arrayOopDesc = sizeof(arrayOopDesc);
77int CompilerToVM::Data::sizeof_BasicLock = sizeof(BasicLock);
78
79address CompilerToVM::Data::dsin;
80address CompilerToVM::Data::dcos;
81address CompilerToVM::Data::dtan;
82address CompilerToVM::Data::dexp;
83address CompilerToVM::Data::dlog;
84address CompilerToVM::Data::dlog10;
85address CompilerToVM::Data::dpow;
86
87address CompilerToVM::Data::symbol_init;
88address CompilerToVM::Data::symbol_clinit;
89
90void CompilerToVM::Data::initialize(JVMCI_TRAPS) {
91 Klass_vtable_start_offset = in_bytes(Klass::vtable_start_offset());
92 Klass_vtable_length_offset = in_bytes(Klass::vtable_length_offset());
93
94 Method_extra_stack_entries = Method::extra_stack_entries();
95
96 SharedRuntime_ic_miss_stub = SharedRuntime::get_ic_miss_stub();
97 SharedRuntime_handle_wrong_method_stub = SharedRuntime::get_handle_wrong_method_stub();
98 SharedRuntime_deopt_blob_unpack = SharedRuntime::deopt_blob()->unpack();
99 SharedRuntime_deopt_blob_uncommon_trap = SharedRuntime::deopt_blob()->uncommon_trap();
100
101 ThreadLocalAllocBuffer_alignment_reserve = ThreadLocalAllocBuffer::alignment_reserve();
102
103 Universe_collectedHeap = Universe::heap();
104 Universe_base_vtable_size = Universe::base_vtable_size();
105 Universe_narrow_oop_base = CompressedOops::base();
106 Universe_narrow_oop_shift = CompressedOops::shift();
107 Universe_narrow_klass_base = CompressedKlassPointers::base();
108 Universe_narrow_klass_shift = CompressedKlassPointers::shift();
109 Universe_non_oop_bits = Universe::non_oop_word();
110 Universe_verify_oop_mask = Universe::verify_oop_mask();
111 Universe_verify_oop_bits = Universe::verify_oop_bits();
112
113 _supports_inline_contig_alloc = Universe::heap()->supports_inline_contig_alloc();
114 _heap_end_addr = _supports_inline_contig_alloc ? Universe::heap()->end_addr() : (HeapWord**) -1;
115 _heap_top_addr = _supports_inline_contig_alloc ? Universe::heap()->top_addr() : (HeapWord* volatile*) -1;
116
117 _max_oop_map_stack_offset = (OopMapValue::register_mask - VMRegImpl::stack2reg(0)->value()) * VMRegImpl::stack_slot_size;
118 int max_oop_map_stack_index = _max_oop_map_stack_offset / VMRegImpl::stack_slot_size;
119 assert(OopMapValue::legal_vm_reg_name(VMRegImpl::stack2reg(max_oop_map_stack_index)), "should be valid");
120 assert(!OopMapValue::legal_vm_reg_name(VMRegImpl::stack2reg(max_oop_map_stack_index + 1)), "should be invalid");
121
122 symbol_init = (address) vmSymbols::object_initializer_name();
123 symbol_clinit = (address) vmSymbols::class_initializer_name();
124
125 _fields_annotations_base_offset = Array<AnnotationArray*>::base_offset_in_bytes();
126
127 BarrierSet* bs = BarrierSet::barrier_set();
128 if (bs->is_a(BarrierSet::CardTableBarrierSet)) {
129 CardTable::CardValue* base = ci_card_table_address();
130 assert(base != NULL, "unexpected byte_map_base");
131 cardtable_start_address = base;
132 cardtable_shift = CardTable::card_shift;
133 } else {
134 // No card mark barriers
135 cardtable_start_address = 0;
136 cardtable_shift = 0;
137 }
138
139 vm_page_size = os::vm_page_size();
140
141#define SET_TRIGFUNC(name) \
142 if (StubRoutines::name() != NULL) { \
143 name = StubRoutines::name(); \
144 } else { \
145 name = CAST_FROM_FN_PTR(address, SharedRuntime::name); \
146 }
147
148 SET_TRIGFUNC(dsin);
149 SET_TRIGFUNC(dcos);
150 SET_TRIGFUNC(dtan);
151 SET_TRIGFUNC(dexp);
152 SET_TRIGFUNC(dlog10);
153 SET_TRIGFUNC(dlog);
154 SET_TRIGFUNC(dpow);
155
156#undef SET_TRIGFUNC
157}
158
159JVMCIObjectArray CompilerToVM::initialize_intrinsics(JVMCI_TRAPS) {
160 JVMCIObjectArray vmIntrinsics = JVMCIENV->new_VMIntrinsicMethod_array(vmIntrinsics::ID_LIMIT - 1, JVMCI_CHECK_NULL);
161 int index = 0;
162 vmSymbols::SID kls_sid = vmSymbols::NO_SID;
163 JVMCIObject kls_str;
164#define VM_SYMBOL_TO_STRING(s) \
165 JVMCIENV->create_string(vmSymbols::symbol_at(vmSymbols::VM_SYMBOL_ENUM_NAME(s)), JVMCI_CHECK_NULL)
166#define VM_INTRINSIC_INFO(id, kls, name, sig, ignore_fcode) { \
167 vmSymbols::SID sid = vmSymbols::VM_SYMBOL_ENUM_NAME(kls); \
168 if (kls_sid != sid) { \
169 kls_str = VM_SYMBOL_TO_STRING(kls); \
170 kls_sid = sid; \
171 } \
172 JVMCIObject name_str = VM_SYMBOL_TO_STRING(name); \
173 JVMCIObject sig_str = VM_SYMBOL_TO_STRING(sig); \
174 JVMCIObject vmIntrinsicMethod = JVMCIENV->new_VMIntrinsicMethod(kls_str, name_str, sig_str, (jint) vmIntrinsics::id, JVMCI_CHECK_NULL); \
175 JVMCIENV->put_object_at(vmIntrinsics, index++, vmIntrinsicMethod); \
176 }
177
178 VM_INTRINSICS_DO(VM_INTRINSIC_INFO, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE)
179#undef VM_SYMBOL_TO_STRING
180#undef VM_INTRINSIC_INFO
181 assert(index == vmIntrinsics::ID_LIMIT - 1, "must be");
182
183 return vmIntrinsics;
184}
185
186#define PREDEFINED_CONFIG_FLAGS(do_bool_flag, do_intx_flag, do_uintx_flag) \
187 do_intx_flag(AllocateInstancePrefetchLines) \
188 do_intx_flag(AllocatePrefetchDistance) \
189 do_intx_flag(AllocatePrefetchInstr) \
190 do_intx_flag(AllocatePrefetchLines) \
191 do_intx_flag(AllocatePrefetchStepSize) \
192 do_intx_flag(AllocatePrefetchStyle) \
193 do_intx_flag(BciProfileWidth) \
194 do_bool_flag(BootstrapJVMCI) \
195 do_bool_flag(CITime) \
196 do_bool_flag(CITimeEach) \
197 do_uintx_flag(CodeCacheSegmentSize) \
198 do_intx_flag(CodeEntryAlignment) \
199 do_bool_flag(CompactFields) \
200 do_intx_flag(ContendedPaddingWidth) \
201 do_bool_flag(DontCompileHugeMethods) \
202 do_bool_flag(EagerJVMCI) \
203 do_bool_flag(EnableContended) \
204 do_intx_flag(FieldsAllocationStyle) \
205 do_bool_flag(FoldStableValues) \
206 do_bool_flag(ForceUnreachable) \
207 do_intx_flag(HugeMethodLimit) \
208 do_bool_flag(Inline) \
209 do_intx_flag(JVMCICounterSize) \
210 do_bool_flag(JVMCIPrintProperties) \
211 do_bool_flag(JVMCIUseFastLocking) \
212 do_intx_flag(MethodProfileWidth) \
213 do_intx_flag(ObjectAlignmentInBytes) \
214 do_bool_flag(PrintInlining) \
215 do_bool_flag(ReduceInitialCardMarks) \
216 do_bool_flag(RestrictContended) \
217 do_intx_flag(StackReservedPages) \
218 do_intx_flag(StackShadowPages) \
219 do_bool_flag(TLABStats) \
220 do_uintx_flag(TLABWasteIncrement) \
221 do_intx_flag(TypeProfileWidth) \
222 do_bool_flag(UseAESIntrinsics) \
223 X86_ONLY(do_intx_flag(UseAVX)) \
224 do_bool_flag(UseBiasedLocking) \
225 do_bool_flag(UseCRC32Intrinsics) \
226 do_bool_flag(UseCompressedClassPointers) \
227 do_bool_flag(UseCompressedOops) \
228 X86_ONLY(do_bool_flag(UseCountLeadingZerosInstruction)) \
229 X86_ONLY(do_bool_flag(UseCountTrailingZerosInstruction)) \
230 do_bool_flag(UseConcMarkSweepGC) \
231 do_bool_flag(UseG1GC) \
232 do_bool_flag(UseParallelGC) \
233 do_bool_flag(UseParallelOldGC) \
234 do_bool_flag(UseSerialGC) \
235 do_bool_flag(UseZGC) \
236 do_bool_flag(UseEpsilonGC) \
237 COMPILER2_PRESENT(do_bool_flag(UseMontgomeryMultiplyIntrinsic)) \
238 COMPILER2_PRESENT(do_bool_flag(UseMontgomerySquareIntrinsic)) \
239 COMPILER2_PRESENT(do_bool_flag(UseMulAddIntrinsic)) \
240 COMPILER2_PRESENT(do_bool_flag(UseMultiplyToLenIntrinsic)) \
241 do_bool_flag(UsePopCountInstruction) \
242 do_bool_flag(UseSHA1Intrinsics) \
243 do_bool_flag(UseSHA256Intrinsics) \
244 do_bool_flag(UseSHA512Intrinsics) \
245 do_intx_flag(UseSSE) \
246 COMPILER2_PRESENT(do_bool_flag(UseSquareToLenIntrinsic)) \
247 do_bool_flag(UseStackBanging) \
248 do_bool_flag(UseTLAB) \
249 do_bool_flag(VerifyOops) \
250
251#define BOXED_BOOLEAN(name, value) name = ((jboolean)(value) ? boxedTrue : boxedFalse)
252#define BOXED_DOUBLE(name, value) do { jvalue p; p.d = (jdouble) (value); name = JVMCIENV->create_box(T_DOUBLE, &p, JVMCI_CHECK_NULL);} while(0)
253#define BOXED_LONG(name, value) \
254 do { \
255 jvalue p; p.j = (jlong) (value); \
256 JVMCIObject* e = longs.get(p.j); \
257 if (e == NULL) { \
258 JVMCIObject h = JVMCIENV->create_box(T_LONG, &p, JVMCI_CHECK_NULL); \
259 longs.put(p.j, h); \
260 name = h; \
261 } else { \
262 name = (*e); \
263 } \
264 } while (0)
265
266#define CSTRING_TO_JSTRING(name, value) \
267 JVMCIObject name; \
268 do { \
269 if (value != NULL) { \
270 JVMCIObject* e = strings.get(value); \
271 if (e == NULL) { \
272 JVMCIObject h = JVMCIENV->create_string(value, JVMCI_CHECK_NULL); \
273 strings.put(value, h); \
274 name = h; \
275 } else { \
276 name = (*e); \
277 } \
278 } \
279 } while (0)
280
281jobjectArray readConfiguration0(JNIEnv *env, JVMCI_TRAPS) {
282 Thread* THREAD = Thread::current();
283 ResourceHashtable<jlong, JVMCIObject> longs;
284 ResourceHashtable<const char*, JVMCIObject, &CompilerToVM::cstring_hash, &CompilerToVM::cstring_equals> strings;
285
286 jvalue prim;
287 prim.z = true; JVMCIObject boxedTrue = JVMCIENV->create_box(T_BOOLEAN, &prim, JVMCI_CHECK_NULL);
288 prim.z = false; JVMCIObject boxedFalse = JVMCIENV->create_box(T_BOOLEAN, &prim, JVMCI_CHECK_NULL);
289
290 CompilerToVM::Data::initialize(JVMCI_CHECK_NULL);
291
292 JVMCIENV->VMField_initialize(JVMCI_CHECK_NULL);
293 JVMCIENV->VMFlag_initialize(JVMCI_CHECK_NULL);
294 JVMCIENV->VMIntrinsicMethod_initialize(JVMCI_CHECK_NULL);
295
296 int len = JVMCIVMStructs::localHotSpotVMStructs_count();
297 JVMCIObjectArray vmFields = JVMCIENV->new_VMField_array(len, JVMCI_CHECK_NULL);
298 for (int i = 0; i < len ; i++) {
299 VMStructEntry vmField = JVMCIVMStructs::localHotSpotVMStructs[i];
300 size_t name_buf_len = strlen(vmField.typeName) + strlen(vmField.fieldName) + 2 /* "::" */;
301 char* name_buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, name_buf_len + 1);
302 sprintf(name_buf, "%s::%s", vmField.typeName, vmField.fieldName);
303 CSTRING_TO_JSTRING(name, name_buf);
304 CSTRING_TO_JSTRING(type, vmField.typeString);
305 JVMCIObject box;
306 if (vmField.isStatic && vmField.typeString != NULL) {
307 if (strcmp(vmField.typeString, "bool") == 0) {
308 BOXED_BOOLEAN(box, *(jbyte*) vmField.address);
309 assert(box.is_non_null(), "must have a box");
310 } else if (strcmp(vmField.typeString, "int") == 0 ||
311 strcmp(vmField.typeString, "jint") == 0) {
312 BOXED_LONG(box, *(jint*) vmField.address);
313 assert(box.is_non_null(), "must have a box");
314 } else if (strcmp(vmField.typeString, "uint64_t") == 0) {
315 BOXED_LONG(box, *(uint64_t*) vmField.address);
316 assert(box.is_non_null(), "must have a box");
317 } else if (strcmp(vmField.typeString, "address") == 0 ||
318 strcmp(vmField.typeString, "intptr_t") == 0 ||
319 strcmp(vmField.typeString, "uintptr_t") == 0 ||
320 strcmp(vmField.typeString, "OopHandle") == 0 ||
321 strcmp(vmField.typeString, "size_t") == 0 ||
322 // All foo* types are addresses.
323 vmField.typeString[strlen(vmField.typeString) - 1] == '*') {
324 BOXED_LONG(box, *((address*) vmField.address));
325 assert(box.is_non_null(), "must have a box");
326 } else {
327 JVMCI_ERROR_NULL("VM field %s has unsupported type %s", name_buf, vmField.typeString);
328 }
329 }
330 JVMCIObject vmFieldObj = JVMCIENV->new_VMField(name, type, vmField.offset, (jlong) vmField.address, box, JVMCI_CHECK_NULL);
331 JVMCIENV->put_object_at(vmFields, i, vmFieldObj);
332 }
333
334 int ints_len = JVMCIVMStructs::localHotSpotVMIntConstants_count();
335 int longs_len = JVMCIVMStructs::localHotSpotVMLongConstants_count();
336 len = ints_len + longs_len;
337 JVMCIObjectArray vmConstants = JVMCIENV->new_Object_array(len * 2, JVMCI_CHECK_NULL);
338 int insert = 0;
339 for (int i = 0; i < ints_len ; i++) {
340 VMIntConstantEntry c = JVMCIVMStructs::localHotSpotVMIntConstants[i];
341 CSTRING_TO_JSTRING(name, c.name);
342 JVMCIObject value;
343 BOXED_LONG(value, c.value);
344 JVMCIENV->put_object_at(vmConstants, insert++, name);
345 JVMCIENV->put_object_at(vmConstants, insert++, value);
346 }
347 for (int i = 0; i < longs_len ; i++) {
348 VMLongConstantEntry c = JVMCIVMStructs::localHotSpotVMLongConstants[i];
349 CSTRING_TO_JSTRING(name, c.name);
350 JVMCIObject value;
351 BOXED_LONG(value, c.value);
352 JVMCIENV->put_object_at(vmConstants, insert++, name);
353 JVMCIENV->put_object_at(vmConstants, insert++, value);
354 }
355 assert(insert == len * 2, "must be");
356
357 len = JVMCIVMStructs::localHotSpotVMAddresses_count();
358 JVMCIObjectArray vmAddresses = JVMCIENV->new_Object_array(len * 2, JVMCI_CHECK_NULL);
359 for (int i = 0; i < len ; i++) {
360 VMAddressEntry a = JVMCIVMStructs::localHotSpotVMAddresses[i];
361 CSTRING_TO_JSTRING(name, a.name);
362 JVMCIObject value;
363 BOXED_LONG(value, a.value);
364 JVMCIENV->put_object_at(vmAddresses, i * 2, name);
365 JVMCIENV->put_object_at(vmAddresses, i * 2 + 1, value);
366 }
367
368#define COUNT_FLAG(ignore) +1
369#ifdef ASSERT
370#define CHECK_FLAG(type, name) { \
371 JVMFlag* flag = JVMFlag::find_flag(#name, strlen(#name), /*allow_locked*/ true, /* return_flag */ true); \
372 assert(flag != NULL, "No such flag named " #name); \
373 assert(flag->is_##type(), "JVMFlag " #name " is not of type " #type); \
374}
375#else
376#define CHECK_FLAG(type, name)
377#endif
378
379#define ADD_FLAG(type, name, convert) { \
380 CHECK_FLAG(type, name) \
381 CSTRING_TO_JSTRING(fname, #name); \
382 CSTRING_TO_JSTRING(ftype, #type); \
383 convert(value, name); \
384 JVMCIObject vmFlagObj = JVMCIENV->new_VMFlag(fname, ftype, value, JVMCI_CHECK_NULL); \
385 JVMCIENV->put_object_at(vmFlags, i++, vmFlagObj); \
386}
387#define ADD_BOOL_FLAG(name) ADD_FLAG(bool, name, BOXED_BOOLEAN)
388#define ADD_INTX_FLAG(name) ADD_FLAG(intx, name, BOXED_LONG)
389#define ADD_UINTX_FLAG(name) ADD_FLAG(uintx, name, BOXED_LONG)
390
391 len = 0 + PREDEFINED_CONFIG_FLAGS(COUNT_FLAG, COUNT_FLAG, COUNT_FLAG);
392 JVMCIObjectArray vmFlags = JVMCIENV->new_VMFlag_array(len, JVMCI_CHECK_NULL);
393 int i = 0;
394 JVMCIObject value;
395 PREDEFINED_CONFIG_FLAGS(ADD_BOOL_FLAG, ADD_INTX_FLAG, ADD_UINTX_FLAG)
396
397 JVMCIObjectArray vmIntrinsics = CompilerToVM::initialize_intrinsics(JVMCI_CHECK_NULL);
398
399 JVMCIObjectArray data = JVMCIENV->new_Object_array(5, JVMCI_CHECK_NULL);
400 JVMCIENV->put_object_at(data, 0, vmFields);
401 JVMCIENV->put_object_at(data, 1, vmConstants);
402 JVMCIENV->put_object_at(data, 2, vmAddresses);
403 JVMCIENV->put_object_at(data, 3, vmFlags);
404 JVMCIENV->put_object_at(data, 4, vmIntrinsics);
405
406 return JVMCIENV->get_jobjectArray(data);
407}
408