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