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