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 
 45   if (FLAG_IS_DEFAULT(AllocatePrefetchDistance)) {
 46     FLAG_SET_DEFAULT(AllocatePrefetchDistance, 0);
 47   }
 48 
 49   if (UseAES || UseAESIntrinsics) {
 50     if (UseAES && !FLAG_IS_DEFAULT(UseAES)) {
 51       warning("AES instructions are not available on this CPU");
 52       FLAG_SET_DEFAULT(UseAES, false);
 53     }
 54     if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 55       warning("AES intrinsics are not available on this CPU");
 56       FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 57     }
 58   }
 59 
 60   if (UseAESCTRIntrinsics) {
 61     warning("AES/CTR intrinsics are not available on this CPU");
 62     FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 63   }
 64 
 65   if (UseSHA) {
 66     warning("SHA instructions are not available on this CPU");
 67     FLAG_SET_DEFAULT(UseSHA, false);
 68   }
 69 
 70   if (UseSHA1Intrinsics) {
 71     warning("Intrinsics for SHA-1 crypto hash functions not available on this CPU.");
 72     FLAG_SET_DEFAULT(UseSHA1Intrinsics, false);
 73   }
 74 
 75   if (UseSHA256Intrinsics) {
 76     warning("Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.");
 77     FLAG_SET_DEFAULT(UseSHA256Intrinsics, false);
 78   }
 79 
 80   if (UseSHA512Intrinsics) {
 81     warning("Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.");
 82     FLAG_SET_DEFAULT(UseSHA512Intrinsics, false);
 83   }
 84 
 85   if (UseSHA3Intrinsics) {
 86     warning("Intrinsics for SHA3-224, SHA3-256, SHA3-384 and SHA3-512 crypto hash functions not available on this CPU.");
 87     FLAG_SET_DEFAULT(UseSHA3Intrinsics, false);
 88   }
 89 
 90   if (UseCRC32Intrinsics) {
 91     warning("CRC32 intrinsics are not available on this CPU.");
 92     FLAG_SET_DEFAULT(UseCRC32Intrinsics, false);
 93   }
 94 
 95   if (UseCRC32CIntrinsics) {
 96     warning("CRC32C intrinsics are not available on this CPU.");
 97     FLAG_SET_DEFAULT(UseCRC32CIntrinsics, false);
 98   }
 99 
