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 <ostream>
21 #include <vector>
22 
23 #include "base/macros.h"
24 #include "instruction_set.h"
25 
26 namespace art {
27 
28 class ArmInstructionSetFeatures;
29 class Arm64InstructionSetFeatures;
30 class MipsInstructionSetFeatures;
31 class Mips64InstructionSetFeatures;
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 const InstructionSetFeatures* FromVariant(InstructionSet isa,
40                                                    const std::string& variant,
41                                                    std::string* error_msg);
42 
43   // Parse a bitmap for the given isa and create an InstructionSetFeatures.
44   static const InstructionSetFeatures* FromBitmap(InstructionSet isa, uint32_t bitmap);
45 
46   // Turn C pre-processor #defines into the equivalent instruction set features for kRuntimeISA.
47   static const InstructionSetFeatures* FromCppDefines();
48 
49   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
50   static const InstructionSetFeatures* FromCpuInfo();
51 
52   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
53   // InstructionSetFeatures.
54   static const InstructionSetFeatures* FromHwcap();
55 
56   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
57   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
58   static const InstructionSetFeatures* FromAssembly();
59 
60   // Parse a string of the form "div,-atomic_ldrd_strd" adding and removing these features to
61   // create a new InstructionSetFeatures.
62   const InstructionSetFeatures* AddFeaturesFromString(const std::string& feature_list,
63                                                       std::string* error_msg) const WARN_UNUSED;
64 
65   // Are these features the same as the other given features?
66   virtual bool Equals(const InstructionSetFeatures* other) const = 0;
67 
68   // Return the ISA these features relate to.
69   virtual InstructionSet GetInstructionSet() const = 0;
70 
71   // Return a bitmap that represents the features. ISA specific.
72   virtual uint32_t AsBitmap() const = 0;
73 
74   // Return a string of the form "div,lpae" or "none".
75   virtual std::string GetFeatureString() const = 0;
76 
77   // Does the instruction set variant require instructions for correctness with SMP?
IsSmp()78   bool IsSmp() const {
79     return smp_;
80   }
81 
82   // Down cast this ArmInstructionFeatures.
83   const ArmInstructionSetFeatures* AsArmInstructionSetFeatures() const;
84 
85   // Down cast this Arm64InstructionFeatures.
86   const Arm64InstructionSetFeatures* AsArm64InstructionSetFeatures() const;
87 
88   // Down cast this MipsInstructionFeatures.
89   const MipsInstructionSetFeatures* AsMipsInstructionSetFeatures() const;
90 
91   // Down cast this Mips64InstructionFeatures.
92   const Mips64InstructionSetFeatures* AsMips64InstructionSetFeatures() const;
93 
94   // Down cast this X86InstructionFeatures.
95   const X86InstructionSetFeatures* AsX86InstructionSetFeatures() const;
96 
97   // Down cast this X86_64InstructionFeatures.
98   const X86_64InstructionSetFeatures* AsX86_64InstructionSetFeatures() const;
99 
~InstructionSetFeatures()100   virtual ~InstructionSetFeatures() {}
101 
102  protected:
InstructionSetFeatures(bool smp)103   explicit InstructionSetFeatures(bool smp) : smp_(smp) {}
104 
105   // Returns true if variant appears in the array variants.
106   static bool FindVariantInArray(const char* const variants[], size_t num_variants,
107                                  const std::string& variant);
108 
109   // Add architecture specific features in sub-classes.
110   virtual const InstructionSetFeatures*
111       AddFeaturesFromSplitString(bool smp, const std::vector<std::string>& features,
112                                  std::string* error_msg) const = 0;
113 
114  private:
115   const bool smp_;
116 
117   DISALLOW_COPY_AND_ASSIGN(InstructionSetFeatures);
118 };
119 std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs);
120 
121 }  // namespace art
122 
123 #endif  // ART_RUNTIME_ARCH_INSTRUCTION_SET_FEATURES_H_
124