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