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   // Use external cpu_features library.
75   static std::unique_ptr<const InstructionSetFeatures> FromCpuFeatures();
76 
77   // Parse a string of the form "div,-atomic_ldrd_strd" adding and removing these features to
78   // create a new InstructionSetFeatures.
79   std::unique_ptr<const InstructionSetFeatures> AddFeaturesFromString(
80       const std::string& feature_list, std::string* error_msg) const WARN_UNUSED;
81 
82   // Are these features the same as the other given features?
83   virtual bool Equals(const InstructionSetFeatures* other) const = 0;
84 
85   // For testing purposes we want to make sure that the system we run on has at
86   // least the options we claim it has. In this cases Equals() does not
87   // suffice and will cause the test to fail, since the runtime cpu feature
88   // detection claims more capabilities then statically specified from the
89   // build system.
90   //
91   // A good example of this is the armv8 ART test target that declares
92   // "CPU_VARIANT=generic". If the generic target is specified and the code
93   // is run on a platform with enhanced capabilities, the
94   // instruction_set_features test will fail if we resort to using Equals()
95   // between statically defined cpu features and runtime cpu features.
96   //
97   // For now we default this to Equals() in case the architecture does not
98   // provide it.
HasAtLeast(const InstructionSetFeatures * other)99   virtual bool HasAtLeast(const InstructionSetFeatures* other) const {
100     return Equals(other);
101   }
102 
103   // Return the ISA these features relate to.
104   virtual InstructionSet GetInstructionSet() const = 0;
105 
106   // Return a bitmap that represents the features. ISA specific.
107   virtual uint32_t AsBitmap() const = 0;
108 
109   // Return a string of the form "div,lpae" or "none".
110   virtual std::string GetFeatureString() const = 0;
111 
112   // Down cast this ArmInstructionFeatures.
113   const ArmInstructionSetFeatures* AsArmInstructionSetFeatures() const;
114 
115   // Down cast this Arm64InstructionFeatures.
116   const Arm64InstructionSetFeatures* AsArm64InstructionSetFeatures() const;
117 
118   // Down cast this X86InstructionFeatures.
119   const X86InstructionSetFeatures* AsX86InstructionSetFeatures() const;
120 
121   // Down cast this X86_64InstructionFeatures.
122   const X86_64InstructionSetFeatures* AsX86_64InstructionSetFeatures() const;
123 
~InstructionSetFeatures()124   virtual ~InstructionSetFeatures() {}
125 
126  protected:
InstructionSetFeatures()127   InstructionSetFeatures() {}
128 
129   // Returns true if variant appears in the array variants.
130   static bool FindVariantInArray(const char* const variants[], size_t num_variants,
131                                  const std::string& variant);
132 
133   // Add architecture specific features in sub-classes.
134   virtual std::unique_ptr<const InstructionSetFeatures>
135       AddFeaturesFromSplitString(const std::vector<std::string>& features,
136                                  std::string* error_msg) const = 0;
137 
138   // Add run-time detected architecture specific features in sub-classes.
139   virtual std::unique_ptr<const InstructionSetFeatures>
140       AddRuntimeDetectedFeatures(const InstructionSetFeatures *features ATTRIBUTE_UNUSED) const;
141 
142  private:
143   DISALLOW_COPY_AND_ASSIGN(InstructionSetFeatures);
144 };
145 std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs);
146 
147 }  // namespace art
148 
149 #endif  // ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_
150