1 /*
  2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "runtime/java.hpp"
 28 #include "runtime/os.hpp"
 29 #include "runtime/vm_version.hpp"
 30 #include "utilities/formatBuffer.hpp"
 31 #include "utilities/macros.hpp"
 32 
 33 #include OS_HEADER_INLINE(os)
 34 
 35 const char* VM_Version::_uarch = "";
 36 uint32_t VM_Version::_initial_vector_length = 0;
 37 
 38 void VM_Version::initialize() {
 39   get_os_cpu_info();
 40 
 41   if (FLAG_IS_DEFAULT(UseFMA)) {
 42     FLAG_SET_DEFAULT(UseFMA, true);
 43   }
 44   if (FLAG_IS_DEFAULT(AllocatePrefetchDistance)) {
 45     FLAG_SET_DEFAULT(AllocatePrefetchDistance, 0);
 46   }
 47 
 48   if (UseAES || UseAESIntrinsics) {
 49     if (UseAES && !FLAG_IS_DEFAULT(UseAES)) {
 50       warning("AES instructions are not available on this CPU");
 51       FLAG_SET_DEFAULT(UseAES, false);
 52     }
 53     if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 54       warning("AES intrinsics are not available on this CPU");
 55       FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 56     }
 57   }
 58 
 59   if (UseAESCTRIntrinsics) {
 60     warning("AES/CTR intrinsics are not available on this CPU");
 61     FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 62   }
 63 
 64   if (UseSHA) {
 65     warning("SHA instructions are not available on this CPU");
 66     FLAG_SET_DEFAULT(UseSHA, false);
 67   }
 68 
 69   if (UseSHA1Intrinsics) {
 70     warning("Intrinsics for SHA-1 crypto hash functions not available on this CPU.");
 71     FLAG_SET_DEFAULT(UseSHA1Intrinsics, false);
 72   }
 73 
 74   if (UseSHA256Intrinsics) {
 75     warning("Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.");
 76     FLAG_SET_DEFAULT(UseSHA256Intrinsics, false);
 77   }
 78 
 79   if (UseSHA512Intrinsics) {
 80     warning("Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.");
 81     FLAG_SET_DEFAULT(UseSHA512Intrinsics, false);
 82   }
 83 
 84   if (UseSHA3Intrinsics) {
 85     warning("Intrinsics for SHA3-224, SHA3-256, SHA3-384 and SHA3-512 crypto hash functions not available on this CPU.");
 86     FLAG_SET_DEFAULT(UseSHA3Intrinsics, false);
 87   }
 88 
 89   if (UseCRC32Intrinsics) {
 90     warning("CRC32 intrinsics are not available on this CPU.");
 91     FLAG_SET_DEFAULT(UseCRC32Intrinsics, false);
 92   }
 93 
 94   if (UseCRC32CIntrinsics) {
 95     warning("CRC32C intrinsics are not available on this CPU.");
 96     FLAG_SET_DEFAULT(UseCRC32CIntrinsics, false);
 97   }
 98 
 99   if (UseMD5Intrinsics) {
100     warning("MD5 intrinsics are not available on this CPU.");
101     FLAG_SET_DEFAULT(UseMD5Intrinsics, false);
102   }
103 
104   if (UseRVV) {
105     if (!(_features & CPU_V)) {
106       warning("RVV is not supported on this CPU");
107       FLAG_SET_DEFAULT(UseRVV, false);
108     } else {
109       // read vector length from vector CSR vlenb
110       _initial_vector_length = get_current_vector_length();
111     }
112   }
113 
114   if (UseRVB && !(_features & CPU_B)) {
115     warning("RVB is not supported on this CPU");
116     FLAG_SET_DEFAULT(UseRVB, false);
117   }
118 
119   if (FLAG_IS_DEFAULT(AvoidUnalignedAccesses)) {
120     FLAG_SET_DEFAULT(AvoidUnalignedAccesses, true);
121   }
122 
123   if (UseRVB) {
124     if (FLAG_IS_DEFAULT(UsePopCountInstruction)) {
125       FLAG_SET_DEFAULT(UsePopCountInstruction, true);
126     }
127   } else {
128     FLAG_SET_DEFAULT(UsePopCountInstruction, false);
129   }
130 
131   char buf[512];
132   buf[0] = '\0';
133   if (_uarch != NULL && strcmp(_uarch, "") != 0) snprintf(buf, sizeof(buf), "%s,", _uarch);
134   strcat(buf, "rv64");
135 #define ADD_FEATURE_IF_SUPPORTED(id, name, bit) if (_features & CPU_##id) strcat(buf, name);
136   CPU_FEATURE_FLAGS(ADD_FEATURE_IF_SUPPORTED)
137 #undef ADD_FEATURE_IF_SUPPORTED
138 
139   _features_string = os::strdup(buf);
140 
141 #ifdef COMPILER2
142   c2_initialize();
143 #endif // COMPILER2
144 }
145 
146 #ifdef COMPILER2
147 void VM_Version::c2_initialize() {
148   if (UseCMoveUnconditionally) {
149     FLAG_SET_DEFAULT(UseCMoveUnconditionally, false);
150   }
151 
152   if (ConditionalMoveLimit > 0) {
153     FLAG_SET_DEFAULT(ConditionalMoveLimit, 0);
154   }
155 
156   if (!UseRVV) {
157     FLAG_SET_DEFAULT(SpecialEncodeISOArray, false);
158   }
159 
160   if (!UseRVV && MaxVectorSize) {
161     FLAG_SET_DEFAULT(MaxVectorSize, 0);
162   }
163 
164   if (UseRVV) {
165     if (FLAG_IS_DEFAULT(MaxVectorSize)) {
166       MaxVectorSize = _initial_vector_length;
167     } else if (MaxVectorSize < 16) {
168       warning("RVV does not support vector length less than 16 bytes. Disabling RVV.");
169       UseRVV = false;
170     } else if (is_power_of_2(MaxVectorSize)) {
171       if (MaxVectorSize > _initial_vector_length) {
172         warning("Current system only supports max RVV vector length %d. Set MaxVectorSize to %d",
173                 _initial_vector_length, _initial_vector_length);
174       }
175       MaxVectorSize = _initial_vector_length;
176     } else {
177       vm_exit_during_initialization(err_msg("Unsupported MaxVectorSize: %d", (int)MaxVectorSize));
178     }
179   }
180 
181   // disable prefetch
182   if (FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
183     FLAG_SET_DEFAULT(AllocatePrefetchStyle, 0);
184   }
185 
186   if (FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {
187     FLAG_SET_DEFAULT(UseMulAddIntrinsic, true);
188   }
189 
190   if (FLAG_IS_DEFAULT(UseMultiplyToLenIntrinsic)) {
191     FLAG_SET_DEFAULT(UseMultiplyToLenIntrinsic, true);
192   }
193 
194   if (FLAG_IS_DEFAULT(UseSquareToLenIntrinsic)) {
195     FLAG_SET_DEFAULT(UseSquareToLenIntrinsic, true);
196   }
197 }
198 #endif // COMPILER2
199 
200 void VM_Version::initialize_cpu_information(void) {
201   // do nothing if cpu info has been initialized
202   if (_initialized) {
203     return;
204   }
205 
206   _no_of_cores  = os::processor_count();
207   _no_of_threads = _no_of_cores;
208   _no_of_sockets = _no_of_cores;
209   snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "RISCV64");
210   snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "RISCV64 %s", _features_string);
211   _initialized = true;
212 }