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