1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "logging/log.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "runtime/java.hpp"
  31 #include "runtime/os.hpp"
  32 #include "runtime/stubCodeGenerator.hpp"
  33 #include "vm_version_x86.hpp"
  34 
  35 
  36 int VM_Version::_cpu;
  37 int VM_Version::_model;
  38 int VM_Version::_stepping;
  39 VM_Version::CpuidInfo VM_Version::_cpuid_info = { 0, };
  40 
  41 // Address of instruction which causes SEGV
  42 address VM_Version::_cpuinfo_segv_addr = 0;
  43 // Address of instruction after the one which causes SEGV
  44 address VM_Version::_cpuinfo_cont_addr = 0;
  45 
  46 static BufferBlob* stub_blob;
  47 static const int stub_size = 1000;
  48 
  49 extern "C" {
  50   typedef void (*get_cpu_info_stub_t)(void*);
  51 }
  52 static get_cpu_info_stub_t get_cpu_info_stub = NULL;
  53 
  54 
  55 class VM_Version_StubGenerator: public StubCodeGenerator {
  56  public:
  57 
  58   VM_Version_StubGenerator(CodeBuffer *c) : StubCodeGenerator(c) {}
  59 
  60   address generate_get_cpu_info() {
  61     // Flags to test CPU type.
  62     const uint32_t HS_EFL_AC = 0x40000;
  63     const uint32_t HS_EFL_ID = 0x200000;
  64     // Values for when we don't have a CPUID instruction.
  65     const int      CPU_FAMILY_SHIFT = 8;
  66     const uint32_t CPU_FAMILY_386 = (3 << CPU_FAMILY_SHIFT);
  67     const uint32_t CPU_FAMILY_486 = (4 << CPU_FAMILY_SHIFT);
  68 
  69     Label detect_486, cpu486, detect_586, std_cpuid1, std_cpuid4;
  70     Label sef_cpuid, ext_cpuid, ext_cpuid1, ext_cpuid5, ext_cpuid7, done, wrapup;
  71     Label legacy_setup, save_restore_except, legacy_save_restore, start_simd_check;
  72 
  73     StubCodeMark mark(this, "VM_Version", "get_cpu_info_stub");
  74 #   define __ _masm->
  75 
  76     address start = __ pc();
  77 
  78     //
  79     // void get_cpu_info(VM_Version::CpuidInfo* cpuid_info);
  80     //
  81     // LP64: rcx and rdx are first and second argument registers on windows
  82 
  83     __ push(rbp);
  84 #ifdef _LP64
  85     __ mov(rbp, c_rarg0); // cpuid_info address
  86 #else
  87     __ movptr(rbp, Address(rsp, 8)); // cpuid_info address
  88 #endif
  89     __ push(rbx);
  90     __ push(rsi);
  91     __ pushf();          // preserve rbx, and flags
  92     __ pop(rax);
  93     __ push(rax);
  94     __ mov(rcx, rax);
  95     //
  96     // if we are unable to change the AC flag, we have a 386
  97     //
  98     __ xorl(rax, HS_EFL_AC);
  99     __ push(rax);
 100     __ popf();
 101     __ pushf();
 102     __ pop(rax);
 103     __ cmpptr(rax, rcx);
 104     __ jccb(Assembler::notEqual, detect_486);
 105 
 106     __ movl(rax, CPU_FAMILY_386);
 107     __ movl(Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())), rax);
 108     __ jmp(done);
 109 
 110     //
 111     // If we are unable to change the ID flag, we have a 486 which does
 112     // not support the "cpuid" instruction.
 113     //
 114     __ bind(detect_486);
 115     __ mov(rax, rcx);
 116     __ xorl(rax, HS_EFL_ID);
 117     __ push(rax);
 118     __ popf();
 119     __ pushf();
 120     __ pop(rax);
 121     __ cmpptr(rcx, rax);
 122     __ jccb(Assembler::notEqual, detect_586);
 123 
 124     __ bind(cpu486);
 125     __ movl(rax, CPU_FAMILY_486);
 126     __ movl(Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())), rax);
 127     __ jmp(done);
 128 
 129     //
 130     // At this point, we have a chip which supports the "cpuid" instruction
 131     //
 132     __ bind(detect_586);
 133     __ xorl(rax, rax);
 134     __ cpuid();
 135     __ orl(rax, rax);
 136     __ jcc(Assembler::equal, cpu486);   // if cpuid doesn't support an input
 137                                         // value of at least 1, we give up and
 138                                         // assume a 486
 139     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset())));
 140     __ movl(Address(rsi, 0), rax);
 141     __ movl(Address(rsi, 4), rbx);
 142     __ movl(Address(rsi, 8), rcx);
 143     __ movl(Address(rsi,12), rdx);
 144 
 145     __ cmpl(rax, 0xa);                  // Is cpuid(0xB) supported?
 146     __ jccb(Assembler::belowEqual, std_cpuid4);
 147 
 148     //
 149     // cpuid(0xB) Processor Topology
 150     //
 151     __ movl(rax, 0xb);
 152     __ xorl(rcx, rcx);   // Threads level
 153     __ cpuid();
 154 
 155     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB0_offset())));
 156     __ movl(Address(rsi, 0), rax);
 157     __ movl(Address(rsi, 4), rbx);
 158     __ movl(Address(rsi, 8), rcx);
 159     __ movl(Address(rsi,12), rdx);
 160 
 161     __ movl(rax, 0xb);
 162     __ movl(rcx, 1);     // Cores level
 163     __ cpuid();
 164     __ push(rax);
 165     __ andl(rax, 0x1f);  // Determine if valid topology level
 166     __ orl(rax, rbx);    // eax[4:0] | ebx[0:15] == 0 indicates invalid level
 167     __ andl(rax, 0xffff);
 168     __ pop(rax);
 169     __ jccb(Assembler::equal, std_cpuid4);
 170 
 171     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB1_offset())));
 172     __ movl(Address(rsi, 0), rax);
 173     __ movl(Address(rsi, 4), rbx);
 174     __ movl(Address(rsi, 8), rcx);
 175     __ movl(Address(rsi,12), rdx);
 176 
 177     __ movl(rax, 0xb);
 178     __ movl(rcx, 2);     // Packages level
 179     __ cpuid();
 180     __ push(rax);
 181     __ andl(rax, 0x1f);  // Determine if valid topology level
 182     __ orl(rax, rbx);    // eax[4:0] | ebx[0:15] == 0 indicates invalid level
 183     __ andl(rax, 0xffff);
 184     __ pop(rax);
 185     __ jccb(Assembler::equal, std_cpuid4);
 186 
 187     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB2_offset())));
 188     __ movl(Address(rsi, 0), rax);
 189     __ movl(Address(rsi, 4), rbx);
 190     __ movl(Address(rsi, 8), rcx);
 191     __ movl(Address(rsi,12), rdx);
 192 
 193     //
 194     // cpuid(0x4) Deterministic cache params
 195     //
 196     __ bind(std_cpuid4);
 197     __ movl(rax, 4);
 198     __ cmpl(rax, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset()))); // Is cpuid(0x4) supported?
 199     __ jccb(Assembler::greater, std_cpuid1);
 200 
 201     __ xorl(rcx, rcx);   // L1 cache
 202     __ cpuid();
 203     __ push(rax);
 204     __ andl(rax, 0x1f);  // Determine if valid cache parameters used
 205     __ orl(rax, rax);    // eax[4:0] == 0 indicates invalid cache
 206     __ pop(rax);
 207     __ jccb(Assembler::equal, std_cpuid1);
 208 
 209     __ lea(rsi, Address(rbp, in_bytes(VM_Version::dcp_cpuid4_offset())));
 210     __ movl(Address(rsi, 0), rax);
 211     __ movl(Address(rsi, 4), rbx);
 212     __ movl(Address(rsi, 8), rcx);
 213     __ movl(Address(rsi,12), rdx);
 214 
 215     //
 216     // Standard cpuid(0x1)
 217     //
 218     __ bind(std_cpuid1);
 219     __ movl(rax, 1);
 220     __ cpuid();
 221     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
 222     __ movl(Address(rsi, 0), rax);
 223     __ movl(Address(rsi, 4), rbx);
 224     __ movl(Address(rsi, 8), rcx);
 225     __ movl(Address(rsi,12), rdx);
 226 
 227     //
 228     // Check if OS has enabled XGETBV instruction to access XCR0
 229     // (OSXSAVE feature flag) and CPU supports AVX
 230     //
 231     __ andl(rcx, 0x18000000); // cpuid1 bits osxsave | avx
 232     __ cmpl(rcx, 0x18000000);
 233     __ jccb(Assembler::notEqual, sef_cpuid); // jump if AVX is not supported
 234 
 235     //
 236     // XCR0, XFEATURE_ENABLED_MASK register
 237     //
 238     __ xorl(rcx, rcx);   // zero for XCR0 register
 239     __ xgetbv();
 240     __ lea(rsi, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset())));
 241     __ movl(Address(rsi, 0), rax);
 242     __ movl(Address(rsi, 4), rdx);
 243 
 244     //
 245     // cpuid(0x7) Structured Extended Features
 246     //
 247     __ bind(sef_cpuid);
 248     __ movl(rax, 7);
 249     __ cmpl(rax, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset()))); // Is cpuid(0x7) supported?
 250     __ jccb(Assembler::greater, ext_cpuid);
 251 
 252     __ xorl(rcx, rcx);
 253     __ cpuid();
 254     __ lea(rsi, Address(rbp, in_bytes(VM_Version::sef_cpuid7_offset())));
 255     __ movl(Address(rsi, 0), rax);
 256     __ movl(Address(rsi, 4), rbx);
 257 
 258     //
 259     // Extended cpuid(0x80000000)
 260     //
 261     __ bind(ext_cpuid);
 262     __ movl(rax, 0x80000000);
 263     __ cpuid();
 264     __ cmpl(rax, 0x80000000);     // Is cpuid(0x80000001) supported?
 265     __ jcc(Assembler::belowEqual, done);
 266     __ cmpl(rax, 0x80000004);     // Is cpuid(0x80000005) supported?
 267     __ jccb(Assembler::belowEqual, ext_cpuid1);
 268     __ cmpl(rax, 0x80000006);     // Is cpuid(0x80000007) supported?
 269     __ jccb(Assembler::belowEqual, ext_cpuid5);
 270     __ cmpl(rax, 0x80000007);     // Is cpuid(0x80000008) supported?
 271     __ jccb(Assembler::belowEqual, ext_cpuid7);
 272     //
 273     // Extended cpuid(0x80000008)
 274     //
 275     __ movl(rax, 0x80000008);
 276     __ cpuid();
 277     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid8_offset())));
 278     __ movl(Address(rsi, 0), rax);
 279     __ movl(Address(rsi, 4), rbx);
 280     __ movl(Address(rsi, 8), rcx);
 281     __ movl(Address(rsi,12), rdx);
 282 
 283     //
 284     // Extended cpuid(0x80000007)
 285     //
 286     __ bind(ext_cpuid7);
 287     __ movl(rax, 0x80000007);
 288     __ cpuid();
 289     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid7_offset())));
 290     __ movl(Address(rsi, 0), rax);
 291     __ movl(Address(rsi, 4), rbx);
 292     __ movl(Address(rsi, 8), rcx);
 293     __ movl(Address(rsi,12), rdx);
 294 
 295     //
 296     // Extended cpuid(0x80000005)
 297     //
 298     __ bind(ext_cpuid5);
 299     __ movl(rax, 0x80000005);
 300     __ cpuid();
 301     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid5_offset())));
 302     __ movl(Address(rsi, 0), rax);
 303     __ movl(Address(rsi, 4), rbx);
 304     __ movl(Address(rsi, 8), rcx);
 305     __ movl(Address(rsi,12), rdx);
 306 
 307     //
 308     // Extended cpuid(0x80000001)
 309     //
 310     __ bind(ext_cpuid1);
 311     __ movl(rax, 0x80000001);
 312     __ cpuid();
 313     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid1_offset())));
 314     __ movl(Address(rsi, 0), rax);
 315     __ movl(Address(rsi, 4), rbx);
 316     __ movl(Address(rsi, 8), rcx);
 317     __ movl(Address(rsi,12), rdx);
 318 
 319     //
 320     // Check if OS has enabled XGETBV instruction to access XCR0
 321     // (OSXSAVE feature flag) and CPU supports AVX
 322     //
 323     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
 324     __ movl(rcx, 0x18000000); // cpuid1 bits osxsave | avx
 325     __ andl(rcx, Address(rsi, 8)); // cpuid1 bits osxsave | avx
 326     __ cmpl(rcx, 0x18000000);
 327     __ jccb(Assembler::notEqual, done); // jump if AVX is not supported
 328 
 329     __ movl(rax, 0x6);
 330     __ andl(rax, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset()))); // xcr0 bits sse | ymm
 331     __ cmpl(rax, 0x6);
 332     __ jccb(Assembler::equal, start_simd_check); // return if AVX is not supported
 333 
 334     // we need to bridge farther than imm8, so we use this island as a thunk
 335     __ bind(done);
 336     __ jmp(wrapup);
 337 
 338     __ bind(start_simd_check);
 339     //
 340     // Some OSs have a bug when upper 128/256bits of YMM/ZMM
 341     // registers are not restored after a signal processing.
 342     // Generate SEGV here (reference through NULL)
 343     // and check upper YMM/ZMM bits after it.
 344     //
 345     intx saved_useavx = UseAVX;
 346     intx saved_usesse = UseSSE;
 347     // check _cpuid_info.sef_cpuid7_ebx.bits.avx512f
 348     __ lea(rsi, Address(rbp, in_bytes(VM_Version::sef_cpuid7_offset())));
 349     __ movl(rax, 0x10000);
 350     __ andl(rax, Address(rsi, 4)); // xcr0 bits sse | ymm
 351     __ cmpl(rax, 0x10000);
 352     __ jccb(Assembler::notEqual, legacy_setup); // jump if EVEX is not supported
 353     // check _cpuid_info.xem_xcr0_eax.bits.opmask
 354     // check _cpuid_info.xem_xcr0_eax.bits.zmm512
 355     // check _cpuid_info.xem_xcr0_eax.bits.zmm32
 356     __ movl(rax, 0xE0);
 357     __ andl(rax, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset()))); // xcr0 bits sse | ymm
 358     __ cmpl(rax, 0xE0);
 359     __ jccb(Assembler::notEqual, legacy_setup); // jump if EVEX is not supported
 360 
 361     // EVEX setup: run in lowest evex mode
 362     VM_Version::set_evex_cpuFeatures(); // Enable temporary to pass asserts
 363     UseAVX = 3;
 364     UseSSE = 2;
 365     // load value into all 64 bytes of zmm7 register
 366     __ movl(rcx, VM_Version::ymm_test_value());
 367     __ movdl(xmm0, rcx);
 368     __ movl(rcx, 0xffff);
 369     __ kmovwl(k1, rcx);
 370     __ evpbroadcastd(xmm0, xmm0, Assembler::AVX_512bit);
 371     __ evmovdqul(xmm7, xmm0, Assembler::AVX_512bit);
 372 #ifdef _LP64
 373     __ evmovdqul(xmm8, xmm0, Assembler::AVX_512bit);
 374     __ evmovdqul(xmm31, xmm0, Assembler::AVX_512bit);
 375 #endif
 376     VM_Version::clean_cpuFeatures();
 377     __ jmp(save_restore_except);
 378 
 379     __ bind(legacy_setup);
 380     // AVX setup
 381     VM_Version::set_avx_cpuFeatures(); // Enable temporary to pass asserts
 382     UseAVX = 1;
 383     UseSSE = 2;
 384     // load value into all 32 bytes of ymm7 register
 385     __ movl(rcx, VM_Version::ymm_test_value());
 386 
 387     __ movdl(xmm0, rcx);
 388     __ pshufd(xmm0, xmm0, 0x00);
 389     __ vinsertf128_high(xmm0, xmm0);
 390     __ vmovdqu(xmm7, xmm0);
 391 #ifdef _LP64
 392     __ vmovdqu(xmm8, xmm0);
 393     __ vmovdqu(xmm15, xmm0);
 394 #endif
 395     VM_Version::clean_cpuFeatures();
 396 
 397     __ bind(save_restore_except);
 398     __ xorl(rsi, rsi);
 399     VM_Version::set_cpuinfo_segv_addr(__ pc());
 400     // Generate SEGV
 401     __ movl(rax, Address(rsi, 0));
 402 
 403     VM_Version::set_cpuinfo_cont_addr(__ pc());
 404     // Returns here after signal. Save xmm0 to check it later.
 405 
 406     // check _cpuid_info.sef_cpuid7_ebx.bits.avx512f
 407     __ lea(rsi, Address(rbp, in_bytes(VM_Version::sef_cpuid7_offset())));
 408     __ movl(rax, 0x10000);
 409     __ andl(rax, Address(rsi, 4));
 410     __ cmpl(rax, 0x10000);
 411     __ jccb(Assembler::notEqual, legacy_save_restore);
 412     // check _cpuid_info.xem_xcr0_eax.bits.opmask
 413     // check _cpuid_info.xem_xcr0_eax.bits.zmm512
 414     // check _cpuid_info.xem_xcr0_eax.bits.zmm32
 415     __ movl(rax, 0xE0);
 416     __ andl(rax, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset()))); // xcr0 bits sse | ymm
 417     __ cmpl(rax, 0xE0);
 418     __ jccb(Assembler::notEqual, legacy_save_restore);
 419 
 420     // EVEX check: run in lowest evex mode
 421     VM_Version::set_evex_cpuFeatures(); // Enable temporary to pass asserts
 422     UseAVX = 3;
 423     UseSSE = 2;
 424     __ lea(rsi, Address(rbp, in_bytes(VM_Version::zmm_save_offset())));
 425     __ evmovdqul(Address(rsi, 0), xmm0, Assembler::AVX_512bit);
 426     __ evmovdqul(Address(rsi, 64), xmm7, Assembler::AVX_512bit);
 427 #ifdef _LP64
 428     __ evmovdqul(Address(rsi, 128), xmm8, Assembler::AVX_512bit);
 429     __ evmovdqul(Address(rsi, 192), xmm31, Assembler::AVX_512bit);
 430 #endif
 431     VM_Version::clean_cpuFeatures();
 432     UseAVX = saved_useavx;
 433     UseSSE = saved_usesse;
 434     __ jmp(wrapup);
 435 
 436     __ bind(legacy_save_restore);
 437     // AVX check
 438     VM_Version::set_avx_cpuFeatures(); // Enable temporary to pass asserts
 439     UseAVX = 1;
 440     UseSSE = 2;
 441     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ymm_save_offset())));
 442     __ vmovdqu(Address(rsi, 0), xmm0);
 443     __ vmovdqu(Address(rsi, 32), xmm7);
 444 #ifdef _LP64
 445     __ vmovdqu(Address(rsi, 64), xmm8);
 446     __ vmovdqu(Address(rsi, 96), xmm15);
 447 #endif
 448     VM_Version::clean_cpuFeatures();
 449     UseAVX = saved_useavx;
 450     UseSSE = saved_usesse;
 451 
 452     __ bind(wrapup);
 453     __ popf();
 454     __ pop(rsi);
 455     __ pop(rbx);
 456     __ pop(rbp);
 457     __ ret(0);
 458 
 459 #   undef __
 460 
 461     return start;
 462   };
 463 };
 464 
 465 void VM_Version::get_processor_features() {
 466 
 467   _cpu = 4; // 486 by default
 468   _model = 0;
 469   _stepping = 0;
 470   _features = 0;
 471   _logical_processors_per_package = 1;
 472   // i486 internal cache is both I&D and has a 16-byte line size
 473   _L1_data_cache_line_size = 16;
 474 
 475   // Get raw processor info
 476 
 477   get_cpu_info_stub(&_cpuid_info);
 478 
 479   assert_is_initialized();
 480   _cpu = extended_cpu_family();
 481   _model = extended_cpu_model();
 482   _stepping = cpu_stepping();
 483 
 484   if (cpu_family() > 4) { // it supports CPUID
 485     _features = feature_flags();
 486     // Logical processors are only available on P4s and above,
 487     // and only if hyperthreading is available.
 488     _logical_processors_per_package = logical_processor_count();
 489     _L1_data_cache_line_size = L1_line_size();
 490   }
 491 
 492   _supports_cx8 = supports_cmpxchg8();
 493   // xchg and xadd instructions
 494   _supports_atomic_getset4 = true;
 495   _supports_atomic_getadd4 = true;
 496   LP64_ONLY(_supports_atomic_getset8 = true);
 497   LP64_ONLY(_supports_atomic_getadd8 = true);
 498 
 499 #ifdef _LP64
 500   // OS should support SSE for x64 and hardware should support at least SSE2.
 501   if (!VM_Version::supports_sse2()) {
 502     vm_exit_during_initialization("Unknown x64 processor: SSE2 not supported");
 503   }
 504   // in 64 bit the use of SSE2 is the minimum
 505   if (UseSSE < 2) UseSSE = 2;
 506 #endif
 507 
 508 #ifdef AMD64
 509   // flush_icache_stub have to be generated first.
 510   // That is why Icache line size is hard coded in ICache class,
 511   // see icache_x86.hpp. It is also the reason why we can't use
 512   // clflush instruction in 32-bit VM since it could be running
 513   // on CPU which does not support it.
 514   //
 515   // The only thing we can do is to verify that flushed
 516   // ICache::line_size has correct value.
 517   guarantee(_cpuid_info.std_cpuid1_edx.bits.clflush != 0, "clflush is not supported");
 518   // clflush_size is size in quadwords (8 bytes).
 519   guarantee(_cpuid_info.std_cpuid1_ebx.bits.clflush_size == 8, "such clflush size is not supported");
 520 #endif
 521 
 522   // If the OS doesn't support SSE, we can't use this feature even if the HW does
 523   if (!os::supports_sse())
 524     _features &= ~(CPU_SSE|CPU_SSE2|CPU_SSE3|CPU_SSSE3|CPU_SSE4A|CPU_SSE4_1|CPU_SSE4_2);
 525 
 526   if (UseSSE < 4) {
 527     _features &= ~CPU_SSE4_1;
 528     _features &= ~CPU_SSE4_2;
 529   }
 530 
 531   if (UseSSE < 3) {
 532     _features &= ~CPU_SSE3;
 533     _features &= ~CPU_SSSE3;
 534     _features &= ~CPU_SSE4A;
 535   }
 536 
 537   if (UseSSE < 2)
 538     _features &= ~CPU_SSE2;
 539 
 540   if (UseSSE < 1)
 541     _features &= ~CPU_SSE;
 542 
 543   // first try initial setting and detect what we can support
 544   if (UseAVX > 0) {
 545     if (UseAVX > 2 && supports_evex()) {
 546       UseAVX = 3;
 547     } else if (UseAVX > 1 && supports_avx2()) {
 548       UseAVX = 2;
 549     } else if (UseAVX > 0 && supports_avx()) {
 550       UseAVX = 1;
 551     } else {
 552       UseAVX = 0;
 553     }
 554   } else if (UseAVX < 0) {
 555     UseAVX = 0;
 556   }
 557 
 558   if (UseAVX < 3) {
 559     _features &= ~CPU_AVX512F;
 560     _features &= ~CPU_AVX512DQ;
 561     _features &= ~CPU_AVX512CD;
 562     _features &= ~CPU_AVX512BW;
 563     _features &= ~CPU_AVX512VL;
 564   }
 565 
 566   if (UseAVX < 2)
 567     _features &= ~CPU_AVX2;
 568 
 569   if (UseAVX < 1)
 570     _features &= ~CPU_AVX;
 571 
 572   if (!UseAES && !FLAG_IS_DEFAULT(UseAES))
 573     _features &= ~CPU_AES;
 574 
 575   if (logical_processors_per_package() == 1) {
 576     // HT processor could be installed on a system which doesn't support HT.
 577     _features &= ~CPU_HT;
 578   }
 579 
 580   char buf[256];
 581   jio_snprintf(buf, sizeof(buf), "(%u cores per cpu, %u threads per core) family %d model %d stepping %d%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
 582                cores_per_cpu(), threads_per_core(),
 583                cpu_family(), _model, _stepping,
 584                (supports_cmov() ? ", cmov" : ""),
 585                (supports_cmpxchg8() ? ", cx8" : ""),
 586                (supports_fxsr() ? ", fxsr" : ""),
 587                (supports_mmx()  ? ", mmx"  : ""),
 588                (supports_sse()  ? ", sse"  : ""),
 589                (supports_sse2() ? ", sse2" : ""),
 590                (supports_sse3() ? ", sse3" : ""),
 591                (supports_ssse3()? ", ssse3": ""),
 592                (supports_sse4_1() ? ", sse4.1" : ""),
 593                (supports_sse4_2() ? ", sse4.2" : ""),
 594                (supports_popcnt() ? ", popcnt" : ""),
 595                (supports_avx()    ? ", avx" : ""),
 596                (supports_avx2()   ? ", avx2" : ""),
 597                (supports_aes()    ? ", aes" : ""),
 598                (supports_clmul()  ? ", clmul" : ""),
 599                (supports_erms()   ? ", erms" : ""),
 600                (supports_rtm()    ? ", rtm" : ""),
 601                (supports_mmx_ext() ? ", mmxext" : ""),
 602                (supports_3dnow_prefetch() ? ", 3dnowpref" : ""),
 603                (supports_lzcnt()   ? ", lzcnt": ""),
 604                (supports_sse4a()   ? ", sse4a": ""),
 605                (supports_ht() ? ", ht": ""),
 606                (supports_tsc() ? ", tsc": ""),
 607                (supports_tscinv_bit() ? ", tscinvbit": ""),
 608                (supports_tscinv() ? ", tscinv": ""),
 609                (supports_bmi1() ? ", bmi1" : ""),
 610                (supports_bmi2() ? ", bmi2" : ""),
 611                (supports_adx() ? ", adx" : ""),
 612                (supports_evex() ? ", evex" : ""),
 613                (supports_sha() ? ", sha" : ""),
 614                (supports_fma() ? ", fma" : ""));
 615   _features_string = os::strdup(buf);
 616 
 617   // UseSSE is set to the smaller of what hardware supports and what
 618   // the command line requires.  I.e., you cannot set UseSSE to 2 on
 619   // older Pentiums which do not support it.
 620   if (UseSSE > 4) UseSSE=4;
 621   if (UseSSE < 0) UseSSE=0;
 622   if (!supports_sse4_1()) // Drop to 3 if no SSE4 support
 623     UseSSE = MIN2((intx)3,UseSSE);
 624   if (!supports_sse3()) // Drop to 2 if no SSE3 support
 625     UseSSE = MIN2((intx)2,UseSSE);
 626   if (!supports_sse2()) // Drop to 1 if no SSE2 support
 627     UseSSE = MIN2((intx)1,UseSSE);
 628   if (!supports_sse ()) // Drop to 0 if no SSE  support
 629     UseSSE = 0;
 630 
 631   // Use AES instructions if available.
 632   if (supports_aes()) {
 633     if (FLAG_IS_DEFAULT(UseAES)) {
 634       FLAG_SET_DEFAULT(UseAES, true);
 635     }
 636     if (!UseAES) {
 637       if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 638         warning("AES intrinsics require UseAES flag to be enabled. Intrinsics will be disabled.");
 639       }
 640       FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 641     } else {
 642       if (UseSSE > 2) {
 643         if (FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 644           FLAG_SET_DEFAULT(UseAESIntrinsics, true);
 645         }
 646       } else {
 647         // The AES intrinsic stubs require AES instruction support (of course)
 648         // but also require sse3 mode or higher for instructions it use.
 649         if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 650           warning("X86 AES intrinsics require SSE3 instructions or higher. Intrinsics will be disabled.");
 651         }
 652         FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 653       }
 654 
 655       // --AES-CTR begins--
 656       if (!UseAESIntrinsics) {
 657         if (UseAESCTRIntrinsics && !FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 658           warning("AES-CTR intrinsics require UseAESIntrinsics flag to be enabled. Intrinsics will be disabled.");
 659           FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 660         }
 661       } else {
 662         if(supports_sse4_1()) {
 663           if (FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 664             FLAG_SET_DEFAULT(UseAESCTRIntrinsics, true);
 665           }
 666         } else {
 667            // The AES-CTR intrinsic stubs require AES instruction support (of course)
 668            // but also require sse4.1 mode or higher for instructions it use.
 669           if (UseAESCTRIntrinsics && !FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 670              warning("X86 AES-CTR intrinsics require SSE4.1 instructions or higher. Intrinsics will be disabled.");
 671            }
 672            FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 673         }
 674       }
 675       // --AES-CTR ends--
 676     }
 677   } else if (UseAES || UseAESIntrinsics || UseAESCTRIntrinsics) {
 678     if (UseAES && !FLAG_IS_DEFAULT(UseAES)) {
 679       warning("AES instructions are not available on this CPU");
 680       FLAG_SET_DEFAULT(UseAES, false);
 681     }
 682     if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 683       warning("AES intrinsics are not available on this CPU");
 684       FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 685     }
 686     if (UseAESCTRIntrinsics && !FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 687       warning("AES-CTR intrinsics are not available on this CPU");
 688       FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 689     }
 690   }
 691 
 692   // Use CLMUL instructions if available.
 693   if (supports_clmul()) {
 694     if (FLAG_IS_DEFAULT(UseCLMUL)) {
 695       UseCLMUL = true;
 696     }
 697   } else if (UseCLMUL) {
 698     if (!FLAG_IS_DEFAULT(UseCLMUL))
 699       warning("CLMUL instructions not available on this CPU (AVX may also be required)");
 700     FLAG_SET_DEFAULT(UseCLMUL, false);
 701   }
 702 
 703   if (UseCLMUL && (UseSSE > 2)) {
 704     if (FLAG_IS_DEFAULT(UseCRC32Intrinsics)) {
 705       UseCRC32Intrinsics = true;
 706     }
 707   } else if (UseCRC32Intrinsics) {
 708     if (!FLAG_IS_DEFAULT(UseCRC32Intrinsics))
 709       warning("CRC32 Intrinsics requires CLMUL instructions (not available on this CPU)");
 710     FLAG_SET_DEFAULT(UseCRC32Intrinsics, false);
 711   }
 712 
 713   if (supports_sse4_2()) {
 714     if (FLAG_IS_DEFAULT(UseCRC32CIntrinsics)) {
 715       UseCRC32CIntrinsics = true;
 716     }
 717   }
 718   else if (UseCRC32CIntrinsics) {
 719     if (!FLAG_IS_DEFAULT(UseCRC32CIntrinsics)) {
 720       warning("CRC32C intrinsics are not available on this CPU");
 721     }
 722     FLAG_SET_DEFAULT(UseCRC32CIntrinsics, false);
 723   }
 724 
 725   // GHASH/GCM intrinsics
 726   if (UseCLMUL && (UseSSE > 2)) {
 727     if (FLAG_IS_DEFAULT(UseGHASHIntrinsics)) {
 728       UseGHASHIntrinsics = true;
 729     }
 730   } else if (UseGHASHIntrinsics) {
 731     if (!FLAG_IS_DEFAULT(UseGHASHIntrinsics))
 732       warning("GHASH intrinsic requires CLMUL and SSE2 instructions on this CPU");
 733     FLAG_SET_DEFAULT(UseGHASHIntrinsics, false);
 734   }
 735 
 736   if (supports_sha() LP64_ONLY(|| supports_avx2() && supports_bmi2())) {
 737     if (FLAG_IS_DEFAULT(UseSHA)) {
 738       UseSHA = true;
 739     }
 740   } else if (UseSHA) {
 741     warning("SHA instructions are not available on this CPU");
 742     FLAG_SET_DEFAULT(UseSHA, false);
 743   }
 744 
 745   if (supports_sha() && UseSHA) {
 746     if (FLAG_IS_DEFAULT(UseSHA1Intrinsics)) {
 747       FLAG_SET_DEFAULT(UseSHA1Intrinsics, true);
 748     }
 749   } else if (UseSHA1Intrinsics) {
 750     warning("Intrinsics for SHA-1 crypto hash functions not available on this CPU.");
 751     FLAG_SET_DEFAULT(UseSHA1Intrinsics, false);
 752   }
 753 
 754   if (UseSHA) {
 755     if (FLAG_IS_DEFAULT(UseSHA256Intrinsics)) {
 756       FLAG_SET_DEFAULT(UseSHA256Intrinsics, true);
 757     }
 758   } else if (UseSHA256Intrinsics) {
 759     warning("Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.");
 760     FLAG_SET_DEFAULT(UseSHA256Intrinsics, false);
 761   }
 762 
 763   if (UseSHA512Intrinsics) {
 764     warning("Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.");
 765     FLAG_SET_DEFAULT(UseSHA512Intrinsics, false);
 766   }
 767 
 768   if (!(UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics)) {
 769     FLAG_SET_DEFAULT(UseSHA, false);
 770   }
 771 
 772   if (UseAdler32Intrinsics) {
 773     warning("Adler32Intrinsics not available on this CPU.");
 774     FLAG_SET_DEFAULT(UseAdler32Intrinsics, false);
 775   }
 776 
 777   if (supports_fma()) {
 778     if (FLAG_IS_DEFAULT(UseFMA)) {
 779       UseFMA = true;
 780     }
 781   }
 782   else if (UseFMA) {
 783     if (!FLAG_IS_DEFAULT(UseFMA))
 784       warning("FMA instructions not available on this CPU");
 785     FLAG_SET_DEFAULT(UseFMA, false);
 786   }
 787 
 788 
 789   // Adjust RTM (Restricted Transactional Memory) flags
 790   if (!supports_rtm() && UseRTMLocking) {
 791     // Can't continue because UseRTMLocking affects UseBiasedLocking flag
 792     // setting during arguments processing. See use_biased_locking().
 793     // VM_Version_init() is executed after UseBiasedLocking is used
 794     // in Thread::allocate().
 795     vm_exit_during_initialization("RTM instructions are not available on this CPU");
 796   }
 797 
 798 #if INCLUDE_RTM_OPT
 799   if (UseRTMLocking) {
 800     if (is_intel_family_core()) {
 801       if ((_model == CPU_MODEL_HASWELL_E3) ||
 802           (_model == CPU_MODEL_HASWELL_E7 && _stepping < 3) ||
 803           (_model == CPU_MODEL_BROADWELL  && _stepping < 4)) {
 804         // currently a collision between SKL and HSW_E3
 805         if (!UnlockExperimentalVMOptions && UseAVX < 3) {
 806           vm_exit_during_initialization("UseRTMLocking is only available as experimental option on this platform. It must be enabled via -XX:+UnlockExperimentalVMOptions flag.");
 807         } else {
 808           warning("UseRTMLocking is only available as experimental option on this platform.");
 809         }
 810       }
 811     }
 812     if (!FLAG_IS_CMDLINE(UseRTMLocking)) {
 813       // RTM locking should be used only for applications with
 814       // high lock contention. For now we do not use it by default.
 815       vm_exit_during_initialization("UseRTMLocking flag should be only set on command line");
 816     }
 817     if (!is_power_of_2(RTMTotalCountIncrRate)) {
 818       warning("RTMTotalCountIncrRate must be a power of 2, resetting it to 64");
 819       FLAG_SET_DEFAULT(RTMTotalCountIncrRate, 64);
 820     }
 821     if (RTMAbortRatio < 0 || RTMAbortRatio > 100) {
 822       warning("RTMAbortRatio must be in the range 0 to 100, resetting it to 50");
 823       FLAG_SET_DEFAULT(RTMAbortRatio, 50);
 824     }
 825   } else { // !UseRTMLocking
 826     if (UseRTMForStackLocks) {
 827       if (!FLAG_IS_DEFAULT(UseRTMForStackLocks)) {
 828         warning("UseRTMForStackLocks flag should be off when UseRTMLocking flag is off");
 829       }
 830       FLAG_SET_DEFAULT(UseRTMForStackLocks, false);
 831     }
 832     if (UseRTMDeopt) {
 833       FLAG_SET_DEFAULT(UseRTMDeopt, false);
 834     }
 835     if (PrintPreciseRTMLockingStatistics) {
 836       FLAG_SET_DEFAULT(PrintPreciseRTMLockingStatistics, false);
 837     }
 838   }
 839 #else
 840   if (UseRTMLocking) {
 841     // Only C2 does RTM locking optimization.
 842     // Can't continue because UseRTMLocking affects UseBiasedLocking flag
 843     // setting during arguments processing. See use_biased_locking().
 844     vm_exit_during_initialization("RTM locking optimization is not supported in this VM");
 845   }
 846 #endif
 847 
 848 #ifdef COMPILER2
 849   if (UseFPUForSpilling) {
 850     if (UseSSE < 2) {
 851       // Only supported with SSE2+
 852       FLAG_SET_DEFAULT(UseFPUForSpilling, false);
 853     }
 854   }
 855 #endif
 856 #if defined(COMPILER2) || INCLUDE_JVMCI
 857   if (MaxVectorSize > 0) {
 858     if (!is_power_of_2(MaxVectorSize)) {
 859       warning("MaxVectorSize must be a power of 2");
 860       FLAG_SET_DEFAULT(MaxVectorSize, 64);
 861     }
 862     if (MaxVectorSize > 64) {
 863       FLAG_SET_DEFAULT(MaxVectorSize, 64);
 864     }
 865     if (MaxVectorSize > 16 && (UseAVX == 0 || !os_supports_avx_vectors())) {
 866       // 32 bytes vectors (in YMM) are only supported with AVX+
 867       FLAG_SET_DEFAULT(MaxVectorSize, 16);
 868     }
 869     if (UseSSE < 2) {
 870       // Vectors (in XMM) are only supported with SSE2+
 871       FLAG_SET_DEFAULT(MaxVectorSize, 0);
 872     }
 873 #if defined(COMPILER2) && defined(ASSERT)
 874     if (supports_avx() && PrintMiscellaneous && Verbose && TraceNewVectors) {
 875       tty->print_cr("State of YMM registers after signal handle:");
 876       int nreg = 2 LP64_ONLY(+2);
 877       const char* ymm_name[4] = {"0", "7", "8", "15"};
 878       for (int i = 0; i < nreg; i++) {
 879         tty->print("YMM%s:", ymm_name[i]);
 880         for (int j = 7; j >=0; j--) {
 881           tty->print(" %x", _cpuid_info.ymm_save[i*8 + j]);
 882         }
 883         tty->cr();
 884       }
 885     }
 886 #endif // COMPILER2 && ASSERT
 887   }
 888 #endif // COMPILER2 || INCLUDE_JVMCI
 889 
 890 #ifdef COMPILER2
 891 #ifdef _LP64
 892   if (FLAG_IS_DEFAULT(UseMultiplyToLenIntrinsic)) {
 893     UseMultiplyToLenIntrinsic = true;
 894   }
 895   if (FLAG_IS_DEFAULT(UseSquareToLenIntrinsic)) {
 896     UseSquareToLenIntrinsic = true;
 897   }
 898   if (FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {
 899     UseMulAddIntrinsic = true;
 900   }
 901   if (FLAG_IS_DEFAULT(UseMontgomeryMultiplyIntrinsic)) {
 902     UseMontgomeryMultiplyIntrinsic = true;
 903   }
 904   if (FLAG_IS_DEFAULT(UseMontgomerySquareIntrinsic)) {
 905     UseMontgomerySquareIntrinsic = true;
 906   }
 907 #else
 908   if (UseMultiplyToLenIntrinsic) {
 909     if (!FLAG_IS_DEFAULT(UseMultiplyToLenIntrinsic)) {
 910       warning("multiplyToLen intrinsic is not available in 32-bit VM");
 911     }
 912     FLAG_SET_DEFAULT(UseMultiplyToLenIntrinsic, false);
 913   }
 914   if (UseMontgomeryMultiplyIntrinsic) {
 915     if (!FLAG_IS_DEFAULT(UseMontgomeryMultiplyIntrinsic)) {
 916       warning("montgomeryMultiply intrinsic is not available in 32-bit VM");
 917     }
 918     FLAG_SET_DEFAULT(UseMontgomeryMultiplyIntrinsic, false);
 919   }
 920   if (UseMontgomerySquareIntrinsic) {
 921     if (!FLAG_IS_DEFAULT(UseMontgomerySquareIntrinsic)) {
 922       warning("montgomerySquare intrinsic is not available in 32-bit VM");
 923     }
 924     FLAG_SET_DEFAULT(UseMontgomerySquareIntrinsic, false);
 925   }
 926   if (UseSquareToLenIntrinsic) {
 927     if (!FLAG_IS_DEFAULT(UseSquareToLenIntrinsic)) {
 928       warning("squareToLen intrinsic is not available in 32-bit VM");
 929     }
 930     FLAG_SET_DEFAULT(UseSquareToLenIntrinsic, false);
 931   }
 932   if (UseMulAddIntrinsic) {
 933     if (!FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {
 934       warning("mulAdd intrinsic is not available in 32-bit VM");
 935     }
 936     FLAG_SET_DEFAULT(UseMulAddIntrinsic, false);
 937   }
 938 #endif
 939 #endif // COMPILER2
 940 
 941   // On new cpus instructions which update whole XMM register should be used
 942   // to prevent partial register stall due to dependencies on high half.
 943   //
 944   // UseXmmLoadAndClearUpper == true  --> movsd(xmm, mem)
 945   // UseXmmLoadAndClearUpper == false --> movlpd(xmm, mem)
 946   // UseXmmRegToRegMoveAll == true  --> movaps(xmm, xmm), movapd(xmm, xmm).
 947   // UseXmmRegToRegMoveAll == false --> movss(xmm, xmm),  movsd(xmm, xmm).
 948 
 949   if( is_amd() ) { // AMD cpus specific settings
 950     if( supports_sse2() && FLAG_IS_DEFAULT(UseAddressNop) ) {
 951       // Use it on new AMD cpus starting from Opteron.
 952       UseAddressNop = true;
 953     }
 954     if( supports_sse2() && FLAG_IS_DEFAULT(UseNewLongLShift) ) {
 955       // Use it on new AMD cpus starting from Opteron.
 956       UseNewLongLShift = true;
 957     }
 958     if( FLAG_IS_DEFAULT(UseXmmLoadAndClearUpper) ) {
 959       if (supports_sse4a()) {
 960         UseXmmLoadAndClearUpper = true; // use movsd only on '10h' Opteron
 961       } else {
 962         UseXmmLoadAndClearUpper = false;
 963       }
 964     }
 965     if( FLAG_IS_DEFAULT(UseXmmRegToRegMoveAll) ) {
 966       if( supports_sse4a() ) {
 967         UseXmmRegToRegMoveAll = true; // use movaps, movapd only on '10h'
 968       } else {
 969         UseXmmRegToRegMoveAll = false;
 970       }
 971     }
 972     if( FLAG_IS_DEFAULT(UseXmmI2F) ) {
 973       if( supports_sse4a() ) {
 974         UseXmmI2F = true;
 975       } else {
 976         UseXmmI2F = false;
 977       }
 978     }
 979     if( FLAG_IS_DEFAULT(UseXmmI2D) ) {
 980       if( supports_sse4a() ) {
 981         UseXmmI2D = true;
 982       } else {
 983         UseXmmI2D = false;
 984       }
 985     }
 986     if (supports_sse4_2()) {
 987       if (FLAG_IS_DEFAULT(UseSSE42Intrinsics)) {
 988         FLAG_SET_DEFAULT(UseSSE42Intrinsics, true);
 989       }
 990     } else {
 991       if (UseSSE42Intrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 992         warning("SSE4.2 intrinsics require SSE4.2 instructions or higher. Intrinsics will be disabled.");
 993       }
 994       FLAG_SET_DEFAULT(UseSSE42Intrinsics, false);
 995     }
 996 
 997     // some defaults for AMD family 15h
 998     if ( cpu_family() == 0x15 ) {
 999       // On family 15h processors default is no sw prefetch
1000       if (FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
1001         AllocatePrefetchStyle = 0;
1002       }
1003       // Also, if some other prefetch style is specified, default instruction type is PREFETCHW
1004       if (FLAG_IS_DEFAULT(AllocatePrefetchInstr)) {
1005         AllocatePrefetchInstr = 3;
1006       }
1007       // On family 15h processors use XMM and UnalignedLoadStores for Array Copy
1008       if (supports_sse2() && FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
1009         UseXMMForArrayCopy = true;
1010       }
1011       if (supports_sse2() && FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1012         UseUnalignedLoadStores = true;
1013       }
1014     }
1015 
1016 #ifdef COMPILER2
1017     if (MaxVectorSize > 16) {
1018       // Limit vectors size to 16 bytes on current AMD cpus.
1019       FLAG_SET_DEFAULT(MaxVectorSize, 16);
1020     }
1021 #endif // COMPILER2
1022   }
1023 
1024   if( is_intel() ) { // Intel cpus specific settings
1025     if( FLAG_IS_DEFAULT(UseStoreImmI16) ) {
1026       UseStoreImmI16 = false; // don't use it on Intel cpus
1027     }
1028     if( cpu_family() == 6 || cpu_family() == 15 ) {
1029       if( FLAG_IS_DEFAULT(UseAddressNop) ) {
1030         // Use it on all Intel cpus starting from PentiumPro
1031         UseAddressNop = true;
1032       }
1033     }
1034     if( FLAG_IS_DEFAULT(UseXmmLoadAndClearUpper) ) {
1035       UseXmmLoadAndClearUpper = true; // use movsd on all Intel cpus
1036     }
1037     if( FLAG_IS_DEFAULT(UseXmmRegToRegMoveAll) ) {
1038       if( supports_sse3() ) {
1039         UseXmmRegToRegMoveAll = true; // use movaps, movapd on new Intel cpus
1040       } else {
1041         UseXmmRegToRegMoveAll = false;
1042       }
1043     }
1044     if( cpu_family() == 6 && supports_sse3() ) { // New Intel cpus
1045 #ifdef COMPILER2
1046       if( FLAG_IS_DEFAULT(MaxLoopPad) ) {
1047         // For new Intel cpus do the next optimization:
1048         // don't align the beginning of a loop if there are enough instructions
1049         // left (NumberOfLoopInstrToAlign defined in c2_globals.hpp)
1050         // in current fetch line (OptoLoopAlignment) or the padding
1051         // is big (> MaxLoopPad).
1052         // Set MaxLoopPad to 11 for new Intel cpus to reduce number of
1053         // generated NOP instructions. 11 is the largest size of one
1054         // address NOP instruction '0F 1F' (see Assembler::nop(i)).
1055         MaxLoopPad = 11;
1056       }
1057 #endif // COMPILER2
1058       if (FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
1059         UseXMMForArrayCopy = true; // use SSE2 movq on new Intel cpus
1060       }
1061       if (supports_sse4_2() && supports_ht()) { // Newest Intel cpus
1062         if (FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1063           UseUnalignedLoadStores = true; // use movdqu on newest Intel cpus
1064         }
1065       }
1066       if (supports_sse4_2()) {
1067         if (FLAG_IS_DEFAULT(UseSSE42Intrinsics)) {
1068           FLAG_SET_DEFAULT(UseSSE42Intrinsics, true);
1069         }
1070       } else {
1071         if (UseSSE42Intrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
1072           warning("SSE4.2 intrinsics require SSE4.2 instructions or higher. Intrinsics will be disabled.");
1073         }
1074         FLAG_SET_DEFAULT(UseSSE42Intrinsics, false);
1075       }
1076     }
1077     if ((cpu_family() == 0x06) &&
1078         ((extended_cpu_model() == 0x36) || // Centerton
1079          (extended_cpu_model() == 0x37) || // Silvermont
1080          (extended_cpu_model() == 0x4D))) {
1081 #ifdef COMPILER2
1082       if (FLAG_IS_DEFAULT(OptoScheduling)) {
1083         OptoScheduling = true;
1084       }
1085 #endif
1086       if (supports_sse4_2()) { // Silvermont
1087         if (FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1088           UseUnalignedLoadStores = true; // use movdqu on newest Intel cpus
1089         }
1090       }
1091     }
1092     if(FLAG_IS_DEFAULT(AllocatePrefetchInstr) && supports_3dnow_prefetch()) {
1093       AllocatePrefetchInstr = 3;
1094     }
1095   }
1096 
1097 #ifdef _LP64
1098   if (UseSSE42Intrinsics) {
1099     if (FLAG_IS_DEFAULT(UseVectorizedMismatchIntrinsic)) {
1100       UseVectorizedMismatchIntrinsic = true;
1101     }
1102   } else if (UseVectorizedMismatchIntrinsic) {
1103     if (!FLAG_IS_DEFAULT(UseVectorizedMismatchIntrinsic))
1104       warning("vectorizedMismatch intrinsics are not available on this CPU");
1105     FLAG_SET_DEFAULT(UseVectorizedMismatchIntrinsic, false);
1106   }
1107 #else
1108   if (UseVectorizedMismatchIntrinsic) {
1109     if (!FLAG_IS_DEFAULT(UseVectorizedMismatchIntrinsic)) {
1110       warning("vectorizedMismatch intrinsic is not available in 32-bit VM");
1111     }
1112     FLAG_SET_DEFAULT(UseVectorizedMismatchIntrinsic, false);
1113   }
1114 #endif // _LP64
1115 
1116   // Use count leading zeros count instruction if available.
1117   if (supports_lzcnt()) {
1118     if (FLAG_IS_DEFAULT(UseCountLeadingZerosInstruction)) {
1119       UseCountLeadingZerosInstruction = true;
1120     }
1121    } else if (UseCountLeadingZerosInstruction) {
1122     warning("lzcnt instruction is not available on this CPU");
1123     FLAG_SET_DEFAULT(UseCountLeadingZerosInstruction, false);
1124   }
1125 
1126   // Use count trailing zeros instruction if available
1127   if (supports_bmi1()) {
1128     // tzcnt does not require VEX prefix
1129     if (FLAG_IS_DEFAULT(UseCountTrailingZerosInstruction)) {
1130       if (!UseBMI1Instructions && !FLAG_IS_DEFAULT(UseBMI1Instructions)) {
1131         // Don't use tzcnt if BMI1 is switched off on command line.
1132         UseCountTrailingZerosInstruction = false;
1133       } else {
1134         UseCountTrailingZerosInstruction = true;
1135       }
1136     }
1137   } else if (UseCountTrailingZerosInstruction) {
1138     warning("tzcnt instruction is not available on this CPU");
1139     FLAG_SET_DEFAULT(UseCountTrailingZerosInstruction, false);
1140   }
1141 
1142   // BMI instructions (except tzcnt) use an encoding with VEX prefix.
1143   // VEX prefix is generated only when AVX > 0.
1144   if (supports_bmi1() && supports_avx()) {
1145     if (FLAG_IS_DEFAULT(UseBMI1Instructions)) {
1146       UseBMI1Instructions = true;
1147     }
1148   } else if (UseBMI1Instructions) {
1149     warning("BMI1 instructions are not available on this CPU (AVX is also required)");
1150     FLAG_SET_DEFAULT(UseBMI1Instructions, false);
1151   }
1152 
1153   if (supports_bmi2() && supports_avx()) {
1154     if (FLAG_IS_DEFAULT(UseBMI2Instructions)) {
1155       UseBMI2Instructions = true;
1156     }
1157   } else if (UseBMI2Instructions) {
1158     warning("BMI2 instructions are not available on this CPU (AVX is also required)");
1159     FLAG_SET_DEFAULT(UseBMI2Instructions, false);
1160   }
1161 
1162   // Use population count instruction if available.
1163   if (supports_popcnt()) {
1164     if (FLAG_IS_DEFAULT(UsePopCountInstruction)) {
1165       UsePopCountInstruction = true;
1166     }
1167   } else if (UsePopCountInstruction) {
1168     warning("POPCNT instruction is not available on this CPU");
1169     FLAG_SET_DEFAULT(UsePopCountInstruction, false);
1170   }
1171 
1172   // Use fast-string operations if available.
1173   if (supports_erms()) {
1174     if (FLAG_IS_DEFAULT(UseFastStosb)) {
1175       UseFastStosb = true;
1176     }
1177   } else if (UseFastStosb) {
1178     warning("fast-string operations are not available on this CPU");
1179     FLAG_SET_DEFAULT(UseFastStosb, false);
1180   }
1181 
1182 #ifdef COMPILER2
1183   if (FLAG_IS_DEFAULT(AlignVector)) {
1184     // Modern processors allow misaligned memory operations for vectors.
1185     AlignVector = !UseUnalignedLoadStores;
1186   }
1187 #endif // COMPILER2
1188 
1189   if( AllocatePrefetchInstr == 3 && !supports_3dnow_prefetch() ) AllocatePrefetchInstr=0;
1190   if( !supports_sse() && supports_3dnow_prefetch() ) AllocatePrefetchInstr = 3;
1191 
1192   // Allocation prefetch settings
1193   intx cache_line_size = prefetch_data_size();
1194   if( cache_line_size > AllocatePrefetchStepSize )
1195     AllocatePrefetchStepSize = cache_line_size;
1196 
1197   AllocatePrefetchDistance = allocate_prefetch_distance();
1198   AllocatePrefetchStyle    = allocate_prefetch_style();
1199 
1200   if (is_intel() && cpu_family() == 6 && supports_sse3()) {
1201     if (AllocatePrefetchStyle == 2) { // watermark prefetching on Core
1202 #ifdef _LP64
1203       AllocatePrefetchDistance = 384;
1204 #else
1205       AllocatePrefetchDistance = 320;
1206 #endif
1207     }
1208     if (supports_sse4_2() && supports_ht()) { // Nehalem based cpus
1209       AllocatePrefetchDistance = 192;
1210       if (FLAG_IS_DEFAULT(AllocatePrefetchLines)) {
1211         FLAG_SET_DEFAULT(AllocatePrefetchLines, 4);
1212       }
1213     }
1214 #ifdef COMPILER2
1215     if (supports_sse4_2()) {
1216       if (FLAG_IS_DEFAULT(UseFPUForSpilling)) {
1217         FLAG_SET_DEFAULT(UseFPUForSpilling, true);
1218       }
1219     }
1220 #endif
1221   }
1222 
1223 #ifdef _LP64
1224   // Prefetch settings
1225   PrefetchCopyIntervalInBytes = prefetch_copy_interval_in_bytes();
1226   PrefetchScanIntervalInBytes = prefetch_scan_interval_in_bytes();
1227   PrefetchFieldsAhead         = prefetch_fields_ahead();
1228 #endif
1229 
1230   if (FLAG_IS_DEFAULT(ContendedPaddingWidth) &&
1231      (cache_line_size > ContendedPaddingWidth))
1232      ContendedPaddingWidth = cache_line_size;
1233 
1234   // This machine allows unaligned memory accesses
1235   if (FLAG_IS_DEFAULT(UseUnalignedAccesses)) {
1236     FLAG_SET_DEFAULT(UseUnalignedAccesses, true);
1237   }
1238 
1239 #ifndef PRODUCT
1240   if (log_is_enabled(Info, os, cpu)) {
1241     outputStream* log = Log(os, cpu)::info_stream();
1242     log->print_cr("Logical CPUs per core: %u",
1243                   logical_processors_per_package());
1244     log->print_cr("L1 data cache line size: %u", L1_data_cache_line_size());
1245     log->print("UseSSE=%d", (int) UseSSE);
1246     if (UseAVX > 0) {
1247       log->print("  UseAVX=%d", (int) UseAVX);
1248     }
1249     if (UseAES) {
1250       log->print("  UseAES=1");
1251     }
1252 #ifdef COMPILER2
1253     if (MaxVectorSize > 0) {
1254       log->print("  MaxVectorSize=%d", (int) MaxVectorSize);
1255     }
1256 #endif
1257     log->cr();
1258     log->print("Allocation");
1259     if (AllocatePrefetchStyle <= 0 || UseSSE == 0 && !supports_3dnow_prefetch()) {
1260       log->print_cr(": no prefetching");
1261     } else {
1262       log->print(" prefetching: ");
1263       if (UseSSE == 0 && supports_3dnow_prefetch()) {
1264         log->print("PREFETCHW");
1265       } else if (UseSSE >= 1) {
1266         if (AllocatePrefetchInstr == 0) {
1267           log->print("PREFETCHNTA");
1268         } else if (AllocatePrefetchInstr == 1) {
1269           log->print("PREFETCHT0");
1270         } else if (AllocatePrefetchInstr == 2) {
1271           log->print("PREFETCHT2");
1272         } else if (AllocatePrefetchInstr == 3) {
1273           log->print("PREFETCHW");
1274         }
1275       }
1276       if (AllocatePrefetchLines > 1) {
1277         log->print_cr(" at distance %d, %d lines of %d bytes", (int) AllocatePrefetchDistance, (int) AllocatePrefetchLines, (int) AllocatePrefetchStepSize);
1278       } else {
1279         log->print_cr(" at distance %d, one line of %d bytes", (int) AllocatePrefetchDistance, (int) AllocatePrefetchStepSize);
1280       }
1281     }
1282 
1283     if (PrefetchCopyIntervalInBytes > 0) {
1284       log->print_cr("PrefetchCopyIntervalInBytes %d", (int) PrefetchCopyIntervalInBytes);
1285     }
1286     if (PrefetchScanIntervalInBytes > 0) {
1287       log->print_cr("PrefetchScanIntervalInBytes %d", (int) PrefetchScanIntervalInBytes);
1288     }
1289     if (PrefetchFieldsAhead > 0) {
1290       log->print_cr("PrefetchFieldsAhead %d", (int) PrefetchFieldsAhead);
1291     }
1292     if (ContendedPaddingWidth > 0) {
1293       log->print_cr("ContendedPaddingWidth %d", (int) ContendedPaddingWidth);
1294     }
1295   }
1296 #endif // !PRODUCT
1297 }
1298 
1299 bool VM_Version::use_biased_locking() {
1300 #if INCLUDE_RTM_OPT
1301   // RTM locking is most useful when there is high lock contention and
1302   // low data contention.  With high lock contention the lock is usually
1303   // inflated and biased locking is not suitable for that case.
1304   // RTM locking code requires that biased locking is off.
1305   // Note: we can't switch off UseBiasedLocking in get_processor_features()
1306   // because it is used by Thread::allocate() which is called before
1307   // VM_Version::initialize().
1308   if (UseRTMLocking && UseBiasedLocking) {
1309     if (FLAG_IS_DEFAULT(UseBiasedLocking)) {
1310       FLAG_SET_DEFAULT(UseBiasedLocking, false);
1311     } else {
1312       warning("Biased locking is not supported with RTM locking; ignoring UseBiasedLocking flag." );
1313       UseBiasedLocking = false;
1314     }
1315   }
1316 #endif
1317   return UseBiasedLocking;
1318 }
1319 
1320 void VM_Version::initialize() {
1321   ResourceMark rm;
1322   // Making this stub must be FIRST use of assembler
1323 
1324   stub_blob = BufferBlob::create("get_cpu_info_stub", stub_size);
1325   if (stub_blob == NULL) {
1326     vm_exit_during_initialization("Unable to allocate get_cpu_info_stub");
1327   }
1328   CodeBuffer c(stub_blob);
1329   VM_Version_StubGenerator g(&c);
1330   get_cpu_info_stub = CAST_TO_FN_PTR(get_cpu_info_stub_t,
1331                                      g.generate_get_cpu_info());
1332 
1333   get_processor_features();
1334 }