| 1 |
// Copyright 2015, 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 |
#include "jit/arm64/vixl/Cpu-vixl.h" |
| 28 |
|
| 29 |
#include "jstypes.h" |
| 30 |
|
| 31 |
#if defined(__aarch64__) && (defined(__ANDROID__) || defined(__linux__)) |
| 32 |
#include <sys/auxv.h> |
| 33 |
#define VIXL_USE_LINUX_HWCAP 1 |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "jit/arm64/vixl/Utils-vixl.h" |
| 37 |
|
| 38 |
|
| 39 |
namespace vixl { |
| 40 |
|
| 41 |
|
| 42 |
const IDRegister::Field AA64PFR0::kFP(16, Field::kSigned); |
| 43 |
const IDRegister::Field AA64PFR0::kAdvSIMD(20, Field::kSigned); |
| 44 |
const IDRegister::Field AA64PFR0::kSVE(32); |
| 45 |
const IDRegister::Field AA64PFR0::kDIT(48); |
| 46 |
|
| 47 |
const IDRegister::Field AA64PFR1::kBT(0); |
| 48 |
|
| 49 |
const IDRegister::Field AA64ISAR0::kAES(4); |
| 50 |
const IDRegister::Field AA64ISAR0::kSHA1(8); |
| 51 |
const IDRegister::Field AA64ISAR0::kSHA2(12); |
| 52 |
const IDRegister::Field AA64ISAR0::kCRC32(16); |
| 53 |
const IDRegister::Field AA64ISAR0::kAtomic(20); |
| 54 |
const IDRegister::Field AA64ISAR0::kRDM(28); |
| 55 |
const IDRegister::Field AA64ISAR0::kSHA3(32); |
| 56 |
const IDRegister::Field AA64ISAR0::kSM3(36); |
| 57 |
const IDRegister::Field AA64ISAR0::kSM4(40); |
| 58 |
const IDRegister::Field AA64ISAR0::kDP(44); |
| 59 |
const IDRegister::Field AA64ISAR0::kFHM(48); |
| 60 |
const IDRegister::Field AA64ISAR0::kTS(52); |
| 61 |
|
| 62 |
const IDRegister::Field AA64ISAR1::kDPB(0); |
| 63 |
const IDRegister::Field AA64ISAR1::kAPA(4); |
| 64 |
const IDRegister::Field AA64ISAR1::kAPI(8); |
| 65 |
const IDRegister::Field AA64ISAR1::kJSCVT(12); |
| 66 |
const IDRegister::Field AA64ISAR1::kFCMA(16); |
| 67 |
const IDRegister::Field AA64ISAR1::kLRCPC(20); |
| 68 |
const IDRegister::Field AA64ISAR1::kGPA(24); |
| 69 |
const IDRegister::Field AA64ISAR1::kGPI(28); |
| 70 |
const IDRegister::Field AA64ISAR1::kFRINTTS(32); |
| 71 |
const IDRegister::Field AA64ISAR1::kSB(36); |
| 72 |
const IDRegister::Field AA64ISAR1::kSPECRES(40); |
| 73 |
|
| 74 |
const IDRegister::Field AA64MMFR1::kLO(16); |
| 75 |
|
| 76 |
CPUFeatures AA64PFR0::GetCPUFeatures() const { |
| 77 |
CPUFeatures f; |
| 78 |
if (Get(kFP) >= 0) f.Combine(CPUFeatures::kFP); |
| 79 |
if (Get(kFP) >= 1) f.Combine(CPUFeatures::kFPHalf); |
| 80 |
if (Get(kAdvSIMD) >= 0) f.Combine(CPUFeatures::kNEON); |
| 81 |
if (Get(kAdvSIMD) >= 1) f.Combine(CPUFeatures::kNEONHalf); |
| 82 |
if (Get(kSVE) >= 1) f.Combine(CPUFeatures::kSVE); |
| 83 |
if (Get(kDIT) >= 1) f.Combine(CPUFeatures::kDIT); |
| 84 |
return f; |
| 85 |
} |
| 86 |
|
| 87 |
CPUFeatures AA64PFR1::GetCPUFeatures() const { |
| 88 |
CPUFeatures f; |
| 89 |
if (Get(kBT) >= 1) f.Combine(CPUFeatures::kBTI); |
| 90 |
return f; |
| 91 |
} |
| 92 |
|
| 93 |
CPUFeatures AA64ISAR0::GetCPUFeatures() const { |
| 94 |
CPUFeatures f; |
| 95 |
if (Get(kAES) >= 1) f.Combine(CPUFeatures::kAES); |
| 96 |
if (Get(kAES) >= 2) f.Combine(CPUFeatures::kPmull1Q); |
| 97 |
if (Get(kSHA1) >= 1) f.Combine(CPUFeatures::kSHA1); |
| 98 |
if (Get(kSHA2) >= 1) f.Combine(CPUFeatures::kSHA2); |
| 99 |
if (Get(kSHA2) >= 2) f.Combine(CPUFeatures::kSHA512); |
| 100 |
if (Get(kCRC32) >= 1) f.Combine(CPUFeatures::kCRC32); |
| 101 |
if (Get(kAtomic) >= 1) f.Combine(CPUFeatures::kAtomics); |
| 102 |
if (Get(kRDM) >= 1) f.Combine(CPUFeatures::kRDM); |
| 103 |
if (Get(kSHA3) >= 1) f.Combine(CPUFeatures::kSHA3); |
| 104 |
if (Get(kSM3) >= 1) f.Combine(CPUFeatures::kSM3); |
| 105 |
if (Get(kSM4) >= 1) f.Combine(CPUFeatures::kSM4); |
| 106 |
if (Get(kDP) >= 1) f.Combine(CPUFeatures::kDotProduct); |
| 107 |
if (Get(kFHM) >= 1) f.Combine(CPUFeatures::kFHM); |
| 108 |
if (Get(kTS) >= 1) f.Combine(CPUFeatures::kFlagM); |
| 109 |
if (Get(kTS) >= 2) f.Combine(CPUFeatures::kAXFlag); |
| 110 |
return f; |
| 111 |
} |
| 112 |
|
| 113 |
CPUFeatures AA64ISAR1::GetCPUFeatures() const { |
| 114 |
CPUFeatures f; |
| 115 |
if (Get(kDPB) >= 1) f.Combine(CPUFeatures::kDCPoP); |
| 116 |
if (Get(kJSCVT) >= 1) f.Combine(CPUFeatures::kJSCVT); |
| 117 |
if (Get(kFCMA) >= 1) f.Combine(CPUFeatures::kFcma); |
| 118 |
if (Get(kLRCPC) >= 1) f.Combine(CPUFeatures::kRCpc); |
| 119 |
if (Get(kLRCPC) >= 2) f.Combine(CPUFeatures::kRCpcImm); |
| 120 |
if (Get(kFRINTTS) >= 1) f.Combine(CPUFeatures::kFrintToFixedSizedInt); |
| 121 |
|
| 122 |
if (Get(kAPI) >= 1) f.Combine(CPUFeatures::kPAuth); |
| 123 |
if (Get(kAPA) >= 1) f.Combine(CPUFeatures::kPAuth, CPUFeatures::kPAuthQARMA); |
| 124 |
if (Get(kGPI) >= 1) f.Combine(CPUFeatures::kPAuthGeneric); |
| 125 |
if (Get(kGPA) >= 1) { |
| 126 |
f.Combine(CPUFeatures::kPAuthGeneric, CPUFeatures::kPAuthGenericQARMA); |
| 127 |
} |
| 128 |
return f; |
| 129 |
} |
| 130 |
|
| 131 |
CPUFeatures AA64MMFR1::GetCPUFeatures() const { |
| 132 |
CPUFeatures f; |
| 133 |
if (Get(kLO) >= 1) f.Combine(CPUFeatures::kLORegions); |
| 134 |
return f; |
| 135 |
} |
| 136 |
|
| 137 |
int IDRegister::Get(IDRegister::Field field) const { |
| 138 |
int msb = field.GetMsb(); |
| 139 |
int lsb = field.GetLsb(); |
| 140 |
VIXL_STATIC_ASSERT(static_cast<size_t>(Field::kMaxWidthInBits) < |
| 141 |
(sizeof(int) * kBitsPerByte)); |
| 142 |
switch (field.GetType()) { |
| 143 |
case Field::kSigned: |
| 144 |
return static_cast<int>(ExtractSignedBitfield64(msb, lsb, value_)); |
| 145 |
case Field::kUnsigned: |
| 146 |
return static_cast<int>(ExtractUnsignedBitfield64(msb, lsb, value_)); |
| 147 |
} |
| 148 |
VIXL_UNREACHABLE(); |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
CPUFeatures CPU::InferCPUFeaturesFromIDRegisters() { |
| 153 |
CPUFeatures f; |
| 154 |
#define VIXL_COMBINE_ID_REG(NAME) f.Combine(Read##NAME().GetCPUFeatures()); |
| 155 |
VIXL_AARCH64_ID_REG_LIST(VIXL_COMBINE_ID_REG) |
| 156 |
#undef VIXL_COMBINE_ID_REG |
| 157 |
return f; |
| 158 |
} |
| 159 |
|
| 160 |
CPUFeatures CPU::InferCPUFeaturesFromOS( |
| 161 |
CPUFeatures::QueryIDRegistersOption option) { |
| 162 |
CPUFeatures features; |
| 163 |
|
| 164 |
#if VIXL_USE_LINUX_HWCAP |
| 165 |
// Map each set bit onto a feature. Ideally, we'd use HWCAP_* macros rather |
| 166 |
// than explicit bits, but explicit bits allow us to identify features that |
| 167 |
// the toolchain doesn't know about. |
| 168 |
static const CPUFeatures::Feature kFeatureBits[] = { |
| 169 |
// Bits 0-7 |
| 170 |
CPUFeatures::kFP, |
| 171 |
CPUFeatures::kNEON, |
| 172 |
CPUFeatures::kNone, // "EVTSTRM", which VIXL doesn't track. |
| 173 |
CPUFeatures::kAES, |
| 174 |
CPUFeatures::kPmull1Q, |
| 175 |
CPUFeatures::kSHA1, |
| 176 |
CPUFeatures::kSHA2, |
| 177 |
CPUFeatures::kCRC32, |
| 178 |
// Bits 8-15 |
| 179 |
CPUFeatures::kAtomics, |
| 180 |
CPUFeatures::kFPHalf, |
| 181 |
CPUFeatures::kNEONHalf, |
| 182 |
CPUFeatures::kIDRegisterEmulation, |
| 183 |
CPUFeatures::kRDM, |
| 184 |
CPUFeatures::kJSCVT, |
| 185 |
CPUFeatures::kFcma, |
| 186 |
CPUFeatures::kRCpc, |
| 187 |
// Bits 16-23 |
| 188 |
CPUFeatures::kDCPoP, |
| 189 |
CPUFeatures::kSHA3, |
| 190 |
CPUFeatures::kSM3, |
| 191 |
CPUFeatures::kSM4, |
| 192 |
CPUFeatures::kDotProduct, |
| 193 |
CPUFeatures::kSHA512, |
| 194 |
CPUFeatures::kSVE, |
| 195 |
CPUFeatures::kFHM, |
| 196 |
// Bits 24-27 |
| 197 |
CPUFeatures::kDIT, |
| 198 |
CPUFeatures::kUSCAT, |
| 199 |
CPUFeatures::kRCpcImm, |
| 200 |
CPUFeatures::kFlagM |
| 201 |
// Bits 28-31 are unassigned. |
| 202 |
}; |
| 203 |
static const size_t kFeatureBitCount = |
| 204 |
sizeof(kFeatureBits) / sizeof(kFeatureBits[0]); |
| 205 |
|
| 206 |
unsigned long auxv = getauxval(AT_HWCAP); // NOLINT(runtime/int) |
| 207 |
|
| 208 |
VIXL_STATIC_ASSERT(kFeatureBitCount < (sizeof(auxv) * kBitsPerByte)); |
| 209 |
for (size_t i = 0; i < kFeatureBitCount; i++) { |
| 210 |
if (auxv & (1UL << i)) features.Combine(kFeatureBits[i]); |
| 211 |
} |
| 212 |
#elif defined(XP_DARWIN) |
| 213 |
// Apple processors have kJSCVT, kDotProduct, and kAtomics features. |
| 214 |
features.Combine(CPUFeatures::kJSCVT, CPUFeatures::kDotProduct, |
| 215 |
CPUFeatures::kAtomics); |
| 216 |
#endif // VIXL_USE_LINUX_HWCAP |
| 217 |
|
| 218 |
if ((option == CPUFeatures::kQueryIDRegistersIfAvailable) && |
| 219 |
(features.Has(CPUFeatures::kIDRegisterEmulation))) { |
| 220 |
features.Combine(InferCPUFeaturesFromIDRegisters()); |
| 221 |
} |
| 222 |
return features; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
#ifdef __aarch64__ |
| 227 |
#define VIXL_READ_ID_REG(NAME) \ |
| 228 |
NAME CPU::Read##NAME() { \ |
| 229 |
uint64_t value = 0; \ |
| 230 |
__asm__("mrs %0, ID_" #NAME "_EL1" : "=r"(value)); \ |
| 231 |
return NAME(value); \ |
| 232 |
} |
| 233 |
#else // __aarch64__ |
| 234 |
#define VIXL_READ_ID_REG(NAME) \ |
| 235 |
NAME CPU::Read##NAME() { \ |
| 236 |
/* TODO: Use VIXL_UNREACHABLE once it works in release builds. */ \ |
| 237 |
VIXL_ABORT(); \ |
| 238 |
} |
| 239 |
#endif // __aarch64__ |
| 240 |
|
| 241 |
VIXL_AARCH64_ID_REG_LIST(VIXL_READ_ID_REG) |
| 242 |
|
| 243 |
#undef VIXL_READ_ID_REG |
| 244 |
|
| 245 |
|
| 246 |
// Initialise to smallest possible cache size. |
| 247 |
unsigned CPU::dcache_line_size_ = 1; |
| 248 |
unsigned CPU::icache_line_size_ = 1; |
| 249 |
|
| 250 |
|
| 251 |
} // namespace vixl |