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_X86_INSTRUCTION_SET_FEATURES_X86_H_
18 #define ART_RUNTIME_ARCH_X86_INSTRUCTION_SET_FEATURES_X86_H_
19 
20 #include "arch/instruction_set_features.h"
21 #include "base/macros.h"
22 
23 #define GET_REX_R       0x04
24 #define GET_REX_X       0x02
25 #define GET_REX_B       0x01
26 #define SET_VEX_R       0x80
27 #define SET_VEX_X       0x40
28 #define SET_VEX_B       0x20
29 #define SET_VEX_M_0F    0x01
30 #define SET_VEX_M_0F_38 0x02
31 #define SET_VEX_M_0F_3A 0x03
32 #define SET_VEX_W       0x80
33 #define SET_VEX_L_128   0x00
34 #define SET_VEL_L_256   0x04
35 #define SET_VEX_PP_NONE 0x00
36 #define SET_VEX_PP_66   0x01
37 #define SET_VEX_PP_F3   0x02
38 #define SET_VEX_PP_F2   0x03
39 #define TWO_BYTE_VEX    0xC5
40 #define THREE_BYTE_VEX  0xC4
41 #define VEX_INIT        0x00
42 
43 namespace art HIDDEN {
44 
45 class X86InstructionSetFeatures;
46 using X86FeaturesUniquePtr = std::unique_ptr<const X86InstructionSetFeatures>;
47 
48 // Instruction set features relevant to the X86 architecture.
49 class X86InstructionSetFeatures : public InstructionSetFeatures {
50  public:
51   // Process a CPU variant string like "atom" or "nehalem" and create InstructionSetFeatures.
52   static X86FeaturesUniquePtr FromVariant(const std::string& variant,
53                                           std::string* error_msg,
54                                           bool x86_64 = false);
55 
56   // Parse a bitmap and create an InstructionSetFeatures.
57   static X86FeaturesUniquePtr FromBitmap(uint32_t bitmap, bool x86_64 = false);
58 
59   // Turn C pre-processor #defines into the equivalent instruction set features.
60   static X86FeaturesUniquePtr FromCppDefines(bool x86_64 = false);
61 
62   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
63   static X86FeaturesUniquePtr FromCpuInfo(bool x86_64 = false);
64 
65   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
66   // InstructionSetFeatures.
67   static X86FeaturesUniquePtr FromHwcap(bool x86_64 = false);
68 
69   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
70   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
71   static X86FeaturesUniquePtr FromAssembly(bool x86_64 = false);
72 
73   // Use external cpu_features library.
74   static X86FeaturesUniquePtr FromCpuFeatures(bool x86_64 = false);
75 
76   bool Equals(const InstructionSetFeatures* other) const override;
77 
78   bool HasAtLeast(const InstructionSetFeatures* other) const override;
79 
GetInstructionSet()80   InstructionSet GetInstructionSet() const override {
81     return InstructionSet::kX86;
82   }
83 
84   uint32_t AsBitmap() const override;
85 
86   std::string GetFeatureString() const override;
87 
~X86InstructionSetFeatures()88   virtual ~X86InstructionSetFeatures() {}
89 
HasSSE4_1()90   bool HasSSE4_1() const { return has_SSE4_1_; }
91 
HasPopCnt()92   bool HasPopCnt() const { return has_POPCNT_; }
93 
HasAVX2()94   bool HasAVX2() const { return has_AVX2_; }
95 
HasAVX()96   bool HasAVX() const { return has_AVX_; }
97 
98  protected:
99   // Parse a string of the form "ssse3" adding these to a new InstructionSetFeatures.
100   std::unique_ptr<const InstructionSetFeatures>
AddFeaturesFromSplitString(const std::vector<std::string> & features,std::string * error_msg)101       AddFeaturesFromSplitString(const std::vector<std::string>& features,
102                                  std::string* error_msg) const override {
103     return AddFeaturesFromSplitString(features, false, error_msg);
104   }
105 
106   std::unique_ptr<const InstructionSetFeatures>
107       AddFeaturesFromSplitString(const std::vector<std::string>& features,
108                                  bool x86_64,
109                                  std::string* error_msg) const;
110 
X86InstructionSetFeatures(bool has_SSSE3,bool has_SSE4_1,bool has_SSE4_2,bool has_AVX,bool has_AVX2,bool has_POPCNT)111   X86InstructionSetFeatures(bool has_SSSE3,
112                             bool has_SSE4_1,
113                             bool has_SSE4_2,
114                             bool has_AVX,
115                             bool has_AVX2,
116                             bool has_POPCNT)
117       : InstructionSetFeatures(),
118         has_SSSE3_(has_SSSE3),
119         has_SSE4_1_(has_SSE4_1),
120         has_SSE4_2_(has_SSE4_2),
121         has_AVX_(has_AVX),
122         has_AVX2_(has_AVX2),
123         has_POPCNT_(has_POPCNT) {
124   }
125 
126   static X86FeaturesUniquePtr Create(bool x86_64,
127                                      bool has_SSSE3,
128                                      bool has_SSE4_1,
129                                      bool has_SSE4_2,
130                                      bool has_AVX,
131                                      bool has_AVX2,
132                                      bool has_POPCNT);
133 
134  private:
135   // Bitmap positions for encoding features as a bitmap.
136   enum {
137     kSsse3Bitfield = 1 << 0,
138     kSse4_1Bitfield = 1 << 1,
139     kSse4_2Bitfield = 1 << 2,
140     kAvxBitfield = 1 << 3,
141     kAvx2Bitfield = 1 << 4,
142     kPopCntBitfield = 1 << 5,
143   };
144 
145   const bool has_SSSE3_;   // x86 128bit SIMD - Supplemental SSE.
146   const bool has_SSE4_1_;  // x86 128bit SIMD SSE4.1.
147   const bool has_SSE4_2_;  // x86 128bit SIMD SSE4.2.
148   const bool has_AVX_;     // x86 256bit SIMD AVX.
149   const bool has_AVX2_;    // x86 256bit SIMD AVX 2.0.
150   const bool has_POPCNT_;  // x86 population count
151 
152   DISALLOW_COPY_AND_ASSIGN(X86InstructionSetFeatures);
153 };
154 
155 }  // namespace art
156 
157 #endif  // ART_RUNTIME_ARCH_X86_INSTRUCTION_SET_FEATURES_X86_H_
158