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