1 /*
2  * Copyright (C) 2014 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_ARM_INSTRUCTION_SET_FEATURES_ARM_H_
18 #define ART_RUNTIME_ARCH_ARM_INSTRUCTION_SET_FEATURES_ARM_H_
19 
20 #include "arch/instruction_set_features.h"
21 
22 namespace art {
23 
24 class ArmInstructionSetFeatures;
25 using ArmFeaturesUniquePtr = std::unique_ptr<const ArmInstructionSetFeatures>;
26 
27 // Instruction set features relevant to the ARM architecture.
28 class ArmInstructionSetFeatures final : public InstructionSetFeatures {
29  public:
30   // Process a CPU variant string like "krait" or "cortex-a15" and create InstructionSetFeatures.
31   static ArmFeaturesUniquePtr FromVariant(const std::string& variant, std::string* error_msg);
32 
33   // Parse a bitmap and create an InstructionSetFeatures.
34   static ArmFeaturesUniquePtr FromBitmap(uint32_t bitmap);
35 
36   // Turn C pre-processor #defines into the equivalent instruction set features.
37   static ArmFeaturesUniquePtr FromCppDefines();
38 
39   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
40   static ArmFeaturesUniquePtr FromCpuInfo();
41 
42   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
43   // InstructionSetFeatures.
44   static ArmFeaturesUniquePtr FromHwcap();
45 
46   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
47   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
48   static ArmFeaturesUniquePtr FromAssembly();
49 
50   bool Equals(const InstructionSetFeatures* other) const override;
51 
52   bool HasAtLeast(const InstructionSetFeatures* other) const override;
53 
GetInstructionSet()54   InstructionSet GetInstructionSet() const override {
55     return InstructionSet::kArm;
56   }
57 
58   uint32_t AsBitmap() const override;
59 
60   // Return a string of the form "div,lpae" or "none".
61   std::string GetFeatureString() const override;
62 
63   // Is the divide instruction feature enabled?
HasDivideInstruction()64   bool HasDivideInstruction() const {
65       return has_div_;
66   }
67 
68   // Are the ldrd and strd instructions atomic? This is commonly true when the Large Physical
69   // Address Extension (LPAE) is present.
HasAtomicLdrdAndStrd()70   bool HasAtomicLdrdAndStrd() const {
71     return has_atomic_ldrd_strd_;
72   }
73 
74   // Are ARMv8-A instructions available?
HasARMv8AInstructions()75   bool HasARMv8AInstructions() const {
76     return has_armv8a_;
77   }
78 
~ArmInstructionSetFeatures()79   virtual ~ArmInstructionSetFeatures() {}
80 
81  protected:
82   // Parse a vector of the form "div", "lpae" adding these to a new ArmInstructionSetFeatures.
83   std::unique_ptr<const InstructionSetFeatures>
84       AddFeaturesFromSplitString(const std::vector<std::string>& features,
85                                  std::string* error_msg) const override;
86 
87  private:
ArmInstructionSetFeatures(bool has_div,bool has_atomic_ldrd_strd,bool has_armv8a)88   ArmInstructionSetFeatures(bool has_div,
89                             bool has_atomic_ldrd_strd,
90                             bool has_armv8a)
91       : InstructionSetFeatures(),
92         has_div_(has_div),
93         has_atomic_ldrd_strd_(has_atomic_ldrd_strd),
94         has_armv8a_(has_armv8a) {}
95 
96   // Bitmap positions for encoding features as a bitmap.
97   enum {
98     kDivBitfield = 1 << 0,
99     kAtomicLdrdStrdBitfield = 1 << 1,
100     kARMv8A = 1 << 2,
101   };
102 
103   const bool has_div_;
104   const bool has_atomic_ldrd_strd_;
105   const bool has_armv8a_;
106 
107   DISALLOW_COPY_AND_ASSIGN(ArmInstructionSetFeatures);
108 };
109 
110 }  // namespace art
111 
112 #endif  // ART_RUNTIME_ARCH_ARM_INSTRUCTION_SET_FEATURES_ARM_H_
113