1 // Copyright 2014, 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_AARCH64_H 28 #define VIXL_CPU_AARCH64_H 29 30 #include "../cpu-features.h" 31 #include "../globals-vixl.h" 32 33 #include "instructions-aarch64.h" 34 #include "simulator-aarch64.h" 35 36 #ifndef VIXL_INCLUDE_TARGET_AARCH64 37 // The supporting .cc file is only compiled when the A64 target is selected. 38 // Throw an explicit error now to avoid a harder-to-debug linker error later. 39 // 40 // These helpers _could_ work on any AArch64 host, even when generating AArch32 41 // code, but we don't support this because the available features may differ 42 // between AArch32 and AArch64 on the same platform, so basing AArch32 code 43 // generation on aarch64::CPU features is probably broken. 44 #error cpu-aarch64.h requires VIXL_INCLUDE_TARGET_AARCH64 (scons target=a64). 45 #endif 46 47 namespace vixl { 48 namespace aarch64 { 49 50 // A CPU ID register, for use with CPUFeatures::kIDRegisterEmulation. Fields 51 // specific to each register are described in relevant subclasses. 52 class IDRegister { 53 protected: IDRegister(uint64_t value = 0)54 explicit IDRegister(uint64_t value = 0) : value_(value) {} 55 56 class Field { 57 public: 58 enum Type { kUnsigned, kSigned }; 59 60 static const int kMaxWidthInBits = 4; 61 62 // This needs to be constexpr so that fields have "constant initialisation". 63 // This avoids initialisation order problems when these values are used to 64 // (dynamically) initialise static variables, etc. Field(int lsb, int bitWidth = kMaxWidthInBits, Type type = kUnsigned)65 explicit constexpr Field(int lsb, 66 int bitWidth = kMaxWidthInBits, 67 Type type = kUnsigned) 68 : lsb_(lsb), bitWidth_(bitWidth), type_(type) {} 69 GetWidthInBits() const70 int GetWidthInBits() const { return bitWidth_; } GetLsb() const71 int GetLsb() const { return lsb_; } GetMsb() const72 int GetMsb() const { return lsb_ + GetWidthInBits() - 1; } GetType() const73 Type GetType() const { return type_; } 74 75 private: 76 int lsb_; 77 int bitWidth_; 78 Type type_; 79 }; 80 81 public: 82 // Extract the specified field, performing sign-extension for signed fields. 83 // This allows us to implement the 'value >= number' detection mechanism 84 // recommended by the Arm ARM, for both signed and unsigned fields. 85 int Get(Field field) const; 86 87 private: 88 uint64_t value_; 89 }; 90 91 class AA64PFR0 : public IDRegister { 92 public: AA64PFR0(uint64_t value)93 explicit AA64PFR0(uint64_t value) : IDRegister(value) {} 94 95 CPUFeatures GetCPUFeatures() const; 96 97 private: 98 static const Field kFP; 99 static const Field kAdvSIMD; 100 static const Field kRAS; 101 static const Field kSVE; 102 static const Field kDIT; 103 static const Field kCSV2; 104 static const Field kCSV3; 105 }; 106 107 class AA64PFR1 : public IDRegister { 108 public: AA64PFR1(uint64_t value)109 explicit AA64PFR1(uint64_t value) : IDRegister(value) {} 110 111 CPUFeatures GetCPUFeatures() const; 112 113 private: 114 static const Field kBT; 115 static const Field kSSBS; 116 static const Field kMTE; 117 static const Field kSME; 118 }; 119 120 class AA64ISAR0 : public IDRegister { 121 public: AA64ISAR0(uint64_t value)122 explicit AA64ISAR0(uint64_t value) : IDRegister(value) {} 123 124 CPUFeatures GetCPUFeatures() const; 125 126 private: 127 static const Field kAES; 128 static const Field kSHA1; 129 static const Field kSHA2; 130 static const Field kCRC32; 131 static const Field kAtomic; 132 static const Field kRDM; 133 static const Field kSHA3; 134 static const Field kSM3; 135 static const Field kSM4; 136 static const Field kDP; 137 static const Field kFHM; 138 static const Field kTS; 139 static const Field kRNDR; 140 }; 141 142 class AA64ISAR1 : public IDRegister { 143 public: AA64ISAR1(uint64_t value)144 explicit AA64ISAR1(uint64_t value) : IDRegister(value) {} 145 146 CPUFeatures GetCPUFeatures() const; 147 148 private: 149 static const Field kDPB; 150 static const Field kAPA; 151 static const Field kAPI; 152 static const Field kJSCVT; 153 static const Field kFCMA; 154 static const Field kLRCPC; 155 static const Field kGPA; 156 static const Field kGPI; 157 static const Field kFRINTTS; 158 static const Field kSB; 159 static const Field kSPECRES; 160 static const Field kBF16; 161 static const Field kDGH; 162 static const Field kI8MM; 163 }; 164 165 class AA64ISAR2 : public IDRegister { 166 public: AA64ISAR2(uint64_t value)167 explicit AA64ISAR2(uint64_t value) : IDRegister(value) {} 168 169 CPUFeatures GetCPUFeatures() const; 170 171 private: 172 static const Field kWFXT; 173 static const Field kRPRES; 174 static const Field kMOPS; 175 static const Field kCSSC; 176 }; 177 178 class AA64MMFR0 : public IDRegister { 179 public: AA64MMFR0(uint64_t value)180 explicit AA64MMFR0(uint64_t value) : IDRegister(value) {} 181 182 CPUFeatures GetCPUFeatures() const; 183 184 private: 185 static const Field kECV; 186 }; 187 188 class AA64MMFR1 : public IDRegister { 189 public: AA64MMFR1(uint64_t value)190 explicit AA64MMFR1(uint64_t value) : IDRegister(value) {} 191 192 CPUFeatures GetCPUFeatures() const; 193 194 private: 195 static const Field kLO; 196 static const Field kAFP; 197 }; 198 199 class AA64MMFR2 : public IDRegister { 200 public: AA64MMFR2(uint64_t value)201 explicit AA64MMFR2(uint64_t value) : IDRegister(value) {} 202 203 CPUFeatures GetCPUFeatures() const; 204 205 private: 206 static const Field kAT; 207 }; 208 209 class AA64ZFR0 : public IDRegister { 210 public: AA64ZFR0(uint64_t value)211 explicit AA64ZFR0(uint64_t value) : IDRegister(value) {} 212 213 CPUFeatures GetCPUFeatures() const; 214 215 private: 216 static const Field kSVEver; 217 static const Field kAES; 218 static const Field kBitPerm; 219 static const Field kBF16; 220 static const Field kSHA3; 221 static const Field kSM4; 222 static const Field kI8MM; 223 static const Field kF32MM; 224 static const Field kF64MM; 225 }; 226 227 class AA64SMFR0 : public IDRegister { 228 public: AA64SMFR0(uint64_t value)229 explicit AA64SMFR0(uint64_t value) : IDRegister(value) {} 230 231 CPUFeatures GetCPUFeatures() const; 232 233 private: 234 static const Field kSMEf32f32; 235 static const Field kSMEb16f32; 236 static const Field kSMEf16f32; 237 static const Field kSMEi8i32; 238 static const Field kSMEf64f64; 239 static const Field kSMEi16i64; 240 static const Field kSMEfa64; 241 }; 242 243 class CPU { 244 public: 245 // Initialise CPU support. 246 static void SetUp(); 247 248 // Ensures the data at a given address and with a given size is the same for 249 // the I and D caches. I and D caches are not automatically coherent on ARM 250 // so this operation is required before any dynamically generated code can 251 // safely run. 252 static void EnsureIAndDCacheCoherency(void *address, size_t length); 253 254 // Read and interpret the ID registers. This requires 255 // CPUFeatures::kIDRegisterEmulation, and therefore cannot be called on 256 // non-AArch64 platforms. 257 static CPUFeatures InferCPUFeaturesFromIDRegisters(); 258 259 // Read and interpret CPUFeatures reported by the OS. Failed queries (or 260 // unsupported platforms) return an empty list. Note that this is 261 // indistinguishable from a successful query on a platform that advertises no 262 // features. 263 // 264 // Non-AArch64 hosts are considered to be unsupported platforms, and this 265 // function returns an empty list. 266 static CPUFeatures InferCPUFeaturesFromOS( 267 CPUFeatures::QueryIDRegistersOption option = 268 CPUFeatures::kQueryIDRegistersIfAvailable); 269 270 // Query the SVE vector length. This requires CPUFeatures::kSVE. 271 static int ReadSVEVectorLengthInBits(); 272 273 // Handle tagged pointers. 274 template <typename T> SetPointerTag(T pointer, uint64_t tag)275 static T SetPointerTag(T pointer, uint64_t tag) { 276 VIXL_ASSERT(IsUintN(kAddressTagWidth, tag)); 277 278 // Use C-style casts to get static_cast behaviour for integral types (T), 279 // and reinterpret_cast behaviour for other types. 280 281 uint64_t raw = (uint64_t)pointer; 282 VIXL_STATIC_ASSERT(sizeof(pointer) == sizeof(raw)); 283 284 raw = (raw & ~kAddressTagMask) | (tag << kAddressTagOffset); 285 return (T)raw; 286 } 287 288 template <typename T> GetPointerTag(T pointer)289 static uint64_t GetPointerTag(T pointer) { 290 // Use C-style casts to get static_cast behaviour for integral types (T), 291 // and reinterpret_cast behaviour for other types. 292 293 uint64_t raw = (uint64_t)pointer; 294 VIXL_STATIC_ASSERT(sizeof(pointer) == sizeof(raw)); 295 296 return (raw & kAddressTagMask) >> kAddressTagOffset; 297 } 298 299 private: 300 #define VIXL_AARCH64_ID_REG_LIST(V) \ 301 V(AA64PFR0, "ID_AA64PFR0_EL1") \ 302 V(AA64PFR1, "ID_AA64PFR1_EL1") \ 303 V(AA64ISAR0, "ID_AA64ISAR0_EL1") \ 304 V(AA64ISAR1, "ID_AA64ISAR1_EL1") \ 305 V(AA64MMFR0, "ID_AA64MMFR0_EL1") \ 306 V(AA64MMFR1, "ID_AA64MMFR1_EL1") \ 307 /* These registers are RES0 in the baseline Arm8.0. We can always safely */ \ 308 /* read them, but some compilers don't accept the symbolic names. */ \ 309 V(AA64SMFR0, "S3_0_C0_C4_5") \ 310 V(AA64ISAR2, "S3_0_C0_C6_2") \ 311 V(AA64MMFR2, "S3_0_C0_C7_2") \ 312 V(AA64ZFR0, "S3_0_C0_C4_4") 313 314 #define VIXL_READ_ID_REG(NAME, MRS_ARG) static NAME Read##NAME(); 315 // On native AArch64 platforms, read the named CPU ID registers. These require 316 // CPUFeatures::kIDRegisterEmulation, and should not be called on non-AArch64 317 // platforms. 318 VIXL_AARCH64_ID_REG_LIST(VIXL_READ_ID_REG) 319 #undef VIXL_READ_ID_REG 320 321 // Return the content of the cache type register. 322 static uint32_t GetCacheType(); 323 324 // I and D cache line size in bytes. 325 static unsigned icache_line_size_; 326 static unsigned dcache_line_size_; 327 }; 328 329 } // namespace aarch64 330 } // namespace vixl 331 332 #endif // VIXL_CPU_AARCH64_H 333