1 /*
   2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2015, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "runtime/java.hpp"
  31 #include "runtime/stubCodeGenerator.hpp"
  32 #include "utilities/macros.hpp"
  33 #include "vm_version_aarch64.hpp"
  34 
  35 #include OS_HEADER_INLINE(os)
  36 
  37 #include <sys/auxv.h>
  38 #include <asm/hwcap.h>
  39 
  40 #ifndef HWCAP_AES
  41 #define HWCAP_AES   (1<<3)
  42 #endif
  43 
  44 #ifndef HWCAP_PMULL
  45 #define HWCAP_PMULL (1<<4)
  46 #endif
  47 
  48 #ifndef HWCAP_SHA1
  49 #define HWCAP_SHA1  (1<<5)
  50 #endif
  51 
  52 #ifndef HWCAP_SHA2
  53 #define HWCAP_SHA2  (1<<6)
  54 #endif
  55 
  56 #ifndef HWCAP_CRC32
  57 #define HWCAP_CRC32 (1<<7)
  58 #endif
  59 
  60 #ifndef HWCAP_ATOMICS
  61 #define HWCAP_ATOMICS (1<<8)
  62 #endif
  63 
  64 int VM_Version::_cpu;
  65 int VM_Version::_model;
  66 int VM_Version::_model2;
  67 int VM_Version::_variant;
  68 int VM_Version::_revision;
  69 int VM_Version::_stepping;
  70 VM_Version::PsrInfo VM_Version::_psr_info   = { 0, };
  71 
  72 static BufferBlob* stub_blob;
  73 static const int stub_size = 550;
  74 
  75 extern "C" {
  76   typedef void (*getPsrInfo_stub_t)(void*);
  77 }
  78 static getPsrInfo_stub_t getPsrInfo_stub = NULL;
  79 
  80 
  81 class VM_Version_StubGenerator: public StubCodeGenerator {
  82  public:
  83 
  84   VM_Version_StubGenerator(CodeBuffer *c) : StubCodeGenerator(c) {}
  85 
  86   address generate_getPsrInfo() {
  87     StubCodeMark mark(this, "VM_Version", "getPsrInfo_stub");
  88 #   define __ _masm->
  89     address start = __ pc();
  90 
  91     // void getPsrInfo(VM_Version::PsrInfo* psr_info);
  92 
  93     address entry = __ pc();
  94 
  95     __ enter();
  96 
  97     __ get_dczid_el0(rscratch1);
  98     __ strw(rscratch1, Address(c_rarg0, in_bytes(VM_Version::dczid_el0_offset())));
  99 
 100     __ get_ctr_el0(rscratch1);
 101     __ strw(rscratch1, Address(c_rarg0, in_bytes(VM_Version::ctr_el0_offset())));
 102 
 103     __ leave();
 104     __ ret(lr);
 105 
 106 #   undef __
 107 
 108     return start;
 109   }
 110 };
 111 
 112 
 113 void VM_Version::get_processor_features() {
 114   _supports_cx8 = true;
 115   _supports_atomic_getset4 = true;
 116   _supports_atomic_getadd4 = true;
 117   _supports_atomic_getset8 = true;
 118   _supports_atomic_getadd8 = true;
 119 
 120   getPsrInfo_stub(&_psr_info);
 121 
 122   int dcache_line = VM_Version::dcache_line_size();
 123 
 124   // Limit AllocatePrefetchDistance so that it does not exceed the
 125   // constraint in AllocatePrefetchDistanceConstraintFunc.
 126   if (FLAG_IS_DEFAULT(AllocatePrefetchDistance))
 127     FLAG_SET_DEFAULT(AllocatePrefetchDistance, MIN2(512, 3*dcache_line));
 128 
 129   if (FLAG_IS_DEFAULT(AllocatePrefetchStepSize))
 130     FLAG_SET_DEFAULT(AllocatePrefetchStepSize, dcache_line);
 131   if (FLAG_IS_DEFAULT(PrefetchScanIntervalInBytes))
 132     FLAG_SET_DEFAULT(PrefetchScanIntervalInBytes, 3*dcache_line);
 133   if (FLAG_IS_DEFAULT(PrefetchCopyIntervalInBytes))
 134     FLAG_SET_DEFAULT(PrefetchCopyIntervalInBytes, 3*dcache_line);
 135   if (FLAG_IS_DEFAULT(SoftwarePrefetchHintDistance))
 136     FLAG_SET_DEFAULT(SoftwarePrefetchHintDistance, 3*dcache_line);
 137 
 138   if (PrefetchCopyIntervalInBytes != -1 &&
 139        ((PrefetchCopyIntervalInBytes & 7) || (PrefetchCopyIntervalInBytes >= 32768))) {
 140     warning("PrefetchCopyIntervalInBytes must be -1, or a multiple of 8 and < 32768");
 141     PrefetchCopyIntervalInBytes &= ~7;
 142     if (PrefetchCopyIntervalInBytes >= 32768)
 143       PrefetchCopyIntervalInBytes = 32760;
 144   }
 145 
 146   if (AllocatePrefetchDistance !=-1 && (AllocatePrefetchDistance & 7)) {
 147     warning("AllocatePrefetchDistance must be multiple of 8");
 148     AllocatePrefetchDistance &= ~7;
 149   }
 150 
 151   if (AllocatePrefetchStepSize & 7) {
 152     warning("AllocatePrefetchStepSize must be multiple of 8");
 153     AllocatePrefetchStepSize &= ~7;
 154   }
 155 
 156   if (SoftwarePrefetchHintDistance != -1 &&
 157        (SoftwarePrefetchHintDistance & 7)) {
 158     warning("SoftwarePrefetchHintDistance must be -1, or a multiple of 8");
 159     SoftwarePrefetchHintDistance &= ~7;
 160   }
 161 
 162   unsigned long auxv = getauxval(AT_HWCAP);
 163 
 164   char buf[512];
 165 
 166   _features = auxv;
 167 
 168   int cpu_lines = 0;
 169   if (FILE *f = fopen("/proc/cpuinfo", "r")) {
 170     char buf[128], *p;
 171     while (fgets(buf, sizeof (buf), f) != NULL) {
 172       if ((p = strchr(buf, ':')) != NULL) {
 173         long v = strtol(p+1, NULL, 0);
 174         if (strncmp(buf, "CPU implementer", sizeof "CPU implementer" - 1) == 0) {
 175           _cpu = v;
 176           cpu_lines++;
 177         } else if (strncmp(buf, "CPU variant", sizeof "CPU variant" - 1) == 0) {
 178           _variant = v;
 179         } else if (strncmp(buf, "CPU part", sizeof "CPU part" - 1) == 0) {
 180           if (_model != v)  _model2 = _model;
 181           _model = v;
 182         } else if (strncmp(buf, "CPU revision", sizeof "CPU revision" - 1) == 0) {
 183           _revision = v;
 184         }
 185       }
 186     }
 187     fclose(f);
 188   }
 189 
 190   // Enable vendor specific features
 191 
 192   // ThunderX
 193   if (_cpu == CPU_CAVIUM && (_model == 0xA1)) {
 194     if (_variant == 0) _features |= CPU_DMB_ATOMICS;
 195     if (FLAG_IS_DEFAULT(AvoidUnalignedAccesses)) {
 196       FLAG_SET_DEFAULT(AvoidUnalignedAccesses, true);
 197     }
 198     if (FLAG_IS_DEFAULT(UseSIMDForMemoryOps)) {
 199       FLAG_SET_DEFAULT(UseSIMDForMemoryOps, (_variant > 0));
 200     }
 201     if (FLAG_IS_DEFAULT(UseSIMDForArrayEquals)) {
 202       FLAG_SET_DEFAULT(UseSIMDForArrayEquals, false);
 203     }
 204   }
 205 
 206   // ThunderX2
 207   if ((_cpu == CPU_CAVIUM && (_model == 0xAF)) ||
 208       (_cpu == CPU_BROADCOM && (_model == 0x516))) {
 209     if (FLAG_IS_DEFAULT(AvoidUnalignedAccesses)) {
 210       FLAG_SET_DEFAULT(AvoidUnalignedAccesses, true);
 211     }
 212     if (FLAG_IS_DEFAULT(UseSIMDForMemoryOps)) {
 213       FLAG_SET_DEFAULT(UseSIMDForMemoryOps, true);
 214     }
 215 #ifdef COMPILER2
 216     if (FLAG_IS_DEFAULT(UseFPUForSpilling)) {
 217       FLAG_SET_DEFAULT(UseFPUForSpilling, true);
 218     }
 219 #endif
 220   }
 221 
 222   // Cortex A53
 223   if (_cpu == CPU_ARM && (_model == 0xd03 || _model2 == 0xd03)) {
 224     _features |= CPU_A53MAC;
 225     if (FLAG_IS_DEFAULT(UseSIMDForArrayEquals)) {
 226       FLAG_SET_DEFAULT(UseSIMDForArrayEquals, false);
 227     }
 228   }
 229 
 230   // Cortex A73
 231   if (_cpu == CPU_ARM && (_model == 0xd09 || _model2 == 0xd09)) {
 232     if (FLAG_IS_DEFAULT(SoftwarePrefetchHintDistance)) {
 233       FLAG_SET_DEFAULT(SoftwarePrefetchHintDistance, -1);
 234     }
 235     // A73 is faster with short-and-easy-for-speculative-execution-loop
 236     if (FLAG_IS_DEFAULT(UseSimpleArrayEquals)) {
 237       FLAG_SET_DEFAULT(UseSimpleArrayEquals, true);
 238     }
 239   }
 240 
 241   // Neoverse N1
 242   if (_cpu == CPU_ARM && (_model == 0xd0c || _model2 == 0xd0c)) {
 243     if (FLAG_IS_DEFAULT(UseSIMDForMemoryOps)) {
 244       FLAG_SET_DEFAULT(UseSIMDForMemoryOps, true);
 245     }
 246   }
 247 
 248   if (_cpu == CPU_ARM && (_model == 0xd07 || _model2 == 0xd07)) _features |= CPU_STXR_PREFETCH;
 249   // If an olde style /proc/cpuinfo (cpu_lines == 1) then if _model is an A57 (0xd07)
 250   // we assume the worst and assume we could be on a big little system and have
 251   // undisclosed A53 cores which we could be swapped to at any stage
 252   if (_cpu == CPU_ARM && cpu_lines == 1 && _model == 0xd07) _features |= CPU_A53MAC;
 253 
 254   sprintf(buf, "0x%02x:0x%x:0x%03x:%d", _cpu, _variant, _model, _revision);
 255   if (_model2) sprintf(buf+strlen(buf), "(0x%03x)", _model2);
 256   if (auxv & HWCAP_ASIMD) strcat(buf, ", simd");
 257   if (auxv & HWCAP_CRC32) strcat(buf, ", crc");
 258   if (auxv & HWCAP_AES)   strcat(buf, ", aes");
 259   if (auxv & HWCAP_SHA1)  strcat(buf, ", sha1");
 260   if (auxv & HWCAP_SHA2)  strcat(buf, ", sha256");
 261   if (auxv & HWCAP_ATOMICS) strcat(buf, ", lse");
 262 
 263   _features_string = os::strdup(buf);
 264 
 265   if (FLAG_IS_DEFAULT(UseCRC32)) {
 266     UseCRC32 = (auxv & HWCAP_CRC32) != 0;
 267   }
 268 
 269   if (UseCRC32 && (auxv & HWCAP_CRC32) == 0) {
 270     warning("UseCRC32 specified, but not supported on this CPU");
 271     FLAG_SET_DEFAULT(UseCRC32, false);
 272   }
 273 
 274   if (FLAG_IS_DEFAULT(UseAdler32Intrinsics)) {
 275     FLAG_SET_DEFAULT(UseAdler32Intrinsics, true);
 276   }
 277 
 278   if (UseVectorizedMismatchIntrinsic) {
 279     warning("UseVectorizedMismatchIntrinsic specified, but not available on this CPU.");
 280     FLAG_SET_DEFAULT(UseVectorizedMismatchIntrinsic, false);
 281   }
 282 
 283   if (auxv & HWCAP_ATOMICS) {
 284     if (FLAG_IS_DEFAULT(UseLSE))
 285       FLAG_SET_DEFAULT(UseLSE, true);
 286   } else {
 287     if (UseLSE) {
 288       warning("UseLSE specified, but not supported on this CPU");
 289       FLAG_SET_DEFAULT(UseLSE, false);
 290     }
 291   }
 292 
 293   if (auxv & HWCAP_AES) {
 294     UseAES = UseAES || FLAG_IS_DEFAULT(UseAES);
 295     UseAESIntrinsics =
 296         UseAESIntrinsics || (UseAES && FLAG_IS_DEFAULT(UseAESIntrinsics));
 297     if (UseAESIntrinsics && !UseAES) {
 298       warning("UseAESIntrinsics enabled, but UseAES not, enabling");
 299       UseAES = true;
 300     }
 301   } else {
 302     if (UseAES) {
 303       warning("AES instructions are not available on this CPU");
 304       FLAG_SET_DEFAULT(UseAES, false);
 305     }
 306     if (UseAESIntrinsics) {
 307       warning("AES intrinsics are not available on this CPU");
 308       FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 309     }
 310   }
 311 
 312   if (UseAESCTRIntrinsics) {
 313     warning("AES/CTR intrinsics are not available on this CPU");
 314     FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 315   }
 316 
 317   if (FLAG_IS_DEFAULT(UseCRC32Intrinsics)) {
 318     UseCRC32Intrinsics = true;
 319   }
 320 
 321   if (auxv & HWCAP_CRC32) {
 322     if (FLAG_IS_DEFAULT(UseCRC32CIntrinsics)) {
 323       FLAG_SET_DEFAULT(UseCRC32CIntrinsics, true);
 324     }
 325   } else if (UseCRC32CIntrinsics) {
 326     warning("CRC32C is not available on the CPU");
 327     FLAG_SET_DEFAULT(UseCRC32CIntrinsics, false);
 328   }
 329 
 330   if (FLAG_IS_DEFAULT(UseFMA)) {
 331     FLAG_SET_DEFAULT(UseFMA, true);
 332   }
 333 
 334   if (auxv & (HWCAP_SHA1 | HWCAP_SHA2)) {
 335     if (FLAG_IS_DEFAULT(UseSHA)) {
 336       FLAG_SET_DEFAULT(UseSHA, true);
 337     }
 338   } else if (UseSHA) {
 339     warning("SHA instructions are not available on this CPU");
 340     FLAG_SET_DEFAULT(UseSHA, false);
 341   }
 342 
 343   if (UseSHA && (auxv & HWCAP_SHA1)) {
 344     if (FLAG_IS_DEFAULT(UseSHA1Intrinsics)) {
 345       FLAG_SET_DEFAULT(UseSHA1Intrinsics, true);
 346     }
 347   } else if (UseSHA1Intrinsics) {
 348     warning("Intrinsics for SHA-1 crypto hash functions not available on this CPU.");
 349     FLAG_SET_DEFAULT(UseSHA1Intrinsics, false);
 350   }
 351 
 352   if (UseSHA && (auxv & HWCAP_SHA2)) {
 353     if (FLAG_IS_DEFAULT(UseSHA256Intrinsics)) {
 354       FLAG_SET_DEFAULT(UseSHA256Intrinsics, true);
 355     }
 356   } else if (UseSHA256Intrinsics) {
 357     warning("Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.");
 358     FLAG_SET_DEFAULT(UseSHA256Intrinsics, false);
 359   }
 360 
 361   if (UseSHA512Intrinsics) {
 362     warning("Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.");
 363     FLAG_SET_DEFAULT(UseSHA512Intrinsics, false);
 364   }
 365 
 366   if (!(UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics)) {
 367     FLAG_SET_DEFAULT(UseSHA, false);
 368   }
 369 
 370   if (auxv & HWCAP_PMULL) {
 371     if (FLAG_IS_DEFAULT(UseGHASHIntrinsics)) {
 372       FLAG_SET_DEFAULT(UseGHASHIntrinsics, true);
 373     }
 374   } else if (UseGHASHIntrinsics) {
 375     warning("GHASH intrinsics are not available on this CPU");
 376     FLAG_SET_DEFAULT(UseGHASHIntrinsics, false);
 377   }
 378 
 379   if (is_zva_enabled()) {
 380     if (FLAG_IS_DEFAULT(UseBlockZeroing)) {
 381       FLAG_SET_DEFAULT(UseBlockZeroing, true);
 382     }
 383     if (FLAG_IS_DEFAULT(BlockZeroingLowLimit)) {
 384       FLAG_SET_DEFAULT(BlockZeroingLowLimit, 4 * VM_Version::zva_length());
 385     }
 386   } else if (UseBlockZeroing) {
 387     warning("DC ZVA is not available on this CPU");
 388     FLAG_SET_DEFAULT(UseBlockZeroing, false);
 389   }
 390 
 391   // This machine allows unaligned memory accesses
 392   if (FLAG_IS_DEFAULT(UseUnalignedAccesses)) {
 393     FLAG_SET_DEFAULT(UseUnalignedAccesses, true);
 394   }
 395 
 396   if (FLAG_IS_DEFAULT(UseBarriersForVolatile)) {
 397     UseBarriersForVolatile = (_features & CPU_DMB_ATOMICS) != 0;
 398   }
 399 
 400   if (FLAG_IS_DEFAULT(UsePopCountInstruction)) {
 401     UsePopCountInstruction = true;
 402   }
 403 
 404 #ifdef COMPILER2
 405   if (FLAG_IS_DEFAULT(UseMultiplyToLenIntrinsic)) {
 406     UseMultiplyToLenIntrinsic = true;
 407   }
 408 
 409   if (FLAG_IS_DEFAULT(UseSquareToLenIntrinsic)) {
 410     UseSquareToLenIntrinsic = true;
 411   }
 412 
 413   if (FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {
 414     UseMulAddIntrinsic = true;
 415   }
 416 
 417   if (FLAG_IS_DEFAULT(UseMontgomeryMultiplyIntrinsic)) {
 418     UseMontgomeryMultiplyIntrinsic = true;
 419   }
 420   if (FLAG_IS_DEFAULT(UseMontgomerySquareIntrinsic)) {
 421     UseMontgomerySquareIntrinsic = true;
 422   }
 423 
 424   if (FLAG_IS_DEFAULT(OptoScheduling)) {
 425     OptoScheduling = true;
 426   }
 427 #endif
 428 }
 429 
 430 void VM_Version::initialize() {
 431   ResourceMark rm;
 432 
 433   stub_blob = BufferBlob::create("getPsrInfo_stub", stub_size);
 434   if (stub_blob == NULL) {
 435     vm_exit_during_initialization("Unable to allocate getPsrInfo_stub");
 436   }
 437 
 438   CodeBuffer c(stub_blob);
 439   VM_Version_StubGenerator g(&c);
 440   getPsrInfo_stub = CAST_TO_FN_PTR(getPsrInfo_stub_t,
 441                                    g.generate_getPsrInfo());
 442 
 443   get_processor_features();
 444 
 445   UNSUPPORTED_OPTION(CriticalJNINatives);
 446 }