1 /*
   2  * Copyright (c) 1997, 2019, 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 "jvm.h"
  27 #include "asm/macroAssembler.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "runtime/java.hpp"
  33 #include "runtime/os.hpp"
  34 #include "runtime/stubCodeGenerator.hpp"
  35 #include "runtime/vm_version.hpp"
  36 #include "utilities/virtualizationSupport.hpp"
  37 
  38 #include OS_HEADER_INLINE(os)
  39 
  40 int VM_Version::_cpu;
  41 int VM_Version::_model;
  42 int VM_Version::_stepping;
  43 VM_Version::CpuidInfo VM_Version::_cpuid_info = { 0, };
  44 
  45 // Address of instruction which causes SEGV
  46 address VM_Version::_cpuinfo_segv_addr = 0;
  47 // Address of instruction after the one which causes SEGV
  48 address VM_Version::_cpuinfo_cont_addr = 0;
  49 
  50 static BufferBlob* stub_blob;
  51 static const int stub_size = 1100;
  52 
  53 extern "C" {
  54   typedef void (*get_cpu_info_stub_t)(void*);
  55 }
  56 static get_cpu_info_stub_t get_cpu_info_stub = NULL;
  57 
  58 
  59 class VM_Version_StubGenerator: public StubCodeGenerator {
  60  public:
  61 
  62   VM_Version_StubGenerator(CodeBuffer *c) : StubCodeGenerator(c) {}
  63 
  64   address generate_get_cpu_info() {
  65     // Flags to test CPU type.
  66     const uint32_t HS_EFL_AC = 0x40000;
  67     const uint32_t HS_EFL_ID = 0x200000;
  68     // Values for when we don't have a CPUID instruction.
  69     const int      CPU_FAMILY_SHIFT = 8;
  70     const uint32_t CPU_FAMILY_386 = (3 << CPU_FAMILY_SHIFT);
  71     const uint32_t CPU_FAMILY_486 = (4 << CPU_FAMILY_SHIFT);
  72     bool use_evex = FLAG_IS_DEFAULT(UseAVX) || (UseAVX > 2);
  73 
  74     Label detect_486, cpu486, detect_586, std_cpuid1, std_cpuid4;
  75     Label sef_cpuid, ext_cpuid, ext_cpuid1, ext_cpuid5, ext_cpuid7, ext_cpuid8, done, wrapup;
  76     Label legacy_setup, save_restore_except, legacy_save_restore, start_simd_check;
  77 
  78     StubCodeMark mark(this, "VM_Version", "get_cpu_info_stub");
  79 #   define __ _masm->
  80 
  81     address start = __ pc();
  82 
  83     //
  84     // void get_cpu_info(VM_Version::CpuidInfo* cpuid_info);
  85     //
  86     // LP64: rcx and rdx are first and second argument registers on windows
  87 
  88     __ push(rbp);
  89 #ifdef _LP64
  90     __ mov(rbp, c_rarg0); // cpuid_info address
  91 #else
  92     __ movptr(rbp, Address(rsp, 8)); // cpuid_info address
  93 #endif
  94     __ push(rbx);
  95     __ push(rsi);
  96     __ pushf();          // preserve rbx, and flags
  97     __ pop(rax);
  98     __ push(rax);
  99     __ mov(rcx, rax);
 100     //
 101     // if we are unable to change the AC flag, we have a 386
 102     //
 103     __ xorl(rax, HS_EFL_AC);
 104     __ push(rax);
 105     __ popf();
 106     __ pushf();
 107     __ pop(rax);
 108     __ cmpptr(rax, rcx);
 109     __ jccb(Assembler::notEqual, detect_486);
 110 
 111     __ movl(rax, CPU_FAMILY_386);
 112     __ movl(Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())), rax);
 113     __ jmp(done);
 114 
 115     //
 116     // If we are unable to change the ID flag, we have a 486 which does
 117     // not support the "cpuid" instruction.
 118     //
 119     __ bind(detect_486);
 120     __ mov(rax, rcx);
 121     __ xorl(rax, HS_EFL_ID);
 122     __ push(rax);
 123     __ popf();
 124     __ pushf();
 125     __ pop(rax);
 126     __ cmpptr(rcx, rax);
 127     __ jccb(Assembler::notEqual, detect_586);
 128 
 129     __ bind(cpu486);
 130     __ movl(rax, CPU_FAMILY_486);
 131     __ movl(Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())), rax);
 132     __ jmp(done);
 133 
 134     //
 135     // At this point, we have a chip which supports the "cpuid" instruction
 136     //
 137     __ bind(detect_586);
 138     __ xorl(rax, rax);
 139     __ cpuid();
 140     __ orl(rax, rax);
 141     __ jcc(Assembler::equal, cpu486);   // if cpuid doesn't support an input
 142                                         // value of at least 1, we give up and
 143                                         // assume a 486
 144     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset())));
 145     __ movl(Address(rsi, 0), rax);
 146     __ movl(Address(rsi, 4), rbx);
 147     __ movl(Address(rsi, 8), rcx);
 148     __ movl(Address(rsi,12), rdx);
 149 
 150     __ cmpl(rax, 0xa);                  // Is cpuid(0xB) supported?
 151     __ jccb(Assembler::belowEqual, std_cpuid4);
 152 
 153     //
 154     // cpuid(0xB) Processor Topology
 155     //
 156     __ movl(rax, 0xb);
 157     __ xorl(rcx, rcx);   // Threads level
 158     __ cpuid();
 159 
 160     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB0_offset())));
 161     __ movl(Address(rsi, 0), rax);
 162     __ movl(Address(rsi, 4), rbx);
 163     __ movl(Address(rsi, 8), rcx);
 164     __ movl(Address(rsi,12), rdx);
 165 
 166     __ movl(rax, 0xb);
 167     __ movl(rcx, 1);     // Cores level
 168     __ cpuid();
 169     __ push(rax);
 170     __ andl(rax, 0x1f);  // Determine if valid topology level
 171     __ orl(rax, rbx);    // eax[4:0] | ebx[0:15] == 0 indicates invalid level
 172     __ andl(rax, 0xffff);
 173     __ pop(rax);
 174     __ jccb(Assembler::equal, std_cpuid4);
 175 
 176     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB1_offset())));
 177     __ movl(Address(rsi, 0), rax);
 178     __ movl(Address(rsi, 4), rbx);
 179     __ movl(Address(rsi, 8), rcx);
 180     __ movl(Address(rsi,12), rdx);
 181 
 182     __ movl(rax, 0xb);
 183     __ movl(rcx, 2);     // Packages level
 184     __ cpuid();
 185     __ push(rax);
 186     __ andl(rax, 0x1f);  // Determine if valid topology level
 187     __ orl(rax, rbx);    // eax[4:0] | ebx[0:15] == 0 indicates invalid level
 188     __ andl(rax, 0xffff);
 189     __ pop(rax);
 190     __ jccb(Assembler::equal, std_cpuid4);
 191 
 192     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB2_offset())));
 193     __ movl(Address(rsi, 0), rax);
 194     __ movl(Address(rsi, 4), rbx);
 195     __ movl(Address(rsi, 8), rcx);
 196     __ movl(Address(rsi,12), rdx);
 197 
 198     //
 199     // cpuid(0x4) Deterministic cache params
 200     //
 201     __ bind(std_cpuid4);
 202     __ movl(rax, 4);
 203     __ cmpl(rax, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset()))); // Is cpuid(0x4) supported?
 204     __ jccb(Assembler::greater, std_cpuid1);
 205 
 206     __ xorl(rcx, rcx);   // L1 cache
 207     __ cpuid();
 208     __ push(rax);
 209     __ andl(rax, 0x1f);  // Determine if valid cache parameters used
 210     __ orl(rax, rax);    // eax[4:0] == 0 indicates invalid cache
 211     __ pop(rax);
 212     __ jccb(Assembler::equal, std_cpuid1);
 213 
 214     __ lea(rsi, Address(rbp, in_bytes(VM_Version::dcp_cpuid4_offset())));
 215     __ movl(Address(rsi, 0), rax);
 216     __ movl(Address(rsi, 4), rbx);
 217     __ movl(Address(rsi, 8), rcx);
 218     __ movl(Address(rsi,12), rdx);
 219 
 220     //
 221     // Standard cpuid(0x1)
 222     //
 223     __ bind(std_cpuid1);
 224     __ movl(rax, 1);
 225     __ cpuid();
 226     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
 227     __ movl(Address(rsi, 0), rax);
 228     __ movl(Address(rsi, 4), rbx);
 229     __ movl(Address(rsi, 8), rcx);
 230     __ movl(Address(rsi,12), rdx);
 231 
 232     //
 233     // Check if OS has enabled XGETBV instruction to access XCR0
 234     // (OSXSAVE feature flag) and CPU supports AVX
 235     //
 236     __ andl(rcx, 0x18000000); // cpuid1 bits osxsave | avx
 237     __ cmpl(rcx, 0x18000000);
 238     __ jccb(Assembler::notEqual, sef_cpuid); // jump if AVX is not supported
 239 
 240     //
 241     // XCR0, XFEATURE_ENABLED_MASK register
 242     //
 243     __ xorl(rcx, rcx);   // zero for XCR0 register
 244     __ xgetbv();
 245     __ lea(rsi, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset())));
 246     __ movl(Address(rsi, 0), rax);
 247     __ movl(Address(rsi, 4), rdx);
 248 
 249     //
 250     // cpuid(0x7) Structured Extended Features
 251     //
 252     __ bind(sef_cpuid);
 253     __ movl(rax, 7);
 254     __ cmpl(rax, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset()))); // Is cpuid(0x7) supported?
 255     __ jccb(Assembler::greater, ext_cpuid);
 256 
 257     __ xorl(rcx, rcx);
 258     __ cpuid();
 259     __ lea(rsi, Address(rbp, in_bytes(VM_Version::sef_cpuid7_offset())));
 260     __ movl(Address(rsi, 0), rax);
 261     __ movl(Address(rsi, 4), rbx);
 262     __ movl(Address(rsi, 8), rcx);
 263     __ movl(Address(rsi, 12), rdx);
 264 
 265     //
 266     // Extended cpuid(0x80000000)
 267     //
 268     __ bind(ext_cpuid);
 269     __ movl(rax, 0x80000000);
 270     __ cpuid();
 271     __ cmpl(rax, 0x80000000);     // Is cpuid(0x80000001) supported?
 272     __ jcc(Assembler::belowEqual, done);
 273     __ cmpl(rax, 0x80000004);     // Is cpuid(0x80000005) supported?
 274     __ jcc(Assembler::belowEqual, ext_cpuid1);
 275     __ cmpl(rax, 0x80000006);     // Is cpuid(0x80000007) supported?
 276     __ jccb(Assembler::belowEqual, ext_cpuid5);
 277     __ cmpl(rax, 0x80000007);     // Is cpuid(0x80000008) supported?
 278     __ jccb(Assembler::belowEqual, ext_cpuid7);
 279     __ cmpl(rax, 0x80000008);     // Is cpuid(0x80000009 and above) supported?
 280     __ jccb(Assembler::belowEqual, ext_cpuid8);
 281     __ cmpl(rax, 0x8000001E);     // Is cpuid(0x8000001E) supported?
 282     __ jccb(Assembler::below, ext_cpuid8);
 283     //
 284     // Extended cpuid(0x8000001E)
 285     //
 286     __ movl(rax, 0x8000001E);
 287     __ cpuid();
 288     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid1E_offset())));
 289     __ movl(Address(rsi, 0), rax);
 290     __ movl(Address(rsi, 4), rbx);
 291     __ movl(Address(rsi, 8), rcx);
 292     __ movl(Address(rsi,12), rdx);
 293 
 294     //
 295     // Extended cpuid(0x80000008)
 296     //
 297     __ bind(ext_cpuid8);
 298     __ movl(rax, 0x80000008);
 299     __ cpuid();
 300     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid8_offset())));
 301     __ movl(Address(rsi, 0), rax);
 302     __ movl(Address(rsi, 4), rbx);
 303     __ movl(Address(rsi, 8), rcx);
 304     __ movl(Address(rsi,12), rdx);
 305 
 306     //
 307     // Extended cpuid(0x80000007)
 308     //
 309     __ bind(ext_cpuid7);
 310     __ movl(rax, 0x80000007);
 311     __ cpuid();
 312     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid7_offset())));
 313     __ movl(Address(rsi, 0), rax);
 314     __ movl(Address(rsi, 4), rbx);
 315     __ movl(Address(rsi, 8), rcx);
 316     __ movl(Address(rsi,12), rdx);
 317 
 318     //
 319     // Extended cpuid(0x80000005)
 320     //
 321     __ bind(ext_cpuid5);
 322     __ movl(rax, 0x80000005);
 323     __ cpuid();
 324     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid5_offset())));
 325     __ movl(Address(rsi, 0), rax);
 326     __ movl(Address(rsi, 4), rbx);
 327     __ movl(Address(rsi, 8), rcx);
 328     __ movl(Address(rsi,12), rdx);
 329 
 330     //
 331     // Extended cpuid(0x80000001)
 332     //
 333     __ bind(ext_cpuid1);
 334     __ movl(rax, 0x80000001);
 335     __ cpuid();
 336     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid1_offset())));
 337     __ movl(Address(rsi, 0), rax);
 338     __ movl(Address(rsi, 4), rbx);
 339     __ movl(Address(rsi, 8), rcx);
 340     __ movl(Address(rsi,12), rdx);
 341 
 342     //
 343     // Check if OS has enabled XGETBV instruction to access XCR0
 344     // (OSXSAVE feature flag) and CPU supports AVX
 345     //
 346     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
 347     __ movl(rcx, 0x18000000); // cpuid1 bits osxsave | avx
 348     __ andl(rcx, Address(rsi, 8)); // cpuid1 bits osxsave | avx
 349     __ cmpl(rcx, 0x18000000);
 350     __ jccb(Assembler::notEqual, done); // jump if AVX is not supported
 351 
 352     __ movl(rax, 0x6);
 353     __ andl(rax, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset()))); // xcr0 bits sse | ymm
 354     __ cmpl(rax, 0x6);
 355     __ jccb(Assembler::equal, start_simd_check); // return if AVX is not supported
 356 
 357     // we need to bridge farther than imm8, so we use this island as a thunk
 358     __ bind(done);
 359     __ jmp(wrapup);
 360 
 361     __ bind(start_simd_check);
 362     //
 363     // Some OSs have a bug when upper 128/256bits of YMM/ZMM
 364     // registers are not restored after a signal processing.
 365     // Generate SEGV here (reference through NULL)
 366     // and check upper YMM/ZMM bits after it.
 367     //
 368     intx saved_useavx = UseAVX;
 369     intx saved_usesse = UseSSE;
 370 
 371     // If UseAVX is unitialized or is set by the user to include EVEX
 372     if (use_evex) {
 373       // check _cpuid_info.sef_cpuid7_ebx.bits.avx512f
 374       __ lea(rsi, Address(rbp, in_bytes(VM_Version::sef_cpuid7_offset())));
 375       __ movl(rax, 0x10000);
 376       __ andl(rax, Address(rsi, 4)); // xcr0 bits sse | ymm
 377       __ cmpl(rax, 0x10000);
 378       __ jccb(Assembler::notEqual, legacy_setup); // jump if EVEX is not supported
 379       // check _cpuid_info.xem_xcr0_eax.bits.opmask
 380       // check _cpuid_info.xem_xcr0_eax.bits.zmm512
 381       // check _cpuid_info.xem_xcr0_eax.bits.zmm32
 382       __ movl(rax, 0xE0);
 383       __ andl(rax, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset()))); // xcr0 bits sse | ymm
 384       __ cmpl(rax, 0xE0);
 385       __ jccb(Assembler::notEqual, legacy_setup); // jump if EVEX is not supported
 386 
 387       if (FLAG_IS_DEFAULT(UseAVX)) {
 388         __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
 389         __ movl(rax, Address(rsi, 0));
 390         __ cmpl(rax, 0x50654);              // If it is Skylake
 391         __ jcc(Assembler::equal, legacy_setup);
 392       }
 393       // EVEX setup: run in lowest evex mode
 394       VM_Version::set_evex_cpuFeatures(); // Enable temporary to pass asserts
 395       UseAVX = 3;
 396       UseSSE = 2;
 397 #ifdef _WINDOWS
 398       // xmm5-xmm15 are not preserved by caller on windows
 399       // https://msdn.microsoft.com/en-us/library/9z1stfyw.aspx
 400       __ subptr(rsp, 64);
 401       __ evmovdqul(Address(rsp, 0), xmm7, Assembler::AVX_512bit);
 402 #ifdef _LP64
 403       __ subptr(rsp, 64);
 404       __ evmovdqul(Address(rsp, 0), xmm8, Assembler::AVX_512bit);
 405       __ subptr(rsp, 64);
 406       __ evmovdqul(Address(rsp, 0), xmm31, Assembler::AVX_512bit);
 407 #endif // _LP64
 408 #endif // _WINDOWS
 409 
 410       // load value into all 64 bytes of zmm7 register
 411       __ movl(rcx, VM_Version::ymm_test_value());
 412       __ movdl(xmm0, rcx);
 413       __ vpbroadcastd(xmm0, xmm0, Assembler::AVX_512bit);
 414       __ evmovdqul(xmm7, xmm0, Assembler::AVX_512bit);
 415 #ifdef _LP64
 416       __ evmovdqul(xmm8, xmm0, Assembler::AVX_512bit);
 417       __ evmovdqul(xmm31, xmm0, Assembler::AVX_512bit);
 418 #endif
 419       VM_Version::clean_cpuFeatures();
 420       __ jmp(save_restore_except);
 421     }
 422 
 423     __ bind(legacy_setup);
 424     // AVX setup
 425     VM_Version::set_avx_cpuFeatures(); // Enable temporary to pass asserts
 426     UseAVX = 1;
 427     UseSSE = 2;
 428 #ifdef _WINDOWS
 429     __ subptr(rsp, 32);
 430     __ vmovdqu(Address(rsp, 0), xmm7);
 431 #ifdef _LP64
 432     __ subptr(rsp, 32);
 433     __ vmovdqu(Address(rsp, 0), xmm8);
 434     __ subptr(rsp, 32);
 435     __ vmovdqu(Address(rsp, 0), xmm15);
 436 #endif // _LP64
 437 #endif // _WINDOWS
 438 
 439     // load value into all 32 bytes of ymm7 register
 440     __ movl(rcx, VM_Version::ymm_test_value());
 441 
 442     __ movdl(xmm0, rcx);
 443     __ pshufd(xmm0, xmm0, 0x00);
 444     __ vinsertf128_high(xmm0, xmm0);
 445     __ vmovdqu(xmm7, xmm0);
 446 #ifdef _LP64
 447     __ vmovdqu(xmm8, xmm0);
 448     __ vmovdqu(xmm15, xmm0);
 449 #endif
 450     VM_Version::clean_cpuFeatures();
 451 
 452     __ bind(save_restore_except);
 453     __ xorl(rsi, rsi);
 454     VM_Version::set_cpuinfo_segv_addr(__ pc());
 455     // Generate SEGV
 456     __ movl(rax, Address(rsi, 0));
 457 
 458     VM_Version::set_cpuinfo_cont_addr(__ pc());
 459     // Returns here after signal. Save xmm0 to check it later.
 460 
 461     // If UseAVX is unitialized or is set by the user to include EVEX
 462     if (use_evex) {
 463       // check _cpuid_info.sef_cpuid7_ebx.bits.avx512f
 464       __ lea(rsi, Address(rbp, in_bytes(VM_Version::sef_cpuid7_offset())));
 465       __ movl(rax, 0x10000);
 466       __ andl(rax, Address(rsi, 4));
 467       __ cmpl(rax, 0x10000);
 468       __ jcc(Assembler::notEqual, legacy_save_restore);
 469       // check _cpuid_info.xem_xcr0_eax.bits.opmask
 470       // check _cpuid_info.xem_xcr0_eax.bits.zmm512
 471       // check _cpuid_info.xem_xcr0_eax.bits.zmm32
 472       __ movl(rax, 0xE0);
 473       __ andl(rax, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset()))); // xcr0 bits sse | ymm
 474       __ cmpl(rax, 0xE0);
 475       __ jcc(Assembler::notEqual, legacy_save_restore);
 476 
 477       if (FLAG_IS_DEFAULT(UseAVX)) {
 478         __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
 479         __ movl(rax, Address(rsi, 0));
 480         __ cmpl(rax, 0x50654);              // If it is Skylake
 481         __ jcc(Assembler::equal, legacy_save_restore);
 482       }
 483       // EVEX check: run in lowest evex mode
 484       VM_Version::set_evex_cpuFeatures(); // Enable temporary to pass asserts
 485       UseAVX = 3;
 486       UseSSE = 2;
 487       __ lea(rsi, Address(rbp, in_bytes(VM_Version::zmm_save_offset())));
 488       __ evmovdqul(Address(rsi, 0), xmm0, Assembler::AVX_512bit);
 489       __ evmovdqul(Address(rsi, 64), xmm7, Assembler::AVX_512bit);
 490 #ifdef _LP64
 491       __ evmovdqul(Address(rsi, 128), xmm8, Assembler::AVX_512bit);
 492       __ evmovdqul(Address(rsi, 192), xmm31, Assembler::AVX_512bit);
 493 #endif
 494 
 495 #ifdef _WINDOWS
 496 #ifdef _LP64
 497       __ evmovdqul(xmm31, Address(rsp, 0), Assembler::AVX_512bit);
 498       __ addptr(rsp, 64);
 499       __ evmovdqul(xmm8, Address(rsp, 0), Assembler::AVX_512bit);
 500       __ addptr(rsp, 64);
 501 #endif // _LP64
 502       __ evmovdqul(xmm7, Address(rsp, 0), Assembler::AVX_512bit);
 503       __ addptr(rsp, 64);
 504 #endif // _WINDOWS
 505       generate_vzeroupper(wrapup);
 506       VM_Version::clean_cpuFeatures();
 507       UseAVX = saved_useavx;
 508       UseSSE = saved_usesse;
 509       __ jmp(wrapup);
 510    }
 511 
 512     __ bind(legacy_save_restore);
 513     // AVX check
 514     VM_Version::set_avx_cpuFeatures(); // Enable temporary to pass asserts
 515     UseAVX = 1;
 516     UseSSE = 2;
 517     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ymm_save_offset())));
 518     __ vmovdqu(Address(rsi, 0), xmm0);
 519     __ vmovdqu(Address(rsi, 32), xmm7);
 520 #ifdef _LP64
 521     __ vmovdqu(Address(rsi, 64), xmm8);
 522     __ vmovdqu(Address(rsi, 96), xmm15);
 523 #endif
 524 
 525 #ifdef _WINDOWS
 526 #ifdef _LP64
 527     __ vmovdqu(xmm15, Address(rsp, 0));
 528     __ addptr(rsp, 32);
 529     __ vmovdqu(xmm8, Address(rsp, 0));
 530     __ addptr(rsp, 32);
 531 #endif // _LP64
 532     __ vmovdqu(xmm7, Address(rsp, 0));
 533     __ addptr(rsp, 32);
 534 #endif // _WINDOWS
 535     generate_vzeroupper(wrapup);
 536     VM_Version::clean_cpuFeatures();
 537     UseAVX = saved_useavx;
 538     UseSSE = saved_usesse;
 539 
 540     __ bind(wrapup);
 541     __ popf();
 542     __ pop(rsi);
 543     __ pop(rbx);
 544     __ pop(rbp);
 545     __ ret(0);
 546 
 547 #   undef __
 548 
 549     return start;
 550   };
 551   void generate_vzeroupper(Label& L_wrapup) {
 552 #   define __ _masm->
 553     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset())));
 554     __ cmpl(Address(rsi, 4), 0x756e6547);  // 'uneG'
 555     __ jcc(Assembler::notEqual, L_wrapup);
 556     __ movl(rcx, 0x0FFF0FF0);
 557     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
 558     __ andl(rcx, Address(rsi, 0));
 559     __ cmpl(rcx, 0x00050670);              // If it is Xeon Phi 3200/5200/7200
 560     __ jcc(Assembler::equal, L_wrapup);
 561     __ cmpl(rcx, 0x00080650);              // If it is Future Xeon Phi
 562     __ jcc(Assembler::equal, L_wrapup);
 563     __ vzeroupper();
 564 #   undef __
 565   }
 566 };
 567 
 568 void VM_Version::get_processor_features() {
 569 
 570   _cpu = 4; // 486 by default
 571   _model = 0;
 572   _stepping = 0;
 573   _features = 0;
 574   _logical_processors_per_package = 1;
 575   // i486 internal cache is both I&D and has a 16-byte line size
 576   _L1_data_cache_line_size = 16;
 577 
 578   // Get raw processor info
 579 
 580   get_cpu_info_stub(&_cpuid_info);
 581 
 582   assert_is_initialized();
 583   _cpu = extended_cpu_family();
 584   _model = extended_cpu_model();
 585   _stepping = cpu_stepping();
 586 
 587   if (cpu_family() > 4) { // it supports CPUID
 588     _features = feature_flags();
 589     // Logical processors are only available on P4s and above,
 590     // and only if hyperthreading is available.
 591     _logical_processors_per_package = logical_processor_count();
 592     _L1_data_cache_line_size = L1_line_size();
 593   }
 594 
 595   _supports_cx8 = supports_cmpxchg8();
 596   // xchg and xadd instructions
 597   _supports_atomic_getset4 = true;
 598   _supports_atomic_getadd4 = true;
 599   LP64_ONLY(_supports_atomic_getset8 = true);
 600   LP64_ONLY(_supports_atomic_getadd8 = true);
 601 
 602 #ifdef _LP64
 603   // OS should support SSE for x64 and hardware should support at least SSE2.
 604   if (!VM_Version::supports_sse2()) {
 605     vm_exit_during_initialization("Unknown x64 processor: SSE2 not supported");
 606   }
 607   // in 64 bit the use of SSE2 is the minimum
 608   if (UseSSE < 2) UseSSE = 2;
 609 #endif
 610 
 611 #ifdef AMD64
 612   // flush_icache_stub have to be generated first.
 613   // That is why Icache line size is hard coded in ICache class,
 614   // see icache_x86.hpp. It is also the reason why we can't use
 615   // clflush instruction in 32-bit VM since it could be running
 616   // on CPU which does not support it.
 617   //
 618   // The only thing we can do is to verify that flushed
 619   // ICache::line_size has correct value.
 620   guarantee(_cpuid_info.std_cpuid1_edx.bits.clflush != 0, "clflush is not supported");
 621   // clflush_size is size in quadwords (8 bytes).
 622   guarantee(_cpuid_info.std_cpuid1_ebx.bits.clflush_size == 8, "such clflush size is not supported");
 623 #endif
 624 
 625 #ifdef _LP64
 626   // assigning this field effectively enables Unsafe.writebackMemory()
 627   // by initing UnsafeConstant.DATA_CACHE_LINE_FLUSH_SIZE to non-zero
 628   // that is only implemented on x86_64 and only if the OS plays ball
 629   if (os::supports_map_sync()) {
 630     // publish data cache line flush size to generic field, otherwise
 631     // let if default to zero thereby disabling writeback
 632     _data_cache_line_flush_size = _cpuid_info.std_cpuid1_ebx.bits.clflush_size * 8;
 633   }
 634 #endif
 635   // If the OS doesn't support SSE, we can't use this feature even if the HW does
 636   if (!os::supports_sse())
 637     _features &= ~(CPU_SSE|CPU_SSE2|CPU_SSE3|CPU_SSSE3|CPU_SSE4A|CPU_SSE4_1|CPU_SSE4_2);
 638 
 639   if (UseSSE < 4) {
 640     _features &= ~CPU_SSE4_1;
 641     _features &= ~CPU_SSE4_2;
 642   }
 643 
 644   if (UseSSE < 3) {
 645     _features &= ~CPU_SSE3;
 646     _features &= ~CPU_SSSE3;
 647     _features &= ~CPU_SSE4A;
 648   }
 649 
 650   if (UseSSE < 2)
 651     _features &= ~CPU_SSE2;
 652 
 653   if (UseSSE < 1)
 654     _features &= ~CPU_SSE;
 655 
 656   //since AVX instructions is slower than SSE in some ZX cpus, force USEAVX=0.
 657   if (is_zx() && ((cpu_family() == 6) || (cpu_family() == 7))) {
 658     UseAVX = 0;
 659   }
 660 
 661   // first try initial setting and detect what we can support
 662   int use_avx_limit = 0;
 663   if (UseAVX > 0) {
 664     if (UseAVX > 2 && supports_evex()) {
 665       use_avx_limit = 3;
 666     } else if (UseAVX > 1 && supports_avx2()) {
 667       use_avx_limit = 2;
 668     } else if (UseAVX > 0 && supports_avx()) {
 669       use_avx_limit = 1;
 670     } else {
 671       use_avx_limit = 0;
 672     }
 673   }
 674   if (FLAG_IS_DEFAULT(UseAVX)) {
 675     FLAG_SET_DEFAULT(UseAVX, use_avx_limit);
 676     if (is_intel_family_core() && _model == CPU_MODEL_SKYLAKE && _stepping < 5) {
 677       FLAG_SET_DEFAULT(UseAVX, 2);  //Set UseAVX=2 for Skylake
 678     }
 679   } else if (UseAVX > use_avx_limit) {
 680     warning("UseAVX=%d is not supported on this CPU, setting it to UseAVX=%d", (int) UseAVX, use_avx_limit);
 681     FLAG_SET_DEFAULT(UseAVX, use_avx_limit);
 682   } else if (UseAVX < 0) {
 683     warning("UseAVX=%d is not valid, setting it to UseAVX=0", (int) UseAVX);
 684     FLAG_SET_DEFAULT(UseAVX, 0);
 685   }
 686 
 687   if (UseAVX < 3) {
 688     _features &= ~CPU_AVX512F;
 689     _features &= ~CPU_AVX512DQ;
 690     _features &= ~CPU_AVX512CD;
 691     _features &= ~CPU_AVX512BW;
 692     _features &= ~CPU_AVX512VL;
 693     _features &= ~CPU_AVX512_VPOPCNTDQ;
 694     _features &= ~CPU_AVX512_VPCLMULQDQ;
 695     _features &= ~CPU_VAES;
 696     _features &= ~CPU_VNNI;
 697     _features &= ~CPU_VBMI2;
 698   }
 699 
 700   if (UseAVX < 2)
 701     _features &= ~CPU_AVX2;
 702 
 703   if (UseAVX < 1) {
 704     _features &= ~CPU_AVX;
 705     _features &= ~CPU_VZEROUPPER;
 706   }
 707 
 708   if (logical_processors_per_package() == 1) {
 709     // HT processor could be installed on a system which doesn't support HT.
 710     _features &= ~CPU_HT;
 711   }
 712 
 713   if (is_intel()) { // Intel cpus specific settings
 714     if (is_knights_family()) {
 715       _features &= ~CPU_VZEROUPPER;
 716     }
 717   }
 718 
 719   char buf[256];
 720   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%s%s%s",
 721                cores_per_cpu(), threads_per_core(),
 722                cpu_family(), _model, _stepping,
 723                (supports_cmov() ? ", cmov" : ""),
 724                (supports_cmpxchg8() ? ", cx8" : ""),
 725                (supports_fxsr() ? ", fxsr" : ""),
 726                (supports_mmx()  ? ", mmx"  : ""),
 727                (supports_sse()  ? ", sse"  : ""),
 728                (supports_sse2() ? ", sse2" : ""),
 729                (supports_sse3() ? ", sse3" : ""),
 730                (supports_ssse3()? ", ssse3": ""),
 731                (supports_sse4_1() ? ", sse4.1" : ""),
 732                (supports_sse4_2() ? ", sse4.2" : ""),
 733                (supports_popcnt() ? ", popcnt" : ""),
 734                (supports_avx()    ? ", avx" : ""),
 735                (supports_avx2()   ? ", avx2" : ""),
 736                (supports_aes()    ? ", aes" : ""),
 737                (supports_clmul()  ? ", clmul" : ""),
 738                (supports_erms()   ? ", erms" : ""),
 739                (supports_rtm()    ? ", rtm" : ""),
 740                (supports_mmx_ext() ? ", mmxext" : ""),
 741                (supports_3dnow_prefetch() ? ", 3dnowpref" : ""),
 742                (supports_lzcnt()   ? ", lzcnt": ""),
 743                (supports_sse4a()   ? ", sse4a": ""),
 744                (supports_ht() ? ", ht": ""),
 745                (supports_tsc() ? ", tsc": ""),
 746                (supports_tscinv_bit() ? ", tscinvbit": ""),
 747                (supports_tscinv() ? ", tscinv": ""),
 748                (supports_bmi1() ? ", bmi1" : ""),
 749                (supports_bmi2() ? ", bmi2" : ""),
 750                (supports_adx() ? ", adx" : ""),
 751                (supports_evex() ? ", evex" : ""),
 752                (supports_sha() ? ", sha" : ""),
 753                (supports_fma() ? ", fma" : ""),
 754                (supports_vbmi2() ? ", vbmi2" : ""),
 755                (supports_vaes() ? ", vaes" : ""),
 756                (supports_vnni() ? ", vnni" : ""));
 757   _features_string = os::strdup(buf);
 758 
 759   // UseSSE is set to the smaller of what hardware supports and what
 760   // the command line requires.  I.e., you cannot set UseSSE to 2 on
 761   // older Pentiums which do not support it.
 762   int use_sse_limit = 0;
 763   if (UseSSE > 0) {
 764     if (UseSSE > 3 && supports_sse4_1()) {
 765       use_sse_limit = 4;
 766     } else if (UseSSE > 2 && supports_sse3()) {
 767       use_sse_limit = 3;
 768     } else if (UseSSE > 1 && supports_sse2()) {
 769       use_sse_limit = 2;
 770     } else if (UseSSE > 0 && supports_sse()) {
 771       use_sse_limit = 1;
 772     } else {
 773       use_sse_limit = 0;
 774     }
 775   }
 776   if (FLAG_IS_DEFAULT(UseSSE)) {
 777     FLAG_SET_DEFAULT(UseSSE, use_sse_limit);
 778   } else if (UseSSE > use_sse_limit) {
 779     warning("UseSSE=%d is not supported on this CPU, setting it to UseSSE=%d", (int) UseSSE, use_sse_limit);
 780     FLAG_SET_DEFAULT(UseSSE, use_sse_limit);
 781   } else if (UseSSE < 0) {
 782     warning("UseSSE=%d is not valid, setting it to UseSSE=0", (int) UseSSE);
 783     FLAG_SET_DEFAULT(UseSSE, 0);
 784   }
 785 
 786   // Use AES instructions if available.
 787   if (supports_aes()) {
 788     if (FLAG_IS_DEFAULT(UseAES)) {
 789       FLAG_SET_DEFAULT(UseAES, true);
 790     }
 791     if (!UseAES) {
 792       if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 793         warning("AES intrinsics require UseAES flag to be enabled. Intrinsics will be disabled.");
 794       }
 795       FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 796     } else {
 797       if (UseSSE > 2) {
 798         if (FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 799           FLAG_SET_DEFAULT(UseAESIntrinsics, true);
 800         }
 801       } else {
 802         // The AES intrinsic stubs require AES instruction support (of course)
 803         // but also require sse3 mode or higher for instructions it use.
 804         if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 805           warning("X86 AES intrinsics require SSE3 instructions or higher. Intrinsics will be disabled.");
 806         }
 807         FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 808       }
 809 
 810       // --AES-CTR begins--
 811       if (!UseAESIntrinsics) {
 812         if (UseAESCTRIntrinsics && !FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 813           warning("AES-CTR intrinsics require UseAESIntrinsics flag to be enabled. Intrinsics will be disabled.");
 814           FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 815         }
 816       } else {
 817         if (supports_sse4_1()) {
 818           if (FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 819             FLAG_SET_DEFAULT(UseAESCTRIntrinsics, true);
 820           }
 821         } else {
 822            // The AES-CTR intrinsic stubs require AES instruction support (of course)
 823            // but also require sse4.1 mode or higher for instructions it use.
 824           if (UseAESCTRIntrinsics && !FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 825              warning("X86 AES-CTR intrinsics require SSE4.1 instructions or higher. Intrinsics will be disabled.");
 826            }
 827            FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 828         }
 829       }
 830       // --AES-CTR ends--
 831     }
 832   } else if (UseAES || UseAESIntrinsics || UseAESCTRIntrinsics) {
 833     if (UseAES && !FLAG_IS_DEFAULT(UseAES)) {
 834       warning("AES instructions are not available on this CPU");
 835       FLAG_SET_DEFAULT(UseAES, false);
 836     }
 837     if (UseAESIntrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
 838       warning("AES intrinsics are not available on this CPU");
 839       FLAG_SET_DEFAULT(UseAESIntrinsics, false);
 840     }
 841     if (UseAESCTRIntrinsics && !FLAG_IS_DEFAULT(UseAESCTRIntrinsics)) {
 842       warning("AES-CTR intrinsics are not available on this CPU");
 843       FLAG_SET_DEFAULT(UseAESCTRIntrinsics, false);
 844     }
 845   }
 846 
 847   // Use CLMUL instructions if available.
 848   if (supports_clmul()) {
 849     if (FLAG_IS_DEFAULT(UseCLMUL)) {
 850       UseCLMUL = true;
 851     }
 852   } else if (UseCLMUL) {
 853     if (!FLAG_IS_DEFAULT(UseCLMUL))
 854       warning("CLMUL instructions not available on this CPU (AVX may also be required)");
 855     FLAG_SET_DEFAULT(UseCLMUL, false);
 856   }
 857 
 858   if (UseCLMUL && (UseSSE > 2)) {
 859     if (FLAG_IS_DEFAULT(UseCRC32Intrinsics)) {
 860       UseCRC32Intrinsics = true;
 861     }
 862   } else if (UseCRC32Intrinsics) {
 863     if (!FLAG_IS_DEFAULT(UseCRC32Intrinsics))
 864       warning("CRC32 Intrinsics requires CLMUL instructions (not available on this CPU)");
 865     FLAG_SET_DEFAULT(UseCRC32Intrinsics, false);
 866   }
 867 
 868   if (supports_sse4_2() && supports_clmul()) {
 869     if (FLAG_IS_DEFAULT(UseCRC32CIntrinsics)) {
 870       UseCRC32CIntrinsics = true;
 871     }
 872   } else if (UseCRC32CIntrinsics) {
 873     if (!FLAG_IS_DEFAULT(UseCRC32CIntrinsics)) {
 874       warning("CRC32C intrinsics are not available on this CPU");
 875     }
 876     FLAG_SET_DEFAULT(UseCRC32CIntrinsics, false);
 877   }
 878 
 879   // GHASH/GCM intrinsics
 880   if (UseCLMUL && (UseSSE > 2)) {
 881     if (FLAG_IS_DEFAULT(UseGHASHIntrinsics)) {
 882       UseGHASHIntrinsics = true;
 883     }
 884   } else if (UseGHASHIntrinsics) {
 885     if (!FLAG_IS_DEFAULT(UseGHASHIntrinsics))
 886       warning("GHASH intrinsic requires CLMUL and SSE2 instructions on this CPU");
 887     FLAG_SET_DEFAULT(UseGHASHIntrinsics, false);
 888   }
 889 
 890   // Base64 Intrinsics (Check the condition for which the intrinsic will be active)
 891   if ((UseAVX > 2) && supports_avx512vl() && supports_avx512bw()) {
 892     if (FLAG_IS_DEFAULT(UseBASE64Intrinsics)) {
 893       UseBASE64Intrinsics = true;
 894     }
 895   } else if (UseBASE64Intrinsics) {
 896      if (!FLAG_IS_DEFAULT(UseBASE64Intrinsics))
 897       warning("Base64 intrinsic requires EVEX instructions on this CPU");
 898     FLAG_SET_DEFAULT(UseBASE64Intrinsics, false);
 899   }
 900 
 901   if (supports_fma() && UseSSE >= 2) { // Check UseSSE since FMA code uses SSE instructions
 902     if (FLAG_IS_DEFAULT(UseFMA)) {
 903       UseFMA = true;
 904     }
 905   } else if (UseFMA) {
 906     warning("FMA instructions are not available on this CPU");
 907     FLAG_SET_DEFAULT(UseFMA, false);
 908   }
 909 
 910   if (supports_sha() LP64_ONLY(|| supports_avx2() && supports_bmi2())) {
 911     if (FLAG_IS_DEFAULT(UseSHA)) {
 912       UseSHA = true;
 913     }
 914   } else if (UseSHA) {
 915     warning("SHA instructions are not available on this CPU");
 916     FLAG_SET_DEFAULT(UseSHA, false);
 917   }
 918 
 919   if (supports_sha() && supports_sse4_1() && UseSHA) {
 920     if (FLAG_IS_DEFAULT(UseSHA1Intrinsics)) {
 921       FLAG_SET_DEFAULT(UseSHA1Intrinsics, true);
 922     }
 923   } else if (UseSHA1Intrinsics) {
 924     warning("Intrinsics for SHA-1 crypto hash functions not available on this CPU.");
 925     FLAG_SET_DEFAULT(UseSHA1Intrinsics, false);
 926   }
 927 
 928   if (supports_sse4_1() && UseSHA) {
 929     if (FLAG_IS_DEFAULT(UseSHA256Intrinsics)) {
 930       FLAG_SET_DEFAULT(UseSHA256Intrinsics, true);
 931     }
 932   } else if (UseSHA256Intrinsics) {
 933     warning("Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.");
 934     FLAG_SET_DEFAULT(UseSHA256Intrinsics, false);
 935   }
 936 
 937 #ifdef _LP64
 938   // These are only supported on 64-bit
 939   if (UseSHA && supports_avx2() && supports_bmi2()) {
 940     if (FLAG_IS_DEFAULT(UseSHA512Intrinsics)) {
 941       FLAG_SET_DEFAULT(UseSHA512Intrinsics, true);
 942     }
 943   } else
 944 #endif
 945   if (UseSHA512Intrinsics) {
 946     warning("Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.");
 947     FLAG_SET_DEFAULT(UseSHA512Intrinsics, false);
 948   }
 949 
 950   if (!(UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics)) {
 951     FLAG_SET_DEFAULT(UseSHA, false);
 952   }
 953 
 954   if (UseAdler32Intrinsics) {
 955     warning("Adler32Intrinsics not available on this CPU.");
 956     FLAG_SET_DEFAULT(UseAdler32Intrinsics, false);
 957   }
 958 
 959   if (!supports_rtm() && UseRTMLocking) {
 960     // Can't continue because UseRTMLocking affects UseBiasedLocking flag
 961     // setting during arguments processing. See use_biased_locking().
 962     // VM_Version_init() is executed after UseBiasedLocking is used
 963     // in Thread::allocate().
 964     vm_exit_during_initialization("RTM instructions are not available on this CPU");
 965   }
 966 
 967 #if INCLUDE_RTM_OPT
 968   if (UseRTMLocking) {
 969     if (is_client_compilation_mode_vm()) {
 970       // Only C2 does RTM locking optimization.
 971       // Can't continue because UseRTMLocking affects UseBiasedLocking flag
 972       // setting during arguments processing. See use_biased_locking().
 973       vm_exit_during_initialization("RTM locking optimization is not supported in this VM");
 974     }
 975     if (is_intel_family_core()) {
 976       if ((_model == CPU_MODEL_HASWELL_E3) ||
 977           (_model == CPU_MODEL_HASWELL_E7 && _stepping < 3) ||
 978           (_model == CPU_MODEL_BROADWELL  && _stepping < 4)) {
 979         // currently a collision between SKL and HSW_E3
 980         if (!UnlockExperimentalVMOptions && UseAVX < 3) {
 981           vm_exit_during_initialization("UseRTMLocking is only available as experimental option on this "
 982                                         "platform. It must be enabled via -XX:+UnlockExperimentalVMOptions flag.");
 983         } else {
 984           warning("UseRTMLocking is only available as experimental option on this platform.");
 985         }
 986       }
 987     }
 988     if (!FLAG_IS_CMDLINE(UseRTMLocking)) {
 989       // RTM locking should be used only for applications with
 990       // high lock contention. For now we do not use it by default.
 991       vm_exit_during_initialization("UseRTMLocking flag should be only set on command line");
 992     }
 993   } else { // !UseRTMLocking
 994     if (UseRTMForStackLocks) {
 995       if (!FLAG_IS_DEFAULT(UseRTMForStackLocks)) {
 996         warning("UseRTMForStackLocks flag should be off when UseRTMLocking flag is off");
 997       }
 998       FLAG_SET_DEFAULT(UseRTMForStackLocks, false);
 999     }
1000     if (UseRTMDeopt) {
1001       FLAG_SET_DEFAULT(UseRTMDeopt, false);
1002     }
1003     if (PrintPreciseRTMLockingStatistics) {
1004       FLAG_SET_DEFAULT(PrintPreciseRTMLockingStatistics, false);
1005     }
1006   }
1007 #else
1008   if (UseRTMLocking) {
1009     // Only C2 does RTM locking optimization.
1010     // Can't continue because UseRTMLocking affects UseBiasedLocking flag
1011     // setting during arguments processing. See use_biased_locking().
1012     vm_exit_during_initialization("RTM locking optimization is not supported in this VM");
1013   }
1014 #endif
1015 
1016 #ifdef COMPILER2
1017   if (UseFPUForSpilling) {
1018     if (UseSSE < 2) {
1019       // Only supported with SSE2+
1020       FLAG_SET_DEFAULT(UseFPUForSpilling, false);
1021     }
1022   }
1023 #endif
1024 
1025 #if COMPILER2_OR_JVMCI
1026   int max_vector_size = 0;
1027   if (UseSSE < 2) {
1028     // Vectors (in XMM) are only supported with SSE2+
1029     // SSE is always 2 on x64.
1030     max_vector_size = 0;
1031   } else if (UseAVX == 0 || !os_supports_avx_vectors()) {
1032     // 16 byte vectors (in XMM) are supported with SSE2+
1033     max_vector_size = 16;
1034   } else if (UseAVX == 1 || UseAVX == 2) {
1035     // 32 bytes vectors (in YMM) are only supported with AVX+
1036     max_vector_size = 32;
1037   } else if (UseAVX > 2) {
1038     // 64 bytes vectors (in ZMM) are only supported with AVX 3
1039     max_vector_size = 64;
1040   }
1041 
1042 #ifdef _LP64
1043   int min_vector_size = 4; // We require MaxVectorSize to be at least 4 on 64bit
1044 #else
1045   int min_vector_size = 0;
1046 #endif
1047 
1048   if (!FLAG_IS_DEFAULT(MaxVectorSize)) {
1049     if (MaxVectorSize < min_vector_size) {
1050       warning("MaxVectorSize must be at least %i on this platform", min_vector_size);
1051       FLAG_SET_DEFAULT(MaxVectorSize, min_vector_size);
1052     }
1053     if (MaxVectorSize > max_vector_size) {
1054       warning("MaxVectorSize must be at most %i on this platform", max_vector_size);
1055       FLAG_SET_DEFAULT(MaxVectorSize, max_vector_size);
1056     }
1057     if (!is_power_of_2(MaxVectorSize)) {
1058       warning("MaxVectorSize must be a power of 2, setting to default: %i", max_vector_size);
1059       FLAG_SET_DEFAULT(MaxVectorSize, max_vector_size);
1060     }
1061   } else {
1062     // If default, use highest supported configuration
1063     FLAG_SET_DEFAULT(MaxVectorSize, max_vector_size);
1064   }
1065 
1066 #if defined(COMPILER2) && defined(ASSERT)
1067   if (MaxVectorSize > 0) {
1068     if (supports_avx() && PrintMiscellaneous && Verbose && TraceNewVectors) {
1069       tty->print_cr("State of YMM registers after signal handle:");
1070       int nreg = 2 LP64_ONLY(+2);
1071       const char* ymm_name[4] = {"0", "7", "8", "15"};
1072       for (int i = 0; i < nreg; i++) {
1073         tty->print("YMM%s:", ymm_name[i]);
1074         for (int j = 7; j >=0; j--) {
1075           tty->print(" %x", _cpuid_info.ymm_save[i*8 + j]);
1076         }
1077         tty->cr();
1078       }
1079     }
1080   }
1081 #endif // COMPILER2 && ASSERT
1082 
1083   if (!FLAG_IS_DEFAULT(AVX3Threshold)) {
1084     if (!is_power_of_2(AVX3Threshold)) {
1085       warning("AVX3Threshold must be a power of 2");
1086       FLAG_SET_DEFAULT(AVX3Threshold, 4096);
1087     }
1088   }
1089 
1090 #ifdef _LP64
1091   if (FLAG_IS_DEFAULT(UseMultiplyToLenIntrinsic)) {
1092     UseMultiplyToLenIntrinsic = true;
1093   }
1094   if (FLAG_IS_DEFAULT(UseSquareToLenIntrinsic)) {
1095     UseSquareToLenIntrinsic = true;
1096   }
1097   if (FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {
1098     UseMulAddIntrinsic = true;
1099   }
1100   if (FLAG_IS_DEFAULT(UseMontgomeryMultiplyIntrinsic)) {
1101     UseMontgomeryMultiplyIntrinsic = true;
1102   }
1103   if (FLAG_IS_DEFAULT(UseMontgomerySquareIntrinsic)) {
1104     UseMontgomerySquareIntrinsic = true;
1105   }
1106 #else
1107   if (UseMultiplyToLenIntrinsic) {
1108     if (!FLAG_IS_DEFAULT(UseMultiplyToLenIntrinsic)) {
1109       warning("multiplyToLen intrinsic is not available in 32-bit VM");
1110     }
1111     FLAG_SET_DEFAULT(UseMultiplyToLenIntrinsic, false);
1112   }
1113   if (UseMontgomeryMultiplyIntrinsic) {
1114     if (!FLAG_IS_DEFAULT(UseMontgomeryMultiplyIntrinsic)) {
1115       warning("montgomeryMultiply intrinsic is not available in 32-bit VM");
1116     }
1117     FLAG_SET_DEFAULT(UseMontgomeryMultiplyIntrinsic, false);
1118   }
1119   if (UseMontgomerySquareIntrinsic) {
1120     if (!FLAG_IS_DEFAULT(UseMontgomerySquareIntrinsic)) {
1121       warning("montgomerySquare intrinsic is not available in 32-bit VM");
1122     }
1123     FLAG_SET_DEFAULT(UseMontgomerySquareIntrinsic, false);
1124   }
1125   if (UseSquareToLenIntrinsic) {
1126     if (!FLAG_IS_DEFAULT(UseSquareToLenIntrinsic)) {
1127       warning("squareToLen intrinsic is not available in 32-bit VM");
1128     }
1129     FLAG_SET_DEFAULT(UseSquareToLenIntrinsic, false);
1130   }
1131   if (UseMulAddIntrinsic) {
1132     if (!FLAG_IS_DEFAULT(UseMulAddIntrinsic)) {
1133       warning("mulAdd intrinsic is not available in 32-bit VM");
1134     }
1135     FLAG_SET_DEFAULT(UseMulAddIntrinsic, false);
1136   }
1137 #endif // _LP64
1138 #endif // COMPILER2_OR_JVMCI
1139 
1140   // On new cpus instructions which update whole XMM register should be used
1141   // to prevent partial register stall due to dependencies on high half.
1142   //
1143   // UseXmmLoadAndClearUpper == true  --> movsd(xmm, mem)
1144   // UseXmmLoadAndClearUpper == false --> movlpd(xmm, mem)
1145   // UseXmmRegToRegMoveAll == true  --> movaps(xmm, xmm), movapd(xmm, xmm).
1146   // UseXmmRegToRegMoveAll == false --> movss(xmm, xmm),  movsd(xmm, xmm).
1147 
1148 
1149   if (is_zx()) { // ZX cpus specific settings
1150     if (FLAG_IS_DEFAULT(UseStoreImmI16)) {
1151       UseStoreImmI16 = false; // don't use it on ZX cpus
1152     }
1153     if ((cpu_family() == 6) || (cpu_family() == 7)) {
1154       if (FLAG_IS_DEFAULT(UseAddressNop)) {
1155         // Use it on all ZX cpus
1156         UseAddressNop = true;
1157       }
1158     }
1159     if (FLAG_IS_DEFAULT(UseXmmLoadAndClearUpper)) {
1160       UseXmmLoadAndClearUpper = true; // use movsd on all ZX cpus
1161     }
1162     if (FLAG_IS_DEFAULT(UseXmmRegToRegMoveAll)) {
1163       if (supports_sse3()) {
1164         UseXmmRegToRegMoveAll = true; // use movaps, movapd on new ZX cpus
1165       } else {
1166         UseXmmRegToRegMoveAll = false;
1167       }
1168     }
1169     if (((cpu_family() == 6) || (cpu_family() == 7)) && supports_sse3()) { // new ZX cpus
1170 #ifdef COMPILER2
1171       if (FLAG_IS_DEFAULT(MaxLoopPad)) {
1172         // For new ZX cpus do the next optimization:
1173         // don't align the beginning of a loop if there are enough instructions
1174         // left (NumberOfLoopInstrToAlign defined in c2_globals.hpp)
1175         // in current fetch line (OptoLoopAlignment) or the padding
1176         // is big (> MaxLoopPad).
1177         // Set MaxLoopPad to 11 for new ZX cpus to reduce number of
1178         // generated NOP instructions. 11 is the largest size of one
1179         // address NOP instruction '0F 1F' (see Assembler::nop(i)).
1180         MaxLoopPad = 11;
1181       }
1182 #endif // COMPILER2
1183       if (FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
1184         UseXMMForArrayCopy = true; // use SSE2 movq on new ZX cpus
1185       }
1186       if (supports_sse4_2()) { // new ZX cpus
1187         if (FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1188           UseUnalignedLoadStores = true; // use movdqu on newest ZX cpus
1189         }
1190       }
1191       if (supports_sse4_2()) {
1192         if (FLAG_IS_DEFAULT(UseSSE42Intrinsics)) {
1193           FLAG_SET_DEFAULT(UseSSE42Intrinsics, true);
1194         }
1195       } else {
1196         if (UseSSE42Intrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
1197           warning("SSE4.2 intrinsics require SSE4.2 instructions or higher. Intrinsics will be disabled.");
1198         }
1199         FLAG_SET_DEFAULT(UseSSE42Intrinsics, false);
1200       }
1201     }
1202 
1203     if (FLAG_IS_DEFAULT(AllocatePrefetchInstr) && supports_3dnow_prefetch()) {
1204       FLAG_SET_DEFAULT(AllocatePrefetchInstr, 3);
1205     }
1206   }
1207 
1208   if (is_amd_family()) { // AMD cpus specific settings
1209     if (supports_sse2() && FLAG_IS_DEFAULT(UseAddressNop)) {
1210       // Use it on new AMD cpus starting from Opteron.
1211       UseAddressNop = true;
1212     }
1213     if (supports_sse2() && FLAG_IS_DEFAULT(UseNewLongLShift)) {
1214       // Use it on new AMD cpus starting from Opteron.
1215       UseNewLongLShift = true;
1216     }
1217     if (FLAG_IS_DEFAULT(UseXmmLoadAndClearUpper)) {
1218       if (supports_sse4a()) {
1219         UseXmmLoadAndClearUpper = true; // use movsd only on '10h' Opteron
1220       } else {
1221         UseXmmLoadAndClearUpper = false;
1222       }
1223     }
1224     if (FLAG_IS_DEFAULT(UseXmmRegToRegMoveAll)) {
1225       if (supports_sse4a()) {
1226         UseXmmRegToRegMoveAll = true; // use movaps, movapd only on '10h'
1227       } else {
1228         UseXmmRegToRegMoveAll = false;
1229       }
1230     }
1231     if (FLAG_IS_DEFAULT(UseXmmI2F)) {
1232       if (supports_sse4a()) {
1233         UseXmmI2F = true;
1234       } else {
1235         UseXmmI2F = false;
1236       }
1237     }
1238     if (FLAG_IS_DEFAULT(UseXmmI2D)) {
1239       if (supports_sse4a()) {
1240         UseXmmI2D = true;
1241       } else {
1242         UseXmmI2D = false;
1243       }
1244     }
1245     if (supports_sse4_2()) {
1246       if (FLAG_IS_DEFAULT(UseSSE42Intrinsics)) {
1247         FLAG_SET_DEFAULT(UseSSE42Intrinsics, true);
1248       }
1249     } else {
1250       if (UseSSE42Intrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
1251         warning("SSE4.2 intrinsics require SSE4.2 instructions or higher. Intrinsics will be disabled.");
1252       }
1253       FLAG_SET_DEFAULT(UseSSE42Intrinsics, false);
1254     }
1255 
1256     // some defaults for AMD family 15h
1257     if (cpu_family() == 0x15) {
1258       // On family 15h processors default is no sw prefetch
1259       if (FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
1260         FLAG_SET_DEFAULT(AllocatePrefetchStyle, 0);
1261       }
1262       // Also, if some other prefetch style is specified, default instruction type is PREFETCHW
1263       if (FLAG_IS_DEFAULT(AllocatePrefetchInstr)) {
1264         FLAG_SET_DEFAULT(AllocatePrefetchInstr, 3);
1265       }
1266       // On family 15h processors use XMM and UnalignedLoadStores for Array Copy
1267       if (supports_sse2() && FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
1268         FLAG_SET_DEFAULT(UseXMMForArrayCopy, true);
1269       }
1270       if (supports_sse2() && FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1271         FLAG_SET_DEFAULT(UseUnalignedLoadStores, true);
1272       }
1273     }
1274 
1275 #ifdef COMPILER2
1276     if (cpu_family() < 0x17 && MaxVectorSize > 16) {
1277       // Limit vectors size to 16 bytes on AMD cpus < 17h.
1278       FLAG_SET_DEFAULT(MaxVectorSize, 16);
1279     }
1280 #endif // COMPILER2
1281 
1282     // Some defaults for AMD family 17h || Hygon family 18h
1283     if (cpu_family() == 0x17 || cpu_family() == 0x18) {
1284       // On family 17h processors use XMM and UnalignedLoadStores for Array Copy
1285       if (supports_sse2() && FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
1286         FLAG_SET_DEFAULT(UseXMMForArrayCopy, true);
1287       }
1288       if (supports_sse2() && FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1289         FLAG_SET_DEFAULT(UseUnalignedLoadStores, true);
1290       }
1291 #ifdef COMPILER2
1292       if (supports_sse4_2() && FLAG_IS_DEFAULT(UseFPUForSpilling)) {
1293         FLAG_SET_DEFAULT(UseFPUForSpilling, true);
1294       }
1295 #endif
1296     }
1297   }
1298 
1299   if (is_intel()) { // Intel cpus specific settings
1300     if (FLAG_IS_DEFAULT(UseStoreImmI16)) {
1301       UseStoreImmI16 = false; // don't use it on Intel cpus
1302     }
1303     if (cpu_family() == 6 || cpu_family() == 15) {
1304       if (FLAG_IS_DEFAULT(UseAddressNop)) {
1305         // Use it on all Intel cpus starting from PentiumPro
1306         UseAddressNop = true;
1307       }
1308     }
1309     if (FLAG_IS_DEFAULT(UseXmmLoadAndClearUpper)) {
1310       UseXmmLoadAndClearUpper = true; // use movsd on all Intel cpus
1311     }
1312     if (FLAG_IS_DEFAULT(UseXmmRegToRegMoveAll)) {
1313       if (supports_sse3()) {
1314         UseXmmRegToRegMoveAll = true; // use movaps, movapd on new Intel cpus
1315       } else {
1316         UseXmmRegToRegMoveAll = false;
1317       }
1318     }
1319     if (cpu_family() == 6 && supports_sse3()) { // New Intel cpus
1320 #ifdef COMPILER2
1321       if (FLAG_IS_DEFAULT(MaxLoopPad)) {
1322         // For new Intel cpus do the next optimization:
1323         // don't align the beginning of a loop if there are enough instructions
1324         // left (NumberOfLoopInstrToAlign defined in c2_globals.hpp)
1325         // in current fetch line (OptoLoopAlignment) or the padding
1326         // is big (> MaxLoopPad).
1327         // Set MaxLoopPad to 11 for new Intel cpus to reduce number of
1328         // generated NOP instructions. 11 is the largest size of one
1329         // address NOP instruction '0F 1F' (see Assembler::nop(i)).
1330         MaxLoopPad = 11;
1331       }
1332 #endif // COMPILER2
1333       if (FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
1334         UseXMMForArrayCopy = true; // use SSE2 movq on new Intel cpus
1335       }
1336       if ((supports_sse4_2() && supports_ht()) || supports_avx()) { // Newest Intel cpus
1337         if (FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1338           UseUnalignedLoadStores = true; // use movdqu on newest Intel cpus
1339         }
1340       }
1341       if (supports_sse4_2()) {
1342         if (FLAG_IS_DEFAULT(UseSSE42Intrinsics)) {
1343           FLAG_SET_DEFAULT(UseSSE42Intrinsics, true);
1344         }
1345       } else {
1346         if (UseSSE42Intrinsics && !FLAG_IS_DEFAULT(UseAESIntrinsics)) {
1347           warning("SSE4.2 intrinsics require SSE4.2 instructions or higher. Intrinsics will be disabled.");
1348         }
1349         FLAG_SET_DEFAULT(UseSSE42Intrinsics, false);
1350       }
1351     }
1352     if (is_atom_family() || is_knights_family()) {
1353 #ifdef COMPILER2
1354       if (FLAG_IS_DEFAULT(OptoScheduling)) {
1355         OptoScheduling = true;
1356       }
1357 #endif
1358       if (supports_sse4_2()) { // Silvermont
1359         if (FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
1360           UseUnalignedLoadStores = true; // use movdqu on newest Intel cpus
1361         }
1362       }
1363       if (FLAG_IS_DEFAULT(UseIncDec)) {
1364         FLAG_SET_DEFAULT(UseIncDec, false);
1365       }
1366     }
1367     if (FLAG_IS_DEFAULT(AllocatePrefetchInstr) && supports_3dnow_prefetch()) {
1368       FLAG_SET_DEFAULT(AllocatePrefetchInstr, 3);
1369     }
1370   }
1371 
1372 #ifdef _LP64
1373   if (UseSSE42Intrinsics) {
1374     if (FLAG_IS_DEFAULT(UseVectorizedMismatchIntrinsic)) {
1375       UseVectorizedMismatchIntrinsic = true;
1376     }
1377   } else if (UseVectorizedMismatchIntrinsic) {
1378     if (!FLAG_IS_DEFAULT(UseVectorizedMismatchIntrinsic))
1379       warning("vectorizedMismatch intrinsics are not available on this CPU");
1380     FLAG_SET_DEFAULT(UseVectorizedMismatchIntrinsic, false);
1381   }
1382 #else
1383   if (UseVectorizedMismatchIntrinsic) {
1384     if (!FLAG_IS_DEFAULT(UseVectorizedMismatchIntrinsic)) {
1385       warning("vectorizedMismatch intrinsic is not available in 32-bit VM");
1386     }
1387     FLAG_SET_DEFAULT(UseVectorizedMismatchIntrinsic, false);
1388   }
1389 #endif // _LP64
1390 
1391   // Use count leading zeros count instruction if available.
1392   if (supports_lzcnt()) {
1393     if (FLAG_IS_DEFAULT(UseCountLeadingZerosInstruction)) {
1394       UseCountLeadingZerosInstruction = true;
1395     }
1396    } else if (UseCountLeadingZerosInstruction) {
1397     warning("lzcnt instruction is not available on this CPU");
1398     FLAG_SET_DEFAULT(UseCountLeadingZerosInstruction, false);
1399   }
1400 
1401   // Use count trailing zeros instruction if available
1402   if (supports_bmi1()) {
1403     // tzcnt does not require VEX prefix
1404     if (FLAG_IS_DEFAULT(UseCountTrailingZerosInstruction)) {
1405       if (!UseBMI1Instructions && !FLAG_IS_DEFAULT(UseBMI1Instructions)) {
1406         // Don't use tzcnt if BMI1 is switched off on command line.
1407         UseCountTrailingZerosInstruction = false;
1408       } else {
1409         UseCountTrailingZerosInstruction = true;
1410       }
1411     }
1412   } else if (UseCountTrailingZerosInstruction) {
1413     warning("tzcnt instruction is not available on this CPU");
1414     FLAG_SET_DEFAULT(UseCountTrailingZerosInstruction, false);
1415   }
1416 
1417   // BMI instructions (except tzcnt) use an encoding with VEX prefix.
1418   // VEX prefix is generated only when AVX > 0.
1419   if (supports_bmi1() && supports_avx()) {
1420     if (FLAG_IS_DEFAULT(UseBMI1Instructions)) {
1421       UseBMI1Instructions = true;
1422     }
1423   } else if (UseBMI1Instructions) {
1424     warning("BMI1 instructions are not available on this CPU (AVX is also required)");
1425     FLAG_SET_DEFAULT(UseBMI1Instructions, false);
1426   }
1427 
1428   if (supports_bmi2() && supports_avx()) {
1429     if (FLAG_IS_DEFAULT(UseBMI2Instructions)) {
1430       UseBMI2Instructions = true;
1431     }
1432   } else if (UseBMI2Instructions) {
1433     warning("BMI2 instructions are not available on this CPU (AVX is also required)");
1434     FLAG_SET_DEFAULT(UseBMI2Instructions, false);
1435   }
1436 
1437   // Use population count instruction if available.
1438   if (supports_popcnt()) {
1439     if (FLAG_IS_DEFAULT(UsePopCountInstruction)) {
1440       UsePopCountInstruction = true;
1441     }
1442   } else if (UsePopCountInstruction) {
1443     warning("POPCNT instruction is not available on this CPU");
1444     FLAG_SET_DEFAULT(UsePopCountInstruction, false);
1445   }
1446 
1447   // Use fast-string operations if available.
1448   if (supports_erms()) {
1449     if (FLAG_IS_DEFAULT(UseFastStosb)) {
1450       UseFastStosb = true;
1451     }
1452   } else if (UseFastStosb) {
1453     warning("fast-string operations are not available on this CPU");
1454     FLAG_SET_DEFAULT(UseFastStosb, false);
1455   }
1456 
1457   // Use XMM/YMM MOVDQU instruction for Object Initialization
1458   if (!UseFastStosb && UseSSE >= 2 && UseUnalignedLoadStores) {
1459     if (FLAG_IS_DEFAULT(UseXMMForObjInit)) {
1460       UseXMMForObjInit = true;
1461     }
1462   } else if (UseXMMForObjInit) {
1463     warning("UseXMMForObjInit requires SSE2 and unaligned load/stores. Feature is switched off.");
1464     FLAG_SET_DEFAULT(UseXMMForObjInit, false);
1465   }
1466 
1467 #ifdef COMPILER2
1468   if (FLAG_IS_DEFAULT(AlignVector)) {
1469     // Modern processors allow misaligned memory operations for vectors.
1470     AlignVector = !UseUnalignedLoadStores;
1471   }
1472 #endif // COMPILER2
1473 
1474   if (FLAG_IS_DEFAULT(AllocatePrefetchInstr)) {
1475     if (AllocatePrefetchInstr == 3 && !supports_3dnow_prefetch()) {
1476       FLAG_SET_DEFAULT(AllocatePrefetchInstr, 0);
1477     } else if (!supports_sse() && supports_3dnow_prefetch()) {
1478       FLAG_SET_DEFAULT(AllocatePrefetchInstr, 3);
1479     }
1480   }
1481 
1482   // Allocation prefetch settings
1483   intx cache_line_size = prefetch_data_size();
1484   if (FLAG_IS_DEFAULT(AllocatePrefetchStepSize) &&
1485       (cache_line_size > AllocatePrefetchStepSize)) {
1486     FLAG_SET_DEFAULT(AllocatePrefetchStepSize, cache_line_size);
1487   }
1488 
1489   if ((AllocatePrefetchDistance == 0) && (AllocatePrefetchStyle != 0)) {
1490     assert(!FLAG_IS_DEFAULT(AllocatePrefetchDistance), "default value should not be 0");
1491     if (!FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
1492       warning("AllocatePrefetchDistance is set to 0 which disable prefetching. Ignoring AllocatePrefetchStyle flag.");
1493     }
1494     FLAG_SET_DEFAULT(AllocatePrefetchStyle, 0);
1495   }
1496 
1497   if (FLAG_IS_DEFAULT(AllocatePrefetchDistance)) {
1498     bool use_watermark_prefetch = (AllocatePrefetchStyle == 2);
1499     FLAG_SET_DEFAULT(AllocatePrefetchDistance, allocate_prefetch_distance(use_watermark_prefetch));
1500   }
1501 
1502   if (is_intel() && cpu_family() == 6 && supports_sse3()) {
1503     if (FLAG_IS_DEFAULT(AllocatePrefetchLines) &&
1504         supports_sse4_2() && supports_ht()) { // Nehalem based cpus
1505       FLAG_SET_DEFAULT(AllocatePrefetchLines, 4);
1506     }
1507 #ifdef COMPILER2
1508     if (FLAG_IS_DEFAULT(UseFPUForSpilling) && supports_sse4_2()) {
1509       FLAG_SET_DEFAULT(UseFPUForSpilling, true);
1510     }
1511 #endif
1512   }
1513 
1514   if (is_zx() && ((cpu_family() == 6) || (cpu_family() == 7)) && supports_sse4_2()) {
1515 #ifdef COMPILER2
1516     if (FLAG_IS_DEFAULT(UseFPUForSpilling)) {
1517       FLAG_SET_DEFAULT(UseFPUForSpilling, true);
1518     }
1519 #endif
1520   }
1521 
1522 #ifdef _LP64
1523   // Prefetch settings
1524 
1525   // Prefetch interval for gc copy/scan == 9 dcache lines.  Derived from
1526   // 50-warehouse specjbb runs on a 2-way 1.8ghz opteron using a 4gb heap.
1527   // Tested intervals from 128 to 2048 in increments of 64 == one cache line.
1528   // 256 bytes (4 dcache lines) was the nearest runner-up to 576.
1529 
1530   // gc copy/scan is disabled if prefetchw isn't supported, because
1531   // Prefetch::write emits an inlined prefetchw on Linux.
1532   // Do not use the 3dnow prefetchw instruction.  It isn't supported on em64t.
1533   // The used prefetcht0 instruction works for both amd64 and em64t.
1534 
1535   if (FLAG_IS_DEFAULT(PrefetchCopyIntervalInBytes)) {
1536     FLAG_SET_DEFAULT(PrefetchCopyIntervalInBytes, 576);
1537   }
1538   if (FLAG_IS_DEFAULT(PrefetchScanIntervalInBytes)) {
1539     FLAG_SET_DEFAULT(PrefetchScanIntervalInBytes, 576);
1540   }
1541   if (FLAG_IS_DEFAULT(PrefetchFieldsAhead)) {
1542     FLAG_SET_DEFAULT(PrefetchFieldsAhead, 1);
1543   }
1544 #endif
1545 
1546   if (FLAG_IS_DEFAULT(ContendedPaddingWidth) &&
1547      (cache_line_size > ContendedPaddingWidth))
1548      ContendedPaddingWidth = cache_line_size;
1549 
1550   // This machine allows unaligned memory accesses
1551   if (FLAG_IS_DEFAULT(UseUnalignedAccesses)) {
1552     FLAG_SET_DEFAULT(UseUnalignedAccesses, true);
1553   }
1554 
1555 #ifndef PRODUCT
1556   if (log_is_enabled(Info, os, cpu)) {
1557     LogStream ls(Log(os, cpu)::info());
1558     outputStream* log = &ls;
1559     log->print_cr("Logical CPUs per core: %u",
1560                   logical_processors_per_package());
1561     log->print_cr("L1 data cache line size: %u", L1_data_cache_line_size());
1562     log->print("UseSSE=%d", (int) UseSSE);
1563     if (UseAVX > 0) {
1564       log->print("  UseAVX=%d", (int) UseAVX);
1565     }
1566     if (UseAES) {
1567       log->print("  UseAES=1");
1568     }
1569 #ifdef COMPILER2
1570     if (MaxVectorSize > 0) {
1571       log->print("  MaxVectorSize=%d", (int) MaxVectorSize);
1572     }
1573 #endif
1574     log->cr();
1575     log->print("Allocation");
1576     if (AllocatePrefetchStyle <= 0 || (UseSSE == 0 && !supports_3dnow_prefetch())) {
1577       log->print_cr(": no prefetching");
1578     } else {
1579       log->print(" prefetching: ");
1580       if (UseSSE == 0 && supports_3dnow_prefetch()) {
1581         log->print("PREFETCHW");
1582       } else if (UseSSE >= 1) {
1583         if (AllocatePrefetchInstr == 0) {
1584           log->print("PREFETCHNTA");
1585         } else if (AllocatePrefetchInstr == 1) {
1586           log->print("PREFETCHT0");
1587         } else if (AllocatePrefetchInstr == 2) {
1588           log->print("PREFETCHT2");
1589         } else if (AllocatePrefetchInstr == 3) {
1590           log->print("PREFETCHW");
1591         }
1592       }
1593       if (AllocatePrefetchLines > 1) {
1594         log->print_cr(" at distance %d, %d lines of %d bytes", (int) AllocatePrefetchDistance, (int) AllocatePrefetchLines, (int) AllocatePrefetchStepSize);
1595       } else {
1596         log->print_cr(" at distance %d, one line of %d bytes", (int) AllocatePrefetchDistance, (int) AllocatePrefetchStepSize);
1597       }
1598     }
1599 
1600     if (PrefetchCopyIntervalInBytes > 0) {
1601       log->print_cr("PrefetchCopyIntervalInBytes %d", (int) PrefetchCopyIntervalInBytes);
1602     }
1603     if (PrefetchScanIntervalInBytes > 0) {
1604       log->print_cr("PrefetchScanIntervalInBytes %d", (int) PrefetchScanIntervalInBytes);
1605     }
1606     if (PrefetchFieldsAhead > 0) {
1607       log->print_cr("PrefetchFieldsAhead %d", (int) PrefetchFieldsAhead);
1608     }
1609     if (ContendedPaddingWidth > 0) {
1610       log->print_cr("ContendedPaddingWidth %d", (int) ContendedPaddingWidth);
1611     }
1612   }
1613 #endif // !PRODUCT
1614 }
1615 
1616 void VM_Version::print_platform_virtualization_info(outputStream* st) {
1617   VirtualizationType vrt = VM_Version::get_detected_virtualization();
1618   if (vrt == XenHVM) {
1619     st->print_cr("Xen hardware-assisted virtualization detected");
1620   } else if (vrt == KVM) {
1621     st->print_cr("KVM virtualization detected");
1622   } else if (vrt == VMWare) {
1623     st->print_cr("VMWare virtualization detected");
1624     VirtualizationSupport::print_virtualization_info(st);
1625   } else if (vrt == HyperV) {
1626     st->print_cr("HyperV virtualization detected");
1627   }
1628 }
1629 
1630 void VM_Version::check_virt_cpuid(uint32_t idx, uint32_t *regs) {
1631 // TODO support 32 bit
1632 #if defined(_LP64)
1633 #if defined(_MSC_VER)
1634   // Allocate space for the code
1635   const int code_size = 100;
1636   ResourceMark rm;
1637   CodeBuffer cb("detect_virt", code_size, 0);
1638   MacroAssembler* a = new MacroAssembler(&cb);
1639   address code = a->pc();
1640   void (*test)(uint32_t idx, uint32_t *regs) = (void(*)(uint32_t idx, uint32_t *regs))code;
1641 
1642   a->movq(r9, rbx); // save nonvolatile register
1643 
1644   // next line would not work on 32-bit
1645   a->movq(rax, c_rarg0 /* rcx */);
1646   a->movq(r8, c_rarg1 /* rdx */);
1647   a->cpuid();
1648   a->movl(Address(r8,  0), rax);
1649   a->movl(Address(r8,  4), rbx);
1650   a->movl(Address(r8,  8), rcx);
1651   a->movl(Address(r8, 12), rdx);
1652 
1653   a->movq(rbx, r9); // restore nonvolatile register
1654   a->ret(0);
1655 
1656   uint32_t *code_end = (uint32_t *)a->pc();
1657   a->flush();
1658 
1659   // execute code
1660   (*test)(idx, regs);
1661 #elif defined(__GNUC__)
1662   __asm__ volatile (
1663      "        cpuid;"
1664      "        mov %%eax,(%1);"
1665      "        mov %%ebx,4(%1);"
1666      "        mov %%ecx,8(%1);"
1667      "        mov %%edx,12(%1);"
1668      : "+a" (idx)
1669      : "S" (regs)
1670      : "ebx", "ecx", "edx", "memory" );
1671 #endif
1672 #endif
1673 }
1674 
1675 
1676 bool VM_Version::use_biased_locking() {
1677 #if INCLUDE_RTM_OPT
1678   // RTM locking is most useful when there is high lock contention and
1679   // low data contention.  With high lock contention the lock is usually
1680   // inflated and biased locking is not suitable for that case.
1681   // RTM locking code requires that biased locking is off.
1682   // Note: we can't switch off UseBiasedLocking in get_processor_features()
1683   // because it is used by Thread::allocate() which is called before
1684   // VM_Version::initialize().
1685   if (UseRTMLocking && UseBiasedLocking) {
1686     if (FLAG_IS_DEFAULT(UseBiasedLocking)) {
1687       FLAG_SET_DEFAULT(UseBiasedLocking, false);
1688     } else {
1689       warning("Biased locking is not supported with RTM locking; ignoring UseBiasedLocking flag." );
1690       UseBiasedLocking = false;
1691     }
1692   }
1693 #endif
1694   return UseBiasedLocking;
1695 }
1696 
1697 // On Xen, the cpuid instruction returns
1698 //  eax / registers[0]: Version of Xen
1699 //  ebx / registers[1]: chars 'XenV'
1700 //  ecx / registers[2]: chars 'MMXe'
1701 //  edx / registers[3]: chars 'nVMM'
1702 //
1703 // On KVM / VMWare / MS Hyper-V, the cpuid instruction returns
1704 //  ebx / registers[1]: chars 'KVMK' / 'VMwa' / 'Micr'
1705 //  ecx / registers[2]: chars 'VMKV' / 'reVM' / 'osof'
1706 //  edx / registers[3]: chars 'M'    / 'ware' / 't Hv'
1707 //
1708 // more information :
1709 // https://kb.vmware.com/s/article/1009458
1710 //
1711 void VM_Version::check_virtualizations() {
1712 #if defined(_LP64)
1713   uint32_t registers[4];
1714   char signature[13];
1715   uint32_t base;
1716   signature[12] = '\0';
1717   memset((void*)registers, 0, 4*sizeof(uint32_t));
1718 
1719   for (base = 0x40000000; base < 0x40010000; base += 0x100) {
1720     check_virt_cpuid(base, registers);
1721 
1722     *(uint32_t *)(signature + 0) = registers[1];
1723     *(uint32_t *)(signature + 4) = registers[2];
1724     *(uint32_t *)(signature + 8) = registers[3];
1725 
1726     if (strncmp("VMwareVMware", signature, 12) == 0) {
1727       Abstract_VM_Version::_detected_virtualization = VMWare;
1728       // check for extended metrics from guestlib
1729       VirtualizationSupport::initialize();
1730     }
1731 
1732     if (strncmp("Microsoft Hv", signature, 12) == 0) {
1733       Abstract_VM_Version::_detected_virtualization = HyperV;
1734     }
1735 
1736     if (strncmp("KVMKVMKVM", signature, 9) == 0) {
1737       Abstract_VM_Version::_detected_virtualization = KVM;
1738     }
1739 
1740     if (strncmp("XenVMMXenVMM", signature, 12) == 0) {
1741       Abstract_VM_Version::_detected_virtualization = XenHVM;
1742     }
1743   }
1744 #endif
1745 }
1746 
1747 void VM_Version::initialize() {
1748   ResourceMark rm;
1749   // Making this stub must be FIRST use of assembler
1750 
1751   stub_blob = BufferBlob::create("get_cpu_info_stub", stub_size);
1752   if (stub_blob == NULL) {
1753     vm_exit_during_initialization("Unable to allocate get_cpu_info_stub");
1754   }
1755   CodeBuffer c(stub_blob);
1756   VM_Version_StubGenerator g(&c);
1757   get_cpu_info_stub = CAST_TO_FN_PTR(get_cpu_info_stub_t,
1758                                      g.generate_get_cpu_info());
1759 
1760   get_processor_features();
1761   if (cpu_family() > 4) { // it supports CPUID
1762     check_virtualizations();
1763   }
1764 }