1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "instruction_set_features.h"
18
19 #include <algorithm>
20 #include <ostream>
21
22 #include "android-base/strings.h"
23 #include "arm/instruction_set_features_arm.h"
24 #include "arm64/instruction_set_features_arm64.h"
25 #include "base/casts.h"
26 #include "base/utils.h"
27 #include "riscv64/instruction_set_features_riscv64.h"
28 #include "x86/instruction_set_features_x86.h"
29 #include "x86_64/instruction_set_features_x86_64.h"
30
31 namespace art HIDDEN {
32
FromVariant(InstructionSet isa,const std::string & variant,std::string * error_msg)33 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromVariant(
34 InstructionSet isa, const std::string& variant, std::string* error_msg) {
35 switch (isa) {
36 case InstructionSet::kArm:
37 case InstructionSet::kThumb2:
38 return ArmInstructionSetFeatures::FromVariant(variant, error_msg);
39 case InstructionSet::kArm64:
40 return Arm64InstructionSetFeatures::FromVariant(variant, error_msg);
41 case InstructionSet::kRiscv64:
42 return Riscv64InstructionSetFeatures::FromVariant(variant, error_msg);
43 case InstructionSet::kX86:
44 return X86InstructionSetFeatures::FromVariant(variant, error_msg);
45 case InstructionSet::kX86_64:
46 return X86_64InstructionSetFeatures::FromVariant(variant, error_msg);
47
48 default:
49 break;
50 }
51 UNIMPLEMENTED(FATAL) << isa;
52 UNREACHABLE();
53 }
54
FromVariantAndHwcap(InstructionSet isa,const std::string & variant,std::string * error_msg)55 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromVariantAndHwcap(
56 InstructionSet isa, const std::string& variant, std::string* error_msg) {
57 auto variant_features = FromVariant(isa, variant, error_msg);
58 if (variant_features == nullptr) {
59 return nullptr;
60 }
61 // Pixel3a is wrongly reporting itself as cortex-a75, so validate the features
62 // with hwcaps.
63 // Note that when cross-compiling on device (using dex2oat32 for compiling
64 // arm64), the hwcaps will report that no feature is supported. This is
65 // currently our best approach to be safe/correct. Maybe using the
66 // cpu_features library could fix this issue.
67 if (isa == InstructionSet::kArm64) {
68 auto new_features = down_cast<const Arm64InstructionSetFeatures*>(variant_features.get())
69 ->IntersectWithHwcap();
70 if (!variant_features->Equals(new_features.get())) {
71 LOG(WARNING) << "Mismatch between instruction set variant of device ("
72 << *variant_features
73 << ") and features returned by the hardware (" << *new_features << ")";
74 }
75 return new_features;
76 } else {
77 // TODO: Implement this validation on all architectures.
78 return variant_features;
79 }
80 }
81
FromBitmap(InstructionSet isa,uint32_t bitmap)82 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromBitmap(InstructionSet isa,
83 uint32_t bitmap) {
84 std::unique_ptr<const InstructionSetFeatures> result;
85 switch (isa) {
86 case InstructionSet::kArm:
87 case InstructionSet::kThumb2:
88 result = ArmInstructionSetFeatures::FromBitmap(bitmap);
89 break;
90 case InstructionSet::kArm64:
91 result = Arm64InstructionSetFeatures::FromBitmap(bitmap);
92 break;
93 case InstructionSet::kRiscv64:
94 result = Riscv64InstructionSetFeatures::FromBitmap(bitmap);
95 break;
96 case InstructionSet::kX86:
97 result = X86InstructionSetFeatures::FromBitmap(bitmap);
98 break;
99 case InstructionSet::kX86_64:
100 result = X86_64InstructionSetFeatures::FromBitmap(bitmap);
101 break;
102
103 default:
104 UNIMPLEMENTED(FATAL) << isa;
105 UNREACHABLE();
106 }
107 CHECK_EQ(bitmap, result->AsBitmap());
108 return result;
109 }
110
FromCppDefines()111 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCppDefines() {
112 switch (kRuntimeISA) {
113 case InstructionSet::kArm:
114 case InstructionSet::kThumb2:
115 return ArmInstructionSetFeatures::FromCppDefines();
116 case InstructionSet::kArm64:
117 return Arm64InstructionSetFeatures::FromCppDefines();
118 case InstructionSet::kRiscv64:
119 return Riscv64InstructionSetFeatures::FromCppDefines();
120 case InstructionSet::kX86:
121 return X86InstructionSetFeatures::FromCppDefines();
122 case InstructionSet::kX86_64:
123 return X86_64InstructionSetFeatures::FromCppDefines();
124
125 default:
126 break;
127 }
128 UNIMPLEMENTED(FATAL) << kRuntimeISA;
129 UNREACHABLE();
130 }
131
FromRuntimeDetection()132 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromRuntimeDetection() {
133 switch (kRuntimeISA) {
134 #ifdef ART_TARGET_ANDROID
135 case InstructionSet::kArm64:
136 return Arm64InstructionSetFeatures::FromHwcap();
137 #endif
138 default:
139 return nullptr;
140 }
141 }
142
FromCpuInfo()143 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCpuInfo() {
144 switch (kRuntimeISA) {
145 case InstructionSet::kArm:
146 case InstructionSet::kThumb2:
147 return ArmInstructionSetFeatures::FromCpuInfo();
148 case InstructionSet::kArm64:
149 return Arm64InstructionSetFeatures::FromCpuInfo();
150 case InstructionSet::kRiscv64:
151 return Riscv64InstructionSetFeatures::FromCpuInfo();
152 case InstructionSet::kX86:
153 return X86InstructionSetFeatures::FromCpuInfo();
154 case InstructionSet::kX86_64:
155 return X86_64InstructionSetFeatures::FromCpuInfo();
156
157 default:
158 break;
159 }
160 UNIMPLEMENTED(FATAL) << kRuntimeISA;
161 UNREACHABLE();
162 }
163
FromHwcap()164 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromHwcap() {
165 switch (kRuntimeISA) {
166 case InstructionSet::kArm:
167 case InstructionSet::kThumb2:
168 return ArmInstructionSetFeatures::FromHwcap();
169 case InstructionSet::kArm64:
170 return Arm64InstructionSetFeatures::FromHwcap();
171 case InstructionSet::kRiscv64:
172 return Riscv64InstructionSetFeatures::FromHwcap();
173 case InstructionSet::kX86:
174 return X86InstructionSetFeatures::FromHwcap();
175 case InstructionSet::kX86_64:
176 return X86_64InstructionSetFeatures::FromHwcap();
177
178 default:
179 break;
180 }
181 UNIMPLEMENTED(FATAL) << kRuntimeISA;
182 UNREACHABLE();
183 }
184
FromAssembly()185 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromAssembly() {
186 switch (kRuntimeISA) {
187 case InstructionSet::kArm:
188 case InstructionSet::kThumb2:
189 return ArmInstructionSetFeatures::FromAssembly();
190 case InstructionSet::kArm64:
191 return Arm64InstructionSetFeatures::FromAssembly();
192 case InstructionSet::kRiscv64:
193 return Riscv64InstructionSetFeatures::FromAssembly();
194 case InstructionSet::kX86:
195 return X86InstructionSetFeatures::FromAssembly();
196 case InstructionSet::kX86_64:
197 return X86_64InstructionSetFeatures::FromAssembly();
198
199 default:
200 break;
201 }
202 UNIMPLEMENTED(FATAL) << kRuntimeISA;
203 UNREACHABLE();
204 }
205
FromCpuFeatures()206 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::FromCpuFeatures() {
207 switch (kRuntimeISA) {
208 case InstructionSet::kArm:
209 case InstructionSet::kThumb2:
210 return ArmInstructionSetFeatures::FromCpuFeatures();
211 case InstructionSet::kArm64:
212 return Arm64InstructionSetFeatures::FromCpuFeatures();
213 case InstructionSet::kRiscv64:
214 return Riscv64InstructionSetFeatures::FromCpuFeatures();
215 case InstructionSet::kX86:
216 return X86InstructionSetFeatures::FromCpuFeatures();
217 case InstructionSet::kX86_64:
218 return X86_64InstructionSetFeatures::FromCpuFeatures();
219
220 default:
221 break;
222 }
223 UNIMPLEMENTED(FATAL) << kRuntimeISA;
224 UNREACHABLE();
225 }
226
AddFeaturesFromString(const std::string & feature_list,std::string * error_msg) const227 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::AddFeaturesFromString(
228 const std::string& feature_list, /* out */ std::string* error_msg) const {
229 std::vector<std::string> features;
230 Split(feature_list, ',', &features);
231 std::transform(std::begin(features), std::end(features), std::begin(features),
232 [](const std::string &s) { return android::base::Trim(s); });
233 auto empty_strings_begin = std::copy_if(std::begin(features), std::end(features),
234 std::begin(features),
235 [](const std::string& s) { return !s.empty(); });
236 features.erase(empty_strings_begin, std::end(features));
237 if (features.empty()) {
238 *error_msg = "No instruction set features specified";
239 return nullptr;
240 }
241
242 bool use_default = false;
243 bool use_runtime_detection = false;
244 for (const std::string& feature : features) {
245 if (feature == "default") {
246 if (features.size() > 1) {
247 *error_msg = "Specific instruction set feature(s) cannot be used when 'default' is used.";
248 return nullptr;
249 }
250 use_default = true;
251 features.pop_back();
252 break;
253 } else if (feature == "runtime") {
254 if (features.size() > 1) {
255 *error_msg = "Specific instruction set feature(s) cannot be used when 'runtime' is used.";
256 return nullptr;
257 }
258 use_runtime_detection = true;
259 features.pop_back();
260 break;
261 }
262 }
263 // Expectation: "default" and "runtime" are standalone, no other feature names.
264 // But an empty features vector after processing can also come along if the
265 // handled feature names are the only ones in the list. So
266 // logically, we check "default or runtime => features.empty."
267 DCHECK((!use_default && !use_runtime_detection) || features.empty());
268
269 std::unique_ptr<const InstructionSetFeatures> runtime_detected_features;
270 if (use_runtime_detection) {
271 runtime_detected_features = FromRuntimeDetection();
272 }
273
274 if (runtime_detected_features != nullptr) {
275 return AddRuntimeDetectedFeatures(runtime_detected_features.get());
276 } else {
277 return AddFeaturesFromSplitString(features, error_msg);
278 }
279 }
280
AsArmInstructionSetFeatures() const281 const ArmInstructionSetFeatures* InstructionSetFeatures::AsArmInstructionSetFeatures() const {
282 DCHECK_EQ(InstructionSet::kArm, GetInstructionSet());
283 return down_cast<const ArmInstructionSetFeatures*>(this);
284 }
285
AsArm64InstructionSetFeatures() const286 const Arm64InstructionSetFeatures* InstructionSetFeatures::AsArm64InstructionSetFeatures() const {
287 DCHECK_EQ(InstructionSet::kArm64, GetInstructionSet());
288 return down_cast<const Arm64InstructionSetFeatures*>(this);
289 }
290
AsRiscv64InstructionSetFeatures() const291 const Riscv64InstructionSetFeatures* InstructionSetFeatures::AsRiscv64InstructionSetFeatures()
292 const {
293 DCHECK_EQ(InstructionSet::kRiscv64, GetInstructionSet());
294 return down_cast<const Riscv64InstructionSetFeatures*>(this);
295 }
296
AsX86InstructionSetFeatures() const297 const X86InstructionSetFeatures* InstructionSetFeatures::AsX86InstructionSetFeatures() const {
298 DCHECK(InstructionSet::kX86 == GetInstructionSet() ||
299 InstructionSet::kX86_64 == GetInstructionSet());
300 return down_cast<const X86InstructionSetFeatures*>(this);
301 }
302
AsX86_64InstructionSetFeatures() const303 const X86_64InstructionSetFeatures* InstructionSetFeatures::AsX86_64InstructionSetFeatures() const {
304 DCHECK_EQ(InstructionSet::kX86_64, GetInstructionSet());
305 return down_cast<const X86_64InstructionSetFeatures*>(this);
306 }
307
FindVariantInArray(const char * const variants[],size_t num_variants,const std::string & variant)308 bool InstructionSetFeatures::FindVariantInArray(const char* const variants[], size_t num_variants,
309 const std::string& variant) {
310 const char* const * begin = variants;
311 const char* const * end = begin + num_variants;
312 return std::find(begin, end, variant) != end;
313 }
314
AddRuntimeDetectedFeatures(const InstructionSetFeatures * features) const315 std::unique_ptr<const InstructionSetFeatures> InstructionSetFeatures::AddRuntimeDetectedFeatures(
316 [[maybe_unused]] const InstructionSetFeatures* features) const {
317 UNIMPLEMENTED(FATAL) << kRuntimeISA;
318 UNREACHABLE();
319 }
320
operator <<(std::ostream & os,const InstructionSetFeatures & rhs)321 std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs) {
322 os << "ISA: " << rhs.GetInstructionSet() << " Feature string: " << rhs.GetFeatureString();
323 return os;
324 }
325
326 } // namespace art
327