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 #ifndef ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_ 18 #define ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_ 19 20 #include <iosfwd> 21 #include <memory> 22 #include <vector> 23 24 #include "arch/instruction_set.h" 25 #include "base/macros.h" 26 27 namespace art { 28 29 class ArmInstructionSetFeatures; 30 class Arm64InstructionSetFeatures; 31 class X86InstructionSetFeatures; 32 class X86_64InstructionSetFeatures; 33 34 // Abstraction used to describe features of a different instruction sets. 35 class InstructionSetFeatures { 36 public: 37 // Process a CPU variant string for the given ISA and create an InstructionSetFeatures. 38 static std::unique_ptr<const InstructionSetFeatures> FromVariant(InstructionSet isa, 39 const std::string& variant, 40 std::string* error_msg); 41 42 // Parse a bitmap for the given isa and create an InstructionSetFeatures. 43 static std::unique_ptr<const InstructionSetFeatures> FromBitmap(InstructionSet isa, 44 uint32_t bitmap); 45 46 // Turn C pre-processor #defines into the equivalent instruction set features for kRuntimeISA. 47 static std::unique_ptr<const InstructionSetFeatures> FromCppDefines(); 48 49 // Check if run-time detection of instruction set features is supported. 50 // 51 // Return: true - if run-time detection is supported on a target device. 52 // false - otherwise IsRuntimeDetectionSupported()53 static bool IsRuntimeDetectionSupported() { 54 return FromRuntimeDetection() != nullptr; 55 } 56 57 // Use run-time detection to get instruction set features. 58 // 59 // Return: a set of detected features or nullptr if runtime detection is not 60 // supported on a target. 61 static std::unique_ptr<const InstructionSetFeatures> FromRuntimeDetection(); 62 63 // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures. 64 static std::unique_ptr<const InstructionSetFeatures> FromCpuInfo(); 65 66 // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce 67 // InstructionSetFeatures. 68 static std::unique_ptr<const InstructionSetFeatures> FromHwcap(); 69 70 // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the 71 // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo. 72 static std::unique_ptr<const InstructionSetFeatures> FromAssembly(); 73 74 // Parse a string of the form "div,-atomic_ldrd_strd" adding and removing these features to 75 // create a new InstructionSetFeatures. 76 std::unique_ptr<const InstructionSetFeatures> AddFeaturesFromString( 77 const std::string& feature_list, std::string* error_msg) const WARN_UNUSED; 78 79 // Are these features the same as the other given features? 80 virtual bool Equals(const InstructionSetFeatures* other) const = 0; 81 82 // For testing purposes we want to make sure that the system we run on has at 83 // least the options we claim it has. In this cases Equals() does not 84 // suffice and will cause the test to fail, since the runtime cpu feature 85 // detection claims more capabilities then statically specified from the 86 // build system. 87 // 88 // A good example of this is the armv8 ART test target that declares 89 // "CPU_VARIANT=generic". If the generic target is specified and the code 90 // is run on a platform with enhanced capabilities, the 91 // instruction_set_features test will fail if we resort to using Equals() 92 // between statically defined cpu features and runtime cpu features. 93 // 94 // For now we default this to Equals() in case the architecture does not 95 // provide it. HasAtLeast(const InstructionSetFeatures * other)96 virtual bool HasAtLeast(const InstructionSetFeatures* other) const { 97 return Equals(other); 98 } 99 100 // Return the ISA these features relate to. 101 virtual InstructionSet GetInstructionSet() const = 0; 102 103 // Return a bitmap that represents the features. ISA specific. 104 virtual uint32_t AsBitmap() const = 0; 105 106 // Return a string of the form "div,lpae" or "none". 107 virtual std::string GetFeatureString() const = 0; 108 109 // Down cast this ArmInstructionFeatures. 110 const ArmInstructionSetFeatures* AsArmInstructionSetFeatures() const; 111 112 // Down cast this Arm64InstructionFeatures. 113 const Arm64InstructionSetFeatures* AsArm64InstructionSetFeatures() const; 114 115 // Down cast this X86InstructionFeatures. 116 const X86InstructionSetFeatures* AsX86InstructionSetFeatures() const; 117 118 // Down cast this X86_64InstructionFeatures. 119 const X86_64InstructionSetFeatures* AsX86_64InstructionSetFeatures() const; 120 ~InstructionSetFeatures()121 virtual ~InstructionSetFeatures() {} 122 123 protected: InstructionSetFeatures()124 InstructionSetFeatures() {} 125 126 // Returns true if variant appears in the array variants. 127 static bool FindVariantInArray(const char* const variants[], size_t num_variants, 128 const std::string& variant); 129 130 // Add architecture specific features in sub-classes. 131 virtual std::unique_ptr<const InstructionSetFeatures> 132 AddFeaturesFromSplitString(const std::vector<std::string>& features, 133 std::string* error_msg) const = 0; 134 135 // Add run-time detected architecture specific features in sub-classes. 136 virtual std::unique_ptr<const InstructionSetFeatures> 137 AddRuntimeDetectedFeatures(const InstructionSetFeatures *features ATTRIBUTE_UNUSED) const; 138 139 private: 140 DISALLOW_COPY_AND_ASSIGN(InstructionSetFeatures); 141 }; 142 std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs); 143 144 } // namespace art 145 146 #endif // ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_ 147