src/cpu/x86/vm/vm_version_x86.cpp

Mon, 31 Mar 2014 13:08:03 -0700

author
kvn
date
Mon, 31 Mar 2014 13:08:03 -0700
changeset 6537
0118c8c7b80f
parent 6429
606acabe7b5c
child 6656
1eba0601f0dd
permissions
-rw-r--r--

8038633: crash in VM_Version::get_processor_features() on startup
Summary: Windows need an exception wrapper around getPsrInfo_stub() call in order to properly handle SEGV for YMM registers test.
Reviewed-by: iveresov, iignatyev

     1 /*
     2  * Copyright (c) 1997, 2013, 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  */
    25 #include "precompiled.hpp"
    26 #include "asm/macroAssembler.hpp"
    27 #include "asm/macroAssembler.inline.hpp"
    28 #include "memory/resourceArea.hpp"
    29 #include "runtime/java.hpp"
    30 #include "runtime/stubCodeGenerator.hpp"
    31 #include "vm_version_x86.hpp"
    32 #ifdef TARGET_OS_FAMILY_linux
    33 # include "os_linux.inline.hpp"
    34 #endif
    35 #ifdef TARGET_OS_FAMILY_solaris
    36 # include "os_solaris.inline.hpp"
    37 #endif
    38 #ifdef TARGET_OS_FAMILY_windows
    39 # include "os_windows.inline.hpp"
    40 #endif
    41 #ifdef TARGET_OS_FAMILY_bsd
    42 # include "os_bsd.inline.hpp"
    43 #endif
    46 int VM_Version::_cpu;
    47 int VM_Version::_model;
    48 int VM_Version::_stepping;
    49 int VM_Version::_cpuFeatures;
    50 const char*           VM_Version::_features_str = "";
    51 VM_Version::CpuidInfo VM_Version::_cpuid_info   = { 0, };
    53 // Address of instruction which causes SEGV
    54 address VM_Version::_cpuinfo_segv_addr = 0;
    55 // Address of instruction after the one which causes SEGV
    56 address VM_Version::_cpuinfo_cont_addr = 0;
    58 static BufferBlob* stub_blob;
    59 static const int stub_size = 600;
    61 extern "C" {
    62   typedef void (*get_cpu_info_stub_t)(void*);
    63 }
    64 static get_cpu_info_stub_t get_cpu_info_stub = NULL;
    67 class VM_Version_StubGenerator: public StubCodeGenerator {
    68  public:
    70   VM_Version_StubGenerator(CodeBuffer *c) : StubCodeGenerator(c) {}
    72   address generate_get_cpu_info() {
    73     // Flags to test CPU type.
    74     const uint32_t HS_EFL_AC           = 0x40000;
    75     const uint32_t HS_EFL_ID           = 0x200000;
    76     // Values for when we don't have a CPUID instruction.
    77     const int      CPU_FAMILY_SHIFT = 8;
    78     const uint32_t CPU_FAMILY_386   = (3 << CPU_FAMILY_SHIFT);
    79     const uint32_t CPU_FAMILY_486   = (4 << CPU_FAMILY_SHIFT);
    81     Label detect_486, cpu486, detect_586, std_cpuid1, std_cpuid4;
    82     Label sef_cpuid, ext_cpuid, ext_cpuid1, ext_cpuid5, ext_cpuid7, done;
    84     StubCodeMark mark(this, "VM_Version", "get_cpu_info_stub");
    85 #   define __ _masm->
    87     address start = __ pc();
    89     //
    90     // void get_cpu_info(VM_Version::CpuidInfo* cpuid_info);
    91     //
    92     // LP64: rcx and rdx are first and second argument registers on windows
    94     __ push(rbp);
    95 #ifdef _LP64
    96     __ mov(rbp, c_rarg0); // cpuid_info address
    97 #else
    98     __ movptr(rbp, Address(rsp, 8)); // cpuid_info address
    99 #endif
   100     __ push(rbx);
   101     __ push(rsi);
   102     __ pushf();          // preserve rbx, and flags
   103     __ pop(rax);
   104     __ push(rax);
   105     __ mov(rcx, rax);
   106     //
   107     // if we are unable to change the AC flag, we have a 386
   108     //
   109     __ xorl(rax, HS_EFL_AC);
   110     __ push(rax);
   111     __ popf();
   112     __ pushf();
   113     __ pop(rax);
   114     __ cmpptr(rax, rcx);
   115     __ jccb(Assembler::notEqual, detect_486);
   117     __ movl(rax, CPU_FAMILY_386);
   118     __ movl(Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())), rax);
   119     __ jmp(done);
   121     //
   122     // If we are unable to change the ID flag, we have a 486 which does
   123     // not support the "cpuid" instruction.
   124     //
   125     __ bind(detect_486);
   126     __ mov(rax, rcx);
   127     __ xorl(rax, HS_EFL_ID);
   128     __ push(rax);
   129     __ popf();
   130     __ pushf();
   131     __ pop(rax);
   132     __ cmpptr(rcx, rax);
   133     __ jccb(Assembler::notEqual, detect_586);
   135     __ bind(cpu486);
   136     __ movl(rax, CPU_FAMILY_486);
   137     __ movl(Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())), rax);
   138     __ jmp(done);
   140     //
   141     // At this point, we have a chip which supports the "cpuid" instruction
   142     //
   143     __ bind(detect_586);
   144     __ xorl(rax, rax);
   145     __ cpuid();
   146     __ orl(rax, rax);
   147     __ jcc(Assembler::equal, cpu486);   // if cpuid doesn't support an input
   148                                         // value of at least 1, we give up and
   149                                         // assume a 486
   150     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset())));
   151     __ movl(Address(rsi, 0), rax);
   152     __ movl(Address(rsi, 4), rbx);
   153     __ movl(Address(rsi, 8), rcx);
   154     __ movl(Address(rsi,12), rdx);
   156     __ cmpl(rax, 0xa);                  // Is cpuid(0xB) supported?
   157     __ jccb(Assembler::belowEqual, std_cpuid4);
   159     //
   160     // cpuid(0xB) Processor Topology
   161     //
   162     __ movl(rax, 0xb);
   163     __ xorl(rcx, rcx);   // Threads level
   164     __ cpuid();
   166     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB0_offset())));
   167     __ movl(Address(rsi, 0), rax);
   168     __ movl(Address(rsi, 4), rbx);
   169     __ movl(Address(rsi, 8), rcx);
   170     __ movl(Address(rsi,12), rdx);
   172     __ movl(rax, 0xb);
   173     __ movl(rcx, 1);     // Cores level
   174     __ cpuid();
   175     __ push(rax);
   176     __ andl(rax, 0x1f);  // Determine if valid topology level
   177     __ orl(rax, rbx);    // eax[4:0] | ebx[0:15] == 0 indicates invalid level
   178     __ andl(rax, 0xffff);
   179     __ pop(rax);
   180     __ jccb(Assembler::equal, std_cpuid4);
   182     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB1_offset())));
   183     __ movl(Address(rsi, 0), rax);
   184     __ movl(Address(rsi, 4), rbx);
   185     __ movl(Address(rsi, 8), rcx);
   186     __ movl(Address(rsi,12), rdx);
   188     __ movl(rax, 0xb);
   189     __ movl(rcx, 2);     // Packages level
   190     __ cpuid();
   191     __ push(rax);
   192     __ andl(rax, 0x1f);  // Determine if valid topology level
   193     __ orl(rax, rbx);    // eax[4:0] | ebx[0:15] == 0 indicates invalid level
   194     __ andl(rax, 0xffff);
   195     __ pop(rax);
   196     __ jccb(Assembler::equal, std_cpuid4);
   198     __ lea(rsi, Address(rbp, in_bytes(VM_Version::tpl_cpuidB2_offset())));
   199     __ movl(Address(rsi, 0), rax);
   200     __ movl(Address(rsi, 4), rbx);
   201     __ movl(Address(rsi, 8), rcx);
   202     __ movl(Address(rsi,12), rdx);
   204     //
   205     // cpuid(0x4) Deterministic cache params
   206     //
   207     __ bind(std_cpuid4);
   208     __ movl(rax, 4);
   209     __ cmpl(rax, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset()))); // Is cpuid(0x4) supported?
   210     __ jccb(Assembler::greater, std_cpuid1);
   212     __ xorl(rcx, rcx);   // L1 cache
   213     __ cpuid();
   214     __ push(rax);
   215     __ andl(rax, 0x1f);  // Determine if valid cache parameters used
   216     __ orl(rax, rax);    // eax[4:0] == 0 indicates invalid cache
   217     __ pop(rax);
   218     __ jccb(Assembler::equal, std_cpuid1);
   220     __ lea(rsi, Address(rbp, in_bytes(VM_Version::dcp_cpuid4_offset())));
   221     __ movl(Address(rsi, 0), rax);
   222     __ movl(Address(rsi, 4), rbx);
   223     __ movl(Address(rsi, 8), rcx);
   224     __ movl(Address(rsi,12), rdx);
   226     //
   227     // Standard cpuid(0x1)
   228     //
   229     __ bind(std_cpuid1);
   230     __ movl(rax, 1);
   231     __ cpuid();
   232     __ lea(rsi, Address(rbp, in_bytes(VM_Version::std_cpuid1_offset())));
   233     __ movl(Address(rsi, 0), rax);
   234     __ movl(Address(rsi, 4), rbx);
   235     __ movl(Address(rsi, 8), rcx);
   236     __ movl(Address(rsi,12), rdx);
   238     //
   239     // Check if OS has enabled XGETBV instruction to access XCR0
   240     // (OSXSAVE feature flag) and CPU supports AVX
   241     //
   242     __ andl(rcx, 0x18000000); // cpuid1 bits osxsave | avx
   243     __ cmpl(rcx, 0x18000000);
   244     __ jccb(Assembler::notEqual, sef_cpuid); // jump if AVX is not supported
   246     //
   247     // XCR0, XFEATURE_ENABLED_MASK register
   248     //
   249     __ xorl(rcx, rcx);   // zero for XCR0 register
   250     __ xgetbv();
   251     __ lea(rsi, Address(rbp, in_bytes(VM_Version::xem_xcr0_offset())));
   252     __ movl(Address(rsi, 0), rax);
   253     __ movl(Address(rsi, 4), rdx);
   255     __ andl(rax, 0x6); // xcr0 bits sse | ymm
   256     __ cmpl(rax, 0x6);
   257     __ jccb(Assembler::notEqual, sef_cpuid); // jump if AVX is not supported
   259     //
   260     // Some OSs have a bug when upper 128bits of YMM
   261     // registers are not restored after a signal processing.
   262     // Generate SEGV here (reference through NULL)
   263     // and check upper YMM bits after it.
   264     //
   265     VM_Version::set_avx_cpuFeatures(); // Enable temporary to pass asserts
   267     // load value into all 32 bytes of ymm7 register
   268     __ movl(rcx, VM_Version::ymm_test_value());
   270     __ movdl(xmm0, rcx);
   271     __ pshufd(xmm0, xmm0, 0x00);
   272     __ vinsertf128h(xmm0, xmm0, xmm0);
   273     __ vmovdqu(xmm7, xmm0);
   274 #ifdef _LP64
   275     __ vmovdqu(xmm8,  xmm0);
   276     __ vmovdqu(xmm15, xmm0);
   277 #endif
   279     __ xorl(rsi, rsi);
   280     VM_Version::set_cpuinfo_segv_addr( __ pc() );
   281     // Generate SEGV
   282     __ movl(rax, Address(rsi, 0));
   284     VM_Version::set_cpuinfo_cont_addr( __ pc() );
   285     // Returns here after signal. Save xmm0 to check it later.
   286     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ymm_save_offset())));
   287     __ vmovdqu(Address(rsi,  0), xmm0);
   288     __ vmovdqu(Address(rsi, 32), xmm7);
   289 #ifdef _LP64
   290     __ vmovdqu(Address(rsi, 64), xmm8);
   291     __ vmovdqu(Address(rsi, 96), xmm15);
   292 #endif
   294     VM_Version::clean_cpuFeatures();
   296     //
   297     // cpuid(0x7) Structured Extended Features
   298     //
   299     __ bind(sef_cpuid);
   300     __ movl(rax, 7);
   301     __ cmpl(rax, Address(rbp, in_bytes(VM_Version::std_cpuid0_offset()))); // Is cpuid(0x7) supported?
   302     __ jccb(Assembler::greater, ext_cpuid);
   304     __ xorl(rcx, rcx);
   305     __ cpuid();
   306     __ lea(rsi, Address(rbp, in_bytes(VM_Version::sef_cpuid7_offset())));
   307     __ movl(Address(rsi, 0), rax);
   308     __ movl(Address(rsi, 4), rbx);
   310     //
   311     // Extended cpuid(0x80000000)
   312     //
   313     __ bind(ext_cpuid);
   314     __ movl(rax, 0x80000000);
   315     __ cpuid();
   316     __ cmpl(rax, 0x80000000);     // Is cpuid(0x80000001) supported?
   317     __ jcc(Assembler::belowEqual, done);
   318     __ cmpl(rax, 0x80000004);     // Is cpuid(0x80000005) supported?
   319     __ jccb(Assembler::belowEqual, ext_cpuid1);
   320     __ cmpl(rax, 0x80000006);     // Is cpuid(0x80000007) supported?
   321     __ jccb(Assembler::belowEqual, ext_cpuid5);
   322     __ cmpl(rax, 0x80000007);     // Is cpuid(0x80000008) supported?
   323     __ jccb(Assembler::belowEqual, ext_cpuid7);
   324     //
   325     // Extended cpuid(0x80000008)
   326     //
   327     __ movl(rax, 0x80000008);
   328     __ cpuid();
   329     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid8_offset())));
   330     __ movl(Address(rsi, 0), rax);
   331     __ movl(Address(rsi, 4), rbx);
   332     __ movl(Address(rsi, 8), rcx);
   333     __ movl(Address(rsi,12), rdx);
   335     //
   336     // Extended cpuid(0x80000007)
   337     //
   338     __ bind(ext_cpuid7);
   339     __ movl(rax, 0x80000007);
   340     __ cpuid();
   341     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid7_offset())));
   342     __ movl(Address(rsi, 0), rax);
   343     __ movl(Address(rsi, 4), rbx);
   344     __ movl(Address(rsi, 8), rcx);
   345     __ movl(Address(rsi,12), rdx);
   347     //
   348     // Extended cpuid(0x80000005)
   349     //
   350     __ bind(ext_cpuid5);
   351     __ movl(rax, 0x80000005);
   352     __ cpuid();
   353     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid5_offset())));
   354     __ movl(Address(rsi, 0), rax);
   355     __ movl(Address(rsi, 4), rbx);
   356     __ movl(Address(rsi, 8), rcx);
   357     __ movl(Address(rsi,12), rdx);
   359     //
   360     // Extended cpuid(0x80000001)
   361     //
   362     __ bind(ext_cpuid1);
   363     __ movl(rax, 0x80000001);
   364     __ cpuid();
   365     __ lea(rsi, Address(rbp, in_bytes(VM_Version::ext_cpuid1_offset())));
   366     __ movl(Address(rsi, 0), rax);
   367     __ movl(Address(rsi, 4), rbx);
   368     __ movl(Address(rsi, 8), rcx);
   369     __ movl(Address(rsi,12), rdx);
   371     //
   372     // return
   373     //
   374     __ bind(done);
   375     __ popf();
   376     __ pop(rsi);
   377     __ pop(rbx);
   378     __ pop(rbp);
   379     __ ret(0);
   381 #   undef __
   383     return start;
   384   };
   385 };
   388 void VM_Version::get_cpu_info_wrapper() {
   389   get_cpu_info_stub(&_cpuid_info);
   390 }
   392 #ifndef CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED
   393   #define CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(f) f()
   394 #endif
   396 void VM_Version::get_processor_features() {
   398   _cpu = 4; // 486 by default
   399   _model = 0;
   400   _stepping = 0;
   401   _cpuFeatures = 0;
   402   _logical_processors_per_package = 1;
   404   if (!Use486InstrsOnly) {
   405     // Get raw processor info
   407     // Some platforms (like Win*) need a wrapper around here
   408     // in order to properly handle SEGV for YMM registers test.
   409     CALL_TEST_FUNC_WITH_WRAPPER_IF_NEEDED(get_cpu_info_wrapper);
   411     assert_is_initialized();
   412     _cpu = extended_cpu_family();
   413     _model = extended_cpu_model();
   414     _stepping = cpu_stepping();
   416     if (cpu_family() > 4) { // it supports CPUID
   417       _cpuFeatures = feature_flags();
   418       // Logical processors are only available on P4s and above,
   419       // and only if hyperthreading is available.
   420       _logical_processors_per_package = logical_processor_count();
   421     }
   422   }
   424   _supports_cx8 = supports_cmpxchg8();
   425   // xchg and xadd instructions
   426   _supports_atomic_getset4 = true;
   427   _supports_atomic_getadd4 = true;
   428   LP64_ONLY(_supports_atomic_getset8 = true);
   429   LP64_ONLY(_supports_atomic_getadd8 = true);
   431 #ifdef _LP64
   432   // OS should support SSE for x64 and hardware should support at least SSE2.
   433   if (!VM_Version::supports_sse2()) {
   434     vm_exit_during_initialization("Unknown x64 processor: SSE2 not supported");
   435   }
   436   // in 64 bit the use of SSE2 is the minimum
   437   if (UseSSE < 2) UseSSE = 2;
   438 #endif
   440 #ifdef AMD64
   441   // flush_icache_stub have to be generated first.
   442   // That is why Icache line size is hard coded in ICache class,
   443   // see icache_x86.hpp. It is also the reason why we can't use
   444   // clflush instruction in 32-bit VM since it could be running
   445   // on CPU which does not support it.
   446   //
   447   // The only thing we can do is to verify that flushed
   448   // ICache::line_size has correct value.
   449   guarantee(_cpuid_info.std_cpuid1_edx.bits.clflush != 0, "clflush is not supported");
   450   // clflush_size is size in quadwords (8 bytes).
   451   guarantee(_cpuid_info.std_cpuid1_ebx.bits.clflush_size == 8, "such clflush size is not supported");
   452 #endif
   454   // If the OS doesn't support SSE, we can't use this feature even if the HW does
   455   if (!os::supports_sse())
   456     _cpuFeatures &= ~(CPU_SSE|CPU_SSE2|CPU_SSE3|CPU_SSSE3|CPU_SSE4A|CPU_SSE4_1|CPU_SSE4_2);
   458   if (UseSSE < 4) {
   459     _cpuFeatures &= ~CPU_SSE4_1;
   460     _cpuFeatures &= ~CPU_SSE4_2;
   461   }
   463   if (UseSSE < 3) {
   464     _cpuFeatures &= ~CPU_SSE3;
   465     _cpuFeatures &= ~CPU_SSSE3;
   466     _cpuFeatures &= ~CPU_SSE4A;
   467   }
   469   if (UseSSE < 2)
   470     _cpuFeatures &= ~CPU_SSE2;
   472   if (UseSSE < 1)
   473     _cpuFeatures &= ~CPU_SSE;
   475   if (UseAVX < 2)
   476     _cpuFeatures &= ~CPU_AVX2;
   478   if (UseAVX < 1)
   479     _cpuFeatures &= ~CPU_AVX;
   481   if (!UseAES && !FLAG_IS_DEFAULT(UseAES))
   482     _cpuFeatures &= ~CPU_AES;
   484   if (logical_processors_per_package() == 1) {
   485     // HT processor could be installed on a system which doesn't support HT.
   486     _cpuFeatures &= ~CPU_HT;
   487   }
   489   char buf[256];
   490   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",
   491                cores_per_cpu(), threads_per_core(),
   492                cpu_family(), _model, _stepping,
   493                (supports_cmov() ? ", cmov" : ""),
   494                (supports_cmpxchg8() ? ", cx8" : ""),
   495                (supports_fxsr() ? ", fxsr" : ""),
   496                (supports_mmx()  ? ", mmx"  : ""),
   497                (supports_sse()  ? ", sse"  : ""),
   498                (supports_sse2() ? ", sse2" : ""),
   499                (supports_sse3() ? ", sse3" : ""),
   500                (supports_ssse3()? ", ssse3": ""),
   501                (supports_sse4_1() ? ", sse4.1" : ""),
   502                (supports_sse4_2() ? ", sse4.2" : ""),
   503                (supports_popcnt() ? ", popcnt" : ""),
   504                (supports_avx()    ? ", avx" : ""),
   505                (supports_avx2()   ? ", avx2" : ""),
   506                (supports_aes()    ? ", aes" : ""),
   507                (supports_clmul()  ? ", clmul" : ""),
   508                (supports_erms()   ? ", erms" : ""),
   509                (supports_rtm()    ? ", rtm" : ""),
   510                (supports_mmx_ext() ? ", mmxext" : ""),
   511                (supports_3dnow_prefetch() ? ", 3dnowpref" : ""),
   512                (supports_lzcnt()   ? ", lzcnt": ""),
   513                (supports_sse4a()   ? ", sse4a": ""),
   514                (supports_ht() ? ", ht": ""),
   515                (supports_tsc() ? ", tsc": ""),
   516                (supports_tscinv_bit() ? ", tscinvbit": ""),
   517                (supports_tscinv() ? ", tscinv": ""),
   518                (supports_bmi1() ? ", bmi1" : ""),
   519                (supports_bmi2() ? ", bmi2" : ""));
   520   _features_str = strdup(buf);
   522   // UseSSE is set to the smaller of what hardware supports and what
   523   // the command line requires.  I.e., you cannot set UseSSE to 2 on
   524   // older Pentiums which do not support it.
   525   if (UseSSE > 4) UseSSE=4;
   526   if (UseSSE < 0) UseSSE=0;
   527   if (!supports_sse4_1()) // Drop to 3 if no SSE4 support
   528     UseSSE = MIN2((intx)3,UseSSE);
   529   if (!supports_sse3()) // Drop to 2 if no SSE3 support
   530     UseSSE = MIN2((intx)2,UseSSE);
   531   if (!supports_sse2()) // Drop to 1 if no SSE2 support
   532     UseSSE = MIN2((intx)1,UseSSE);
   533   if (!supports_sse ()) // Drop to 0 if no SSE  support
   534     UseSSE = 0;
   536   if (UseAVX > 2) UseAVX=2;
   537   if (UseAVX < 0) UseAVX=0;
   538   if (!supports_avx2()) // Drop to 1 if no AVX2 support
   539     UseAVX = MIN2((intx)1,UseAVX);
   540   if (!supports_avx ()) // Drop to 0 if no AVX  support
   541     UseAVX = 0;
   543   // Use AES instructions if available.
   544   if (supports_aes()) {
   545     if (FLAG_IS_DEFAULT(UseAES)) {
   546       UseAES = true;
   547     }
   548   } else if (UseAES) {
   549     if (!FLAG_IS_DEFAULT(UseAES))
   550       warning("AES instructions are not available on this CPU");
   551     FLAG_SET_DEFAULT(UseAES, false);
   552   }
   554   // Use CLMUL instructions if available.
   555   if (supports_clmul()) {
   556     if (FLAG_IS_DEFAULT(UseCLMUL)) {
   557       UseCLMUL = true;
   558     }
   559   } else if (UseCLMUL) {
   560     if (!FLAG_IS_DEFAULT(UseCLMUL))
   561       warning("CLMUL instructions not available on this CPU (AVX may also be required)");
   562     FLAG_SET_DEFAULT(UseCLMUL, false);
   563   }
   565   if (UseCLMUL && (UseAVX > 0) && (UseSSE > 2)) {
   566     if (FLAG_IS_DEFAULT(UseCRC32Intrinsics)) {
   567       UseCRC32Intrinsics = true;
   568     }
   569   } else if (UseCRC32Intrinsics) {
   570     if (!FLAG_IS_DEFAULT(UseCRC32Intrinsics))
   571       warning("CRC32 Intrinsics requires AVX and CLMUL instructions (not available on this CPU)");
   572     FLAG_SET_DEFAULT(UseCRC32Intrinsics, false);
   573   }
   575   // The AES intrinsic stubs require AES instruction support (of course)
   576   // but also require sse3 mode for instructions it use.
   577   if (UseAES && (UseSSE > 2)) {
   578     if (FLAG_IS_DEFAULT(UseAESIntrinsics)) {
   579       UseAESIntrinsics = true;
   580     }
   581   } else if (UseAESIntrinsics) {
   582     if (!FLAG_IS_DEFAULT(UseAESIntrinsics))
   583       warning("AES intrinsics are not available on this CPU");
   584     FLAG_SET_DEFAULT(UseAESIntrinsics, false);
   585   }
   587   // Adjust RTM (Restricted Transactional Memory) flags
   588   if (!supports_rtm() && UseRTMLocking) {
   589     // Can't continue because UseRTMLocking affects UseBiasedLocking flag
   590     // setting during arguments processing. See use_biased_locking().
   591     // VM_Version_init() is executed after UseBiasedLocking is used
   592     // in Thread::allocate().
   593     vm_exit_during_initialization("RTM instructions are not available on this CPU");
   594   }
   596 #if INCLUDE_RTM_OPT
   597   if (UseRTMLocking) {
   598     if (!FLAG_IS_CMDLINE(UseRTMLocking)) {
   599       // RTM locking should be used only for applications with
   600       // high lock contention. For now we do not use it by default.
   601       vm_exit_during_initialization("UseRTMLocking flag should be only set on command line");
   602     }
   603     if (!is_power_of_2(RTMTotalCountIncrRate)) {
   604       warning("RTMTotalCountIncrRate must be a power of 2, resetting it to 64");
   605       FLAG_SET_DEFAULT(RTMTotalCountIncrRate, 64);
   606     }
   607     if (RTMAbortRatio < 0 || RTMAbortRatio > 100) {
   608       warning("RTMAbortRatio must be in the range 0 to 100, resetting it to 50");
   609       FLAG_SET_DEFAULT(RTMAbortRatio, 50);
   610     }
   611   } else { // !UseRTMLocking
   612     if (UseRTMForStackLocks) {
   613       if (!FLAG_IS_DEFAULT(UseRTMForStackLocks)) {
   614         warning("UseRTMForStackLocks flag should be off when UseRTMLocking flag is off");
   615       }
   616       FLAG_SET_DEFAULT(UseRTMForStackLocks, false);
   617     }
   618     if (UseRTMDeopt) {
   619       FLAG_SET_DEFAULT(UseRTMDeopt, false);
   620     }
   621     if (PrintPreciseRTMLockingStatistics) {
   622       FLAG_SET_DEFAULT(PrintPreciseRTMLockingStatistics, false);
   623     }
   624   }
   625 #else
   626   if (UseRTMLocking) {
   627     // Only C2 does RTM locking optimization.
   628     // Can't continue because UseRTMLocking affects UseBiasedLocking flag
   629     // setting during arguments processing. See use_biased_locking().
   630     vm_exit_during_initialization("RTM locking optimization is not supported in this VM");
   631   }
   632 #endif
   634 #ifdef COMPILER2
   635   if (UseFPUForSpilling) {
   636     if (UseSSE < 2) {
   637       // Only supported with SSE2+
   638       FLAG_SET_DEFAULT(UseFPUForSpilling, false);
   639     }
   640   }
   641   if (MaxVectorSize > 0) {
   642     if (!is_power_of_2(MaxVectorSize)) {
   643       warning("MaxVectorSize must be a power of 2");
   644       FLAG_SET_DEFAULT(MaxVectorSize, 32);
   645     }
   646     if (MaxVectorSize > 32) {
   647       FLAG_SET_DEFAULT(MaxVectorSize, 32);
   648     }
   649     if (MaxVectorSize > 16 && (UseAVX == 0 || !os_supports_avx_vectors())) {
   650       // 32 bytes vectors (in YMM) are only supported with AVX+
   651       FLAG_SET_DEFAULT(MaxVectorSize, 16);
   652     }
   653     if (UseSSE < 2) {
   654       // Vectors (in XMM) are only supported with SSE2+
   655       FLAG_SET_DEFAULT(MaxVectorSize, 0);
   656     }
   657 #ifdef ASSERT
   658     if (supports_avx() && PrintMiscellaneous && Verbose && TraceNewVectors) {
   659       tty->print_cr("State of YMM registers after signal handle:");
   660       int nreg = 2 LP64_ONLY(+2);
   661       const char* ymm_name[4] = {"0", "7", "8", "15"};
   662       for (int i = 0; i < nreg; i++) {
   663         tty->print("YMM%s:", ymm_name[i]);
   664         for (int j = 7; j >=0; j--) {
   665           tty->print(" %x", _cpuid_info.ymm_save[i*8 + j]);
   666         }
   667         tty->cr();
   668       }
   669     }
   670 #endif
   671   }
   672 #endif
   674   // On new cpus instructions which update whole XMM register should be used
   675   // to prevent partial register stall due to dependencies on high half.
   676   //
   677   // UseXmmLoadAndClearUpper == true  --> movsd(xmm, mem)
   678   // UseXmmLoadAndClearUpper == false --> movlpd(xmm, mem)
   679   // UseXmmRegToRegMoveAll == true  --> movaps(xmm, xmm), movapd(xmm, xmm).
   680   // UseXmmRegToRegMoveAll == false --> movss(xmm, xmm),  movsd(xmm, xmm).
   682   if( is_amd() ) { // AMD cpus specific settings
   683     if( supports_sse2() && FLAG_IS_DEFAULT(UseAddressNop) ) {
   684       // Use it on new AMD cpus starting from Opteron.
   685       UseAddressNop = true;
   686     }
   687     if( supports_sse2() && FLAG_IS_DEFAULT(UseNewLongLShift) ) {
   688       // Use it on new AMD cpus starting from Opteron.
   689       UseNewLongLShift = true;
   690     }
   691     if( FLAG_IS_DEFAULT(UseXmmLoadAndClearUpper) ) {
   692       if( supports_sse4a() ) {
   693         UseXmmLoadAndClearUpper = true; // use movsd only on '10h' Opteron
   694       } else {
   695         UseXmmLoadAndClearUpper = false;
   696       }
   697     }
   698     if( FLAG_IS_DEFAULT(UseXmmRegToRegMoveAll) ) {
   699       if( supports_sse4a() ) {
   700         UseXmmRegToRegMoveAll = true; // use movaps, movapd only on '10h'
   701       } else {
   702         UseXmmRegToRegMoveAll = false;
   703       }
   704     }
   705     if( FLAG_IS_DEFAULT(UseXmmI2F) ) {
   706       if( supports_sse4a() ) {
   707         UseXmmI2F = true;
   708       } else {
   709         UseXmmI2F = false;
   710       }
   711     }
   712     if( FLAG_IS_DEFAULT(UseXmmI2D) ) {
   713       if( supports_sse4a() ) {
   714         UseXmmI2D = true;
   715       } else {
   716         UseXmmI2D = false;
   717       }
   718     }
   719     if( FLAG_IS_DEFAULT(UseSSE42Intrinsics) ) {
   720       if( supports_sse4_2() && UseSSE >= 4 ) {
   721         UseSSE42Intrinsics = true;
   722       }
   723     }
   725     // some defaults for AMD family 15h
   726     if ( cpu_family() == 0x15 ) {
   727       // On family 15h processors default is no sw prefetch
   728       if (FLAG_IS_DEFAULT(AllocatePrefetchStyle)) {
   729         AllocatePrefetchStyle = 0;
   730       }
   731       // Also, if some other prefetch style is specified, default instruction type is PREFETCHW
   732       if (FLAG_IS_DEFAULT(AllocatePrefetchInstr)) {
   733         AllocatePrefetchInstr = 3;
   734       }
   735       // On family 15h processors use XMM and UnalignedLoadStores for Array Copy
   736       if (supports_sse2() && FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
   737         UseXMMForArrayCopy = true;
   738       }
   739       if (supports_sse2() && FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
   740         UseUnalignedLoadStores = true;
   741       }
   742     }
   744 #ifdef COMPILER2
   745     if (MaxVectorSize > 16) {
   746       // Limit vectors size to 16 bytes on current AMD cpus.
   747       FLAG_SET_DEFAULT(MaxVectorSize, 16);
   748     }
   749 #endif // COMPILER2
   750   }
   752   if( is_intel() ) { // Intel cpus specific settings
   753     if( FLAG_IS_DEFAULT(UseStoreImmI16) ) {
   754       UseStoreImmI16 = false; // don't use it on Intel cpus
   755     }
   756     if( cpu_family() == 6 || cpu_family() == 15 ) {
   757       if( FLAG_IS_DEFAULT(UseAddressNop) ) {
   758         // Use it on all Intel cpus starting from PentiumPro
   759         UseAddressNop = true;
   760       }
   761     }
   762     if( FLAG_IS_DEFAULT(UseXmmLoadAndClearUpper) ) {
   763       UseXmmLoadAndClearUpper = true; // use movsd on all Intel cpus
   764     }
   765     if( FLAG_IS_DEFAULT(UseXmmRegToRegMoveAll) ) {
   766       if( supports_sse3() ) {
   767         UseXmmRegToRegMoveAll = true; // use movaps, movapd on new Intel cpus
   768       } else {
   769         UseXmmRegToRegMoveAll = false;
   770       }
   771     }
   772     if( cpu_family() == 6 && supports_sse3() ) { // New Intel cpus
   773 #ifdef COMPILER2
   774       if( FLAG_IS_DEFAULT(MaxLoopPad) ) {
   775         // For new Intel cpus do the next optimization:
   776         // don't align the beginning of a loop if there are enough instructions
   777         // left (NumberOfLoopInstrToAlign defined in c2_globals.hpp)
   778         // in current fetch line (OptoLoopAlignment) or the padding
   779         // is big (> MaxLoopPad).
   780         // Set MaxLoopPad to 11 for new Intel cpus to reduce number of
   781         // generated NOP instructions. 11 is the largest size of one
   782         // address NOP instruction '0F 1F' (see Assembler::nop(i)).
   783         MaxLoopPad = 11;
   784       }
   785 #endif // COMPILER2
   786       if (FLAG_IS_DEFAULT(UseXMMForArrayCopy)) {
   787         UseXMMForArrayCopy = true; // use SSE2 movq on new Intel cpus
   788       }
   789       if (supports_sse4_2() && supports_ht()) { // Newest Intel cpus
   790         if (FLAG_IS_DEFAULT(UseUnalignedLoadStores)) {
   791           UseUnalignedLoadStores = true; // use movdqu on newest Intel cpus
   792         }
   793       }
   794       if (supports_sse4_2() && UseSSE >= 4) {
   795         if (FLAG_IS_DEFAULT(UseSSE42Intrinsics)) {
   796           UseSSE42Intrinsics = true;
   797         }
   798       }
   799     }
   800   }
   802   // Use count leading zeros count instruction if available.
   803   if (supports_lzcnt()) {
   804     if (FLAG_IS_DEFAULT(UseCountLeadingZerosInstruction)) {
   805       UseCountLeadingZerosInstruction = true;
   806     }
   807    } else if (UseCountLeadingZerosInstruction) {
   808     warning("lzcnt instruction is not available on this CPU");
   809     FLAG_SET_DEFAULT(UseCountLeadingZerosInstruction, false);
   810   }
   812   if (supports_bmi1()) {
   813     if (FLAG_IS_DEFAULT(UseBMI1Instructions)) {
   814       UseBMI1Instructions = true;
   815     }
   816   } else if (UseBMI1Instructions) {
   817     warning("BMI1 instructions are not available on this CPU");
   818     FLAG_SET_DEFAULT(UseBMI1Instructions, false);
   819   }
   821   // Use count trailing zeros instruction if available
   822   if (supports_bmi1()) {
   823     if (FLAG_IS_DEFAULT(UseCountTrailingZerosInstruction)) {
   824       UseCountTrailingZerosInstruction = UseBMI1Instructions;
   825     }
   826   } else if (UseCountTrailingZerosInstruction) {
   827     warning("tzcnt instruction is not available on this CPU");
   828     FLAG_SET_DEFAULT(UseCountTrailingZerosInstruction, false);
   829   }
   831   // Use population count instruction if available.
   832   if (supports_popcnt()) {
   833     if (FLAG_IS_DEFAULT(UsePopCountInstruction)) {
   834       UsePopCountInstruction = true;
   835     }
   836   } else if (UsePopCountInstruction) {
   837     warning("POPCNT instruction is not available on this CPU");
   838     FLAG_SET_DEFAULT(UsePopCountInstruction, false);
   839   }
   841   // Use fast-string operations if available.
   842   if (supports_erms()) {
   843     if (FLAG_IS_DEFAULT(UseFastStosb)) {
   844       UseFastStosb = true;
   845     }
   846   } else if (UseFastStosb) {
   847     warning("fast-string operations are not available on this CPU");
   848     FLAG_SET_DEFAULT(UseFastStosb, false);
   849   }
   851 #ifdef COMPILER2
   852   if (FLAG_IS_DEFAULT(AlignVector)) {
   853     // Modern processors allow misaligned memory operations for vectors.
   854     AlignVector = !UseUnalignedLoadStores;
   855   }
   856 #endif // COMPILER2
   858   assert(0 <= ReadPrefetchInstr && ReadPrefetchInstr <= 3, "invalid value");
   859   assert(0 <= AllocatePrefetchInstr && AllocatePrefetchInstr <= 3, "invalid value");
   861   // set valid Prefetch instruction
   862   if( ReadPrefetchInstr < 0 ) ReadPrefetchInstr = 0;
   863   if( ReadPrefetchInstr > 3 ) ReadPrefetchInstr = 3;
   864   if( ReadPrefetchInstr == 3 && !supports_3dnow_prefetch() ) ReadPrefetchInstr = 0;
   865   if( !supports_sse() && supports_3dnow_prefetch() ) ReadPrefetchInstr = 3;
   867   if( AllocatePrefetchInstr < 0 ) AllocatePrefetchInstr = 0;
   868   if( AllocatePrefetchInstr > 3 ) AllocatePrefetchInstr = 3;
   869   if( AllocatePrefetchInstr == 3 && !supports_3dnow_prefetch() ) AllocatePrefetchInstr=0;
   870   if( !supports_sse() && supports_3dnow_prefetch() ) AllocatePrefetchInstr = 3;
   872   // Allocation prefetch settings
   873   intx cache_line_size = prefetch_data_size();
   874   if( cache_line_size > AllocatePrefetchStepSize )
   875     AllocatePrefetchStepSize = cache_line_size;
   877   assert(AllocatePrefetchLines > 0, "invalid value");
   878   if( AllocatePrefetchLines < 1 )     // set valid value in product VM
   879     AllocatePrefetchLines = 3;
   880   assert(AllocateInstancePrefetchLines > 0, "invalid value");
   881   if( AllocateInstancePrefetchLines < 1 ) // set valid value in product VM
   882     AllocateInstancePrefetchLines = 1;
   884   AllocatePrefetchDistance = allocate_prefetch_distance();
   885   AllocatePrefetchStyle    = allocate_prefetch_style();
   887   if( is_intel() && cpu_family() == 6 && supports_sse3() ) {
   888     if( AllocatePrefetchStyle == 2 ) { // watermark prefetching on Core
   889 #ifdef _LP64
   890       AllocatePrefetchDistance = 384;
   891 #else
   892       AllocatePrefetchDistance = 320;
   893 #endif
   894     }
   895     if( supports_sse4_2() && supports_ht() ) { // Nehalem based cpus
   896       AllocatePrefetchDistance = 192;
   897       AllocatePrefetchLines = 4;
   898 #ifdef COMPILER2
   899       if (AggressiveOpts && FLAG_IS_DEFAULT(UseFPUForSpilling)) {
   900         FLAG_SET_DEFAULT(UseFPUForSpilling, true);
   901       }
   902 #endif
   903     }
   904   }
   905   assert(AllocatePrefetchDistance % AllocatePrefetchStepSize == 0, "invalid value");
   907 #ifdef _LP64
   908   // Prefetch settings
   909   PrefetchCopyIntervalInBytes = prefetch_copy_interval_in_bytes();
   910   PrefetchScanIntervalInBytes = prefetch_scan_interval_in_bytes();
   911   PrefetchFieldsAhead         = prefetch_fields_ahead();
   912 #endif
   914   if (FLAG_IS_DEFAULT(ContendedPaddingWidth) &&
   915      (cache_line_size > ContendedPaddingWidth))
   916      ContendedPaddingWidth = cache_line_size;
   918 #ifndef PRODUCT
   919   if (PrintMiscellaneous && Verbose) {
   920     tty->print_cr("Logical CPUs per core: %u",
   921                   logical_processors_per_package());
   922     tty->print("UseSSE=%d",UseSSE);
   923     if (UseAVX > 0) {
   924       tty->print("  UseAVX=%d",UseAVX);
   925     }
   926     if (UseAES) {
   927       tty->print("  UseAES=1");
   928     }
   929 #ifdef COMPILER2
   930     if (MaxVectorSize > 0) {
   931       tty->print("  MaxVectorSize=%d", MaxVectorSize);
   932     }
   933 #endif
   934     tty->cr();
   935     tty->print("Allocation");
   936     if (AllocatePrefetchStyle <= 0 || UseSSE == 0 && !supports_3dnow_prefetch()) {
   937       tty->print_cr(": no prefetching");
   938     } else {
   939       tty->print(" prefetching: ");
   940       if (UseSSE == 0 && supports_3dnow_prefetch()) {
   941         tty->print("PREFETCHW");
   942       } else if (UseSSE >= 1) {
   943         if (AllocatePrefetchInstr == 0) {
   944           tty->print("PREFETCHNTA");
   945         } else if (AllocatePrefetchInstr == 1) {
   946           tty->print("PREFETCHT0");
   947         } else if (AllocatePrefetchInstr == 2) {
   948           tty->print("PREFETCHT2");
   949         } else if (AllocatePrefetchInstr == 3) {
   950           tty->print("PREFETCHW");
   951         }
   952       }
   953       if (AllocatePrefetchLines > 1) {
   954         tty->print_cr(" at distance %d, %d lines of %d bytes", AllocatePrefetchDistance, AllocatePrefetchLines, AllocatePrefetchStepSize);
   955       } else {
   956         tty->print_cr(" at distance %d, one line of %d bytes", AllocatePrefetchDistance, AllocatePrefetchStepSize);
   957       }
   958     }
   960     if (PrefetchCopyIntervalInBytes > 0) {
   961       tty->print_cr("PrefetchCopyIntervalInBytes %d", PrefetchCopyIntervalInBytes);
   962     }
   963     if (PrefetchScanIntervalInBytes > 0) {
   964       tty->print_cr("PrefetchScanIntervalInBytes %d", PrefetchScanIntervalInBytes);
   965     }
   966     if (PrefetchFieldsAhead > 0) {
   967       tty->print_cr("PrefetchFieldsAhead %d", PrefetchFieldsAhead);
   968     }
   969     if (ContendedPaddingWidth > 0) {
   970       tty->print_cr("ContendedPaddingWidth %d", ContendedPaddingWidth);
   971     }
   972   }
   973 #endif // !PRODUCT
   974 }
   976 bool VM_Version::use_biased_locking() {
   977 #if INCLUDE_RTM_OPT
   978   // RTM locking is most useful when there is high lock contention and
   979   // low data contention.  With high lock contention the lock is usually
   980   // inflated and biased locking is not suitable for that case.
   981   // RTM locking code requires that biased locking is off.
   982   // Note: we can't switch off UseBiasedLocking in get_processor_features()
   983   // because it is used by Thread::allocate() which is called before
   984   // VM_Version::initialize().
   985   if (UseRTMLocking && UseBiasedLocking) {
   986     if (FLAG_IS_DEFAULT(UseBiasedLocking)) {
   987       FLAG_SET_DEFAULT(UseBiasedLocking, false);
   988     } else {
   989       warning("Biased locking is not supported with RTM locking; ignoring UseBiasedLocking flag." );
   990       UseBiasedLocking = false;
   991     }
   992   }
   993 #endif
   994   return UseBiasedLocking;
   995 }
   997 void VM_Version::initialize() {
   998   ResourceMark rm;
   999   // Making this stub must be FIRST use of assembler
  1001   stub_blob = BufferBlob::create("get_cpu_info_stub", stub_size);
  1002   if (stub_blob == NULL) {
  1003     vm_exit_during_initialization("Unable to allocate get_cpu_info_stub");
  1005   CodeBuffer c(stub_blob);
  1006   VM_Version_StubGenerator g(&c);
  1007   get_cpu_info_stub = CAST_TO_FN_PTR(get_cpu_info_stub_t,
  1008                                      g.generate_get_cpu_info());
  1010   get_processor_features();

mercurial