1 /*
  2  * Copyright (c) 1998, 2025, 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 #include "cds/cdsConfig.hpp"
 26 #include "compiler/compilerDefinitions.hpp"
 27 #include "jvm_io.h"
 28 #include "runtime/arguments.hpp"
 29 #include "runtime/vm_version.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 
 32 const char* Abstract_VM_Version::_s_vm_release = Abstract_VM_Version::vm_release();
 33 const char* Abstract_VM_Version::_s_internal_vm_info_string = Abstract_VM_Version::internal_vm_info_string();
 34 
 35 uint64_t Abstract_VM_Version::_features = 0;
 36 const char* Abstract_VM_Version::_features_string = "";
 37 const char* Abstract_VM_Version::_cpu_info_string = "";
 38 uint64_t Abstract_VM_Version::_cpu_features = 0;
 39 
 40 #ifndef SUPPORTS_NATIVE_CX8
 41 bool Abstract_VM_Version::_supports_cx8 = false;
 42 #endif
 43 bool Abstract_VM_Version::_supports_atomic_getset4 = false;
 44 bool Abstract_VM_Version::_supports_atomic_getset8 = false;
 45 bool Abstract_VM_Version::_supports_atomic_getadd4 = false;
 46 bool Abstract_VM_Version::_supports_atomic_getadd8 = false;
 47 unsigned int Abstract_VM_Version::_logical_processors_per_package = 1U;
 48 unsigned int Abstract_VM_Version::_L1_data_cache_line_size = 0;
 49 unsigned int Abstract_VM_Version::_data_cache_line_flush_size = 0;
 50 
 51 VirtualizationType Abstract_VM_Version::_detected_virtualization = NoDetectedVirtualization;
 52 
 53 #ifndef HOTSPOT_VERSION_STRING
 54   #error HOTSPOT_VERSION_STRING must be defined
 55 #endif
 56 
 57 #ifndef VERSION_FEATURE
 58   #error VERSION_FEATURE must be defined
 59 #endif
 60 #ifndef VERSION_INTERIM
 61   #error VERSION_INTERIM must be defined
 62 #endif
 63 #ifndef VERSION_UPDATE
 64   #error VERSION_UPDATE must be defined
 65 #endif
 66 #ifndef VERSION_PATCH
 67   #error VERSION_PATCH must be defined
 68 #endif
 69 #ifndef VERSION_BUILD
 70   #error VERSION_BUILD must be defined
 71 #endif
 72 
 73 #ifndef VERSION_STRING
 74   #error VERSION_STRING must be defined
 75 #endif
 76 
 77 #ifndef DEBUG_LEVEL
 78   #error DEBUG_LEVEL must be defined
 79 #endif
 80 
 81 #ifndef HOTSPOT_BUILD_TIME
 82   #error HOTSPOT_BUILD_TIME must be defined
 83 #endif
 84 
 85 #ifndef JVM_VARIANT
 86   #error JVM_VARIANT must be defined
 87 #endif
 88 
 89 #define VM_RELEASE HOTSPOT_VERSION_STRING
 90 
 91 // HOTSPOT_VERSION_STRING equals the JDK VERSION_STRING (unless overridden
 92 // in a standalone build).
 93 int Abstract_VM_Version::_vm_major_version = VERSION_FEATURE;
 94 int Abstract_VM_Version::_vm_minor_version = VERSION_INTERIM;
 95 int Abstract_VM_Version::_vm_security_version = VERSION_UPDATE;
 96 int Abstract_VM_Version::_vm_patch_version = VERSION_PATCH;
 97 int Abstract_VM_Version::_vm_build_number = VERSION_BUILD;
 98 
 99 #if defined(_LP64)
100   #define VMLP "64-Bit "
101 #else
102   #define VMLP ""
103 #endif
104 
105 #ifndef VMTYPE
106   #if COMPILER1_AND_COMPILER2
107     #define VMTYPE "Server"
108   #else // COMPILER1_AND_COMPILER2
109   #ifdef ZERO
110     #define VMTYPE "Zero"
111   #else // ZERO
112      #define VMTYPE COMPILER1_PRESENT("Client")   \
113                     COMPILER2_PRESENT("Server")
114   #endif // ZERO
115   #endif // COMPILER1_AND_COMPILER2
116 #endif
117 
118 #ifndef HOTSPOT_VM_DISTRO
119   #error HOTSPOT_VM_DISTRO must be defined
120 #endif
121 #define VMNAME HOTSPOT_VM_DISTRO " " VMLP VMTYPE " VM"
122 
123 const char* Abstract_VM_Version::vm_name() {
124   return VMNAME;
125 }
126 
127 #ifndef VENDOR_PADDING
128 # define VENDOR_PADDING 64
129 #endif
130 #ifndef VENDOR
131 # define VENDOR  "Oracle Corporation"
132 #endif
133 
134 static const char vm_vendor_string[sizeof(VENDOR) < VENDOR_PADDING ? VENDOR_PADDING : sizeof(VENDOR)] = VENDOR;
135 
136 const char* Abstract_VM_Version::vm_vendor() {
137   return vm_vendor_string;
138 }
139 
140 
141 // The VM info string should be a constant, but its value cannot be finalized until after VM arguments
142 // have been fully processed. And we want to avoid dynamic memory allocation which will cause ASAN
143 // report error, so we enumerate all the cases by static const string value.
144 const char* Abstract_VM_Version::vm_info_string() {
145   switch (Arguments::mode()) {
146     case Arguments::_int:
147       if (is_vm_statically_linked()) {
148         return CDSConfig::is_using_archive() ? "interpreted mode, static, sharing" : "interpreted mode, static";
149       } else {
150         return CDSConfig::is_using_archive() ? "interpreted mode, sharing" : "interpreted mode";
151       }
152     case Arguments::_mixed:
153       if (is_vm_statically_linked()) {
154         if (CompilationModeFlag::quick_only()) {
155           return CDSConfig::is_using_archive() ? "mixed mode, emulated-client, static, sharing" : "mixed mode, emulated-client, static";
156         } else {
157           return CDSConfig::is_using_archive() ? "mixed mode, static, sharing" : "mixed mode, static";
158          }
159       } else {
160         if (CompilationModeFlag::quick_only()) {
161           return CDSConfig::is_using_archive() ? "mixed mode, emulated-client, sharing" : "mixed mode, emulated-client";
162         } else {
163           return CDSConfig::is_using_archive() ? "mixed mode, sharing" : "mixed mode";
164         }
165       }
166     case Arguments::_comp:
167       if (is_vm_statically_linked()) {
168         if (CompilationModeFlag::quick_only()) {
169           return CDSConfig::is_using_archive() ? "compiled mode, emulated-client, static, sharing" : "compiled mode, emulated-client, static";
170         }
171         return CDSConfig::is_using_archive() ? "compiled mode, static, sharing" : "compiled mode, static";
172       } else {
173         if (CompilationModeFlag::quick_only()) {
174           return CDSConfig::is_using_archive() ? "compiled mode, emulated-client, sharing" : "compiled mode, emulated-client";
175         }
176         return CDSConfig::is_using_archive() ? "compiled mode, sharing" : "compiled mode";
177       }
178   }
179   ShouldNotReachHere();
180   return "";
181 }
182 
183 // NOTE: do *not* use stringStream. this function is called by
184 //       fatal error handler. if the crash is in native thread,
185 //       stringStream cannot get resource allocated and will SEGV.
186 const char* Abstract_VM_Version::vm_release() {
187   return VM_RELEASE;
188 }
189 
190 #define OS       LINUX_ONLY("linux")             \
191                  WINDOWS_ONLY("windows")         \
192                  AIX_ONLY("aix")                 \
193                  BSD_ONLY("bsd")
194 
195 #ifndef CPU
196 #ifdef ZERO
197 #define CPU      ZERO_LIBARCH
198 #elif defined(PPC64)
199 #if defined(VM_LITTLE_ENDIAN)
200 #define CPU      "ppc64le"
201 #else
202 #define CPU      "ppc64"
203 #endif // PPC64
204 #else
205 #define CPU      AARCH64_ONLY("aarch64")         \
206                  AMD64_ONLY("amd64")             \
207                  IA32_ONLY("x86")                \
208                  S390_ONLY("s390")               \
209                  RISCV64_ONLY("riscv64")
210 #endif // !ZERO
211 #endif // !CPU
212 
213 const char *Abstract_VM_Version::vm_platform_string() {
214   return OS "-" CPU;
215 }
216 
217 const char* Abstract_VM_Version::vm_variant() {
218   return JVM_VARIANT;
219 }
220 
221 const char* Abstract_VM_Version::internal_vm_info_string() {
222   #ifndef HOTSPOT_BUILD_COMPILER
223     #ifdef _MSC_VER
224       #if _MSC_VER == 1911
225         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.3 (VS2017)"
226       #elif _MSC_VER == 1912
227         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.5 (VS2017)"
228       #elif _MSC_VER == 1913
229         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.6 (VS2017)"
230       #elif _MSC_VER == 1914
231         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.7 (VS2017)"
232       #elif _MSC_VER == 1915
233         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.8 (VS2017)"
234       #elif _MSC_VER == 1916
235         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.9 (VS2017)"
236       #elif _MSC_VER == 1920
237         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.0 (VS2019)"
238       #elif _MSC_VER == 1921
239         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.1 (VS2019)"
240       #elif _MSC_VER == 1922
241         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.2 (VS2019)"
242       #elif _MSC_VER == 1923
243         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.3 (VS2019)"
244       #elif _MSC_VER == 1924
245         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.4 (VS2019)"
246       #elif _MSC_VER == 1925
247         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.5 (VS2019)"
248       #elif _MSC_VER == 1926
249         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.6 (VS2019)"
250       #elif _MSC_VER == 1927
251         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.7 (VS2019)"
252       #elif _MSC_VER == 1928
253         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.8 / 16.9 (VS2019)"
254       #elif _MSC_VER == 1929
255         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.10 / 16.11 (VS2019)"
256       #elif _MSC_VER == 1930
257         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.0 (VS2022)"
258       #elif _MSC_VER == 1931
259         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.1 (VS2022)"
260       #elif _MSC_VER == 1932
261         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.2 (VS2022)"
262       #elif _MSC_VER == 1933
263         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.3 (VS2022)"
264       #elif _MSC_VER == 1934
265         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.4 (VS2022)"
266       #elif _MSC_VER == 1935
267         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.5 (VS2022)"
268       #elif _MSC_VER == 1936
269         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.6 (VS2022)"
270       #elif _MSC_VER == 1937
271         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.7 (VS2022)"
272       #elif _MSC_VER == 1938
273         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.8 (VS2022)"
274       #else
275         #define HOTSPOT_BUILD_COMPILER "unknown MS VC++:" XSTR(_MSC_VER)
276       #endif
277     #elif defined(__clang_version__)
278         #define HOTSPOT_BUILD_COMPILER "clang " __VERSION__
279     #elif defined(__GNUC__)
280         #define HOTSPOT_BUILD_COMPILER "gcc " __VERSION__
281     #else
282       #define HOTSPOT_BUILD_COMPILER "unknown compiler"
283     #endif
284   #endif
285 
286   #ifndef FLOAT_ARCH
287     #if defined(__SOFTFP__)
288       #define FLOAT_ARCH_STR "-sflt"
289     #else
290       #define FLOAT_ARCH_STR ""
291     #endif
292   #else
293     #define FLOAT_ARCH_STR XSTR(FLOAT_ARCH)
294   #endif
295 
296   #ifdef MUSL_LIBC
297     #define LIBC_STR "-" XSTR(LIBC)
298   #else
299     #define LIBC_STR ""
300   #endif
301 
302   #define INTERNAL_VERSION_SUFFIX VM_RELEASE ")" \
303          " for " OS "-" CPU FLOAT_ARCH_STR LIBC_STR \
304          " JRE (" VERSION_STRING "), built on " HOTSPOT_BUILD_TIME \
305          " with " HOTSPOT_BUILD_COMPILER
306 
307   return strcmp(DEBUG_LEVEL, "release") == 0
308       ? VMNAME " (" INTERNAL_VERSION_SUFFIX
309       : VMNAME " (" DEBUG_LEVEL " " INTERNAL_VERSION_SUFFIX;
310 }
311 
312 const char *Abstract_VM_Version::jdk_debug_level() {
313   return DEBUG_LEVEL;
314 }
315 
316 const char *Abstract_VM_Version::printable_jdk_debug_level() {
317   // Debug level is not printed for "release" builds
318   return strcmp(DEBUG_LEVEL, "release") == 0 ? "" : DEBUG_LEVEL " ";
319 }
320 
321 unsigned int Abstract_VM_Version::jvm_version() {
322   return ((Abstract_VM_Version::vm_major_version() & 0xFF) << 24) |
323          ((Abstract_VM_Version::vm_minor_version() & 0xFF) << 16) |
324          ((Abstract_VM_Version::vm_security_version() & 0xFF) << 8) |
325          (Abstract_VM_Version::vm_build_number() & 0xFF);
326 }
327 
328 const char* Abstract_VM_Version::extract_features_string(const char* cpu_info_string,
329                                                          size_t cpu_info_string_len,
330                                                          size_t features_offset) {
331   assert(features_offset <= cpu_info_string_len, "");
332   if (features_offset < cpu_info_string_len) {
333     assert(cpu_info_string[features_offset + 0] == ',', "");
334     assert(cpu_info_string[features_offset + 1] == ' ', "");
335     return cpu_info_string + features_offset + 2; // skip initial ", "
336   } else {
337     return ""; // empty
338   }
339 }
340 
341 bool Abstract_VM_Version::print_matching_lines_from_file(const char* filename, outputStream* st, const char* keywords_to_match[]) {
342   char line[500];
343   FILE* fp = os::fopen(filename, "r");
344   if (fp == nullptr) {
345     return false;
346   }
347 
348   st->print_cr("Virtualization information:");
349   while (fgets(line, sizeof(line), fp) != nullptr) {
350     int i = 0;
351     while (keywords_to_match[i] != nullptr) {
352       if (strncmp(line, keywords_to_match[i], strlen(keywords_to_match[i])) == 0) {
353         st->print("%s", line);
354         break;
355       }
356       i++;
357     }
358   }
359   fclose(fp);
360   return true;
361 }
362 
363 // Abstract_VM_Version statics
364 int   Abstract_VM_Version::_no_of_threads = 0;
365 int   Abstract_VM_Version::_no_of_cores = 0;
366 int   Abstract_VM_Version::_no_of_sockets = 0;
367 bool  Abstract_VM_Version::_initialized = false;
368 char  Abstract_VM_Version::_cpu_name[CPU_TYPE_DESC_BUF_SIZE] = {0};
369 char  Abstract_VM_Version::_cpu_desc[CPU_DETAILED_DESC_BUF_SIZE] = {0};
370 
371 int Abstract_VM_Version::number_of_threads(void) {
372   assert(_initialized, "should be initialized");
373   return _no_of_threads;
374 }
375 
376 int Abstract_VM_Version::number_of_cores(void) {
377   assert(_initialized, "should be initialized");
378   return _no_of_cores;
379 }
380 
381 int Abstract_VM_Version::number_of_sockets(void) {
382   assert(_initialized, "should be initialized");
383   return _no_of_sockets;
384 }
385 
386 const char* Abstract_VM_Version::cpu_name(void) {
387   assert(_initialized, "should be initialized");
388   char* tmp = NEW_C_HEAP_ARRAY_RETURN_NULL(char, CPU_TYPE_DESC_BUF_SIZE, mtTracing);
389   if (nullptr == tmp) {
390     return nullptr;
391   }
392   strncpy(tmp, _cpu_name, CPU_TYPE_DESC_BUF_SIZE);
393   return tmp;
394 }
395 
396 const char* Abstract_VM_Version::cpu_description(void) {
397   assert(_initialized, "should be initialized");
398   char* tmp = NEW_C_HEAP_ARRAY_RETURN_NULL(char, CPU_DETAILED_DESC_BUF_SIZE, mtTracing);
399   if (nullptr == tmp) {
400     return nullptr;
401   }
402   strncpy(tmp, _cpu_desc, CPU_DETAILED_DESC_BUF_SIZE);
403   return tmp;
404 }