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