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::unsetenv(const char* name) {
 157   assert(name != NULL, "Null pointer");
 158   return (SetEnvironmentVariable(name, NULL) == TRUE);
 159 }
 160 
 161 // No setuid programs under Windows.
 162 bool os::have_special_privileges() {
 163   return false;
 164 }
 165 
 166 
 167 // This method is  a periodic task to check for misbehaving JNI applications
 168 // under CheckJNI, we can add any periodic checks here.
 169 // For Windows at the moment does nothing
 170 void os::run_periodic_checks() {
 171   return;
 172 }
 173 
 174 // previous UnhandledExceptionFilter, if there is one
 175 static LPTOP_LEVEL_EXCEPTION_FILTER prev_uef_handler = NULL;
 176 
 177 LONG WINAPI Handle_FLT_Exception(struct _EXCEPTION_POINTERS* exceptionInfo);
 178 
 179 void os::init_system_properties_values() {
 180   // sysclasspath, java_home, dll_dir
 181   {
 182     char *home_path;
 183     char *dll_path;
 184     char *pslash;
 185     char *bin = "\\bin";
 186     char home_dir[MAX_PATH + 1];
 187     char *alt_home_dir = ::getenv("_ALT_JAVA_HOME_DIR");
 188 
 189     if (alt_home_dir != NULL)  {
 190       strncpy(home_dir, alt_home_dir, MAX_PATH + 1);
 191       home_dir[MAX_PATH] = '\0';
 192     } else {
 193       os::jvm_path(home_dir, sizeof(home_dir));
 194       // Found the full path to jvm.dll.
 195       // Now cut the path to <java_home>/jre if we can.
 196       *(strrchr(home_dir, '\\')) = '\0';  // get rid of \jvm.dll
 197       pslash = strrchr(home_dir, '\\');
 198       if (pslash != NULL) {
 199         *pslash = '\0';                   // get rid of \{client|server}
 200         pslash = strrchr(home_dir, '\\');
 201         if (pslash != NULL) {
 202           *pslash = '\0';                 // get rid of \bin
 203         }
 204       }
 205     }
 206 
 207     home_path = NEW_C_HEAP_ARRAY(char, strlen(home_dir) + 1, mtInternal);
 208     if (home_path == NULL) {
 209       return;
 210     }
 211     strcpy(home_path, home_dir);
 212     Arguments::set_java_home(home_path);
 213     FREE_C_HEAP_ARRAY(char, home_path);
 214 
 215     dll_path = NEW_C_HEAP_ARRAY(char, strlen(home_dir) + strlen(bin) + 1,
 216                                 mtInternal);
 217     if (dll_path == NULL) {
 218       return;
 219     }
 220     strcpy(dll_path, home_dir);
 221     strcat(dll_path, bin);
 222     Arguments::set_dll_dir(dll_path);
 223     FREE_C_HEAP_ARRAY(char, dll_path);
 224 
 225     if (!set_boot_path('\\', ';')) {
 226       return;
 227     }
 228   }
 229 
 230 // library_path
 231 #define EXT_DIR "\\lib\\ext"
 232 #define BIN_DIR "\\bin"
 233 #define PACKAGE_DIR "\\Sun\\Java"
 234   {
 235     // Win32 library search order (See the documentation for LoadLibrary):
 236     //
 237     // 1. The directory from which application is loaded.
 238     // 2. The system wide Java Extensions directory (Java only)
 239     // 3. System directory (GetSystemDirectory)
 240     // 4. Windows directory (GetWindowsDirectory)
 241     // 5. The PATH environment variable
 242     // 6. The current directory
 243 
 244     char *library_path;
 245     char tmp[MAX_PATH];
 246     char *path_str = ::getenv("PATH");
 247 
 248     library_path = NEW_C_HEAP_ARRAY(char, MAX_PATH * 5 + sizeof(PACKAGE_DIR) +
 249                                     sizeof(BIN_DIR) + (path_str ? strlen(path_str) : 0) + 10, mtInternal);
 250 
 251     library_path[0] = '\0';
 252 
 253     GetModuleFileName(NULL, tmp, sizeof(tmp));
 254     *(strrchr(tmp, '\\')) = '\0';
 255     strcat(library_path, tmp);
 256 
 257     GetWindowsDirectory(tmp, sizeof(tmp));
 258     strcat(library_path, ";");
 259     strcat(library_path, tmp);
 260     strcat(library_path, PACKAGE_DIR BIN_DIR);
 261 
 262     GetSystemDirectory(tmp, sizeof(tmp));
 263     strcat(library_path, ";");
 264     strcat(library_path, tmp);
 265 
 266     GetWindowsDirectory(tmp, sizeof(tmp));
 267     strcat(library_path, ";");
 268     strcat(library_path, tmp);
 269 
 270     if (path_str) {
 271       strcat(library_path, ";");
 272       strcat(library_path, path_str);
 273     }
 274 
 275     strcat(library_path, ";.");
 276 
 277     Arguments::set_library_path(library_path);
 278     FREE_C_HEAP_ARRAY(char, library_path);
 279   }
 280 
 281   // Default extensions directory
 282   {
 283     char path[MAX_PATH];
 284     char buf[2 * MAX_PATH + 2 * sizeof(EXT_DIR) + sizeof(PACKAGE_DIR) + 1];
 285     GetWindowsDirectory(path, MAX_PATH);
 286     sprintf(buf, "%s%s;%s%s%s", Arguments::get_java_home(), EXT_DIR,
 287             path, PACKAGE_DIR, EXT_DIR);
 288     Arguments::set_ext_dirs(buf);
 289   }
 290   #undef EXT_DIR
 291   #undef BIN_DIR
 292   #undef PACKAGE_DIR
 293 
 294 #ifndef _WIN64
 295   // set our UnhandledExceptionFilter and save any previous one
 296   prev_uef_handler = SetUnhandledExceptionFilter(Handle_FLT_Exception);
 297 #endif
 298 
 299   // Done
 300   return;
 301 }
 302 
 303 void os::breakpoint() {
 304   DebugBreak();
 305 }
 306 
 307 // Invoked from the BREAKPOINT Macro
 308 extern "C" void breakpoint() {
 309   os::breakpoint();
 310 }
 311 
 312 // RtlCaptureStackBackTrace Windows API may not exist prior to Windows XP.
 313 // So far, this method is only used by Native Memory Tracking, which is
 314 // only supported on Windows XP or later.
 315 //
 316 int os::get_native_stack(address* stack, int frames, int toSkip) {
 317 #ifdef _NMT_NOINLINE_
 318   toSkip++;
 319 #endif
 320   int captured = Kernel32Dll::RtlCaptureStackBackTrace(toSkip + 1, frames,
 321                                                        (PVOID*)stack, NULL);
 322   for (int index = captured; index < frames; index ++) {
 323     stack[index] = NULL;
 324   }
 325   return captured;
 326 }
 327 
 328 
 329 // os::current_stack_base()
 330 //
 331 //   Returns the base of the stack, which is the stack's
 332 //   starting address.  This function must be called
 333 //   while running on the stack of the thread being queried.
 334 
 335 address os::current_stack_base() {
 336   MEMORY_BASIC_INFORMATION minfo;
 337   address stack_bottom;
 338   size_t stack_size;
 339 
 340   VirtualQuery(&minfo, &minfo, sizeof(minfo));
 341   stack_bottom =  (address)minfo.AllocationBase;
 342   stack_size = minfo.RegionSize;
 343 
 344   // Add up the sizes of all the regions with the same
 345   // AllocationBase.
 346   while (1) {
 347     VirtualQuery(stack_bottom+stack_size, &minfo, sizeof(minfo));
 348     if (stack_bottom == (address)minfo.AllocationBase) {
 349       stack_size += minfo.RegionSize;
 350     } else {
 351       break;
 352     }
 353   }
 354 
 355 #ifdef _M_IA64
 356   // IA64 has memory and register stacks
 357   //
 358   // This is the stack layout you get on NT/IA64 if you specify 1MB stack limit
 359   // at thread creation (1MB backing store growing upwards, 1MB memory stack
 360   // growing downwards, 2MB summed up)
 361   //
 362   // ...
 363   // ------- top of stack (high address) -----
 364   // |
 365   // |      1MB
 366   // |      Backing Store (Register Stack)
 367   // |
 368   // |         / \
 369   // |          |
 370   // |          |
 371   // |          |
 372   // ------------------------ stack base -----
 373   // |      1MB
 374   // |      Memory Stack
 375   // |
 376   // |          |
 377   // |          |
 378   // |          |
 379   // |         \ /
 380   // |
 381   // ----- bottom of stack (low address) -----
 382   // ...
 383 
 384   stack_size = stack_size / 2;
 385 #endif
 386   return stack_bottom + stack_size;
 387 }
 388 
 389 size_t os::current_stack_size() {
 390   size_t sz;
 391   MEMORY_BASIC_INFORMATION minfo;
 392   VirtualQuery(&minfo, &minfo, sizeof(minfo));
 393   sz = (size_t)os::current_stack_base() - (size_t)minfo.AllocationBase;
 394   return sz;
 395 }
 396 
 397 struct tm* os::localtime_pd(const time_t* clock, struct tm* res) {
 398   const struct tm* time_struct_ptr = localtime(clock);
 399   if (time_struct_ptr != NULL) {
 400     *res = *time_struct_ptr;
 401     return res;
 402   }
 403   return NULL;
 404 }
 405 
 406 LONG WINAPI topLevelExceptionFilter(struct _EXCEPTION_POINTERS* exceptionInfo);
 407 
 408 // Thread start routine for all new Java threads
 409 static unsigned __stdcall java_start(Thread* thread) {
 410   // Try to randomize the cache line index of hot stack frames.
 411   // This helps when threads of the same stack traces evict each other's
 412   // cache lines. The threads can be either from the same JVM instance, or
 413   // from different JVM instances. The benefit is especially true for
 414   // processors with hyperthreading technology.
 415   static int counter = 0;
 416   int pid = os::current_process_id();
 417   _alloca(((pid ^ counter++) & 7) * 128);
 418 
 419   OSThread* osthr = thread->osthread();
 420   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 421 
 422   if (UseNUMA) {
 423     int lgrp_id = os::numa_get_group_id();
 424     if (lgrp_id != -1) {
 425       thread->set_lgrp_id(lgrp_id);
 426     }
 427   }
 428 
 429   // Diagnostic code to investigate JDK-6573254
 430   int res = 30115;  // non-java thread
 431   if (thread->is_Java_thread()) {
 432     res = 20115;    // java thread
 433   }
 434 
 435   // Install a win32 structured exception handler around every thread created
 436   // by VM, so VM can generate error dump when an exception occurred in non-
 437   // Java thread (e.g. VM thread).
 438   __try {
 439     thread->run();
 440   } __except(topLevelExceptionFilter(
 441                                      (_EXCEPTION_POINTERS*)_exception_info())) {
 442     // Nothing to do.
 443   }
 444 
 445   // One less thread is executing
 446   // When the VMThread gets here, the main thread may have already exited
 447   // which frees the CodeHeap containing the Atomic::add code
 448   if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
 449     Atomic::dec_ptr((intptr_t*)&os::win32::_os_thread_count);
 450   }
 451 
 452   // Thread must not return from exit_process_or_thread(), but if it does,
 453   // let it proceed to exit normally
 454   return (unsigned)os::win32::exit_process_or_thread(os::win32::EPT_THREAD, res);
 455 }
 456 
 457 static OSThread* create_os_thread(Thread* thread, HANDLE thread_handle,
 458                                   int thread_id) {
 459   // Allocate the OSThread object
 460   OSThread* osthread = new OSThread(NULL, NULL);
 461   if (osthread == NULL) return NULL;
 462 
 463   // Initialize support for Java interrupts
 464   HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
 465   if (interrupt_event == NULL) {
 466     delete osthread;
 467     return NULL;
 468   }
 469   osthread->set_interrupt_event(interrupt_event);
 470 
 471   // Store info on the Win32 thread into the OSThread
 472   osthread->set_thread_handle(thread_handle);
 473   osthread->set_thread_id(thread_id);
 474 
 475   if (UseNUMA) {
 476     int lgrp_id = os::numa_get_group_id();
 477     if (lgrp_id != -1) {
 478       thread->set_lgrp_id(lgrp_id);
 479     }
 480   }
 481 
 482   // Initial thread state is INITIALIZED, not SUSPENDED
 483   osthread->set_state(INITIALIZED);
 484 
 485   return osthread;
 486 }
 487 
 488 
 489 bool os::create_attached_thread(JavaThread* thread) {
 490 #ifdef ASSERT
 491   thread->verify_not_published();
 492 #endif
 493   HANDLE thread_h;
 494   if (!DuplicateHandle(main_process, GetCurrentThread(), GetCurrentProcess(),
 495                        &thread_h, THREAD_ALL_ACCESS, false, 0)) {
 496     fatal("DuplicateHandle failed\n");
 497   }
 498   OSThread* osthread = create_os_thread(thread, thread_h,
 499                                         (int)current_thread_id());
 500   if (osthread == NULL) {
 501     return false;
 502   }
 503 
 504   // Initial thread state is RUNNABLE
 505   osthread->set_state(RUNNABLE);
 506 
 507   thread->set_osthread(osthread);
 508   return true;
 509 }
 510 
 511 bool os::create_main_thread(JavaThread* thread) {
 512 #ifdef ASSERT
 513   thread->verify_not_published();
 514 #endif
 515   if (_starting_thread == NULL) {
 516     _starting_thread = create_os_thread(thread, main_thread, main_thread_id);
 517     if (_starting_thread == NULL) {
 518       return false;
 519     }
 520   }
 521 
 522   // The primordial thread is runnable from the start)
 523   _starting_thread->set_state(RUNNABLE);
 524 
 525   thread->set_osthread(_starting_thread);
 526   return true;
 527 }
 528 
 529 // Allocate and initialize a new OSThread
 530 bool os::create_thread(Thread* thread, ThreadType thr_type,
 531                        size_t stack_size) {
 532   unsigned thread_id;
 533 
 534   // Allocate the OSThread object
 535   OSThread* osthread = new OSThread(NULL, NULL);
 536   if (osthread == NULL) {
 537     return false;
 538   }
 539 
 540   // Initialize support for Java interrupts
 541   HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);
 542   if (interrupt_event == NULL) {
 543     delete osthread;
 544     return NULL;
 545   }
 546   osthread->set_interrupt_event(interrupt_event);
 547   osthread->set_interrupted(false);
 548 
 549   thread->set_osthread(osthread);
 550 
 551   if (stack_size == 0) {
 552     switch (thr_type) {
 553     case os::java_thread:
 554       // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
 555       if (JavaThread::stack_size_at_create() > 0) {
 556         stack_size = JavaThread::stack_size_at_create();
 557       }
 558       break;
 559     case os::compiler_thread:
 560       if (CompilerThreadStackSize > 0) {
 561         stack_size = (size_t)(CompilerThreadStackSize * K);
 562         break;
 563       } // else fall through:
 564         // use VMThreadStackSize if CompilerThreadStackSize is not defined
 565     case os::vm_thread:
 566     case os::pgc_thread:
 567     case os::cgc_thread:
 568     case os::watcher_thread:
 569       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
 570       break;
 571     }
 572   }
 573 
 574   // Create the Win32 thread
 575   //
 576   // Contrary to what MSDN document says, "stack_size" in _beginthreadex()
 577   // does not specify stack size. Instead, it specifies the size of
 578   // initially committed space. The stack size is determined by
 579   // PE header in the executable. If the committed "stack_size" is larger
 580   // than default value in the PE header, the stack is rounded up to the
 581   // nearest multiple of 1MB. For example if the launcher has default
 582   // stack size of 320k, specifying any size less than 320k does not
 583   // affect the actual stack size at all, it only affects the initial
 584   // commitment. On the other hand, specifying 'stack_size' larger than
 585   // default value may cause significant increase in memory usage, because
 586   // not only the stack space will be rounded up to MB, but also the
 587   // entire space is committed upfront.
 588   //
 589   // Finally Windows XP added a new flag 'STACK_SIZE_PARAM_IS_A_RESERVATION'
 590   // for CreateThread() that can treat 'stack_size' as stack size. However we
 591   // are not supposed to call CreateThread() directly according to MSDN
 592   // document because JVM uses C runtime library. The good news is that the
 593   // flag appears to work with _beginthredex() as well.
 594 
 595 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
 596   #define STACK_SIZE_PARAM_IS_A_RESERVATION  (0x10000)
 597 #endif
 598 
 599   HANDLE thread_handle =
 600     (HANDLE)_beginthreadex(NULL,
 601                            (unsigned)stack_size,
 602                            (unsigned (__stdcall *)(void*)) java_start,
 603                            thread,
 604                            CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
 605                            &thread_id);
 606   if (thread_handle == NULL) {
 607     // perhaps STACK_SIZE_PARAM_IS_A_RESERVATION is not supported, try again
 608     // without the flag.
 609     thread_handle =
 610       (HANDLE)_beginthreadex(NULL,
 611                              (unsigned)stack_size,
 612                              (unsigned (__stdcall *)(void*)) java_start,
 613                              thread,
 614                              CREATE_SUSPENDED,
 615                              &thread_id);
 616   }
 617   if (thread_handle == NULL) {
 618     // Need to clean up stuff we've allocated so far
 619     CloseHandle(osthread->interrupt_event());
 620     thread->set_osthread(NULL);
 621     delete osthread;
 622     return NULL;
 623   }
 624 
 625   Atomic::inc_ptr((intptr_t*)&os::win32::_os_thread_count);
 626 
 627   // Store info on the Win32 thread into the OSThread
 628   osthread->set_thread_handle(thread_handle);
 629   osthread->set_thread_id(thread_id);
 630 
 631   // Initial thread state is INITIALIZED, not SUSPENDED
 632   osthread->set_state(INITIALIZED);
 633 
 634   // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
 635   return true;
 636 }
 637 
 638 
 639 // Free Win32 resources related to the OSThread
 640 void os::free_thread(OSThread* osthread) {
 641   assert(osthread != NULL, "osthread not set");
 642   CloseHandle(osthread->thread_handle());
 643   CloseHandle(osthread->interrupt_event());
 644   delete osthread;
 645 }
 646 
 647 static jlong first_filetime;
 648 static jlong initial_performance_count;
 649 static jlong performance_frequency;
 650 
 651 
 652 jlong as_long(LARGE_INTEGER x) {
 653   jlong result = 0; // initialization to avoid warning
 654   set_high(&result, x.HighPart);
 655   set_low(&result, x.LowPart);
 656   return result;
 657 }
 658 
 659 
 660 jlong os::elapsed_counter() {
 661   LARGE_INTEGER count;
 662   if (win32::_has_performance_count) {
 663     QueryPerformanceCounter(&count);
 664     return as_long(count) - initial_performance_count;
 665   } else {
 666     FILETIME wt;
 667     GetSystemTimeAsFileTime(&wt);
 668     return (jlong_from(wt.dwHighDateTime, wt.dwLowDateTime) - first_filetime);
 669   }
 670 }
 671 
 672 
 673 jlong os::elapsed_frequency() {
 674   if (win32::_has_performance_count) {
 675     return performance_frequency;
 676   } else {
 677     // the FILETIME time is the number of 100-nanosecond intervals since January 1,1601.
 678     return 10000000;
 679   }
 680 }
 681 
 682 
 683 julong os::available_memory() {
 684   return win32::available_memory();
 685 }
 686 
 687 julong os::win32::available_memory() {
 688   // Use GlobalMemoryStatusEx() because GlobalMemoryStatus() may return incorrect
 689   // value if total memory is larger than 4GB
 690   MEMORYSTATUSEX ms;
 691   ms.dwLength = sizeof(ms);
 692   GlobalMemoryStatusEx(&ms);
 693 
 694   return (julong)ms.ullAvailPhys;
 695 }
 696 
 697 julong os::physical_memory() {
 698   return win32::physical_memory();
 699 }
 700 
 701 bool os::has_allocatable_memory_limit(julong* limit) {
 702   MEMORYSTATUSEX ms;
 703   ms.dwLength = sizeof(ms);
 704   GlobalMemoryStatusEx(&ms);
 705 #ifdef _LP64
 706   *limit = (julong)ms.ullAvailVirtual;
 707   return true;
 708 #else
 709   // Limit to 1400m because of the 2gb address space wall
 710   *limit = MIN2((julong)1400*M, (julong)ms.ullAvailVirtual);
 711   return true;
 712 #endif
 713 }
 714 
 715 // VC6 lacks DWORD_PTR
 716 #if _MSC_VER < 1300
 717 typedef UINT_PTR DWORD_PTR;
 718 #endif
 719 
 720 int os::active_processor_count() {
 721   DWORD_PTR lpProcessAffinityMask = 0;
 722   DWORD_PTR lpSystemAffinityMask = 0;
 723   int proc_count = processor_count();
 724   if (proc_count <= sizeof(UINT_PTR) * BitsPerByte &&
 725       GetProcessAffinityMask(GetCurrentProcess(), &lpProcessAffinityMask, &lpSystemAffinityMask)) {
 726     // Nof active processors is number of bits in process affinity mask
 727     int bitcount = 0;
 728     while (lpProcessAffinityMask != 0) {
 729       lpProcessAffinityMask = lpProcessAffinityMask & (lpProcessAffinityMask-1);
 730       bitcount++;
 731     }
 732     return bitcount;
 733   } else {
 734     return proc_count;
 735   }
 736 }
 737 
 738 void os::set_native_thread_name(const char *name) {
 739 
 740   // See: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
 741   //
 742   // Note that unfortunately this only works if the process
 743   // is already attached to a debugger; debugger must observe
 744   // the exception below to show the correct name.
 745 
 746   const DWORD MS_VC_EXCEPTION = 0x406D1388;
 747   struct {
 748     DWORD dwType;     // must be 0x1000
 749     LPCSTR szName;    // pointer to name (in user addr space)
 750     DWORD dwThreadID; // thread ID (-1=caller thread)
 751     DWORD dwFlags;    // reserved for future use, must be zero
 752   } info;
 753 
 754   info.dwType = 0x1000;
 755   info.szName = name;
 756   info.dwThreadID = -1;
 757   info.dwFlags = 0;
 758 
 759   __try {
 760     RaiseException (MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (const ULONG_PTR*)&info );
 761   } __except(EXCEPTION_CONTINUE_EXECUTION) {}
 762 }
 763 
 764 bool os::distribute_processes(uint length, uint* distribution) {
 765   // Not yet implemented.
 766   return false;
 767 }
 768 
 769 bool os::bind_to_processor(uint processor_id) {
 770   // Not yet implemented.
 771   return false;
 772 }
 773 
 774 void os::win32::initialize_performance_counter() {
 775   LARGE_INTEGER count;
 776   if (QueryPerformanceFrequency(&count)) {
 777     win32::_has_performance_count = 1;
 778     performance_frequency = as_long(count);
 779     QueryPerformanceCounter(&count);
 780     initial_performance_count = as_long(count);
 781   } else {
 782     win32::_has_performance_count = 0;
 783     FILETIME wt;
 784     GetSystemTimeAsFileTime(&wt);
 785     first_filetime = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
 786   }
 787 }
 788 
 789 
 790 double os::elapsedTime() {
 791   return (double) elapsed_counter() / (double) elapsed_frequency();
 792 }
 793 
 794 
 795 // Windows format:
 796 //   The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.
 797 // Java format:
 798 //   Java standards require the number of milliseconds since 1/1/1970
 799 
 800 // Constant offset - calculated using offset()
 801 static jlong  _offset   = 116444736000000000;
 802 // Fake time counter for reproducible results when debugging
 803 static jlong  fake_time = 0;
 804 
 805 #ifdef ASSERT
 806 // Just to be safe, recalculate the offset in debug mode
 807 static jlong _calculated_offset = 0;
 808 static int   _has_calculated_offset = 0;
 809 
 810 jlong offset() {
 811   if (_has_calculated_offset) return _calculated_offset;
 812   SYSTEMTIME java_origin;
 813   java_origin.wYear          = 1970;
 814   java_origin.wMonth         = 1;
 815   java_origin.wDayOfWeek     = 0; // ignored
 816   java_origin.wDay           = 1;
 817   java_origin.wHour          = 0;
 818   java_origin.wMinute        = 0;
 819   java_origin.wSecond        = 0;
 820   java_origin.wMilliseconds  = 0;
 821   FILETIME jot;
 822   if (!SystemTimeToFileTime(&java_origin, &jot)) {
 823     fatal(err_msg("Error = %d\nWindows error", GetLastError()));
 824   }
 825   _calculated_offset = jlong_from(jot.dwHighDateTime, jot.dwLowDateTime);
 826   _has_calculated_offset = 1;
 827   assert(_calculated_offset == _offset, "Calculated and constant time offsets must be equal");
 828   return _calculated_offset;
 829 }
 830 #else
 831 jlong offset() {
 832   return _offset;
 833 }
 834 #endif
 835 
 836 jlong windows_to_java_time(FILETIME wt) {
 837   jlong a = jlong_from(wt.dwHighDateTime, wt.dwLowDateTime);
 838   return (a - offset()) / 10000;
 839 }
 840 
 841 // Returns time ticks in (10th of micro seconds)
 842 jlong windows_to_time_ticks(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 ticks = windows_to_time_ticks(wt); // 10th of micros
 886   jlong secs = jlong(ticks / 10000000); // 10000 * 1000
 887   seconds = secs;
 888   nanos = jlong(ticks - (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 10000:
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     static volatile jint process_exiting = 0;
3809     int i, j;
3810     DWORD res;
3811     HANDLE hproc, hthr;
3812 
3813     // The first thread that reached this point, initializes the critical section.
3814     if (!InitOnceExecuteOnce(&init_once_crit_sect, init_crit_sect_call, &crit_sect, NULL)) {
3815       warning("crit_sect initialization failed in %s: %d\n", __FILE__, __LINE__);
3816     } else if (OrderAccess::load_acquire(&process_exiting) == 0) {
3817       EnterCriticalSection(&crit_sect);
3818 
3819       if (what == EPT_THREAD && OrderAccess::load_acquire(&process_exiting) == 0) {
3820         // Remove from the array those handles of the threads that have completed exiting.
3821         for (i = 0, j = 0; i < handle_count; ++i) {
3822           res = WaitForSingleObject(handles[i], 0 /* don't wait */);
3823           if (res == WAIT_TIMEOUT) {
3824             handles[j++] = handles[i];
3825           } else {
3826             if (res == WAIT_FAILED) {
3827               warning("WaitForSingleObject failed (%u) in %s: %d\n",
3828                       GetLastError(), __FILE__, __LINE__);
3829             }
3830             // Don't keep the handle, if we failed waiting for it.
3831             CloseHandle(handles[i]);
3832           }
3833         }
3834 
3835         // If there's no free slot in the array of the kept handles, we'll have to
3836         // wait until at least one thread completes exiting.
3837         if ((handle_count = j) == MAXIMUM_WAIT_OBJECTS) {
3838           // Raise the priority of the oldest exiting thread to increase its chances
3839           // to complete sooner.
3840           SetThreadPriority(handles[0], THREAD_PRIORITY_ABOVE_NORMAL);
3841           res = WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, handles, FALSE, EXIT_TIMEOUT);
3842           if (res >= WAIT_OBJECT_0 && res < (WAIT_OBJECT_0 + MAXIMUM_WAIT_OBJECTS)) {
3843             i = (res - WAIT_OBJECT_0);
3844             handle_count = MAXIMUM_WAIT_OBJECTS - 1;
3845             for (; i < handle_count; ++i) {
3846               handles[i] = handles[i + 1];
3847             }
3848           } else {
3849             warning("WaitForMultipleObjects %s (%u) in %s: %d\n",
3850                     (res == WAIT_FAILED ? "failed" : "timed out"),
3851                     GetLastError(), __FILE__, __LINE__);
3852             // Don't keep handles, if we failed waiting for them.
3853             for (i = 0; i < MAXIMUM_WAIT_OBJECTS; ++i) {
3854               CloseHandle(handles[i]);
3855             }
3856             handle_count = 0;
3857           }
3858         }
3859 
3860         // Store a duplicate of the current thread handle in the array of handles.
3861         hproc = GetCurrentProcess();
3862         hthr = GetCurrentThread();
3863         if (!DuplicateHandle(hproc, hthr, hproc, &handles[handle_count],
3864                              0, FALSE, DUPLICATE_SAME_ACCESS)) {
3865           warning("DuplicateHandle failed (%u) in %s: %d\n",
3866                   GetLastError(), __FILE__, __LINE__);
3867         } else {
3868           ++handle_count;
3869         }
3870 
3871         // The current exiting thread has stored its handle in the array, and now
3872         // should leave the critical section before calling _endthreadex().
3873 
3874       } else if (what != EPT_THREAD) {
3875         if (handle_count > 0) {
3876           // Before ending the process, make sure all the threads that had called
3877           // _endthreadex() completed.
3878 
3879           // Set the priority level of the current thread to the same value as
3880           // the priority level of exiting threads.
3881           // This is to ensure it will be given a fair chance to execute if
3882           // the timeout expires.
3883           hthr = GetCurrentThread();
3884           SetThreadPriority(hthr, THREAD_PRIORITY_ABOVE_NORMAL);
3885           for (i = 0; i < handle_count; ++i) {
3886             SetThreadPriority(handles[i], THREAD_PRIORITY_ABOVE_NORMAL);
3887           }
3888           res = WaitForMultipleObjects(handle_count, handles, TRUE, EXIT_TIMEOUT);
3889           if (res == WAIT_FAILED || res == WAIT_TIMEOUT) {
3890             warning("WaitForMultipleObjects %s (%u) in %s: %d\n",
3891                     (res == WAIT_FAILED ? "failed" : "timed out"),
3892                     GetLastError(), __FILE__, __LINE__);
3893           }
3894           for (i = 0; i < handle_count; ++i) {
3895             CloseHandle(handles[i]);
3896           }
3897           handle_count = 0;
3898         }
3899 
3900         OrderAccess::release_store(&process_exiting, 1);
3901       }
3902 
3903       LeaveCriticalSection(&crit_sect);
3904     }
3905 
3906     if (what == EPT_THREAD) {
3907       while (OrderAccess::load_acquire(&process_exiting) != 0) {
3908         // Some other thread is about to call exit(), so we
3909         // don't let the current thread proceed to _endthreadex()
3910         SuspendThread(GetCurrentThread());
3911         // Avoid busy-wait loop, if SuspendThread() failed.
3912         Sleep(EXIT_TIMEOUT);
3913       }
3914     }
3915   }
3916 
3917   // We are here if either
3918   // - there's no 'race at exit' bug on this OS release;
3919   // - initialization of the critical section failed (unlikely);
3920   // - the current thread has stored its handle and left the critical section;
3921   // - the process-exiting thread has raised the flag and left the critical section.
3922   if (what == EPT_THREAD) {
3923     _endthreadex((unsigned)exit_code);
3924   } else if (what == EPT_PROCESS) {
3925     ::exit(exit_code);
3926   } else {
3927     _exit(exit_code);
3928   }
3929 
3930   // Should not reach here
3931   return exit_code;
3932 }
3933 
3934 #undef EXIT_TIMEOUT
3935 
3936 void os::win32::setmode_streams() {
3937   _setmode(_fileno(stdin), _O_BINARY);
3938   _setmode(_fileno(stdout), _O_BINARY);
3939   _setmode(_fileno(stderr), _O_BINARY);
3940 }
3941 
3942 
3943 bool os::is_debugger_attached() {
3944   return IsDebuggerPresent() ? true : false;
3945 }
3946 
3947 
3948 void os::wait_for_keypress_at_exit(void) {
3949   if (PauseAtExit) {
3950     fprintf(stderr, "Press any key to continue...\n");
3951     fgetc(stdin);
3952   }
3953 }
3954 
3955 
3956 int os::message_box(const char* title, const char* message) {
3957   int result = MessageBox(NULL, message, title,
3958                           MB_YESNO | MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFAULT_DESKTOP_ONLY);
3959   return result == IDYES;
3960 }
3961 
3962 int os::allocate_thread_local_storage() {
3963   return TlsAlloc();
3964 }
3965 
3966 
3967 void os::free_thread_local_storage(int index) {
3968   TlsFree(index);
3969 }
3970 
3971 
3972 void os::thread_local_storage_at_put(int index, void* value) {
3973   TlsSetValue(index, value);
3974   assert(thread_local_storage_at(index) == value, "Just checking");
3975 }
3976 
3977 
3978 void* os::thread_local_storage_at(int index) {
3979   return TlsGetValue(index);
3980 }
3981 
3982 
3983 #ifndef PRODUCT
3984 #ifndef _WIN64
3985 // Helpers to check whether NX protection is enabled
3986 int nx_exception_filter(_EXCEPTION_POINTERS *pex) {
3987   if (pex->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
3988       pex->ExceptionRecord->NumberParameters > 0 &&
3989       pex->ExceptionRecord->ExceptionInformation[0] ==
3990       EXCEPTION_INFO_EXEC_VIOLATION) {
3991     return EXCEPTION_EXECUTE_HANDLER;
3992   }
3993   return EXCEPTION_CONTINUE_SEARCH;
3994 }
3995 
3996 void nx_check_protection() {
3997   // If NX is enabled we'll get an exception calling into code on the stack
3998   char code[] = { (char)0xC3 }; // ret
3999   void *code_ptr = (void *)code;
4000   __try {
4001     __asm call code_ptr
4002   } __except(nx_exception_filter((_EXCEPTION_POINTERS*)_exception_info())) {
4003     tty->print_raw_cr("NX protection detected.");
4004   }
4005 }
4006 #endif // _WIN64
4007 #endif // PRODUCT
4008 
4009 // this is called _before_ the global arguments have been parsed
4010 void os::init(void) {
4011   _initial_pid = _getpid();
4012 
4013   init_random(1234567);
4014 
4015   win32::initialize_system_info();
4016   win32::setmode_streams();
4017   init_page_sizes((size_t) win32::vm_page_size());
4018 
4019   // This may be overridden later when argument processing is done.
4020   FLAG_SET_ERGO(bool, UseLargePagesIndividualAllocation,
4021                 os::win32::is_windows_2003());
4022 
4023   // Initialize main_process and main_thread
4024   main_process = GetCurrentProcess();  // Remember main_process is a pseudo handle
4025   if (!DuplicateHandle(main_process, GetCurrentThread(), main_process,
4026                        &main_thread, THREAD_ALL_ACCESS, false, 0)) {
4027     fatal("DuplicateHandle failed\n");
4028   }
4029   main_thread_id = (int) GetCurrentThreadId();
4030 }
4031 
4032 // To install functions for atexit processing
4033 extern "C" {
4034   static void perfMemory_exit_helper() {
4035     perfMemory_exit();
4036   }
4037 }
4038 
4039 static jint initSock();
4040 
4041 // this is called _after_ the global arguments have been parsed
4042 jint os::init_2(void) {
4043   // Allocate a single page and mark it as readable for safepoint polling
4044   address polling_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_READONLY);
4045   guarantee(polling_page != NULL, "Reserve Failed for polling page");
4046 
4047   address return_page  = (address)VirtualAlloc(polling_page, os::vm_page_size(), MEM_COMMIT, PAGE_READONLY);
4048   guarantee(return_page != NULL, "Commit Failed for polling page");
4049 
4050   os::set_polling_page(polling_page);
4051 
4052 #ifndef PRODUCT
4053   if (Verbose && PrintMiscellaneous) {
4054     tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n",
4055                (intptr_t)polling_page);
4056   }
4057 #endif
4058 
4059   if (!UseMembar) {
4060     address mem_serialize_page = (address)VirtualAlloc(NULL, os::vm_page_size(), MEM_RESERVE, PAGE_READWRITE);
4061     guarantee(mem_serialize_page != NULL, "Reserve Failed for memory serialize page");
4062 
4063     return_page  = (address)VirtualAlloc(mem_serialize_page, os::vm_page_size(), MEM_COMMIT, PAGE_READWRITE);
4064     guarantee(return_page != NULL, "Commit Failed for memory serialize page");
4065 
4066     os::set_memory_serialize_page(mem_serialize_page);
4067 
4068 #ifndef PRODUCT
4069     if (Verbose && PrintMiscellaneous) {
4070       tty->print("[Memory Serialize  Page address: " INTPTR_FORMAT "]\n",
4071                  (intptr_t)mem_serialize_page);
4072     }
4073 #endif
4074   }
4075 
4076   // Setup Windows Exceptions
4077 
4078   // for debugging float code generation bugs
4079   if (ForceFloatExceptions) {
4080 #ifndef  _WIN64
4081     static long fp_control_word = 0;
4082     __asm { fstcw fp_control_word }
4083     // see Intel PPro Manual, Vol. 2, p 7-16
4084     const long precision = 0x20;
4085     const long underflow = 0x10;
4086     const long overflow  = 0x08;
4087     const long zero_div  = 0x04;
4088     const long denorm    = 0x02;
4089     const long invalid   = 0x01;
4090     fp_control_word |= invalid;
4091     __asm { fldcw fp_control_word }
4092 #endif
4093   }
4094 
4095   // If stack_commit_size is 0, windows will reserve the default size,
4096   // but only commit a small portion of it.
4097   size_t stack_commit_size = round_to(ThreadStackSize*K, os::vm_page_size());
4098   size_t default_reserve_size = os::win32::default_stack_size();
4099   size_t actual_reserve_size = stack_commit_size;
4100   if (stack_commit_size < default_reserve_size) {
4101     // If stack_commit_size == 0, we want this too
4102     actual_reserve_size = default_reserve_size;
4103   }
4104 
4105   // Check minimum allowable stack size for thread creation and to initialize
4106   // the java system classes, including StackOverflowError - depends on page
4107   // size.  Add a page for compiler2 recursion in main thread.
4108   // Add in 2*BytesPerWord times page size to account for VM stack during
4109   // class initialization depending on 32 or 64 bit VM.
4110   size_t min_stack_allowed =
4111             (size_t)(StackYellowPages+StackRedPages+StackShadowPages+
4112                      2*BytesPerWord COMPILER2_PRESENT(+1)) * os::vm_page_size();
4113   if (actual_reserve_size < min_stack_allowed) {
4114     tty->print_cr("\nThe stack size specified is too small, "
4115                   "Specify at least %dk",
4116                   min_stack_allowed / K);
4117     return JNI_ERR;
4118   }
4119 
4120   JavaThread::set_stack_size_at_create(stack_commit_size);
4121 
4122   // Calculate theoretical max. size of Threads to guard gainst artifical
4123   // out-of-memory situations, where all available address-space has been
4124   // reserved by thread stacks.
4125   assert(actual_reserve_size != 0, "Must have a stack");
4126 
4127   // Calculate the thread limit when we should start doing Virtual Memory
4128   // banging. Currently when the threads will have used all but 200Mb of space.
4129   //
4130   // TODO: consider performing a similar calculation for commit size instead
4131   // as reserve size, since on a 64-bit platform we'll run into that more
4132   // often than running out of virtual memory space.  We can use the
4133   // lower value of the two calculations as the os_thread_limit.
4134   size_t max_address_space = ((size_t)1 << (BitsPerWord - 1)) - (200 * K * K);
4135   win32::_os_thread_limit = (intx)(max_address_space / actual_reserve_size);
4136 
4137   // at exit methods are called in the reverse order of their registration.
4138   // there is no limit to the number of functions registered. atexit does
4139   // not set errno.
4140 
4141   if (PerfAllowAtExitRegistration) {
4142     // only register atexit functions if PerfAllowAtExitRegistration is set.
4143     // atexit functions can be delayed until process exit time, which
4144     // can be problematic for embedded VM situations. Embedded VMs should
4145     // call DestroyJavaVM() to assure that VM resources are released.
4146 
4147     // note: perfMemory_exit_helper atexit function may be removed in
4148     // the future if the appropriate cleanup code can be added to the
4149     // VM_Exit VMOperation's doit method.
4150     if (atexit(perfMemory_exit_helper) != 0) {
4151       warning("os::init_2 atexit(perfMemory_exit_helper) failed");
4152     }
4153   }
4154 
4155 #ifndef _WIN64
4156   // Print something if NX is enabled (win32 on AMD64)
4157   NOT_PRODUCT(if (PrintMiscellaneous && Verbose) nx_check_protection());
4158 #endif
4159 
4160   // initialize thread priority policy
4161   prio_init();
4162 
4163   if (UseNUMA && !ForceNUMA) {
4164     UseNUMA = false; // We don't fully support this yet
4165   }
4166 
4167   if (UseNUMAInterleaving) {
4168     // first check whether this Windows OS supports VirtualAllocExNuma, if not ignore this flag
4169     bool success = numa_interleaving_init();
4170     if (!success) UseNUMAInterleaving = false;
4171   }
4172 
4173   if (initSock() != JNI_OK) {
4174     return JNI_ERR;
4175   }
4176 
4177   return JNI_OK;
4178 }
4179 
4180 // Mark the polling page as unreadable
4181 void os::make_polling_page_unreadable(void) {
4182   DWORD old_status;
4183   if (!VirtualProtect((char *)_polling_page, os::vm_page_size(),
4184                       PAGE_NOACCESS, &old_status)) {
4185     fatal("Could not disable polling page");
4186   }
4187 }
4188 
4189 // Mark the polling page as readable
4190 void os::make_polling_page_readable(void) {
4191   DWORD old_status;
4192   if (!VirtualProtect((char *)_polling_page, os::vm_page_size(),
4193                       PAGE_READONLY, &old_status)) {
4194     fatal("Could not enable polling page");
4195   }
4196 }
4197 
4198 
4199 int os::stat(const char *path, struct stat *sbuf) {
4200   char pathbuf[MAX_PATH];
4201   if (strlen(path) > MAX_PATH - 1) {
4202     errno = ENAMETOOLONG;
4203     return -1;
4204   }
4205   os::native_path(strcpy(pathbuf, path));
4206   int ret = ::stat(pathbuf, sbuf);
4207   if (sbuf != NULL && UseUTCFileTimestamp) {
4208     // Fix for 6539723.  st_mtime returned from stat() is dependent on
4209     // the system timezone and so can return different values for the
4210     // same file if/when daylight savings time changes.  This adjustment
4211     // makes sure the same timestamp is returned regardless of the TZ.
4212     //
4213     // See:
4214     // http://msdn.microsoft.com/library/
4215     //   default.asp?url=/library/en-us/sysinfo/base/
4216     //   time_zone_information_str.asp
4217     // and
4218     // http://msdn.microsoft.com/library/default.asp?url=
4219     //   /library/en-us/sysinfo/base/settimezoneinformation.asp
4220     //
4221     // NOTE: there is a insidious bug here:  If the timezone is changed
4222     // after the call to stat() but before 'GetTimeZoneInformation()', then
4223     // the adjustment we do here will be wrong and we'll return the wrong
4224     // value (which will likely end up creating an invalid class data
4225     // archive).  Absent a better API for this, or some time zone locking
4226     // mechanism, we'll have to live with this risk.
4227     TIME_ZONE_INFORMATION tz;
4228     DWORD tzid = GetTimeZoneInformation(&tz);
4229     int daylightBias =
4230       (tzid == TIME_ZONE_ID_DAYLIGHT) ?  tz.DaylightBias : tz.StandardBias;
4231     sbuf->st_mtime += (tz.Bias + daylightBias) * 60;
4232   }
4233   return ret;
4234 }
4235 
4236 
4237 #define FT2INT64(ft) \
4238   ((jlong)((jlong)(ft).dwHighDateTime << 32 | (julong)(ft).dwLowDateTime))
4239 
4240 
4241 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
4242 // are used by JVM M&M and JVMTI to get user+sys or user CPU time
4243 // of a thread.
4244 //
4245 // current_thread_cpu_time() and thread_cpu_time(Thread*) returns
4246 // the fast estimate available on the platform.
4247 
4248 // current_thread_cpu_time() is not optimized for Windows yet
4249 jlong os::current_thread_cpu_time() {
4250   // return user + sys since the cost is the same
4251   return os::thread_cpu_time(Thread::current(), true /* user+sys */);
4252 }
4253 
4254 jlong os::thread_cpu_time(Thread* thread) {
4255   // consistent with what current_thread_cpu_time() returns.
4256   return os::thread_cpu_time(thread, true /* user+sys */);
4257 }
4258 
4259 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
4260   return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
4261 }
4262 
4263 jlong os::thread_cpu_time(Thread* thread, bool user_sys_cpu_time) {
4264   // This code is copy from clasic VM -> hpi::sysThreadCPUTime
4265   // If this function changes, os::is_thread_cpu_time_supported() should too
4266   if (os::win32::is_nt()) {
4267     FILETIME CreationTime;
4268     FILETIME ExitTime;
4269     FILETIME KernelTime;
4270     FILETIME UserTime;
4271 
4272     if (GetThreadTimes(thread->osthread()->thread_handle(), &CreationTime,
4273                        &ExitTime, &KernelTime, &UserTime) == 0) {
4274       return -1;
4275     } else if (user_sys_cpu_time) {
4276       return (FT2INT64(UserTime) + FT2INT64(KernelTime)) * 100;
4277     } else {
4278       return FT2INT64(UserTime) * 100;
4279     }
4280   } else {
4281     return (jlong) timeGetTime() * 1000000;
4282   }
4283 }
4284 
4285 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4286   info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
4287   info_ptr->may_skip_backward = false;      // GetThreadTimes returns absolute time
4288   info_ptr->may_skip_forward = false;       // GetThreadTimes returns absolute time
4289   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
4290 }
4291 
4292 void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
4293   info_ptr->max_value = ALL_64_BITS;        // the max value -- all 64 bits
4294   info_ptr->may_skip_backward = false;      // GetThreadTimes returns absolute time
4295   info_ptr->may_skip_forward = false;       // GetThreadTimes returns absolute time
4296   info_ptr->kind = JVMTI_TIMER_TOTAL_CPU;   // user+system time is returned
4297 }
4298 
4299 bool os::is_thread_cpu_time_supported() {
4300   // see os::thread_cpu_time
4301   if (os::win32::is_nt()) {
4302     FILETIME CreationTime;
4303     FILETIME ExitTime;
4304     FILETIME KernelTime;
4305     FILETIME UserTime;
4306 
4307     if (GetThreadTimes(GetCurrentThread(), &CreationTime, &ExitTime,
4308                        &KernelTime, &UserTime) == 0) {
4309       return false;
4310     } else {
4311       return true;
4312     }
4313   } else {
4314     return false;
4315   }
4316 }
4317 
4318 // Windows does't provide a loadavg primitive so this is stubbed out for now.
4319 // It does have primitives (PDH API) to get CPU usage and run queue length.
4320 // "\\Processor(_Total)\\% Processor Time", "\\System\\Processor Queue Length"
4321 // If we wanted to implement loadavg on Windows, we have a few options:
4322 //
4323 // a) Query CPU usage and run queue length and "fake" an answer by
4324 //    returning the CPU usage if it's under 100%, and the run queue
4325 //    length otherwise.  It turns out that querying is pretty slow
4326 //    on Windows, on the order of 200 microseconds on a fast machine.
4327 //    Note that on the Windows the CPU usage value is the % usage
4328 //    since the last time the API was called (and the first call
4329 //    returns 100%), so we'd have to deal with that as well.
4330 //
4331 // b) Sample the "fake" answer using a sampling thread and store
4332 //    the answer in a global variable.  The call to loadavg would
4333 //    just return the value of the global, avoiding the slow query.
4334 //
4335 // c) Sample a better answer using exponential decay to smooth the
4336 //    value.  This is basically the algorithm used by UNIX kernels.
4337 //
4338 // Note that sampling thread starvation could affect both (b) and (c).
4339 int os::loadavg(double loadavg[], int nelem) {
4340   return -1;
4341 }
4342 
4343 
4344 // DontYieldALot=false by default: dutifully perform all yields as requested by JVM_Yield()
4345 bool os::dont_yield() {
4346   return DontYieldALot;
4347 }
4348 
4349 // This method is a slightly reworked copy of JDK's sysOpen
4350 // from src/windows/hpi/src/sys_api_md.c
4351 
4352 int os::open(const char *path, int oflag, int mode) {
4353   char pathbuf[MAX_PATH];
4354 
4355   if (strlen(path) > MAX_PATH - 1) {
4356     errno = ENAMETOOLONG;
4357     return -1;
4358   }
4359   os::native_path(strcpy(pathbuf, path));
4360   return ::open(pathbuf, oflag | O_BINARY | O_NOINHERIT, mode);
4361 }
4362 
4363 FILE* os::open(int fd, const char* mode) {
4364   return ::_fdopen(fd, mode);
4365 }
4366 
4367 // Is a (classpath) directory empty?
4368 bool os::dir_is_empty(const char* path) {
4369   WIN32_FIND_DATA fd;
4370   HANDLE f = FindFirstFile(path, &fd);
4371   if (f == INVALID_HANDLE_VALUE) {
4372     return true;
4373   }
4374   FindClose(f);
4375   return false;
4376 }
4377 
4378 // create binary file, rewriting existing file if required
4379 int os::create_binary_file(const char* path, bool rewrite_existing) {
4380   int oflags = _O_CREAT | _O_WRONLY | _O_BINARY;
4381   if (!rewrite_existing) {
4382     oflags |= _O_EXCL;
4383   }
4384   return ::open(path, oflags, _S_IREAD | _S_IWRITE);
4385 }
4386 
4387 // return current position of file pointer
4388 jlong os::current_file_offset(int fd) {
4389   return (jlong)::_lseeki64(fd, (__int64)0L, SEEK_CUR);
4390 }
4391 
4392 // move file pointer to the specified offset
4393 jlong os::seek_to_file_offset(int fd, jlong offset) {
4394   return (jlong)::_lseeki64(fd, (__int64)offset, SEEK_SET);
4395 }
4396 
4397 
4398 jlong os::lseek(int fd, jlong offset, int whence) {
4399   return (jlong) ::_lseeki64(fd, offset, whence);
4400 }
4401 
4402 size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {
4403   OVERLAPPED ov;
4404   DWORD nread;
4405   BOOL result;
4406 
4407   ZeroMemory(&ov, sizeof(ov));
4408   ov.Offset = (DWORD)offset;
4409   ov.OffsetHigh = (DWORD)(offset >> 32);
4410 
4411   HANDLE h = (HANDLE)::_get_osfhandle(fd);
4412 
4413   result = ReadFile(h, (LPVOID)buf, nBytes, &nread, &ov);
4414 
4415   return result ? nread : 0;
4416 }
4417 
4418 
4419 // This method is a slightly reworked copy of JDK's sysNativePath
4420 // from src/windows/hpi/src/path_md.c
4421 
4422 // Convert a pathname to native format.  On win32, this involves forcing all
4423 // separators to be '\\' rather than '/' (both are legal inputs, but Win95
4424 // sometimes rejects '/') and removing redundant separators.  The input path is
4425 // assumed to have been converted into the character encoding used by the local
4426 // system.  Because this might be a double-byte encoding, care is taken to
4427 // treat double-byte lead characters correctly.
4428 //
4429 // This procedure modifies the given path in place, as the result is never
4430 // longer than the original.  There is no error return; this operation always
4431 // succeeds.
4432 char * os::native_path(char *path) {
4433   char *src = path, *dst = path, *end = path;
4434   char *colon = NULL;  // If a drive specifier is found, this will
4435                        // point to the colon following the drive letter
4436 
4437   // Assumption: '/', '\\', ':', and drive letters are never lead bytes
4438   assert(((!::IsDBCSLeadByte('/')) && (!::IsDBCSLeadByte('\\'))
4439           && (!::IsDBCSLeadByte(':'))), "Illegal lead byte");
4440 
4441   // Check for leading separators
4442 #define isfilesep(c) ((c) == '/' || (c) == '\\')
4443   while (isfilesep(*src)) {
4444     src++;
4445   }
4446 
4447   if (::isalpha(*src) && !::IsDBCSLeadByte(*src) && src[1] == ':') {
4448     // Remove leading separators if followed by drive specifier.  This
4449     // hack is necessary to support file URLs containing drive
4450     // specifiers (e.g., "file://c:/path").  As a side effect,
4451     // "/c:/path" can be used as an alternative to "c:/path".
4452     *dst++ = *src++;
4453     colon = dst;
4454     *dst++ = ':';
4455     src++;
4456   } else {
4457     src = path;
4458     if (isfilesep(src[0]) && isfilesep(src[1])) {
4459       // UNC pathname: Retain first separator; leave src pointed at
4460       // second separator so that further separators will be collapsed
4461       // into the second separator.  The result will be a pathname
4462       // beginning with "\\\\" followed (most likely) by a host name.
4463       src = dst = path + 1;
4464       path[0] = '\\';     // Force first separator to '\\'
4465     }
4466   }
4467 
4468   end = dst;
4469 
4470   // Remove redundant separators from remainder of path, forcing all
4471   // separators to be '\\' rather than '/'. Also, single byte space
4472   // characters are removed from the end of the path because those
4473   // are not legal ending characters on this operating system.
4474   //
4475   while (*src != '\0') {
4476     if (isfilesep(*src)) {
4477       *dst++ = '\\'; src++;
4478       while (isfilesep(*src)) src++;
4479       if (*src == '\0') {
4480         // Check for trailing separator
4481         end = dst;
4482         if (colon == dst - 2) break;  // "z:\\"
4483         if (dst == path + 1) break;   // "\\"
4484         if (dst == path + 2 && isfilesep(path[0])) {
4485           // "\\\\" is not collapsed to "\\" because "\\\\" marks the
4486           // beginning of a UNC pathname.  Even though it is not, by
4487           // itself, a valid UNC pathname, we leave it as is in order
4488           // to be consistent with the path canonicalizer as well
4489           // as the win32 APIs, which treat this case as an invalid
4490           // UNC pathname rather than as an alias for the root
4491           // directory of the current drive.
4492           break;
4493         }
4494         end = --dst;  // Path does not denote a root directory, so
4495                       // remove trailing separator
4496         break;
4497       }
4498       end = dst;
4499     } else {
4500       if (::IsDBCSLeadByte(*src)) {  // Copy a double-byte character
4501         *dst++ = *src++;
4502         if (*src) *dst++ = *src++;
4503         end = dst;
4504       } else {  // Copy a single-byte character
4505         char c = *src++;
4506         *dst++ = c;
4507         // Space is not a legal ending character
4508         if (c != ' ') end = dst;
4509       }
4510     }
4511   }
4512 
4513   *end = '\0';
4514 
4515   // For "z:", add "." to work around a bug in the C runtime library
4516   if (colon == dst - 1) {
4517     path[2] = '.';
4518     path[3] = '\0';
4519   }
4520 
4521   return path;
4522 }
4523 
4524 // This code is a copy of JDK's sysSetLength
4525 // from src/windows/hpi/src/sys_api_md.c
4526 
4527 int os::ftruncate(int fd, jlong length) {
4528   HANDLE h = (HANDLE)::_get_osfhandle(fd);
4529   long high = (long)(length >> 32);
4530   DWORD ret;
4531 
4532   if (h == (HANDLE)(-1)) {
4533     return -1;
4534   }
4535 
4536   ret = ::SetFilePointer(h, (long)(length), &high, FILE_BEGIN);
4537   if ((ret == 0xFFFFFFFF) && (::GetLastError() != NO_ERROR)) {
4538     return -1;
4539   }
4540 
4541   if (::SetEndOfFile(h) == FALSE) {
4542     return -1;
4543   }
4544 
4545   return 0;
4546 }
4547 
4548 
4549 // This code is a copy of JDK's sysSync
4550 // from src/windows/hpi/src/sys_api_md.c
4551 // except for the legacy workaround for a bug in Win 98
4552 
4553 int os::fsync(int fd) {
4554   HANDLE handle = (HANDLE)::_get_osfhandle(fd);
4555 
4556   if ((!::FlushFileBuffers(handle)) &&
4557       (GetLastError() != ERROR_ACCESS_DENIED)) {
4558     // from winerror.h
4559     return -1;
4560   }
4561   return 0;
4562 }
4563 
4564 static int nonSeekAvailable(int, long *);
4565 static int stdinAvailable(int, long *);
4566 
4567 #define S_ISCHR(mode)   (((mode) & _S_IFCHR) == _S_IFCHR)
4568 #define S_ISFIFO(mode)  (((mode) & _S_IFIFO) == _S_IFIFO)
4569 
4570 // This code is a copy of JDK's sysAvailable
4571 // from src/windows/hpi/src/sys_api_md.c
4572 
4573 int os::available(int fd, jlong *bytes) {
4574   jlong cur, end;
4575   struct _stati64 stbuf64;
4576 
4577   if (::_fstati64(fd, &stbuf64) >= 0) {
4578     int mode = stbuf64.st_mode;
4579     if (S_ISCHR(mode) || S_ISFIFO(mode)) {
4580       int ret;
4581       long lpbytes;
4582       if (fd == 0) {
4583         ret = stdinAvailable(fd, &lpbytes);
4584       } else {
4585         ret = nonSeekAvailable(fd, &lpbytes);
4586       }
4587       (*bytes) = (jlong)(lpbytes);
4588       return ret;
4589     }
4590     if ((cur = ::_lseeki64(fd, 0L, SEEK_CUR)) == -1) {
4591       return FALSE;
4592     } else if ((end = ::_lseeki64(fd, 0L, SEEK_END)) == -1) {
4593       return FALSE;
4594     } else if (::_lseeki64(fd, cur, SEEK_SET) == -1) {
4595       return FALSE;
4596     }
4597     *bytes = end - cur;
4598     return TRUE;
4599   } else {
4600     return FALSE;
4601   }
4602 }
4603 
4604 // This code is a copy of JDK's nonSeekAvailable
4605 // from src/windows/hpi/src/sys_api_md.c
4606 
4607 static int nonSeekAvailable(int fd, long *pbytes) {
4608   // This is used for available on non-seekable devices
4609   // (like both named and anonymous pipes, such as pipes
4610   //  connected to an exec'd process).
4611   // Standard Input is a special case.
4612   HANDLE han;
4613 
4614   if ((han = (HANDLE) ::_get_osfhandle(fd)) == (HANDLE)(-1)) {
4615     return FALSE;
4616   }
4617 
4618   if (! ::PeekNamedPipe(han, NULL, 0, NULL, (LPDWORD)pbytes, NULL)) {
4619     // PeekNamedPipe fails when at EOF.  In that case we
4620     // simply make *pbytes = 0 which is consistent with the
4621     // behavior we get on Solaris when an fd is at EOF.
4622     // The only alternative is to raise an Exception,
4623     // which isn't really warranted.
4624     //
4625     if (::GetLastError() != ERROR_BROKEN_PIPE) {
4626       return FALSE;
4627     }
4628     *pbytes = 0;
4629   }
4630   return TRUE;
4631 }
4632 
4633 #define MAX_INPUT_EVENTS 2000
4634 
4635 // This code is a copy of JDK's stdinAvailable
4636 // from src/windows/hpi/src/sys_api_md.c
4637 
4638 static int stdinAvailable(int fd, long *pbytes) {
4639   HANDLE han;
4640   DWORD numEventsRead = 0;  // Number of events read from buffer
4641   DWORD numEvents = 0;      // Number of events in buffer
4642   DWORD i = 0;              // Loop index
4643   DWORD curLength = 0;      // Position marker
4644   DWORD actualLength = 0;   // Number of bytes readable
4645   BOOL error = FALSE;       // Error holder
4646   INPUT_RECORD *lpBuffer;   // Pointer to records of input events
4647 
4648   if ((han = ::GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
4649     return FALSE;
4650   }
4651 
4652   // Construct an array of input records in the console buffer
4653   error = ::GetNumberOfConsoleInputEvents(han, &numEvents);
4654   if (error == 0) {
4655     return nonSeekAvailable(fd, pbytes);
4656   }
4657 
4658   // lpBuffer must fit into 64K or else PeekConsoleInput fails
4659   if (numEvents > MAX_INPUT_EVENTS) {
4660     numEvents = MAX_INPUT_EVENTS;
4661   }
4662 
4663   lpBuffer = (INPUT_RECORD *)os::malloc(numEvents * sizeof(INPUT_RECORD), mtInternal);
4664   if (lpBuffer == NULL) {
4665     return FALSE;
4666   }
4667 
4668   error = ::PeekConsoleInput(han, lpBuffer, numEvents, &numEventsRead);
4669   if (error == 0) {
4670     os::free(lpBuffer);
4671     return FALSE;
4672   }
4673 
4674   // Examine input records for the number of bytes available
4675   for (i=0; i<numEvents; i++) {
4676     if (lpBuffer[i].EventType == KEY_EVENT) {
4677 
4678       KEY_EVENT_RECORD *keyRecord = (KEY_EVENT_RECORD *)
4679                                       &(lpBuffer[i].Event);
4680       if (keyRecord->bKeyDown == TRUE) {
4681         CHAR *keyPressed = (CHAR *) &(keyRecord->uChar);
4682         curLength++;
4683         if (*keyPressed == '\r') {
4684           actualLength = curLength;
4685         }
4686       }
4687     }
4688   }
4689 
4690   if (lpBuffer != NULL) {
4691     os::free(lpBuffer);
4692   }
4693 
4694   *pbytes = (long) actualLength;
4695   return TRUE;
4696 }
4697 
4698 // Map a block of memory.
4699 char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
4700                         char *addr, size_t bytes, bool read_only,
4701                         bool allow_exec) {
4702   HANDLE hFile;
4703   char* base;
4704 
4705   hFile = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
4706                      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
4707   if (hFile == NULL) {
4708     if (PrintMiscellaneous && Verbose) {
4709       DWORD err = GetLastError();
4710       tty->print_cr("CreateFile() failed: GetLastError->%ld.", err);
4711     }
4712     return NULL;
4713   }
4714 
4715   if (allow_exec) {
4716     // CreateFileMapping/MapViewOfFileEx can't map executable memory
4717     // unless it comes from a PE image (which the shared archive is not.)
4718     // Even VirtualProtect refuses to give execute access to mapped memory
4719     // that was not previously executable.
4720     //
4721     // Instead, stick the executable region in anonymous memory.  Yuck.
4722     // Penalty is that ~4 pages will not be shareable - in the future
4723     // we might consider DLLizing the shared archive with a proper PE
4724     // header so that mapping executable + sharing is possible.
4725 
4726     base = (char*) VirtualAlloc(addr, bytes, MEM_COMMIT | MEM_RESERVE,
4727                                 PAGE_READWRITE);
4728     if (base == NULL) {
4729       if (PrintMiscellaneous && Verbose) {
4730         DWORD err = GetLastError();
4731         tty->print_cr("VirtualAlloc() failed: GetLastError->%ld.", err);
4732       }
4733       CloseHandle(hFile);
4734       return NULL;
4735     }
4736 
4737     DWORD bytes_read;
4738     OVERLAPPED overlapped;
4739     overlapped.Offset = (DWORD)file_offset;
4740     overlapped.OffsetHigh = 0;
4741     overlapped.hEvent = NULL;
4742     // ReadFile guarantees that if the return value is true, the requested
4743     // number of bytes were read before returning.
4744     bool res = ReadFile(hFile, base, (DWORD)bytes, &bytes_read, &overlapped) != 0;
4745     if (!res) {
4746       if (PrintMiscellaneous && Verbose) {
4747         DWORD err = GetLastError();
4748         tty->print_cr("ReadFile() failed: GetLastError->%ld.", err);
4749       }
4750       release_memory(base, bytes);
4751       CloseHandle(hFile);
4752       return NULL;
4753     }
4754   } else {
4755     HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_WRITECOPY, 0, 0,
4756                                     NULL /* file_name */);
4757     if (hMap == NULL) {
4758       if (PrintMiscellaneous && Verbose) {
4759         DWORD err = GetLastError();
4760         tty->print_cr("CreateFileMapping() failed: GetLastError->%ld.", err);
4761       }
4762       CloseHandle(hFile);
4763       return NULL;
4764     }
4765 
4766     DWORD access = read_only ? FILE_MAP_READ : FILE_MAP_COPY;
4767     base = (char*)MapViewOfFileEx(hMap, access, 0, (DWORD)file_offset,
4768                                   (DWORD)bytes, addr);
4769     if (base == NULL) {
4770       if (PrintMiscellaneous && Verbose) {
4771         DWORD err = GetLastError();
4772         tty->print_cr("MapViewOfFileEx() failed: GetLastError->%ld.", err);
4773       }
4774       CloseHandle(hMap);
4775       CloseHandle(hFile);
4776       return NULL;
4777     }
4778 
4779     if (CloseHandle(hMap) == 0) {
4780       if (PrintMiscellaneous && Verbose) {
4781         DWORD err = GetLastError();
4782         tty->print_cr("CloseHandle(hMap) failed: GetLastError->%ld.", err);
4783       }
4784       CloseHandle(hFile);
4785       return base;
4786     }
4787   }
4788 
4789   if (allow_exec) {
4790     DWORD old_protect;
4791     DWORD exec_access = read_only ? PAGE_EXECUTE_READ : PAGE_EXECUTE_READWRITE;
4792     bool res = VirtualProtect(base, bytes, exec_access, &old_protect) != 0;
4793 
4794     if (!res) {
4795       if (PrintMiscellaneous && Verbose) {
4796         DWORD err = GetLastError();
4797         tty->print_cr("VirtualProtect() failed: GetLastError->%ld.", err);
4798       }
4799       // Don't consider this a hard error, on IA32 even if the
4800       // VirtualProtect fails, we should still be able to execute
4801       CloseHandle(hFile);
4802       return base;
4803     }
4804   }
4805 
4806   if (CloseHandle(hFile) == 0) {
4807     if (PrintMiscellaneous && Verbose) {
4808       DWORD err = GetLastError();
4809       tty->print_cr("CloseHandle(hFile) failed: GetLastError->%ld.", err);
4810     }
4811     return base;
4812   }
4813 
4814   return base;
4815 }
4816 
4817 
4818 // Remap a block of memory.
4819 char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
4820                           char *addr, size_t bytes, bool read_only,
4821                           bool allow_exec) {
4822   // This OS does not allow existing memory maps to be remapped so we
4823   // have to unmap the memory before we remap it.
4824   if (!os::unmap_memory(addr, bytes)) {
4825     return NULL;
4826   }
4827 
4828   // There is a very small theoretical window between the unmap_memory()
4829   // call above and the map_memory() call below where a thread in native
4830   // code may be able to access an address that is no longer mapped.
4831 
4832   return os::map_memory(fd, file_name, file_offset, addr, bytes,
4833                         read_only, allow_exec);
4834 }
4835 
4836 
4837 // Unmap a block of memory.
4838 // Returns true=success, otherwise false.
4839 
4840 bool os::pd_unmap_memory(char* addr, size_t bytes) {
4841   BOOL result = UnmapViewOfFile(addr);
4842   if (result == 0) {
4843     if (PrintMiscellaneous && Verbose) {
4844       DWORD err = GetLastError();
4845       tty->print_cr("UnmapViewOfFile() failed: GetLastError->%ld.", err);
4846     }
4847     return false;
4848   }
4849   return true;
4850 }
4851 
4852 void os::pause() {
4853   char filename[MAX_PATH];
4854   if (PauseAtStartupFile && PauseAtStartupFile[0]) {
4855     jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
4856   } else {
4857     jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
4858   }
4859 
4860   int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
4861   if (fd != -1) {
4862     struct stat buf;
4863     ::close(fd);
4864     while (::stat(filename, &buf) == 0) {
4865       Sleep(100);
4866     }
4867   } else {
4868     jio_fprintf(stderr,
4869                 "Could not open pause file '%s', continuing immediately.\n", filename);
4870   }
4871 }
4872 
4873 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
4874   assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
4875 }
4876 
4877 // See the caveats for this class in os_windows.hpp
4878 // Protects the callback call so that raised OS EXCEPTIONS causes a jump back
4879 // into this method and returns false. If no OS EXCEPTION was raised, returns
4880 // true.
4881 // The callback is supposed to provide the method that should be protected.
4882 //
4883 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
4884   assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
4885   assert(!WatcherThread::watcher_thread()->has_crash_protection(),
4886          "crash_protection already set?");
4887 
4888   bool success = true;
4889   __try {
4890     WatcherThread::watcher_thread()->set_crash_protection(this);
4891     cb.call();
4892   } __except(EXCEPTION_EXECUTE_HANDLER) {
4893     // only for protection, nothing to do
4894     success = false;
4895   }
4896   WatcherThread::watcher_thread()->set_crash_protection(NULL);
4897   return success;
4898 }
4899 
4900 // An Event wraps a win32 "CreateEvent" kernel handle.
4901 //
4902 // We have a number of choices regarding "CreateEvent" win32 handle leakage:
4903 //
4904 // 1:  When a thread dies return the Event to the EventFreeList, clear the ParkHandle
4905 //     field, and call CloseHandle() on the win32 event handle.  Unpark() would
4906 //     need to be modified to tolerate finding a NULL (invalid) win32 event handle.
4907 //     In addition, an unpark() operation might fetch the handle field, but the
4908 //     event could recycle between the fetch and the SetEvent() operation.
4909 //     SetEvent() would either fail because the handle was invalid, or inadvertently work,
4910 //     as the win32 handle value had been recycled.  In an ideal world calling SetEvent()
4911 //     on an stale but recycled handle would be harmless, but in practice this might
4912 //     confuse other non-Sun code, so it's not a viable approach.
4913 //
4914 // 2:  Once a win32 event handle is associated with an Event, it remains associated
4915 //     with the Event.  The event handle is never closed.  This could be construed
4916 //     as handle leakage, but only up to the maximum # of threads that have been extant
4917 //     at any one time.  This shouldn't be an issue, as windows platforms typically
4918 //     permit a process to have hundreds of thousands of open handles.
4919 //
4920 // 3:  Same as (1), but periodically, at stop-the-world time, rundown the EventFreeList
4921 //     and release unused handles.
4922 //
4923 // 4:  Add a CRITICAL_SECTION to the Event to protect LD+SetEvent from LD;ST(null);CloseHandle.
4924 //     It's not clear, however, that we wouldn't be trading one type of leak for another.
4925 //
4926 // 5.  Use an RCU-like mechanism (Read-Copy Update).
4927 //     Or perhaps something similar to Maged Michael's "Hazard pointers".
4928 //
4929 // We use (2).
4930 //
4931 // TODO-FIXME:
4932 // 1.  Reconcile Doug's JSR166 j.u.c park-unpark with the objectmonitor implementation.
4933 // 2.  Consider wrapping the WaitForSingleObject(Ex) calls in SEH try/finally blocks
4934 //     to recover from (or at least detect) the dreaded Windows 841176 bug.
4935 // 3.  Collapse the interrupt_event, the JSR166 parker event, and the objectmonitor ParkEvent
4936 //     into a single win32 CreateEvent() handle.
4937 //
4938 // Assumption:
4939 //    Only one parker can exist on an event, which is why we allocate
4940 //    them per-thread. Multiple unparkers can coexist.
4941 //
4942 // _Event transitions in park()
4943 //   -1 => -1 : illegal
4944 //    1 =>  0 : pass - return immediately
4945 //    0 => -1 : block; then set _Event to 0 before returning
4946 //
4947 // _Event transitions in unpark()
4948 //    0 => 1 : just return
4949 //    1 => 1 : just return
4950 //   -1 => either 0 or 1; must signal target thread
4951 //         That is, we can safely transition _Event from -1 to either
4952 //         0 or 1.
4953 //
4954 // _Event serves as a restricted-range semaphore.
4955 //   -1 : thread is blocked, i.e. there is a waiter
4956 //    0 : neutral: thread is running or ready,
4957 //        could have been signaled after a wait started
4958 //    1 : signaled - thread is running or ready
4959 //
4960 // Another possible encoding of _Event would be with
4961 // explicit "PARKED" == 01b and "SIGNALED" == 10b bits.
4962 //
4963 
4964 int os::PlatformEvent::park(jlong Millis) {
4965   // Transitions for _Event:
4966   //   -1 => -1 : illegal
4967   //    1 =>  0 : pass - return immediately
4968   //    0 => -1 : block; then set _Event to 0 before returning
4969 
4970   guarantee(_ParkHandle != NULL , "Invariant");
4971   guarantee(Millis > 0          , "Invariant");
4972 
4973   // CONSIDER: defer assigning a CreateEvent() handle to the Event until
4974   // the initial park() operation.
4975   // Consider: use atomic decrement instead of CAS-loop
4976 
4977   int v;
4978   for (;;) {
4979     v = _Event;
4980     if (Atomic::cmpxchg(v-1, &_Event, v) == v) break;
4981   }
4982   guarantee((v == 0) || (v == 1), "invariant");
4983   if (v != 0) return OS_OK;
4984 
4985   // Do this the hard way by blocking ...
4986   // TODO: consider a brief spin here, gated on the success of recent
4987   // spin attempts by this thread.
4988   //
4989   // We decompose long timeouts into series of shorter timed waits.
4990   // Evidently large timo values passed in WaitForSingleObject() are problematic on some
4991   // versions of Windows.  See EventWait() for details.  This may be superstition.  Or not.
4992   // We trust the WAIT_TIMEOUT indication and don't track the elapsed wait time
4993   // with os::javaTimeNanos().  Furthermore, we assume that spurious returns from
4994   // ::WaitForSingleObject() caused by latent ::setEvent() operations will tend
4995   // to happen early in the wait interval.  Specifically, after a spurious wakeup (rv ==
4996   // WAIT_OBJECT_0 but _Event is still < 0) we don't bother to recompute Millis to compensate
4997   // for the already waited time.  This policy does not admit any new outcomes.
4998   // In the future, however, we might want to track the accumulated wait time and
4999   // adjust Millis accordingly if we encounter a spurious wakeup.
5000 
5001   const int MAXTIMEOUT = 0x10000000;
5002   DWORD rv = WAIT_TIMEOUT;
5003   while (_Event < 0 && Millis > 0) {
5004     DWORD prd = Millis;     // set prd = MAX (Millis, MAXTIMEOUT)
5005     if (Millis > MAXTIMEOUT) {
5006       prd = MAXTIMEOUT;
5007     }
5008     rv = ::WaitForSingleObject(_ParkHandle, prd);
5009     assert(rv == WAIT_OBJECT_0 || rv == WAIT_TIMEOUT, "WaitForSingleObject failed");
5010     if (rv == WAIT_TIMEOUT) {
5011       Millis -= prd;
5012     }
5013   }
5014   v = _Event;
5015   _Event = 0;
5016   // see comment at end of os::PlatformEvent::park() below:
5017   OrderAccess::fence();
5018   // If we encounter a nearly simultanous timeout expiry and unpark()
5019   // we return OS_OK indicating we awoke via unpark().
5020   // Implementor's license -- returning OS_TIMEOUT would be equally valid, however.
5021   return (v >= 0) ? OS_OK : OS_TIMEOUT;
5022 }
5023 
5024 void os::PlatformEvent::park() {
5025   // Transitions for _Event:
5026   //   -1 => -1 : illegal
5027   //    1 =>  0 : pass - return immediately
5028   //    0 => -1 : block; then set _Event to 0 before returning
5029 
5030   guarantee(_ParkHandle != NULL, "Invariant");
5031   // Invariant: Only the thread associated with the Event/PlatformEvent
5032   // may call park().
5033   // Consider: use atomic decrement instead of CAS-loop
5034   int v;
5035   for (;;) {
5036     v = _Event;
5037     if (Atomic::cmpxchg(v-1, &_Event, v) == v) break;
5038   }
5039   guarantee((v == 0) || (v == 1), "invariant");
5040   if (v != 0) return;
5041 
5042   // Do this the hard way by blocking ...
5043   // TODO: consider a brief spin here, gated on the success of recent
5044   // spin attempts by this thread.
5045   while (_Event < 0) {
5046     DWORD rv = ::WaitForSingleObject(_ParkHandle, INFINITE);
5047     assert(rv == WAIT_OBJECT_0, "WaitForSingleObject failed");
5048   }
5049 
5050   // Usually we'll find _Event == 0 at this point, but as
5051   // an optional optimization we clear it, just in case can
5052   // multiple unpark() operations drove _Event up to 1.
5053   _Event = 0;
5054   OrderAccess::fence();
5055   guarantee(_Event >= 0, "invariant");
5056 }
5057 
5058 void os::PlatformEvent::unpark() {
5059   guarantee(_ParkHandle != NULL, "Invariant");
5060 
5061   // Transitions for _Event:
5062   //    0 => 1 : just return
5063   //    1 => 1 : just return
5064   //   -1 => either 0 or 1; must signal target thread
5065   //         That is, we can safely transition _Event from -1 to either
5066   //         0 or 1.
5067   // See also: "Semaphores in Plan 9" by Mullender & Cox
5068   //
5069   // Note: Forcing a transition from "-1" to "1" on an unpark() means
5070   // that it will take two back-to-back park() calls for the owning
5071   // thread to block. This has the benefit of forcing a spurious return
5072   // from the first park() call after an unpark() call which will help
5073   // shake out uses of park() and unpark() without condition variables.
5074 
5075   if (Atomic::xchg(1, &_Event) >= 0) return;
5076 
5077   ::SetEvent(_ParkHandle);
5078 }
5079 
5080 
5081 // JSR166
5082 // -------------------------------------------------------
5083 
5084 // The Windows implementation of Park is very straightforward: Basic
5085 // operations on Win32 Events turn out to have the right semantics to
5086 // use them directly. We opportunistically resuse the event inherited
5087 // from Monitor.
5088 
5089 void Parker::park(bool isAbsolute, jlong time) {
5090   guarantee(_ParkEvent != NULL, "invariant");
5091   // First, demultiplex/decode time arguments
5092   if (time < 0) { // don't wait
5093     return;
5094   } else if (time == 0 && !isAbsolute) {
5095     time = INFINITE;
5096   } else if (isAbsolute) {
5097     time -= os::javaTimeMillis(); // convert to relative time
5098     if (time <= 0) {  // already elapsed
5099       return;
5100     }
5101   } else { // relative
5102     time /= 1000000;  // Must coarsen from nanos to millis
5103     if (time == 0) {  // Wait for the minimal time unit if zero
5104       time = 1;
5105     }
5106   }
5107 
5108   JavaThread* thread = (JavaThread*)(Thread::current());
5109   assert(thread->is_Java_thread(), "Must be JavaThread");
5110   JavaThread *jt = (JavaThread *)thread;
5111 
5112   // Don't wait if interrupted or already triggered
5113   if (Thread::is_interrupted(thread, false) ||
5114       WaitForSingleObject(_ParkEvent, 0) == WAIT_OBJECT_0) {
5115     ResetEvent(_ParkEvent);
5116     return;
5117   } else {
5118     ThreadBlockInVM tbivm(jt);
5119     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
5120     jt->set_suspend_equivalent();
5121 
5122     WaitForSingleObject(_ParkEvent, time);
5123     ResetEvent(_ParkEvent);
5124 
5125     // If externally suspended while waiting, re-suspend
5126     if (jt->handle_special_suspend_equivalent_condition()) {
5127       jt->java_suspend_self();
5128     }
5129   }
5130 }
5131 
5132 void Parker::unpark() {
5133   guarantee(_ParkEvent != NULL, "invariant");
5134   SetEvent(_ParkEvent);
5135 }
5136 
5137 // Run the specified command in a separate process. Return its exit value,
5138 // or -1 on failure (e.g. can't create a new process).
5139 int os::fork_and_exec(char* cmd) {
5140   STARTUPINFO si;
5141   PROCESS_INFORMATION pi;
5142 
5143   memset(&si, 0, sizeof(si));
5144   si.cb = sizeof(si);
5145   memset(&pi, 0, sizeof(pi));
5146   BOOL rslt = CreateProcess(NULL,   // executable name - use command line
5147                             cmd,    // command line
5148                             NULL,   // process security attribute
5149                             NULL,   // thread security attribute
5150                             TRUE,   // inherits system handles
5151                             0,      // no creation flags
5152                             NULL,   // use parent's environment block
5153                             NULL,   // use parent's starting directory
5154                             &si,    // (in) startup information
5155                             &pi);   // (out) process information
5156 
5157   if (rslt) {
5158     // Wait until child process exits.
5159     WaitForSingleObject(pi.hProcess, INFINITE);
5160 
5161     DWORD exit_code;
5162     GetExitCodeProcess(pi.hProcess, &exit_code);
5163 
5164     // Close process and thread handles.
5165     CloseHandle(pi.hProcess);
5166     CloseHandle(pi.hThread);
5167 
5168     return (int)exit_code;
5169   } else {
5170     return -1;
5171   }
5172 }
5173 
5174 //--------------------------------------------------------------------------------------------------
5175 // Non-product code
5176 
5177 static int mallocDebugIntervalCounter = 0;
5178 static int mallocDebugCounter = 0;
5179 bool os::check_heap(bool force) {
5180   if (++mallocDebugCounter < MallocVerifyStart && !force) return true;
5181   if (++mallocDebugIntervalCounter >= MallocVerifyInterval || force) {
5182     // Note: HeapValidate executes two hardware breakpoints when it finds something
5183     // wrong; at these points, eax contains the address of the offending block (I think).
5184     // To get to the exlicit error message(s) below, just continue twice.
5185     HANDLE heap = GetProcessHeap();
5186 
5187     // If we fail to lock the heap, then gflags.exe has been used
5188     // or some other special heap flag has been set that prevents
5189     // locking. We don't try to walk a heap we can't lock.
5190     if (HeapLock(heap) != 0) {
5191       PROCESS_HEAP_ENTRY phe;
5192       phe.lpData = NULL;
5193       while (HeapWalk(heap, &phe) != 0) {
5194         if ((phe.wFlags & PROCESS_HEAP_ENTRY_BUSY) &&
5195             !HeapValidate(heap, 0, phe.lpData)) {
5196           tty->print_cr("C heap has been corrupted (time: %d allocations)", mallocDebugCounter);
5197           tty->print_cr("corrupted block near address %#x, length %d", phe.lpData, phe.cbData);
5198           fatal("corrupted C heap");
5199         }
5200       }
5201       DWORD err = GetLastError();
5202       if (err != ERROR_NO_MORE_ITEMS && err != ERROR_CALL_NOT_IMPLEMENTED) {
5203         fatal(err_msg("heap walk aborted with error %d", err));
5204       }
5205       HeapUnlock(heap);
5206     }
5207     mallocDebugIntervalCounter = 0;
5208   }
5209   return true;
5210 }
5211 
5212 
5213 bool os::find(address addr, outputStream* st) {
5214   // Nothing yet
5215   return false;
5216 }
5217 
5218 LONG WINAPI os::win32::serialize_fault_filter(struct _EXCEPTION_POINTERS* e) {
5219   DWORD exception_code = e->ExceptionRecord->ExceptionCode;
5220 
5221   if (exception_code == EXCEPTION_ACCESS_VIOLATION) {
5222     JavaThread* thread = (JavaThread*)ThreadLocalStorage::get_thread_slow();
5223     PEXCEPTION_RECORD exceptionRecord = e->ExceptionRecord;
5224     address addr = (address) exceptionRecord->ExceptionInformation[1];
5225 
5226     if (os::is_memory_serialize_page(thread, addr)) {
5227       return EXCEPTION_CONTINUE_EXECUTION;
5228     }
5229   }
5230 
5231   return EXCEPTION_CONTINUE_SEARCH;
5232 }
5233 
5234 // We don't build a headless jre for Windows
5235 bool os::is_headless_jre() { return false; }
5236 
5237 static jint initSock() {
5238   WSADATA wsadata;
5239 
5240   if (!os::WinSock2Dll::WinSock2Available()) {
5241     jio_fprintf(stderr, "Could not load Winsock (error: %d)\n",
5242                 ::GetLastError());
5243     return JNI_ERR;
5244   }
5245 
5246   if (os::WinSock2Dll::WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
5247     jio_fprintf(stderr, "Could not initialize Winsock (error: %d)\n",
5248                 ::GetLastError());
5249     return JNI_ERR;
5250   }
5251   return JNI_OK;
5252 }
5253 
5254 struct hostent* os::get_host_by_name(char* name) {
5255   return (struct hostent*)os::WinSock2Dll::gethostbyname(name);
5256 }
5257 
5258 int os::socket_close(int fd) {
5259   return ::closesocket(fd);
5260 }
5261 
5262 int os::socket(int domain, int type, int protocol) {
5263   return ::socket(domain, type, protocol);
5264 }
5265 
5266 int os::connect(int fd, struct sockaddr* him, socklen_t len) {
5267   return ::connect(fd, him, len);
5268 }
5269 
5270 int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
5271   return ::recv(fd, buf, (int)nBytes, flags);
5272 }
5273 
5274 int os::send(int fd, char* buf, size_t nBytes, uint flags) {
5275   return ::send(fd, buf, (int)nBytes, flags);
5276 }
5277 
5278 int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
5279   return ::send(fd, buf, (int)nBytes, flags);
5280 }
5281 
5282 // WINDOWS CONTEXT Flags for THREAD_SAMPLING
5283 #if defined(IA32)
5284   #define sampling_context_flags (CONTEXT_FULL | CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS)
5285 #elif defined (AMD64)
5286   #define sampling_context_flags (CONTEXT_FULL | CONTEXT_FLOATING_POINT)
5287 #endif
5288 
5289 // returns true if thread could be suspended,
5290 // false otherwise
5291 static bool do_suspend(HANDLE* h) {
5292   if (h != NULL) {
5293     if (SuspendThread(*h) != ~0) {
5294       return true;
5295     }
5296   }
5297   return false;
5298 }
5299 
5300 // resume the thread
5301 // calling resume on an active thread is a no-op
5302 static void do_resume(HANDLE* h) {
5303   if (h != NULL) {
5304     ResumeThread(*h);
5305   }
5306 }
5307 
5308 // retrieve a suspend/resume context capable handle
5309 // from the tid. Caller validates handle return value.
5310 void get_thread_handle_for_extended_context(HANDLE* h,
5311                                             OSThread::thread_id_t tid) {
5312   if (h != NULL) {
5313     *h = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, tid);
5314   }
5315 }
5316 
5317 // Thread sampling implementation
5318 //
5319 void os::SuspendedThreadTask::internal_do_task() {
5320   CONTEXT    ctxt;
5321   HANDLE     h = NULL;
5322 
5323   // get context capable handle for thread
5324   get_thread_handle_for_extended_context(&h, _thread->osthread()->thread_id());
5325 
5326   // sanity
5327   if (h == NULL || h == INVALID_HANDLE_VALUE) {
5328     return;
5329   }
5330 
5331   // suspend the thread
5332   if (do_suspend(&h)) {
5333     ctxt.ContextFlags = sampling_context_flags;
5334     // get thread context
5335     GetThreadContext(h, &ctxt);
5336     SuspendedThreadTaskContext context(_thread, &ctxt);
5337     // pass context to Thread Sampling impl
5338     do_task(context);
5339     // resume thread
5340     do_resume(&h);
5341   }
5342 
5343   // close handle
5344   CloseHandle(h);
5345 }
5346 
5347 
5348 // Kernel32 API
5349 typedef SIZE_T (WINAPI* GetLargePageMinimum_Fn)(void);
5350 typedef LPVOID (WINAPI *VirtualAllocExNuma_Fn)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD, DWORD);
5351 typedef BOOL (WINAPI *GetNumaHighestNodeNumber_Fn)(PULONG);
5352 typedef BOOL (WINAPI *GetNumaNodeProcessorMask_Fn)(UCHAR, PULONGLONG);
5353 typedef USHORT (WINAPI* RtlCaptureStackBackTrace_Fn)(ULONG, ULONG, PVOID*, PULONG);
5354 
5355 GetLargePageMinimum_Fn      os::Kernel32Dll::_GetLargePageMinimum = NULL;
5356 VirtualAllocExNuma_Fn       os::Kernel32Dll::_VirtualAllocExNuma = NULL;
5357 GetNumaHighestNodeNumber_Fn os::Kernel32Dll::_GetNumaHighestNodeNumber = NULL;
5358 GetNumaNodeProcessorMask_Fn os::Kernel32Dll::_GetNumaNodeProcessorMask = NULL;
5359 RtlCaptureStackBackTrace_Fn os::Kernel32Dll::_RtlCaptureStackBackTrace = NULL;
5360 
5361 
5362 BOOL                        os::Kernel32Dll::initialized = FALSE;
5363 SIZE_T os::Kernel32Dll::GetLargePageMinimum() {
5364   assert(initialized && _GetLargePageMinimum != NULL,
5365          "GetLargePageMinimumAvailable() not yet called");
5366   return _GetLargePageMinimum();
5367 }
5368 
5369 BOOL os::Kernel32Dll::GetLargePageMinimumAvailable() {
5370   if (!initialized) {
5371     initialize();
5372   }
5373   return _GetLargePageMinimum != NULL;
5374 }
5375 
5376 BOOL os::Kernel32Dll::NumaCallsAvailable() {
5377   if (!initialized) {
5378     initialize();
5379   }
5380   return _VirtualAllocExNuma != NULL;
5381 }
5382 
5383 LPVOID os::Kernel32Dll::VirtualAllocExNuma(HANDLE hProc, LPVOID addr,
5384                                            SIZE_T bytes, DWORD flags,
5385                                            DWORD prot, DWORD node) {
5386   assert(initialized && _VirtualAllocExNuma != NULL,
5387          "NUMACallsAvailable() not yet called");
5388 
5389   return _VirtualAllocExNuma(hProc, addr, bytes, flags, prot, node);
5390 }
5391 
5392 BOOL os::Kernel32Dll::GetNumaHighestNodeNumber(PULONG ptr_highest_node_number) {
5393   assert(initialized && _GetNumaHighestNodeNumber != NULL,
5394          "NUMACallsAvailable() not yet called");
5395 
5396   return _GetNumaHighestNodeNumber(ptr_highest_node_number);
5397 }
5398 
5399 BOOL os::Kernel32Dll::GetNumaNodeProcessorMask(UCHAR node,
5400                                                PULONGLONG proc_mask) {
5401   assert(initialized && _GetNumaNodeProcessorMask != NULL,
5402          "NUMACallsAvailable() not yet called");
5403 
5404   return _GetNumaNodeProcessorMask(node, proc_mask);
5405 }
5406 
5407 USHORT os::Kernel32Dll::RtlCaptureStackBackTrace(ULONG FrameToSkip,
5408                                                  ULONG FrameToCapture,
5409                                                  PVOID* BackTrace,
5410                                                  PULONG BackTraceHash) {
5411   if (!initialized) {
5412     initialize();
5413   }
5414 
5415   if (_RtlCaptureStackBackTrace != NULL) {
5416     return _RtlCaptureStackBackTrace(FrameToSkip, FrameToCapture,
5417                                      BackTrace, BackTraceHash);
5418   } else {
5419     return 0;
5420   }
5421 }
5422 
5423 void os::Kernel32Dll::initializeCommon() {
5424   if (!initialized) {
5425     HMODULE handle = ::GetModuleHandle("Kernel32.dll");
5426     assert(handle != NULL, "Just check");
5427     _GetLargePageMinimum = (GetLargePageMinimum_Fn)::GetProcAddress(handle, "GetLargePageMinimum");
5428     _VirtualAllocExNuma = (VirtualAllocExNuma_Fn)::GetProcAddress(handle, "VirtualAllocExNuma");
5429     _GetNumaHighestNodeNumber = (GetNumaHighestNodeNumber_Fn)::GetProcAddress(handle, "GetNumaHighestNodeNumber");
5430     _GetNumaNodeProcessorMask = (GetNumaNodeProcessorMask_Fn)::GetProcAddress(handle, "GetNumaNodeProcessorMask");
5431     _RtlCaptureStackBackTrace = (RtlCaptureStackBackTrace_Fn)::GetProcAddress(handle, "RtlCaptureStackBackTrace");
5432     initialized = TRUE;
5433   }
5434 }
5435 
5436 
5437 
5438 #ifndef JDK6_OR_EARLIER
5439 
5440 void os::Kernel32Dll::initialize() {
5441   initializeCommon();
5442 }
5443 
5444 
5445 // Kernel32 API
5446 inline BOOL os::Kernel32Dll::SwitchToThread() {
5447   return ::SwitchToThread();
5448 }
5449 
5450 inline BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
5451   return true;
5452 }
5453 
5454 // Help tools
5455 inline BOOL os::Kernel32Dll::HelpToolsAvailable() {
5456   return true;
5457 }
5458 
5459 inline HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,
5460                                                         DWORD th32ProcessId) {
5461   return ::CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
5462 }
5463 
5464 inline BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,
5465                                            LPMODULEENTRY32 lpme) {
5466   return ::Module32First(hSnapshot, lpme);
5467 }
5468 
5469 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,
5470                                           LPMODULEENTRY32 lpme) {
5471   return ::Module32Next(hSnapshot, lpme);
5472 }
5473 
5474 inline void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
5475   ::GetNativeSystemInfo(lpSystemInfo);
5476 }
5477 
5478 // PSAPI API
5479 inline BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess,
5480                                              HMODULE *lpModule, DWORD cb,
5481                                              LPDWORD lpcbNeeded) {
5482   return ::EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
5483 }
5484 
5485 inline DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess,
5486                                                HMODULE hModule,
5487                                                LPTSTR lpFilename,
5488                                                DWORD nSize) {
5489   return ::GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
5490 }
5491 
5492 inline BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess,
5493                                                HMODULE hModule,
5494                                                LPMODULEINFO lpmodinfo,
5495                                                DWORD cb) {
5496   return ::GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
5497 }
5498 
5499 inline BOOL os::PSApiDll::PSApiAvailable() {
5500   return true;
5501 }
5502 
5503 
5504 // WinSock2 API
5505 inline BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested,
5506                                         LPWSADATA lpWSAData) {
5507   return ::WSAStartup(wVersionRequested, lpWSAData);
5508 }
5509 
5510 inline struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
5511   return ::gethostbyname(name);
5512 }
5513 
5514 inline BOOL os::WinSock2Dll::WinSock2Available() {
5515   return true;
5516 }
5517 
5518 // Advapi API
5519 inline BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
5520                                                    BOOL DisableAllPrivileges,
5521                                                    PTOKEN_PRIVILEGES NewState,
5522                                                    DWORD BufferLength,
5523                                                    PTOKEN_PRIVILEGES PreviousState,
5524                                                    PDWORD ReturnLength) {
5525   return ::AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
5526                                  BufferLength, PreviousState, ReturnLength);
5527 }
5528 
5529 inline BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle,
5530                                               DWORD DesiredAccess,
5531                                               PHANDLE TokenHandle) {
5532   return ::OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
5533 }
5534 
5535 inline BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName,
5536                                                   LPCTSTR lpName,
5537                                                   PLUID lpLuid) {
5538   return ::LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
5539 }
5540 
5541 inline BOOL os::Advapi32Dll::AdvapiAvailable() {
5542   return true;
5543 }
5544 
5545 void* os::get_default_process_handle() {
5546   return (void*)GetModuleHandle(NULL);
5547 }
5548 
5549 // Builds a platform dependent Agent_OnLoad_<lib_name> function name
5550 // which is used to find statically linked in agents.
5551 // Additionally for windows, takes into account __stdcall names.
5552 // Parameters:
5553 //            sym_name: Symbol in library we are looking for
5554 //            lib_name: Name of library to look in, NULL for shared libs.
5555 //            is_absolute_path == true if lib_name is absolute path to agent
5556 //                                     such as "C:/a/b/L.dll"
5557 //            == false if only the base name of the library is passed in
5558 //               such as "L"
5559 char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
5560                                     bool is_absolute_path) {
5561   char *agent_entry_name;
5562   size_t len;
5563   size_t name_len;
5564   size_t prefix_len = strlen(JNI_LIB_PREFIX);
5565   size_t suffix_len = strlen(JNI_LIB_SUFFIX);
5566   const char *start;
5567 
5568   if (lib_name != NULL) {
5569     len = name_len = strlen(lib_name);
5570     if (is_absolute_path) {
5571       // Need to strip path, prefix and suffix
5572       if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
5573         lib_name = ++start;
5574       } else {
5575         // Need to check for drive prefix
5576         if ((start = strchr(lib_name, ':')) != NULL) {
5577           lib_name = ++start;
5578         }
5579       }
5580       if (len <= (prefix_len + suffix_len)) {
5581         return NULL;
5582       }
5583       lib_name += prefix_len;
5584       name_len = strlen(lib_name) - suffix_len;
5585     }
5586   }
5587   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
5588   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
5589   if (agent_entry_name == NULL) {
5590     return NULL;
5591   }
5592   if (lib_name != NULL) {
5593     const char *p = strrchr(sym_name, '@');
5594     if (p != NULL && p != sym_name) {
5595       // sym_name == _Agent_OnLoad@XX
5596       strncpy(agent_entry_name, sym_name, (p - sym_name));
5597       agent_entry_name[(p-sym_name)] = '\0';
5598       // agent_entry_name == _Agent_OnLoad
5599       strcat(agent_entry_name, "_");
5600       strncat(agent_entry_name, lib_name, name_len);
5601       strcat(agent_entry_name, p);
5602       // agent_entry_name == _Agent_OnLoad_lib_name@XX
5603     } else {
5604       strcpy(agent_entry_name, sym_name);
5605       strcat(agent_entry_name, "_");
5606       strncat(agent_entry_name, lib_name, name_len);
5607     }
5608   } else {
5609     strcpy(agent_entry_name, sym_name);
5610   }
5611   return agent_entry_name;
5612 }
5613 
5614 #else
5615 // Kernel32 API
5616 typedef BOOL (WINAPI* SwitchToThread_Fn)(void);
5617 typedef HANDLE (WINAPI* CreateToolhelp32Snapshot_Fn)(DWORD, DWORD);
5618 typedef BOOL (WINAPI* Module32First_Fn)(HANDLE, LPMODULEENTRY32);
5619 typedef BOOL (WINAPI* Module32Next_Fn)(HANDLE, LPMODULEENTRY32);
5620 typedef void (WINAPI* GetNativeSystemInfo_Fn)(LPSYSTEM_INFO);
5621 
5622 SwitchToThread_Fn           os::Kernel32Dll::_SwitchToThread = NULL;
5623 CreateToolhelp32Snapshot_Fn os::Kernel32Dll::_CreateToolhelp32Snapshot = NULL;
5624 Module32First_Fn            os::Kernel32Dll::_Module32First = NULL;
5625 Module32Next_Fn             os::Kernel32Dll::_Module32Next = NULL;
5626 GetNativeSystemInfo_Fn      os::Kernel32Dll::_GetNativeSystemInfo = NULL;
5627 
5628 void os::Kernel32Dll::initialize() {
5629   if (!initialized) {
5630     HMODULE handle = ::GetModuleHandle("Kernel32.dll");
5631     assert(handle != NULL, "Just check");
5632 
5633     _SwitchToThread = (SwitchToThread_Fn)::GetProcAddress(handle, "SwitchToThread");
5634     _CreateToolhelp32Snapshot = (CreateToolhelp32Snapshot_Fn)
5635       ::GetProcAddress(handle, "CreateToolhelp32Snapshot");
5636     _Module32First = (Module32First_Fn)::GetProcAddress(handle, "Module32First");
5637     _Module32Next = (Module32Next_Fn)::GetProcAddress(handle, "Module32Next");
5638     _GetNativeSystemInfo = (GetNativeSystemInfo_Fn)::GetProcAddress(handle, "GetNativeSystemInfo");
5639     initializeCommon();  // resolve the functions that always need resolving
5640 
5641     initialized = TRUE;
5642   }
5643 }
5644 
5645 BOOL os::Kernel32Dll::SwitchToThread() {
5646   assert(initialized && _SwitchToThread != NULL,
5647          "SwitchToThreadAvailable() not yet called");
5648   return _SwitchToThread();
5649 }
5650 
5651 
5652 BOOL os::Kernel32Dll::SwitchToThreadAvailable() {
5653   if (!initialized) {
5654     initialize();
5655   }
5656   return _SwitchToThread != NULL;
5657 }
5658 
5659 // Help tools
5660 BOOL os::Kernel32Dll::HelpToolsAvailable() {
5661   if (!initialized) {
5662     initialize();
5663   }
5664   return _CreateToolhelp32Snapshot != NULL &&
5665          _Module32First != NULL &&
5666          _Module32Next != NULL;
5667 }
5668 
5669 HANDLE os::Kernel32Dll::CreateToolhelp32Snapshot(DWORD dwFlags,
5670                                                  DWORD th32ProcessId) {
5671   assert(initialized && _CreateToolhelp32Snapshot != NULL,
5672          "HelpToolsAvailable() not yet called");
5673 
5674   return _CreateToolhelp32Snapshot(dwFlags, th32ProcessId);
5675 }
5676 
5677 BOOL os::Kernel32Dll::Module32First(HANDLE hSnapshot,LPMODULEENTRY32 lpme) {
5678   assert(initialized && _Module32First != NULL,
5679          "HelpToolsAvailable() not yet called");
5680 
5681   return _Module32First(hSnapshot, lpme);
5682 }
5683 
5684 inline BOOL os::Kernel32Dll::Module32Next(HANDLE hSnapshot,
5685                                           LPMODULEENTRY32 lpme) {
5686   assert(initialized && _Module32Next != NULL,
5687          "HelpToolsAvailable() not yet called");
5688 
5689   return _Module32Next(hSnapshot, lpme);
5690 }
5691 
5692 
5693 BOOL os::Kernel32Dll::GetNativeSystemInfoAvailable() {
5694   if (!initialized) {
5695     initialize();
5696   }
5697   return _GetNativeSystemInfo != NULL;
5698 }
5699 
5700 void os::Kernel32Dll::GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo) {
5701   assert(initialized && _GetNativeSystemInfo != NULL,
5702          "GetNativeSystemInfoAvailable() not yet called");
5703 
5704   _GetNativeSystemInfo(lpSystemInfo);
5705 }
5706 
5707 // PSAPI API
5708 
5709 
5710 typedef BOOL (WINAPI *EnumProcessModules_Fn)(HANDLE, HMODULE *, DWORD, LPDWORD);
5711 typedef BOOL (WINAPI *GetModuleFileNameEx_Fn)(HANDLE, HMODULE, LPTSTR, DWORD);
5712 typedef BOOL (WINAPI *GetModuleInformation_Fn)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
5713 
5714 EnumProcessModules_Fn   os::PSApiDll::_EnumProcessModules = NULL;
5715 GetModuleFileNameEx_Fn  os::PSApiDll::_GetModuleFileNameEx = NULL;
5716 GetModuleInformation_Fn os::PSApiDll::_GetModuleInformation = NULL;
5717 BOOL                    os::PSApiDll::initialized = FALSE;
5718 
5719 void os::PSApiDll::initialize() {
5720   if (!initialized) {
5721     HMODULE handle = os::win32::load_Windows_dll("PSAPI.DLL", NULL, 0);
5722     if (handle != NULL) {
5723       _EnumProcessModules = (EnumProcessModules_Fn)::GetProcAddress(handle,
5724                                                                     "EnumProcessModules");
5725       _GetModuleFileNameEx = (GetModuleFileNameEx_Fn)::GetProcAddress(handle,
5726                                                                       "GetModuleFileNameExA");
5727       _GetModuleInformation = (GetModuleInformation_Fn)::GetProcAddress(handle,
5728                                                                         "GetModuleInformation");
5729     }
5730     initialized = TRUE;
5731   }
5732 }
5733 
5734 
5735 
5736 BOOL os::PSApiDll::EnumProcessModules(HANDLE hProcess, HMODULE *lpModule,
5737                                       DWORD cb, LPDWORD lpcbNeeded) {
5738   assert(initialized && _EnumProcessModules != NULL,
5739          "PSApiAvailable() not yet called");
5740   return _EnumProcessModules(hProcess, lpModule, cb, lpcbNeeded);
5741 }
5742 
5743 DWORD os::PSApiDll::GetModuleFileNameEx(HANDLE hProcess, HMODULE hModule,
5744                                         LPTSTR lpFilename, DWORD nSize) {
5745   assert(initialized && _GetModuleFileNameEx != NULL,
5746          "PSApiAvailable() not yet called");
5747   return _GetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
5748 }
5749 
5750 BOOL os::PSApiDll::GetModuleInformation(HANDLE hProcess, HMODULE hModule,
5751                                         LPMODULEINFO lpmodinfo, DWORD cb) {
5752   assert(initialized && _GetModuleInformation != NULL,
5753          "PSApiAvailable() not yet called");
5754   return _GetModuleInformation(hProcess, hModule, lpmodinfo, cb);
5755 }
5756 
5757 BOOL os::PSApiDll::PSApiAvailable() {
5758   if (!initialized) {
5759     initialize();
5760   }
5761   return _EnumProcessModules != NULL &&
5762     _GetModuleFileNameEx != NULL &&
5763     _GetModuleInformation != NULL;
5764 }
5765 
5766 
5767 // WinSock2 API
5768 typedef int (PASCAL FAR* WSAStartup_Fn)(WORD, LPWSADATA);
5769 typedef struct hostent *(PASCAL FAR *gethostbyname_Fn)(...);
5770 
5771 WSAStartup_Fn    os::WinSock2Dll::_WSAStartup = NULL;
5772 gethostbyname_Fn os::WinSock2Dll::_gethostbyname = NULL;
5773 BOOL             os::WinSock2Dll::initialized = FALSE;
5774 
5775 void os::WinSock2Dll::initialize() {
5776   if (!initialized) {
5777     HMODULE handle = os::win32::load_Windows_dll("ws2_32.dll", NULL, 0);
5778     if (handle != NULL) {
5779       _WSAStartup = (WSAStartup_Fn)::GetProcAddress(handle, "WSAStartup");
5780       _gethostbyname = (gethostbyname_Fn)::GetProcAddress(handle, "gethostbyname");
5781     }
5782     initialized = TRUE;
5783   }
5784 }
5785 
5786 
5787 BOOL os::WinSock2Dll::WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData) {
5788   assert(initialized && _WSAStartup != NULL,
5789          "WinSock2Available() not yet called");
5790   return _WSAStartup(wVersionRequested, lpWSAData);
5791 }
5792 
5793 struct hostent* os::WinSock2Dll::gethostbyname(const char *name) {
5794   assert(initialized && _gethostbyname != NULL,
5795          "WinSock2Available() not yet called");
5796   return _gethostbyname(name);
5797 }
5798 
5799 BOOL os::WinSock2Dll::WinSock2Available() {
5800   if (!initialized) {
5801     initialize();
5802   }
5803   return _WSAStartup != NULL &&
5804     _gethostbyname != NULL;
5805 }
5806 
5807 typedef BOOL (WINAPI *AdjustTokenPrivileges_Fn)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
5808 typedef BOOL (WINAPI *OpenProcessToken_Fn)(HANDLE, DWORD, PHANDLE);
5809 typedef BOOL (WINAPI *LookupPrivilegeValue_Fn)(LPCTSTR, LPCTSTR, PLUID);
5810 
5811 AdjustTokenPrivileges_Fn os::Advapi32Dll::_AdjustTokenPrivileges = NULL;
5812 OpenProcessToken_Fn      os::Advapi32Dll::_OpenProcessToken = NULL;
5813 LookupPrivilegeValue_Fn  os::Advapi32Dll::_LookupPrivilegeValue = NULL;
5814 BOOL                     os::Advapi32Dll::initialized = FALSE;
5815 
5816 void os::Advapi32Dll::initialize() {
5817   if (!initialized) {
5818     HMODULE handle = os::win32::load_Windows_dll("advapi32.dll", NULL, 0);
5819     if (handle != NULL) {
5820       _AdjustTokenPrivileges = (AdjustTokenPrivileges_Fn)::GetProcAddress(handle,
5821                                                                           "AdjustTokenPrivileges");
5822       _OpenProcessToken = (OpenProcessToken_Fn)::GetProcAddress(handle,
5823                                                                 "OpenProcessToken");
5824       _LookupPrivilegeValue = (LookupPrivilegeValue_Fn)::GetProcAddress(handle,
5825                                                                         "LookupPrivilegeValueA");
5826     }
5827     initialized = TRUE;
5828   }
5829 }
5830 
5831 BOOL os::Advapi32Dll::AdjustTokenPrivileges(HANDLE TokenHandle,
5832                                             BOOL DisableAllPrivileges,
5833                                             PTOKEN_PRIVILEGES NewState,
5834                                             DWORD BufferLength,
5835                                             PTOKEN_PRIVILEGES PreviousState,
5836                                             PDWORD ReturnLength) {
5837   assert(initialized && _AdjustTokenPrivileges != NULL,
5838          "AdvapiAvailable() not yet called");
5839   return _AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges, NewState,
5840                                 BufferLength, PreviousState, ReturnLength);
5841 }
5842 
5843 BOOL os::Advapi32Dll::OpenProcessToken(HANDLE ProcessHandle,
5844                                        DWORD DesiredAccess,
5845                                        PHANDLE TokenHandle) {
5846   assert(initialized && _OpenProcessToken != NULL,
5847          "AdvapiAvailable() not yet called");
5848   return _OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle);
5849 }
5850 
5851 BOOL os::Advapi32Dll::LookupPrivilegeValue(LPCTSTR lpSystemName,
5852                                            LPCTSTR lpName, PLUID lpLuid) {
5853   assert(initialized && _LookupPrivilegeValue != NULL,
5854          "AdvapiAvailable() not yet called");
5855   return _LookupPrivilegeValue(lpSystemName, lpName, lpLuid);
5856 }
5857 
5858 BOOL os::Advapi32Dll::AdvapiAvailable() {
5859   if (!initialized) {
5860     initialize();
5861   }
5862   return _AdjustTokenPrivileges != NULL &&
5863     _OpenProcessToken != NULL &&
5864     _LookupPrivilegeValue != NULL;
5865 }
5866 
5867 #endif
5868 
5869 #ifndef PRODUCT
5870 
5871 // test the code path in reserve_memory_special() that tries to allocate memory in a single
5872 // contiguous memory block at a particular address.
5873 // The test first tries to find a good approximate address to allocate at by using the same
5874 // method to allocate some memory at any address. The test then tries to allocate memory in
5875 // the vicinity (not directly after it to avoid possible by-chance use of that location)
5876 // This is of course only some dodgy assumption, there is no guarantee that the vicinity of
5877 // the previously allocated memory is available for allocation. The only actual failure
5878 // that is reported is when the test tries to allocate at a particular location but gets a
5879 // different valid one. A NULL return value at this point is not considered an error but may
5880 // be legitimate.
5881 // If -XX:+VerboseInternalVMTests is enabled, print some explanatory messages.
5882 void TestReserveMemorySpecial_test() {
5883   if (!UseLargePages) {
5884     if (VerboseInternalVMTests) {
5885       gclog_or_tty->print("Skipping test because large pages are disabled");
5886     }
5887     return;
5888   }
5889   // save current value of globals
5890   bool old_use_large_pages_individual_allocation = UseLargePagesIndividualAllocation;
5891   bool old_use_numa_interleaving = UseNUMAInterleaving;
5892 
5893   // set globals to make sure we hit the correct code path
5894   UseLargePagesIndividualAllocation = UseNUMAInterleaving = false;
5895 
5896   // do an allocation at an address selected by the OS to get a good one.
5897   const size_t large_allocation_size = os::large_page_size() * 4;
5898   char* result = os::reserve_memory_special(large_allocation_size, os::large_page_size(), NULL, false);
5899   if (result == NULL) {
5900     if (VerboseInternalVMTests) {
5901       gclog_or_tty->print("Failed to allocate control block with size "SIZE_FORMAT". Skipping remainder of test.",
5902                           large_allocation_size);
5903     }
5904   } else {
5905     os::release_memory_special(result, large_allocation_size);
5906 
5907     // allocate another page within the recently allocated memory area which seems to be a good location. At least
5908     // we managed to get it once.
5909     const size_t expected_allocation_size = os::large_page_size();
5910     char* expected_location = result + os::large_page_size();
5911     char* actual_location = os::reserve_memory_special(expected_allocation_size, os::large_page_size(), expected_location, false);
5912     if (actual_location == NULL) {
5913       if (VerboseInternalVMTests) {
5914         gclog_or_tty->print("Failed to allocate any memory at "PTR_FORMAT" size "SIZE_FORMAT". Skipping remainder of test.",
5915                             expected_location, large_allocation_size);
5916       }
5917     } else {
5918       // release memory
5919       os::release_memory_special(actual_location, expected_allocation_size);
5920       // only now check, after releasing any memory to avoid any leaks.
5921       assert(actual_location == expected_location,
5922              err_msg("Failed to allocate memory at requested location "PTR_FORMAT" of size "SIZE_FORMAT", is "PTR_FORMAT" instead",
5923              expected_location, expected_allocation_size, actual_location));
5924     }
5925   }
5926 
5927   // restore globals
5928   UseLargePagesIndividualAllocation = old_use_large_pages_individual_allocation;
5929   UseNUMAInterleaving = old_use_numa_interleaving;
5930 }
5931 #endif // PRODUCT