xref: /aosp_15_r20/art/runtime/arch/instruction_set_features.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <iosfwd>
21*795d594fSAndroid Build Coastguard Worker #include <memory>
22*795d594fSAndroid Build Coastguard Worker #include <vector>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker class ArmInstructionSetFeatures;
30*795d594fSAndroid Build Coastguard Worker class Arm64InstructionSetFeatures;
31*795d594fSAndroid Build Coastguard Worker class Riscv64InstructionSetFeatures;
32*795d594fSAndroid Build Coastguard Worker class X86InstructionSetFeatures;
33*795d594fSAndroid Build Coastguard Worker class X86_64InstructionSetFeatures;
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker // Abstraction used to describe features of a different instruction sets.
36*795d594fSAndroid Build Coastguard Worker class InstructionSetFeatures {
37*795d594fSAndroid Build Coastguard Worker  public:
38*795d594fSAndroid Build Coastguard Worker   // Process a CPU variant string for the given ISA and create an InstructionSetFeatures.
39*795d594fSAndroid Build Coastguard Worker   EXPORT static std::unique_ptr<const InstructionSetFeatures> FromVariant(
40*795d594fSAndroid Build Coastguard Worker       InstructionSet isa, const std::string& variant, std::string* error_msg);
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker   // Process a CPU variant string for the given ISA and make sure the features advertised
43*795d594fSAndroid Build Coastguard Worker   // are supported by the hardware. This is needed for Pixel3a which wrongly
44*795d594fSAndroid Build Coastguard Worker   // reports itself as cortex-a75.
45*795d594fSAndroid Build Coastguard Worker   EXPORT static std::unique_ptr<const InstructionSetFeatures> FromVariantAndHwcap(
46*795d594fSAndroid Build Coastguard Worker       InstructionSet isa, const std::string& variant, std::string* error_msg);
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker   // Parse a bitmap for the given isa and create an InstructionSetFeatures.
49*795d594fSAndroid Build Coastguard Worker   EXPORT static std::unique_ptr<const InstructionSetFeatures> FromBitmap(InstructionSet isa,
50*795d594fSAndroid Build Coastguard Worker                                                                          uint32_t bitmap);
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   // Turn C pre-processor #defines into the equivalent instruction set features for kRuntimeISA.
53*795d594fSAndroid Build Coastguard Worker   EXPORT static std::unique_ptr<const InstructionSetFeatures> FromCppDefines();
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker   // Check if run-time detection of instruction set features is supported.
56*795d594fSAndroid Build Coastguard Worker   //
57*795d594fSAndroid Build Coastguard Worker   // Return: true - if run-time detection is supported on a target device.
58*795d594fSAndroid Build Coastguard Worker   //         false - otherwise
IsRuntimeDetectionSupported()59*795d594fSAndroid Build Coastguard Worker   static bool IsRuntimeDetectionSupported() {
60*795d594fSAndroid Build Coastguard Worker     return FromRuntimeDetection() != nullptr;
61*795d594fSAndroid Build Coastguard Worker   }
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker   // Use run-time detection to get instruction set features.
64*795d594fSAndroid Build Coastguard Worker   //
65*795d594fSAndroid Build Coastguard Worker   // Return: a set of detected features or nullptr if runtime detection is not
66*795d594fSAndroid Build Coastguard Worker   //         supported on a target.
67*795d594fSAndroid Build Coastguard Worker   static std::unique_ptr<const InstructionSetFeatures> FromRuntimeDetection();
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
70*795d594fSAndroid Build Coastguard Worker   static std::unique_ptr<const InstructionSetFeatures> FromCpuInfo();
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
73*795d594fSAndroid Build Coastguard Worker   // InstructionSetFeatures.
74*795d594fSAndroid Build Coastguard Worker   static std::unique_ptr<const InstructionSetFeatures> FromHwcap();
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
77*795d594fSAndroid Build Coastguard Worker   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
78*795d594fSAndroid Build Coastguard Worker   static std::unique_ptr<const InstructionSetFeatures> FromAssembly();
79*795d594fSAndroid Build Coastguard Worker 
80*795d594fSAndroid Build Coastguard Worker   // Use external cpu_features library.
81*795d594fSAndroid Build Coastguard Worker   static std::unique_ptr<const InstructionSetFeatures> FromCpuFeatures();
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker   // Parse a string of the form "div,-atomic_ldrd_strd" adding and removing these features to
84*795d594fSAndroid Build Coastguard Worker   // create a new InstructionSetFeatures.
85*795d594fSAndroid Build Coastguard Worker   EXPORT std::unique_ptr<const InstructionSetFeatures> AddFeaturesFromString(
86*795d594fSAndroid Build Coastguard Worker       const std::string& feature_list, std::string* error_msg) const WARN_UNUSED;
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   // Are these features the same as the other given features?
89*795d594fSAndroid Build Coastguard Worker   virtual bool Equals(const InstructionSetFeatures* other) const = 0;
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker   // For testing purposes we want to make sure that the system we run on has at
92*795d594fSAndroid Build Coastguard Worker   // least the options we claim it has. In this cases Equals() does not
93*795d594fSAndroid Build Coastguard Worker   // suffice and will cause the test to fail, since the runtime cpu feature
94*795d594fSAndroid Build Coastguard Worker   // detection claims more capabilities then statically specified from the
95*795d594fSAndroid Build Coastguard Worker   // build system.
96*795d594fSAndroid Build Coastguard Worker   //
97*795d594fSAndroid Build Coastguard Worker   // A good example of this is the armv8 ART test target that declares
98*795d594fSAndroid Build Coastguard Worker   // "CPU_VARIANT=generic". If the generic target is specified and the code
99*795d594fSAndroid Build Coastguard Worker   // is run on a platform with enhanced capabilities, the
100*795d594fSAndroid Build Coastguard Worker   // instruction_set_features test will fail if we resort to using Equals()
101*795d594fSAndroid Build Coastguard Worker   // between statically defined cpu features and runtime cpu features.
102*795d594fSAndroid Build Coastguard Worker   //
103*795d594fSAndroid Build Coastguard Worker   // For now we default this to Equals() in case the architecture does not
104*795d594fSAndroid Build Coastguard Worker   // provide it.
HasAtLeast(const InstructionSetFeatures * other)105*795d594fSAndroid Build Coastguard Worker   virtual bool HasAtLeast(const InstructionSetFeatures* other) const {
106*795d594fSAndroid Build Coastguard Worker     return Equals(other);
107*795d594fSAndroid Build Coastguard Worker   }
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker   // Return the ISA these features relate to.
110*795d594fSAndroid Build Coastguard Worker   virtual InstructionSet GetInstructionSet() const = 0;
111*795d594fSAndroid Build Coastguard Worker 
112*795d594fSAndroid Build Coastguard Worker   // Return a bitmap that represents the features. ISA specific.
113*795d594fSAndroid Build Coastguard Worker   virtual uint32_t AsBitmap() const = 0;
114*795d594fSAndroid Build Coastguard Worker 
115*795d594fSAndroid Build Coastguard Worker   // Return a string of the form "div,lpae" or "none".
116*795d594fSAndroid Build Coastguard Worker   virtual std::string GetFeatureString() const = 0;
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker   // Down cast this ArmInstructionFeatures.
119*795d594fSAndroid Build Coastguard Worker   const ArmInstructionSetFeatures* AsArmInstructionSetFeatures() const;
120*795d594fSAndroid Build Coastguard Worker 
121*795d594fSAndroid Build Coastguard Worker   // Down cast this Arm64InstructionFeatures.
122*795d594fSAndroid Build Coastguard Worker   EXPORT const Arm64InstructionSetFeatures* AsArm64InstructionSetFeatures() const;
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker   // Down cast this Riscv64InstructionFeatures.
125*795d594fSAndroid Build Coastguard Worker   EXPORT const Riscv64InstructionSetFeatures* AsRiscv64InstructionSetFeatures() const;
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker   // Down cast this X86InstructionFeatures.
128*795d594fSAndroid Build Coastguard Worker   const X86InstructionSetFeatures* AsX86InstructionSetFeatures() const;
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   // Down cast this X86_64InstructionFeatures.
131*795d594fSAndroid Build Coastguard Worker   const X86_64InstructionSetFeatures* AsX86_64InstructionSetFeatures() const;
132*795d594fSAndroid Build Coastguard Worker 
~InstructionSetFeatures()133*795d594fSAndroid Build Coastguard Worker   virtual ~InstructionSetFeatures() {}
134*795d594fSAndroid Build Coastguard Worker 
135*795d594fSAndroid Build Coastguard Worker  protected:
InstructionSetFeatures()136*795d594fSAndroid Build Coastguard Worker   InstructionSetFeatures() {}
137*795d594fSAndroid Build Coastguard Worker 
138*795d594fSAndroid Build Coastguard Worker   // Returns true if variant appears in the array variants.
139*795d594fSAndroid Build Coastguard Worker   static bool FindVariantInArray(const char* const variants[], size_t num_variants,
140*795d594fSAndroid Build Coastguard Worker                                  const std::string& variant);
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker   // Add architecture specific features in sub-classes.
143*795d594fSAndroid Build Coastguard Worker   virtual std::unique_ptr<const InstructionSetFeatures>
144*795d594fSAndroid Build Coastguard Worker       AddFeaturesFromSplitString(const std::vector<std::string>& features,
145*795d594fSAndroid Build Coastguard Worker                                  std::string* error_msg) const = 0;
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker   // Add run-time detected architecture specific features in sub-classes.
148*795d594fSAndroid Build Coastguard Worker   virtual std::unique_ptr<const InstructionSetFeatures> AddRuntimeDetectedFeatures(
149*795d594fSAndroid Build Coastguard Worker       [[maybe_unused]] const InstructionSetFeatures* features) const;
150*795d594fSAndroid Build Coastguard Worker 
151*795d594fSAndroid Build Coastguard Worker  private:
152*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(InstructionSetFeatures);
153*795d594fSAndroid Build Coastguard Worker };
154*795d594fSAndroid Build Coastguard Worker EXPORT std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs);
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker }  // namespace art
157*795d594fSAndroid Build Coastguard Worker 
158*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_
159