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