1b8021494Sopenharmony_ci// Copyright 2015, VIXL authors
2b8021494Sopenharmony_ci// All rights reserved.
3b8021494Sopenharmony_ci//
4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
6b8021494Sopenharmony_ci//
7b8021494Sopenharmony_ci//   * Redistributions of source code must retain the above copyright notice,
8b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer.
9b8021494Sopenharmony_ci//   * Redistributions in binary form must reproduce the above copyright notice,
10b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer in the documentation
11b8021494Sopenharmony_ci//     and/or other materials provided with the distribution.
12b8021494Sopenharmony_ci//   * Neither the name of ARM Limited nor the names of its contributors may be
13b8021494Sopenharmony_ci//     used to endorse or promote products derived from this software without
14b8021494Sopenharmony_ci//     specific prior written permission.
15b8021494Sopenharmony_ci//
16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b8021494Sopenharmony_ci
27b8021494Sopenharmony_ci#if defined(__aarch64__) && (defined(__ANDROID__) || defined(__linux__))
28b8021494Sopenharmony_ci#include <sys/auxv.h>
29b8021494Sopenharmony_ci#define VIXL_USE_LINUX_HWCAP 1
30b8021494Sopenharmony_ci#endif
31b8021494Sopenharmony_ci
32b8021494Sopenharmony_ci#include "../utils-vixl.h"
33b8021494Sopenharmony_ci
34b8021494Sopenharmony_ci#include "cpu-aarch64.h"
35b8021494Sopenharmony_ci
36b8021494Sopenharmony_cinamespace vixl {
37b8021494Sopenharmony_cinamespace aarch64 {
38b8021494Sopenharmony_ci
39b8021494Sopenharmony_ci
40b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR0::kFP(16, Field::kSigned);
41b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR0::kAdvSIMD(20, Field::kSigned);
42b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR0::kRAS(28);
43b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR0::kSVE(32);
44b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR0::kDIT(48);
45b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR0::kCSV2(56);
46b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR0::kCSV3(60);
47b8021494Sopenharmony_ci
48b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR1::kBT(0);
49b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR1::kSSBS(4);
50b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR1::kMTE(8);
51b8021494Sopenharmony_ciconst IDRegister::Field AA64PFR1::kSME(24);
52b8021494Sopenharmony_ci
53b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kAES(4);
54b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kSHA1(8);
55b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kSHA2(12);
56b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kCRC32(16);
57b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kAtomic(20);
58b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kRDM(28);
59b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kSHA3(32);
60b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kSM3(36);
61b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kSM4(40);
62b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kDP(44);
63b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kFHM(48);
64b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kTS(52);
65b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR0::kRNDR(60);
66b8021494Sopenharmony_ci
67b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kDPB(0);
68b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kAPA(4);
69b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kAPI(8);
70b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kJSCVT(12);
71b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kFCMA(16);
72b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kLRCPC(20);
73b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kGPA(24);
74b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kGPI(28);
75b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kFRINTTS(32);
76b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kSB(36);
77b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kSPECRES(40);
78b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kBF16(44);
79b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kDGH(48);
80b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR1::kI8MM(52);
81b8021494Sopenharmony_ci
82b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR2::kWFXT(0);
83b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR2::kRPRES(4);
84b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR2::kMOPS(16);
85b8021494Sopenharmony_ciconst IDRegister::Field AA64ISAR2::kCSSC(52);
86b8021494Sopenharmony_ci
87b8021494Sopenharmony_ciconst IDRegister::Field AA64MMFR0::kECV(60);
88b8021494Sopenharmony_ci
89b8021494Sopenharmony_ciconst IDRegister::Field AA64MMFR1::kLO(16);
90b8021494Sopenharmony_ciconst IDRegister::Field AA64MMFR1::kAFP(44);
91b8021494Sopenharmony_ci
92b8021494Sopenharmony_ciconst IDRegister::Field AA64MMFR2::kAT(32);
93b8021494Sopenharmony_ci
94b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kSVEver(0);
95b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kAES(4);
96b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kBitPerm(16);
97b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kBF16(20);
98b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kSHA3(32);
99b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kSM4(40);
100b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kI8MM(44);
101b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kF32MM(52);
102b8021494Sopenharmony_ciconst IDRegister::Field AA64ZFR0::kF64MM(56);
103b8021494Sopenharmony_ci
104b8021494Sopenharmony_ciconst IDRegister::Field AA64SMFR0::kSMEf32f32(32, 1);
105b8021494Sopenharmony_ciconst IDRegister::Field AA64SMFR0::kSMEb16f32(34, 1);
106b8021494Sopenharmony_ciconst IDRegister::Field AA64SMFR0::kSMEf16f32(35, 1);
107b8021494Sopenharmony_ciconst IDRegister::Field AA64SMFR0::kSMEi8i32(36);
108b8021494Sopenharmony_ciconst IDRegister::Field AA64SMFR0::kSMEf64f64(48, 1);
109b8021494Sopenharmony_ciconst IDRegister::Field AA64SMFR0::kSMEi16i64(52);
110b8021494Sopenharmony_ciconst IDRegister::Field AA64SMFR0::kSMEfa64(63, 1);
111b8021494Sopenharmony_ci
112b8021494Sopenharmony_ciCPUFeatures AA64PFR0::GetCPUFeatures() const {
113b8021494Sopenharmony_ci  CPUFeatures f;
114b8021494Sopenharmony_ci  if (Get(kFP) >= 0) f.Combine(CPUFeatures::kFP);
115b8021494Sopenharmony_ci  if (Get(kFP) >= 1) f.Combine(CPUFeatures::kFPHalf);
116b8021494Sopenharmony_ci  if (Get(kAdvSIMD) >= 0) f.Combine(CPUFeatures::kNEON);
117b8021494Sopenharmony_ci  if (Get(kAdvSIMD) >= 1) f.Combine(CPUFeatures::kNEONHalf);
118b8021494Sopenharmony_ci  if (Get(kRAS) >= 1) f.Combine(CPUFeatures::kRAS);
119b8021494Sopenharmony_ci  if (Get(kSVE) >= 1) f.Combine(CPUFeatures::kSVE);
120b8021494Sopenharmony_ci  if (Get(kDIT) >= 1) f.Combine(CPUFeatures::kDIT);
121b8021494Sopenharmony_ci  if (Get(kCSV2) >= 1) f.Combine(CPUFeatures::kCSV2);
122b8021494Sopenharmony_ci  if (Get(kCSV2) >= 2) f.Combine(CPUFeatures::kSCXTNUM);
123b8021494Sopenharmony_ci  if (Get(kCSV3) >= 1) f.Combine(CPUFeatures::kCSV3);
124b8021494Sopenharmony_ci  return f;
125b8021494Sopenharmony_ci}
126b8021494Sopenharmony_ci
127b8021494Sopenharmony_ciCPUFeatures AA64PFR1::GetCPUFeatures() const {
128b8021494Sopenharmony_ci  CPUFeatures f;
129b8021494Sopenharmony_ci  if (Get(kBT) >= 1) f.Combine(CPUFeatures::kBTI);
130b8021494Sopenharmony_ci  if (Get(kSSBS) >= 1) f.Combine(CPUFeatures::kSSBS);
131b8021494Sopenharmony_ci  if (Get(kSSBS) >= 2) f.Combine(CPUFeatures::kSSBSControl);
132b8021494Sopenharmony_ci  if (Get(kMTE) >= 1) f.Combine(CPUFeatures::kMTEInstructions);
133b8021494Sopenharmony_ci  if (Get(kMTE) >= 2) f.Combine(CPUFeatures::kMTE);
134b8021494Sopenharmony_ci  if (Get(kMTE) >= 3) f.Combine(CPUFeatures::kMTE3);
135b8021494Sopenharmony_ci  if (Get(kSME) >= 1) f.Combine(CPUFeatures::kSME);
136b8021494Sopenharmony_ci  return f;
137b8021494Sopenharmony_ci}
138b8021494Sopenharmony_ci
139b8021494Sopenharmony_ciCPUFeatures AA64ISAR0::GetCPUFeatures() const {
140b8021494Sopenharmony_ci  CPUFeatures f;
141b8021494Sopenharmony_ci  if (Get(kAES) >= 1) f.Combine(CPUFeatures::kAES);
142b8021494Sopenharmony_ci  if (Get(kAES) >= 2) f.Combine(CPUFeatures::kPmull1Q);
143b8021494Sopenharmony_ci  if (Get(kSHA1) >= 1) f.Combine(CPUFeatures::kSHA1);
144b8021494Sopenharmony_ci  if (Get(kSHA2) >= 1) f.Combine(CPUFeatures::kSHA2);
145b8021494Sopenharmony_ci  if (Get(kSHA2) >= 2) f.Combine(CPUFeatures::kSHA512);
146b8021494Sopenharmony_ci  if (Get(kCRC32) >= 1) f.Combine(CPUFeatures::kCRC32);
147b8021494Sopenharmony_ci  if (Get(kAtomic) >= 1) f.Combine(CPUFeatures::kAtomics);
148b8021494Sopenharmony_ci  if (Get(kRDM) >= 1) f.Combine(CPUFeatures::kRDM);
149b8021494Sopenharmony_ci  if (Get(kSHA3) >= 1) f.Combine(CPUFeatures::kSHA3);
150b8021494Sopenharmony_ci  if (Get(kSM3) >= 1) f.Combine(CPUFeatures::kSM3);
151b8021494Sopenharmony_ci  if (Get(kSM4) >= 1) f.Combine(CPUFeatures::kSM4);
152b8021494Sopenharmony_ci  if (Get(kDP) >= 1) f.Combine(CPUFeatures::kDotProduct);
153b8021494Sopenharmony_ci  if (Get(kFHM) >= 1) f.Combine(CPUFeatures::kFHM);
154b8021494Sopenharmony_ci  if (Get(kTS) >= 1) f.Combine(CPUFeatures::kFlagM);
155b8021494Sopenharmony_ci  if (Get(kTS) >= 2) f.Combine(CPUFeatures::kAXFlag);
156b8021494Sopenharmony_ci  if (Get(kRNDR) >= 1) f.Combine(CPUFeatures::kRNG);
157b8021494Sopenharmony_ci  return f;
158b8021494Sopenharmony_ci}
159b8021494Sopenharmony_ci
160b8021494Sopenharmony_ciCPUFeatures AA64ISAR1::GetCPUFeatures() const {
161b8021494Sopenharmony_ci  CPUFeatures f;
162b8021494Sopenharmony_ci  if (Get(kDPB) >= 1) f.Combine(CPUFeatures::kDCPoP);
163b8021494Sopenharmony_ci  if (Get(kDPB) >= 2) f.Combine(CPUFeatures::kDCCVADP);
164b8021494Sopenharmony_ci  if (Get(kJSCVT) >= 1) f.Combine(CPUFeatures::kJSCVT);
165b8021494Sopenharmony_ci  if (Get(kFCMA) >= 1) f.Combine(CPUFeatures::kFcma);
166b8021494Sopenharmony_ci  if (Get(kLRCPC) >= 1) f.Combine(CPUFeatures::kRCpc);
167b8021494Sopenharmony_ci  if (Get(kLRCPC) >= 2) f.Combine(CPUFeatures::kRCpcImm);
168b8021494Sopenharmony_ci  if (Get(kFRINTTS) >= 1) f.Combine(CPUFeatures::kFrintToFixedSizedInt);
169b8021494Sopenharmony_ci  if (Get(kSB) >= 1) f.Combine(CPUFeatures::kSB);
170b8021494Sopenharmony_ci  if (Get(kSPECRES) >= 1) f.Combine(CPUFeatures::kSPECRES);
171b8021494Sopenharmony_ci  if (Get(kBF16) >= 1) f.Combine(CPUFeatures::kBF16);
172b8021494Sopenharmony_ci  if (Get(kBF16) >= 2) f.Combine(CPUFeatures::kEBF16);
173b8021494Sopenharmony_ci  if (Get(kDGH) >= 1) f.Combine(CPUFeatures::kDGH);
174b8021494Sopenharmony_ci  if (Get(kI8MM) >= 1) f.Combine(CPUFeatures::kI8MM);
175b8021494Sopenharmony_ci
176b8021494Sopenharmony_ci  // Only one of these fields should be non-zero, but they have the same
177b8021494Sopenharmony_ci  // encodings, so merge the logic.
178b8021494Sopenharmony_ci  int apx = std::max(Get(kAPI), Get(kAPA));
179b8021494Sopenharmony_ci  if (apx >= 1) {
180b8021494Sopenharmony_ci    f.Combine(CPUFeatures::kPAuth);
181b8021494Sopenharmony_ci    // APA (rather than API) indicates QARMA.
182b8021494Sopenharmony_ci    if (Get(kAPA) >= 1) f.Combine(CPUFeatures::kPAuthQARMA);
183b8021494Sopenharmony_ci    if (apx == 0b0010) f.Combine(CPUFeatures::kPAuthEnhancedPAC);
184b8021494Sopenharmony_ci    if (apx >= 0b0011) f.Combine(CPUFeatures::kPAuthEnhancedPAC2);
185b8021494Sopenharmony_ci    if (apx >= 0b0100) f.Combine(CPUFeatures::kPAuthFPAC);
186b8021494Sopenharmony_ci    if (apx >= 0b0101) f.Combine(CPUFeatures::kPAuthFPACCombined);
187b8021494Sopenharmony_ci  }
188b8021494Sopenharmony_ci
189b8021494Sopenharmony_ci  if (Get(kGPI) >= 1) f.Combine(CPUFeatures::kPAuthGeneric);
190b8021494Sopenharmony_ci  if (Get(kGPA) >= 1) {
191b8021494Sopenharmony_ci    f.Combine(CPUFeatures::kPAuthGeneric, CPUFeatures::kPAuthGenericQARMA);
192b8021494Sopenharmony_ci  }
193b8021494Sopenharmony_ci  return f;
194b8021494Sopenharmony_ci}
195b8021494Sopenharmony_ci
196b8021494Sopenharmony_ciCPUFeatures AA64ISAR2::GetCPUFeatures() const {
197b8021494Sopenharmony_ci  CPUFeatures f;
198b8021494Sopenharmony_ci  if (Get(kWFXT) >= 2) f.Combine(CPUFeatures::kWFXT);
199b8021494Sopenharmony_ci  if (Get(kRPRES) >= 1) f.Combine(CPUFeatures::kRPRES);
200b8021494Sopenharmony_ci  if (Get(kMOPS) >= 1) f.Combine(CPUFeatures::kMOPS);
201b8021494Sopenharmony_ci  if (Get(kCSSC) >= 1) f.Combine(CPUFeatures::kCSSC);
202b8021494Sopenharmony_ci  return f;
203b8021494Sopenharmony_ci}
204b8021494Sopenharmony_ci
205b8021494Sopenharmony_ciCPUFeatures AA64MMFR0::GetCPUFeatures() const {
206b8021494Sopenharmony_ci  CPUFeatures f;
207b8021494Sopenharmony_ci  if (Get(kECV) >= 1) f.Combine(CPUFeatures::kECV);
208b8021494Sopenharmony_ci  return f;
209b8021494Sopenharmony_ci}
210b8021494Sopenharmony_ci
211b8021494Sopenharmony_ciCPUFeatures AA64MMFR1::GetCPUFeatures() const {
212b8021494Sopenharmony_ci  CPUFeatures f;
213b8021494Sopenharmony_ci  if (Get(kLO) >= 1) f.Combine(CPUFeatures::kLORegions);
214b8021494Sopenharmony_ci  if (Get(kAFP) >= 1) f.Combine(CPUFeatures::kAFP);
215b8021494Sopenharmony_ci  return f;
216b8021494Sopenharmony_ci}
217b8021494Sopenharmony_ci
218b8021494Sopenharmony_ciCPUFeatures AA64MMFR2::GetCPUFeatures() const {
219b8021494Sopenharmony_ci  CPUFeatures f;
220b8021494Sopenharmony_ci  if (Get(kAT) >= 1) f.Combine(CPUFeatures::kUSCAT);
221b8021494Sopenharmony_ci  return f;
222b8021494Sopenharmony_ci}
223b8021494Sopenharmony_ci
224b8021494Sopenharmony_ciCPUFeatures AA64ZFR0::GetCPUFeatures() const {
225b8021494Sopenharmony_ci  // This register is only available with SVE, but reads-as-zero in its absence,
226b8021494Sopenharmony_ci  // so it's always safe to read it.
227b8021494Sopenharmony_ci  CPUFeatures f;
228b8021494Sopenharmony_ci  if (Get(kF64MM) >= 1) f.Combine(CPUFeatures::kSVEF64MM);
229b8021494Sopenharmony_ci  if (Get(kF32MM) >= 1) f.Combine(CPUFeatures::kSVEF32MM);
230b8021494Sopenharmony_ci  if (Get(kI8MM) >= 1) f.Combine(CPUFeatures::kSVEI8MM);
231b8021494Sopenharmony_ci  if (Get(kSM4) >= 1) f.Combine(CPUFeatures::kSVESM4);
232b8021494Sopenharmony_ci  if (Get(kSHA3) >= 1) f.Combine(CPUFeatures::kSVESHA3);
233b8021494Sopenharmony_ci  if (Get(kBF16) >= 1) f.Combine(CPUFeatures::kSVEBF16);
234b8021494Sopenharmony_ci  if (Get(kBF16) >= 2) f.Combine(CPUFeatures::kSVE_EBF16);
235b8021494Sopenharmony_ci  if (Get(kBitPerm) >= 1) f.Combine(CPUFeatures::kSVEBitPerm);
236b8021494Sopenharmony_ci  if (Get(kAES) >= 1) f.Combine(CPUFeatures::kSVEAES);
237b8021494Sopenharmony_ci  if (Get(kAES) >= 2) f.Combine(CPUFeatures::kSVEPmull128);
238b8021494Sopenharmony_ci  if (Get(kSVEver) >= 1) f.Combine(CPUFeatures::kSVE2);
239b8021494Sopenharmony_ci  return f;
240b8021494Sopenharmony_ci}
241b8021494Sopenharmony_ci
242b8021494Sopenharmony_ciCPUFeatures AA64SMFR0::GetCPUFeatures() const {
243b8021494Sopenharmony_ci  CPUFeatures f;
244b8021494Sopenharmony_ci  if (Get(kSMEf32f32) >= 1) f.Combine(CPUFeatures::kSMEf32f32);
245b8021494Sopenharmony_ci  if (Get(kSMEb16f32) >= 1) f.Combine(CPUFeatures::kSMEb16f32);
246b8021494Sopenharmony_ci  if (Get(kSMEf16f32) >= 1) f.Combine(CPUFeatures::kSMEf16f32);
247b8021494Sopenharmony_ci  if (Get(kSMEi8i32) >= 15) f.Combine(CPUFeatures::kSMEi8i32);
248b8021494Sopenharmony_ci  if (Get(kSMEf64f64) >= 1) f.Combine(CPUFeatures::kSMEf64f64);
249b8021494Sopenharmony_ci  if (Get(kSMEi16i64) >= 15) f.Combine(CPUFeatures::kSMEi16i64);
250b8021494Sopenharmony_ci  if (Get(kSMEfa64) >= 1) f.Combine(CPUFeatures::kSMEfa64);
251b8021494Sopenharmony_ci  return f;
252b8021494Sopenharmony_ci}
253b8021494Sopenharmony_ci
254b8021494Sopenharmony_ciint IDRegister::Get(IDRegister::Field field) const {
255b8021494Sopenharmony_ci  int msb = field.GetMsb();
256b8021494Sopenharmony_ci  int lsb = field.GetLsb();
257b8021494Sopenharmony_ci  VIXL_STATIC_ASSERT(static_cast<size_t>(Field::kMaxWidthInBits) <
258b8021494Sopenharmony_ci                     (sizeof(int) * kBitsPerByte));
259b8021494Sopenharmony_ci  switch (field.GetType()) {
260b8021494Sopenharmony_ci    case Field::kSigned:
261b8021494Sopenharmony_ci      return static_cast<int>(ExtractSignedBitfield64(msb, lsb, value_));
262b8021494Sopenharmony_ci    case Field::kUnsigned:
263b8021494Sopenharmony_ci      return static_cast<int>(ExtractUnsignedBitfield64(msb, lsb, value_));
264b8021494Sopenharmony_ci  }
265b8021494Sopenharmony_ci  VIXL_UNREACHABLE();
266b8021494Sopenharmony_ci  return 0;
267b8021494Sopenharmony_ci}
268b8021494Sopenharmony_ci
269b8021494Sopenharmony_ciCPUFeatures CPU::InferCPUFeaturesFromIDRegisters() {
270b8021494Sopenharmony_ci  CPUFeatures f;
271b8021494Sopenharmony_ci#define VIXL_COMBINE_ID_REG(NAME, MRS_ARG) \
272b8021494Sopenharmony_ci  f.Combine(Read##NAME().GetCPUFeatures());
273b8021494Sopenharmony_ci  VIXL_AARCH64_ID_REG_LIST(VIXL_COMBINE_ID_REG)
274b8021494Sopenharmony_ci#undef VIXL_COMBINE_ID_REG
275b8021494Sopenharmony_ci  return f;
276b8021494Sopenharmony_ci}
277b8021494Sopenharmony_ci
278b8021494Sopenharmony_ciCPUFeatures CPU::InferCPUFeaturesFromOS(
279b8021494Sopenharmony_ci    CPUFeatures::QueryIDRegistersOption option) {
280b8021494Sopenharmony_ci  CPUFeatures features;
281b8021494Sopenharmony_ci
282b8021494Sopenharmony_ci#ifdef VIXL_USE_LINUX_HWCAP
283b8021494Sopenharmony_ci  // Map each set bit onto a feature. Ideally, we'd use HWCAP_* macros rather
284b8021494Sopenharmony_ci  // than explicit bits, but explicit bits allow us to identify features that
285b8021494Sopenharmony_ci  // the toolchain doesn't know about.
286b8021494Sopenharmony_ci  static const CPUFeatures::Feature kFeatureBitsLow[] =
287b8021494Sopenharmony_ci      {// Bits 0-7
288b8021494Sopenharmony_ci       CPUFeatures::kFP,
289b8021494Sopenharmony_ci       CPUFeatures::kNEON,
290b8021494Sopenharmony_ci       CPUFeatures::kNone,  // "EVTSTRM", which VIXL doesn't track.
291b8021494Sopenharmony_ci       CPUFeatures::kAES,
292b8021494Sopenharmony_ci       CPUFeatures::kPmull1Q,
293b8021494Sopenharmony_ci       CPUFeatures::kSHA1,
294b8021494Sopenharmony_ci       CPUFeatures::kSHA2,
295b8021494Sopenharmony_ci       CPUFeatures::kCRC32,
296b8021494Sopenharmony_ci       // Bits 8-15
297b8021494Sopenharmony_ci       CPUFeatures::kAtomics,
298b8021494Sopenharmony_ci       CPUFeatures::kFPHalf,
299b8021494Sopenharmony_ci       CPUFeatures::kNEONHalf,
300b8021494Sopenharmony_ci       CPUFeatures::kIDRegisterEmulation,
301b8021494Sopenharmony_ci       CPUFeatures::kRDM,
302b8021494Sopenharmony_ci       CPUFeatures::kJSCVT,
303b8021494Sopenharmony_ci       CPUFeatures::kFcma,
304b8021494Sopenharmony_ci       CPUFeatures::kRCpc,
305b8021494Sopenharmony_ci       // Bits 16-23
306b8021494Sopenharmony_ci       CPUFeatures::kDCPoP,
307b8021494Sopenharmony_ci       CPUFeatures::kSHA3,
308b8021494Sopenharmony_ci       CPUFeatures::kSM3,
309b8021494Sopenharmony_ci       CPUFeatures::kSM4,
310b8021494Sopenharmony_ci       CPUFeatures::kDotProduct,
311b8021494Sopenharmony_ci       CPUFeatures::kSHA512,
312b8021494Sopenharmony_ci       CPUFeatures::kSVE,
313b8021494Sopenharmony_ci       CPUFeatures::kFHM,
314b8021494Sopenharmony_ci       // Bits 24-31
315b8021494Sopenharmony_ci       CPUFeatures::kDIT,
316b8021494Sopenharmony_ci       CPUFeatures::kUSCAT,
317b8021494Sopenharmony_ci       CPUFeatures::kRCpcImm,
318b8021494Sopenharmony_ci       CPUFeatures::kFlagM,
319b8021494Sopenharmony_ci       CPUFeatures::kSSBSControl,
320b8021494Sopenharmony_ci       CPUFeatures::kSB,
321b8021494Sopenharmony_ci       CPUFeatures::kPAuth,
322b8021494Sopenharmony_ci       CPUFeatures::kPAuthGeneric};
323b8021494Sopenharmony_ci  VIXL_STATIC_ASSERT(ArrayLength(kFeatureBitsLow) < 64);
324b8021494Sopenharmony_ci
325b8021494Sopenharmony_ci  static const CPUFeatures::Feature kFeatureBitsHigh[] =
326b8021494Sopenharmony_ci      {// Bits 0-7
327b8021494Sopenharmony_ci       CPUFeatures::kDCCVADP,
328b8021494Sopenharmony_ci       CPUFeatures::kSVE2,
329b8021494Sopenharmony_ci       CPUFeatures::kSVEAES,
330b8021494Sopenharmony_ci       CPUFeatures::kSVEPmull128,
331b8021494Sopenharmony_ci       CPUFeatures::kSVEBitPerm,
332b8021494Sopenharmony_ci       CPUFeatures::kSVESHA3,
333b8021494Sopenharmony_ci       CPUFeatures::kSVESM4,
334b8021494Sopenharmony_ci       CPUFeatures::kAXFlag,
335b8021494Sopenharmony_ci       // Bits 8-15
336b8021494Sopenharmony_ci       CPUFeatures::kFrintToFixedSizedInt,
337b8021494Sopenharmony_ci       CPUFeatures::kSVEI8MM,
338b8021494Sopenharmony_ci       CPUFeatures::kSVEF32MM,
339b8021494Sopenharmony_ci       CPUFeatures::kSVEF64MM,
340b8021494Sopenharmony_ci       CPUFeatures::kSVEBF16,
341b8021494Sopenharmony_ci       CPUFeatures::kI8MM,
342b8021494Sopenharmony_ci       CPUFeatures::kBF16,
343b8021494Sopenharmony_ci       CPUFeatures::kDGH,
344b8021494Sopenharmony_ci       // Bits 16-23
345b8021494Sopenharmony_ci       CPUFeatures::kRNG,
346b8021494Sopenharmony_ci       CPUFeatures::kBTI,
347b8021494Sopenharmony_ci       CPUFeatures::kMTE,
348b8021494Sopenharmony_ci       CPUFeatures::kECV,
349b8021494Sopenharmony_ci       CPUFeatures::kAFP,
350b8021494Sopenharmony_ci       CPUFeatures::kRPRES,
351b8021494Sopenharmony_ci       CPUFeatures::kMTE3,
352b8021494Sopenharmony_ci       CPUFeatures::kSME,
353b8021494Sopenharmony_ci       // Bits 24-31
354b8021494Sopenharmony_ci       CPUFeatures::kSMEi16i64,
355b8021494Sopenharmony_ci       CPUFeatures::kSMEf64f64,
356b8021494Sopenharmony_ci       CPUFeatures::kSMEi8i32,
357b8021494Sopenharmony_ci       CPUFeatures::kSMEf16f32,
358b8021494Sopenharmony_ci       CPUFeatures::kSMEb16f32,
359b8021494Sopenharmony_ci       CPUFeatures::kSMEf32f32,
360b8021494Sopenharmony_ci       CPUFeatures::kSMEfa64,
361b8021494Sopenharmony_ci       CPUFeatures::kWFXT,
362b8021494Sopenharmony_ci       // Bits 32-39
363b8021494Sopenharmony_ci       CPUFeatures::kEBF16,
364b8021494Sopenharmony_ci       CPUFeatures::kSVE_EBF16};
365b8021494Sopenharmony_ci  VIXL_STATIC_ASSERT(ArrayLength(kFeatureBitsHigh) < 64);
366b8021494Sopenharmony_ci
367b8021494Sopenharmony_ci  auto combine_features = [&features](uint64_t hwcap,
368b8021494Sopenharmony_ci                                      const CPUFeatures::Feature* feature_array,
369b8021494Sopenharmony_ci                                      size_t features_size) {
370b8021494Sopenharmony_ci    for (size_t i = 0; i < features_size; i++) {
371b8021494Sopenharmony_ci      if (hwcap & (UINT64_C(1) << i)) features.Combine(feature_array[i]);
372b8021494Sopenharmony_ci    }
373b8021494Sopenharmony_ci  };
374b8021494Sopenharmony_ci
375b8021494Sopenharmony_ci  uint64_t hwcap_low = getauxval(AT_HWCAP);
376b8021494Sopenharmony_ci  uint64_t hwcap_high = getauxval(AT_HWCAP2);
377b8021494Sopenharmony_ci
378b8021494Sopenharmony_ci  combine_features(hwcap_low, kFeatureBitsLow, ArrayLength(kFeatureBitsLow));
379b8021494Sopenharmony_ci  combine_features(hwcap_high, kFeatureBitsHigh, ArrayLength(kFeatureBitsHigh));
380b8021494Sopenharmony_ci
381b8021494Sopenharmony_ci  // MTE support from HWCAP2 signifies FEAT_MTE1 and FEAT_MTE2 support
382b8021494Sopenharmony_ci  if (features.Has(CPUFeatures::kMTE)) {
383b8021494Sopenharmony_ci    features.Combine(CPUFeatures::kMTEInstructions);
384b8021494Sopenharmony_ci  }
385b8021494Sopenharmony_ci#endif  // VIXL_USE_LINUX_HWCAP
386b8021494Sopenharmony_ci
387b8021494Sopenharmony_ci  if ((option == CPUFeatures::kQueryIDRegistersIfAvailable) &&
388b8021494Sopenharmony_ci      (features.Has(CPUFeatures::kIDRegisterEmulation))) {
389b8021494Sopenharmony_ci    features.Combine(InferCPUFeaturesFromIDRegisters());
390b8021494Sopenharmony_ci  }
391b8021494Sopenharmony_ci  return features;
392b8021494Sopenharmony_ci}
393b8021494Sopenharmony_ci
394b8021494Sopenharmony_ci
395b8021494Sopenharmony_ci#ifdef __aarch64__
396b8021494Sopenharmony_ci#define VIXL_READ_ID_REG(NAME, MRS_ARG)        \
397b8021494Sopenharmony_ci  NAME CPU::Read##NAME() {                     \
398b8021494Sopenharmony_ci    uint64_t value = 0;                        \
399b8021494Sopenharmony_ci    __asm__("mrs %0, " MRS_ARG : "=r"(value)); \
400b8021494Sopenharmony_ci    return NAME(value);                        \
401b8021494Sopenharmony_ci  }
402b8021494Sopenharmony_ci#else  // __aarch64__
403b8021494Sopenharmony_ci#define VIXL_READ_ID_REG(NAME, MRS_ARG) \
404b8021494Sopenharmony_ci  NAME CPU::Read##NAME() {              \
405b8021494Sopenharmony_ci    VIXL_UNREACHABLE();                 \
406b8021494Sopenharmony_ci    return NAME(0);                     \
407b8021494Sopenharmony_ci  }
408b8021494Sopenharmony_ci#endif  // __aarch64__
409b8021494Sopenharmony_ci
410b8021494Sopenharmony_ciVIXL_AARCH64_ID_REG_LIST(VIXL_READ_ID_REG)
411b8021494Sopenharmony_ci
412b8021494Sopenharmony_ci#undef VIXL_READ_ID_REG
413b8021494Sopenharmony_ci
414b8021494Sopenharmony_ci
415b8021494Sopenharmony_ci// Initialise to smallest possible cache size.
416b8021494Sopenharmony_ciunsigned CPU::dcache_line_size_ = 1;
417b8021494Sopenharmony_ciunsigned CPU::icache_line_size_ = 1;
418b8021494Sopenharmony_ci
419b8021494Sopenharmony_ci
420b8021494Sopenharmony_ci// Currently computes I and D cache line size.
421b8021494Sopenharmony_civoid CPU::SetUp() {
422b8021494Sopenharmony_ci  uint32_t cache_type_register = GetCacheType();
423b8021494Sopenharmony_ci
424b8021494Sopenharmony_ci  // The cache type register holds information about the caches, including I
425b8021494Sopenharmony_ci  // D caches line size.
426b8021494Sopenharmony_ci  static const int kDCacheLineSizeShift = 16;
427b8021494Sopenharmony_ci  static const int kICacheLineSizeShift = 0;
428b8021494Sopenharmony_ci  static const uint32_t kDCacheLineSizeMask = 0xf << kDCacheLineSizeShift;
429b8021494Sopenharmony_ci  static const uint32_t kICacheLineSizeMask = 0xf << kICacheLineSizeShift;
430b8021494Sopenharmony_ci
431b8021494Sopenharmony_ci  // The cache type register holds the size of the I and D caches in words as
432b8021494Sopenharmony_ci  // a power of two.
433b8021494Sopenharmony_ci  uint32_t dcache_line_size_power_of_two =
434b8021494Sopenharmony_ci      (cache_type_register & kDCacheLineSizeMask) >> kDCacheLineSizeShift;
435b8021494Sopenharmony_ci  uint32_t icache_line_size_power_of_two =
436b8021494Sopenharmony_ci      (cache_type_register & kICacheLineSizeMask) >> kICacheLineSizeShift;
437b8021494Sopenharmony_ci
438b8021494Sopenharmony_ci  dcache_line_size_ = 4 << dcache_line_size_power_of_two;
439b8021494Sopenharmony_ci  icache_line_size_ = 4 << icache_line_size_power_of_two;
440b8021494Sopenharmony_ci}
441b8021494Sopenharmony_ci
442b8021494Sopenharmony_ci
443b8021494Sopenharmony_ciuint32_t CPU::GetCacheType() {
444b8021494Sopenharmony_ci#ifdef __aarch64__
445b8021494Sopenharmony_ci  uint64_t cache_type_register;
446b8021494Sopenharmony_ci  // Copy the content of the cache type register to a core register.
447b8021494Sopenharmony_ci  __asm__ __volatile__("mrs %[ctr], ctr_el0"  // NOLINT(runtime/references)
448b8021494Sopenharmony_ci                       : [ctr] "=r"(cache_type_register));
449b8021494Sopenharmony_ci  VIXL_ASSERT(IsUint32(cache_type_register));
450b8021494Sopenharmony_ci  return static_cast<uint32_t>(cache_type_register);
451b8021494Sopenharmony_ci#else
452b8021494Sopenharmony_ci  // This will lead to a cache with 1 byte long lines, which is fine since
453b8021494Sopenharmony_ci  // neither EnsureIAndDCacheCoherency nor the simulator will need this
454b8021494Sopenharmony_ci  // information.
455b8021494Sopenharmony_ci  return 0;
456b8021494Sopenharmony_ci#endif
457b8021494Sopenharmony_ci}
458b8021494Sopenharmony_ci
459b8021494Sopenharmony_ci
460b8021494Sopenharmony_ci// Query the SVE vector length. This requires CPUFeatures::kSVE.
461b8021494Sopenharmony_ciint CPU::ReadSVEVectorLengthInBits() {
462b8021494Sopenharmony_ci#ifdef __aarch64__
463b8021494Sopenharmony_ci  uint64_t vl;
464b8021494Sopenharmony_ci  // To support compilers that don't understand `rdvl`, encode the value
465b8021494Sopenharmony_ci  // directly and move it manually.
466b8021494Sopenharmony_ci  __asm__(
467b8021494Sopenharmony_ci      "   .word 0x04bf5100\n"  // rdvl x0, #8
468b8021494Sopenharmony_ci      "   mov %[vl], x0\n"
469b8021494Sopenharmony_ci      : [vl] "=r"(vl)
470b8021494Sopenharmony_ci      :
471b8021494Sopenharmony_ci      : "x0");
472b8021494Sopenharmony_ci  VIXL_ASSERT(vl <= INT_MAX);
473b8021494Sopenharmony_ci  return static_cast<int>(vl);
474b8021494Sopenharmony_ci#else
475b8021494Sopenharmony_ci  VIXL_UNREACHABLE();
476b8021494Sopenharmony_ci  return 0;
477b8021494Sopenharmony_ci#endif
478b8021494Sopenharmony_ci}
479b8021494Sopenharmony_ci
480b8021494Sopenharmony_ci
481b8021494Sopenharmony_civoid CPU::EnsureIAndDCacheCoherency(void* address, size_t length) {
482b8021494Sopenharmony_ci#ifdef __aarch64__
483b8021494Sopenharmony_ci  // Implement the cache synchronisation for all targets where AArch64 is the
484b8021494Sopenharmony_ci  // host, even if we're building the simulator for an AAarch64 host. This
485b8021494Sopenharmony_ci  // allows for cases where the user wants to simulate code as well as run it
486b8021494Sopenharmony_ci  // natively.
487b8021494Sopenharmony_ci
488b8021494Sopenharmony_ci  if (length == 0) {
489b8021494Sopenharmony_ci    return;
490b8021494Sopenharmony_ci  }
491b8021494Sopenharmony_ci
492b8021494Sopenharmony_ci  // The code below assumes user space cache operations are allowed.
493b8021494Sopenharmony_ci
494b8021494Sopenharmony_ci  // Work out the line sizes for each cache, and use them to determine the
495b8021494Sopenharmony_ci  // start addresses.
496b8021494Sopenharmony_ci  uintptr_t start = reinterpret_cast<uintptr_t>(address);
497b8021494Sopenharmony_ci  uintptr_t dsize = static_cast<uintptr_t>(dcache_line_size_);
498b8021494Sopenharmony_ci  uintptr_t isize = static_cast<uintptr_t>(icache_line_size_);
499b8021494Sopenharmony_ci  uintptr_t dline = start & ~(dsize - 1);
500b8021494Sopenharmony_ci  uintptr_t iline = start & ~(isize - 1);
501b8021494Sopenharmony_ci
502b8021494Sopenharmony_ci  // Cache line sizes are always a power of 2.
503b8021494Sopenharmony_ci  VIXL_ASSERT(IsPowerOf2(dsize));
504b8021494Sopenharmony_ci  VIXL_ASSERT(IsPowerOf2(isize));
505b8021494Sopenharmony_ci  uintptr_t end = start + length;
506b8021494Sopenharmony_ci
507b8021494Sopenharmony_ci  do {
508b8021494Sopenharmony_ci    __asm__ __volatile__(
509b8021494Sopenharmony_ci        // Clean each line of the D cache containing the target data.
510b8021494Sopenharmony_ci        //
511b8021494Sopenharmony_ci        // dc       : Data Cache maintenance
512b8021494Sopenharmony_ci        //     c    : Clean
513b8021494Sopenharmony_ci        //      va  : by (Virtual) Address
514b8021494Sopenharmony_ci        //        u : to the point of Unification
515b8021494Sopenharmony_ci        // The point of unification for a processor is the point by which the
516b8021494Sopenharmony_ci        // instruction and data caches are guaranteed to see the same copy of a
517b8021494Sopenharmony_ci        // memory location. See ARM DDI 0406B page B2-12 for more information.
518b8021494Sopenharmony_ci        "   dc    cvau, %[dline]\n"
519b8021494Sopenharmony_ci        :
520b8021494Sopenharmony_ci        : [dline] "r"(dline)
521b8021494Sopenharmony_ci        // This code does not write to memory, but the "memory" dependency
522b8021494Sopenharmony_ci        // prevents GCC from reordering the code.
523b8021494Sopenharmony_ci        : "memory");
524b8021494Sopenharmony_ci    dline += dsize;
525b8021494Sopenharmony_ci  } while (dline < end);
526b8021494Sopenharmony_ci
527b8021494Sopenharmony_ci  __asm__ __volatile__(
528b8021494Sopenharmony_ci      // Make sure that the data cache operations (above) complete before the
529b8021494Sopenharmony_ci      // instruction cache operations (below).
530b8021494Sopenharmony_ci      //
531b8021494Sopenharmony_ci      // dsb      : Data Synchronisation Barrier
532b8021494Sopenharmony_ci      //      ish : Inner SHareable domain
533b8021494Sopenharmony_ci      //
534b8021494Sopenharmony_ci      // The point of unification for an Inner Shareable shareability domain is
535b8021494Sopenharmony_ci      // the point by which the instruction and data caches of all the
536b8021494Sopenharmony_ci      // processors
537b8021494Sopenharmony_ci      // in that Inner Shareable shareability domain are guaranteed to see the
538b8021494Sopenharmony_ci      // same copy of a memory location. See ARM DDI 0406B page B2-12 for more
539b8021494Sopenharmony_ci      // information.
540b8021494Sopenharmony_ci      "   dsb   ish\n"
541b8021494Sopenharmony_ci      :
542b8021494Sopenharmony_ci      :
543b8021494Sopenharmony_ci      : "memory");
544b8021494Sopenharmony_ci
545b8021494Sopenharmony_ci  do {
546b8021494Sopenharmony_ci    __asm__ __volatile__(
547b8021494Sopenharmony_ci        // Invalidate each line of the I cache containing the target data.
548b8021494Sopenharmony_ci        //
549b8021494Sopenharmony_ci        // ic      : Instruction Cache maintenance
550b8021494Sopenharmony_ci        //    i    : Invalidate
551b8021494Sopenharmony_ci        //     va  : by Address
552b8021494Sopenharmony_ci        //       u : to the point of Unification
553b8021494Sopenharmony_ci        "   ic   ivau, %[iline]\n"
554b8021494Sopenharmony_ci        :
555b8021494Sopenharmony_ci        : [iline] "r"(iline)
556b8021494Sopenharmony_ci        : "memory");
557b8021494Sopenharmony_ci    iline += isize;
558b8021494Sopenharmony_ci  } while (iline < end);
559b8021494Sopenharmony_ci
560b8021494Sopenharmony_ci  __asm__ __volatile__(
561b8021494Sopenharmony_ci      // Make sure that the instruction cache operations (above) take effect
562b8021494Sopenharmony_ci      // before the isb (below).
563b8021494Sopenharmony_ci      "   dsb  ish\n"
564b8021494Sopenharmony_ci
565b8021494Sopenharmony_ci      // Ensure that any instructions already in the pipeline are discarded and
566b8021494Sopenharmony_ci      // reloaded from the new data.
567b8021494Sopenharmony_ci      // isb : Instruction Synchronisation Barrier
568b8021494Sopenharmony_ci      "   isb\n"
569b8021494Sopenharmony_ci      :
570b8021494Sopenharmony_ci      :
571b8021494Sopenharmony_ci      : "memory");
572b8021494Sopenharmony_ci#else
573b8021494Sopenharmony_ci  // If the host isn't AArch64, we must be using the simulator, so this function
574b8021494Sopenharmony_ci  // doesn't have to do anything.
575b8021494Sopenharmony_ci  USE(address, length);
576b8021494Sopenharmony_ci#endif
577b8021494Sopenharmony_ci}
578b8021494Sopenharmony_ci
579b8021494Sopenharmony_ci
580b8021494Sopenharmony_ci}  // namespace aarch64
581b8021494Sopenharmony_ci}  // namespace vixl
582