1 /*
   2  * Copyright (c) 2000, 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 "jvmci/jvmci_globals.hpp"
  28 #include "gc/shared/gcConfig.hpp"
  29 #include "utilities/defaultStream.hpp"
  30 #include "utilities/ostream.hpp"
  31 #include "runtime/globals_extension.hpp"
  32 
  33 fileStream* JVMCIGlobals::_jni_config_file = NULL;
  34 
  35 // Return true if jvmci flags are consistent.
  36 bool JVMCIGlobals::check_jvmci_flags_are_consistent() {
  37 
  38 #ifndef PRODUCT
  39 #define APPLY_JVMCI_FLAGS(params3, params4) \
  40   JVMCI_FLAGS(params4, params3, params4, params3, params4, params3, params4, params4, IGNORE_RANGE, IGNORE_CONSTRAINT, IGNORE_WRITEABLE)
  41 #define JVMCI_DECLARE_CHECK4(type, name, value, doc) bool name##checked = false;
  42 #define JVMCI_DECLARE_CHECK3(type, name, doc)        bool name##checked = false;
  43 #define JVMCI_FLAG_CHECKED(name)                          name##checked = true;
  44   APPLY_JVMCI_FLAGS(JVMCI_DECLARE_CHECK3, JVMCI_DECLARE_CHECK4)
  45 #else
  46 #define JVMCI_FLAG_CHECKED(name)
  47 #endif
  48 
  49   // Checks that a given flag is not set if a given guard flag is false.
  50 #define CHECK_NOT_SET(FLAG, GUARD)                     \
  51   JVMCI_FLAG_CHECKED(FLAG)                             \
  52   if (!GUARD && !FLAG_IS_DEFAULT(FLAG)) {              \
  53     jio_fprintf(defaultStream::error_stream(),         \
  54         "Improperly specified VM option '%s': '%s' must be enabled\n", #FLAG, #GUARD); \
  55     return false;                                      \
  56   }
  57 
  58   if (EnableJVMCIProduct) {
  59     if (FLAG_IS_DEFAULT(EnableJVMCI)) {
  60       FLAG_SET_DEFAULT(EnableJVMCI, true);
  61     }
  62     if (EnableJVMCI && FLAG_IS_DEFAULT(UseJVMCICompiler)) {
  63       FLAG_SET_DEFAULT(UseJVMCICompiler, true);
  64     }
  65   }
  66 
  67   JVMCI_FLAG_CHECKED(UseJVMCICompiler)
  68   JVMCI_FLAG_CHECKED(EnableJVMCI)
  69   JVMCI_FLAG_CHECKED(EnableJVMCIProduct)
  70 
  71   CHECK_NOT_SET(BootstrapJVMCI,   UseJVMCICompiler)
  72   CHECK_NOT_SET(PrintBootstrap,   UseJVMCICompiler)
  73   CHECK_NOT_SET(JVMCIThreads,     UseJVMCICompiler)
  74   CHECK_NOT_SET(JVMCIHostThreads, UseJVMCICompiler)
  75 
  76   if (UseJVMCICompiler) {
  77     if (FLAG_IS_DEFAULT(UseJVMCINativeLibrary) && !UseJVMCINativeLibrary) {
  78       char path[JVM_MAXPATHLEN];
  79       if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), JVMCI_SHARED_LIBRARY_NAME)) {
  80         // If a JVMCI native library is present,
  81         // we enable UseJVMCINativeLibrary by default.
  82         FLAG_SET_DEFAULT(UseJVMCINativeLibrary, true);
  83       }
  84     }
  85     if (!FLAG_IS_DEFAULT(EnableJVMCI) && !EnableJVMCI) {
  86       jio_fprintf(defaultStream::error_stream(),
  87           "Improperly specified VM option UseJVMCICompiler: EnableJVMCI cannot be disabled\n");
  88       return false;
  89     }
  90     FLAG_SET_DEFAULT(EnableJVMCI, true);
  91     if (BootstrapJVMCI && UseJVMCINativeLibrary) {
  92       jio_fprintf(defaultStream::error_stream(), "-XX:+BootstrapJVMCI is not compatible with -XX:+UseJVMCINativeLibrary\n");
  93       return false;
  94     }
  95   }
  96 
  97   if (!EnableJVMCI) {
  98     // Switch off eager JVMCI initialization if JVMCI is disabled.
  99     // Don't throw error if EagerJVMCI is set to allow testing.
 100     if (EagerJVMCI) {
 101       FLAG_SET_DEFAULT(EagerJVMCI, false);
 102     }
 103   }
 104   JVMCI_FLAG_CHECKED(EagerJVMCI)
 105 
 106   CHECK_NOT_SET(JVMCITraceLevel,              EnableJVMCI)
 107   CHECK_NOT_SET(JVMCICounterSize,             EnableJVMCI)
 108   CHECK_NOT_SET(JVMCICountersExcludeCompiler, EnableJVMCI)
 109   CHECK_NOT_SET(JVMCIUseFastLocking,          EnableJVMCI)
 110   CHECK_NOT_SET(JVMCINMethodSizeLimit,        EnableJVMCI)
 111   CHECK_NOT_SET(MethodProfileWidth,           EnableJVMCI)
 112   CHECK_NOT_SET(JVMCIPrintProperties,         EnableJVMCI)
 113   CHECK_NOT_SET(UseJVMCINativeLibrary,        EnableJVMCI)
 114   CHECK_NOT_SET(JVMCILibPath,                 EnableJVMCI)
 115   CHECK_NOT_SET(JVMCILibDumpJNIConfig,        EnableJVMCI)
 116 
 117 #ifndef COMPILER2
 118   JVMCI_FLAG_CHECKED(MaxVectorSize)
 119   JVMCI_FLAG_CHECKED(ReduceInitialCardMarks)
 120   JVMCI_FLAG_CHECKED(UseMultiplyToLenIntrinsic)
 121   JVMCI_FLAG_CHECKED(UseSquareToLenIntrinsic)
 122   JVMCI_FLAG_CHECKED(UseMulAddIntrinsic)
 123   JVMCI_FLAG_CHECKED(UseMontgomeryMultiplyIntrinsic)
 124   JVMCI_FLAG_CHECKED(UseMontgomerySquareIntrinsic)
 125 #endif // !COMPILER2
 126 
 127 #ifndef PRODUCT
 128 #define JVMCI_CHECK4(type, name, value, doc) assert(name##checked, #name " flag not checked");
 129 #define JVMCI_CHECK3(type, name, doc)        assert(name##checked, #name " flag not checked");
 130   // Ensures that all JVMCI flags are checked by this method.
 131   APPLY_JVMCI_FLAGS(JVMCI_CHECK3, JVMCI_CHECK4)
 132 #undef APPLY_JVMCI_FLAGS
 133 #undef JVMCI_DECLARE_CHECK3
 134 #undef JVMCI_DECLARE_CHECK4
 135 #undef JVMCI_CHECK3
 136 #undef JVMCI_CHECK4
 137 #undef JVMCI_FLAG_CHECKED
 138 #endif // PRODUCT
 139 #undef CHECK_NOT_SET
 140 
 141   if (JVMCILibDumpJNIConfig != NULL) {
 142     _jni_config_file = new(ResourceObj::C_HEAP, mtJVMCI) fileStream(JVMCILibDumpJNIConfig);
 143     if (_jni_config_file == NULL || !_jni_config_file->is_open()) {
 144       jio_fprintf(defaultStream::error_stream(),
 145           "Could not open file for dumping JVMCI shared library JNI config: %s\n", JVMCILibDumpJNIConfig);
 146       return false;
 147     }
 148   }
 149 
 150   return true;
 151 }
 152 
 153 // Convert JVMCI flags from experimental to product
 154 bool JVMCIGlobals::enable_jvmci_product_mode(JVMFlag::Flags origin) {
 155   const char *JVMCIFlags[] = {
 156     "EnableJVMCI",
 157     "EnableJVMCIProduct",
 158     "UseJVMCICompiler",
 159     "JVMCIPrintProperties",
 160     "EagerJVMCI",
 161     "JVMCIThreads",
 162     "JVMCICounterSize",
 163     "JVMCICountersExcludeCompiler",
 164     "JVMCINMethodSizeLimit",
 165     "JVMCILibPath",
 166     "JVMCILibDumpJNIConfig",
 167     "UseJVMCINativeLibrary",
 168     NULL
 169   };
 170 
 171   for (int i = 0; JVMCIFlags[i] != NULL; i++) {
 172     JVMFlag *jvmciFlag = (JVMFlag *)JVMFlag::find_declared_flag(JVMCIFlags[i]);
 173     if (jvmciFlag == NULL) {
 174       return false;
 175     }
 176     jvmciFlag->clear_experimental();
 177     jvmciFlag->set_product();
 178   }
 179 
 180   bool value = true;
 181   JVMFlag *jvmciEnableFlag = JVMFlag::find_flag("EnableJVMCIProduct");
 182   if (JVMFlag::boolAtPut(jvmciEnableFlag, &value, origin) != JVMFlag::SUCCESS) {
 183     return false;
 184   }
 185 
 186   // Effect of EnableJVMCIProduct on changing defaults of EnableJVMCI
 187   // and UseJVMCICompiler is deferred to check_jvmci_flags_are_consistent
 188   // so that setting these flags explicitly (e.g. on the command line)
 189   // takes precedence.
 190 
 191   return true;
 192 }
 193 
 194 void JVMCIGlobals::check_jvmci_supported_gc() {
 195   if (EnableJVMCI) {
 196     // Check if selected GC is supported by JVMCI and Java compiler
 197     if (!(UseSerialGC || UseParallelGC || UseParallelOldGC || UseG1GC)) {
 198       vm_exit_during_initialization("JVMCI Compiler does not support selected GC", GCConfig::hs_err_name());
 199       FLAG_SET_DEFAULT(EnableJVMCI, false);
 200       FLAG_SET_DEFAULT(UseJVMCICompiler, false);
 201     }
 202   }
 203 }