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 #endif // !ZERO
188 #endif // !CPU
189 
190 const char *Abstract_VM_Version::vm_platform_string() {
191   return OS "-" CPU;
192 }
193 
194 const char* Abstract_VM_Version::internal_vm_info_string() {
195   #ifndef HOTSPOT_BUILD_USER
196     #define HOTSPOT_BUILD_USER unknown
197   #endif
198 
199   #ifndef HOTSPOT_BUILD_COMPILER
200     #ifdef _MSC_VER
201       #if _MSC_VER == 1800
202         #define HOTSPOT_BUILD_COMPILER "MS VC++ 12.0 (VS2013)"
203       #elif _MSC_VER == 1900
204         #define HOTSPOT_BUILD_COMPILER "MS VC++ 14.0 (VS2015)"
205       #elif _MSC_VER == 1911
206         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.3 (VS2017)"
207       #elif _MSC_VER == 1912
208         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.5 (VS2017)"
209       #elif _MSC_VER == 1913
210         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.6 (VS2017)"
211       #elif _MSC_VER == 1914
212         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.7 (VS2017)"
213       #elif _MSC_VER == 1915
214         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.8 (VS2017)"
215       #elif _MSC_VER == 1916
216         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.9 (VS2017)"
217       #elif _MSC_VER == 1920
218         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.0 (VS2019)"
219       #elif _MSC_VER == 1921
220         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.1 (VS2019)"
221       #elif _MSC_VER == 1922
222         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.2 (VS2019)"
223       #elif _MSC_VER == 1923
224         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.3 (VS2019)"
225       #elif _MSC_VER == 1924
226         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.4 (VS2019)"
227       #elif _MSC_VER == 1925
228         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.5 (VS2019)"
229       #elif _MSC_VER == 1926
230         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.6 (VS2019)"
231       #elif _MSC_VER == 1927
232         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.7 (VS2019)"
233       #elif _MSC_VER == 1928
234         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.8 / 16.9 (VS2019)"
235       #elif _MSC_VER == 1929
236         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.10 / 16.11 (VS2019)"
237       #elif _MSC_VER == 1930
238         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.0 (VS2022)"
239       #elif _MSC_VER == 1931
240         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.1 (VS2022)"
241       #elif _MSC_VER == 1932
242         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.2 (VS2022)"
243       #elif _MSC_VER == 1933
244         #define HOTSPOT_BUILD_COMPILER "MS VC++ 17.3 (VS2022)"
245       #else
246         #define HOTSPOT_BUILD_COMPILER "unknown MS VC++:" XSTR(_MSC_VER)
247       #endif
248     #elif defined(__clang_version__)
249         #define HOTSPOT_BUILD_COMPILER "clang " __VERSION__
250     #elif defined(__GNUC__)
251         #define HOTSPOT_BUILD_COMPILER "gcc " __VERSION__
252     #else
253       #define HOTSPOT_BUILD_COMPILER "unknown compiler"
254     #endif
255   #endif
256 
257   #ifndef FLOAT_ARCH
258     #if defined(__SOFTFP__)
259       #define FLOAT_ARCH_STR "-sflt"
260     #else
261       #define FLOAT_ARCH_STR ""
262     #endif
263   #else
264     #define FLOAT_ARCH_STR XSTR(FLOAT_ARCH)
265   #endif
266 
267   #ifdef MUSL_LIBC
268     #define LIBC_STR "-" XSTR(LIBC)
269   #else
270     #define LIBC_STR ""
271   #endif
272 
273   #ifndef HOTSPOT_BUILD_TIME
274     #define HOTSPOT_BUILD_TIME __DATE__ " " __TIME__
275   #endif
276 
277   #define INTERNAL_VERSION_SUFFIX VM_RELEASE ")" \
278          " for " OS "-" CPU FLOAT_ARCH_STR LIBC_STR \
279          " JRE (" VERSION_STRING "), built on " HOTSPOT_BUILD_TIME \
280          " by " XSTR(HOTSPOT_BUILD_USER) " with " HOTSPOT_BUILD_COMPILER
281 
282   return strcmp(DEBUG_LEVEL, "release") == 0
283       ? VMNAME " (" INTERNAL_VERSION_SUFFIX
284       : VMNAME " (" DEBUG_LEVEL " " INTERNAL_VERSION_SUFFIX;
285 }
286 
287 const char *Abstract_VM_Version::vm_build_user() {
288   return HOTSPOT_BUILD_USER;
289 }
290 
291 const char *Abstract_VM_Version::jdk_debug_level() {
292   return DEBUG_LEVEL;
293 }
294 
295 const char *Abstract_VM_Version::printable_jdk_debug_level() {
296   // Debug level is not printed for "release" builds
297   return strcmp(DEBUG_LEVEL, "release") == 0 ? "" : DEBUG_LEVEL " ";
298 }
299 
300 unsigned int Abstract_VM_Version::jvm_version() {
301   return ((Abstract_VM_Version::vm_major_version() & 0xFF) << 24) |
302          ((Abstract_VM_Version::vm_minor_version() & 0xFF) << 16) |
303          ((Abstract_VM_Version::vm_security_version() & 0xFF) << 8) |
304          (Abstract_VM_Version::vm_build_number() & 0xFF);
305 }
306 
307 void Abstract_VM_Version::insert_features_names(char* buf, size_t buflen, const char* features_names[]) {
308   uint64_t features = _features;
309   uint features_names_index = 0;
310 
311   while (features != 0) {
312     if (features & 1) {
313       int res = jio_snprintf(buf, buflen, ", %s", features_names[features_names_index]);
314       assert(res > 0, "not enough temporary space allocated");
315       buf += res;
316       buflen -= res;
317     }
318     features >>= 1;
319     ++features_names_index;
320   }
321 }
322 
323 bool Abstract_VM_Version::print_matching_lines_from_file(const char* filename, outputStream* st, const char* keywords_to_match[]) {
324   char line[500];
325   FILE* fp = fopen(filename, "r");
326   if (fp == NULL) {
327     return false;
328   }
329 
330   st->print_cr("Virtualization information:");
331   while (fgets(line, sizeof(line), fp) != NULL) {
332     int i = 0;
333     while (keywords_to_match[i] != NULL) {
334       if (strncmp(line, keywords_to_match[i], strlen(keywords_to_match[i])) == 0) {
335         st->print("%s", line);
336         break;
337       }
338       i++;
339     }
340   }
341   fclose(fp);
342   return true;
343 }