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 // Must be at least Windows Vista or Server 2008 to use InitOnceExecuteOnce
  26 #define _WIN32_WINNT 0x0600
  27 
  28 // no precompiled headers
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/icBuffer.hpp"
  33 #include "code/vtableStubs.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/disassembler.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "jvm_windows.h"
  38 #include "memory/allocation.inline.hpp"
  39 #include "memory/filemap.hpp"
  40 #include "mutex_windows.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "os_share_windows.hpp"
  43 #include "os_windows.inline.hpp"
  44 #include "prims/jniFastGetField.hpp"
  45 #include "prims/jvm.h"
  46 #include "prims/jvm_misc.hpp"
  47 #include "runtime/arguments.hpp"
  48 #include "runtime/atomic.inline.hpp"
  49 #include "runtime/extendedPC.hpp"
  50 #include "runtime/globals.hpp"
  51 #include "runtime/interfaceSupport.hpp"
  52 #include "runtime/java.hpp"
  53 #include "runtime/javaCalls.hpp"
  54 #include "runtime/mutexLocker.hpp"
  55 #include "runtime/objectMonitor.hpp"
  56 #include "runtime/orderAccess.inline.hpp"
  57 #include "runtime/osThread.hpp"
  58 #include "runtime/perfMemory.hpp"
  59 #include "runtime/sharedRuntime.hpp"
  60 #include "runtime/statSampler.hpp"
  61 #include "runtime/stubRoutines.hpp"
  62 #include "runtime/thread.inline.hpp"
  63 #include "runtime/threadCritical.hpp"
  64 #include "runtime/timer.hpp"
  65 #include "runtime/vm_version.hpp"
  66 #include "services/attachListener.hpp"
  67 #include "services/memTracker.hpp"
  68 #include "services/runtimeService.hpp"
  69 #include "utilities/decoder.hpp"
  70 #include "utilities/defaultStream.hpp"
  71 #include "utilities/events.hpp"
  72 #include "utilities/growableArray.hpp"
  73 #include "utilities/vmError.hpp"
  74 
  75 #ifdef _DEBUG
  76 #include <crtdbg.h>
  77 #endif
  78 
  79 
  80 #include <windows.h>
  81 #include <sys/types.h>
  82 #include <sys/stat.h>
  83 #include <sys/timeb.h>
  84 #include <objidl.h>
  85 #include <shlobj.h>
  86 
  87 #include <malloc.h>
  88 #include <signal.h>
  89 #include <direct.h>
  90 #include <errno.h>
  91 #include <fcntl.h>
  92 #include <io.h>
  93 #include <process.h>              // For _beginthreadex(), _endthreadex()
  94 #include <imagehlp.h>             // For os::dll_address_to_function_name
  95 // for enumerating dll libraries
  96 #include <vdmdbg.h>
  97 
  98 // for timer info max values which include all bits
  99 #define ALL_64_BITS CONST64(-1)
 100 
 101 // For DLL loading/load error detection
 102 // Values of PE COFF
 103 #define IMAGE_FILE_PTR_TO_SIGNATURE 0x3c
 104 #define IMAGE_FILE_SIGNATURE_LENGTH 4
 105 
 106 static HANDLE main_process;
 107 static HANDLE main_thread;
 108 static int    main_thread_id;
 109 
 110 static FILETIME process_creation_time;
 111 static FILETIME process_exit_time;
 112 static FILETIME process_user_time;
 113 static FILETIME process_kernel_time;
 114 
 115 #ifdef _M_IA64
 116   #define __CPU__ ia64
 117 #elif _M_AMD64
 118   #define __CPU__ amd64
 119 #else
 120   #define __CPU__ i486
 121 #endif
 122 
 123 // save DLL module handle, used by GetModuleFileName
 124 
 125 HINSTANCE vm_lib_handle;
 126 
 127 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
 128   switch (reason) {
 129   case DLL_PROCESS_ATTACH:
 130     vm_lib_handle = hinst;
 131     if (ForceTimeHighResolution) {
 132       timeBeginPeriod(1L);
 133     }
 134     break;
 135   case DLL_PROCESS_DETACH:
 136     if (ForceTimeHighResolution) {
 137       timeEndPeriod(1L);
 138     }
 139     break;
 140   default:
 141     break;
 142   }
 143   return true;
 144 }
 145 
 146 static inline double fileTimeAsDouble(FILETIME* time) {
 147   const double high  = (double) ((unsigned int) ~0);
 148   const double split = 10000000.0;
 149   double result = (time->dwLowDateTime / split) +
 150                    time->dwHighDateTime * (high/split);
 151   return result;
 152 }
 153 
 154 // Implementation of os
 155 
 156 bool os::getenv(const char* name, char* buffer, int len) {
 157   int result = GetEnvironmentVariable(name, buffer, len);
 158   return result > 0 && result < len;
 159 }
 160 
 161 bool os::unsetenv(const char* name) {
 162   assert(name != NULL, "Null pointer");
 163   return (SetEnvironmentVariable(name, NULL) == TRUE);
 164 }
 165 
 166 // No setuid programs under Windows.
 167 bool os::have_special_privileges() {
 168   return false;
 169 }
 170 
 171 
 172 // This method is  a periodic task to check for misbehaving JNI applications
 173 // under CheckJNI, we can add any periodic checks here.
 174 // For Windows at the moment does nothing
 175 void os::run_periodic_checks() {
 176   return;
 177 }
 178 
 179 // previous UnhandledExceptionFilter, if there is one
 180 static LPTOP_LEVEL_EXCEPTION_FILTER prev_uef_handler = NULL;
 181 
 182 LONG WINAPI Handle_FLT_Exception(struct _EXCEPTION_POINTERS* exceptionInfo);
 183 
 184 void os::init_system_properties_values() {
 185   // sysclasspath, java_home, dll_dir
 186   {
 187     char *home_path;
 188     char *dll_path;
 189     char *pslash;
 190     char *bin = "\\bin";
 191     char home_dir[MAX_PATH];
 192 
 193     if (!getenv("_ALT_JAVA_HOME_DIR", home_dir, MAX_PATH)) {
 194       os::jvm_path(home_dir, sizeof(home_dir));
 195       // Found the full path to jvm.dll.
 196       // Now cut the path to <java_home>/jre if we can.
 197       *(strrchr(home_dir, '\\')) = '\0';  // get rid of \jvm.dll
 198       pslash = strrchr(home_dir, '\\');
 199       if (pslash != NULL) {
 200         *pslash = '\0';                   // get rid of \{client|server}
 201         pslash = strrchr(home_dir, '\\');
 202         if (pslash != NULL) {
 203           *pslash = '\0';                 // get rid of \bin
 204         }
 205       }
 206     }
 207 
 208     home_path = NEW_C_HEAP_ARRAY(char, strlen(home_dir) + 1, mtInternal);
 209     if (home_path == NULL) {
 210       return;
 211     }
 212     strcpy(home_path, home_dir);
 213     Arguments::set_java_home(home_path);
 214     FREE_C_HEAP_ARRAY(char, home_path);
 215 
 216     dll_path = NEW_C_HEAP_ARRAY(char, strlen(home_dir) + strlen(bin) + 1,
 217                                 mtInternal);
 218     if (dll_path == NULL) {
 219       return;
 220     }
 221     strcpy(dll_path, home_dir);
 222     strcat(dll_path, bin);
 223     Arguments::set_dll_dir(dll_path);
 224     FREE_C_HEAP_ARRAY(char, dll_path);
 225 
 226     if (!set_boot_path('\\', ';')) {
 227       return;
 228     }
 229   }
 230 
 231 // library_path
 232 #define EXT_DIR "\\lib\\ext"
 233 #define BIN_DIR "\\bin"
 234 #define PACKAGE_DIR "\\Sun\\Java"
 235   {
 236     // Win32 library search order (See the documentation for LoadLibrary):
 237     //
 238     // 1. The directory from which application is loaded.
 239     // 2. The system wide Java Extensions directory (Java only)
 240     // 3. System directory (GetSystemDirectory)
 241     // 4. Windows directory (GetWindowsDirectory)
 242     // 5. The PATH environment variable
 243     // 6. The current directory
 244 
 245     char *library_path;
 246     char tmp[MAX_PATH];
 247     char *path_str = ::getenv("PATH");
 248 
 249     library_path = NEW_C_HEAP_ARRAY(char, MAX_PATH * 5 + sizeof(PACKAGE_DIR) +
 250                                     sizeof(BIN_DIR) + (path_str ? strlen(path_str) : 0) + 10, mtInternal);
 251 
 252     library_path[0] = '\0';
 253 
 254     GetModuleFileName(NULL, tmp, sizeof(tmp));
 255     *(strrchr(tmp, '\\')) = '\0';
 256     strcat(library_path, tmp);
 257 
 258     GetWindowsDirectory(tmp, sizeof(tmp));
 259     strcat(library_path, ";");
 260     strcat(library_path, tmp);
 261     strcat(library_path, PACKAGE_DIR BIN_DIR);
 262 
 263     GetSystemDirectory(tmp, sizeof(tmp));
 264     strcat(library_path, ";");
 265     strcat(library_path, tmp);
 266 
 267     GetWindowsDirectory(tmp, sizeof(tmp));
 268     strcat(library_path, ";");
 269     strcat(library_path, tmp);
 270 
 271     if (path_str) {
 272       strcat(library_path, ";");
 273       strcat(library_path, path_str);
 274     }
 275 
 276     strcat(library_path, ";.");
 277 
 278     Arguments::set_library_path(library_path);
 279     FREE_C_HEAP_ARRAY(char, library_path);
 280   }
 281 
 282   // Default extensions directory
 283   {
 284     char path[MAX_PATH];
 285     char buf[2 * MAX_PATH + 2 * sizeof(EXT_DIR) + sizeof(PACKAGE_DIR) + 1];
 286     GetWindowsDirectory(path, MAX_PATH);
 287     sprintf(buf, "%s%s;%s%s%s", Arguments::get_java_home(), EXT_DIR,
 288             path, PACKAGE_DIR, EXT_DIR);
 289     Arguments::set_ext_dirs(buf);
 290   }
 291   #undef EXT_DIR
 292   #undef BIN_DIR
 293   #undef PACKAGE_DIR
 294 
 295 #ifndef _WIN64
 296   // set our UnhandledExceptionFilter and save any previous one
 297   prev_uef_handler = SetUnhandledExceptionFilter(Handle_FLT_Exception);
 298 #endif
 299 
 300   // Done
 301   return;
 302 }
 303 
 304 void os::breakpoint() {
 305   DebugBreak();
 306 }
 307 
 308 // Invoked from the BREAKPOINT Macro
 309 extern "C" void breakpoint() {
 310   os::breakpoint();
 311 }
 312 
 313 // RtlCaptureStackBackTrace Windows API may not exist prior to Windows XP.
 314 // So far, this method is only used by Native Memory Tracking, which is
 315 // only supported on Windows XP or later.
 316 //
 317 int os::get_native_stack(address* stack, int frames, int toSkip) {
 318 #ifdef _NMT_NOINLINE_
 319   toSkip++;
 320 #endif
 321   int captured = Kernel32Dll::RtlCaptureStackBackTrace(toSkip + 1, frames,
 322                                                        (PVOID*)stack, NULL);
 323   for (int index = captured; index < frames; index ++) {
 324     stack[index] = NULL;
 325   }
 326   return captured;
 327 }
 328 
 329 
 330 // os::current_stack_base()
 331 //
 332 //   Returns the base of the stack, which is the stack's
 333 //   starting address.  This function must be called
 334 //   while running on the stack of the thread being queried.
 335 
 336 address os::current_stack_base() {
 337   MEMORY_BASIC_INFORMATION minfo;
 338   address stack_bottom;
 339   size_t stack_size;
 340 
 341   VirtualQuery(&minfo, &minfo, sizeof(minfo));
 342   stack_bottom =  (address)minfo.AllocationBase;
 343   stack_size = minfo.RegionSize;
 344 
 345   // Add up the sizes of all the regions with the same
 346   // AllocationBase.
 347   while (1) {
 348     VirtualQuery(stack_bottom+stack_size, &minfo, sizeof(minfo));
 349     if (stack_bottom == (address)minfo.AllocationBase) {
 350       stack_size += minfo.RegionSize;
 351     } else {
 352       break;
 353     }
 354   }
 355 
 356 #ifdef _M_IA64
 357   // IA64 has memory and register stacks
 358   //
 359   // This is the stack layout you get on NT/IA64 if you specify 1MB stack limit
 360   // at thread creation (1MB backing store growing upwards, 1MB memory stack
 361   // growing downwards, 2MB summed up)
 362   //
 363   // ...
 364   // ------- top of stack (high address) -----
 365   // |
 366   // |      1MB
 367   // |      Backing Store (Register Stack)
 368   // |
 369   // |         / \
 370   // |          |
 371   // |          |
 372   // |          |
 373   // ------------------------ stack base -----
 374   // |      1MB
 375   // |      Memory Stack
 376   // |
 377   // |          |
 378   // |          |
 379   // |          |
 380   // |         \ /
 381   // |
 382   // ----- bottom of stack (low address) -----
 383   // ...
 384 
 385   stack_size = stack_size / 2;
 386 #endif
 387   return stack_bottom + stack_size;
 388 }
 389 
 390 size_t os::current_stack_size() {
 391   size_t sz;
 392   MEMORY_BASIC_INFORMATION minfo;
 393   VirtualQuery(&minfo, &minfo, sizeof(minfo));
 394   sz = (size_t)os::current_stack_base() - (size_t)minfo.AllocationBase;
 395   return sz;
 396 }
 397 
 398 struct tm* os::localtime_pd(const time_t* clock, struct tm* res) {
 399   const struct tm* time_struct_ptr = localtime(clock);
 400   if (time_struct_ptr != NULL) {
 401     *res = *time_struct_ptr;
 402     return res;
 403   }
 404   return NULL;
 405 }
 406 
 407 LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo);
 408 
 409 // Thread start routine for all new Java threads
 410 static unsigned __stdcall java_start(Thread* thread) {
 411   // Try to randomize the cache line index of hot stack frames.
 412   // This helps when threads of the same stack traces evict each other's
 413   // cache lines. The threads can be either from the same JVM instance, or
 414   // from different JVM instances. The benefit is especially true for
 415   // processors with hyperthreading technology.
 416   static int counter = 0;
 417   int pid = os::current_process_id();
 418   _alloca(((pid ^ counter++) & 7) * 128);
 419 
 420   OSThread* osthr = thread->osthread();
 421   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 422 
 423   if (UseNUMA) {
 424     int lgrp_id = os::numa_get_group_id();
 425     if (lgrp_id != -1) {
 426       thread->set_lgrp_id(lgrp_id);
 427     }
 428   }
 429 
 430   // Diagnostic code to investigate JDK-6573254
 431   int res = 50115;  // non-java thread
 432   if (thread->is_Java_thread()) {
 433     res = 40115;    // java thread
 434   }
 435 
 436   // Install a win32 structured exception handler around every thread created
 437   // by VM, so VM can generate error dump when an exception occurred in non-
 438   // Java thread (e.g. VM thread).
 439   __try {
 440     thread->run();
 441   } __except(topLevelExceptionFilter(
 442                                      (_EXCEPTION_POINTERS*)_exception_info())) {
 443     // Nothing to do.
 444   }
 445 
 446   // One less thread is executing
 447   // When the VMThread gets here, the main thread may have already exited
 448   // which frees the CodeHeap containing the Atomic::add code
 449   if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
 450     Atomic::dec_ptr((intptr_t*)&os::win32::_os_thread_count);
 451   }
 452 
 453   // Thread must not return from exit_process_or_thread(), but if it does,
 454   // let it proceed to exit normally
 455   return (unsigned)os::win32::exit_process_or_thread(os::win32::EPT_THREAD, res);
 456 }
 457 
 458 static OSThread* create_os_thread(Thread* thread, HANDLE thread_handle,
 459                                   int thread_id) {
 460   // Allocate the OSThread object
 461   OSThread* osthread = new OSThread(NULL, NULL);
 462   if (osthread == NULL) return NULL;
 463 
 464   // Initialize support for Java interrupts
 465   HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
 466   if (interrupt_event == NULL) {
 467     delete osthread;
 468     return NULL;
 469   }
 470   osthread->set_interrupt_event(interrupt_event);
 471 
 472   // Store info on the Win32 thread into the OSThread
 473   osthread->set_thread_handle(thread_handle);
 474   osthread->set_thread_id(thread_id);
 475 
 476   if (UseNUMA) {
 477     int lgrp_id = os::numa_get_group_id();
 478     if (lgrp_id != -1) {
 479       thread->set_lgrp_id(lgrp_id);
 480     }
 481   }
 482 
 483   // Initial thread state is INITIALIZED, not SUSPENDED
 484   osthread->set_state(INITIALIZED);
 485 
 486   return osthread;
 487 }
 488 
 489 
 490 bool os::create_attached_thread(JavaThread* thread) {
 491 #ifdef ASSERT
 492   thread->verify_not_published();
 493 #endif
 494   HANDLE thread_h;
 495   if (!DuplicateHandle(main_process, GetCurrentThread(), GetCurrentProcess(),
 496                        &thread_h, THREAD_ALL_ACCESS, false, 0)) {
 497     fatal("DuplicateHandle failed\n");
 498   }
 499   OSThread* osthread = create_os_thread(thread, thread_h,
 500                                         (int)current_thread_id());
 501   if (osthread == NULL) {
 502     return false;
 503   }
 504 
 505   // Initial thread state is RUNNABLE
 506   osthread->set_state(RUNNABLE);
 507 
 508   thread->set_osthread(osthread);
 509   return true;
 510 }
 511 
 512 bool os::create_main_thread(JavaThread* thread) {
 513 #ifdef ASSERT
 514   thread->verify_not_published();
 515 #endif
 516   if (_starting_thread == NULL) {
 517     _starting_thread = create_os_thread(thread, main_thread, main_thread_id);
 518     if (_starting_thread == NULL) {
 519       return false;
 520     }
 521   }
 522 
 523   // The primordial thread is runnable from the start)
 524   _starting_thread->set_state(RUNNABLE);
 525 
 526   thread->set_osthread(_starting_thread);
 527   return true;
 528 }
 529 
 530 // Allocate and initialize a new OSThread
 531 bool os::create_thread(Thread* thread, ThreadType thr_type,
 532                        size_t stack_size) {
 533   unsigned thread_id;
 534 
 535   // Allocate the OSThread object
 536   OSThread* osthread = new OSThread(NULL, NULL);
 537   if (osthread == NULL) {
 538     return false;
 539   }
 540 
 541   // Initialize support for Java interrupts
 542   HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
 543   if (interrupt_event == NULL) {
 544     delete osthread;
 545     return NULL;
 546   }
 547   osthread->set_interrupt_event(interrupt_event);
 548   osthread->set_interrupted(false);
 549 
 550   thread->set_osthread(osthread);
 551 
 552   if (stack_size == 0) {
 553     switch (thr_type) {
 554     case os::java_thread:
 555       // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
 556       if (JavaThread::stack_size_at_create() > 0) {
 557         stack_size = JavaThread::stack_size_at_create();
 558       }
 559       break;
 560     case os::compiler_thread:
 561       if (CompilerThreadStackSize > 0) {
 562         stack_size = (size_t)(CompilerThreadStackSize * K);
 563         break;
 564       } // else fall through:
 565         // use VMThreadStackSize if CompilerThreadStackSize is not defined
 566     case os::vm_thread:
 567     case os::pgc_thread:
 568     case os::cgc_thread:
 569     case os::watcher_thread:
 570       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
 571       break;
 572     }
 573   }
 574 
 575   // Create the Win32 thread
 576   //
 577   // Contrary to what MSDN document says, "stack_size" in _beginthreadex()
 578   // does not specify stack size. Instead, it specifies the size of
 579   // initially committed space. The stack size is determined by
 580   // PE header in the executable. If the committed "stack_size" is larger
 581   // than default value in the PE header, the stack is rounded up to the
 582   // nearest multiple of 1MB. For example if the launcher has default
 583   // stack size of 320k, specifying any size less than 320k does not
 584   // affect the actual stack size at all, it only affects the initial
 585   // commitment. On the other hand, specifying 'stack_size' larger than
 586   // default value may cause significant increase in memory usage, because
 587   // not only the stack space will be rounded up to MB, but also the
 588   // entire space is committed upfront.
 589   //
 590   // Finally Windows XP added a new flag 'STACK_SIZE_PARAM_IS_A_RESERVATION'
 591   // for CreateThread() that can treat 'stack_size' as stack size. However we
 592   // are not supposed to call CreateThread() directly according to MSDN
 593   // document because JVM uses C runtime library. The good news is that the
 594   // flag appears to work with _beginthredex() as well.
 595 
 596 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
 597   #define STACK_SIZE_PARAM_IS_A_RESERVATION  (0x10000)
 598 #endif
 599 
 600   HANDLE thread_handle =
 601     (HANDLE)_beginthreadex(NULL,
 602                            (unsigned)stack_size,
 603                            (unsigned (__stdcall *)(void*)) java_start,
 604                            thread,
 605                            CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
 606                            &thread_id);
 607   if (thread_handle == NULL) {
 608     // perhaps STACK_SIZE_PARAM_IS_A_RESERVATION is not supported, try again
 609     // without the flag.
 610     thread_handle =
 611       (HANDLE)_beginthreadex(NULL,
 612                              (unsigned)stack_size,
 613                              (unsigned (__stdcall *)(void*)) java_start,
 614                              thread,
 615                              CREATE_SUSPENDED,
 616                              &thread_id);
 617   }
 618   if (thread_handle == NULL) {
 619     // Need to clean up stuff we've allocated so far
 620     CloseHandle(osthread->interrupt_event());
 621     thread->set_osthread(NULL);
 622     delete osthread;
 623     return NULL;
 624   }
 625 
 626   Atomic::inc_ptr((intptr_t*)&os::win32::_os_thread_count);
 627 
 628   // Store info on the Win32 thread into the OSThread
 629   osthread->set_thread_handle(thread_handle);
 630   osthread->set_thread_id(thread_id);
 631 
 632   // Initial thread state is INITIALIZED, not SUSPENDED
 633   osthread->set_state(INITIALIZED);
 634 
 635   // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
 636   return true;
 637 }
 638 
 639 
 640 // Free Win32 resources related to the OSThread
 641 void os::free_thread(OSThread* osthread) {
 642   assert(osthread != NULL, "osthread not set");
 643   CloseHandle(osthread->thread_handle());
 644   CloseHandle(osthread->interrupt_event());
 645   delete osthread;
 646 }
 647 
 648 static jlong first_filetime;
 649 static jlong initial_performance_count;
 650 static jlong performance_frequency;
 651 
 652 
 653 jlong as_long(LARGE_INTEGER x) {
 654   jlong result = 0; // initialization to avoid warning
 655   set_high(&result, x.HighPart);
 656   set_low(&result, x.LowPart);
 657   return result;
 658 }
 659 
 660 
 661 jlong os::elapsed_counter() {
 662   LARGE_INTEGER count;
 663   if (win32::_has_performance_count) {
 664     QueryPerformanceCounter(&count);
 665     return as_long(count) - initial_performance_count;
 666   } else {
 667     FILETIME wt;
 668     GetSystemTimeAsFileTime(&wt);
 669     return (jlong_from(wt.dwHighDateTime, wt.dwLowDateTime) - first_filetime);
 670   }
 671 }
 672 
 673 
 674 jlong os::elapsed_frequency() {
 675   if (win32::_has_performance_count) {
 676     return performance_frequency;
 677   } else {
 678     // the FILETIME time is the number of 100-nanosecond intervals since January 1,1601.
 679     return 10000000;
 680   }
 681 }
 682 
 683 
 684 julong os::available_memory() {
 685   return win32::available_memory();
 686 }
 687 
 688 julong os::win32::available_memory() {
 689   // Use GlobalMemoryStatusEx() because GlobalMemoryStatus() may return incorrect
 690   // value if total memory is larger than 4GB
 691   MEMORYSTATUSEX ms;
 692   ms.dwLength = sizeof(ms);
 693   GlobalMemoryStatusEx(&ms);
 694 
 695   return (julong)ms.ullAvailPhys;
 696 }
 697 
 698 julong os::physical_memory() {
 699   return win32::physical_memory();
 700 }
 701 
 702 bool os::has_allocatable_memory_limit(julong* limit) {
 703   MEMORYSTATUSEX ms;
 704   ms.dwLength = sizeof(ms);
 705   GlobalMemoryStatusEx(&ms);
 706 #ifdef _LP64
 707   *limit = (julong)ms.ullAvailVirtual;
 708   return true;
 709 #else
 710   // Limit to 1400m because of the 2gb address space wall
 711   *limit = MIN2((julong)1400*M, (julong)ms.ullAvailVirtual);
 712   return true;
 713 #endif
 714 }
 715 
 716 // VC6 lacks DWORD_PTR
 717 #if _MSC_VER < 1300
 718 typedef UINT_PTR DWORD_PTR;
 719 #endif
 720 
 721 int os::active_processor_count() {
 722   DWORD_PTR lpProcessAffinityMask = 0;
 723   DWORD_PTR lpSystemAffinityMask = 0;
 724   int proc_count = processor_count();
 725   if (proc_count <= sizeof(UINT_PTR) * BitsPerByte &&
 726       GetProcessAffinityMask(GetCurrentProcess(), &lpProcessAffinityMask, &lpSystemAffinityMask)) {
 727     // Nof active processors is number of bits in process affinity mask
 728     int bitcount = 0;
 729     while (lpProcessAffinityMask != 0) {
 730       lpProcessAffinityMask = lpProcessAffinityMask & (lpProcessAffinityMask-1);
 731       bitcount++;
 732     }
 733     return bitcount;
 734   } else {
 735     return proc_count;
 736   }
 737 }
 738 
 739 void os::set_native_thread_name(const char *name) {
 740 
 741   // See: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
 742   //
 743   // Note that unfortunately this only works if the process
 744   // is already attached to a debugger; debugger must observe
 745   // the exception below to show the correct name.
 746 
 747   const DWORD MS_VC_EXCEPTION = 0x406D1388;
 748   struct {
 749     DWORD dwType;     // must be 0x1000
 750     LPCSTR szName;    // pointer to name (in user addr space)
 751     DWORD dwThreadID; // thread ID (-1=caller thread)
 752     DWORD dwFlags;    // reserved for future use, must be zero
 753   } info;
 754 
 755   info.dwType = 0x1000;
 756   info.szName = name;
 757   info.dwThreadID = -1;
 758   info.dwFlags = 0;
 759 
 760   __try {
 761     RaiseException (MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (const ULONG_PTR*)&info );
 762   } __except(EXCEPTION_CONTINUE_EXECUTION) {}
 763 }
 764 
 765 bool os::distribute_processes(uint length, uint* distribution) {
 766   // Not yet implemented.
 767   return false;
 768 }
 769 
 770 bool os::bind_to_processor(uint processor_id) {
 771   // Not yet implemented.
 772   return false;
 773 }
 774 
 775 void os::win32::initialize_performance_counter() {
 776   LARGE_INTEGER count;
 777   if (QueryPerformanceFrequency(&count)) {
 778     win32::_has_performance_count = 1;
 779     performance_frequency = as_long(count);
 780     QueryPerformanceCounter(&count);
 781     initial_performance_count = as_long(count);
 782   } else {
 783     win32::_has_performance_count = 0;
 784     FILETIME wt;
 785     GetSystemTimeAsFileTime(&wt);
 786     first_filetime = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
 787   }
 788 }
 789 
 790 
 791 double os::elapsedTime() {
 792   return (double) elapsed_counter() / (double) elapsed_frequency();
 793 }
 794 
 795 
 796 // Windows format:
 797 //   The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.
 798 // Java format:
 799 //   Java standards require the number of milliseconds since 1/1/1970
 800 
 801 // Constant offset - calculated using offset()
 802 static jlong  _offset   = 116444736000000000;
 803 // Fake time counter for reproducible results when debugging
 804 static jlong  fake_time = 0;
 805 
 806 #ifdef ASSERT
 807 // Just to be safe, recalculate the offset in debug mode
 808 static jlong _calculated_offset = 0;
 809 static int   _has_calculated_offset = 0;
 810 
 811 jlong offset() {
 812   if (_has_calculated_offset) return _calculated_offset;
 813   SYSTEMTIME java_origin;
 814   java_origin.wYear          = 1970;
 815   java_origin.wMonth         = 1;
 816   java_origin.wDayOfWeek     = 0; // ignored
 817   java_origin.wDay           = 1;
 818   java_origin.wHour          = 0;
 819   java_origin.wMinute        = 0;
 820   java_origin.wSecond        = 0;
 821   java_origin.wMilliseconds  = 0;
 822   FILETIME jot;
 823   if (!SystemTimeToFileTime(&java_origin, &jot)) {
 824     fatal(err_msg("Error = %d\nWindows error", GetLastError()));
 825   }
 826   _calculated_offset = jlong_from(jot.dwHighDateTime, jot.dwLowDateTime);
 827   _has_calculated_offset = 1;
 828   assert(_calculated_offset == _offset, "Calculated and constant time offsets must be equal");
 829   return _calculated_offset;
 830 }
 831 #else
 832 jlong offset() {
 833   return _offset;
 834 }
 835 #endif
 836 
 837 jlong windows_to_java_time(FILETIME wt) {
 838   jlong a = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
 839   return (a - offset()) / 10000;
 840 }
 841 
 842 jlong windows_time_stamp(FILETIME wt) {
 843   jlong a = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
 844   return (a - offset());
 845 }
 846 
 847 FILETIME java_to_windows_time(jlong l) {
 848   jlong a = (l * 10000) + offset();
 849   FILETIME result;
 850   result.dwHighDateTime = high(a);
 851   result.dwLowDateTime  = low(a);
 852   return result;
 853 }
 854 
 855 bool os::supports_vtime() { return true; }
 856 bool os::enable_vtime() { return false; }
 857 bool os::vtime_enabled() { return false; }
 858 
 859 double os::elapsedVTime() {
 860   FILETIME created;
 861   FILETIME exited;
 862   FILETIME kernel;
 863   FILETIME user;
 864   if (GetThreadTimes(GetCurrentThread(), &created, &exited, &kernel, &user) != 0) {
 865     // the resolution of windows_to_java_time() should be sufficient (ms)
 866     return (double) (windows_to_java_time(kernel) + windows_to_java_time(user)) / MILLIUNITS;
 867   } else {
 868     return elapsedTime();
 869   }
 870 }
 871 
 872 jlong os::javaTimeMillis() {
 873   if (UseFakeTimers) {
 874     return fake_time++;
 875   } else {
 876     FILETIME wt;
 877     GetSystemTimeAsFileTime(&wt);
 878     return windows_to_java_time(wt);
 879   }
 880 }
 881 
 882 void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {
 883   FILETIME wt;
 884   GetSystemTimeAsFileTime(&wt);
 885   jlong ts = windows_time_stamp(wt); // 10th of micros
 886   jlong secs = jlong(ts / 10000000); // 10000 * 1000
 887   seconds = secs; 
 888   nanos = jlong(ts - (secs*10000000)) * 100;
 889 }
 890 
 891 jlong os::javaTimeNanos() {
 892   if (!win32::_has_performance_count) {
 893     return javaTimeMillis() * NANOSECS_PER_MILLISEC; // the best we can do.
 894   } else {
 895     LARGE_INTEGER current_count;
 896     QueryPerformanceCounter(&current_count);
 897     double current = as_long(current_count);
 898     double freq = performance_frequency;
 899     jlong time = (jlong)((current/freq) * NANOSECS_PER_SEC);
 900     return time;
 901   }
 902 }
 903 
 904 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
 905   if (!win32::_has_performance_count) {
 906     // javaTimeMillis() doesn't have much percision,
 907     // but it is not going to wrap -- so all 64 bits
 908     info_ptr->max_value = ALL_64_BITS;
 909 
 910     // this is a wall clock timer, so may skip
 911     info_ptr->may_skip_backward = true;
 912     info_ptr->may_skip_forward = true;
 913   } else {
 914     jlong freq = performance_frequency;
 915     if (freq < NANOSECS_PER_SEC) {
 916       // the performance counter is 64 bits and we will
 917       // be multiplying it -- so no wrap in 64 bits
 918       info_ptr->max_value = ALL_64_BITS;
 919     } else if (freq > NANOSECS_PER_SEC) {
 920       // use the max value the counter can reach to
 921       // determine the max value which could be returned
 922       julong max_counter = (julong)ALL_64_BITS;
 923       info_ptr->max_value = (jlong)(max_counter / (freq / NANOSECS_PER_SEC));
 924     } else {
 925       // the performance counter is 64 bits and we will
 926       // be using it directly -- so no wrap in 64 bits
 927       info_ptr->max_value = ALL_64_BITS;
 928     }
 929 
 930     // using a counter, so no skipping
 931     info_ptr->may_skip_backward = false;
 932     info_ptr->may_skip_forward = false;
 933   }
 934   info_ptr->kind = JVMTI_TIMER_ELAPSED;                // elapsed not CPU time
 935 }
 936 
 937 char* os::local_time_string(char *buf, size_t buflen) {
 938   SYSTEMTIME st;
 939   GetLocalTime(&st);
 940   jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
 941                st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
 942   return buf;
 943 }
 944 
 945 bool os::getTimesSecs(double* process_real_time,
 946                       double* process_user_time,
 947                       double* process_system_time) {
 948   HANDLE h_process = GetCurrentProcess();
 949   FILETIME create_time, exit_time, kernel_time, user_time;
 950   BOOL result = GetProcessTimes(h_process,
 951                                 &create_time,
 952                                 &exit_time,
 953                                 &kernel_time,
 954                                 &user_time);
 955   if (result != 0) {
 956     FILETIME wt;
 957     GetSystemTimeAsFileTime(&wt);
 958     jlong rtc_millis = windows_to_java_time(wt);
 959     jlong user_millis = windows_to_java_time(user_time);
 960     jlong system_millis = windows_to_java_time(kernel_time);
 961     *process_real_time = ((double) rtc_millis) / ((double) MILLIUNITS);
 962     *process_user_time = ((double) user_millis) / ((double) MILLIUNITS);
 963     *process_system_time = ((double) system_millis) / ((double) MILLIUNITS);
 964     return true;
 965   } else {
 966     return false;
 967   }
 968 }
 969 
 970 void os::shutdown() {
 971   // allow PerfMemory to attempt cleanup of any persistent resources
 972   perfMemory_exit();
 973 
 974   // flush buffered output, finish log files
 975   ostream_abort();
 976 
 977   // Check for abort hook
 978   abort_hook_t abort_hook = Arguments::abort_hook();
 979   if (abort_hook != NULL) {
 980     abort_hook();
 981   }
 982 }
 983 
 984 
 985 static BOOL (WINAPI *_MiniDumpWriteDump)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE,
 986                                          PMINIDUMP_EXCEPTION_INFORMATION,
 987                                          PMINIDUMP_USER_STREAM_INFORMATION,
 988                                          PMINIDUMP_CALLBACK_INFORMATION);
 989 
 990 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
 991   HINSTANCE dbghelp;
 992   EXCEPTION_POINTERS ep;
 993   MINIDUMP_EXCEPTION_INFORMATION mei;
 994   MINIDUMP_EXCEPTION_INFORMATION* pmei;
 995 
 996   HANDLE hProcess = GetCurrentProcess();
 997   DWORD processId = GetCurrentProcessId();
 998   HANDLE dumpFile;
 999   MINIDUMP_TYPE dumpType;
