1 /*
  2  * Copyright (c) 1998, 2022, 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 "precompiled.hpp"
 26 #include "jvm_io.h"
 27 #include "compiler/compilerDefinitions.hpp"
 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 
 38 bool Abstract_VM_Version::_supports_cx8 = false;
 39 bool Abstract_VM_Version::_supports_atomic_getset4 = false;
 40 bool Abstract_VM_Version::_supports_atomic_getset8 = false;
 41 bool Abstract_VM_Version::_supports_atomic_getadd4 = false;
 42 bool Abstract_VM_Version::_supports_atomic_getadd8 = false;
 43 unsigned int Abstract_VM_Version::_logical_processors_per_package = 1U;
 44 unsigned int Abstract_VM_Version::_L1_data_cache_line_size = 0;
 45 unsigned int Abstract_VM_Version::_data_cache_line_flush_size = 0;
 46 
 47 VirtualizationType Abstract_VM_Version::_detected_virtualization = NoDetectedVirtualization;
 48 
 49 #ifndef HOTSPOT_VERSION_STRING
 50   #error HOTSPOT_VERSION_STRING must be defined
 51 #endif
 52 
 53 #ifndef VERSION_FEATURE
 54   #error VERSION_FEATURE must be defined
 55 #endif
 56 #ifndef VERSION_INTERIM
 57   #error VERSION_INTERIM must be defined
 58 #endif
 59 #ifndef VERSION_UPDATE
 60   #error VERSION_UPDATE must be defined
 61 #endif
 62 #ifndef VERSION_PATCH
 63   #error VERSION_PATCH must be defined
 64 #endif
 65 #ifndef VERSION_BUILD
 66   #error VERSION_BUILD must be defined
 67 #endif
 68 
 69 #ifndef VERSION_STRING
 70   #error VERSION_STRING must be defined
 71 #endif
 72 
 73 #ifndef DEBUG_LEVEL
 74   #error DEBUG_LEVEL must be defined
 75 #endif
 76 
 77 #define VM_RELEASE HOTSPOT_VERSION_STRING
 78 
 79 // HOTSPOT_VERSION_STRING equals the JDK VERSION_STRING (unless overridden
 80 // in a standalone build).
 81 int Abstract_VM_Version::_vm_major_version = VERSION_FEATURE;
 82 int Abstract_VM_Version::_vm_minor_version = VERSION_INTERIM;
 83 int Abstract_VM_Version::_vm_security_version = VERSION_UPDATE;
 84 int Abstract_VM_Version::_vm_patch_version = VERSION_PATCH;
 85 int Abstract_VM_Version::_vm_build_number = VERSION_BUILD;
 86 
 87 #if defined(_LP64)
 88   #define VMLP "64-Bit "
 89 #else
 90   #define VMLP ""
 91 #endif
 92 
 93 #ifndef VMTYPE
 94   #if COMPILER1_AND_COMPILER2
 95     #define VMTYPE "Server"
 96   #else // COMPILER1_AND_COMPILER2
 97   #ifdef ZERO
 98     #define VMTYPE "Zero"
 99   #else // ZERO
100      #define VMTYPE COMPILER1_PRESENT("Client")   \
101                     COMPILER2_PRESENT("Server")
102   #endif // ZERO
103   #endif // COMPILER1_AND_COMPILER2
104 #endif
105 
106 #ifndef HOTSPOT_VM_DISTRO
107   #error HOTSPOT_VM_DISTRO must be defined
108 #endif
109 #define VMNAME HOTSPOT_VM_DISTRO " " VMLP VMTYPE " VM"
110 
111 const char* Abstract_VM_Version::vm_name() {
112   return VMNAME;
113 }
114 
115 
116 const char* Abstract_VM_Version::vm_vendor() {
117 #ifdef VENDOR
118   return VENDOR;
119 #else
120   return "Oracle Corporation";
121 #endif
122 }
123 
124 
125 const char* Abstract_VM_Version::vm_info_string() {
126   switch (Arguments::mode()) {
127     case Arguments::_int:
128       return UseSharedSpaces ? "interpreted mode, sharing" : "interpreted mode";
129     case Arguments::_mixed:
130       if (UseSharedSpaces) {
131         if (CompilationModeFlag::quick_only()) {
132           return "mixed mode, emulated-client, sharing";
133         } else {
134           return "mixed mode, sharing";
135          }
136       } else {
137         if (CompilationModeFlag::quick_only()) {
138           return "mixed mode, emulated-client";
139         } else {
140           return "mixed mode";
141         }
142       }
143     case Arguments::_comp:
144       if (CompilationModeFlag::quick_only()) {
145          return UseSharedSpaces ? "compiled mode, emulated-client, sharing" : "compiled mode, emulated-client";
146       }
147       return UseSharedSpaces ? "compiled mode, sharing" : "compiled mode";
148   }
149   ShouldNotReachHere();
150   return "";
151 }
152 
153 // NOTE: do *not* use stringStream. this function is called by
154 //       fatal error handler. if the crash is in native thread,
155 //       stringStream cannot get resource allocated and will SEGV.
156 const char* Abstract_VM_Version::vm_release() {
157   return VM_RELEASE;
158 }
159 
160 // NOTE: do *not* use stringStream. this function is called by
161 //       fatal error handlers. if the crash is in native thread,
162 //       stringStream cannot get resource allocated and will SEGV.
163 const char* Abstract_VM_Version::jre_release_version() {
164   return VERSION_STRING;
165 }
166 
167 #define OS       LINUX_ONLY("linux")             \
168                  WINDOWS_ONLY("windows")         \
169                  AIX_ONLY("aix")                 \
170                  BSD_ONLY("bsd")
171 
172 #ifndef CPU
173 #ifdef ZERO
174 #define CPU      ZERO_LIBARCH
175 #elif defined(PPC64)
176 #if defined(VM_LITTLE_ENDIAN)
177 #define CPU      "ppc64le"
178 #else
179 #define CPU      "ppc64"
180 #endif // PPC64
181 #else
182 #define CPU      AARCH64_ONLY("aarch64")         \
183                  AMD64_ONLY("amd64")             \
184                  IA32_ONLY("x86")                \
185                  IA64_ONLY("ia64")               \
186                  S390_ONLY("s390")               \
187                  RISCV64_ONLY("riscv64")
188 #endif // !ZERO
189 #endif // !CPU
190 
191 const char *Abstract_VM_Version::vm_platform_string() {
192   return OS "-" CPU;
193 }
194 
195 const char* Abstract_VM_Version::internal_vm_info_string() {
196   #ifndef HOTSPOT_BUILD_USER
197     #define HOTSPOT_BUILD_USER unknown
198   #endif
199 
200   #ifndef HOTSPOT_BUILD_COMPILER
201     #ifdef _MSC_VER
202       #if _MSC_VER == 1800
203         #define HOTSPOT_BUILD_COMPILER "MS VC++ 12.0 (VS2013)"
204       #elif _MSC_VER == 1900
205         #define HOTSPOT_BUILD_COMPILER "MS VC++ 14.0 (VS2015)"
206       #elif _MSC_VER == 1911
207         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.3 (VS2017)"
208       #elif _MSC_VER == 1912
209         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.5 (VS2017)"
210       #elif _MSC_VER == 1913
211         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.6 (VS2017)"
212       #elif _MSC_VER == 1914
213         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.7 (VS2017)"
214       #elif _MSC_VER == 1915
215         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.8 (VS2017)"
216       #elif _MSC_VER == 1916
217         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.9 (VS2017)"
218       #elif _MSC_VER == 1920
219         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.0 (VS2019)"
220       #elif _MSC_VER == 1921
221         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.1 (VS2019)"
222       #elif _MSC_VER == 1922
223         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.2 (VS2019)"
224       #elif _MSC_VER == 1923
225         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.3 (VS2019)"
226       #elif _MSC_VER == 1924
227         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.4 (VS2019)"
228       #elif _MSC_VER == 1925
229         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.5 (VS2019)"
230       #elif _MSC_VER == 1926
231         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.6 (VS2019)"
232       #elif _MSC_VER == 1927
233         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.7 (VS2019)"
234       #elif _MSC_VER == 1928
235         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.8 / 16.9 (VS2019)"
236       #elif _MSC_VER == 1929
237         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.10 / 16.11 (VS2019)"
238       #elif _MSC_VER == 1930
239         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.0 (VS2022)"
240       #elif _MSC_VER == 1931
241         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.1 (VS2022)"
242       #elif _MSC_VER == 1932
243         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.2 (VS2022)"
244       #elif _MSC_VER == 1933
245         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.3 (VS2022)"
246       #else
247         #define HOTSPOT_BUILD_COMPILER "unknown MS VC++:" XSTR(_MSC_VER)
248       #endif
249     #elif defined(__clang_version__)
250         #define HOTSPOT_BUILD_COMPILER "clang " __VERSION__
251     #elif defined(__GNUC__)
252         #define HOTSPOT_BUILD_COMPILER "gcc " __VERSION__
253     #else
254       #define HOTSPOT_BUILD_COMPILER "unknown compiler"
255     #endif
256   #endif
257 
258   #ifndef FLOAT_ARCH
259     #if defined(__SOFTFP__)
260       #define FLOAT_ARCH_STR "-sflt"
261     #else
262       #define FLOAT_ARCH_STR ""
263     #endif
264   #else
265     #define FLOAT_ARCH_STR XSTR(FLOAT_ARCH)
266   #endif
267 
268   #ifdef MUSL_LIBC
269     #define LIBC_STR "-" XSTR(LIBC)
270   #else
271     #define LIBC_STR ""
272   #endif
273 
274   #ifndef HOTSPOT_BUILD_TIME
275     #define HOTSPOT_BUILD_TIME __DATE__ " " __TIME__
276   #endif
277 
278   #define INTERNAL_VERSION_SUFFIX VM_RELEASE ")" \
279          " for " OS "-" CPU FLOAT_ARCH_STR LIBC_STR \
280          " JRE (" VERSION_STRING "), built on " HOTSPOT_BUILD_TIME \
281          " by " XSTR(HOTSPOT_BUILD_USER) " with " HOTSPOT_BUILD_COMPILER
282 
283   return strcmp(DEBUG_LEVEL, "release") == 0
284       ? VMNAME " (" INTERNAL_VERSION_SUFFIX
285       : VMNAME " (" DEBUG_LEVEL " " INTERNAL_VERSION_SUFFIX;
286 }
287 
288 const char *Abstract_VM_Version::vm_build_user() {
289   return HOTSPOT_BUILD_USER;
290 }
291 
292 const char *Abstract_VM_Version::jdk_debug_level() {
293   return DEBUG_LEVEL;
294 }
295 
296 const char *Abstract_VM_Version::printable_jdk_debug_level() {
297   // Debug level is not printed for "release" builds
298   return strcmp(DEBUG_LEVEL, "release") == 0 ? "" : DEBUG_LEVEL " ";
299 }
300 
301 unsigned int Abstract_VM_Version::jvm_version() {
302   return ((Abstract_VM_Version::vm_major_version() & 0xFF) << 24) |
303          ((Abstract_VM_Version::vm_minor_version() & 0xFF) << 16) |
304          ((Abstract_VM_Version::vm_security_version() & 0xFF) << 8) |
305          (Abstract_VM_Version::vm_build_number() & 0xFF);
306 }
307 
308 void Abstract_VM_Version::insert_features_names(char* buf, size_t buflen, const char* features_names[]) {
309   uint64_t features = _features;
310   uint features_names_index = 0;
311 
312   while (features != 0) {
313     if (features & 1) {
314       int res = jio_snprintf(buf, buflen, ", %s", features_names[features_names_index]);
315       assert(res > 0, "not enough temporary space allocated");
316       buf += res;
317       buflen -= res;
318     }
319     features >>= 1;
320     ++features_names_index;
321   }
322 }
323 
324 bool Abstract_VM_Version::print_matching_lines_from_file(const char* filename, outputStream* st, const char* keywords_to_match[]) {
325   char line[500];
326   FILE* fp = fopen(filename, "r");
327   if (fp == NULL) {
328     return false;
329   }
330 
331   st->print_cr("Virtualization information:");
332   while (fgets(line, sizeof(line), fp) != NULL) {
333     int i = 0;
334     while (keywords_to_match[i] != NULL) {
335       if (strncmp(line, keywords_to_match[i], strlen(keywords_to_match[i])) == 0) {
336         st->print("%s", line);
337         break;
338       }
339       i++;
340     }
341   }
342   fclose(fp);
343   return true;
344 }