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