1000   static const char* cwd;
1001 
1002 // Default is to always create dump for debug builds, on product builds only dump on server versions of Windows.
1003 #ifndef ASSERT
1004   // If running on a client version of Windows and user has not explicitly enabled dumping
1005   if (!os::win32::is_windows_server() && !CreateMinidumpOnCrash) {
1006     VMError::report_coredump_status("Minidumps are not enabled by default on client versions of Windows", false);
1007     return;
1008     // If running on a server version of Windows and user has explictly disabled dumping
1009   } else if (os::win32::is_windows_server() && !FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) {
1010     VMError::report_coredump_status("Minidump has been disabled from the command line", false);
1011     return;
1012   }
1013 #else
1014   if (!FLAG_IS_DEFAULT(CreateMinidumpOnCrash) && !CreateMinidumpOnCrash) {
1015     VMError::report_coredump_status("Minidump has been disabled from the command line", false);
1016     return;
1017   }
1018 #endif
1019 
1020   dbghelp = os::win32::load_Windows_dll("DBGHELP.DLL", NULL, 0);
1021 
1022   if (dbghelp == NULL) {
1023     VMError::report_coredump_status("Failed to load dbghelp.dll", false);
1024     return;
1025   }
1026 
1027   _MiniDumpWriteDump =
1028       CAST_TO_FN_PTR(BOOL(WINAPI *)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE,
1029                                     PMINIDUMP_EXCEPTION_INFORMATION,
1030                                     PMINIDUMP_USER_STREAM_INFORMATION,
1031                                     PMINIDUMP_CALLBACK_INFORMATION),
1032                                     GetProcAddress(dbghelp,
1033                                     "MiniDumpWriteDump"));
1034 
1035   if (_MiniDumpWriteDump == NULL) {
1036     VMError::report_coredump_status("Failed to find MiniDumpWriteDump() in module dbghelp.dll", false);
1037     return;
1038   }
1039 
1040   dumpType = (MINIDUMP_TYPE)(MiniDumpWithFullMemory | MiniDumpWithHandleData);
1041 
1042 // Older versions of dbghelp.h doesn't contain all the dumptypes we want, dbghelp.h with
1043 // API_VERSION_NUMBER 11 or higher contains the ones we want though
1044 #if API_VERSION_NUMBER >= 11
1045   dumpType = (MINIDUMP_TYPE)(dumpType | MiniDumpWithFullMemoryInfo | MiniDumpWithThreadInfo |
1046                              MiniDumpWithUnloadedModules);
1047 #endif
1048 
1049   cwd = get_current_directory(NULL, 0);
1050   jio_snprintf(buffer, bufferSize, "%s\\hs_err_pid%u.mdmp", cwd, current_process_id());
1051   dumpFile = CreateFile(buffer, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1052 
1053   if (dumpFile == INVALID_HANDLE_VALUE) {
1054     VMError::report_coredump_status("Failed to create file for dumping", false);
1055     return;
1056   }
1057   if (exceptionRecord != NULL && contextRecord != NULL) {
1058     ep.ContextRecord = (PCONTEXT) contextRecord;
1059     ep.ExceptionRecord = (PEXCEPTION_RECORD) exceptionRecord;
1060 
1061     mei.ThreadId = GetCurrentThreadId();
1062     mei.ExceptionPointers = &ep;
1063     pmei = &mei;
1064   } else {
1065     pmei = NULL;
1066   }
1067 
1068 
1069   // Older versions of dbghelp.dll (the one shipped with Win2003 for example) may not support all
1070   // the dump types we really want. If first call fails, lets fall back to just use MiniDumpWithFullMemory then.
1071   if (_MiniDumpWriteDump(hProcess, processId, dumpFile, dumpType, pmei, NULL, NULL) == false &&
1072       _MiniDumpWriteDump(hProcess, processId, dumpFile, (MINIDUMP_TYPE)MiniDumpWithFullMemory, pmei, NULL, NULL) == false) {
1073     DWORD error = GetLastError();
1074     LPTSTR msgbuf = NULL;
1075 
1076     if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1077                       FORMAT_MESSAGE_FROM_SYSTEM |
1078                       FORMAT_MESSAGE_IGNORE_INSERTS,
1079                       NULL, error, 0, (LPTSTR)&msgbuf, 0, NULL) != 0) {
1080 
1081       jio_snprintf(buffer, bufferSize, "Call to MiniDumpWriteDump() failed (Error 0x%x: %s)", error, msgbuf);
1082       LocalFree(msgbuf);
1083     } else {
1084       // Call to FormatMessage failed, just include the result from GetLastError
1085       jio_snprintf(buffer, bufferSize, "Call to MiniDumpWriteDump() failed (Error 0x%x)", error);
1086     }
1087     VMError::report_coredump_status(buffer, false);
1088   } else {
1089     VMError::report_coredump_status(buffer, true);
1090   }
1091 
1092   CloseHandle(dumpFile);
1093 }
1094 
1095 
1096 void os::abort(bool dump_core) {
1097   os::shutdown();
1098   // no core dump on Windows
1099   win32::exit_process_or_thread(win32::EPT_PROCESS, 1);
1100 }
1101 
1102 // Die immediately, no exit hook, no abort hook, no cleanup.
1103 void os::die() {
1104   win32::exit_process_or_thread(win32::EPT_PROCESS_DIE, -1);
1105 }
1106 
1107 // Directory routines copied from src/win32/native/java/io/dirent_md.c
1108 //  * dirent_md.c       1.15 00/02/02
1109 //
1110 // The declarations for DIR and struct dirent are in jvm_win32.h.
1111 
1112 // Caller must have already run dirname through JVM_NativePath, which removes
1113 // duplicate slashes and converts all instances of '/' into '\\'.
1114 
1115 DIR * os::opendir(const char *dirname) {
1116   assert(dirname != NULL, "just checking");   // hotspot change
1117   DIR *dirp = (DIR *)malloc(sizeof(DIR), mtInternal);
1118   DWORD fattr;                                // hotspot change
1119   char alt_dirname[4] = { 0, 0, 0, 0 };
1120 
1121   if (dirp == 0) {
1122     errno = ENOMEM;
1123     return 0;
1124   }
1125 
1126   // Win32 accepts "\" in its POSIX stat(), but refuses to treat it
1127   // as a directory in FindFirstFile().  We detect this case here and
1128   // prepend the current drive name.
1129   //
1130   if (dirname[1] == '\0' && dirname[0] == '\\') {
1131     alt_dirname[0] = _getdrive() + 'A' - 1;
1132     alt_dirname[1] = ':';
1133     alt_dirname[2] = '\\';
1134     alt_dirname[3] = '\0';
1135     dirname = alt_dirname;
1136   }
1137 
1138   dirp->path = (char *)malloc(strlen(dirname) + 5, mtInternal);
1139   if (dirp->path == 0) {
1140     free(dirp);
1141     errno = ENOMEM;
1142     return 0;
1143   }
1144   strcpy(dirp->path, dirname);
1145 
1146   fattr = GetFileAttributes(dirp->path);
1147   if (fattr == 0xffffffff) {
1148     free(dirp->path);
1149     free(dirp);
1150     errno = ENOENT;
1151     return 0;
1152   } else if ((fattr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
1153     free(dirp->path);
1154     free(dirp);
1155     errno = ENOTDIR;
1156     return 0;
1157   }
1158 
1159   // Append "*.*", or possibly "\\*.*", to path
1160   if (dirp->path[1] == ':' &&
1161       (dirp->path[2] == '\0' ||
1162       (dirp->path[2] == '\\' && dirp->path[3] == '\0'))) {
1163     // No '\\' needed for cases like "Z:" or "Z:\"
1164     strcat(dirp->path, "*.*");
1165   } else {
1166     strcat(dirp->path, "\\*.*");
1167   }
1168 
1169   dirp->handle = FindFirstFile(dirp->path, &dirp->find_data);
1170   if (dirp->handle == INVALID_HANDLE_VALUE) {
1171     if (GetLastError() != ERROR_FILE_NOT_FOUND) {
1172       free(dirp->path);
1173       free(dirp);
1174       errno = EACCES;
1175       return 0;
1176     }
1177   }
1178   return dirp;
1179 }
1180 
1181 // parameter dbuf unused on Windows
1182 struct dirent * os::readdir(DIR *dirp, dirent *dbuf) {
1183   assert(dirp != NULL, "just checking");      // hotspot change
1184   if (dirp->handle == INVALID_HANDLE_VALUE) {
1185     return 0;
1186   }
1187 
1188   strcpy(dirp->dirent.d_name, dirp->find_data.cFileName);
1189 
1190   if (!FindNextFile(dirp->handle, &dirp->find_data)) {
1191     if (GetLastError() == ERROR_INVALID_HANDLE) {
1192       errno = EBADF;
1193       return 0;
1194     }
1195     FindClose(dirp->handle);
1196     dirp->handle = INVALID_HANDLE_VALUE;
1197   }
1198 
1199   return &dirp->dirent;
1200 }
1201 
1202 int os::closedir(DIR *dirp) {
1203   assert(dirp != NULL, "just checking");      // hotspot change
1204   if (dirp->handle != INVALID_HANDLE_VALUE) {
1205     if (!FindClose(dirp->handle)) {
1206       errno = EBADF;
1207       return -1;
1208     }
1209     dirp->handle = INVALID_HANDLE_VALUE;
1210   }
1211   free(dirp->path);
1212   free(dirp);
1213   return 0;
1214 }
1215 
1216 // This must be hard coded because it's the system's temporary
1217 // directory not the java application's temp directory, ala java.io.tmpdir.
1218 const char* os::get_temp_directory() {
1219   static char path_buf[MAX_PATH];
1220   if (GetTempPath(MAX_PATH, path_buf) > 0) {
1221     return path_buf;
1222   } else {
1223     path_buf[0] = '\0';
1224     return path_buf;
1225   }
1226 }
1227 
1228 static bool file_exists(const char* filename) {
1229   if (filename == NULL || strlen(filename) == 0) {
1230     return false;
1231   }
1232   return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
1233 }
1234 
1235 bool os::dll_build_name(char *buffer, size_t buflen,
1236                         const char* pname, const char* fname) {
1237   bool retval = false;
1238   const size_t pnamelen = pname ? strlen(pname) : 0;
1239   const char c = (pnamelen > 0) ? pname[pnamelen-1] : 0;
1240 
1241   // Return error on buffer overflow.
1242   if (pnamelen + strlen(fname) + 10 > buflen) {
1243     return retval;
1244   }
1245 
1246   if (pnamelen == 0) {
1247     jio_snprintf(buffer, buflen, "%s.dll", fname);
1248     retval = true;
1249   } else if (c == ':' || c == '\\') {
1250     jio_snprintf(buffer, buflen, "%s%s.dll", pname, fname);
1251     retval = true;
1252   } else if (strchr(pname, *os::path_separator()) != NULL) {
1253     int n;
1254     char** pelements = split_path(pname, &n);
1255     if (pelements == NULL) {
1256       return false;
1257     }
1258     for (int i = 0; i < n; i++) {
1259       char* path = pelements[i];
1260       // Really shouldn't be NULL, but check can't hurt
1261       size_t plen = (path == NULL) ? 0 : strlen(path);
1262       if (plen == 0) {
1263         continue; // skip the empty path values
1264       }
1265       const char lastchar = path[plen - 1];
1266       if (lastchar == ':' || lastchar == '\\') {
1267         jio_snprintf(buffer, buflen, "%s%s.dll", path, fname);
1268       } else {
1269         jio_snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
1270       }
1271       if (file_exists(buffer)) {
1272         retval = true;
1273         break;
1274       }
1275     }
1276     // release the storage
1277     for (int i = 0; i < n; i++) {
1278       if (pelements[i] != NULL) {
1279         FREE_C_HEAP_ARRAY(char, pelements[i]);
1280       }
1281     }
1282     if (pelements != NULL) {
1283       FREE_C_HEAP_ARRAY(char*, pelements);
1284     }
1285   } else {
1286     jio_snprintf(buffer, buflen, "%s\\%s.dll", pname, fname);
1287     retval = true;
1288   }
1289   return retval;
1290 }
1291 
1292 // Needs to be in os specific directory because windows requires another
1293 // header file <direct.h>
1294 const char* os::get_current_directory(char *buf, size_t buflen) {
1295   int n = static_cast<int>(buflen);
1296   if (buflen > INT_MAX)  n = INT_MAX;
1297   return _getcwd(buf, n);
1298 }
1299 
1300 //-----------------------------------------------------------
1301 // Helper functions for fatal error handler
1302 #ifdef _WIN64
1303 // Helper routine which returns true if address in
1304 // within the NTDLL address space.
1305 //
1306 static bool _addr_in_ntdll(address addr) {
1307   HMODULE hmod;
1308   MODULEINFO minfo;
1309 
1310   hmod = GetModuleHandle("NTDLL.DLL");
1311   if (hmod == NULL) return false;
1312   if (!os::PSApiDll::GetModuleInformation(GetCurrentProcess(), hmod,
1313                                           &minfo, sizeof(MODULEINFO))) {
1314     return false;
1315   }
1316 
1317   if ((addr >= minfo.lpBaseOfDll) &&
1318       (addr < (address)((uintptr_t)minfo.lpBaseOfDll + (uintptr_t)minfo.SizeOfImage))) {
1319     return true;
1320   } else {
1321     return false;
1322   }
1323 }
1324 #endif
1325 
1326 struct _modinfo {
1327   address addr;
1328   char*   full_path;   // point to a char buffer
1329   int     buflen;      // size of the buffer
1330   address base_addr;
1331 };
1332 
1333 static int _locate_module_by_addr(const char * mod_fname, address base_addr,
1334                                   address top_address, void * param) {
1335   struct _modinfo *pmod = (struct _modinfo *)param;
1336   if (!pmod) return -1;
1337 
1338   if (base_addr   <= pmod->addr &&
1339       top_address > pmod->addr) {
1340     // if a buffer is provided, copy path name to the buffer
1341     if (pmod->full_path) {
1342       jio_snprintf(pmod->full_path, pmod->buflen, "%s", mod_fname);
1343     }
1344     pmod->base_addr = base_addr;
1345     return 1;
1346   }
1347   return 0;
1348 }
1349 
1350 bool os::dll_address_to_library_name(address addr, char* buf,
1351                                      int buflen, int* offset) {
1352   // buf is not optional, but offset is optional
1353   assert(buf != NULL, "sanity check");
1354 
1355 // NOTE: the reason we don't use SymGetModuleInfo() is it doesn't always
1356 //       return the full path to the DLL file, sometimes it returns path
1357 //       to the corresponding PDB file (debug info); sometimes it only
1358 //       returns partial path, which makes life painful.
1359 
1360   struct _modinfo mi;
1361   mi.addr      = addr;
1362   mi.full_path = buf;
1363   mi.buflen    = buflen;
1364   if (get_loaded_modules_info(_locate_module_by_addr, (void *)&mi)) {
1365     // buf already contains path name
1366     if (offset) *offset = addr - mi.base_addr;
1367     return true;
1368   }
1369 
1370   buf[0] = '\0';
1371   if (offset) *offset = -1;
1372   return false;
1373 }
1374 
1375 bool os::dll_address_to_function_name(address addr, char *buf,
1376                                       int buflen, int *offset) {
1377   // buf is not optional, but offset is optional
1378   assert(buf != NULL, "sanity check");
1379 
1380   if (Decoder::decode(addr, buf, buflen, offset)) {
1381     return true;
1382   }
1383   if (offset != NULL)  *offset  = -1;
1384   buf[0] = '\0';
1385   return false;
1386 }
1387 
1388 // save the start and end address of jvm.dll into param[0] and param[1]
1389 static int _locate_jvm_dll(const char* mod_fname, address base_addr,
1390                            address top_address, void * param) {
1391   if (!param) return -1;
1392 
1393   if (base_addr   <= (address)_locate_jvm_dll &&
1394       top_address > (address)_locate_jvm_dll) {
1395     ((address*)param)[0] = base_addr;
1396     ((address*)param)[1] = top_address;
1397     return 1;
1398   }
1399   return 0;
1400 }
1401 
1402 address vm_lib_location[2];    // start and end address of jvm.dll
1403 
1404 // check if addr is inside jvm.dll
1405 bool os::address_is_in_vm(address addr) {
1406   if (!vm_lib_location[0] || !vm_lib_location[1]) {
1407     if (!get_loaded_modules_info(_locate_jvm_dll, (void *)vm_lib_location)) {
1408       assert(false, "Can't find jvm module.");
1409       return false;
1410     }
1411   }
1412 
1413   return (vm_lib_location[0] <= addr) && (addr < vm_lib_location[1]);
1414 }
1415 
1416 // print module info; param is outputStream*
1417 static int _print_module(const char* fname, address base_address,
1418                          address top_address, void* param) {
1419   if (!param) return -1;
1420 
1421   outputStream* st = (outputStream*)param;
1422 
1423   st->print(PTR_FORMAT " - " PTR_FORMAT " \t%s\n", base_address, top_address, fname);
1424   return 0;
1425 }
1426 
1427 // Loads .dll/.so and
1428 // in case of error it checks if .dll/.so was built for the
1429 // same architecture as Hotspot is running on
1430 void * os::dll_load(const char *name, char *ebuf, int ebuflen) {
1431   void * result = LoadLibrary(name);
1432   if (result != NULL) {
1433     return result;
1434   }
1435 
1436   DWORD errcode = GetLastError();
1437   if (errcode == ERROR_MOD_NOT_FOUND) {
1438     strncpy(ebuf, "Can't find dependent libraries", ebuflen - 1);
1439     ebuf[ebuflen - 1] = '\0';
1440     return NULL;
1441   }
1442 
1443   // Parsing dll below
1444   // If we can read dll-info and find that dll was built
1445   // for an architecture other than Hotspot is running in
1446   // - then print to buffer "DLL was built for a different architecture"
1447   // else call os::lasterror to obtain system error message
1448 
1449   // Read system error message into ebuf
1450   // It may or may not be overwritten below (in the for loop and just above)
1451   lasterror(ebuf, (size_t) ebuflen);
1452   ebuf[ebuflen - 1] = '\0';
1453   int fd = ::open(name, O_RDONLY | O_BINARY, 0);
1454   if (fd < 0) {
1455     return NULL;
1456   }
1457 
1458   uint32_t signature_offset;
1459   uint16_t lib_arch = 0;
1460   bool failed_to_get_lib_arch =
1461     ( // Go to position 3c in the dll
1462      (os::seek_to_file_offset(fd, IMAGE_FILE_PTR_TO_SIGNATURE) < 0)
1463      ||
1464      // Read location of signature
1465      (sizeof(signature_offset) !=
1466      (os::read(fd, (void*)&signature_offset, sizeof(signature_offset))))
1467      ||
1468      // Go to COFF File Header in dll
1469      // that is located after "signature" (4 bytes long)
1470      (os::seek_to_file_offset(fd,
1471      signature_offset + IMAGE_FILE_SIGNATURE_LENGTH) < 0)
1472      ||
1473      // Read field that contains code of architecture
1474      // that dll was built for
1475      (sizeof(lib_arch) != (os::read(fd, (void*)&lib_arch, sizeof(lib_arch))))
1476     );
1477 
1478   ::close(fd);
1479   if (failed_to_get_lib_arch) {
1480     // file i/o error - report os::lasterror(...) msg
1481     return NULL;
1482   }
1483 
1484   typedef struct {
1485     uint16_t arch_code;
1486     char* arch_name;
1487   } arch_t;
1488 
1489   static const arch_t arch_array[] = {
1490     {IMAGE_FILE_MACHINE_I386,      (char*)"IA 32"},
1491     {IMAGE_FILE_MACHINE_AMD64,     (char*)"AMD 64"},
1492     {IMAGE_FILE_MACHINE_IA64,      (char*)"IA 64"}
1493   };
1494 #if   (defined _M_IA64)
1495   static const uint16_t running_arch = IMAGE_FILE_MACHINE_IA64;
1496 #elif (defined _M_AMD64)
1497   static const uint16_t running_arch = IMAGE_FILE_MACHINE_AMD64;
1498 #elif (defined _M_IX86)
1499   static const uint16_t running_arch = IMAGE_FILE_MACHINE_I386;
1500 #else
1501   #error Method os::dll_load requires that one of following \
1502          is defined :_M_IA64,_M_AMD64 or _M_IX86
1503 #endif
1504 
1505 
1506   // Obtain a string for printf operation
1507   // lib_arch_str shall contain string what platform this .dll was built for
1508   // running_arch_str shall string contain what platform Hotspot was built for
1509   char *running_arch_str = NULL, *lib_arch_str = NULL;
1510   for (unsigned int i = 0; i < ARRAY_SIZE(arch_array); i++) {
1511     if (lib_arch == arch_array[i].arch_code) {
1512       lib_arch_str = arch_array[i].arch_name;
1513     }
1514     if (running_arch == arch_array[i].arch_code) {
1515       running_arch_str = arch_array[i].arch_name;
1516     }
1517   }
1518 
1519   assert(running_arch_str,
1520          "Didn't find running architecture code in arch_array");
1521 
1522   // If the architecture is right
1523   // but some other error took place - report os::lasterror(...) msg
1524   if (lib_arch == running_arch) {
1525     return NULL;
1526   }
1527 
1528   if (lib_arch_str != NULL) {
1529     ::_snprintf(ebuf, ebuflen - 1,
1530                 "Can't load %s-bit .dll on a %s-bit platform",
1531                 lib_arch_str, running_arch_str);
1532   } else {
1533     // don't know what architecture this dll was build for
1534     ::_snprintf(ebuf, ebuflen - 1,
1535                 "Can't load this .dll (machine code=0x%x) on a %s-bit platform",
1536                 lib_arch, running_arch_str);
1537   }
1538 
1539   return NULL;
1540 }
1541 
1542 void os::print_dll_info(outputStream *st) {
1543   st->print_cr("Dynamic libraries:");
1544   get_loaded_modules_info(_print_module, (void *)st);
1545 }
1546 
1547 int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {
1548   HANDLE   hProcess;
1549 
1550 # define MAX_NUM_MODULES 128
1551   HMODULE     modules[MAX_NUM_MODULES];
1552   static char filename[MAX_PATH];
1553   int         result = 0;
1554 
1555   if (!os::PSApiDll::PSApiAvailable()) {
1556     return 0;
1557   }
1558 
1559   int pid = os::current_process_id();
1560   hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
1561                          FALSE, pid);
1562   if (hProcess == NULL) return 0;
1563 
1564   DWORD size_needed;
1565   if (!os::PSApiDll::EnumProcessModules(hProcess, modules,
1566                                         sizeof(modules), &size_needed)) {
1567     CloseHandle(hProcess);
1568     return 0;
1569   }
1570 
1571   // number of modules that are currently loaded
1572   int num_modules = size_needed / sizeof(HMODULE);
1573 
1574   for (int i = 0; i < MIN2(num_modules, MAX_NUM_MODULES); i++) {
1575     // Get Full pathname:
1576     if (!os::PSApiDll::GetModuleFileNameEx(hProcess, modules[i],
1577                                            filename, sizeof(filename))) {
1578       filename[0] = '\0';
1579     }
1580 
1581     MODULEINFO modinfo;
1582     if (!os::PSApiDll::GetModuleInformation(hProcess, modules[i],
1583                                             &modinfo, sizeof(modinfo))) {
1584       modinfo.lpBaseOfDll = NULL;
1585       modinfo.SizeOfImage = 0;
1586     }
1587 
1588     // Invoke callback function
1589     result = callback(filename, (address)modinfo.lpBaseOfDll,
1590                       (address)((u8)modinfo.lpBaseOfDll + (u8)modinfo.SizeOfImage), param);
1591     if (result) break;
1592   }
1593 
1594   CloseHandle(hProcess);
1595   return result;
1596 }
1597 
1598 void os::print_os_info_brief(outputStream* st) {
1599   os::print_os_info(st);
1600 }
1601 
1602 void os::print_os_info(outputStream* st) {
1603 #ifdef ASSERT
1604   char buffer[1024];
1605   DWORD size = sizeof(buffer);
1606   st->print(" HostName: ");
1607   if (GetComputerNameEx(ComputerNameDnsHostname, buffer, &size)) {
1608     st->print("%s", buffer);
1609   } else {
1610     st->print("N/A");
1611   }
1612 #endif
1613   st->print(" OS:");
1614   os::win32::print_windows_version(st);
1615 }
1616 
1617 void os::win32::print_windows_version(outputStream* st) {
1618   OSVERSIONINFOEX osvi;
1619   VS_FIXEDFILEINFO *file_info;
1620   TCHAR kernel32_path[MAX_PATH];
1621   UINT len, ret;
1622 
1623   // Use the GetVersionEx information to see if we're on a server or
1624   // workstation edition of Windows. Starting with Windows 8.1 we can't
1625   // trust the OS version information returned by this API.
1626   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
1627   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1628   if (!GetVersionEx((OSVERSIONINFO *)&osvi)) {
1629     st->print_cr("Call to GetVersionEx failed");
1630     return;
1631   }
1632   bool is_workstation = (osvi.wProductType == VER_NT_WORKSTATION);
1633 
1634   // Get the full path to \Windows\System32\kernel32.dll and use that for
1635   // determining what version of Windows we're running on.
1636   len = MAX_PATH - (UINT)strlen("\\kernel32.dll") - 1;
1637   ret = GetSystemDirectory(kernel32_path, len);
1638   if (ret == 0 || ret > len) {
1639     st->print_cr("Call to GetSystemDirectory failed");
1640     return;
1641   }
1642   strncat(kernel32_path, "\\kernel32.dll", MAX_PATH - ret);
1643 
1644   DWORD version_size = GetFileVersionInfoSize(kernel32_path, NULL);
1645   if (version_size == 0) {
1646     st->print_cr("Call to GetFileVersionInfoSize failed");
1647     return;
1648   }
1649 
1650   LPTSTR version_info = (LPTSTR)os::malloc(version_size, mtInternal);
1651   if (version_info == NULL) {
1652     st->print_cr("Failed to allocate version_info");
1653     return;
1654   }
1655 
1656   if (!GetFileVersionInfo(kernel32_path, NULL, version_size, version_info)) {
1657     os::free(version_info);
1658     st->print_cr("Call to GetFileVersionInfo failed");
1659     return;
1660   }
1661 
1662   if (!VerQueryValue(version_info, TEXT("\\"), (LPVOID*)&file_info, &len)) {
1663     os::free(version_info);
1664     st->print_cr("Call to VerQueryValue failed");
1665     return;
1666   }
1667 
1668   int major_version = HIWORD(file_info->dwProductVersionMS);
1669   int minor_version = LOWORD(file_info->dwProductVersionMS);
1670   int build_number = HIWORD(file_info->dwProductVersionLS);
1671   int build_minor = LOWORD(file_info->dwProductVersionLS);
1672   int os_vers = major_version * 1000 + minor_version;
1673   os::free(version_info);
1674 
1675   st->print(" Windows ");
1676   switch (os_vers) {
1677 
1678   case 6000:
1679     if (is_workstation) {
1680       st->print("Vista");
1681     } else {
1682       st->print("Server 2008");
1683     }
1684     break;
1685 
1686   case 6001:
1687     if (is_workstation) {
1688       st->print("7");
1689     } else {
1690       st->print("Server 2008 R2");
1691     }
1692     break;
1693 
1694   case 6002:
1695     if (is_workstation) {
1696       st->print("8");
1697     } else {
1698       st->print("Server 2012");
1699     }
1700     break;
1701 
1702   case 6003:
1703     if (is_workstation) {
1704       st->print("8.1");
1705     } else {
1706       st->print("Server 2012 R2");
1707     }
1708     break;
1709 
1710   case 6004:
1711     if (is_workstation) {
1712       st->print("10");
1713     } else {
1714       // The server version name of Windows 10 is not known at this time
1715       st->print("%d.%d", major_version, minor_version);
1716     }
1717     break;
1718 
1719   default:
1720     // Unrecognized windows, print out its major and minor versions
1721     st->print("%d.%d", major_version, minor_version);
1722     break;
1723   }
1724 
1725   // Retrieve SYSTEM_INFO from GetNativeSystemInfo call so that we could
1726   // find out whether we are running on 64 bit processor or not
1727   SYSTEM_INFO si;
1728   ZeroMemory(&si, sizeof(SYSTEM_INFO));
1729   os::Kernel32Dll::GetNativeSystemInfo(&si);
1730   if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
1731     st->print(" , 64 bit");
1732   }
1733 
1734   st->print(" Build %d", build_number);
1735   st->print(" (%d.%d.%d.%d)", major_version, minor_version, build_number, build_minor);
1736   st->cr();
1737 }
1738 
1739 void os::pd_print_cpu_info(outputStream* st) {
1740   // Nothing to do for now.
1741 }
1742 
1743 void os::print_memory_info(outputStream* st) {
1744   st->print("Memory:");
1745   st->print(" %dk page", os::vm_page_size()>>10);
1746 
1747   // Use GlobalMemoryStatusEx() because GlobalMemoryStatus() may return incorrect
1748   // value if total memory is larger than 4GB
1749   MEMORYSTATUSEX ms;
1750   ms.dwLength = sizeof(ms);
1751   GlobalMemoryStatusEx(&ms);
1752 
1753   st->print(", physical %uk", os::physical_memory() >> 10);
1754   st->print("(%uk free)", os::available_memory() >> 10);
1755 
1756   st->print(", swap %uk", ms.ullTotalPageFile >> 10);
1757   st->print("(%uk free)", ms.ullAvailPageFile >> 10);
1758   st->cr();
1759 }
1760 
1761 void os::print_siginfo(outputStream *st, void *siginfo) {
1762   EXCEPTION_RECORD* er = (EXCEPTION_RECORD*)siginfo;
1763   st->print("siginfo:");
1764   st->print(" ExceptionCode=0x%x", er->ExceptionCode);
1765 
1766   if (er->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
1767       er->NumberParameters >= 2) {
1768     switch (er->ExceptionInformation[0]) {
1769     case 0: st->print(", reading address"); break;
1770     case 1: st->print(", writing address"); break;
1771     default: st->print(", ExceptionInformation=" INTPTR_FORMAT,
1772                        er->ExceptionInformation[0]);
1773     }
1774     st->print(" " INTPTR_FORMAT, er->ExceptionInformation[1]);
1775   } else if (er->ExceptionCode == EXCEPTION_IN_PAGE_ERROR &&
1776              er->NumberParameters >= 2 && UseSharedSpaces) {
1777     FileMapInfo* mapinfo = FileMapInfo::current_info();
1778     if (mapinfo->is_in_shared_space((void*)er->ExceptionInformation[1])) {
1779       st->print("\n\nError accessing class data sharing archive."       \
1780                 " Mapped file inaccessible during execution, "          \
1781                 " possible disk/network problem.");
1782     }
1783   } else {
1784     int num = er->NumberParameters;
1785     if (num > 0) {
1786       st->print(", ExceptionInformation=");
1787       for (int i = 0; i < num; i++) {
1788         st->print(INTPTR_FORMAT " ", er->ExceptionInformation[i]);
1789       }
1790     }
1791   }
1792   st->cr();
1793 }
1794 
1795 void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
1796   // do nothing
1797 }
1798 
1799 static char saved_jvm_path[MAX_PATH] = {0};
1800 
1801 // Find the full path to the current module, jvm.dll
1802 void os::jvm_path(char *buf, jint buflen) {
1803   // Error checking.
1804   if (buflen < MAX_PATH) {
1805     assert(false, "must use a large-enough buffer");
1806     buf[0] = '\0';
1807     return;
1808   }
1809   // Lazy resolve the path to current module.
1810   if (saved_jvm_path[0] != 0) {
1811     strcpy(buf, saved_jvm_path);
1812     return;
1813   }
1814 
1815   buf[0] = '\0';
1816   if (Arguments::sun_java_launcher_is_altjvm()) {
1817     // Support for the java launcher's '-XXaltjvm=<path>' option. Check
1818     // for a JAVA_HOME environment variable and fix up the path so it
1819     // looks like jvm.dll is installed there (append a fake suffix
1820     // hotspot/jvm.dll).
1821     char* java_home_var = ::getenv("JAVA_HOME");
1822     if (java_home_var != NULL && java_home_var[0] != 0 &&
1823         strlen(java_home_var) < (size_t)buflen) {
1824       strncpy(buf, java_home_var, buflen);
1825 
1826       // determine if this is a legacy image or modules image
1827       // modules image doesn't have "jre" subdirectory
1828       size_t len = strlen(buf);
1829       char* jrebin_p = buf + len;
1830       jio_snprintf(jrebin_p, buflen-len, "\\jre\\bin\\");
1831       if (0 != _access(buf, 0)) {
1832         jio_snprintf(jrebin_p, buflen-len, "\\bin\\");
1833       }
1834       len = strlen(buf);
1835       jio_snprintf(buf + len, buflen-len, "hotspot\\jvm.dll");
1836     }
1837   }
1838 
1839   if (buf[0] == '\0') {
1840     GetModuleFileName(vm_lib_handle, buf, buflen);
1841   }
1842   strncpy(saved_jvm_path, buf, MAX_PATH);
1843   saved_jvm_path[MAX_PATH - 1] = '\0';
1844 }
1845 
1846 
1847 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
1848 #ifndef _WIN64
1849   st->print("_");
1850 #endif
1851 }
1852 
1853 
1854 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
1855 #ifndef _WIN64
1856   st->print("@%d", args_size  * sizeof(int));
1857 #endif
1858 }
1859 
1860 // This method is a copy of JDK's sysGetLastErrorString
1861 // from src/windows/hpi/src/system_md.c
1862 
1863 size_t os::lasterror(char* buf, size_t len) {
1864   DWORD errval;
1865 
1866   if ((errval = GetLastError()) != 0) {
1867     // DOS error
1868     size_t n = (size_t)FormatMessage(
1869                                      FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
1870                                      NULL,
1871                                      errval,
1872                                      0,
1873                                      buf,
1874                                      (DWORD)len,
1875                                      NULL);
1876     if (n > 3) {
1877       // Drop final '.', CR, LF
1878       if (buf[n - 1] == '\n') n--;
1879       if (buf[n - 1] == '\r') n--;
1880       if (buf[n - 1] == '.') n--;
1881       buf[n] = '\0';
1882     }
1883     return n;
1884   }
1885 
1886   if (errno != 0) {
1887     // C runtime error that has no corresponding DOS error code
1888     const char* s = strerror(errno);
1889     size_t n = strlen(s);
1890     if (n >= len) n = len - 1;
1891     strncpy(buf, s, n);
1892     buf[n] = '\0';
1893     return n;
1894   }
1895 
1896   return 0;
1897 }
1898 
1899 int os::get_last_error() {
1900   DWORD error = GetLastError();
1901   if (error == 0) {
1902     error = errno;
1903   }
1904   return (int)error;
1905 }
1906 
1907 // sun.misc.Signal
1908 // NOTE that this is a workaround for an apparent kernel bug where if
1909 // a signal handler for SIGBREAK is installed then that signal handler
1910 // takes priority over the console control handler for CTRL_CLOSE_EVENT.
1911 // See bug 4416763.
1912 static void (*sigbreakHandler)(int) = NULL;
1913 
1914 static void UserHandler(int sig, void *siginfo, void *context) {
1915   os::signal_notify(sig);
1916   // We need to reinstate the signal handler each time...
1917   os::signal(sig, (void*)UserHandler);
1918 }
1919 
1920 void* os::user_handler() {
1921   return (void*) UserHandler;
1922 }
1923 
1924 void* os::signal(int signal_number, void* handler) {
1925   if ((signal_number == SIGBREAK) && (!ReduceSignalUsage)) {
1926     void (*oldHandler)(int) = sigbreakHandler;
1927     sigbreakHandler = (void (*)(int)) handler;
1928     return (void*) oldHandler;
1929   } else {
1930     return (void*)::signal(signal_number, (void (*)(int))handler);
1931   }
1932 }
1933 
1934 void os::signal_raise(int signal_number) {
1935   raise(signal_number);
1936 }
1937 
1938 // The Win32 C runtime library maps all console control events other than ^C
1939 // into SIGBREAK, which makes it impossible to distinguish ^BREAK from close,
1940 // logoff, and shutdown events.  We therefore install our own console handler
1941 // that raises SIGTERM for the latter cases.
1942 //
1943 static BOOL WINAPI consoleHandler(DWORD event) {
1944   switch (event) {
1945   case CTRL_C_EVENT:
1946     if (is_error_reported()) {
1947       // Ctrl-C is pressed during error reporting, likely because the error
1948       // handler fails to abort. Let VM die immediately.
1949       os::die();
1950     }
1951 
1952     os::signal_raise(SIGINT);
1953     return TRUE;
1954     break;
1955   case CTRL_BREAK_EVENT:
1956     if (sigbreakHandler != NULL) {
1957       (*sigbreakHandler)(SIGBREAK);
1958     }
1959     return TRUE;
1960     break;
1961   case CTRL_LOGOFF_EVENT: {
1962     // Don't terminate JVM if it is running in a non-interactive session,
1963     // such as a service process.
1964     USEROBJECTFLAGS flags;
1965     HANDLE handle = GetProcessWindowStation();
1966     if (handle != NULL &&
1967         GetUserObjectInformation(handle, UOI_FLAGS, &flags,
1968         sizeof(USEROBJECTFLAGS), NULL)) {
1969       // If it is a non-interactive session, let next handler to deal
1970       // with it.
1971       if ((flags.dwFlags & WSF_VISIBLE) == 0) {
1972         return FALSE;
1973       }
1974     }
1975   }
1976   case CTRL_CLOSE_EVENT:
1977   case CTRL_SHUTDOWN_EVENT:
1978     os::signal_raise(SIGTERM);
1979     return TRUE;
1980     break;
1981   default:
1982     break;
1983   }
1984   return FALSE;
1985 }
1986 
1987 // The following code is moved from os.cpp for making this
1988 // code platform specific, which it is by its very nature.
1989 
1990 // Return maximum OS signal used + 1 for internal use only
1991 // Used as exit signal for signal_thread
1992 int os::sigexitnum_pd() {
1993   return NSIG;
1994 }
1995 
1996 // a counter for each possible signal value, including signal_thread exit signal
1997 static volatile jint pending_signals[NSIG+1] = { 0 };
1998 static HANDLE sig_sem = NULL;
1999 
2000 void os::signal_init_pd() {
2001   // Initialize signal structures
2002   memset((void*)pending_signals, 0, sizeof(pending_signals));
2003 
2004   sig_sem = ::CreateSemaphore(NULL, 0, NSIG+1, NULL);
2005 
2006   // Programs embedding the VM do not want it to attempt to receive
2007   // events like CTRL_LOGOFF_EVENT, which are used to implement the
2008   // shutdown hooks mechanism introduced in 1.3.  For example, when
2009   // the VM is run as part of a Windows NT service (i.e., a servlet
2010   // engine in a web server), the correct behavior is for any console
2011   // control handler to return FALSE, not TRUE, because the OS's
2012   // "final" handler for such events allows the process to continue if
2013   // it is a service (while terminating it if it is not a service).
2014   // To make this behavior uniform and the mechanism simpler, we
2015   // completely disable the VM's usage of these console events if -Xrs
2016   // (=ReduceSignalUsage) is specified.  This means, for example, that
2017   // the CTRL-BREAK thread dump mechanism is also disabled in this
2018   // case.  See bugs 4323062, 4345157, and related bugs.
2019 
2020   if (!ReduceSignalUsage) {
2021     // Add a CTRL-C handler
2022     SetConsoleCtrlHandler(consoleHandler, TRUE);
2023   }
2024 }
2025 
2026 void os::signal_notify(int signal_number) {
2027   BOOL ret;
2028   if (sig_sem != NULL) {
2029     Atomic::inc(&pending_signals[signal_number]);
2030     ret = ::ReleaseSemaphore(sig_sem, 1, NULL);
2031     assert(ret != 0, "ReleaseSemaphore() failed");
2032   }
2033 }
2034 
2035 static int check_pending_signals(bool wait_for_signal) {
2036   DWORD ret;
2037   while (true) {
2038     for (int i = 0; i < NSIG + 1; i++) {
2039       jint n = pending_signals[i];
2040       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
2041         return i;
2042       }
2043     }
2044     if (!wait_for_signal) {
2045       return -1;
2046     }
2047 
2048     JavaThread *thread = JavaThread::current();
2049 
2050     ThreadBlockInVM tbivm(thread);
2051 
2052     bool threadIsSuspended;
2053     do {
2054       thread->set_suspend_equivalent();
2055       // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
2056       ret = ::WaitForSingleObject(sig_sem, INFINITE);
2057       assert(ret == WAIT_OBJECT_0, "WaitForSingleObject() failed");
2058 
2059       // were we externally suspended while we were waiting?
2060       threadIsSuspended = thread->handle_special_suspend_equivalent_condition();
2061       if (threadIsSuspended) {
2062         // The semaphore has been incremented, but while we were waiting
2063         // another thread suspended us. We don't want to continue running
2064         // while suspended because that would surprise the thread that
2065         // suspended us.
2066         ret = ::ReleaseSemaphore(sig_sem, 1, NULL);
2067         assert(ret != 0, "ReleaseSemaphore() failed");
2068 
2069         thread->java_suspend_self();
2070       }
2071     } while (threadIsSuspended);
2072   }
2073 }
2074 
2075 int os::signal_lookup() {
2076   return check_pending_signals(false);
2077 }
2078 
2079 int os::signal_wait() {
2080   return check_pending_signals(true);
2081 }
2082 
2083 // Implicit OS exception handling
2084 
2085 LONG Handle_Exception(struct _EXCEPTION_POINTERS* exceptionInfo,
2086                       address handler) {
2087   JavaThread* thread = JavaThread::current();
2088   // Save pc in thread
2089 #ifdef _M_IA64
2090   // Do not blow up if no thread info available.
2091   if (thread) {
2092     // Saving PRECISE pc (with slot information) in thread.
2093     uint64_t precise_pc = (uint64_t) exceptionInfo->ExceptionRecord->ExceptionAddress;
2094     // Convert precise PC into "Unix" format
2095     precise_pc = (precise_pc & 0xFFFFFFFFFFFFFFF0) | ((precise_pc & 0xF) >> 2);
2096     thread->set_saved_exception_pc((address)precise_pc);
2097   }
2098   // Set pc to handler
2099   exceptionInfo->ContextRecord->StIIP = (DWORD64)handler;
2100   // Clear out psr.ri (= Restart Instruction) in order to continue
2101   // at the beginning of the target bundle.
2102   exceptionInfo->ContextRecord->StIPSR &= 0xFFFFF9FFFFFFFFFF;
2103   assert(((DWORD64)handler & 0xF) == 0, "Target address must point to the beginning of a bundle!");
2104 #elif _M_AMD64
2105   // Do not blow up if no thread info available.
2106   if (thread) {
2107     thread->set_saved_exception_pc((address)(DWORD_PTR)exceptionInfo->ContextRecord->Rip);
2108   }
2109   // Set pc to handler
2110   exceptionInfo->ContextRecord->Rip = (DWORD64)handler;
2111 #else
2112   // Do not blow up if no thread info available.
2113   if (thread) {
2114     thread->set_saved_exception_pc((address)(DWORD_PTR)exceptionInfo->ContextRecord->Eip);
2115   }
2116   // Set pc to handler
2117   exceptionInfo->ContextRecord->Eip = (DWORD)(DWORD_PTR)handler;
2118 #endif
2119 
2120   // Continue the execution
2121   return EXCEPTION_CONTINUE_EXECUTION;
2122 }
2123 
2124 
2125 // Used for PostMortemDump
2126 extern "C" void safepoints();
2127 extern "C" void find(int x);
2128 extern "C" void events();
2129 
2130 // According to Windows API documentation, an illegal instruction sequence should generate
2131 // the 0xC000001C exception code. However, real world experience shows that occasionnaly
2132 // the execution of an illegal instruction can generate the exception code 0xC000001E. This
2133 // seems to be an undocumented feature of Win NT 4.0 (and probably other Windows systems).
2134 
2135 #define EXCEPTION_ILLEGAL_INSTRUCTION_2 0xC000001E
2136 
2137 // From "Execution Protection in the Windows Operating System" draft 0.35
2138 // Once a system header becomes available, the "real" define should be
2139 // included or copied here.
2140 #define EXCEPTION_INFO_EXEC_VIOLATION 0x08
2141 
2142 // Handle NAT Bit consumption on IA64.
2143 #ifdef _M_IA64
2144   #define EXCEPTION_REG_NAT_CONSUMPTION    STATUS_REG_NAT_CONSUMPTION
2145 #endif
2146 
2147 // Windows Vista/2008 heap corruption check
2148 #define EXCEPTION_HEAP_CORRUPTION        0xC0000374
2149 
2150 #define def_excpt(val) #val, val
2151 
2152 struct siglabel {
2153   char *name;
2154   int   number;
2155 };
2156 
2157 // All Visual C++ exceptions thrown from code generated by the Microsoft Visual
2158 // C++ compiler contain this error code. Because this is a compiler-generated
2159 // error, the code is not listed in the Win32 API header files.
2160 // The code is actually a cryptic mnemonic device, with the initial "E"
2161 // standing for "exception" and the final 3 bytes (0x6D7363) representing the
2162 // ASCII values of "msc".
2163 
2164 #define EXCEPTION_UNCAUGHT_CXX_EXCEPTION    0xE06D7363
2165 
2166 
2167 struct siglabel exceptlabels[] = {
2168     def_excpt(EXCEPTION_ACCESS_VIOLATION),
2169     def_excpt(EXCEPTION_DATATYPE_MISALIGNMENT),
2170     def_excpt(EXCEPTION_BREAKPOINT),
2171     def_excpt(EXCEPTION_SINGLE_STEP),
2172     def_excpt(EXCEPTION_ARRAY_BOUNDS_EXCEEDED),
2173     def_excpt(EXCEPTION_FLT_DENORMAL_OPERAND),
2174     def_excpt(EXCEPTION_FLT_DIVIDE_BY_ZERO),
2175     def_excpt(EXCEPTION_FLT_INEXACT_RESULT),
2176     def_excpt(EXCEPTION_FLT_INVALID_OPERATION),
2177     def_excpt(EXCEPTION_FLT_OVERFLOW),
2178     def_excpt(EXCEPTION_FLT_STACK_CHECK),
2179     def_excpt(EXCEPTION_FLT_UNDERFLOW),
2180     def_excpt(EXCEPTION_INT_DIVIDE_BY_ZERO),
2181     def_excpt(EXCEPTION_INT_OVERFLOW),
2182     def_excpt(EXCEPTION_PRIV_INSTRUCTION),
2183     def_excpt(EXCEPTION_IN_PAGE_ERROR),
2184     def_excpt(EXCEPTION_ILLEGAL_INSTRUCTION),
2185     def_excpt(EXCEPTION_ILLEGAL_INSTRUCTION_2),
2186     def_excpt(EXCEPTION_NONCONTINUABLE_EXCEPTION),
2187     def_excpt(EXCEPTION_STACK_OVERFLOW),
2188     def_excpt(EXCEPTION_INVALID_DISPOSITION),
2189     def_excpt(EXCEPTION_GUARD_PAGE),
2190     def_excpt(EXCEPTION_INVALID_HANDLE),
2191     def_excpt(EXCEPTION_UNCAUGHT_CXX_EXCEPTION),
2192     def_excpt(EXCEPTION_HEAP_CORRUPTION),
2193 #ifdef _M_IA64
2194     def_excpt(EXCEPTION_REG_NAT_CONSUMPTION),
2195 #endif
2196     NULL, 0
2197 };
2198 
2199 const char* os::exception_name(int exception_code, char *buf, size_t size) {
2200   for (int i = 0; exceptlabels[i].name != NULL; i++) {
2201     if (exceptlabels[i].number == exception_code) {
2202       jio_snprintf(buf, size, "%s", exceptlabels[i].name);
2203       return buf;
2204     }
2205   }
2206 
2207   return NULL;
2208 }
2209 
2210 //-----------------------------------------------------------------------------
2211 LONG Handle_IDiv_Exception(struct _EXCEPTION_POINTERS* exceptionInfo) {
2212   // handle exception caused by idiv; should only happen for -MinInt/-1
2213   // (division by zero is handled explicitly)
2214 #ifdef _M_IA64
2215   assert(0, "Fix Handle_IDiv_Exception");
2216 #elif _M_AMD64
2217   PCONTEXT ctx = exceptionInfo->ContextRecord;
2218   address pc = (address)ctx->Rip;
2219   assert(pc[0] == 0xF7, "not an idiv opcode");
2220   assert((pc[1] & ~0x7) == 0xF8, "cannot handle non-register operands");
2221   assert(ctx->Rax == min_jint, "unexpected idiv exception");
2222   // set correct result values and continue after idiv instruction
2223   ctx->Rip = (DWORD)pc + 2;        // idiv reg, reg  is 2 bytes
2224   ctx->Rax = (DWORD)min_jint;      // result
2225   ctx->Rdx = (DWORD)0;             // remainder
2226   // Continue the execution
2227 #else
2228   PCONTEXT ctx = exceptionInfo->ContextRecord;
2229   address pc = (address)ctx->Eip;
2230   assert(pc[0] == 0xF7, "not an idiv opcode");
2231   assert((pc[1] & ~0x7) == 0xF8, "cannot handle non-register operands");
2232   assert(ctx->Eax == min_jint, "unexpected idiv exception");
2233   // set correct result values and continue after idiv instruction
2234   ctx->Eip = (DWORD)pc + 2;        // idiv reg, reg  is 2 bytes
2235   ctx->Eax = (DWORD)min_jint;      // result
2236   ctx->Edx = (DWORD)0;             // remainder
2237   // Continue the execution
2238 #endif
2239   return EXCEPTION_CONTINUE_EXECUTION;
2240 }
2241 
2242 //-----------------------------------------------------------------------------
2243 LONG WINAPI Handle_FLT_Exception(struct _EXCEPTION_POINTERS* exceptionInfo) {
2244   PCONTEXT ctx = exceptionInfo->ContextRecord;
2245 #ifndef  _WIN64
2246   // handle exception caused by native method modifying control word
2247   DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
2248 
2249   switch (exception_code) {
2250   case EXCEPTION_FLT_DENORMAL_OPERAND:
2251   case EXCEPTION_FLT_DIVIDE_BY_ZERO:
2252   case EXCEPTION_FLT_INEXACT_RESULT:
2253   case EXCEPTION_FLT_INVALID_OPERATION:
2254   case EXCEPTION_FLT_OVERFLOW:
2255   case EXCEPTION_FLT_STACK_CHECK:
2256   case EXCEPTION_FLT_UNDERFLOW:
2257     jint fp_control_word = (* (jint*) StubRoutines::addr_fpu_cntrl_wrd_std());
2258     if (fp_control_word != ctx->FloatSave.ControlWord) {
2259       // Restore FPCW and mask out FLT exceptions
2260       ctx->FloatSave.ControlWord = fp_control_word | 0xffffffc0;
2261       // Mask out pending FLT exceptions
2262       ctx->FloatSave.StatusWord &=  0xffffff00;
2263       return EXCEPTION_CONTINUE_EXECUTION;
2264     }
2265   }
2266 
2267   if (prev_uef_handler != NULL) {
2268     // We didn't handle this exception so pass it to the previous
2269     // UnhandledExceptionFilter.
2270     return (prev_uef_handler)(exceptionInfo);
2271   }
2272 #else // !_WIN64
2273   // On Windows, the mxcsr control bits are non-volatile across calls
2274   // See also CR 6192333
2275   //
2276   jint MxCsr = INITIAL_MXCSR;
2277   // we can't use StubRoutines::addr_mxcsr_std()
2278   // because in Win64 mxcsr is not saved there
2279   if (MxCsr != ctx->MxCsr) {
2280     ctx->MxCsr = MxCsr;
2281     return EXCEPTION_CONTINUE_EXECUTION;
2282   }
2283 #endif // !_WIN64
2284 
2285   return EXCEPTION_CONTINUE_SEARCH;
2286 }
2287 
2288 static inline void report_error(Thread* t, DWORD exception_code,
2289                                 address addr, void* siginfo, void* context) {
2290   VMError err(t, exception_code, addr, siginfo, context);
2291   err.report_and_die();
2292 
2293   // If UseOsErrorReporting, this will return here and save the error file
2294   // somewhere where we can find it in the minidump.
2295 }
2296 
2297 //-----------------------------------------------------------------------------
2298 LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
2299   if (InterceptOSException) return EXCEPTION_CONTINUE_SEARCH;
2300   DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
2301 #ifdef _M_IA64
2302   // On Itanium, we need the "precise pc", which has the slot number coded
2303   // into the least 4 bits: 0000=slot0, 0100=slot1, 1000=slot2 (Windows format).
2304   address pc = (address) exceptionInfo->ExceptionRecord->ExceptionAddress;
2305   // Convert the pc to "Unix format", which has the slot number coded
2306   // into the least 2 bits: 0000=slot0, 0001=slot1, 0010=slot2
2307   // This is needed for IA64 because "relocation" / "implicit null check" / "poll instruction"
2308   // information is saved in the Unix format.
2309   address pc_unix_format = (address) ((((uint64_t)pc) & 0xFFFFFFFFFFFFFFF0) | ((((uint64_t)pc) & 0xF) >> 2));
2310 #elif _M_AMD64
2311   address pc = (address) exceptionInfo->ContextRecord->Rip;
2312 #else
2313   address pc = (address) exceptionInfo->ContextRecord->Eip;
2314 #endif
2315   Thread* t = ThreadLocalStorage::get_thread_slow();          // slow & steady
2316 
2317   // Handle SafeFetch32 and SafeFetchN exceptions.
2318   if (StubRoutines::is_safefetch_fault(pc)) {
2319     return Handle_Exception(exceptionInfo, StubRoutines::continuation_for_safefetch_fault(pc));
2320   }
2321 
2322 #ifndef _WIN64
2323   // Execution protection violation - win32 running on AMD64 only
2324   // Handled first to avoid misdiagnosis as a "normal" access violation;
2325   // This is safe to do because we have a new/unique ExceptionInformation
2326   // code for this condition.
2327   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2328     PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2329     int exception_subcode = (int) exceptionRecord->ExceptionInformation[0];
2330     address addr = (address) exceptionRecord->ExceptionInformation[1];
2331 
2332     if (exception_subcode == EXCEPTION_INFO_EXEC_VIOLATION) {
2333       int page_size = os::vm_page_size();
2334 
2335       // Make sure the pc and the faulting address are sane.
2336       //
2337       // If an instruction spans a page boundary, and the page containing
2338       // the beginning of the instruction is executable but the following
2339       // page is not, the pc and the faulting address might be slightly
2340       // different - we still want to unguard the 2nd page in this case.
2341       //
2342       // 15 bytes seems to be a (very) safe value for max instruction size.
2343       bool pc_is_near_addr =
2344         (pointer_delta((void*) addr, (void*) pc, sizeof(char)) < 15);
2345       bool instr_spans_page_boundary =
2346         (align_size_down((intptr_t) pc ^ (intptr_t) addr,
2347                          (intptr_t) page_size) > 0);
2348 
2349       if (pc == addr || (pc_is_near_addr && instr_spans_page_boundary)) {
2350         static volatile address last_addr =
2351           (address) os::non_memory_address_word();
2352 
2353         // In conservative mode, don't unguard unless the address is in the VM
2354         if (UnguardOnExecutionViolation > 0 && addr != last_addr &&
2355             (UnguardOnExecutionViolation > 1 || os::address_is_in_vm(addr))) {
2356 
2357           // Set memory to RWX and retry
2358           address page_start =
2359             (address) align_size_down((intptr_t) addr, (intptr_t) page_size);
2360           bool res = os::protect_memory((char*) page_start, page_size,
2361                                         os::MEM_PROT_RWX);
2362 
2363           if (PrintMiscellaneous && Verbose) {
2364             char buf[256];
2365             jio_snprintf(buf, sizeof(buf), "Execution protection violation "
2366                          "at " INTPTR_FORMAT
2367                          ", unguarding " INTPTR_FORMAT ": %s", addr,
2368                          page_start, (res ? "success" : strerror(errno)));
2369             tty->print_raw_cr(buf);
2370           }
2371 
2372           // Set last_addr so if we fault again at the same address, we don't
2373           // end up in an endless loop.
2374           //
2375           // There are two potential complications here.  Two threads trapping
2376           // at the same address at the same time could cause one of the
2377           // threads to think it already unguarded, and abort the VM.  Likely
2378           // very rare.
2379           //
2380           // The other race involves two threads alternately trapping at
2381           // different addresses and failing to unguard the page, resulting in
2382           // an endless loop.  This condition is probably even more unlikely
2383           // than the first.
2384           //
2385           // Although both cases could be avoided by using locks or thread
2386           // local last_addr, these solutions are unnecessary complication:
2387           // this handler is a best-effort safety net, not a complete solution.
2388           // It is disabled by default and should only be used as a workaround
2389           // in case we missed any no-execute-unsafe VM code.
2390 
2391           last_addr = addr;
2392 
2393           return EXCEPTION_CONTINUE_EXECUTION;
2394         }
2395       }
2396 
2397       // Last unguard failed or not unguarding
2398       tty->print_raw_cr("Execution protection violation");
2399       report_error(t, exception_code, addr, exceptionInfo->ExceptionRecord,
2400                    exceptionInfo->ContextRecord);
2401       return EXCEPTION_CONTINUE_SEARCH;
2402     }
2403   }
2404 #endif // _WIN64
2405 
2406   // Check to see if we caught the safepoint code in the
2407   // process of write protecting the memory serialization page.
2408   // It write enables the page immediately after protecting it
2409   // so just return.
2410   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2411     JavaThread* thread = (JavaThread*) t;
2412     PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2413     address addr = (address) exceptionRecord->ExceptionInformation[1];
2414     if (os::is_memory_serialize_page(thread, addr)) {
2415       // Block current thread until the memory serialize page permission restored.
2416       os::block_on_serialize_page_trap();
2417       return EXCEPTION_CONTINUE_EXECUTION;
2418     }
2419   }
2420 
2421   if ((exception_code == EXCEPTION_ACCESS_VIOLATION) &&
2422       VM_Version::is_cpuinfo_segv_addr(pc)) {
2423     // Verify that OS save/restore AVX registers.
2424     return Handle_Exception(exceptionInfo, VM_Version::cpuinfo_cont_addr());
2425   }
2426 
2427   if (t != NULL && t->is_Java_thread()) {
2428     JavaThread* thread = (JavaThread*) t;
2429     bool in_java = thread->thread_state() == _thread_in_Java;
2430 
2431     // Handle potential stack overflows up front.
2432     if (exception_code == EXCEPTION_STACK_OVERFLOW) {
2433       if (os::uses_stack_guard_pages()) {
2434 #ifdef _M_IA64
2435         // Use guard page for register stack.
2436         PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2437         address addr = (address) exceptionRecord->ExceptionInformation[1];
2438         // Check for a register stack overflow on Itanium
2439         if (thread->addr_inside_register_stack_red_zone(addr)) {
2440           // Fatal red zone violation happens if the Java program
2441           // catches a StackOverflow error and does so much processing
2442           // that it runs beyond the unprotected yellow guard zone. As
2443           // a result, we are out of here.
2444           fatal("ERROR: Unrecoverable stack overflow happened. JVM will exit.");
2445         } else if(thread->addr_inside_register_stack(addr)) {
2446           // Disable the yellow zone which sets the state that
2447           // we've got a stack overflow problem.
2448           if (thread->stack_yellow_zone_enabled()) {
2449             thread->disable_stack_yellow_zone();
2450           }
2451           // Give us some room to process the exception.
2452           thread->disable_register_stack_guard();
2453           // Tracing with +Verbose.
2454           if (Verbose) {
2455             tty->print_cr("SOF Compiled Register Stack overflow at " INTPTR_FORMAT " (SIGSEGV)", pc);
2456             tty->print_cr("Register Stack access at " INTPTR_FORMAT, addr);
2457             tty->print_cr("Register Stack base " INTPTR_FORMAT, thread->register_stack_base());
2458             tty->print_cr("Register Stack [" INTPTR_FORMAT "," INTPTR_FORMAT "]",
2459                           thread->register_stack_base(),
2460                           thread->register_stack_base() + thread->stack_size());
2461           }
2462 
2463           // Reguard the permanent register stack red zone just to be sure.
2464           // We saw Windows silently disabling this without telling us.
2465           thread->enable_register_stack_red_zone();
2466 
2467           return Handle_Exception(exceptionInfo,
2468                                   SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
2469         }
2470 #endif
2471         if (thread->stack_yellow_zone_enabled()) {
2472           // Yellow zone violation.  The o/s has unprotected the first yellow
2473           // zone page for us.  Note:  must call disable_stack_yellow_zone to
2474           // update the enabled status, even if the zone contains only one page.
2475           thread->disable_stack_yellow_zone();
2476           // If not in java code, return and hope for the best.
2477           return in_java
2478               ? Handle_Exception(exceptionInfo, SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW))
2479               :  EXCEPTION_CONTINUE_EXECUTION;
2480         } else {
2481           // Fatal red zone violation.
2482           thread->disable_stack_red_zone();
2483           tty->print_raw_cr("An unrecoverable stack overflow has occurred.");
2484           report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2485                        exceptionInfo->ContextRecord);
2486           return EXCEPTION_CONTINUE_SEARCH;
2487         }
2488       } else if (in_java) {
2489         // JVM-managed guard pages cannot be used on win95/98.  The o/s provides
2490         // a one-time-only guard page, which it has released to us.  The next
2491         // stack overflow on this thread will result in an ACCESS_VIOLATION.
2492         return Handle_Exception(exceptionInfo,
2493                                 SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
2494       } else {
2495         // Can only return and hope for the best.  Further stack growth will
2496         // result in an ACCESS_VIOLATION.
2497         return EXCEPTION_CONTINUE_EXECUTION;
2498       }
2499     } else if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2500       // Either stack overflow or null pointer exception.
2501       if (in_java) {
2502         PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2503         address addr = (address) exceptionRecord->ExceptionInformation[1];
2504         address stack_end = thread->stack_base() - thread->stack_size();
2505         if (addr < stack_end && addr >= stack_end - os::vm_page_size()) {
2506           // Stack overflow.
2507           assert(!os::uses_stack_guard_pages(),
2508                  "should be caught by red zone code above.");
2509           return Handle_Exception(exceptionInfo,
2510                                   SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW));
2511         }
2512         // Check for safepoint polling and implicit null
2513         // We only expect null pointers in the stubs (vtable)
2514         // the rest are checked explicitly now.
2515         CodeBlob* cb = CodeCache::find_blob(pc);
2516         if (cb != NULL) {
2517           if (os::is_poll_address(addr)) {
2518             address stub = SharedRuntime::get_poll_stub(pc);
2519             return Handle_Exception(exceptionInfo, stub);
2520           }
2521         }
2522         {
2523 #ifdef _WIN64
2524           // If it's a legal stack address map the entire region in
2525           //
2526           PEXCEPTION_RECORD exceptionRecord = exceptionInfo->ExceptionRecord;
2527           address addr = (address) exceptionRecord->ExceptionInformation[1];
2528           if (addr > thread->stack_yellow_zone_base() && addr < thread->stack_base()) {
2529             addr = (address)((uintptr_t)addr &
2530                              (~((uintptr_t)os::vm_page_size() - (uintptr_t)1)));
2531             os::commit_memory((char *)addr, thread->stack_base() - addr,
2532                               !ExecMem);
2533             return EXCEPTION_CONTINUE_EXECUTION;
2534           } else
2535 #endif
2536           {
2537             // Null pointer exception.
2538 #ifdef _M_IA64
2539             // Process implicit null checks in compiled code. Note: Implicit null checks
2540             // can happen even if "ImplicitNullChecks" is disabled, e.g. in vtable stubs.
2541             if (CodeCache::contains((void*) pc_unix_format) && !MacroAssembler::needs_explicit_null_check((intptr_t) addr)) {
2542               CodeBlob *cb = CodeCache::find_blob_unsafe(pc_unix_format);
2543               // Handle implicit null check in UEP method entry
2544               if (cb && (cb->is_frame_complete_at(pc) ||
2545                          (cb->is_nmethod() && ((nmethod *)cb)->inlinecache_check_contains(pc)))) {
2546                 if (Verbose) {
2547                   intptr_t *bundle_start = (intptr_t*) ((intptr_t) pc_unix_format & 0xFFFFFFFFFFFFFFF0);
2548                   tty->print_cr("trap: null_check at " INTPTR_FORMAT " (SIGSEGV)", pc_unix_format);
2549                   tty->print_cr("      to addr " INTPTR_FORMAT, addr);
2550                   tty->print_cr("      bundle is " INTPTR_FORMAT " (high), " INTPTR_FORMAT " (low)",
2551                                 *(bundle_start + 1), *bundle_start);
2552                 }
2553                 return Handle_Exception(exceptionInfo,
2554                                         SharedRuntime::continuation_for_implicit_exception(thread, pc_unix_format, SharedRuntime::IMPLICIT_NULL));
2555               }
2556             }
2557 
2558             // Implicit null checks were processed above.  Hence, we should not reach
2559             // here in the usual case => die!
2560             if (Verbose) tty->print_raw_cr("Access violation, possible null pointer exception");
2561             report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2562                          exceptionInfo->ContextRecord);
2563             return EXCEPTION_CONTINUE_SEARCH;
2564 
2565 #else // !IA64
2566 
2567             // Windows 98 reports faulting addresses incorrectly
2568             if (!MacroAssembler::needs_explicit_null_check((intptr_t)addr) ||
2569                 !os::win32::is_nt()) {
2570               address stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL);
2571               if (stub != NULL) return Handle_Exception(exceptionInfo, stub);
2572             }
2573             report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2574                          exceptionInfo->ContextRecord);
2575             return EXCEPTION_CONTINUE_SEARCH;
2576 #endif
2577           }
2578         }
2579       }
2580 
2581 #ifdef _WIN64
2582       // Special care for fast JNI field accessors.
2583       // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks
2584       // in and the heap gets shrunk before the field access.
2585       if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2586         address addr = JNI_FastGetField::find_slowcase_pc(pc);
2587         if (addr != (address)-1) {
2588           return Handle_Exception(exceptionInfo, addr);
2589         }
2590       }
2591 #endif
2592 
2593       // Stack overflow or null pointer exception in native code.
2594       report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2595                    exceptionInfo->ContextRecord);
2596       return EXCEPTION_CONTINUE_SEARCH;
2597     } // /EXCEPTION_ACCESS_VIOLATION
2598     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2599 #if defined _M_IA64
2600     else if ((exception_code == EXCEPTION_ILLEGAL_INSTRUCTION ||
2601               exception_code == EXCEPTION_ILLEGAL_INSTRUCTION_2)) {
2602       M37 handle_wrong_method_break(0, NativeJump::HANDLE_WRONG_METHOD, PR0);
2603 
2604       // Compiled method patched to be non entrant? Following conditions must apply:
2605       // 1. must be first instruction in bundle
2606       // 2. must be a break instruction with appropriate code
2607       if ((((uint64_t) pc & 0x0F) == 0) &&
2608           (((IPF_Bundle*) pc)->get_slot0() == handle_wrong_method_break.bits())) {
2609         return Handle_Exception(exceptionInfo,
2610                                 (address)SharedRuntime::get_handle_wrong_method_stub());
2611       }
2612     } // /EXCEPTION_ILLEGAL_INSTRUCTION
2613 #endif
2614 
2615 
2616     if (in_java) {
2617       switch (exception_code) {
2618       case EXCEPTION_INT_DIVIDE_BY_ZERO:
2619         return Handle_Exception(exceptionInfo, SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_DIVIDE_BY_ZERO));
2620 
2621       case EXCEPTION_INT_OVERFLOW:
2622         return Handle_IDiv_Exception(exceptionInfo);
2623 
2624       } // switch
2625     }
2626     if (((thread->thread_state() == _thread_in_Java) ||
2627          (thread->thread_state() == _thread_in_native)) &&
2628          exception_code != EXCEPTION_UNCAUGHT_CXX_EXCEPTION) {
2629       LONG result=Handle_FLT_Exception(exceptionInfo);
2630       if (result==EXCEPTION_CONTINUE_EXECUTION) return result;
2631     }
2632   }
2633 
2634   if (exception_code != EXCEPTION_BREAKPOINT) {
2635     report_error(t, exception_code, pc, exceptionInfo->ExceptionRecord,
2636                  exceptionInfo->ContextRecord);
2637   }
2638   return EXCEPTION_CONTINUE_SEARCH;
2639 }
2640 
2641 #ifndef _WIN64
2642 // Special care for fast JNI accessors.
2643 // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in and
2644 // the heap gets shrunk before the field access.
2645 // Need to install our own structured exception handler since native code may
2646 // install its own.
2647 LONG WINAPI fastJNIAccessorExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo) {
2648   DWORD exception_code = exceptionInfo->ExceptionRecord->ExceptionCode;
2649   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
2650     address pc = (address) exceptionInfo->ContextRecord->Eip;
2651     address addr = JNI_FastGetField::find_slowcase_pc(pc);
2652     if (addr != (address)-1) {
2653       return Handle_Exception(exceptionInfo, addr);
2654     }
2655   }
2656   return EXCEPTION_CONTINUE_SEARCH;
2657 }
2658 
2659 #define DEFINE_FAST_GETFIELD(Return, Fieldname, Result)                     \
2660   Return JNICALL jni_fast_Get##Result##Field_wrapper(JNIEnv *env,           \
2661                                                      jobject obj,           \
2662                                                      jfieldID fieldID) {    \
2663     __try {                                                                 \
2664       return (*JNI_FastGetField::jni_fast_Get##Result##Field_fp)(env,       \
2665                                                                  obj,       \
2666                                                                  fieldID);  \
2667     } __except(fastJNIAccessorExceptionFilter((_EXCEPTION_POINTERS*)        \
2668                                               _exception_info())) {         \
2669     }                                                                       \
2670     return 0;                                                               \
2671   }
2672 
2673 DEFINE_FAST_GETFIELD(jboolean, bool,   Boolean)
2674 DEFINE_FAST_GETFIELD(jbyte,    byte,   Byte)
2675 DEFINE_FAST_GETFIELD(jchar,    char,   Char)
2676 DEFINE_FAST_GETFIELD(jshort,   short,  Short)
2677 DEFINE_FAST_GETFIELD(jint,     int,    Int)
2678 DEFINE_FAST_GETFIELD(jlong,    long,   Long)
2679 DEFINE_FAST_GETFIELD(jfloat,   float,  Float)
2680 DEFINE_FAST_GETFIELD(jdouble,  double, Double)
2681 
2682 address os::win32::fast_jni_accessor_wrapper(BasicType type) {
2683   switch (type) {
2684   case T_BOOLEAN: return (address)jni_fast_GetBooleanField_wrapper;
2685   case T_BYTE:    return (address)jni_fast_GetByteField_wrapper;
2686   case T_CHAR:    return (address)jni_fast_GetCharField_wrapper;
2687   case T_SHORT:   return (address)jni_fast_GetShortField_wrapper;
2688   case T_INT:     return (address)jni_fast_GetIntField_wrapper;
2689   case T_LONG:    return (address)jni_fast_GetLongField_wrapper;
2690   case T_FLOAT:   return (address)jni_fast_GetFloatField_wrapper;
2691   case T_DOUBLE:  return (address)jni_fast_GetDoubleField_wrapper;
2692   default:        ShouldNotReachHere();
2693   }
2694   return (address)-1;
2695 }
2696 #endif
2697 
2698 void os::win32::call_test_func_with_wrapper(void (*funcPtr)(void)) {
2699   // Install a win32 structured exception handler around the test
2700   // function call so the VM can generate an error dump if needed.
2701   __try {
2702     (*funcPtr)();
2703   } __except(topLevelExceptionFilter(
2704                                      (_EXCEPTION_POINTERS*)_exception_info())) {
2705     // Nothing to do.
2706   }
2707 }
2708 
2709 // Virtual Memory
2710 
2711 int os::vm_page_size() { return os::win32::vm_page_size(); }
2712 int os::vm_allocation_granularity() {
2713   return os::win32::vm_allocation_granularity();
2714 }
2715 
2716 // Windows large page support is available on Windows 2003. In order to use
2717 // large page memory, the administrator must first assign additional privilege
2718 // to the user:
2719 //   + select Control Panel -> Administrative Tools -> Local Security Policy
2720 //   + select Local Policies -> User Rights Assignment
2721 //   + double click "Lock pages in memory", add users and/or groups
2722 //   + reboot
2723 // Note the above steps are needed for administrator as well, as administrators
2724 // by default do not have the privilege to lock pages in memory.
2725 //
2726 // Note about Windows 2003: although the API supports committing large page
2727 // memory on a page-by-page basis and VirtualAlloc() returns success under this
2728 // scenario, I found through experiment it only uses large page if the entire
2729 // memory region is reserved and committed in a single VirtualAlloc() call.
2730 // This makes Windows large page support more or less like Solaris ISM, in
2731 // that the entire heap must be committed upfront. This probably will change
2732 // in the future, if so the code below needs to be revisited.
2733 
2734 #ifndef MEM_LARGE_PAGES
2735   #define MEM_LARGE_PAGES 0x20000000
2736 #endif
2737 
2738 static HANDLE    _hProcess;
2739 static HANDLE    _hToken;
2740 
2741 // Container for NUMA node list info
2742 class NUMANodeListHolder {
2743  private:
2744   int *_numa_used_node_list;  // allocated below
2745   int _numa_used_node_count;
2746 
2747   void free_node_list() {
2748     if (_numa_used_node_list != NULL) {
2749       FREE_C_HEAP_ARRAY(int, _numa_used_node_list);
2750     }
2751   }
2752 
2753  public:
2754   NUMANodeListHolder() {
2755     _numa_used_node_count = 0;
2756     _numa_used_node_list = NULL;
2757     // do rest of initialization in build routine (after function pointers are set up)
2758   }
2759 
2760   ~NUMANodeListHolder() {
2761     free_node_list();
2762   }
2763 
2764   bool build() {
2765     DWORD_PTR proc_aff_mask;
2766     DWORD_PTR sys_aff_mask;
2767     if (!GetProcessAffinityMask(GetCurrentProcess(), &proc_aff_mask, &sys_aff_mask)) return false;
2768     ULONG highest_node_number;
2769     if (!os::Kernel32Dll::GetNumaHighestNodeNumber(&highest_node_number)) return false;
2770     free_node_list();
2771     _numa_used_node_list = NEW_C_HEAP_ARRAY(int, highest_node_number + 1, mtInternal);
2772     for (unsigned int i = 0; i <= highest_node_number; i++) {
2773       ULONGLONG proc_mask_numa_node;
2774       if (!os::Kernel32Dll::GetNumaNodeProcessorMask(i, &proc_mask_numa_node)) return false;
2775       if ((proc_aff_mask & proc_mask_numa_node)!=0) {
2776         _numa_used_node_list[_numa_used_node_count++] = i;
2777       }
2778     }
2779     return (_numa_used_node_count > 1);
2780   }
2781 
2782   int get_count() { return _numa_used_node_count; }
2783   int get_node_list_entry(int n) {
2784     // for indexes out of range, returns -1
2785     return (n < _numa_used_node_count ? _numa_used_node_list[n] : -1);
2786   }
2787 
2788 } numa_node_list_holder;
2789 
2790 
2791 
2792 static size_t _large_page_size = 0;
2793 
2794 static bool resolve_functions_for_large_page_init() {
2795   return os::Kernel32Dll::GetLargePageMinimumAvailable() &&
2796     os::Advapi32Dll::AdvapiAvailable();
2797 }
2798 
2799 static bool request_lock_memory_privilege() {
2800   _hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
2801                           os::current_process_id());
2802 
2803   LUID luid;
2804   if (_hProcess != NULL &&
2805       os::Advapi32Dll::OpenProcessToken(_hProcess, TOKEN_ADJUST_PRIVILEGES, &_hToken) &&
2806       os::Advapi32Dll::LookupPrivilegeValue(NULL, "SeLockMemoryPrivilege", &luid)) {
2807 
2808     TOKEN_PRIVILEGES tp;
2809     tp.PrivilegeCount = 1;
2810     tp.Privileges[0].Luid = luid;
2811     tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
2812 
2813     // AdjustTokenPrivileges() may return TRUE even when it couldn't change the
2814     // privilege. Check GetLastError() too. See MSDN document.
2815     if (os::Advapi32Dll::AdjustTokenPrivileges(_hToken, false, &tp, sizeof(tp), NULL, NULL) &&
2816         (GetLastError() == ERROR_SUCCESS)) {
2817       return true;
2818     }
2819   }
2820 
2821   return false;
2822 }
2823 
2824 static void cleanup_after_large_page_init() {
2825   if (_hProcess) CloseHandle(_hProcess);
2826   _hProcess = NULL;
2827   if (_hToken) CloseHandle(_hToken);
2828   _hToken = NULL;
2829 }
2830 
2831 static bool numa_interleaving_init() {
2832   bool success = false;
2833   bool use_numa_interleaving_specified = !FLAG_IS_DEFAULT(UseNUMAInterleaving);
2834 
2835   // print a warning if UseNUMAInterleaving flag is specified on command line
2836   bool warn_on_failure = use_numa_interleaving_specified;
2837 #define WARN(msg) if (warn_on_failure) { warning(msg); }
2838 
2839   // NUMAInterleaveGranularity cannot be less than vm_allocation_granularity (or _large_page_size if using large pages)
2840   size_t min_interleave_granularity = UseLargePages ? _large_page_size : os::vm_allocation_granularity();
2841   NUMAInterleaveGranularity = align_size_up(NUMAInterleaveGranularity, min_interleave_granularity);
2842 
2843   if (os::Kernel32Dll::NumaCallsAvailable()) {
2844     if (numa_node_list_holder.build()) {
2845       if (PrintMiscellaneous && Verbose) {
2846         tty->print("NUMA UsedNodeCount=%d, namely ", numa_node_list_holder.get_count());
2847         for (int i = 0; i < numa_node_list_holder.get_count(); i++) {
2848           tty->print("%d ", numa_node_list_holder.get_node_list_entry(i));
2849         }
2850         tty->print("\n");
2851       }
2852       success = true;
2853     } else {
2854       WARN("Process does not cover multiple NUMA nodes.");
2855     }
2856   } else {
2857     WARN("NUMA Interleaving is not supported by the operating system.");
2858   }
2859   if (!success) {
2860     if (use_numa_interleaving_specified) WARN("...Ignoring UseNUMAInterleaving flag.");
2861   }
2862   return success;
2863 #undef WARN
2864 }
2865 
2866 // this routine is used whenever we need to reserve a contiguous VA range
2867 // but we need to make separate VirtualAlloc calls for each piece of the range
2868 // Reasons for doing this:
2869 //  * UseLargePagesIndividualAllocation was set (normally only needed on WS2003 but possible to be set otherwise)
2870 //  * UseNUMAInterleaving requires a separate node for each piece
2871 static char* allocate_pages_individually(size_t bytes, char* addr, DWORD flags,
2872                                          DWORD prot,
2873                                          bool should_inject_error = false) {
2874   char * p_buf;
2875   // note: at setup time we guaranteed that NUMAInterleaveGranularity was aligned up to a page size
2876   size_t page_size = UseLargePages ? _large_page_size : os::vm_allocation_granularity();
2877   size_t chunk_size = UseNUMAInterleaving ? NUMAInterleaveGranularity : page_size;
2878 
2879   // first reserve enough address space in advance since we want to be
2880   // able to break a single contiguous virtual address range into multiple
2881   // large page commits but WS2003 does not allow reserving large page space
2882   // so we just use 4K pages for reserve, this gives us a legal contiguous
2883   // address space. then we will deallocate that reservation, and re alloc
2884   // using large pages
2885   const size_t size_of_reserve = bytes + chunk_size;
2886   if (bytes > size_of_reserve) {
2887     // Overflowed.
2888     return NULL;
2889   }
2890   p_buf = (char *) VirtualAlloc(addr,
2891                                 size_of_reserve,  // size of Reserve
2892                                 MEM_RESERVE,
2893                                 PAGE_READWRITE);
2894   // If reservation failed, return NULL
2895   if (p_buf == NULL) return NULL;
2896   MemTracker::record_virtual_memory_reserve((address)p_buf, size_of_reserve, CALLER_PC);
2897   os::release_memory(p_buf, bytes + chunk_size);
2898 
2899   // we still need to round up to a page boundary (in case we are using large pages)
2900   // but not to a chunk boundary (in case InterleavingGranularity doesn't align with page size)
2901   // instead we handle this in the bytes_to_rq computation below
2902   p_buf = (char *) align_size_up((size_t)p_buf, page_size);
2903 
2904   // now go through and allocate one chunk at a time until all bytes are
2905   // allocated
2906   size_t  bytes_remaining = bytes;
2907   // An overflow of align_size_up() would have been caught above
2908   // in the calculation of size_of_reserve.
2909   char * next_alloc_addr = p_buf;
2910   HANDLE hProc = GetCurrentProcess();
2911 
2912 #ifdef ASSERT
2913   // Variable for the failure injection
2914   long ran_num = os::random();
2915   size_t fail_after = ran_num % bytes;
2916 #endif
2917 
2918   int count=0;
2919   while (bytes_remaining) {
2920     // select bytes_to_rq to get to the next chunk_size boundary
2921 
2922     size_t bytes_to_rq = MIN2(bytes_remaining, chunk_size - ((size_t)next_alloc_addr % chunk_size));
2923     // Note allocate and commit
2924     char * p_new;
2925 
2926 #ifdef ASSERT
2927     bool inject_error_now = should_inject_error && (bytes_remaining <= fail_after);
2928 #else
2929     const bool inject_error_now = false;
2930 #endif
2931 
2932     if (inject_error_now) {
2933       p_new = NULL;
2934     } else {
2935       if (!UseNUMAInterleaving) {
2936         p_new = (char *) VirtualAlloc(next_alloc_addr,
2937                                       bytes_to_rq,
2938                                       flags,
2939                                       prot);
2940       } else {
2941         // get the next node to use from the used_node_list
2942         assert(numa_node_list_holder.get_count() > 0, "Multiple NUMA nodes expected");
2943         DWORD node = numa_node_list_holder.get_node_list_entry(count % numa_node_list_holder.get_count());
2944         p_new = (char *)os::Kernel32Dll::VirtualAllocExNuma(hProc,
2945                                                             next_alloc_addr,
2946                                                             bytes_to_rq,
2947                                                             flags,
2948                                                             prot,
2949                                                             node);
2950       }
2951     }
2952 
2953     if (p_new == NULL) {
2954       // Free any allocated pages
2955       if (next_alloc_addr > p_buf) {
2956         // Some memory was committed so release it.
2957         size_t bytes_to_release = bytes - bytes_remaining;
2958         // NMT has yet to record any individual blocks, so it
2959         // need to create a dummy 'reserve' record to match
2960         // the release.
2961         MemTracker::record_virtual_memory_reserve((address)p_buf,
2962                                                   bytes_to_release, CALLER_PC);
2963         os::release_memory(p_buf, bytes_to_release);
2964       }
2965 #ifdef ASSERT
2966       if (should_inject_error) {
2967         if (TracePageSizes && Verbose) {
2968           tty->print_cr("Reserving pages individually failed.");
2969         }
2970       }
2971 #endif
2972       return NULL;
2973     }
2974 
2975     bytes_remaining -= bytes_to_rq;
2976     next_alloc_addr += bytes_to_rq;
2977     count++;
2978   }
2979   // Although the memory is allocated individually, it is returned as one.
2980   // NMT records it as one block.
2981   if ((flags & MEM_COMMIT) != 0) {
2982     MemTracker::record_virtual_memory_reserve_and_commit((address)p_buf, bytes, CALLER_PC);
2983   } else {
2984     MemTracker::record_virtual_memory_reserve((address)p_buf, bytes, CALLER_PC);
2985   }
2986 
2987   // made it this far, success
2988   return p_buf;
2989 }
2990 
2991 
2992 
2993 void os::large_page_init() {
2994   if (!UseLargePages) return;
2995 
2996   // print a warning if any large page related flag is specified on command line
2997   bool warn_on_failure = !FLAG_IS_DEFAULT(UseLargePages) ||
2998                          !FLAG_IS_DEFAULT(LargePageSizeInBytes);
2999   bool success = false;
3000 
3001 #define WARN(msg) if (warn_on_failure) { warning(msg); }
3002   if (resolve_functions_for_large_page_init()) {
3003     if (request_lock_memory_privilege()) {
3004       size_t s = os::Kernel32Dll::GetLargePageMinimum();
3005       if (s) {
3006 #if defined(IA32) || defined(AMD64)
3007         if (s > 4*M || LargePageSizeInBytes > 4*M) {
3008           WARN("JVM cannot use large pages bigger than 4mb.");
3009         } else {
3010 #endif
3011           if (LargePageSizeInBytes && LargePageSizeInBytes % s == 0) {
3012             _large_page_size = LargePageSizeInBytes;
3013           } else {
3014             _large_page_size = s;
3015           }
3016           success = true;
3017 #if defined(IA32) || defined(AMD64)
3018         }
3019 #endif
3020       } else {
3021         WARN("Large page is not supported by the processor.");
3022       }
3023     } else {
3024       WARN("JVM cannot use large page memory because it does not have enough privilege to lock pages in memory.");
3025     }
3026   } else {
3027     WARN("Large page is not supported by the operating system.");
3028   }
3029 #undef WARN
3030 
3031   const size_t default_page_size = (size_t) vm_page_size();
3032   if (success && _large_page_size > default_page_size) {
3033     _page_sizes[0] = _large_page_size;
3034     _page_sizes[1] = default_page_size;
3035     _page_sizes[2] = 0;
3036   }
3037 
3038   cleanup_after_large_page_init();
3039   UseLargePages = success;
3040 }
3041 
3042 // On win32, one cannot release just a part of reserved memory, it's an
3043 // all or nothing deal.  When we split a reservation, we must break the
3044 // reservation into two reservations.
3045 void os::pd_split_reserved_memory(char *base, size_t size, size_t split,
3046                                   bool realloc) {
3047   if (size > 0) {
3048     release_memory(base, size);
3049     if (realloc) {
3050       reserve_memory(split, base);
3051     }
3052     if (size != split) {
3053       reserve_memory(size - split, base + split);
3054     }
3055   }
3056 }
3057 
3058 // Multiple threads can race in this code but it's not possible to unmap small sections of
3059 // virtual space to get requested alignment, like posix-like os's.
3060 // Windows prevents multiple thread from remapping over each other so this loop is thread-safe.
3061 char* os::reserve_memory_aligned(size_t size, size_t alignment) {
3062   assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
3063          "Alignment must be a multiple of allocation granularity (page size)");
3064   assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
3065 
3066   size_t extra_size = size + alignment;
3067   assert(extra_size >= size, "overflow, size is too large to allow alignment");
3068 
3069   char* aligned_base = NULL;
3070 
3071   do {
3072     char* extra_base = os::reserve_memory(extra_size, NULL, alignment);
3073     if (extra_base == NULL) {
3074       return NULL;
3075     }
3076     // Do manual alignment
3077     aligned_base = (char*) align_size_up((uintptr_t) extra_base, alignment);
3078 
3079     os::release_memory(extra_base, extra_size);
3080 
3081     aligned_base = os::reserve_memory(size, aligned_base);
3082 
3083   } while (aligned_base == NULL);
3084 
3085   return aligned_base;
3086 }
3087 
3088 char* os::pd_reserve_memory(size_t bytes, char* addr, size_t alignment_hint) {
3089   assert((size_t)addr % os::vm_allocation_granularity() == 0,
3090          "reserve alignment");
3091   assert(bytes % os::vm_page_size() == 0, "reserve page size");
3092   char* res;
3093   // note that if UseLargePages is on, all the areas that require interleaving
3094   // will go thru reserve_memory_special rather than thru here.
3095   bool use_individual = (UseNUMAInterleaving && !UseLargePages);
3096   if (!use_individual) {
3097     res = (char*)VirtualAlloc(addr, bytes, MEM_RESERVE, PAGE_READWRITE);
3098   } else {
3099     elapsedTimer reserveTimer;
3100     if (Verbose && PrintMiscellaneous) reserveTimer.start();
3101     // in numa interleaving, we have to allocate pages individually
3102     // (well really chunks of NUMAInterleaveGranularity size)
3103     res = allocate_pages_individually(bytes, addr, MEM_RESERVE, PAGE_READWRITE);
3104     if (res == NULL) {
3105       warning("NUMA page allocation failed");
3106     }
3107     if (Verbose && PrintMiscellaneous) {
3108       reserveTimer.stop();
3109       tty->print_cr("reserve_memory of %Ix bytes took " JLONG_FORMAT " ms (" JLONG_FORMAT " ticks)", bytes,
3110                     reserveTimer.milliseconds(), reserveTimer.ticks());
3111     }
3112   }
3113   assert(res == NULL || addr == NULL || addr == res,
3114          "Unexpected address from reserve.");
3115 
3116   return res;
3117 }
3118 
3119 // Reserve memory at an arbitrary address, only if that area is
3120 // available (and not reserved for something else).
3121 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
3122   // Windows os::reserve_memory() fails of the requested address range is
3123   // not avilable.
3124   return reserve_memory(bytes, requested_addr);
3125 }
3126 
3127 size_t os::large_page_size() {
3128   return _large_page_size;
3129 }
3130 
3131 bool os::can_commit_large_page_memory() {
3132   // Windows only uses large page memory when the entire region is reserved
3133   // and committed in a single VirtualAlloc() call. This may change in the
3134   // future, but with Windows 2003 it's not possible to commit on demand.
3135   return false;
3136 }
3137 
3138 bool os::can_execute_large_page_memory() {
3139   return true;
3140 }
3141 
3142 char* os::reserve_memory_special(size_t bytes, size_t alignment, char* addr,
3143                                  bool exec) {
3144   assert(UseLargePages, "only for large pages");
3145 
3146   if (!is_size_aligned(bytes, os::large_page_size()) || alignment > os::large_page_size()) {
3147     return NULL; // Fallback to small pages.
3148   }
3149 
3150   const DWORD prot = exec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
3151   const DWORD flags = MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES;
3152 
3153   // with large pages, there are two cases where we need to use Individual Allocation
3154   // 1) the UseLargePagesIndividualAllocation flag is set (set by default on WS2003)
3155   // 2) NUMA Interleaving is enabled, in which case we use a different node for each page
3156   if (UseLargePagesIndividualAllocation || UseNUMAInterleaving) {
3157     if (TracePageSizes && Verbose) {
3158       tty->print_cr("Reserving large pages individually.");
3159     }
3160     char * p_buf = allocate_pages_individually(bytes, addr, flags, prot, LargePagesIndividualAllocationInjectError);
3161     if (p_buf == NULL) {
3162       // give an appropriate warning message
3163       if (UseNUMAInterleaving) {
3164         warning("NUMA large page allocation failed, UseLargePages flag ignored");
3165       }
3166       if (UseLargePagesIndividualAllocation) {
3167         warning("Individually allocated large pages failed, "
3168                 "use -XX:-UseLargePagesIndividualAllocation to turn off");
3169       }
3170       return NULL;
3171     }
3172 
3173     return p_buf;
3174 
3175   } else {
3176     if (TracePageSizes && Verbose) {
3177       tty->print_cr("Reserving large pages in a single large chunk.");
3178     }
3179     // normal policy just allocate it all at once
3180     DWORD flag = MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES;
3181     char * res = (char *)VirtualAlloc(addr, bytes, flag, prot);
3182     if (res != NULL) {
3183       MemTracker::record_virtual_memory_reserve_and_commit((address)res, bytes, CALLER_PC);
3184     }
3185 
3186     return res;
3187   }
3188 }
3189 
3190 bool os::release_memory_special(char* base, size_t bytes) {
3191   assert(base != NULL, "Sanity check");
3192   return release_memory(base, bytes);
3193 }
3194 
3195 void os::print_statistics() {
3196 }
3197 
3198 static void warn_fail_commit_memory(char* addr, size_t bytes, bool exec) {
3199   int err = os::get_last_error();
3200   char buf[256];
3201   size_t buf_len = os::lasterror(buf, sizeof(buf));
3202   warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT
3203           ", %d) failed; error='%s' (DOS error/errno=%d)", addr, bytes,
3204           exec, buf_len != 0 ? buf : "<no_error_string>", err);
3205 }
3206 
3207 bool os::pd_commit_memory(char* addr, size_t bytes, bool exec) {
3208   if (bytes == 0) {
3209     // Don't bother the OS with noops.
3210     return true;
3211   }
3212   assert((size_t) addr % os::vm_page_size() == 0, "commit on page boundaries");
3213   assert(bytes % os::vm_page_size() == 0, "commit in page-sized chunks");
3214   // Don't attempt to print anything if the OS call fails. We're
3215   // probably low on resources, so the print itself may cause crashes.
3216 
3217   // unless we have NUMAInterleaving enabled, the range of a commit
3218   // is always within a reserve covered by a single VirtualAlloc
3219   // in that case we can just do a single commit for the requested size
3220   if (!UseNUMAInterleaving) {
3221     if (VirtualAlloc(addr, bytes, MEM_COMMIT, PAGE_READWRITE) == NULL) {
3222       NOT_PRODUCT(warn_fail_commit_memory(addr, bytes, exec);)
3223       return false;
3224     }
3225     if (exec) {
3226       DWORD oldprot;
3227       // Windows doc says to use VirtualProtect to get execute permissions
3228       if (!VirtualProtect(addr, bytes, PAGE_EXECUTE_READWRITE, &oldprot)) {
3229         NOT_PRODUCT(warn_fail_commit_memory(addr, bytes, exec);)
3230         return false;
3231       }
3232     }
3233     return true;
3234   } else {
3235 
3236     // when NUMAInterleaving is enabled, the commit might cover a range that
3237     // came from multiple VirtualAlloc reserves (using allocate_pages_individually).
3238     // VirtualQuery can help us determine that.  The RegionSize that VirtualQuery
3239     // returns represents the number of bytes that can be committed in one step.
3240     size_t bytes_remaining = bytes;
3241     char * next_alloc_addr = addr;
3242     while (bytes_remaining > 0) {
3243       MEMORY_BASIC_INFORMATION alloc_info;
3244       VirtualQuery(next_alloc_addr, &alloc_info, sizeof(alloc_info));
3245       size_t bytes_to_rq = MIN2(bytes_remaining, (size_t)alloc_info.RegionSize);
3246       if (VirtualAlloc(next_alloc_addr, bytes_to_rq, MEM_COMMIT,
3247                        PAGE_READWRITE) == NULL) {
3248         NOT_PRODUCT(warn_fail_commit_memory(next_alloc_addr, bytes_to_rq,
3249                                             exec);)
3250         return false;
3251       }
3252       if (exec) {
3253         DWORD oldprot;
3254         if (!VirtualProtect(next_alloc_addr, bytes_to_rq,
3255                             PAGE_EXECUTE_READWRITE, &oldprot)) {
3256           NOT_PRODUCT(warn_fail_commit_memory(next_alloc_addr, bytes_to_rq,
3257                                               exec);)
3258           return false;
3259         }
3260       }
3261       bytes_remaining -= bytes_to_rq;
3262       next_alloc_addr += bytes_to_rq;
3263     }
3264   }
3265   // if we made it this far, return true
3266   return true;
3267 }
3268 
3269 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,
3270                           bool exec) {
3271   // alignment_hint is ignored on this OS
3272   return pd_commit_memory(addr, size, exec);
3273 }
3274 
3275 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
3276                                   const char* mesg) {
3277   assert(mesg != NULL, "mesg must be specified");
3278   if (!pd_commit_memory(addr, size, exec)) {
3279     warn_fail_commit_memory(addr, size, exec);
3280     vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);
3281   }
3282 }
3283 
3284 void os::pd_commit_memory_or_exit(char* addr, size_t size,
3285                                   size_t alignment_hint, bool exec,
3286                                   const char* mesg) {
3287   // alignment_hint is ignored on this OS
3288   pd_commit_memory_or_exit(addr, size, exec, mesg);
3289 }
3290 
3291 bool os::pd_uncommit_memory(char* addr, size_t bytes) {
3292   if (bytes == 0) {
3293     // Don't bother the OS with noops.
3294     return true;
3295   }
3296   assert((size_t) addr % os::vm_page_size() == 0, "uncommit on page boundaries");
3297   assert(bytes % os::vm_page_size() == 0, "uncommit in page-sized chunks");
3298   return (VirtualFree(addr, bytes, MEM_DECOMMIT) != 0);
3299 }
3300 
3301 bool os::pd_release_memory(char* addr, size_t bytes) {
3302   return VirtualFree(addr, 0, MEM_RELEASE) != 0;
3303 }
3304 
3305 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
3306   return os::commit_memory(addr, size, !ExecMem);
3307 }
3308 
3309 bool os::remove_stack_guard_pages(char* addr, size_t size) {
3310   return os::uncommit_memory(addr, size);
3311 }
3312 
3313 // Set protections specified
3314 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
3315                         bool is_committed) {
3316   unsigned int p = 0;
3317   switch (prot) {
3318   case MEM_PROT_NONE: p = PAGE_NOACCESS; break;
3319   case MEM_PROT_READ: p = PAGE_READONLY; break;
3320   case MEM_PROT_RW:   p = PAGE_READWRITE; break;
3321   case MEM_PROT_RWX:  p = PAGE_EXECUTE_READWRITE; break;
3322   default:
3323     ShouldNotReachHere();
3324   }
3325 
3326   DWORD old_status;
3327 
3328   // Strange enough, but on Win32 one can change protection only for committed
3329   // memory, not a big deal anyway, as bytes less or equal than 64K
3330   if (!is_committed) {
3331     commit_memory_or_exit(addr, bytes, prot == MEM_PROT_RWX,
3332                           "cannot commit protection page");
3333   }
3334   // One cannot use os::guard_memory() here, as on Win32 guard page
3335   // have different (one-shot) semantics, from MSDN on PAGE_GUARD:
3336   //
3337   // Pages in the region become guard pages. Any attempt to access a guard page
3338   // causes the system to raise a STATUS_GUARD_PAGE exception and turn off
3339   // the guard page status. Guard pages thus act as a one-time access alarm.
3340   return VirtualProtect(addr, bytes, p, &old_status) != 0;
3341 }
3342 
3343 bool os::guard_memory(char* addr, size_t bytes) {
3344   DWORD old_status;
3345   return VirtualProtect(addr, bytes, PAGE_READWRITE | PAGE_GUARD, &old_status) != 0;
3346 }
3347 
3348 bool os::unguard_memory(char* addr, size_t bytes) {
3349   DWORD old_status;
3350   return VirtualProtect(addr, bytes, PAGE_READWRITE, &old_status) != 0;
3351 }
3352 
3353 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) { }
3354 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) { }
3355 void os::numa_make_global(char *addr, size_t bytes)    { }
3356 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint)    { }
3357 bool os::numa_topology_changed()                       { return false; }
3358 size_t os::numa_get_groups_num()                       { return MAX2(numa_node_list_holder.get_count(), 1); }
3359 int os::numa_get_group_id()                            { return 0; }
3360 size_t os::numa_get_leaf_groups(int *ids, size_t size) {
3361   if (numa_node_list_holder.get_count() == 0 && size > 0) {
3362     // Provide an answer for UMA systems
3363     ids[0] = 0;
3364     return 1;
3365   } else {
3366     // check for size bigger than actual groups_num
3367     size = MIN2(size, numa_get_groups_num());
3368     for (int i = 0; i < (int)size; i++) {
3369       ids[i] = numa_node_list_holder.get_node_list_entry(i);
3370     }
3371     return size;
3372   }
3373 }
3374 
3375 bool os::get_page_info(char *start, page_info* info) {
3376   return false;
3377 }
3378 
3379 char *os::scan_pages(char *start, char* end, page_info* page_expected,
3380                      page_info* page_found) {
3381   return end;
3382 }
3383 
3384 char* os::non_memory_address_word() {
3385   // Must never look like an address returned by reserve_memory,
3386   // even in its subfields (as defined by the CPU immediate fields,
3387   // if the CPU splits constants across multiple instructions).
3388   return (char*)-1;
3389 }
3390 
3391 #define MAX_ERROR_COUNT 100
3392 #define SYS_THREAD_ERROR 0xffffffffUL
3393 
3394 void os::pd_start_thread(Thread* thread) {
3395   DWORD ret = ResumeThread(thread->osthread()->thread_handle());
3396   // Returns previous suspend state:
3397   // 0:  Thread was not suspended
3398   // 1:  Thread is running now
3399   // >1: Thread is still suspended.
3400   assert(ret != SYS_THREAD_ERROR, "StartThread failed"); // should propagate back
3401 }
3402 
3403 class HighResolutionInterval : public CHeapObj<mtThread> {
3404   // The default timer resolution seems to be 10 milliseconds.
3405   // (Where is this written down?)
3406   // If someone wants to sleep for only a fraction of the default,
3407   // then we set the timer resolution down to 1 millisecond for
3408   // the duration of their interval.
3409   // We carefully set the resolution back, since otherwise we
3410   // seem to incur an overhead (3%?) that we don't need.
3411   // CONSIDER: if ms is small, say 3, then we should run with a high resolution time.
3412   // Buf if ms is large, say 500, or 503, we should avoid the call to timeBeginPeriod().
3413   // Alternatively, we could compute the relative error (503/500 = .6%) and only use
3414   // timeBeginPeriod() if the relative error exceeded some threshold.
3415   // timeBeginPeriod() has been linked to problems with clock drift on win32 systems and
3416   // to decreased efficiency related to increased timer "tick" rates.  We want to minimize
3417   // (a) calls to timeBeginPeriod() and timeEndPeriod() and (b) time spent with high
3418   // resolution timers running.
3419  private:
3420   jlong resolution;
3421  public:
3422   HighResolutionInterval(jlong ms) {
3423     resolution = ms % 10L;
3424     if (resolution != 0) {
3425       MMRESULT result = timeBeginPeriod(1L);
3426     }
3427   }
3428   ~HighResolutionInterval() {
3429     if (resolution != 0) {
3430       MMRESULT result = timeEndPeriod(1L);
3431     }
3432     resolution = 0L;
3433   }
3434 };
3435 
3436 int os::sleep(Thread* thread, jlong ms, bool interruptable) {
3437   jlong limit = (jlong) MAXDWORD;
3438 
3439   while (ms > limit) {
3440     int res;
3441     if ((res = sleep(thread, limit, interruptable)) != OS_TIMEOUT) {
3442       return res;
3443     }
3444     ms -= limit;
3445   }
3446 
3447   assert(thread == Thread::current(), "thread consistency check");
3448   OSThread* osthread = thread->osthread();
3449   OSThreadWaitState osts(osthread, false /* not Object.wait() */);
3450   int result;
3451   if (interruptable) {
3452     assert(thread->is_Java_thread(), "must be java thread");
3453     JavaThread *jt = (JavaThread *) thread;
3454     ThreadBlockInVM tbivm(jt);
3455 
3456     jt->set_suspend_equivalent();
3457     // cleared by handle_special_suspend_equivalent_condition() or
3458     // java_suspend_self() via check_and_wait_while_suspended()
3459 
3460     HANDLE events[1];
3461     events[0] = osthread->interrupt_event();
3462     HighResolutionInterval *phri=NULL;
3463     if (!ForceTimeHighResolution) {
3464       phri = new HighResolutionInterval(ms);
3465     }
3466     if (WaitForMultipleObjects(1, events, FALSE, (DWORD)ms) == WAIT_TIMEOUT) {
3467       result = OS_TIMEOUT;
3468     } else {
3469       ResetEvent(osthread->interrupt_event());
3470       osthread->set_interrupted(false);
3471       result = OS_INTRPT;
3472     }
3473     delete phri; //if it is NULL, harmless
3474 
3475     // were we externally suspended while we were waiting?
3476     jt->check_and_wait_while_suspended();
3477   } else {
3478     assert(!thread->is_Java_thread(), "must not be java thread");
3479     Sleep((long) ms);
3480     result = OS_TIMEOUT;
3481   }
3482   return result;
3483 }
3484 
3485 // Short sleep, direct OS call.
3486 //
3487 // ms = 0, means allow others (if any) to run.
3488 //
3489 void os::naked_short_sleep(jlong ms) {
3490   assert(ms < 1000, "Un-interruptable sleep, short time use only");
3491   Sleep(ms);
3492 }
3493 
3494 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3495 void os::infinite_sleep() {
3496   while (true) {    // sleep forever ...
3497     Sleep(100000);  // ... 100 seconds at a time
3498   }
3499 }
3500 
3501 typedef BOOL (WINAPI * STTSignature)(void);
3502 
3503 void os::naked_yield() {
3504   // Use either SwitchToThread() or Sleep(0)
3505   // Consider passing back the return value from SwitchToThread().
3506   if (os::Kernel32Dll::SwitchToThreadAvailable()) {
3507     SwitchToThread();
3508   } else {
3509     Sleep(0);
3510   }
3511 }
3512 
3513 // Win32 only gives you access to seven real priorities at a time,
3514 // so we compress Java's ten down to seven.  It would be better
3515 // if we dynamically adjusted relative priorities.
3516 
3517 int os::java_to_os_priority[CriticalPriority + 1] = {
3518   THREAD_PRIORITY_IDLE,                         // 0  Entry should never be used
3519   THREAD_PRIORITY_LOWEST,                       // 1  MinPriority
3520   THREAD_PRIORITY_LOWEST,                       // 2
3521   THREAD_PRIORITY_BELOW_NORMAL,                 // 3
3522   THREAD_PRIORITY_BELOW_NORMAL,                 // 4
3523   THREAD_PRIORITY_NORMAL,                       // 5  NormPriority
3524   THREAD_PRIORITY_NORMAL,                       // 6
3525   THREAD_PRIORITY_ABOVE_NORMAL,                 // 7
3526   THREAD_PRIORITY_ABOVE_NORMAL,                 // 8
3527   THREAD_PRIORITY_HIGHEST,                      // 9  NearMaxPriority
3528   THREAD_PRIORITY_HIGHEST,                      // 10 MaxPriority
3529   THREAD_PRIORITY_HIGHEST                       // 11 CriticalPriority
3530 };
3531 
3532 int prio_policy1[CriticalPriority + 1] = {
3533   THREAD_PRIORITY_IDLE,                         // 0  Entry should never be used
3534   THREAD_PRIORITY_LOWEST,                       // 1  MinPriority
3535   THREAD_PRIORITY_LOWEST,                       // 2
3536   THREAD_PRIORITY_BELOW_NORMAL,                 // 3
3537   THREAD_PRIORITY_BELOW_NORMAL,                 // 4
3538   THREAD_PRIORITY_NORMAL,                       // 5  NormPriority
3539   THREAD_PRIORITY_ABOVE_NORMAL,                 // 6
3540   THREAD_PRIORITY_ABOVE_NORMAL,                 // 7
3541   THREAD_PRIORITY_HIGHEST,                      // 8
3542   THREAD_PRIORITY_HIGHEST,                      // 9  NearMaxPriority
3543   THREAD_PRIORITY_TIME_CRITICAL,                // 10 MaxPriority
3544   THREAD_PRIORITY_TIME_CRITICAL                 // 11 CriticalPriority
3545 };
3546 
3547 static int prio_init() {
3548   // If ThreadPriorityPolicy is 1, switch tables
3549   if (ThreadPriorityPolicy == 1) {
3550     int i;
3551     for (i = 0; i < CriticalPriority + 1; i++) {
3552       os::java_to_os_priority[i] = prio_policy1[i];
3553     }
3554   }
3555   if (UseCriticalJavaThreadPriority) {
3556     os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority];
3557   }
3558   return 0;
3559 }
3560 
3561 OSReturn os::set_native_priority(Thread* thread, int priority) {
3562   if (!UseThreadPriorities) return OS_OK;
3563   bool ret = SetThreadPriority(thread->osthread()->thread_handle(), priority) != 0;
3564   return ret ? OS_OK : OS_ERR;
3565 }
3566 
3567 OSReturn os::get_native_priority(const Thread* const thread,
3568                                  int* priority_ptr) {
3569   if (!UseThreadPriorities) {
3570     *priority_ptr = java_to_os_priority[NormPriority];
3571     return OS_OK;
3572   }
3573   int os_prio = GetThreadPriority(thread->osthread()->thread_handle());
3574   if (os_prio == THREAD_PRIORITY_ERROR_RETURN) {
3575     assert(false, "GetThreadPriority failed");
3576     return OS_ERR;
3577   }
3578   *priority_ptr = os_prio;
3579   return OS_OK;
3580 }
3581 
3582 
3583 // Hint to the underlying OS that a task switch would not be good.
3584 // Void return because it's a hint and can fail.
3585 void os::hint_no_preempt() {}
3586 
3587 void os::interrupt(Thread* thread) {
3588   assert(!thread->is_Java_thread() || Thread::current() == thread ||
3589          Threads_lock->owned_by_self(),
3590          "possibility of dangling Thread pointer");
3591 
3592   OSThread* osthread = thread->osthread();
3593   osthread->set_interrupted(true);
3594   // More than one thread can get here with the same value of osthread,
3595   // resulting in multiple notifications.  We do, however, want the store
3596   // to interrupted() to be visible to other threads before we post
3597   // the interrupt event.
3598   OrderAccess::release();
3599   SetEvent(osthread->interrupt_event());
3600   // For JSR166:  unpark after setting status
3601   if (thread->is_Java_thread()) {
3602     ((JavaThread*)thread)->parker()->unpark();
3603   }
3604 
3605   ParkEvent * ev = thread->_ParkEvent;
3606   if (ev != NULL) ev->unpark();
3607 }
3608 
3609 
3610 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
3611   assert(!thread->is_Java_thread() || Thread::current() == thread || Threads_lock->owned_by_self(),
3612          "possibility of dangling Thread pointer");
3613 
3614   OSThread* osthread = thread->osthread();
3615   // There is no synchronization between the setting of the interrupt
3616   // and it being cleared here. It is critical - see 6535709 - that
3617   // we only clear the interrupt state, and reset the interrupt event,
3618   // if we are going to report that we were indeed interrupted - else
3619   // an interrupt can be "lost", leading to spurious wakeups or lost wakeups
3620   // depending on the timing. By checking thread interrupt event to see
3621   // if the thread gets real interrupt thus prevent spurious wakeup.
3622   bool interrupted = osthread->interrupted() && (WaitForSingleObject(osthread->interrupt_event(), 0) == WAIT_OBJECT_0);
3623   if (interrupted && clear_interrupted) {
3624     osthread->set_interrupted(false);
3625     ResetEvent(osthread->interrupt_event());
3626   } // Otherwise leave the interrupted state alone
3627 
3628   return interrupted;
3629 }
3630 
3631 // Get's a pc (hint) for a running thread. Currently used only for profiling.
3632 ExtendedPC os::get_thread_pc(Thread* thread) {
3633   CONTEXT context;
3634   context.ContextFlags = CONTEXT_CONTROL;
3635   HANDLE handle = thread->osthread()->thread_handle();
3636 #ifdef _M_IA64
3637   assert(0, "Fix get_thread_pc");
3638   return ExtendedPC(NULL);
3639 #else
3640   if (GetThreadContext(handle, &context)) {
3641 #ifdef _M_AMD64
3642     return ExtendedPC((address) context.Rip);
3643 #else
3644     return ExtendedPC((address) context.Eip);
3645 #endif
3646   } else {
3647     return ExtendedPC(NULL);
3648   }
3649 #endif
3650 }
3651 
3652 // GetCurrentThreadId() returns DWORD
3653 intx os::current_thread_id()  { return GetCurrentThreadId(); }
3654 
3655 static int _initial_pid = 0;
3656 
3657 int os::current_process_id() {
3658   return (_initial_pid ? _initial_pid : _getpid());
3659 }
3660 
3661 int    os::win32::_vm_page_size              = 0;
3662 int    os::win32::_vm_allocation_granularity = 0;
3663 int    os::win32::_processor_type            = 0;
3664 // Processor level is not available on non-NT systems, use vm_version instead
3665 int    os::win32::_processor_level           = 0;
3666 julong os::win32::_physical_memory           = 0;
3667 size_t os::win32::_default_stack_size        = 0;
3668 
3669 intx          os::win32::_os_thread_limit    = 0;
3670 volatile intx os::win32::_os_thread_count    = 0;
3671 
3672 bool   os::win32::_is_nt                     = false;
3673 bool   os::win32::_is_windows_2003           = false;
3674 bool   os::win32::_is_windows_server         = false;
3675 
3676 // 6573254
3677 // Currently, the bug is observed across all the supported Windows releases,
3678 // including the latest one (as of this writing - Windows Server 2012 R2)
3679 bool   os::win32::_has_exit_bug              = true;
3680 bool   os::win32::_has_performance_count     = 0;
3681 
3682 void os::win32::initialize_system_info() {
3683   SYSTEM_INFO si;
3684   GetSystemInfo(&si);
3685   _vm_page_size    = si.dwPageSize;
3686   _vm_allocation_granularity = si.dwAllocationGranularity;
3687   _processor_type  = si.dwProcessorType;
3688   _processor_level = si.wProcessorLevel;
3689   set_processor_count(si.dwNumberOfProcessors);
3690 
3691   MEMORYSTATUSEX ms;
3692   ms.dwLength = sizeof(ms);
3693 
3694   // also returns dwAvailPhys (free physical memory bytes), dwTotalVirtual, dwAvailVirtual,
3695   // dwMemoryLoad (% of memory in use)
3696   GlobalMemoryStatusEx(&ms);
3697   _physical_memory = ms.ullTotalPhys;
3698 
3699   OSVERSIONINFOEX oi;
3700   oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
3701   GetVersionEx((OSVERSIONINFO*)&oi);
3702   switch (oi.dwPlatformId) {
3703   case VER_PLATFORM_WIN32_WINDOWS: _is_nt = false; break;
3704   case VER_PLATFORM_WIN32_NT:
3705     _is_nt = true;
3706     {
3707       int os_vers = oi.dwMajorVersion * 1000 + oi.dwMinorVersion;
3708       if (os_vers == 5002) {
3709         _is_windows_2003 = true;
3710       }
3711       if (oi.wProductType == VER_NT_DOMAIN_CONTROLLER ||
3712           oi.wProductType == VER_NT_SERVER) {
3713         _is_windows_server = true;
3714       }
3715     }
3716     break;
3717   default: fatal("Unknown platform");
3718   }
3719 
3720   _default_stack_size = os::current_stack_size();
3721   assert(_default_stack_size > (size_t) _vm_page_size, "invalid stack size");
3722   assert((_default_stack_size & (_vm_page_size - 1)) == 0,
3723          "stack size not a multiple of page size");
3724 
3725   initialize_performance_counter();
3726 
3727   // Win95/Win98 scheduler bug work-around. The Win95/98 scheduler is
3728   // known to deadlock the system, if the VM issues to thread operations with
3729   // a too high frequency, e.g., such as changing the priorities.
3730   // The 6000 seems to work well - no deadlocks has been notices on the test
3731   // programs that we have seen experience this problem.
3732   if (!os::win32::is_nt()) {
3733     StarvationMonitorInterval = 6000;
3734   }
3735 }
3736 
3737 
3738 HINSTANCE os::win32::load_Windows_dll(const char* name, char *ebuf,
3739                                       int ebuflen) {
3740   char path[MAX_PATH];
3741   DWORD size;
3742   DWORD pathLen = (DWORD)sizeof(path);
3743   HINSTANCE result = NULL;
3744 
3745   // only allow library name without path component
3746   assert(strchr(name, '\\') == NULL, "path not allowed");
3747   assert(strchr(name, ':') == NULL, "path not allowed");
3748   if (strchr(name, '\\') != NULL || strchr(name, ':') != NULL) {
3749     jio_snprintf(ebuf, ebuflen,
3750                  "Invalid parameter while calling os::win32::load_windows_dll(): cannot take path: %s", name);
3751     return NULL;
3752   }
3753 
3754   // search system directory
3755   if ((size = GetSystemDirectory(path, pathLen)) > 0) {
3756     if (size >= pathLen) {
3757       return NULL; // truncated
3758     }
3759     if (jio_snprintf(path + size, pathLen - size, "\\%s", name) == -1) {
3760       return NULL; // truncated
3761     }
3762     if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3763       return result;
3764     }
3765   }
3766 
3767   // try Windows directory
3768   if ((size = GetWindowsDirectory(path, pathLen)) > 0) {
3769     if (size >= pathLen) {
3770       return NULL; // truncated
3771     }
3772     if (jio_snprintf(path + size, pathLen - size, "\\%s", name) == -1) {
3773       return NULL; // truncated
3774     }
3775     if ((result = (HINSTANCE)os::dll_load(path, ebuf, ebuflen)) != NULL) {
3776       return result;
3777     }
3778   }
3779 
3780   jio_snprintf(ebuf, ebuflen,
3781                "os::win32::load_windows_dll() cannot load %s from system directories.", name);
3782   return NULL;
3783 }
3784 
3785 #define EXIT_TIMEOUT     PRODUCT_ONLY(1000) NOT_PRODUCT(4000) /* 1 sec in product, 4 sec in debug */
3786 
3787 static BOOL CALLBACK init_crit_sect_call(PINIT_ONCE, PVOID pcrit_sect, PVOID*) {
3788   InitializeCriticalSection((CRITICAL_SECTION*)pcrit_sect);
3789   return TRUE;
3790 }
3791 
3792 int os::win32::exit_process_or_thread(Ept what, int exit_code) {
3793   // Basic approach:
3794   //  - Each exiting thread registers its intent to exit and then does so.
3795   //  - A thread trying to terminate the process must wait for all
3796   //    threads currently exiting to complete their exit.
3797 
3798   if (os::win32::has_exit_bug()) {
3799     // The array holds handles of the threads that have started exiting by calling
3800     // _endthreadex().
3801     // Should be large enough to avoid blocking the exiting thread due to lack of
3802     // a free slot.
3803     static HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3804     static int handle_count = 0;
3805 
3806     static INIT_ONCE init_once_crit_sect = INIT_ONCE_STATIC_INIT;
3807     static CRITICAL_SECTION crit_sect;
3808     int i, j;
3809     DWORD res;
3810     HANDLE hproc, hthr;
3811 
3812     // The first thread that reached this point, initializes the critical section.
3813     if (!InitOnceExecuteOnce(&init_once_crit_sect, init_crit_sect_call, &crit_sect, NULL)) {
3814       warning("crit_sect initialization failed in %s: %d\n", __FILE__, __LINE__);
3815     } else {
3816       EnterCriticalSection(&crit_sect);
3817 
3818       if (what == EPT_THREAD) {
3819         // Remove from the array those handles of the threads that have completed exiting.
3820         for (i = 0, j = 0; i < handle_count; ++i) {
3821           res = WaitForSingleObject(handles[i], 0 /* don't wait */);
3822           if (res == WAIT_TIMEOUT) {
3823             handles[j++] = handles[i];
3824           } else {
3825             if (res == WAIT_FAILED) {
3826               warning("WaitForSingleObject failed (%u) in %s: %d\n",
3827                       GetLastError(), __FILE__, __LINE__);
3828             }
3829             // Don't keep the handle, if we failed waiting for it.
3830             CloseHandle(handles[i]);
3831           }
3832         }
3833 
3834         // If there's no free slot in the array of the kept handles, we'll have to
3835         // wait until at least one thread completes exiting.
3836         if ((handle_count = j) == MAXIMUM_WAIT_OBJECTS) {
3837           // Raise the priority of the oldest exiting thread to increase its chances
3838           // to complete sooner.
3839           SetThreadPriority(handles[0], THREAD_PRIORITY_ABOVE_NORMAL);
3840           res = WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, handles, FALSE, EXIT_TIMEOUT);
3841           if (res >= WAIT_OBJECT_0 && res < (WAIT_OBJECT_0 + MAXIMUM_WAIT_OBJECTS)) {
3842             i = (res - WAIT_OBJECT_0);
3843             handle_count = MAXIMUM_WAIT_OBJECTS - 1;
3844             for (; i < handle_count; ++i) {
3845               handles[i] = handles[i + 1];
3846             }
3847           } else {
3848             warning("WaitForMultipleObjects %s (%u) in %s: %d\n",
3849                     (res == WAIT_FAILED ? "failed" : "timed out"),
3850                     GetLastError(), __FILE__, __LINE__);
3851             // Don't keep handles, if we failed waiting for them.
3852             for (i = 0; i < MAXIMUM_WAIT_OBJECTS; ++i) {
3853               CloseHandle(handles[i]);
3854             }
3855             handle_count = 0;
3856           }
3857         }
3858 
3859         // Store a duplicate of the current thread handle in the array of handles.
3860         hproc = GetCurrentProcess();
3861         hthr = GetCurrentThread();
3862         if (!DuplicateHandle(hproc, hthr, hproc, &handles[handle_count],
3863                              0, FALSE, DUPLICATE_SAME_ACCESS)) {
3864           warning("DuplicateHandle failed (%u) in %s: %d\n",
3865                   GetLastError(), __FILE__, __LINE__);
3866         } else {
3867           ++handle_count;
3868         }
3869 
3870         // The current exiting thread has stored its handle in the array, and now
3871         // should leave the critical section before calling _endthreadex().
3872 
3873       } else { // what != EPT_THREAD
3874         if (handle_count > 0) {
3875           // Before ending the process, make sure all the threads that had called
3876           // _endthreadex() completed.
3877 
3878           // Set the priority level of the current thread to the same value as
3879           // the priority level of exiting threads.
3880           // This is to ensure it will be given a fair chance to execute if
3881           // the timeout expires.
3882           hthr = GetCurrentThread();
3883           SetThreadPriority(hthr, THREAD_PRIORITY_ABOVE_NORMAL);
3884           for (i = 0; i < handle_count; ++i) {
3885             SetThreadPriority(handles[i], THREAD_PRIORITY_ABOVE_NORMAL);
3886           }
3887           res = WaitForMultipleObjects(handle_count, handles, TRUE, EXIT_TIMEOUT);
3888           if (res == WAIT_FAILED || res == WAIT_TIMEOUT) {
3889             warning("WaitForMultipleObjects %s (%u) in %s: %d\n",
3890                     (res == WAIT_FAILED ? "failed" : "timed out"),
3891                     GetLastError(), __FILE__, __LINE__);
3892           }
3893           for (i = 0; i < handle_count; ++i) {
3894             CloseHandle(handles[i]);
3895           }
3896           handle_count = 0;
3897         }
3898 
3899         // End the process, not leaving critical section.
3900         // This makes sure no other thread executes exit-related code at the same
3901         // time, thus a race is avoided.
3902         if (what == EPT_PROCESS) {
3903           ::exit(exit_code);
3904         } else {
3905           _exit(exit_code);
3906         }
3907       }
3908 
3909       LeaveCriticalSection(&crit_sect);
3910     }
3911   }
3912 
3913   // We are here if either
3914   // - there's no 'race at exit' bug on this OS release;
3915   // - initialization of the critical section failed (unlikely);
3916   // - the current thread has stored its handle and left the critical section.
3917   if (what == EPT_THREAD) {
3918     _endthreadex((unsigned)exit_code);
3919   } else if (what == EPT_PROCESS) {
3920     ::exit(exit_code);
3921   } else {
3922     _exit(exit_code);
3923   }
3924 
3925   // Should not reach here
3926   return exit_code;
3927 }
3928 
3929 #undef EXIT_TIMEOUT
3930 
3931 void os::win32::setmode_streams() {
3932   _setmode(_fileno(stdin), _O_BINARY);
3933   _setmode(_fileno(stdout), _O_BINARY);
3934   _setmode(_fileno(stderr), _O_BINARY);
3935 }
3936 
3937 
3938 bool os::is_debugger_attached() {
3939   return IsDebuggerPresent() ? true : false;
3940 }
3941 
3942 
3943 void os::wait_for_keypress_at_exit(void) {
3944   if (PauseAtExit) {
3945     fprintf(stderr, "Press any key to continue...\n");
3946     fgetc(stdin);
3947   }
3948 }
3949 
3950 
3951 int os::message_box(const char* title, const char* message) {
3952   int result = MessageBox(NULL, message, title,
3953                           MB_YESNO | MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFAULT_DESKTOP_ONLY);
3954   return result == IDYES;
3955 }
3956 
3957 int os::allocate_thread_local_storage() {
3958   return TlsAlloc();
3959 }
3960 
3961 
3962 void os::free_thread_local_storage(int index) {
3963   TlsFree(index);
3964 }
3965 
3966 
3967 void os::thread_local_storage_at_put(int index, void* value) {
3968   TlsSetValue(index, value);
3969   assert(thread_local_storage_at(index) == value, "Just checking");
3970 }
3971 
3972 
3973 void* os::thread_local_storage_at(int index) {
3974   return TlsGetValue(index);
3975 }
3976 
3977 
3978 #ifndef PRODUCT
3979 #ifndef _WIN64
3980 // Helpers to check whether NX protection is enabled
3981 int nx_exception_filter(_EXCEPTION_POINTERS *pex) {
3982   if (pex->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
3983       pex->ExceptionRecord->NumberParameters > 0 &&
3984       pex->ExceptionRecord->ExceptionInformation[0] ==
3985       EXCEPTION_INFO_EXEC_VIOLATION) {
3986     return EXCEPTION_EXECUTE_HANDLER;
3987   }
3988   return EXCEPTION_CONTINUE_SEARCH;
3989 }
3990 
3991 void nx_check_protection() {
3992   // If NX is enabled we'll get an exception calling into code on the stack
3993   char code[] = { (char)0xC3 }; // ret
3994   void *code_ptr = (void *)code;
3995   __try {
3996     __asm call code_ptr
3997   } __except(nx_exception_filter((_EXCEPTION_POINTERS*)_exception_info())) {
3998     tty->print_raw_cr("NX protection detected.");
3999   }
4000 }
4001 #endif // _WIN64
4002 #endif // PRODUCT
4003 
4004 // this is called _before_ the global arguments have been parsed
4005 void os::init(void) {
4006   _initial_pid = _getpid();
4007 
4008   init_random(1234567);
4009 
4010   win32::initialize_system_info();
4011   win32::setmode_streams();
4012   init_page_sizes((size_t) win32::vm_page_size());
4013 
4014   // This may be overridden later when argument processing is done.
4015   FLAG_SET_ERGO(bool, UseLargePagesIndividualAllocation,
4016                 os::win32::is_windows_2003());
4017 
4018   // Initialize main_process and main_thread
4019   main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
4020   if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
4021                        &main_thread, THREAD_ALL_ACCESS, false, 0)) {
4022     fatal("DuplicateHandle failed\n");
4023   }
4024   main_thread_id = (int) GetCurrentThreadId();
4025 }
4026 
4027 // To install functions for atexit processing
4028 extern "C" {
4029   static void perfMemory_exit_helper() {
4030     perfMemory_exit();
4031   }
4032 }
4033 
4034 static jint initSock();
4035 
4036 // this is called _after_ the global arguments have been parsed
4037 jint os::init_2(void) {
4038   // Allocate a single page and mark it as readable for safepoint polling
4039   address polling_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_READONLY);
4040   guarantee(polling_page != NULL, "Reserve Failed for polling page");
4041 
4042   address return_page  = (address)VirtualAlloc(polling_page, os::vm_page_size(), MEM_COMMIT, PAGE_READONLY);
4043   guarantee(return_page != NULL, "Commit Failed for polling page");
4044 
4045   os::set_polling_page(polling_page);
4046 
4047 #ifndef PRODUCT
4048   if (Verbose && PrintMiscellaneous) {
4049     tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n",
4050                (intptr_t)polling_page);
4051   }
4052 #endif
4053 
4054   if (!UseMembar) {
4055     address mem_serialize_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_READWRITE);
4056     guarantee(mem_serialize_page != NULL, "Reserve Failed for memory serialize page");
4057 
4058     return_page  = (address)VirtualAlloc(mem_serialize_page, os::vm_page_size(), MEM_COMMIT, PAGE_READWRITE);
4059     guarantee(return_page != NULL, "Commit Failed for memory serialize page");
4060 
4061     os::set_memory_serialize_page(mem_serialize_page);
4062 
4063 #ifndef PRODUCT
4064     if (Verbose && PrintMiscellaneous) {
4065       tty->print("[Memory Serialize  Page address: " INTPTR_FORMAT "]\n",
4066                  (intptr_t)mem_serialize_page);
4067     }
4068 #endif
4069   }
4070 
4071   // Setup Windows Exceptions
4072 
4073   // for debugging float code generation bugs
4074   if (ForceFloatExceptions) {
4075 #ifndef  _WIN64
4076     static long fp_control_word = 0;
4077     __asm { fstcw fp_control_word }
4078     // see Intel PPro Manual, Vol. 2, p 7-16
4079     const long precision = 0x20;
4080     const long underflow = 0x10;
4081     const long overflow  = 0x08;
4082     const long zero_div  = 0x04;
4083     const long denorm    = 0x02;
4084     const long invalid   = 0x01;
4085     fp_control_word |= invalid;
4086     __asm { fldcw fp_control_word }
4087 #endif
4088   }
4089 
4090   // If stack_commit_size is 0, windows will reserve the default size,
4091   // but only commit a small portion of it.
4092   size_t stack_commit_size = round_to(ThreadStackSize*K, os::vm_page_size());
4093   size_t default_reserve_size = os::win32::default_stack_size();
4094   size_t actual_reserve_size = stack_commit_size;
4095   if (stack_commit_size < default_reserve_size) {
4096     // If stack_commit_size == 0, we want this too
4097     actual_reserve_size = default_reserve_size;
4098   }
4099 
4100   // Check minimum allowable stack size for thread creation and to initialize
4101   // the java system classes, including StackOverflowError - depends on page
4102   // size.  Add a page for compiler2 recursion in main thread.
4103   // Add in 2*BytesPerWord times page size to account for VM stack during
4104   // class initialization depending on 32 or 64 bit VM.
4105   size_t min_stack_allowed =
4106             (size_t)(StackYellowPages+StackRedPages+StackShadowPages+
4107                      2*BytesPerWord COMPILER2_PRESENT(+1)) * os::vm_page_size();
4108   if (actual_reserve_size < min_stack_allowed) {
4109     tty->print_cr("\nThe stack size specified is too small, "
4110                   "Specify at least %dk",
4111                   min_stack_allowed / K);
4112     return JNI_ERR;
4113   }
4114 
4115   JavaThread::set_stack_size_at_create(stack_commit_size);
4116 
4117   // Calculate theoretical max. size of Threads to guard gainst artifical
4118   // out-of-memory situations, where all available address-space has been
4119   // reserved by thread stacks.
4120   assert(actual_reserve_size != 0, "Must have a stack");
4121 
4122   // Calculate the thread limit when we should start doing Virtual Memory
4123   // banging. Currently when the threads will have used all but 200Mb of space.
4124   //
4125   // TODO: consider performing a similar calculation for commit size instead
4126   // as reserve size, since on a 64-bit platform we'll run into that more
4127   // often than running out of virtual memory space.  We can use the
4128   // lower value of the two calculations as the os_thread_limit.
4129   size_t max_address_space = ((size_t)1 << (BitsPerWord - 1)) - (200 * K * K);
4130   win32::_os_thread_limit = (intx)(max_address_space / actual_reserve_size);
4131 
4132   // at exit methods are called in the reverse order of their registration.
4133   // there is no limit to the number of functions registered. atexit does
4134   // not set errno.
4135 
4136   if (PerfAllowAtExitRegistration) {
4137     // only register atexit functions if PerfAllowAtExitRegistration is set.
4138     // atexit functions can be delayed until process exit time, which
4139     // can be problematic for embedded VM situations. Embedded VMs should
4140     // call DestroyJavaVM() to assure that VM resources are released.
4141 
4142     // note: perfMemory_exit_helper atexit function may be removed in
4143     // the future if the appropriate cleanup code can be added to the
4144     // VM_Exit VMOperation's doit method.
4145     if (atexit(perfMemory_exit_helper) != 0) {
4146       warning("os::init_2 atexit(perfMemory_exit_helper) failed");
4147     }
4148   }
4149 
4150 #ifndef _WIN64
4151   // Print something if NX is enabled (win32 on AMD64)
4152   NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection());
4153 #endif
4154 
4155   // initialize thread priority policy
4156   prio_init();
4157 
4158   if (UseNUMA && !ForceNUMA) {
4159     UseNUMA = false; // We don't fully support this yet
4160   }
4161 
4162   if (UseNUMAInterleaving) {
4163     // first check whether this Windows OS supports VirtualAllocExNuma, if not ignore this flag
4164     bool success = numa_interleaving_init();
4165     if (!success) UseNUMAInterleaving = false;
4166   }
4167 
4168   if (initSock() != JNI_OK) {
4169     return JNI_ERR;
4170   }
4171 
4172   return JNI_OK;
4173 }
4174 
4175 // Mark the polling page as unreadable
4176 void os::make_polling_page_unreadable(void) {
4177   DWORD old_status;
4178   if (!VirtualProtect((char *)_polling_page, os::vm_page_size(),
4179                       PAGE_NOACCESS, &old_status)) {
4180     fatal("Could not disable polling page");
4181   }
4182 }
4183 
4184 // Mark the polling page as readable
4185 void os::make_polling_page_readable(void) {
4186   DWORD old_status;
4187   if (!VirtualProtect((char *)_polling_page, os::vm_page_size(),
4188                       PAGE_READONLY, &old_status)) {
4189     fatal("Could not enable polling page");
4190   }
4191 }
4192 
4193 
4194 int os::stat(const char *path, struct stat *sbuf) {
4195   char pathbuf[MAX_PATH];
4196   if (strlen(path) > MAX_PATH - 1) {
4197     errno = ENAMETOOLONG;
4198     return -1;
4199   }
4200   os::native_path(strcpy(pathbuf, path));
4201   int ret = ::stat(pathbuf, sbuf);
4202   if (sbuf != NULL && UseUTCFileTimestamp) {
4203     // Fix for 6539723.  st_mtime returned from stat() is dependent on
4204     // the system timezone and so can return different values for the
4205     // same file if/when daylight savings time changes.  This adjustment
4206     // makes sure the same timestamp is returned regardless of the TZ.
4207     //
4208     // See:
4209     // http://msdn.microsoft.com/library/
4210     //   default.asp?url=/library/en-us/sysinfo/base/
4211     //   time_zone_information_str.asp
4212     // and
4213     // http://msdn.microsoft.com/library/default.asp?url=
4214     //   /library/en-us/sysinfo/base/settimezoneinformation.asp
4215     //
4216     // NOTE: there is a insidious bug here:  If the timezone is changed
4217     // after the call to stat() but before 'GetTimeZoneInformation()', then
4218     // the adjustment we do here will be wrong and we'll return the wrong
4219     // value (which will likely end up creating an invalid class data
4220     // archive).  Absent a better API for this, or some time zone locking
4221     // mechanism, we'll have to live with this risk.
4222     TIME_ZONE_INFORMATION tz;
4223     DWORD tzid = GetTimeZoneInformation(&tz);
4224     int daylightBias =
4225       (tzid == TIME_ZONE_ID_DAYLIGHT) ?  tz.DaylightBias : tz.StandardBias;
4226     sbuf->st_mtime += (tz.Bias + daylightBias) * 60;
4227   }
4228   return ret;
4229 }
4230 
4231 
4232 #define FT2INT64(ft) \
4233   ((jlong)((jlong)(ft).dwHighDateTime << 32 | (julong)(ft).dwLowDateTime))
4234 
4235 
4236 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
4237 // are used by JVM M&M and JVMTI to get user+sys or user CPU time
4238 // of a thread.
4239 //
4240 // current_thread_cpu_time() and thread_cpu_time(Thread*) returns
4241 // the fast estimate available on the platform.
4242 
4243 // current_thread_cpu_time() is not optimized for Windows yet
4244 jlong os::current_thread_cpu_time() {
4245   // return user + sys since the cost is the same
4246   return os::thread_cpu_time(Thread::current(), true /* user+sys */);
4247 }
4248 
4249 jlong os::thread_cpu_time(Thread* thread) {
4250   // consistent with what current_thread_cpu_time() returns.
4251   return os::thread_cpu_time(thread, true /* user+sys */);
4252 }
4253 
4254 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
4255   return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
4256 }
4257 
4258 jlong os::thread_cpu_time(Thread* thread, bool user_sys_cpu_time) {
4259   // This code is copy from clasic VM -> hpi::sysThreadCPUTime
4260   // If this function changes, os::is_thread_cpu_time_supported() should too
4261   if (os::win32::is_nt()) {
4262     FILETIME CreationTime;
4263     FILETIME ExitTime;
4264     FILETIME KernelTime;
4265     FILETIME UserTime;
4266 
4267     if (GetThreadTimes(thread->osthread()->thread_handle(), &CreationTime,
4268                        &ExitTime, &KernelTime, &UserTime) == 0) {
4269       return -1;
4270     } else if (user_sys_cpu_time) {
4271       return (FT2INT64(UserTime) + FT2INT64(KernelTime)) * 100;
4272     } else {
4273       return FT2INT64(UserTime) * 100;
4274     }
4275   } else {
4276     return (jlong) timeGetTime() * 1000000;
4277   }
4278 }
4279 
4280 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4281   info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
4282   info_ptr->may_skip_backward = false;      // GetThreadTimes returns absolute time
4283   info_ptr->may_skip_forward = false;       // GetThreadTimes returns absolute time
4284   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
4285 }
4286 
4287 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4288   info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
4289   info_ptr->may_skip_backward = false;      // GetThreadTimes returns absolute time
4290   info_ptr->may_skip_forward = false;       // GetThreadTimes returns absolute time
4291   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
4292 }
4293 
4294 bool os::is_thread_cpu_time_supported() {
4295   // see os::thread_cpu_time
4296   if (os::win32::is_nt()) {
4297     FILETIME CreationTime;
4298     FILETIME ExitTime;
4299     FILETIME KernelTime;
4300     FILETIME UserTime;
4301 
4302     if (GetThreadTimes(GetCurrentThread(), &CreationTime, &ExitTime,
4303                        &KernelTime, &UserTime) == 0) {
4304       return false;
4305     } else {
4306       return true;
4307     }
4308   } else {
4309     return false;
4310   }
4311 }
4312 
4313 // Windows does't provide a loadavg primitive so this is stubbed out for now.
4314 // It does have primitives (PDH API) to get CPU usage and run queue length.
4315 // "\\Processor(_Total)\\% Processor Time", "\\System\\Processor Queue Length"
4316 // If we wanted to implement loadavg on Windows, we have a few options:
4317 //
4318 // a) Query CPU usage and run queue length and "fake" an answer by
4319 //    returning the CPU usage if it's under 100%, and the run queue
4320 //    length otherwise.  It turns out that querying is pretty slow
4321 //    on Windows, on the order of 200 microseconds on a fast machine.
4322 //    Note that on the Windows the CPU usage value is the % usage
4323 //    since the last time the API was called (and the first call
4324 //    returns 100%), so we'd have to deal with that as well.
4325 //
4326 // b) Sample the "fake" answer using a sampling thread and store
4327 //    the answer in a global variable.  The call to loadavg would
4328 //    just return the value of the global, avoiding the slow query.
4329 //
4330 // c) Sample a better answer using exponential decay to smooth the
4331 //    value.  This is basically the algorithm used by UNIX kernels.
4332 //
4333 // Note that sampling thread starvation could affect both (b) and (c).
4334 int os::loadavg(double loadavg[], int nelem) {
4335   return -1;
4336 }
4337 
4338 
4339 // DontYieldALot=false by default: dutifully perform all yields as requested by JVM_Yield()
4340 bool os::dont_yield() {
4341   return DontYieldALot;
4342 }
4343 
4344 // This method is a slightly reworked copy of JDK's sysOpen
4345 // from src/windows/hpi/src/sys_api_md.c
4346 
4347 int os::open(const char *path, int oflag, int mode) {
4348   char pathbuf[MAX_PATH];
4349 
4350   if (strlen(path) > MAX_PATH - 1) {
4351     errno = ENAMETOOLONG;
4352     return -1;
4353   }
4354   os::native_path(strcpy(pathbuf, path));
4355   return ::open(pathbuf, oflag | O_BINARY | O_NOINHERIT, mode);
4356 }
4357 
4358 FILE* os::open(int fd, const char* mode) {
4359   return ::_fdopen(fd, mode);
4360 }
4361 
4362 // Is a (classpath) directory empty?
4363 bool os::dir_is_empty(const char* path) {
4364   WIN32_FIND_DATA fd;
4365   HANDLE f = FindFirstFile(path, &fd);
4366   if (f == INVALID_HANDLE_VALUE) {
4367     return true;
4368   }
4369   FindClose(f);
4370   return false;
4371 }
4372 
4373 // create binary file, rewriting existing file if required
4374 int os::create_binary_file(const char* path, bool rewrite_existing) {
4375   int oflags = _O_CREAT | _O_WRONLY | _O_BINARY;
4376   if (!rewrite_existing) {
4377     oflags |= _O_EXCL;
4378   }
4379   return ::open(path, oflags, _S_IREAD | _S_IWRITE);
4380 }
4381 
4382 // return current position of file pointer
4383 jlong os::current_file_offset(int fd) {
4384   return (jlong)::_lseeki64(fd, (__int64)0L, SEEK_CUR);
4385 }
4386 
4387 // move file pointer to the specified offset
4388 jlong os::seek_to_file_offset(int fd, jlong offset) {
4389   return (jlong)::_lseeki64(fd, (__int64)offset, SEEK_SET);
4390 }
4391 
4392 
4393 jlong os::lseek(int fd, jlong offset, int whence) {
4394   return (jlong) ::_lseeki64(fd, offset, whence);
4395 }
4396 
4397 size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {
4398   OVERLAPPED ov;
4399   DWORD nread;
4400   BOOL result;
4401 
4402   ZeroMemory(&ov, sizeof(ov));
4403   ov.Offset = (DWORD)offset;
4404   ov.OffsetHigh = (DWORD)(offset >> 32);
4405 
4406   HANDLE h = (HANDLE)::_get_osfhandle(fd);
4407 
4408   result = ReadFile(h, (LPVOID)buf, nBytes, &nread, &ov);
4409 
4410   return result ? nread : 0;
4411 }
4412 
4413 
4414 // This method is a slightly reworked copy of JDK's sysNativePath
4415 // from src/windows/hpi/src/path_md.c
4416 
4417 // Convert a pathname to native format.  On win32, this involves forcing all
4418 // separators to be '\\' rather than '/' (both are legal inputs, but Win95
4419 // sometimes rejects '/') and removing redundant separators.  The input path is
4420 // assumed to have been converted into the character encoding used by the local
4421 // system.  Because this might be a double-byte encoding, care is taken to
4422 // treat double-byte lead characters correctly.
4423 //
4424 // This procedure modifies the given path in place, as the result is never
4425 // longer than the original.  There is no error return; this operation always
4426 // succeeds.
4427 char * os::native_path(char *path) {
4428   char *src = path, *dst = path, *end = path;
4429   char *colon = NULL;  // If a drive specifier is found, this will
4430                        // point to the colon following the drive letter
4431 
4432   // Assumption: '/', '\\', ':', and drive letters are never lead bytes
4433   assert(((!::IsDBCSLeadByte('/')) && (!::IsDBCSLeadByte('\\'))
4434           && (!::IsDBCSLeadByte(':'))), "Illegal lead byte");
4435 
4436   // Check for leading separators
4437 #define isfilesep(c) ((c) == '/' || (c) == '\\')
4438   while (isfilesep(*src)) {
4439     src++;
4440   }
4441 
4442   if (::isalpha(*src) && !::IsDBCSLeadByte(*src) && src[1] == ':') {
4443     // Remove leading separators if followed by drive specifier.  This
4444     // hack is necessary to support file URLs containing drive
4445     // specifiers (e.g., "file://c:/path").  As a side effect,
4446     // "/c:/path" can be used as an alternative to "c:/path".
4447     *dst++ = *src++;
4448     colon = dst;
4449     *dst++ = ':';
4450     src++;
4451   } else {
4452     src = path;
4453     if (isfilesep(src[0]) && isfilesep(src[1])) {
4454       // UNC pathname: Retain first separator; leave src pointed at
4455       // second separator so that further separators will be collapsed
4456       // into the second separator.  The result will be a pathname
4457       // beginning with "\\\\" followed (most likely) by a host name.
4458       src = dst = path + 1;
4459       path[0] = '\\';     // Force first separator to '\\'
4460     }
4461   }
4462 
4463   end = dst;
4464 
4465   // Remove redundant separators from remainder of path, forcing all
4466   // separators to be '\\' rather than '/'. Also, single byte space
4467   // characters are removed from the end of the path because those
4468   // are not legal ending characters on this operating system.
4469   //
4470   while (*src != '\0') {
4471     if (isfilesep(*src)) {
4472       *dst++ = '\\'; src++;
4473       while (isfilesep(*src)) src++;
4474       if (*src == '\0') {
4475         // Check for trailing separator
4476         end = dst;
4477         if (colon == dst - 2) break;  // "z:\\"
4478         if (dst == path + 1) break;   // "\\"
4479         if (dst == path + 2 && isfilesep(path[0])) {
4480           // "\\\\" is not collapsed to "\\" because "\\\\" marks the
4481           // beginning of a UNC pathname.  Even though it is not, by
4482           // itself, a valid UNC pathname, we leave it as is in order
4483           // to be consistent with the path canonicalizer as well
4484           // as the win32 APIs, which treat this case as an invalid
4485           // UNC pathname rather than as an alias for the root
4486           // directory of the current drive.
4487           break;
4488         }
4489         end = --dst;  // Path does not denote a root directory, so
4490                       // remove trailing separator
4491         break;
4492       }
4493       end = dst;
4494     } else {
4495       if (::IsDBCSLeadByte(*src)) {  // Copy a double-byte character
4496         *dst++ = *src++;
4497         if (*src) *dst++ = *src++;
4498         end = dst;
4499       } else {  // Copy a single-byte character
4500         char c = *src++;
4501         *dst++ = c;
4502         // Space is not a legal ending character
4503         if (c != ' ') end = dst;
4504       }
4505     }
4506   }
4507 
4508   *end = '\0';
4509 
4510   // For "z:", add "." to work around a bug in the C runtime library
4511   if (colon == dst - 1) {
4512     path[2] = '.';
4513     path[3] = '\0';
4514   }
4515 
4516   return path;
4517 }
4518 
4519 // This code is a copy of JDK's sysSetLength
4520 // from src/windows/hpi/src/sys_api_md.c
4521 
4522 int os::ftruncate(int fd, jlong length) {
4523   HANDLE h = (HANDLE)::_get_osfhandle(fd);
4524   long high = (long)(length >> 32);
4525   DWORD ret;
4526 
4527   if (h == (HANDLE)(-1)) {
4528     return -1;
4529   }
4530 
4531   ret = ::SetFilePointer(h, (long)(length), &high, FILE_BEGIN);
4532   if ((ret == 0xFFFFFFFF) && (::GetLastError() != NO_ERROR)) {
4533     return -1;
4534   }
4535 
4536   if (::SetEndOfFile(h) == FALSE) {
4537     return -1;
4538   }
4539 
4540   return 0;
4541 }
4542 
4543 
4544 // This code is a copy of JDK's sysSync
4545 // from src/windows/hpi/src/sys_api_md.c
4546 // except for the legacy workaround for a bug in Win 98
4547 
4548 int os::fsync(int fd) {
4549   HANDLE handle = (HANDLE)::_get_osfhandle(fd);
4550 
4551   if ((!::FlushFileBuffers(handle)) &&
4552       (GetLastError() != ERROR_ACCESS_DENIED)) {
4553     // from winerror.h
4554     return -1;
4555   }
4556   return 0;
4557 }
4558 
4559 static int nonSeekAvailable(int, long *);
4560 static int stdinAvailable(int, long *);
4561 
4562 #define S_ISCHR(mode)   (((mode) & _S_IFCHR) == _S_IFCHR)
4563 #define S_ISFIFO(mode)  (((mode) & _S_IFIFO) == _S_IFIFO)
4564 
4565 // This code is a copy of JDK's sysAvailable
4566 // from src/windows/hpi/src/sys_api_md.c
4567 
4568 int os::available(int fd, jlong *bytes) {
4569   jlong cur, end;
4570   struct _stati64 stbuf64;
4571 
4572   if (::_fstati64(fd, &stbuf64) >= 0) {
4573     int mode = stbuf64.st_mode;
4574     if (S_ISCHR(mode) || S_ISFIFO(mode)) {
4575       int ret;
4576       long lpbytes;
4577       if (fd == 0) {
4578         ret = stdinAvailable(fd, &lpbytes);
4579       } else {
4580         ret = nonSeekAvailable(fd, &lpbytes);
4581       }
4582       (*bytes) = (jlong)(lpbytes);
4583       return ret;
4584     }
4585     if ((cur = ::_lseeki64(fd, 0L, SEEK_CUR)) == -1) {
4586       return FALSE;
4587     } else if ((end = ::_lseeki64(fd, 0L, SEEK_END)) == -1) {
4588       return FALSE;
4589     } else if (::_lseeki64(fd, cur, SEEK_SET) == -1) {
4590       return FALSE;
4591     }
4592     *bytes = end - cur;
4593     return TRUE;
4594   } else {
4595     return FALSE;
4596   }
4597 }
4598 
4599 // This code is a copy of JDK's nonSeekAvailable
4600 // from src/windows/hpi/src/sys_api_md.c
4601 
4602 static int nonSeekAvailable(int fd, long *pbytes) {
4603   // This is used for available on non-seekable devices
4604   // (like both named and anonymous pipes, such as pipes
4605   //  connected to an exec'd process).
4606   // Standard Input is a special case.
4607   HANDLE han;
4608 
4609   if ((han = (HANDLE) ::_get_osfhandle(fd)) == (HANDLE)(-1)) {
4610     return FALSE;
4611   }
4612 
4613   if (! ::PeekNamedPipe(han, NULL, 0, NULL, (LPDWORD)pbytes, NULL)) {
4614     // PeekNamedPipe fails when at EOF.  In that case we
4615     // simply make *pbytes = 0 which is consistent with the
4616     // behavior we get on Solaris when an fd is at EOF.
4617     // The only alternative is to raise an Exception,
4618     // which isn't really warranted.
4619     //
4620     if (::GetLastError() != ERROR_BROKEN_PIPE) {
4621       return FALSE;
4622     }
4623     *pbytes = 0;
4624   }
4625   return TRUE;
4626 }
4627 
4628 #define MAX_INPUT_EVENTS 2000
4629 
4630 // This code is a copy of JDK's stdinAvailable
4631 // from src/windows/hpi/src/sys_api_md.c
4632 
4633 static int stdinAvailable(int fd, long *pbytes) {
4634   HANDLE han;
4635   DWORD numEventsRead = 0;  // Number of events read from buffer
4636   DWORD numEvents = 0;      // Number of events in buffer
4637   DWORD i = 0;              // Loop index
4638   DWORD curLength = 0;      // Position marker
4639   DWORD actualLength = 0;   // Number of bytes readable
4640   BOOL error = FALSE;       // Error holder
4641   INPUT_RECORD *lpBuffer;   // Pointer to records of input events
4642 
4643   if ((han = ::GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
4644     return FALSE;
4645   }
4646 
4647   // Construct an array of input records in the console buffer
4648   error = ::GetNumberOfConsoleInputEvents(han, &numEvents);
4649   if (error == 0) {
4650     return nonSeekAvailable(fd, pbytes);
4651   }
4652 
4653   // lpBuffer must fit into 64K or else PeekConsoleInput fails
4654   if (numEvents > MAX_INPUT_EVENTS) {
4655     numEvents = MAX_INPUT_EVENTS;
4656   }
4657 
4658   lpBuffer = (INPUT_RECORD *)os::malloc(numEvents * sizeof(INPUT_RECORD), mtInternal);
4659   if (lpBuffer == NULL) {
4660     return FALSE;
4661   }
4662 
4663   error = ::PeekConsoleInput(han, lpBuffer, numEvents, &numEventsRead);
4664   if (error == 0) {
4665     os::free(lpBuffer);
4666     return FALSE;
4667   }
4668 
4669   // Examine input records for the number of bytes available
4670   for (i=0; i<numEvents; i++) {
4671     if (lpBuffer[i].EventType == KEY_EVENT) {
4672 
4673       KEY_EVENT_RECORD *keyRecord = (KEY_EVENT_RECORD *)
4674                                       &(lpBuffer[i].Event);
4675       if (keyRecord->bKeyDown == TRUE) {
4676         CHAR *keyPressed = (CHAR *) &(keyRecord->uChar);
4677         curLength++;
4678         if (*keyPressed == '\r') {
4679           actualLength = curLength;
4680         }
4681       }
4682     }
4683   }
4684 
4685   if (lpBuffer != NULL) {
4686     os::free(lpBuffer);
4687   }
4688 
4689   *pbytes = (long) actualLength;
4690   return TRUE;
4691 }
4692 
4693 // Map a block of memory.
4694 char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
4695                         char *addr, size_t bytes, bool read_only,
4696                         bool allow_exec) {
4697   HANDLE hFile;
4698   char* base;
4699 
4700   hFile = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
4701                      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
4702   if (hFile == NULL) {
4703     if (PrintMiscellaneous && Verbose) {
4704       DWORD err = GetLastError();
4705       tty->print_cr("CreateFile() failed: GetLastError->%ld.", err);
4706     }
4707     return NULL;
4708   }
4709 
4710   if (allow_exec) {
4711     // CreateFileMapping/MapViewOfFileEx can't map executable memory
4712     // unless it comes from a PE image (which the shared archive is not.)
4713     // Even VirtualProtect refuses to give execute access to mapped memory
4714     // that was not previously executable.
4715     //
4716     // Instead, stick the executable region in anonymous memory.  Yuck.
4717     // Penalty is that ~4 pages will not be shareable - in the future
4718     // we might consider DLLizing the shared archive with a proper PE
4719     // header so that mapping executable + sharing is possible.
4720 
4721     base = (char*) VirtualAlloc(addr, bytes, MEM_COMMIT | MEM_RESERVE,
4722                                 PAGE_READWRITE);
4723     if (base == NULL) {
4724       if (PrintMiscellaneous && Verbose) {
4725         DWORD err = GetLastError();
4726         tty->print_cr("VirtualAlloc() failed: GetLastError->%ld.", err);
4727       }
4728       CloseHandle(hFile);
4729       return NULL;
4730     }
4731 
4732     DWORD bytes_read;
4733     OVERLAPPED overlapped;
4734     overlapped.Offset = (DWORD)file_offset;
4735     overlapped.OffsetHigh = 0;
4736     overlapped.hEvent = NULL;
4737     // ReadFile guarantees that if the return value is true, the requested
4738     // number of bytes were read before returning.
4739     bool res = ReadFile(hFile, base, (DWORD)bytes, &bytes_read, &overlapped) != 0;
4740     if (!res) {
4741       if (PrintMiscellaneous && Verbose) {
4742         DWORD err = GetLastError();
4743         tty->print_cr("ReadFile() failed: GetLastError->%ld.", err);
4744       }
4745       release_memory(base, bytes);
4746       CloseHandle(hFile);
4747       return NULL;
4748     }
4749   } else {
4750     HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_WRITECOPY, 0, 0,
4751                                     NULL /* file_name */);
4752     if (hMap == NULL) {
4753       if (PrintMiscellaneous && Verbose) {
4754         DWORD err = GetLastError();
4755         tty->print_cr("CreateFileMapping() failed: GetLastError->%ld.", err);
4756       }
4757       CloseHandle(hFile);
4758       return NULL;
4759     }
4760 
4761     DWORD access = read_only ? FILE_MAP_READ : FILE_MAP_COPY;
4762     base = (char*)MapViewOfFileEx(hMap, access, 0, (DWORD)file_offset,
4763                                   (DWORD)bytes, addr);
4764     if (base == NULL) {
4765       if (PrintMiscellaneous && Verbose) {
4766         DWORD err = GetLastError();
4767         tty->print_cr("MapViewOfFileEx() failed: GetLastError->%ld.", err);
4768       }
4769       CloseHandle(hMap);
4770       CloseHandle(hFile);
4771       return NULL;
4772     }
4773 
4774     if (CloseHandle(hMap) == 0) {
4775       if (PrintMiscellaneous && Verbose) {
4776         DWORD err = GetLastError();
4777         tty->print_cr("CloseHandle(hMap) failed: GetLastError->%ld.", err);
4778       }
4779       CloseHandle(hFile);
4780       return base;
4781     }
4782   }
4783 
4784   if (allow_exec) {
4785     DWORD old_protect;
4786     DWORD exec_access = read_only ? PAGE_EXECUTE_READ : PAGE_EXECUTE_READWRITE;
4787     bool res = VirtualProtect(base, bytes, exec_access, &old_protect) != 0;
4788 
4789     if (!res) {
4790       if (PrintMiscellaneous && Verbose) {
4791         DWORD err = GetLastError();
4792         tty->print_cr("VirtualProtect() failed: GetLastError->%ld.", err);
4793       }
4794       // Don't consider this a hard error, on IA32 even if the
4795       // VirtualProtect fails, we should still be able to execute
4796       CloseHandle(hFile);
4797       return base;
4798     }
4799   }
4800 
4801   if (CloseHandle(hFile) == 0) {
4802     if (PrintMiscellaneous && Verbose) {
4803       DWORD err = GetLastError();
4804       tty->print_cr("CloseHandle(hFile) failed: GetLastError->%ld.", err);
4805     }
4806     return base;
4807   }
4808 
4809   return base;
4810 }
4811 
4812 
4813 // Remap a block of memory.
4814 char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
4815                           char *addr, size_t bytes, bool read_only,
4816                           bool allow_exec) {
4817   // This OS does not allow existing memory maps to be remapped so we
4818   // have to unmap the memory before we remap it.
4819   if (!os::unmap_memory(addr, bytes)) {
4820     return NULL;
4821   }
4822 
4823   // There is a very small theoretical window between the unmap_memory()
4824   // call above and the map_memory() call below where a thread in native
4825   // code may be able to access an address that is no longer mapped.
4826 
4827   return os::map_memory(fd, file_name, file_offset, addr, bytes,
4828                         read_only, allow_exec);
4829 }
4830 
4831 
4832 // Unmap a block of memory.
4833 // Returns true=success, otherwise false.
4834 
4835 bool os::pd_unmap_memory(char* addr, size_t bytes) {
4836   BOOL result = UnmapViewOfFile(addr);
4837   if (result == 0) {
4838     if (PrintMiscellaneous && Verbose) {
4839       DWORD err = GetLastError();
4840       tty->print_cr("UnmapViewOfFile() failed: GetLastError->%ld.", err);
4841     }
4842     return false;
4843   }
4844   return true;
4845 }
4846 
4847 void os::pause() {
4848   char filename[MAX_PATH];
4849   if (PauseAtStartupFile && PauseAtStartupFile[0]) {
4850     jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
4851   } else {
4852     jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
4853   }
4854 
4855   int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
4856   if (fd != -1) {
4857     struct stat buf;
4858     ::close(fd);
4859     while (::stat(filename, &buf) == 0) {
4860       Sleep(100);
4861     }
4862   } else {
4863     jio_fprintf(stderr,
4864                 "Could not open pause file '%s', continuing immediately.\n", filename);
4865   }
4866 }
4867 
4868 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
4869   assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
4870 }
4871 
4872 // See the caveats for this class in os_windows.hpp
4873 // Protects the callback call so that raised OS EXCEPTIONS causes a jump back
4874 // into this method and returns false. If no OS EXCEPTION was raised, returns
4875 // true.
4876 // The callback is supposed to provide the method that should be protected.
4877 //
4878 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
4879   assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
4880   assert(!WatcherThread::watcher_thread()->has_crash_protection(),
4881          "crash_protection already set?");
4882 
4883   bool success = true;
4884   __try {
4885     WatcherThread::watcher_thread()->set_crash_protection(this);
4886     cb.call();
4887   } __except(EXCEPTION_EXECUTE_HANDLER) {
4888     // only for protection, nothing to do
4889     success = false;
4890   }
4891   WatcherThread::watcher_thread()->set_crash_protection(NULL);
4892   return success;
4893 }
4894 
4895 // An Event wraps a win32 "CreateEvent" kernel handle.
4896 //
4897 // We have a number of choices regarding "CreateEvent" win32 handle leakage:
4898 //
4899 // 1:  When a thread dies return the Event to the EventFreeList, clear the ParkHandle
4900 //     field, and call CloseHandle() on the win32 event handle.  Unpark() would
4901 //     need to be modified to tolerate finding a NULL (invalid) win32 event handle.
4902 //     In addition, an unpark() operation might fetch the handle field, but the
4903 //     event could recycle between the fetch and the SetEvent() operation.
4904 //     SetEvent() would either fail because the handle was invalid, or inadvertently work,
4905 //     as the win32 handle value had been recycled.  In an ideal world calling SetEvent()
4906 //     on an stale but recycled handle would be harmless, but in practice this might
4907 //     confuse other non-Sun code, so it's not a viable approach.
4908 //
4909 // 2:  Once a win32 event handle is associated with an Event, it remains associated
4910 //     with the Event.  The event handle is never closed.  This could be construed
4911 //     as handle leakage, but only up to the maximum # of threads that have been extant
4912 //     at any one time.  This shouldn't be an issue, as windows platforms typically
4913 //     permit a process to have hundreds of thousands of open handles.
4914 //
4915 // 3:  Same as (1), but periodically, at stop-the-world time, rundown the EventFreeList
4916 //     and release unused handles.
4917 //
4918 // 4:  Add a CRITICAL_SECTION to the Event to protect LD+SetEvent from LD;ST(null);CloseHandle.
4919 //     It's not clear, however, that we wouldn't be trading one type of leak for another.
4920 //
4921 // 5.  Use an RCU-like mechanism (Read-Copy Update).
4922 //     Or perhaps something similar to Maged Michael's "Hazard pointers".
4923 //
4924 // We use (2).
4925 //
4926 // TODO-FIXME:
4927 // 1.  Reconcile Doug's JSR166 j.u.c park-unpark with the objectmonitor implementation.
4928 // 2.  Consider wrapping the WaitForSingleObject(Ex) calls in SEH try/finally blocks
4929 //     to recover from (or at least detect) the dreaded Windows 841176 bug.
4930 // 3.  Collapse the interrupt_event, the JSR166 parker event, and the objectmonitor ParkEvent
4931 //     into a single win32 CreateEvent() handle.
4932 //
4933 // Assumption:
4934 //    Only one parker can exist on an event, which is why we allocate
4935 //    them per-thread. Multiple unparkers can coexist.
4936 //
4937 // _Event transitions in park()
4938 //   -1 => -1 : illegal
4939 //    1 =>  0 : pass - return immediately
4940 //    0 => -1 : block; then set _Event to 0 before returning
4941 //
4942 // _Event transitions in unpark()
4943 //    0 => 1 : just return
4944 //    1 => 1 : just return
4945 //   -1 => either 0 or 1; must signal target thread
4946 //         That is, we can safely transition _Event from -1 to either
4947 //         0 or 1.
4948 //
4949 // _Event serves as a restricted-range semaphore.
4950 //   -1 : thread is blocked, i.e. there is a waiter
4951 //    0 : neutral: thread is running or ready,
4952 //        could have been signaled after a wait started
4953 //    1 : signaled - thread is running or ready
4954 //
4955 // Another possible encoding of _Event would be with
4956 // explicit "PARKED" == 01b and "SIGNALED" == 10b bits.
4957 //
4958 
4959 int os::PlatformEvent::park(jlong Millis) {
4960   // Transitions for _Event:
4961   //   -1 => -1 : illegal
4962   //    1 =>  0 : pass - return immediately
4963   //    0 => -1 : block; then set _Event to 0 before returning
4964 
4965   guarantee(_ParkHandle != NULL , "Invariant");
4966   guarantee(Millis > 0          , "Invariant");
4967 
4968   // CONSIDER: defer assigning a CreateEvent() handle to the Event until
4969   // the initial park() operation.
4970   // Consider: use atomic decrement instead of CAS-loop
4971 
4972   int v;
4973   for (;;) {
4974     v = _Event;
4975     if (Atomic::cmpxchg(v-1, &_Event, v) == v) break;
4976   }
4977   guarantee((v == 0) || (v == 1), "invariant");
4978   if (v != 0) return OS_OK;
4979 
4980   // Do this the hard way by blocking ...
4981   // TODO: consider a brief spin here, gated on the success of recent
4982   // spin attempts by this thread.
4983   //
4984   // We decompose long timeouts into series of shorter timed waits.
4985   // Evidently large timo values passed in WaitForSingleObject() are problematic on some
4986   // versions of Windows.  See EventWait() for details.  This may be superstition.  Or not.
4987   // We trust the WAIT_TIMEOUT indication and don't track the elapsed wait time
4988   // with os::javaTimeNanos().  Furthermore, we assume that spurious returns from
4989   // ::WaitForSingleObject() caused by latent ::setEvent() operations will tend
4990   // to happen early in the wait interval.  Specifically, after a spurious wakeup (rv ==
4991   // WAIT_OBJECT_0 but _Event is still < 0) we don't bother to recompute Millis to compensate
4992   // for the already waited time.  This policy does not admit any new outcomes.
4993   // In the future, however, we might want to track the accumulated wait time and
4994   // adjust Millis accordingly if we encounter a spurious wakeup.
4995 
4996   const int MAXTIMEOUT = 0x10000000;
4997   DWORD rv = WAIT_TIMEOUT;
4998   while (_Event < 0 && Millis > 0) {
4999     DWORD prd = Millis;     // set prd = MAX (Millis, MAXTIMEOUT)
5000     if (Millis > MAXTIMEOUT) {
5001       prd = MAXTIMEOUT;
5002     }
5003     rv = ::WaitForSingleObject(_ParkHandle, prd);
5004     assert(rv == WAIT_OBJECT_0 || rv == WAIT_TIMEOUT, "WaitForSingleObject failed");
5005     if (rv == WAIT_TIMEOUT) {
5006       Millis -= prd;
5007     }
5008   }
5009   v = _Event;
5010   _Event = 0;
5011   // see comment at end of os::PlatformEvent::park() below:
5012   OrderAccess::fence();
5013   // If we encounter a nearly simultanous timeout expiry and unpark()
5014   // we return OS_OK indicating we awoke via unpark().
5015   // Implementor's license -- returning OS_TIMEOUT would be equally valid, however.
5016   return (v >= 0) ? OS_OK : OS_TIMEOUT;
5017 }
5018 
5019 void os::PlatformEvent::park() {
5020   // Transitions for _Event:
5021   //   -1 => -1 : illegal
5022   //    1 =>  0 : pass - return immediately
5023   //    0 => -1 : block; then set _Event to 0 before returning
5024 
5025   guarantee(_ParkHandle != NULL, "Invariant");
5026   // Invariant: Only the thread associated with the Event/PlatformEvent
5027   // may call park().
5028   // Consider: use atomic decrement instead of CAS-loop
5029   int v;
5030   for (;;) {
5031     v = _Event;
5032     if (Atomic::cmpxchg(v-1, &_Event, v) == v) break;
5033   }
5034   guarantee((v == 0) || (v == 1), "invariant");
5035   if (v != 0) return;
5036 
5037   // Do this the hard way by blocking ...
5038   // TODO: consider a brief spin here, gated on the success of recent
5039   // spin attempts by this thread.
5040   while (_Event < 0) {
5041     DWORD rv = ::WaitForSingleObject(_ParkHandle, INFINITE);
5042     assert(rv == WAIT_OBJECT_0, "WaitForSingleObject failed");
5043   }
5044 
5045   // Usually we'll find _Event == 0 at this point, but as
5046   // an optional optimization we clear it, just in case can
5047   // multiple unpark() operations drove _Event up to 1.
5048   _Event = 0;
5049   OrderAccess::fence();
5050   guarantee(_Event >= 0, "invariant");
5051 }
5052 
5053 void os::PlatformEvent::unpark() {
5054   guarantee(_ParkHandle != NULL, "Invariant");
5055 
5056   // Transitions for _Event:
5057   //    0 => 1 : just return
5058   //    1 => 1 : just return
5059   //   -1 => either 0 or 1; must signal target thread
5060   //         That is, we can safely transition _Event from -1 to either
5061   //         0 or 1.
5062   // See also: "Semaphores in Plan 9" by Mullender & Cox
5063   //
5064   // Note: Forcing a transition from "-1" to "1" on an unpark() means
5065   // that it will take two back-to-back park() calls for the owning
5066   // thread to block. This has the benefit of forcing a spurious return
5067   // from the first park() call after an unpark() call which will help
5068   // shake out uses of park() and unpark() without condition variables.
5069 
5070   if (Atomic::xchg(1, &_Event) >= 0) return;
5071 
5072   ::SetEvent(_ParkHandle);
5073 }
5074 
5075 
5076 // JSR166
5077 // -------------------------------------------------------
5078 
5079 // The Windows implementation of Park is very straightforward: Basic
5080 // operations on Win32 Events turn out to have the right semantics to
5081 // use them directly. We opportunistically resuse the event inherited
5082 // from Monitor.
5083 
5084 void Parker::park(bool isAbsolute, jlong time) {
5085   guarantee(_ParkEvent != NULL, "invariant");
5086   // First, demultiplex/decode time arguments
5087   if (time < 0) { // don't wait
5088     return;
5089   } else if (time == 0 && !isAbsolute) {
5090     time = INFINITE;
5091   } else if (isAbsolute) {
5092     time -= os::javaTimeMillis(); // convert to relative time
5093     if (time <= 0) {  // already elapsed
5094       return;
5095     }
5096   } else { // relative
5097     time /= 1000000;  // Must coarsen from nanos to millis
5098     if (time == 0) {  // Wait for the minimal time unit if zero
5099       time = 1;
5100     }
5101   }
5102 
5103   JavaThread* thread = (JavaThread*)(Thread::current());
5104   assert(thread->is_Java_thread(), "Must be JavaThread");
5105   JavaThread *jt = (JavaThread *)thread;
5106 
5107   // Don't wait if interrupted or already triggered
5108   if (Thread::is_interrupted(thread, false) ||
5109       WaitForSingleObject(_ParkEvent, 0) == WAIT_OBJECT_0) {
5110     ResetEvent(_ParkEvent);
5111     return;
5112   } else {
5113     ThreadBlockInVM tbivm(jt);
5114     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
5115     jt->set_suspend_equivalent();
5116 
5117     WaitForSingleObject(_ParkEvent, time);
5118     ResetEvent(_ParkEvent);
5119 
5120     // If externally suspended while waiting, re-suspend
5121     if (jt->handle_special_suspend_equivalent_condition()) {
5122       jt->java_suspend_self();
5123     }
5124   }
5125 }
5126 
5127 void Parker::unpark() {
5128   guarantee(_ParkEvent != NULL, "invariant");
5129   SetEvent(_ParkEvent);
5130 }
5131 
5132 // Run the specified command in a separate process. Return its exit value,
5133 // or -1 on failure (e.g. can't create a new process).
5134 int os::fork_and_exec(char* cmd) {
5135   STARTUPINFO si;
5136   PROCESS_INFORMATION pi;
5137 
5138   memset(&si, 0, sizeof(si));
5139   si.cb = sizeof(si);
5140   memset(&pi, 0, sizeof(pi));
5141   BOOL rslt = CreateProcess(NULL,   // executable name - use command line
5142                             cmd,    // command line
5143                             NULL,   // process security attribute
5144                             NULL,   // thread security attribute
5145                             TRUE,   // inherits system handles
5146                             0,      // no creation flags
5147                             NULL,   // use parent's environment block
5148                             NULL,   // use parent's starting directory
5149                             &si,    // (in) startup information
5150                             &pi);   // (out) process information
5151 
5152   if (rslt) {
5153     // Wait until child process exits.
5154     WaitForSingleObject(pi.hProcess, INFINITE);
5155 
5156     DWORD exit_code;
5157     GetExitCodeProcess(pi.hProcess, &exit_code);
5158 
5159     // Close process and thread handles.
5160     CloseHandle(pi.hProcess);
5161     CloseHandle(pi.hThread);
5162 
5163     return (int)exit_code;
5164   } else {
5165     return -1;
5166   }
5167 }
5168 
5169 //--------------------------------------------------------------------------------------------------
5170 // Non-product code
5171 
5172 static int mallocDebugIntervalCounter = 0;
5173 static int mallocDebugCounter = 0;
5174 bool os::check_heap(bool force) {
5175   if (++mallocDebugCounter < MallocVerifyStart && !force) return true;
5176   if (++mallocDebugIntervalCounter >= MallocVerifyInterval || force) {
5177     // Note: HeapValidate executes two hardware breakpoints when it finds something
5178     // wrong; at these points, eax contains the address of the offending block (I think).
5179     // To get to the exlicit error message(s) below, just continue twice.
5180     HANDLE heap = GetProcessHeap();
5181 
5182     // If we fail to lock the heap, then gflags.exe has been used
5183     // or some other special heap flag has been set that prevents
5184     // locking. We don't try to walk a heap we can't lock.
5185     if (HeapLock(heap) != 0) {
5186       PROCESS_HEAP_ENTRY phe;
5187       phe.lpData = NULL;
5188       while (HeapWalk(heap, &phe) != 0) {
5189         if ((phe.wFlags & PROCESS_HEAP_ENTRY_BUSY) &&
5190             !HeapValidate(heap, 0, phe.lpData)) {
5191           tty->print_cr("C heap has been corrupted (time: %d allocations)", mallocDebugCounter);
5192           tty->print_cr("corrupted block near address %#x, length %d", phe.lpData, phe.cbData);
5193           fatal("corrupted C heap");
5194         }
5195       }
5196       DWORD err = GetLastError();
5197       if (err != ERROR_NO_MORE_ITEMS && err != ERROR_CALL_NOT_IMPLEMENTED) {
5198         fatal(err_msg("heap walk aborted with error %d", err));
5199       }
5200       HeapUnlock(heap);
5201     }
5202     mallocDebugIntervalCounter = 0;
5203   }
5204   return true;
5205 }
5206 
5207 
5208 bool os::find(address addr, outputStream* st) {
5209   // Nothing yet
5210   return false;
5211 }
5212 
5213 LONG WINAPI os::win32::serialize_fault_filter(struct _EXCEPTION_POINTERS* e) {
5214   DWORD exception_code = e->ExceptionRecord->ExceptionCode;
5215 
5216   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
5217     JavaThread* thread = (JavaThread*)ThreadLocalStorage::get_thread_slow();
5218     PEXCEPTION_RECORD exceptionRecord = e->ExceptionRecord;
5219     address addr = (address) exceptionRecord->ExceptionInformation[1];
5220 
5221     if (os::is_memory_serialize_page(thread, addr)) {
5222       return EXCEPTION_CONTINUE_EXECUTION;
5223     }
5224   }
5225 
5226   return EXCEPTION_CONTINUE_SEARCH;
5227 }
5228 
5229 // We don't build a headless jre for Windows
5230 bool os::is_headless_jre() { return false; }
5231 
5232 static jint initSock() {
5233   WSADATA wsadata;
5234 
5235   if (!os::WinSock2Dll::WinSock2Available()) {
5236     jio_fprintf(stderr, "Could not load Winsock (error: %d)\n",
5237                 ::GetLastError());
5238     return JNI_ERR;
5239   }
5240 
5241   if (os::WinSock2Dll::WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
5242     jio_fprintf(stderr, "Could not initialize Winsock (error: %d)\n",
5243                 ::GetLastError());
5244     return JNI_ERR;
5245   }
5246   return JNI_OK;
5247 }
5248 
5249 struct hostent* os::get_host_by_name(char* name) {
5250   return (struct hostent*)os::WinSock2Dll::gethostbyname(name);
5251 }
5252 
5253 int os::socket_close(int fd) {
5254   return ::closesocket(fd);
5255 }
5256 
5257 int os::socket(int domain, int type, int protocol) {
5258   return ::socket(domain, type, protocol);
5259 }
5260 
5261 int os::connect(int fd, struct sockaddr* him, socklen_t len) {
5262   return ::connect(fd, him, len);
5263 }
5264 
5265 int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
5266   return ::recv(fd, buf, (int)nBytes, flags);
5267 }
5268 
5269 int os::send(int fd, char* buf, size_t nBytes, uint flags) {
5270   return ::send(fd, buf, (int)nBytes, flags);
5271 }
5272 
5273 int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
5274   return ::send(fd, buf, (int)nBytes, flags);
5275 }
5276 
5277 // WINDOWS CONTEXT Flags for THREAD_SAMPLING
5278 #if defined(IA32)
5279   #define sampling_context_flags (CONTEXT_FULL | CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS)
5280 #elif defined (AMD64)
5281   #define sampling_context_flags (CONTEXT_FULL | CONTEXT_FLOATING_POINT)
5282 #endif
5283 
5284 // returns true if thread could be suspended,
5285 // false otherwise
5286 static bool do_suspend(HANDLE* h) {
5287   if (h != NULL) {
5288     if (SuspendThread(*h) != ~0) {
5289       return true;
5290     }
5291   }
5292   return false;
5293 }
5294 
5295 // resume the thread
5296 // calling resume on an active thread is a no-op
5297 static void do_resume(HANDLE* h) {
5298   if (h != NULL) {
5299     ResumeThread(*h);
5300   }
5301 }
5302 
5303 // retrieve a suspend/resume context capable handle
5304 // from the tid. Caller validates handle return value.
5305 void get_thread_handle_for_extended_context(HANDLE* h,
5306                                             OSThread::thread_id_t tid) {
5307   if (h != NULL) {
5308     *h = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, tid);
5309   }
5310 }
5311 
5312 // Thread sampling implementation
5313 //
5314 void os::SuspendedThreadTask::internal_do_task() {
5315   CONTEXT    ctxt;
5316   HANDLE     h = NULL;
5317 
5318   // get context capable handle for thread
5319   get_thread_handle_for_extended_context(&h, _thread->osthread()->thread_id());
5320 
5321   // sanity
5322   if (h == NULL || h == INVALID_HANDLE_VALUE) {
5323     return;
5324   }
5325 
5326   // suspend the thread
5327   if (do_suspend(&h)) {
5328     ctxt.ContextFlags = sampling_context_flags;
5329     // get thread context
5330     GetThreadContext(h, &ctxt);
5331     SuspendedThreadTaskContext context(_thread, &ctxt);
5332     // pass context to Thread Sampling impl
5333     do_task(context);
5334     // resume thread
5335     do_resume(&h);
5336   }
5337 
5338   // close handle
5339   CloseHandle(h);
5340 }
5341 
5342 
5343 // Kernel32 API
5344 typedef SIZE_T (WINAPI* GetLargePageMinimum_Fn)(void);
5345 typedef LPVOID (WINAPI *VirtualAllocExNuma_Fn)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
5346 typedef BOOL (WINAPI *GetNumaHighestNodeNumber_Fn)(PULONG);
5347 typedef BOOL (WINAPI *GetNumaNodeProcessorMask_Fn)(UCHAR, PULONGLONG);
5348 typedef USHORT (WINAPI* RtlCaptureStackBackTrace_Fn)(ULONG, ULONG, PVOID*, PULONG);
5349 
5350 GetLargePageMinimum_Fn      os::Kernel32Dll::_GetLargePageMinimum = NULL;
5351 VirtualAllocExNuma_Fn       os::Kernel32Dll::_VirtualAllocExNuma = NULL;
5352 GetNumaHighestNodeNumber_Fn os::Kernel32Dll::_GetNumaHighestNodeNumber = NULL;
5353 GetNumaNodeProcessorMask_Fn os::Kernel32Dll::_GetNumaNodeProcessorMask = NULL;
5354 RtlCaptureStackBackTrace_Fn os::Kernel32Dll::_RtlCaptureStackBackTrace = NULL;
5355 
5356 
5357 BOOL                        os::Kernel32Dll::initialized = FALSE;
5358 SIZE_T os::Kernel32Dll::GetLargePageMinimum() {
5359   assert(initialized && _GetLargePageMinimum != NULL,
5360          "GetLargePageMinimumAvailable() not yet called");
5361   return _GetLargePageMinimum();
5362 }
5363 
5364 BOOL os::Kernel32Dll::GetLargePageMinimumAvailable() {
5365   if (!initialized) {
5366     initialize();
5367   }
5368   return _GetLargePageMinimum != NULL;
5369 }
5370 
5371 BOOL os::Kernel32Dll::NumaCallsAvailable() {
5372   if (!initialized) {
5373     initialize();
5374   }
5375   return _VirtualAllocExNuma != NULL;
5376 }
5377 
5378 LPVOID os::Kernel32Dll::VirtualAllocExNuma(HANDLE hProc, LPVOID addr,
5379                                            SIZE_T bytes, DWORD flags,
5380                                            DWORD prot, DWORD node) {
5381   assert(initialized && _VirtualAllocExNuma != NULL,
5382          "NUMACallsAvailable() not yet called");
5383 
5384   return _VirtualAllocExNuma(hProc, addr, bytes, flags, prot, node);
5385 }
5386 
5387 BOOL os::Kernel32Dll::GetNumaHighestNodeNumber(PULONG ptr_highest_node_number) {
5388   assert(initialized && _GetNumaHighestNodeNumber != NULL,
5389          "NUMACallsAvailable() not yet called");
5390 
5391   return _GetNumaHighestNodeNumber(ptr_highest_node_number);
5392 }
5393 
5394 BOOL os::Kernel32Dll::GetNumaNodeProcessorMask(UCHAR node,
5395                                                PULONGLONG proc_mask) {
5396   assert(initialized && _GetNumaNodeProcessorMask != NULL,
5397          "NUMACallsAvailable() not yet called");
5398 
5399   return _GetNumaNodeProcessorMask(node, proc_mask);
5400 }
5401 
5402 USHORT os::Kernel32Dll::RtlCaptureStackBackTrace(ULONG FrameToSkip,
5403                                                  ULONG FrameToCapture,
5404                                                  PVOID* BackTrace,
5405                                                  PULONG BackTraceHash) {
5406   if (!initialized) {
5407     initialize();
5408   }
5409 
5410   if (_RtlCaptureStackBackTrace != NULL) {
5411     return _RtlCaptureStackBackTrace(FrameToSkip, FrameToCapture,
5412                                      BackTrace, BackTraceHash);
5413   } else {
5414     return 0;
5415   }
5416 }
5417 
5418 void os::Kernel32Dll::initializeCommon() {
5419   if (!initialized) {
5420     HMODULE handle = ::GetModuleHandle("Kernel32.dll");
5421     assert(handle != NULL, "Just check");
5422     _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
5423     _VirtualAllocExNuma = (VirtualAllocExNuma_Fn)::GetProcAddress(handle, "VirtualAllocExNuma");
5424     _GetNumaHighestNodeNumber = (GetNumaHighestNodeNumber_Fn)::GetProcAddress(handle, "GetNumaHighestNodeNumber");
5425     _GetNumaNodeProcessorMask = (GetNumaNodeProcessorMask_Fn)::GetProcAddress(handle, "GetNumaNodeProcessorMask");
5426     _RtlCaptureStackBackTrace = (RtlCaptureStackBackTrace_Fn)::GetProcAddress(handle, "RtlCaptureStackBackTrace");
5427     initialized = TRUE;
5428   }
5429 }
5430 
5431 
5432 
5433 #ifndef JDK6_OR_EARLIER
5434 
5435 void os::Kernel32Dll::initialize() {
5436   initializeCommon();
5437 }
5438 
5439 
5440 // Kernel32 API
5441 inline BOOL os::Kernel32Dll::SwitchToThread() {
5442   return ::SwitchToThread();
5443 }
5444 
5445 inline BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
5446   return true;
5447 }
5448 
5449 // Help tools
5450 inline BOOL os::Kernel32Dll::HelpToolsAvailable() {
5451   return true;
5452 }
5453 
5454 inline HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,
5455                                                         DWORD th32ProcessId) {
5456   return ::CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
5457 }
5458 
5459 inline BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,
5460                                            LPMODULEENTRY32 lpme) {
5461   return ::Module32First(hSnapshot, lpme);
5462 }
5463 
5464 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,
5465                                           LPMODULEENTRY32 lpme) {
5466   return ::Module32Next(hSnapshot, lpme);
5467 }
5468 
5469 inline void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
5470   ::GetNativeSystemInfo(lpSystemInfo);
5471 }
5472 
5473 // PSAPI API
5474 inline BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess,
5475                                              HMODULE *lpModule, DWORD cb,
5476                                              LPDWORD lpcbNeeded) {
5477   return ::EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
5478 }
5479 
5480 inline DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess,
5481                                                HMODULE hModule,
5482                                                LPTSTR lpFilename,
5483                                                DWORD nSize) {
5484   return ::GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
5485 }
5486 
5487 inline BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess,
5488                                                HMODULE hModule,
5489                                                LPMODULEINFO lpmodinfo,
5490                                                DWORD cb) {
5491   return ::GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
5492 }
5493 
5494 inline BOOL os::PSApiDll::PSApiAvailable() {
5495   return true;
5496 }
5497 
5498 
5499 // WinSock2 API
5500 inline BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested,
5501                                         LPWSADATA lpWSAData) {
5502   return ::WSAStartup(wVersionRequested, lpWSAData);
5503 }
5504 
5505 inline struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
5506   return ::gethostbyname(name);
5507 }
5508 
5509 inline BOOL os::WinSock2Dll::WinSock2Available() {
5510   return true;
5511 }
5512 
5513 // Advapi API
5514 inline BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
5515                                                    BOOL DisableAllPrivileges,
5516                                                    PTOKEN_PRIVILEGES NewState,
5517                                                    DWORD BufferLength,
5518                                                    PTOKEN_PRIVILEGES PreviousState,
5519                                                    PDWORD ReturnLength) {
5520   return ::AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
5521                                  BufferLength, PreviousState, ReturnLength);
5522 }
5523 
5524 inline BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle,
5525                                               DWORD DesiredAccess,
5526                                               PHANDLE TokenHandle) {
5527   return ::OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
5528 }
5529 
5530 inline BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName,
5531                                                   LPCTSTR lpName,
5532                                                   PLUID lpLuid) {
5533   return ::LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
5534 }
5535 
5536 inline BOOL os::Advapi32Dll::AdvapiAvailable() {
5537   return true;
5538 }
5539 
5540 void* os::get_default_process_handle() {
5541   return (void*)GetModuleHandle(NULL);
5542 }
5543 
5544 // Builds a platform dependent Agent_OnLoad_<lib_name> function name
5545 // which is used to find statically linked in agents.
5546 // Additionally for windows, takes into account __stdcall names.
5547 // Parameters:
5548 //            sym_name: Symbol in library we are looking for
5549 //            lib_name: Name of library to look in, NULL for shared libs.
5550 //            is_absolute_path == true if lib_name is absolute path to agent
5551 //                                     such as "C:/a/b/L.dll"
5552 //            == false if only the base name of the library is passed in
5553 //               such as "L"
5554 char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
5555                                     bool is_absolute_path) {
5556   char *agent_entry_name;
5557   size_t len;
5558   size_t name_len;
5559   size_t prefix_len = strlen(JNI_LIB_PREFIX);
5560   size_t suffix_len = strlen(JNI_LIB_SUFFIX);
5561   const char *start;
5562 
5563   if (lib_name != NULL) {
5564     len = name_len = strlen(lib_name);
5565     if (is_absolute_path) {
5566       // Need to strip path, prefix and suffix
5567       if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
5568         lib_name = ++start;
5569       } else {
5570         // Need to check for drive prefix
5571         if ((start = strchr(lib_name, ':')) != NULL) {
5572           lib_name = ++start;
5573         }
5574       }
5575       if (len <= (prefix_len + suffix_len)) {
5576         return NULL;
5577       }
5578       lib_name += prefix_len;
5579       name_len = strlen(lib_name) - suffix_len;
5580     }
5581   }
5582   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
5583   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
5584   if (agent_entry_name == NULL) {
5585     return NULL;
5586   }
5587   if (lib_name != NULL) {
5588     const char *p = strrchr(sym_name, '@');
5589     if (p != NULL && p != sym_name) {
5590       // sym_name == _Agent_OnLoad@XX
5591       strncpy(agent_entry_name, sym_name, (p - sym_name));
5592       agent_entry_name[(p-sym_name)] = '\0';
5593       // agent_entry_name == _Agent_OnLoad
5594       strcat(agent_entry_name, "_");
5595       strncat(agent_entry_name, lib_name, name_len);
5596       strcat(agent_entry_name, p);
5597       // agent_entry_name == _Agent_OnLoad_lib_name@XX
5598     } else {
5599       strcpy(agent_entry_name, sym_name);
5600       strcat(agent_entry_name, "_");
5601       strncat(agent_entry_name, lib_name, name_len);
5602     }
5603   } else {
5604     strcpy(agent_entry_name, sym_name);
5605   }
5606   return agent_entry_name;
5607 }
5608 
5609 #else
5610 // Kernel32 API
5611 typedef BOOL (WINAPI* SwitchToThread_Fn)(void);
5612 typedef HANDLE (WINAPI* CreateToolhelp32Snapshot_Fn)(DWORD, DWORD);
5613 typedef BOOL (WINAPI* Module32First_Fn)(HANDLE, LPMODULEENTRY32);
5614 typedef BOOL (WINAPI* Module32Next_Fn)(HANDLE, LPMODULEENTRY32);
5615 typedef void (WINAPI* GetNativeSystemInfo_Fn)(LPSYSTEM_INFO);
5616 
5617 SwitchToThread_Fn           os::Kernel32Dll::_SwitchToThread = NULL;
5618 CreateToolhelp32Snapshot_Fn os::Kernel32Dll::_CreateToolhelp32Snapshot = NULL;
5619 Module32First_Fn            os::Kernel32Dll::_Module32First = NULL;
5620 Module32Next_Fn             os::Kernel32Dll::_Module32Next = NULL;
5621 GetNativeSystemInfo_Fn      os::Kernel32Dll::_GetNativeSystemInfo = NULL;
5622 
5623 void os::Kernel32Dll::initialize() {
5624   if (!initialized) {
5625     HMODULE handle = ::GetModuleHandle("Kernel32.dll");
5626     assert(handle != NULL, "Just check");
5627 
5628     _SwitchToThread = (SwitchToThread_Fn)::GetProcAddress(handle, "SwitchToThread");
5629     _CreateToolhelp32Snapshot = (CreateToolhelp32Snapshot_Fn)
5630       ::GetProcAddress(handle, "CreateToolhelp32Snapshot");
5631     _Module32First = (Module32First_Fn)::GetProcAddress(handle, "Module32First");
5632     _Module32Next = (Module32Next_Fn)::GetProcAddress(handle, "Module32Next");
5633     _GetNativeSystemInfo = (GetNativeSystemInfo_Fn)::GetProcAddress(handle, "GetNativeSystemInfo");
5634     initializeCommon();  // resolve the functions that always need resolving
5635 
5636     initialized = TRUE;
5637   }
5638 }
5639 
5640 BOOL os::Kernel32Dll::SwitchToThread() {
5641   assert(initialized && _SwitchToThread != NULL,
5642          "SwitchToThreadAvailable() not yet called");
5643   return _SwitchToThread();
5644 }
5645 
5646 
5647 BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
5648   if (!initialized) {
5649     initialize();
5650   }
5651   return _SwitchToThread != NULL;
5652 }
5653 
5654 // Help tools
5655 BOOL os::Kernel32Dll::HelpToolsAvailable() {
5656   if (!initialized) {
5657     initialize();
5658   }
5659   return _CreateToolhelp32Snapshot != NULL &&
5660          _Module32First != NULL &&
5661          _Module32Next != NULL;
5662 }
5663 
5664 HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,
5665                                                  DWORD th32ProcessId) {
5666   assert(initialized && _CreateToolhelp32Snapshot != NULL,
5667          "HelpToolsAvailable() not yet called");
5668 
5669   return _CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
5670 }
5671 
5672 BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
5673   assert(initialized && _Module32First != NULL,
5674          "HelpToolsAvailable() not yet called");
5675 
5676   return _Module32First(hSnapshot, lpme);
5677 }
5678 
5679 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,
5680                                           LPMODULEENTRY32 lpme) {
5681   assert(initialized && _Module32Next != NULL,
5682          "HelpToolsAvailable() not yet called");
5683 
5684   return _Module32Next(hSnapshot, lpme);
5685 }
5686 
5687 
5688 BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
5689   if (!initialized) {
5690     initialize();
5691   }
5692   return _GetNativeSystemInfo != NULL;
5693 }
5694 
5695 void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
5696   assert(initialized && _GetNativeSystemInfo != NULL,
5697          "GetNativeSystemInfoAvailable() not yet called");
5698 
5699   _GetNativeSystemInfo(lpSystemInfo);
5700 }
5701 
5702 // PSAPI API
5703 
5704 
5705 typedef BOOL (WINAPI *EnumProcessModules_Fn)(HANDLE, HMODULE *, DWORD, LPDWORD);
5706 typedef BOOL (WINAPI *GetModuleFileNameEx_Fn)(HANDLE, HMODULE, LPTSTR, DWORD);
5707 typedef BOOL (WINAPI *GetModuleInformation_Fn)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
5708 
5709 EnumProcessModules_Fn   os::PSApiDll::_EnumProcessModules = NULL;
5710 GetModuleFileNameEx_Fn  os::PSApiDll::_GetModuleFileNameEx = NULL;
5711 GetModuleInformation_Fn os::PSApiDll::_GetModuleInformation = NULL;
5712 BOOL                    os::PSApiDll::initialized = FALSE;
5713 
5714 void os::PSApiDll::initialize() {
5715   if (!initialized) {
5716     HMODULE handle = os::win32::load_Windows_dll("PSAPI.DLL", NULL, 0);
5717     if (handle != NULL) {
5718       _EnumProcessModules = (EnumProcessModules_Fn)::GetProcAddress(handle,
5719                                                                     "EnumProcessModules");
5720       _GetModuleFileNameEx = (GetModuleFileNameEx_Fn)::GetProcAddress(handle,
5721                                                                       "GetModuleFileNameExA");
5722       _GetModuleInformation = (GetModuleInformation_Fn)::GetProcAddress(handle,
5723                                                                         "GetModuleInformation");
5724     }
5725     initialized = TRUE;
5726   }
5727 }
5728 
5729 
5730 
5731 BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule,
5732                                       DWORD cb, LPDWORD lpcbNeeded) {
5733   assert(initialized && _EnumProcessModules != NULL,
5734          "PSApiAvailable() not yet called");
5735   return _EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
5736 }
5737 
5738 DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule,
5739                                         LPTSTR lpFilename, DWORD nSize) {
5740   assert(initialized && _GetModuleFileNameEx != NULL,
5741          "PSApiAvailable() not yet called");
5742   return _GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
5743 }
5744 
5745 BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule,
5746                                         LPMODULEINFO lpmodinfo, DWORD cb) {
5747   assert(initialized && _GetModuleInformation != NULL,
5748          "PSApiAvailable() not yet called");
5749   return _GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
5750 }
5751 
5752 BOOL os::PSApiDll::PSApiAvailable() {
5753   if (!initialized) {
5754     initialize();
5755   }
5756   return _EnumProcessModules != NULL &&
5757     _GetModuleFileNameEx != NULL &&
5758     _GetModuleInformation != NULL;
5759 }
5760 
5761 
5762 // WinSock2 API
5763 typedef int (PASCAL FAR* WSAStartup_Fn)(WORD, LPWSADATA);
5764 typedef struct hostent *(PASCAL FAR *gethostbyname_Fn)(...);
5765 
5766 WSAStartup_Fn    os::WinSock2Dll::_WSAStartup = NULL;
5767 gethostbyname_Fn os::WinSock2Dll::_gethostbyname = NULL;
5768 BOOL             os::WinSock2Dll::initialized = FALSE;
5769 
5770 void os::WinSock2Dll::initialize() {
5771   if (!initialized) {
5772     HMODULE handle = os::win32::load_Windows_dll("ws2_32.dll", NULL, 0);
5773     if (handle != NULL) {
5774       _WSAStartup = (WSAStartup_Fn)::GetProcAddress(handle, "WSAStartup");
5775       _gethostbyname = (gethostbyname_Fn)::GetProcAddress(handle, "gethostbyname");
5776     }
5777     initialized = TRUE;
5778   }
5779 }
5780 
5781 
5782 BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
5783   assert(initialized && _WSAStartup != NULL,
5784          "WinSock2Available() not yet called");
5785   return _WSAStartup(wVersionRequested, lpWSAData);
5786 }
5787 
5788 struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
5789   assert(initialized && _gethostbyname != NULL,
5790          "WinSock2Available() not yet called");
5791   return _gethostbyname(name);
5792 }
5793 
5794 BOOL os::WinSock2Dll::WinSock2Available() {
5795   if (!initialized) {
5796     initialize();
5797   }
5798   return _WSAStartup != NULL &&
5799     _gethostbyname != NULL;
5800 }
5801 
5802 typedef BOOL (WINAPI *AdjustTokenPrivileges_Fn)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
5803 typedef BOOL (WINAPI *OpenProcessToken_Fn)(HANDLE, DWORD, PHANDLE);
5804 typedef BOOL (WINAPI *LookupPrivilegeValue_Fn)(LPCTSTR, LPCTSTR, PLUID);
5805 
5806 AdjustTokenPrivileges_Fn os::Advapi32Dll::_AdjustTokenPrivileges = NULL;
5807 OpenProcessToken_Fn      os::Advapi32Dll::_OpenProcessToken = NULL;
5808 LookupPrivilegeValue_Fn  os::Advapi32Dll::_LookupPrivilegeValue = NULL;
5809 BOOL                     os::Advapi32Dll::initialized = FALSE;
5810 
5811 void os::Advapi32Dll::initialize() {
5812   if (!initialized) {
5813     HMODULE handle = os::win32::load_Windows_dll("advapi32.dll", NULL, 0);
5814     if (handle != NULL) {
5815       _AdjustTokenPrivileges = (AdjustTokenPrivileges_Fn)::GetProcAddress(handle,
5816                                                                           "AdjustTokenPrivileges");
5817       _OpenProcessToken = (OpenProcessToken_Fn)::GetProcAddress(handle,
5818                                                                 "OpenProcessToken");
5819       _LookupPrivilegeValue = (LookupPrivilegeValue_Fn)::GetProcAddress(handle,
5820                                                                         "LookupPrivilegeValueA");
5821     }
5822     initialized = TRUE;
5823   }
5824 }
5825 
5826 BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
5827                                             BOOL DisableAllPrivileges,
5828                                             PTOKEN_PRIVILEGES NewState,
5829                                             DWORD BufferLength,
5830                                             PTOKEN_PRIVILEGES PreviousState,
5831                                             PDWORD ReturnLength) {
5832   assert(initialized && _AdjustTokenPrivileges != NULL,
5833          "AdvapiAvailable() not yet called");
5834   return _AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
5835                                 BufferLength, PreviousState, ReturnLength);
5836 }
5837 
5838 BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle,
5839                                        DWORD DesiredAccess,
5840                                        PHANDLE TokenHandle) {
5841   assert(initialized && _OpenProcessToken != NULL,
5842          "AdvapiAvailable() not yet called");
5843   return _OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
5844 }
5845 
5846 BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName,
5847                                            LPCTSTR lpName, PLUID lpLuid) {
5848   assert(initialized && _LookupPrivilegeValue != NULL,
5849          "AdvapiAvailable() not yet called");
5850   return _LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
5851 }
5852 
5853 BOOL os::Advapi32Dll::AdvapiAvailable() {
5854   if (!initialized) {
5855     initialize();
5856   }
5857   return _AdjustTokenPrivileges != NULL &&
5858     _OpenProcessToken != NULL &&
5859     _LookupPrivilegeValue != NULL;
5860 }
5861 
5862 #endif
5863 
5864 #ifndef PRODUCT
5865 
5866 // test the code path in reserve_memory_special() that tries to allocate memory in a single
5867 // contiguous memory block at a particular address.
5868 // The test first tries to find a good approximate address to allocate at by using the same
5869 // method to allocate some memory at any address. The test then tries to allocate memory in
5870 // the vicinity (not directly after it to avoid possible by-chance use of that location)
5871 // This is of course only some dodgy assumption, there is no guarantee that the vicinity of
5872 // the previously allocated memory is available for allocation. The only actual failure
5873 // that is reported is when the test tries to allocate at a particular location but gets a
5874 // different valid one. A NULL return value at this point is not considered an error but may
5875 // be legitimate.
5876 // If -XX:+VerboseInternalVMTests is enabled, print some explanatory messages.
5877 void TestReserveMemorySpecial_test() {
5878   if (!UseLargePages) {
5879     if (VerboseInternalVMTests) {
5880       gclog_or_tty->print("Skipping test because large pages are disabled");
5881     }
5882     return;
5883   }
5884   // save current value of globals
5885   bool old_use_large_pages_individual_allocation = UseLargePagesIndividualAllocation;
5886   bool old_use_numa_interleaving = UseNUMAInterleaving;
5887 
5888   // set globals to make sure we hit the correct code path
5889   UseLargePagesIndividualAllocation = UseNUMAInterleaving = false;
5890 
5891   // do an allocation at an address selected by the OS to get a good one.
5892   const size_t large_allocation_size = os::large_page_size() * 4;
5893   char* result = os::reserve_memory_special(large_allocation_size, os::large_page_size(), NULL, false);
5894   if (result == NULL) {
5895     if (VerboseInternalVMTests) {
5896       gclog_or_tty->print("Failed to allocate control block with size "SIZE_FORMAT". Skipping remainder of test.",
5897                           large_allocation_size);
5898     }
5899   } else {
5900     os::release_memory_special(result, large_allocation_size);
5901 
5902     // allocate another page within the recently allocated memory area which seems to be a good location. At least
5903     // we managed to get it once.
5904     const size_t expected_allocation_size = os::large_page_size();
5905     char* expected_location = result + os::large_page_size();
5906     char* actual_location = os::reserve_memory_special(expected_allocation_size, os::large_page_size(), expected_location, false);
5907     if (actual_location == NULL) {
5908       if (VerboseInternalVMTests) {
5909         gclog_or_tty->print("Failed to allocate any memory at "PTR_FORMAT" size "SIZE_FORMAT". Skipping remainder of test.",
5910                             expected_location, large_allocation_size);
5911       }
5912     } else {
5913       // release memory
5914       os::release_memory_special(actual_location, expected_allocation_size);
5915       // only now check, after releasing any memory to avoid any leaks.
5916       assert(actual_location == expected_location,
5917              err_msg("Failed to allocate memory at requested location "PTR_FORMAT" of size "SIZE_FORMAT", is "PTR_FORMAT" instead",
5918              expected_location, expected_allocation_size, actual_location));
5919     }
5920   }
5921 
5922   // restore globals
5923   UseLargePagesIndividualAllocation = old_use_large_pages_individual_allocation;
5924   UseNUMAInterleaving = old_use_numa_interleaving;
5925 }
5926 #endif // PRODUCT
5927