100   if (UseMD5Intrinsics) {
101     warning("MD5 intrinsics are not available on this CPU.");
102     FLAG_SET_DEFAULT(UseMD5Intrinsics, false);
103   }
104 
105   if (UseRVV) {
106     if (!(_features & CPU_V)) {
107       warning("RVV is not supported on this CPU");
108       FLAG_SET_DEFAULT(UseRVV, false);
109     } else {
110       // read vector length from vector CSR vlenb
111       _initial_vector_length = get_current_vector_length();
112     }
113   }
114 
115   if (UseRVB && !(_features & CPU_B)) {
116     warning("RVB is not supported on this CPU");
117     FLAG_SET_DEFAULT(UseRVB, false);
118   }
119 
120   if (UseRVC && !(_features & CPU_C)) {
121     warning("RVC is not supported on this CPU");
122     FLAG_SET_DEFAULT(UseRVC, false);
123   }
124 
125   if (FLAG_IS_DEFAULT(AvoidUnalignedAccesses)) {
126     FLAG_SET_DEFAULT(AvoidUnalignedAccesses, true);
127   }
128 
129   if (UseRVB) {
130     if (FLAG_IS_DEFAULT(UsePopCountInstruction)) {
131       FLAG_SET_DEFAULT(UsePopCountInstruction, true);
132     }
133   } else {
134     FLAG_SET_DEFAULT(UsePopCountInstruction, false);
135   }
136 
137   char buf[512];
138   buf[0] = '\0';
139   if (_uarch != NULL && strcmp(_uarch, "") != 0) snprintf(buf, sizeof(buf), "%s,", _uarch);
140   strcat(buf, "rv64");
141 #define ADD_FEATURE_IF_SUPPORTED(id, name, bit) if (_features & CPU_##id) strcat(buf, name);
142   CPU_FEATURE_FLAGS(ADD_FEATURE_IF_SUPPORTED)
143 #undef ADD_FEATURE_IF_SUPPORTED
144 
145   _features_string = os::strdup(buf);
146 
147 #ifdef COMPILER2
148   c2_initialize();
149 #endif // COMPILER2
150 }
151 
152 #ifdef COMPILER2
153 void VM_Version::c2_initialize() {
154   if (UseCMoveUnconditionally) {
155     FLAG_SET_DEFAULT(UseCMoveUnconditionally, false);
156   }
157 
158   if (ConditionalMoveLimit > 0) {
159     FLAG_SET_DEFAULT(ConditionalMoveLimit, 0);
160   }
161 
162   if (!UseRVV) {
163     FLAG_SET_DEFAULT(SpecialEncodeISOArray, false);
164   }
165 
166   if (!UseRVV && MaxVectorSize) {
167     FLAG_SET_DEFAULT(MaxVectorSize, 0);
168   }
169 
170   if (!UseRVV) {
171     FLAG_SET_DEFAULT(UseRVVForBigIntegerShiftIntrinsics, false);
172   }
173 
174   if (UseRVV) {
175     if (FLAG_IS_DEFAULT(MaxVectorSize)) {
176       MaxVectorSize = _initial_vector_length;
177     } else if (MaxVectorSize < 16) {
178       warning("RVV does not support vector length less than 16 bytes. Disabling RVV.");
179       UseRVV = false;
180     } else if (is_power_of_2(MaxVectorSize)) {
181       if (MaxVectorSize > _initial_vector_length) {
182         warning("Current system only supports max RVV vector length %d. Set MaxVectorSize to %d",
183                 _initial_vector_length, _initial_vector_length);
184       }
185       MaxVectorSize = _initial_vector_length;
186     } else {
187       vm_exit_during_initialization(err_msg("Unsupported MaxVectorSize: %d", (int)MaxVectorSize));
188     }
189   }
190 
191   // disable prefetch
192   if (FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
193     FLAG_SET_DEFAULT(AllocatePrefetchStyle, 0);
194   }
195 
196   if (FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {
197     FLAG_SET_DEFAULT(UseMulAddIntrinsic, true);
198   }
199 
200   if (FLAG_IS_DEFAULT(UseMultiplyToLenIntrinsic)) {
201     FLAG_SET_DEFAULT(UseMultiplyToLenIntrinsic, true);
202   }
203 
204   if (FLAG_IS_DEFAULT(UseSquareToLenIntrinsic)) {
205     FLAG_SET_DEFAULT(UseSquareToLenIntrinsic, true);
206   }
207 
208   if (FLAG_IS_DEFAULT(UseMontgomeryMultiplyIntrinsic)) {
209     FLAG_SET_DEFAULT(UseMontgomeryMultiplyIntrinsic, true);
210   }
211 
212   if (FLAG_IS_DEFAULT(UseMontgomerySquareIntrinsic)) {
213     FLAG_SET_DEFAULT(UseMontgomerySquareIntrinsic, true);
214   }
215 }
216 #endif // COMPILER2
217 
218 void VM_Version::initialize_cpu_information(void) {
219   // do nothing if cpu info has been initialized
220   if (_initialized) {
221     return;
222   }
223 
224   _no_of_cores  = os::processor_count();
225   _no_of_threads = _no_of_cores;
226   _no_of_sockets = _no_of_cores;
227   snprintf(_cpu_name, CPU_TYPE_DESC_BUF_SIZE - 1, "RISCV64");
228   snprintf(_cpu_desc, CPU_DETAILED_DESC_BUF_SIZE, "RISCV64 %s", _features_string);
229   _initialized = true;
230 }