1 /*
   2  * Copyright (c) 1997, 2021, 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 "classfile/javaClasses.hpp"
  28 #include "classfile/moduleEntry.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmClasses.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/codeCache.hpp"
  33 #include "code/icBuffer.hpp"
  34 #include "code/vtableStubs.hpp"
  35 #include "gc/shared/gcVMOperations.hpp"
  36 #include "logging/log.hpp"
  37 #include "interpreter/interpreter.hpp"
  38 #include "logging/log.hpp"
  39 #include "logging/logStream.hpp"
  40 #include "memory/allocation.inline.hpp"
  41 #include "memory/guardedMemory.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "memory/universe.hpp"
  44 #include "oops/compressedKlass.hpp"
  45 #include "oops/compressedOops.inline.hpp"
  46 #include "oops/oop.inline.hpp"
  47 #include "prims/jvm_misc.hpp"
  48 #include "runtime/arguments.hpp"
  49 #include "runtime/atomic.hpp"
  50 #include "runtime/frame.inline.hpp"
  51 #include "runtime/handles.inline.hpp"
  52 #include "runtime/interfaceSupport.inline.hpp"
  53 #include "runtime/java.hpp"
  54 #include "runtime/javaCalls.hpp"
  55 #include "runtime/jniHandles.hpp"
  56 #include "runtime/mutexLocker.hpp"
  57 #include "runtime/os.inline.hpp"
  58 #include "runtime/osThread.hpp"
  59 #include "runtime/safefetch.inline.hpp"
  60 #include "runtime/sharedRuntime.hpp"
  61 #include "runtime/thread.inline.hpp"
  62 #include "runtime/threadSMR.hpp"
  63 #include "runtime/vmOperations.hpp"
  64 #include "runtime/vm_version.hpp"
  65 #include "services/attachListener.hpp"
  66 #include "services/mallocTracker.hpp"
  67 #include "services/memTracker.hpp"
  68 #include "services/nmtPreInit.hpp"
  69 #include "services/nmtCommon.hpp"
  70 #include "services/threadService.hpp"
  71 #include "utilities/align.hpp"
  72 #include "utilities/count_trailing_zeros.hpp"
  73 #include "utilities/defaultStream.hpp"
  74 #include "utilities/events.hpp"
  75 #include "utilities/powerOfTwo.hpp"
  76 
  77 # include <signal.h>
  78 # include <errno.h>
  79 
  80 OSThread*         os::_starting_thread    = NULL;
  81 address           os::_polling_page       = NULL;
  82 volatile unsigned int os::_rand_seed      = 1234567;
  83 int               os::_processor_count    = 0;
  84 int               os::_initial_active_processor_count = 0;
  85 os::PageSizes     os::_page_sizes;
  86 
  87 #ifndef PRODUCT
  88 julong os::num_mallocs = 0;         // # of calls to malloc/realloc
  89 julong os::alloc_bytes = 0;         // # of bytes allocated
  90 julong os::num_frees = 0;           // # of calls to free
  91 julong os::free_bytes = 0;          // # of bytes freed
  92 #endif
  93 
  94 static size_t cur_malloc_words = 0;  // current size for MallocMaxTestWords
  95 
  96 DEBUG_ONLY(bool os::_mutex_init_done = false;)
  97 
  98 int os::snprintf(char* buf, size_t len, const char* fmt, ...) {
  99   va_list args;
 100   va_start(args, fmt);
 101   int result = os::vsnprintf(buf, len, fmt, args);
 102   va_end(args);
 103   return result;
 104 }
 105 
 106 // Fill in buffer with current local time as an ISO-8601 string.
 107 // E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz.
 108 // Returns buffer, or NULL if it failed.
 109 char* os::iso8601_time(char* buffer, size_t buffer_length, bool utc) {
 110   const jlong now = javaTimeMillis();
 111   return os::iso8601_time(now, buffer, buffer_length, utc);
 112 }
 113 
 114 // Fill in buffer with an ISO-8601 string corresponding to the given javaTimeMillis value
 115 // E.g., yyyy-mm-ddThh:mm:ss-zzzz.
 116 // Returns buffer, or NULL if it failed.
 117 // This would mostly be a call to
 118 //     strftime(...., "%Y-%m-%d" "T" "%H:%M:%S" "%z", ....)
 119 // except that on Windows the %z behaves badly, so we do it ourselves.
 120 // Also, people wanted milliseconds on there,
 121 // and strftime doesn't do milliseconds.
 122 char* os::iso8601_time(jlong milliseconds_since_19700101, char* buffer, size_t buffer_length, bool utc) {
 123   // Output will be of the form "YYYY-MM-DDThh:mm:ss.mmm+zzzz\0"
 124 
 125   // Sanity check the arguments
 126   if (buffer == NULL) {
 127     assert(false, "NULL buffer");
 128     return NULL;
 129   }
 130   if (buffer_length < os::iso8601_timestamp_size) {
 131     assert(false, "buffer_length too small");
 132     return NULL;
 133   }
 134   const int milliseconds_per_microsecond = 1000;
 135   const time_t seconds_since_19700101 =
 136     milliseconds_since_19700101 / milliseconds_per_microsecond;
 137   const int milliseconds_after_second =
 138     milliseconds_since_19700101 % milliseconds_per_microsecond;
 139   // Convert the time value to a tm and timezone variable
 140   struct tm time_struct;
 141   if (utc) {
 142     if (gmtime_pd(&seconds_since_19700101, &time_struct) == NULL) {
 143       assert(false, "Failed gmtime_pd");
 144       return NULL;
 145     }
 146   } else {
 147     if (localtime_pd(&seconds_since_19700101, &time_struct) == NULL) {
 148       assert(false, "Failed localtime_pd");
 149       return NULL;
 150     }
 151   }
 152 
 153   const time_t seconds_per_minute = 60;
 154   const time_t minutes_per_hour = 60;
 155   const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour;
 156 
 157   // No offset when dealing with UTC
 158   time_t UTC_to_local = 0;
 159   if (!utc) {
 160 #if defined(_ALLBSD_SOURCE) || defined(_GNU_SOURCE)
 161     UTC_to_local = -(time_struct.tm_gmtoff);
 162 #elif defined(_WINDOWS)
 163     long zone;
 164     _get_timezone(&zone);
 165     UTC_to_local = static_cast<time_t>(zone);
 166 #else
 167     UTC_to_local = timezone;
 168 #endif
 169 
 170     // tm_gmtoff already includes adjustment for daylight saving
 171 #if !defined(_ALLBSD_SOURCE) && !defined(_GNU_SOURCE)
 172     // If daylight savings time is in effect,
 173     // we are 1 hour East of our time zone
 174     if (time_struct.tm_isdst > 0) {
 175       UTC_to_local = UTC_to_local - seconds_per_hour;
 176     }
 177 #endif
 178   }
 179 
 180   // Compute the time zone offset.
 181   //    localtime_pd() sets timezone to the difference (in seconds)
 182   //    between UTC and and local time.
 183   //    ISO 8601 says we need the difference between local time and UTC,
 184   //    we change the sign of the localtime_pd() result.
 185   const time_t local_to_UTC = -(UTC_to_local);
 186   // Then we have to figure out if if we are ahead (+) or behind (-) UTC.
 187   char sign_local_to_UTC = '+';
 188   time_t abs_local_to_UTC = local_to_UTC;
 189   if (local_to_UTC < 0) {
 190     sign_local_to_UTC = '-';
 191     abs_local_to_UTC = -(abs_local_to_UTC);
 192   }
 193   // Convert time zone offset seconds to hours and minutes.
 194   const time_t zone_hours = (abs_local_to_UTC / seconds_per_hour);
 195   const time_t zone_min =
 196     ((abs_local_to_UTC % seconds_per_hour) / seconds_per_minute);
 197 
 198   // Print an ISO 8601 date and time stamp into the buffer
 199   const int year = 1900 + time_struct.tm_year;
 200   const int month = 1 + time_struct.tm_mon;
 201   const int printed = jio_snprintf(buffer, buffer_length,
 202                                    "%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d%02d",
 203                                    year,
 204                                    month,
 205                                    time_struct.tm_mday,
 206                                    time_struct.tm_hour,
 207                                    time_struct.tm_min,
 208                                    time_struct.tm_sec,
 209                                    milliseconds_after_second,
 210                                    sign_local_to_UTC,
 211                                    zone_hours,
 212                                    zone_min);
 213   if (printed == 0) {
 214     assert(false, "Failed jio_printf");
 215     return NULL;
 216   }
 217   return buffer;
 218 }
 219 
 220 OSReturn os::set_priority(Thread* thread, ThreadPriority p) {
 221   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 222 
 223   if ((p >= MinPriority && p <= MaxPriority) ||
 224       (p == CriticalPriority && thread->is_ConcurrentGC_thread())) {
 225     int priority = java_to_os_priority[p];
 226     return set_native_priority(thread, priority);
 227   } else {
 228     assert(false, "Should not happen");
 229     return OS_ERR;
 230   }
 231 }
 232 
 233 // The mapping from OS priority back to Java priority may be inexact because
 234 // Java priorities can map M:1 with native priorities. If you want the definite
 235 // Java priority then use JavaThread::java_priority()
 236 OSReturn os::get_priority(const Thread* const thread, ThreadPriority& priority) {
 237   int p;
 238   int os_prio;
 239   OSReturn ret = get_native_priority(thread, &os_prio);
 240   if (ret != OS_OK) return ret;
 241 
 242   if (java_to_os_priority[MaxPriority] > java_to_os_priority[MinPriority]) {
 243     for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] > os_prio; p--) ;
 244   } else {
 245     // niceness values are in reverse order
 246     for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] < os_prio; p--) ;
 247   }
 248   priority = (ThreadPriority)p;
 249   return OS_OK;
 250 }
 251 
 252 bool os::dll_build_name(char* buffer, size_t size, const char* fname) {
 253   int n = jio_snprintf(buffer, size, "%s%s%s", JNI_LIB_PREFIX, fname, JNI_LIB_SUFFIX);
 254   return (n != -1);
 255 }
 256 
 257 #if !defined(LINUX) && !defined(_WINDOWS)
 258 bool os::committed_in_range(address start, size_t size, address& committed_start, size_t& committed_size) {
 259   committed_start = start;
 260   committed_size = size;
 261   return true;
 262 }
 263 #endif
 264 
 265 // Helper for dll_locate_lib.
 266 // Pass buffer and printbuffer as we already printed the path to buffer
 267 // when we called get_current_directory. This way we avoid another buffer
 268 // of size MAX_PATH.
 269 static bool conc_path_file_and_check(char *buffer, char *printbuffer, size_t printbuflen,
 270                                      const char* pname, char lastchar, const char* fname) {
 271 
 272   // Concatenate path and file name, but don't print double path separators.
 273   const char *filesep = (WINDOWS_ONLY(lastchar == ':' ||) lastchar == os::file_separator()[0]) ?
 274                         "" : os::file_separator();
 275   int ret = jio_snprintf(printbuffer, printbuflen, "%s%s%s", pname, filesep, fname);
 276   // Check whether file exists.
 277   if (ret != -1) {
 278     struct stat statbuf;
 279     return os::stat(buffer, &statbuf) == 0;
 280   }
 281   return false;
 282 }
 283 
 284 // Frees all memory allocated on the heap for the
 285 // supplied array of arrays of chars (a), where n
 286 // is the number of elements in the array.
 287 static void free_array_of_char_arrays(char** a, size_t n) {
 288       while (n > 0) {
 289           n--;
 290           if (a[n] != NULL) {
 291             FREE_C_HEAP_ARRAY(char, a[n]);
 292           }
 293       }
 294       FREE_C_HEAP_ARRAY(char*, a);
 295 }
 296 
 297 bool os::dll_locate_lib(char *buffer, size_t buflen,
 298                         const char* pname, const char* fname) {
 299   bool retval = false;
 300 
 301   size_t fullfnamelen = strlen(JNI_LIB_PREFIX) + strlen(fname) + strlen(JNI_LIB_SUFFIX);
 302   char* fullfname = NEW_C_HEAP_ARRAY(char, fullfnamelen + 1, mtInternal);
 303   if (dll_build_name(fullfname, fullfnamelen + 1, fname)) {
 304     const size_t pnamelen = pname ? strlen(pname) : 0;
 305 
 306     if (pnamelen == 0) {
 307       // If no path given, use current working directory.
 308       const char* p = get_current_directory(buffer, buflen);
 309       if (p != NULL) {
 310         const size_t plen = strlen(buffer);
 311         const char lastchar = buffer[plen - 1];
 312         retval = conc_path_file_and_check(buffer, &buffer[plen], buflen - plen,
 313                                           "", lastchar, fullfname);
 314       }
 315     } else if (strchr(pname, *os::path_separator()) != NULL) {
 316       // A list of paths. Search for the path that contains the library.
 317       size_t n;
 318       char** pelements = split_path(pname, &n, fullfnamelen);
 319       if (pelements != NULL) {
 320         for (size_t i = 0; i < n; i++) {
 321           char* path = pelements[i];
 322           // Really shouldn't be NULL, but check can't hurt.
 323           size_t plen = (path == NULL) ? 0 : strlen(path);
 324           if (plen == 0) {
 325             continue; // Skip the empty path values.
 326           }
 327           const char lastchar = path[plen - 1];
 328           retval = conc_path_file_and_check(buffer, buffer, buflen, path, lastchar, fullfname);
 329           if (retval) break;
 330         }
 331         // Release the storage allocated by split_path.
 332         free_array_of_char_arrays(pelements, n);
 333       }
 334     } else {
 335       // A definite path.
 336       const char lastchar = pname[pnamelen-1];
 337       retval = conc_path_file_and_check(buffer, buffer, buflen, pname, lastchar, fullfname);
 338     }
 339   }
 340 
 341   FREE_C_HEAP_ARRAY(char*, fullfname);
 342   return retval;
 343 }
 344 
 345 // --------------------- sun.misc.Signal (optional) ---------------------
 346 
 347 
 348 // SIGBREAK is sent by the keyboard to query the VM state
 349 #ifndef SIGBREAK
 350 #define SIGBREAK SIGQUIT
 351 #endif
 352 
 353 // sigexitnum_pd is a platform-specific special signal used for terminating the Signal thread.
 354 
 355 
 356 static void signal_thread_entry(JavaThread* thread, TRAPS) {
 357   os::set_priority(thread, NearMaxPriority);
 358   while (true) {
 359     int sig;
 360     {
 361       // FIXME : Currently we have not decided what should be the status
 362       //         for this java thread blocked here. Once we decide about
 363       //         that we should fix this.
 364       sig = os::signal_wait();
 365     }
 366     if (sig == os::sigexitnum_pd()) {
 367        // Terminate the signal thread
 368        return;
 369     }
 370 
 371     switch (sig) {
 372       case SIGBREAK: {
 373 #if INCLUDE_SERVICES
 374         // Check if the signal is a trigger to start the Attach Listener - in that
 375         // case don't print stack traces.
 376         if (!DisableAttachMechanism) {
 377           // Attempt to transit state to AL_INITIALIZING.
 378           AttachListenerState cur_state = AttachListener::transit_state(AL_INITIALIZING, AL_NOT_INITIALIZED);
 379           if (cur_state == AL_INITIALIZING) {
 380             // Attach Listener has been started to initialize. Ignore this signal.
 381             continue;
 382           } else if (cur_state == AL_NOT_INITIALIZED) {
 383             // Start to initialize.
 384             if (AttachListener::is_init_trigger()) {
 385               // Attach Listener has been initialized.
 386               // Accept subsequent request.
 387               continue;
 388             } else {
 389               // Attach Listener could not be started.
 390               // So we need to transit the state to AL_NOT_INITIALIZED.
 391               AttachListener::set_state(AL_NOT_INITIALIZED);
 392             }
 393           } else if (AttachListener::check_socket_file()) {
 394             // Attach Listener has been started, but unix domain socket file
 395             // does not exist. So restart Attach Listener.
 396             continue;
 397           }
 398         }
 399 #endif
 400         // Print stack traces
 401         // Any SIGBREAK operations added here should make sure to flush
 402         // the output stream (e.g. tty->flush()) after output.  See 4803766.
 403         // Each module also prints an extra carriage return after its output.
 404         VM_PrintThreads op(tty, PrintConcurrentLocks, false /* no extended info */, true /* print JNI handle info */);
 405         VMThread::execute(&op);
 406         VM_FindDeadlocks op1(tty);
 407         VMThread::execute(&op1);
 408         Universe::print_heap_at_SIGBREAK();
 409         if (PrintClassHistogram) {
 410           VM_GC_HeapInspection op1(tty, true /* force full GC before heap inspection */);
 411           VMThread::execute(&op1);
 412         }
 413         if (JvmtiExport::should_post_data_dump()) {
 414           JvmtiExport::post_data_dump();
 415         }
 416         break;
 417       }
 418       default: {
 419         // Dispatch the signal to java
 420         HandleMark hm(THREAD);
 421         Klass* klass = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_misc_Signal(), THREAD);
 422         if (klass != NULL) {
 423           JavaValue result(T_VOID);
 424           JavaCallArguments args;
 425           args.push_int(sig);
 426           JavaCalls::call_static(
 427             &result,
 428             klass,
 429             vmSymbols::dispatch_name(),
 430             vmSymbols::int_void_signature(),
 431             &args,
 432             THREAD
 433           );
 434         }
 435         if (HAS_PENDING_EXCEPTION) {
 436           // tty is initialized early so we don't expect it to be null, but
 437           // if it is we can't risk doing an initialization that might
 438           // trigger additional out-of-memory conditions
 439           if (tty != NULL) {
 440             char klass_name[256];
 441             char tmp_sig_name[16];
 442             const char* sig_name = "UNKNOWN";
 443             InstanceKlass::cast(PENDING_EXCEPTION->klass())->
 444               name()->as_klass_external_name(klass_name, 256);
 445             if (os::exception_name(sig, tmp_sig_name, 16) != NULL)
 446               sig_name = tmp_sig_name;
 447             warning("Exception %s occurred dispatching signal %s to handler"
 448                     "- the VM may need to be forcibly terminated",
 449                     klass_name, sig_name );
 450           }
 451           CLEAR_PENDING_EXCEPTION;
 452         }
 453       }
 454     }
 455   }
 456 }
 457 
 458 void os::init_before_ergo() {
 459   initialize_initial_active_processor_count();
 460   // We need to initialize large page support here because ergonomics takes some
 461   // decisions depending on large page support and the calculated large page size.
 462   large_page_init();
 463 
 464   StackOverflow::initialize_stack_zone_sizes();
 465 
 466   // VM version initialization identifies some characteristics of the
 467   // platform that are used during ergonomic decisions.
 468   VM_Version::init_before_ergo();
 469 }
 470 
 471 void os::initialize_jdk_signal_support(TRAPS) {
 472   if (!ReduceSignalUsage) {
 473     // Setup JavaThread for processing signals
 474     const char* name = "Signal Dispatcher";
 475     Handle thread_oop = JavaThread::create_system_thread_object(name, true /* visible */, CHECK);
 476 
 477     JavaThread* thread = new JavaThread(&signal_thread_entry);
 478     JavaThread::vm_exit_on_osthread_failure(thread);
 479 
 480     JavaThread::start_internal_daemon(THREAD, thread, thread_oop, NearMaxPriority);
 481 
 482     // Handle ^BREAK
 483     os::signal(SIGBREAK, os::user_handler());
 484   }
 485 }
 486 
 487 
 488 void os::terminate_signal_thread() {
 489   if (!ReduceSignalUsage)
 490     signal_notify(sigexitnum_pd());
 491 }
 492 
 493 
 494 // --------------------- loading libraries ---------------------
 495 
 496 typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
 497 extern struct JavaVM_ main_vm;
 498 
 499 static void* _native_java_library = NULL;
 500 
 501 void* os::native_java_library() {
 502   if (_native_java_library == NULL) {
 503     char buffer[JVM_MAXPATHLEN];
 504     char ebuf[1024];
 505 
 506     // Load java dll
 507     if (dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
 508                        "java")) {
 509       _native_java_library = dll_load(buffer, ebuf, sizeof(ebuf));
 510     }
 511     if (_native_java_library == NULL) {
 512       vm_exit_during_initialization("Unable to load native library", ebuf);
 513     }
 514 
 515 #if defined(__OpenBSD__)
 516     // Work-around OpenBSD's lack of $ORIGIN support by pre-loading libnet.so
 517     // ignore errors
 518     if (dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
 519                        "net")) {
 520       dll_load(buffer, ebuf, sizeof(ebuf));
 521     }
 522 #endif
 523   }
 524   return _native_java_library;
 525 }
 526 
 527 /*
 528  * Support for finding Agent_On(Un)Load/Attach<_lib_name> if it exists.
 529  * If check_lib == true then we are looking for an
 530  * Agent_OnLoad_lib_name or Agent_OnAttach_lib_name function to determine if
 531  * this library is statically linked into the image.
 532  * If check_lib == false then we will look for the appropriate symbol in the
 533  * executable if agent_lib->is_static_lib() == true or in the shared library
 534  * referenced by 'handle'.
 535  */
 536 void* os::find_agent_function(AgentLibrary *agent_lib, bool check_lib,
 537                               const char *syms[], size_t syms_len) {
 538   assert(agent_lib != NULL, "sanity check");
 539   const char *lib_name;
 540   void *handle = agent_lib->os_lib();
 541   void *entryName = NULL;
 542   char *agent_function_name;
 543   size_t i;
 544 
 545   // If checking then use the agent name otherwise test is_static_lib() to
 546   // see how to process this lookup
 547   lib_name = ((check_lib || agent_lib->is_static_lib()) ? agent_lib->name() : NULL);
 548   for (i = 0; i < syms_len; i++) {
 549     agent_function_name = build_agent_function_name(syms[i], lib_name, agent_lib->is_absolute_path());
 550     if (agent_function_name == NULL) {
 551       break;
 552     }
 553     entryName = dll_lookup(handle, agent_function_name);
 554     FREE_C_HEAP_ARRAY(char, agent_function_name);
 555     if (entryName != NULL) {
 556       break;
 557     }
 558   }
 559   return entryName;
 560 }
 561 
 562 // See if the passed in agent is statically linked into the VM image.
 563 bool os::find_builtin_agent(AgentLibrary *agent_lib, const char *syms[],
 564                             size_t syms_len) {
 565   void *ret;
 566   void *proc_handle;
 567   void *save_handle;
 568 
 569   assert(agent_lib != NULL, "sanity check");
 570   if (agent_lib->name() == NULL) {
 571     return false;
 572   }
 573   proc_handle = get_default_process_handle();
 574   // Check for Agent_OnLoad/Attach_lib_name function
 575   save_handle = agent_lib->os_lib();
 576   // We want to look in this process' symbol table.
 577   agent_lib->set_os_lib(proc_handle);
 578   ret = find_agent_function(agent_lib, true, syms, syms_len);
 579   if (ret != NULL) {
 580     // Found an entry point like Agent_OnLoad_lib_name so we have a static agent
 581     agent_lib->set_valid();
 582     agent_lib->set_static_lib(true);
 583     return true;
 584   }
 585   agent_lib->set_os_lib(save_handle);
 586   return false;
 587 }
 588 
 589 // --------------------- heap allocation utilities ---------------------
 590 
 591 char *os::strdup(const char *str, MEMFLAGS flags) {
 592   size_t size = strlen(str);
 593   char *dup_str = (char *)malloc(size + 1, flags);
 594   if (dup_str == NULL) return NULL;
 595   strcpy(dup_str, str);
 596   return dup_str;
 597 }
 598 
 599 char* os::strdup_check_oom(const char* str, MEMFLAGS flags) {
 600   char* p = os::strdup(str, flags);
 601   if (p == NULL) {
 602     vm_exit_out_of_memory(strlen(str) + 1, OOM_MALLOC_ERROR, "os::strdup_check_oom");
 603   }
 604   return p;
 605 }
 606 
 607 
 608 #define paranoid                 0  /* only set to 1 if you suspect checking code has bug */
 609 
 610 #ifdef ASSERT
 611 
 612 static void verify_memory(void* ptr) {
 613   GuardedMemory guarded(ptr);
 614   if (!guarded.verify_guards()) {
 615     LogTarget(Warning, malloc, free) lt;
 616     ResourceMark rm;
 617     LogStream ls(lt);
 618     ls.print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 619     ls.print_cr("## memory stomp:");
 620     guarded.print_on(&ls);
 621     fatal("memory stomping error");
 622   }
 623 }
 624 
 625 #endif
 626 
 627 //
 628 // This function supports testing of the malloc out of memory
 629 // condition without really running the system out of memory.
 630 //
 631 static bool has_reached_max_malloc_test_peak(size_t alloc_size) {
 632   if (MallocMaxTestWords > 0) {
 633     size_t words = (alloc_size / BytesPerWord);
 634 
 635     if ((cur_malloc_words + words) > MallocMaxTestWords) {
 636       return true;
 637     }
 638     Atomic::add(&cur_malloc_words, words);
 639   }
 640   return false;
 641 }
 642 
 643 void* os::malloc(size_t size, MEMFLAGS flags) {
 644   return os::malloc(size, flags, CALLER_PC);
 645 }
 646 
 647 void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 648   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 649   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 650 
 651 #if INCLUDE_NMT
 652   {
 653     void* rc = NULL;
 654     if (NMTPreInit::handle_malloc(&rc, size)) {
 655       return rc;
 656     }
 657   }
 658 #endif
 659 
 660   // Since os::malloc can be called when the libjvm.{dll,so} is
 661   // first loaded and we don't have a thread yet we must accept NULL also here.
 662   assert(!os::ThreadCrashProtection::is_crash_protected(Thread::current_or_null()),
 663          "malloc() not allowed when crash protection is set");
 664 
 665   if (size == 0) {
 666     // return a valid pointer if size is zero
 667     // if NULL is returned the calling functions assume out of memory.
 668     size = 1;
 669   }
 670 
 671   // NMT support
 672   NMT_TrackingLevel level = MemTracker::tracking_level();
 673   const size_t nmt_overhead =
 674       MemTracker::malloc_header_size(level) + MemTracker::malloc_footer_size(level);
 675 
 676 #ifndef ASSERT
 677   const size_t alloc_size = size + nmt_overhead;
 678 #else
 679   const size_t alloc_size = GuardedMemory::get_total_size(size + nmt_overhead);
 680   if (size + nmt_overhead > alloc_size) { // Check for rollover.
 681     return NULL;
 682   }
 683 #endif
 684 
 685   // For the test flag -XX:MallocMaxTestWords
 686   if (has_reached_max_malloc_test_peak(size)) {
 687     return NULL;
 688   }
 689 
 690   u_char* ptr;
 691   ptr = (u_char*)::malloc(alloc_size);
 692 
 693 #ifdef ASSERT
 694   if (ptr == NULL) {
 695     return NULL;
 696   }
 697   // Wrap memory with guard
 698   GuardedMemory guarded(ptr, size + nmt_overhead);
 699   ptr = guarded.get_user_ptr();
 700 
 701   if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
 702     log_warning(malloc, free)("os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, p2i(ptr));
 703     breakpoint();
 704   }
 705   if (paranoid) {
 706     verify_memory(ptr);
 707   }
 708 #endif
 709 
 710   // we do not track guard memory
 711   return MemTracker::record_malloc((address)ptr, size, memflags, stack, level);
 712 }
 713 
 714 void* os::realloc(void *memblock, size_t size, MEMFLAGS flags) {
 715   return os::realloc(memblock, size, flags, CALLER_PC);
 716 }
 717 
 718 void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 719 
 720 #if INCLUDE_NMT
 721   {
 722     void* rc = NULL;
 723     if (NMTPreInit::handle_realloc(&rc, memblock, size)) {
 724       return rc;
 725     }
 726   }
 727 #endif
 728 
 729   // For the test flag -XX:MallocMaxTestWords
 730   if (has_reached_max_malloc_test_peak(size)) {
 731     return NULL;
 732   }
 733 
 734   if (size == 0) {
 735     // return a valid pointer if size is zero
 736     // if NULL is returned the calling functions assume out of memory.
 737     size = 1;
 738   }
 739 
 740 #ifndef ASSERT
 741   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 742   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 743    // NMT support
 744   NMT_TrackingLevel level = MemTracker::tracking_level();
 745   void* membase = MemTracker::record_free(memblock, level);
 746   const size_t nmt_overhead =
 747       MemTracker::malloc_header_size(level) + MemTracker::malloc_footer_size(level);
 748   void* ptr = ::realloc(membase, size + nmt_overhead);
 749   return MemTracker::record_malloc(ptr, size, memflags, stack, level);
 750 #else
 751   if (memblock == NULL) {
 752     return os::malloc(size, memflags, stack);
 753   }
 754   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 755     log_warning(malloc, free)("os::realloc caught " PTR_FORMAT, p2i(memblock));
 756     breakpoint();
 757   }
 758   // NMT support
 759   void* membase = MemTracker::malloc_base(memblock);
 760   verify_memory(membase);
 761   // always move the block
 762   void* ptr = os::malloc(size, memflags, stack);
 763   // Copy to new memory if malloc didn't fail
 764   if (ptr != NULL ) {
 765     GuardedMemory guarded(MemTracker::malloc_base(memblock));
 766     // Guard's user data contains NMT header
 767     NMT_TrackingLevel level = MemTracker::tracking_level();
 768     const size_t nmt_overhead =
 769         MemTracker::malloc_header_size(level) + MemTracker::malloc_footer_size(level);
 770     size_t memblock_size = guarded.get_user_size() - nmt_overhead;
 771     memcpy(ptr, memblock, MIN2(size, memblock_size));
 772     if (paranoid) {
 773       verify_memory(MemTracker::malloc_base(ptr));
 774     }
 775     os::free(memblock);
 776   }
 777   return ptr;
 778 #endif
 779 }
 780 
 781 // handles NULL pointers
 782 void  os::free(void *memblock) {
 783 
 784 #if INCLUDE_NMT
 785   if (NMTPreInit::handle_free(memblock)) {
 786     return;
 787   }
 788 #endif
 789 
 790   NOT_PRODUCT(inc_stat_counter(&num_frees, 1));
 791 #ifdef ASSERT
 792   if (memblock == NULL) return;
 793   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 794     log_warning(malloc, free)("os::free caught " PTR_FORMAT, p2i(memblock));
 795     breakpoint();
 796   }
 797   void* membase = MemTracker::record_free(memblock, MemTracker::tracking_level());
 798   verify_memory(membase);
 799 
 800   GuardedMemory guarded(membase);
 801   size_t size = guarded.get_user_size();
 802   inc_stat_counter(&free_bytes, size);
 803   membase = guarded.release_for_freeing();
 804   ::free(membase);
 805 #else
 806   void* membase = MemTracker::record_free(memblock, MemTracker::tracking_level());
 807   ::free(membase);
 808 #endif
 809 }
 810 
 811 void os::init_random(unsigned int initval) {
 812   _rand_seed = initval;
 813 }
 814 
 815 
 816 int os::next_random(unsigned int rand_seed) {
 817   /* standard, well-known linear congruential random generator with
 818    * next_rand = (16807*seed) mod (2**31-1)
 819    * see
 820    * (1) "Random Number Generators: Good Ones Are Hard to Find",
 821    *      S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
 822    * (2) "Two Fast Implementations of the 'Minimal Standard' Random
 823    *     Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), pp. 87-88.
 824   */
 825   const unsigned int a = 16807;
 826   const unsigned int m = 2147483647;
 827   const int q = m / a;        assert(q == 127773, "weird math");
 828   const int r = m % a;        assert(r == 2836, "weird math");
 829 
 830   // compute az=2^31p+q
 831   unsigned int lo = a * (rand_seed & 0xFFFF);
 832   unsigned int hi = a * (rand_seed >> 16);
 833   lo += (hi & 0x7FFF) << 16;
 834 
 835   // if q overflowed, ignore the overflow and increment q
 836   if (lo > m) {
 837     lo &= m;
 838     ++lo;
 839   }
 840   lo += hi >> 15;
 841 
 842   // if (p+q) overflowed, ignore the overflow and increment (p+q)
 843   if (lo > m) {
 844     lo &= m;
 845     ++lo;
 846   }
 847   return lo;
 848 }
 849 
 850 int os::random() {
 851   // Make updating the random seed thread safe.
 852   while (true) {
 853     unsigned int seed = _rand_seed;
 854     unsigned int rand = next_random(seed);
 855     if (Atomic::cmpxchg(&_rand_seed, seed, rand, memory_order_relaxed) == seed) {
 856       return static_cast<int>(rand);
 857     }
 858   }
 859 }
 860 
 861 // The INITIALIZED state is distinguished from the SUSPENDED state because the
 862 // conditions in which a thread is first started are different from those in which
 863 // a suspension is resumed.  These differences make it hard for us to apply the
 864 // tougher checks when starting threads that we want to do when resuming them.
 865 // However, when start_thread is called as a result of Thread.start, on a Java
 866 // thread, the operation is synchronized on the Java Thread object.  So there
 867 // cannot be a race to start the thread and hence for the thread to exit while
 868 // we are working on it.  Non-Java threads that start Java threads either have
 869 // to do so in a context in which races are impossible, or should do appropriate
 870 // locking.
 871 
 872 void os::start_thread(Thread* thread) {
 873   OSThread* osthread = thread->osthread();
 874   osthread->set_state(RUNNABLE);
 875   pd_start_thread(thread);
 876 }
 877 
 878 void os::abort(bool dump_core) {
 879   abort(dump_core && CreateCoredumpOnCrash, NULL, NULL);
 880 }
 881 
 882 //---------------------------------------------------------------------------
 883 // Helper functions for fatal error handler
 884 
 885 bool os::print_function_and_library_name(outputStream* st,
 886                                          address addr,
 887                                          char* buf, int buflen,
 888                                          bool shorten_paths,
 889                                          bool demangle,
 890                                          bool strip_arguments) {
 891   // If no scratch buffer given, allocate one here on stack.
 892   // (used during error handling; its a coin toss, really, if on-stack allocation
 893   //  is worse than (raw) C-heap allocation in that case).
 894   char* p = buf;
 895   if (p == NULL) {
 896     p = (char*)::alloca(O_BUFLEN);
 897     buflen = O_BUFLEN;
 898   }
 899   int offset = 0;
 900   bool have_function_name = dll_address_to_function_name(addr, p, buflen,
 901                                                          &offset, demangle);
 902   bool is_function_descriptor = false;
 903 #ifdef HAVE_FUNCTION_DESCRIPTORS
 904   // When we deal with a function descriptor instead of a real code pointer, try to
 905   // resolve it. There is a small chance that a random pointer given to this function
 906   // may just happen to look like a valid descriptor, but this is rare and worth the
 907   // risk to see resolved function names. But we will print a little suffix to mark
 908   // this as a function descriptor for the reader (see below).
 909   if (!have_function_name && os::is_readable_pointer(addr)) {
 910     address addr2 = (address)os::resolve_function_descriptor(addr);
 911     if (have_function_name = is_function_descriptor =
 912         dll_address_to_function_name(addr2, p, buflen, &offset, demangle)) {
 913       addr = addr2;
 914     }
 915   }
 916 #endif // HAVE_FUNCTION_DESCRIPTORS
 917 
 918   if (have_function_name) {
 919     // Print function name, optionally demangled
 920     if (demangle && strip_arguments) {
 921       char* args_start = strchr(p, '(');
 922       if (args_start != NULL) {
 923         *args_start = '\0';
 924       }
 925     }
 926     // Print offset. Omit printing if offset is zero, which makes the output
 927     // more readable if we print function pointers.
 928     if (offset == 0) {
 929       st->print("%s", p);
 930     } else {
 931       st->print("%s+%d", p, offset);
 932     }
 933   } else {
 934     st->print(PTR_FORMAT, p2i(addr));
 935   }
 936   offset = 0;
 937 
 938   const bool have_library_name = dll_address_to_library_name(addr, p, buflen, &offset);
 939   if (have_library_name) {
 940     // Cut path parts
 941     if (shorten_paths) {
 942       char* p2 = strrchr(p, os::file_separator()[0]);
 943       if (p2 != NULL) {
 944         p = p2 + 1;
 945       }
 946     }
 947     st->print(" in %s", p);
 948     if (!have_function_name) { // Omit offset if we already printed the function offset
 949       st->print("+%d", offset);
 950     }
 951   }
 952 
 953   // Write a trailing marker if this was a function descriptor
 954   if (have_function_name && is_function_descriptor) {
 955     st->print_raw(" (FD)");
 956   }
 957 
 958   return have_function_name || have_library_name;
 959 }
 960 
 961 void os::print_hex_dump(outputStream* st, address start, address end, int unitsize,
 962                         int bytes_per_line, address logical_start) {
 963   assert(unitsize == 1 || unitsize == 2 || unitsize == 4 || unitsize == 8, "just checking");
 964 
 965   start = align_down(start, unitsize);
 966   logical_start = align_down(logical_start, unitsize);
 967   bytes_per_line = align_up(bytes_per_line, 8);
 968 
 969   int cols = 0;
 970   int cols_per_line = bytes_per_line / unitsize;
 971 
 972   address p = start;
 973   address logical_p = logical_start;
 974 
 975   // Print out the addresses as if we were starting from logical_start.
 976   st->print(PTR_FORMAT ":   ", p2i(logical_p));
 977   while (p < end) {
 978     if (is_readable_pointer(p)) {
 979       switch (unitsize) {
 980         case 1: st->print("%02x", *(u1*)p); break;
 981         case 2: st->print("%04x", *(u2*)p); break;
 982         case 4: st->print("%08x", *(u4*)p); break;
 983         case 8: st->print("%016" FORMAT64_MODIFIER "x", *(u8*)p); break;
 984       }
 985     } else {
 986       st->print("%*.*s", 2*unitsize, 2*unitsize, "????????????????");
 987     }
 988     p += unitsize;
 989     logical_p += unitsize;
 990     cols++;
 991     if (cols >= cols_per_line && p < end) {
 992        cols = 0;
 993        st->cr();
 994        st->print(PTR_FORMAT ":   ", p2i(logical_p));
 995     } else {
 996        st->print(" ");
 997     }
 998   }
 999   st->cr();
1000 }
1001 
1002 void os::print_dhm(outputStream* st, const char* startStr, long sec) {
1003   long days    = sec/86400;
1004   long hours   = (sec/3600) - (days * 24);
1005   long minutes = (sec/60) - (days * 1440) - (hours * 60);
1006   if (startStr == NULL) startStr = "";
1007   st->print_cr("%s %ld days %ld:%02ld hours", startStr, days, hours, minutes);
1008 }
1009 
1010 void os::print_instructions(outputStream* st, address pc, int unitsize) {
1011   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", p2i(pc));
1012   print_hex_dump(st, pc - 256, pc + 256, unitsize);
1013 }
1014 
1015 void os::print_environment_variables(outputStream* st, const char** env_list) {
1016   if (env_list) {
1017     st->print_cr("Environment Variables:");
1018 
1019     for (int i = 0; env_list[i] != NULL; i++) {
1020       char *envvar = ::getenv(env_list[i]);
1021       if (envvar != NULL) {
1022         st->print("%s", env_list[i]);
1023         st->print("=");
1024         st->print("%s", envvar);
1025         // Use separate cr() printing to avoid unnecessary buffer operations that might cause truncation.
1026         st->cr();
1027       }
1028     }
1029   }
1030 }
1031 
1032 void os::print_cpu_info(outputStream* st, char* buf, size_t buflen) {
1033   // cpu
1034   st->print("CPU:");
1035 #if defined(__APPLE__) && !defined(ZERO)
1036    if (VM_Version::is_cpu_emulated()) {
1037      st->print(" (EMULATED)");
1038    }
1039 #endif
1040   st->print(" total %d", os::processor_count());
1041   // It's not safe to query number of active processors after crash
1042   // st->print("(active %d)", os::active_processor_count()); but we can
1043   // print the initial number of active processors.
1044   // We access the raw value here because the assert in the accessor will
1045   // fail if the crash occurs before initialization of this value.
1046   st->print(" (initial active %d)", _initial_active_processor_count);
1047   st->print(" %s", VM_Version::features_string());
1048   st->cr();
1049   pd_print_cpu_info(st, buf, buflen);
1050 }
1051 
1052 // Print a one line string summarizing the cpu, number of cores, memory, and operating system version
1053 void os::print_summary_info(outputStream* st, char* buf, size_t buflen) {
1054   st->print("Host: ");
1055 #ifndef PRODUCT
1056   if (get_host_name(buf, buflen)) {
1057     st->print("%s, ", buf);
1058   }
1059 #endif // PRODUCT
1060   get_summary_cpu_info(buf, buflen);
1061   st->print("%s, ", buf);
1062   size_t mem = physical_memory()/G;
1063   if (mem == 0) {  // for low memory systems
1064     mem = physical_memory()/M;
1065     st->print("%d cores, " SIZE_FORMAT "M, ", processor_count(), mem);
1066   } else {
1067     st->print("%d cores, " SIZE_FORMAT "G, ", processor_count(), mem);
1068   }
1069   get_summary_os_info(buf, buflen);
1070   st->print_raw(buf);
1071   st->cr();
1072 }
1073 
1074 void os::print_date_and_time(outputStream *st, char* buf, size_t buflen) {
1075   const int secs_per_day  = 86400;
1076   const int secs_per_hour = 3600;
1077   const int secs_per_min  = 60;
1078 
1079   time_t tloc;
1080   (void)time(&tloc);
1081   char* timestring = ctime(&tloc);  // ctime adds newline.
1082   // edit out the newline
1083   char* nl = strchr(timestring, '\n');
1084   if (nl != NULL) {
1085     *nl = '\0';
1086   }
1087 
1088   struct tm tz;
1089   if (localtime_pd(&tloc, &tz) != NULL) {
1090     wchar_t w_buf[80];
1091     size_t n = ::wcsftime(w_buf, 80, L"%Z", &tz);
1092     if (n > 0) {
1093       ::wcstombs(buf, w_buf, buflen);
1094       st->print("Time: %s %s", timestring, buf);
1095     } else {
1096       st->print("Time: %s", timestring);
1097     }
1098   } else {
1099     st->print("Time: %s", timestring);
1100   }
1101 
1102   double t = os::elapsedTime();
1103   // NOTE: a crash using printf("%f",...) on Linux was historically noted here.
1104   int eltime = (int)t;  // elapsed time in seconds
1105   int eltimeFraction = (int) ((t - eltime) * 1000000);
1106 
1107   // print elapsed time in a human-readable format:
1108   int eldays = eltime / secs_per_day;
1109   int day_secs = eldays * secs_per_day;
1110   int elhours = (eltime - day_secs) / secs_per_hour;
1111   int hour_secs = elhours * secs_per_hour;
1112   int elmins = (eltime - day_secs - hour_secs) / secs_per_min;
1113   int minute_secs = elmins * secs_per_min;
1114   int elsecs = (eltime - day_secs - hour_secs - minute_secs);
1115   st->print_cr(" elapsed time: %d.%06d seconds (%dd %dh %dm %ds)", eltime, eltimeFraction, eldays, elhours, elmins, elsecs);
1116 }
1117 
1118 
1119 // Check if pointer can be read from (4-byte read access).
1120 // Helps to prove validity of a not-NULL pointer.
1121 // Returns true in very early stages of VM life when stub is not yet generated.
1122 #define SAFEFETCH_DEFAULT true
1123 bool os::is_readable_pointer(const void* p) {
1124   if (!CanUseSafeFetch32()) {
1125     return SAFEFETCH_DEFAULT;
1126   }
1127   int* const aligned = (int*) align_down((intptr_t)p, 4);
1128   int cafebabe = 0xcafebabe;  // tester value 1
1129   int deadbeef = 0xdeadbeef;  // tester value 2
1130   return (SafeFetch32(aligned, cafebabe) != cafebabe) || (SafeFetch32(aligned, deadbeef) != deadbeef);
1131 }
1132 
1133 bool os::is_readable_range(const void* from, const void* to) {
1134   if ((uintptr_t)from >= (uintptr_t)to) return false;
1135   for (uintptr_t p = align_down((uintptr_t)from, min_page_size()); p < (uintptr_t)to; p += min_page_size()) {
1136     if (!is_readable_pointer((const void*)p)) {
1137       return false;
1138     }
1139   }
1140   return true;
1141 }
1142 
1143 
1144 // moved from debug.cpp (used to be find()) but still called from there
1145 // The verbose parameter is only set by the debug code in one case
1146 void os::print_location(outputStream* st, intptr_t x, bool verbose) {
1147   address addr = (address)x;
1148   // Handle NULL first, so later checks don't need to protect against it.
1149   if (addr == NULL) {
1150     st->print_cr("0x0 is NULL");
1151     return;
1152   }
1153 
1154   // Check if addr points into a code blob.
1155   CodeBlob* b = CodeCache::find_blob_unsafe(addr);
1156   if (b != NULL) {
1157     b->dump_for_addr(addr, st, verbose);
1158     return;
1159   }
1160 
1161   // Check if addr points into Java heap.
1162   if (Universe::heap()->print_location(st, addr)) {
1163     return;
1164   }
1165 
1166   bool accessible = is_readable_pointer(addr);
1167 
1168   // Check if addr is a JNI handle.
1169   if (align_down((intptr_t)addr, sizeof(intptr_t)) != 0 && accessible) {
1170     if (JNIHandles::is_global_handle((jobject) addr)) {
1171       st->print_cr(INTPTR_FORMAT " is a global jni handle", p2i(addr));
1172       return;
1173     }
1174     if (JNIHandles::is_weak_global_handle((jobject) addr)) {
1175       st->print_cr(INTPTR_FORMAT " is a weak global jni handle", p2i(addr));
1176       return;
1177     }
1178   }
1179 
1180   // Check if addr belongs to a Java thread.
1181   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
1182     // If the addr is a java thread print information about that.
1183     if (addr == (address)thread) {
1184       if (verbose) {
1185         thread->print_on(st);
1186       } else {
1187         st->print_cr(INTPTR_FORMAT " is a thread", p2i(addr));
1188       }
1189       return;
1190     }
1191     // If the addr is in the stack region for this thread then report that
1192     // and print thread info
1193     if (thread->is_in_full_stack(addr)) {
1194       st->print_cr(INTPTR_FORMAT " is pointing into the stack for thread: "
1195                    INTPTR_FORMAT, p2i(addr), p2i(thread));
1196       if (verbose) thread->print_on(st);
1197       return;
1198     }
1199   }
1200 
1201   // Check if in metaspace and print types that have vptrs
1202   if (Metaspace::contains(addr)) {
1203     if (Klass::is_valid((Klass*)addr)) {
1204       st->print_cr(INTPTR_FORMAT " is a pointer to class: ", p2i(addr));
1205       ((Klass*)addr)->print_on(st);
1206     } else if (Method::is_valid_method((const Method*)addr)) {
1207       ((Method*)addr)->print_value_on(st);
1208       st->cr();
1209     } else {
1210       // Use addr->print() from the debugger instead (not here)
1211       st->print_cr(INTPTR_FORMAT " is pointing into metadata", p2i(addr));
1212     }
1213     return;
1214   }
1215 
1216   // Compressed klass needs to be decoded first.
1217 #ifdef _LP64
1218   if (UseCompressedClassPointers && ((uintptr_t)addr &~ (uintptr_t)max_juint) == 0) {
1219     narrowKlass narrow_klass = (narrowKlass)(uintptr_t)addr;
1220     Klass* k = CompressedKlassPointers::decode_raw(narrow_klass);
1221 
1222     if (Klass::is_valid(k)) {
1223       st->print_cr(UINT32_FORMAT " is a compressed pointer to class: " INTPTR_FORMAT, narrow_klass, p2i((HeapWord*)k));
1224       k->print_on(st);
1225       return;
1226     }
1227   }
1228 #endif
1229 
1230   // Try an OS specific find
1231   if (os::find(addr, st)) {
1232     return;
1233   }
1234 
1235   if (accessible) {
1236     st->print(INTPTR_FORMAT " points into unknown readable memory:", p2i(addr));
1237     if (is_aligned(addr, sizeof(intptr_t))) {
1238       st->print(" " PTR_FORMAT " |", *(intptr_t*)addr);
1239     }
1240     for (address p = addr; p < align_up(addr + 1, sizeof(intptr_t)); ++p) {
1241       st->print(" %02x", *(u1*)p);
1242     }
1243     st->cr();
1244     return;
1245   }
1246 
1247   st->print_cr(INTPTR_FORMAT " is an unknown value", p2i(addr));
1248 }
1249 
1250 // Looks like all platforms can use the same function to check if C
1251 // stack is walkable beyond current frame.
1252 bool os::is_first_C_frame(frame* fr) {
1253 
1254 #ifdef _WINDOWS
1255   return true; // native stack isn't walkable on windows this way.
1256 #endif
1257 
1258   // Load up sp, fp, sender sp and sender fp, check for reasonable values.
1259   // Check usp first, because if that's bad the other accessors may fault
1260   // on some architectures.  Ditto ufp second, etc.
1261   uintptr_t fp_align_mask = (uintptr_t)(sizeof(address)-1);
1262   // sp on amd can be 32 bit aligned.
1263   uintptr_t sp_align_mask = (uintptr_t)(sizeof(int)-1);
1264 
1265   uintptr_t usp    = (uintptr_t)fr->sp();
1266   if ((usp & sp_align_mask) != 0) return true;
1267 
1268   uintptr_t ufp    = (uintptr_t)fr->fp();
1269   if ((ufp & fp_align_mask) != 0) return true;
1270 
1271   uintptr_t old_sp = (uintptr_t)fr->sender_sp();
1272   if ((old_sp & sp_align_mask) != 0) return true;
1273   if (old_sp == 0 || old_sp == (uintptr_t)-1) return true;
1274 
1275   uintptr_t old_fp = (uintptr_t)fr->link();
1276   if ((old_fp & fp_align_mask) != 0) return true;
1277   if (old_fp == 0 || old_fp == (uintptr_t)-1 || old_fp == ufp) return true;
1278 
1279   // stack grows downwards; if old_fp is below current fp or if the stack
1280   // frame is too large, either the stack is corrupted or fp is not saved
1281   // on stack (i.e. on x86, ebp may be used as general register). The stack
1282   // is not walkable beyond current frame.
1283   if (old_fp < ufp) return true;
1284   if (old_fp - ufp > 64 * K) return true;
1285 
1286   return false;
1287 }
1288 
1289 
1290 // Set up the boot classpath.
1291 
1292 char* os::format_boot_path(const char* format_string,
1293                            const char* home,
1294                            int home_len,
1295                            char fileSep,
1296                            char pathSep) {
1297     assert((fileSep == '/' && pathSep == ':') ||
1298            (fileSep == '\\' && pathSep == ';'), "unexpected separator chars");
1299 
1300     // Scan the format string to determine the length of the actual
1301     // boot classpath, and handle platform dependencies as well.
1302     int formatted_path_len = 0;
1303     const char* p;
1304     for (p = format_string; *p != 0; ++p) {
1305         if (*p == '%') formatted_path_len += home_len - 1;
1306         ++formatted_path_len;
1307     }
1308 
1309     char* formatted_path = NEW_C_HEAP_ARRAY(char, formatted_path_len + 1, mtInternal);
1310 
1311     // Create boot classpath from format, substituting separator chars and
1312     // java home directory.
1313     char* q = formatted_path;
1314     for (p = format_string; *p != 0; ++p) {
1315         switch (*p) {
1316         case '%':
1317             strcpy(q, home);
1318             q += home_len;
1319             break;
1320         case '/':
1321             *q++ = fileSep;
1322             break;
1323         case ':':
1324             *q++ = pathSep;
1325             break;
1326         default:
1327             *q++ = *p;
1328         }
1329     }
1330     *q = '\0';
1331 
1332     assert((q - formatted_path) == formatted_path_len, "formatted_path size botched");
1333     return formatted_path;
1334 }
1335 
1336 // This function is a proxy to fopen, it tries to add a non standard flag ('e' or 'N')
1337 // that ensures automatic closing of the file on exec. If it can not find support in
1338 // the underlying c library, it will make an extra system call (fcntl) to ensure automatic
1339 // closing of the file on exec.
1340 FILE* os::fopen(const char* path, const char* mode) {
1341   char modified_mode[20];
1342   assert(strlen(mode) + 1 < sizeof(modified_mode), "mode chars plus one extra must fit in buffer");
1343   sprintf(modified_mode, "%s" LINUX_ONLY("e") BSD_ONLY("e") WINDOWS_ONLY("N"), mode);
1344   FILE* file = ::fopen(path, modified_mode);
1345 
1346 #if !(defined LINUX || defined BSD || defined _WINDOWS)
1347   // assume fcntl FD_CLOEXEC support as a backup solution when 'e' or 'N'
1348   // is not supported as mode in fopen
1349   if (file != NULL) {
1350     int fd = fileno(file);
1351     if (fd != -1) {
1352       int fd_flags = fcntl(fd, F_GETFD);
1353       if (fd_flags != -1) {
1354         fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC);
1355       }
1356     }
1357   }
1358 #endif
1359 
1360   return file;
1361 }
1362 
1363 ssize_t os::read(int fd, void *buf, unsigned int nBytes) {
1364   return ::read(fd, buf, nBytes);
1365 }
1366 
1367 bool os::set_boot_path(char fileSep, char pathSep) {
1368   const char* home = Arguments::get_java_home();
1369   int home_len = (int)strlen(home);
1370 
1371   struct stat st;
1372 
1373   // modular image if "modules" jimage exists
1374   char* jimage = format_boot_path("%/lib/" MODULES_IMAGE_NAME, home, home_len, fileSep, pathSep);
1375   if (jimage == NULL) return false;
1376   bool has_jimage = (os::stat(jimage, &st) == 0);
1377   if (has_jimage) {
1378     Arguments::set_sysclasspath(jimage, true);
1379     FREE_C_HEAP_ARRAY(char, jimage);
1380     return true;
1381   }
1382   FREE_C_HEAP_ARRAY(char, jimage);
1383 
1384   // check if developer build with exploded modules
1385   char* base_classes = format_boot_path("%/modules/" JAVA_BASE_NAME, home, home_len, fileSep, pathSep);
1386   if (base_classes == NULL) return false;
1387   if (os::stat(base_classes, &st) == 0) {
1388     Arguments::set_sysclasspath(base_classes, false);
1389     FREE_C_HEAP_ARRAY(char, base_classes);
1390     return true;
1391   }
1392   FREE_C_HEAP_ARRAY(char, base_classes);
1393 
1394   return false;
1395 }
1396 
1397 bool os::file_exists(const char* filename) {
1398   struct stat statbuf;
1399   if (filename == NULL || strlen(filename) == 0) {
1400     return false;
1401   }
1402   return os::stat(filename, &statbuf) == 0;
1403 }
1404 
1405 // Splits a path, based on its separator, the number of
1406 // elements is returned back in "elements".
1407 // file_name_length is used as a modifier for each path's
1408 // length when compared to JVM_MAXPATHLEN. So if you know
1409 // each returned path will have something appended when
1410 // in use, you can pass the length of that in
1411 // file_name_length, to ensure we detect if any path
1412 // exceeds the maximum path length once prepended onto
1413 // the sub-path/file name.
1414 // It is the callers responsibility to:
1415 //   a> check the value of "elements", which may be 0.
1416 //   b> ignore any empty path elements
1417 //   c> free up the data.
1418 char** os::split_path(const char* path, size_t* elements, size_t file_name_length) {
1419   *elements = (size_t)0;
1420   if (path == NULL || strlen(path) == 0 || file_name_length == (size_t)NULL) {
1421     return NULL;
1422   }
1423   const char psepchar = *os::path_separator();
1424   char* inpath = NEW_C_HEAP_ARRAY(char, strlen(path) + 1, mtInternal);
1425   strcpy(inpath, path);
1426   size_t count = 1;
1427   char* p = strchr(inpath, psepchar);
1428   // Get a count of elements to allocate memory
1429   while (p != NULL) {
1430     count++;
1431     p++;
1432     p = strchr(p, psepchar);
1433   }
1434 
1435   char** opath = NEW_C_HEAP_ARRAY(char*, count, mtInternal);
1436 
1437   // do the actual splitting
1438   p = inpath;
1439   for (size_t i = 0 ; i < count ; i++) {
1440     size_t len = strcspn(p, os::path_separator());
1441     if (len + file_name_length > JVM_MAXPATHLEN) {
1442       // release allocated storage before exiting the vm
1443       free_array_of_char_arrays(opath, i++);
1444       vm_exit_during_initialization("The VM tried to use a path that exceeds the maximum path length for "
1445                                     "this system. Review path-containing parameters and properties, such as "
1446                                     "sun.boot.library.path, to identify potential sources for this path.");
1447     }
1448     // allocate the string and add terminator storage
1449     char* s = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
1450     strncpy(s, p, len);
1451     s[len] = '\0';
1452     opath[i] = s;
1453     p += len + 1;
1454   }
1455   FREE_C_HEAP_ARRAY(char, inpath);
1456   *elements = count;
1457   return opath;
1458 }
1459 
1460 // Returns true if the current stack pointer is above the stack shadow
1461 // pages, false otherwise.
1462 bool os::stack_shadow_pages_available(Thread *thread, const methodHandle& method, address sp) {
1463   if (!thread->is_Java_thread()) return false;
1464   // Check if we have StackShadowPages above the yellow zone.  This parameter
1465   // is dependent on the depth of the maximum VM call stack possible from
1466   // the handler for stack overflow.  'instanceof' in the stack overflow
1467   // handler or a println uses at least 8k stack of VM and native code
1468   // respectively.
1469   const int framesize_in_bytes =
1470     Interpreter::size_top_interpreter_activation(method()) * wordSize;
1471 
1472   address limit = JavaThread::cast(thread)->stack_end() +
1473                   (StackOverflow::stack_guard_zone_size() + StackOverflow::stack_shadow_zone_size());
1474 
1475   return sp > (limit + framesize_in_bytes);
1476 }
1477 
1478 size_t os::page_size_for_region(size_t region_size, size_t min_pages, bool must_be_aligned) {
1479   assert(min_pages > 0, "sanity");
1480   if (UseLargePages) {
1481     const size_t max_page_size = region_size / min_pages;
1482 
1483     for (size_t page_size = page_sizes().largest(); page_size != 0;
1484          page_size = page_sizes().next_smaller(page_size)) {
1485       if (page_size <= max_page_size) {
1486         if (!must_be_aligned || is_aligned(region_size, page_size)) {
1487           return page_size;
1488         }
1489       }
1490     }
1491   }
1492 
1493   return vm_page_size();
1494 }
1495 
1496 size_t os::page_size_for_region_aligned(size_t region_size, size_t min_pages) {
1497   return page_size_for_region(region_size, min_pages, true);
1498 }
1499 
1500 size_t os::page_size_for_region_unaligned(size_t region_size, size_t min_pages) {
1501   return page_size_for_region(region_size, min_pages, false);
1502 }
1503 
1504 static const char* errno_to_string (int e, bool short_text) {
1505   #define ALL_SHARED_ENUMS(X) \
1506     X(E2BIG, "Argument list too long") \
1507     X(EACCES, "Permission denied") \
1508     X(EADDRINUSE, "Address in use") \
1509     X(EADDRNOTAVAIL, "Address not available") \
1510     X(EAFNOSUPPORT, "Address family not supported") \
1511     X(EAGAIN, "Resource unavailable, try again") \
1512     X(EALREADY, "Connection already in progress") \
1513     X(EBADF, "Bad file descriptor") \
1514     X(EBADMSG, "Bad message") \
1515     X(EBUSY, "Device or resource busy") \
1516     X(ECANCELED, "Operation canceled") \
1517     X(ECHILD, "No child processes") \
1518     X(ECONNABORTED, "Connection aborted") \
1519     X(ECONNREFUSED, "Connection refused") \
1520     X(ECONNRESET, "Connection reset") \
1521     X(EDEADLK, "Resource deadlock would occur") \
1522     X(EDESTADDRREQ, "Destination address required") \
1523     X(EDOM, "Mathematics argument out of domain of function") \
1524     X(EEXIST, "File exists") \
1525     X(EFAULT, "Bad address") \
1526     X(EFBIG, "File too large") \
1527     X(EHOSTUNREACH, "Host is unreachable") \
1528     X(EIDRM, "Identifier removed") \
1529     X(EILSEQ, "Illegal byte sequence") \
1530     X(EINPROGRESS, "Operation in progress") \
1531     X(EINTR, "Interrupted function") \
1532     X(EINVAL, "Invalid argument") \
1533     X(EIO, "I/O error") \
1534     X(EISCONN, "Socket is connected") \
1535     X(EISDIR, "Is a directory") \
1536     X(ELOOP, "Too many levels of symbolic links") \
1537     X(EMFILE, "Too many open files") \
1538     X(EMLINK, "Too many links") \
1539     X(EMSGSIZE, "Message too large") \
1540     X(ENAMETOOLONG, "Filename too long") \
1541     X(ENETDOWN, "Network is down") \
1542     X(ENETRESET, "Connection aborted by network") \
1543     X(ENETUNREACH, "Network unreachable") \
1544     X(ENFILE, "Too many files open in system") \
1545     X(ENOBUFS, "No buffer space available") \
1546     X(ENODATA, "No message is available on the STREAM head read queue") \
1547     X(ENODEV, "No such device") \
1548     X(ENOENT, "No such file or directory") \
1549     X(ENOEXEC, "Executable file format error") \
1550     X(ENOLCK, "No locks available") \
1551     X(ENOLINK, "Reserved") \
1552     X(ENOMEM, "Not enough space") \
1553     X(ENOMSG, "No message of the desired type") \
1554     X(ENOPROTOOPT, "Protocol not available") \
1555     X(ENOSPC, "No space left on device") \
1556     X(ENOSR, "No STREAM resources") \
1557     X(ENOSTR, "Not a STREAM") \
1558     X(ENOSYS, "Function not supported") \
1559     X(ENOTCONN, "The socket is not connected") \
1560     X(ENOTDIR, "Not a directory") \
1561     X(ENOTEMPTY, "Directory not empty") \
1562     X(ENOTSOCK, "Not a socket") \
1563     X(ENOTSUP, "Not supported") \
1564     X(ENOTTY, "Inappropriate I/O control operation") \
1565     X(ENXIO, "No such device or address") \
1566     X(EOPNOTSUPP, "Operation not supported on socket") \
1567     X(EOVERFLOW, "Value too large to be stored in data type") \
1568     X(EPERM, "Operation not permitted") \
1569     X(EPIPE, "Broken pipe") \
1570     X(EPROTO, "Protocol error") \
1571     X(EPROTONOSUPPORT, "Protocol not supported") \
1572     X(EPROTOTYPE, "Protocol wrong type for socket") \
1573     X(ERANGE, "Result too large") \
1574     X(EROFS, "Read-only file system") \
1575     X(ESPIPE, "Invalid seek") \
1576     X(ESRCH, "No such process") \
1577     X(ETIME, "Stream ioctl() timeout") \
1578     X(ETIMEDOUT, "Connection timed out") \
1579     X(ETXTBSY, "Text file busy") \
1580     X(EWOULDBLOCK, "Operation would block") \
1581     X(EXDEV, "Cross-device link")
1582 
1583   #define DEFINE_ENTRY(e, text) { e, #e, text },
1584 
1585   static const struct {
1586     int v;
1587     const char* short_text;
1588     const char* long_text;
1589   } table [] = {
1590 
1591     ALL_SHARED_ENUMS(DEFINE_ENTRY)
1592 
1593     // The following enums are not defined on all platforms.
1594     #ifdef ESTALE
1595     DEFINE_ENTRY(ESTALE, "Reserved")
1596     #endif
1597     #ifdef EDQUOT
1598     DEFINE_ENTRY(EDQUOT, "Reserved")
1599     #endif
1600     #ifdef EMULTIHOP
1601     DEFINE_ENTRY(EMULTIHOP, "Reserved")
1602     #endif
1603 
1604     // End marker.
1605     { -1, "Unknown errno", "Unknown error" }
1606 
1607   };
1608 
1609   #undef DEFINE_ENTRY
1610   #undef ALL_FLAGS
1611 
1612   int i = 0;
1613   while (table[i].v != -1 && table[i].v != e) {
1614     i ++;
1615   }
1616 
1617   return short_text ? table[i].short_text : table[i].long_text;
1618 
1619 }
1620 
1621 const char* os::strerror(int e) {
1622   return errno_to_string(e, false);
1623 }
1624 
1625 const char* os::errno_name(int e) {
1626   return errno_to_string(e, true);
1627 }
1628 
1629 #define trace_page_size_params(size) byte_size_in_exact_unit(size), exact_unit_for_byte_size(size)
1630 
1631 void os::trace_page_sizes(const char* str,
1632                           const size_t region_min_size,
1633                           const size_t region_max_size,
1634                           const size_t page_size,
1635                           const char* base,
1636                           const size_t size) {
1637 
1638   log_info(pagesize)("%s: "
1639                      " min=" SIZE_FORMAT "%s"
1640                      " max=" SIZE_FORMAT "%s"
1641                      " base=" PTR_FORMAT
1642                      " page_size=" SIZE_FORMAT "%s"
1643                      " size=" SIZE_FORMAT "%s",
1644                      str,
1645                      trace_page_size_params(region_min_size),
1646                      trace_page_size_params(region_max_size),
1647                      p2i(base),
1648                      trace_page_size_params(page_size),
1649                      trace_page_size_params(size));
1650 }
1651 
1652 void os::trace_page_sizes_for_requested_size(const char* str,
1653                                              const size_t requested_size,
1654                                              const size_t page_size,
1655                                              const size_t alignment,
1656                                              const char* base,
1657                                              const size_t size) {
1658 
1659   log_info(pagesize)("%s:"
1660                      " req_size=" SIZE_FORMAT "%s"
1661                      " base=" PTR_FORMAT
1662                      " page_size=" SIZE_FORMAT "%s"
1663                      " alignment=" SIZE_FORMAT "%s"
1664                      " size=" SIZE_FORMAT "%s",
1665                      str,
1666                      trace_page_size_params(requested_size),
1667                      p2i(base),
1668                      trace_page_size_params(page_size),
1669                      trace_page_size_params(alignment),
1670                      trace_page_size_params(size));
1671 }
1672 
1673 
1674 // This is the working definition of a server class machine:
1675 // >= 2 physical CPU's and >=2GB of memory, with some fuzz
1676 // because the graphics memory (?) sometimes masks physical memory.
1677 // If you want to change the definition of a server class machine
1678 // on some OS or platform, e.g., >=4GB on Windows platforms,
1679 // then you'll have to parameterize this method based on that state,
1680 // as was done for logical processors here, or replicate and
1681 // specialize this method for each platform.  (Or fix os to have
1682 // some inheritance structure and use subclassing.  Sigh.)
1683 // If you want some platform to always or never behave as a server
1684 // class machine, change the setting of AlwaysActAsServerClassMachine
1685 // and NeverActAsServerClassMachine in globals*.hpp.
1686 bool os::is_server_class_machine() {
1687   // First check for the early returns
1688   if (NeverActAsServerClassMachine) {
1689     return false;
1690   }
1691   if (AlwaysActAsServerClassMachine) {
1692     return true;
1693   }
1694   // Then actually look at the machine
1695   bool         result            = false;
1696   const unsigned int    server_processors = 2;
1697   const julong server_memory     = 2UL * G;
1698   // We seem not to get our full complement of memory.
1699   //     We allow some part (1/8?) of the memory to be "missing",
1700   //     based on the sizes of DIMMs, and maybe graphics cards.
1701   const julong missing_memory   = 256UL * M;
1702 
1703   /* Is this a server class machine? */
1704   if ((os::active_processor_count() >= (int)server_processors) &&
1705       (os::physical_memory() >= (server_memory - missing_memory))) {
1706     const unsigned int logical_processors =
1707       VM_Version::logical_processors_per_package();
1708     if (logical_processors > 1) {
1709       const unsigned int physical_packages =
1710         os::active_processor_count() / logical_processors;
1711       if (physical_packages >= server_processors) {
1712         result = true;
1713       }
1714     } else {
1715       result = true;
1716     }
1717   }
1718   return result;
1719 }
1720 
1721 void os::initialize_initial_active_processor_count() {
1722   assert(_initial_active_processor_count == 0, "Initial active processor count already set.");
1723   _initial_active_processor_count = active_processor_count();
1724   log_debug(os)("Initial active processor count set to %d" , _initial_active_processor_count);
1725 }
1726 
1727 void os::SuspendedThreadTask::run() {
1728   internal_do_task();
1729   _done = true;
1730 }
1731 
1732 bool os::create_stack_guard_pages(char* addr, size_t bytes) {
1733   return os::pd_create_stack_guard_pages(addr, bytes);
1734 }
1735 
1736 char* os::reserve_memory(size_t bytes, bool executable, MEMFLAGS flags) {
1737   char* result = pd_reserve_memory(bytes, executable);
1738   if (result != NULL) {
1739     MemTracker::record_virtual_memory_reserve(result, bytes, CALLER_PC);
1740     if (flags != mtOther) {
1741       MemTracker::record_virtual_memory_type(result, flags);
1742     }
1743   }
1744 
1745   return result;
1746 }
1747 
1748 char* os::attempt_reserve_memory_at(char* addr, size_t bytes, bool executable) {
1749   char* result = pd_attempt_reserve_memory_at(addr, bytes, executable);
1750   if (result != NULL) {
1751     MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
1752   } else {
1753     log_debug(os)("Attempt to reserve memory at " INTPTR_FORMAT " for "
1754                  SIZE_FORMAT " bytes failed, errno %d", p2i(addr), bytes, get_last_error());
1755   }
1756   return result;
1757 }
1758 
1759 bool os::commit_memory(char* addr, size_t bytes, bool executable) {
1760   bool res = pd_commit_memory(addr, bytes, executable);
1761   if (res) {
1762     MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
1763   }
1764   return res;
1765 }
1766 
1767 bool os::commit_memory(char* addr, size_t size, size_t alignment_hint,
1768                               bool executable) {
1769   bool res = os::pd_commit_memory(addr, size, alignment_hint, executable);
1770   if (res) {
1771     MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
1772   }
1773   return res;
1774 }
1775 
1776 void os::commit_memory_or_exit(char* addr, size_t bytes, bool executable,
1777                                const char* mesg) {
1778   pd_commit_memory_or_exit(addr, bytes, executable, mesg);
1779   MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
1780 }
1781 
1782 void os::commit_memory_or_exit(char* addr, size_t size, size_t alignment_hint,
1783                                bool executable, const char* mesg) {
1784   os::pd_commit_memory_or_exit(addr, size, alignment_hint, executable, mesg);
1785   MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
1786 }
1787 
1788 bool os::uncommit_memory(char* addr, size_t bytes, bool executable) {
1789   bool res;
1790   if (MemTracker::enabled()) {
1791     Tracker tkr(Tracker::uncommit);
1792     res = pd_uncommit_memory(addr, bytes, executable);
1793     if (res) {
1794       tkr.record((address)addr, bytes);
1795     }
1796   } else {
1797     res = pd_uncommit_memory(addr, bytes, executable);
1798   }
1799   return res;
1800 }
1801 
1802 bool os::release_memory(char* addr, size_t bytes) {
1803   bool res;
1804   if (MemTracker::enabled()) {
1805     // Note: Tracker contains a ThreadCritical.
1806     Tracker tkr(Tracker::release);
1807     res = pd_release_memory(addr, bytes);
1808     if (res) {
1809       tkr.record((address)addr, bytes);
1810     }
1811   } else {
1812     res = pd_release_memory(addr, bytes);
1813   }
1814   if (!res) {
1815     log_info(os)("os::release_memory failed (" PTR_FORMAT ", " SIZE_FORMAT ")", p2i(addr), bytes);
1816   }
1817   return res;
1818 }
1819 
1820 // Prints all mappings
1821 void os::print_memory_mappings(outputStream* st) {
1822   os::print_memory_mappings(nullptr, (size_t)-1, st);
1823 }
1824 
1825 void os::pretouch_memory(void* start, void* end, size_t page_size) {
1826   for (volatile char *p = (char*)start; p < (char*)end; p += page_size) {
1827     // Note: this must be a store, not a load. On many OSes loads from fresh
1828     // memory would be satisfied from a single mapped page containing all zeros.
1829     // We need to store something to each page to get them backed by their own
1830     // memory, which is the effect we want here.
1831     *p = 0;
1832   }
1833 }
1834 
1835 char* os::map_memory_to_file(size_t bytes, int file_desc) {
1836   // Could have called pd_reserve_memory() followed by replace_existing_mapping_with_file_mapping(),
1837   // but AIX may use SHM in which case its more trouble to detach the segment and remap memory to the file.
1838   // On all current implementations NULL is interpreted as any available address.
1839   char* result = os::map_memory_to_file(NULL /* addr */, bytes, file_desc);
1840   if (result != NULL) {
1841     MemTracker::record_virtual_memory_reserve_and_commit(result, bytes, CALLER_PC);
1842   }
1843   return result;
1844 }
1845 
1846 char* os::attempt_map_memory_to_file_at(char* addr, size_t bytes, int file_desc) {
1847   char* result = pd_attempt_map_memory_to_file_at(addr, bytes, file_desc);
1848   if (result != NULL) {
1849     MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, CALLER_PC);
1850   }
1851   return result;
1852 }
1853 
1854 char* os::map_memory(int fd, const char* file_name, size_t file_offset,
1855                            char *addr, size_t bytes, bool read_only,
1856                            bool allow_exec, MEMFLAGS flags) {
1857   char* result = pd_map_memory(fd, file_name, file_offset, addr, bytes, read_only, allow_exec);
1858   if (result != NULL) {
1859     MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, CALLER_PC, flags);
1860   }
1861   return result;
1862 }
1863 
1864 char* os::remap_memory(int fd, const char* file_name, size_t file_offset,
1865                              char *addr, size_t bytes, bool read_only,
1866                              bool allow_exec) {
1867   return pd_remap_memory(fd, file_name, file_offset, addr, bytes,
1868                     read_only, allow_exec);
1869 }
1870 
1871 bool os::unmap_memory(char *addr, size_t bytes) {
1872   bool result;
1873   if (MemTracker::enabled()) {
1874     Tracker tkr(Tracker::release);
1875     result = pd_unmap_memory(addr, bytes);
1876     if (result) {
1877       tkr.record((address)addr, bytes);
1878     }
1879   } else {
1880     result = pd_unmap_memory(addr, bytes);
1881   }
1882   return result;
1883 }
1884 
1885 void os::free_memory(char *addr, size_t bytes, size_t alignment_hint) {
1886   pd_free_memory(addr, bytes, alignment_hint);
1887 }
1888 
1889 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
1890   pd_realign_memory(addr, bytes, alignment_hint);
1891 }
1892 
1893 char* os::reserve_memory_special(size_t size, size_t alignment, size_t page_size,
1894                                  char* addr, bool executable) {
1895 
1896   assert(is_aligned(addr, alignment), "Unaligned request address");
1897 
1898   char* result = pd_reserve_memory_special(size, alignment, page_size, addr, executable);
1899   if (result != NULL) {
1900     // The memory is committed
1901     MemTracker::record_virtual_memory_reserve_and_commit((address)result, size, CALLER_PC);
1902   }
1903 
1904   return result;
1905 }
1906 
1907 bool os::release_memory_special(char* addr, size_t bytes) {
1908   bool res;
1909   if (MemTracker::enabled()) {
1910     // Note: Tracker contains a ThreadCritical.
1911     Tracker tkr(Tracker::release);
1912     res = pd_release_memory_special(addr, bytes);
1913     if (res) {
1914       tkr.record((address)addr, bytes);
1915     }
1916   } else {
1917     res = pd_release_memory_special(addr, bytes);
1918   }
1919   return res;
1920 }
1921 
1922 #ifndef _WINDOWS
1923 /* try to switch state from state "from" to state "to"
1924  * returns the state set after the method is complete
1925  */
1926 os::SuspendResume::State os::SuspendResume::switch_state(os::SuspendResume::State from,
1927                                                          os::SuspendResume::State to)
1928 {
1929   os::SuspendResume::State result = Atomic::cmpxchg(&_state, from, to);
1930   if (result == from) {
1931     // success
1932     return to;
1933   }
1934   return result;
1935 }
1936 #endif
1937 
1938 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1939 // times. Only for use by non-JavaThreads.
1940 void os::naked_sleep(jlong millis) {
1941   assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1942   const jlong limit = 999;
1943   while (millis > limit) {
1944     naked_short_sleep(limit);
1945     millis -= limit;
1946   }
1947   naked_short_sleep(millis);
1948 }
1949 
1950 
1951 ////// Implementation of PageSizes
1952 
1953 void os::PageSizes::add(size_t page_size) {
1954   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_HEX, page_size);
1955   _v |= page_size;
1956 }
1957 
1958 bool os::PageSizes::contains(size_t page_size) const {
1959   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_HEX, page_size);
1960   return (_v & page_size) != 0;
1961 }
1962 
1963 size_t os::PageSizes::next_smaller(size_t page_size) const {
1964   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_HEX, page_size);
1965   size_t v2 = _v & (page_size - 1);
1966   if (v2 == 0) {
1967     return 0;
1968   }
1969   return round_down_power_of_2(v2);
1970 }
1971 
1972 size_t os::PageSizes::next_larger(size_t page_size) const {
1973   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_HEX, page_size);
1974   if (page_size == max_power_of_2<size_t>()) { // Shift by 32/64 would be UB
1975     return 0;
1976   }
1977   // Remove current and smaller page sizes
1978   size_t v2 = _v & ~(page_size + (page_size - 1));
1979   if (v2 == 0) {
1980     return 0;
1981   }
1982   return (size_t)1 << count_trailing_zeros(v2);
1983 }
1984 
1985 size_t os::PageSizes::largest() const {
1986   const size_t max = max_power_of_2<size_t>();
1987   if (contains(max)) {
1988     return max;
1989   }
1990   return next_smaller(max);
1991 }
1992 
1993 size_t os::PageSizes::smallest() const {
1994   // Strictly speaking the set should not contain sizes < os::vm_page_size().
1995   // But this is not enforced.
1996   return next_larger(1);
1997 }
1998 
1999 void os::PageSizes::print_on(outputStream* st) const {
2000   bool first = true;
2001   for (size_t sz = smallest(); sz != 0; sz = next_larger(sz)) {
2002     if (first) {
2003       first = false;
2004     } else {
2005       st->print_raw(", ");
2006     }
2007     if (sz < M) {
2008       st->print(SIZE_FORMAT "k", sz / K);
2009     } else if (sz < G) {
2010       st->print(SIZE_FORMAT "M", sz / M);
2011     } else {
2012       st->print(SIZE_FORMAT "G", sz / G);
2013     }
2014   }
2015   if (first) {
2016     st->print("empty");
2017   }
2018 }