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