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