blob: 88b6914ede36c46d71b6bcdec3df714493625c15 [file] [log] [blame]
Jacob Bramley2af191d2018-05-16 10:22:44 +01001// Copyright 2018, VIXL authors
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// * Neither the name of ARM Limited nor the names of its contributors may be
13// used to endorse or promote products derived from this software without
14// specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef VIXL_CPU_FEATURES_H
28#define VIXL_CPU_FEATURES_H
29
30#include <ostream>
31
32#include "globals-vixl.h"
33
34namespace vixl {
35
36
37// clang-format off
38#define VIXL_CPU_FEATURE_LIST(V) \
39 /* If set, the OS traps and emulates MRS accesses to relevant (EL1) ID_* */ \
40 /* registers, so that the detailed feature registers can be read */ \
41 /* directly. */ \
42 V(kIDRegisterEmulation, "ID register emulation", "cpuid") \
43 \
44 V(kFP, "FP", "fp") \
45 V(kNEON, "NEON", "asimd") \
46 V(kCRC32, "CRC32", "crc32") \
47 /* Cryptographic support instructions. */ \
48 V(kAES, "AES", "aes") \
49 V(kSHA1, "SHA1", "sha1") \
50 V(kSHA2, "SHA2", "sha2") \
51 /* A form of PMULL{2} with a 128-bit (1Q) result. */ \
52 V(kPmull1Q, "Pmull1Q", "pmull") \
53 /* Atomic operations on memory: CAS, LDADD, STADD, SWP, etc. */ \
54 V(kAtomics, "Atomics", "atomics") \
55 /* Limited ordering regions: LDLAR, STLLR and their variants. */ \
56 V(kLORegions, "LORegions", NULL) \
57 /* Rounding doubling multiply add/subtract: SQRDMLAH and SQRDMLSH. */ \
58 V(kRDM, "RDM", "asimdrdm") \
Jacob Bramley4482be72018-09-14 15:57:57 +010059 /* Scalable Vector Extension. */ \
60 V(kSVE, "SVE", "sve") \
Jacob Bramley2af191d2018-05-16 10:22:44 +010061 /* SDOT and UDOT support (in NEON). */ \
62 V(kDotProduct, "DotProduct", "asimddp") \
63 /* Half-precision (FP16) support for FP and NEON, respectively. */ \
64 V(kFPHalf, "FPHalf", "fphp") \
65 V(kNEONHalf, "NEONHalf", "asimdhp") \
Jacob Bramleyca789742018-09-13 14:25:46 +010066 /* The RAS extension, including the ESB instruction. */ \
67 V(kRAS, "RAS", NULL) \
Jacob Bramley2af191d2018-05-16 10:22:44 +010068 /* Data cache clean to the point of persistence: DC CVAP. */ \
69 V(kDCPoP, "DCPoP", "dcpop") \
70 /* Cryptographic support instructions. */ \
71 V(kSHA3, "SHA3", "sha3") \
72 V(kSHA512, "SHA512", "sha512") \
73 V(kSM3, "SM3", "sm3") \
74 V(kSM4, "SM4", "sm4") \
75 /* Pointer authentication for addresses. */ \
76 V(kPAuth, "PAuth", NULL) \
77 /* Pointer authentication for addresses uses QARMA. */ \
78 V(kPAuthQARMA, "PAuthQARMA", NULL) \
79 /* Generic authentication (using the PACGA instruction). */ \
80 V(kPAuthGeneric, "PAuthGeneric", NULL) \
81 /* Generic authentication uses QARMA. */ \
82 V(kPAuthGenericQARMA, "PAuthGenericQARMA", NULL) \
Jacob Bramley4482be72018-09-14 15:57:57 +010083 /* JavaScript-style FP -> integer conversion instruction: FJCVTZS. */ \
Jacob Bramley2af191d2018-05-16 10:22:44 +010084 V(kJSCVT, "JSCVT", "jscvt") \
Jacob Bramley4482be72018-09-14 15:57:57 +010085 /* Complex number support for NEON: FCMLA and FCADD. */ \
86 V(kFcma, "Fcma", "fcma") \
Jacob Bramley2af191d2018-05-16 10:22:44 +010087 /* RCpc-based model (for weaker release consistency): LDAPR and variants. */ \
88 V(kRCpc, "RCpc", "lrcpc") \
Jacob Bramley4482be72018-09-14 15:57:57 +010089 V(kRCpcImm, "RCpc (imm)", "ilrcpc") \
90 /* Flag manipulation instructions: SETF{8,16}, CFINV, RMIF. */ \
91 V(kFlagM, "FlagM", "flagm") \
92 /* Unaligned single-copy atomicity. */ \
93 V(kUSCAT, "USCAT", "uscat") \
94 /* FP16 fused multiply-add or -subtract long: FMLAL{2}, FMLSL{2}. */ \
95 V(kFHM, "FHM", "asimdfhm") \
96 /* Data-independent timing (for selected instructions). */ \
Martyn Capewellcb963f72018-10-22 15:25:28 +010097 V(kDIT, "DIT", "dit") \
98 /* Branch target identification. */ \
99 V(kBTI, "BTI", NULL)
Jacob Bramley2af191d2018-05-16 10:22:44 +0100100// clang-format on
101
102
103class CPUFeaturesConstIterator;
104
105// A representation of the set of features known to be supported by the target
106// device. Each feature is represented by a simple boolean flag.
107//
Jacob Bramley5997b462018-06-05 14:05:30 +0100108// - When the Assembler is asked to assemble an instruction, it asserts (in
109// debug mode) that the necessary features are available.
Jacob Bramley2af191d2018-05-16 10:22:44 +0100110//
Jacob Bramley2af191d2018-05-16 10:22:44 +0100111// - TODO: The MacroAssembler relies on the Assembler's assertions, but in
112// some cases it may be useful for macros to generate a fall-back sequence
113// in case features are not available.
114//
Jacob Bramleyc44ce3d2018-06-12 15:39:09 +0100115// - The Simulator assumes by default that all features are available, but it
116// is possible to configure it to fail if the simulated code uses features
117// that are not enabled.
118//
119// The Simulator also offers pseudo-instructions to allow features to be
120// enabled and disabled dynamically. This is useful when you want to ensure
121// that some features are constrained to certain areas of code.
122//
123// - The base Disassembler knows nothing about CPU features, but the
124// PrintDisassembler can be configured to annotate its output with warnings
125// about unavailable features. The Simulator uses this feature when
126// instruction trace is enabled.
127//
128// - The Decoder-based components -- the Simulator and PrintDisassembler --
129// rely on a CPUFeaturesAuditor visitor. This visitor keeps a list of
130// features actually encountered so that a large block of code can be
131// examined (either directly or through simulation), and the required
132// features analysed later.
133//
Jacob Bramley2af191d2018-05-16 10:22:44 +0100134// Expected usage:
135//
136// // By default, VIXL uses CPUFeatures::AArch64LegacyBaseline(), for
137// // compatibility with older version of VIXL.
138// MacroAssembler masm;
139//
140// // Generate code only for the current CPU.
141// masm.SetCPUFeatures(CPUFeatures::InferFromOS());
142//
143// // Turn off feature checking entirely.
144// masm.SetCPUFeatures(CPUFeatures::All());
145//
146// Feature set manipulation:
147//
148// CPUFeatures f; // The default constructor gives an empty set.
149// // Individual features can be added (or removed).
150// f.Combine(CPUFeatures::kFP, CPUFeatures::kNEON, CPUFeatures::AES);
151// f.Remove(CPUFeatures::kNEON);
152//
153// // Some helpers exist for extensions that provide several features.
154// f.Remove(CPUFeatures::All());
155// f.Combine(CPUFeatures::AArch64LegacyBaseline());
156//
157// // Chained construction is also possible.
158// CPUFeatures g =
159// f.With(CPUFeatures::kPmull1Q).Without(CPUFeatures::kCRC32);
160//
161// // Features can be queried. Where multiple features are given, they are
162// // combined with logical AND.
163// if (h.Has(CPUFeatures::kNEON)) { ... }
164// if (h.Has(CPUFeatures::kFP, CPUFeatures::kNEON)) { ... }
165// if (h.Has(g)) { ... }
166// // If the empty set is requested, the result is always 'true'.
167// VIXL_ASSERT(h.Has(CPUFeatures()));
168//
169// // For debug and reporting purposes, features can be enumerated (or
170// // printed directly):
171// std::cout << CPUFeatures::kNEON; // Prints something like "NEON".
172// std::cout << f; // Prints something like "FP, NEON, CRC32".
173class CPUFeatures {
174 public:
175 // clang-format off
176 // Individual features.
177 // These should be treated as opaque tokens. User code should not rely on
178 // specific numeric values or ordering.
179 enum Feature {
180 // Refer to VIXL_CPU_FEATURE_LIST (above) for the list of feature names that
181 // this class supports.
182
Jacob Bramleyfdf332a2018-09-17 11:17:54 +0100183 kNone = -1,
Jacob Bramley2af191d2018-05-16 10:22:44 +0100184#define VIXL_DECLARE_FEATURE(SYMBOL, NAME, CPUINFO) SYMBOL,
185 VIXL_CPU_FEATURE_LIST(VIXL_DECLARE_FEATURE)
186#undef VIXL_DECLARE_FEATURE
Jacob Bramleyfdf332a2018-09-17 11:17:54 +0100187 kNumberOfFeatures
Jacob Bramley2af191d2018-05-16 10:22:44 +0100188 };
189 // clang-format on
190
191 // By default, construct with no features enabled.
192 CPUFeatures() : features_(0) {}
193
194 // Construct with some features already enabled.
195 CPUFeatures(Feature feature0,
196 Feature feature1 = kNone,
197 Feature feature2 = kNone,
198 Feature feature3 = kNone);
199
200 // Construct with all features enabled. This can be used to disable feature
201 // checking: `Has(...)` returns true regardless of the argument.
202 static CPUFeatures All();
203
Jacob Bramley7b8fc822018-06-26 16:48:21 +0100204 // Construct an empty CPUFeatures. This is equivalent to the default
205 // constructor, but is provided for symmetry and convenience.
206 static CPUFeatures None() { return CPUFeatures(); }
207
Jacob Bramley2af191d2018-05-16 10:22:44 +0100208 // The presence of these features was assumed by version of VIXL before this
209 // API was added, so using this set by default ensures API compatibility.
210 static CPUFeatures AArch64LegacyBaseline() {
211 return CPUFeatures(kFP, kNEON, kCRC32);
212 }
213
214 // Construct a new CPUFeatures object based on what the OS reports.
215 static CPUFeatures InferFromOS();
216
217 // Combine another CPUFeatures object into this one. Features that already
218 // exist in this set are left unchanged.
219 void Combine(const CPUFeatures& other);
220
221 // Combine specific features into this set. Features that already exist in
222 // this set are left unchanged.
223 void Combine(Feature feature0,
224 Feature feature1 = kNone,
225 Feature feature2 = kNone,
226 Feature feature3 = kNone);
227
228 // Remove features in another CPUFeatures object from this one.
229 void Remove(const CPUFeatures& other);
230
231 // Remove specific features from this set.
232 void Remove(Feature feature0,
233 Feature feature1 = kNone,
234 Feature feature2 = kNone,
235 Feature feature3 = kNone);
236
237 // Chaining helpers for convenient construction.
238 CPUFeatures With(const CPUFeatures& other) const;
239 CPUFeatures With(Feature feature0,
240 Feature feature1 = kNone,
241 Feature feature2 = kNone,
242 Feature feature3 = kNone) const;
243 CPUFeatures Without(const CPUFeatures& other) const;
244 CPUFeatures Without(Feature feature0,
245 Feature feature1 = kNone,
246 Feature feature2 = kNone,
247 Feature feature3 = kNone) const;
248
249 // Query features.
250 // Note that an empty query (like `Has(kNone)`) always returns true.
251 bool Has(const CPUFeatures& other) const;
252 bool Has(Feature feature0,
253 Feature feature1 = kNone,
254 Feature feature2 = kNone,
255 Feature feature3 = kNone) const;
256
Jacob Bramleyc44ce3d2018-06-12 15:39:09 +0100257 // Return the number of enabled features.
258 size_t Count() const;
259
260 // Check for equivalence.
261 bool operator==(const CPUFeatures& other) const {
262 return Has(other) && other.Has(*this);
263 }
264 bool operator!=(const CPUFeatures& other) const { return !(*this == other); }
265
Jacob Bramley2af191d2018-05-16 10:22:44 +0100266 typedef CPUFeaturesConstIterator const_iterator;
267
268 const_iterator begin() const;
269 const_iterator end() const;
270
271 private:
272 // Each bit represents a feature. This field will be replaced as needed if
273 // features are added.
274 uint64_t features_;
275
276 friend std::ostream& operator<<(std::ostream& os,
277 const vixl::CPUFeatures& features);
278};
279
280std::ostream& operator<<(std::ostream& os, vixl::CPUFeatures::Feature feature);
281std::ostream& operator<<(std::ostream& os, const vixl::CPUFeatures& features);
282
283// This is not a proper C++ iterator type, but it simulates enough of
284// ForwardIterator that simple loops can be written.
285class CPUFeaturesConstIterator {
286 public:
287 CPUFeaturesConstIterator(const CPUFeatures* cpu_features = NULL,
288 CPUFeatures::Feature start = CPUFeatures::kNone)
289 : cpu_features_(cpu_features), feature_(start) {
290 VIXL_ASSERT(IsValid());
291 }
292
293 bool operator==(const CPUFeaturesConstIterator& other) const;
294 bool operator!=(const CPUFeaturesConstIterator& other) const {
295 return !(*this == other);
296 }
297 CPUFeatures::Feature operator++();
298 CPUFeatures::Feature operator++(int);
299
300 CPUFeatures::Feature operator*() const {
301 VIXL_ASSERT(IsValid());
302 return feature_;
303 }
304
305 // For proper support of C++'s simplest "Iterator" concept, this class would
306 // have to define member types (such as CPUFeaturesIterator::pointer) to make
307 // it appear as if it iterates over Feature objects in memory. That is, we'd
308 // need CPUFeatures::iterator to behave like std::vector<Feature>::iterator.
309 // This is at least partially possible -- the std::vector<bool> specialisation
310 // does something similar -- but it doesn't seem worthwhile for a
311 // special-purpose debug helper, so they are omitted here.
312 private:
313 const CPUFeatures* cpu_features_;
314 CPUFeatures::Feature feature_;
315
316 bool IsValid() const {
317 return ((cpu_features_ == NULL) && (feature_ == CPUFeatures::kNone)) ||
318 cpu_features_->Has(feature_);
319 }
320};
321
322// A convenience scope for temporarily modifying a CPU features object. This
323// allows features to be enabled for short sequences.
324//
325// Expected usage:
326//
327// {
328// CPUFeaturesScope cpu(&masm, CPUFeatures::kCRC32);
329// // This scope can now use CRC32, as well as anything else that was enabled
330// // before the scope.
331//
332// ...
333//
334// // At the end of the scope, the original CPU features are restored.
335// }
336class CPUFeaturesScope {
337 public:
338 // Start a CPUFeaturesScope on any object that implements
339 // `CPUFeatures* GetCPUFeatures()`.
340 template <typename T>
341 explicit CPUFeaturesScope(T* cpu_features_wrapper,
342 CPUFeatures::Feature feature0 = CPUFeatures::kNone,
343 CPUFeatures::Feature feature1 = CPUFeatures::kNone,
344 CPUFeatures::Feature feature2 = CPUFeatures::kNone,
345 CPUFeatures::Feature feature3 = CPUFeatures::kNone)
346 : cpu_features_(cpu_features_wrapper->GetCPUFeatures()),
347 old_features_(*cpu_features_) {
348 cpu_features_->Combine(feature0, feature1, feature2, feature3);
349 }
350
351 template <typename T>
352 CPUFeaturesScope(T* cpu_features_wrapper, const CPUFeatures& other)
353 : cpu_features_(cpu_features_wrapper->GetCPUFeatures()),
354 old_features_(*cpu_features_) {
355 cpu_features_->Combine(other);
356 }
357
358 ~CPUFeaturesScope() { *cpu_features_ = old_features_; }
359
360 // For advanced usage, the CPUFeatures object can be accessed directly.
361 // The scope will restore the original state when it ends.
362
363 CPUFeatures* GetCPUFeatures() const { return cpu_features_; }
364
365 void SetCPUFeatures(const CPUFeatures& cpu_features) {
366 *cpu_features_ = cpu_features;
367 }
368
369 private:
370 CPUFeatures* const cpu_features_;
371 const CPUFeatures old_features_;
372};
373
374
375} // namespace vixl
376
377#endif // VIXL_CPU_FEATURES_H