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_intx_flag(ObjectAlignmentInBytes) \ 221 do_bool_flag(PrintInlining) \ 222 do_bool_flag(ReduceInitialCardMarks) \ 223 do_bool_flag(RestrictContended) \ 224 do_intx_flag(StackReservedPages) \ 225 do_intx_flag(StackShadowPages) \ 226 do_bool_flag(TLABStats) \ 227 do_uintx_flag(TLABWasteIncrement) \ 228 do_intx_flag(TypeProfileWidth) \ 229 do_bool_flag(UseAESIntrinsics) \ 230 X86_ONLY(do_intx_flag(UseAVX)) \ 231 do_bool_flag(UseBiasedLocking) \ 232 do_bool_flag(UseCRC32Intrinsics) \ 233 do_bool_flag(UseAdler32Intrinsics) \ 234 do_bool_flag(UseCompressedClassPointers) \ 235 do_bool_flag(UseCompressedOops) \ 236 X86_ONLY(do_bool_flag(UseCountLeadingZerosInstruction)) \ 237 X86_ONLY(do_bool_flag(UseCountTrailingZerosInstruction)) \ 238 do_bool_flag(UseG1GC) \ 239 do_bool_flag(UseParallelGC) \ 240 do_bool_flag(UseSerialGC) \ 241 do_bool_flag(UseZGC) \ 242 do_bool_flag(UseEpsilonGC) \ 243 COMPILER2_PRESENT(do_bool_flag(UseMontgomeryMultiplyIntrinsic)) \ 244 COMPILER2_PRESENT(do_bool_flag(UseMontgomerySquareIntrinsic)) \ 245 COMPILER2_PRESENT(do_bool_flag(UseMulAddIntrinsic)) \ 246 COMPILER2_PRESENT(do_bool_flag(UseMultiplyToLenIntrinsic)) \ 247 do_bool_flag(UsePopCountInstruction) \ 248 do_bool_flag(UseSHA1Intrinsics) \ 249 do_bool_flag(UseSHA256Intrinsics) \ 250 do_bool_flag(UseSHA512Intrinsics) \ 251 X86_ONLY(do_intx_flag(UseSSE)) \ 252 COMPILER2_PRESENT(do_bool_flag(UseSquareToLenIntrinsic)) \ 253 do_bool_flag(UseTLAB) \ 254 do_bool_flag(VerifyOops) \ 255 256 #define BOXED_BOOLEAN(name, value) name = ((jboolean)(value) ? boxedTrue : boxedFalse) 257 #define BOXED_DOUBLE(name, value) do { jvalue p; p.d = (jdouble) (value); name = JVMCIENV->create_box(T_DOUBLE, &p, JVMCI_CHECK_NULL);} while(0) 258 #define BOXED_LONG(name, value) \ 259 do { \ 260 jvalue p; p.j = (jlong) (value); \ 261 JVMCIObject* e = longs.get(p.j); \ 262 if (e == NULL) { \ 263 JVMCIObject h = JVMCIENV->create_box(T_LONG, &p, JVMCI_CHECK_NULL); \ 264 longs.put(p.j, h); \ 265 name = h; \ 266 } else { \ 267 name = (*e); \ 268 } \ 269 } while (0) 270 271 #define CSTRING_TO_JSTRING(name, value) \ 272 JVMCIObject name; \ 273 do { \ 274 if (value != NULL) { \ 275 JVMCIObject* e = strings.get(value); \ 276 if (e == NULL) { \ 277 JVMCIObject h = JVMCIENV->create_string(value, JVMCI_CHECK_NULL); \ 278 strings.put(value, h); \ 279 name = h; \ 280 } else { \ 281 name = (*e); \ 282 } \ 283 } \ 284 } while (0) 285 286 jobjectArray readConfiguration0(JNIEnv *env, JVMCI_TRAPS) { 287 JavaThread* THREAD = JavaThread::current(); // For exception macros. 288 ResourceHashtable<jlong, JVMCIObject> longs; 289 ResourceHashtable<const char*, JVMCIObject, &CompilerToVM::cstring_hash, &CompilerToVM::cstring_equals> strings; 290 291 jvalue prim; 292 prim.z = true; JVMCIObject boxedTrue = JVMCIENV->create_box(T_BOOLEAN, &prim, JVMCI_CHECK_NULL); 293 prim.z = false; JVMCIObject boxedFalse = JVMCIENV->create_box(T_BOOLEAN, &prim, JVMCI_CHECK_NULL); 294 295 CompilerToVM::Data::initialize(JVMCI_CHECK_NULL); 296 297 JVMCIENV->VMField_initialize(JVMCI_CHECK_NULL); 298 JVMCIENV->VMFlag_initialize(JVMCI_CHECK_NULL); 299 JVMCIENV->VMIntrinsicMethod_initialize(JVMCI_CHECK_NULL); 300 301 int len = JVMCIVMStructs::localHotSpotVMStructs_count(); 302 JVMCIObjectArray vmFields = JVMCIENV->new_VMField_array(len, JVMCI_CHECK_NULL); 303 for (int i = 0; i < len ; i++) { 304 VMStructEntry vmField = JVMCIVMStructs::localHotSpotVMStructs[i]; 305 size_t name_buf_len = strlen(vmField.typeName) + strlen(vmField.fieldName) + 2 /* "::" */; 306 char* name_buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, name_buf_len + 1); 307 sprintf(name_buf, "%s::%s", vmField.typeName, vmField.fieldName); 308 CSTRING_TO_JSTRING(name, name_buf); 309 CSTRING_TO_JSTRING(type, vmField.typeString); 310 JVMCIObject box; 311 if (vmField.isStatic && vmField.typeString != NULL) { 312 if (strcmp(vmField.typeString, "bool") == 0) { 313 BOXED_BOOLEAN(box, *(jbyte*) vmField.address); 314 assert(box.is_non_null(), "must have a box"); 315 } else if (strcmp(vmField.typeString, "int") == 0 || 316 strcmp(vmField.typeString, "jint") == 0 || 317 strcmp(vmField.typeString, "uint32_t") == 0) { 318 BOXED_LONG(box, *(jint*) vmField.address); 319 assert(box.is_non_null(), "must have a box"); 320 } else if (strcmp(vmField.typeString, "uint64_t") == 0) { 321 BOXED_LONG(box, *(uint64_t*) vmField.address); 322 assert(box.is_non_null(), "must have a box"); 323 } else if (strcmp(vmField.typeString, "address") == 0 || 324 strcmp(vmField.typeString, "intptr_t") == 0 || 325 strcmp(vmField.typeString, "uintptr_t") == 0 || 326 strcmp(vmField.typeString, "OopHandle") == 0 || 327 strcmp(vmField.typeString, "size_t") == 0 || 328 // All foo* types are addresses. 329 vmField.typeString[strlen(vmField.typeString) - 1] == '*') { 330 BOXED_LONG(box, *((address*) vmField.address)); 331 assert(box.is_non_null(), "must have a box"); 332 } else { 333 JVMCI_ERROR_NULL("VM field %s has unsupported type %s", name_buf, vmField.typeString); 334 } 335 } 336 JVMCIObject vmFieldObj = JVMCIENV->new_VMField(name, type, vmField.offset, (jlong) vmField.address, box, JVMCI_CHECK_NULL); 337 JVMCIENV->put_object_at(vmFields, i, vmFieldObj); 338 } 339 340 int ints_len = JVMCIVMStructs::localHotSpotVMIntConstants_count(); 341 int longs_len = JVMCIVMStructs::localHotSpotVMLongConstants_count(); 342 len = ints_len + longs_len; 343 JVMCIObjectArray vmConstants = JVMCIENV->new_Object_array(len * 2, JVMCI_CHECK_NULL); 344 int insert = 0; 345 for (int i = 0; i < ints_len ; i++) { 346 VMIntConstantEntry c = JVMCIVMStructs::localHotSpotVMIntConstants[i]; 347 CSTRING_TO_JSTRING(name, c.name); 348 JVMCIObject value; 349 BOXED_LONG(value, c.value); 350 JVMCIENV->put_object_at(vmConstants, insert++, name); 351 JVMCIENV->put_object_at(vmConstants, insert++, value); 352 } 353 for (int i = 0; i < longs_len ; i++) { 354 VMLongConstantEntry c = JVMCIVMStructs::localHotSpotVMLongConstants[i]; 355 CSTRING_TO_JSTRING(name, c.name); 356 JVMCIObject value; 357 BOXED_LONG(value, c.value); 358 JVMCIENV->put_object_at(vmConstants, insert++, name); 359 JVMCIENV->put_object_at(vmConstants, insert++, value); 360 } 361 assert(insert == len * 2, "must be"); 362 363 len = JVMCIVMStructs::localHotSpotVMAddresses_count(); 364 JVMCIObjectArray vmAddresses = JVMCIENV->new_Object_array(len * 2, JVMCI_CHECK_NULL); 365 for (int i = 0; i < len ; i++) { 366 VMAddressEntry a = JVMCIVMStructs::localHotSpotVMAddresses[i]; 367 CSTRING_TO_JSTRING(name, a.name); 368 JVMCIObject value; 369 BOXED_LONG(value, a.value); 370 JVMCIENV->put_object_at(vmAddresses, i * 2, name); 371 JVMCIENV->put_object_at(vmAddresses, i * 2 + 1, value); 372 } 373 374 #define COUNT_FLAG(ignore) +1 375 #ifdef ASSERT 376 #define CHECK_FLAG(type, name) { \ 377 const JVMFlag* flag = JVMFlag::find_declared_flag(#name); \ 378 assert(flag != NULL, "No such flag named " #name); \ 379 assert(flag->is_##type(), "JVMFlag " #name " is not of type " #type); \ 380 } 381 #else 382 #define CHECK_FLAG(type, name) 383 #endif 384 385 #define ADD_FLAG(type, name, convert) { \ 386 CHECK_FLAG(type, name) \ 387 CSTRING_TO_JSTRING(fname, #name); \ 388 CSTRING_TO_JSTRING(ftype, #type); \ 389 convert(value, name); \ 390 JVMCIObject vmFlagObj = JVMCIENV->new_VMFlag(fname, ftype, value, JVMCI_CHECK_NULL); \ 391 JVMCIENV->put_object_at(vmFlags, i++, vmFlagObj); \ 392 } 393 #define ADD_BOOL_FLAG(name) ADD_FLAG(bool, name, BOXED_BOOLEAN) 394 #define ADD_INTX_FLAG(name) ADD_FLAG(intx, name, BOXED_LONG) 395 #define ADD_UINTX_FLAG(name) ADD_FLAG(uintx, name, BOXED_LONG) 396 397 len = 0 + PREDEFINED_CONFIG_FLAGS(COUNT_FLAG, COUNT_FLAG, COUNT_FLAG); 398 JVMCIObjectArray vmFlags = JVMCIENV->new_VMFlag_array(len, JVMCI_CHECK_NULL); 399 int i = 0; 400 JVMCIObject value; 401 PREDEFINED_CONFIG_FLAGS(ADD_BOOL_FLAG, ADD_INTX_FLAG, ADD_UINTX_FLAG) 402 403 JVMCIObjectArray vmIntrinsics = CompilerToVM::initialize_intrinsics(JVMCI_CHECK_NULL); 404 405 JVMCIObjectArray data = JVMCIENV->new_Object_array(5, JVMCI_CHECK_NULL); 406 JVMCIENV->put_object_at(data, 0, vmFields); 407 JVMCIENV->put_object_at(data, 1, vmConstants); 408 JVMCIENV->put_object_at(data, 2, vmAddresses); 409 JVMCIENV->put_object_at(data, 3, vmFlags); 410 JVMCIENV->put_object_at(data, 4, vmIntrinsics); 411 412 return JVMCIENV->get_jobjectArray(data); 413 }